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
d50ce62e2ff21557b3ed9297f551b90d8a16792c
7cef822f3b952965621309e88eadf618da0c8ae9
/src/data/set/lattice.lean
35ad931375892232d884b18c4f8efa7800b0d2b7
[ "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
34,586
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, Johannes Hölzl, Mario Carneiro -- QUESTION: can make the first argument in ∀ x ∈ a, ... implicit? -/ import logic.basic data.set.basic data.equiv.basic import order.complete_boolean_algebra category.basic import tactic.finish data.sigma.basic order.galois_connection open function tactic set lattice auto universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} namespace set instance lattice_set : complete_lattice (set α) := { le := (⊆), lt := λ x y, x ⊆ y ∧ ¬ y ⊆ x, sup := (∪), inf := (∩), top := univ, bot := ∅, Sup := λs, {a | ∃ t ∈ s, a ∈ t }, Inf := λs, {a | ∀ t ∈ s, a ∈ t }, le_Sup := assume s t t_in a a_in, ⟨t, ⟨t_in, a_in⟩⟩, Sup_le := assume s t h a ⟨t', ⟨t'_in, a_in⟩⟩, h t' t'_in a_in, le_Inf := assume s t h a a_in t' t'_in, h t' t'_in a_in, Inf_le := assume s t t_in a h, h _ t_in, .. (infer_instance : complete_lattice (α → Prop)) } instance : distrib_lattice (set α) := { le_sup_inf := λ s t u x, or_and_distrib_left.2, ..set.lattice_set } lemma monotone_image {f : α → β} : monotone (image f) := assume s t, assume h : s ⊆ t, image_subset _ h theorem monotone_inter [preorder β] {f g : β → set α} (hf : monotone f) (hg : monotone g) : monotone (λx, f x ∩ g x) := assume b₁ b₂ h, inter_subset_inter (hf h) (hg h) theorem monotone_union [preorder β] {f g : β → set α} (hf : monotone f) (hg : monotone g) : monotone (λx, f x ∪ g x) := assume b₁ b₂ h, union_subset_union (hf h) (hg h) theorem monotone_set_of [preorder α] {p : α → β → Prop} (hp : ∀b, monotone (λa, p a b)) : monotone (λa, {b | p a b}) := assume a a' h b, hp b h section galois_connection variables {f : α → β} protected lemma image_preimage : galois_connection (image f) (preimage f) := assume a b, image_subset_iff def kern_image (f : α → β) (s : set α) : set β := {y | ∀x, f x = y → x ∈ s} protected lemma preimage_kern_image : galois_connection (preimage f) (kern_image f) := assume a b, ⟨ assume h x hx y hy, have f y ∈ a, from hy.symm ▸ hx, h this, assume h x (hx : f x ∈ a), h hx x rfl⟩ end galois_connection /- union and intersection over a family of sets indexed by a type -/ /-- Indexed union of a family of sets -/ @[reducible] def Union (s : ι → set β) : set β := supr s /-- Indexed intersection of a family of sets -/ @[reducible] def Inter (s : ι → set β) : set β := infi s notation `⋃` binders `, ` r:(scoped f, Union f) := r notation `⋂` binders `, ` r:(scoped f, Inter f) := r @[simp] theorem mem_Union {x : β} {s : ι → set β} : x ∈ Union s ↔ ∃ i, x ∈ s i := ⟨assume ⟨t, ⟨⟨a, (t_eq : s a = t)⟩, (h : x ∈ t)⟩⟩, ⟨a, t_eq.symm ▸ h⟩, assume ⟨a, h⟩, ⟨s a, ⟨⟨a, rfl⟩, h⟩⟩⟩ /- alternative proof: dsimp [Union, supr, Sup]; simp -/ -- TODO: more rewrite rules wrt forall / existentials and logical connectives -- TODO: also eliminate ∃i, ... ∧ i = t ∧ ... @[simp] theorem mem_Inter {x : β} {s : ι → set β} : x ∈ Inter s ↔ ∀ i, x ∈ s i := ⟨assume (h : ∀a ∈ {a : set β | ∃i, s i = a}, x ∈ a) a, h (s a) ⟨a, rfl⟩, assume h t ⟨a, (eq : s a = t)⟩, eq ▸ h a⟩ theorem Union_subset {s : ι → set β} {t : set β} (h : ∀ i, s i ⊆ t) : (⋃ i, s i) ⊆ t := -- TODO: should be simpler when sets' order is based on lattices @supr_le (set β) _ set.lattice_set _ _ h theorem Union_subset_iff {α : Sort u} {s : α → set β} {t : set β} : (⋃ i, s i) ⊆ t ↔ (∀ i, s i ⊆ t) := ⟨assume h i, subset.trans (le_supr s _) h, Union_subset⟩ theorem mem_Inter_of_mem {α : Sort u} {x : β} {s : α → set β} : (∀ i, x ∈ s i) → (x ∈ ⋂ i, s i) := mem_Inter.2 theorem subset_Inter {t : set β} {s : α → set β} (h : ∀ i, t ⊆ s i) : t ⊆ ⋂ i, s i := -- TODO: should be simpler when sets' order is based on lattices @le_infi (set β) _ set.lattice_set _ _ h theorem subset_Union : ∀ (s : ι → set β) (i : ι), s i ⊆ (⋃ i, s i) := le_supr theorem Inter_subset : ∀ (s : ι → set β) (i : ι), (⋂ i, s i) ⊆ s i := infi_le theorem Union_const [inhabited ι] (s : set β) : (⋃ i:ι, s) = s := ext $ by simp theorem Inter_const [inhabited ι] (s : set β) : (⋂ i:ι, s) = s := ext $ by simp @[simp] -- complete_boolean_algebra theorem compl_Union (s : ι → set β) : - (⋃ i, s i) = (⋂ i, - s i) := ext (by simp) -- classical -- complete_boolean_algebra theorem compl_Inter (s : ι → set β) : -(⋂ i, s i) = (⋃ i, - s i) := ext (λ x, by simp [classical.not_forall]) -- classical -- complete_boolean_algebra theorem Union_eq_comp_Inter_comp (s : ι → set β) : (⋃ i, s i) = - (⋂ i, - s i) := by simp [compl_Inter, compl_compl] -- classical -- complete_boolean_algebra theorem Inter_eq_comp_Union_comp (s : ι → set β) : (⋂ i, s i) = - (⋃ i, -s i) := by simp [compl_compl] theorem inter_Union (s : set β) (t : ι → set β) : s ∩ (⋃ i, t i) = ⋃ i, s ∩ t i := ext $ by simp theorem Union_inter (s : set β) (t : ι → set β) : (⋃ i, t i) ∩ s = ⋃ i, t i ∩ s := ext $ by simp theorem Union_union_distrib (s : ι → set β) (t : ι → set β) : (⋃ i, s i ∪ t i) = (⋃ i, s i) ∪ (⋃ i, t i) := ext $ by simp [exists_or_distrib] theorem Inter_inter_distrib (s : ι → set β) (t : ι → set β) : (⋂ i, s i ∩ t i) = (⋂ i, s i) ∩ (⋂ i, t i) := ext $ by simp [forall_and_distrib] theorem union_Union [inhabited ι] (s : set β) (t : ι → set β) : s ∪ (⋃ i, t i) = ⋃ i, s ∪ t i := by rw [Union_union_distrib, Union_const] theorem Union_union [inhabited ι] (s : set β) (t : ι → set β) : (⋃ i, t i) ∪ s = ⋃ i, t i ∪ s := by rw [Union_union_distrib, Union_const] theorem inter_Inter [inhabited ι] (s : set β) (t : ι → set β) : s ∩ (⋂ i, t i) = ⋂ i, s ∩ t i := by rw [Inter_inter_distrib, Inter_const] theorem Inter_inter [inhabited ι] (s : set β) (t : ι → set β) : (⋂ i, t i) ∩ s = ⋂ i, t i ∩ s := by rw [Inter_inter_distrib, Inter_const] -- classical theorem union_Inter (s : set β) (t : ι → set β) : s ∪ (⋂ i, t i) = ⋂ i, s ∪ t i := ext $ assume x, by simp [classical.forall_or_distrib_left] theorem Union_diff (s : set β) (t : ι → set β) : (⋃ i, t i) \ s = ⋃ i, t i \ s := Union_inter _ _ theorem diff_Union [inhabited ι] (s : set β) (t : ι → set β) : s \ (⋃ i, t i) = ⋂ i, s \ t i := by rw [diff_eq, compl_Union, inter_Inter]; refl theorem diff_Inter (s : set β) (t : ι → set β) : s \ (⋂ i, t i) = ⋃ i, s \ t i := by rw [diff_eq, compl_Inter, inter_Union]; refl /- bounded unions and intersections -/ theorem mem_bUnion_iff {s : set α} {t : α → set β} {y : β} : y ∈ (⋃ x ∈ s, t x) ↔ ∃ x, x ∈ s ∧ y ∈ t x := by simp theorem mem_bInter_iff {s : set α} {t : α → set β} {y : β} : y ∈ (⋂ x ∈ s, t x) ↔ ∀ x ∈ s, y ∈ t x := by simp theorem mem_bUnion {s : set α} {t : α → set β} {x : α} {y : β} (xs : x ∈ s) (ytx : y ∈ t x) : y ∈ ⋃ x ∈ s, t x := by simp; exact ⟨x, ⟨xs, ytx⟩⟩ theorem mem_bInter {s : set α} {t : α → set β} {y : β} (h : ∀ x ∈ s, y ∈ t x) : y ∈ ⋂ x ∈ s, t x := by simp; assumption theorem bUnion_subset {s : set α} {t : set β} {u : α → set β} (h : ∀ x ∈ s, u x ⊆ t) : (⋃ x ∈ s, u x) ⊆ t := show (⨆ x ∈ s, u x) ≤ t, -- TODO: should not be necessary when sets' order is based on lattices from supr_le $ assume x, supr_le (h x) theorem subset_bInter {s : set α} {t : set β} {u : α → set β} (h : ∀ x ∈ s, t ⊆ u x) : t ⊆ (⋂ x ∈ s, u x) := show t ≤ (⨅ x ∈ s, u x), -- TODO: should not be necessary when sets' order is based on lattices from le_infi $ assume x, le_infi (h x) theorem subset_bUnion_of_mem {s : set α} {u : α → set β} {x : α} (xs : x ∈ s) : u x ⊆ (⋃ x ∈ s, u x) := show u x ≤ (⨆ x ∈ s, u x), from le_supr_of_le x $ le_supr _ xs theorem bInter_subset_of_mem {s : set α} {t : α → set β} {x : α} (xs : x ∈ s) : (⋂ x ∈ s, t x) ⊆ t x := show (⨅x ∈ s, t x) ≤ t x, from infi_le_of_le x $ infi_le _ xs theorem bUnion_subset_bUnion_left {s s' : set α} {t : α → set β} (h : s ⊆ s') : (⋃ x ∈ s, t x) ⊆ (⋃ x ∈ s', t x) := bUnion_subset (λ x xs, subset_bUnion_of_mem (h xs)) theorem bInter_subset_bInter_left {s s' : set α} {t : α → set β} (h : s' ⊆ s) : (⋂ x ∈ s, t x) ⊆ (⋂ x ∈ s', t x) := subset_bInter (λ x xs, bInter_subset_of_mem (h xs)) theorem bUnion_subset_bUnion_right {s : set α} {t1 t2 : α → set β} (h : ∀ x ∈ s, t1 x ⊆ t2 x) : (⋃ x ∈ s, t1 x) ⊆ (⋃ x ∈ s, t2 x) := bUnion_subset (λ x xs, subset.trans (h x xs) (subset_bUnion_of_mem xs)) theorem bInter_subset_bInter_right {s : set α} {t1 t2 : α → set β} (h : ∀ x ∈ s, t1 x ⊆ t2 x) : (⋂ x ∈ s, t1 x) ⊆ (⋂ x ∈ s, t2 x) := subset_bInter (λ x xs, subset.trans (bInter_subset_of_mem xs) (h x xs)) theorem bUnion_eq_Union (s : set α) (t : α → set β) : (⋃ x ∈ s, t x) = (⋃ x : s, t x.1) := set.ext $ by simp theorem bInter_eq_Inter (s : set α) (t : α → set β) : (⋂ x ∈ s, t x) = (⋂ x : s, t x.1) := set.ext $ by simp @[simp] theorem bInter_empty (u : α → set β) : (⋂ x ∈ (∅ : set α), u x) = univ := show (⨅x ∈ (∅ : set α), u x) = ⊤, -- simplifier should be able to rewrite x ∈ ∅ to false. from infi_emptyset @[simp] theorem bInter_univ (u : α → set β) : (⋂ x ∈ @univ α, u x) = ⋂ x, u x := infi_univ -- TODO(Jeremy): here is an artifact of the the encoding of bounded intersection: -- without dsimp, the next theorem fails to type check, because there is a lambda -- in a type that needs to be contracted. Using simp [eq_of_mem_singleton xa] also works. @[simp] theorem bInter_singleton (a : α) (s : α → set β) : (⋂ x ∈ ({a} : set α), s x) = s a := show (⨅ x ∈ ({a} : set α), s x) = s a, by simp theorem bInter_union (s t : set α) (u : α → set β) : (⋂ x ∈ s ∪ t, u x) = (⋂ x ∈ s, u x) ∩ (⋂ x ∈ t, u x) := show (⨅ x ∈ s ∪ t, u x) = (⨅ x ∈ s, u x) ⊓ (⨅ x ∈ t, u x), from infi_union -- TODO(Jeremy): simp [insert_eq, bInter_union] doesn't work @[simp] theorem bInter_insert (a : α) (s : set α) (t : α → set β) : (⋂ x ∈ insert a s, t x) = t a ∩ (⋂ x ∈ s, t x) := begin rw insert_eq, simp [bInter_union] end -- TODO(Jeremy): another example of where an annotation is needed theorem bInter_pair (a b : α) (s : α → set β) : (⋂ x ∈ ({a, b} : set α), s x) = s a ∩ s b := by rw insert_of_has_insert; simp [inter_comm] @[simp] theorem bUnion_empty (s : α → set β) : (⋃ x ∈ (∅ : set α), s x) = ∅ := supr_emptyset @[simp] theorem bUnion_univ (s : α → set β) : (⋃ x ∈ @univ α, s x) = ⋃ x, s x := supr_univ @[simp] theorem bUnion_singleton (a : α) (s : α → set β) : (⋃ x ∈ ({a} : set α), s x) = s a := supr_singleton @[simp] theorem bUnion_of_singleton (s : set α) : (⋃ x ∈ s, {x}) = s := ext $ by simp theorem bUnion_union (s t : set α) (u : α → set β) : (⋃ x ∈ s ∪ t, u x) = (⋃ x ∈ s, u x) ∪ (⋃ x ∈ t, u x) := supr_union -- TODO(Jeremy): once again, simp doesn't do it alone. @[simp] theorem bUnion_insert (a : α) (s : set α) (t : α → set β) : (⋃ x ∈ insert a s, t x) = t a ∪ (⋃ x ∈ s, t x) := begin rw [insert_eq], simp [bUnion_union] end theorem bUnion_pair (a b : α) (s : α → set β) : (⋃ x ∈ ({a, b} : set α), s x) = s a ∪ s b := by rw insert_of_has_insert; simp [union_comm] @[simp] -- complete_boolean_algebra theorem compl_bUnion (s : set α) (t : α → set β) : - (⋃ i ∈ s, t i) = (⋂ i ∈ s, - t i) := ext (λ x, by simp) -- classical -- complete_boolean_algebra theorem compl_bInter (s : set α) (t : α → set β) : -(⋂ i ∈ s, t i) = (⋃ i ∈ s, - t i) := ext (λ x, by simp [classical.not_forall]) theorem inter_bUnion (s : set α) (t : α → set β) (u : set β) : u ∩ (⋃ i ∈ s, t i) = ⋃ i ∈ s, u ∩ t i := begin ext x, simp only [exists_prop, mem_Union, mem_inter_eq], exact ⟨λ ⟨hx, ⟨i, is, xi⟩⟩, ⟨i, is, hx, xi⟩, λ ⟨i, is, hx, xi⟩, ⟨hx, ⟨i, is, xi⟩⟩⟩ end theorem bUnion_inter (s : set α) (t : α → set β) (u : set β) : (⋃ i ∈ s, t i) ∩ u = (⋃ i ∈ s, t i ∩ u) := by simp [@inter_comm _ _ u, inter_bUnion] /-- Intersection of a set of sets. -/ @[reducible] def sInter (S : set (set α)) : set α := Inf S prefix `⋂₀`:110 := sInter theorem mem_sUnion_of_mem {x : α} {t : set α} {S : set (set α)} (hx : x ∈ t) (ht : t ∈ S) : x ∈ ⋃₀ S := ⟨t, ⟨ht, hx⟩⟩ @[simp] theorem mem_sUnion {x : α} {S : set (set α)} : x ∈ ⋃₀ S ↔ ∃t ∈ S, x ∈ t := iff.rfl -- is this theorem really necessary? theorem not_mem_of_not_mem_sUnion {x : α} {t : set α} {S : set (set α)} (hx : x ∉ ⋃₀ S) (ht : t ∈ S) : x ∉ t := λ h, hx ⟨t, ht, h⟩ @[simp] theorem mem_sInter {x : α} {S : set (set α)} : x ∈ ⋂₀ S ↔ ∀ t ∈ S, x ∈ t := iff.rfl theorem sInter_subset_of_mem {S : set (set α)} {t : set α} (tS : t ∈ S) : ⋂₀ S ⊆ t := Inf_le tS theorem subset_sUnion_of_mem {S : set (set α)} {t : set α} (tS : t ∈ S) : t ⊆ ⋃₀ S := le_Sup tS lemma subset_sUnion_of_subset {s : set α} (t : set (set α)) (u : set α) (h₁ : s ⊆ u) (h₂ : u ∈ t) : s ⊆ ⋃₀ t := subset.trans h₁ (subset_sUnion_of_mem h₂) theorem sUnion_subset {S : set (set α)} {t : set α} (h : ∀t' ∈ S, t' ⊆ t) : (⋃₀ S) ⊆ t := Sup_le h theorem sUnion_subset_iff {s : set (set α)} {t : set α} : ⋃₀ s ⊆ t ↔ ∀t' ∈ s, t' ⊆ t := ⟨assume h t' ht', subset.trans (subset_sUnion_of_mem ht') h, sUnion_subset⟩ theorem subset_sInter {S : set (set α)} {t : set α} (h : ∀t' ∈ S, t ⊆ t') : t ⊆ (⋂₀ S) := le_Inf h theorem sUnion_subset_sUnion {S T : set (set α)} (h : S ⊆ T) : ⋃₀ S ⊆ ⋃₀ T := sUnion_subset $ λ s hs, subset_sUnion_of_mem (h hs) theorem sInter_subset_sInter {S T : set (set α)} (h : S ⊆ T) : ⋂₀ T ⊆ ⋂₀ S := subset_sInter $ λ s hs, sInter_subset_of_mem (h hs) @[simp] theorem sUnion_empty : ⋃₀ ∅ = (∅ : set α) := Sup_empty @[simp] theorem sInter_empty : ⋂₀ ∅ = (univ : set α) := Inf_empty @[simp] theorem sUnion_singleton (s : set α) : ⋃₀ {s} = s := Sup_singleton @[simp] theorem sInter_singleton (s : set α) : ⋂₀ {s} = s := Inf_singleton theorem sUnion_union (S T : set (set α)) : ⋃₀ (S ∪ T) = ⋃₀ S ∪ ⋃₀ T := Sup_union theorem sInter_union (S T : set (set α)) : ⋂₀ (S ∪ T) = ⋂₀ S ∩ ⋂₀ T := Inf_union @[simp] theorem sUnion_insert (s : set α) (T : set (set α)) : ⋃₀ (insert s T) = s ∪ ⋃₀ T := Sup_insert @[simp] theorem sInter_insert (s : set α) (T : set (set α)) : ⋂₀ (insert s T) = s ∩ ⋂₀ T := Inf_insert theorem sUnion_pair (s t : set α) : ⋃₀ {s, t} = s ∪ t := (sUnion_insert _ _).trans $ by rw [union_comm, sUnion_singleton] theorem sInter_pair (s t : set α) : ⋂₀ {s, t} = s ∩ t := (sInter_insert _ _).trans $ by rw [inter_comm, sInter_singleton] @[simp] theorem sUnion_image (f : α → set β) (s : set α) : ⋃₀ (f '' s) = ⋃ x ∈ s, f x := Sup_image @[simp] theorem sInter_image (f : α → set β) (s : set α) : ⋂₀ (f '' s) = ⋂ x ∈ s, f x := Inf_image @[simp] theorem sUnion_range (f : ι → set β) : ⋃₀ (range f) = ⋃ x, f x := Sup_range @[simp] theorem sInter_range (f : ι → set β) : ⋂₀ (range f) = ⋂ x, f x := Inf_range lemma sUnion_eq_univ_iff {c : set (set α)} : ⋃₀ c = @set.univ α ↔ ∀ a, ∃ b ∈ c, a ∈ b := ⟨λ H a, let ⟨b, hm, hb⟩ := mem_sUnion.1 $ by rw H; exact mem_univ a in ⟨b, hm, hb⟩, λ H, set.univ_subset_iff.1 $ λ x hx, let ⟨b, hm, hb⟩ := H x in set.mem_sUnion_of_mem hb hm⟩ theorem compl_sUnion (S : set (set α)) : - ⋃₀ S = ⋂₀ (compl '' S) := set.ext $ assume x, ⟨assume : ¬ (∃s∈S, x ∈ s), assume s h, match s, h with ._, ⟨t, hs, rfl⟩ := assume h, this ⟨t, hs, h⟩ end, assume : ∀s, s ∈ compl '' S → x ∈ s, assume ⟨t, tS, xt⟩, this (compl t) (mem_image_of_mem _ tS) xt⟩ -- classical theorem sUnion_eq_compl_sInter_compl (S : set (set α)) : ⋃₀ S = - ⋂₀ (compl '' S) := by rw [←compl_compl (⋃₀ S), compl_sUnion] -- classical theorem compl_sInter (S : set (set α)) : - ⋂₀ S = ⋃₀ (compl '' S) := by rw [sUnion_eq_compl_sInter_compl, compl_compl_image] -- classical theorem sInter_eq_comp_sUnion_compl (S : set (set α)) : ⋂₀ S = -(⋃₀ (compl '' S)) := by rw [←compl_compl (⋂₀ S), compl_sInter] theorem inter_empty_of_inter_sUnion_empty {s t : set α} {S : set (set α)} (hs : t ∈ S) (h : s ∩ ⋃₀ S = ∅) : s ∩ t = ∅ := eq_empty_of_subset_empty $ by rw ← h; exact inter_subset_inter_right _ (subset_sUnion_of_mem hs) theorem range_sigma_eq_Union_range {γ : α → Type*} (f : sigma γ → β) : range f = ⋃ a, range (λ b, f ⟨a, b⟩) := set.ext $ by simp theorem Union_eq_range_sigma (s : α → set β) : (⋃ i, s i) = range (λ a : Σ i, s i, a.2) := by simp [set.ext_iff] theorem Union_image_preimage_sigma_mk_eq_self {ι : Type*} {σ : ι → Type*} (s : set (sigma σ)) : (⋃ i, sigma.mk i '' (sigma.mk i ⁻¹' s)) = s := begin ext x, simp only [mem_Union, mem_image, mem_preimage], split, { rintros ⟨i, a, h, rfl⟩, exact h }, { intro h, cases x with i a, exact ⟨i, a, h, rfl⟩ } end lemma sUnion_mono {s t : set (set α)} (h : s ⊆ t) : (⋃₀ s) ⊆ (⋃₀ t) := sUnion_subset $ assume t' ht', subset_sUnion_of_mem $ h ht' lemma Union_subset_Union {s t : ι → set α} (h : ∀i, s i ⊆ t i) : (⋃i, s i) ⊆ (⋃i, t i) := @supr_le_supr (set α) ι _ s t h lemma Union_subset_Union2 {ι₂ : Sort*} {s : ι → set α} {t : ι₂ → set α} (h : ∀i, ∃j, s i ⊆ t j) : (⋃i, s i) ⊆ (⋃i, t i) := @supr_le_supr2 (set α) ι ι₂ _ s t h lemma Union_subset_Union_const {ι₂ : Sort x} {s : set α} (h : ι → ι₂) : (⋃ i:ι, s) ⊆ (⋃ j:ι₂, s) := @supr_le_supr_const (set α) ι ι₂ _ s h @[simp] lemma Union_of_singleton (α : Type u) : (⋃(x : α), {x}) = @set.univ α := ext $ λ x, ⟨λ h, ⟨⟩, λ h, ⟨{x}, ⟨⟨x, rfl⟩, mem_singleton x⟩⟩⟩ theorem bUnion_subset_Union (s : set α) (t : α → set β) : (⋃ x ∈ s, t x) ⊆ (⋃ x, t x) := Union_subset_Union $ λ i, Union_subset $ λ h, by refl lemma sUnion_eq_bUnion {s : set (set α)} : (⋃₀ s) = (⋃ (i : set α) (h : i ∈ s), i) := set.ext $ by simp lemma sInter_eq_bInter {s : set (set α)} : (⋂₀ s) = (⋂ (i : set α) (h : i ∈ s), i) := set.ext $ by simp lemma sUnion_eq_Union {s : set (set α)} : (⋃₀ s) = (⋃ (i : s), i.1) := set.ext $ λ x, by simp lemma sInter_eq_Inter {s : set (set α)} : (⋂₀ s) = (⋂ (i : s), i.1) := set.ext $ λ x, by simp lemma union_eq_Union {s₁ s₂ : set α} : s₁ ∪ s₂ = ⋃ b : bool, cond b s₁ s₂ := set.ext $ λ x, by simp [bool.exists_bool, or_comm] lemma inter_eq_Inter {s₁ s₂ : set α} : s₁ ∩ s₂ = ⋂ b : bool, cond b s₁ s₂ := set.ext $ λ x, by simp [bool.forall_bool, and_comm] instance : complete_boolean_algebra (set α) := { neg := compl, sub := (\), inf_neg_eq_bot := assume s, ext $ assume x, ⟨assume ⟨h, nh⟩, nh h, false.elim⟩, sup_neg_eq_top := assume s, ext $ assume x, ⟨assume h, trivial, assume _, classical.em $ x ∈ s⟩, le_sup_inf := distrib_lattice.le_sup_inf, sub_eq := assume x y, rfl, infi_sup_le_sup_Inf := assume s t x, show x ∈ (⋂ b ∈ t, s ∪ b) → x ∈ s ∪ (⋂₀ t), by simp; exact assume h, or.imp_right (assume hn : x ∉ s, assume i hi, or.resolve_left (h i hi) hn) (classical.em $ x ∈ s), inf_Sup_le_supr_inf := assume s t x, show x ∈ s ∩ (⋃₀ t) → x ∈ (⋃ b ∈ t, s ∩ b), by simp [-and_imp, and.left_comm], ..set.lattice_set } lemma sInter_union_sInter {S T : set (set α)} : (⋂₀S) ∪ (⋂₀T) = (⋂p ∈ set.prod S T, (p : (set α) × (set α)).1 ∪ p.2) := Inf_sup_Inf lemma sUnion_inter_sUnion {s t : set (set α)} : (⋃₀s) ∩ (⋃₀t) = (⋃p ∈ set.prod s t, (p : (set α) × (set α )).1 ∩ p.2) := Sup_inf_Sup lemma sInter_bUnion {S : set (set α)} {T : set α → set (set α)} (hT : ∀s∈S, s = ⋂₀ T s) : ⋂₀ (⋃s∈S, T s) = ⋂₀ S := begin ext, simp only [and_imp, exists_prop, set.mem_sInter, set.mem_Union, exists_imp_distrib], split, { assume H s sS, rw [hT s sS, mem_sInter], assume t tTs, apply H t s sS tTs }, { assume H t s sS tTs, have xs : x ∈ s := H s sS, have : s ⊆ t, { have Z := hT s sS, rw sInter_eq_bInter at Z, rw Z, apply bInter_subset_of_mem, exact tTs }, exact this xs } end lemma sUnion_bUnion {S : set (set α)} {T : set α → set (set α)} (hT : ∀s∈S, s = ⋃₀ T s) : ⋃₀ (⋃s∈S, T s) = ⋃₀ S := begin ext, simp only [exists_prop, set.mem_Union, set.mem_set_of_eq], split, { rintros ⟨t, ⟨⟨s, ⟨sS, tTs⟩⟩, xt⟩⟩, refine ⟨s, ⟨sS, _⟩⟩, rw hT s sS, exact subset_sUnion_of_mem tTs xt }, { rintros ⟨s, ⟨sS, xs⟩⟩, rw hT s sS at xs, rcases mem_sUnion.1 xs with ⟨t, tTs, xt⟩, exact ⟨t, ⟨⟨s, ⟨sS, tTs⟩⟩, xt⟩⟩ } end lemma Union_range_eq_sUnion {α β : Type*} (C : set (set α)) {f : ∀(s : C), β → s} (hf : ∀(s : C), surjective (f s)) : (⋃(y : β), range (λ(s : C), (f s y).val)) = ⋃₀ C := begin ext x, split, { rintro ⟨s, ⟨y, rfl⟩, ⟨⟨s, hs⟩, rfl⟩⟩, refine ⟨_, hs, _⟩, exact (f ⟨s, hs⟩ y).2 }, { rintro ⟨s, hs, hx⟩, cases hf ⟨s, hs⟩ ⟨x, hx⟩ with y hy, refine ⟨_, ⟨y, rfl⟩, ⟨⟨s, hs⟩, _⟩⟩, exact congr_arg subtype.val hy } end lemma Union_range_eq_Union {ι α β : Type*} (C : ι → set α) {f : ∀(x : ι), β → C x} (hf : ∀(x : ι), surjective (f x)) : (⋃(y : β), range (λ(x : ι), (f x y).val)) = ⋃x, C x := begin ext x, rw [mem_Union, mem_Union], split, { rintro ⟨y, ⟨i, rfl⟩⟩, exact ⟨i, (f i y).2⟩ }, { rintro ⟨i, hx⟩, cases hf i ⟨x, hx⟩ with y hy, refine ⟨y, ⟨i, congr_arg subtype.val hy⟩⟩ } end @[simp] theorem sub_eq_diff (s t : set α) : s - t = s \ t := rfl section variables {p : Prop} {μ : p → set α} @[simp] lemma Inter_pos (hp : p) : (⋂h:p, μ h) = μ hp := infi_pos hp @[simp] lemma Inter_neg (hp : ¬ p) : (⋂h:p, μ h) = univ := infi_neg hp @[simp] lemma Union_pos (hp : p) : (⋃h:p, μ h) = μ hp := supr_pos hp @[simp] lemma Union_neg (hp : ¬ p) : (⋃h:p, μ h) = ∅ := supr_neg hp @[simp] lemma Union_empty {ι : Sort*} : (⋃i:ι, ∅:set α) = ∅ := supr_bot @[simp] lemma Inter_univ {ι : Sort*} : (⋂i:ι, univ:set α) = univ := infi_top end section image lemma image_Union {f : α → β} {s : ι → set α} : f '' (⋃ i, s i) = (⋃i, f '' s i) := begin apply set.ext, intro x, simp [image, exists_and_distrib_right.symm, -exists_and_distrib_right], exact exists_swap end lemma univ_subtype {p : α → Prop} : (univ : set (subtype p)) = (⋃x (h : p x), {⟨x, h⟩}) := set.ext $ assume ⟨x, h⟩, by simp [h] lemma range_eq_Union {ι} (f : ι → α) : range f = (⋃i, {f i}) := set.ext $ assume a, by simp [@eq_comm α a] lemma image_eq_Union (f : α → β) (s : set α) : f '' s = (⋃i∈s, {f i}) := set.ext $ assume b, by simp [@eq_comm β b] lemma bUnion_range {f : ι → α} {g : α → set β} : (⋃x ∈ range f, g x) = (⋃y, g (f y)) := by rw [← sUnion_image, ← range_comp, sUnion_range] lemma bInter_range {f : ι → α} {g : α → set β} : (⋂x ∈ range f, g x) = (⋂y, g (f y)) := by rw [← sInter_image, ← range_comp, sInter_range] variables {s : set γ} {f : γ → α} {g : α → set β} lemma bUnion_image : (⋃x∈ (f '' s), g x) = (⋃y ∈ s, g (f y)) := by rw [← sUnion_image, ← image_comp, sUnion_image] lemma bInter_image : (⋂x∈ (f '' s), g x) = (⋂y ∈ s, g (f y)) := by rw [← sInter_image, ← image_comp, sInter_image] end image section preimage theorem monotone_preimage {f : α → β} : monotone (preimage f) := assume a b h, preimage_mono h @[simp] theorem preimage_Union {ι : Sort w} {f : α → β} {s : ι → set β} : preimage f (⋃i, s i) = (⋃i, preimage f (s i)) := set.ext $ by simp [preimage] theorem preimage_bUnion {ι} {f : α → β} {s : set ι} {t : ι → set β} : preimage f (⋃i ∈ s, t i) = (⋃i ∈ s, preimage f (t i)) := by simp @[simp] theorem preimage_sUnion {f : α → β} {s : set (set β)} : preimage f (⋃₀ s) = (⋃t ∈ s, preimage f t) := set.ext $ by simp [preimage] lemma preimage_Inter {ι : Sort*} {s : ι → set β} {f : α → β} : f ⁻¹' (⋂ i, s i) = (⋂ i, f ⁻¹' s i) := by ext; simp lemma preimage_bInter {s : γ → set β} {t : set γ} {f : α → β} : f ⁻¹' (⋂ i∈t, s i) = (⋂ i∈t, f ⁻¹' s i) := by ext; simp end preimage section seq def seq (s : set (α → β)) (t : set α) : set β := {b | ∃f∈s, ∃a∈t, (f : α → β) a = b} lemma seq_def {s : set (α → β)} {t : set α} : seq s t = ⋃f∈s, f '' t := set.ext $ by simp [seq] @[simp] lemma mem_seq_iff {s : set (α → β)} {t : set α} {b : β} : b ∈ seq s t ↔ ∃ (f ∈ s) (a ∈ t), (f : α → β) a = b := iff.rfl lemma seq_subset {s : set (α → β)} {t : set α} {u : set β} : seq s t ⊆ u ↔ (∀f∈s, ∀a∈t, (f : α → β) a ∈ u) := iff.intro (assume h f hf a ha, h ⟨f, hf, a, ha, rfl⟩) (assume h b ⟨f, hf, a, ha, eq⟩, eq ▸ h f hf a ha) lemma seq_mono {s₀ s₁ : set (α → β)} {t₀ t₁ : set α} (hs : s₀ ⊆ s₁) (ht : t₀ ⊆ t₁) : seq s₀ t₀ ⊆ seq s₁ t₁ := assume b ⟨f, hf, a, ha, eq⟩, ⟨f, hs hf, a, ht ha, eq⟩ lemma singleton_seq {f : α → β} {t : set α} : set.seq {f} t = f '' t := set.ext $ by simp lemma seq_singleton {s : set (α → β)} {a : α} : set.seq s {a} = (λf:α→β, f a) '' s := set.ext $ by simp lemma seq_seq {s : set (β → γ)} {t : set (α → β)} {u : set α} : seq s (seq t u) = seq (seq ((∘) '' s) t) u := begin refine set.ext (assume c, iff.intro _ _), { rintros ⟨f, hfs, b, ⟨g, hg, a, hau, rfl⟩, rfl⟩, exact ⟨f ∘ g, ⟨(∘) f, mem_image_of_mem _ hfs, g, hg, rfl⟩, a, hau, rfl⟩ }, { rintros ⟨fg, ⟨fc, ⟨f, hfs, rfl⟩, g, hgt, rfl⟩, a, ha, rfl⟩, exact ⟨f, hfs, g a, ⟨g, hgt, a, ha, rfl⟩, rfl⟩ } end lemma image_seq {f : β → γ} {s : set (α → β)} {t : set α} : f '' seq s t = seq ((∘) f '' s) t := by rw [← singleton_seq, ← singleton_seq, seq_seq, image_singleton] lemma prod_eq_seq {s : set α} {t : set β} : set.prod s t = (prod.mk '' s).seq t := begin ext ⟨a, b⟩, split, { rintros ⟨ha, hb⟩, exact ⟨prod.mk a, ⟨a, ha, rfl⟩, b, hb, rfl⟩ }, { rintros ⟨f, ⟨x, hx, rfl⟩, y, hy, eq⟩, rw ← eq, exact ⟨hx, hy⟩ } end lemma prod_image_seq_comm (s : set α) (t : set β) : (prod.mk '' s).seq t = seq ((λb a, (a, b)) '' t) s := by rw [← prod_eq_seq, ← image_swap_prod, prod_eq_seq, image_seq, ← image_comp] end seq theorem monotone_prod [preorder α] {f : α → set β} {g : α → set γ} (hf : monotone f) (hg : monotone g) : monotone (λx, set.prod (f x) (g x)) := assume a b h, prod_mono (hf h) (hg h) instance : monad set := { pure := λ(α : Type u) a, {a}, bind := λ(α β : Type u) s f, ⋃i∈s, f i, seq := λ(α β : Type u), set.seq, map := λ(α β : Type u), set.image } instance : is_lawful_monad set := { pure_bind := assume α β x f, by simp, bind_assoc := assume α β γ s f g, set.ext $ assume a, by simp [exists_and_distrib_right.symm, -exists_and_distrib_right, exists_and_distrib_left.symm, -exists_and_distrib_left, and_assoc]; exact exists_swap, id_map := assume α, id_map, bind_pure_comp_eq_map := assume α β f s, set.ext $ by simp [set.image, eq_comm], bind_map_eq_seq := assume α β s t, by simp [seq_def] } instance : is_comm_applicative (set : Type u → Type u) := ⟨ assume α β s t, prod_image_seq_comm s t ⟩ section monad variables {α' β' : Type u} {s : set α'} {f : α' → set β'} {g : set (α' → β')} @[simp] lemma bind_def : s >>= f = ⋃i∈s, f i := rfl @[simp] lemma fmap_eq_image (f : α' → β') : f <$> s = f '' s := rfl @[simp] lemma seq_eq_set_seq {α β : Type*} (s : set (α → β)) (t : set α) : s <*> t = s.seq t := rfl @[simp] lemma pure_def (a : α) : (pure a : set α) = {a} := rfl end monad section pi lemma pi_def {α : Type*} {π : α → Type*} (i : set α) (s : Πa, set (π a)) : pi i s = (⋂ a∈i, ((λf:(Πa, π a), f a) ⁻¹' (s a))) := by ext; simp [pi] end pi end set /- disjoint sets -/ section disjoint variable [semilattice_inf_bot α] /-- Two elements of a lattice are disjoint if their inf is the bottom element. (This generalizes disjoint sets, viewed as members of the subset lattice.) -/ def disjoint (a b : α) : Prop := a ⊓ b ≤ ⊥ theorem disjoint.eq_bot {a b : α} (h : disjoint a b) : a ⊓ b = ⊥ := eq_bot_iff.2 h theorem disjoint_iff {a b : α} : disjoint a b ↔ a ⊓ b = ⊥ := eq_bot_iff.symm theorem disjoint.comm {a b : α} : disjoint a b ↔ disjoint b a := by rw [disjoint, disjoint, inf_comm] theorem disjoint.symm {a b : α} : disjoint a b → disjoint b a := disjoint.comm.1 @[simp] theorem disjoint_bot_left {a : α} : disjoint ⊥ a := disjoint_iff.2 bot_inf_eq @[simp] theorem disjoint_bot_right {a : α} : disjoint a ⊥ := disjoint_iff.2 inf_bot_eq theorem disjoint_mono {a b c d : α} (h₁ : a ≤ b) (h₂ : c ≤ d) : disjoint b d → disjoint a c := le_trans (inf_le_inf h₁ h₂) theorem disjoint_mono_left {a b c : α} (h : a ≤ b) : disjoint b c → disjoint a c := disjoint_mono h (le_refl _) theorem disjoint_mono_right {a b c : α} (h : b ≤ c) : disjoint a c → disjoint a b := disjoint_mono (le_refl _) h @[simp] lemma disjoint_self {a : α} : disjoint a a ↔ a = ⊥ := by simp [disjoint] lemma ne_of_disjoint {a b : α} (ha : a ≠ ⊥) (hab : disjoint a b) : a ≠ b := by { intro h, rw [←h, disjoint_self] at hab, exact ha hab } end disjoint namespace set protected theorem disjoint_iff {s t : set α} : disjoint s t ↔ s ∩ t ⊆ ∅ := iff.rfl lemma not_disjoint_iff {s t : set α} : ¬disjoint s t ↔ ∃x, x ∈ s ∧ x ∈ t := by { rw [set.disjoint_iff, subset_empty_iff], apply ne_empty_iff_exists_mem } lemma disjoint_left {s t : set α} : disjoint s t ↔ ∀ {a}, a ∈ s → a ∉ t := show (∀ x, ¬(x ∈ s ∩ t)) ↔ _, from ⟨λ h a, not_and.1 $ h a, λ h a, not_and.2 $ h a⟩ theorem disjoint_right {s t : set α} : disjoint s t ↔ ∀ {a}, a ∈ t → a ∉ s := by rw [disjoint.comm, disjoint_left] theorem disjoint_diff {a b : set α} : disjoint a (b \ a) := disjoint_iff.2 (inter_diff_self _ _) theorem disjoint_compl (s : set α) : disjoint s (-s) := assume a ⟨h₁, h₂⟩, h₂ h₁ theorem disjoint_singleton_left {a : α} {s : set α} : disjoint {a} s ↔ a ∉ s := by simp [set.disjoint_iff, subset_def]; exact iff.rfl theorem disjoint_singleton_right {a : α} {s : set α} : disjoint s {a} ↔ a ∉ s := by rw [disjoint.comm]; exact disjoint_singleton_left theorem disjoint_image_image {f : β → α} {g : γ → α} {s : set β} {t : set γ} (h : ∀b∈s, ∀c∈t, f b ≠ g c) : disjoint (f '' s) (g '' t) := by rintros a ⟨⟨b, hb, eq⟩, ⟨c, hc, rfl⟩⟩; exact h b hb c hc eq def pairwise_disjoint (s : set (set α)) : Prop := pairwise_on s disjoint lemma pairwise_disjoint_subset {s t : set (set α)} (h : s ⊆ t) (ht : pairwise_disjoint t) : pairwise_disjoint s := pairwise_on.mono h ht lemma pairwise_disjoint_range {s : set (set α)} (f : s → set α) (hf : ∀(x : s), f x ⊆ x.1) (ht : pairwise_disjoint s) : pairwise_disjoint (range f) := begin rintro _ ⟨x, rfl⟩ _ ⟨y, rfl⟩ hxy, refine disjoint_mono (hf x) (hf y) (ht _ x.2 _ y.2 _), intro h, apply hxy, apply congr_arg f, exact subtype.eq h end /- warning: classical -/ lemma pairwise_disjoint_elim {s : set (set α)} (h : pairwise_disjoint s) {x y : set α} (hx : x ∈ s) (hy : y ∈ s) (z : α) (hzx : z ∈ x) (hzy : z ∈ y) : x = y := begin haveI := classical.prop_decidable, by_contra, have : x ∩ y ≠ ∅, { rw [ne_empty_iff_exists_mem], exact ⟨z, ⟨hzx, hzy⟩⟩ }, apply this, exact disjoint_iff.mp (h x hx y hy a), end end set namespace set variables (t : α → set β) def sigma_to_Union (x : Σi, t i) : (⋃i, t i) := ⟨x.2, mem_Union.2 ⟨x.1, x.2.2⟩⟩ lemma surjective_sigma_to_Union : surjective (sigma_to_Union t) | ⟨b, hb⟩ := have ∃a, b ∈ t a, by simpa using hb, let ⟨a, hb⟩ := this in ⟨⟨a, ⟨b, hb⟩⟩, rfl⟩ lemma injective_sigma_to_Union (h : ∀i j, i ≠ j → disjoint (t i) (t j)) : injective (sigma_to_Union t) | ⟨a₁, ⟨b₁, h₁⟩⟩ ⟨a₂, ⟨b₂, h₂⟩⟩ eq := have b_eq : b₁ = b₂, from congr_arg subtype.val eq, have a_eq : a₁ = a₂, from classical.by_contradiction $ assume ne, have b₁ ∈ t a₁ ∩ t a₂, from ⟨h₁, b_eq.symm ▸ h₂⟩, h _ _ ne this, sigma.eq a_eq $ subtype.eq $ by subst b_eq; subst a_eq lemma bijective_sigma_to_Union (h : ∀i j, i ≠ j → disjoint (t i) (t j)) : bijective (sigma_to_Union t) := ⟨injective_sigma_to_Union t h, surjective_sigma_to_Union t⟩ noncomputable def Union_eq_sigma_of_disjoint {t : α → set β} (h : ∀i j, i ≠ j → disjoint (t i) (t j)) : (⋃i, t i) ≃ (Σi, t i) := (equiv.of_bijective $ bijective_sigma_to_Union t h).symm noncomputable def bUnion_eq_sigma_of_disjoint {s : set α} {t : α → set β} (h : pairwise_on s (disjoint on t)) : (⋃i∈s, t i) ≃ (Σi:s, t i.val) := equiv.trans (equiv.set_congr (bUnion_eq_Union _ _)) $ Union_eq_sigma_of_disjoint $ assume ⟨i, hi⟩ ⟨j, hj⟩ ne, h _ hi _ hj $ assume eq, ne $ subtype.eq eq end set
a2489696a2996bbc6f3e92da1f87c22aea0c90f8
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/field/power.lean
502aba88feb9358b025033cf0082ee7516a0abaf
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
1,007
lean
/- Copyright (c) 2014 Robert Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Lewis, Leonardo de Moura, Johannes Hölzl, Mario Carneiro -/ import algebra.field.defs import algebra.group_with_zero.power import algebra.parity /-! # Results about powers in fields or division rings. This file exists to ensure we can define `field` with minimal imports, so contains some lemmas about powers of elements which need imports beyond those needed for the basic definition. -/ variables {α : Type*} section division_ring variables [division_ring α] {n : ℤ} @[simp] lemma zpow_bit1_neg (a : α) (n : ℤ) : (-a) ^ bit1 n = - a ^ bit1 n := by rw [zpow_bit1', zpow_bit1', neg_mul_neg, neg_mul_eq_mul_neg] lemma odd.neg_zpow (h : odd n) (a : α) : (-a) ^ n = - a ^ n := by { obtain ⟨k, rfl⟩ := h.exists_bit1, exact zpow_bit1_neg _ _ } lemma odd.neg_one_zpow (h : odd n) : (-1 : α) ^ n = -1 := by rw [h.neg_zpow, one_zpow] end division_ring
00f0f9fb8693a62338d21aee53425daf4646c71d
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/coefun.lean
78c9ca7cf1233669837088be979e597899660419
[ "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
278
lean
inductive obj (A : Type) := mk : A → obj A inductive fn (A B : Type) := mk : (obj A → obj B) → fn A B definition to_fun [coercion] {A B : Type} (f : fn A B) : obj A → obj B := fn.rec (λf, f) f constant n : Type.{1} constant f : fn n n constant a : obj n check (f a)
db1f99daafa7cfb95f3970354abd02ecdc1294ea
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/library/data/fin.lean
e34e918b8bf6c421e800186839cbc0f59cf605b6
[ "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
4,009
lean
import data.nat logic.cast open nat inductive fin : nat → Type := fz : Π n, fin (succ n), fs : Π {n}, fin n → fin (succ n) namespace fin definition z_cases_on (C : fin zero → Type) (p : fin zero) : C p := have aux : Π (C : Type) (n : nat) (p : fin n), n = zero → C, from λ C n p, fin.rec_on p (λ n h, nat.no_confusion h) (λ n f ih h, nat.no_confusion h), aux (C p) zero p rfl definition nz_cases_on {C : Π n, fin (succ n) → Type} (H₁ : Π n, C n (fz n)) (H₂ : Π n (f : fin n), C n (fs f)) {n : nat} (f : fin (succ n)) : C n f := have aux : Π (n₁ : nat) (f₁ : fin n₁) (heq₁ : n₁ = succ n) (f : fin (succ n)) (heq₂ : f₁ == f), C n f, from λ n₁ f₁, fin.rec_on f₁ (λ (n₁ : nat) (heq₁ : succ n₁ = succ n), have heq₁' : n₁ = n, from nat.no_confusion heq₁ (λ e, e), eq.rec_on heq₁' (λ (f : fin (succ n₁)) (heq₂ : fz n₁ == f), have heq₂' : fz n₁ = f, from heq.to_eq heq₂, have Cfz : C n₁ (fz n₁), from H₁ n₁, eq.rec_on heq₂' Cfz)) (λ (n₁ : nat) (f₁ : fin n₁) (ih : _) (heq₁ : succ n₁ = succ n), have heq₁' : n₁ = n, from nat.no_confusion heq₁ (λ e, e), eq.rec_on heq₁' (λ (f : fin (succ n₁)) (heq₂ : @fs n₁ f₁ == f), have heq₂' : @fs n₁ f₁ = f, from heq.to_eq heq₂, have Cfs : C n₁ (@fs n₁ f₁), from H₂ n₁ f₁, eq.rec_on heq₂' Cfs)), aux (succ n) f rfl f !heq.refl definition to_nat {n : nat} (f : fin n) : nat := fin.rec_on f (λ n, zero) (λ n f r, succ r) theorem to_nat.lt {n : nat} (f : fin n) : to_nat f < n := fin.rec_on f (λ n, calc to_nat (fz n) = 0 : rfl ... < succ n : succ_pos n) (λ n f ih, calc to_nat (fs f) = succ (to_nat f) : rfl ... < succ n : succ_lt ih) definition lift {n : nat} (f : fin n) : Π m, fin (m + n) := fin.rec_on f (λ n m, fz (m + n)) (λ n f ih m, fs (ih m)) theorem to_nat.lift {n : nat} (f : fin n) : ∀m, to_nat f = to_nat (lift f m) := fin.rec_on f (λ n m, rfl) (λ n f ih m, calc to_nat (fs f) = succ (to_nat f) : rfl ... = succ (to_nat (lift f m)) : ih ... = to_nat (lift (fs f) m) : rfl) private definition of_nat_core (p : nat) : fin (succ p) := nat.rec_on p (fz zero) (λ a r, fs r) private theorem to_nat.of_nat_core (p : nat) : to_nat (of_nat_core p) = p := nat.induction_on p rfl (λ p₁ ih, calc to_nat (of_nat_core (succ p₁)) = succ (to_nat (of_nat_core p₁)) : rfl ... = succ p₁ : ih) private lemma of_nat_eq {p n : nat} (H : p < n) : n - succ p + succ p = n := add_sub_ge_left (lt_imp_le_succ H) definition of_nat (p : nat) (n : nat) : p < n → fin n := λ H : p < n, eq.rec_on (of_nat_eq H) (lift (of_nat_core p) (n - succ p)) theorem of_nat_def (p : nat) (n : nat) (H : p < n) : of_nat p n H = eq.rec_on (of_nat_eq H) (lift (of_nat_core p) (n - succ p)) := rfl theorem of_nat_heq (p : nat) (n : nat) (H : p < n) : of_nat p n H == lift (of_nat_core p) (n - succ p) := heq.symm (eq_rec_to_heq (eq.symm (of_nat_def p n H))) theorem to_nat.of_nat (p : nat) (n : nat) (H : p < n) : to_nat (of_nat p n H) = p := have aux₁ : to_nat (of_nat p n H) == to_nat (lift (of_nat_core p) (n - succ p)), from hcongr_arg2 @to_nat (eq.symm (of_nat_eq H)) (of_nat_heq p n H), have aux₂ : to_nat (lift (of_nat_core p) (n - succ p)) = p, from calc to_nat (lift (of_nat_core p) (n - succ p)) = to_nat (of_nat_core p) : to_nat.lift ... = p : to_nat.of_nat_core, heq.to_eq (heq.trans aux₁ (heq.from_eq aux₂)) end fin
040794f12216d2d20cd04bed7177fc6da7c78b32
f3849be5d845a1cb97680f0bbbe03b85518312f0
/tests/lean/let4.lean
22359ccef6c2377b2c8e6d4384d6b9931b00cecd
[ "Apache-2.0" ]
permissive
bjoeris/lean
0ed95125d762b17bfcb54dad1f9721f953f92eeb
4e496b78d5e73545fa4f9a807155113d8e6b0561
refs/heads/master
1,611,251,218,281
1,495,337,658,000
1,495,337,658,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
318
lean
-- constant f : num → num → num → num #check let a : num := 10, b : num := 10, c : num := 10 in f a b (f a 10 c) #check let a : num := 10, b : num := let c : num := 10 in f a c (f a a (f 10 a c)), d : num := 10, e : num := f (f 10 10 d) (f d 10 10) a in f a b (f e d 10)
b255818a9d701293d2ddc507dcf59eef405ad9d5
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/sec2.lean
c0f965a0fabebf29fd50cf92c7b687b0c4b66102
[ "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
581
lean
structure A : Type := mk structure B : Type := mk constant f : A → B constant g : B → B constant a : A namespace foo attribute f [coercion] print coercions check g a end foo check g a -- Error section boo attribute f [coercion] print coercions check g a end boo -- The metaobjects defined in the section persist to the outer level. -- This is not a bug. The idea is: we can use the scope to define -- auxiliary variables that are then used to define classes/instances/etc. -- That is, the whole point of the scope is to define these metaobjects. check g a -- Ok
98b95aae9e942468f1dad4dd661665f36c96c33a
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/test/conv/apply_congr.lean
915adaa99b5ae21e53be5469a89223c08f293358
[ "Apache-2.0" ]
permissive
dupuisf/mathlib
62de4ec6544bf3b79086afd27b6529acfaf2c1bb
8582b06b0a5d06c33ee07d0bdf7c646cae22cf36
refs/heads/master
1,669,494,854,016
1,595,692,409,000
1,595,692,409,000
272,046,630
0
0
Apache-2.0
1,592,066,143,000
1,592,066,142,000
null
UTF-8
Lean
false
false
3,160
lean
/- Copyright (c) 2019 Lucas Allen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Lucas Allen, Scott Morrison -/ import algebra.big_operators.basic import data.finsupp import tactic.converter.apply_congr import tactic.interactive example (f g : ℤ → ℤ) (S : finset ℤ) (h : ∀ m ∈ S, f m = g m) : finset.sum S f = finset.sum S g := begin conv_lhs { -- If we just call `congr` here, in the second goal we're helpless, -- because we are only given the opportunity to rewrite `f`. -- However `apply_congr` uses the appropriate `@[congr]` lemma, -- so we get to rewrite `f x`, in the presence of the crucial `H : x ∈ S` hypothesis. apply_congr, skip, simp [h, H], } end -- Again, with some `guard` statements. example (f g : ℤ → ℤ) (S : finset ℤ) (h : ∀ m ∈ S, f m = g m) : finset.sum S f = finset.sum S g := begin conv_lhs { apply_congr finset.sum_congr, -- (See the note about get_goals/set_goals inside apply_congr) (do ng ← tactic.num_goals, guard $ ng = 2), guard_target S, skip, guard_target f x, simp [h, H] } end -- Verify we can `rw` as well as `simp`. example (f g : ℤ → ℤ) (S : finset ℤ) (h : ∀ m ∈ S, f m = g m) : finset.sum S f = finset.sum S g := by conv_lhs { apply_congr, skip, rw h x H, } -- Check that the appropriate `@[congr]` lemma is automatically selected. example (f g : ℤ → ℤ) (S : finset ℤ) (h : ∀ m ∈ S, f m = g m) : finset.prod S f = finset.prod S g := by conv_lhs { apply_congr, skip, simp [h, H], } example (f g : ℤ → ℤ) (S : finset ℤ) (h : ∀ m ∈ S, f m = g m) : finset.fold (+) 0 f S = finset.fold (+) 0 g S := begin -- This time, the automatically selected congruence lemma is "too good"! -- `finset.sum_congr` matches, and so the `conv` block actually -- rewrites the left hand side into a `finset.sum`. conv_lhs { apply_congr, skip, simp [h, H], }, -- So we need a `refl` to identify that we're done. refl, end -- This can be avoided by selecting the congruence lemma by hand. example (f g : ℤ → ℤ) (S : finset ℤ) (h : ∀ m ∈ S, f m = g m) : finset.fold (+) 0 f S = finset.fold (+) 0 g S := begin conv_lhs { apply_congr finset.fold_congr, simp [h, H], }, end example (f : ℤ → ℤ) (S : finset ℤ) (h : ∀ m ∈ S, f m = 0) : finset.sum S f = 0 := begin conv_lhs { apply_congr, skip, simp [h, H], }, simp, end -- An example using `finsupp.sum` open_locale classical example {k G : Type} [semiring k] [group G] (g : G →₀ k) (a₁ x : G) (b₁ : k) (t : ∀ (a₂ : G), a₁ * a₂ = x ↔ a₁⁻¹ * x = a₂) : g.sum (λ (a₂ : G) (b₂ : k), ite (a₁ * a₂ = x) (b₁ * b₂) 0) = b₁ * g (a₁⁻¹ * x) := begin -- In fact, `congr` works fine here, because our rewrite works globally. conv_lhs { apply_congr, skip, dsimp, rw t, }, rw finset.sum_ite_eq g.support, -- it's a pity we can't just use `simp` here. split_ifs, { refl, }, { simp [finsupp.not_mem_support_iff.1 h], }, end example : true := begin success_if_fail { conv { apply_congr, }, }, trivial end
99c1ade56e111c4321a802b5705540332b0083b1
94e33a31faa76775069b071adea97e86e218a8ee
/src/number_theory/class_number/finite.lean
c201beff98c05ec22ff40f985d69a9fae638b133
[ "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
17,888
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 analysis.special_functions.pow import linear_algebra.free_module.pid import linear_algebra.matrix.absolute_value import number_theory.class_number.admissible_absolute_value import ring_theory.class_group import ring_theory.dedekind_domain.integral_closure import ring_theory.norm /-! # Class numbers of global fields In this file, we use the notion of "admissible absolute value" to prove finiteness of the class group for number fields and function fields, and define `class_number` as the order of this group. ## Main definitions - `class_group.fintype_of_admissible`: if `R` has an admissible absolute value, its integral closure has a finite class group -/ open_locale big_operators open_locale non_zero_divisors namespace class_group open ring open_locale big_operators section euclidean_domain variables (R S K L : Type*) [euclidean_domain R] [comm_ring S] [is_domain S] variables [field K] [field L] variables [algebra R K] [is_fraction_ring R K] variables [algebra K L] [finite_dimensional K L] [is_separable K L] variables [algRL : algebra R L] [is_scalar_tower R K L] variables [algebra R S] [algebra S L] variables [ist : is_scalar_tower R S L] [iic : is_integral_closure S R L] variables {R S} (abv : absolute_value R ℤ) variables {ι : Type*} [decidable_eq ι] [fintype ι] (bS : basis ι R S) /-- If `b` is an `R`-basis of `S` of cardinality `n`, then `norm_bound abv b` is an integer such that for every `R`-integral element `a : S` with coordinates `≤ y`, we have algebra.norm a ≤ norm_bound abv b * y ^ n`. (See also `norm_le` and `norm_lt`). -/ noncomputable def norm_bound : ℤ := let n := fintype.card ι, i : ι := nonempty.some bS.index_nonempty, m : ℤ := finset.max' (finset.univ.image (λ (ijk : ι × ι × ι), abv (algebra.left_mul_matrix bS (bS ijk.1) ijk.2.1 ijk.2.2))) ⟨_, finset.mem_image.mpr ⟨⟨i, i, i⟩, finset.mem_univ _, rfl⟩⟩ in nat.factorial n • (n • m) ^ n lemma norm_bound_pos : 0 < norm_bound abv bS := begin obtain ⟨i, j, k, hijk⟩ : ∃ i j k, algebra.left_mul_matrix bS (bS i) j k ≠ 0, { by_contra' h, obtain ⟨i⟩ := bS.index_nonempty, apply bS.ne_zero i, apply (injective_iff_map_eq_zero (algebra.left_mul_matrix bS)).mp (algebra.left_mul_matrix_injective bS), ext j k, simp [h, dmatrix.zero_apply] }, simp only [norm_bound, algebra.smul_def, eq_nat_cast], refine mul_pos (int.coe_nat_pos.mpr (nat.factorial_pos _)) _, refine pow_pos (mul_pos (int.coe_nat_pos.mpr (fintype.card_pos_iff.mpr ⟨i⟩)) _) _, refine lt_of_lt_of_le (abv.pos hijk) (finset.le_max' _ _ _), exact finset.mem_image.mpr ⟨⟨i, j, k⟩, finset.mem_univ _, rfl⟩ end /-- If the `R`-integral element `a : S` has coordinates `≤ y` with respect to some basis `b`, its norm is less than `norm_bound abv b * y ^ dim S`. -/ lemma norm_le (a : S) {y : ℤ} (hy : ∀ k, abv (bS.repr a k) ≤ y) : abv (algebra.norm R a) ≤ norm_bound abv bS * y ^ fintype.card ι := begin conv_lhs { rw ← bS.sum_repr a }, rw [algebra.norm_apply, ← linear_map.det_to_matrix bS], simp only [algebra.norm_apply, alg_hom.map_sum, alg_hom.map_smul, linear_equiv.map_sum, linear_equiv.map_smul, algebra.to_matrix_lmul_eq, norm_bound, smul_mul_assoc, ← mul_pow], convert matrix.det_sum_smul_le finset.univ _ hy using 3, { rw [finset.card_univ, smul_mul_assoc, mul_comm] }, { intros i j k, apply finset.le_max', exact finset.mem_image.mpr ⟨⟨i, j, k⟩, finset.mem_univ _, rfl⟩ }, end /-- If the `R`-integral element `a : S` has coordinates `< y` with respect to some basis `b`, its norm is strictly less than `norm_bound abv b * y ^ dim S`. -/ lemma norm_lt {T : Type*} [linear_ordered_ring T] (a : S) {y : T} (hy : ∀ k, (abv (bS.repr a k) : T) < y) : (abv (algebra.norm R a) : T) < norm_bound abv bS * y ^ fintype.card ι := begin obtain ⟨i⟩ := bS.index_nonempty, have him : (finset.univ.image (λ k, abv (bS.repr a k))).nonempty := ⟨_, finset.mem_image.mpr ⟨i, finset.mem_univ _, rfl⟩⟩, set y' : ℤ := finset.max' _ him with y'_def, have hy' : ∀ k, abv (bS.repr a k) ≤ y', { intro k, exact finset.le_max' _ _ (finset.mem_image.mpr ⟨k, finset.mem_univ _, rfl⟩) }, have : (y' : T) < y, { rw [y'_def, ← finset.max'_image (show monotone (coe : ℤ → T), from λ x y h, int.cast_le.mpr h)], apply (finset.max'_lt_iff _ (him.image _)).mpr, simp only [finset.mem_image, exists_prop], rintros _ ⟨x, ⟨k, -, rfl⟩, rfl⟩, exact hy k }, have y'_nonneg : 0 ≤ y' := le_trans (abv.nonneg _) (hy' i), apply (int.cast_le.mpr (norm_le abv bS a hy')).trans_lt, simp only [int.cast_mul, int.cast_pow], apply mul_lt_mul' le_rfl, { exact pow_lt_pow_of_lt_left this (int.cast_nonneg.mpr y'_nonneg) (fintype.card_pos_iff.mpr ⟨i⟩) }, { exact pow_nonneg (int.cast_nonneg.mpr y'_nonneg) _ }, { exact int.cast_pos.mpr (norm_bound_pos abv bS) }, { apply_instance } end /-- A nonzero ideal has an element of minimal norm. -/ lemma exists_min (I : (ideal S)⁰) : ∃ b ∈ (I : ideal S), b ≠ 0 ∧ ∀ c ∈ (I : ideal S), abv (algebra.norm R c) < abv (algebra.norm R b) → c = (0 : S) := begin obtain ⟨_, ⟨b, b_mem, b_ne_zero, rfl⟩, min⟩ := @int.exists_least_of_bdd (λ a, ∃ b ∈ (I : ideal S), b ≠ (0 : S) ∧ abv (algebra.norm R b) = a) _ _, { refine ⟨b, b_mem, b_ne_zero, _⟩, intros c hc lt, contrapose! lt with c_ne_zero, exact min _ ⟨c, hc, c_ne_zero, rfl⟩ }, { use 0, rintros _ ⟨b, b_mem, b_ne_zero, rfl⟩, apply abv.nonneg }, { obtain ⟨b, b_mem, b_ne_zero⟩ := (I : ideal S).ne_bot_iff.mp (non_zero_divisors.coe_ne_zero I), exact ⟨_, ⟨b, b_mem, b_ne_zero, rfl⟩⟩ } end section is_admissible variables (L) {abv} (adm : abv.is_admissible) include adm /-- If we have a large enough set of elements in `R^ι`, then there will be a pair whose remainders are close together. We'll show that all sets of cardinality at least `cardM bS adm` elements satisfy this condition. The value of `cardM` is not at all optimal: for specific choices of `R`, the minimum cardinality can be exponentially smaller. -/ noncomputable def cardM : ℕ := adm.card (norm_bound abv bS ^ (-1 / (fintype.card ι) : ℝ)) ^ fintype.card ι variables [infinite R] /-- In the following results, we need a large set of distinct elements of `R`. -/ noncomputable def distinct_elems : fin (cardM bS adm).succ ↪ R := function.embedding.trans (fin.coe_embedding _).to_embedding (infinite.nat_embedding R) variables [decidable_eq R] /-- `finset_approx` is a finite set such that each fractional ideal in the integral closure contains an element close to `finset_approx`. -/ noncomputable def finset_approx : finset R := ((finset.univ.product finset.univ) .image (λ (xy : _ × _), distinct_elems bS adm xy.1 - distinct_elems bS adm xy.2)) .erase 0 lemma finset_approx.zero_not_mem : (0 : R) ∉ finset_approx bS adm := finset.not_mem_erase _ _ @[simp] lemma mem_finset_approx {x : R} : x ∈ finset_approx bS adm ↔ ∃ i j, i ≠ j ∧ distinct_elems bS adm i - distinct_elems bS adm j = x := begin simp only [finset_approx, finset.mem_erase, finset.mem_image], split, { rintros ⟨hx, ⟨i, j⟩, _, rfl⟩, refine ⟨i, j, _, rfl⟩, rintro rfl, simpa using hx }, { rintros ⟨i, j, hij, rfl⟩, refine ⟨_, ⟨i, j⟩, finset.mem_product.mpr ⟨finset.mem_univ _, finset.mem_univ _⟩, rfl⟩, rw [ne.def, sub_eq_zero], exact λ h, hij ((distinct_elems bS adm).injective h) } end section real open real local attribute [-instance] real.decidable_eq /-- We can approximate `a / b : L` with `q / r`, where `r` has finitely many options for `L`. -/ theorem exists_mem_finset_approx (a : S) {b} (hb : b ≠ (0 : R)) : ∃ (q : S) (r ∈ finset_approx bS adm), abv (algebra.norm R (r • a - b • q)) < abv (algebra.norm R (algebra_map R S b)) := begin have dim_pos := fintype.card_pos_iff.mpr bS.index_nonempty, set ε : ℝ := norm_bound abv bS ^ (-1 / (fintype.card ι) : ℝ) with ε_eq, have hε : 0 < ε := real.rpow_pos_of_pos (int.cast_pos.mpr (norm_bound_pos abv bS)) _, have ε_le : (norm_bound abv bS : ℝ) * (abv b • ε) ^ fintype.card ι ≤ (abv b ^ fintype.card ι), { have := norm_bound_pos abv bS, have := abv.nonneg b, rw [ε_eq, algebra.smul_def, ring_hom.eq_int_cast, ← rpow_nat_cast, mul_rpow, ← rpow_mul, div_mul_cancel, rpow_neg_one, mul_left_comm, mul_inv_cancel, mul_one, rpow_nat_cast]; try { norm_cast, linarith }, { apply rpow_nonneg_of_nonneg, norm_cast, linarith } }, let μ : fin (cardM bS adm).succ ↪ R := distinct_elems bS adm, set s := bS.repr a, have s_eq : ∀ i, s i = bS.repr a i := λ i, rfl, set qs := λ j i, (μ j * s i) / b, have q_eq : ∀ j i, qs j i = (μ j * s i) / b := λ i j, rfl, set rs := λ j i, (μ j * s i) % b with r_eq, have r_eq : ∀ j i, rs j i = (μ j * s i) % b := λ i j, rfl, have μ_eq : ∀ i j, μ j * s i = b * qs j i + rs j i, { intros i j, rw [q_eq, r_eq, euclidean_domain.div_add_mod], }, have μ_mul_a_eq : ∀ j, μ j • a = b • ∑ i, qs j i • bS i + ∑ i, rs j i • bS i, { intro j, rw ← bS.sum_repr a, simp only [finset.smul_sum, ← finset.sum_add_distrib], refine finset.sum_congr rfl (λ i _, _), rw [← s_eq, ← mul_smul, μ_eq, add_smul, mul_smul] }, obtain ⟨j, k, j_ne_k, hjk⟩ := adm.exists_approx hε hb (λ j i, μ j * s i), have hjk' : ∀ i, (abv (rs k i - rs j i) : ℝ) < abv b • ε, { simpa only [r_eq] using hjk }, set q := ∑ i, (qs k i - qs j i) • bS i with q_eq, set r := μ k - μ j with r_eq, refine ⟨q, r, (mem_finset_approx bS adm).mpr _, _⟩, { exact ⟨k, j, j_ne_k.symm, rfl⟩ }, have : r • a - b • q = (∑ (x : ι), (rs k x • bS x - rs j x • bS x)), { simp only [r_eq, sub_smul, μ_mul_a_eq, q_eq, finset.smul_sum, ← finset.sum_add_distrib, ← finset.sum_sub_distrib, smul_sub], refine finset.sum_congr rfl (λ x _, _), ring }, rw [this, algebra.norm_algebra_map_of_basis bS, abv.map_pow], refine int.cast_lt.mp ((norm_lt abv bS _ (λ i, lt_of_le_of_lt _ (hjk' i))).trans_le _), { apply le_of_eq, congr, simp_rw [linear_equiv.map_sum, linear_equiv.map_sub, linear_equiv.map_smul, finset.sum_apply', finsupp.sub_apply, finsupp.smul_apply, finset.sum_sub_distrib, basis.repr_self_apply, smul_eq_mul, mul_boole, finset.sum_ite_eq', finset.mem_univ, if_true] }, { exact_mod_cast ε_le }, end include ist iic /-- We can approximate `a / b : L` with `q / r`, where `r` has finitely many options for `L`. -/ theorem exists_mem_finset_approx' (h : algebra.is_algebraic R L) (a : S) {b : S} (hb : b ≠ 0) : ∃ (q : S) (r ∈ finset_approx bS adm), abv (algebra.norm R (r • a - q * b)) < abv (algebra.norm R b) := begin have inj : function.injective (algebra_map R L), { rw is_scalar_tower.algebra_map_eq R S L, exact (is_integral_closure.algebra_map_injective S R L).comp bS.algebra_map_injective }, obtain ⟨a', b', hb', h⟩ := is_integral_closure.exists_smul_eq_mul h inj a hb, obtain ⟨q, r, hr, hqr⟩ := exists_mem_finset_approx bS adm a' hb', refine ⟨q, r, hr, _⟩, refine lt_of_mul_lt_mul_left _ (show 0 ≤ abv (algebra.norm R (algebra_map R S b')), from abv.nonneg _), refine lt_of_le_of_lt (le_of_eq _) (mul_lt_mul hqr le_rfl (abv.pos ((algebra.norm_ne_zero_iff_of_basis bS).mpr hb)) (abv.nonneg _)), rw [← abv.map_mul, ← monoid_hom.map_mul, ← abv.map_mul, ← monoid_hom.map_mul, ← algebra.smul_def, smul_sub b', sub_mul, smul_comm, h, mul_comm b a', algebra.smul_mul_assoc r a' b, algebra.smul_mul_assoc b' q b] end end real lemma prod_finset_approx_ne_zero : algebra_map R S (∏ m in finset_approx bS adm, m) ≠ 0 := begin refine mt ((injective_iff_map_eq_zero _).mp bS.algebra_map_injective _) _, simp only [finset.prod_eq_zero_iff, not_exists], rintros x hx rfl, exact finset_approx.zero_not_mem bS adm hx end lemma ne_bot_of_prod_finset_approx_mem (J : ideal S) (h : algebra_map _ _ (∏ m in finset_approx bS adm, m) ∈ J) : J ≠ ⊥ := (submodule.ne_bot_iff _).mpr ⟨_, h, prod_finset_approx_ne_zero _ _⟩ include ist iic /-- Each class in the class group contains an ideal `J` such that `M := Π m ∈ finset_approx` is in `J`. -/ theorem exists_mk0_eq_mk0 [is_dedekind_domain S] [is_fraction_ring S L] (h : algebra.is_algebraic R L) (I : (ideal S)⁰) : ∃ (J : (ideal S)⁰), class_group.mk0 L I = class_group.mk0 L J ∧ algebra_map _ _ (∏ m in finset_approx bS adm, m) ∈ (J : ideal S) := begin set M := ∏ m in finset_approx bS adm, m with M_eq, have hM : algebra_map R S M ≠ 0 := prod_finset_approx_ne_zero bS adm, obtain ⟨b, b_mem, b_ne_zero, b_min⟩ := exists_min abv I, suffices : ideal.span {b} ∣ ideal.span {algebra_map _ _ M} * I.1, { obtain ⟨J, hJ⟩ := this, refine ⟨⟨J, _⟩, _, _⟩, { rw mem_non_zero_divisors_iff_ne_zero, rintro rfl, rw [ideal.zero_eq_bot, ideal.mul_bot] at hJ, exact hM (ideal.span_singleton_eq_bot.mp (I.2 _ hJ)) }, { rw class_group.mk0_eq_mk0_iff, exact ⟨algebra_map _ _ M, b, hM, b_ne_zero, hJ⟩ }, rw [← set_like.mem_coe, ← set.singleton_subset_iff, ← ideal.span_le, ← ideal.dvd_iff_le], refine (mul_dvd_mul_iff_left _).mp _, swap, { exact mt ideal.span_singleton_eq_bot.mp b_ne_zero }, rw [subtype.coe_mk, ideal.dvd_iff_le, ← hJ, mul_comm], apply ideal.mul_mono le_rfl, rw [ideal.span_le, set.singleton_subset_iff], exact b_mem }, rw [ideal.dvd_iff_le, ideal.mul_le], intros r' hr' a ha, rw ideal.mem_span_singleton at ⊢ hr', obtain ⟨q, r, r_mem, lt⟩ := exists_mem_finset_approx' L bS adm h a b_ne_zero, apply @dvd_of_mul_left_dvd _ _ q, simp only [algebra.smul_def] at lt, rw ← sub_eq_zero.mp (b_min _ (I.1.sub_mem (I.1.mul_mem_left _ ha) (I.1.mul_mem_left _ b_mem)) lt), refine mul_dvd_mul_right (dvd_trans (ring_hom.map_dvd _ _) hr') _, exact multiset.dvd_prod (multiset.mem_map.mpr ⟨_, r_mem, rfl⟩) end omit iic ist /-- `class_group.mk_M_mem` is a specialization of `class_group.mk0` to (the finite set of) ideals that contain `M := ∏ m in finset_approx L f abs, m`. By showing this function is surjective, we prove that the class group is finite. -/ noncomputable def mk_M_mem [is_fraction_ring S L] [is_dedekind_domain S] (J : {J : ideal S // algebra_map _ _ (∏ m in finset_approx bS adm, m) ∈ J}) : class_group S L := class_group.mk0 _ ⟨J.1, mem_non_zero_divisors_iff_ne_zero.mpr (ne_bot_of_prod_finset_approx_mem bS adm J.1 J.2)⟩ include iic ist lemma mk_M_mem_surjective [is_fraction_ring S L] [is_dedekind_domain S] (h : algebra.is_algebraic R L) : function.surjective (class_group.mk_M_mem L bS adm) := begin intro I', obtain ⟨⟨I, hI⟩, rfl⟩ := class_group.mk0_surjective I', obtain ⟨J, mk0_eq_mk0, J_dvd⟩ := exists_mk0_eq_mk0 L bS adm h ⟨I, hI⟩, exact ⟨⟨J, J_dvd⟩, mk0_eq_mk0.symm⟩ end open_locale classical /-- The main theorem: the class group of an integral closure `S` of `R` in an algebraic extension `L` is finite if there is an admissible absolute value. See also `class_group.fintype_of_admissible_of_finite` where `L` is a finite extension of `K = Frac(R)`, supplying most of the required assumptions automatically. -/ noncomputable def fintype_of_admissible_of_algebraic [is_fraction_ring S L] [is_dedekind_domain S] (h : algebra.is_algebraic R L) : fintype (class_group S L) := @fintype.of_surjective _ _ _ (@fintype.of_equiv _ {J // J ∣ ideal.span ({algebra_map R S (∏ (m : R) in finset_approx bS adm, m)} : set S)} (unique_factorization_monoid.fintype_subtype_dvd _ (by { rw [ne.def, ideal.zero_eq_bot, ideal.span_singleton_eq_bot], exact prod_finset_approx_ne_zero bS adm })) ((equiv.refl _).subtype_equiv (λ I, ideal.dvd_iff_le.trans (by rw [equiv.refl_apply, ideal.span_le, set.singleton_subset_iff])))) (class_group.mk_M_mem L bS adm) (class_group.mk_M_mem_surjective L bS adm h) /-- The main theorem: the class group of an integral closure `S` of `R` in a finite extension `L` of `K = Frac(R)` is finite if there is an admissible absolute value. See also `class_group.fintype_of_admissible_of_algebraic` where `L` is an algebraic extension of `R`, that includes some extra assumptions. -/ noncomputable def fintype_of_admissible_of_finite [is_dedekind_domain R] : fintype (@class_group S L _ _ _ (is_integral_closure.is_fraction_ring_of_finite_extension R K L S)) := begin letI := classical.dec_eq L, letI := is_integral_closure.is_fraction_ring_of_finite_extension R K L S, letI := is_integral_closure.is_dedekind_domain R K L S, choose s b hb_int using finite_dimensional.exists_is_basis_integral R K L, obtain ⟨n, b⟩ := submodule.basis_of_pid_of_le_span _ (is_integral_closure.range_le_span_dual_basis S b hb_int), let bS := b.map ((linear_map.quot_ker_equiv_range _).symm ≪≫ₗ _), refine fintype_of_admissible_of_algebraic L bS adm (λ x, (is_fraction_ring.is_algebraic_iff R K L).mpr (algebra.is_algebraic_of_finite _ _ x)), { rw linear_map.ker_eq_bot.mpr, { exact submodule.quot_equiv_of_eq_bot _ rfl }, { exact is_integral_closure.algebra_map_injective _ R _ } }, { refine (basis.linear_independent _).restrict_scalars _, simp only [algebra.smul_def, mul_one], apply is_fraction_ring.injective } end end is_admissible end euclidean_domain end class_group
e8f22e4c9ba80bbea856b058c348325ba3d08450
b32d3853770e6eaf06817a1b8c52064baaed0ef1
/src/super/lpo.lean
0d22b6e3c44fa8f150b5167284652d0d448c90e9
[]
no_license
gebner/super2
4d58b7477b6f7d945d5d866502982466db33ab0b
9bc5256c31750021ab97d6b59b7387773e54b384
refs/heads/master
1,635,021,682,021
1,634,886,326,000
1,634,886,326,000
225,600,688
4
2
null
1,598,209,306,000
1,575,371,550,000
Lean
UTF-8
Lean
false
false
2,409
lean
/- Copyright (c) 2017 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ -- Polytime version of lexicographic path order as described in: -- Things to know when implementing LPO, Bernd Löchner, ESFOR 2004 import super.utils namespace super open expr decidable monad native def lex {T} [decidable_eq T] (gt : T → T → bool) : list T → list T → bool | (s::ss) (t::ts) := if s = t then lex ss ts else gt s t | _ _ := ff def majo {T} (gt : T → T → bool) (s : T) : list T → bool | [] := tt | (t::ts) := gt s t && majo ts meta def alpha (lpo : expr → expr → bool) : list expr → expr → bool | [] _ := ff | (s::ss) t := (s = t) || lpo s t || alpha ss t meta def lex_ma (lpo : expr → expr → bool) (s t : expr) : list expr → list expr → bool | (si::ss) (ti::ts) := if si = ti then lex_ma ss ts else if lpo si ti then majo lpo s ts else alpha lpo (si::ss) t | _ _ := ff meta def lpo (prec_gt : expr → expr → bool) : expr → expr → bool | s t := if prec_gt (get_app_fn s) (get_app_fn t) then majo lpo s (get_app_args t) else if get_app_fn s = get_app_fn t then lex_ma lpo s t (get_app_args s).reverse (get_app_args t).reverse else alpha lpo (get_app_args s) t meta def name_of_funsym : expr → name | (local_const uniq _ _ _) := uniq | (const n _) := n | _ := name.anonymous private meta def contained_funsyms' : expr → name_set → name_set | (var _) m := m | (sort _) m := m | (const n ls) m := m.insert n | (mvar _ _ t) m := contained_funsyms' t m | (local_const uniq pp bi t) m := m.insert uniq | (app a b) m := contained_funsyms' a (contained_funsyms' b m) | (lam _ _ d b) m := contained_funsyms' d (contained_funsyms' b m) | (pi _ _ d b) m := contained_funsyms' d (contained_funsyms' b m) | (elet _ t v b) m := contained_funsyms' t (contained_funsyms' v (contained_funsyms' b m)) | (macro _ _) m := m meta def contained_funsyms (e : expr) : name_set := contained_funsyms' e mk_name_set meta def prec_gt_of_name_list (ns : list name) : expr → expr → bool := let nis := rb_map.of_list ns.zip_with_index in λ s t, match (nis.find (name_of_funsym s), nis.find (name_of_funsym t)) with | (some si, some ti) := si > ti | _ := ff end meta def mk_lpo (ns : list name) : expr → expr → bool := let prec_gt := prec_gt_of_name_list ns in lpo (prec_gt_of_name_list ns) end super
1914995cf3d32e35f74c85c2b1271418058b5d6e
1abd1ed12aa68b375cdef28959f39531c6e95b84
/src/measure_theory/integral/lebesgue.lean
ecef358eaa22f9bcca2a62cd90149f4a66b633c0
[ "Apache-2.0" ]
permissive
jumpy4/mathlib
d3829e75173012833e9f15ac16e481e17596de0f
af36f1a35f279f0e5b3c2a77647c6bf2cfd51a13
refs/heads/master
1,693,508,842,818
1,636,203,271,000
1,636,203,271,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
109,004
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 measure_theory.measure.measure_space import measure_theory.constructions.borel_space import algebra.indicator_function import algebra.support /-! # Lebesgue integral for `ℝ≥0∞`-valued functions We define simple functions and show that each Borel measurable function on `ℝ≥0∞` can be approximated by a sequence of simple functions. To prove something for an arbitrary measurable function into `ℝ≥0∞`, the theorem `measurable.ennreal_induction` shows that is it sufficient to show that the property holds for (multiples of) characteristic functions and is closed under addition and supremum of increasing sequences of functions. ## Notation We introduce the following notation for the lower Lebesgue integral of a function `f : α → ℝ≥0∞`. * `∫⁻ x, f x ∂μ`: integral of a function `f : α → ℝ≥0∞` with respect to a measure `μ`; * `∫⁻ x, f x`: integral of a function `f : α → ℝ≥0∞` with respect to the canonical measure `volume` on `α`; * `∫⁻ x in s, f x ∂μ`: integral of a function `f : α → ℝ≥0∞` over a set `s` with respect to a measure `μ`, defined as `∫⁻ x, f x ∂(μ.restrict s)`; * `∫⁻ x in s, f x`: integral of a function `f : α → ℝ≥0∞` over a set `s` with respect to the canonical measure `volume`, defined as `∫⁻ x, f x ∂(volume.restrict s)`. -/ noncomputable theory open set (hiding restrict restrict_apply) filter ennreal function (support) open_locale classical topological_space big_operators nnreal ennreal measure_theory namespace measure_theory variables {α β γ δ : Type*} /-- A function `f` from a measurable space to any type is called *simple*, if every preimage `f ⁻¹' {x}` is measurable, and the range is finite. This structure bundles a function with these properties. -/ structure {u v} simple_func (α : Type u) [measurable_space α] (β : Type v) := (to_fun : α → β) (measurable_set_fiber' : ∀ x, measurable_set (to_fun ⁻¹' {x})) (finite_range' : (set.range to_fun).finite) local infixr ` →ₛ `:25 := simple_func namespace simple_func section measurable variables [measurable_space α] instance has_coe_to_fun : has_coe_to_fun (α →ₛ β) (λ _, α → β) := ⟨to_fun⟩ lemma coe_injective ⦃f g : α →ₛ β⦄ (H : (f : α → β) = g) : f = g := by cases f; cases g; congr; exact H @[ext] theorem ext {f g : α →ₛ β} (H : ∀ a, f a = g a) : f = g := coe_injective $ funext H lemma finite_range (f : α →ₛ β) : (set.range f).finite := f.finite_range' lemma measurable_set_fiber (f : α →ₛ β) (x : β) : measurable_set (f ⁻¹' {x}) := f.measurable_set_fiber' x /-- Range of a simple function `α →ₛ β` as a `finset β`. -/ protected def range (f : α →ₛ β) : finset β := f.finite_range.to_finset @[simp] theorem mem_range {f : α →ₛ β} {b} : b ∈ f.range ↔ b ∈ range f := finite.mem_to_finset _ theorem mem_range_self (f : α →ₛ β) (x : α) : f x ∈ f.range := mem_range.2 ⟨x, rfl⟩ @[simp] lemma coe_range (f : α →ₛ β) : (↑f.range : set β) = set.range f := f.finite_range.coe_to_finset theorem mem_range_of_measure_ne_zero {f : α →ₛ β} {x : β} {μ : measure α} (H : μ (f ⁻¹' {x}) ≠ 0) : x ∈ f.range := let ⟨a, ha⟩ := nonempty_of_measure_ne_zero H in mem_range.2 ⟨a, ha⟩ lemma forall_range_iff {f : α →ₛ β} {p : β → Prop} : (∀ y ∈ f.range, p y) ↔ ∀ x, p (f x) := by simp only [mem_range, set.forall_range_iff] lemma exists_range_iff {f : α →ₛ β} {p : β → Prop} : (∃ y ∈ f.range, p y) ↔ ∃ x, p (f x) := by simpa only [mem_range, exists_prop] using set.exists_range_iff lemma preimage_eq_empty_iff (f : α →ₛ β) (b : β) : f ⁻¹' {b} = ∅ ↔ b ∉ f.range := preimage_singleton_eq_empty.trans $ not_congr mem_range.symm lemma exists_forall_le [nonempty β] [directed_order β] (f : α →ₛ β) : ∃ C, ∀ x, f x ≤ C := f.range.exists_le.imp $ λ C, forall_range_iff.1 /-- Constant function as a `simple_func`. -/ def const (α) {β} [measurable_space α] (b : β) : α →ₛ β := ⟨λ a, b, λ x, measurable_set.const _, finite_range_const⟩ instance [inhabited β] : inhabited (α →ₛ β) := ⟨const _ (default _)⟩ theorem const_apply (a : α) (b : β) : (const α b) a = b := rfl @[simp] theorem coe_const (b : β) : ⇑(const α b) = function.const α b := rfl @[simp] lemma range_const (α) [measurable_space α] [nonempty α] (b : β) : (const α b).range = {b} := finset.coe_injective $ by simp lemma range_const_subset (α) [measurable_space α] (b : β) : (const α b).range ⊆ {b} := finset.coe_subset.1 $ by simp lemma measurable_set_cut (r : α → β → Prop) (f : α →ₛ β) (h : ∀b, measurable_set {a | r a b}) : measurable_set {a | r a (f a)} := begin have : {a | r a (f a)} = ⋃ b ∈ range f, {a | r a b} ∩ f ⁻¹' {b}, { ext a, suffices : r a (f a) ↔ ∃ i, r a (f i) ∧ f a = f i, by simpa, exact ⟨λ h, ⟨a, ⟨h, rfl⟩⟩, λ ⟨a', ⟨h', e⟩⟩, e.symm ▸ h'⟩ }, rw this, exact measurable_set.bUnion f.finite_range.countable (λ b _, measurable_set.inter (h b) (f.measurable_set_fiber _)) end @[measurability] theorem measurable_set_preimage (f : α →ₛ β) (s) : measurable_set (f ⁻¹' s) := measurable_set_cut (λ _ b, b ∈ s) f (λ b, measurable_set.const (b ∈ s)) /-- A simple function is measurable -/ @[measurability] protected theorem measurable [measurable_space β] (f : α →ₛ β) : measurable f := λ s _, measurable_set_preimage f s @[measurability] protected theorem ae_measurable [measurable_space β] {μ : measure α} (f : α →ₛ β) : ae_measurable f μ := f.measurable.ae_measurable protected lemma sum_measure_preimage_singleton (f : α →ₛ β) {μ : measure α} (s : finset β) : ∑ y in s, μ (f ⁻¹' {y}) = μ (f ⁻¹' ↑s) := sum_measure_preimage_singleton _ (λ _ _, f.measurable_set_fiber _) lemma sum_range_measure_preimage_singleton (f : α →ₛ β) (μ : measure α) : ∑ y in f.range, μ (f ⁻¹' {y}) = μ univ := by rw [f.sum_measure_preimage_singleton, coe_range, preimage_range] /-- If-then-else as a `simple_func`. -/ def piecewise (s : set α) (hs : measurable_set s) (f g : α →ₛ β) : α →ₛ β := ⟨s.piecewise f g, λ x, by letI : measurable_space β := ⊤; exact f.measurable.piecewise hs g.measurable trivial, (f.finite_range.union g.finite_range).subset range_ite_subset⟩ @[simp] theorem coe_piecewise {s : set α} (hs : measurable_set s) (f g : α →ₛ β) : ⇑(piecewise s hs f g) = s.piecewise f g := rfl theorem piecewise_apply {s : set α} (hs : measurable_set s) (f g : α →ₛ β) (a) : piecewise s hs f g a = if a ∈ s then f a else g a := rfl @[simp] lemma piecewise_compl {s : set α} (hs : measurable_set sᶜ) (f g : α →ₛ β) : piecewise sᶜ hs f g = piecewise s hs.of_compl g f := coe_injective $ by simp [hs] @[simp] lemma piecewise_univ (f g : α →ₛ β) : piecewise univ measurable_set.univ f g = f := coe_injective $ by simp @[simp] lemma piecewise_empty (f g : α →ₛ β) : piecewise ∅ measurable_set.empty f g = g := coe_injective $ by simp lemma support_indicator [has_zero β] {s : set α} (hs : measurable_set s) (f : α →ₛ β) : function.support (f.piecewise s hs (simple_func.const α 0)) = s ∩ function.support f := set.support_indicator lemma range_indicator {s : set α} (hs : measurable_set s) (hs_nonempty : s.nonempty) (hs_ne_univ : s ≠ univ) (x y : β) : (piecewise s hs (const α x) (const α y)).range = {x, y} := begin ext1 z, rw [mem_range, set.mem_range, finset.mem_insert, finset.mem_singleton], simp_rw piecewise_apply, split; intro h, { obtain ⟨a, haz⟩ := h, by_cases has : a ∈ s, { left, simp only [has, function.const_apply, if_true, coe_const] at haz, exact haz.symm, }, { right, simp only [has, function.const_apply, if_false, coe_const] at haz, exact haz.symm, }, }, { cases h, { obtain ⟨a, has⟩ : ∃ a, a ∈ s, from hs_nonempty, exact ⟨a, by simpa [has] using h.symm⟩, }, { obtain ⟨a, has⟩ : ∃ a, a ∉ s, { by_contra, push_neg at h, refine hs_ne_univ _, ext1 a, simp [h a], }, exact ⟨a, by simpa [has] using h.symm⟩, }, }, end lemma measurable_bind [measurable_space γ] (f : α →ₛ β) (g : β → α → γ) (hg : ∀ b, measurable (g b)) : measurable (λ a, g (f a) a) := λ s hs, f.measurable_set_cut (λ a b, g b a ∈ s) $ λ b, hg b hs /-- If `f : α →ₛ β` is a simple function and `g : β → α →ₛ γ` is a family of simple functions, then `f.bind g` binds the first argument of `g` to `f`. In other words, `f.bind g a = g (f a) a`. -/ def bind (f : α →ₛ β) (g : β → α →ₛ γ) : α →ₛ γ := ⟨λa, g (f a) a, λ c, f.measurable_set_cut (λ a b, g b a = c) $ λ b, (g b).measurable_set_preimage {c}, (f.finite_range.bUnion (λ b _, (g b).finite_range)).subset $ by rintro _ ⟨a, rfl⟩; simp; exact ⟨a, a, rfl⟩⟩ @[simp] theorem bind_apply (f : α →ₛ β) (g : β → α →ₛ γ) (a) : f.bind g a = g (f a) a := rfl /-- Given a function `g : β → γ` and a simple function `f : α →ₛ β`, `f.map g` return the simple function `g ∘ f : α →ₛ γ` -/ def map (g : β → γ) (f : α →ₛ β) : α →ₛ γ := bind f (const α ∘ g) theorem map_apply (g : β → γ) (f : α →ₛ β) (a) : f.map g a = g (f a) := rfl theorem map_map (g : β → γ) (h: γ → δ) (f : α →ₛ β) : (f.map g).map h = f.map (h ∘ g) := rfl @[simp] theorem coe_map (g : β → γ) (f : α →ₛ β) : (f.map g : α → γ) = g ∘ f := rfl @[simp] theorem range_map [decidable_eq γ] (g : β → γ) (f : α →ₛ β) : (f.map g).range = f.range.image g := finset.coe_injective $ by simp [range_comp] @[simp] theorem map_const (g : β → γ) (b : β) : (const α b).map g = const α (g b) := rfl lemma map_preimage (f : α →ₛ β) (g : β → γ) (s : set γ) : (f.map g) ⁻¹' s = f ⁻¹' ↑(f.range.filter (λb, g b ∈ s)) := by { simp only [coe_range, sep_mem_eq, set.mem_range, function.comp_app, coe_map, finset.coe_filter, ← mem_preimage, inter_comm, preimage_inter_range], apply preimage_comp } lemma map_preimage_singleton (f : α →ₛ β) (g : β → γ) (c : γ) : (f.map g) ⁻¹' {c} = f ⁻¹' ↑(f.range.filter (λ b, g b = c)) := map_preimage _ _ _ /-- Composition of a `simple_fun` and a measurable function is a `simple_func`. -/ def comp [measurable_space β] (f : β →ₛ γ) (g : α → β) (hgm : measurable g) : α →ₛ γ := { to_fun := f ∘ g, finite_range' := f.finite_range.subset $ set.range_comp_subset_range _ _, measurable_set_fiber' := λ z, hgm (f.measurable_set_fiber z) } @[simp] lemma coe_comp [measurable_space β] (f : β →ₛ γ) {g : α → β} (hgm : measurable g) : ⇑(f.comp g hgm) = f ∘ g := rfl lemma range_comp_subset_range [measurable_space β] (f : β →ₛ γ) {g : α → β} (hgm : measurable g) : (f.comp g hgm).range ⊆ f.range := finset.coe_subset.1 $ by simp only [coe_range, coe_comp, set.range_comp_subset_range] /-- If `f` is a simple function taking values in `β → γ` and `g` is another simple function with the same domain and codomain `β`, then `f.seq g = f a (g a)`. -/ def seq (f : α →ₛ (β → γ)) (g : α →ₛ β) : α →ₛ γ := f.bind (λf, g.map f) @[simp] lemma seq_apply (f : α →ₛ (β → γ)) (g : α →ₛ β) (a : α) : f.seq g a = f a (g a) := rfl /-- Combine two simple functions `f : α →ₛ β` and `g : α →ₛ β` into `λ a, (f a, g a)`. -/ def pair (f : α →ₛ β) (g : α →ₛ γ) : α →ₛ (β × γ) := (f.map prod.mk).seq g @[simp] lemma pair_apply (f : α →ₛ β) (g : α →ₛ γ) (a) : pair f g a = (f a, g a) := rfl lemma pair_preimage (f : α →ₛ β) (g : α →ₛ γ) (s : set β) (t : set γ) : (pair f g) ⁻¹' (set.prod s t) = (f ⁻¹' s) ∩ (g ⁻¹' t) := rfl /- A special form of `pair_preimage` -/ lemma pair_preimage_singleton (f : α →ₛ β) (g : α →ₛ γ) (b : β) (c : γ) : (pair f g) ⁻¹' {(b, c)} = (f ⁻¹' {b}) ∩ (g ⁻¹' {c}) := by { rw ← singleton_prod_singleton, exact pair_preimage _ _ _ _ } theorem bind_const (f : α →ₛ β) : f.bind (const α) = f := by ext; simp instance [has_zero β] : has_zero (α →ₛ β) := ⟨const α 0⟩ instance [has_add β] : has_add (α →ₛ β) := ⟨λf g, (f.map (+)).seq g⟩ instance [has_mul β] : has_mul (α →ₛ β) := ⟨λf g, (f.map (*)).seq g⟩ instance [has_sup β] : has_sup (α →ₛ β) := ⟨λf g, (f.map (⊔)).seq g⟩ instance [has_inf β] : has_inf (α →ₛ β) := ⟨λf g, (f.map (⊓)).seq g⟩ instance [has_le β] : has_le (α →ₛ β) := ⟨λf g, ∀a, f a ≤ g a⟩ @[simp, norm_cast] lemma coe_zero [has_zero β] : ⇑(0 : α →ₛ β) = 0 := rfl @[simp] lemma const_zero [has_zero β] : const α (0:β) = 0 := rfl @[simp, norm_cast] lemma coe_add [has_add β] (f g : α →ₛ β) : ⇑(f + g) = f + g := rfl @[simp, norm_cast] lemma coe_mul [has_mul β] (f g : α →ₛ β) : ⇑(f * g) = f * g := rfl @[simp, norm_cast] lemma coe_le [preorder β] {f g : α →ₛ β} : (f : α → β) ≤ g ↔ f ≤ g := iff.rfl @[simp] lemma range_zero [nonempty α] [has_zero β] : (0 : α →ₛ β).range = {0} := finset.ext $ λ x, by simp [eq_comm] @[simp] lemma range_eq_empty_of_is_empty {β} [hα : is_empty α] (f : α →ₛ β) : f.range = ∅ := begin rw ← finset.not_nonempty_iff_eq_empty, by_contra, obtain ⟨y, hy_mem⟩ := h, rw [simple_func.mem_range, set.mem_range] at hy_mem, obtain ⟨x, hxy⟩ := hy_mem, rw is_empty_iff at hα, exact hα x, end lemma eq_zero_of_mem_range_zero [has_zero β] : ∀ {y : β}, y ∈ (0 : α →ₛ β).range → y = 0 := forall_range_iff.2 $ λ x, rfl lemma sup_apply [has_sup β] (f g : α →ₛ β) (a : α) : (f ⊔ g) a = f a ⊔ g a := rfl lemma mul_apply [has_mul β] (f g : α →ₛ β) (a : α) : (f * g) a = f a * g a := rfl lemma add_apply [has_add β] (f g : α →ₛ β) (a : α) : (f + g) a = f a + g a := rfl lemma add_eq_map₂ [has_add β] (f g : α →ₛ β) : f + g = (pair f g).map (λp:β×β, p.1 + p.2) := rfl lemma mul_eq_map₂ [has_mul β] (f g : α →ₛ β) : f * g = (pair f g).map (λp:β×β, p.1 * p.2) := rfl lemma sup_eq_map₂ [has_sup β] (f g : α →ₛ β) : f ⊔ g = (pair f g).map (λp:β×β, p.1 ⊔ p.2) := rfl lemma const_mul_eq_map [has_mul β] (f : α →ₛ β) (b : β) : const α b * f = f.map (λa, b * a) := rfl theorem map_add [has_add β] [has_add γ] {g : β → γ} (hg : ∀ x y, g (x + y) = g x + g y) (f₁ f₂ : α →ₛ β) : (f₁ + f₂).map g = f₁.map g + f₂.map g := ext $ λ x, hg _ _ instance [add_monoid β] : add_monoid (α →ₛ β) := function.injective.add_monoid (λ f, show α → β, from f) coe_injective coe_zero coe_add instance add_comm_monoid [add_comm_monoid β] : add_comm_monoid (α →ₛ β) := function.injective.add_comm_monoid (λ f, show α → β, from f) coe_injective coe_zero coe_add instance [has_neg β] : has_neg (α →ₛ β) := ⟨λf, f.map (has_neg.neg)⟩ @[simp, norm_cast] lemma coe_neg [has_neg β] (f : α →ₛ β) : ⇑(-f) = -f := rfl instance [has_sub β] : has_sub (α →ₛ β) := ⟨λf g, (f.map (has_sub.sub)).seq g⟩ @[simp, norm_cast] lemma coe_sub [has_sub β] (f g : α →ₛ β) : ⇑(f - g) = f - g := rfl lemma sub_apply [has_sub β] (f g : α →ₛ β) (x : α) : (f - g) x = f x - g x := rfl instance [add_group β] : add_group (α →ₛ β) := function.injective.add_group (λ f, show α → β, from f) coe_injective coe_zero coe_add coe_neg coe_sub instance [add_comm_group β] : add_comm_group (α →ₛ β) := function.injective.add_comm_group (λ f, show α → β, from f) coe_injective coe_zero coe_add coe_neg coe_sub variables {K : Type*} instance [has_scalar K β] : has_scalar K (α →ₛ β) := ⟨λk f, f.map ((•) k)⟩ @[simp] lemma coe_smul [has_scalar K β] (c : K) (f : α →ₛ β) : ⇑(c • f) = c • f := rfl lemma smul_apply [has_scalar K β] (k : K) (f : α →ₛ β) (a : α) : (k • f) a = k • f a := rfl instance [semiring K] [add_comm_monoid β] [module K β] : module K (α →ₛ β) := function.injective.module K ⟨λ f, show α → β, from f, coe_zero, coe_add⟩ coe_injective coe_smul lemma smul_eq_map [has_scalar K β] (k : K) (f : α →ₛ β) : k • f = f.map ((•) k) := rfl instance [preorder β] : preorder (α →ₛ β) := { le_refl := λf a, le_refl _, le_trans := λf g h hfg hgh a, le_trans (hfg _) (hgh a), .. simple_func.has_le } instance [partial_order β] : partial_order (α →ₛ β) := { le_antisymm := assume f g hfg hgf, ext $ assume a, le_antisymm (hfg a) (hgf a), .. simple_func.preorder } instance [order_bot β] : order_bot (α →ₛ β) := { bot := const α ⊥, bot_le := λf a, bot_le, .. simple_func.partial_order } instance [order_top β] : order_top (α →ₛ β) := { top := const α ⊤, le_top := λf a, le_top, .. simple_func.partial_order } instance [semilattice_inf β] : semilattice_inf (α →ₛ β) := { inf := (⊓), inf_le_left := assume f g a, inf_le_left, inf_le_right := assume f g a, inf_le_right, le_inf := assume f g h hfh hgh a, le_inf (hfh a) (hgh a), .. simple_func.partial_order } instance [semilattice_sup β] : semilattice_sup (α →ₛ β) := { sup := (⊔), le_sup_left := assume f g a, le_sup_left, le_sup_right := assume f g a, le_sup_right, sup_le := assume f g h hfh hgh a, sup_le (hfh a) (hgh a), .. simple_func.partial_order } instance [semilattice_sup_bot β] : semilattice_sup_bot (α →ₛ β) := { .. simple_func.semilattice_sup,.. simple_func.order_bot } instance [lattice β] : lattice (α →ₛ β) := { .. simple_func.semilattice_sup,.. simple_func.semilattice_inf } instance [bounded_lattice β] : bounded_lattice (α →ₛ β) := { .. simple_func.lattice, .. simple_func.order_bot, .. simple_func.order_top } lemma finset_sup_apply [semilattice_sup_bot β] {f : γ → α →ₛ β} (s : finset γ) (a : α) : s.sup f a = s.sup (λc, f c a) := begin refine finset.induction_on s rfl _, assume a s hs ih, rw [finset.sup_insert, finset.sup_insert, sup_apply, ih] end section restrict variables [has_zero β] /-- Restrict a simple function `f : α →ₛ β` to a set `s`. If `s` is measurable, then `f.restrict s a = if a ∈ s then f a else 0`, otherwise `f.restrict s = const α 0`. -/ def restrict (f : α →ₛ β) (s : set α) : α →ₛ β := if hs : measurable_set s then piecewise s hs f 0 else 0 theorem restrict_of_not_measurable {f : α →ₛ β} {s : set α} (hs : ¬measurable_set s) : restrict f s = 0 := dif_neg hs @[simp] theorem coe_restrict (f : α →ₛ β) {s : set α} (hs : measurable_set s) : ⇑(restrict f s) = indicator s f := by { rw [restrict, dif_pos hs], refl } @[simp] theorem restrict_univ (f : α →ₛ β) : restrict f univ = f := by simp [restrict] @[simp] theorem restrict_empty (f : α →ₛ β) : restrict f ∅ = 0 := by simp [restrict] theorem map_restrict_of_zero [has_zero γ] {g : β → γ} (hg : g 0 = 0) (f : α →ₛ β) (s : set α) : (f.restrict s).map g = (f.map g).restrict s := ext $ λ x, if hs : measurable_set s then by simp [hs, set.indicator_comp_of_zero hg] else by simp [restrict_of_not_measurable hs, hg] theorem map_coe_ennreal_restrict (f : α →ₛ ℝ≥0) (s : set α) : (f.restrict s).map (coe : ℝ≥0 → ℝ≥0∞) = (f.map coe).restrict s := map_restrict_of_zero ennreal.coe_zero _ _ theorem map_coe_nnreal_restrict (f : α →ₛ ℝ≥0) (s : set α) : (f.restrict s).map (coe : ℝ≥0 → ℝ) = (f.map coe).restrict s := map_restrict_of_zero nnreal.coe_zero _ _ theorem restrict_apply (f : α →ₛ β) {s : set α} (hs : measurable_set s) (a) : restrict f s a = indicator s f a := by simp only [f.coe_restrict hs] theorem restrict_preimage (f : α →ₛ β) {s : set α} (hs : measurable_set s) {t : set β} (ht : (0:β) ∉ t) : restrict f s ⁻¹' t = s ∩ f ⁻¹' t := by simp [hs, indicator_preimage_of_not_mem _ _ ht, inter_comm] theorem restrict_preimage_singleton (f : α →ₛ β) {s : set α} (hs : measurable_set s) {r : β} (hr : r ≠ 0) : restrict f s ⁻¹' {r} = s ∩ f ⁻¹' {r} := f.restrict_preimage hs hr.symm lemma mem_restrict_range {r : β} {s : set α} {f : α →ₛ β} (hs : measurable_set s) : r ∈ (restrict f s).range ↔ (r = 0 ∧ s ≠ univ) ∨ (r ∈ f '' s) := by rw [← finset.mem_coe, coe_range, coe_restrict _ hs, mem_range_indicator] lemma mem_image_of_mem_range_restrict {r : β} {s : set α} {f : α →ₛ β} (hr : r ∈ (restrict f s).range) (h0 : r ≠ 0) : r ∈ f '' s := if hs : measurable_set s then by simpa [mem_restrict_range hs, h0] using hr else by { rw [restrict_of_not_measurable hs] at hr, exact (h0 $ eq_zero_of_mem_range_zero hr).elim } @[mono] lemma restrict_mono [preorder β] (s : set α) {f g : α →ₛ β} (H : f ≤ g) : f.restrict s ≤ g.restrict s := if hs : measurable_set s then λ x, by simp only [coe_restrict _ hs, indicator_le_indicator (H x)] else by simp only [restrict_of_not_measurable hs, le_refl] end restrict section approx section variables [semilattice_sup_bot β] [has_zero β] /-- Fix a sequence `i : ℕ → β`. Given a function `α → β`, its `n`-th approximation by simple functions is defined so that in case `β = ℝ≥0∞` it sends each `a` to the supremum of the set `{i k | k ≤ n ∧ i k ≤ f a}`, see `approx_apply` and `supr_approx_apply` for details. -/ def approx (i : ℕ → β) (f : α → β) (n : ℕ) : α →ₛ β := (finset.range n).sup (λk, restrict (const α (i k)) {a:α | i k ≤ f a}) lemma approx_apply [topological_space β] [order_closed_topology β] [measurable_space β] [opens_measurable_space β] {i : ℕ → β} {f : α → β} {n : ℕ} (a : α) (hf : measurable f) : (approx i f n : α →ₛ β) a = (finset.range n).sup (λk, if i k ≤ f a then i k else 0) := begin dsimp only [approx], rw [finset_sup_apply], congr, funext k, rw [restrict_apply], refl, exact (hf measurable_set_Ici) end lemma monotone_approx (i : ℕ → β) (f : α → β) : monotone (approx i f) := assume n m h, finset.sup_mono $ finset.range_subset.2 h lemma approx_comp [topological_space β] [order_closed_topology β] [measurable_space β] [opens_measurable_space β] [measurable_space γ] {i : ℕ → β} {f : γ → β} {g : α → γ} {n : ℕ} (a : α) (hf : measurable f) (hg : measurable g) : (approx i (f ∘ g) n : α →ₛ β) a = (approx i f n : γ →ₛ β) (g a) := by rw [approx_apply _ hf, approx_apply _ (hf.comp hg)] end lemma supr_approx_apply [topological_space β] [complete_lattice β] [order_closed_topology β] [has_zero β] [measurable_space β] [opens_measurable_space β] (i : ℕ → β) (f : α → β) (a : α) (hf : measurable f) (h_zero : (0 : β) = ⊥) : (⨆n, (approx i f n : α →ₛ β) a) = (⨆k (h : i k ≤ f a), i k) := begin refine le_antisymm (supr_le $ assume n, _) (supr_le $ assume k, supr_le $ assume hk, _), { rw [approx_apply a hf, h_zero], refine finset.sup_le (assume k hk, _), split_ifs, exact le_supr_of_le k (le_supr _ h), exact bot_le }, { refine le_supr_of_le (k+1) _, rw [approx_apply a hf], have : k ∈ finset.range (k+1) := finset.mem_range.2 (nat.lt_succ_self _), refine le_trans (le_of_eq _) (finset.le_sup this), rw [if_pos hk] } end end approx section eapprox /-- A sequence of `ℝ≥0∞`s such that its range is the set of non-negative rational numbers. -/ def ennreal_rat_embed (n : ℕ) : ℝ≥0∞ := ennreal.of_real ((encodable.decode ℚ n).get_or_else (0 : ℚ)) lemma ennreal_rat_embed_encode (q : ℚ) : ennreal_rat_embed (encodable.encode q) = real.to_nnreal q := by rw [ennreal_rat_embed, encodable.encodek]; refl /-- Approximate a function `α → ℝ≥0∞` by a sequence of simple functions. -/ def eapprox : (α → ℝ≥0∞) → ℕ → α →ₛ ℝ≥0∞ := approx ennreal_rat_embed lemma eapprox_lt_top (f : α → ℝ≥0∞) (n : ℕ) (a : α) : eapprox f n a < ∞ := begin simp only [eapprox, approx, finset_sup_apply, finset.sup_lt_iff, with_top.zero_lt_top, finset.mem_range, ennreal.bot_eq_zero, restrict], assume b hb, split_ifs, { simp only [coe_zero, coe_piecewise, piecewise_eq_indicator, coe_const], calc {a : α | ennreal_rat_embed b ≤ f a}.indicator (λ x, ennreal_rat_embed b) a ≤ ennreal_rat_embed b : indicator_le_self _ _ a ... < ⊤ : ennreal.coe_lt_top }, { exact with_top.zero_lt_top }, end @[mono] lemma monotone_eapprox (f : α → ℝ≥0∞) : monotone (eapprox f) := monotone_approx _ f lemma supr_eapprox_apply (f : α → ℝ≥0∞) (hf : measurable f) (a : α) : (⨆n, (eapprox f n : α →ₛ ℝ≥0∞) a) = f a := begin rw [eapprox, supr_approx_apply ennreal_rat_embed f a hf rfl], refine le_antisymm (supr_le $ assume i, supr_le $ assume hi, hi) (le_of_not_gt _), assume h, rcases ennreal.lt_iff_exists_rat_btwn.1 h with ⟨q, hq, lt_q, q_lt⟩, have : (real.to_nnreal q : ℝ≥0∞) ≤ (⨆ (k : ℕ) (h : ennreal_rat_embed k ≤ f a), ennreal_rat_embed k), { refine le_supr_of_le (encodable.encode q) _, rw [ennreal_rat_embed_encode q], refine le_supr_of_le (le_of_lt q_lt) _, exact le_refl _ }, exact lt_irrefl _ (lt_of_le_of_lt this lt_q) end lemma eapprox_comp [measurable_space γ] {f : γ → ℝ≥0∞} {g : α → γ} {n : ℕ} (hf : measurable f) (hg : measurable g) : (eapprox (f ∘ g) n : α → ℝ≥0∞) = (eapprox f n : γ →ₛ ℝ≥0∞) ∘ g := funext $ assume a, approx_comp a hf hg /-- Approximate a function `α → ℝ≥0∞` by a series of simple functions taking their values in `ℝ≥0`. -/ def eapprox_diff (f : α → ℝ≥0∞) : ∀ (n : ℕ), α →ₛ ℝ≥0 | 0 := (eapprox f 0).map ennreal.to_nnreal | (n+1) := (eapprox f (n+1) - eapprox f n).map ennreal.to_nnreal lemma sum_eapprox_diff (f : α → ℝ≥0∞) (n : ℕ) (a : α) : (∑ k in finset.range (n+1), (eapprox_diff f k a : ℝ≥0∞)) = eapprox f n a := begin induction n with n IH, { simp only [nat.nat_zero_eq_zero, finset.sum_singleton, finset.range_one], refl }, { rw [finset.sum_range_succ, nat.succ_eq_add_one, IH, eapprox_diff, coe_map, function.comp_app, coe_sub, pi.sub_apply, ennreal.coe_to_nnreal, add_tsub_cancel_of_le (monotone_eapprox f (nat.le_succ _) _)], apply (lt_of_le_of_lt _ (eapprox_lt_top f (n+1) a)).ne, rw tsub_le_iff_right, exact le_self_add }, end lemma tsum_eapprox_diff (f : α → ℝ≥0∞) (hf : measurable f) (a : α) : (∑' n, (eapprox_diff f n a : ℝ≥0∞)) = f a := by simp_rw [ennreal.tsum_eq_supr_nat' (tendsto_add_at_top_nat 1), sum_eapprox_diff, supr_eapprox_apply f hf a] end eapprox end measurable section measure variables {m : measurable_space α} {μ ν : measure α} /-- Integral of a simple function whose codomain is `ℝ≥0∞`. -/ def lintegral {m : measurable_space α} (f : α →ₛ ℝ≥0∞) (μ : measure α) : ℝ≥0∞ := ∑ x in f.range, x * μ (f ⁻¹' {x}) lemma lintegral_eq_of_subset (f : α →ₛ ℝ≥0∞) {s : finset ℝ≥0∞} (hs : ∀ x, f x ≠ 0 → μ (f ⁻¹' {f x}) ≠ 0 → f x ∈ s) : f.lintegral μ = ∑ x in s, x * μ (f ⁻¹' {x}) := begin refine finset.sum_bij_ne_zero (λr _ _, r) _ _ _ _, { simpa only [forall_range_iff, mul_ne_zero_iff, and_imp] }, { intros, assumption }, { intros b _ hb, refine ⟨b, _, hb, rfl⟩, rw [mem_range, ← preimage_singleton_nonempty], exact nonempty_of_measure_ne_zero (mul_ne_zero_iff.1 hb).2 }, { intros, refl } end lemma lintegral_eq_of_subset' (f : α →ₛ ℝ≥0∞) {s : finset ℝ≥0∞} (hs : f.range \ {0} ⊆ s) : f.lintegral μ = ∑ x in s, x * μ (f ⁻¹' {x}) := f.lintegral_eq_of_subset $ λ x hfx _, hs $ finset.mem_sdiff.2 ⟨f.mem_range_self x, mt finset.mem_singleton.1 hfx⟩ /-- Calculate the integral of `(g ∘ f)`, where `g : β → ℝ≥0∞` and `f : α →ₛ β`. -/ lemma map_lintegral (g : β → ℝ≥0∞) (f : α →ₛ β) : (f.map g).lintegral μ = ∑ x in f.range, g x * μ (f ⁻¹' {x}) := begin simp only [lintegral, range_map], refine finset.sum_image' _ (assume b hb, _), rcases mem_range.1 hb with ⟨a, rfl⟩, rw [map_preimage_singleton, ← f.sum_measure_preimage_singleton, finset.mul_sum], refine finset.sum_congr _ _, { congr }, { assume x, simp only [finset.mem_filter], rintro ⟨_, h⟩, rw h }, end lemma add_lintegral (f g : α →ₛ ℝ≥0∞) : (f + g).lintegral μ = f.lintegral μ + g.lintegral μ := calc (f + g).lintegral μ = ∑ x in (pair f g).range, (x.1 * μ (pair f g ⁻¹' {x}) + x.2 * μ (pair f g ⁻¹' {x})) : by rw [add_eq_map₂, map_lintegral]; exact finset.sum_congr rfl (assume a ha, add_mul _ _ _) ... = ∑ x in (pair f g).range, x.1 * μ (pair f g ⁻¹' {x}) + ∑ x in (pair f g).range, x.2 * μ (pair f g ⁻¹' {x}) : by rw [finset.sum_add_distrib] ... = ((pair f g).map prod.fst).lintegral μ + ((pair f g).map prod.snd).lintegral μ : by rw [map_lintegral, map_lintegral] ... = lintegral f μ + lintegral g μ : rfl lemma const_mul_lintegral (f : α →ₛ ℝ≥0∞) (x : ℝ≥0∞) : (const α x * f).lintegral μ = x * f.lintegral μ := calc (f.map (λa, x * a)).lintegral μ = ∑ r in f.range, x * r * μ (f ⁻¹' {r}) : map_lintegral _ _ ... = ∑ r in f.range, x * (r * μ (f ⁻¹' {r})) : finset.sum_congr rfl (assume a ha, mul_assoc _ _ _) ... = x * f.lintegral μ : finset.mul_sum.symm /-- Integral of a simple function `α →ₛ ℝ≥0∞` as a bilinear map. -/ def lintegralₗ {m : measurable_space α} : (α →ₛ ℝ≥0∞) →ₗ[ℝ≥0∞] measure α →ₗ[ℝ≥0∞] ℝ≥0∞ := { to_fun := λ f, { to_fun := lintegral f, map_add' := by simp [lintegral, mul_add, finset.sum_add_distrib], map_smul' := λ c μ, by simp [lintegral, mul_left_comm _ c, finset.mul_sum] }, map_add' := λ f g, linear_map.ext (λ μ, add_lintegral f g), map_smul' := λ c f, linear_map.ext (λ μ, const_mul_lintegral f c) } @[simp] lemma zero_lintegral : (0 : α →ₛ ℝ≥0∞).lintegral μ = 0 := linear_map.ext_iff.1 lintegralₗ.map_zero μ lemma lintegral_add {ν} (f : α →ₛ ℝ≥0∞) : f.lintegral (μ + ν) = f.lintegral μ + f.lintegral ν := (lintegralₗ f).map_add μ ν lemma lintegral_smul (f : α →ₛ ℝ≥0∞) (c : ℝ≥0∞) : f.lintegral (c • μ) = c • f.lintegral μ := (lintegralₗ f).map_smul c μ @[simp] lemma lintegral_zero [measurable_space α] (f : α →ₛ ℝ≥0∞) : f.lintegral 0 = 0 := (lintegralₗ f).map_zero lemma lintegral_sum {m : measurable_space α} {ι} (f : α →ₛ ℝ≥0∞) (μ : ι → measure α) : f.lintegral (measure.sum μ) = ∑' i, f.lintegral (μ i) := begin simp only [lintegral, measure.sum_apply, f.measurable_set_preimage, ← finset.tsum_subtype, ← ennreal.tsum_mul_left], apply ennreal.tsum_comm end lemma restrict_lintegral (f : α →ₛ ℝ≥0∞) {s : set α} (hs : measurable_set s) : (restrict f s).lintegral μ = ∑ r in f.range, r * μ (f ⁻¹' {r} ∩ s) := calc (restrict f s).lintegral μ = ∑ r in f.range, r * μ (restrict f s ⁻¹' {r}) : lintegral_eq_of_subset _ $ λ x hx, if hxs : x ∈ s then λ _, by simp only [f.restrict_apply hs, indicator_of_mem hxs, mem_range_self] else false.elim $ hx $ by simp [*] ... = ∑ r in f.range, r * μ (f ⁻¹' {r} ∩ s) : finset.sum_congr rfl $ forall_range_iff.2 $ λ b, if hb : f b = 0 then by simp only [hb, zero_mul] else by rw [restrict_preimage_singleton _ hs hb, inter_comm] lemma lintegral_restrict {m : measurable_space α} (f : α →ₛ ℝ≥0∞) (s : set α) (μ : measure α) : f.lintegral (μ.restrict s) = ∑ y in f.range, y * μ (f ⁻¹' {y} ∩ s) := by simp only [lintegral, measure.restrict_apply, f.measurable_set_preimage] lemma restrict_lintegral_eq_lintegral_restrict (f : α →ₛ ℝ≥0∞) {s : set α} (hs : measurable_set s) : (restrict f s).lintegral μ = f.lintegral (μ.restrict s) := by rw [f.restrict_lintegral hs, lintegral_restrict] lemma const_lintegral (c : ℝ≥0∞) : (const α c).lintegral μ = c * μ univ := begin rw [lintegral], casesI is_empty_or_nonempty α, { simp [μ.eq_zero_of_is_empty] }, { simp [preimage_const_of_mem] }, end lemma const_lintegral_restrict (c : ℝ≥0∞) (s : set α) : (const α c).lintegral (μ.restrict s) = c * μ s := by rw [const_lintegral, measure.restrict_apply measurable_set.univ, univ_inter] lemma restrict_const_lintegral (c : ℝ≥0∞) {s : set α} (hs : measurable_set s) : ((const α c).restrict s).lintegral μ = c * μ s := by rw [restrict_lintegral_eq_lintegral_restrict _ hs, const_lintegral_restrict] lemma le_sup_lintegral (f g : α →ₛ ℝ≥0∞) : f.lintegral μ ⊔ g.lintegral μ ≤ (f ⊔ g).lintegral μ := calc f.lintegral μ ⊔ g.lintegral μ = ((pair f g).map prod.fst).lintegral μ ⊔ ((pair f g).map prod.snd).lintegral μ : rfl ... ≤ ∑ x in (pair f g).range, (x.1 ⊔ x.2) * μ (pair f g ⁻¹' {x}) : begin rw [map_lintegral, map_lintegral], refine sup_le _ _; refine finset.sum_le_sum (λ a _, mul_le_mul_right' _ _), exact le_sup_left, exact le_sup_right end ... = (f ⊔ g).lintegral μ : by rw [sup_eq_map₂, map_lintegral] /-- `simple_func.lintegral` is monotone both in function and in measure. -/ @[mono] lemma lintegral_mono {f g : α →ₛ ℝ≥0∞} (hfg : f ≤ g) (hμν : μ ≤ ν) : f.lintegral μ ≤ g.lintegral ν := calc f.lintegral μ ≤ f.lintegral μ ⊔ g.lintegral μ : le_sup_left ... ≤ (f ⊔ g).lintegral μ : le_sup_lintegral _ _ ... = g.lintegral μ : by rw [sup_of_le_right hfg] ... ≤ g.lintegral ν : finset.sum_le_sum $ λ y hy, ennreal.mul_left_mono $ hμν _ (g.measurable_set_preimage _) /-- `simple_func.lintegral` depends only on the measures of `f ⁻¹' {y}`. -/ lemma lintegral_eq_of_measure_preimage [measurable_space β] {f : α →ₛ ℝ≥0∞} {g : β →ₛ ℝ≥0∞} {ν : measure β} (H : ∀ y, μ (f ⁻¹' {y}) = ν (g ⁻¹' {y})) : f.lintegral μ = g.lintegral ν := begin simp only [lintegral, ← H], apply lintegral_eq_of_subset, simp only [H], intros, exact mem_range_of_measure_ne_zero ‹_› end /-- If two simple functions are equal a.e., then their `lintegral`s are equal. -/ lemma lintegral_congr {f g : α →ₛ ℝ≥0∞} (h : f =ᵐ[μ] g) : f.lintegral μ = g.lintegral μ := lintegral_eq_of_measure_preimage $ λ y, measure_congr $ eventually.set_eq $ h.mono $ λ x hx, by simp [hx] lemma lintegral_map {β} [measurable_space β] {μ' : measure β} (f : α →ₛ ℝ≥0∞) (g : β →ₛ ℝ≥0∞) (m' : α → β) (eq : ∀ a, f a = g (m' a)) (h : ∀s, measurable_set s → μ' s = μ (m' ⁻¹' s)) : f.lintegral μ = g.lintegral μ' := lintegral_eq_of_measure_preimage $ λ y, by { simp only [preimage, eq], exact (h (g ⁻¹' {y}) (g.measurable_set_preimage _)).symm } /-- The `lintegral` of simple functions transforms appropriately under a measurable equivalence. (Compare `lintegral_map`, which applies to a broader class of transformations of the domain, but requires measurability of the function being integrated.) -/ lemma lintegral_map_equiv {β} [measurable_space β] (g : β →ₛ ℝ≥0∞) (m' : α ≃ᵐ β) : (g.comp m' m'.measurable).lintegral μ = g.lintegral (measure.map m' μ) := begin simp [simple_func.lintegral], have : (g.comp m' m'.measurable).range = g.range, { refine le_antisymm _ _, { exact g.range_comp_subset_range m'.measurable }, convert (g.comp m' m'.measurable).range_comp_subset_range m'.symm.measurable, apply simple_func.ext, intros a, exact congr_arg g (congr_fun m'.self_comp_symm.symm a) }, rw this, congr' 1, funext, rw [m'.map_apply (g ⁻¹' {x})], refl, end end measure section fin_meas_supp open finset function lemma support_eq [measurable_space α] [has_zero β] (f : α →ₛ β) : support f = ⋃ y ∈ f.range.filter (λ y, y ≠ 0), f ⁻¹' {y} := set.ext $ λ x, by simp only [finset.set_bUnion_preimage_singleton, mem_support, set.mem_preimage, finset.mem_coe, mem_filter, mem_range_self, true_and] variables {m : measurable_space α} [has_zero β] [has_zero γ] {μ : measure α} {f : α →ₛ β} lemma measurable_set_support [measurable_space α] (f : α →ₛ β) : measurable_set (support f) := by { rw f.support_eq, exact finset.measurable_set_bUnion _ (λ y hy, measurable_set_fiber _ _), } /-- A `simple_func` has finite measure support if it is equal to `0` outside of a set of finite measure. -/ protected def fin_meas_supp {m : measurable_space α} (f : α →ₛ β) (μ : measure α) : Prop := f =ᶠ[μ.cofinite] 0 lemma fin_meas_supp_iff_support : f.fin_meas_supp μ ↔ μ (support f) < ∞ := iff.rfl lemma fin_meas_supp_iff : f.fin_meas_supp μ ↔ ∀ y ≠ 0, μ (f ⁻¹' {y}) < ∞ := begin split, { refine λ h y hy, lt_of_le_of_lt (measure_mono _) h, exact λ x hx (H : f x = 0), hy $ H ▸ eq.symm hx }, { intro H, rw [fin_meas_supp_iff_support, support_eq], refine lt_of_le_of_lt (measure_bUnion_finset_le _ _) (sum_lt_top _), exact λ y hy, (H y (finset.mem_filter.1 hy).2).ne } end namespace fin_meas_supp lemma meas_preimage_singleton_ne_zero (h : f.fin_meas_supp μ) {y : β} (hy : y ≠ 0) : μ (f ⁻¹' {y}) < ∞ := fin_meas_supp_iff.1 h y hy protected lemma map {g : β → γ} (hf : f.fin_meas_supp μ) (hg : g 0 = 0) : (f.map g).fin_meas_supp μ := flip lt_of_le_of_lt hf (measure_mono $ support_comp_subset hg f) lemma of_map {g : β → γ} (h : (f.map g).fin_meas_supp μ) (hg : ∀b, g b = 0 → b = 0) : f.fin_meas_supp μ := flip lt_of_le_of_lt h $ measure_mono $ support_subset_comp hg _ lemma map_iff {g : β → γ} (hg : ∀ {b}, g b = 0 ↔ b = 0) : (f.map g).fin_meas_supp μ ↔ f.fin_meas_supp μ := ⟨λ h, h.of_map $ λ b, hg.1, λ h, h.map $ hg.2 rfl⟩ protected lemma pair {g : α →ₛ γ} (hf : f.fin_meas_supp μ) (hg : g.fin_meas_supp μ) : (pair f g).fin_meas_supp μ := calc μ (support $ pair f g) = μ (support f ∪ support g) : congr_arg μ $ support_prod_mk f g ... ≤ μ (support f) + μ (support g) : measure_union_le _ _ ... < _ : add_lt_top.2 ⟨hf, hg⟩ protected lemma map₂ [has_zero δ] (hf : f.fin_meas_supp μ) {g : α →ₛ γ} (hg : g.fin_meas_supp μ) {op : β → γ → δ} (H : op 0 0 = 0) : ((pair f g).map (function.uncurry op)).fin_meas_supp μ := (hf.pair hg).map H protected lemma add {β} [add_monoid β] {f g : α →ₛ β} (hf : f.fin_meas_supp μ) (hg : g.fin_meas_supp μ) : (f + g).fin_meas_supp μ := by { rw [add_eq_map₂], exact hf.map₂ hg (zero_add 0) } protected lemma mul {β} [monoid_with_zero β] {f g : α →ₛ β} (hf : f.fin_meas_supp μ) (hg : g.fin_meas_supp μ) : (f * g).fin_meas_supp μ := by { rw [mul_eq_map₂], exact hf.map₂ hg (zero_mul 0) } lemma lintegral_lt_top {f : α →ₛ ℝ≥0∞} (hm : f.fin_meas_supp μ) (hf : ∀ᵐ a ∂μ, f a ≠ ∞) : f.lintegral μ < ∞ := begin refine sum_lt_top (λ a ha, _), rcases eq_or_ne a ∞ with rfl|ha, { simp only [ae_iff, ne.def, not_not] at hf, simp [set.preimage, hf] }, { by_cases ha0 : a = 0, { subst a, rwa [zero_mul] }, { exact mul_ne_top ha (fin_meas_supp_iff.1 hm _ ha0).ne } } end lemma of_lintegral_ne_top {f : α →ₛ ℝ≥0∞} (h : f.lintegral μ ≠ ∞) : f.fin_meas_supp μ := begin refine fin_meas_supp_iff.2 (λ b hb, _), rw [f.lintegral_eq_of_subset' (finset.subset_insert b _)] at h, refine ennreal.lt_top_of_mul_ne_top_right _ hb, exact (lt_top_of_sum_ne_top h (finset.mem_insert_self _ _)).ne end lemma iff_lintegral_lt_top {f : α →ₛ ℝ≥0∞} (hf : ∀ᵐ a ∂μ, f a ≠ ∞) : f.fin_meas_supp μ ↔ f.lintegral μ < ∞ := ⟨λ h, h.lintegral_lt_top hf, λ h, of_lintegral_ne_top h.ne⟩ end fin_meas_supp end fin_meas_supp /-- To prove something for an arbitrary simple function, it suffices to show that the property holds for (multiples of) characteristic functions and is closed under addition (of functions with disjoint support). It is possible to make the hypotheses in `h_add` a bit stronger, and such conditions can be added once we need them (for example it is only necessary to consider the case where `g` is a multiple of a characteristic function, and that this multiple doesn't appear in the image of `f`) -/ @[elab_as_eliminator] protected lemma induction {α γ} [measurable_space α] [add_monoid γ] {P : simple_func α γ → Prop} (h_ind : ∀ c {s} (hs : measurable_set s), P (simple_func.piecewise s hs (simple_func.const _ c) (simple_func.const _ 0))) (h_add : ∀ ⦃f g : simple_func α γ⦄, disjoint (support f) (support g) → P f → P g → P (f + g)) (f : simple_func α γ) : P f := begin generalize' h : f.range \ {0} = s, rw [← finset.coe_inj, finset.coe_sdiff, finset.coe_singleton, simple_func.coe_range] at h, revert s f h, refine finset.induction _ _, { intros f hf, rw [finset.coe_empty, diff_eq_empty, range_subset_singleton] at hf, convert h_ind 0 measurable_set.univ, ext x, simp [hf] }, { intros x s hxs ih f hf, have mx := f.measurable_set_preimage {x}, let g := simple_func.piecewise (f ⁻¹' {x}) mx 0 f, have Pg : P g, { apply ih, simp only [g, simple_func.coe_piecewise, range_piecewise], rw [image_compl_preimage, union_diff_distrib, diff_diff_comm, hf, finset.coe_insert, insert_diff_self_of_not_mem, diff_eq_empty.mpr, set.empty_union], { rw [set.image_subset_iff], convert set.subset_univ _, exact preimage_const_of_mem (mem_singleton _) }, { rwa [finset.mem_coe] }}, convert h_add _ Pg (h_ind x mx), { ext1 y, by_cases hy : y ∈ f ⁻¹' {x}; [simpa [hy], simp [hy]] }, rintro y, by_cases hy : y ∈ f ⁻¹' {x}; simp [hy] } end end simple_func section lintegral open simple_func variables {m : measurable_space α} {μ ν : measure α} /-- The **lower Lebesgue integral** of a function `f` with respect to a measure `μ`. -/ def lintegral {m : measurable_space α} (μ : measure α) (f : α → ℝ≥0∞) : ℝ≥0∞ := ⨆ (g : α →ₛ ℝ≥0∞) (hf : ⇑g ≤ f), g.lintegral μ /-! 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 := lintegral μ r notation `∫⁻` binders `, ` r:(scoped:60 f, lintegral volume f) := r notation `∫⁻` binders ` in ` s `, ` r:(scoped:60 f, f) ` ∂` μ:70 := lintegral (measure.restrict μ s) r notation `∫⁻` binders ` in ` s `, ` r:(scoped:60 f, lintegral (measure.restrict volume s) f) := r theorem simple_func.lintegral_eq_lintegral {m : measurable_space α} (f : α →ₛ ℝ≥0∞) (μ : measure α) : ∫⁻ a, f a ∂ μ = f.lintegral μ := le_antisymm (bsupr_le $ λ g hg, lintegral_mono hg $ le_refl _) (le_supr_of_le f $ le_supr_of_le (le_refl _) (le_refl _)) @[mono] lemma lintegral_mono' {m : measurable_space α} ⦃μ ν : measure α⦄ (hμν : μ ≤ ν) ⦃f g : α → ℝ≥0∞⦄ (hfg : f ≤ g) : ∫⁻ a, f a ∂μ ≤ ∫⁻ a, g a ∂ν := supr_le_supr $ λ φ, supr_le_supr2 $ λ hφ, ⟨le_trans hφ hfg, lintegral_mono (le_refl φ) hμν⟩ lemma lintegral_mono ⦃f g : α → ℝ≥0∞⦄ (hfg : f ≤ g) : ∫⁻ a, f a ∂μ ≤ ∫⁻ a, g a ∂μ := lintegral_mono' (le_refl μ) hfg lemma lintegral_mono_nnreal {f g : α → ℝ≥0} (h : f ≤ g) : ∫⁻ a, f a ∂μ ≤ ∫⁻ a, g a ∂μ := begin refine lintegral_mono _, intro a, rw ennreal.coe_le_coe, exact h a, end lemma lintegral_mono_set {m : measurable_space α} ⦃μ : measure α⦄ {s t : set α} {f : α → ℝ≥0∞} (hst : s ⊆ t) : ∫⁻ x in s, f x ∂μ ≤ ∫⁻ x in t, f x ∂μ := lintegral_mono' (measure.restrict_mono hst (le_refl μ)) (le_refl f) lemma lintegral_mono_set' {m : measurable_space α} ⦃μ : measure α⦄ {s t : set α} {f : α → ℝ≥0∞} (hst : s ≤ᵐ[μ] t) : ∫⁻ x in s, f x ∂μ ≤ ∫⁻ x in t, f x ∂μ := lintegral_mono' (measure.restrict_mono' hst (le_refl μ)) (le_refl f) lemma monotone_lintegral {m : measurable_space α} (μ : measure α) : monotone (lintegral μ) := lintegral_mono @[simp] lemma lintegral_const (c : ℝ≥0∞) : ∫⁻ a, c ∂μ = c * μ univ := by rw [← simple_func.const_lintegral, ← simple_func.lintegral_eq_lintegral, simple_func.coe_const] @[simp] lemma lintegral_one : ∫⁻ a, (1 : ℝ≥0∞) ∂μ = μ univ := by rw [lintegral_const, one_mul] lemma set_lintegral_const (s : set α) (c : ℝ≥0∞) : ∫⁻ a in s, c ∂μ = c * μ s := by rw [lintegral_const, measure.restrict_apply_univ] lemma set_lintegral_one (s) : ∫⁻ a in s, 1 ∂μ = μ s := by rw [set_lintegral_const, one_mul] /-- `∫⁻ a in s, f a ∂μ` is defined as the supremum of integrals of simple functions `φ : α →ₛ ℝ≥0∞` such that `φ ≤ f`. This lemma says that it suffices to take functions `φ : α →ₛ ℝ≥0`. -/ lemma lintegral_eq_nnreal {m : measurable_space α} (f : α → ℝ≥0∞) (μ : measure α) : (∫⁻ a, f a ∂μ) = (⨆ (φ : α →ₛ ℝ≥0) (hf : ∀ x, ↑(φ x) ≤ f x), (φ.map (coe : ℝ≥0 → ℝ≥0∞)).lintegral μ) := begin refine le_antisymm (bsupr_le $ assume φ hφ, _) (supr_le_supr2 $ λ φ, ⟨φ.map (coe : ℝ≥0 → ℝ≥0∞), le_refl _⟩), by_cases h : ∀ᵐ a ∂μ, φ a ≠ ∞, { let ψ := φ.map ennreal.to_nnreal, replace h : ψ.map (coe : ℝ≥0 → ℝ≥0∞) =ᵐ[μ] φ := h.mono (λ a, ennreal.coe_to_nnreal), have : ∀ x, ↑(ψ x) ≤ f x := λ x, le_trans ennreal.coe_to_nnreal_le_self (hφ x), exact le_supr_of_le (φ.map ennreal.to_nnreal) (le_supr_of_le this (ge_of_eq $ lintegral_congr h)) }, { have h_meas : μ (φ ⁻¹' {∞}) ≠ 0, from mt measure_zero_iff_ae_nmem.1 h, refine le_trans le_top (ge_of_eq $ (supr_eq_top _).2 $ λ b hb, _), obtain ⟨n, hn⟩ : ∃ n : ℕ, b < n * μ (φ ⁻¹' {∞}), from exists_nat_mul_gt h_meas (ne_of_lt hb), use (const α (n : ℝ≥0)).restrict (φ ⁻¹' {∞}), simp only [lt_supr_iff, exists_prop, coe_restrict, φ.measurable_set_preimage, coe_const, ennreal.coe_indicator, map_coe_ennreal_restrict, map_const, ennreal.coe_nat, restrict_const_lintegral], refine ⟨indicator_le (λ x hx, le_trans _ (hφ _)), hn⟩, simp only [mem_preimage, mem_singleton_iff] at hx, simp only [hx, le_top] } end lemma exists_simple_func_forall_lintegral_sub_lt_of_pos {f : α → ℝ≥0∞} (h : ∫⁻ x, f x ∂μ ≠ ∞) {ε : ℝ≥0∞} (hε : ε ≠ 0) : ∃ φ : α →ₛ ℝ≥0, (∀ x, ↑(φ x) ≤ f x) ∧ ∀ ψ : α →ₛ ℝ≥0, (∀ x, ↑(ψ x) ≤ f x) → (map coe (ψ - φ)).lintegral μ < ε := begin rw lintegral_eq_nnreal at h, have := ennreal.lt_add_right h hε, erw ennreal.bsupr_add at this; [skip, exact ⟨0, λ x, by simp⟩], simp_rw [lt_supr_iff, supr_lt_iff, supr_le_iff] at this, rcases this with ⟨φ, hle : ∀ x, ↑(φ x) ≤ f x, b, hbφ, hb⟩, refine ⟨φ, hle, λ ψ hψ, _⟩, have : (map coe φ).lintegral μ ≠ ∞, from ne_top_of_le_ne_top h (le_bsupr φ hle), rw [← add_lt_add_iff_left this, ← add_lintegral, ← map_add @ennreal.coe_add], refine (hb _ (λ x, le_trans _ (max_le (hle x) (hψ x)))).trans_lt hbφ, norm_cast, simp only [add_apply, sub_apply, add_tsub_eq_max] end theorem supr_lintegral_le {ι : Sort*} (f : ι → α → ℝ≥0∞) : (⨆i, ∫⁻ a, f i a ∂μ) ≤ (∫⁻ a, ⨆i, f i a ∂μ) := begin simp only [← supr_apply], exact (monotone_lintegral μ).le_map_supr end theorem supr2_lintegral_le {ι : Sort*} {ι' : ι → Sort*} (f : Π i, ι' i → α → ℝ≥0∞) : (⨆i (h : ι' i), ∫⁻ a, f i h a ∂μ) ≤ (∫⁻ a, ⨆i (h : ι' i), f i h a ∂μ) := by { convert (monotone_lintegral μ).le_map_supr2 f, ext1 a, simp only [supr_apply] } theorem le_infi_lintegral {ι : Sort*} (f : ι → α → ℝ≥0∞) : (∫⁻ a, ⨅i, f i a ∂μ) ≤ (⨅i, ∫⁻ a, f i a ∂μ) := by { simp only [← infi_apply], exact (monotone_lintegral μ).map_infi_le } theorem le_infi2_lintegral {ι : Sort*} {ι' : ι → Sort*} (f : Π i, ι' i → α → ℝ≥0∞) : (∫⁻ a, ⨅ i (h : ι' i), f i h a ∂μ) ≤ (⨅ i (h : ι' i), ∫⁻ a, f i h a ∂μ) := by { convert (monotone_lintegral μ).map_infi2_le f, ext1 a, simp only [infi_apply] } lemma lintegral_mono_ae {f g : α → ℝ≥0∞} (h : ∀ᵐ a ∂μ, f a ≤ g a) : (∫⁻ a, f a ∂μ) ≤ (∫⁻ a, g a ∂μ) := begin rcases exists_measurable_superset_of_null h with ⟨t, hts, ht, ht0⟩, have : ∀ᵐ x ∂μ, x ∉ t := measure_zero_iff_ae_nmem.1 ht0, refine (supr_le $ assume s, supr_le $ assume hfs, le_supr_of_le (s.restrict tᶜ) $ le_supr_of_le _ _), { assume a, by_cases a ∈ t; simp [h, restrict_apply, ht.compl], exact le_trans (hfs a) (by_contradiction $ assume hnfg, h (hts hnfg)) }, { refine le_of_eq (simple_func.lintegral_congr $ this.mono $ λ a hnt, _), by_cases hat : a ∈ t; simp [hat, ht.compl], exact (hnt hat).elim } end lemma set_lintegral_mono_ae {s : set α} {f g : α → ℝ≥0∞} (hf : measurable f) (hg : measurable g) (hfg : ∀ᵐ x ∂μ, x ∈ s → f x ≤ g x) : ∫⁻ x in s, f x ∂μ ≤ ∫⁻ x in s, g x ∂μ := lintegral_mono_ae $ (ae_restrict_iff $ measurable_set_le hf hg).2 hfg lemma set_lintegral_mono {s : set α} {f g : α → ℝ≥0∞} (hf : measurable f) (hg : measurable g) (hfg : ∀ x ∈ s, f x ≤ g x) : ∫⁻ x in s, f x ∂μ ≤ ∫⁻ x in s, g x ∂μ := set_lintegral_mono_ae hf hg (ae_of_all _ hfg) lemma lintegral_congr_ae {f g : α → ℝ≥0∞} (h : f =ᵐ[μ] g) : (∫⁻ a, f a ∂μ) = (∫⁻ a, g a ∂μ) := le_antisymm (lintegral_mono_ae $ h.le) (lintegral_mono_ae $ h.symm.le) lemma lintegral_congr {f g : α → ℝ≥0∞} (h : ∀ a, f a = g a) : (∫⁻ a, f a ∂μ) = (∫⁻ a, g a ∂μ) := by simp only [h] lemma set_lintegral_congr {f : α → ℝ≥0∞} {s t : set α} (h : s =ᵐ[μ] t) : ∫⁻ x in s, f x ∂μ = ∫⁻ x in t, f x ∂μ := by rw [restrict_congr_set h] lemma set_lintegral_congr_fun {f g : α → ℝ≥0∞} {s : set α} (hs : measurable_set s) (hfg : ∀ᵐ x ∂μ, x ∈ s → f x = g x) : ∫⁻ x in s, f x ∂μ = ∫⁻ x in s, g x ∂μ := by { rw lintegral_congr_ae, rw eventually_eq, rwa ae_restrict_iff' hs, } /-- Monotone convergence theorem -- sometimes called Beppo-Levi convergence. See `lintegral_supr_directed` for a more general form. -/ theorem lintegral_supr {f : ℕ → α → ℝ≥0∞} (hf : ∀n, measurable (f n)) (h_mono : monotone f) : (∫⁻ a, ⨆n, f n a ∂μ) = (⨆n, ∫⁻ a, f n a ∂μ) := begin set c : ℝ≥0 → ℝ≥0∞ := coe, set F := λ a:α, ⨆n, f n a, have hF : measurable F := measurable_supr hf, refine le_antisymm _ (supr_lintegral_le _), rw [lintegral_eq_nnreal], refine supr_le (assume s, supr_le (assume hsf, _)), refine ennreal.le_of_forall_lt_one_mul_le (assume a ha, _), rcases ennreal.lt_iff_exists_coe.1 ha with ⟨r, rfl, ha⟩, have ha : r < 1 := ennreal.coe_lt_coe.1 ha, let rs := s.map (λa, r * a), have eq_rs : (const α r : α →ₛ ℝ≥0∞) * map c s = rs.map c, { ext1 a, exact ennreal.coe_mul.symm }, have eq : ∀p, (rs.map c) ⁻¹' {p} = (⋃n, (rs.map c) ⁻¹' {p} ∩ {a | p ≤ f n a}), { assume p, rw [← inter_Union, ← inter_univ ((map c rs) ⁻¹' {p})] {occs := occurrences.pos [1]}, refine set.ext (assume x, and_congr_right $ assume hx, (true_iff _).2 _), by_cases p_eq : p = 0, { simp [p_eq] }, simp at hx, subst hx, have : r * s x ≠ 0, { rwa [(≠), ← ennreal.coe_eq_zero] }, have : s x ≠ 0, { refine mt _ this, assume h, rw [h, mul_zero] }, have : (rs.map c) x < ⨆ (n : ℕ), f n x, { refine lt_of_lt_of_le (ennreal.coe_lt_coe.2 (_)) (hsf x), suffices : r * s x < 1 * s x, simpa [rs], exact mul_lt_mul_of_pos_right ha (pos_iff_ne_zero.2 this) }, rcases lt_supr_iff.1 this with ⟨i, hi⟩, exact mem_Union.2 ⟨i, le_of_lt hi⟩ }, have mono : ∀r:ℝ≥0∞, monotone (λn, (rs.map c) ⁻¹' {r} ∩ {a | r ≤ f n a}), { assume r i j h, refine inter_subset_inter (subset.refl _) _, assume x hx, exact le_trans hx (h_mono h x) }, have h_meas : ∀n, measurable_set {a : α | ⇑(map c rs) a ≤ f n a} := assume n, measurable_set_le (simple_func.measurable _) (hf n), calc (r:ℝ≥0∞) * (s.map c).lintegral μ = ∑ r in (rs.map c).range, r * μ ((rs.map c) ⁻¹' {r}) : by rw [← const_mul_lintegral, eq_rs, simple_func.lintegral] ... ≤ ∑ r in (rs.map c).range, r * μ (⋃n, (rs.map c) ⁻¹' {r} ∩ {a | r ≤ f n a}) : le_of_eq (finset.sum_congr rfl $ assume x hx, by rw ← eq) ... ≤ ∑ r in (rs.map c).range, (⨆n, r * μ ((rs.map c) ⁻¹' {r} ∩ {a | r ≤ f n a})) : le_of_eq (finset.sum_congr rfl $ assume x hx, begin rw [measure_Union_eq_supr _ (directed_of_sup $ mono x), ennreal.mul_supr], { assume i, refine ((rs.map c).measurable_set_preimage _).inter _, exact hf i measurable_set_Ici } end) ... ≤ ⨆n, ∑ r in (rs.map c).range, r * μ ((rs.map c) ⁻¹' {r} ∩ {a | r ≤ f n a}) : begin refine le_of_eq _, rw [ennreal.finset_sum_supr_nat], assume p i j h, exact mul_le_mul_left' (measure_mono $ mono p h) _ end ... ≤ (⨆n:ℕ, ((rs.map c).restrict {a | (rs.map c) a ≤ f n a}).lintegral μ) : begin refine supr_le_supr (assume n, _), rw [restrict_lintegral _ (h_meas n)], { refine le_of_eq (finset.sum_congr rfl $ assume r hr, _), congr' 2 with a, refine and_congr_right _, simp {contextual := tt} } end ... ≤ (⨆n, ∫⁻ a, f n a ∂μ) : begin refine supr_le_supr (assume n, _), rw [← simple_func.lintegral_eq_lintegral], refine lintegral_mono (assume a, _), simp only [map_apply] at h_meas, simp only [coe_map, restrict_apply _ (h_meas _), (∘)], exact indicator_apply_le id, end end /-- Monotone convergence theorem -- sometimes called Beppo-Levi convergence. Version with ae_measurable functions. -/ theorem lintegral_supr' {f : ℕ → α → ℝ≥0∞} (hf : ∀n, ae_measurable (f n) μ) (h_mono : ∀ᵐ x ∂μ, monotone (λ n, f n x)) : (∫⁻ a, ⨆n, f n a ∂μ) = (⨆n, ∫⁻ a, f n a ∂μ) := begin simp_rw ←supr_apply, let p : α → (ℕ → ℝ≥0∞) → Prop := λ x f', monotone f', have hp : ∀ᵐ x ∂μ, p x (λ i, f i x), from h_mono, have h_ae_seq_mono : monotone (ae_seq hf p), { intros n m hnm x, by_cases hx : x ∈ ae_seq_set hf p, { exact ae_seq.prop_of_mem_ae_seq_set hf hx hnm, }, { simp only [ae_seq, hx, if_false], exact le_refl _, }, }, rw lintegral_congr_ae (ae_seq.supr hf hp).symm, simp_rw supr_apply, rw @lintegral_supr _ _ μ _ (ae_seq.measurable hf p) h_ae_seq_mono, congr, exact funext (λ n, lintegral_congr_ae (ae_seq.ae_seq_n_eq_fun_n_ae hf hp n)), end /-- Monotone convergence theorem expressed with limits -/ theorem lintegral_tendsto_of_tendsto_of_monotone {f : ℕ → α → ℝ≥0∞} {F : α → ℝ≥0∞} (hf : ∀n, ae_measurable (f n) μ) (h_mono : ∀ᵐ x ∂μ, monotone (λ n, f n x)) (h_tendsto : ∀ᵐ x ∂μ, tendsto (λ n, f n x) at_top (𝓝 $ F x)) : tendsto (λ n, ∫⁻ x, f n x ∂μ) at_top (𝓝 $ ∫⁻ x, F x ∂μ) := begin have : monotone (λ n, ∫⁻ x, f n x ∂μ) := λ i j hij, lintegral_mono_ae (h_mono.mono $ λ x hx, hx hij), suffices key : ∫⁻ x, F x ∂μ = ⨆n, ∫⁻ x, f n x ∂μ, { rw key, exact tendsto_at_top_supr this }, rw ← lintegral_supr' hf h_mono, refine lintegral_congr_ae _, filter_upwards [h_mono, h_tendsto], exact λ x hx_mono hx_tendsto, tendsto_nhds_unique hx_tendsto (tendsto_at_top_supr hx_mono), end lemma lintegral_eq_supr_eapprox_lintegral {f : α → ℝ≥0∞} (hf : measurable f) : (∫⁻ a, f a ∂μ) = (⨆n, (eapprox f n).lintegral μ) := calc (∫⁻ a, f a ∂μ) = (∫⁻ a, ⨆n, (eapprox f n : α → ℝ≥0∞) a ∂μ) : by congr; ext a; rw [supr_eapprox_apply f hf] ... = (⨆n, ∫⁻ a, (eapprox f n : α → ℝ≥0∞) a ∂μ) : begin rw [lintegral_supr], { measurability, }, { assume i j h, exact (monotone_eapprox f h) } end ... = (⨆n, (eapprox f n).lintegral μ) : by congr; ext n; rw [(eapprox f n).lintegral_eq_lintegral] /-- If `f` has finite integral, then `∫⁻ x in s, f x ∂μ` is absolutely continuous in `s`: it tends to zero as `μ s` tends to zero. This lemma states states this fact in terms of `ε` and `δ`. -/ lemma exists_pos_set_lintegral_lt_of_measure_lt {f : α → ℝ≥0∞} (h : ∫⁻ x, f x ∂μ ≠ ∞) {ε : ℝ≥0∞} (hε : ε ≠ 0) : ∃ δ > 0, ∀ s, μ s < δ → ∫⁻ x in s, f x ∂μ < ε := begin rcases exists_between hε.bot_lt with ⟨ε₂, hε₂0 : 0 < ε₂, hε₂ε⟩, rcases exists_between hε₂0 with ⟨ε₁, hε₁0, hε₁₂⟩, rcases exists_simple_func_forall_lintegral_sub_lt_of_pos h hε₁0.ne' with ⟨φ, hle, hφ⟩, rcases φ.exists_forall_le with ⟨C, hC⟩, use [(ε₂ - ε₁) / C, ennreal.div_pos_iff.2 ⟨(tsub_pos_iff_lt.2 hε₁₂).ne', ennreal.coe_ne_top⟩], refine λ s hs, lt_of_le_of_lt _ hε₂ε, simp only [lintegral_eq_nnreal, supr_le_iff], intros ψ hψ, calc (map coe ψ).lintegral (μ.restrict s) ≤ (map coe φ).lintegral (μ.restrict s) + (map coe (ψ - φ)).lintegral (μ.restrict s) : begin rw [← simple_func.add_lintegral, ← simple_func.map_add @ennreal.coe_add], refine simple_func.lintegral_mono (λ x, _) le_rfl, simp [-ennreal.coe_add, add_tsub_eq_max, le_max_right] end ... ≤ (map coe φ).lintegral (μ.restrict s) + ε₁ : begin refine add_le_add le_rfl (le_trans _ (hφ _ hψ).le), exact simple_func.lintegral_mono le_rfl measure.restrict_le_self end ... ≤ (simple_func.const α (C : ℝ≥0∞)).lintegral (μ.restrict s) + ε₁ : by { mono*, exacts [λ x, coe_le_coe.2 (hC x), le_rfl, le_rfl] } ... = C * μ s + ε₁ : by simp [← simple_func.lintegral_eq_lintegral] ... ≤ C * ((ε₂ - ε₁) / C) + ε₁ : by { mono*, exacts [le_rfl, hs.le, le_rfl] } ... ≤ (ε₂ - ε₁) + ε₁ : add_le_add mul_div_le le_rfl ... = ε₂ : tsub_add_cancel_of_le hε₁₂.le, end /-- 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 tendsto_set_lintegral_zero {ι} {f : α → ℝ≥0∞} (h : ∫⁻ x, f x ∂μ ≠ ∞) {l : filter ι} {s : ι → set α} (hl : tendsto (μ ∘ s) l (𝓝 0)) : tendsto (λ i, ∫⁻ x in s i, f x ∂μ) l (𝓝 0) := begin simp only [ennreal.nhds_zero, tendsto_infi, tendsto_principal, mem_Iio, ← pos_iff_ne_zero] at hl ⊢, intros ε ε0, rcases exists_pos_set_lintegral_lt_of_measure_lt h ε0.ne' with ⟨δ, δ0, hδ⟩, exact (hl δ δ0).mono (λ i, hδ _) end @[simp] lemma lintegral_add {f g : α → ℝ≥0∞} (hf : measurable f) (hg : measurable g) : (∫⁻ a, f a + g a ∂μ) = (∫⁻ a, f a ∂μ) + (∫⁻ a, g a ∂μ) := calc (∫⁻ a, f a + g a ∂μ) = (∫⁻ a, (⨆n, (eapprox f n : α → ℝ≥0∞) a) + (⨆n, (eapprox g n : α → ℝ≥0∞) a) ∂μ) : by simp only [supr_eapprox_apply, hf, hg] ... = (∫⁻ a, (⨆n, (eapprox f n + eapprox g n : α → ℝ≥0∞) a) ∂μ) : begin congr, funext a, rw [ennreal.supr_add_supr_of_monotone], { refl }, { assume i j h, exact monotone_eapprox _ h a }, { assume i j h, exact monotone_eapprox _ h a }, end ... = (⨆n, (eapprox f n).lintegral μ + (eapprox g n).lintegral μ) : begin rw [lintegral_supr], { congr, funext n, rw [← simple_func.add_lintegral, ← simple_func.lintegral_eq_lintegral], refl }, { measurability, }, { assume i j h a, exact add_le_add (monotone_eapprox _ h _) (monotone_eapprox _ h _) } end ... = (⨆n, (eapprox f n).lintegral μ) + (⨆n, (eapprox g n).lintegral μ) : by refine (ennreal.supr_add_supr_of_monotone _ _).symm; { assume i j h, exact simple_func.lintegral_mono (monotone_eapprox _ h) (le_refl μ) } ... = (∫⁻ a, f a ∂μ) + (∫⁻ a, g a ∂μ) : by rw [lintegral_eq_supr_eapprox_lintegral hf, lintegral_eq_supr_eapprox_lintegral hg] lemma lintegral_add' {f g : α → ℝ≥0∞} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : (∫⁻ a, f a + g a ∂μ) = (∫⁻ a, f a ∂μ) + (∫⁻ a, g a ∂μ) := calc (∫⁻ a, f a + g a ∂μ) = (∫⁻ a, hf.mk f a + hg.mk g a ∂μ) : lintegral_congr_ae (eventually_eq.add hf.ae_eq_mk hg.ae_eq_mk) ... = (∫⁻ a, hf.mk f a ∂μ) + (∫⁻ a, hg.mk g a ∂μ) : lintegral_add hf.measurable_mk hg.measurable_mk ... = (∫⁻ a, f a ∂μ) + (∫⁻ a, g a ∂μ) : begin congr' 1, { exact lintegral_congr_ae hf.ae_eq_mk.symm }, { exact lintegral_congr_ae hg.ae_eq_mk.symm }, end lemma lintegral_zero : (∫⁻ a:α, 0 ∂μ) = 0 := by simp lemma lintegral_zero_fun : (∫⁻ a:α, (0 : α → ℝ≥0∞) a ∂μ) = 0 := by simp @[simp] lemma lintegral_smul_measure (c : ℝ≥0∞) (f : α → ℝ≥0∞) : ∫⁻ a, f a ∂ (c • μ) = c * ∫⁻ a, f a ∂μ := by simp only [lintegral, supr_subtype', simple_func.lintegral_smul, ennreal.mul_supr, smul_eq_mul] @[simp] lemma lintegral_sum_measure {m : measurable_space α} {ι} (f : α → ℝ≥0∞) (μ : ι → measure α) : ∫⁻ a, f a ∂(measure.sum μ) = ∑' i, ∫⁻ a, f a ∂(μ i) := begin simp only [lintegral, supr_subtype', simple_func.lintegral_sum, ennreal.tsum_eq_supr_sum], rw [supr_comm], congr, funext s, induction s using finset.induction_on with i s hi hs, { apply bot_unique, simp }, simp only [finset.sum_insert hi, ← hs], refine (ennreal.supr_add_supr _).symm, intros φ ψ, exact ⟨⟨φ ⊔ ψ, λ x, sup_le (φ.2 x) (ψ.2 x)⟩, add_le_add (simple_func.lintegral_mono le_sup_left (le_refl _)) (finset.sum_le_sum $ λ j hj, simple_func.lintegral_mono le_sup_right (le_refl _))⟩ end @[simp] lemma lintegral_add_measure {m : measurable_space α} (f : α → ℝ≥0∞) (μ ν : measure α) : ∫⁻ a, f a ∂ (μ + ν) = ∫⁻ a, f a ∂μ + ∫⁻ a, f a ∂ν := by simpa [tsum_fintype] using lintegral_sum_measure f (λ b, cond b μ ν) @[simp] lemma lintegral_zero_measure {m : measurable_space α} (f : α → ℝ≥0∞) : ∫⁻ a, f a ∂(0 : measure α) = 0 := bot_unique $ by simp [lintegral] lemma set_lintegral_empty (f : α → ℝ≥0∞) : ∫⁻ x in ∅, f x ∂μ = 0 := by rw [measure.restrict_empty, lintegral_zero_measure] lemma set_lintegral_univ (f : α → ℝ≥0∞) : ∫⁻ x in univ, f x ∂μ = ∫⁻ x, f x ∂μ := by rw measure.restrict_univ lemma set_lintegral_measure_zero (s : set α) (f : α → ℝ≥0∞) (hs' : μ s = 0) : ∫⁻ x in s, f x ∂μ = 0 := begin convert lintegral_zero_measure _, exact measure.restrict_eq_zero.2 hs', end lemma lintegral_finset_sum (s : finset β) {f : β → α → ℝ≥0∞} (hf : ∀ b ∈ s, measurable (f b)) : (∫⁻ a, ∑ b in s, f b a ∂μ) = ∑ b in s, ∫⁻ a, f b a ∂μ := begin induction s using finset.induction_on with a s has ih, { simp }, { simp only [finset.sum_insert has], rw [finset.forall_mem_insert] at hf, rw [lintegral_add hf.1 (s.measurable_sum hf.2), ih hf.2] } end @[simp] lemma lintegral_const_mul (r : ℝ≥0∞) {f : α → ℝ≥0∞} (hf : measurable f) : (∫⁻ a, r * f a ∂μ) = r * (∫⁻ a, f a ∂μ) := calc (∫⁻ a, r * f a ∂μ) = (∫⁻ a, (⨆n, (const α r * eapprox f n) a) ∂μ) : by { congr, funext a, rw [← supr_eapprox_apply f hf, ennreal.mul_supr], refl } ... = (⨆n, r * (eapprox f n).lintegral μ) : begin rw [lintegral_supr], { congr, funext n, rw [← simple_func.const_mul_lintegral, ← simple_func.lintegral_eq_lintegral] }, { assume n, exact simple_func.measurable _ }, { assume i j h a, exact mul_le_mul_left' (monotone_eapprox _ h _) _ } end ... = r * (∫⁻ a, f a ∂μ) : by rw [← ennreal.mul_supr, lintegral_eq_supr_eapprox_lintegral hf] lemma lintegral_const_mul'' (r : ℝ≥0∞) {f : α → ℝ≥0∞} (hf : ae_measurable f μ) : (∫⁻ a, r * f a ∂μ) = r * (∫⁻ a, f a ∂μ) := begin have A : ∫⁻ a, f a ∂μ = ∫⁻ a, hf.mk f a ∂μ := lintegral_congr_ae hf.ae_eq_mk, have B : ∫⁻ a, r * f a ∂μ = ∫⁻ a, r * hf.mk f a ∂μ := lintegral_congr_ae (eventually_eq.fun_comp hf.ae_eq_mk _), rw [A, B, lintegral_const_mul _ hf.measurable_mk], end lemma lintegral_const_mul_le (r : ℝ≥0∞) (f : α → ℝ≥0∞) : r * (∫⁻ a, f a ∂μ) ≤ (∫⁻ a, r * f a ∂μ) := begin rw [lintegral, ennreal.mul_supr], refine supr_le (λs, _), rw [ennreal.mul_supr], simp only [supr_le_iff, ge_iff_le], assume hs, rw ← simple_func.const_mul_lintegral, refine le_supr_of_le (const α r * s) (le_supr_of_le (λx, _) (le_refl _)), exact mul_le_mul_left' (hs x) _ end lemma lintegral_const_mul' (r : ℝ≥0∞) (f : α → ℝ≥0∞) (hr : r ≠ ∞) : (∫⁻ a, r * f a ∂μ) = r * (∫⁻ a, f a ∂μ) := begin by_cases h : r = 0, { simp [h] }, apply le_antisymm _ (lintegral_const_mul_le r f), have rinv : r * r⁻¹ = 1 := ennreal.mul_inv_cancel h hr, have rinv' : r ⁻¹ * r = 1, by { rw mul_comm, exact rinv }, have := lintegral_const_mul_le (r⁻¹) (λx, r * f x), simp [(mul_assoc _ _ _).symm, rinv'] at this, simpa [(mul_assoc _ _ _).symm, rinv] using mul_le_mul_left' this r end lemma lintegral_mul_const (r : ℝ≥0∞) {f : α → ℝ≥0∞} (hf : measurable f) : ∫⁻ a, f a * r ∂μ = ∫⁻ a, f a ∂μ * r := by simp_rw [mul_comm, lintegral_const_mul r hf] lemma lintegral_mul_const'' (r : ℝ≥0∞) {f : α → ℝ≥0∞} (hf : ae_measurable f μ) : ∫⁻ a, f a * r ∂μ = ∫⁻ a, f a ∂μ * r := by simp_rw [mul_comm, lintegral_const_mul'' r hf] lemma lintegral_mul_const_le (r : ℝ≥0∞) (f : α → ℝ≥0∞) : ∫⁻ a, f a ∂μ * r ≤ ∫⁻ a, f a * r ∂μ := by simp_rw [mul_comm, lintegral_const_mul_le r f] lemma lintegral_mul_const' (r : ℝ≥0∞) (f : α → ℝ≥0∞) (hr : r ≠ ∞): ∫⁻ a, f a * r ∂μ = ∫⁻ a, f a ∂μ * r := by simp_rw [mul_comm, lintegral_const_mul' r f hr] /- A double integral of a product where each factor contains only one variable is a product of integrals -/ lemma lintegral_lintegral_mul {β} [measurable_space β] {ν : measure β} {f : α → ℝ≥0∞} {g : β → ℝ≥0∞} (hf : ae_measurable f μ) (hg : ae_measurable g ν) : ∫⁻ x, ∫⁻ y, f x * g y ∂ν ∂μ = ∫⁻ x, f x ∂μ * ∫⁻ y, g y ∂ν := by simp [lintegral_const_mul'' _ hg, lintegral_mul_const'' _ hf] -- TODO: Need a better way of rewriting inside of a integral lemma lintegral_rw₁ {f f' : α → β} (h : f =ᵐ[μ] f') (g : β → ℝ≥0∞) : (∫⁻ a, g (f a) ∂μ) = (∫⁻ a, g (f' a) ∂μ) := lintegral_congr_ae $ h.mono $ λ a h, by rw h -- TODO: Need a better way of rewriting inside of a integral lemma lintegral_rw₂ {f₁ f₁' : α → β} {f₂ f₂' : α → γ} (h₁ : f₁ =ᵐ[μ] f₁') (h₂ : f₂ =ᵐ[μ] f₂') (g : β → γ → ℝ≥0∞) : (∫⁻ a, g (f₁ a) (f₂ a) ∂μ) = (∫⁻ a, g (f₁' a) (f₂' a) ∂μ) := lintegral_congr_ae $ h₁.mp $ h₂.mono $ λ _ h₂ h₁, by rw [h₁, h₂] @[simp] lemma lintegral_indicator (f : α → ℝ≥0∞) {s : set α} (hs : measurable_set s) : ∫⁻ a, s.indicator f a ∂μ = ∫⁻ a in s, f a ∂μ := begin simp only [lintegral, ← restrict_lintegral_eq_lintegral_restrict _ hs, supr_subtype'], apply le_antisymm; refine supr_le_supr2 (subtype.forall.2 $ λ φ hφ, _), { refine ⟨⟨φ, le_trans hφ (indicator_le_self _ _)⟩, _⟩, refine simple_func.lintegral_mono (λ x, _) (le_refl _), by_cases hx : x ∈ s, { simp [hx, hs, le_refl] }, { apply le_trans (hφ x), simp [hx, hs, le_refl] } }, { refine ⟨⟨φ.restrict s, λ x, _⟩, le_refl _⟩, simp [hφ x, hs, indicator_le_indicator] } end lemma set_lintegral_eq_const {f : α → ℝ≥0∞} (hf : measurable f) (r : ℝ≥0∞) : ∫⁻ x in {x | f x = r}, f x ∂μ = r * μ {x | f x = r} := begin have : ∀ᵐ x ∂μ, x ∈ {x | f x = r} → f x = r := ae_of_all μ (λ _ hx, hx), erw [set_lintegral_congr_fun _ this, lintegral_const, measure.restrict_apply measurable_set.univ, set.univ_inter], exact hf (measurable_set_singleton r) end /-- **Markov's inequality** also known as **Chebyshev's first inequality**. -/ lemma mul_meas_ge_le_lintegral {f : α → ℝ≥0∞} (hf : measurable f) (ε : ℝ≥0∞) : ε * μ {x | ε ≤ f x} ≤ ∫⁻ a, f a ∂μ := begin have : measurable_set {a : α | ε ≤ f a }, from hf measurable_set_Ici, rw [← simple_func.restrict_const_lintegral _ this, ← simple_func.lintegral_eq_lintegral], refine lintegral_mono (λ a, _), simp only [restrict_apply _ this], exact indicator_apply_le id end lemma lintegral_eq_top_of_measure_eq_top_pos {f : α → ℝ≥0∞} (hf : measurable f) (hμf : 0 < μ {x | f x = ∞}) : ∫⁻ x, f x ∂μ = ∞ := eq_top_iff.mpr $ calc ∞ = ∞ * μ {x | ∞ ≤ f x} : by simp [mul_eq_top, hμf.ne.symm] ... ≤ ∫⁻ x, f x ∂μ : mul_meas_ge_le_lintegral hf ∞ lemma meas_ge_le_lintegral_div {f : α → ℝ≥0∞} (hf : measurable f) {ε : ℝ≥0∞} (hε : ε ≠ 0) (hε' : ε ≠ ∞) : μ {x | ε ≤ f x} ≤ (∫⁻ a, f a ∂μ) / ε := (ennreal.le_div_iff_mul_le (or.inl hε) (or.inl hε')).2 $ by { rw [mul_comm], exact mul_meas_ge_le_lintegral hf ε } @[simp] lemma lintegral_eq_zero_iff {f : α → ℝ≥0∞} (hf : measurable f) : ∫⁻ a, f a ∂μ = 0 ↔ (f =ᵐ[μ] 0) := begin refine iff.intro (assume h, _) (assume h, _), { have : ∀n:ℕ, ∀ᵐ a ∂μ, f a < n⁻¹, { assume n, rw [ae_iff, ← nonpos_iff_eq_zero, ← @ennreal.zero_div n⁻¹, ennreal.le_div_iff_mul_le, mul_comm], simp only [not_lt], -- TODO: why `rw ← h` fails with "not an equality or an iff"? exacts [h ▸ mul_meas_ge_le_lintegral hf n⁻¹, or.inl (ennreal.inv_ne_zero.2 ennreal.coe_nat_ne_top), or.inr ennreal.zero_ne_top] }, refine (ae_all_iff.2 this).mono (λ a ha, _), by_contradiction h, rcases ennreal.exists_inv_nat_lt h with ⟨n, hn⟩, exact (lt_irrefl _ $ lt_trans hn $ ha n).elim }, { calc ∫⁻ a, f a ∂μ = ∫⁻ a, 0 ∂μ : lintegral_congr_ae h ... = 0 : lintegral_zero } end @[simp] lemma lintegral_eq_zero_iff' {f : α → ℝ≥0∞} (hf : ae_measurable f μ) : ∫⁻ a, f a ∂μ = 0 ↔ (f =ᵐ[μ] 0) := begin have : ∫⁻ a, f a ∂μ = ∫⁻ a, hf.mk f a ∂μ := lintegral_congr_ae hf.ae_eq_mk, rw [this, lintegral_eq_zero_iff hf.measurable_mk], exact ⟨λ H, hf.ae_eq_mk.trans H, λ H, hf.ae_eq_mk.symm.trans H⟩ end lemma lintegral_pos_iff_support {f : α → ℝ≥0∞} (hf : measurable f) : 0 < ∫⁻ a, f a ∂μ ↔ 0 < μ (function.support f) := by simp [pos_iff_ne_zero, hf, filter.eventually_eq, ae_iff, function.support] /-- Weaker version of the monotone convergence theorem-/ lemma lintegral_supr_ae {f : ℕ → α → ℝ≥0∞} (hf : ∀n, measurable (f n)) (h_mono : ∀n, ∀ᵐ a ∂μ, f n a ≤ f n.succ a) : (∫⁻ a, ⨆n, f n a ∂μ) = (⨆n, ∫⁻ a, f n a ∂μ) := let ⟨s, hs⟩ := exists_measurable_superset_of_null (ae_iff.1 (ae_all_iff.2 h_mono)) in let g := λ n a, if a ∈ s then 0 else f n a in have g_eq_f : ∀ᵐ a ∂μ, ∀n, g n a = f n a, from (measure_zero_iff_ae_nmem.1 hs.2.2).mono (assume a ha n, if_neg ha), calc ∫⁻ a, ⨆n, f n a ∂μ = ∫⁻ a, ⨆n, g n a ∂μ : lintegral_congr_ae $ g_eq_f.mono $ λ a ha, by simp only [ha] ... = ⨆n, (∫⁻ a, g n a ∂μ) : lintegral_supr (assume n, measurable_const.piecewise hs.2.1 (hf n)) (monotone_nat_of_le_succ $ assume n a, classical.by_cases (assume h : a ∈ s, by simp [g, if_pos h]) (assume h : a ∉ s, begin simp only [g, if_neg h], have := hs.1, rw subset_def at this, have := mt (this a) h, simp only [not_not, mem_set_of_eq] at this, exact this n end)) ... = ⨆n, (∫⁻ a, f n a ∂μ) : by simp only [lintegral_congr_ae (g_eq_f.mono $ λ a ha, ha _)] lemma lintegral_sub {f g : α → ℝ≥0∞} (hf : measurable f) (hg : measurable g) (hg_fin : ∫⁻ a, g a ∂μ ≠ ∞) (h_le : g ≤ᵐ[μ] f) : ∫⁻ a, f a - g a ∂μ = ∫⁻ a, f a ∂μ - ∫⁻ a, g a ∂μ := begin rw [← ennreal.add_left_inj hg_fin, tsub_add_cancel_of_le (lintegral_mono_ae h_le), ← lintegral_add (hf.sub hg) hg], refine lintegral_congr_ae (h_le.mono $ λ x hx, _), exact tsub_add_cancel_of_le hx end lemma lintegral_sub_le (f g : α → ℝ≥0∞) (hf : measurable f) (hg : measurable g) (h : f ≤ᵐ[μ] g) : ∫⁻ x, g x ∂μ - ∫⁻ x, f x ∂μ ≤ ∫⁻ x, g x - f x ∂μ := begin by_cases hfi : ∫⁻ x, f x ∂μ = ∞, { rw [hfi, ennreal.sub_top], exact bot_le }, { rw lintegral_sub hg hf hfi h, refl' } end lemma lintegral_strict_mono_of_ae_le_of_ae_lt_on {f g : α → ℝ≥0∞} (hf : measurable f) (hg : measurable g) (hfi : ∫⁻ x, f x ∂μ ≠ ∞) (h_le : f ≤ᵐ[μ] g) {s : set α} (hμs : μ s ≠ 0) (h : ∀ᵐ x ∂μ, x ∈ s → f x < g x) : ∫⁻ x, f x ∂μ < ∫⁻ x, g x ∂μ := begin rw [← tsub_pos_iff_lt, ← lintegral_sub hg hf hfi h_le], by_contra hnlt, rw [not_lt, nonpos_iff_eq_zero, lintegral_eq_zero_iff (hg.sub hf), filter.eventually_eq] at hnlt, simp only [ae_iff, tsub_eq_zero_iff_le, pi.zero_apply, not_lt, not_le] at hnlt h, refine hμs _, push_neg at h, have hs_eq : s = {a : α | a ∈ s ∧ g a ≤ f a} ∪ {a : α | a ∈ s ∧ f a < g a}, { ext1 x, simp_rw [set.mem_union, set.mem_set_of_eq, ← not_le], tauto, }, rw hs_eq, refine measure_union_null h (measure_mono_null _ hnlt), simp, end lemma lintegral_strict_mono {f g : α → ℝ≥0∞} (hμ : μ ≠ 0) (hf : measurable f) (hg : measurable g) (hfi : ∫⁻ x, f x ∂μ ≠ ∞) (h : ∀ᵐ x ∂μ, f x < g x) : ∫⁻ x, f x ∂μ < ∫⁻ x, g x ∂μ := begin rw [ne.def, ← measure.measure_univ_eq_zero] at hμ, refine lintegral_strict_mono_of_ae_le_of_ae_lt_on hf hg hfi (ae_le_of_ae_lt h) hμ _, simpa using h, end lemma set_lintegral_strict_mono {f g : α → ℝ≥0∞} {s : set α} (hsm : measurable_set s) (hs : μ s ≠ 0) (hf : measurable f) (hg : measurable g) (hfi : ∫⁻ x in s, f x ∂μ ≠ ∞) (h : ∀ᵐ x ∂μ, x ∈ s → f x < g x) : ∫⁻ x in s, f x ∂μ < ∫⁻ x in s, g x ∂μ := lintegral_strict_mono (by simp [hs]) hf hg hfi ((ae_restrict_iff' hsm).mpr h) /-- Monotone convergence theorem for nonincreasing sequences of functions -/ lemma lintegral_infi_ae {f : ℕ → α → ℝ≥0∞} (h_meas : ∀n, measurable (f n)) (h_mono : ∀n:ℕ, f n.succ ≤ᵐ[μ] f n) (h_fin : ∫⁻ a, f 0 a ∂μ ≠ ∞) : ∫⁻ a, ⨅n, f n a ∂μ = ⨅n, ∫⁻ a, f n a ∂μ := have fn_le_f0 : ∫⁻ a, ⨅n, f n a ∂μ ≤ ∫⁻ a, f 0 a ∂μ, from lintegral_mono (assume a, infi_le_of_le 0 (le_refl _)), have fn_le_f0' : (⨅n, ∫⁻ a, f n a ∂μ) ≤ ∫⁻ a, f 0 a ∂μ, from infi_le_of_le 0 (le_refl _), (ennreal.sub_right_inj h_fin fn_le_f0 fn_le_f0').1 $ show ∫⁻ a, f 0 a ∂μ - ∫⁻ a, ⨅n, f n a ∂μ = ∫⁻ a, f 0 a ∂μ - (⨅n, ∫⁻ a, f n a ∂μ), from calc ∫⁻ a, f 0 a ∂μ - (∫⁻ a, ⨅n, f n a ∂μ) = ∫⁻ a, f 0 a - ⨅n, f n a ∂μ: (lintegral_sub (h_meas 0) (measurable_infi h_meas) (ne_top_of_le_ne_top h_fin $ lintegral_mono (assume a, infi_le _ _)) (ae_of_all _ $ assume a, infi_le _ _)).symm ... = ∫⁻ a, ⨆n, f 0 a - f n a ∂μ : congr rfl (funext (assume a, ennreal.sub_infi)) ... = ⨆n, ∫⁻ a, f 0 a - f n a ∂μ : lintegral_supr_ae (assume n, (h_meas 0).sub (h_meas n)) (assume n, (h_mono n).mono $ assume a ha, tsub_le_tsub (le_refl _) ha) ... = ⨆n, ∫⁻ a, f 0 a ∂μ - ∫⁻ a, f n a ∂μ : have h_mono : ∀ᵐ a ∂μ, ∀n:ℕ, f n.succ a ≤ f n a := ae_all_iff.2 h_mono, have h_mono : ∀n, ∀ᵐ a ∂μ, f n a ≤ f 0 a := assume n, h_mono.mono $ assume a h, begin induction n with n ih, {exact le_refl _}, {exact le_trans (h n) ih} end, congr_arg supr $ funext $ assume n, lintegral_sub (h_meas _) (h_meas _) (ne_top_of_le_ne_top h_fin $ lintegral_mono_ae $ h_mono n) (h_mono n) ... = ∫⁻ a, f 0 a ∂μ - ⨅n, ∫⁻ a, f n a ∂μ : ennreal.sub_infi.symm /-- Monotone convergence theorem for nonincreasing sequences of functions -/ lemma lintegral_infi {f : ℕ → α → ℝ≥0∞} (h_meas : ∀n, measurable (f n)) (h_anti : antitone f) (h_fin : ∫⁻ a, f 0 a ∂μ ≠ ∞) : ∫⁻ a, ⨅n, f n a ∂μ = ⨅n, ∫⁻ a, f n a ∂μ := lintegral_infi_ae h_meas (λ n, ae_of_all _ $ h_anti n.le_succ) h_fin /-- Known as Fatou's lemma, version with `ae_measurable` functions -/ lemma lintegral_liminf_le' {f : ℕ → α → ℝ≥0∞} (h_meas : ∀n, ae_measurable (f n) μ) : ∫⁻ a, liminf at_top (λ n, f n a) ∂μ ≤ liminf at_top (λ n, ∫⁻ a, f n a ∂μ) := calc ∫⁻ a, liminf at_top (λ n, f n a) ∂μ = ∫⁻ a, ⨆n:ℕ, ⨅i≥n, f i a ∂μ : by simp only [liminf_eq_supr_infi_of_nat] ... = ⨆n:ℕ, ∫⁻ a, ⨅i≥n, f i a ∂μ : lintegral_supr' (assume n, ae_measurable_binfi _ (countable_encodable _) h_meas) (ae_of_all μ (assume a n m hnm, infi_le_infi_of_subset $ λ i hi, le_trans hnm hi)) ... ≤ ⨆n:ℕ, ⨅i≥n, ∫⁻ a, f i a ∂μ : supr_le_supr $ λ n, le_infi2_lintegral _ ... = at_top.liminf (λ n, ∫⁻ a, f n a ∂μ) : filter.liminf_eq_supr_infi_of_nat.symm /-- Known as Fatou's lemma -/ lemma lintegral_liminf_le {f : ℕ → α → ℝ≥0∞} (h_meas : ∀n, measurable (f n)) : ∫⁻ a, liminf at_top (λ n, f n a) ∂μ ≤ liminf at_top (λ n, ∫⁻ a, f n a ∂μ) := lintegral_liminf_le' (λ n, (h_meas n).ae_measurable) lemma limsup_lintegral_le {f : ℕ → α → ℝ≥0∞} {g : α → ℝ≥0∞} (hf_meas : ∀ n, measurable (f n)) (h_bound : ∀n, f n ≤ᵐ[μ] g) (h_fin : ∫⁻ a, g a ∂μ ≠ ∞) : limsup at_top (λn, ∫⁻ a, f n a ∂μ) ≤ ∫⁻ a, limsup at_top (λn, f n a) ∂μ := calc limsup at_top (λn, ∫⁻ a, f n a ∂μ) = ⨅n:ℕ, ⨆i≥n, ∫⁻ a, f i a ∂μ : limsup_eq_infi_supr_of_nat ... ≤ ⨅n:ℕ, ∫⁻ a, ⨆i≥n, f i a ∂μ : infi_le_infi $ assume n, supr2_lintegral_le _ ... = ∫⁻ a, ⨅n:ℕ, ⨆i≥n, f i a ∂μ : begin refine (lintegral_infi _ _ _).symm, { assume n, exact measurable_bsupr _ (countable_encodable _) hf_meas }, { assume n m hnm a, exact (supr_le_supr_of_subset $ λ i hi, le_trans hnm hi) }, { refine ne_top_of_le_ne_top h_fin (lintegral_mono_ae _), refine (ae_all_iff.2 h_bound).mono (λ n hn, _), exact supr_le (λ i, supr_le $ λ hi, hn i) } end ... = ∫⁻ a, limsup at_top (λn, f n a) ∂μ : by simp only [limsup_eq_infi_supr_of_nat] /-- Dominated convergence theorem for nonnegative functions -/ lemma tendsto_lintegral_of_dominated_convergence {F : ℕ → α → ℝ≥0∞} {f : α → ℝ≥0∞} (bound : α → ℝ≥0∞) (hF_meas : ∀n, measurable (F n)) (h_bound : ∀n, F n ≤ᵐ[μ] bound) (h_fin : ∫⁻ a, bound a ∂μ ≠ ∞) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top (𝓝 (f a))) : tendsto (λn, ∫⁻ a, F n a ∂μ) at_top (𝓝 (∫⁻ a, f a ∂μ)) := tendsto_of_le_liminf_of_limsup_le (calc ∫⁻ a, f a ∂μ = ∫⁻ a, liminf at_top (λ (n : ℕ), F n a) ∂μ : lintegral_congr_ae $ h_lim.mono $ assume a h, h.liminf_eq.symm ... ≤ liminf at_top (λ n, ∫⁻ a, F n a ∂μ) : lintegral_liminf_le hF_meas) (calc limsup at_top (λ (n : ℕ), ∫⁻ a, F n a ∂μ) ≤ ∫⁻ a, limsup at_top (λn, F n a) ∂μ : limsup_lintegral_le hF_meas h_bound h_fin ... = ∫⁻ a, f a ∂μ : lintegral_congr_ae $ h_lim.mono $ λ a h, h.limsup_eq) /-- Dominated convergence theorem for nonnegative functions which are just almost everywhere measurable. -/ lemma tendsto_lintegral_of_dominated_convergence' {F : ℕ → α → ℝ≥0∞} {f : α → ℝ≥0∞} (bound : α → ℝ≥0∞) (hF_meas : ∀n, ae_measurable (F n) μ) (h_bound : ∀n, F n ≤ᵐ[μ] bound) (h_fin : ∫⁻ 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 have : ∀ n, ∫⁻ a, F n a ∂μ = ∫⁻ a, (hF_meas n).mk (F n) a ∂μ := λ n, lintegral_congr_ae (hF_meas n).ae_eq_mk, simp_rw this, apply tendsto_lintegral_of_dominated_convergence bound (λ n, (hF_meas n).measurable_mk) _ h_fin, { have : ∀ n, ∀ᵐ a ∂μ, (hF_meas n).mk (F n) a = F n a := λ n, (hF_meas n).ae_eq_mk.symm, have : ∀ᵐ a ∂μ, ∀ n, (hF_meas n).mk (F n) a = F n a := ae_all_iff.mpr this, filter_upwards [this, h_lim], assume a H H', simp_rw H, exact H' }, { assume n, filter_upwards [h_bound n, (hF_meas n).ae_eq_mk], assume a H H', rwa H' at H } end /-- Dominated convergence theorem for filters with a countable basis -/ lemma tendsto_lintegral_filter_of_dominated_convergence {ι} {l : filter ι} [l.is_countably_generated] {F : ι → α → ℝ≥0∞} {f : α → ℝ≥0∞} (bound : α → ℝ≥0∞) (hF_meas : ∀ᶠ n in l, measurable (F n)) (h_bound : ∀ᶠ n in l, ∀ᵐ a ∂μ, F n a ≤ bound a) (h_fin : ∫⁻ a, bound a ∂μ ≠ ∞) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) l (𝓝 (f a))) : tendsto (λn, ∫⁻ a, F n a ∂μ) l (𝓝 $ ∫⁻ a, f a ∂μ) := begin rw tendsto_iff_seq_tendsto, intros x xl, have hxl, { rw tendsto_at_top' at xl, exact xl }, have h := inter_mem hF_meas h_bound, replace h := hxl _ h, rcases h with ⟨k, h⟩, rw ← tendsto_add_at_top_iff_nat k, refine tendsto_lintegral_of_dominated_convergence _ _ _ _ _, { exact bound }, { intro, refine (h _ _).1, exact nat.le_add_left _ _ }, { intro, refine (h _ _).2, exact nat.le_add_left _ _ }, { assumption }, { refine h_lim.mono (λ a h_lim, _), apply @tendsto.comp _ _ _ (λn, x (n + k)) (λn, F n a), { assumption }, rw tendsto_add_at_top_iff_nat, assumption } end section open encodable /-- Monotone convergence for a suprema over a directed family and indexed by an encodable type -/ theorem lintegral_supr_directed [encodable β] {f : β → α → ℝ≥0∞} (hf : ∀b, measurable (f b)) (h_directed : directed (≤) f) : ∫⁻ a, ⨆b, f b a ∂μ = ⨆b, ∫⁻ a, f b a ∂μ := begin casesI is_empty_or_nonempty β, { simp [supr_of_empty] }, inhabit β, have : ∀a, (⨆ b, f b a) = (⨆ n, f (h_directed.sequence f n) a), { assume a, refine le_antisymm (supr_le $ assume b, _) (supr_le $ assume n, le_supr (λn, f n a) _), exact le_supr_of_le (encode b + 1) (h_directed.le_sequence b a) }, calc ∫⁻ a, ⨆ b, f b a ∂μ = ∫⁻ a, ⨆ n, f (h_directed.sequence f n) a ∂μ : by simp only [this] ... = ⨆ n, ∫⁻ a, f (h_directed.sequence f n) a ∂μ : lintegral_supr (assume n, hf _) h_directed.sequence_mono ... = ⨆ b, ∫⁻ a, f b a ∂μ : begin refine le_antisymm (supr_le $ assume n, _) (supr_le $ assume b, _), { exact le_supr (λb, ∫⁻ a, f b a ∂μ) _ }, { exact le_supr_of_le (encode b + 1) (lintegral_mono $ h_directed.le_sequence b) } end end end lemma lintegral_tsum [encodable β] {f : β → α → ℝ≥0∞} (hf : ∀i, measurable (f i)) : ∫⁻ a, ∑' i, f i a ∂μ = ∑' i, ∫⁻ a, f i a ∂μ := begin simp only [ennreal.tsum_eq_supr_sum], rw [lintegral_supr_directed], { simp [lintegral_finset_sum _ (λ i _, hf i)] }, { assume b, exact finset.measurable_sum _ (λ i _, hf i) }, { assume s t, use [s ∪ t], split, exact assume a, finset.sum_le_sum_of_subset (finset.subset_union_left _ _), exact assume a, finset.sum_le_sum_of_subset (finset.subset_union_right _ _) } end open measure lemma lintegral_Union [encodable β] {s : β → set α} (hm : ∀ i, measurable_set (s i)) (hd : pairwise (disjoint on s)) (f : α → ℝ≥0∞) : ∫⁻ a in ⋃ i, s i, f a ∂μ = ∑' i, ∫⁻ a in s i, f a ∂μ := by simp only [measure.restrict_Union hd hm, lintegral_sum_measure] lemma lintegral_Union_le [encodable β] (s : β → set α) (f : α → ℝ≥0∞) : ∫⁻ a in ⋃ i, s i, f a ∂μ ≤ ∑' i, ∫⁻ a in s i, f a ∂μ := begin rw [← lintegral_sum_measure], exact lintegral_mono' restrict_Union_le (le_refl _) end lemma lintegral_union {f : α → ℝ≥0∞} {A B : set α} (hA : measurable_set A) (hB : measurable_set B) (hAB : disjoint A B) : ∫⁻ a in A ∪ B, f a ∂μ = ∫⁻ a in A, f a ∂μ + ∫⁻ a in B, f a ∂μ := begin rw [set.union_eq_Union, lintegral_Union, tsum_bool, add_comm], { simp only [to_bool_false_eq_ff, to_bool_true_eq_tt, cond] }, { intros i, exact measurable_set.cond hA hB }, { rwa pairwise_disjoint_on_bool } end lemma lintegral_add_compl (f : α → ℝ≥0∞) {A : set α} (hA : measurable_set A) : ∫⁻ x in A, f x ∂μ + ∫⁻ x in Aᶜ, f x ∂μ = ∫⁻ x, f x ∂μ := by rw [← lintegral_add_measure, measure.restrict_add_restrict_compl hA] lemma lintegral_map [measurable_space β] {f : β → ℝ≥0∞} {g : α → β} (hf : measurable f) (hg : measurable g) : ∫⁻ a, f a ∂(map g μ) = ∫⁻ a, f (g a) ∂μ := begin simp only [lintegral_eq_supr_eapprox_lintegral, hf, hf.comp hg], { congr, funext n, symmetry, apply simple_func.lintegral_map, { assume a, exact congr_fun (simple_func.eapprox_comp hf hg) a }, { assume s hs, exact map_apply hg hs } }, end lemma lintegral_map' [measurable_space β] {f : β → ℝ≥0∞} {g : α → β} (hf : ae_measurable f (measure.map g μ)) (hg : measurable g) : ∫⁻ a, f a ∂(measure.map g μ) = ∫⁻ a, f (g a) ∂μ := calc ∫⁻ a, f a ∂(measure.map g μ) = ∫⁻ a, hf.mk f a ∂(measure.map g μ) : lintegral_congr_ae hf.ae_eq_mk ... = ∫⁻ a, hf.mk f (g a) ∂μ : lintegral_map hf.measurable_mk hg ... = ∫⁻ a, f (g a) ∂μ : lintegral_congr_ae (ae_eq_comp hg hf.ae_eq_mk.symm) lemma lintegral_comp [measurable_space β] {f : β → ℝ≥0∞} {g : α → β} (hf : measurable f) (hg : measurable g) : lintegral μ (f ∘ g) = ∫⁻ a, f a ∂(map g μ) := (lintegral_map hf hg).symm lemma set_lintegral_map [measurable_space β] {f : β → ℝ≥0∞} {g : α → β} {s : set β} (hs : measurable_set s) (hf : measurable f) (hg : measurable g) : ∫⁻ y in s, f y ∂(map g μ) = ∫⁻ x in g ⁻¹' s, f (g x) ∂μ := by rw [restrict_map hg hs, lintegral_map hf hg] /-- The `lintegral` transforms appropriately under a measurable equivalence `g : α ≃ᵐ β`. (Compare `lintegral_map`, which applies to a wider class of functions `g : α → β`, but requires measurability of the function being integrated.) -/ lemma lintegral_map_equiv [measurable_space β] (f : β → ℝ≥0∞) (g : α ≃ᵐ β) : ∫⁻ a, f a ∂(map g μ) = ∫⁻ a, f (g a) ∂μ := begin refine le_antisymm _ _, { refine supr_le_supr2 _, intros f₀, use f₀.comp g g.measurable, refine supr_le_supr2 _, intros hf₀, use λ x, hf₀ (g x), exact (lintegral_map_equiv f₀ g).symm.le }, { refine supr_le_supr2 _, intros f₀, use f₀.comp g.symm g.symm.measurable, refine supr_le_supr2 _, intros hf₀, have : (λ a, (f₀.comp (g.symm) g.symm.measurable) a) ≤ λ (a : β), f a, { convert λ x, hf₀ (g.symm x), funext, simp [congr_arg f (congr_fun g.self_comp_symm a)] }, use this, convert (lintegral_map_equiv (f₀.comp g.symm g.symm.measurable) g).le, apply simple_func.ext, intros a, convert congr_arg f₀ (congr_fun g.symm_comp_self a).symm using 1 } end section dirac_and_count variable [measurable_space α] lemma lintegral_dirac' (a : α) {f : α → ℝ≥0∞} (hf : measurable f) : ∫⁻ a, f a ∂(dirac a) = f a := by simp [lintegral_congr_ae (ae_eq_dirac' hf)] lemma lintegral_dirac [measurable_singleton_class α] (a : α) (f : α → ℝ≥0∞) : ∫⁻ a, f a ∂(dirac a) = f a := by simp [lintegral_congr_ae (ae_eq_dirac f)] lemma lintegral_encodable {α : Type*} {m : measurable_space α} [encodable α] [measurable_singleton_class α] (f : α → ℝ≥0∞) (μ : measure α) : ∫⁻ a, f a ∂μ = ∑' a, f a * μ {a} := begin conv_lhs { rw [← sum_smul_dirac μ, lintegral_sum_measure] }, congr' 1 with a : 1, rw [lintegral_smul_measure, lintegral_dirac, mul_comm], end lemma lintegral_count' {f : α → ℝ≥0∞} (hf : measurable f) : ∫⁻ a, f a ∂count = ∑' a, f a := begin rw [count, lintegral_sum_measure], congr, exact funext (λ a, lintegral_dirac' a hf), end lemma lintegral_count [measurable_singleton_class α] (f : α → ℝ≥0∞) : ∫⁻ a, f a ∂count = ∑' a, f a := begin rw [count, lintegral_sum_measure], congr, exact funext (λ a, lintegral_dirac a f), end end dirac_and_count lemma ae_lt_top {f : α → ℝ≥0∞} (hf : measurable f) (h2f : ∫⁻ x, f x ∂μ ≠ ∞) : ∀ᵐ x ∂μ, f x < ∞ := begin simp_rw [ae_iff, ennreal.not_lt_top], by_contra h, apply h2f.lt_top.not_le, have : (f ⁻¹' {∞}).indicator ⊤ ≤ f, { intro x, by_cases hx : x ∈ f ⁻¹' {∞}; [simpa [hx], simp [hx]] }, convert lintegral_mono this, rw [lintegral_indicator _ (hf (measurable_set_singleton ∞))], simp [ennreal.top_mul, preimage, h] end lemma ae_lt_top' {f : α → ℝ≥0∞} (hf : ae_measurable f μ) (h2f : ∫⁻ x, f x ∂μ ≠ ∞) : ∀ᵐ x ∂μ, f x < ∞ := begin have h2f_meas : ∫⁻ x, hf.mk f x ∂μ ≠ ∞, by rwa ← lintegral_congr_ae hf.ae_eq_mk, exact (ae_lt_top hf.measurable_mk h2f_meas).mp (hf.ae_eq_mk.mono (λ x hx h, by rwa hx)), end lemma set_lintegral_lt_top_of_bdd_above {s : set α} (hs : μ s ≠ ∞) {f : α → ℝ≥0} (hf : measurable f) (hbdd : bdd_above (f '' s)) : ∫⁻ x in s, f x ∂μ < ∞ := begin obtain ⟨M, hM⟩ := hbdd, rw mem_upper_bounds at hM, refine lt_of_le_of_lt (set_lintegral_mono hf.coe_nnreal_ennreal (@measurable_const _ _ _ _ ↑M) _) _, { simpa using hM }, { rw lintegral_const, refine ennreal.mul_lt_top ennreal.coe_lt_top.ne _, simp [hs] } end lemma set_lintegral_lt_top_of_is_compact [topological_space α] [opens_measurable_space α] {s : set α} (hs : μ s ≠ ∞) (hsc : is_compact s) {f : α → ℝ≥0} (hf : continuous f) : ∫⁻ x in s, f x ∂μ < ∞ := set_lintegral_lt_top_of_bdd_above hs hf.measurable (hsc.image hf).bdd_above /-- Given a measure `μ : measure α` and a function `f : α → ℝ≥0∞`, `μ.with_density f` is the measure such that for a measurable set `s` we have `μ.with_density f s = ∫⁻ a in s, f a ∂μ`. -/ def measure.with_density {m : measurable_space α} (μ : measure α) (f : α → ℝ≥0∞) : measure α := measure.of_measurable (λs hs, ∫⁻ a in s, f a ∂μ) (by simp) (λ s hs hd, lintegral_Union hs hd _) @[simp] lemma with_density_apply (f : α → ℝ≥0∞) {s : set α} (hs : measurable_set s) : μ.with_density f s = ∫⁻ a in s, f a ∂μ := measure.of_measurable_apply s hs lemma with_density_add {f g : α → ℝ≥0∞} (hf : measurable f) (hg : measurable g) : μ.with_density (f + g) = μ.with_density f + μ.with_density g := begin refine measure.ext (λ s hs, _), rw [with_density_apply _ hs, measure.coe_add, pi.add_apply, with_density_apply _ hs, with_density_apply _ hs, ← lintegral_add hf hg], refl, end lemma with_density_smul (r : ℝ≥0∞) {f : α → ℝ≥0∞} (hf : measurable f) : μ.with_density (r • f) = r • μ.with_density f := begin refine measure.ext (λ s hs, _), rw [with_density_apply _ hs, measure.coe_smul, pi.smul_apply, with_density_apply _ hs, smul_eq_mul, ← lintegral_const_mul r hf], refl, end lemma with_density_smul' (r : ℝ≥0∞) (f : α → ℝ≥0∞) (hr : r ≠ ∞) : μ.with_density (r • f) = r • μ.with_density f := begin refine measure.ext (λ s hs, _), rw [with_density_apply _ hs, measure.coe_smul, pi.smul_apply, with_density_apply _ hs, smul_eq_mul, ← lintegral_const_mul' r f hr], refl, end lemma is_finite_measure_with_density {f : α → ℝ≥0∞} (hf : ∫⁻ a, f a ∂μ ≠ ∞) : is_finite_measure (μ.with_density f) := { measure_univ_lt_top := by rwa [with_density_apply _ measurable_set.univ, measure.restrict_univ, lt_top_iff_ne_top] } lemma with_density_absolutely_continuous {m : measurable_space α} (μ : measure α) (f : α → ℝ≥0∞) : μ.with_density f ≪ μ := begin refine absolutely_continuous.mk (λ s hs₁ hs₂, _), rw with_density_apply _ hs₁, exact set_lintegral_measure_zero _ _ hs₂ end @[simp] lemma with_density_zero : μ.with_density 0 = 0 := begin ext1 s hs, simp [with_density_apply _ hs], end @[simp] lemma with_density_one : μ.with_density 1 = μ := begin ext1 s hs, simp [with_density_apply _ hs], end lemma with_density_tsum {f : ℕ → α → ℝ≥0∞} (h : ∀ i, measurable (f i)) : μ.with_density (∑' n, f n) = sum (λ n, μ.with_density (f n)) := begin ext1 s hs, simp_rw [sum_apply _ hs, with_density_apply _ hs], change ∫⁻ x in s, (∑' n, f n) x ∂μ = ∑' (i : ℕ), ∫⁻ x, f i x ∂(μ.restrict s), rw ← lintegral_tsum h, refine lintegral_congr (λ x, tsum_apply (pi.summable.2 (λ _, ennreal.summable))), end lemma with_density_indicator {s : set α} (hs : measurable_set s) (f : α → ℝ≥0∞) : μ.with_density (s.indicator f) = (μ.restrict s).with_density f := begin ext1 t ht, rw [with_density_apply _ ht, lintegral_indicator _ hs, restrict_comm hs ht, ← with_density_apply _ ht] end lemma with_density_of_real_mutually_singular {f : α → ℝ} (hf : measurable f) : μ.with_density (λ x, ennreal.of_real $ f x) ⊥ₘ μ.with_density (λ x, ennreal.of_real $ -f x) := begin set S : set α := { x | f x < 0 } with hSdef, have hS : measurable_set S := measurable_set_lt hf measurable_const, refine ⟨S, hS, _, _⟩, { rw [with_density_apply _ hS, hSdef], have hf0 : ∀ᵐ x ∂μ, x ∈ S → ennreal.of_real (f x) = 0, { refine ae_of_all _ (λ _ hx, _), rw [ennreal.of_real_eq_zero.2 (le_of_lt hx)] }, rw set_lintegral_congr_fun hS hf0, exact lintegral_zero }, { rw [with_density_apply _ hS.compl, hSdef], have hf0 : ∀ᵐ x ∂μ, x ∈ Sᶜ → ennreal.of_real (-f x) = 0, { refine ae_of_all _ (λ x hx, _), rw ennreal.of_real_eq_zero.2, rwa [neg_le, neg_zero, ← not_lt] }, rw set_lintegral_congr_fun hS.compl hf0, exact lintegral_zero }, end lemma restrict_with_density {s : set α} (hs : measurable_set s) (f : α → ℝ≥0∞) : (μ.with_density f).restrict s = (μ.restrict s).with_density f := begin ext1 t ht, rw [restrict_apply ht, with_density_apply _ ht, with_density_apply _ (ht.inter hs), restrict_restrict ht], end lemma with_density_eq_zero {f : α → ℝ≥0∞} (hf : ae_measurable f μ) (h : μ.with_density f = 0) : f =ᵐ[μ] 0 := by rw [← lintegral_eq_zero_iff' hf, ← set_lintegral_univ, ← with_density_apply _ measurable_set.univ, h, measure.coe_zero, pi.zero_apply] end lintegral end measure_theory open measure_theory measure_theory.simple_func /-- To prove something for an arbitrary measurable function into `ℝ≥0∞`, it suffices to show that the property holds for (multiples of) characteristic functions and is closed under addition and supremum of increasing sequences of functions. It is possible to make the hypotheses in the induction steps a bit stronger, and such conditions can be added once we need them (for example in `h_add` it is only necessary to consider the sum of a simple function with a multiple of a characteristic function and that the intersection of their images is a subset of `{0}`. -/ @[elab_as_eliminator] theorem measurable.ennreal_induction {α} [measurable_space α] {P : (α → ℝ≥0∞) → Prop} (h_ind : ∀ (c : ℝ≥0∞) ⦃s⦄, measurable_set s → P (indicator s (λ _, c))) (h_add : ∀ ⦃f g : α → ℝ≥0∞⦄, disjoint (support f) (support g) → measurable f → measurable g → P f → P g → P (f + g)) (h_supr : ∀ ⦃f : ℕ → α → ℝ≥0∞⦄ (hf : ∀n, measurable (f n)) (h_mono : monotone f) (hP : ∀ n, P (f n)), P (λ x, ⨆ n, f n x)) ⦃f : α → ℝ≥0∞⦄ (hf : measurable f) : P f := begin convert h_supr (λ n, (eapprox f n).measurable) (monotone_eapprox f) _, { ext1 x, rw [supr_eapprox_apply f hf] }, { exact λ n, simple_func.induction (λ c s hs, h_ind c hs) (λ f g hfg hf hg, h_add hfg f.measurable g.measurable hf hg) (eapprox f n) } end namespace measure_theory variables {α : Type*} {m m0 : measurable_space α} include m /-- This is Exercise 1.2.1 from [tao2010]. It allows you to express integration of a measurable function with respect to `(μ.with_density f)` as an integral with respect to `μ`, called the base measure. `μ` is often the Lebesgue measure, and in this circumstance `f` is the probability density function, and `(μ.with_density f)` represents any continuous random variable as a probability measure, such as the uniform distribution between 0 and 1, the Gaussian distribution, the exponential distribution, the Beta distribution, or the Cauchy distribution (see Section 2.4 of [wasserman2004]). Thus, this method shows how to one can calculate expectations, variances, and other moments as a function of the probability density function. -/ lemma lintegral_with_density_eq_lintegral_mul (μ : measure α) {f : α → ℝ≥0∞} (h_mf : measurable f) : ∀ {g : α → ℝ≥0∞}, measurable g → ∫⁻ a, g a ∂(μ.with_density f) = ∫⁻ a, (f * g) a ∂μ := begin apply measurable.ennreal_induction, { intros c s h_ms, simp [*, mul_comm _ c, ← indicator_mul_right], }, { intros g h h_univ h_mea_g h_mea_h h_ind_g h_ind_h, simp [mul_add, *, measurable.mul] }, { intros g h_mea_g h_mono_g h_ind, have : monotone (λ n a, f a * g n a) := λ m n hmn x, ennreal.mul_le_mul le_rfl (h_mono_g hmn x), simp [lintegral_supr, ennreal.mul_supr, h_mf.mul (h_mea_g _), *] } end lemma with_density_mul (μ : measure α) {f g : α → ℝ≥0∞} (hf : measurable f) (hg : measurable g) : μ.with_density (f * g) = (μ.with_density f).with_density g := begin ext1 s hs, simp [with_density_apply _ hs, restrict_with_density hs, lintegral_with_density_eq_lintegral_mul _ hf hg], end lemma set_lintegral_with_density_eq_set_lintegral_mul (μ : measure α) {f g : α → ℝ≥0∞} (hf : measurable f) (hg : measurable g) {s : set α} (hs : measurable_set s) : ∫⁻ x in s, g x ∂μ.with_density f = ∫⁻ x in s, (f * g) x ∂μ := by rw [restrict_with_density hs, lintegral_with_density_eq_lintegral_mul _ hf hg] /-- In a sigma-finite measure space, there exists an integrable function which is positive everywhere (and with an arbitrarily small integral). -/ lemma exists_pos_lintegral_lt_of_sigma_finite (μ : measure α) [sigma_finite μ] {ε : ℝ≥0∞} (ε0 : ε ≠ 0) : ∃ g : α → ℝ≥0, (∀ x, 0 < g x) ∧ measurable g ∧ (∫⁻ x, g x ∂μ < ε) := begin /- Let `s` be a covering of `α` by pairwise disjoint measurable sets of finite measure. Let `δ : ℕ → ℝ≥0` be a positive function such that `∑' i, μ (s i) * δ i < ε`. Then the function that is equal to `δ n` on `s n` is a positive function with integral less than `ε`. -/ set s : ℕ → set α := disjointed (spanning_sets μ), have : ∀ n, μ (s n) < ∞, from λ n, (measure_mono $ disjointed_subset _ _).trans_lt (measure_spanning_sets_lt_top μ n), obtain ⟨δ, δpos, δsum⟩ : ∃ δ : ℕ → ℝ≥0, (∀ i, 0 < δ i) ∧ ∑' i, μ (s i) * δ i < ε, from ennreal.exists_pos_tsum_mul_lt_of_encodable ε0 _ (λ n, (this n).ne), set N : α → ℕ := spanning_sets_index μ, have hN_meas : measurable N := measurable_spanning_sets_index μ, have hNs : ∀ n, N ⁻¹' {n} = s n := preimage_spanning_sets_index_singleton μ, refine ⟨δ ∘ N, λ x, δpos _, measurable_from_nat.comp hN_meas, _⟩, simpa [lintegral_comp measurable_from_nat.coe_nnreal_ennreal hN_meas, hNs, lintegral_encodable, measurable_spanning_sets_index, mul_comm] using δsum, end lemma lintegral_trim {μ : measure α} (hm : m ≤ m0) {f : α → ℝ≥0∞} (hf : @measurable _ _ m _ f) : ∫⁻ a, f a ∂(μ.trim hm) = ∫⁻ a, f a ∂μ := begin refine @measurable.ennreal_induction α m (λ f, ∫⁻ a, f a ∂(μ.trim hm) = ∫⁻ a, f a ∂μ) _ _ _ f hf, { intros c s hs, rw [lintegral_indicator _ hs, lintegral_indicator _ (hm s hs), set_lintegral_const, set_lintegral_const], suffices h_trim_s : μ.trim hm s = μ s, by rw h_trim_s, exact trim_measurable_set_eq hm hs, }, { intros f g hfg hf hg hf_prop hg_prop, have h_m := lintegral_add hf hg, have h_m0 := lintegral_add (measurable.mono hf hm le_rfl) (measurable.mono hg hm le_rfl), rwa [hf_prop, hg_prop, ← h_m0] at h_m, }, { intros f hf hf_mono hf_prop, rw lintegral_supr hf hf_mono, rw lintegral_supr (λ n, measurable.mono (hf n) hm le_rfl) hf_mono, congr, exact funext (λ n, hf_prop n), }, end lemma lintegral_trim_ae {μ : measure α} (hm : m ≤ m0) {f : α → ℝ≥0∞} (hf : ae_measurable f (μ.trim hm)) : ∫⁻ a, f a ∂(μ.trim hm) = ∫⁻ a, f a ∂μ := by rw [lintegral_congr_ae (ae_eq_of_ae_eq_trim hf.ae_eq_mk), lintegral_congr_ae hf.ae_eq_mk, lintegral_trim hm hf.measurable_mk] section sigma_finite variables {E : Type*} [normed_group E] [measurable_space E] [opens_measurable_space E] lemma univ_le_of_forall_fin_meas_le {μ : measure α} (hm : m ≤ m0) [@sigma_finite _ m (μ.trim hm)] (C : ℝ≥0∞) {f : set α → ℝ≥0∞} (hf : ∀ s, measurable_set[m] s → μ s ≠ ∞ → f s ≤ C) (h_F_lim : ∀ S : ℕ → set α, (∀ n, measurable_set[m] (S n)) → monotone S → f (⋃ n, S n) ≤ ⨆ n, f (S n)) : f univ ≤ C := begin let S := @spanning_sets _ m (μ.trim hm) _, have hS_mono : monotone S, from @monotone_spanning_sets _ m (μ.trim hm) _, have hS_meas : ∀ n, measurable_set[m] (S n), from @measurable_spanning_sets _ m (μ.trim hm) _, rw ← @Union_spanning_sets _ m (μ.trim hm), refine (h_F_lim S hS_meas hS_mono).trans _, refine supr_le (λ n, hf (S n) (hS_meas n) _), exact ((le_trim hm).trans_lt (@measure_spanning_sets_lt_top _ m (μ.trim hm) _ n)).ne, end /-- If the Lebesgue integral of a function is bounded by some constant on all sets with finite measure in a sub-σ-algebra and the measure is σ-finite on that sub-σ-algebra, then the integral over the whole space is bounded by that same constant. Version for a measurable function. See `lintegral_le_of_forall_fin_meas_le'` for the more general `ae_measurable` version. -/ lemma lintegral_le_of_forall_fin_meas_le_of_measurable {μ : measure α} (hm : m ≤ m0) [@sigma_finite _ m (μ.trim hm)] (C : ℝ≥0∞) {f : α → ℝ≥0∞} (hf_meas : measurable f) (hf : ∀ s, measurable_set[m] s → μ s ≠ ∞ → ∫⁻ x in s, f x ∂μ ≤ C) : ∫⁻ x, f x ∂μ ≤ C := begin have : ∫⁻ x in univ, f x ∂μ = ∫⁻ x, f x ∂μ, by simp only [measure.restrict_univ], rw ← this, refine univ_le_of_forall_fin_meas_le hm C hf (λ S hS_meas hS_mono, _), rw ← lintegral_indicator, swap, { exact hm (⋃ n, S n) (@measurable_set.Union _ _ m _ _ hS_meas), }, have h_integral_indicator : (⨆ n, ∫⁻ x in S n, f x ∂μ) = ⨆ n, ∫⁻ x, (S n).indicator f x ∂μ, { congr, ext1 n, rw lintegral_indicator _ (hm _ (hS_meas n)), }, rw [h_integral_indicator, ← lintegral_supr], { refine le_of_eq (lintegral_congr (λ x, _)), simp_rw indicator_apply, by_cases hx_mem : x ∈ Union S, { simp only [hx_mem, if_true], obtain ⟨n, hxn⟩ := mem_Union.mp hx_mem, refine le_antisymm (trans _ (le_supr _ n)) (supr_le (λ i, _)), { simp only [hxn, le_refl, if_true], }, { by_cases hxi : x ∈ S i; simp [hxi], }, }, { simp only [hx_mem, if_false], rw mem_Union at hx_mem, push_neg at hx_mem, refine le_antisymm (zero_le _) (supr_le (λ n, _)), simp only [hx_mem n, if_false, nonpos_iff_eq_zero], }, }, { exact λ n, hf_meas.indicator (hm _ (hS_meas n)), }, { intros n₁ n₂ hn₁₂ a, simp_rw indicator_apply, split_ifs, { exact le_rfl, }, { exact absurd (mem_of_mem_of_subset h (hS_mono hn₁₂)) h_1, }, { exact zero_le _, }, { exact le_rfl, }, }, end /-- If the Lebesgue integral of a function is bounded by some constant on all sets with finite measure in a sub-σ-algebra and the measure is σ-finite on that sub-σ-algebra, then the integral over the whole space is bounded by that same constant. -/ lemma lintegral_le_of_forall_fin_meas_le' {μ : measure α} (hm : m ≤ m0) [@sigma_finite _ m (μ.trim hm)] (C : ℝ≥0∞) {f : _ → ℝ≥0∞} (hf_meas : ae_measurable f μ) (hf : ∀ s, measurable_set[m] s → μ s ≠ ∞ → ∫⁻ x in s, f x ∂μ ≤ C) : ∫⁻ x, f x ∂μ ≤ C := begin let f' := hf_meas.mk f, have hf' : ∀ s, measurable_set[m] s → μ s ≠ ∞ → ∫⁻ x in s, f' x ∂μ ≤ C, { refine λ s hs hμs, (le_of_eq _).trans (hf s hs hμs), refine lintegral_congr_ae (ae_restrict_of_ae (hf_meas.ae_eq_mk.mono (λ x hx, _))), rw hx, }, rw lintegral_congr_ae hf_meas.ae_eq_mk, exact lintegral_le_of_forall_fin_meas_le_of_measurable hm C hf_meas.measurable_mk hf', end omit m /-- If the Lebesgue integral of a function is bounded by some constant on all sets with finite measure and the measure is σ-finite, then the integral over the whole space is bounded by that same constant. -/ lemma lintegral_le_of_forall_fin_meas_le [measurable_space α] {μ : measure α} [sigma_finite μ] (C : ℝ≥0∞) {f : α → ℝ≥0∞} (hf_meas : ae_measurable f μ) (hf : ∀ s, measurable_set s → μ s ≠ ∞ → ∫⁻ x in s, f x ∂μ ≤ C) : ∫⁻ x, f x ∂μ ≤ C := @lintegral_le_of_forall_fin_meas_le' _ _ _ _ le_rfl (by rwa trim_eq_self) C _ hf_meas hf /-- A sigma-finite measure is absolutely continuous with respect to some finite measure. -/ lemma exists_absolutely_continuous_is_finite_measure {m : measurable_space α} (μ : measure α) [sigma_finite μ] : ∃ (ν : measure α), is_finite_measure ν ∧ μ ≪ ν := begin obtain ⟨g, gpos, gmeas, hg⟩ : ∃ (g : α → ℝ≥0), (∀ (x : α), 0 < g x) ∧ measurable g ∧ ∫⁻ (x : α), ↑(g x) ∂μ < 1 := exists_pos_lintegral_lt_of_sigma_finite μ (ennreal.zero_lt_one).ne', refine ⟨μ.with_density (λ x, g x), is_finite_measure_with_density hg.ne_top, _⟩, have : μ = (μ.with_density (λ x, g x)).with_density (λ x, (g x)⁻¹), { have A : (λ (x : α), (g x : ℝ≥0∞)) * (λ (x : α), (↑(g x))⁻¹) = 1, { ext1 x, exact ennreal.mul_inv_cancel (ennreal.coe_ne_zero.2 ((gpos x).ne')) ennreal.coe_ne_top }, rw [← with_density_mul _ gmeas.coe_nnreal_ennreal gmeas.coe_nnreal_ennreal.inv, A, with_density_one] }, conv_lhs { rw this }, exact with_density_absolutely_continuous _ _, end end sigma_finite end measure_theory
96897bed52001e35e96b0252df7ab3cdaa441bb2
9dc8cecdf3c4634764a18254e94d43da07142918
/src/combinatorics/set_family/intersecting.lean
03aad14f5b461fb4fb9c34e23d1b2479a4f134ed
[ "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,555
lean
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import data.fintype.basic import order.upper_lower /-! # Intersecting families This file defines intersecting families and proves their basic properties. ## Main declarations * `set.intersecting`: Predicate for a set of elements in a generalized boolean algebra to be an intersecting family. * `set.intersecting.card_le`: An intersecting family can only take up to half the elements, because `a` and `aᶜ` cannot simultaneously be in it. * `set.intersecting.is_max_iff_card_eq`: Any maximal intersecting family takes up half the elements. ## References * [D. J. Kleitman, *Families of non-disjoint subsets*][kleitman1966] -/ open finset variables {α : Type*} namespace set section semilattice_inf variables [semilattice_inf α] [order_bot α] {s t : set α} {a b c : α} /-- A set family is intersecting if every pair of elements is non-disjoint. -/ def intersecting (s : set α) : Prop := ∀ ⦃a⦄, a ∈ s → ∀ ⦃b⦄, b ∈ s → ¬ disjoint a b @[mono] lemma intersecting.mono (h : t ⊆ s) (hs : s.intersecting) : t.intersecting := λ a ha b hb, hs (h ha) (h hb) lemma intersecting.not_bot_mem (hs : s.intersecting) : ⊥ ∉ s := λ h, hs h h disjoint_bot_left lemma intersecting.ne_bot (hs : s.intersecting) (ha : a ∈ s) : a ≠ ⊥ := ne_of_mem_of_not_mem ha hs.not_bot_mem lemma intersecting_empty : (∅ : set α).intersecting := λ _, false.elim @[simp] lemma intersecting_singleton : ({a} : set α).intersecting ↔ a ≠ ⊥ := by simp [intersecting] lemma intersecting.insert (hs : s.intersecting) (ha : a ≠ ⊥) (h : ∀ b ∈ s, ¬ disjoint a b) : (insert a s).intersecting := begin rintro b (rfl | hb) c (rfl | hc), { rwa disjoint_self }, { exact h _ hc }, { exact λ H, h _ hb H.symm }, { exact hs hb hc } end lemma intersecting_insert : (insert a s).intersecting ↔ s.intersecting ∧ a ≠ ⊥ ∧ ∀ b ∈ s, ¬ disjoint a b := ⟨λ h, ⟨h.mono $ subset_insert _ _, h.ne_bot $ mem_insert _ _, λ b hb, h (mem_insert _ _) $ mem_insert_of_mem _ hb⟩, λ h, h.1.insert h.2.1 h.2.2⟩ lemma intersecting_iff_pairwise_not_disjoint : s.intersecting ↔ s.pairwise (λ a b, ¬ disjoint a b) ∧ s ≠ {⊥} := begin refine ⟨λ h, ⟨λ a ha b hb _, h ha hb, _⟩, λ h a ha b hb hab, _⟩, { rintro rfl, exact intersecting_singleton.1 h rfl }, { have := h.1.eq ha hb (not_not.2 hab), rw [this, disjoint_self] at hab, rw hab at hb, exact h.2 (eq_singleton_iff_unique_mem.2 ⟨hb, λ c hc, not_ne_iff.1 $ λ H, h.1 hb hc H.symm disjoint_bot_left⟩) } end protected lemma subsingleton.intersecting (hs : s.subsingleton) : s.intersecting ↔ s ≠ {⊥} := intersecting_iff_pairwise_not_disjoint.trans $ and_iff_right $ hs.pairwise _ lemma intersecting_iff_eq_empty_of_subsingleton [subsingleton α] (s : set α) : s.intersecting ↔ s = ∅ := begin refine subsingleton_of_subsingleton.intersecting.trans ⟨not_imp_comm.2 $ λ h, subsingleton_of_subsingleton.eq_singleton_of_mem _, _⟩, { obtain ⟨a, ha⟩ := ne_empty_iff_nonempty.1 h, rwa subsingleton.elim ⊥ a }, { rintro rfl, exact (set.singleton_nonempty _).ne_empty.symm } end /-- Maximal intersecting families are upper sets. -/ protected lemma intersecting.is_upper_set (hs : s.intersecting) (h : ∀ t : set α, t.intersecting → s ⊆ t → s = t) : is_upper_set s := begin classical, rintro a b hab ha, rw h (insert b s) _ (subset_insert _ _), { exact mem_insert _ _ }, exact hs.insert (mt (eq_bot_mono hab) $ hs.ne_bot ha) (λ c hc hbc, hs ha hc $ hbc.mono_left hab), end /-- Maximal intersecting families are upper sets. Finset version. -/ lemma intersecting.is_upper_set' {s : finset α} (hs : (s : set α).intersecting) (h : ∀ t : finset α, (t : set α).intersecting → s ⊆ t → s = t) : is_upper_set (s : set α) := begin classical, rintro a b hab ha, rw h (insert b s) _ (finset.subset_insert _ _), { exact mem_insert_self _ _ }, rw coe_insert, exact hs.insert (mt (eq_bot_mono hab) $ hs.ne_bot ha) (λ c hc hbc, hs ha hc $ hbc.mono_left hab), end end semilattice_inf lemma intersecting.exists_mem_set {𝒜 : set (set α)} (h𝒜 : 𝒜.intersecting) {s t : set α} (hs : s ∈ 𝒜) (ht : t ∈ 𝒜) : ∃ a, a ∈ s ∧ a ∈ t := not_disjoint_iff.1 $ h𝒜 hs ht lemma intersecting.exists_mem_finset [decidable_eq α] {𝒜 : set (finset α)} (h𝒜 : 𝒜.intersecting) {s t : finset α} (hs : s ∈ 𝒜) (ht : t ∈ 𝒜) : ∃ a, a ∈ s ∧ a ∈ t := not_disjoint_iff.1 $ disjoint_coe.not.2 $ h𝒜 hs ht variables [boolean_algebra α] lemma intersecting.not_compl_mem {s : set α} (hs : s.intersecting) {a : α} (ha : a ∈ s) : aᶜ ∉ s := λ h, hs ha h disjoint_compl_right lemma intersecting.not_mem {s : set α} (hs : s.intersecting) {a : α} (ha : aᶜ ∈ s) : a ∉ s := λ h, hs ha h disjoint_compl_left variables [fintype α] {s : finset α} lemma intersecting.card_le (hs : (s : set α).intersecting) : 2 * s.card ≤ fintype.card α := begin classical, refine (s ∪ s.map ⟨compl, compl_injective⟩).card_le_univ.trans_eq' _, rw [two_mul, card_union_eq, card_map], rintro x hx, rw [finset.inf_eq_inter, finset.mem_inter, mem_map] at hx, obtain ⟨x, hx', rfl⟩ := hx.2, exact hs.not_compl_mem hx' hx.1, end variables [nontrivial α] -- Note, this lemma is false when `α` has exactly one element and boring when `α` is empty. lemma intersecting.is_max_iff_card_eq (hs : (s : set α).intersecting) : (∀ t : finset α, (t : set α).intersecting → s ⊆ t → s = t) ↔ 2 * s.card = fintype.card α := begin classical, refine ⟨λ h, _, λ h t ht hst, finset.eq_of_subset_of_card_le hst $ le_of_mul_le_mul_left (ht.card_le.trans_eq h.symm) two_pos⟩, suffices : s ∪ s.map ⟨compl, compl_injective⟩ = finset.univ, { rw [fintype.card, ←this, two_mul, card_union_eq, card_map], rintro x hx, rw [finset.inf_eq_inter, finset.mem_inter, mem_map] at hx, obtain ⟨x, hx', rfl⟩ := hx.2, exact hs.not_compl_mem hx' hx.1 }, rw [←coe_eq_univ, coe_union, coe_map, function.embedding.coe_fn_mk, image_eq_preimage_of_inverse compl_compl compl_compl], refine eq_univ_of_forall (λ a, _), simp_rw [mem_union, mem_preimage], by_contra' ha, refine s.ne_insert_of_not_mem _ ha.1 (h _ _ $ s.subset_insert _), rw coe_insert, refine hs.insert _ (λ b hb hab, ha.2 $ (hs.is_upper_set' h) hab.le_compl_left hb), rintro rfl, have := h {⊤} (by { rw coe_singleton, exact intersecting_singleton.2 top_ne_bot }), rw compl_bot at ha, rw coe_eq_empty.1 ((hs.is_upper_set' h).not_top_mem.1 ha.2) at this, exact singleton_ne_empty _ (this $ empty_subset _).symm, end lemma intersecting.exists_card_eq (hs : (s : set α).intersecting) : ∃ t, s ⊆ t ∧ 2 * t.card = fintype.card α ∧ (t : set α).intersecting := begin have := hs.card_le, rw [mul_comm, ←nat.le_div_iff_mul_le' two_pos] at this, revert hs, refine s.strong_downward_induction_on _ this, rintro s ih hcard hs, by_cases ∀ t : finset α, (t : set α).intersecting → s ⊆ t → s = t, { exact ⟨s, subset.rfl, hs.is_max_iff_card_eq.1 h, hs⟩ }, push_neg at h, obtain ⟨t, ht, hst⟩ := h, refine (ih _ (_root_.ssubset_iff_subset_ne.2 hst) ht).imp (λ u, and.imp_left hst.1.trans), rw [nat.le_div_iff_mul_le' two_pos, mul_comm], exact ht.card_le, end end set
ea96c36b2fd95d9f6e55ab0fe5be19c3b8620e55
b7f22e51856f4989b970961f794f1c435f9b8f78
/hott/hit/default.hlean
2ddfc9354c78b379aab3aac691cab9e25cb81e19
[ "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
211
hlean
/- Copyright (c) 2016 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import .two_quotient .colimit .coeq .refl_quotient
2bfa6a0c008a830fc2d44a50cc0a0836a35a1555
dc253be9829b840f15d96d986e0c13520b085033
/homotopy/susp.hlean
51f992c60c680ed8fc9f4ea6c8d31ad68dbb9a43
[ "Apache-2.0" ]
permissive
cmu-phil/Spectral
4ce68e5c1ef2a812ffda5260e9f09f41b85ae0ea
3b078f5f1de251637decf04bd3fc8aa01930a6b3
refs/heads/master
1,685,119,195,535
1,684,169,772,000
1,684,169,772,000
46,450,197
42
13
null
1,505,516,767,000
1,447,883,921,000
Lean
UTF-8
Lean
false
false
8,795
hlean
import .pushout types.pointed2 ..move_to_lib open susp eq pointed function is_equiv lift equiv is_trunc nat namespace susp variables {X X' Y Y' Z : Type*} definition susp_functor_of_fn [constructor] (f : X → Y) : susp X →* susp Y := pmap.mk (susp_functor' f) idp definition susp_pequiv_of_equiv [constructor] (f : X ≃ Y) : susp X ≃* susp Y := pequiv_of_equiv (susp.equiv f) idp definition iterate_susp_iterate_susp_rev (n m : ℕ) (A : Type*) : iterate_susp n (iterate_susp m A) ≃* iterate_susp (m + n) A := begin induction n with n e, { reflexivity }, { exact susp_pequiv e } end definition iterate_susp_pequiv [constructor] (n : ℕ) {X Y : Type*} (f : X ≃* Y) : iterate_susp n X ≃* iterate_susp n Y := begin induction n with n e, { exact f }, { exact susp_pequiv e } end open algebra nat definition iterate_susp_iterate_susp (n m : ℕ) (A : Type*) : iterate_susp n (iterate_susp m A) ≃* iterate_susp (n + m) A := iterate_susp_iterate_susp_rev n m A ⬝e* pequiv_of_eq (ap (λk, iterate_susp k A) (add.comm m n)) definition plift_susp.{u v} : Π(A : Type*), plift.{u v} (susp A) ≃* susp (plift.{u v} A) := begin intro A, calc plift.{u v} (susp A) ≃* susp A : by exact (pequiv_plift (susp A))⁻¹ᵉ* ... ≃* susp (plift.{u v} A) : by exact susp_pequiv (pequiv_plift.{u v} A) end definition is_contr_susp [instance] (A : Type) [H : is_contr A] : is_contr (susp A) := begin apply is_contr.mk north, intro x, induction x, reflexivity, exact merid !center, apply eq_pathover_constant_left_id_right, apply square_of_eq, exact whisker_left idp (ap merid !eq_of_is_contr) end definition loop_susp_pintro_phomotopy {X Y : Type*} {f g : ⅀ X →* Y} (p : f ~* g) : loop_susp_pintro X Y f ~* loop_susp_pintro X Y g := pwhisker_right (loop_susp_unit X) (Ω⇒ p) variables {A₀₀ A₂₀ A₀₂ A₂₂ : Type*} {f₁₀ : A₀₀ →* A₂₀} {f₁₂ : A₀₂ →* A₂₂} {f₀₁ : A₀₀ →* A₀₂} {f₂₁ : A₂₀ →* A₂₂} definition susp_functor_psquare (p : psquare f₁₀ f₁₂ f₀₁ f₂₁) : psquare (⅀→ f₁₀) (⅀→ f₁₂) (⅀→ f₀₁) (⅀→ f₂₁) := !susp_functor_pcompose⁻¹* ⬝* susp_functor_phomotopy p ⬝* !susp_functor_pcompose definition susp_to_loop_psquare (f₁₀ : A₀₀ →* A₂₀) (f₁₂ : A₀₂ →* A₂₂) (f₀₁ : susp A₀₀ →* A₀₂) (f₂₁ : susp A₂₀ →* A₂₂) : psquare (⅀→ f₁₀) f₁₂ f₀₁ f₂₁ → psquare f₁₀ (Ω→ f₁₂) (loop_susp_pintro A₀₀ A₀₂ f₀₁) (loop_susp_pintro A₂₀ A₂₂ f₂₁) := begin intro p, refine pvconcat _ (ap1_psquare p), exact (loop_susp_unit_natural f₁₀)⁻¹* end definition loop_to_susp_square (f₁₀ : A₀₀ →* A₂₀) (f₁₂ : A₀₂ →* A₂₂) (f₀₁ : A₀₀ →* Ω A₀₂) (f₂₁ : A₂₀ →* Ω A₂₂) : psquare f₁₀ (Ω→ f₁₂) f₀₁ f₂₁ → psquare (⅀→ f₁₀) f₁₂ (susp_pelim A₀₀ A₀₂ f₀₁) (susp_pelim A₂₀ A₂₂ f₂₁) := begin intro p, refine susp_functor_psquare p ⬝v* _, exact ptranspose (loop_susp_counit_natural f₁₂) end open pushout unit prod sigma sigma.ops section parameters {A : Type*} {n : ℕ} [HA : is_conn n A] -- we end up not using this, because to prove that the -- composition with the first projection is loop_susp_counit A -- is hideous without HIT computations on path constructors parameter (A) definition pullback_diagonal_prod_of_wedge : susp (Ω A) ≃ Σ (a : A) (w : wedge A A), prod_of_wedge w = (a, a) := begin refine equiv.trans _ (comm_equiv_unc (λ z, prod_of_wedge (prod.pr1 z) = (prod.pr2 z, prod.pr2 z))), apply equiv.symm, apply equiv.trans (sigma_equiv_sigma_right (λ w, sigma_equiv_sigma_right (λ a, prod_eq_equiv (prod_of_wedge w) (a, a)))), apply equiv.trans !pushout.flattening', esimp, fapply pushout.equiv (λ z, ⟨pt, z.2⟩) (λ z, ⟨pt, glue z.1 ▸ z.2⟩) (λ p, star) (λ p, star), { apply equiv.trans !sigma_unit_left, fapply equiv.MK, { intro z, induction z with a w, induction w with p q, exact p ⬝ q⁻¹ }, { intro p, exact ⟨pt, (p, idp)⟩ }, { intro p, reflexivity }, { intro z, induction z with a w, induction w with p q, induction q, reflexivity } }, { fapply equiv.MK, { intro z, exact star }, { intro u, exact ⟨pt, ⟨pt, (idp, idp)⟩ ⟩ }, { intro u, induction u, reflexivity }, { intro z, induction z with a w, induction w with b z, induction z with p q, induction p, esimp at q, induction q, reflexivity } }, { fapply equiv.MK, { intro z, exact star }, { intro u, exact ⟨pt, ⟨pt, (idp, idp)⟩ ⟩ }, { intro u, induction u, reflexivity }, { intro z, induction z with a w, induction w with b z, induction z with p q, induction q, esimp at p, induction p, reflexivity } }, { intro z, induction z with u w, induction u, induction w with a z, induction z with p q, reflexivity }, { intro z, induction z with u w, induction u, induction w with a z, induction z with p q, reflexivity } end parameter {A} -- instead we directly compare the fibers, using flattening twice definition fiber_loop_susp_counit_equiv (a : A) : fiber (loop_susp_counit A) a ≃ fiber prod_of_wedge (a, a) := begin apply equiv.trans !fiber.sigma_char, apply equiv.trans !pushout.flattening', apply equiv.symm, apply equiv.trans !fiber.sigma_char, apply equiv.trans (sigma_equiv_sigma_right (λ w, prod_eq_equiv (prod_of_wedge w) (a, a))), esimp, apply equiv.trans !pushout.flattening', esimp, fapply pushout.equiv (λ z, ⟨pt, z.2⟩) (λ z, ⟨pt, glue z.1 ▸ z.2⟩) (λ z, ⟨star, z.2⟩) (λ z, ⟨star, glue z.1 ▸ z.2⟩), { fapply equiv.MK, { intro w, induction w with u z, induction z with p q, exact ⟨q ⬝ p⁻¹, q⟩ }, { intro z, induction z with p q, apply dpair star, exact (p⁻¹ ⬝ q, q) }, { intro z, induction z with p q, esimp, induction q, esimp, rewrite [idp_con,inv_inv] }, { intro w, induction w with u z, induction u, induction z with p q, esimp, induction q, rewrite [idp_con,inv_inv] } }, { fapply equiv.MK, { intro w, induction w with b z, induction z with p q, exact ⟨star, q⟩ }, { intro z, induction z with u p, induction u, esimp at p, esimp, apply dpair a, esimp, exact (idp, p) }, { intro z, induction z with u p, induction u, reflexivity }, { intro w, induction w with b z, induction z with p q, esimp, induction p, reflexivity } }, { fapply equiv.MK, { intro w, induction w with b z, induction z with p q, exact ⟨star, p⟩ }, { intro z, induction z with u p, induction u, esimp at p, esimp, apply dpair a, esimp, exact (p, idp) }, { intro z, induction z with u p, induction u, reflexivity }, { intro w, induction w with b z, induction z with p q, esimp, induction q, reflexivity } }, { intro w, induction w with u z, induction u, induction z with p q, reflexivity }, { intro w, induction w with u z, induction u, induction z with p q, esimp, induction q, esimp, krewrite prod_transport, fapply sigma_eq, { exact idp }, { esimp, rewrite eq_transport_Fl, rewrite eq_transport_Fl, krewrite elim_glue, krewrite [-ap_compose' pr1 prod_of_wedge (glue star)], krewrite elim_glue, esimp, apply eq_pathover, rewrite idp_con, esimp, apply square_of_eq, rewrite [idp_con,idp_con,inv_inv] } } end include HA open is_conn trunc_index parameter (A) -- connectivity of loop_susp_counit definition is_conn_fun_loop_susp_counit {k : ℕ} (H : k ≤ 2 * n) : is_conn_fun k (loop_susp_counit A) := begin intro a, apply is_conn.is_conn_equiv_closed_rev k (fiber_loop_susp_counit_equiv a), fapply @is_conn.is_conn_of_le (fiber prod_of_wedge (a, a)) k (2 * n) (of_nat_le_of_nat H), assert H : of_nat (2 * n) = of_nat n + of_nat n, { rewrite (of_nat_add_of_nat n n), apply ap of_nat, apply trans (nat.mul_comm 2 n), apply ap (λ k, k + n), exact nat.zero_add n }, rewrite H, exact is_conn_fun_prod_of_wedge n n A A (a, a) end end end susp
d3541edbde3bf76cd94504f8c35beb4183cd8612
367134ba5a65885e863bdc4507601606690974c1
/src/tactic/lint/frontend.lean
197c98117dd379e7d4ff1e627077a3b7279549f0
[ "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
13,850
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Robert Y. Lewis, Gabriel Ebner -/ import tactic.lint.basic /-! # Linter frontend and commands This file defines the linter commands which spot common mistakes in the code. * `#lint`: check all declarations in the current file * `#lint_mathlib`: check all declarations in mathlib (so excluding core or other projects, and also excluding the current file) * `#lint_all`: check all declarations in the environment (the current file and all imported files) For a list of default / non-default linters, see the "Linting Commands" user command doc entry. The command `#list_linters` prints a list of the names of all available linters. You can append a `*` to any command (e.g. `#lint_mathlib*`) to omit the slow tests (4). You can append a `-` to any command (e.g. `#lint_mathlib-`) to run a silent lint that suppresses the output if all checks pass. A silent lint will fail if any test fails. You can append a `+` to any command (e.g. `#lint_mathlib+`) to run a verbose lint that reports the result of each linter, including the successes. You can append a sequence of linter names to any command to run extra tests, in addition to the default ones. e.g. `#lint doc_blame_thm` will run all default tests and `doc_blame_thm`. You can append `only name1 name2 ...` to any command to run a subset of linters, e.g. `#lint only unused_arguments` You can add custom linters by defining a term of type `linter` in the `linter` namespace. A linter defined with the name `linter.my_new_check` can be run with `#lint my_new_check` or `lint only my_new_check`. If you add the attribute `@[linter]` to `linter.my_new_check` it will run by default. Adding the attribute `@[nolint doc_blame unused_arguments]` to a declaration omits it from only the specified linter checks. ## Tags sanity check, lint, cleanup, command, tactic -/ open tactic expr native setup_tactic_parser /-- Verbosity for the linter output. * `low`: only print failing checks, print nothing on success. * `medium`: only print failing checks, print confirmation on success. * `high`: print output of every check. -/ @[derive [decidable_eq, inhabited]] inductive lint_verbosity | low | medium | high /-- `get_checks slow extra use_only` produces a list of linters. `extras` is a list of names that should resolve to declarations with type `linter`. If `use_only` is true, it only uses the linters in `extra`. Otherwise, it uses all linters in the environment tagged with `@[linter]`. If `slow` is false, it only uses the fast default tests. -/ meta def get_checks (slow : bool) (extra : list name) (use_only : bool) : tactic (list (name × linter)) := do default ← if use_only then return [] else attribute.get_instances `linter >>= get_linters, let default := if slow then default else default.filter (λ l, l.2.is_fast), list.append default <$> get_linters extra /-- `lint_core all_decls non_auto_decls checks` applies the linters `checks` to the list of declarations. If `auto_decls` is false for a linter (default) the linter is applied to `non_auto_decls`. If `auto_decls` is true, then it is applied to `all_decls`. The resulting list has one element for each linter, containing the linter as well as a map from declaration name to warning. -/ meta def lint_core (all_decls non_auto_decls : list declaration) (checks : list (name × linter)) : tactic (list (name × linter × rb_map name string)) := do checks.mmap $ λ ⟨linter_name, linter⟩, do let test_decls := if linter.auto_decls then all_decls else non_auto_decls, test_decls ← test_decls.mfilter (λ decl, should_be_linted linter_name decl.to_name), s ← read, let results := test_decls.map_async_chunked $ λ decl, prod.mk decl.to_name $ match linter.test decl s with | result.success w _ := w | result.exception msg _ _ := some $ "LINTER FAILED:\n" ++ msg.elim "(no message)" (λ msg, to_string $ msg ()) end, let results := results.foldl (λ (results : rb_map name string) warning, match warning with | (decl_name, some w) := results.insert decl_name w | (_, none) := results end) mk_rb_map, pure (linter_name, linter, results) /-- Sorts a map with declaration keys as names by line number. -/ meta def sort_results {α} (e : environment) (results : rb_map name α) : list (name × α) := list.reverse $ rb_lmap.values $ rb_lmap.of_list $ results.fold [] $ λ decl linter_warning results, (((e.decl_pos decl).get_or_else ⟨0,0⟩).line, (decl, linter_warning)) :: results /-- Formats a linter warning as `#print` command with comment. -/ meta def print_warning (decl_name : name) (warning : string) : format := "#print " ++ to_fmt decl_name ++ " /- " ++ warning ++ " -/" /-- Formats a map of linter warnings using `print_warning`, sorted by line number. -/ meta def print_warnings (env : environment) (results : rb_map name string) : format := format.intercalate format.line $ (sort_results env results).map $ λ ⟨decl_name, warning⟩, print_warning decl_name warning /-- Formats a map of linter warnings grouped by filename with `-- filename` comments. The first `drop_fn_chars` characters are stripped from the filename. -/ meta def grouped_by_filename (e : environment) (results : rb_map name string) (drop_fn_chars := 0) (formatter: rb_map name string → format) : format := let results := results.fold (rb_map.mk string (rb_map name string)) $ λ decl_name linter_warning results, let fn := (e.decl_olean decl_name).get_or_else "" in results.insert fn (((results.find fn).get_or_else mk_rb_map).insert decl_name linter_warning) in let l := results.to_list.reverse.map (λ ⟨fn, results⟩, ("-- " ++ fn.popn drop_fn_chars ++ "\n" ++ formatter results : format)) in format.intercalate "\n\n" l ++ "\n" /-- Formats the linter results as Lean code with comments and `#print` commands. -/ meta def format_linter_results (env : environment) (results : list (name × linter × rb_map name string)) (decls non_auto_decls : list declaration) (group_by_filename : option nat) (where_desc : string) (slow : bool) (verbose : lint_verbosity) : format := do let formatted_results := results.map $ λ ⟨linter_name, linter, results⟩, let report_str : format := to_fmt "/- The `" ++ to_fmt linter_name ++ "` linter reports: -/\n" in if ¬ results.empty then let warnings := match group_by_filename with | none := print_warnings env results | some dropped := grouped_by_filename env results dropped (print_warnings env) end in report_str ++ "/- " ++ linter.errors_found ++ ": -/\n" ++ warnings ++ "\n" else if verbose = lint_verbosity.high then "/- OK: " ++ linter.no_errors_found ++ ". -/" else format.nil, let s := format.intercalate "\n" (formatted_results.filter (λ f, ¬ f.is_nil)), let s := if verbose = lint_verbosity.low then s else format!"/- Checking {non_auto_decls.length} declarations (plus {decls.length - non_auto_decls.length} automatically generated ones) {where_desc} -/\n\n" ++ s, let s := if slow then s else s ++ "/- (slow tests skipped) -/\n", s /-- The common denominator of `#lint[|mathlib|all]`. The different commands have different configurations for `l`, `group_by_filename` and `where_desc`. If `slow` is false, doesn't do the checks that take a lot of time. If `verbose` is false, it will suppress messages from passing checks. By setting `checks` you can customize which checks are performed. Returns a `name_set` containing the names of all declarations that fail any check in `check`, and a `format` object describing the failures. -/ meta def lint_aux (decls : list declaration) (group_by_filename : option nat) (where_desc : string) (slow : bool) (verbose : lint_verbosity) (checks : list (name × linter)) : tactic (name_set × format) := do e ← get_env, let non_auto_decls := decls.filter (λ d, ¬ d.is_auto_or_internal e), results ← lint_core decls non_auto_decls checks, let s := format_linter_results e results decls non_auto_decls group_by_filename where_desc slow verbose, let ns := name_set.of_list (do (_,_,rs) ← results, rs.keys), pure (ns, s) /-- Return the message printed by `#lint` and a `name_set` containing all declarations that fail. -/ meta def lint (slow : bool := tt) (verbose : lint_verbosity := lint_verbosity.medium) (extra : list name := []) (use_only : bool := ff) : tactic (name_set × format) := do checks ← get_checks slow extra use_only, e ← get_env, let l := e.filter (λ d, e.in_current_file d.to_name), lint_aux l none "in the current file" slow verbose checks /-- Returns the declarations considered by the mathlib linter. -/ meta def lint_mathlib_decls : tactic (list declaration) := do e ← get_env, ml ← get_mathlib_dir, pure $ e.filter $ λ d, e.is_prefix_of_file ml d.to_name /-- Return the message printed by `#lint_mathlib` and a `name_set` containing all declarations that fail. -/ meta def lint_mathlib (slow : bool := tt) (verbose : lint_verbosity := lint_verbosity.medium) (extra : list name := []) (use_only : bool := ff) : tactic (name_set × format) := do checks ← get_checks slow extra use_only, decls ← lint_mathlib_decls, mathlib_path_len ← string.length <$> get_mathlib_dir, lint_aux decls mathlib_path_len "in mathlib (only in imported files)" slow verbose checks /-- Return the message printed by `#lint_all` and a `name_set` containing all declarations that fail. -/ meta def lint_all (slow : bool := tt) (verbose : lint_verbosity := lint_verbosity.medium) (extra : list name := []) (use_only : bool := ff) : tactic (name_set × format) := do checks ← get_checks slow extra use_only, e ← get_env, let l := e.get_decls, lint_aux l (some 0) "in all imported files (including this one)" slow verbose checks /-- Parses an optional `only`, followed by a sequence of zero or more identifiers. Prepends `linter.` to each of these identifiers. -/ private meta def parse_lint_additions : parser (bool × list name) := prod.mk <$> only_flag <*> (list.map (name.append `linter) <$> ident_*) /-- Parses a "-" or "+", returning `lint_verbosity.low` or `lint_verbosity.high` respectively, or returns `none`. -/ private meta def parse_verbosity : parser (option lint_verbosity) := tk "-" >> return lint_verbosity.low <|> tk "+" >> return lint_verbosity.high <|> return none /-- The common denominator of `lint_cmd`, `lint_mathlib_cmd`, `lint_all_cmd` -/ private meta def lint_cmd_aux (scope : bool → lint_verbosity → list name → bool → tactic (name_set × format)) : parser unit := do verbosity ← parse_verbosity, fast_only ← optional (tk "*"), -- allow either order of *- verbosity ← if verbosity.is_some then return verbosity else parse_verbosity, let verbosity := verbosity.get_or_else lint_verbosity.medium, (use_only, extra) ← parse_lint_additions, (failed, s) ← scope fast_only.is_none verbosity extra use_only, when (¬ s.is_nil) $ trace s, when (verbosity = lint_verbosity.low ∧ ¬ failed.empty) $ fail "Linting did not succeed", when (verbosity = lint_verbosity.medium ∧ failed.empty) $ trace "/- All linting checks passed! -/" /-- The command `#lint` at the bottom of a file will warn you about some common mistakes in that file. Usage: `#lint`, `#lint linter_1 linter_2`, `#lint only linter_1 linter_2`. `#lint-` will suppress the output if all checks pass. `#lint+` will enable verbose output. Use the command `#list_linters` to see all available linters. -/ @[user_command] meta def lint_cmd (_ : parse $ tk "#lint") : parser unit := lint_cmd_aux @lint /-- The command `#lint_mathlib` checks all of mathlib for certain mistakes. Usage: `#lint_mathlib`, `#lint_mathlib linter_1 linter_2`, `#lint_mathlib only linter_1 linter_2`. `#lint_mathlib-` will suppress the output if all checks pass. `lint_mathlib+` will enable verbose output. Use the command `#list_linters` to see all available linters. -/ @[user_command] meta def lint_mathlib_cmd (_ : parse $ tk "#lint_mathlib") : parser unit := lint_cmd_aux @lint_mathlib /-- The command `#lint_all` checks all imported files for certain mistakes. Usage: `#lint_all`, `#lint_all linter_1 linter_2`, `#lint_all only linter_1 linter_2`. `#lint_all-` will suppress the output if all checks pass. `lint_all+` will enable verbose output. Use the command `#list_linters` to see all available linters. -/ @[user_command] meta def lint_all_cmd (_ : parse $ tk "#lint_all") : parser unit := lint_cmd_aux @lint_all /-- The command `#list_linters` prints a list of all available linters. -/ @[user_command] meta def list_linters (_ : parse $ tk "#list_linters") : parser unit := do env ← get_env, let ns := env.decl_filter_map $ λ dcl, if (dcl.to_name.get_prefix = `linter) && (dcl.type = `(linter)) then some dcl.to_name else none, trace "Available linters:\n linters marked with (*) are in the default lint set\n", ns.mmap' $ λ n, do b ← has_attribute' `linter n, trace $ n.pop_prefix.to_string ++ if b then " (*)" else "" /-- Invoking the hole command `lint` ("Find common mistakes in current file") will print text that indicates mistakes made in the file above the command. It is equivalent to copying and pasting the output of `#lint`. On large files, it may take some time before the output appears. -/ @[hole_command] meta def lint_hole_cmd : hole_command := { name := "Lint", descr := "Lint: Find common mistakes in current file.", action := λ es, do (_, s) ← lint, return [(s.to_string,"")] } add_tactic_doc { name := "Lint", category := doc_category.hole_cmd, decl_names := [`lint_hole_cmd], tags := ["linting"] }
fc32fafa7361e1f62833dba0388f007ef54a4221
77c5b91fae1b966ddd1db969ba37b6f0e4901e88
/src/topology/sheaves/sheaf_condition/unique_gluing.lean
e3dd76baefb72286dc1399ff710c1193b1584647
[ "Apache-2.0" ]
permissive
dexmagic/mathlib
ff48eefc56e2412429b31d4fddd41a976eb287ce
7a5d15a955a92a90e1d398b2281916b9c41270b2
refs/heads/master
1,693,481,322,046
1,633,360,193,000
1,633,360,193,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,000
lean
/- Copyright (c) 2021 Justus Springer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Justus Springer -/ import algebra.category.CommRing.limits import topology.sheaves.forget import topology.sheaves.sheaf import category_theory.limits.shapes.types import category_theory.types /-! # The sheaf condition in terms of unique gluings We provide an alternative formulation of the sheaf condition in terms of unique gluings. We work with sheaves valued in a concrete category `C` admitting all limits, whose forgetful functor `C ⥤ Type` preserves limits and reflects isomorphisms. The usual categories of algebraic structures, such as `Mon`, `AddCommGroup`, `Ring`, `CommRing` etc. are all examples of this kind of category. A presheaf `F : presheaf C X` satisfies the sheaf condition if and only if, for every compatible family of sections `sf : Π i : ι, F.obj (op (U i))`, there exists a unique gluing `s : F.obj (op (supr U))`. Here, the family `sf` is called compatible, if for all `i j : ι`, the restrictions of `sf i` and `sf j` to `U i ⊓ U j` agree. A section `s : F.obj (op (supr U))` is a gluing for the family `sf`, if `s` restricts to `sf i` on `U i` for all `i : ι` We show that the sheaf condition in terms of unique gluings is equivalent to the definition in terms of equalizers. Our approach is as follows: First, we show them to be equivalent for `Type`-valued presheaves. Then we use that composing a presheaf with a limit-preserving and isomorphism-reflecting functor leaves the sheaf condition invariant, as shown in `topology/sheaves/forget.lean`. -/ noncomputable theory open Top open Top.presheaf open Top.presheaf.sheaf_condition_equalizer_products open category_theory open category_theory.limits open topological_space open topological_space.opens open opposite universes u v variables {C : Type u} [category.{v} C] [concrete_category.{v} C] namespace Top namespace presheaf section local attribute [instance] concrete_category.has_coe_to_sort concrete_category.has_coe_to_fun variables {X : Top.{v}} (F : presheaf C X) {ι : Type v} (U : ι → opens X) /-- A family of sections `sf` is compatible, if the restrictions of `sf i` and `sf j` to `U i ⊓ U j` agree, for all `i` and `j` -/ def is_compatible (sf : Π i : ι, F.obj (op (U i))) : Prop := ∀ i j : ι, F.map (inf_le_left (U i) (U j)).op (sf i) = F.map (inf_le_right (U i) (U j)).op (sf j) /-- A section `s` is a gluing for a family of sections `sf` if it restricts to `sf i` on `U i`, for all `i` -/ def is_gluing (sf : Π i : ι, F.obj (op (U i))) (s : F.obj (op (supr U))) : Prop := ∀ i : ι, F.map (opens.le_supr U i).op s = sf i /-- The subtype of all gluings for a given family of sections -/ @[nolint has_inhabited_instance] def gluing (sf : Π i : ι, F.obj (op (U i))) : Type v := {s : F.obj (op (supr U)) // is_gluing F U sf s} /-- The sheaf condition in terms of unique gluings. A presheaf `F : presheaf C X` satisfies this sheaf condition if and only if, for every compatible family of sections `sf : Π i : ι, F.obj (op (U i))`, there exists a unique gluing `s : F.obj (op (supr U))`. We prove this to be equivalent to the usual one below in `sheaf_condition_equiv_sheaf_condition_unique_gluing` -/ @[derive subsingleton, nolint has_inhabited_instance] def sheaf_condition_unique_gluing : Type (v+1) := Π ⦃ι : Type v⦄ (U : ι → opens X) (sf : Π i : ι, F.obj (op (U i))), is_compatible F U sf → unique (gluing F U sf) end section type_valued variables {X : Top.{v}} (F : presheaf (Type v) X) {ι : Type v} (U : ι → opens X) /-- For presheaves of types, terms of `pi_opens F U` are just families of sections. -/ def pi_opens_iso_sections_family : pi_opens F U ≅ Π i : ι, F.obj (op (U i)) := limits.is_limit.cone_point_unique_up_to_iso (limit.is_limit (discrete.functor (λ i : ι, F.obj (op (U i))))) ((types.product_limit_cone (λ i : ι, F.obj (op (U i)))).is_limit) /-- Under the isomorphism `pi_opens_iso_sections_family`, compatibility of sections is the same as being equalized by the arrows `left_res` and `right_res` of the equalizer diagram. -/ lemma compatible_iff_left_res_eq_right_res (sf : pi_opens F U) : is_compatible F U ((pi_opens_iso_sections_family F U).hom sf) ↔ left_res F U sf = right_res F U sf := begin split ; intros h, { ext ⟨i, j⟩, rw [left_res, types.limit.lift_π_apply, fan.mk_π_app, right_res, types.limit.lift_π_apply, fan.mk_π_app], exact h i j, }, { intros i j, convert congr_arg (limits.pi.π (λ p : ι × ι, F.obj (op (U p.1 ⊓ U p.2))) (i,j)) h, { rw [left_res, types.pi_lift_π_apply], refl }, { rw [right_res, types.pi_lift_π_apply], refl } } end /-- Under the isomorphism `pi_opens_iso_sections_family`, being a gluing of a family of sections `sf` is the same as lying in the preimage of `res` (the leftmost arrow of the equalizer diagram). -/ @[simp] lemma is_gluing_iff_eq_res (sf : pi_opens F U) (s : F.obj (op (supr U))): is_gluing F U ((pi_opens_iso_sections_family F U).hom sf) s ↔ res F U s = sf := begin split ; intros h, { ext i, rw [res, types.limit.lift_π_apply, fan.mk_π_app], exact h i, }, { intro i, convert congr_arg (limits.pi.π (λ i : ι, F.obj (op (U i))) i) h, rw [res, types.pi_lift_π_apply], refl }, end /-- The "equalizer" sheaf condition can be obtained from the sheaf condition in terms of unique gluings. -/ def sheaf_condition_of_sheaf_condition_unique_gluing_types : F.sheaf_condition_unique_gluing → F.sheaf_condition := λ Fsh ι U, begin refine fork.is_limit.mk' _ (λ s, ⟨_, _, _⟩) ; dsimp, { intro x, refine (Fsh U ((pi_opens_iso_sections_family F U).hom (s.ι x)) _).default.1, apply (compatible_iff_left_res_eq_right_res F U (s.ι x)).mpr, convert congr_fun s.condition x, }, { ext i x, simp [res], let t : gluing F U _ := _, exact t.2 i }, { intros m hm, ext x, refine congr_arg subtype.val ((Fsh U ((pi_opens_iso_sections_family F U).hom (s.ι x)) _).uniq ⟨m x, _⟩), apply (is_gluing_iff_eq_res F U _ _).mpr, exact congr_fun hm x }, end /-- The sheaf condition in terms of unique gluings can be obtained from the usual "equalizer" sheaf condition. -/ def sheaf_condition_unique_gluing_of_sheaf_condition_types : F.sheaf_condition → F.sheaf_condition_unique_gluing := λ Fsh ι U sf hsf, { default := begin let sf' := (pi_opens_iso_sections_family F U).inv sf, have hsf' : left_res F U sf' = right_res F U sf' := by rwa [← compatible_iff_left_res_eq_right_res F U sf', inv_hom_id_apply], choose s s_spec s_uniq using types.unique_of_type_equalizer _ _ (Fsh U) sf' hsf', use s, convert (is_gluing_iff_eq_res F U _ _).mpr s_spec, rw inv_hom_id_apply end, uniq := begin intro s, /- Unfortunately, type inference doesn't yet know about the `inhabited` instance of `gluing F U sf` We therefore introduce a metavariable and use unification to get our hands on the default value of `gluing F U sf`. -/ let t : F.gluing U sf := _, change s = t, ext, let sf' := (pi_opens_iso_sections_family F U).inv sf, have hsf' : left_res F U sf' = right_res F U sf' := by rwa [← compatible_iff_left_res_eq_right_res F U sf', inv_hom_id_apply], choose gl gl_spec gl_uniq using types.unique_of_type_equalizer _ _ (Fsh U) sf' hsf', refine eq.trans (gl_uniq s.1 _) (gl_uniq t.1 _).symm ; rw [← is_gluing_iff_eq_res F U _ _, inv_hom_id_apply], exacts [s.2, t.2] end } /-- For type-valued presheaves, the sheaf condition in terms of unique gluings is equivalent to the usual sheaf condition in terms of equalizer diagrams. -/ def sheaf_condition_equiv_sheaf_condition_unique_gluing_types : F.sheaf_condition ≃ F.sheaf_condition_unique_gluing := equiv_of_subsingleton_of_subsingleton F.sheaf_condition_unique_gluing_of_sheaf_condition_types F.sheaf_condition_of_sheaf_condition_unique_gluing_types end type_valued section local attribute [instance] concrete_category.has_coe_to_sort concrete_category.has_coe_to_fun variables [has_limits C] [reflects_isomorphisms (forget C)] [preserves_limits (forget C)] variables {X : Top.{v}} (F : presheaf C X) {ι : Type v} (U : ι → opens X) /-- For presheaves valued in a concrete category, whose forgetful functor reflects isomorphisms and preserves limits, the sheaf condition in terms of unique gluings is equivalent to the usual one in terms of equalizer diagrams. -/ def sheaf_condition_equiv_sheaf_condition_unique_gluing : F.sheaf_condition ≃ F.sheaf_condition_unique_gluing := equiv.trans (sheaf_condition_equiv_sheaf_condition_comp (forget C) F) (sheaf_condition_equiv_sheaf_condition_unique_gluing_types (F ⋙ forget C)) /-- A slightly more convenient way of obtaining the sheaf condition for sheaves of algebraic structures. -/ def sheaf_condition_of_exists_unique_gluing (h : ∀ ⦃ι : Type v⦄ (U : ι → opens X) (sf : Π i : ι, F.obj (op (U i))), is_compatible F U sf → ∃! s : F.obj (op (supr U)), is_gluing F U sf s) : F.sheaf_condition := (sheaf_condition_equiv_sheaf_condition_unique_gluing F).inv_fun $ λ ι U sf hsf, { default := begin choose gl gl_spec gl_uniq using h U sf hsf, exact ⟨gl, gl_spec⟩ end, uniq := begin intro s, let t : F.gluing U sf := _, change s = t, ext, choose gl gl_spec gl_uniq using h U sf hsf, refine eq.trans (gl_uniq s.1 _) (gl_uniq t.1 _).symm, exacts [s.2, t.2] end, } end end presheaf namespace sheaf open presheaf open category_theory section local attribute [instance] concrete_category.has_coe_to_sort concrete_category.has_coe_to_fun variables [has_limits C] [reflects_isomorphisms (concrete_category.forget C)] variables [preserves_limits (concrete_category.forget C)] variables {X : Top.{v}} (F : sheaf C X) {ι : Type v} (U : ι → opens X) /-- A more convenient way of obtaining a unique gluing of sections for a sheaf. -/ lemma exists_unique_gluing (sf : Π i : ι, F.presheaf.obj (op (U i))) (h : is_compatible F.presheaf U sf ) : ∃! s : F.presheaf.obj (op (supr U)), is_gluing F.presheaf U sf s := begin have := sheaf_condition_equiv_sheaf_condition_unique_gluing _ F.sheaf_condition U sf h, refine ⟨this.default.1, this.default.2, _⟩, intros s hs, exact congr_arg subtype.val (this.uniq ⟨s, hs⟩), end /-- In this version of the lemma, the inclusion homs `iUV` can be specified directly by the user, which can be more convenient in practice. -/ lemma exists_unique_gluing' (V : opens X) (iUV : Π i : ι, U i ⟶ V) (hcover : V ≤ supr U) (sf : Π i : ι, F.presheaf.obj (op (U i))) (h : is_compatible F.presheaf U sf) : ∃! s : F.presheaf.obj (op V), ∀ i : ι, F.presheaf.map (iUV i).op s = sf i := begin have V_eq_supr_U : V = supr U := le_antisymm hcover (supr_le (λ i, (iUV i).le)), obtain ⟨gl, gl_spec, gl_uniq⟩ := F.exists_unique_gluing U sf h, refine ⟨F.presheaf.map (eq_to_hom V_eq_supr_U).op gl, _, _⟩, { intro i, rw [← comp_apply, ← F.presheaf.map_comp], exact gl_spec i }, { intros gl' gl'_spec, convert congr_arg _ (gl_uniq (F.presheaf.map (eq_to_hom V_eq_supr_U.symm).op gl') (λ i,_)) ; rw [← comp_apply, ← F.presheaf.map_comp], { rw [eq_to_hom_op, eq_to_hom_op, eq_to_hom_trans, eq_to_hom_refl, F.presheaf.map_id, id_apply] }, { convert gl'_spec i } } end @[ext] lemma eq_of_locally_eq (s t : F.presheaf.obj (op (supr U))) (h : ∀ i, F.presheaf.map (opens.le_supr U i).op s = F.presheaf.map (opens.le_supr U i).op t) : s = t := begin let sf : Π i : ι, F.presheaf.obj (op (U i)) := λ i, F.presheaf.map (opens.le_supr U i).op s, have sf_compatible : is_compatible _ U sf, { intros i j, simp_rw [← comp_apply, ← F.presheaf.map_comp], refl }, obtain ⟨gl, -, gl_uniq⟩ := F.exists_unique_gluing U sf sf_compatible, transitivity gl, { apply gl_uniq, intro i, refl }, { symmetry, apply gl_uniq, intro i, rw ← h }, end /-- In this version of the lemma, the inclusion homs `iUV` can be specified directly by the user, which can be more convenient in practice. -/ lemma eq_of_locally_eq' (V : opens X) (iUV : Π i : ι, U i ⟶ V) (hcover : V ≤ supr U) (s t : F.presheaf.obj (op V)) (h : ∀ i, F.presheaf.map (iUV i).op s = F.presheaf.map (iUV i).op t) : s = t := begin have V_eq_supr_U : V = supr U := le_antisymm hcover (supr_le (λ i, (iUV i).le)), suffices : F.presheaf.map (eq_to_hom V_eq_supr_U.symm).op s = F.presheaf.map (eq_to_hom V_eq_supr_U.symm).op t, { convert congr_arg (F.presheaf.map (eq_to_hom V_eq_supr_U).op) this ; rw [← comp_apply, ← F.presheaf.map_comp, eq_to_hom_op, eq_to_hom_op, eq_to_hom_trans, eq_to_hom_refl, F.presheaf.map_id, id_apply] }, apply eq_of_locally_eq, intro i, rw [← comp_apply, ← comp_apply, ← F.presheaf.map_comp], convert h i, end end end sheaf end Top
e5b535784d351c2d6dada99beace87e36332c576
4950bf76e5ae40ba9f8491647d0b6f228ddce173
/src/linear_algebra/basic.lean
788d195a997fa35baaf0485ad65f9b842fb240ae
[ "Apache-2.0" ]
permissive
ntzwq/mathlib
ca50b21079b0a7c6781c34b62199a396dd00cee2
36eec1a98f22df82eaccd354a758ef8576af2a7f
refs/heads/master
1,675,193,391,478
1,607,822,996,000
1,607,822,996,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
105,281
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Kevin Buzzard, Yury Kudryashov -/ import algebra.big_operators.pi import algebra.module.pi import algebra.module.prod import algebra.module.submodule import algebra.group.prod import data.finsupp.basic import algebra.pointwise /-! # Linear algebra This file defines the basics of linear algebra. It sets up the "categorical/lattice structure" of modules over a ring, submodules, and linear maps. If `p` and `q` are submodules of a module, `p ≤ q` means that `p ⊆ q`. Many of the relevant definitions, including `module`, `submodule`, and `linear_map`, are found in `src/algebra/module`. ## Main definitions * Many constructors for linear maps, including `prod` and `coprod` * `submodule.span s` is defined to be the smallest submodule containing the set `s`. * If `p` is a submodule of `M`, `submodule.quotient p` is the quotient of `M` with respect to `p`: that is, elements of `M` are identified if their difference is in `p`. This is itself a module. * The kernel `ker` and range `range` of a linear map are submodules of the domain and codomain respectively. * The general linear group is defined to be the group of invertible linear maps from `M` to itself. ## Main statements * The first and second isomorphism laws for modules are proved as `quot_ker_equiv_range` and `quotient_inf_equiv_sup_quotient`. ## Notations * We continue to use the notation `M →ₗ[R] M₂` for the type of linear maps from `M` to `M₂` over the ring `R`. * We introduce the notations `M ≃ₗ M₂` and `M ≃ₗ[R] M₂` for `linear_equiv M M₂`. In the first, the ring `R` is implicit. ## Implementation notes We note that, when constructing linear maps, it is convenient to use operations defined on bundled maps (`prod`, `coprod`, arithmetic operations like `+`) instead of defining a function and proving it is linear. ## Tags linear algebra, vector space, module -/ open function open_locale big_operators reserve infix ` ≃ₗ `:25 universes u v w x y z u' v' w' y' variables {R : Type u} {K : Type u'} {M : Type v} {V : Type v'} {M₂ : Type w} {V₂ : Type w'} variables {M₃ : Type y} {V₃ : Type y'} {M₄ : Type z} {ι : Type x} namespace finsupp lemma smul_sum {α : Type u} {β : Type v} {R : Type w} {M : Type y} [has_zero β] [semiring R] [add_comm_monoid M] [semimodule R M] {v : α →₀ β} {c : R} {h : α → β → M} : c • (v.sum h) = v.sum (λa b, c • h a b) := finset.smul_sum end finsupp section open_locale classical /-- decomposing `x : ι → R` as a sum along the canonical basis -/ lemma pi_eq_sum_univ {ι : Type u} [fintype ι] {R : Type v} [semiring R] (x : ι → R) : x = ∑ i, x i • (λj, if i = j then 1 else 0) := by { ext, simp } end /-! ### Properties of linear maps -/ namespace linear_map section add_comm_monoid variables [semiring R] variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M₄] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] [semimodule R M₄] variables (f g : M →ₗ[R] M₂) include R @[simp] theorem comp_id : f.comp id = f := linear_map.ext $ λ x, rfl @[simp] theorem id_comp : id.comp f = f := linear_map.ext $ λ x, rfl theorem comp_assoc (g : M₂ →ₗ[R] M₃) (h : M₃ →ₗ[R] M₄) : (h.comp g).comp f = h.comp (g.comp f) := rfl /-- The restriction of a linear map `f : M → M₂` to a submodule `p ⊆ M` gives a linear map `p → M₂`. -/ def dom_restrict (f : M →ₗ[R] M₂) (p : submodule R M) : p →ₗ[R] M₂ := f.comp p.subtype @[simp] lemma dom_restrict_apply (f : M →ₗ[R] M₂) (p : submodule R M) (x : p) : f.dom_restrict p x = f x := rfl /-- A linear map `f : M₂ → M` whose values lie in a submodule `p ⊆ M` can be restricted to a linear map M₂ → p. -/ def cod_restrict (p : submodule R M) (f : M₂ →ₗ[R] M) (h : ∀c, f c ∈ p) : M₂ →ₗ[R] p := by refine {to_fun := λc, ⟨f c, h c⟩, ..}; intros; apply set_coe.ext; simp @[simp] theorem cod_restrict_apply (p : submodule R M) (f : M₂ →ₗ[R] M) {h} (x : M₂) : (cod_restrict p f h x : M) = f x := rfl @[simp] lemma comp_cod_restrict (p : submodule R M₂) (h : ∀b, f b ∈ p) (g : M₃ →ₗ[R] M) : (cod_restrict p f h).comp g = cod_restrict p (f.comp g) (assume b, h _) := ext $ assume b, rfl @[simp] lemma subtype_comp_cod_restrict (p : submodule R M₂) (h : ∀b, f b ∈ p) : p.subtype.comp (cod_restrict p f h) = f := ext $ assume b, rfl /-- Restrict domain and codomain of an endomorphism. -/ def restrict (f : M →ₗ[R] M) {p : submodule R M} (hf : ∀ x ∈ p, f x ∈ p) : p →ₗ[R] p := (f.dom_restrict p).cod_restrict p $ submodule.forall.2 hf lemma restrict_apply {f : M →ₗ[R] M} {p : submodule R M} (hf : ∀ x ∈ p, f x ∈ p) (x : p) : f.restrict hf x = ⟨f x, hf x.1 x.2⟩ := rfl lemma subtype_comp_restrict {f : M →ₗ[R] M} {p : submodule R M} (hf : ∀ x ∈ p, f x ∈ p) : p.subtype.comp (f.restrict hf) = f.dom_restrict p := rfl lemma restrict_eq_cod_restrict_dom_restrict {f : M →ₗ[R] M} {p : submodule R M} (hf : ∀ x ∈ p, f x ∈ p) : f.restrict hf = (f.dom_restrict p).cod_restrict p (λ x, hf x.1 x.2) := rfl lemma restrict_eq_dom_restrict_cod_restrict {f : M →ₗ[R] M} {p : submodule R M} (hf : ∀ x, f x ∈ p) : f.restrict (λ x _, hf x) = (f.cod_restrict p hf).dom_restrict p := rfl /-- The constant 0 map is linear. -/ instance : has_zero (M →ₗ[R] M₂) := ⟨⟨λ _, 0, by simp, by simp⟩⟩ instance : inhabited (M →ₗ[R] M₂) := ⟨0⟩ @[simp] lemma zero_apply (x : M) : (0 : M →ₗ[R] M₂) x = 0 := rfl @[simp] lemma default_def : default (M →ₗ[R] M₂) = 0 := rfl instance unique_of_left [subsingleton M] : unique (M →ₗ[R] M₂) := { uniq := λ f, ext $ λ x, by rw [subsingleton.elim x 0, map_zero, map_zero], .. linear_map.inhabited } instance unique_of_right [subsingleton M₂] : unique (M →ₗ[R] M₂) := coe_injective.unique /-- The sum of two linear maps is linear. -/ instance : has_add (M →ₗ[R] M₂) := ⟨λ f g, ⟨λ b, f b + g b, by simp [add_comm, add_left_comm], by simp [smul_add]⟩⟩ @[simp] lemma add_apply (x : M) : (f + g) x = f x + g x := rfl /-- The type of linear maps is an additive monoid. -/ instance : add_comm_monoid (M →ₗ[R] M₂) := by refine {zero := 0, add := (+), ..}; intros; ext; simp [add_comm, add_left_comm] instance linear_map_apply_is_add_monoid_hom (a : M) : is_add_monoid_hom (λ f : M →ₗ[R] M₂, f a) := { map_add := λ f g, linear_map.add_apply f g a, map_zero := rfl } lemma add_comp (g : M₂ →ₗ[R] M₃) (h : M₂ →ₗ[R] M₃) : (h + g).comp f = h.comp f + g.comp f := rfl lemma comp_add (g : M →ₗ[R] M₂) (h : M₂ →ₗ[R] M₃) : h.comp (f + g) = h.comp f + h.comp g := by { ext, simp } lemma sum_apply (t : finset ι) (f : ι → M →ₗ[R] M₂) (b : M) : (∑ d in t, f d) b = ∑ d in t, f d b := (t.sum_hom (λ g : M →ₗ[R] M₂, g b)).symm /-- `λb, f b • x` is a linear map. -/ def smul_right (f : M₂ →ₗ[R] R) (x : M) : M₂ →ₗ[R] M := ⟨λb, f b • x, by simp [add_smul], by simp [smul_smul]⟩. @[simp] theorem smul_right_apply (f : M₂ →ₗ[R] R) (x : M) (c : M₂) : (smul_right f x : M₂ → M) c = f c • x := rfl instance : has_one (M →ₗ[R] M) := ⟨linear_map.id⟩ instance : has_mul (M →ₗ[R] M) := ⟨linear_map.comp⟩ lemma mul_eq_comp (f g : M →ₗ[R] M) : f * g = f.comp g := rfl @[simp] lemma one_app (x : M) : (1 : M →ₗ[R] M) x = x := rfl @[simp] lemma mul_app (A B : M →ₗ[R] M) (x : M) : (A * B) x = A (B x) := rfl @[simp] theorem comp_zero : f.comp (0 : M₃ →ₗ[R] M) = 0 := ext $ assume c, by rw [comp_apply, zero_apply, zero_apply, f.map_zero] @[simp] theorem zero_comp : (0 : M₂ →ₗ[R] M₃).comp f = 0 := rfl @[norm_cast] lemma coe_fn_sum {ι : Type*} (t : finset ι) (f : ι → M →ₗ[R] M₂) : ⇑(∑ i in t, f i) = ∑ i in t, (f i : M → M₂) := add_monoid_hom.map_sum ⟨@to_fun R M M₂ _ _ _ _ _, rfl, λ x y, rfl⟩ _ _ instance : monoid (M →ₗ[R] M) := by refine {mul := (*), one := 1, ..}; { intros, apply linear_map.ext, simp {proj := ff} } section open_locale classical /-- A linear map `f` applied to `x : ι → R` can be computed using the image under `f` of elements of the canonical basis. -/ lemma pi_apply_eq_sum_univ [fintype ι] (f : (ι → R) →ₗ[R] M) (x : ι → R) : f x = ∑ i, x i • (f (λj, if i = j then 1 else 0)) := begin conv_lhs { rw [pi_eq_sum_univ x, f.map_sum] }, apply finset.sum_congr rfl (λl hl, _), rw f.map_smul end end section variables (R M M₂) /-- The first projection of a product is a linear map. -/ def fst : M × M₂ →ₗ[R] M := ⟨prod.fst, λ x y, rfl, λ x y, rfl⟩ /-- The second projection of a product is a linear map. -/ def snd : M × M₂ →ₗ[R] M₂ := ⟨prod.snd, λ x y, rfl, λ x y, rfl⟩ end @[simp] theorem fst_apply (x : M × M₂) : fst R M M₂ x = x.1 := rfl @[simp] theorem snd_apply (x : M × M₂) : snd R M M₂ x = x.2 := rfl /-- The prod of two linear maps is a linear map. -/ def prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : M →ₗ[R] M₂ × M₃ := { to_fun := λ x, (f x, g x), map_add' := λ x y, by simp only [prod.mk_add_mk, map_add], map_smul' := λ c x, by simp only [prod.smul_mk, map_smul] } @[simp] theorem prod_apply (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) (x : M) : prod f g x = (f x, g x) := rfl @[simp] theorem fst_prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : (fst R M₂ M₃).comp (prod f g) = f := by ext; refl @[simp] theorem snd_prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : (snd R M₂ M₃).comp (prod f g) = g := by ext; refl @[simp] theorem pair_fst_snd : prod (fst R M M₂) (snd R M M₂) = linear_map.id := by ext; refl section variables (R M M₂) /-- The left injection into a product is a linear map. -/ def inl : M →ₗ[R] M × M₂ := by refine ⟨add_monoid_hom.inl _ _, _, _⟩; intros; simp /-- The right injection into a product is a linear map. -/ def inr : M₂ →ₗ[R] M × M₂ := by refine ⟨add_monoid_hom.inr _ _, _, _⟩; intros; simp end @[simp] theorem inl_apply (x : M) : inl R M M₂ x = (x, 0) := rfl @[simp] theorem inr_apply (x : M₂) : inr R M M₂ x = (0, x) := rfl theorem inl_injective : function.injective (inl R M M₂) := λ _, by simp theorem inr_injective : function.injective (inr R M M₂) := λ _, by simp /-- The coprod function `λ x : M × M₂, f x.1 + g x.2` is a linear map. -/ def coprod (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) : M × M₂ →ₗ[R] M₃ := { to_fun := λ x, f x.1 + g x.2, map_add' := λ x y, by simp only [map_add, prod.snd_add, prod.fst_add]; cc, map_smul' := λ x y, by simp only [smul_add, prod.smul_snd, prod.smul_fst, map_smul] } @[simp] theorem coprod_apply (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) (x : M) (y : M₂) : coprod f g (x, y) = f x + g y := rfl @[simp] theorem coprod_inl (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) : (coprod f g).comp (inl R M M₂) = f := by ext; simp only [map_zero, add_zero, coprod_apply, inl_apply, comp_apply] @[simp] theorem coprod_inr (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) : (coprod f g).comp (inr R M M₂) = g := by ext; simp only [map_zero, coprod_apply, inr_apply, zero_add, comp_apply] @[simp] theorem coprod_inl_inr : coprod (inl R M M₂) (inr R M M₂) = linear_map.id := by ext ⟨x, y⟩; simp only [prod.mk_add_mk, add_zero, id_apply, coprod_apply, inl_apply, inr_apply, zero_add] theorem fst_eq_coprod : fst R M M₂ = coprod linear_map.id 0 := by ext ⟨x, y⟩; simp theorem snd_eq_coprod : snd R M M₂ = coprod 0 linear_map.id := by ext ⟨x, y⟩; simp theorem inl_eq_prod : inl R M M₂ = prod linear_map.id 0 := rfl theorem inr_eq_prod : inr R M M₂ = prod 0 linear_map.id := rfl /-- `prod.map` of two linear maps. -/ def prod_map (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₄) : (M × M₂) →ₗ[R] (M₃ × M₄) := (f.comp (fst R M M₂)).prod (g.comp (snd R M M₂)) @[simp] theorem prod_map_apply (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₄) (x) : f.prod_map g x = (f x.1, g x.2) := rfl end add_comm_monoid section add_comm_group variables [semiring R] [add_comm_monoid M] [add_comm_group M₂] [add_comm_group M₃] [add_comm_group M₄] [semimodule R M] [semimodule R M₂] [semimodule R M₃] [semimodule R M₄] (f g : M →ₗ[R] M₂) /-- The negation of a linear map is linear. -/ instance : has_neg (M →ₗ[R] M₂) := ⟨λ f, ⟨λ b, - f b, by simp [add_comm], by simp⟩⟩ @[simp] lemma neg_apply (x : M) : (- f) x = - f x := rfl @[simp] lemma comp_neg (g : M₂ →ₗ[R] M₃) : g.comp (- f) = - g.comp f := by { ext, simp } /-- The type of linear maps is an additive group. -/ instance : add_comm_group (M →ₗ[R] M₂) := by refine {zero := 0, add := (+), neg := has_neg.neg, ..}; intros; ext; simp [add_comm, add_left_comm] instance linear_map_apply_is_add_group_hom (a : M) : is_add_group_hom (λ f : M →ₗ[R] M₂, f a) := { map_add := λ f g, linear_map.add_apply f g a } @[simp] lemma sub_apply (x : M) : (f - g) x = f x - g x := rfl lemma sub_comp (g : M₂ →ₗ[R] M₃) (h : M₂ →ₗ[R] M₃) : (g - h).comp f = g.comp f - h.comp f := rfl lemma comp_sub (g : M →ₗ[R] M₂) (h : M₂ →ₗ[R] M₃) : h.comp (g - f) = h.comp g - h.comp f := by { ext, simp } end add_comm_group section has_scalar variables {S : Type*} [semiring R] [monoid S] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [semimodule R M] [semimodule R M₂] [semimodule R M₃] [distrib_mul_action S M₂] [smul_comm_class R S M₂] (f : M →ₗ[R] M₂) instance : has_scalar S (M →ₗ[R] M₂) := ⟨λ a f, ⟨λ b, a • f b, λ x y, by rw [f.map_add, smul_add], λ c x, by simp only [f.map_smul, smul_comm c]⟩⟩ @[simp] lemma smul_apply (a : S) (x : M) : (a • f) x = a • f x := rfl instance : distrib_mul_action S (M →ₗ[R] M₂) := { one_smul := λ f, ext $ λ _, one_smul _ _, mul_smul := λ c c' f, ext $ λ _, mul_smul _ _ _, smul_add := λ c f g, ext $ λ x, smul_add _ _ _, smul_zero := λ c, ext $ λ x, smul_zero _ } theorem smul_comp (a : S) (g : M₃ →ₗ[R] M₂) (f : M →ₗ[R] M₃) : (a • g).comp f = a • (g.comp f) := rfl end has_scalar section semimodule variables {S : Type*} [semiring R] [semiring S] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [semimodule R M] [semimodule R M₂] [semimodule R M₃] [semimodule S M₂] [semimodule S M₃] [smul_comm_class R S M₂] [smul_comm_class R S M₃] (f : M →ₗ[R] M₂) instance : semimodule S (M →ₗ[R] M₂) := { add_smul := λ a b f, ext $ λ x, add_smul _ _ _, zero_smul := λ f, ext $ λ x, zero_smul _ _ } variable (S) /-- Applying a linear map at `v : M`, seen as `S`-linear map from `M →ₗ[R] M₂` to `M₂`. See `applyₗ` for a version where `S = R` -/ def applyₗ' (v : M) : (M →ₗ[R] M₂) →ₗ[S] M₂ := { to_fun := λ f, f v, map_add' := λ f g, f.add_apply g v, map_smul' := λ x f, f.smul_apply x v } end semimodule section comm_semiring variables [comm_semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] variables (f g : M →ₗ[R] M₂) include R theorem comp_smul (g : M₂ →ₗ[R] M₃) (a : R) : g.comp (a • f) = a • (g.comp f) := ext $ assume b, by rw [comp_apply, smul_apply, g.map_smul]; refl /-- Composition by `f : M₂ → M₃` is a linear map from the space of linear maps `M → M₂` to the space of linear maps `M₂ → M₃`. -/ def comp_right (f : M₂ →ₗ[R] M₃) : (M →ₗ[R] M₂) →ₗ[R] (M →ₗ[R] M₃) := ⟨f.comp, λ _ _, linear_map.ext $ λ _, f.2 _ _, λ _ _, linear_map.ext $ λ _, f.3 _ _⟩ /-- Applying a linear map at `v : M`, seen as a linear map from `M →ₗ[R] M₂` to `M₂`. See also `linear_map.applyₗ'` for a version that works with two different semirings. -/ def applyₗ (v : M) : (M →ₗ[R] M₂) →ₗ[R] M₂ := applyₗ' R v end comm_semiring section semiring variables [semiring R] [add_comm_monoid M] [semimodule R M] instance endomorphism_semiring : semiring (M →ₗ[R] M) := by refine {mul := (*), one := 1, ..linear_map.add_comm_monoid, ..}; { intros, apply linear_map.ext, simp {proj := ff} } lemma mul_apply (f g : M →ₗ[R] M) (x : M) : (f * g) x = f (g x) := rfl end semiring section ring variables [ring R] [add_comm_group M] [semimodule R M] instance endomorphism_ring : ring (M →ₗ[R] M) := { ..linear_map.endomorphism_semiring, ..linear_map.add_comm_group } end ring section comm_ring variables [comm_ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] /-- The family of linear maps `M₂ → M` parameterised by `f ∈ M₂ → R`, `x ∈ M`, is linear in `f`, `x`. -/ def smul_rightₗ : (M₂ →ₗ[R] R) →ₗ[R] M →ₗ[R] M₂ →ₗ[R] M := { to_fun := λ f, { to_fun := linear_map.smul_right f, map_add' := λ m m', by { ext, apply smul_add, }, map_smul' := λ c m, by { ext, apply smul_comm, } }, map_add' := λ f f', by { ext, apply add_smul, }, map_smul' := λ c f, by { ext, apply mul_smul, } } @[simp] lemma smul_rightₗ_apply (f : M₂ →ₗ[R] R) (x : M) (c : M₂) : (smul_rightₗ : (M₂ →ₗ R) →ₗ M →ₗ M₂ →ₗ M) f x c = (f c) • x := rfl end comm_ring end linear_map /-! ### Properties of submodules -/ namespace submodule section add_comm_monoid variables [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] variables (p p' : submodule R M) (q q' : submodule R M₂) variables {r : R} {x y : M} open set instance : partial_order (submodule R M) := { le := λ p p', ∀ ⦃x⦄, x ∈ p → x ∈ p', ..partial_order.lift (coe : submodule R M → set M) coe_injective } variables {p p'} lemma le_def : p ≤ p' ↔ (p : set M) ⊆ p' := iff.rfl lemma le_def' : p ≤ p' ↔ ∀ x ∈ p, x ∈ p' := iff.rfl lemma lt_def : p < p' ↔ (p : set M) ⊂ p' := iff.rfl lemma not_le_iff_exists : ¬ (p ≤ p') ↔ ∃ x ∈ p, x ∉ p' := not_subset lemma exists_of_lt {p p' : submodule R M} : p < p' → ∃ x ∈ p', x ∉ p := exists_of_ssubset lemma lt_iff_le_and_exists : p < p' ↔ p ≤ p' ∧ ∃ x ∈ p', x ∉ p := by rw [lt_iff_le_not_le, not_le_iff_exists] /-- If two submodules `p` and `p'` satisfy `p ⊆ p'`, then `of_le p p'` is the linear map version of this inclusion. -/ def of_le (h : p ≤ p') : p →ₗ[R] p' := p.subtype.cod_restrict p' $ λ ⟨x, hx⟩, h hx @[simp] theorem coe_of_le (h : p ≤ p') (x : p) : (of_le h x : M) = x := rfl theorem of_le_apply (h : p ≤ p') (x : p) : of_le h x = ⟨x, h x.2⟩ := rfl variables (p p') lemma subtype_comp_of_le (p q : submodule R M) (h : p ≤ q) : q.subtype.comp (of_le h) = p.subtype := by { ext ⟨b, hb⟩, refl } /-- The set `{0}` is the bottom element of the lattice of submodules. -/ instance : has_bot (submodule R M) := ⟨{ carrier := {0}, smul_mem' := by simp { contextual := tt }, .. (⊥ : add_submonoid M)}⟩ instance inhabited' : inhabited (submodule R M) := ⟨⊥⟩ @[simp] lemma bot_coe : ((⊥ : submodule R M) : set M) = {0} := rfl section variables (R) @[simp] lemma mem_bot : x ∈ (⊥ : submodule R M) ↔ x = 0 := mem_singleton_iff end lemma nonzero_mem_of_bot_lt {I : submodule R M} (bot_lt : ⊥ < I) : ∃ a : I, a ≠ 0 := begin have h := (submodule.lt_iff_le_and_exists.1 bot_lt).2, tidy, end instance : order_bot (submodule R M) := { bot := ⊥, bot_le := λ p x, by simp {contextual := tt}, ..submodule.partial_order } protected lemma eq_bot_iff (p : submodule R M) : p = ⊥ ↔ ∀ x ∈ p, x = (0 : M) := ⟨ λ h, h.symm ▸ λ x hx, (mem_bot R).mp hx, λ h, eq_bot_iff.mpr (λ x hx, (mem_bot R).mpr (h x hx)) ⟩ protected lemma ne_bot_iff (p : submodule R M) : p ≠ ⊥ ↔ ∃ x ∈ p, x ≠ (0 : M) := by { haveI := classical.prop_decidable, simp_rw [ne.def, p.eq_bot_iff, not_forall] } /-- The universal set is the top element of the lattice of submodules. -/ instance : has_top (submodule R M) := ⟨{ carrier := univ, smul_mem' := λ _ _ _, trivial, .. (⊤ : add_submonoid M)}⟩ @[simp] lemma top_coe : ((⊤ : submodule R M) : set M) = univ := rfl @[simp] lemma mem_top : x ∈ (⊤ : submodule R M) := trivial lemma eq_bot_of_zero_eq_one (zero_eq_one : (0 : R) = 1) : p = ⊥ := by ext x; simp [semimodule.eq_zero_of_zero_eq_one x zero_eq_one] instance : order_top (submodule R M) := { top := ⊤, le_top := λ p x _, trivial, ..submodule.partial_order } instance : has_Inf (submodule R M) := ⟨λ S, { carrier := ⋂ s ∈ S, (s : set M), zero_mem' := by simp, add_mem' := by simp [add_mem] {contextual := tt}, smul_mem' := by simp [smul_mem] {contextual := tt} }⟩ private lemma Inf_le' {S : set (submodule R M)} {p} : p ∈ S → Inf S ≤ p := bInter_subset_of_mem private lemma le_Inf' {S : set (submodule R M)} {p} : (∀p' ∈ S, p ≤ p') → p ≤ Inf S := subset_bInter instance : has_inf (submodule R M) := ⟨λ p p', { carrier := p ∩ p', zero_mem' := by simp, add_mem' := by simp [add_mem] {contextual := tt}, smul_mem' := by simp [smul_mem] {contextual := tt} }⟩ instance : complete_lattice (submodule R M) := { sup := λ a b, Inf {x | a ≤ x ∧ b ≤ x}, le_sup_left := λ a b, le_Inf' $ λ x ⟨ha, hb⟩, ha, le_sup_right := λ a b, le_Inf' $ λ x ⟨ha, hb⟩, hb, sup_le := λ a b c h₁ h₂, Inf_le' ⟨h₁, h₂⟩, inf := (⊓), le_inf := λ a b c, subset_inter, inf_le_left := λ a b, inter_subset_left _ _, inf_le_right := λ a b, inter_subset_right _ _, Sup := λtt, Inf {t | ∀t'∈tt, t' ≤ t}, le_Sup := λ s p hs, le_Inf' $ λ p' hp', hp' _ hs, Sup_le := λ s p hs, Inf_le' hs, Inf := Inf, le_Inf := λ s a, le_Inf', Inf_le := λ s a, Inf_le', ..submodule.order_top, ..submodule.order_bot } instance add_comm_monoid_submodule : add_comm_monoid (submodule R M) := { add := (⊔), add_assoc := λ _ _ _, sup_assoc, zero := ⊥, zero_add := λ _, bot_sup_eq, add_zero := λ _, sup_bot_eq, add_comm := λ _ _, sup_comm } @[simp] lemma add_eq_sup (p q : submodule R M) : p + q = p ⊔ q := rfl @[simp] lemma zero_eq_bot : (0 : submodule R M) = ⊥ := rfl lemma eq_top_iff' {p : submodule R M} : p = ⊤ ↔ ∀ x, x ∈ p := eq_top_iff.trans ⟨λ h x, @h x trivial, λ h x _, h x⟩ lemma bot_ne_top [nontrivial M] : (⊥ : submodule R M) ≠ ⊤ := λ h, let ⟨a, ha⟩ := exists_ne (0 : M) in ha $ (mem_bot R).1 $ (eq_top_iff.1 h) trivial @[simp] theorem inf_coe : (p ⊓ p' : set M) = p ∩ p' := rfl @[simp] theorem mem_inf {p p' : submodule R M} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl @[simp] theorem Inf_coe (P : set (submodule R M)) : (↑(Inf P) : set M) = ⋂ p ∈ P, ↑p := rfl @[simp] theorem infi_coe {ι} (p : ι → submodule R M) : (↑⨅ i, p i : set M) = ⋂ i, ↑(p i) := by rw [infi, Inf_coe]; ext a; simp; exact ⟨λ h i, h _ i rfl, λ h i x e, e ▸ h _⟩ @[simp] lemma mem_Inf {S : set (submodule R M)} {x : M} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p := set.mem_bInter_iff @[simp] theorem mem_infi {ι} (p : ι → submodule R M) : x ∈ (⨅ i, p i) ↔ ∀ i, x ∈ p i := by rw [← mem_coe, infi_coe, mem_Inter]; refl theorem disjoint_def {p p' : submodule R M} : disjoint p p' ↔ ∀ x ∈ p, x ∈ p' → x = (0:M) := show (∀ x, x ∈ p ∧ x ∈ p' → x ∈ ({0} : set M)) ↔ _, by simp theorem disjoint_def' {p p' : submodule R M} : disjoint p p' ↔ ∀ (x ∈ p) (y ∈ p'), x = y → x = (0:M) := disjoint_def.trans ⟨λ h x hx y hy hxy, h x hx $ hxy.symm ▸ hy, λ h x hx hx', h _ hx x hx' rfl⟩ theorem mem_right_iff_eq_zero_of_disjoint {p p' : submodule R M} (h : disjoint p p') {x : p} : (x:M) ∈ p' ↔ x = 0 := ⟨λ hx, coe_eq_zero.1 $ disjoint_def.1 h x x.2 hx, λ h, h.symm ▸ p'.zero_mem⟩ theorem mem_left_iff_eq_zero_of_disjoint {p p' : submodule R M} (h : disjoint p p') {x : p'} : (x:M) ∈ p ↔ x = 0 := ⟨λ hx, coe_eq_zero.1 $ disjoint_def.1 h x hx x.2, λ h, h.symm ▸ p.zero_mem⟩ /-- The pushforward of a submodule `p ⊆ M` by `f : M → M₂` -/ def map (f : M →ₗ[R] M₂) (p : submodule R M) : submodule R M₂ := { carrier := f '' p, smul_mem' := by rintro a _ ⟨b, hb, rfl⟩; exact ⟨_, p.smul_mem _ hb, f.map_smul _ _⟩, .. p.to_add_submonoid.map f.to_add_monoid_hom } @[simp] lemma map_coe (f : M →ₗ[R] M₂) (p : submodule R M) : (map f p : set M₂) = f '' p := rfl @[simp] lemma mem_map {f : M →ₗ[R] M₂} {p : submodule R M} {x : M₂} : x ∈ map f p ↔ ∃ y, y ∈ p ∧ f y = x := iff.rfl theorem mem_map_of_mem {f : M →ₗ[R] M₂} {p : submodule R M} {r} (h : r ∈ p) : f r ∈ map f p := set.mem_image_of_mem _ h @[simp] lemma map_id : map linear_map.id p = p := submodule.ext $ λ a, by simp lemma map_comp (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) (p : submodule R M) : map (g.comp f) p = map g (map f p) := submodule.coe_injective $ by simp [map_coe]; rw ← image_comp lemma map_mono {f : M →ₗ[R] M₂} {p p' : submodule R M} : p ≤ p' → map f p ≤ map f p' := image_subset _ @[simp] lemma map_zero : map (0 : M →ₗ[R] M₂) p = ⊥ := have ∃ (x : M), x ∈ p := ⟨0, p.zero_mem⟩, ext $ by simp [this, eq_comm] /-- The pullback of a submodule `p ⊆ M₂` along `f : M → M₂` -/ def comap (f : M →ₗ[R] M₂) (p : submodule R M₂) : submodule R M := { carrier := f ⁻¹' p, smul_mem' := λ a x h, by simp [p.smul_mem _ h], .. p.to_add_submonoid.comap f.to_add_monoid_hom } @[simp] lemma comap_coe (f : M →ₗ[R] M₂) (p : submodule R M₂) : (comap f p : set M) = f ⁻¹' p := rfl @[simp] lemma mem_comap {f : M →ₗ[R] M₂} {p : submodule R M₂} : x ∈ comap f p ↔ f x ∈ p := iff.rfl lemma comap_id : comap linear_map.id p = p := submodule.coe_injective rfl lemma comap_comp (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) (p : submodule R M₃) : comap (g.comp f) p = comap f (comap g p) := rfl lemma comap_mono {f : M →ₗ[R] M₂} {q q' : submodule R M₂} : q ≤ q' → comap f q ≤ comap f q' := preimage_mono lemma map_le_iff_le_comap {f : M →ₗ[R] M₂} {p : submodule R M} {q : submodule R M₂} : map f p ≤ q ↔ p ≤ comap f q := image_subset_iff lemma gc_map_comap (f : M →ₗ[R] M₂) : galois_connection (map f) (comap f) | p q := map_le_iff_le_comap @[simp] lemma map_bot (f : M →ₗ[R] M₂) : map f ⊥ = ⊥ := (gc_map_comap f).l_bot @[simp] lemma map_sup (f : M →ₗ[R] M₂) : map f (p ⊔ p') = map f p ⊔ map f p' := (gc_map_comap f).l_sup @[simp] lemma map_supr {ι : Sort*} (f : M →ₗ[R] M₂) (p : ι → submodule R M) : map f (⨆i, p i) = (⨆i, map f (p i)) := (gc_map_comap f).l_supr @[simp] lemma comap_top (f : M →ₗ[R] M₂) : comap f ⊤ = ⊤ := rfl @[simp] lemma comap_inf (f : M →ₗ[R] M₂) : comap f (q ⊓ q') = comap f q ⊓ comap f q' := rfl @[simp] lemma comap_infi {ι : Sort*} (f : M →ₗ[R] M₂) (p : ι → submodule R M₂) : comap f (⨅i, p i) = (⨅i, comap f (p i)) := (gc_map_comap f).u_infi @[simp] lemma comap_zero : comap (0 : M →ₗ[R] M₂) q = ⊤ := ext $ by simp lemma map_comap_le (f : M →ₗ[R] M₂) (q : submodule R M₂) : map f (comap f q) ≤ q := (gc_map_comap f).l_u_le _ lemma le_comap_map (f : M →ₗ[R] M₂) (p : submodule R M) : p ≤ comap f (map f p) := (gc_map_comap f).le_u_l _ --TODO(Mario): is there a way to prove this from order properties? lemma map_inf_eq_map_inf_comap {f : M →ₗ[R] M₂} {p : submodule R M} {p' : submodule R M₂} : map f p ⊓ p' = map f (p ⊓ comap f p') := le_antisymm (by rintro _ ⟨⟨x, h₁, rfl⟩, h₂⟩; exact ⟨_, ⟨h₁, h₂⟩, rfl⟩) (le_inf (map_mono inf_le_left) (map_le_iff_le_comap.2 inf_le_right)) lemma map_comap_subtype : map p.subtype (comap p.subtype p') = p ⊓ p' := ext $ λ x, ⟨by rintro ⟨⟨_, h₁⟩, h₂, rfl⟩; exact ⟨h₁, h₂⟩, λ ⟨h₁, h₂⟩, ⟨⟨_, h₁⟩, h₂, rfl⟩⟩ lemma eq_zero_of_bot_submodule : ∀(b : (⊥ : submodule R M)), b = 0 | ⟨b', hb⟩ := subtype.eq $ show b' = 0, from (mem_bot R).1 hb section variables (R) /-- The span of a set `s ⊆ M` is the smallest submodule of M that contains `s`. -/ def span (s : set M) : submodule R M := Inf {p | s ⊆ p} end variables {s t : set M} lemma mem_span : x ∈ span R s ↔ ∀ p : submodule R M, s ⊆ p → x ∈ p := mem_bInter_iff lemma subset_span : s ⊆ span R s := λ x h, mem_span.2 $ λ p hp, hp h lemma span_le {p} : span R s ≤ p ↔ s ⊆ p := ⟨subset.trans subset_span, λ ss x h, mem_span.1 h _ ss⟩ lemma span_mono (h : s ⊆ t) : span R s ≤ span R t := span_le.2 $ subset.trans h subset_span lemma span_eq_of_le (h₁ : s ⊆ p) (h₂ : p ≤ span R s) : span R s = p := le_antisymm (span_le.2 h₁) h₂ @[simp] lemma span_eq : span R (p : set M) = p := span_eq_of_le _ (subset.refl _) subset_span lemma map_span (f : M →ₗ[R] M₂) (s : set M) : (span R s).map f = span R (f '' s) := eq.symm $ span_eq_of_le _ (set.image_subset f subset_span) $ map_le_iff_le_comap.2 $ span_le.2 $ λ x hx, subset_span ⟨x, hx, rfl⟩ /-- An induction principle for span membership. If `p` holds for 0 and all elements of `s`, and is preserved under addition and scalar multiplication, then `p` holds for all elements of the span of `s`. -/ @[elab_as_eliminator] lemma span_induction {p : M → Prop} (h : x ∈ span R s) (Hs : ∀ x ∈ s, p x) (H0 : p 0) (H1 : ∀ x y, p x → p y → p (x + y)) (H2 : ∀ (a:R) x, p x → p (a • x)) : p x := (@span_le _ _ _ _ _ _ ⟨p, H0, H1, H2⟩).2 Hs h section variables (R M) /-- `span` forms a Galois insertion with the coercion from submodule to set. -/ protected def gi : galois_insertion (@span R M _ _ _) coe := { choice := λ s _, span R s, gc := λ s t, span_le, le_l_u := λ s, subset_span, choice_eq := λ s h, rfl } end @[simp] lemma span_empty : span R (∅ : set M) = ⊥ := (submodule.gi R M).gc.l_bot @[simp] lemma span_univ : span R (univ : set M) = ⊤ := eq_top_iff.2 $ le_def.2 $ subset_span lemma span_union (s t : set M) : span R (s ∪ t) = span R s ⊔ span R t := (submodule.gi R M).gc.l_sup lemma span_Union {ι} (s : ι → set M) : span R (⋃ i, s i) = ⨆ i, span R (s i) := (submodule.gi R M).gc.l_supr @[simp] theorem coe_supr_of_directed {ι} [hι : nonempty ι] (S : ι → submodule R M) (H : directed (≤) S) : ((supr S : submodule R M) : set M) = ⋃ i, S i := begin refine subset.antisymm _ (Union_subset $ le_supr S), suffices : (span R (⋃ i, (S i : set M)) : set M) ⊆ ⋃ (i : ι), ↑(S i), by simpa only [span_Union, span_eq] using this, refine (λ x hx, span_induction hx (λ _, id) _ _ _); simp only [mem_Union, exists_imp_distrib], { exact hι.elim (λ i, ⟨i, (S i).zero_mem⟩) }, { intros x y i hi j hj, rcases H i j with ⟨k, ik, jk⟩, exact ⟨k, add_mem _ (ik hi) (jk hj)⟩ }, { exact λ a x i hi, ⟨i, smul_mem _ a hi⟩ }, end lemma mem_sup_left {S T : submodule R M} : ∀ {x : M}, x ∈ S → x ∈ S ⊔ T := show S ≤ S ⊔ T, from le_sup_left lemma mem_sup_right {S T : submodule R M} : ∀ {x : M}, x ∈ T → x ∈ S ⊔ T := show T ≤ S ⊔ T, from le_sup_right lemma mem_supr_of_mem {ι : Sort*} {b : M} {p : ι → submodule R M} (i : ι) (h : b ∈ p i) : b ∈ (⨆i, p i) := have p i ≤ (⨆i, p i) := le_supr p i, @this b h lemma mem_Sup_of_mem {S : set (submodule R M)} {s : submodule R M} (hs : s ∈ S) : ∀ {x : M}, x ∈ s → x ∈ Sup S := show s ≤ Sup S, from le_Sup hs @[simp] theorem mem_supr_of_directed {ι} [nonempty ι] (S : ι → submodule R M) (H : directed (≤) S) {x} : x ∈ supr S ↔ ∃ i, x ∈ S i := by { rw [← mem_coe, coe_supr_of_directed S H, mem_Union], refl } theorem mem_Sup_of_directed {s : set (submodule R M)} {z} (hs : s.nonempty) (hdir : directed_on (≤) s) : z ∈ Sup s ↔ ∃ y ∈ s, z ∈ y := begin haveI : nonempty s := hs.to_subtype, simp only [Sup_eq_supr', mem_supr_of_directed _ hdir.directed_coe, set_coe.exists, subtype.coe_mk] end section variables {p p'} lemma mem_sup : x ∈ p ⊔ p' ↔ ∃ (y ∈ p) (z ∈ p'), y + z = x := ⟨λ h, begin rw [← span_eq p, ← span_eq p', ← span_union] at h, apply span_induction h, { rintro y (h | h), { exact ⟨y, h, 0, by simp, by simp⟩ }, { exact ⟨0, by simp, y, h, by simp⟩ } }, { exact ⟨0, by simp, 0, by simp⟩ }, { rintro _ _ ⟨y₁, hy₁, z₁, hz₁, rfl⟩ ⟨y₂, hy₂, z₂, hz₂, rfl⟩, exact ⟨_, add_mem _ hy₁ hy₂, _, add_mem _ hz₁ hz₂, by simp [add_assoc]; cc⟩ }, { rintro a _ ⟨y, hy, z, hz, rfl⟩, exact ⟨_, smul_mem _ a hy, _, smul_mem _ a hz, by simp [smul_add]⟩ } end, by rintro ⟨y, hy, z, hz, rfl⟩; exact add_mem _ ((le_sup_left : p ≤ p ⊔ p') hy) ((le_sup_right : p' ≤ p ⊔ p') hz)⟩ lemma mem_sup' : x ∈ p ⊔ p' ↔ ∃ (y : p) (z : p'), (y:M) + z = x := mem_sup.trans $ by simp only [submodule.exists, coe_mk] end lemma mem_span_singleton_self (x : M) : x ∈ span R ({x} : set M) := subset_span rfl lemma nontrivial_span_singleton {x : M} (h : x ≠ 0) : nontrivial (submodule.span R ({x} : set M)) := ⟨begin use [0, x, submodule.mem_span_singleton_self x], intros H, rw [eq_comm, submodule.mk_eq_zero] at H, exact h H end⟩ lemma mem_span_singleton {y : M} : x ∈ span R ({y} : set M) ↔ ∃ a:R, a • y = x := ⟨λ h, begin apply span_induction h, { rintro y (rfl|⟨⟨⟩⟩), exact ⟨1, by simp⟩ }, { exact ⟨0, by simp⟩ }, { rintro _ _ ⟨a, rfl⟩ ⟨b, rfl⟩, exact ⟨a + b, by simp [add_smul]⟩ }, { rintro a _ ⟨b, rfl⟩, exact ⟨a * b, by simp [smul_smul]⟩ } end, by rintro ⟨a, y, rfl⟩; exact smul_mem _ _ (subset_span $ by simp)⟩ lemma le_span_singleton_iff {s : submodule R M} {v₀ : M} : s ≤ span R {v₀} ↔ ∀ v ∈ s, ∃ r : R, r • v₀ = v := by simp_rw [le_def', mem_span_singleton] lemma span_singleton_eq_range (y : M) : (span R ({y} : set M) : set M) = range ((• y) : R → M) := set.ext $ λ x, mem_span_singleton lemma span_singleton_smul_le (r : R) (x : M) : span R ({r • x} : set M) ≤ span R {x} := begin rw [span_le, set.singleton_subset_iff, mem_coe], exact smul_mem _ _ (mem_span_singleton_self _) end lemma span_singleton_smul_eq {K E : Type*} [division_ring K] [add_comm_group E] [module K E] {r : K} (x : E) (hr : r ≠ 0) : span K ({r • x} : set E) = span K {x} := begin refine le_antisymm (span_singleton_smul_le r x) _, convert span_singleton_smul_le r⁻¹ (r • x), exact (inv_smul_smul' hr _).symm end lemma disjoint_span_singleton {K E : Type*} [division_ring K] [add_comm_group E] [module K E] {s : submodule K E} {x : E} : disjoint s (span K {x}) ↔ (x ∈ s → x = 0) := begin refine disjoint_def.trans ⟨λ H hx, H x hx $ subset_span $ mem_singleton x, _⟩, assume H y hy hyx, obtain ⟨c, hc⟩ := mem_span_singleton.1 hyx, subst y, classical, by_cases hc : c = 0, by simp only [hc, zero_smul], rw [s.smul_mem_iff hc] at hy, rw [H hy, smul_zero] end lemma disjoint_span_singleton' {K E : Type*} [division_ring K] [add_comm_group E] [module K E] {p : submodule K E} {x : E} (x0 : x ≠ 0) : disjoint p (span K {x}) ↔ x ∉ p := disjoint_span_singleton.trans ⟨λ h₁ h₂, x0 (h₁ h₂), λ h₁ h₂, (h₁ h₂).elim⟩ lemma mem_span_insert {y} : x ∈ span R (insert y s) ↔ ∃ (a:R) (z ∈ span R s), x = a • y + z := begin simp only [← union_singleton, span_union, mem_sup, mem_span_singleton, exists_prop, exists_exists_eq_and], rw [exists_comm], simp only [eq_comm, add_comm, exists_and_distrib_left] end lemma span_insert_eq_span (h : x ∈ span R s) : span R (insert x s) = span R s := span_eq_of_le _ (set.insert_subset.mpr ⟨h, subset_span⟩) (span_mono $ subset_insert _ _) lemma span_span : span R (span R s : set M) = span R s := span_eq _ lemma span_eq_bot : span R (s : set M) = ⊥ ↔ ∀ x ∈ s, (x:M) = 0 := eq_bot_iff.trans ⟨ λ H x h, (mem_bot R).1 $ H $ subset_span h, λ H, span_le.2 (λ x h, (mem_bot R).2 $ H x h)⟩ @[simp] lemma span_singleton_eq_bot : span R ({x} : set M) = ⊥ ↔ x = 0 := span_eq_bot.trans $ by simp @[simp] lemma span_zero : span R (0 : set M) = ⊥ := by rw [←singleton_zero, span_singleton_eq_bot] @[simp] lemma span_image (f : M →ₗ[R] M₂) : span R (f '' s) = map f (span R s) := span_eq_of_le _ (image_subset _ subset_span) $ map_le_iff_le_comap.2 $ span_le.2 $ image_subset_iff.1 subset_span lemma supr_eq_span {ι : Sort w} (p : ι → submodule R M) : (⨆ (i : ι), p i) = submodule.span R (⋃ (i : ι), ↑(p i)) := le_antisymm (supr_le $ assume i, subset.trans (assume m hm, set.mem_Union.mpr ⟨i, hm⟩) subset_span) (span_le.mpr $ Union_subset_iff.mpr $ assume i m hm, mem_supr_of_mem i hm) lemma span_singleton_le_iff_mem (m : M) (p : submodule R M) : span R {m} ≤ p ↔ m ∈ p := by rw [span_le, singleton_subset_iff, mem_coe] lemma lt_add_iff_not_mem {I : submodule R M} {a : M} : I < I + span R {a} ↔ a ∉ I := begin split, { intro h, by_contra akey, have h1 : I + span R {a} ≤ I, { simp only [add_eq_sup, sup_le_iff], split, { exact le_refl I, }, { exact (span_singleton_le_iff_mem a I).mpr akey, } }, have h2 := gt_of_ge_of_gt h1 h, exact lt_irrefl I h2, }, { intro h, apply lt_iff_le_and_exists.mpr, split, simp only [add_eq_sup, le_sup_left], use a, split, swap, { assumption, }, { have : span R {a} ≤ I + span R{a} := le_sup_right, exact this (mem_span_singleton_self a), } }, end lemma mem_supr {ι : Sort w} (p : ι → submodule R M) {m : M} : (m ∈ ⨆ i, p i) ↔ (∀ N, (∀ i, p i ≤ N) → m ∈ N) := begin rw [← span_singleton_le_iff_mem, le_supr_iff], simp only [span_singleton_le_iff_mem], end /-- The product of two submodules is a submodule. -/ def prod : submodule R (M × M₂) := { carrier := set.prod p q, smul_mem' := by rintro a ⟨x, y⟩ ⟨hx, hy⟩; exact ⟨smul_mem _ a hx, smul_mem _ a hy⟩, .. p.to_add_submonoid.prod q.to_add_submonoid } @[simp] lemma prod_coe : (prod p q : set (M × M₂)) = set.prod p q := rfl @[simp] lemma mem_prod {p : submodule R M} {q : submodule R M₂} {x : M × M₂} : x ∈ prod p q ↔ x.1 ∈ p ∧ x.2 ∈ q := set.mem_prod lemma span_prod_le (s : set M) (t : set M₂) : span R (set.prod s t) ≤ prod (span R s) (span R t) := span_le.2 $ set.prod_mono subset_span subset_span @[simp] lemma prod_top : (prod ⊤ ⊤ : submodule R (M × M₂)) = ⊤ := by ext; simp @[simp] lemma prod_bot : (prod ⊥ ⊥ : submodule R (M × M₂)) = ⊥ := by ext ⟨x, y⟩; simp [prod.zero_eq_mk] lemma prod_mono {p p' : submodule R M} {q q' : submodule R M₂} : p ≤ p' → q ≤ q' → prod p q ≤ prod p' q' := prod_mono @[simp] lemma prod_inf_prod : prod p q ⊓ prod p' q' = prod (p ⊓ p') (q ⊓ q') := coe_injective set.prod_inter_prod @[simp] lemma prod_sup_prod : prod p q ⊔ prod p' q' = prod (p ⊔ p') (q ⊔ q') := begin refine le_antisymm (sup_le (prod_mono le_sup_left le_sup_left) (prod_mono le_sup_right le_sup_right)) _, simp [le_def'], intros xx yy hxx hyy, rcases mem_sup.1 hxx with ⟨x, hx, x', hx', rfl⟩, rcases mem_sup.1 hyy with ⟨y, hy, y', hy', rfl⟩, refine mem_sup.2 ⟨(x, y), ⟨hx, hy⟩, (x', y'), ⟨hx', hy'⟩, rfl⟩ end end add_comm_monoid variables [ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] variables (p p' : submodule R M) (q q' : submodule R M₂) variables {r : R} {x y : M} open set @[simp] lemma neg_coe : -(p : set M) = p := set.ext $ λ x, p.neg_mem_iff @[simp] protected lemma map_neg (f : M →ₗ[R] M₂) : map (-f) p = map f p := ext $ λ y, ⟨λ ⟨x, hx, hy⟩, hy ▸ ⟨-x, neg_mem _ hx, f.map_neg x⟩, λ ⟨x, hx, hy⟩, hy ▸ ⟨-x, neg_mem _ hx, ((-f).map_neg _).trans (neg_neg (f x))⟩⟩ @[simp] lemma span_neg (s : set M) : span R (-s) = span R s := calc span R (-s) = span R ((-linear_map.id : M →ₗ[R] M) '' s) : by simp ... = map (-linear_map.id) (span R s) : (map_span _ _).symm ... = span R s : by simp lemma mem_span_insert' {y} {s : set M} : x ∈ span R (insert y s) ↔ ∃(a:R), x + a • y ∈ span R s := begin rw mem_span_insert, split, { rintro ⟨a, z, hz, rfl⟩, exact ⟨-a, by simp [hz, add_assoc]⟩ }, { rintro ⟨a, h⟩, exact ⟨-a, _, h, by simp [add_comm, add_left_comm]⟩ } end -- TODO(Mario): Factor through add_subgroup /-- The equivalence relation associated to a submodule `p`, defined by `x ≈ y` iff `y - x ∈ p`. -/ def quotient_rel : setoid M := ⟨λ x y, x - y ∈ p, λ x, by simp, λ x y h, by simpa using neg_mem _ h, λ x y z h₁ h₂, by simpa [sub_eq_add_neg, add_left_comm, add_assoc] using add_mem _ h₁ h₂⟩ /-- The quotient of a module `M` by a submodule `p ⊆ M`. -/ def quotient : Type* := quotient (quotient_rel p) namespace quotient /-- Map associating to an element of `M` the corresponding element of `M/p`, when `p` is a submodule of `M`. -/ def mk {p : submodule R M} : M → quotient p := quotient.mk' @[simp] theorem mk_eq_mk {p : submodule R M} (x : M) : (quotient.mk x : quotient p) = mk x := rfl @[simp] theorem mk'_eq_mk {p : submodule R M} (x : M) : (quotient.mk' x : quotient p) = mk x := rfl @[simp] theorem quot_mk_eq_mk {p : submodule R M} (x : M) : (quot.mk _ x : quotient p) = mk x := rfl protected theorem eq {x y : M} : (mk x : quotient p) = mk y ↔ x - y ∈ p := quotient.eq' instance : has_zero (quotient p) := ⟨mk 0⟩ instance : inhabited (quotient p) := ⟨0⟩ @[simp] theorem mk_zero : mk 0 = (0 : quotient p) := rfl @[simp] theorem mk_eq_zero : (mk x : quotient p) = 0 ↔ x ∈ p := by simpa using (quotient.eq p : mk x = 0 ↔ _) instance : has_add (quotient p) := ⟨λ a b, quotient.lift_on₂' a b (λ a b, mk (a + b)) $ λ a₁ a₂ b₁ b₂ h₁ h₂, (quotient.eq p).2 $ by simpa [sub_eq_add_neg, add_left_comm, add_comm] using add_mem p h₁ h₂⟩ @[simp] theorem mk_add : (mk (x + y) : quotient p) = mk x + mk y := rfl instance : has_neg (quotient p) := ⟨λ a, quotient.lift_on' a (λ a, mk (-a)) $ λ a b h, (quotient.eq p).2 $ by simpa using neg_mem p h⟩ @[simp] theorem mk_neg : (mk (-x) : quotient p) = -mk x := rfl instance : add_comm_group (quotient p) := by refine {zero := 0, add := (+), neg := has_neg.neg, ..}; repeat {rintro ⟨⟩}; simp [-mk_zero, (mk_zero p).symm, -mk_add, (mk_add p).symm, -mk_neg, (mk_neg p).symm]; cc instance : has_scalar R (quotient p) := ⟨λ a x, quotient.lift_on' x (λ x, mk (a • x)) $ λ x y h, (quotient.eq p).2 $ by simpa [smul_sub] using smul_mem p a h⟩ @[simp] theorem mk_smul : (mk (r • x) : quotient p) = r • mk x := rfl instance : semimodule R (quotient p) := semimodule.of_core $ by refine {smul := (•), ..}; repeat {rintro ⟨⟩ <|> intro}; simp [smul_add, add_smul, smul_smul, -mk_add, (mk_add p).symm, -mk_smul, (mk_smul p).symm] lemma mk_surjective : function.surjective (@mk _ _ _ _ _ p) := by { rintros ⟨x⟩, exact ⟨x, rfl⟩ } lemma nontrivial_of_lt_top (h : p < ⊤) : nontrivial (p.quotient) := begin obtain ⟨x, _, not_mem_s⟩ := exists_of_lt h, refine ⟨⟨mk x, 0, _⟩⟩, simpa using not_mem_s end end quotient lemma quot_hom_ext ⦃f g : quotient p →ₗ[R] M₂⦄ (h : ∀ x, f (quotient.mk x) = g (quotient.mk x)) : f = g := linear_map.ext $ λ x, quotient.induction_on' x h end submodule namespace submodule variables [field K] variables [add_comm_group V] [vector_space K V] variables [add_comm_group V₂] [vector_space K V₂] lemma comap_smul (f : V →ₗ[K] V₂) (p : submodule K V₂) (a : K) (h : a ≠ 0) : p.comap (a • f) = p.comap f := by ext b; simp only [submodule.mem_comap, p.smul_mem_iff h, linear_map.smul_apply] lemma map_smul (f : V →ₗ[K] V₂) (p : submodule K V) (a : K) (h : a ≠ 0) : p.map (a • f) = p.map f := le_antisymm begin rw [map_le_iff_le_comap, comap_smul f _ a h, ← map_le_iff_le_comap], exact le_refl _ end begin rw [map_le_iff_le_comap, ← comap_smul f _ a h, ← map_le_iff_le_comap], exact le_refl _ end lemma comap_smul' (f : V →ₗ[K] V₂) (p : submodule K V₂) (a : K) : p.comap (a • f) = (⨅ h : a ≠ 0, p.comap f) := by classical; by_cases a = 0; simp [h, comap_smul] lemma map_smul' (f : V →ₗ[K] V₂) (p : submodule K V) (a : K) : p.map (a • f) = (⨆ h : a ≠ 0, p.map f) := by classical; by_cases a = 0; simp [h, map_smul] end submodule /-! ### Properties of linear maps -/ namespace linear_map section add_comm_monoid variables [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] include R open submodule /-- If two linear maps are equal on a set `s`, then they are equal on `submodule.span s`. See also `linear_map.eq_on_span'` for a version using `set.eq_on`. -/ lemma eq_on_span {s : set M} {f g : M →ₗ[R] M₂} (H : set.eq_on f g s) ⦃x⦄ (h : x ∈ span R s) : f x = g x := by apply span_induction h H; simp {contextual := tt} /-- If two linear maps are equal on a set `s`, then they are equal on `submodule.span s`. This version uses `set.eq_on`, and the hidden argument will expand to `h : x ∈ (span R s : set M)`. See `linear_map.eq_on_span` for a version that takes `h : x ∈ span R s` as an argument. -/ lemma eq_on_span' {s : set M} {f g : M →ₗ[R] M₂} (H : set.eq_on f g s) : set.eq_on f g (span R s : set M) := eq_on_span H /-- If `s` generates the whole semimodule and linear maps `f`, `g` are equal on `s`, then they are equal. -/ lemma ext_on {s : set M} {f g : M →ₗ[R] M₂} (hv : span R s = ⊤) (h : set.eq_on f g s) : f = g := linear_map.ext (λ x, eq_on_span h (eq_top_iff'.1 hv _)) /-- If the range of `v : ι → M` generates the whole semimodule and linear maps `f`, `g` are equal at each `v i`, then they are equal. -/ lemma ext_on_range {v : ι → M} {f g : M →ₗ[R] M₂} (hv : span R (set.range v) = ⊤) (h : ∀i, f (v i) = g (v i)) : f = g := ext_on hv (set.forall_range_iff.2 h) @[simp] lemma finsupp_sum {γ} [has_zero γ] (f : M →ₗ[R] M₂) {t : ι →₀ γ} {g : ι → γ → M} : f (t.sum g) = t.sum (λi d, f (g i d)) := f.map_sum theorem map_cod_restrict (p : submodule R M) (f : M₂ →ₗ[R] M) (h p') : submodule.map (cod_restrict p f h) p' = comap p.subtype (p'.map f) := submodule.ext $ λ ⟨x, hx⟩, by simp [subtype.ext_iff_val] theorem comap_cod_restrict (p : submodule R M) (f : M₂ →ₗ[R] M) (hf p') : submodule.comap (cod_restrict p f hf) p' = submodule.comap f (map p.subtype p') := submodule.ext $ λ x, ⟨λ h, ⟨⟨_, hf x⟩, h, rfl⟩, by rintro ⟨⟨_, _⟩, h, ⟨⟩⟩; exact h⟩ /-- The range of a linear map `f : M → M₂` is a submodule of `M₂`. -/ def range (f : M →ₗ[R] M₂) : submodule R M₂ := map f ⊤ theorem range_coe (f : M →ₗ[R] M₂) : (range f : set M₂) = set.range f := set.image_univ @[simp] theorem mem_range {f : M →ₗ[R] M₂} : ∀ {x}, x ∈ range f ↔ ∃ y, f y = x := set.ext_iff.1 (range_coe f) theorem mem_range_self (f : M →ₗ[R] M₂) (x : M) : f x ∈ f.range := mem_range.2 ⟨x, rfl⟩ @[simp] theorem range_id : range (linear_map.id : M →ₗ[R] M) = ⊤ := map_id _ theorem range_comp (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) : range (g.comp f) = map g (range f) := map_comp _ _ _ theorem range_comp_le_range (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) : range (g.comp f) ≤ range g := by rw range_comp; exact map_mono le_top theorem range_eq_top {f : M →ₗ[R] M₂} : range f = ⊤ ↔ surjective f := by rw [submodule.ext'_iff, range_coe, top_coe, set.range_iff_surjective] lemma range_le_iff_comap {f : M →ₗ[R] M₂} {p : submodule R M₂} : range f ≤ p ↔ comap f p = ⊤ := by rw [range, map_le_iff_le_comap, eq_top_iff] lemma map_le_range {f : M →ₗ[R] M₂} {p : submodule R M} : map f p ≤ range f := map_mono le_top lemma range_coprod (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) : (f.coprod g).range = f.range ⊔ g.range := submodule.ext $ λ x, by simp [mem_sup] lemma is_compl_range_inl_inr : is_compl (inl R M M₂).range (inr R M M₂).range := begin split, { rintros ⟨_, _⟩ ⟨⟨x, -, hx⟩, ⟨y, -, hy⟩⟩, simp only [prod.ext_iff, inl_apply, inr_apply, mem_bot] at hx hy ⊢, exact ⟨hy.1.symm, hx.2.symm⟩ }, { rintros ⟨x, y⟩ -, simp only [mem_sup, mem_range, exists_prop], refine ⟨(x, 0), ⟨x, rfl⟩, (0, y), ⟨y, rfl⟩, _⟩, simp } end lemma sup_range_inl_inr : (inl R M M₂).range ⊔ (inr R M M₂).range = ⊤ := is_compl_range_inl_inr.sup_eq_top /-- Restrict the codomain of a linear map `f` to `f.range`. -/ @[reducible] def range_restrict (f : M →ₗ[R] M₂) : M →ₗ[R] f.range := f.cod_restrict f.range f.mem_range_self section variables (R) (M) /-- Given an element `x` of a module `M` over `R`, the natural map from `R` to scalar multiples of `x`.-/ def to_span_singleton (x : M) : R →ₗ[R] M := linear_map.id.smul_right x /-- The range of `to_span_singleton x` is the span of `x`.-/ lemma span_singleton_eq_range (x : M) : span R {x} = (to_span_singleton R M x).range := submodule.ext $ λ y, by {refine iff.trans _ mem_range.symm, exact mem_span_singleton } lemma to_span_singleton_one (x : M) : to_span_singleton R M x 1 = x := one_smul _ _ end /-- The kernel of a linear map `f : M → M₂` is defined to be `comap f ⊥`. This is equivalent to the set of `x : M` such that `f x = 0`. The kernel is a submodule of `M`. -/ def ker (f : M →ₗ[R] M₂) : submodule R M := comap f ⊥ @[simp] theorem mem_ker {f : M →ₗ[R] M₂} {y} : y ∈ ker f ↔ f y = 0 := mem_bot R @[simp] theorem ker_id : ker (linear_map.id : M →ₗ[R] M) = ⊥ := rfl @[simp] theorem map_coe_ker (f : M →ₗ[R] M₂) (x : ker f) : f x = 0 := mem_ker.1 x.2 lemma comp_ker_subtype (f : M →ₗ[R] M₂) : f.comp f.ker.subtype = 0 := linear_map.ext $ λ x, suffices f x = 0, by simp [this], mem_ker.1 x.2 theorem ker_comp (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) : ker (g.comp f) = comap f (ker g) := rfl theorem ker_le_ker_comp (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) : ker f ≤ ker (g.comp f) := by rw ker_comp; exact comap_mono bot_le theorem disjoint_ker {f : M →ₗ[R] M₂} {p : submodule R M} : disjoint p (ker f) ↔ ∀ x ∈ p, f x = 0 → x = 0 := by simp [disjoint_def] lemma disjoint_inl_inr : disjoint (inl R M M₂).range (inr R M M₂).range := by simp [disjoint_def, @eq_comm M 0, @eq_comm M₂ 0] {contextual := tt}; intros; refl theorem ker_eq_bot' {f : M →ₗ[R] M₂} : ker f = ⊥ ↔ (∀ m, f m = 0 → m = 0) := by simpa [disjoint] using @disjoint_ker _ _ _ _ _ _ _ _ f ⊤ lemma le_ker_iff_map {f : M →ₗ[R] M₂} {p : submodule R M} : p ≤ ker f ↔ map f p = ⊥ := by rw [ker, eq_bot_iff, map_le_iff_le_comap] lemma ker_cod_restrict (p : submodule R M) (f : M₂ →ₗ[R] M) (hf) : ker (cod_restrict p f hf) = ker f := by rw [ker, comap_cod_restrict, map_bot]; refl lemma range_cod_restrict (p : submodule R M) (f : M₂ →ₗ[R] M) (hf) : range (cod_restrict p f hf) = comap p.subtype f.range := map_cod_restrict _ _ _ _ lemma ker_restrict {p : submodule R M} {f : M →ₗ[R] M} (hf : ∀ x : M, x ∈ p → f x ∈ p) : ker (f.restrict hf) = (f.dom_restrict p).ker := by rw [restrict_eq_cod_restrict_dom_restrict, ker_cod_restrict] lemma map_comap_eq (f : M →ₗ[R] M₂) (q : submodule R M₂) : map f (comap f q) = range f ⊓ q := le_antisymm (le_inf (map_mono le_top) (map_comap_le _ _)) $ by rintro _ ⟨⟨x, _, rfl⟩, hx⟩; exact ⟨x, hx, rfl⟩ lemma map_comap_eq_self {f : M →ₗ[R] M₂} {q : submodule R M₂} (h : q ≤ range f) : map f (comap f q) = q := by rwa [map_comap_eq, inf_eq_right] @[simp] theorem ker_zero : ker (0 : M →ₗ[R] M₂) = ⊤ := eq_top_iff'.2 $ λ x, by simp @[simp] theorem range_zero : range (0 : M →ₗ[R] M₂) = ⊥ := submodule.map_zero _ theorem ker_eq_top {f : M →ₗ[R] M₂} : ker f = ⊤ ↔ f = 0 := ⟨λ h, ext $ λ x, mem_ker.1 $ h.symm ▸ trivial, λ h, h.symm ▸ ker_zero⟩ lemma range_le_bot_iff (f : M →ₗ[R] M₂) : range f ≤ ⊥ ↔ f = 0 := by rw [range_le_iff_comap]; exact ker_eq_top lemma range_le_ker_iff {f : M →ₗ[R] M₂} {g : M₂ →ₗ[R] M₃} : range f ≤ ker g ↔ g.comp f = 0 := ⟨λ h, ker_eq_top.1 $ eq_top_iff'.2 $ λ x, h $ mem_map_of_mem trivial, λ h x hx, mem_ker.2 $ exists.elim hx $ λ y ⟨_, hy⟩, by rw [←hy, ←comp_apply, h, zero_apply]⟩ theorem comap_le_comap_iff {f : M →ₗ[R] M₂} (hf : range f = ⊤) {p p'} : comap f p ≤ comap f p' ↔ p ≤ p' := ⟨λ H x hx, by rcases range_eq_top.1 hf x with ⟨y, hy, rfl⟩; exact H hx, comap_mono⟩ theorem comap_injective {f : M →ₗ[R] M₂} (hf : range f = ⊤) : injective (comap f) := λ p p' h, le_antisymm ((comap_le_comap_iff hf).1 (le_of_eq h)) ((comap_le_comap_iff hf).1 (ge_of_eq h)) theorem map_coprod_prod (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) (p : submodule R M) (q : submodule R M₂) : map (coprod f g) (p.prod q) = map f p ⊔ map g q := begin refine le_antisymm _ (sup_le (map_le_iff_le_comap.2 _) (map_le_iff_le_comap.2 _)), { rw le_def', rintro _ ⟨x, ⟨h₁, h₂⟩, rfl⟩, exact mem_sup.2 ⟨_, ⟨_, h₁, rfl⟩, _, ⟨_, h₂, rfl⟩, rfl⟩ }, { exact λ x hx, ⟨(x, 0), by simp [hx]⟩ }, { exact λ x hx, ⟨(0, x), by simp [hx]⟩ } end theorem comap_prod_prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) (p : submodule R M₂) (q : submodule R M₃) : comap (prod f g) (p.prod q) = comap f p ⊓ comap g q := submodule.ext $ λ x, iff.rfl theorem prod_eq_inf_comap (p : submodule R M) (q : submodule R M₂) : p.prod q = p.comap (linear_map.fst R M M₂) ⊓ q.comap (linear_map.snd R M M₂) := submodule.ext $ λ x, iff.rfl theorem prod_eq_sup_map (p : submodule R M) (q : submodule R M₂) : p.prod q = p.map (linear_map.inl R M M₂) ⊔ q.map (linear_map.inr R M M₂) := by rw [← map_coprod_prod, coprod_inl_inr, map_id] lemma span_inl_union_inr {s : set M} {t : set M₂} : span R (inl R M M₂ '' s ∪ inr R M M₂ '' t) = (span R s).prod (span R t) := by rw [span_union, prod_eq_sup_map, ← span_image, ← span_image]; refl @[simp] lemma ker_prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : ker (prod f g) = ker f ⊓ ker g := by rw [ker, ← prod_bot, comap_prod_prod]; refl lemma range_prod_le (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : range (prod f g) ≤ (range f).prod (range g) := begin simp only [le_def', prod_apply, mem_range, mem_coe, mem_prod, exists_imp_distrib], rintro _ x rfl, exact ⟨⟨x, rfl⟩, ⟨x, rfl⟩⟩ end theorem ker_eq_bot_of_injective {f : M →ₗ[R] M₂} (hf : injective f) : ker f = ⊥ := begin have : disjoint ⊤ f.ker, by { rw [disjoint_ker, ← map_zero f], exact λ x hx H, hf H }, simpa [disjoint] end end add_comm_monoid section add_comm_group variables [semiring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] include R open submodule lemma comap_map_eq (f : M →ₗ[R] M₂) (p : submodule R M) : comap f (map f p) = p ⊔ ker f := begin refine le_antisymm _ (sup_le (le_comap_map _ _) (comap_mono bot_le)), rintro x ⟨y, hy, e⟩, exact mem_sup.2 ⟨y, hy, x - y, by simpa using sub_eq_zero.2 e.symm, by simp⟩ end lemma comap_map_eq_self {f : M →ₗ[R] M₂} {p : submodule R M} (h : ker f ≤ p) : comap f (map f p) = p := by rw [comap_map_eq, sup_of_le_left h] theorem map_le_map_iff (f : M →ₗ[R] M₂) {p p'} : map f p ≤ map f p' ↔ p ≤ p' ⊔ ker f := by rw [map_le_iff_le_comap, comap_map_eq] theorem map_le_map_iff' {f : M →ₗ[R] M₂} (hf : ker f = ⊥) {p p'} : map f p ≤ map f p' ↔ p ≤ p' := by rw [map_le_map_iff, hf, sup_bot_eq] theorem map_injective {f : M →ₗ[R] M₂} (hf : ker f = ⊥) : injective (map f) := λ p p' h, le_antisymm ((map_le_map_iff' hf).1 (le_of_eq h)) ((map_le_map_iff' hf).1 (ge_of_eq h)) theorem map_eq_top_iff {f : M →ₗ[R] M₂} (hf : range f = ⊤) {p : submodule R M} : p.map f = ⊤ ↔ p ⊔ f.ker = ⊤ := by simp_rw [← top_le_iff, ← hf, range, map_le_map_iff] end add_comm_group section ring variables [ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] include R open submodule theorem sub_mem_ker_iff {f : M →ₗ[R] M₂} {x y} : x - y ∈ f.ker ↔ f x = f y := by rw [mem_ker, map_sub, sub_eq_zero] theorem disjoint_ker' {f : M →ₗ[R] M₂} {p : submodule R M} : disjoint p (ker f) ↔ ∀ x y ∈ p, f x = f y → x = y := disjoint_ker.trans ⟨λ H x y hx hy h, eq_of_sub_eq_zero $ H _ (sub_mem _ hx hy) (by simp [h]), λ H x h₁ h₂, H x 0 h₁ (zero_mem _) (by simpa using h₂)⟩ theorem inj_of_disjoint_ker {f : M →ₗ[R] M₂} {p : submodule R M} {s : set M} (h : s ⊆ p) (hd : disjoint p (ker f)) : ∀ x y ∈ s, f x = f y → x = y := λ x y hx hy, disjoint_ker'.1 hd _ _ (h hx) (h hy) theorem ker_eq_bot {f : M →ₗ[R] M₂} : ker f = ⊥ ↔ injective f := by simpa [disjoint] using @disjoint_ker' _ _ _ _ _ _ _ _ f ⊤ /-- If the union of the kernels `ker f` and `ker g` spans the domain, then the range of `prod f g` is equal to the product of `range f` and `range g`. -/ lemma range_prod_eq {f : M →ₗ[R] M₂} {g : M →ₗ[R] M₃} (h : ker f ⊔ ker g = ⊤) : range (prod f g) = (range f).prod (range g) := begin refine le_antisymm (f.range_prod_le g) _, simp only [le_def', prod_apply, mem_range, mem_coe, mem_prod, exists_imp_distrib, and_imp, prod.forall], rintros _ _ x rfl y rfl, simp only [prod.mk.inj_iff, ← sub_mem_ker_iff], have : y - x ∈ ker f ⊔ ker g, { simp only [h, mem_top] }, rcases mem_sup.1 this with ⟨x', hx', y', hy', H⟩, refine ⟨x' + x, _, _⟩, { rwa add_sub_cancel }, { rwa [← eq_sub_iff_add_eq.1 H, add_sub_add_right_eq_sub, ← neg_mem_iff, neg_sub, add_sub_cancel'] } end end ring section field variables [field K] variables [add_comm_group V] [vector_space K V] variables [add_comm_group V₂] [vector_space K V₂] lemma ker_smul (f : V →ₗ[K] V₂) (a : K) (h : a ≠ 0) : ker (a • f) = ker f := submodule.comap_smul f _ a h lemma ker_smul' (f : V →ₗ[K] V₂) (a : K) : ker (a • f) = ⨅(h : a ≠ 0), ker f := submodule.comap_smul' f _ a lemma range_smul (f : V →ₗ[K] V₂) (a : K) (h : a ≠ 0) : range (a • f) = range f := submodule.map_smul f _ a h lemma range_smul' (f : V →ₗ[K] V₂) (a : K) : range (a • f) = ⨆(h : a ≠ 0), range f := submodule.map_smul' f _ a end field end linear_map lemma submodule.sup_eq_range [semiring R] [add_comm_monoid M] [semimodule R M] (p q : submodule R M) : p ⊔ q = (p.subtype.coprod q.subtype).range := submodule.ext $ λ x, by simp [submodule.mem_sup, submodule.exists] namespace is_linear_map lemma is_linear_map_add [semiring R] [add_comm_monoid M] [semimodule R M] : is_linear_map R (λ (x : M × M), x.1 + x.2) := begin apply is_linear_map.mk, { intros x y, simp, cc }, { intros x y, simp [smul_add] } end lemma is_linear_map_sub {R M : Type*} [semiring R] [add_comm_group M] [semimodule R M]: is_linear_map R (λ (x : M × M), x.1 - x.2) := begin apply is_linear_map.mk, { intros x y, simp [add_comm, add_left_comm, sub_eq_add_neg] }, { intros x y, simp [smul_sub] } end end is_linear_map namespace submodule section add_comm_monoid variables {T : semiring R} [add_comm_monoid M] [add_comm_monoid M₂] [semimodule R M] [semimodule R M₂] variables (p p' : submodule R M) (q : submodule R M₂) include T open linear_map @[simp] theorem map_top (f : M →ₗ[R] M₂) : map f ⊤ = range f := rfl @[simp] theorem comap_bot (f : M →ₗ[R] M₂) : comap f ⊥ = ker f := rfl @[simp] theorem ker_subtype : p.subtype.ker = ⊥ := ker_eq_bot_of_injective $ λ x y, subtype.ext_val @[simp] theorem range_subtype : p.subtype.range = p := by simpa using map_comap_subtype p ⊤ lemma map_subtype_le (p' : submodule R p) : map p.subtype p' ≤ p := by simpa using (map_mono le_top : map p.subtype p' ≤ p.subtype.range) /-- Under the canonical linear map from a submodule `p` to the ambient space `M`, the image of the maximal submodule of `p` is just `p `. -/ @[simp] lemma map_subtype_top : map p.subtype (⊤ : submodule R p) = p := by simp @[simp] lemma comap_subtype_eq_top {p p' : submodule R M} : comap p.subtype p' = ⊤ ↔ p ≤ p' := eq_top_iff.trans $ map_le_iff_le_comap.symm.trans $ by rw [map_subtype_top] @[simp] lemma comap_subtype_self : comap p.subtype p = ⊤ := comap_subtype_eq_top.2 (le_refl _) @[simp] theorem ker_of_le (p p' : submodule R M) (h : p ≤ p') : (of_le h).ker = ⊥ := by rw [of_le, ker_cod_restrict, ker_subtype] lemma range_of_le (p q : submodule R M) (h : p ≤ q) : (of_le h).range = comap q.subtype p := by rw [← map_top, of_le, linear_map.map_cod_restrict, map_top, range_subtype] @[simp] theorem map_inl : p.map (inl R M M₂) = prod p ⊥ := by { ext ⟨x, y⟩, simp only [and.left_comm, eq_comm, mem_map, prod.mk.inj_iff, inl_apply, mem_bot, exists_eq_left', mem_prod] } @[simp] theorem map_inr : q.map (inr R M M₂) = prod ⊥ q := by ext ⟨x, y⟩; simp [and.left_comm, eq_comm] @[simp] theorem comap_fst : p.comap (fst R M M₂) = prod p ⊤ := by ext ⟨x, y⟩; simp @[simp] theorem comap_snd : q.comap (snd R M M₂) = prod ⊤ q := by ext ⟨x, y⟩; simp @[simp] theorem prod_comap_inl : (prod p q).comap (inl R M M₂) = p := by ext; simp @[simp] theorem prod_comap_inr : (prod p q).comap (inr R M M₂) = q := by ext; simp @[simp] theorem prod_map_fst : (prod p q).map (fst R M M₂) = p := by ext x; simp [(⟨0, zero_mem _⟩ : ∃ x, x ∈ q)] @[simp] theorem prod_map_snd : (prod p q).map (snd R M M₂) = q := by ext x; simp [(⟨0, zero_mem _⟩ : ∃ x, x ∈ p)] @[simp] theorem ker_inl : (inl R M M₂).ker = ⊥ := by rw [ker, ← prod_bot, prod_comap_inl] @[simp] theorem ker_inr : (inr R M M₂).ker = ⊥ := by rw [ker, ← prod_bot, prod_comap_inr] @[simp] theorem range_fst : (fst R M M₂).range = ⊤ := by rw [range, ← prod_top, prod_map_fst] @[simp] theorem range_snd : (snd R M M₂).range = ⊤ := by rw [range, ← prod_top, prod_map_snd] end add_comm_monoid section ring variables {T : ring R} [add_comm_group M] [add_comm_group M₂] [semimodule R M] [semimodule R M₂] variables (p p' : submodule R M) (q : submodule R M₂) include T open linear_map lemma disjoint_iff_comap_eq_bot {p q : submodule R M} : disjoint p q ↔ comap p.subtype q = ⊥ := by rw [eq_bot_iff, ← map_le_map_iff' p.ker_subtype, map_bot, map_comap_subtype, disjoint] /-- If `N ⊆ M` then submodules of `N` are the same as submodules of `M` contained in `N` -/ def map_subtype.rel_iso : submodule R p ≃o {p' : submodule R M // p' ≤ p} := { to_fun := λ p', ⟨map p.subtype p', map_subtype_le p _⟩, inv_fun := λ q, comap p.subtype q, left_inv := λ p', comap_map_eq_self $ by simp, right_inv := λ ⟨q, hq⟩, subtype.ext_val $ by simp [map_comap_subtype p, inf_of_le_right hq], map_rel_iff' := λ p₁ p₂, (map_le_map_iff' (ker_subtype p)).symm } /-- If `p ⊆ M` is a submodule, the ordering of submodules of `p` is embedded in the ordering of submodules of `M`. -/ def map_subtype.order_embedding : submodule R p ↪o submodule R M := (rel_iso.to_rel_embedding $ map_subtype.rel_iso p).trans (subtype.rel_embedding _ _) @[simp] lemma map_subtype_embedding_eq (p' : submodule R p) : map_subtype.order_embedding p p' = map p.subtype p' := rfl /-- The map from a module `M` to the quotient of `M` by a submodule `p` as a linear map. -/ def mkq : M →ₗ[R] p.quotient := ⟨quotient.mk, by simp, by simp⟩ @[simp] theorem mkq_apply (x : M) : p.mkq x = quotient.mk x := rfl /-- The map from the quotient of `M` by a submodule `p` to `M₂` induced by a linear map `f : M → M₂` vanishing on `p`, as a linear map. -/ def liftq (f : M →ₗ[R] M₂) (h : p ≤ f.ker) : p.quotient →ₗ[R] M₂ := ⟨λ x, _root_.quotient.lift_on' x f $ λ a b (ab : a - b ∈ p), eq_of_sub_eq_zero $ by simpa using h ab, by rintro ⟨x⟩ ⟨y⟩; exact f.map_add x y, by rintro a ⟨x⟩; exact f.map_smul a x⟩ @[simp] theorem liftq_apply (f : M →ₗ[R] M₂) {h} (x : M) : p.liftq f h (quotient.mk x) = f x := rfl @[simp] theorem liftq_mkq (f : M →ₗ[R] M₂) (h) : (p.liftq f h).comp p.mkq = f := by ext; refl @[simp] theorem range_mkq : p.mkq.range = ⊤ := eq_top_iff'.2 $ by rintro ⟨x⟩; exact ⟨x, trivial, rfl⟩ @[simp] theorem ker_mkq : p.mkq.ker = p := by ext; simp lemma le_comap_mkq (p' : submodule R p.quotient) : p ≤ comap p.mkq p' := by simpa using (comap_mono bot_le : p.mkq.ker ≤ comap p.mkq p') @[simp] theorem mkq_map_self : map p.mkq p = ⊥ := by rw [eq_bot_iff, map_le_iff_le_comap, comap_bot, ker_mkq]; exact le_refl _ @[simp] theorem comap_map_mkq : comap p.mkq (map p.mkq p') = p ⊔ p' := by simp [comap_map_eq, sup_comm] @[simp] theorem map_mkq_eq_top : map p.mkq p' = ⊤ ↔ p ⊔ p' = ⊤ := by simp only [map_eq_top_iff p.range_mkq, sup_comm, ker_mkq] /-- The map from the quotient of `M` by submodule `p` to the quotient of `M₂` by submodule `q` along `f : M → M₂` is linear. -/ def mapq (f : M →ₗ[R] M₂) (h : p ≤ comap f q) : p.quotient →ₗ[R] q.quotient := p.liftq (q.mkq.comp f) $ by simpa [ker_comp] using h @[simp] theorem mapq_apply (f : M →ₗ[R] M₂) {h} (x : M) : mapq p q f h (quotient.mk x) = quotient.mk (f x) := rfl theorem mapq_mkq (f : M →ₗ[R] M₂) {h} : (mapq p q f h).comp p.mkq = q.mkq.comp f := by ext x; refl theorem comap_liftq (f : M →ₗ[R] M₂) (h) : q.comap (p.liftq f h) = (q.comap f).map (mkq p) := le_antisymm (by rintro ⟨x⟩ hx; exact ⟨_, hx, rfl⟩) (by rw [map_le_iff_le_comap, ← comap_comp, liftq_mkq]; exact le_refl _) theorem map_liftq (f : M →ₗ[R] M₂) (h) (q : submodule R (quotient p)) : q.map (p.liftq f h) = (q.comap p.mkq).map f := le_antisymm (by rintro _ ⟨⟨x⟩, hxq, rfl⟩; exact ⟨x, hxq, rfl⟩) (by rintro _ ⟨x, hxq, rfl⟩; exact ⟨quotient.mk x, hxq, rfl⟩) theorem ker_liftq (f : M →ₗ[R] M₂) (h) : ker (p.liftq f h) = (ker f).map (mkq p) := comap_liftq _ _ _ _ theorem range_liftq (f : M →ₗ[R] M₂) (h) : range (p.liftq f h) = range f := map_liftq _ _ _ _ theorem ker_liftq_eq_bot (f : M →ₗ[R] M₂) (h) (h' : ker f ≤ p) : ker (p.liftq f h) = ⊥ := by rw [ker_liftq, le_antisymm h h', mkq_map_self] /-- The correspondence theorem for modules: there is an order isomorphism between submodules of the quotient of `M` by `p`, and submodules of `M` larger than `p`. -/ def comap_mkq.rel_iso : submodule R p.quotient ≃o {p' : submodule R M // p ≤ p'} := { to_fun := λ p', ⟨comap p.mkq p', le_comap_mkq p _⟩, inv_fun := λ q, map p.mkq q, left_inv := λ p', map_comap_eq_self $ by simp, right_inv := λ ⟨q, hq⟩, subtype.ext_val $ by simpa [comap_map_mkq p], map_rel_iff' := λ p₁ p₂, (comap_le_comap_iff $ range_mkq _).symm } /-- The ordering on submodules of the quotient of `M` by `p` embeds into the ordering on submodules of `M`. -/ def comap_mkq.order_embedding : submodule R p.quotient ↪o submodule R M := (rel_iso.to_rel_embedding $ comap_mkq.rel_iso p).trans (subtype.rel_embedding _ _) @[simp] lemma comap_mkq_embedding_eq (p' : submodule R p.quotient) : comap_mkq.order_embedding p p' = comap p.mkq p' := rfl end ring end submodule namespace linear_map variables [ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [module R M] [module R M₂] [module R M₃] lemma range_mkq_comp (f : M →ₗ[R] M₂) : f.range.mkq.comp f = 0 := linear_map.ext $ λ x, by simp lemma ker_le_range_iff {f : M →ₗ[R] M₂} {g : M₂ →ₗ[R] M₃} : g.ker ≤ f.range ↔ f.range.mkq.comp g.ker.subtype = 0 := by rw [←range_le_ker_iff, submodule.ker_mkq, submodule.range_subtype] /-- A monomorphism is injective. -/ lemma ker_eq_bot_of_cancel {f : M →ₗ[R] M₂} (h : ∀ (u v : f.ker →ₗ[R] M), f.comp u = f.comp v → u = v) : f.ker = ⊥ := begin have h₁ : f.comp (0 : f.ker →ₗ[R] M) = 0 := comp_zero _, rw [←submodule.range_subtype f.ker, ←h 0 f.ker.subtype (eq.trans h₁ (comp_ker_subtype f).symm)], exact range_zero end /-- An epimorphism is surjective. -/ lemma range_eq_top_of_cancel {f : M →ₗ[R] M₂} (h : ∀ (u v : M₂ →ₗ[R] f.range.quotient), u.comp f = v.comp f → u = v) : f.range = ⊤ := begin have h₁ : (0 : M₂ →ₗ[R] f.range.quotient).comp f = 0 := zero_comp _, rw [←submodule.ker_mkq f.range, ←h 0 f.range.mkq (eq.trans h₁ (range_mkq_comp _).symm)], exact ker_zero end end linear_map @[simp] lemma linear_map.range_range_restrict [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [semimodule R M] [semimodule R M₂] (f : M →ₗ[R] M₂) : f.range_restrict.range = ⊤ := by simp [f.range_cod_restrict _] /-! ### Linear equivalences -/ namespace linear_equiv section add_comm_monoid variables [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M₄] section variables {semimodule_M : semimodule R M} {semimodule_M₂ : semimodule R M₂} variables (e e' : M ≃ₗ[R] M₂) lemma map_eq_comap {p : submodule R M} : (p.map e : submodule R M₂) = p.comap e.symm := submodule.coe_injective $ by simp [e.image_eq_preimage] /-- A linear equivalence of two modules restricts to a linear equivalence from any submodule of the domain onto the image of the submodule. -/ def of_submodule (p : submodule R M) : p ≃ₗ[R] ↥(p.map ↑e : submodule R M₂) := { inv_fun := λ y, ⟨e.symm y, by { rcases y with ⟨y', hy⟩, rw submodule.mem_map at hy, rcases hy with ⟨x, hx, hxy⟩, subst hxy, simp only [symm_apply_apply, submodule.coe_mk, coe_coe, hx], }⟩, left_inv := λ x, by simp, right_inv := λ y, by { apply set_coe.ext, simp, }, ..((e : M →ₗ[R] M₂).dom_restrict p).cod_restrict (p.map ↑e) (λ x, ⟨x, by simp⟩) } @[simp] lemma of_submodule_apply (p : submodule R M) (x : p) : ↑(e.of_submodule p x) = e x := rfl @[simp] lemma of_submodule_symm_apply (p : submodule R M) (x : (p.map ↑e : submodule R M₂)) : ↑((e.of_submodule p).symm x) = e.symm x := rfl end section prod variables {semimodule_M : semimodule R M} {semimodule_M₂ : semimodule R M₂} variables {semimodule_M₃ : semimodule R M₃} {semimodule_M₄ : semimodule R M₄} variables (e₁ : M ≃ₗ[R] M₂) (e₂ : M₃ ≃ₗ[R] M₄) /-- Product of linear equivalences; the maps come from `equiv.prod_congr`. -/ protected def prod : (M × M₃) ≃ₗ[R] (M₂ × M₄) := { map_add' := λ x y, prod.ext (e₁.map_add _ _) (e₂.map_add _ _), map_smul' := λ c x, prod.ext (e₁.map_smul c _) (e₂.map_smul c _), .. equiv.prod_congr e₁.to_equiv e₂.to_equiv } lemma prod_symm : (e₁.prod e₂).symm = e₁.symm.prod e₂.symm := rfl @[simp] lemma prod_apply (p) : e₁.prod e₂ p = (e₁ p.1, e₂ p.2) := rfl @[simp, norm_cast] lemma coe_prod : (e₁.prod e₂ : (M × M₃) →ₗ[R] (M₂ × M₄)) = (e₁ : M →ₗ[R] M₂).prod_map (e₂ : M₃ →ₗ[R] M₄) := rfl end prod section uncurry variables (V V₂ R) /-- Linear equivalence between a curried and uncurried function. Differs from `tensor_product.curry`. -/ protected def uncurry : (V → V₂ → R) ≃ₗ[R] (V × V₂ → R) := { map_add' := λ _ _, by { ext ⟨⟩, refl }, map_smul' := λ _ _, by { ext ⟨⟩, refl }, .. equiv.arrow_arrow_equiv_prod_arrow _ _ _} @[simp] lemma coe_uncurry : ⇑(linear_equiv.uncurry R V V₂) = uncurry := rfl @[simp] lemma coe_uncurry_symm : ⇑(linear_equiv.uncurry R V V₂).symm = curry := rfl end uncurry section variables {semimodule_M : semimodule R M} {semimodule_M₂ : semimodule R M₂} variables (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M) (e : M ≃ₗ[R] M₂) variables (p q : submodule R M) /-- Linear equivalence between two equal submodules. -/ def of_eq (h : p = q) : p ≃ₗ[R] q := { map_smul' := λ _ _, rfl, map_add' := λ _ _, rfl, .. equiv.set.of_eq (congr_arg _ h) } variables {p q} @[simp] lemma coe_of_eq_apply (h : p = q) (x : p) : (of_eq p q h x : M) = x := rfl @[simp] lemma of_eq_symm (h : p = q) : (of_eq p q h).symm = of_eq q p h.symm := rfl /-- A linear equivalence which maps a submodule of one module onto another, restricts to a linear equivalence of the two submodules. -/ def of_submodules (p : submodule R M) (q : submodule R M₂) (h : p.map ↑e = q) : p ≃ₗ[R] q := (e.of_submodule p).trans (linear_equiv.of_eq _ _ h) @[simp] lemma of_submodules_apply {p : submodule R M} {q : submodule R M₂} (h : p.map ↑e = q) (x : p) : ↑(e.of_submodules p q h x) = e x := rfl @[simp] lemma of_submodules_symm_apply {p : submodule R M} {q : submodule R M₂} (h : p.map ↑e = q) (x : q) : ↑((e.of_submodules p q h).symm x) = e.symm x := rfl variable (p) /-- The top submodule of `M` is linearly equivalent to `M`. -/ def of_top (h : p = ⊤) : p ≃ₗ[R] M := { inv_fun := λ x, ⟨x, h.symm ▸ trivial⟩, left_inv := λ ⟨x, h⟩, rfl, right_inv := λ x, rfl, .. p.subtype } @[simp] theorem of_top_apply {h} (x : p) : of_top p h x = x := rfl @[simp] theorem coe_of_top_symm_apply {h} (x : M) : ((of_top p h).symm x : M) = x := rfl theorem of_top_symm_apply {h} (x : M) : (of_top p h).symm x = ⟨x, h.symm ▸ trivial⟩ := rfl /-- If a linear map has an inverse, it is a linear equivalence. -/ def of_linear (h₁ : f.comp g = linear_map.id) (h₂ : g.comp f = linear_map.id) : M ≃ₗ[R] M₂ := { inv_fun := g, left_inv := linear_map.ext_iff.1 h₂, right_inv := linear_map.ext_iff.1 h₁, ..f } @[simp] theorem of_linear_apply {h₁ h₂} (x : M) : of_linear f g h₁ h₂ x = f x := rfl @[simp] theorem of_linear_symm_apply {h₁ h₂} (x : M₂) : (of_linear f g h₁ h₂).symm x = g x := rfl @[simp] protected theorem range : (e : M →ₗ[R] M₂).range = ⊤ := linear_map.range_eq_top.2 e.to_equiv.surjective lemma eq_bot_of_equiv [semimodule R M₂] (e : p ≃ₗ[R] (⊥ : submodule R M₂)) : p = ⊥ := begin refine bot_unique (submodule.le_def'.2 $ assume b hb, (submodule.mem_bot R).2 _), rw [← p.mk_eq_zero hb, ← e.map_eq_zero_iff], apply submodule.eq_zero_of_bot_submodule end @[simp] protected theorem ker : (e : M →ₗ[R] M₂).ker = ⊥ := linear_map.ker_eq_bot_of_injective e.to_equiv.injective end end add_comm_monoid section add_comm_group variables [semiring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] [add_comm_group M₄] variables {semimodule_M : semimodule R M} {semimodule_M₂ : semimodule R M₂} variables {semimodule_M₃ : semimodule R M₃} {semimodule_M₄ : semimodule R M₄} variables (e e₁ : M ≃ₗ[R] M₂) (e₂ : M₃ ≃ₗ[R] M₄) @[simp] theorem map_neg (a : M) : e (-a) = -e a := e.to_linear_map.map_neg a @[simp] theorem map_sub (a b : M) : e (a - b) = e a - e b := e.to_linear_map.map_sub a b /-- Equivalence given by a block lower diagonal matrix. `e₁` and `e₂` are diagonal square blocks, and `f` is a rectangular block below the diagonal. -/ protected def skew_prod (f : M →ₗ[R] M₄) : (M × M₃) ≃ₗ[R] M₂ × M₄ := { inv_fun := λ p : M₂ × M₄, (e₁.symm p.1, e₂.symm (p.2 - f (e₁.symm p.1))), left_inv := λ p, by simp, right_inv := λ p, by simp, .. ((e₁ : M →ₗ[R] M₂).comp (linear_map.fst R M M₃)).prod ((e₂ : M₃ →ₗ[R] M₄).comp (linear_map.snd R M M₃) + f.comp (linear_map.fst R M M₃)) } @[simp] lemma skew_prod_apply (f : M →ₗ[R] M₄) (x) : e₁.skew_prod e₂ f x = (e₁ x.1, e₂ x.2 + f x.1) := rfl @[simp] lemma skew_prod_symm_apply (f : M →ₗ[R] M₄) (x) : (e₁.skew_prod e₂ f).symm x = (e₁.symm x.1, e₂.symm (x.2 - f (e₁.symm x.1))) := rfl end add_comm_group section neg variables (R) [semiring R] [add_comm_group M] [semimodule R M] /-- `x ↦ -x` as a `linear_equiv` -/ def neg : M ≃ₗ[R] M := { .. equiv.neg M, .. (-linear_map.id : M →ₗ[R] M) } variable {R} @[simp] lemma coe_neg : ⇑(neg R : M ≃ₗ[R] M) = -id := rfl lemma neg_apply (x : M) : neg R x = -x := by simp @[simp] lemma symm_neg : (neg R : M ≃ₗ[R] M).symm = neg R := rfl end neg section ring variables [ring R] [add_comm_group M] [add_comm_group M₂] variables {semimodule_M : semimodule R M} {semimodule_M₂ : semimodule R M₂} variables (f : M →ₗ[R] M₂) (e : M ≃ₗ[R] M₂) /-- An `injective` linear map `f : M →ₗ[R] M₂` defines a linear equivalence between `M` and `f.range`. -/ noncomputable def of_injective (h : f.ker = ⊥) : M ≃ₗ[R] f.range := { .. (equiv.set.range f $ linear_map.ker_eq_bot.1 h).trans (equiv.set.of_eq f.range_coe.symm), .. f.cod_restrict f.range (λ x, f.mem_range_self x) } @[simp] theorem of_injective_apply {h : f.ker = ⊥} (x : M) : ↑(of_injective f h x) = f x := rfl /-- A bijective linear map is a linear equivalence. Here, bijectivity is described by saying that the kernel of `f` is `{0}` and the range is the universal set. -/ noncomputable def of_bijective (hf₁ : f.ker = ⊥) (hf₂ : f.range = ⊤) : M ≃ₗ[R] M₂ := (of_injective f hf₁).trans (of_top _ hf₂) @[simp] theorem of_bijective_apply {hf₁ hf₂} (x : M) : of_bijective f hf₁ hf₂ x = f x := rfl end ring section comm_ring variables [comm_ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] open linear_map /-- Multiplying by a unit `a` of the ring `R` is a linear equivalence. -/ def smul_of_unit (a : units R) : M ≃ₗ[R] M := of_linear ((a:R) • 1 : M →ₗ M) (((a⁻¹ : units R) : R) • 1 : M →ₗ M) (by rw [smul_comp, comp_smul, smul_smul, units.mul_inv, one_smul]; refl) (by rw [smul_comp, comp_smul, smul_smul, units.inv_mul, one_smul]; refl) /-- A linear isomorphism between the domains and codomains of two spaces of linear maps gives a linear isomorphism between the two function spaces. -/ def arrow_congr {R M₁ M₂ M₂₁ M₂₂ : Sort*} [comm_ring R] [add_comm_group M₁] [add_comm_group M₂] [add_comm_group M₂₁] [add_comm_group M₂₂] [module R M₁] [module R M₂] [module R M₂₁] [module R M₂₂] (e₁ : M₁ ≃ₗ[R] M₂) (e₂ : M₂₁ ≃ₗ[R] M₂₂) : (M₁ →ₗ[R] M₂₁) ≃ₗ[R] (M₂ →ₗ[R] M₂₂) := { to_fun := λ f, (e₂ : M₂₁ →ₗ[R] M₂₂).comp $ f.comp e₁.symm, inv_fun := λ f, (e₂.symm : M₂₂ →ₗ[R] M₂₁).comp $ f.comp e₁, left_inv := λ f, by { ext x, simp }, right_inv := λ f, by { ext x, simp }, map_add' := λ f g, by { ext x, simp }, map_smul' := λ c f, by { ext x, simp } } @[simp] lemma arrow_congr_apply {R M₁ M₂ M₂₁ M₂₂ : Sort*} [comm_ring R] [add_comm_group M₁] [add_comm_group M₂] [add_comm_group M₂₁] [add_comm_group M₂₂] [module R M₁] [module R M₂] [module R M₂₁] [module R M₂₂] (e₁ : M₁ ≃ₗ[R] M₂) (e₂ : M₂₁ ≃ₗ[R] M₂₂) (f : M₁ →ₗ[R] M₂₁) (x : M₂) : arrow_congr e₁ e₂ f x = e₂ (f (e₁.symm x)) := rfl @[simp] lemma arrow_congr_symm_apply {R M₁ M₂ M₂₁ M₂₂ : Sort*} [comm_ring R] [add_comm_group M₁] [add_comm_group M₂] [add_comm_group M₂₁] [add_comm_group M₂₂] [module R M₁] [module R M₂] [module R M₂₁] [module R M₂₂] (e₁ : M₁ ≃ₗ[R] M₂) (e₂ : M₂₁ ≃ₗ[R] M₂₂) (f : M₂ →ₗ[R] M₂₂) (x : M₁) : (arrow_congr e₁ e₂).symm f x = e₂.symm (f (e₁ x)) := rfl lemma arrow_congr_comp {N N₂ N₃ : Sort*} [add_comm_group N] [add_comm_group N₂] [add_comm_group N₃] [module R N] [module R N₂] [module R N₃] (e₁ : M ≃ₗ[R] N) (e₂ : M₂ ≃ₗ[R] N₂) (e₃ : M₃ ≃ₗ[R] N₃) (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) : arrow_congr e₁ e₃ (g.comp f) = (arrow_congr e₂ e₃ g).comp (arrow_congr e₁ e₂ f) := by { ext, simp only [symm_apply_apply, arrow_congr_apply, linear_map.comp_apply], } lemma arrow_congr_trans {M₁ M₂ M₃ N₁ N₂ N₃ : Sort*} [add_comm_group M₁] [module R M₁] [add_comm_group M₂] [module R M₂] [add_comm_group M₃] [module R M₃] [add_comm_group N₁] [module R N₁] [add_comm_group N₂] [module R N₂] [add_comm_group N₃] [module R N₃] (e₁ : M₁ ≃ₗ[R] M₂) (e₂ : N₁ ≃ₗ[R] N₂) (e₃ : M₂ ≃ₗ[R] M₃) (e₄ : N₂ ≃ₗ[R] N₃) : (arrow_congr e₁ e₂).trans (arrow_congr e₃ e₄) = arrow_congr (e₁.trans e₃) (e₂.trans e₄) := rfl /-- If `M₂` and `M₃` are linearly isomorphic then the two spaces of linear maps from `M` into `M₂` and `M` into `M₃` are linearly isomorphic. -/ def congr_right (f : M₂ ≃ₗ[R] M₃) : (M →ₗ[R] M₂) ≃ₗ (M →ₗ M₃) := arrow_congr (linear_equiv.refl R M) f /-- If `M` and `M₂` are linearly isomorphic then the two spaces of linear maps from `M` and `M₂` to themselves are linearly isomorphic. -/ def conj (e : M ≃ₗ[R] M₂) : (module.End R M) ≃ₗ[R] (module.End R M₂) := arrow_congr e e lemma conj_apply (e : M ≃ₗ[R] M₂) (f : module.End R M) : e.conj f = ((↑e : M →ₗ[R] M₂).comp f).comp e.symm := rfl lemma symm_conj_apply (e : M ≃ₗ[R] M₂) (f : module.End R M₂) : e.symm.conj f = ((↑e.symm : M₂ →ₗ[R] M).comp f).comp e := rfl lemma conj_comp (e : M ≃ₗ[R] M₂) (f g : module.End R M) : e.conj (g.comp f) = (e.conj g).comp (e.conj f) := arrow_congr_comp e e e f g lemma conj_trans (e₁ : M ≃ₗ[R] M₂) (e₂ : M₂ ≃ₗ[R] M₃) : e₁.conj.trans e₂.conj = (e₁.trans e₂).conj := by { ext f x, refl, } @[simp] lemma conj_id (e : M ≃ₗ[R] M₂) : e.conj linear_map.id = linear_map.id := by { ext, simp [conj_apply], } end comm_ring section field variables [field K] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [module K M] [module K M₂] [module K M₃] variables (K) (M) open linear_map /-- Multiplying by a nonzero element `a` of the field `K` is a linear equivalence. -/ def smul_of_ne_zero (a : K) (ha : a ≠ 0) : M ≃ₗ[K] M := smul_of_unit $ units.mk0 a ha section noncomputable theory open_locale classical lemma ker_to_span_singleton {x : M} (h : x ≠ 0) : (to_span_singleton K M x).ker = ⊥ := begin ext c, split, { intros hc, rw submodule.mem_bot, rw mem_ker at hc, by_contra hc', have : x = 0, calc x = c⁻¹ • (c • x) : by rw [← mul_smul, inv_mul_cancel hc', one_smul] ... = c⁻¹ • ((to_span_singleton K M x) c) : rfl ... = 0 : by rw [hc, smul_zero], tauto }, { rw [mem_ker, submodule.mem_bot], intros h, rw h, simp } end /-- Given a nonzero element `x` of a vector space `M` over a field `K`, the natural map from `K` to the span of `x`, with invertibility check to consider it as an isomorphism.-/ def to_span_nonzero_singleton (x : M) (h : x ≠ 0) : K ≃ₗ[K] (submodule.span K ({x} : set M)) := linear_equiv.trans (linear_equiv.of_injective (to_span_singleton K M x) (ker_to_span_singleton K M h)) (of_eq (to_span_singleton K M x).range (submodule.span K {x}) (span_singleton_eq_range K M x).symm) lemma to_span_nonzero_singleton_one (x : M) (h : x ≠ 0) : to_span_nonzero_singleton K M x h 1 = (⟨x, submodule.mem_span_singleton_self x⟩ : submodule.span K ({x} : set M)) := begin apply submodule.coe_eq_coe.mp, have : ↑(to_span_nonzero_singleton K M x h 1) = to_span_singleton K M x 1 := rfl, rw [this, to_span_singleton_one, submodule.coe_mk], end /-- Given a nonzero element `x` of a vector space `M` over a field `K`, the natural map from the span of `x` to `K`.-/ abbreviation coord (x : M) (h : x ≠ 0) : (submodule.span K ({x} : set M)) ≃ₗ[K] K := (to_span_nonzero_singleton K M x h).symm lemma coord_self (x : M) (h : x ≠ 0) : (coord K M x h) ( ⟨x, submodule.mem_span_singleton_self x⟩ : submodule.span K ({x} : set M)) = 1 := by rw [← to_span_nonzero_singleton_one K M x h, symm_apply_apply] end end field end linear_equiv namespace submodule section semimodule variables [semiring R] [add_comm_monoid M] [semimodule R M] /-- If `s ≤ t`, then we can view `s` as a submodule of `t` by taking the comap of `t.subtype`. -/ def comap_subtype_equiv_of_le {p q : submodule R M} (hpq : p ≤ q) : comap q.subtype p ≃ₗ[R] p := { to_fun := λ x, ⟨x, x.2⟩, inv_fun := λ x, ⟨⟨x, hpq x.2⟩, x.2⟩, left_inv := λ x, by simp only [coe_mk, submodule.eta, coe_coe], right_inv := λ x, by simp only [subtype.coe_mk, submodule.eta, coe_coe], map_add' := λ x y, rfl, map_smul' := λ c x, rfl } end semimodule variables [ring R] [add_comm_group M] [module R M] variables (p : submodule R M) open linear_map /-- If `p = ⊥`, then `M / p ≃ₗ[R] M`. -/ def quot_equiv_of_eq_bot (hp : p = ⊥) : p.quotient ≃ₗ[R] M := linear_equiv.of_linear (p.liftq id $ hp.symm ▸ bot_le) p.mkq (liftq_mkq _ _ _) $ p.quot_hom_ext $ λ x, rfl @[simp] lemma quot_equiv_of_eq_bot_apply_mk (hp : p = ⊥) (x : M) : p.quot_equiv_of_eq_bot hp (quotient.mk x) = x := rfl @[simp] lemma quot_equiv_of_eq_bot_symm_apply (hp : p = ⊥) (x : M) : (p.quot_equiv_of_eq_bot hp).symm x = quotient.mk x := rfl @[simp] lemma coe_quot_equiv_of_eq_bot_symm (hp : p = ⊥) : ((p.quot_equiv_of_eq_bot hp).symm : M →ₗ[R] p.quotient) = p.mkq := rfl variables (q : submodule R M) /-- Quotienting by equal submodules gives linearly equivalent quotients. -/ def quot_equiv_of_eq (h : p = q) : p.quotient ≃ₗ[R] q.quotient := { map_add' := by { rintros ⟨x⟩ ⟨y⟩, refl }, map_smul' := by { rintros x ⟨y⟩, refl }, ..@quotient.congr _ _ (quotient_rel p) (quotient_rel q) (equiv.refl _) $ λ a b, by { subst h, refl } } end submodule namespace submodule variables [comm_ring R] [add_comm_group M] [add_comm_group M₂] [module R M] [module R M₂] variables (p : submodule R M) (q : submodule R M₂) @[simp] lemma mem_map_equiv {e : M ≃ₗ[R] M₂} {x : M₂} : x ∈ p.map (e : M →ₗ[R] M₂) ↔ e.symm x ∈ p := begin rw submodule.mem_map, split, { rintros ⟨y, hy, hx⟩, simp [←hx, hy], }, { intros hx, refine ⟨e.symm x, hx, by simp⟩, }, end lemma comap_le_comap_smul (f : M →ₗ[R] M₂) (c : R) : comap f q ≤ comap (c • f) q := begin rw le_def', intros m h, change c • (f m) ∈ q, change f m ∈ q at h, apply q.smul_mem _ h, end lemma inf_comap_le_comap_add (f₁ f₂ : M →ₗ[R] M₂) : comap f₁ q ⊓ comap f₂ q ≤ comap (f₁ + f₂) q := begin rw le_def', intros m h, change f₁ m + f₂ m ∈ q, change f₁ m ∈ q ∧ f₂ m ∈ q at h, apply q.add_mem h.1 h.2, end /-- Given modules `M`, `M₂` over a commutative ring, together with submodules `p ⊆ M`, `q ⊆ M₂`, the set of maps $\\{f ∈ Hom(M, M₂) | f(p) ⊆ q \\}$ is a submodule of `Hom(M, M₂)`. -/ def compatible_maps : submodule R (M →ₗ[R] M₂) := { carrier := {f | p ≤ comap f q}, zero_mem' := by { change p ≤ comap 0 q, rw comap_zero, refine le_top, }, add_mem' := λ f₁ f₂ h₁ h₂, by { apply le_trans _ (inf_comap_le_comap_add q f₁ f₂), rw le_inf_iff, exact ⟨h₁, h₂⟩, }, smul_mem' := λ c f h, le_trans h (comap_le_comap_smul q f c), } /-- Given modules `M`, `M₂` over a commutative ring, together with submodules `p ⊆ M`, `q ⊆ M₂`, the natural map $\\{f ∈ Hom(M, M₂) | f(p) ⊆ q \\} \to Hom(M/p, M₂/q)$ is linear. -/ def mapq_linear : compatible_maps p q →ₗ[R] p.quotient →ₗ[R] q.quotient := { to_fun := λ f, mapq _ _ f.val f.property, map_add' := λ x y, by { ext m', apply quotient.induction_on' m', intros m, refl, }, map_smul' := λ c f, by { ext m', apply quotient.induction_on' m', intros m, refl, } } end submodule namespace equiv variables [semiring R] [add_comm_monoid M] [semimodule R M] [add_comm_monoid M₂] [semimodule R M₂] /-- An equivalence whose underlying function is linear is a linear equivalence. -/ def to_linear_equiv (e : M ≃ M₂) (h : is_linear_map R (e : M → M₂)) : M ≃ₗ[R] M₂ := { .. e, .. h.mk' e} end equiv namespace add_equiv variables [semiring R] [add_comm_monoid M] [semimodule R M] [add_comm_monoid M₂] [semimodule R M₂] /-- An additive equivalence whose underlying function preserves `smul` is a linear equivalence. -/ def to_linear_equiv (e : M ≃+ M₂) (h : ∀ (c : R) x, e (c • x) = c • e x) : M ≃ₗ[R] M₂ := { map_smul' := h, .. e, } @[simp] lemma coe_to_linear_equiv (e : M ≃+ M₂) (h : ∀ (c : R) x, e (c • x) = c • e x) : ⇑(e.to_linear_equiv h) = e := rfl @[simp] lemma coe_to_linear_equiv_symm (e : M ≃+ M₂) (h : ∀ (c : R) x, e (c • x) = c • e x) : ⇑(e.to_linear_equiv h).symm = e.symm := rfl end add_equiv namespace linear_map open submodule section isomorphism_laws variables [ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [module R M] [module R M₂] [module R M₃] variables (f : M →ₗ[R] M₂) /-- The first isomorphism law for modules. The quotient of `M` by the kernel of `f` is linearly equivalent to the range of `f`. -/ noncomputable def quot_ker_equiv_range : f.ker.quotient ≃ₗ[R] f.range := (linear_equiv.of_injective (f.ker.liftq f $ le_refl _) $ submodule.ker_liftq_eq_bot _ _ _ (le_refl f.ker)).trans (linear_equiv.of_eq _ _ $ submodule.range_liftq _ _ _) @[simp] lemma quot_ker_equiv_range_apply_mk (x : M) : (f.quot_ker_equiv_range (submodule.quotient.mk x) : M₂) = f x := rfl @[simp] lemma quot_ker_equiv_range_symm_apply_image (x : M) (h : f x ∈ f.range) : f.quot_ker_equiv_range.symm ⟨f x, h⟩ = f.ker.mkq x := f.quot_ker_equiv_range.symm_apply_apply (f.ker.mkq x) /-- Canonical linear map from the quotient `p/(p ∩ p')` to `(p+p')/p'`, mapping `x + (p ∩ p')` to `x + p'`, where `p` and `p'` are submodules of an ambient module. -/ def quotient_inf_to_sup_quotient (p p' : submodule R M) : (comap p.subtype (p ⊓ p')).quotient →ₗ[R] (comap (p ⊔ p').subtype p').quotient := (comap p.subtype (p ⊓ p')).liftq ((comap (p ⊔ p').subtype p').mkq.comp (of_le le_sup_left)) begin rw [ker_comp, of_le, comap_cod_restrict, ker_mkq, map_comap_subtype], exact comap_mono (inf_le_inf_right _ le_sup_left) end /-- Second Isomorphism Law : the canonical map from `p/(p ∩ p')` to `(p+p')/p'` as a linear isomorphism. -/ noncomputable def quotient_inf_equiv_sup_quotient (p p' : submodule R M) : (comap p.subtype (p ⊓ p')).quotient ≃ₗ[R] (comap (p ⊔ p').subtype p').quotient := linear_equiv.of_bijective (quotient_inf_to_sup_quotient p p') begin rw [quotient_inf_to_sup_quotient, ker_liftq_eq_bot], rw [ker_comp, ker_mkq], exact λ ⟨x, hx1⟩ hx2, ⟨hx1, hx2⟩ end begin rw [quotient_inf_to_sup_quotient, range_liftq, eq_top_iff'], rintros ⟨x, hx⟩, rcases mem_sup.1 hx with ⟨y, hy, z, hz, rfl⟩, use [⟨y, hy⟩, trivial], apply (submodule.quotient.eq _).2, change y - (y + z) ∈ p', rwa [sub_add_eq_sub_sub, sub_self, zero_sub, neg_mem_iff] end @[simp] lemma coe_quotient_inf_to_sup_quotient (p p' : submodule R M) : ⇑(quotient_inf_to_sup_quotient p p') = quotient_inf_equiv_sup_quotient p p' := rfl @[simp] lemma quotient_inf_equiv_sup_quotient_apply_mk (p p' : submodule R M) (x : p) : quotient_inf_equiv_sup_quotient p p' (submodule.quotient.mk x) = submodule.quotient.mk (of_le (le_sup_left : p ≤ p ⊔ p') x) := rfl lemma quotient_inf_equiv_sup_quotient_symm_apply_left (p p' : submodule R M) (x : p ⊔ p') (hx : (x:M) ∈ p) : (quotient_inf_equiv_sup_quotient p p').symm (submodule.quotient.mk x) = submodule.quotient.mk ⟨x, hx⟩ := (linear_equiv.symm_apply_eq _).2 $ by simp [of_le_apply] @[simp] lemma quotient_inf_equiv_sup_quotient_symm_apply_eq_zero_iff {p p' : submodule R M} {x : p ⊔ p'} : (quotient_inf_equiv_sup_quotient p p').symm (submodule.quotient.mk x) = 0 ↔ (x:M) ∈ p' := (linear_equiv.symm_apply_eq _).trans $ by simp [of_le_apply] lemma quotient_inf_equiv_sup_quotient_symm_apply_right (p p' : submodule R M) {x : p ⊔ p'} (hx : (x:M) ∈ p') : (quotient_inf_equiv_sup_quotient p p').symm (submodule.quotient.mk x) = 0 := quotient_inf_equiv_sup_quotient_symm_apply_eq_zero_iff.2 hx end isomorphism_laws section prod lemma is_linear_map_prod_iso {R M M₂ M₃ : Type*} [comm_semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_group M₃] [semimodule R M] [semimodule R M₂] [semimodule R M₃] : is_linear_map R (λ(p : (M →ₗ[R] M₂) × (M →ₗ[R] M₃)), (linear_map.prod p.1 p.2 : (M →ₗ[R] (M₂ × M₃)))) := ⟨λu v, rfl, λc u, rfl⟩ end prod section pi universe i variables [semiring R] [add_comm_monoid M₂] [semimodule R M₂] [add_comm_monoid M₃] [semimodule R M₃] {φ : ι → Type i} [∀i, add_comm_monoid (φ i)] [∀i, semimodule R (φ i)] /-- `pi` construction for linear functions. From a family of linear functions it produces a linear function into a family of modules. -/ def pi (f : Πi, M₂ →ₗ[R] φ i) : M₂ →ₗ[R] (Πi, φ i) := ⟨λc i, f i c, λ c d, funext $ λ i, (f i).map_add _ _, λ c d, funext $ λ i, (f i).map_smul _ _⟩ @[simp] lemma pi_apply (f : Πi, M₂ →ₗ[R] φ i) (c : M₂) (i : ι) : pi f c i = f i c := rfl lemma ker_pi (f : Πi, M₂ →ₗ[R] φ i) : ker (pi f) = (⨅i:ι, ker (f i)) := by ext c; simp [funext_iff]; refl lemma pi_eq_zero (f : Πi, M₂ →ₗ[R] φ i) : pi f = 0 ↔ (∀i, f i = 0) := by simp only [linear_map.ext_iff, pi_apply, funext_iff]; exact ⟨λh a b, h b a, λh a b, h b a⟩ lemma pi_zero : pi (λi, 0 : Πi, M₂ →ₗ[R] φ i) = 0 := by ext; refl lemma pi_comp (f : Πi, M₂ →ₗ[R] φ i) (g : M₃ →ₗ[R] M₂) : (pi f).comp g = pi (λi, (f i).comp g) := rfl /-- The projections from a family of modules are linear maps. -/ def proj (i : ι) : (Πi, φ i) →ₗ[R] φ i := ⟨ λa, a i, assume f g, rfl, assume c f, rfl ⟩ @[simp] lemma proj_apply (i : ι) (b : Πi, φ i) : (proj i : (Πi, φ i) →ₗ[R] φ i) b = b i := rfl lemma proj_pi (f : Πi, M₂ →ₗ[R] φ i) (i : ι) : (proj i).comp (pi f) = f i := ext $ assume c, rfl lemma infi_ker_proj : (⨅i, ker (proj i) : submodule R (Πi, φ i)) = ⊥ := bot_unique $ submodule.le_def'.2 $ assume a h, begin simp only [mem_infi, mem_ker, proj_apply] at h, exact (mem_bot _).2 (funext $ assume i, h i) end section variables (R φ) /-- If `I` and `J` are disjoint index sets, the product of the kernels of the `J`th projections of `φ` is linearly equivalent to the product over `I`. -/ def infi_ker_proj_equiv {I J : set ι} [decidable_pred (λi, i ∈ I)] (hd : disjoint I J) (hu : set.univ ⊆ I ∪ J) : (⨅i ∈ J, ker (proj i) : submodule R (Πi, φ i)) ≃ₗ[R] (Πi:I, φ i) := begin refine linear_equiv.of_linear (pi $ λi, (proj (i:ι)).comp (submodule.subtype _)) (cod_restrict _ (pi $ λi, if h : i ∈ I then proj (⟨i, h⟩ : I) else 0) _) _ _, { assume b, simp only [mem_infi, mem_ker, funext_iff, proj_apply, pi_apply], assume j hjJ, have : j ∉ I := assume hjI, hd ⟨hjI, hjJ⟩, rw [dif_neg this, zero_apply] }, { simp only [pi_comp, comp_assoc, subtype_comp_cod_restrict, proj_pi, dif_pos, subtype.coe_prop], ext b ⟨j, hj⟩, refl }, { ext1 ⟨b, hb⟩, apply subtype.ext, ext j, have hb : ∀i ∈ J, b i = 0, { simpa only [mem_infi, mem_ker, proj_apply] using (mem_infi _).1 hb }, simp only [comp_apply, pi_apply, id_apply, proj_apply, subtype_apply, cod_restrict_apply], split_ifs, { refl }, { exact (hb _ $ (hu trivial).resolve_left h).symm } } end end section variable [decidable_eq ι] /-- `diag i j` is the identity map if `i = j`. Otherwise it is the constant 0 map. -/ def diag (i j : ι) : φ i →ₗ[R] φ j := @function.update ι (λj, φ i →ₗ[R] φ j) _ 0 i id j lemma update_apply (f : Πi, M₂ →ₗ[R] φ i) (c : M₂) (i j : ι) (b : M₂ →ₗ[R] φ i) : (update f i b j) c = update (λi, f i c) i (b c) j := begin by_cases j = i, { rw [h, update_same, update_same] }, { rw [update_noteq h, update_noteq h] } end end section variable [decidable_eq ι] variables (R φ) /-- The standard basis of the product of `φ`. -/ def std_basis (i : ι) : φ i →ₗ[R] (Πi, φ i) := pi (diag i) lemma std_basis_apply (i : ι) (b : φ i) : std_basis R φ i b = update 0 i b := by ext j; rw [std_basis, pi_apply, diag, update_apply]; refl @[simp] lemma std_basis_same (i : ι) (b : φ i) : std_basis R φ i b i = b := by rw [std_basis_apply, update_same] lemma std_basis_ne (i j : ι) (h : j ≠ i) (b : φ i) : std_basis R φ i b j = 0 := by rw [std_basis_apply, update_noteq h]; refl lemma ker_std_basis (i : ι) : ker (std_basis R φ i) = ⊥ := ker_eq_bot_of_injective $ assume f g hfg, have std_basis R φ i f i = std_basis R φ i g i := hfg ▸ rfl, by simpa only [std_basis_same] lemma proj_comp_std_basis (i j : ι) : (proj i).comp (std_basis R φ j) = diag j i := by rw [std_basis, proj_pi] lemma proj_std_basis_same (i : ι) : (proj i).comp (std_basis R φ i) = id := by ext b; simp lemma proj_std_basis_ne (i j : ι) (h : i ≠ j) : (proj i).comp (std_basis R φ j) = 0 := by ext b; simp [std_basis_ne R φ _ _ h] lemma supr_range_std_basis_le_infi_ker_proj (I J : set ι) (h : disjoint I J) : (⨆i∈I, range (std_basis R φ i)) ≤ (⨅i∈J, ker (proj i)) := begin refine (supr_le $ assume i, supr_le $ assume hi, range_le_iff_comap.2 _), simp only [(ker_comp _ _).symm, eq_top_iff, le_def', mem_ker, comap_infi, mem_infi], assume b hb j hj, have : i ≠ j := assume eq, h ⟨hi, eq.symm ▸ hj⟩, rw [proj_std_basis_ne R φ j i this.symm, zero_apply] end lemma infi_ker_proj_le_supr_range_std_basis {I : finset ι} {J : set ι} (hu : set.univ ⊆ ↑I ∪ J) : (⨅ i∈J, ker (proj i)) ≤ (⨆i∈I, range (std_basis R φ i)) := submodule.le_def'.2 begin assume b hb, simp only [mem_infi, mem_ker, proj_apply] at hb, rw ← show ∑ i in I, std_basis R φ i (b i) = b, { ext i, rw [finset.sum_apply, ← std_basis_same R φ i (b i)], refine finset.sum_eq_single i (assume j hjI ne, std_basis_ne _ _ _ _ ne.symm _) _, assume hiI, rw [std_basis_same], exact hb _ ((hu trivial).resolve_left hiI) }, exact sum_mem _ (assume i hiI, mem_supr_of_mem i $ mem_supr_of_mem hiI $ (std_basis R φ i).mem_range_self (b i)) end lemma supr_range_std_basis_eq_infi_ker_proj {I J : set ι} (hd : disjoint I J) (hu : set.univ ⊆ I ∪ J) (hI : set.finite I) : (⨆i∈I, range (std_basis R φ i)) = (⨅i∈J, ker (proj i)) := begin refine le_antisymm (supr_range_std_basis_le_infi_ker_proj _ _ _ _ hd) _, have : set.univ ⊆ ↑hI.to_finset ∪ J, { rwa [hI.coe_to_finset] }, refine le_trans (infi_ker_proj_le_supr_range_std_basis R φ this) (supr_le_supr $ assume i, _), rw [set.finite.mem_to_finset], exact le_refl _ end lemma supr_range_std_basis [fintype ι] : (⨆i:ι, range (std_basis R φ i)) = ⊤ := have (set.univ : set ι) ⊆ ↑(finset.univ : finset ι) ∪ ∅ := by rw [finset.coe_univ, set.union_empty], begin apply top_unique, convert (infi_ker_proj_le_supr_range_std_basis R φ this), exact infi_emptyset.symm, exact (funext $ λi, (@supr_pos _ _ _ (λh, range (std_basis R φ i)) $ finset.mem_univ i).symm) end lemma disjoint_std_basis_std_basis (I J : set ι) (h : disjoint I J) : disjoint (⨆i∈I, range (std_basis R φ i)) (⨆i∈J, range (std_basis R φ i)) := begin refine disjoint.mono (supr_range_std_basis_le_infi_ker_proj _ _ _ _ $ disjoint_compl_right) (supr_range_std_basis_le_infi_ker_proj _ _ _ _ $ disjoint_compl_right) _, simp only [disjoint, submodule.le_def', mem_infi, mem_inf, mem_ker, mem_bot, proj_apply, funext_iff], rintros b ⟨hI, hJ⟩ i, classical, by_cases hiI : i ∈ I, { by_cases hiJ : i ∈ J, { exact (h ⟨hiI, hiJ⟩).elim }, { exact hJ i hiJ } }, { exact hI i hiI } end lemma std_basis_eq_single {a : R} : (λ (i : ι), (std_basis R (λ _ : ι, R) i) a) = λ (i : ι), (finsupp.single i a) := begin ext i j, rw [std_basis_apply, finsupp.single_apply], split_ifs, { rw [h, function.update_same] }, { rw [function.update_noteq (ne.symm h)], refl }, end end end pi section fun_left variables (R M) [semiring R] [add_comm_monoid M] [semimodule R M] variables {m n p : Type*} /-- Given an `R`-module `M` and a function `m → n` between arbitrary types, construct a linear map `(n → M) →ₗ[R] (m → M)` -/ def fun_left (f : m → n) : (n → M) →ₗ[R] (m → M) := mk (∘f) (λ _ _, rfl) (λ _ _, rfl) @[simp] theorem fun_left_apply (f : m → n) (g : n → M) (i : m) : fun_left R M f g i = g (f i) := rfl @[simp] theorem fun_left_id (g : n → M) : fun_left R M _root_.id g = g := rfl theorem fun_left_comp (f₁ : n → p) (f₂ : m → n) : fun_left R M (f₁ ∘ f₂) = (fun_left R M f₂).comp (fun_left R M f₁) := rfl /-- Given an `R`-module `M` and an equivalence `m ≃ n` between arbitrary types, construct a linear equivalence `(n → M) ≃ₗ[R] (m → M)` -/ def fun_congr_left (e : m ≃ n) : (n → M) ≃ₗ[R] (m → M) := linear_equiv.of_linear (fun_left R M e) (fun_left R M e.symm) (ext $ λ x, funext $ λ i, by rw [id_apply, ← fun_left_comp, equiv.symm_comp_self, fun_left_id]) (ext $ λ x, funext $ λ i, by rw [id_apply, ← fun_left_comp, equiv.self_comp_symm, fun_left_id]) @[simp] theorem fun_congr_left_apply (e : m ≃ n) (x : n → M) : fun_congr_left R M e x = fun_left R M e x := rfl @[simp] theorem fun_congr_left_id : fun_congr_left R M (equiv.refl n) = linear_equiv.refl R (n → M) := rfl @[simp] theorem fun_congr_left_comp (e₁ : m ≃ n) (e₂ : n ≃ p) : fun_congr_left R M (equiv.trans e₁ e₂) = linear_equiv.trans (fun_congr_left R M e₂) (fun_congr_left R M e₁) := rfl @[simp] lemma fun_congr_left_symm (e : m ≃ n) : (fun_congr_left R M e).symm = fun_congr_left R M e.symm := rfl end fun_left universe i variables [semiring R] [add_comm_monoid M] [semimodule R M] variables (R M) instance automorphism_group : group (M ≃ₗ[R] M) := { mul := λ f g, g.trans f, one := linear_equiv.refl R M, inv := λ f, f.symm, mul_assoc := λ f g h, by {ext, refl}, mul_one := λ f, by {ext, refl}, one_mul := λ f, by {ext, refl}, mul_left_inv := λ f, by {ext, exact f.left_inv x} } instance automorphism_group.to_linear_map_is_monoid_hom : is_monoid_hom (linear_equiv.to_linear_map : (M ≃ₗ[R] M) → (M →ₗ[R] M)) := { map_one := rfl, map_mul := λ f g, rfl } /-- The group of invertible linear maps from `M` to itself -/ @[reducible] def general_linear_group := units (M →ₗ[R] M) namespace general_linear_group variables {R M} instance : has_coe_to_fun (general_linear_group R M) := by apply_instance /-- An invertible linear map `f` determines an equivalence from `M` to itself. -/ def to_linear_equiv (f : general_linear_group R M) : (M ≃ₗ[R] M) := { inv_fun := f.inv.to_fun, left_inv := λ m, show (f.inv * f.val) m = m, by erw f.inv_val; simp, right_inv := λ m, show (f.val * f.inv) m = m, by erw f.val_inv; simp, ..f.val } /-- An equivalence from `M` to itself determines an invertible linear map. -/ def of_linear_equiv (f : (M ≃ₗ[R] M)) : general_linear_group R M := { val := f, inv := f.symm, val_inv := linear_map.ext $ λ _, f.apply_symm_apply _, inv_val := linear_map.ext $ λ _, f.symm_apply_apply _ } variables (R M) /-- The general linear group on `R` and `M` is multiplicatively equivalent to the type of linear equivalences between `M` and itself. -/ def general_linear_equiv : general_linear_group R M ≃* (M ≃ₗ[R] M) := { to_fun := to_linear_equiv, inv_fun := of_linear_equiv, left_inv := λ f, by { ext, refl }, right_inv := λ f, by { ext, refl }, map_mul' := λ x y, by {ext, refl} } @[simp] lemma general_linear_equiv_to_linear_map (f : general_linear_group R M) : (general_linear_equiv R M f : M →ₗ[R] M) = f := by {ext, refl} end general_linear_group end linear_map
3e027f99e039152e70d78b886d90275b90e516c9
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/data/polynomial/iterated_deriv.lean
3c5164c3ccdeb40f8de56a06da26f8ff5f4879b9
[ "Apache-2.0" ]
permissive
waynemunro/mathlib
e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552
065a70810b5480d584033f7bbf8e0409480c2118
refs/heads/master
1,693,417,182,397
1,634,644,781,000
1,634,644,781,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,969
lean
/- Copyright (c) 2020 Jujian Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jujian Zhang -/ import data.nat.interval import data.polynomial.derivative import tactic.linarith /-! # Theory of iterated derivative We define and prove some lemmas about iterated (formal) derivative for polynomials over a semiring. -/ noncomputable theory open finset nat polynomial open_locale big_operators namespace polynomial universes u variable {R : Type u} section semiring variables [semiring R] (r : R) (f p q : polynomial R) (n k : ℕ) /-- `iterated_deriv f n` is the `n`-th formal derivative of the polynomial `f` -/ def iterated_deriv : polynomial R := derivative ^[n] f @[simp] lemma iterated_deriv_zero_right : iterated_deriv f 0 = f := rfl lemma iterated_deriv_succ : iterated_deriv f (n + 1) = (iterated_deriv f n).derivative := by rw [iterated_deriv, iterated_deriv, function.iterate_succ'] @[simp] lemma iterated_deriv_zero_left : iterated_deriv (0 : polynomial R) n = 0 := begin induction n with n hn, { exact iterated_deriv_zero_right _ }, { rw [iterated_deriv_succ, hn, derivative_zero] }, end @[simp] lemma iterated_deriv_add : iterated_deriv (p + q) n = iterated_deriv p n + iterated_deriv q n := begin induction n with n ih, { simp only [iterated_deriv_zero_right], }, { simp only [iterated_deriv_succ, ih, derivative_add] } end @[simp] lemma iterated_deriv_smul : iterated_deriv (r • p) n = r • iterated_deriv p n := begin induction n with n ih, { simp only [iterated_deriv_zero_right] }, { simp only [iterated_deriv_succ, ih, derivative_smul] } end @[simp] lemma iterated_deriv_X_zero : iterated_deriv (X : polynomial R) 0 = X := by simp only [iterated_deriv_zero_right] @[simp] lemma iterated_deriv_X_one : iterated_deriv (X : polynomial R) 1 = 1 := by simp only [iterated_deriv, derivative_X, function.iterate_one] @[simp] lemma iterated_deriv_X (h : 1 < n) : iterated_deriv (X : polynomial R) n = 0 := begin induction n with n ih, { exfalso, exact nat.not_lt_zero 1 h }, { simp only [iterated_deriv_succ], by_cases H : n = 1, { rw H, simp only [iterated_deriv_X_one, derivative_one] }, { replace h : 1 < n := array.push_back_idx h (ne.symm H), rw ih h, simp only [derivative_zero] } } end @[simp] lemma iterated_deriv_C_zero : iterated_deriv (C r) 0 = C r := by simp only [iterated_deriv_zero_right] @[simp] lemma iterated_deriv_C (h : 0 < n) : iterated_deriv (C r) n = 0 := begin induction n with n ih, { exfalso, exact nat.lt_asymm h h }, { by_cases H : n = 0, { rw [iterated_deriv_succ, H], simp only [iterated_deriv_C_zero, derivative_C] }, { replace h : 0 < n := nat.pos_of_ne_zero H, rw [iterated_deriv_succ, ih h], simp only [derivative_zero] } } end @[simp] lemma iterated_deriv_one_zero : iterated_deriv (1 : polynomial R) 0 = 1 := by simp only [iterated_deriv_zero_right] @[simp] lemma iterated_deriv_one : 0 < n → iterated_deriv (1 : polynomial R) n = 0 := λ h, begin have eq1 : (1 : polynomial R) = C 1 := by simp only [ring_hom.map_one], rw eq1, exact iterated_deriv_C _ _ h, end end semiring section ring variables [ring R] (p q : polynomial R) (n : ℕ) @[simp] lemma iterated_deriv_neg : iterated_deriv (-p) n = - iterated_deriv p n := begin induction n with n ih, { simp only [iterated_deriv_zero_right] }, { simp only [iterated_deriv_succ, ih, derivative_neg] } end @[simp] lemma iterated_deriv_sub : iterated_deriv (p - q) n = iterated_deriv p n - iterated_deriv q n := by rw [sub_eq_add_neg, iterated_deriv_add, iterated_deriv_neg, ←sub_eq_add_neg] end ring section comm_semiring variable [comm_semiring R] variables (f p q : polynomial R) (n k : ℕ) lemma coeff_iterated_deriv_as_prod_Ico : ∀ m : ℕ, (iterated_deriv f k).coeff m = (∏ i in Ico m.succ (m + k.succ), i) * (f.coeff (m+k)) := begin induction k with k ih, { simp only [add_zero, forall_const, one_mul, Ico_self, eq_self_iff_true, iterated_deriv_zero_right, prod_empty] }, { intro m, rw [iterated_deriv_succ, coeff_derivative, ih (m+1), mul_right_comm], apply congr_arg2, { have set_eq : (Ico m.succ (m + k.succ.succ)) = (Ico (m + 1).succ (m + 1 + k.succ)) ∪ {m+1}, { rw [union_comm, ←insert_eq, Ico_insert_succ_left, add_succ, add_succ, add_succ _ k, ←succ_eq_add_one, succ_add], rw succ_eq_add_one, linarith }, rw [set_eq, prod_union], apply congr_arg2, { refl }, { simp only [prod_singleton], norm_cast }, { rw [disjoint_singleton_right, mem_Ico], exact λ h, (nat.lt_succ_self _).not_le h.1 } }, { exact congr_arg _ (succ_add m k) } }, end lemma coeff_iterated_deriv_as_prod_range : ∀ m : ℕ, (iterated_deriv f k).coeff m = f.coeff (m + k) * (∏ i in range k, ↑(m + k - i)) := begin induction k with k ih, { simp }, intro m, calc (f.iterated_deriv k.succ).coeff m = f.coeff (m + k.succ) * (∏ i in range k, ↑(m + k.succ - i)) * (m + 1) : by rw [iterated_deriv_succ, coeff_derivative, ih m.succ, succ_add, add_succ] ... = f.coeff (m + k.succ) * (∏ i in range k, ↑(m + k.succ - i)) * ↑(m + 1) : by push_cast ... = f.coeff (m + k.succ) * (∏ i in range k.succ, ↑(m + k.succ - i)) : by rw [prod_range_succ, nat.add_sub_assoc k.le_succ, succ_sub le_rfl, nat.sub_self, mul_assoc] end lemma iterated_deriv_eq_zero_of_nat_degree_lt (h : f.nat_degree < n) : iterated_deriv f n = 0 := begin ext m, rw [coeff_iterated_deriv_as_prod_range, coeff_zero, coeff_eq_zero_of_nat_degree_lt, zero_mul], linarith end lemma iterated_deriv_mul : iterated_deriv (p * q) n = ∑ k in range n.succ, (C (n.choose k : R)) * iterated_deriv p (n - k) * iterated_deriv q k := begin induction n with n IH, { simp }, calc (p * q).iterated_deriv n.succ = (∑ (k : ℕ) in range n.succ, C ↑(n.choose k) * p.iterated_deriv (n - k) * q.iterated_deriv k).derivative : by rw [iterated_deriv_succ, IH] ... = ∑ (k : ℕ) in range n.succ, C ↑(n.choose k) * p.iterated_deriv (n - k + 1) * q.iterated_deriv k + ∑ (k : ℕ) in range n.succ, C ↑(n.choose k) * p.iterated_deriv (n - k) * q.iterated_deriv (k + 1) : by simp_rw [derivative_sum, derivative_mul, derivative_C, zero_mul, zero_add, iterated_deriv_succ, sum_add_distrib] ... = (∑ (k : ℕ) in range n.succ, C ↑(n.choose k.succ) * p.iterated_deriv (n - k) * q.iterated_deriv (k + 1) + C ↑1 * p.iterated_deriv n.succ * q.iterated_deriv 0) + ∑ (k : ℕ) in range n.succ, C ↑(n.choose k) * p.iterated_deriv (n - k) * q.iterated_deriv (k + 1) : _ ... = ∑ (k : ℕ) in range n.succ, C ↑(n.choose k) * p.iterated_deriv (n - k) * q.iterated_deriv (k + 1) + ∑ (k : ℕ) in range n.succ, C ↑(n.choose k.succ) * p.iterated_deriv (n - k) * q.iterated_deriv (k + 1) + C ↑1 * p.iterated_deriv n.succ * q.iterated_deriv 0 : by ring ... = ∑ (i : ℕ) in range n.succ, C ↑((n+1).choose (i+1)) * p.iterated_deriv (n + 1 - (i+1)) * q.iterated_deriv (i+1) + C ↑1 * p.iterated_deriv n.succ * q.iterated_deriv 0 : by simp_rw [choose_succ_succ, succ_sub_succ, cast_add, C.map_add, add_mul, sum_add_distrib] ... = ∑ (k : ℕ) in range n.succ.succ, C ↑(n.succ.choose k) * p.iterated_deriv (n.succ - k) * q.iterated_deriv k : by rw [sum_range_succ' _ n.succ, choose_zero_right, nat.sub_zero], congr, refine (sum_range_succ' _ _).trans (congr_arg2 (+) _ _), { rw [sum_range_succ, nat.choose_succ_self, cast_zero, C.map_zero, zero_mul, zero_mul, add_zero], refine sum_congr rfl (λ k hk, _), rw mem_range at hk, congr, rw [← nat.sub_add_comm (nat.succ_le_of_lt hk), nat.succ_sub_succ] }, { rw [choose_zero_right, nat.sub_zero] }, end end comm_semiring end polynomial
3e381dbc4f55df707986f48695791940c1bb1ecb
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Lean/Compiler/IR/Sorry.lean
0536b07d74d0c45c5b063997a25cc452683c1d7c
[ "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,268
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.Compiler.IR.CompilerM namespace Lean.IR namespace Sorry structure State where localSorryMap : NameMap Name := {} modified : Bool := false abbrev M := StateT State CompilerM def visitExpr : Expr → ExceptT Name M Unit | Expr.fap f _ => getSorryDepFor? f | Expr.pap f _ => getSorryDepFor? f | _ => return () where getSorryDepFor? (f : Name) : ExceptT Name M Unit := do let found (g : Name) := if g == ``sorryAx then throw f else throw g if f == ``sorryAx then throw f else if let some g := (← get).localSorryMap.find? f then found g else match (← findDecl f) with | Decl.fdecl (info := { sorryDep? := some g, .. }) .. => found g | _ => return () partial def visitFndBody (b : FnBody) : ExceptT Name M Unit := do match b with | FnBody.vdecl _ _ v b => visitExpr v; visitFndBody b | FnBody.jdecl _ _ v b => visitFndBody v; visitFndBody b | FnBody.case _ _ _ alts => alts.forM fun alt => visitFndBody alt.body | _ => unless b.isTerminal do let (instr, b) := b.split visitFndBody b def visitDecl (d : Decl) : M Unit := do match d with | Decl.fdecl (f := f) (body := b) .. => match (← get).localSorryMap.find? f with | some _ => return () | none => match (← visitFndBody b |>.run) with | Except.ok _ => return () | Except.error g => modify fun s => { localSorryMap := s.localSorryMap.insert f g modified := true } | _ => return () partial def collect (decls : Array Decl) : M Unit := do modify fun s => { s with modified := false } decls.forM visitDecl if (← get).modified then collect decls end Sorry def updateSorryDep (decls : Array Decl) : CompilerM (Array Decl) := do let (_, s) ← Sorry.collect decls |>.run {} return decls.map fun decl => match decl with | Decl.fdecl f xs t b info => match s.localSorryMap.find? f with | some g => Decl.fdecl f xs t b { info with sorryDep? := some g } | _ => decl | _ => decl end Lean.IR
3cba40b9107c827b83cce0835ffcfad35ec0b2f4
fecda8e6b848337561d6467a1e30cf23176d6ad0
/src/data/mv_polynomial/basic.lean
920170892f0c5a69105c5bed75918104002e0248
[ "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
34,655
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Johan Commelin, Mario Carneiro -/ import data.polynomial.eval /-! # Multivariate polynomials This file defines polynomial rings over a base ring (or even semiring), with variables from a general type `σ` (which could be infinite). ## Important definitions Let `R` be a commutative ring (or a semiring) and let `σ` be an arbitrary type. This file creates the type `mv_polynomial σ R`, which mathematicians might denote $R[X_i : i \in \sigma]$. It is the type of multivariate (a.k.a. multivariable) polynomials, with variables corresponding to the terms in `σ`, and coefficients in `R`. ### Notation In the definitions below, we use the following notation: + `σ : Type*` (indexing the variables) + `R : Type*` `[comm_semiring R]` (the coefficients) + `s : σ →₀ ℕ`, a function from `σ` to `ℕ` which is zero away from a finite set. This will give rise to a monomial in `mv_polynomial σ R` which mathematicians might call `X^s` + `a : R` + `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians + `p : mv_polynomial σ R` ### Definitions * `mv_polynomial σ R` : the type of polynomials with variables of type `σ` and coefficients in the commutative semiring `R` * `monomial s a` : the monomial which mathematically would be denoted `a * X^s` * `C a` : the constant polynomial with value `a` * `X i` : the degree one monomial corresponding to i; mathematically this might be denoted `Xᵢ`. * `coeff s p` : the coefficient of `s` in `p`. * `eval₂ (f : R → S) (g : σ → S) p` : given a semiring homomorphism from `R` to another semiring `S`, and a map `σ → S`, evaluates `p` at this valuation, returning a term of type `S`. Note that `eval₂` can be made using `eval` and `map` (see below), and it has been suggested that sticking to `eval` and `map` might make the code less brittle. * `eval (g : σ → R) p` : given a map `σ → R`, evaluates `p` at this valuation, returning a term of type `R` * `map (f : R → S) p` : returns the multivariate polynomial obtained from `p` by the change of coefficient semiring corresponding to `f` ## Implementation notes Recall that if `Y` has a zero, then `X →₀ Y` is the type of functions from `X` to `Y` with finite support, i.e. such that only finitely many elements of `X` get sent to non-zero terms in `Y`. The definition of `mv_polynomial σ α` is `(σ →₀ ℕ) →₀ α` ; here `σ →₀ ℕ` denotes the space of all monomials in the variables, and the function to `α` sends a monomial to its coefficient in the polynomial being represented. ## Tags polynomial, multivariate polynomial, multivariable polynomial -/ noncomputable theory open_locale classical big_operators open set function finsupp add_monoid_algebra open_locale big_operators universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} /-- Multivariate polynomial, where `σ` is the index set of the variables and `α` is the coefficient ring -/ def mv_polynomial (σ : Type*) (α : Type*) [comm_semiring α] := add_monoid_algebra α (σ →₀ ℕ) namespace mv_polynomial variables {σ : Type*} {a a' a₁ a₂ : α} {e : ℕ} {n m : σ} {s : σ →₀ ℕ} section comm_semiring variables [comm_semiring α] {p q : mv_polynomial σ α} instance decidable_eq_mv_polynomial [decidable_eq σ] [decidable_eq α] : decidable_eq (mv_polynomial σ α) := finsupp.decidable_eq instance : comm_semiring (mv_polynomial σ α) := add_monoid_algebra.comm_semiring instance : inhabited (mv_polynomial σ α) := ⟨0⟩ instance : has_scalar α (mv_polynomial σ α) := add_monoid_algebra.has_scalar instance : semimodule α (mv_polynomial σ α) := add_monoid_algebra.semimodule instance : algebra α (mv_polynomial σ α) := add_monoid_algebra.algebra /-- the coercion turning an `mv_polynomial` into the function which reports the coefficient of a given monomial -/ def coeff_coe_to_fun : has_coe_to_fun (mv_polynomial σ α) := finsupp.has_coe_to_fun local attribute [instance] coeff_coe_to_fun /-- `monomial s a` is the monomial `a * X^s` -/ def monomial (s : σ →₀ ℕ) (a : α) : mv_polynomial σ α := single s a /-- `C a` is the constant polynomial with value `a` -/ def C : α →+* mv_polynomial σ α := { to_fun := monomial 0, map_zero' := by simp [monomial], map_one' := rfl, map_add' := λ a a', single_add, map_mul' := λ a a', by simp [monomial, single_mul_single] } variables (α σ) theorem algebra_map_eq : algebra_map α (mv_polynomial σ α) = C := rfl variables {α σ} /-- `X n` is the degree `1` monomial `1*n` -/ def X (n : σ) : mv_polynomial σ α := monomial (single n 1) 1 @[simp] lemma C_0 : C 0 = (0 : mv_polynomial σ α) := by simp [C, monomial]; refl @[simp] lemma C_1 : C 1 = (1 : mv_polynomial σ α) := rfl lemma C_mul_monomial : C a * monomial s a' = monomial s (a * a') := by simp [C, monomial, single_mul_single] @[simp] lemma C_add : (C (a + a') : mv_polynomial σ α) = C a + C a' := single_add @[simp] lemma C_mul : (C (a * a') : mv_polynomial σ α) = C a * C a' := C_mul_monomial.symm @[simp] lemma C_pow (a : α) (n : ℕ) : (C (a^n) : mv_polynomial σ α) = (C a)^n := by induction n; simp [pow_succ, *] lemma C_injective (σ : Type*) (R : Type*) [comm_ring R] : function.injective (C : R → mv_polynomial σ R) := finsupp.single_injective _ @[simp] lemma C_inj {σ : Type*} (R : Type*) [comm_ring R] (r s : R) : (C r : mv_polynomial σ R) = C s ↔ r = s := (C_injective σ R).eq_iff lemma C_eq_coe_nat (n : ℕ) : (C ↑n : mv_polynomial σ α) = n := by induction n; simp [nat.succ_eq_add_one, *] theorem C_mul' : mv_polynomial.C a * p = a • p := begin apply finsupp.induction p, { exact (mul_zero $ mv_polynomial.C a).trans (@smul_zero α (mv_polynomial σ α) _ _ _ a).symm }, intros p b f haf hb0 ih, rw [mul_add, ih, @smul_add α (mv_polynomial σ α) _ _ _ a], congr' 1, rw [add_monoid_algebra.mul_def, finsupp.smul_single], simp only [mv_polynomial.C], dsimp [mv_polynomial.monomial], rw [finsupp.sum_single_index, finsupp.sum_single_index, zero_add], { rw [mul_zero, finsupp.single_zero] }, { rw finsupp.sum_single_index, all_goals { rw [zero_mul, finsupp.single_zero] }, } end lemma smul_eq_C_mul (p : mv_polynomial σ α) (a : α) : a • p = C a * p := C_mul'.symm lemma X_pow_eq_single : X n ^ e = monomial (single n e) (1 : α) := begin induction e, { simp [X], refl }, { simp [pow_succ, e_ih], simp [X, monomial, single_mul_single, nat.succ_eq_add_one, add_comm] } end lemma monomial_add_single : monomial (s + single n e) a = (monomial s a * X n ^ e) := by rw [X_pow_eq_single, monomial, monomial, monomial, single_mul_single]; simp lemma monomial_single_add : monomial (single n e + s) a = (X n ^ e * monomial s a) := by rw [X_pow_eq_single, monomial, monomial, monomial, single_mul_single]; simp lemma single_eq_C_mul_X {s : σ} {a : α} {n : ℕ} : monomial (single s n) a = C a * (X s)^n := by { rw [← zero_add (single s n), monomial_add_single, C], refl } @[simp] lemma monomial_add {s : σ →₀ ℕ} {a b : α} : monomial s a + monomial s b = monomial s (a + b) := by simp [monomial] @[simp] lemma monomial_mul {s s' : σ →₀ ℕ} {a b : α} : monomial s a * monomial s' b = monomial (s + s') (a * b) := by rw [monomial, monomial, monomial, add_monoid_algebra.single_mul_single] @[simp] lemma monomial_zero {s : σ →₀ ℕ}: monomial s (0 : α) = 0 := by rw [monomial, single_zero]; refl @[simp] lemma sum_monomial {A : Type*} [add_comm_monoid A] {u : σ →₀ ℕ} {r : α} {b : (σ →₀ ℕ) → α → A} (w : b u 0 = 0) : sum (monomial u r) b = b u r := sum_single_index w lemma monomial_eq : monomial s a = C a * (s.prod $ λn e, X n ^ e : mv_polynomial σ α) := begin apply @finsupp.induction σ ℕ _ _ s, { simp only [C, prod_zero_index]; exact (mul_one _).symm }, { assume n e s hns he ih, rw [monomial_single_add, ih, prod_add_index, prod_single_index, mul_left_comm], { simp only [pow_zero], }, { intro a, simp only [pow_zero], }, { intros, rw pow_add, }, } end @[recursor 5] lemma induction_on {M : mv_polynomial σ α → Prop} (p : mv_polynomial σ α) (h_C : ∀a, M (C a)) (h_add : ∀p q, M p → M q → M (p + q)) (h_X : ∀p n, M p → M (p * X n)) : M p := have ∀s a, M (monomial s a), begin assume s a, apply @finsupp.induction σ ℕ _ _ s, { show M (monomial 0 a), from h_C a, }, { assume n e p hpn he ih, have : ∀e:ℕ, M (monomial p a * X n ^ e), { intro e, induction e, { simp [ih] }, { simp [ih, pow_succ', (mul_assoc _ _ _).symm, h_X, e_ih] } }, simp [add_comm, monomial_add_single, this] } end, finsupp.induction p (by have : M (C 0) := h_C 0; rwa [C_0] at this) (assume s a p hsp ha hp, h_add _ _ (this s a) hp) theorem induction_on' {P : mv_polynomial σ α → Prop} (p : mv_polynomial σ α) (h1 : ∀ (u : σ →₀ ℕ) (a : α), P (monomial u a)) (h2 : ∀ (p q : mv_polynomial σ α), P p → P q → P (p + q)) : P p := finsupp.induction p (suffices P (monomial 0 0), by rwa monomial_zero at this, show P (monomial 0 0), from h1 0 0) (λ a b f ha hb hPf, h2 _ _ (h1 _ _) hPf) lemma hom_eq_hom [semiring γ] (f g : mv_polynomial σ α →+* γ) (hC : ∀a:α, f (C a) = g (C a)) (hX : ∀n:σ, f (X n) = g (X n)) (p : mv_polynomial σ α) : f p = g p := mv_polynomial.induction_on p hC begin assume p q hp hq, rw [is_semiring_hom.map_add f, is_semiring_hom.map_add g, hp, hq] end begin assume p n hp, rw [is_semiring_hom.map_mul f, is_semiring_hom.map_mul g, hp, hX] end lemma is_id (f : mv_polynomial σ α →+* mv_polynomial σ α) (hC : ∀a:α, f (C a) = (C a)) (hX : ∀n:σ, f (X n) = (X n)) (p : mv_polynomial σ α) : f p = p := hom_eq_hom f (ring_hom.id _) hC hX p lemma ring_hom_ext {A : Type*} [comm_semiring A] (f g : mv_polynomial σ α →+* A) (hC : ∀ r, f (C r) = g (C r)) (hX : ∀ i, f (X i) = g (X i)) : f = g := begin ext p : 1, apply mv_polynomial.induction_on' p, { intros m r, rw [monomial_eq, finsupp.prod], simp only [monomial_eq, ring_hom.map_mul, ring_hom.map_prod, ring_hom.map_pow, hC, hX], }, { intros p q hp hq, simp only [ring_hom.map_add, hp, hq] } end lemma alg_hom_ext {A : Type*} [comm_semiring A] [algebra α A] (f g : mv_polynomial σ α →ₐ[α] A) (hf : ∀ i : σ, f (X i) = g (X i)) : f = g := begin apply alg_hom.coe_ring_hom_injective, apply ring_hom_ext, { intro r, calc f (C r) = algebra_map α A r : f.commutes r ... = g (C r) : (g.commutes r).symm }, { simpa only [hf] }, end @[simp] lemma alg_hom_C (f : mv_polynomial σ α →ₐ[α] mv_polynomial σ α) (r : α) : f (C r) = C r := f.commutes r section coeff section -- While setting up `coeff`, we make `mv_polynomial` reducible so we can treat it as a function. local attribute [reducible] mv_polynomial /-- The coefficient of the monomial `m` in the multi-variable polynomial `p`. -/ def coeff (m : σ →₀ ℕ) (p : mv_polynomial σ α) : α := p m end lemma ext (p q : mv_polynomial σ α) : (∀ m, coeff m p = coeff m q) → p = q := ext lemma ext_iff (p q : mv_polynomial σ α) : p = q ↔ (∀ m, coeff m p = coeff m q) := ⟨ λ h m, by rw h, ext p q⟩ @[simp] lemma coeff_add (m : σ →₀ ℕ) (p q : mv_polynomial σ α) : coeff m (p + q) = coeff m p + coeff m q := add_apply @[simp] lemma coeff_zero (m : σ →₀ ℕ) : coeff m (0 : mv_polynomial σ α) = 0 := rfl @[simp] lemma coeff_zero_X (i : σ) : coeff 0 (X i : mv_polynomial σ α) = 0 := single_eq_of_ne (λ h, by cases single_eq_zero.1 h) instance coeff.is_add_monoid_hom (m : σ →₀ ℕ) : is_add_monoid_hom (coeff m : mv_polynomial σ α → α) := { map_add := coeff_add m, map_zero := coeff_zero m } lemma coeff_sum {X : Type*} (s : finset X) (f : X → mv_polynomial σ α) (m : σ →₀ ℕ) : coeff m (∑ x in s, f x) = ∑ x in s, coeff m (f x) := (s.sum_hom _).symm lemma monic_monomial_eq (m) : monomial m (1:α) = (m.prod $ λn e, X n ^ e : mv_polynomial σ α) := by simp [monomial_eq] @[simp] lemma coeff_monomial (m n) (a) : coeff m (monomial n a : mv_polynomial σ α) = if n = m then a else 0 := by convert single_apply @[simp] lemma coeff_C (m) (a) : coeff m (C a : mv_polynomial σ α) = if 0 = m then a else 0 := by convert single_apply lemma coeff_X_pow (i : σ) (m) (k : ℕ) : coeff m (X i ^ k : mv_polynomial σ α) = if single i k = m then 1 else 0 := begin have := coeff_monomial m (finsupp.single i k) (1:α), rwa [@monomial_eq _ _ (1:α) (finsupp.single i k) _, C_1, one_mul, finsupp.prod_single_index] at this, exact pow_zero _ end lemma coeff_X' (i : σ) (m) : coeff m (X i : mv_polynomial σ α) = if single i 1 = m then 1 else 0 := by rw [← coeff_X_pow, pow_one] @[simp] lemma coeff_X (i : σ) : coeff (single i 1) (X i : mv_polynomial σ α) = 1 := by rw [coeff_X', if_pos rfl] @[simp] lemma coeff_C_mul (m) (a : α) (p : mv_polynomial σ α) : coeff m (C a * p) = a * coeff m p := begin rw [mul_def], simp only [C, monomial], dsimp, rw [monomial], rw sum_single_index, { simp only [zero_add], convert sum_apply, simp only [single_apply, finsupp.sum], rw finset.sum_eq_single m, { rw if_pos rfl, refl }, { intros m' hm' H, apply if_neg, exact H }, { intros hm, rw if_pos rfl, rw not_mem_support_iff at hm, simp [hm] } }, simp only [zero_mul, single_zero, zero_add], exact sum_zero, -- TODO doesn't work if we put this inside the simp end lemma coeff_mul (p q : mv_polynomial σ α) (n : σ →₀ ℕ) : coeff n (p * q) = ∑ x in (antidiagonal n).support, coeff x.1 p * coeff x.2 q := begin rw mul_def, have := @finset.sum_sigma (σ →₀ ℕ) α _ _ p.support (λ _, q.support) (λ x, if (x.1 + x.2 = n) then coeff x.1 p * coeff x.2 q else 0), convert this.symm using 1; clear this, { rw [coeff], repeat {rw sum_apply, apply finset.sum_congr rfl, intros, dsimp only}, convert single_apply }, { have : (antidiagonal n).support.filter (λ x, x.1 ∈ p.support ∧ x.2 ∈ q.support) ⊆ (antidiagonal n).support := finset.filter_subset _, rw [← finset.sum_sdiff this, finset.sum_eq_zero, zero_add], swap, { intros x hx, rw [finset.mem_sdiff, not_iff_not_of_iff (finset.mem_filter), not_and, not_and, not_mem_support_iff] at hx, by_cases H : x.1 ∈ p.support, { rw [coeff, coeff, hx.2 hx.1 H, mul_zero] }, { rw not_mem_support_iff at H, rw [coeff, H, zero_mul] } }, symmetry, rw [← finset.sum_sdiff (finset.filter_subset _), finset.sum_eq_zero, zero_add], swap, { intros x hx, rw [finset.mem_sdiff, not_iff_not_of_iff (finset.mem_filter), not_and] at hx, simp only [if_neg (hx.2 hx.1)] }, { apply finset.sum_bij, swap 5, { intros x hx, exact (x.1, x.2) }, { intros x hx, rw [finset.mem_filter, finset.mem_sigma] at hx, simpa [finset.mem_filter, mem_antidiagonal_support] using hx.symm }, { intros x hx, rw finset.mem_filter at hx, simp only [if_pos hx.2], }, { rintros ⟨i,j⟩ ⟨k,l⟩ hij hkl, simpa using and.intro }, { rintros ⟨i,j⟩ hij, refine ⟨⟨i,j⟩, _, _⟩, { apply_instance }, { rw [finset.mem_filter, mem_antidiagonal_support] at hij, simpa [finset.mem_filter, finset.mem_sigma] using hij.symm }, { refl } } }, all_goals { apply_instance } } end @[simp] lemma coeff_mul_X (m) (s : σ) (p : mv_polynomial σ α) : coeff (m + single s 1) (p * X s) = coeff m p := begin have : (m, single s 1) ∈ (m + single s 1).antidiagonal.support := mem_antidiagonal_support.2 rfl, rw [coeff_mul, ← finset.insert_erase this, finset.sum_insert (finset.not_mem_erase _ _), finset.sum_eq_zero, add_zero, coeff_X, mul_one], rintros ⟨i,j⟩ hij, rw [finset.mem_erase, mem_antidiagonal_support] at hij, by_cases H : single s 1 = j, { subst j, simpa using hij }, { rw [coeff_X', if_neg H, mul_zero] }, end lemma coeff_mul_X' (m) (s : σ) (p : mv_polynomial σ α) : coeff m (p * X s) = if s ∈ m.support then coeff (m - single s 1) p else 0 := begin split_ifs with h h, { conv_rhs {rw ← coeff_mul_X _ s}, congr' with t, by_cases hj : s = t, { subst t, simp only [nat_sub_apply, add_apply, single_eq_same], refine (nat.sub_add_cancel $ nat.pos_of_ne_zero _).symm, rwa mem_support_iff at h }, { simp [single_eq_of_ne hj] } }, { delta coeff, rw ← not_mem_support_iff, intro hm, apply h, have H := support_mul _ _ hm, simp only [finset.mem_bind] at H, rcases H with ⟨j, hj, i', hi', H⟩, delta X monomial at hi', rw mem_support_single at hi', cases hi', subst i', erw finset.mem_singleton at H, subst m, rw [mem_support_iff, add_apply, single_apply, if_pos rfl], intro H, rw [_root_.add_eq_zero_iff] at H, exact one_ne_zero H.2 } end lemma eq_zero_iff {p : mv_polynomial σ α} : p = 0 ↔ ∀ d, coeff d p = 0 := by { rw ext_iff, simp only [coeff_zero], } lemma ne_zero_iff {p : mv_polynomial σ α} : p ≠ 0 ↔ ∃ d, coeff d p ≠ 0 := by { rw [ne.def, eq_zero_iff], push_neg, } lemma exists_coeff_ne_zero {p : mv_polynomial σ α} (h : p ≠ 0) : ∃ d, coeff d p ≠ 0 := ne_zero_iff.mp h end coeff section constant_coeff /-- `constant_coeff p` returns the constant term of the polynomial `p`, defined as `coeff 0 p`. This is a ring homomorphism. -/ def constant_coeff : mv_polynomial σ α →+* α := { to_fun := coeff 0, map_one' := by simp [coeff, add_monoid_algebra.one_def], map_mul' := by simp [coeff_mul, finsupp.support_single_ne_zero], map_zero' := coeff_zero _, map_add' := coeff_add _ } lemma constant_coeff_eq : (constant_coeff : mv_polynomial σ α → α) = coeff 0 := rfl @[simp] lemma constant_coeff_C (r : α) : constant_coeff (C r : mv_polynomial σ α) = r := by simp [constant_coeff_eq] @[simp] lemma constant_coeff_X (i : σ) : constant_coeff (X i : mv_polynomial σ α) = 0 := by simp [constant_coeff_eq] lemma constant_coeff_monomial (d : σ →₀ ℕ) (r : α) : constant_coeff (monomial d r) = if d = 0 then r else 0 := by rw [constant_coeff_eq, coeff_monomial] end constant_coeff section as_sum @[simp] lemma support_sum_monomial_coeff (p : mv_polynomial σ α) : ∑ v in p.support, monomial v (coeff v p) = p := finsupp.sum_single p lemma as_sum (p : mv_polynomial σ α) : p = ∑ v in p.support, monomial v (coeff v p) := (support_sum_monomial_coeff p).symm end as_sum section eval₂ variables [comm_semiring β] variables (f : α →+* β) (g : σ → β) /-- Evaluate a polynomial `p` given a valuation `g` of all the variables and a ring hom `f` from the scalar ring to the target -/ def eval₂ (p : mv_polynomial σ α) : β := p.sum (λs a, f a * s.prod (λn e, g n ^ e)) lemma eval₂_eq (g : α →+* β) (x : σ → β) (f : mv_polynomial σ α) : f.eval₂ g x = ∑ d in f.support, g (f.coeff d) * ∏ i in d.support, x i ^ d i := rfl lemma eval₂_eq' [fintype σ] (g : α →+* β) (x : σ → β) (f : mv_polynomial σ α) : f.eval₂ g x = ∑ d in f.support, g (f.coeff d) * ∏ i, x i ^ d i := by { simp only [eval₂_eq, ← finsupp.prod_pow], refl } @[simp] lemma eval₂_zero : (0 : mv_polynomial σ α).eval₂ f g = 0 := finsupp.sum_zero_index section @[simp] lemma eval₂_add : (p + q).eval₂ f g = p.eval₂ f g + q.eval₂ f g := finsupp.sum_add_index (by simp [is_semiring_hom.map_zero f]) (by simp [add_mul, is_semiring_hom.map_add f]) @[simp] lemma eval₂_monomial : (monomial s a).eval₂ f g = f a * s.prod (λn e, g n ^ e) := finsupp.sum_single_index (by simp [is_semiring_hom.map_zero f]) @[simp] lemma eval₂_C (a) : (C a).eval₂ f g = f a := by simp [eval₂_monomial, C, prod_zero_index] @[simp] lemma eval₂_one : (1 : mv_polynomial σ α).eval₂ f g = 1 := (eval₂_C _ _ _).trans (is_semiring_hom.map_one f) @[simp] lemma eval₂_X (n) : (X n).eval₂ f g = g n := by simp [eval₂_monomial, is_semiring_hom.map_one f, X, prod_single_index, pow_one] lemma eval₂_mul_monomial : ∀{s a}, (p * monomial s a).eval₂ f g = p.eval₂ f g * f a * s.prod (λn e, g n ^ e) := begin apply mv_polynomial.induction_on p, { assume a' s a, simp [C_mul_monomial, eval₂_monomial, is_semiring_hom.map_mul f] }, { assume p q ih_p ih_q, simp [add_mul, eval₂_add, ih_p, ih_q] }, { assume p n ih s a, from calc (p * X n * monomial s a).eval₂ f g = (p * monomial (single n 1 + s) a).eval₂ f g : by simp [monomial_single_add, -add_comm, pow_one, mul_assoc] ... = (p * monomial (single n 1) 1).eval₂ f g * f a * s.prod (λn e, g n ^ e) : by simp [ih, prod_single_index, prod_add_index, pow_one, pow_add, mul_assoc, mul_left_comm, is_semiring_hom.map_one f, -add_comm] } end @[simp] lemma eval₂_mul : ∀{p}, (p * q).eval₂ f g = p.eval₂ f g * q.eval₂ f g := begin apply mv_polynomial.induction_on q, { simp [C, eval₂_monomial, eval₂_mul_monomial, prod_zero_index] }, { simp [mul_add, eval₂_add] {contextual := tt} }, { simp [X, eval₂_monomial, eval₂_mul_monomial, (mul_assoc _ _ _).symm] { contextual := tt} } end @[simp] lemma eval₂_pow {p:mv_polynomial σ α} : ∀{n:ℕ}, (p ^ n).eval₂ f g = (p.eval₂ f g)^n | 0 := eval₂_one _ _ | (n + 1) := by rw [pow_add, pow_one, pow_add, pow_one, eval₂_mul, eval₂_pow] instance eval₂.is_semiring_hom : is_semiring_hom (eval₂ f g) := { map_zero := eval₂_zero _ _, map_one := eval₂_one _ _, map_add := λ p q, eval₂_add _ _, map_mul := λ p q, eval₂_mul _ _ } /-- `mv_polynomial.eval₂` as a `ring_hom`. -/ def eval₂_hom (f : α →+* β) (g : σ → β) : mv_polynomial σ α →+* β := ring_hom.of (eval₂ f g) @[simp] lemma coe_eval₂_hom (f : α →+* β) (g : σ → β) : ⇑(eval₂_hom f g) = eval₂ f g := rfl lemma eval₂_hom_congr {f₁ f₂ : α →+* β} {g₁ g₂ : σ → β} {p₁ p₂ : mv_polynomial σ α} : f₁ = f₂ → g₁ = g₂ → p₁ = p₂ → eval₂_hom f₁ g₁ p₁ = eval₂_hom f₂ g₂ p₂ := by rintros rfl rfl rfl; refl end @[simp] lemma eval₂_hom_C (f : α →+* β) (g : σ → β) (r : α) : eval₂_hom f g (C r) = f r := eval₂_C f g r @[simp] lemma eval₂_hom_X' (f : α →+* β) (g : σ → β) (i : σ) : eval₂_hom f g (X i) = g i := eval₂_X f g i @[simp] lemma comp_eval₂_hom [comm_semiring γ] (f : α →+* β) (g : σ → β) (φ : β →+* γ) : φ.comp (eval₂_hom f g) = (eval₂_hom (φ.comp f) (λ i, φ (g i))) := begin apply mv_polynomial.ring_hom_ext, { intro r, rw [ring_hom.comp_apply, eval₂_hom_C, eval₂_hom_C, ring_hom.comp_apply] }, { intro i, rw [ring_hom.comp_apply, eval₂_hom_X', eval₂_hom_X'] } end lemma map_eval₂_hom [comm_semiring γ] (f : α →+* β) (g : σ → β) (φ : β →+* γ) (p : mv_polynomial σ α) : φ (eval₂_hom f g p) = (eval₂_hom (φ.comp f) (λ i, φ (g i)) p) := by { rw ← comp_eval₂_hom, refl } lemma eval₂_hom_monomial (f : α →+* β) (g : σ → β) (d : σ →₀ ℕ) (r : α) : eval₂_hom f g (monomial d r) = f r * d.prod (λ i k, g i ^ k) := by simp only [monomial_eq, ring_hom.map_mul, eval₂_hom_C, finsupp.prod, ring_hom.map_prod, ring_hom.map_pow, eval₂_hom_X'] section local attribute [instance, priority 10] is_semiring_hom.comp lemma eval₂_comp_left {γ} [comm_semiring γ] (k : β →+* γ) (f : α →+* β) (g : σ → β) (p) : k (eval₂ f g p) = eval₂ (k.comp f) (k ∘ g) p := by apply mv_polynomial.induction_on p; simp [ eval₂_add, k.map_add, eval₂_mul, k.map_mul] {contextual := tt} end @[simp] lemma eval₂_eta (p : mv_polynomial σ α) : eval₂ C X p = p := by apply mv_polynomial.induction_on p; simp [eval₂_add, eval₂_mul] {contextual := tt} lemma eval₂_congr (g₁ g₂ : σ → β) (h : ∀ {i : σ} {c : σ →₀ ℕ}, i ∈ c.support → coeff c p ≠ 0 → g₁ i = g₂ i) : p.eval₂ f g₁ = p.eval₂ f g₂ := begin apply finset.sum_congr rfl, intros c hc, dsimp, congr' 1, apply finset.prod_congr rfl, intros i hi, dsimp, congr' 1, apply h hi, rwa finsupp.mem_support_iff at hc end @[simp] lemma eval₂_prod (s : finset γ) (p : γ → mv_polynomial σ α) : eval₂ f g (∏ x in s, p x) = ∏ x in s, eval₂ f g (p x) := (s.prod_hom _).symm @[simp] lemma eval₂_sum (s : finset γ) (p : γ → mv_polynomial σ α) : eval₂ f g (∑ x in s, p x) = ∑ x in s, eval₂ f g (p x) := (s.sum_hom _).symm attribute [to_additive] eval₂_prod lemma eval₂_assoc (q : γ → mv_polynomial σ α) (p : mv_polynomial γ α) : eval₂ f (λ t, eval₂ f g (q t)) p = eval₂ f g (eval₂ C q p) := begin show _ = eval₂_hom f g (eval₂ C q p), rw eval₂_comp_left (eval₂_hom f g), congr' with a, simp, end end eval₂ section eval variables {f : σ → α} /-- Evaluate a polynomial `p` given a valuation `f` of all the variables -/ def eval (f : σ → α) : mv_polynomial σ α →+* α := eval₂_hom (ring_hom.id _) f lemma eval_eq (x : σ → α) (f : mv_polynomial σ α) : eval x f = ∑ d in f.support, f.coeff d * ∏ i in d.support, x i ^ d i := rfl lemma eval_eq' [fintype σ] (x : σ → α) (f : mv_polynomial σ α) : eval x f = ∑ d in f.support, f.coeff d * ∏ i, x i ^ d i := eval₂_eq' (ring_hom.id α) x f lemma eval_monomial : eval f (monomial s a) = a * s.prod (λn e, f n ^ e) := eval₂_monomial _ _ @[simp] lemma eval_C : ∀ a, eval f (C a) = a := eval₂_C _ _ @[simp] lemma eval_X : ∀ n, eval f (X n) = f n := eval₂_X _ _ @[simp] lemma smul_eval (x) (p : mv_polynomial σ α) (s) : eval x (s • p) = s * eval x p := by rw [smul_eq_C_mul, (eval x).map_mul, eval_C] lemma eval_sum {ι : Type*} (s : finset ι) (f : ι → mv_polynomial σ α) (g : σ → α) : eval g (∑ i in s, f i) = ∑ i in s, eval g (f i) := (eval g).map_sum _ _ @[to_additive] lemma eval_prod {ι : Type*} (s : finset ι) (f : ι → mv_polynomial σ α) (g : σ → α) : eval g (∏ i in s, f i) = ∏ i in s, eval g (f i) := (eval g).map_prod _ _ theorem eval_assoc {τ} (f : σ → mv_polynomial τ α) (g : τ → α) (p : mv_polynomial σ α) : eval (eval g ∘ f) p = eval g (eval₂ C f p) := begin rw eval₂_comp_left (eval g), unfold eval, simp only [coe_eval₂_hom], congr' with a, simp end end eval section map variables [comm_semiring β] variables (f : α →+* β) /-- `map f p` maps a polynomial `p` across a ring hom `f` -/ def map : mv_polynomial σ α →+* mv_polynomial σ β := eval₂_hom (C.comp f) X @[simp] theorem map_monomial (s : σ →₀ ℕ) (a : α) : map f (monomial s a) = monomial s (f a) := (eval₂_monomial _ _).trans monomial_eq.symm @[simp] theorem map_C : ∀ (a : α), map f (C a : mv_polynomial σ α) = C (f a) := map_monomial _ _ @[simp] theorem map_X : ∀ (n : σ), map f (X n : mv_polynomial σ α) = X n := eval₂_X _ _ theorem map_id : ∀ (p : mv_polynomial σ α), map (ring_hom.id α) p = p := eval₂_eta theorem map_map [comm_semiring γ] (g : β →+* γ) (p : mv_polynomial σ α) : map g (map f p) = map (g.comp f) p := (eval₂_comp_left (map g) (C.comp f) X p).trans $ begin congr, { ext1 a, simp only [map_C, comp_app, ring_hom.coe_comp], }, { ext1 n, simp only [map_X, comp_app], } end theorem eval₂_eq_eval_map (g : σ → β) (p : mv_polynomial σ α) : p.eval₂ f g = eval g (map f p) := begin unfold map eval, simp only [coe_eval₂_hom], have h := eval₂_comp_left (eval₂_hom _ g), dsimp at h, rw h, congr, { ext1 a, simp only [coe_eval₂_hom, ring_hom.id_apply, comp_app, eval₂_C, ring_hom.coe_comp], }, { ext1 n, simp only [comp_app, eval₂_X], }, end lemma eval₂_comp_right {γ} [comm_semiring γ] (k : β →+* γ) (f : α →+* β) (g : σ → β) (p) : k (eval₂ f g p) = eval₂ k (k ∘ g) (map f p) := begin apply mv_polynomial.induction_on p, { intro r, rw [eval₂_C, map_C, eval₂_C] }, { intros p q hp hq, rw [eval₂_add, k.map_add, (map f).map_add, eval₂_add, hp, hq] }, { intros p s hp, rw [eval₂_mul, k.map_mul, (map f).map_mul, eval₂_mul, map_X, hp, eval₂_X, eval₂_X] } end lemma map_eval₂ (f : α →+* β) (g : γ → mv_polynomial δ α) (p : mv_polynomial γ α) : map f (eval₂ C g p) = eval₂ C (map f ∘ g) (map f p) := begin apply mv_polynomial.induction_on p, { intro r, rw [eval₂_C, map_C, map_C, eval₂_C] }, { intros p q hp hq, rw [eval₂_add, (map f).map_add, hp, hq, (map f).map_add, eval₂_add] }, { intros p s hp, rw [eval₂_mul, (map f).map_mul, hp, (map f).map_mul, map_X, eval₂_mul, eval₂_X, eval₂_X] } end lemma coeff_map (p : mv_polynomial σ α) : ∀ (m : σ →₀ ℕ), coeff m (map f p) = f (coeff m p) := begin apply mv_polynomial.induction_on p; clear p, { intros r m, rw [map_C], simp only [coeff_C], split_ifs, {refl}, rw f.map_zero }, { intros p q hp hq m, simp only [hp, hq, (map f).map_add, coeff_add], rw f.map_add }, { intros p i hp m, simp only [hp, (map f).map_mul, map_X], simp only [hp, mem_support_iff, coeff_mul_X'], split_ifs, {refl}, rw is_semiring_hom.map_zero f } end lemma map_injective (hf : function.injective f) : function.injective (map f : mv_polynomial σ α → mv_polynomial σ β) := begin intros p q h, simp only [ext_iff, coeff_map] at h ⊢, intro m, exact hf (h m), end @[simp] lemma eval_map (f : α →+* β) (g : σ → β) (p : mv_polynomial σ α) : eval g (map f p) = eval₂ f g p := by { apply mv_polynomial.induction_on p; { simp { contextual := tt } } } @[simp] lemma eval₂_map [comm_semiring γ] (f : α →+* β) (g : σ → γ) (φ : β →+* γ) (p : mv_polynomial σ α) : eval₂ φ g (map f p) = eval₂ (φ.comp f) g p := by { rw [← eval_map, ← eval_map, map_map], } @[simp] lemma eval₂_hom_map_hom [comm_semiring γ] (f : α →+* β) (g : σ → γ) (φ : β →+* γ) (p : mv_polynomial σ α) : eval₂_hom φ g (map f p) = eval₂_hom (φ.comp f) g p := eval₂_map f g φ p @[simp] lemma constant_coeff_map (f : α →+* β) (φ : mv_polynomial σ α) : constant_coeff (mv_polynomial.map f φ) = f (constant_coeff φ) := coeff_map f φ 0 lemma constant_coeff_comp_map (f : α →+* β) : (constant_coeff : mv_polynomial σ β →+* β).comp (mv_polynomial.map f) = f.comp (constant_coeff) := by { ext, apply constant_coeff_map } lemma support_map_subset (p : mv_polynomial σ α) : (map f p).support ⊆ p.support := begin intro x, simp only [finsupp.mem_support_iff], contrapose!, change p.coeff x = 0 → (map f p).coeff x = 0, rw coeff_map, intro hx, rw hx, exact ring_hom.map_zero f end lemma support_map_of_injective (p : mv_polynomial σ α) {f : α →+* β} (hf : injective f) : (map f p).support = p.support := begin apply finset.subset.antisymm, { exact mv_polynomial.support_map_subset _ _ }, intros x hx, rw finsupp.mem_support_iff, contrapose! hx, simp only [not_not, finsupp.mem_support_iff], change (map f p).coeff x = 0 at hx, rw [coeff_map, ← f.map_zero] at hx, exact hf hx end end map section aeval /-! ### The algebra of multivariate polynomials -/ variables {R : Type u} {A : Type v} {S : Type w} (f : σ → A) variables [comm_semiring R] [comm_semiring A] [algebra R A] [comm_semiring S] /-- A map `σ → A` where `A` is an algebra over `R` generates an `R`-algebra homomorphism from multivariate polynomials over `σ` to `A`. -/ def aeval : mv_polynomial σ R →ₐ[R] A := { commutes' := λ r, eval₂_C _ _ _ .. eval₂_hom (algebra_map R A) f } theorem aeval_def (p : mv_polynomial σ R) : aeval f p = eval₂ (algebra_map R A) f p := rfl lemma aeval_eq_eval₂_hom (p : mv_polynomial σ R) : aeval f p = eval₂_hom (algebra_map R A) f p := rfl @[simp] lemma aeval_X (s : σ) : aeval f (X s : mv_polynomial _ R) = f s := eval₂_X _ _ _ @[simp] lemma aeval_C (r : R) : aeval f (C r) = algebra_map R A r := eval₂_C _ _ _ theorem eval_unique (φ : mv_polynomial σ R →ₐ[R] A) : φ = aeval (φ ∘ X) := begin ext p, apply mv_polynomial.induction_on p, { intro r, rw aeval_C, exact φ.commutes r }, { intros f g ih1 ih2, rw [φ.map_add, ih1, ih2, alg_hom.map_add] }, { intros p j ih, rw [φ.map_mul, alg_hom.map_mul, aeval_X, ih] } end lemma comp_aeval {B : Type*} [comm_semiring B] [algebra R B] (φ : A →ₐ[R] B) : φ.comp (aeval f) = (aeval (λ i, φ (f i))) := begin apply mv_polynomial.alg_hom_ext, intros i, rw [alg_hom.comp_apply, aeval_X, aeval_X], end @[simp] lemma map_aeval {B : Type*} [comm_semiring B] (g : σ → A) (φ : A →+* B) (p : mv_polynomial σ R) : φ (aeval g p) = (eval₂_hom (φ.comp (algebra_map R A)) (λ i, φ (g i)) p) := by { rw ← comp_eval₂_hom, refl } @[simp] lemma aeval_zero (p : mv_polynomial σ R) : aeval (0 : σ → A) p = algebra_map _ _ (constant_coeff p) := begin apply mv_polynomial.induction_on p, { simp only [aeval_C, forall_const, if_true, constant_coeff_C, eq_self_iff_true] }, { intros, simp only [*, alg_hom.map_add, ring_hom.map_add, coeff_add] }, { intros, simp only [ring_hom.map_mul, constant_coeff_X, pi.zero_apply, ring_hom.map_zero, eq_self_iff_true, mem_support_iff, not_true, aeval_X, if_false, ne.def, mul_zero, alg_hom.map_mul, zero_apply] } end @[simp] lemma aeval_zero' (p : mv_polynomial σ R) : aeval (λ _, 0 : σ → A) p = algebra_map _ _ (constant_coeff p) := aeval_zero p lemma aeval_monomial (g : σ → A) (d : σ →₀ ℕ) (r : R) : aeval g (monomial d r) = algebra_map _ _ r * d.prod (λ i k, g i ^ k) := eval₂_hom_monomial _ _ _ _ lemma eval₂_hom_eq_zero (f : R →+* S) (g : σ → S) (φ : mv_polynomial σ R) (h : ∀ d, φ.coeff d ≠ 0 → ∃ i ∈ d.support, g i = 0) : eval₂_hom f g φ = 0 := begin rw [φ.as_sum, ring_hom.map_sum, finset.sum_eq_zero], intros d hd, obtain ⟨i, hi, hgi⟩ : ∃ i ∈ d.support, g i = 0 := h d (finsupp.mem_support_iff.mp hd), rw [eval₂_hom_monomial, finsupp.prod, finset.prod_eq_zero hi, mul_zero], rw [hgi, zero_pow], rwa [nat.pos_iff_ne_zero, ← finsupp.mem_support_iff] end lemma aeval_eq_zero [algebra R S] (f : σ → S) (φ : mv_polynomial σ R) (h : ∀ d, φ.coeff d ≠ 0 → ∃ i ∈ d.support, f i = 0) : aeval f φ = 0 := eval₂_hom_eq_zero _ _ _ h end aeval end comm_semiring end mv_polynomial
29c34a223eb3c816f8bf4ee31ee520e29416b223
957a80ea22c5abb4f4670b250d55534d9db99108
/tests/lean/io_bug2.lean
ba0860b41566328e76204c92e6e9c1a21eb92772
[ "Apache-2.0" ]
permissive
GaloisInc/lean
aa1e64d604051e602fcf4610061314b9a37ab8cd
f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0
refs/heads/master
1,592,202,909,807
1,504,624,387,000
1,504,624,387,000
75,319,626
2
1
Apache-2.0
1,539,290,164,000
1,480,616,104,000
C++
UTF-8
Lean
false
false
163
lean
import system.io open io variable [io.interface] def main : io unit := do print_ln "t1", (x, y) ← return ((1 : nat), (2 : ℕ)), print_ln "t2" #eval main
84bd33400fe1bd6175f64ded50e2f08e5f59c476
c8b4b578b2fe61d500fbca7480e506f6603ea698
/src/may_assume/lemmas.lean
c40224ed91b8cc2d8ebfa08cba670e6316746d32
[]
no_license
leanprover-community/flt-regular
aa7e564f2679dfd2e86015a5a9674a6e1197f7cc
67fb3e176584bbc03616c221a7be6fa28c5ccd32
refs/heads/master
1,692,188,905,751
1,691,766,312,000
1,691,766,312,000
421,021,216
19
4
null
1,694,532,115,000
1,635,166,136,000
Lean
UTF-8
Lean
false
false
5,036
lean
import flt_three.flt_three import algebra.gcd_monoid.finset import field_theory.finite.basic import algebra.gcd_monoid.div import number_theory.regular_primes open int finset namespace flt_regular namespace may_assume lemma p_ne_three {a b c : ℤ} {n : ℕ} (hprod : a * b * c ≠ 0) (h : a ^ n + b ^ n = c^ n) : n ≠ 3 := begin intro hn, have ha : a ≠ 0 := λ ha, by simpa [ha] using hprod, have hb : b ≠ 0 := λ hb, by simpa [hb] using hprod, have hc : c ≠ 0 := λ hc, by simpa [hc] using hprod, simpa [hn, flt_three ha hb hc] using h end lemma coprime {a b c : ℤ} {n : ℕ} (H : a ^ n + b ^ n = c ^ n) (hprod : a * b * c ≠ 0) : let d := ({a, b, c} : finset ℤ).gcd id in (a / d) ^ n + (b / d) ^ n = (c / d) ^ n ∧ ({a / d, b / d, c / d} : finset ℤ).gcd id = 1 ∧ (a / d) * (b / d) * (c / d) ≠ 0 := begin have ha : a ≠ 0 := λ ha, by simpa [ha] using hprod, have hb : b ≠ 0 := λ hb, by simpa [hb] using hprod, have hc : c ≠ 0 := λ hc, by simpa [hc] using hprod, let s : finset ℤ := {a, b, c}, set d : ℤ := s.gcd id with hddef, have hadiv : d ∣ a := gcd_dvd (by simp), have hbdiv : d ∣ b := gcd_dvd (by simp), have hcdiv : d ∣ c := gcd_dvd (by simp), have hdzero : d ≠ 0 := λ hdzero, by simpa [ha] using finset.gcd_eq_zero_iff.1 hdzero a (by simp), have hdp : d ^ n ≠ 0 := λ hdn, hdzero (pow_eq_zero hdn), refine ⟨_, _, λ habs, _⟩, { obtain ⟨na, hna⟩ := hadiv, obtain ⟨nb, hnb⟩ := hbdiv, obtain ⟨nc, hnc⟩ := hcdiv, rw [← hddef], simpa [hna, hnb, hnc, mul_pow, hdzero, int.add_mul_div_left (d ^ n * na ^ n) (nb ^ n), hdp] using congr_arg (/ d ^ n) H }, { simpa [gcd_eq_gcd_image] using finset.int.gcd_div_id_eq_one (show a ∈ ({a, b, c} : finset ℤ), by simp) ha }, { simp only [mul_eq_zero] at habs, rcases habs with (Ha | Hb) | Hc, { exact ha (int.eq_zero_of_div_eq_zero (by exact gcd_dvd (by simp)) Ha) }, { exact hb (int.eq_zero_of_div_eq_zero (by exact gcd_dvd (by simp)) Hb) }, { exact hc (int.eq_zero_of_div_eq_zero (by exact gcd_dvd (by simp)) Hc) } } end end may_assume lemma p_dvd_c_of_ab_of_anegc {p : ℕ} {a b c : ℤ} (hpri : p.prime) (hp : p ≠ 3) (h : a ^ p + b ^ p = c ^ p) (hab : a ≡ b [ZMOD p]) (hbc : b ≡ -c [ZMOD p]) : ↑p ∣ c := begin letI : fact p.prime := ⟨hpri⟩, replace h := congr_arg (λ (n : ℤ), (n : zmod p)) h, simp only [int.coe_nat_pow, int.cast_add, int.cast_pow, zmod.pow_card] at h, simp only [← zmod.int_coe_eq_int_coe_iff, int.cast_neg] at hbc hab, rw [hab, hbc, ← sub_eq_zero, ← sub_eq_add_neg, ← int.cast_neg, ← int.cast_sub, ← int.cast_sub] at h, ring_nf at h, simp only [int.cast_neg, int.cast_mul, int.cast_bit1, int.cast_one, int.cast_coe_nat, neg_eq_zero, mul_eq_zero] at h, rw [← zmod.int_coe_zmod_eq_zero_iff_dvd], refine or.resolve_left h (λ h3, _), rw [show (3 : zmod p) = ↑(3 : ℕ), by simp, zmod.nat_coe_zmod_eq_zero_iff_dvd, nat.dvd_prime (nat.prime_three)] at h3, cases h3 with H₁ H₂, { exact hpri.ne_one H₁ }, { exact hp H₂ } end lemma a_not_cong_b {p : ℕ} {a b c : ℤ} (hpri : p.prime) (hp5 : 5 ≤ p) (hprod : a * b * c ≠ 0) (h : a ^ p + b ^ p = c ^ p) (hgcd : ({a, b, c} : finset ℤ).gcd id = 1) (caseI : ¬ ↑p ∣ (a * b * c)) : ∃ (x y z : ℤ), x ^ p + y ^ p = z ^ p ∧ ({x, y, z} : finset ℤ).gcd id = 1 ∧ ¬x ≡ y [ZMOD p] ∧ x * y * z ≠ 0 ∧ ¬ ↑p ∣ x * y * z := begin by_cases H : a ≡ b [ZMOD p], swap, { exact ⟨a, b, c, ⟨h, hgcd, H, hprod, caseI⟩⟩ }, refine ⟨a, -c, -b, ⟨_, _, λ habs, _, _, _⟩⟩, { have hpodd : p ≠ 2 := by linarith, simp only [neg_pow, (or.resolve_left hpri.eq_two_or_odd' hpodd).neg_one_pow, neg_one_mul, ← sub_eq_add_neg, sub_eq_iff_eq_add], symmetry, rw [neg_add_eq_iff_eq_add, add_comm], exact h.symm }, { convert hgcd using 1, have : ({a, -c, -b} : finset ℤ) = {a, -b, -c}, { refine finset.ext (λ x, ⟨λ hx, _, λ hx, _⟩); { simp only [mem_insert, mem_singleton] at hx, rcases hx with (H | H | H); simp [H] } }, rw [this], simp only [gcd_insert, id.def, gcd_singleton, normalize_apply, neg_mul], congr' 1, rw [← coe_gcd, ← coe_gcd, int.gcd_eq_nat_abs, int.gcd_eq_nat_abs], simp only [nat_abs_neg, nat.cast_inj], rcases ⟨int.is_unit_iff.1 (norm_unit (-c)).is_unit, int.is_unit_iff.1 (norm_unit c).is_unit⟩ with ⟨H₁ | H₂, H₃ | H₄⟩, { simp [H₁, H₃] }, { simp [H₁, H₄] }, { simp [H₂, H₃] }, { simp [H₂, H₄] } }, { have hp3 : p ≠ 3 := by linarith, rw [← zmod.int_coe_eq_int_coe_iff] at habs H, rw [H] at habs, rw [zmod.int_coe_eq_int_coe_iff] at habs H, obtain ⟨n, hn⟩ := p_dvd_c_of_ab_of_anegc hpri hp3 h H habs, refine caseI ⟨a * b * n, _⟩, rw [hn], ring }, { convert hprod using 1, ring }, { ring_nf at ⊢ caseI, exact caseI } end end flt_regular
240b9d826476911044e71b3a71a3d7de4d3ece15
a156d865507798f08f46a2f6ca204d9727f4734d
/src/group_theory/monster.lean
6037637146285f68ca503ba8bbd48021f14b53e6
[]
no_license
jesse-michael-han/formalabstracts
4e2fc8c107a3388823ffbd1671dd1e54108ea394
63a949de7989f17c791c40e580c3011516af57e0
refs/heads/master
1,625,470,495,408
1,551,315,841,000
1,551,316,547,000
136,272,424
0
0
null
1,528,259,798,000
1,528,259,798,000
null
UTF-8
Lean
false
false
2,164
lean
import ..data.dvector .presentation import tactic.fattribute open category_theory (mk_ob) local notation h :: t := dvector.cons h t local notation `[` l:(foldr `, ` (h t, dvector.cons h t) dvector.nil `]`) := l /- The Monster group -/ /- according to: https://mathoverflow.net/questions/142205/presentation-of-the-monster-group There's a 12-generator 80-relator presentation for the Monster group. Specifically, we have 78 relators for the Coxeter group Y443:12 relators of the form x^2=1 , one for each node in the Coxeter-Dynkin diagram; 11 relators of the form (xy)3=1, one for each pair of adjacent nodes; 55 relators of the form (xy)2=1 (commutators), one for each pair of non-adjacent nodes; together with a single 'spider' relator, (ab₁c₁ab₂c₂ab₃c₃)^10=1 , which results in the group M×C2. We can get rid of the C2 by quotienting out by an eightieth relation, x=1, where x is the unique non-identity element in the centre of the group. -/ noncomputable def Y443 : Group := coxeter_group $ matrix_of_graph (coxeter_edges [4,4,3]) namespace monster open coxeter_vertices /- coxeter_vertices [p,q,r] consists of a left arm of length p, a right arm of length q, and a bottom arm of length r, with one node in the center connecting them. arm i j gets the jth element of the ith arm, where both i and j start indexing from 0. -/ private def a : Y443 := generated_of torso private def b₁ : Y443 := generated_of $ arm (by to_dfin 0) (by to_dfin 0) private def c₁ : Y443 := generated_of $ arm (by to_dfin 0) (by to_dfin 1) private def b₂ : Y443 := generated_of $ arm (by to_dfin 1) (by to_dfin 0) private def c₂ : Y443 := generated_of $ arm (by to_dfin 1) (by to_dfin 1) private def b₃ : Y443 := generated_of $ arm (by to_dfin 2) (by to_dfin 0) private def c₃ : Y443 := generated_of $ arm (by to_dfin 2) (by to_dfin 1) /- (ab₁c₁ab₂c₂ab₃c₃)^10 -/ noncomputable def spider : Y443 := (a * b₁ * c₁ * a * b₂ * c₂ * a * b₃ * c₃)^10 /-- The Fischer-Griess monster group -/ @[fabstract] noncomputable def Monster : Group := mk_ob $ quotient_group.quotient $ is_subgroup.center $ Y443/⟪{spider}⟫ end monster
6da780bb087910b33d0bd3184b09d28e6ed68353
646fc4b41ca4adb82b3f7fbae3ea3c58ff496b4c
/Example/Basic.lean
06b9f77d806e6da6f30ed23619b880f08aa8aaa2
[]
no_license
cpehle/lean4-plugin-example
82e63420463483381c91de0705b5626a336863fa
d32d3b310645cfecf4c33acafe996755f67cf30b
refs/heads/main
1,690,367,192,612
1,631,531,751,000
1,631,531,751,000
405,148,785
2
1
null
null
null
null
UTF-8
Lean
false
false
71
lean
namespace Example @[extern "funA"] constant funA (a : UInt64) : UInt64
542fcdfcf51703f8392da9222899b6e46476a266
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/stage0/src/Init/Lean/Elab/Match.lean
19e5f54bc8ee7cff28ca42f92341fe546f623eea
[ "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
1,433
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 -/ prelude import Init.Lean.Elab.Term namespace Lean namespace Elab namespace Term private def expandSimpleMatch (stx discr lhsVar rhs : Syntax) (expectedType? : Option Expr) : TermElabM Expr := do newStx ← `(let $lhsVar := $discr; $rhs); withMacroExpansion stx newStx $ elabTerm newStx expectedType? private def expandSimpleMatchWithType (stx discr lhsVar type rhs : Syntax) (expectedType? : Option Expr) : TermElabM Expr := do newStx ← `(let $lhsVar : $type := $discr; $rhs); withMacroExpansion stx newStx $ elabTerm newStx expectedType? -- parser! "match " >> sepBy1 termParser ", " >> optType >> " with " >> matchAlts @[builtinTermElab «match»] def elabMatch : TermElab := fun stx expectedType? => match_syntax stx with | `(match $discr:term with $y:ident => $rhs:term) => expandSimpleMatch stx discr y rhs expectedType? | `(match $discr:term with | $y:ident => $rhs:term) => expandSimpleMatch stx discr y rhs expectedType? | `(match $discr:term : $type with $y:ident => $rhs:term) => expandSimpleMatchWithType stx discr y type rhs expectedType? | `(match $discr:term : $type with | $y:ident => $rhs:term) => expandSimpleMatchWithType stx discr y type rhs expectedType? | _ => throwUnsupportedSyntax end Term end Elab end Lean
bc868500e484e9d277fe6b45708566a1d50b63dc
9b9a16fa2cb737daee6b2785474678b6fa91d6d4
/src/logic/embedding.lean
2947b56ec6eb729b766a86b853d8fc9a9f4709c2
[ "Apache-2.0" ]
permissive
johoelzl/mathlib
253f46daa30b644d011e8e119025b01ad69735c4
592e3c7a2dfbd5826919b4605559d35d4d75938f
refs/heads/master
1,625,657,216,488
1,551,374,946,000
1,551,374,946,000
98,915,829
0
0
Apache-2.0
1,522,917,267,000
1,501,524,499,000
Lean
UTF-8
Lean
false
false
5,867
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 Injective functions. -/ import data.equiv.basic data.option.basic universes u v w x namespace function structure embedding (α : Sort*) (β : Sort*) := (to_fun : α → β) (inj : injective to_fun) infixr ` ↪ `:25 := embedding instance {α : Sort u} {β : Sort v} : has_coe_to_fun (α ↪ β) := ⟨_, embedding.to_fun⟩ end function protected def equiv.to_embedding {α : Sort u} {β : Sort v} (f : α ≃ β) : α ↪ β := ⟨f, f.bijective.1⟩ @[simp] theorem equiv.to_embedding_coe_fn {α : Sort u} {β : Sort v} (f : α ≃ β) : (f.to_embedding : α → β) = f := rfl namespace function namespace embedding @[simp] theorem to_fun_eq_coe {α β} (f : α ↪ β) : to_fun f = f := rfl @[simp] theorem coe_fn_mk {α β} (f : α → β) (i) : (@mk _ _ f i : α → β) = f := rfl theorem inj' {α β} : ∀ (f : α ↪ β), injective f | ⟨f, hf⟩ := hf @[refl] protected def refl (α : Sort*) : α ↪ α := ⟨id, injective_id⟩ @[trans] protected def trans {α β γ} (f : α ↪ β) (g : β ↪ γ) : α ↪ γ := ⟨_, injective_comp g.inj' f.inj'⟩ @[simp] theorem refl_apply {α} (x : α) : embedding.refl α x = x := rfl @[simp] theorem trans_apply {α β γ} (f : α ↪ β) (g : β ↪ γ) (a : α) : (f.trans g) a = g (f a) := rfl protected def congr {α : Sort u} {β : Sort v} {γ : Sort w} {δ : Sort x} (e₁ : α ≃ β) (e₂ : γ ≃ δ) (f : α ↪ γ) : (β ↪ δ) := (equiv.to_embedding e₁.symm).trans (f.trans e₂.to_embedding) protected noncomputable def of_surjective {α β} {f : β → α} (hf : surjective f) : α ↪ β := ⟨surj_inv hf, injective_surj_inv _⟩ protected noncomputable def equiv_of_surjective {α β} (f : α ↪ β) (hf : surjective f) : α ≃ β := equiv.of_bijective ⟨f.inj, hf⟩ protected def of_not_nonempty {α β} (hα : ¬ nonempty α) : α ↪ β := ⟨λa, (hα ⟨a⟩).elim, assume a, (hα ⟨a⟩).elim⟩ noncomputable def set_value {α β} (f : α ↪ β) (a : α) (b : β) : α ↪ β := by haveI := classical.dec; exact if h : ∃ a', f a' = b then (equiv.swap a (classical.some h)).to_embedding.trans f else ⟨λ a', if a' = a then b else f a', λ a₁ a₂ e, begin simp at e, split_ifs at e with h₁ h₂, { cc }, { cases h ⟨_, e.symm⟩ }, { cases h ⟨_, e⟩ }, { exact f.2 e } end⟩ theorem set_value_eq {α β} (f : α ↪ β) (a : α) (b : β) : set_value f a b a = b := begin rw [set_value], cases classical.dec (∃ a', f a' = b); dsimp [dite], {simp}, simp [equiv.swap_apply_left], apply classical.some_spec h end /-- Embedding into `option` -/ protected def some {α} : α ↪ option α := ⟨some, option.injective_some α⟩ def subtype {α} (p : α → Prop) : subtype p ↪ α := ⟨subtype.val, λ _ _, subtype.eq'⟩ /-- Restrict the codomain of an embedding. -/ def cod_restrict {α β} (p : set β) (f : α ↪ β) (H : ∀ a, f a ∈ p) : α ↪ p := ⟨λ a, ⟨f a, H a⟩, λ a b h, f.inj (@congr_arg _ _ _ _ subtype.val h)⟩ @[simp] theorem cod_restrict_apply {α β} (p) (f : α ↪ β) (H a) : cod_restrict p f H a = ⟨f a, H a⟩ := rfl def prod_congr {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : α × γ ↪ β × δ := ⟨assume ⟨a, b⟩, (e₁ a, e₂ b), assume ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ h, have a₁ = a₂ ∧ b₁ = b₂, from (prod.mk.inj h).imp (assume h, e₁.inj h) (assume h, e₂.inj h), this.left ▸ this.right ▸ rfl⟩ section sum open sum def sum_congr {α β γ δ : Type*} (e₁ : α ↪ β) (e₂ : γ ↪ δ) : α ⊕ γ ↪ β ⊕ δ := ⟨assume s, match s with inl a := inl (e₁ a) | inr b := inr (e₂ b) end, assume s₁ s₂ h, match s₁, s₂, h with | inl a₁, inl a₂, h := congr_arg inl $ e₁.inj $ inl.inj h | inr b₁, inr b₂, h := congr_arg inr $ e₂.inj $ inr.inj h end⟩ @[simp] theorem sum_congr_apply_inl {α β γ δ} (e₁ : α ↪ β) (e₂ : γ ↪ δ) (a) : sum_congr e₁ e₂ (inl a) = inl (e₁ a) := rfl @[simp] theorem sum_congr_apply_inr {α β γ δ} (e₁ : α ↪ β) (e₂ : γ ↪ δ) (b) : sum_congr e₁ e₂ (inr b) = inr (e₂ b) := rfl end sum section sigma open sigma def sigma_congr_right {α : Type*} {β γ : α → Type*} (e : ∀ a, β a ↪ γ a) : sigma β ↪ sigma γ := ⟨λ ⟨a, b⟩, ⟨a, e a b⟩, λ ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ h, begin injection h with h₁ h₂, subst a₂, congr, exact (e a₁).2 (eq_of_heq h₂) end⟩ end sigma def Pi_congr_right {α : Sort*} {β γ : α → Sort*} (e : ∀ a, β a ↪ γ a) : (Π a, β a) ↪ (Π a, γ a) := ⟨λf a, e a (f a), λ f₁ f₂ h, funext $ λ a, (e a).inj (congr_fun h a)⟩ def arrow_congr_left {α : Sort u} {β : Sort v} {γ : Sort w} (e : α ↪ β) : (γ → α) ↪ (γ → β) := Pi_congr_right (λ _, e) noncomputable def arrow_congr_right {α : Sort u} {β : Sort v} {γ : Sort w} [inhabited γ] (e : α ↪ β) : (α → γ) ↪ (β → γ) := by haveI := classical.prop_decidable; exact let f' : (α → γ) → (β → γ) := λf b, if h : ∃c, e c = b then f (classical.some h) else default γ in ⟨f', assume f₁ f₂ h, funext $ assume c, have ∃c', e c' = e c, from ⟨c, rfl⟩, have eq' : f' f₁ (e c) = f' f₂ (e c), from congr_fun h _, have eq_b : classical.some this = c, from e.inj $ classical.some_spec this, by simp [f', this, if_pos, eq_b] at eq'; assumption⟩ end embedding end function namespace set /-- The injection map is an embedding between subsets. -/ def embedding_of_subset {α} {s t : set α} (h : s ⊆ t) : s ↪ t := ⟨λ x, ⟨x.1, h x.2⟩, λ ⟨x, hx⟩ ⟨y, hy⟩ h, by congr; injection h⟩ end set
c872ee46d1851d0c319b7792361399d9b8206aba
e6b8240a90527fd55d42d0ec6649253d5d0bd414
/src/topology/metric_space/gromov_hausdorff.lean
2eef983445a4ffccb50368e89a4f59fd4adeee76
[ "Apache-2.0" ]
permissive
mattearnshaw/mathlib
ac90f9fb8168aa642223bea3ffd0286b0cfde44f
d8dc1445cf8a8c74f8df60b9f7a1f5cf10946666
refs/heads/master
1,606,308,351,137
1,576,594,130,000
1,576,594,130,000
228,666,195
0
0
Apache-2.0
1,576,603,094,000
1,576,603,093,000
null
UTF-8
Lean
false
false
56,277
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Sébastien Gouëzel -/ import topology.metric_space.closeds set_theory.cardinal topology.metric_space.gromov_hausdorff_realized topology.metric_space.completion /-! # Gromov-Hausdorff distance This file defines the Gromov-Hausdorff distance on the space of nonempty compact metric spaces up to isometry. We introduces the space of all nonempty compact metric spaces, up to isometry, called `GH_space`, and endow it with a metric space structure. The distance, known as the Gromov-Hausdorff distance, is defined as follows: given two nonempty compact spaces `X` and `Y`, their distance is the minimum Hausdorff distance between all possible isometric embeddings of `X` and `Y` in all metric spaces. To define properly the Gromov-Hausdorff space, we consider the non-empty compact subsets of `ℓ^∞(ℝ)` up to isometry, which is a well-defined type, and define the distance as the infimum of the Hausdorff distance over all embeddings in ℓ^∞(ℝ). We prove that this coincides with the previous description, as all separable metric spaces embed isometrically into `ℓ^∞(ℝ)`, through an embedding called the Kuratowski embedding. To prove that we have a distance, we should show that if spaces can be coupled to be arbitrarily close, then they are isometric. More generally, the Gromov-Hausdorff distance is realized, i.e., there is a coupling for which the Hausdorff distance is exactly the Gromov-Hausdorff distance. This follows from a compactness argument, essentially following from Arzela-Ascoli. ## Main results We prove the most important properties of the Gromov-Hausdorff space: it is a polish space, i.e., it is complete and second countable. We also prove the Gromov compactness criterion. -/ noncomputable theory open_locale classical open_locale topological_space universes u v w open classical lattice set function topological_space filter metric quotient open bounded_continuous_function nat Kuratowski_embedding open sum (inl inr) set_option class.instance_max_depth 50 local attribute [instance] metric_space_sum namespace Gromov_Hausdorff section GH_space /- In this section, we define the Gromov-Hausdorff space, denoted `GH_space` as the quotient of nonempty compact subsets of ℓ^∞(ℝ) by identifying isometric sets. Using the Kuratwoski embedding, we get a canonical map `to_GH_space` mapping any nonempty compact type to GH_space. -/ /-- Equivalence relation identifying two nonempty compact sets which are isometric -/ private definition isometry_rel : nonempty_compacts ℓ_infty_ℝ → nonempty_compacts ℓ_infty_ℝ → Prop := λx y, nonempty (x.val ≃ᵢ y.val) /-- This is indeed an equivalence relation -/ private lemma is_equivalence_isometry_rel : equivalence isometry_rel := ⟨λx, ⟨isometric.refl _⟩, λx y ⟨e⟩, ⟨e.symm⟩, λ x y z ⟨e⟩ ⟨f⟩, ⟨e.trans f⟩⟩ /-- setoid instance identifying two isometric nonempty compact subspaces of ℓ^∞(ℝ) -/ instance isometry_rel.setoid : setoid (nonempty_compacts ℓ_infty_ℝ) := setoid.mk isometry_rel is_equivalence_isometry_rel /-- The Gromov-Hausdorff space -/ definition GH_space : Type := quotient (isometry_rel.setoid) /-- Map any nonempty compact type to GH_space -/ definition to_GH_space (α : Type u) [metric_space α] [compact_space α] [nonempty α] : GH_space := ⟦nonempty_compacts.Kuratowski_embedding α⟧ /-- A metric space representative of any abstract point in GH_space -/ definition GH_space.rep (p : GH_space) : Type := (quot.out p).val lemma eq_to_GH_space_iff {α : Type u} [metric_space α] [compact_space α] [nonempty α] {p : nonempty_compacts ℓ_infty_ℝ} : ⟦p⟧ = to_GH_space α ↔ ∃Ψ : α → ℓ_infty_ℝ, isometry Ψ ∧ range Ψ = p.val := begin simp only [to_GH_space, quotient.eq], split, { assume h, rcases setoid.symm h with ⟨e⟩, have f := (Kuratowski_embedding.isometry α).isometric_on_range.trans e, use λx, f x, split, { apply isometry_subtype_val.comp f.isometry }, { rw [range_comp, f.range_coe, set.image_univ, set.range_coe_subtype] } }, { rintros ⟨Ψ, ⟨isomΨ, rangeΨ⟩⟩, have f := ((Kuratowski_embedding.isometry α).isometric_on_range.symm.trans isomΨ.isometric_on_range).symm, have E : (range Ψ ≃ᵢ (nonempty_compacts.Kuratowski_embedding α).val) = (p.val ≃ᵢ range (Kuratowski_embedding α)), by { dunfold nonempty_compacts.Kuratowski_embedding, rw [rangeΨ]; refl }, have g := cast E f, exact ⟨g⟩ } end lemma eq_to_GH_space {p : nonempty_compacts ℓ_infty_ℝ} : ⟦p⟧ = to_GH_space p.val := begin refine eq_to_GH_space_iff.2 ⟨((λx, x) : p.val → ℓ_infty_ℝ), _, subtype.val_range⟩, apply isometry_subtype_val end section local attribute [reducible] GH_space.rep instance rep_GH_space_metric_space {p : GH_space} : metric_space (p.rep) := by apply_instance instance rep_GH_space_compact_space {p : GH_space} : compact_space (p.rep) := by apply_instance instance rep_GH_space_nonempty {p : GH_space} : nonempty (p.rep) := by apply_instance end lemma GH_space.to_GH_space_rep (p : GH_space) : to_GH_space (p.rep) = p := begin change to_GH_space (quot.out p).val = p, rw ← eq_to_GH_space, exact quot.out_eq p end /-- Two nonempty compact spaces have the same image in GH_space if and only if they are isometric -/ lemma to_GH_space_eq_to_GH_space_iff_isometric {α : Type u} [metric_space α] [compact_space α] [nonempty α] {β : Type u} [metric_space β] [compact_space β] [nonempty β] : to_GH_space α = to_GH_space β ↔ nonempty (α ≃ᵢ β) := ⟨begin simp only [to_GH_space, quotient.eq], assume h, rcases h with e, have I : ((nonempty_compacts.Kuratowski_embedding α).val ≃ᵢ (nonempty_compacts.Kuratowski_embedding β).val) = ((range (Kuratowski_embedding α)) ≃ᵢ (range (Kuratowski_embedding β))), by { dunfold nonempty_compacts.Kuratowski_embedding, refl }, have e' := cast I e, have f := (Kuratowski_embedding.isometry α).isometric_on_range, have g := (Kuratowski_embedding.isometry β).isometric_on_range.symm, have h := (f.trans e').trans g, exact ⟨h⟩ end, begin rintros ⟨e⟩, simp only [to_GH_space, quotient.eq], have f := (Kuratowski_embedding.isometry α).isometric_on_range.symm, have g := (Kuratowski_embedding.isometry β).isometric_on_range, have h := (f.trans e).trans g, have I : ((range (Kuratowski_embedding α)) ≃ᵢ (range (Kuratowski_embedding β))) = ((nonempty_compacts.Kuratowski_embedding α).val ≃ᵢ (nonempty_compacts.Kuratowski_embedding β).val), by { dunfold nonempty_compacts.Kuratowski_embedding, refl }, have h' := cast I h, exact ⟨h'⟩ end⟩ /-- Distance on GH_space : the distance between two nonempty compact spaces is the infimum Hausdorff distance between isometric copies of the two spaces in a metric space. For the definition, we only consider embeddings in ℓ^∞(ℝ), but we will prove below that it works for all spaces. -/ instance : has_dist (GH_space) := { dist := λx y, Inf ((λp : nonempty_compacts ℓ_infty_ℝ × nonempty_compacts ℓ_infty_ℝ, Hausdorff_dist p.1.val p.2.val) '' (set.prod {a | ⟦a⟧ = x} {b | ⟦b⟧ = y})) } def GH_dist (α : Type u) (β : Type v) [metric_space α] [nonempty α] [compact_space α] [metric_space β] [nonempty β] [compact_space β] : ℝ := dist (to_GH_space α) (to_GH_space β) lemma dist_GH_dist (p q : GH_space) : dist p q = GH_dist (p.rep) (q.rep) := by rw [GH_dist, p.to_GH_space_rep, q.to_GH_space_rep] /-- The Gromov-Hausdorff distance between two spaces is bounded by the Hausdorff distance of isometric copies of the spaces, in any metric space. -/ theorem GH_dist_le_Hausdorff_dist {α : Type u} [metric_space α] [compact_space α] [nonempty α] {β : Type v} [metric_space β] [compact_space β] [nonempty β] {γ : Type w} [metric_space γ] {Φ : α → γ} {Ψ : β → γ} (ha : isometry Φ) (hb : isometry Ψ) : GH_dist α β ≤ Hausdorff_dist (range Φ) (range Ψ) := begin /- For the proof, we want to embed γ in ℓ^∞(ℝ), to say that the Hausdorff distance is realized in ℓ^∞(ℝ) and therefore bounded below by the Gromov-Hausdorff-distance. However, γ is not separable in general. We restrict to the union of the images of α and β in γ, which is separable and therefore embeddable in ℓ^∞(ℝ). -/ rcases exists_mem_of_nonempty α with ⟨xα, _⟩, letI : inhabited α := ⟨xα⟩, letI : inhabited β := classical.inhabited_of_nonempty (by assumption), let s : set γ := (range Φ) ∪ (range Ψ), let Φ' : α → subtype s := λy, ⟨Φ y, mem_union_left _ (mem_range_self _)⟩, let Ψ' : β → subtype s := λy, ⟨Ψ y, mem_union_right _ (mem_range_self _)⟩, have IΦ' : isometry Φ' := λx y, ha x y, have IΨ' : isometry Ψ' := λx y, hb x y, have : compact s, { apply compact_union_of_compact, { rw ← image_univ, apply compact_image compact_univ ha.continuous }, { rw ← image_univ, apply compact_image compact_univ hb.continuous } }, letI : metric_space (subtype s) := by apply_instance, haveI : compact_space (subtype s) := ⟨compact_iff_compact_univ.1 ‹compact s›⟩, haveI : nonempty (subtype s) := ⟨Φ' xα⟩, have ΦΦ' : Φ = subtype.val ∘ Φ', by { funext, refl }, have ΨΨ' : Ψ = subtype.val ∘ Ψ', by { funext, refl }, have : Hausdorff_dist (range Φ) (range Ψ) = Hausdorff_dist (range Φ') (range Ψ'), { rw [ΦΦ', ΨΨ', range_comp, range_comp], exact Hausdorff_dist_image (isometry_subtype_val) }, rw this, -- Embed s in ℓ^∞(ℝ) through its Kuratowski embedding let F := Kuratowski_embedding (subtype s), have : Hausdorff_dist (F '' (range Φ')) (F '' (range Ψ')) = Hausdorff_dist (range Φ') (range Ψ') := Hausdorff_dist_image (Kuratowski_embedding.isometry _), rw ← this, -- Let A and B be the images of α and β under this embedding. They are in ℓ^∞(ℝ), and -- their Hausdorff distance is the same as in the original space. let A : nonempty_compacts ℓ_infty_ℝ := ⟨F '' (range Φ'), ⟨by simp, begin rw [← range_comp, ← image_univ], exact compact_image compact_univ ((Kuratowski_embedding.isometry _).continuous.comp IΦ'.continuous), end⟩⟩, let B : nonempty_compacts ℓ_infty_ℝ := ⟨F '' (range Ψ'), ⟨by simp, begin rw [← range_comp, ← image_univ], exact compact_image compact_univ ((Kuratowski_embedding.isometry _).continuous.comp IΨ'.continuous), end⟩⟩, have Aα : ⟦A⟧ = to_GH_space α, { rw eq_to_GH_space_iff, exact ⟨λx, F (Φ' x), ⟨(Kuratowski_embedding.isometry _).comp IΦ', by rw range_comp⟩⟩ }, have Bβ : ⟦B⟧ = to_GH_space β, { rw eq_to_GH_space_iff, exact ⟨λx, F (Ψ' x), ⟨(Kuratowski_embedding.isometry _).comp IΨ', by rw range_comp⟩⟩ }, refine cInf_le ⟨0, begin simp [lower_bounds], assume t _ _ _ _ ht, rw ← ht, exact Hausdorff_dist_nonneg end⟩ _, apply (mem_image _ _ _).2, existsi (⟨A, B⟩ : nonempty_compacts ℓ_infty_ℝ × nonempty_compacts ℓ_infty_ℝ), simp [Aα, Bβ] end local attribute [instance, priority 10] inhabited_of_nonempty' /-- The optimal coupling constructed above realizes exactly the Gromov-Hausdorff distance, essentially by design. -/ lemma Hausdorff_dist_optimal {α : Type u} [metric_space α] [compact_space α] [nonempty α] {β : Type v} [metric_space β] [compact_space β] [nonempty β] : Hausdorff_dist (range (optimal_GH_injl α β)) (range (optimal_GH_injr α β)) = GH_dist α β := begin /- we only need to check the inequality ≤, as the other one follows from the previous lemma. As the Gromov-Hausdorff distance is an infimum, we need to check that the Hausdorff distance in the optimal coupling is smaller than the Hausdorff distance of any coupling. First, we check this for couplings which already have small Hausdorff distance: in this case, the induced "distance" on α ⊕ β belongs to the candidates family introduced in the definition of the optimal coupling, and the conclusion follows from the optimality of the optimal coupling within this family. -/ have A : ∀p q : nonempty_compacts (ℓ_infty_ℝ), ⟦p⟧ = to_GH_space α → ⟦q⟧ = to_GH_space β → Hausdorff_dist (p.val) (q.val) < diam (univ : set α) + 1 + diam (univ : set β) → Hausdorff_dist (range (optimal_GH_injl α β)) (range (optimal_GH_injr α β)) ≤ Hausdorff_dist (p.val) (q.val), { assume p q hp hq bound, rcases eq_to_GH_space_iff.1 hp with ⟨Φ, ⟨Φisom, Φrange⟩⟩, rcases eq_to_GH_space_iff.1 hq with ⟨Ψ, ⟨Ψisom, Ψrange⟩⟩, have I : diam (range Φ ∪ range Ψ) ≤ 2 * diam (univ : set α) + 1 + 2 * diam (univ : set β), { rcases exists_mem_of_nonempty α with ⟨xα, _⟩, have : ∃y ∈ range Ψ, dist (Φ xα) y < diam (univ : set α) + 1 + diam (univ : set β), { rw Ψrange, have : Φ xα ∈ p.val := begin rw ← Φrange, exact mem_range_self _ end, exact exists_dist_lt_of_Hausdorff_dist_lt this bound (Hausdorff_edist_ne_top_of_ne_empty_of_bounded p.2.1 q.2.1 (bounded_of_compact p.2.2) (bounded_of_compact q.2.2)) }, rcases this with ⟨y, hy, dy⟩, rcases mem_range.1 hy with ⟨z, hzy⟩, rw ← hzy at dy, have DΦ : diam (range Φ) = diam (univ : set α) := begin rw [← image_univ], apply metric.isometry.diam_image Φisom end, have DΨ : diam (range Ψ) = diam (univ : set β) := begin rw [← image_univ], apply metric.isometry.diam_image Ψisom end, calc diam (range Φ ∪ range Ψ) ≤ diam (range Φ) + dist (Φ xα) (Ψ z) + diam (range Ψ) : diam_union (mem_range_self _) (mem_range_self _) ... ≤ diam (univ : set α) + (diam (univ : set α) + 1 + diam (univ : set β)) + diam (univ : set β) : by { rw [DΦ, DΨ], apply add_le_add (add_le_add (le_refl _) (le_of_lt dy)) (le_refl _) } ... = 2 * diam (univ : set α) + 1 + 2 * diam (univ : set β) : by ring }, let f : α ⊕ β → ℓ_infty_ℝ := λx, match x with | inl y := Φ y | inr z := Ψ z end, let F : (α ⊕ β) × (α ⊕ β) → ℝ := λp, dist (f p.1) (f p.2), -- check that the induced "distance" is a candidate have Fgood : F ∈ candidates α β, { simp only [candidates, forall_const, and_true, add_comm, eq_self_iff_true, dist_eq_zero, and_self, set.mem_set_of_eq], repeat {split}, { exact λx y, calc F (inl x, inl y) = dist (Φ x) (Φ y) : rfl ... = dist x y : Φisom.dist_eq }, { exact λx y, calc F (inr x, inr y) = dist (Ψ x) (Ψ y) : rfl ... = dist x y : Ψisom.dist_eq }, { exact λx y, dist_comm _ _ }, { exact λx y z, dist_triangle _ _ _ }, { exact λx y, calc F (x, y) ≤ diam (range Φ ∪ range Ψ) : begin have A : ∀z : α ⊕ β, f z ∈ range Φ ∪ range Ψ, { assume z, cases z, { apply mem_union_left, apply mem_range_self }, { apply mem_union_right, apply mem_range_self } }, refine dist_le_diam_of_mem _ (A _) (A _), rw [Φrange, Ψrange], exact bounded_of_compact (compact_union_of_compact p.2.2 q.2.2), end ... ≤ 2 * diam (univ : set α) + 1 + 2 * diam (univ : set β) : I } }, let Fb := candidates_b_of_candidates F Fgood, have : Hausdorff_dist (range (optimal_GH_injl α β)) (range (optimal_GH_injr α β)) ≤ HD Fb := Hausdorff_dist_optimal_le_HD _ _ (candidates_b_of_candidates_mem F Fgood), refine le_trans this (le_of_forall_le_of_dense (λr hr, _)), have I1 : ∀x : α, infi (λy:β, Fb (inl x, inr y)) ≤ r, { assume x, have : f (inl x) ∈ p.val, by { rw [← Φrange], apply mem_range_self }, rcases exists_dist_lt_of_Hausdorff_dist_lt this hr (Hausdorff_edist_ne_top_of_ne_empty_of_bounded p.2.1 q.2.1 (bounded_of_compact p.2.2) (bounded_of_compact q.2.2)) with ⟨z, zq, hz⟩, have : z ∈ range Ψ, by rwa [← Ψrange] at zq, rcases mem_range.1 this with ⟨y, hy⟩, calc infi (λy:β, Fb (inl x, inr y)) ≤ Fb (inl x, inr y) : cinfi_le (by simpa using HD_below_aux1 0) ... = dist (Φ x) (Ψ y) : rfl ... = dist (f (inl x)) z : by rw hy ... ≤ r : le_of_lt hz }, have I2 : ∀y : β, infi (λx:α, Fb (inl x, inr y)) ≤ r, { assume y, have : f (inr y) ∈ q.val, by { rw [← Ψrange], apply mem_range_self }, rcases exists_dist_lt_of_Hausdorff_dist_lt' this hr (Hausdorff_edist_ne_top_of_ne_empty_of_bounded p.2.1 q.2.1 (bounded_of_compact p.2.2) (bounded_of_compact q.2.2)) with ⟨z, zq, hz⟩, have : z ∈ range Φ, by rwa [← Φrange] at zq, rcases mem_range.1 this with ⟨x, hx⟩, calc infi (λx:α, Fb (inl x, inr y)) ≤ Fb (inl x, inr y) : cinfi_le (by simpa using HD_below_aux2 0) ... = dist (Φ x) (Ψ y) : rfl ... = dist z (f (inr y)) : by rw hx ... ≤ r : le_of_lt hz }, simp [HD, csupr_le I1, csupr_le I2] }, /- Get the same inequality for any coupling. If the coupling is quite good, the desired inequality has been proved above. If it is bad, then the inequality is obvious. -/ have B : ∀p q : nonempty_compacts (ℓ_infty_ℝ), ⟦p⟧ = to_GH_space α → ⟦q⟧ = to_GH_space β → Hausdorff_dist (range (optimal_GH_injl α β)) (range (optimal_GH_injr α β)) ≤ Hausdorff_dist (p.val) (q.val), { assume p q hp hq, by_cases h : Hausdorff_dist (p.val) (q.val) < diam (univ : set α) + 1 + diam (univ : set β), { exact A p q hp hq h }, { calc Hausdorff_dist (range (optimal_GH_injl α β)) (range (optimal_GH_injr α β)) ≤ HD (candidates_b_dist α β) : Hausdorff_dist_optimal_le_HD _ _ (candidates_b_dist_mem_candidates_b) ... ≤ diam (univ : set α) + 1 + diam (univ : set β) : HD_candidates_b_dist_le ... ≤ Hausdorff_dist (p.val) (q.val) : not_lt.1 h } }, refine le_antisymm _ _, { apply le_cInf, { rw ne_empty_iff_exists_mem, simp only [set.mem_image, nonempty_of_inhabited, set.mem_set_of_eq, prod.exists], existsi [Hausdorff_dist (nonempty_compacts.Kuratowski_embedding α).val (nonempty_compacts.Kuratowski_embedding β).val, nonempty_compacts.Kuratowski_embedding α, nonempty_compacts.Kuratowski_embedding β], simp [to_GH_space, -quotient.eq] }, { rintro b ⟨⟨p, q⟩, ⟨hp, hq⟩, rfl⟩, exact B p q hp hq } }, { exact GH_dist_le_Hausdorff_dist (isometry_optimal_GH_injl α β) (isometry_optimal_GH_injr α β) } end /-- The Gromov-Hausdorff distance can also be realized by a coupling in ℓ^∞(ℝ), by embedding the optimal coupling through its Kuratowski embedding. -/ theorem GH_dist_eq_Hausdorff_dist (α : Type u) [metric_space α] [compact_space α] [nonempty α] (β : Type v) [metric_space β] [compact_space β] [nonempty β] : ∃Φ : α → ℓ_infty_ℝ, ∃Ψ : β → ℓ_infty_ℝ, isometry Φ ∧ isometry Ψ ∧ GH_dist α β = Hausdorff_dist (range Φ) (range Ψ) := begin let F := Kuratowski_embedding (optimal_GH_coupling α β), let Φ := F ∘ optimal_GH_injl α β, let Ψ := F ∘ optimal_GH_injr α β, refine ⟨Φ, Ψ, _, _, _⟩, { exact (Kuratowski_embedding.isometry _).comp (isometry_optimal_GH_injl α β) }, { exact (Kuratowski_embedding.isometry _).comp (isometry_optimal_GH_injr α β) }, { rw [← image_univ, ← image_univ, image_comp F, image_univ, image_comp F (optimal_GH_injr α β), image_univ, ← Hausdorff_dist_optimal], exact (Hausdorff_dist_image (Kuratowski_embedding.isometry _)).symm }, end -- without the next two lines, { exact closed_of_compact (range Φ) hΦ } in the next -- proof is very slow, as the t2_space instance is very hard to find local attribute [instance, priority 10] orderable_topology.t2_space local attribute [instance, priority 10] ordered_topology.to_t2_space /-- The Gromov-Hausdorff distance defines a genuine distance on the Gromov-Hausdorff space. -/ instance GH_space_metric_space : metric_space GH_space := { dist_self := λx, begin rcases exists_rep x with ⟨y, hy⟩, refine le_antisymm _ _, { apply cInf_le, { exact ⟨0, by { rintro b ⟨⟨u, v⟩, ⟨hu, hv⟩, rfl⟩, exact Hausdorff_dist_nonneg } ⟩}, { simp, existsi [y, y], simpa } }, { apply le_cInf, { simp only [set.image_eq_empty, ne.def], apply ne_empty_iff_exists_mem.2, existsi (⟨y, y⟩ : nonempty_compacts ℓ_infty_ℝ × nonempty_compacts ℓ_infty_ℝ), simpa }, { rintro b ⟨⟨u, v⟩, ⟨hu, hv⟩, rfl⟩, exact Hausdorff_dist_nonneg } }, end, dist_comm := λx y, begin have A : (λ (p : nonempty_compacts ℓ_infty_ℝ × nonempty_compacts ℓ_infty_ℝ), Hausdorff_dist ((p.fst).val) ((p.snd).val)) '' (set.prod {a | ⟦a⟧ = x} {b | ⟦b⟧ = y}) = ((λ (p : nonempty_compacts ℓ_infty_ℝ × nonempty_compacts ℓ_infty_ℝ), Hausdorff_dist ((p.fst).val) ((p.snd).val)) ∘ prod.swap) '' (set.prod {a | ⟦a⟧ = x} {b | ⟦b⟧ = y}) := by { congr, funext, simp, rw Hausdorff_dist_comm }, simp only [dist, A, image_comp, prod.swap, image_swap_prod], end, eq_of_dist_eq_zero := λx y hxy, begin /- To show that two spaces at zero distance are isometric, we argue that the distance is realized by some coupling. In this coupling, the two spaces are at zero Hausdorff distance, i.e., they coincide. Therefore, the original spaces are isometric. -/ rcases GH_dist_eq_Hausdorff_dist x.rep y.rep with ⟨Φ, Ψ, Φisom, Ψisom, DΦΨ⟩, rw [← dist_GH_dist, hxy] at DΦΨ, have : range Φ = range Ψ, { have hΦ : compact (range Φ) := by { rw [← image_univ], exact compact_image compact_univ Φisom.continuous }, have hΨ : compact (range Ψ) := by { rw [← image_univ], exact compact_image compact_univ Ψisom.continuous }, apply (Hausdorff_dist_zero_iff_eq_of_closed _ _ _).1 (DΦΨ.symm), { exact closed_of_compact (range Φ) hΦ }, { exact closed_of_compact (range Ψ) hΨ }, { exact Hausdorff_edist_ne_top_of_ne_empty_of_bounded (by simp [-nonempty_subtype]) (by simp [-nonempty_subtype]) (bounded_of_compact hΦ) (bounded_of_compact hΨ) } }, have T : ((range Ψ) ≃ᵢ y.rep) = ((range Φ) ≃ᵢ y.rep), by rw this, have eΨ := cast T Ψisom.isometric_on_range.symm, have e := Φisom.isometric_on_range.trans eΨ, rw [← x.to_GH_space_rep, ← y.to_GH_space_rep, to_GH_space_eq_to_GH_space_iff_isometric], exact ⟨e⟩ end, dist_triangle := λx y z, begin /- To show the triangular inequality between X, Y and Z, realize an optimal coupling between X and Y in a space γ1, and an optimal coupling between Y and Z in a space γ2. Then, glue these metric spaces along Y. We get a new space γ in which X and Y are optimally coupled, as well as Y and Z. Apply the triangle inequality for the Hausdorff distance in γ to conclude. -/ let X := x.rep, let Y := y.rep, let Z := z.rep, let γ1 := optimal_GH_coupling X Y, let γ2 := optimal_GH_coupling Y Z, let Φ : Y → γ1 := optimal_GH_injr X Y, have hΦ : isometry Φ := isometry_optimal_GH_injr X Y, let Ψ : Y → γ2 := optimal_GH_injl Y Z, have hΨ : isometry Ψ := isometry_optimal_GH_injl Y Z, let γ := glue_space hΦ hΨ, letI : metric_space γ := metric.metric_space_glue_space hΦ hΨ, have Comm : (to_glue_l hΦ hΨ) ∘ (optimal_GH_injr X Y) = (to_glue_r hΦ hΨ) ∘ (optimal_GH_injl Y Z) := to_glue_commute hΦ hΨ, calc dist x z = dist (to_GH_space X) (to_GH_space Z) : by rw [x.to_GH_space_rep, z.to_GH_space_rep] ... ≤ Hausdorff_dist (range ((to_glue_l hΦ hΨ) ∘ (optimal_GH_injl X Y))) (range ((to_glue_r hΦ hΨ) ∘ (optimal_GH_injr Y Z))) : GH_dist_le_Hausdorff_dist ((to_glue_l_isometry hΦ hΨ).comp (isometry_optimal_GH_injl X Y)) ((to_glue_r_isometry hΦ hΨ).comp (isometry_optimal_GH_injr Y Z)) ... ≤ Hausdorff_dist (range ((to_glue_l hΦ hΨ) ∘ (optimal_GH_injl X Y))) (range ((to_glue_l hΦ hΨ) ∘ (optimal_GH_injr X Y))) + Hausdorff_dist (range ((to_glue_l hΦ hΨ) ∘ (optimal_GH_injr X Y))) (range ((to_glue_r hΦ hΨ) ∘ (optimal_GH_injr Y Z))) : begin refine Hausdorff_dist_triangle (Hausdorff_edist_ne_top_of_ne_empty_of_bounded (by simp [-nonempty_subtype]) (by simp [-nonempty_subtype]) _ _), { rw [← image_univ], exact bounded_of_compact (compact_image compact_univ (isometry.continuous ((to_glue_l_isometry hΦ hΨ).comp (isometry_optimal_GH_injl X Y)))) }, { rw [← image_univ], exact bounded_of_compact (compact_image compact_univ (isometry.continuous ((to_glue_l_isometry hΦ hΨ).comp (isometry_optimal_GH_injr X Y)))) } end ... = Hausdorff_dist ((to_glue_l hΦ hΨ) '' (range (optimal_GH_injl X Y))) ((to_glue_l hΦ hΨ) '' (range (optimal_GH_injr X Y))) + Hausdorff_dist ((to_glue_r hΦ hΨ) '' (range (optimal_GH_injl Y Z))) ((to_glue_r hΦ hΨ) '' (range (optimal_GH_injr Y Z))) : by simp only [eq.symm range_comp, Comm, eq_self_iff_true, add_right_inj] ... = Hausdorff_dist (range (optimal_GH_injl X Y)) (range (optimal_GH_injr X Y)) + Hausdorff_dist (range (optimal_GH_injl Y Z)) (range (optimal_GH_injr Y Z)) : by rw [Hausdorff_dist_image (to_glue_l_isometry hΦ hΨ), Hausdorff_dist_image (to_glue_r_isometry hΦ hΨ)] ... = dist (to_GH_space X) (to_GH_space Y) + dist (to_GH_space Y) (to_GH_space Z) : by rw [Hausdorff_dist_optimal, Hausdorff_dist_optimal, GH_dist, GH_dist] ... = dist x y + dist y z: by rw [x.to_GH_space_rep, y.to_GH_space_rep, z.to_GH_space_rep] end } end GH_space --section end Gromov_Hausdorff /-- In particular, nonempty compacts of a metric space map to GH_space. We register this in the topological_space namespace to take advantage of the notation p.to_GH_space -/ definition topological_space.nonempty_compacts.to_GH_space {α : Type u} [metric_space α] (p : nonempty_compacts α) : Gromov_Hausdorff.GH_space := Gromov_Hausdorff.to_GH_space p.val open topological_space namespace Gromov_Hausdorff section nonempty_compacts variables {α : Type u} [metric_space α] theorem GH_dist_le_nonempty_compacts_dist (p q : nonempty_compacts α) : dist p.to_GH_space q.to_GH_space ≤ dist p q := begin have ha : isometry (subtype.val : p.val → α) := isometry_subtype_val, have hb : isometry (subtype.val : q.val → α) := isometry_subtype_val, have A : dist p q = Hausdorff_dist p.val q.val := rfl, have I : p.val = range (subtype.val : p.val → α), by simp, have J : q.val = range (subtype.val : q.val → α), by simp, rw [I, J] at A, rw A, exact GH_dist_le_Hausdorff_dist ha hb end lemma to_GH_space_lipschitz : lipschitz_with 1 (nonempty_compacts.to_GH_space : nonempty_compacts α → GH_space) := lipschitz_with.one_mk GH_dist_le_nonempty_compacts_dist lemma to_GH_space_continuous : continuous (nonempty_compacts.to_GH_space : nonempty_compacts α → GH_space) := to_GH_space_lipschitz.to_continuous end nonempty_compacts section /- In this section, we show that if two metric spaces are isometric up to ε2, then their Gromov-Hausdorff distance is bounded by ε2 / 2. More generally, if there are subsets which are ε1-dense and ε3-dense in two spaces, and isometric up to ε2, then the Gromov-Hausdorff distance between the spaces is bounded by ε1 + ε2/2 + ε3. For this, we construct a suitable coupling between the two spaces, by gluing them (approximately) along the two matching subsets. -/ variables {α : Type u} [metric_space α] [compact_space α] [nonempty α] {β : Type v} [metric_space β] [compact_space β] [nonempty β] -- we want to ignore these instances in the following theorem local attribute [instance, priority 10] sum.topological_space sum.uniform_space /-- If there are subsets which are ε1-dense and ε3-dense in two spaces, and isometric up to ε2, then the Gromov-Hausdorff distance between the spaces is bounded by ε1 + ε2/2 + ε3. -/ theorem GH_dist_le_of_approx_subsets {s : set α} (Φ : s → β) {ε1 ε2 ε3 : ℝ} (hs : ∀x : α, ∃y ∈ s, dist x y ≤ ε1) (hs' : ∀x : β, ∃y : s, dist x (Φ y) ≤ ε3) (H : ∀x y : s, abs (dist x y - dist (Φ x) (Φ y)) ≤ ε2) : GH_dist α β ≤ ε1 + ε2 / 2 + ε3 := begin refine real.le_of_forall_epsilon_le (λδ δ0, _), rcases exists_mem_of_nonempty α with ⟨xα, _⟩, rcases hs xα with ⟨xs, hxs, Dxs⟩, have sne : s ≠ ∅ := ne_empty_of_mem hxs, letI : nonempty (subtype s) := ⟨⟨xs, hxs⟩⟩, have : 0 ≤ ε2 := le_trans (abs_nonneg _) (H ⟨xs, hxs⟩ ⟨xs, hxs⟩), have : ∀ p q : s, abs (dist p q - dist (Φ p) (Φ q)) ≤ 2 * (ε2/2 + δ) := λp q, calc abs (dist p q - dist (Φ p) (Φ q)) ≤ ε2 : H p q ... ≤ 2 * (ε2/2 + δ) : by linarith, -- glue α and β along the almost matching subsets letI : metric_space (α ⊕ β) := glue_metric_approx (@subtype.val α s) (λx, Φ x) (ε2/2 + δ) (by linarith) this, let Fl := @sum.inl α β, let Fr := @sum.inr α β, have Il : isometry Fl := isometry_emetric_iff_metric.2 (λx y, rfl), have Ir : isometry Fr := isometry_emetric_iff_metric.2 (λx y, rfl), /- The proof goes as follows : the GH_dist is bounded by the Hausdorff distance of the images in the coupling, which is bounded (using the triangular inequality) by the sum of the Hausdorff distances of α and s (in the coupling or, equivalently in the original space), of s and Φ s, and of Φ s and β (in the coupling or, equivalently, in the original space). The first term is bounded by ε1, by ε1-density. The third one is bounded by ε3. And the middle one is bounded by ε2/2 as in the coupling the points x and Φ x are at distance ε2/2 by construction of the coupling (in fact ε2/2 + δ where δ is an arbitrarily small positive constant where positivity is used to ensure that the coupling is really a metric space and not a premetric space on α ⊕ β). -/ have : GH_dist α β ≤ Hausdorff_dist (range Fl) (range Fr) := GH_dist_le_Hausdorff_dist Il Ir, have : Hausdorff_dist (range Fl) (range Fr) ≤ Hausdorff_dist (range Fl) (Fl '' s) + Hausdorff_dist (Fl '' s) (range Fr), { have B : bounded (range Fl) := bounded_of_compact (compact_range Il.continuous), exact Hausdorff_dist_triangle (Hausdorff_edist_ne_top_of_ne_empty_of_bounded (by simpa) (by simpa) B (bounded.subset (image_subset_range _ _) B)) }, have : Hausdorff_dist (Fl '' s) (range Fr) ≤ Hausdorff_dist (Fl '' s) (Fr '' (range Φ)) + Hausdorff_dist (Fr '' (range Φ)) (range Fr), { have B : bounded (range Fr) := bounded_of_compact (compact_range Ir.continuous), exact Hausdorff_dist_triangle' (Hausdorff_edist_ne_top_of_ne_empty_of_bounded (by simpa [-nonempty_subtype]) (by simpa) (bounded.subset (image_subset_range _ _) B) B) }, have : Hausdorff_dist (range Fl) (Fl '' s) ≤ ε1, { rw [← image_univ, Hausdorff_dist_image Il], have : 0 ≤ ε1 := le_trans dist_nonneg Dxs, refine Hausdorff_dist_le_of_mem_dist this (λx hx, hs x) (λx hx, ⟨x, mem_univ _, by simpa⟩) }, have : Hausdorff_dist (Fl '' s) (Fr '' (range Φ)) ≤ ε2/2 + δ, { refine Hausdorff_dist_le_of_mem_dist (by linarith) _ _, { assume x' hx', rcases (set.mem_image _ _ _).1 hx' with ⟨x, ⟨x_in_s, xx'⟩⟩, rw ← xx', use [Fr (Φ ⟨x, x_in_s⟩), mem_image_of_mem Fr (mem_range_self _)], exact le_of_eq (glue_dist_glued_points (@subtype.val α s) Φ (ε2/2 + δ) ⟨x, x_in_s⟩) }, { assume x' hx', rcases (set.mem_image _ _ _).1 hx' with ⟨y, ⟨y_in_s', yx'⟩⟩, rcases mem_range.1 y_in_s' with ⟨x, xy⟩, use [Fl x, mem_image_of_mem _ x.2], rw [← yx', ← xy, dist_comm], exact le_of_eq (glue_dist_glued_points (@subtype.val α s) Φ (ε2/2 + δ) x) } }, have : Hausdorff_dist (Fr '' (range Φ)) (range Fr) ≤ ε3, { rw [← @image_univ _ _ Fr, Hausdorff_dist_image Ir], rcases exists_mem_of_nonempty β with ⟨xβ, _⟩, rcases hs' xβ with ⟨xs', Dxs'⟩, have : 0 ≤ ε3 := le_trans dist_nonneg Dxs', refine Hausdorff_dist_le_of_mem_dist this (λx hx, ⟨x, mem_univ _, by simpa⟩) (λx _, _), rcases hs' x with ⟨y, Dy⟩, exact ⟨Φ y, mem_range_self _, Dy⟩ }, linarith end end --section /-- The Gromov-Hausdorff space is second countable. -/ lemma second_countable : second_countable_topology GH_space := begin refine second_countable_of_countable_discretization (λδ δpos, _), let ε := (2/5) * δ, have εpos : 0 < ε := mul_pos (by norm_num) δpos, have : ∀p:GH_space, ∃s : set (p.rep), finite s ∧ (univ ⊆ (⋃x∈s, ball x ε)) := λp, by simpa using finite_cover_balls_of_compact (@compact_univ p.rep _ _) εpos, -- for each p, s p is a finite ε-dense subset of p (or rather the metric space -- p.rep representing p) choose s hs using this, have : ∀p:GH_space, ∀t:set (p.rep), finite t → ∃n:ℕ, ∃e:equiv t (fin n), true, { assume p t ht, letI : fintype t := finite.fintype ht, rcases fintype.exists_equiv_fin t with ⟨n, hn⟩, rcases hn with e, exact ⟨n, e, trivial⟩ }, choose N e hne using this, -- cardinality of the nice finite subset s p of p.rep, called N p let N := λp:GH_space, N p (s p) (hs p).1, -- equiv from s p, a nice finite subset of p.rep, to fin (N p), called E p let E := λp:GH_space, e p (s p) (hs p).1, -- A function F associating to p ∈ GH_space the data of all distances of points -- in the ε-dense set s p. let F : GH_space → Σn:ℕ, (fin n → fin n → ℤ) := λp, ⟨N p, λa b, floor (ε⁻¹ * dist ((E p).inv_fun a) ((E p).inv_fun b))⟩, refine ⟨_, by apply_instance, F, λp q hpq, _⟩, /- As the target space of F is countable, it suffices to show that two points p and q with F p = F q are at distance ≤ δ. For this, we construct a map Φ from s p ⊆ p.rep (representing p) to q.rep (representing q) which is almost an isometry on s p, and with image s q. For this, we compose the identification of s p with fin (N p) and the inverse of the identification of s q with fin (N q). Together with the fact that N p = N q, this constructs Ψ between s p and s q, and then composing with the canonical inclusion we get Φ. -/ have Npq : N p = N q := (sigma.mk.inj_iff.1 hpq).1, let Ψ : s p → s q := λx, (E q).inv_fun (fin.cast Npq ((E p).to_fun x)), let Φ : s p → q.rep := λx, Ψ x, -- Use the almost isometry Φ to show that p.rep and q.rep -- are within controlled Gromov-Hausdorff distance. have main : GH_dist p.rep q.rep ≤ ε + ε/2 + ε, { refine GH_dist_le_of_approx_subsets Φ _ _ _, show ∀x : p.rep, ∃ (y : p.rep) (H : y ∈ s p), dist x y ≤ ε, { -- by construction, s p is ε-dense assume x, have : x ∈ ⋃y∈(s p), ball y ε := (hs p).2 (mem_univ _), rcases mem_bUnion_iff.1 this with ⟨y, ⟨ys, hy⟩⟩, exact ⟨y, ys, le_of_lt hy⟩ }, show ∀x : q.rep, ∃ (z : s p), dist x (Φ z) ≤ ε, { -- by construction, s q is ε-dense, and it is the range of Φ assume x, have : x ∈ ⋃y∈(s q), ball y ε := (hs q).2 (mem_univ _), rcases mem_bUnion_iff.1 this with ⟨y, ⟨ys, hy⟩⟩, let i := ((E q).to_fun ⟨y, ys⟩).1, let hi := ((E q).to_fun ⟨y, ys⟩).2, have ihi_eq : (⟨i, hi⟩ : fin (N q)) = (E q).to_fun ⟨y, ys⟩, by rw fin.ext_iff, have hiq : i < N q := hi, have hip : i < N p, { rwa Npq.symm at hiq }, let z := (E p).inv_fun ⟨i, hip⟩, use z, have C1 : (E p).to_fun z = ⟨i, hip⟩ := (E p).right_inv ⟨i, hip⟩, have C2 : fin.cast Npq ⟨i, hip⟩ = ⟨i, hi⟩ := rfl, have C3 : (E q).inv_fun ⟨i, hi⟩ = ⟨y, ys⟩, by { rw ihi_eq, exact (E q).left_inv ⟨y, ys⟩ }, have : Φ z = y := by { simp only [Φ, Ψ], rw [C1, C2, C3], refl }, rw this, exact le_of_lt hy }, show ∀x y : s p, abs (dist x y - dist (Φ x) (Φ y)) ≤ ε, { /- the distance between x and y is encoded in F p, and the distance between Φ x and Φ y (two points of s q) is encoded in F q, all this up to ε. As F p = F q, the distances are almost equal. -/ assume x y, have : dist (Φ x) (Φ y) = dist (Ψ x) (Ψ y) := rfl, rw this, -- introduce i, that codes both x and Φ x in fin (N p) = fin (N q) let i := ((E p).to_fun x).1, have hip : i < N p := ((E p).to_fun x).2, have hiq : i < N q, by rwa Npq at hip, have i' : i = ((E q).to_fun (Ψ x)).1, by { simp [Ψ, (E q).right_inv _] }, -- introduce j, that codes both y and Φ y in fin (N p) = fin (N q) let j := ((E p).to_fun y).1, have hjp : j < N p := ((E p).to_fun y).2, have hjq : j < N q, by rwa Npq at hjp, have j' : j = ((E q).to_fun (Ψ y)).1, by { simp [Ψ, (E q).right_inv _] }, -- Express dist x y in terms of F p have : (F p).2 ((E p).to_fun x) ((E p).to_fun y) = floor (ε⁻¹ * dist x y), by simp only [F, (E p).left_inv _], have Ap : (F p).2 ⟨i, hip⟩ ⟨j, hjp⟩ = floor (ε⁻¹ * dist x y), by { rw ← this, congr; apply (fin.ext_iff _ _).2; refl }, -- Express dist (Φ x) (Φ y) in terms of F q have : (F q).2 ((E q).to_fun (Ψ x)) ((E q).to_fun (Ψ y)) = floor (ε⁻¹ * dist (Ψ x) (Ψ y)), by simp only [F, (E q).left_inv _], have Aq : (F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩ = floor (ε⁻¹ * dist (Ψ x) (Ψ y)), by { rw ← this, congr; apply (fin.ext_iff _ _).2; [exact i', exact j'] }, -- use the equality between F p and F q to deduce that the distances have equal -- integer parts have : (F p).2 ⟨i, hip⟩ ⟨j, hjp⟩ = (F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩, { -- we want to `subst hpq` where `hpq : F p = F q`, except that `subst` only works -- with a constant, so replace `F q` (and everything that depends on it) by a constant f -- then subst revert hiq hjq, change N q with (F q).1, generalize_hyp : F q = f at hpq ⊢, subst hpq, intros, refl }, rw [Ap, Aq] at this, -- deduce that the distances coincide up to ε, by a straightforward computation -- that should be automated have I := calc abs (ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y)) = abs (ε⁻¹ * (dist x y - dist (Ψ x) (Ψ y))) : (abs_mul _ _).symm ... = abs ((ε⁻¹ * dist x y) - (ε⁻¹ * dist (Ψ x) (Ψ y))) : by { congr, ring } ... ≤ 1 : le_of_lt (abs_sub_lt_one_of_floor_eq_floor this), calc abs (dist x y - dist (Ψ x) (Ψ y)) = (ε * ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y)) : by rw [mul_inv_cancel (ne_of_gt εpos), one_mul] ... = ε * (abs (ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y))) : by rw [abs_of_nonneg (le_of_lt (inv_pos εpos)), mul_assoc] ... ≤ ε * 1 : mul_le_mul_of_nonneg_left I (le_of_lt εpos) ... = ε : mul_one _ } }, calc dist p q = GH_dist (p.rep) (q.rep) : dist_GH_dist p q ... ≤ ε + ε/2 + ε : main ... = δ : by { simp [ε], ring } end /-- Compactness criterion : a closed set of compact metric spaces is compact if the spaces have a uniformly bounded diameter, and for all ε the number of balls of radius ε required to cover the space is uniformly bounded. This is an equivalence, but we only prove the interesting direction that these conditions imply compactness. -/ lemma totally_bounded {t : set GH_space} {C : ℝ} {u : ℕ → ℝ} {K : ℕ → ℕ} (ulim : tendsto u at_top (𝓝 0)) (hdiam : ∀p ∈ t, diam (univ : set (GH_space.rep p)) ≤ C) (hcov : ∀p ∈ t, ∀n:ℕ, ∃s : set (GH_space.rep p), cardinal.mk s ≤ K n ∧ univ ⊆ ⋃x∈s, ball x (u n)) : totally_bounded t := begin /- Let δ>0, and ε = δ/5. For each p, we construct a finite subset s p of p, which is ε-dense and has cardinality at most K n. Encoding the mutual distances of points in s p, up to ε, we will get a map F associating to p finitely many data, and making it possible to reconstruct p up to ε. This is enough to prove total boundedness. -/ refine metric.totally_bounded_of_finite_discretization (λδ δpos, _), let ε := (1/5) * δ, have εpos : 0 < ε := mul_pos (by norm_num) δpos, -- choose n for which ε < u n rcases metric.tendsto_at_top.1 ulim ε εpos with ⟨n, hn⟩, have u_le_ε : u n ≤ ε, { have := hn n (le_refl _), simp only [real.dist_eq, add_zero, sub_eq_add_neg, neg_zero] at this, exact le_of_lt (lt_of_le_of_lt (le_abs_self _) this) }, -- construct a finite subset s p of p which is ε-dense and has cardinal ≤ K n have : ∀p:GH_space, ∃s : set (p.rep), ∃N ≤ K n, ∃E : equiv s (fin N), p ∈ t → univ ⊆ ⋃x∈s, ball x (u n), { assume p, by_cases hp : p ∉ t, { have : nonempty (equiv (∅ : set (p.rep)) (fin 0)), { rw ← fintype.card_eq, simp }, use [∅, 0, bot_le, choice (this)] }, { rcases hcov _ (set.not_not_mem.1 hp) n with ⟨s, ⟨scard, scover⟩⟩, rcases cardinal.lt_omega.1 (lt_of_le_of_lt scard (cardinal.nat_lt_omega _)) with ⟨N, hN⟩, rw [hN, cardinal.nat_cast_le] at scard, have : cardinal.mk s = cardinal.mk (fin N), by rw [hN, cardinal.mk_fin], cases quotient.exact this with E, use [s, N, scard, E], simp [hp, scover] } }, choose s N hN E hs using this, -- Define a function F taking values in a finite type and associating to p enough data -- to reconstruct it up to ε, namely the (discretized) distances between elements of s p. let M := (floor (ε⁻¹ * max C 0)).to_nat, let F : GH_space → (Σk:fin ((K n).succ), (fin k → fin k → fin (M.succ))) := λp, ⟨⟨N p, lt_of_le_of_lt (hN p) (nat.lt_succ_self _)⟩, λa b, ⟨min M (floor (ε⁻¹ * dist ((E p).inv_fun a) ((E p).inv_fun b))).to_nat, lt_of_le_of_lt ( min_le_left _ _) (nat.lt_succ_self _) ⟩ ⟩, refine ⟨_, by apply_instance, (λp, F p), _⟩, -- It remains to show that if F p = F q, then p and q are ε-close rintros ⟨p, pt⟩ ⟨q, qt⟩ hpq, have Npq : N p = N q := (fin.ext_iff _ _).1 (sigma.mk.inj_iff.1 hpq).1, let Ψ : s p → s q := λx, (E q).inv_fun (fin.cast Npq ((E p).to_fun x)), let Φ : s p → q.rep := λx, Ψ x, have main : GH_dist (p.rep) (q.rep) ≤ ε + ε/2 + ε, { -- to prove the main inequality, argue that s p is ε-dense in p, and s q is ε-dense in q, -- and s p and s q are almost isometric. Then closeness follows -- from GH_dist_le_of_approx_subsets refine GH_dist_le_of_approx_subsets Φ _ _ _, show ∀x : p.rep, ∃ (y : p.rep) (H : y ∈ s p), dist x y ≤ ε, { -- by construction, s p is ε-dense assume x, have : x ∈ ⋃y∈(s p), ball y (u n) := (hs p pt) (mem_univ _), rcases mem_bUnion_iff.1 this with ⟨y, ⟨ys, hy⟩⟩, exact ⟨y, ys, le_trans (le_of_lt hy) u_le_ε⟩ }, show ∀x : q.rep, ∃ (z : s p), dist x (Φ z) ≤ ε, { -- by construction, s q is ε-dense, and it is the range of Φ assume x, have : x ∈ ⋃y∈(s q), ball y (u n) := (hs q qt) (mem_univ _), rcases mem_bUnion_iff.1 this with ⟨y, ⟨ys, hy⟩⟩, let i := ((E q).to_fun ⟨y, ys⟩).1, let hi := ((E q).to_fun ⟨y, ys⟩).2, have ihi_eq : (⟨i, hi⟩ : fin (N q)) = (E q).to_fun ⟨y, ys⟩, by rw fin.ext_iff, have hiq : i < N q := hi, have hip : i < N p, { rwa Npq.symm at hiq }, let z := (E p).inv_fun ⟨i, hip⟩, use z, have C1 : (E p).to_fun z = ⟨i, hip⟩ := (E p).right_inv ⟨i, hip⟩, have C2 : fin.cast Npq ⟨i, hip⟩ = ⟨i, hi⟩ := rfl, have C3 : (E q).inv_fun ⟨i, hi⟩ = ⟨y, ys⟩, by { rw ihi_eq, exact (E q).left_inv ⟨y, ys⟩ }, have : Φ z = y := by { simp only [Φ, Ψ], rw [C1, C2, C3], refl }, rw this, exact le_trans (le_of_lt hy) u_le_ε }, show ∀x y : s p, abs (dist x y - dist (Φ x) (Φ y)) ≤ ε, { /- the distance between x and y is encoded in F p, and the distance between Φ x and Φ y (two points of s q) is encoded in F q, all this up to ε. As F p = F q, the distances are almost equal. -/ assume x y, have : dist (Φ x) (Φ y) = dist (Ψ x) (Ψ y) := rfl, rw this, -- introduce i, that codes both x and Φ x in fin (N p) = fin (N q) let i := ((E p).to_fun x).1, have hip : i < N p := ((E p).to_fun x).2, have hiq : i < N q, by rwa Npq at hip, have i' : i = ((E q).to_fun (Ψ x)).1, by { simp [Ψ, (E q).right_inv _] }, -- introduce j, that codes both y and Φ y in fin (N p) = fin (N q) let j := ((E p).to_fun y).1, have hjp : j < N p := ((E p).to_fun y).2, have hjq : j < N q, by rwa Npq at hjp, have j' : j = ((E q).to_fun (Ψ y)).1, by { simp [Ψ, (E q).right_inv _] }, -- Express dist x y in terms of F p have Ap : ((F p).2 ⟨i, hip⟩ ⟨j, hjp⟩).1 = (floor (ε⁻¹ * dist x y)).to_nat := calc ((F p).2 ⟨i, hip⟩ ⟨j, hjp⟩).1 = ((F p).2 ((E p).to_fun x) ((E p).to_fun y)).1 : by { congr; apply (fin.ext_iff _ _).2; refl } ... = min M (floor (ε⁻¹ * dist x y)).to_nat : by simp only [F, (E p).left_inv _] ... = (floor (ε⁻¹ * dist x y)).to_nat : begin refine min_eq_right (int.to_nat_le_to_nat (floor_mono _)), refine mul_le_mul_of_nonneg_left (le_trans _ (le_max_left _ _)) (le_of_lt (inv_pos εpos)), change dist (x : p.rep) y ≤ C, refine le_trans (dist_le_diam_of_mem (bounded_of_compact compact_univ) (mem_univ _) (mem_univ _)) _, exact hdiam p pt end, -- Express dist (Φ x) (Φ y) in terms of F q have Aq : ((F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩).1 = (floor (ε⁻¹ * dist (Ψ x) (Ψ y))).to_nat := calc ((F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩).1 = ((F q).2 ((E q).to_fun (Ψ x)) ((E q).to_fun (Ψ y))).1 : by { congr; apply (fin.ext_iff _ _).2; [exact i', exact j'] } ... = min M (floor (ε⁻¹ * dist (Ψ x) (Ψ y))).to_nat : by simp only [F, (E q).left_inv _] ... = (floor (ε⁻¹ * dist (Ψ x) (Ψ y))).to_nat : begin refine min_eq_right (int.to_nat_le_to_nat (floor_mono _)), refine mul_le_mul_of_nonneg_left (le_trans _ (le_max_left _ _)) (le_of_lt (inv_pos εpos)), change dist (Ψ x : q.rep) (Ψ y) ≤ C, refine le_trans (dist_le_diam_of_mem (bounded_of_compact compact_univ) (mem_univ _) (mem_univ _)) _, exact hdiam q qt end, -- use the equality between F p and F q to deduce that the distances have equal -- integer parts have : ((F p).2 ⟨i, hip⟩ ⟨j, hjp⟩).1 = ((F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩).1, { -- we want to `subst hpq` where `hpq : F p = F q`, except that `subst` only works -- with a constant, so replace `F q` (and everything that depends on it) by a constant f -- then subst revert hiq hjq, change N q with (F q).1, generalize_hyp : F q = f at hpq ⊢, subst hpq, intros, refl }, have : floor (ε⁻¹ * dist x y) = floor (ε⁻¹ * dist (Ψ x) (Ψ y)), { rw [Ap, Aq] at this, have D : 0 ≤ floor (ε⁻¹ * dist x y) := floor_nonneg.2 (mul_nonneg (le_of_lt (inv_pos εpos)) dist_nonneg), have D' : floor (ε⁻¹ * dist (Ψ x) (Ψ y)) ≥ 0 := floor_nonneg.2 (mul_nonneg (le_of_lt (inv_pos εpos)) dist_nonneg), rw [← int.to_nat_of_nonneg D, ← int.to_nat_of_nonneg D', this] }, -- deduce that the distances coincide up to ε, by a straightforward computation -- that should be automated have I := calc abs (ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y)) = abs (ε⁻¹ * (dist x y - dist (Ψ x) (Ψ y))) : (abs_mul _ _).symm ... = abs ((ε⁻¹ * dist x y) - (ε⁻¹ * dist (Ψ x) (Ψ y))) : by { congr, ring } ... ≤ 1 : le_of_lt (abs_sub_lt_one_of_floor_eq_floor this), calc abs (dist x y - dist (Ψ x) (Ψ y)) = (ε * ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y)) : by rw [mul_inv_cancel (ne_of_gt εpos), one_mul] ... = ε * (abs (ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y))) : by rw [abs_of_nonneg (le_of_lt (inv_pos εpos)), mul_assoc] ... ≤ ε * 1 : mul_le_mul_of_nonneg_left I (le_of_lt εpos) ... = ε : mul_one _ } }, calc dist p q = GH_dist (p.rep) (q.rep) : dist_GH_dist p q ... ≤ ε + ε/2 + ε : main ... = δ/2 : by { simp [ε], ring } ... < δ : half_lt_self δpos end section complete /- We will show that a sequence `u n` of compact metric spaces satisfying `dist (u n) (u (n+1)) < 1/2^n` converges, which implies completeness of the Gromov-Hausdorff space. We need to exhibit the limiting compact metric space. For this, start from a sequence `X n` of representatives of `u n`, and glue in an optimal way `X n` to `X (n+1)` for all `n`, in a common metric space. Formally, this is done as follows. Start from `Y 0 = X 0`. Then, glue `X 0` to `X 1` in an optimal way, yielding a space `Y 1` (with an embedding of `X 1`). Then, consider an optimal gluing of `X 1` and `X 2`, and glue it to `Y 1` along their common subspace `X 1`. This gives a new space `Y 2`, with an embedding of `X 2`. Go on, to obtain a sequence of spaces `Y n`. Let `Z0` be the inductive limit of the `Y n`, and finally let `Z` be the completion of `Z0`. The images `X2 n` of `X n` in `Z` are at Hausdorff distance `< 1/2^n` by construction, hence they form a Cauchy sequence for the Hausdorff distance. By completeness (of `Z`, and therefore of its set of nonempty compact subsets), they converge to a limit `L`. This is the nonempty compact metric space we are looking for. -/ variables (X : ℕ → Type) [∀n, metric_space (X n)] [∀n, compact_space (X n)] [∀n, nonempty (X n)] structure aux_gluing_struct (A : Type) [metric_space A] : Type 1 := (space : Type) (metric : metric_space space) (embed : A → space) (isom : isometry embed) def aux_gluing (n : ℕ) : aux_gluing_struct (X n) := nat.rec_on n { space := X 0, metric := by apply_instance, embed := id, isom := λx y, rfl } (λn a, by letI : metric_space a.space := a.metric; exact { space := glue_space a.isom (isometry_optimal_GH_injl (X n) (X n.succ)), metric := metric.metric_space_glue_space a.isom (isometry_optimal_GH_injl (X n) (X n.succ)), embed := (to_glue_r a.isom (isometry_optimal_GH_injl (X n) (X n.succ))) ∘ (optimal_GH_injr (X n) (X n.succ)), isom := (to_glue_r_isometry _ _).comp (isometry_optimal_GH_injr (X n) (X n.succ)) }) /-- The Gromov-Hausdorff space is complete. -/ instance : complete_space (GH_space) := begin have : ∀ (n : ℕ), 0 < ((1:ℝ) / 2) ^ n, by { apply _root_.pow_pos, norm_num }, -- start from a sequence of nonempty compact metric spaces within distance 1/2^n of each other refine metric.complete_of_convergent_controlled_sequences (λn, (1/2)^n) this (λu hu, _), -- X n is a representative of u n let X := λn, (u n).rep, -- glue them together successively in an optimal way, getting a sequence of metric spaces Y n let Y := aux_gluing X, letI : ∀n, metric_space (Y n).space := λn, (Y n).metric, have E : ∀n:ℕ, glue_space (Y n).isom (isometry_optimal_GH_injl (X n) (X n.succ)) = (Y n.succ).space := λn, by { simp [Y, aux_gluing], refl }, let c := λn, cast (E n), have ic : ∀n, isometry (c n) := λn x y, rfl, -- there is a canonical embedding of Y n in Y (n+1), by construction let f : Πn, (Y n).space → (Y n.succ).space := λn, (c n) ∘ (to_glue_l (aux_gluing X n).isom (isometry_optimal_GH_injl (X n) (X n.succ))), have I : ∀n, isometry (f n), { assume n, apply isometry.comp, { assume x y, refl }, { apply to_glue_l_isometry } }, -- consider the inductive limit Z0 of the Y n, and then its completion Z let Z0 := metric.inductive_limit I, let Z := uniform_space.completion Z0, let Φ := to_inductive_limit I, let coeZ := (coe : Z0 → Z), -- let X2 n be the image of X n in the space Z let X2 := λn, range (coeZ ∘ (Φ n) ∘ (Y n).embed), have isom : ∀n, isometry (coeZ ∘ (Φ n) ∘ (Y n).embed), { assume n, apply isometry.comp completion.coe_isometry _, apply isometry.comp _ (Y n).isom, apply to_inductive_limit_isometry }, -- The Hausdorff distance of `X2 n` and `X2 (n+1)` is by construction the distance between -- `u n` and `u (n+1)`, therefore bounded by 1/2^n have D2 : ∀n, Hausdorff_dist (X2 n) (X2 n.succ) < (1/2)^n, { assume n, have X2n : X2 n = range ((coeZ ∘ (Φ n.succ) ∘ (c n) ∘ (to_glue_r (Y n).isom (isometry_optimal_GH_injl (X n) (X n.succ)))) ∘ (optimal_GH_injl (X n) (X n.succ))), { change X2 n = range (coeZ ∘ (Φ n.succ) ∘ (c n) ∘ (to_glue_r (Y n).isom (isometry_optimal_GH_injl (X n) (X n.succ))) ∘ (optimal_GH_injl (X n) (X n.succ))), simp only [X2, Φ], rw [← to_inductive_limit_commute I], simp only [f], rw ← to_glue_commute }, rw range_comp at X2n, have X2nsucc : X2 n.succ = range ((coeZ ∘ (Φ n.succ) ∘ (c n) ∘ (to_glue_r (Y n).isom (isometry_optimal_GH_injl (X n) (X n.succ)))) ∘ (optimal_GH_injr (X n) (X n.succ))), by refl, rw range_comp at X2nsucc, rw [X2n, X2nsucc, Hausdorff_dist_image, Hausdorff_dist_optimal, ← dist_GH_dist], { exact hu n n n.succ (le_refl n) (le_succ n) }, { apply isometry.comp completion.coe_isometry _, apply isometry.comp _ ((ic n).comp (to_glue_r_isometry _ _)), apply to_inductive_limit_isometry } }, -- consider `X2 n` as a member `X3 n` of the type of nonempty compact subsets of `Z`, which -- is a metric space let X3 : ℕ → nonempty_compacts Z := λn, ⟨X2 n, ⟨by { simp only [X2, set.range_eq_empty, not_not, ne.def], apply_instance }, compact_range (isom n).continuous ⟩⟩, -- `X3 n` is a Cauchy sequence by construction, as the successive distances are -- bounded by (1/2)^n have : cauchy_seq X3, { refine cauchy_seq_of_le_geometric (1/2) 1 (by norm_num) (λn, _), rw one_mul, exact le_of_lt (D2 n) }, -- therefore, it converges to a limit `L` rcases cauchy_seq_tendsto_of_complete this with ⟨L, hL⟩, -- the images of `X3 n` in the Gromov-Hausdorff space converge to the image of `L` have M : tendsto (λn, (X3 n).to_GH_space) at_top (𝓝 L.to_GH_space) := tendsto.comp (to_GH_space_continuous.tendsto _) hL, -- By construction, the image of `X3 n` in the Gromov-Hausdorff space is `u n`. have : ∀n, (X3 n).to_GH_space = u n, { assume n, rw [nonempty_compacts.to_GH_space, ← (u n).to_GH_space_rep, to_GH_space_eq_to_GH_space_iff_isometric], constructor, convert (isom n).isometric_on_range.symm, }, -- Finally, we have proved the convergence of `u n` exact ⟨L.to_GH_space, by simpa [this] using M⟩ end end complete--section end Gromov_Hausdorff --namespace
b6abd2a33ed2f02eadf94195a63ab46cb7d5e43a
c45b34bfd44d8607a2e8762c926e3cfaa7436201
/uexp/src/uexp/rules/commonexp.lean
9f5d0e82ac0d559d896702c00b74cb6958018c02
[ "BSD-2-Clause" ]
permissive
Shamrock-Frost/Cosette
b477c442c07e45082348a145f19ebb35a7f29392
24cbc4adebf627f13f5eac878f04ffa20d1209af
refs/heads/master
1,619,721,304,969
1,526,082,841,000
1,526,082,841,000
121,695,605
1
0
null
1,518,737,210,000
1,518,737,210,000
null
UTF-8
Lean
false
false
1,146
lean
import ..sql import ..tactics import ..u_semiring import ..extra_constants import ..ucongr import ..TDP import ..cosette_tactics -- NOTE: this one cannot solved by ucongr, need to be revisited open Expr open Proj open Pred open SQL open binary_operators theorem rule : forall (Γ s1 : Schema) (a : relation s1) (x : Column datatypes.int s1) (y : Column datatypes.int s1), let xVar : Expr (Γ ++ s1) _ := uvariable (right⋅x), yVar : Expr (Γ ++ s1) _ := uvariable (right⋅y) in denoteSQL (SELECT1 (e2p (binaryExpr add xVar xVar)) FROM1 (table a WHERE (equal xVar yVar)) : SQL Γ _) = denoteSQL (SELECT1 (e2p (binaryExpr add xVar yVar)) FROM1 (table a WHERE (equal xVar yVar)) : SQL Γ _) := begin intros, funext, unfold_all_denotations, simp, congr, funext, apply congr_arg _, have eq_subst_l' : ∀ {s: Schema} (t₁ t₂: Tuple s) (R: Tuple s → Tuple s) (e : Tuple s), (t₁ ≃ t₂) * (e ≃ R t₁) = (t₁ ≃ t₂) * (e ≃ R t₂), { intros, rw eq_subst_l }, rewrite eq_subst_l', end
80cb9a80441bc4ccfb378844f6c7cfdbe1e1f105
037dba89703a79cd4a4aec5e959818147f97635d
/src/2020/tactic_hints/examples.lean
417958ace14ce5e6bb1ee538e8fbb4c866a8c0a0
[]
no_license
ImperialCollegeLondon/M40001_lean
3a6a09298da395ab51bc220a535035d45bbe919b
62a76fa92654c855af2b2fc2bef8e60acd16ccec
refs/heads/master
1,666,750,403,259
1,665,771,117,000
1,665,771,117,000
209,141,835
115
12
null
1,640,270,596,000
1,568,749,174,000
Lean
UTF-8
Lean
false
false
5,037
lean
import tactic /-! # Tactic cheat sheet. -- natnumgame tactics apply, exact (and assumption) split use (use `use` to make progress with `nonempty X`) -/ /-! ## 1) Extracting information from hypotheses -/ /-! ### 1a) cases and rcases Many objects in Lean are pairs of data. For example, a proof of `P ∧ Q` is stored as a pair consisting of a proof of `P` and a proof of `Q`. The hypothesis `∃ n : ℕ, f n = 37` is stored internally as a pair, namely a natural `n` and a proof that `f n = 37`. Note that "hypothesis" and "proof" mean the same thing. If `h : X` is something which is stored as a pair in Lean, then `cases h with a b` will destroy `h` and replace it with the two pieces of data which made up `h`, calling them `a` and `b`. -/ example (h : ∃ n : ℕ, n ^ 2 = 2) : false := begin -- h: ∃ (n : ℕ), n ^ 2 = 2 cases h with n hn, -- n: ℕ -- hn: n ^ 2 = 2 sorry end example (P Q : Prop) (h : P ∧ Q) : P := begin -- h: P ∧ Q cases h with hP hQ, -- hP: P -- hQ: Q exact hP, end -- Some things are more than two pieces of data! You can do much more -- elaborate deconstructions with the `rcases` command. example (R : ℕ → ℕ → Prop) (hR : equivalence R) : symmetric R := begin -- hR: equivalence R rcases hR with ⟨hrefl, hsymm, htrans⟩, -- hrefl: reflexive R -- hsymm: symmetric R -- htrans: transitive R exact hsymm, end /-! ## 1b) specialize Say you have a long hypothesis `h : ∀ n : ℕ, f n > 37 → n = 23`. This hypothesis is a *function*. It takes as inputs a natural number n and a proof that `f n > 37`, and then it gives as output a proof that `n = 23`. You can feed in some inputs and specialize the function. Say for example you you manage to prove the hypothesis `ht : f t > 37` for some natural number `t`. Then `specialize h t ft` would change `h` to `t = 23`. -/ example (X Y : set ℕ) (a : ℕ) (h : ∀ n : ℕ, n ∈ X → n ∈ Y) (haX : a ∈ X) : a ∈ Y := begin -- a: ℕ -- haX: a ∈ X -- h: ∀ (n : ℕ), n ∈ X → n ∈ Y specialize h a haX, -- h: a ∈ Y assumption, end /-! # 2) Making new hypothesis -/ /-! ## have The `have` tactic makes a new hypothesis. The proof of the current goal is paused and a new goal is created. Generally one should now put braces `{ }` because if there is more than one goal then understanding what the code is doing can get very difficult. -/ example (a b c n : ℕ) (hn : n > 2) : a^n + b^n = c^n → a * b = 0 := begin -- ⊢ a ^ n + b ^ n = c ^ n → a * b = 0 -- looks a bit tricky -- why not prove something easier first have ha : (a + 1) + 1 = a + 2, { -- ⊢ a + 1 + 1 = a + 2 apply add_assoc, }, -- ha: a + 1 + 1 = a + 2 -- ⊢ a ^ n + b ^ n = c ^ n → a * b = 0 sorry end /-! # 3) Using hypotheses to change the goal. -/ /-! ## 2a) rw The generic `sub in` tactic. If `h : X = Y` then `rw h` will change all `X`'s in the goal to `Y`'s. Also works with `h : P ↔ Q` if `P` and `Q` are true-false statements. -/ example (X Y : set ℕ) (hXY : X = Y) (a : ℕ) (haX : a ∈ Y) : a ∈ X := begin -- hXY: X = Y -- ⊢ a ∈ X rw hXY, -- hXY: X = Y -- ⊢ a ∈ Y assumption end -- Variants -- `rw h1 at h2`, `rw h1 at h2 ⊢`, `rw h at *` /-! ## 2b) convert `convert` is in some sense the opposite way of thinking to `rw`. Instead of continually rewriting the goal until it becomes one of your assumptions, why not just tell Lean that the assumption is basically the right answer modulo a few loose ends, which Lean will then leave for you as new goals. -/ example (X Y : set ℕ) (hX : 37 ∈ X) : 37 ∈ Y := begin -- hX: 37 ∈ X -- ⊢ 37 ∈ Y convert hX, -- ⊢ Y = X sorry end /- # 4) Changing the goal without using hypotheses -/ /-! ### 4a) intro and rintro -/ -- `intro` is a basic tactic for introducing hypotheses example (P Q : Prop) : P → Q := begin -- ⊢ P → Q intro hP, -- hP: P -- ⊢ Q sorry end -- `rintro` is to `intro` what `rcases` is to `cases`. It enables -- you to assume something and simultaneously take it apart. example (f : ℕ → ℚ) : (∃ n : ℕ, f n > 37) → (∃ n : ℕ, f n > 36) := begin -- ⊢ (∃ (n : ℕ), f n > 37) → P rintro ⟨n, hn⟩, -- n: ℕ -- hn: f n > 37 -- ⊢ P sorry, end /-! ## 4b) ext -/ -- `ext` is Lean's extensionality tactic. If your goal is to prove that -- two extensional things are equal (e.g. sets, functions, binary relations) -- then `ext a` or `ext a b` or whatever is appropriate, will turn the -- question into the assertion that they behave in the same way. Let's look -- at some examples example (A B : set ℕ) : A = B := begin -- ⊢ A = B ext x, -- x : ℕ -- ⊢ x ∈ A ↔ x ∈ B sorry end example (X Y : Type) (f g : X → Y) : f = g := begin -- ⊢ f = g ext x, -- x : X -- ⊢ f x = g x sorry end example (α : Type) (R S : α → α → Prop) : R = S := begin -- ⊢ R = S ext a b, -- a b : α -- ⊢ R a b ↔ S a b sorry end
74aa877e9c877c299cce13adcd8081c94148fd6c
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/ring_theory/polynomial/rational_root_auto.lean
eaa3cee9dc64669d27be1ac990d54d93e01dfd67
[]
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,015
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.ring_theory.polynomial.scale_roots import Mathlib.ring_theory.localization import Mathlib.PostPort universes u_1 u_4 u_2 namespace Mathlib /-! # Rational root theorem and integral root theorem This file contains the rational root theorem and integral root theorem. The rational root theorem for a unique factorization domain `A` with localization `S`, states that the roots of `p : polynomial A` in `A`'s field of fractions are of the form `x / y` with `x y : A`, `x ∣ p.coeff 0` and `y ∣ p.leading_coeff`. The corollary is the integral root theorem `is_integer_of_is_root_of_monic`: if `p` is monic, its roots must be integers. Finally, we use this to show unique factorization domains are integrally closed. ## References * https://en.wikipedia.org/wiki/Rational_root_theorem -/ theorem scale_roots_aeval_eq_zero_of_aeval_mk'_eq_zero {A : Type u_1} {S : Type u_4} [integral_domain A] [comm_ring S] {M : submonoid A} {f : localization_map M S} {p : polynomial A} {r : A} {s : ↥M} (hr : coe_fn (polynomial.aeval (localization_map.mk' f r s)) p = 0) : coe_fn (polynomial.aeval (coe_fn (localization_map.to_map f) r)) (scale_roots p ↑s) = 0 := sorry theorem num_is_root_scale_roots_of_aeval_eq_zero {A : Type u_1} {K : Type u_2} [integral_domain A] [field K] [unique_factorization_monoid A] (g : fraction_map A K) {p : polynomial A} {x : localization_map.codomain g} (hr : coe_fn (polynomial.aeval x) p = 0) : polynomial.is_root (scale_roots p ↑(fraction_map.denom g x)) (fraction_map.num g x) := sorry /-- Rational root theorem part 1: if `r : f.codomain` is a root of a polynomial over the ufd `A`, then the numerator of `r` divides the constant coefficient -/ theorem num_dvd_of_is_root {A : Type u_1} {K : Type u_2} [integral_domain A] [unique_factorization_monoid A] [field K] {f : fraction_map A K} {p : polynomial A} {r : localization_map.codomain f} (hr : coe_fn (polynomial.aeval r) p = 0) : fraction_map.num f r ∣ polynomial.coeff p 0 := sorry /-- Rational root theorem part 2: if `r : f.codomain` is a root of a polynomial over the ufd `A`, then the denominator of `r` divides the leading coefficient -/ theorem denom_dvd_of_is_root {A : Type u_1} {K : Type u_2} [integral_domain A] [unique_factorization_monoid A] [field K] {f : fraction_map A K} {p : polynomial A} {r : localization_map.codomain f} (hr : coe_fn (polynomial.aeval r) p = 0) : ↑(fraction_map.denom f r) ∣ polynomial.leading_coeff p := sorry /-- Integral root theorem: if `r : f.codomain` is a root of a monic polynomial over the ufd `A`, then `r` is an integer -/ theorem is_integer_of_is_root_of_monic {A : Type u_1} {K : Type u_2} [integral_domain A] [unique_factorization_monoid A] [field K] {f : fraction_map A K} {p : polynomial A} (hp : polynomial.monic p) {r : localization_map.codomain f} (hr : coe_fn (polynomial.aeval r) p = 0) : localization_map.is_integer f r := fraction_map.is_integer_of_is_unit_denom f (is_unit_of_dvd_one (↑(fraction_map.denom f r)) (hp ▸ denom_dvd_of_is_root hr)) namespace unique_factorization_monoid theorem integer_of_integral {A : Type u_1} {K : Type u_2} [integral_domain A] [unique_factorization_monoid A] [field K] {f : fraction_map A K} {x : localization_map.codomain f} : is_integral A x → localization_map.is_integer f x := sorry theorem integrally_closed {A : Type u_1} {K : Type u_2} [integral_domain A] [unique_factorization_monoid A] [field K] {f : fraction_map A K} : integral_closure A (localization_map.codomain f) = ⊥ := iff.mpr eq_bot_iff fun (x : localization_map.codomain f) (hx : x ∈ ↑(integral_closure A (localization_map.codomain f))) => iff.mpr algebra.mem_bot (integer_of_integral hx) end Mathlib
aab8e311472f3945dd813184637018399aa27b2b
a4673261e60b025e2c8c825dfa4ab9108246c32e
/src/Lean/Elab/Tactic.lean
f55d98262a6302651b99d336d2e2cb142dbd1074
[ "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
498
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ import Lean.Elab.Term import Lean.Elab.Tactic.Basic import Lean.Elab.Tactic.ElabTerm import Lean.Elab.Tactic.Induction import Lean.Elab.Tactic.Generalize import Lean.Elab.Tactic.Injection import Lean.Elab.Tactic.Match import Lean.Elab.Tactic.Binders import Lean.Elab.Tactic.Rewrite import Lean.Elab.Tactic.Location
680cc64a771f7613fc23a593df92c10430992bed
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/algebra/big_operators/order.lean
2da6043e64d59de85ceafebb7d4eda6e06057cec
[ "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
20,732
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import algebra.big_operators.basic /-! # Results about big operators with values in an ordered algebraic structure. Mostly monotonicity results for the `∏` and `∑` operations. -/ open_locale big_operators variables {ι α β M N G k R : Type*} namespace finset section variables [comm_monoid M] [ordered_comm_monoid N] /-- Let `{x | p x}` be a subsemigroup of a commutative monoid `M`. Let `f : M → N` be a map submultiplicative on `{x | p x}`, i.e., `p x → p y → f (x * y) ≤ f x * f y`. Let `g i`, `i ∈ s`, be a nonempty finite family of elements of `M` such that `∀ i ∈ s, p (g i)`. Then `f (∏ x in s, g x) ≤ ∏ x in s, f (g x)`. -/ @[to_additive le_sum_nonempty_of_subadditive_on_pred] lemma le_prod_nonempty_of_submultiplicative_on_pred (f : M → N) (p : M → Prop) (h_mul : ∀ x y, p x → p y → f (x * y) ≤ f x * f y) (hp_mul : ∀ x y, p x → p y → p (x * y)) (g : ι → M) (s : finset ι) (hs_nonempty : s.nonempty) (hs : ∀ i ∈ s, p (g i)) : f (∏ i in s, g i) ≤ ∏ i in s, f (g i) := begin refine le_trans (multiset.le_prod_nonempty_of_submultiplicative_on_pred f p h_mul hp_mul _ _ _) _, { simp [hs_nonempty.ne_empty], }, { exact multiset.forall_mem_map_iff.mpr hs, }, rw multiset.map_map, refl, end /-- Let `{x | p x}` be an additive subsemigroup of an additive commutative monoid `M`. Let `f : M → N` be a map subadditive on `{x | p x}`, i.e., `p x → p y → f (x + y) ≤ f x + f y`. Let `g i`, `i ∈ s`, be a nonempty finite family of elements of `M` such that `∀ i ∈ s, p (g i)`. Then `f (∑ i in s, g i) ≤ ∏ i in s, f (g i)`. -/ add_decl_doc le_sum_nonempty_of_subadditive_on_pred /-- If `f : M → N` is a submultiplicative function, `f (x * y) ≤ f x * f y` and `g i`, `i ∈ s`, is a nonempty finite family of elements of `M`, then `f (∏ i in s, g i) ≤ ∏ i in s, f (g i)`. -/ @[to_additive le_sum_nonempty_of_subadditive] lemma le_prod_nonempty_of_submultiplicative (f : M → N) (h_mul : ∀ x y, f (x * y) ≤ f x * f y) {s : finset ι} (hs : s.nonempty) (g : ι → M) : f (∏ i in s, g i) ≤ ∏ i in s, f (g i) := le_prod_nonempty_of_submultiplicative_on_pred f (λ i, true) (λ x y _ _, h_mul x y) (λ _ _ _ _, trivial) g s hs (λ _ _, trivial) /-- If `f : M → N` is a subadditive function, `f (x + y) ≤ f x + f y` and `g i`, `i ∈ s`, is a nonempty finite family of elements of `M`, then `f (∑ i in s, g i) ≤ ∑ i in s, f (g i)`. -/ add_decl_doc le_sum_nonempty_of_subadditive /-- Let `{x | p x}` be a subsemigroup of a commutative monoid `M`. Let `f : M → N` be a map such that `f 1 = 1` and `f` is submultiplicative on `{x | p x}`, i.e., `p x → p y → f (x * y) ≤ f x * f y`. Let `g i`, `i ∈ s`, be a finite family of elements of `M` such that `∀ i ∈ s, p (g i)`. Then `f (∏ i in s, g i) ≤ ∏ i in s, f (g i)`. -/ @[to_additive le_sum_of_subadditive_on_pred] lemma le_prod_of_submultiplicative_on_pred (f : M → N) (p : M → Prop) (h_one : f 1 = 1) (h_mul : ∀ x y, p x → p y → f (x * y) ≤ f x * f y) (hp_mul : ∀ x y, p x → p y → p (x * y)) (g : ι → M) {s : finset ι} (hs : ∀ i ∈ s, p (g i)) : f (∏ i in s, g i) ≤ ∏ i in s, f (g i) := begin rcases eq_empty_or_nonempty s with rfl|hs_nonempty, { simp [h_one] }, { exact le_prod_nonempty_of_submultiplicative_on_pred f p h_mul hp_mul g s hs_nonempty hs, }, end /-- Let `{x | p x}` be a subsemigroup of a commutative monoid `M`. Let `f : M → N` be a map such that `f 1 = 1` and `f` is submultiplicative on `{x | p x}`, i.e., `p x → p y → f (x * y) ≤ f x * f y`. Let `g i`, `i ∈ s`, be a finite family of elements of `M` such that `∀ i ∈ s, p (g i)`. Then `f (∏ x in s, g x) ≤ ∏ x in s, f (g x)`. -/ add_decl_doc le_sum_of_subadditive_on_pred /-- If `f : M → N` is a submultiplicative function, `f (x * y) ≤ f x * f y`, `f 1 = 1`, and `g i`, `i ∈ s`, is a finite family of elements of `M`, then `f (∏ i in s, g i) ≤ ∏ i in s, f (g i)`. -/ @[to_additive le_sum_of_subadditive] lemma le_prod_of_submultiplicative (f : M → N) (h_one : f 1 = 1) (h_mul : ∀ x y, f (x * y) ≤ f x * f y) (s : finset ι) (g : ι → M) : f (∏ i in s, g i) ≤ ∏ i in s, f (g i) := begin refine le_trans (multiset.le_prod_of_submultiplicative f h_one h_mul _) _, rw multiset.map_map, refl, end /-- If `f : M → N` is a submultiplicative function, `f (x * y) ≤ f x * f y`, `f 1 = 1`, and `g i`, `i ∈ s`, is a finite family of elements of `M`, then `f (∏ i in s, g i) ≤ ∏ i in s, f (g i)`. -/ add_decl_doc le_sum_of_subadditive variables {f g : ι → N} {s t : finset ι} /-- In an ordered commutative monoid, if each factor `f i` of one finite product is less than or equal to the corresponding factor `g i` of another finite product, then `∏ i in s, f i ≤ ∏ i in s, g i`. -/ @[to_additive sum_le_sum] lemma prod_le_prod'' (h : ∀ i ∈ s, f i ≤ g i) : ∏ i in s, f i ≤ ∏ i in s, g i := begin classical, induction s using finset.induction_on with i s hi ihs h, { refl }, { simp only [prod_insert hi], exact mul_le_mul' (h _ (mem_insert_self _ _)) (ihs $ λ j hj, h j (mem_insert_of_mem hj)) } end /-- In an ordered additive commutative monoid, if each summand `f i` of one finite sum is less than or equal to the corresponding summand `g i` of another finite sum, then `∑ i in s, f i ≤ ∑ i in s, g i`. -/ add_decl_doc sum_le_sum @[to_additive sum_nonneg] lemma one_le_prod' (h : ∀i ∈ s, 1 ≤ f i) : 1 ≤ (∏ i in s, f i) := le_trans (by rw prod_const_one) (prod_le_prod'' h) @[to_additive sum_nonpos] lemma prod_le_one' (h : ∀i ∈ s, f i ≤ 1) : (∏ i in s, f i) ≤ 1 := (prod_le_prod'' h).trans_eq (by rw prod_const_one) @[to_additive sum_le_sum_of_subset_of_nonneg] lemma prod_le_prod_of_subset_of_one_le' (h : s ⊆ t) (hf : ∀ i ∈ t, i ∉ s → 1 ≤ f i) : ∏ i in s, f i ≤ ∏ i in t, f i := by classical; calc (∏ i in s, f i) ≤ (∏ i in t \ s, f i) * (∏ i in s, f i) : le_mul_of_one_le_left' $ one_le_prod' $ by simpa only [mem_sdiff, and_imp] ... = ∏ i in t \ s ∪ s, f i : (prod_union sdiff_disjoint).symm ... = ∏ i in t, f i : by rw [sdiff_union_of_subset h] @[to_additive sum_mono_set_of_nonneg] lemma prod_mono_set_of_one_le' (hf : ∀ x, 1 ≤ f x) : monotone (λ s, ∏ x in s, f x) := λ s t hst, prod_le_prod_of_subset_of_one_le' hst $ λ x _ _, hf x @[to_additive sum_le_univ_sum_of_nonneg] lemma prod_le_univ_prod_of_one_le' [fintype ι] {s : finset ι} (w : ∀ x, 1 ≤ f x) : ∏ x in s, f x ≤ ∏ x, f x := prod_le_prod_of_subset_of_one_le' (subset_univ s) (λ a _ _, w a) @[to_additive sum_eq_zero_iff_of_nonneg] lemma prod_eq_one_iff_of_one_le' : (∀ i ∈ s, 1 ≤ f i) → (∏ i in s, f i = 1 ↔ ∀ i ∈ s, f i = 1) := begin classical, apply finset.induction_on s, exact λ _, ⟨λ _ _, false.elim, λ _, rfl⟩, assume a s ha ih H, have : ∀ i ∈ s, 1 ≤ f i, from λ _, H _ ∘ mem_insert_of_mem, rw [prod_insert ha, mul_eq_one_iff' (H _ $ mem_insert_self _ _) (one_le_prod' this), forall_mem_insert, ih this] end @[to_additive sum_eq_zero_iff_of_nonneg] lemma prod_eq_one_iff_of_le_one' : (∀ i ∈ s, f i ≤ 1) → (∏ i in s, f i = 1 ↔ ∀ i ∈ s, f i = 1) := @prod_eq_one_iff_of_one_le' _ (order_dual N) _ _ _ @[to_additive single_le_sum] lemma single_le_prod' (hf : ∀ i ∈ s, 1 ≤ f i) {a} (h : a ∈ s) : f a ≤ (∏ x in s, f x) := calc f a = ∏ i in {a}, f i : prod_singleton.symm ... ≤ ∏ i in s, f i : prod_le_prod_of_subset_of_one_le' (singleton_subset_iff.2 h) $ λ i hi _, hf i hi variables {ι' : Type*} [decidable_eq ι'] @[to_additive sum_fiberwise_le_sum_of_sum_fiber_nonneg] lemma prod_fiberwise_le_prod_of_one_le_prod_fiber' {t : finset ι'} {g : ι → ι'} {f : ι → N} (h : ∀ y ∉ t, (1 : N) ≤ ∏ x in s.filter (λ x, g x = y), f x) : ∏ y in t, ∏ x in s.filter (λ x, g x = y), f x ≤ ∏ x in s, f x := calc (∏ y in t, ∏ x in s.filter (λ x, g x = y), f x) ≤ (∏ y in t ∪ s.image g, ∏ x in s.filter (λ x, g x = y), f x) : prod_le_prod_of_subset_of_one_le' (subset_union_left _ _) $ λ y hyts, h y ... = ∏ x in s, f x : prod_fiberwise_of_maps_to (λ x hx, mem_union.2 $ or.inr $ mem_image_of_mem _ hx) _ @[to_additive sum_le_sum_fiberwise_of_sum_fiber_nonpos] lemma prod_le_prod_fiberwise_of_prod_fiber_le_one' {t : finset ι'} {g : ι → ι'} {f : ι → N} (h : ∀ y ∉ t, (∏ x in s.filter (λ x, g x = y), f x) ≤ 1) : (∏ x in s, f x) ≤ ∏ y in t, ∏ x in s.filter (λ x, g x = y), f x := @prod_fiberwise_le_prod_of_one_le_prod_fiber' _ (order_dual N) _ _ _ _ _ _ _ h end lemma abs_sum_le_sum_abs {G : Type*} [linear_ordered_add_comm_group G] (f : ι → G) (s : finset ι) : abs (∑ i in s, f i) ≤ ∑ i in s, abs (f i) := le_sum_of_subadditive _ abs_zero abs_add s f lemma abs_prod {R : Type*} [linear_ordered_comm_ring R] {f : ι → R} {s : finset ι} : abs (∏ x in s, f x) = ∏ x in s, abs (f x) := (abs_hom.to_monoid_hom : R →* R).map_prod _ _ section pigeonhole variable [decidable_eq β] theorem card_le_mul_card_image_of_maps_to {f : α → β} {s : finset α} {t : finset β} (Hf : ∀ a ∈ s, f a ∈ t) (n : ℕ) (hn : ∀ a ∈ t, (s.filter (λ x, f x = a)).card ≤ n) : s.card ≤ n * t.card := calc s.card = (∑ a in t, (s.filter (λ x, f x = a)).card) : card_eq_sum_card_fiberwise Hf ... ≤ (∑ _ in t, n) : sum_le_sum hn ... = _ : by simp [mul_comm] theorem card_le_mul_card_image {f : α → β} (s : finset α) (n : ℕ) (hn : ∀ a ∈ s.image f, (s.filter (λ x, f x = a)).card ≤ n) : s.card ≤ n * (s.image f).card := card_le_mul_card_image_of_maps_to (λ x, mem_image_of_mem _) n hn theorem mul_card_image_le_card_of_maps_to {f : α → β} {s : finset α} {t : finset β} (Hf : ∀ a ∈ s, f a ∈ t) (n : ℕ) (hn : ∀ a ∈ t, n ≤ (s.filter (λ x, f x = a)).card) : n * t.card ≤ s.card := calc n * t.card = (∑ _ in t, n) : by simp [mul_comm] ... ≤ (∑ a in t, (s.filter (λ x, f x = a)).card) : sum_le_sum hn ... = s.card : by rw ← card_eq_sum_card_fiberwise Hf theorem mul_card_image_le_card {f : α → β} (s : finset α) (n : ℕ) (hn : ∀ a ∈ s.image f, n ≤ (s.filter (λ x, f x = a)).card) : n * (s.image f).card ≤ s.card := mul_card_image_le_card_of_maps_to (λ x, mem_image_of_mem _) n hn end pigeonhole section canonically_ordered_monoid variables [canonically_ordered_monoid M] {f : ι → M} {s t : finset ι} @[simp, to_additive sum_eq_zero_iff] lemma prod_eq_one_iff' : ∏ x in s, f x = 1 ↔ ∀ x ∈ s, f x = 1 := prod_eq_one_iff_of_one_le' $ λ x hx, one_le (f x) @[to_additive sum_le_sum_of_subset] lemma prod_le_prod_of_subset' (h : s ⊆ t) : ∏ x in s, f x ≤ ∏ x in t, f x := prod_le_prod_of_subset_of_one_le' h $ assume x h₁ h₂, one_le _ @[to_additive sum_mono_set] lemma prod_mono_set' (f : ι → M) : monotone (λ s, ∏ x in s, f x) := λ s₁ s₂ hs, prod_le_prod_of_subset' hs @[to_additive sum_le_sum_of_ne_zero] lemma prod_le_prod_of_ne_one' (h : ∀ x ∈ s, f x ≠ 1 → x ∈ t) : ∏ x in s, f x ≤ ∏ x in t, f x := by classical; calc ∏ x in s, f x = (∏ x in s.filter (λ x, f x = 1), f x) * ∏ x in s.filter (λ x, f x ≠ 1), f x : by rw [← prod_union, filter_union_filter_neg_eq]; exact disjoint_filter.2 (assume _ _ h n_h, n_h h) ... ≤ (∏ x in t, f x) : mul_le_of_le_one_of_le (prod_le_one' $ by simp only [mem_filter, and_imp]; exact λ _ _, le_of_eq) (prod_le_prod_of_subset' $ by simpa only [subset_iff, mem_filter, and_imp]) end canonically_ordered_monoid section ordered_cancel_comm_monoid variables [ordered_cancel_comm_monoid M] {f g : ι → M} {s t : finset ι} @[to_additive sum_lt_sum] theorem prod_lt_prod' (Hle : ∀ i ∈ s, f i ≤ g i) (Hlt : ∃ i ∈ s, f i < g i) : ∏ i in s, f i < ∏ i in s, g i := begin classical, rcases Hlt with ⟨i, hi, hlt⟩, rw [← insert_erase hi, prod_insert (not_mem_erase _ _), prod_insert (not_mem_erase _ _)], exact mul_lt_mul_of_lt_of_le hlt (prod_le_prod'' $ λ j hj, Hle j $ mem_of_mem_erase hj) end @[to_additive sum_lt_sum_of_nonempty] lemma prod_lt_prod_of_nonempty' (hs : s.nonempty) (Hlt : ∀ i ∈ s, f i < g i) : ∏ i in s, f i < ∏ i in s, g i := begin apply prod_lt_prod', { intros i hi, apply le_of_lt (Hlt i hi) }, cases hs with i hi, exact ⟨i, hi, Hlt i hi⟩, end @[to_additive sum_lt_sum_of_subset] lemma prod_lt_prod_of_subset' (h : s ⊆ t) {i : ι} (ht : i ∈ t) (hs : i ∉ s) (hlt : 1 < f i) (hle : ∀ j ∈ t, j ∉ s → 1 ≤ f j) : ∏ j in s, f j < ∏ j in t, f j := by classical; calc ∏ j in s, f j < ∏ j in insert i s, f j : begin rw prod_insert hs, exact lt_mul_of_one_lt_left' (∏ j in s, f j) hlt, end ... ≤ ∏ j in t, f j : begin apply prod_le_prod_of_subset_of_one_le', { simp [finset.insert_subset, h, ht] }, { assume x hx h'x, simp only [mem_insert, not_or_distrib] at h'x, exact hle x hx h'x.2 } end @[to_additive single_lt_sum] lemma single_lt_prod' {i j : ι} (hij : j ≠ i) (hi : i ∈ s) (hj : j ∈ s) (hlt : 1 < f j) (hle : ∀ k ∈ s, k ≠ i → 1 ≤ f k) : f i < ∏ k in s, f k := calc f i = ∏ k in {i}, f k : prod_singleton.symm ... < ∏ k in s, f k : prod_lt_prod_of_subset' (singleton_subset_iff.2 hi) hj (mt mem_singleton.1 hij) hlt $ λ k hks hki, hle k hks (mt mem_singleton.2 hki) end ordered_cancel_comm_monoid section linear_ordered_cancel_comm_monoid variables [linear_ordered_cancel_comm_monoid M] {f g : ι → M} {s t : finset ι} @[to_additive exists_lt_of_sum_lt] theorem exists_lt_of_prod_lt' (Hlt : ∏ i in s, f i < ∏ i in s, g i) : ∃ i ∈ s, f i < g i := begin contrapose! Hlt with Hle, exact prod_le_prod'' Hle end @[to_additive exists_le_of_sum_le] theorem exists_le_of_prod_le' (hs : s.nonempty) (Hle : ∏ i in s, f i ≤ ∏ i in s, g i) : ∃ i ∈ s, f i ≤ g i := begin contrapose! Hle with Hlt, exact prod_lt_prod_of_nonempty' hs Hlt end @[to_additive exists_pos_of_sum_zero_of_exists_nonzero] lemma exists_one_lt_of_prod_one_of_exists_ne_one' (f : ι → M) (h₁ : ∏ i in s, f i = 1) (h₂ : ∃ i ∈ s, f i ≠ 1) : ∃ i ∈ s, 1 < f i := begin contrapose! h₁, obtain ⟨i, m, i_ne⟩ : ∃ i ∈ s, f i ≠ 1 := h₂, apply ne_of_lt, calc ∏ j in s, f j < ∏ j in s, 1 : prod_lt_prod' h₁ ⟨i, m, (h₁ i m).lt_of_ne i_ne⟩ ... = 1 : prod_const_one end end linear_ordered_cancel_comm_monoid section ordered_comm_semiring variables [ordered_comm_semiring R] {f g : ι → R} {s t : finset ι} open_locale classical /- this is also true for a ordered commutative multiplicative monoid -/ lemma prod_nonneg (h0 : ∀ i ∈ s, 0 ≤ f i) : 0 ≤ ∏ i in s, f i := prod_induction f (λ i, 0 ≤ i) (λ _ _ ha hb, mul_nonneg ha hb) zero_le_one h0 /- this is also true for a ordered commutative multiplicative monoid -/ lemma prod_pos [nontrivial R] (h0 : ∀ i ∈ s, 0 < f i) : 0 < ∏ i in s, f i := prod_induction f (λ x, 0 < x) (λ _ _ ha hb, mul_pos ha hb) zero_lt_one h0 /-- If all `f i`, `i ∈ s`, are nonnegative and each `f i` is less than or equal to `g i`, then the product of `f i` is less than or equal to the product of `g i`. See also `finset.prod_le_prod''` for the case of an ordered commutative multiplicative monoid. -/ lemma prod_le_prod (h0 : ∀ i ∈ s, 0 ≤ f i) (h1 : ∀ i ∈ s, f i ≤ g i) : ∏ i in s, f i ≤ ∏ i in s, g i := begin induction s using finset.induction with a s has ih h, { simp }, { simp only [prod_insert has], apply mul_le_mul, { exact h1 a (mem_insert_self a s) }, { apply ih (λ x H, h0 _ _) (λ x H, h1 _ _); exact (mem_insert_of_mem H) }, { apply prod_nonneg (λ x H, h0 x (mem_insert_of_mem H)) }, { apply le_trans (h0 a (mem_insert_self a s)) (h1 a (mem_insert_self a s)) } } end /-- If each `f i`, `i ∈ s` belongs to `[0, 1]`, then their product is less than or equal to one. See also `finset.prod_le_one'` for the case of an ordered commutative multiplicative monoid. -/ lemma prod_le_one (h0 : ∀ i ∈ s, 0 ≤ f i) (h1 : ∀ i ∈ s, f i ≤ 1) : ∏ i in s, f i ≤ 1 := begin convert ← prod_le_prod h0 h1, exact finset.prod_const_one end /-- If `g, h ≤ f` and `g i + h i ≤ f i`, then the product of `f` over `s` is at least the sum of the products of `g` and `h`. This is the version for `ordered_comm_semiring`. -/ lemma prod_add_prod_le {i : ι} {f g h : ι → R} (hi : i ∈ s) (h2i : g i + h i ≤ f i) (hgf : ∀ j ∈ s, j ≠ i → g j ≤ f j) (hhf : ∀ j ∈ s, j ≠ i → h j ≤ f j) (hg : ∀ i ∈ s, 0 ≤ g i) (hh : ∀ i ∈ s, 0 ≤ h i) : ∏ i in s, g i + ∏ i in s, h i ≤ ∏ i in s, f i := begin simp_rw [prod_eq_mul_prod_diff_singleton hi], refine le_trans _ (mul_le_mul_of_nonneg_right h2i _), { rw [right_distrib], apply add_le_add; apply mul_le_mul_of_nonneg_left; try { apply_assumption; assumption }; apply prod_le_prod; simp * { contextual := tt } }, { apply prod_nonneg, simp only [and_imp, mem_sdiff, mem_singleton], intros j h1j h2j, exact le_trans (hg j h1j) (hgf j h1j h2j) } end end ordered_comm_semiring section canonically_ordered_comm_semiring variables [canonically_ordered_comm_semiring R] {f g h : ι → R} {s : finset ι} {i : ι} lemma prod_le_prod' (h : ∀ i ∈ s, f i ≤ g i) : ∏ i in s, f i ≤ ∏ i in s, g i := begin classical, induction s using finset.induction with a s has ih h, { simp }, { rw [finset.prod_insert has, finset.prod_insert has], apply canonically_ordered_semiring.mul_le_mul, { exact h _ (finset.mem_insert_self a s) }, { exact ih (λ i hi, h _ (finset.mem_insert_of_mem hi)) } } end /-- If `g, h ≤ f` and `g i + h i ≤ f i`, then the product of `f` over `s` is at least the sum of the products of `g` and `h`. This is the version for `canonically_ordered_comm_semiring`. -/ lemma prod_add_prod_le' (hi : i ∈ s) (h2i : g i + h i ≤ f i) (hgf : ∀ j ∈ s, j ≠ i → g j ≤ f j) (hhf : ∀ j ∈ s, j ≠ i → h j ≤ f j) : ∏ i in s, g i + ∏ i in s, h i ≤ ∏ i in s, f i := begin classical, simp_rw [prod_eq_mul_prod_diff_singleton hi], refine le_trans _ (canonically_ordered_semiring.mul_le_mul_right' h2i _), rw [right_distrib], apply add_le_add; apply canonically_ordered_semiring.mul_le_mul_left'; apply prod_le_prod'; simp only [and_imp, mem_sdiff, mem_singleton]; intros; apply_assumption; assumption end end canonically_ordered_comm_semiring end finset namespace fintype variables [fintype ι] @[mono, to_additive sum_mono] lemma prod_mono' [ordered_comm_monoid M] : monotone (λ f : ι → M, ∏ i, f i) := λ f g hfg, finset.prod_le_prod'' $ λ x _, hfg x @[to_additive sum_strict_mono] lemma prod_strict_mono' [ordered_cancel_comm_monoid M] : strict_mono (λ f : ι → M, ∏ x, f x) := λ f g hfg, let ⟨hle, i, hlt⟩ := pi.lt_def.mp hfg in finset.prod_lt_prod' (λ i _, hle i) ⟨i, finset.mem_univ i, hlt⟩ end fintype namespace with_top open finset /-- A product of finite numbers is still finite -/ lemma prod_lt_top [canonically_ordered_comm_semiring R] [nontrivial R] [decidable_eq R] {s : finset ι} {f : ι → with_top R} (h : ∀ i ∈ s, f i < ⊤) : ∏ i in s, f i < ⊤ := prod_induction f (λ a, a < ⊤) (λ a b, mul_lt_top) (coe_lt_top 1) h /-- A sum of finite numbers is still finite -/ lemma sum_lt_top [ordered_add_comm_monoid M] {s : finset ι} {f : ι → with_top M} : (∀ i ∈ s, f i < ⊤) → (∑ i in s, f i) < ⊤ := sum_induction f (λ a, a < ⊤) (by { simp_rw add_lt_top, tauto }) zero_lt_top /-- A sum of numbers is infinite iff one of them is infinite -/ lemma sum_eq_top_iff [ordered_add_comm_monoid M] {s : finset ι} {f : ι → with_top M} : ∑ i in s, f i = ⊤ ↔ ∃ i ∈ s, f i = ⊤ := begin classical, split, { contrapose!, exact λ h, (sum_lt_top $ λ i hi, lt_top_iff_ne_top.2 (h i hi)).ne }, { rintro ⟨i, his, hi⟩, rw [sum_eq_add_sum_diff_singleton his, hi, top_add] } end /-- A sum of finite numbers is still finite -/ lemma sum_lt_top_iff [ordered_add_comm_monoid M] {s : finset ι} {f : ι → with_top M} : ∑ i in s, f i < ⊤ ↔ ∀ i ∈ s, f i < ⊤ := by simp only [lt_top_iff_ne_top, ne.def, sum_eq_top_iff, not_exists] end with_top
82f524d0d6e08b5419b2f0716f3bc10a32443ef1
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/ctx.lean
21acb10f1e37e06a579198208ad986bffb64140b
[ "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
158
lean
open prod universe variables u definition foo {A B : Type u} (a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 : nat) (b1 b2 b3 : bool) (h : A = B) (p1 p2 : A × B) : nat := _
2a638696436cce3f128370165a30d4016b67f18a
159fed64bfae88f3b6a6166836d6278f953bcbf9
/Structure/Generic/Axioms/DependentGeneralizedProperties.lean
cf4875b9e62df8f96485fb517399f4e0f8083b2d
[ "MIT" ]
permissive
SReichelt/lean4-experiments
3e56830c8b2fbe3814eda071c48e3c8810d254a8
ff55357a01a34a91bf670d712637480089085ee4
refs/heads/main
1,683,977,454,907
1,622,991,121,000
1,622,991,121,000
340,765,677
2
0
null
null
null
null
UTF-8
Lean
false
false
6,242
lean
import Structure.Generic.Axioms.Universes import Structure.Generic.Axioms.AbstractFunctors import Structure.Generic.Axioms.AbstractEquivalences import Structure.Generic.Axioms.GeneralizedProperties import Structure.Generic.Notation open GeneralizedProperty open GeneralizedRelation set_option autoBoundImplicitLocal false --set_option pp.universes true def GeneralizedDependentProperty {U V : Universe} (P : GeneralizedProperty ⌈U⌉ V) (W : Universe) := ∀ {α}, P α → (α → W) namespace GeneralizedDependentProperty variable {U V : Universe} {P : GeneralizedProperty ⌈U⌉ V} {W : Universe} section Properties variable (D : GeneralizedDependentProperty P W) class HasDependentInst [h : HasInst P] where (inst {α : U} (a : α) : D (h.inst α) a) def instProp [h : HasInst P] (α : U) : GeneralizedProperty ⌈α⌉ W := D (h.inst α) instance [HasInst P] [h : HasDependentInst D] (α : U) : HasInst (instProp D α) := ⟨h.inst⟩ end Properties end GeneralizedDependentProperty open GeneralizedDependentProperty def GeneralizedDependentRelation {U V : Universe} (R : GeneralizedRelation ⌈U⌉ V) (W : Universe) := ∀ {α β}, R α β → (α → β → W) namespace GeneralizedDependentRelation variable {U V : Universe} {R : GeneralizedRelation ⌈U⌉ V} {W : Universe} section Properties variable (D : GeneralizedDependentRelation R W) class HasDependentRefl [h : HasRefl R] where (refl {α : U} (a : α) : D (h.refl α) a a) def reflRel [h : HasRefl R] (α : U) : GeneralizedRelation ⌈α⌉ W := D (h.refl α) instance [HasRefl R] [h : HasDependentRefl D] (α : U) : HasRefl (reflRel D α) := ⟨h.refl⟩ variable [HasInternalFunctors V] [HasInternalFunctors W] class HasDependentTrans [h : HasTrans R] where (trans {α β γ : U} {F : R α β} {G : R β γ} {a : α} {b : β} {c : γ} : D F a b ⟶ D G b c ⟶ D (h.trans' F G) a c) class IsDependentPreorder [h : IsPreorder R] extends HasDependentRefl D, HasDependentTrans D variable [HasInternalEquivalences V] [HasInternalEquivalences W] class HasDependentSymm [h : HasSymm R] where (symm {α β : U} {F : R α β} {a : α} {b : β} : D F a b ⟷ D (h.symm' F) b a) class IsDependentEquivalence [h : IsEquivalence R] extends IsDependentPreorder D, HasDependentSymm D end Properties def HasDependentTrans.trans' {D : GeneralizedDependentRelation R W} [HasInternalFunctors V] [HasInternalFunctors W] [HasTrans R] [h : HasDependentTrans D] {α β γ : U} {F : R α β} {G : R β γ} {a : α} {b : β} {c : γ} (f : D F a b) (g : D G b c) : D (HasTrans.trans' F G) a c := h.trans f g def HasDependentSymm.symm' {D : GeneralizedDependentRelation R W} [HasInternalFunctors V] [HasInternalFunctors W] [HasInternalEquivalences V] [HasInternalEquivalences W] [HasSymm R] [h : HasDependentSymm D] {α β : U} {F : R α β} {a : α} {b : β} (f : D F a b) : D (HasSymm.symm' F) b a := HasInternalEquivalences.to h.symm f section Notation @[reducible] def depRevComp {D : GeneralizedDependentRelation R W} [HasInternalFunctors V] [HasInternalFunctors W] [HasTrans R] [h : HasDependentTrans D] {α β γ : U} {F : R α β} {G : R β γ} {a : α} {b : β} {c : γ} (g : D G b c) (f : D F a b) : D (G • F) a c := h.trans' f g -- TODO: What is the correct way to overload this? --infixr:90 " • " => depRevComp @[reducible] def depIdent (D : GeneralizedDependentRelation R W) [HasRefl R] [h : HasDependentRefl D] {α : U} (a : α) : D (ident R α) a a := h.refl a @[reducible] def depInv {D : GeneralizedDependentRelation R W} [HasInternalFunctors V] [HasInternalFunctors W] [HasInternalEquivalences V] [HasInternalEquivalences W] [HasSymm R] [h : HasDependentSymm D] {α β : U} {F : R α β} {a : α} {b : β} (f : D F a b) : D F⁻¹ b a := h.symm' f --postfix:max "⁻¹" => depInv end Notation end GeneralizedDependentRelation open GeneralizedDependentRelation section AttachedDependentRelations variable (U V W : Universe) [HasInternalFunctors V] [HasInternalFunctors W] class HasDependentArrows [h : HasArrows ⌈U⌉ V] where (Arrow : GeneralizedDependentRelation h.Arrow W) [isPreorder : IsDependentPreorder Arrow] namespace HasDependentArrows variable [HasArrows ⌈U⌉ V] [h : HasDependentArrows U V W] instance arrowPreorder : IsDependentPreorder h.Arrow := h.isPreorder notation:20 a:21 " ⇝[" F:0 "] " b:21 => HasDependentArrows.Arrow F a b notation:20 a:21 " ⇝[" F:0 "," V':0 "," W':0 "] " b:21 => HasDependentArrows.Arrow (V := V') (W := W') F a b end HasDependentArrows variable [HasInternalEquivalences V] [HasInternalEquivalences W] class HasDependentEquivalences [h : HasEquivalences ⌈U⌉ V] where (Equiv : GeneralizedDependentRelation h.Equiv W) [isEquiv : IsDependentEquivalence Equiv] namespace HasDependentEquivalences variable [HasEquivalences ⌈U⌉ V] [h : HasDependentEquivalences U V W] instance equivEquivalence : IsDependentEquivalence h.Equiv := h.isEquiv notation:25 a:26 " ≃[" F:0 "] " b:26 => HasDependentEquivalences.Equiv F a b notation:25 a:26 " ≃[" F:0 "," V':0 "," W':0 "] " b:26 => HasDependentEquivalences.Equiv (V := V') (W := W') F a b end HasDependentEquivalences class HasDependentProducts [h : HasProducts ⌈U⌉ V] where (Product : GeneralizedDependentRelation h.Product W) [hasSymm : HasDependentSymm Product] namespace HasDependentProducts variable [HasProducts ⌈U⌉ V] [h : HasDependentProducts U V W] instance productSymm : HasDependentSymm h.Product := h.hasSymm notation:35 a:36 " ⧆[" P:0 "] " b:36 => HasDependentProducts.Product P a b notation:35 a:36 " ⧆[" P:0 "," V':0 "," W':0 "] " b:36 => HasDependentProducts.Product (V := V') (W := W') P a b end HasDependentProducts end AttachedDependentRelations
1a93d1893bded1fac7f316dedef15b602a043196
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/topology/algebra/ordered.lean
cf7e05e4db6507cb809e00424741c9bf6a8c9ba3
[]
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
159,950
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Yury Kudryashov -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.tactic.linarith.default import Mathlib.tactic.tfae import Mathlib.algebra.archimedean import Mathlib.algebra.group.pi import Mathlib.algebra.ordered_ring import Mathlib.order.liminf_limsup import Mathlib.data.set.intervals.image_preimage import Mathlib.data.set.intervals.ord_connected import Mathlib.data.set.intervals.surj_on import Mathlib.data.set.intervals.pi import Mathlib.topology.algebra.group import Mathlib.topology.extend_from_subset import Mathlib.order.filter.interval import Mathlib.PostPort universes u_1 l u v w u_2 namespace Mathlib /-! # Theory of topology on ordered spaces ## Main definitions The order topology on an ordered space is the topology generated by all open intervals (or equivalently by those of the form `(-∞, a)` and `(b, +∞)`). We define it as `preorder.topology α`. However, we do *not* register it as an instance (as many existing ordered types already have topologies, which would be equal but not definitionally equal to `preorder.topology α`). Instead, we introduce a class `order_topology α`(which is a `Prop`, also known as a mixin) saying that on the type `α` having already a topological space structure and a preorder structure, the topological structure is equal to the order topology. We also introduce another (mixin) class `order_closed_topology α` saying that the set of points `(x, y)` with `x ≤ y` is closed in the product space. This is automatically satisfied on a linear order with the order topology. We prove many basic properties of such topologies. ## Main statements This file contains the proofs of the following facts. For exact requirements (`order_closed_topology` vs `order_topology`, `preorder` vs `partial_order` vs `linear_order` etc) see their statements. ### Open / closed sets * `is_open_lt` : if `f` and `g` are continuous functions, then `{x | f x < g x}` is open; * `is_open_Iio`, `is_open_Ioi`, `is_open_Ioo` : open intervals are open; * `is_closed_le` : if `f` and `g` are continuous functions, then `{x | f x ≤ g x}` is closed; * `is_closed_Iic`, `is_closed_Ici`, `is_closed_Icc` : closed intervals are closed; * `frontier_le_subset_eq`, `frontier_lt_subset_eq` : frontiers of both `{x | f x ≤ g x}` and `{x | f x < g x}` are included by `{x | f x = g x}`; * `exists_Ioc_subset_of_mem_nhds`, `exists_Ico_subset_of_mem_nhds` : if `x < y`, then any neighborhood of `x` includes an interval `[x, z)` for some `z ∈ (x, y]`, and any neighborhood of `y` includes an interval `(z, y]` for some `z ∈ [x, y)`. ### Convergence and inequalities * `le_of_tendsto_of_tendsto` : if `f` converges to `a`, `g` converges to `b`, and eventually `f x ≤ g x`, then `a ≤ b` * `le_of_tendsto`, `ge_of_tendsto` : if `f` converges to `a` and eventually `f x ≤ b` (resp., `b ≤ f x`), then `a ≤ b` (resp., `b ≤ a); we also provide primed versions that assume the inequalities to hold for all `x`. ### Min, max, `Sup` and `Inf` * `continuous.min`, `continuous.max`: pointwise `min`/`max` of two continuous functions is continuous. * `tendsto.min`, `tendsto.max` : if `f` tends to `a` and `g` tends to `b`, then their pointwise `min`/`max` tend to `min a b` and `max a b`, respectively. * `tendsto_of_tendsto_of_tendsto_of_le_of_le` : theorem known as squeeze theorem, sandwich theorem, theorem of Carabinieri, and two policemen (and a drunk) theorem; if `g` and `h` both converge to `a`, and eventually `g x ≤ f x ≤ h x`, then `f` converges to `a`. ### Connected sets and Intermediate Value Theorem * `is_preconnected_I??` : all intervals `I??` are preconnected, * `is_preconnected.intermediate_value`, `intermediate_value_univ` : Intermediate Value Theorem for connected sets and connected spaces, respectively; * `intermediate_value_Icc`, `intermediate_value_Icc'`: Intermediate Value Theorem for functions on closed intervals. ### Miscellaneous facts * `is_compact.exists_forall_le`, `is_compact.exists_forall_ge` : extreme value theorem, a continuous function on a compact set takes its minimum and maximum values. * `is_closed.Icc_subset_of_forall_mem_nhds_within` : “Continuous induction” principle; if `s ∩ [a, b]` is closed, `a ∈ s`, and for each `x ∈ [a, b) ∩ s` some of its right neighborhoods is included `s`, then `[a, b] ⊆ s`. * `is_closed.Icc_subset_of_forall_exists_gt`, `is_closed.mem_of_ge_of_forall_exists_gt` : two other versions of the “continuous induction” principle. ## Implementation We do _not_ register the order topology as an instance on a preorder (or even on a linear order). Indeed, on many such spaces, a topology has already been constructed in a different way (think of the discrete spaces `ℕ` or `ℤ`, or `ℝ` that could inherit a topology as the completion of `ℚ`), and is in general not defeq to the one generated by the intervals. We make it available as a definition `preorder.topology α` though, that can be registered as an instance when necessary, or for specific types. -/ /-- A topology on a set which is both a topological space and a preorder is _order-closed_ if the set of points `(x, y)` with `x ≤ y` is closed in the product space. We introduce this as a mixin. This property is satisfied for the order topology on a linear order, but it can be satisfied more generally, and suffices to derive many interesting properties relating order and topology. -/ class order_closed_topology (α : Type u_1) [topological_space α] [preorder α] where is_closed_le' : is_closed (set_of fun (p : α × α) => prod.fst p ≤ prod.snd p) protected instance order_dual.topological_space {α : Type u} [topological_space α] : topological_space (order_dual α) := id theorem is_closed_le_prod {α : Type u} [topological_space α] [preorder α] [t : order_closed_topology α] : is_closed (set_of fun (p : α × α) => prod.fst p ≤ prod.snd p) := order_closed_topology.is_closed_le' theorem is_closed_le {α : Type u} {β : Type v} [topological_space α] [preorder α] [t : order_closed_topology α] [topological_space β] {f : β → α} {g : β → α} (hf : continuous f) (hg : continuous g) : is_closed (set_of fun (b : β) => f b ≤ g b) := iff.mp continuous_iff_is_closed (continuous.prod_mk hf hg) (set_of fun (p : α × α) => prod.fst p ≤ prod.snd p) is_closed_le_prod theorem is_closed_le' {α : Type u} [topological_space α] [preorder α] [t : order_closed_topology α] (a : α) : is_closed (set_of fun (b : α) => b ≤ a) := is_closed_le continuous_id continuous_const theorem is_closed_Iic {α : Type u} [topological_space α] [preorder α] [t : order_closed_topology α] {a : α} : is_closed (set.Iic a) := is_closed_le' a theorem is_closed_ge' {α : Type u} [topological_space α] [preorder α] [t : order_closed_topology α] (a : α) : is_closed (set_of fun (b : α) => a ≤ b) := is_closed_le continuous_const continuous_id theorem is_closed_Ici {α : Type u} [topological_space α] [preorder α] [t : order_closed_topology α] {a : α} : is_closed (set.Ici a) := is_closed_ge' a protected instance order_dual.order_closed_topology {α : Type u} [topological_space α] [preorder α] [t : order_closed_topology α] : order_closed_topology (order_dual α) := order_closed_topology.mk (is_closed.preimage continuous_swap order_closed_topology.is_closed_le') theorem is_closed_Icc {α : Type u} [topological_space α] [preorder α] [t : order_closed_topology α] {a : α} {b : α} : is_closed (set.Icc a b) := is_closed_inter is_closed_Ici is_closed_Iic @[simp] theorem closure_Icc {α : Type u} [topological_space α] [preorder α] [t : order_closed_topology α] (a : α) (b : α) : closure (set.Icc a b) = set.Icc a b := is_closed.closure_eq is_closed_Icc @[simp] theorem closure_Iic {α : Type u} [topological_space α] [preorder α] [t : order_closed_topology α] (a : α) : closure (set.Iic a) = set.Iic a := is_closed.closure_eq is_closed_Iic @[simp] theorem closure_Ici {α : Type u} [topological_space α] [preorder α] [t : order_closed_topology α] (a : α) : closure (set.Ici a) = set.Ici a := is_closed.closure_eq is_closed_Ici theorem le_of_tendsto_of_tendsto {α : Type u} {β : Type v} [topological_space α] [preorder α] [t : order_closed_topology α] {f : β → α} {g : β → α} {b : filter β} {a₁ : α} {a₂ : α} [filter.ne_bot b] (hf : filter.tendsto f b (nhds a₁)) (hg : filter.tendsto g b (nhds a₂)) (h : filter.eventually_le b f g) : a₁ ≤ a₂ := sorry theorem le_of_tendsto_of_tendsto' {α : Type u} {β : Type v} [topological_space α] [preorder α] [t : order_closed_topology α] {f : β → α} {g : β → α} {b : filter β} {a₁ : α} {a₂ : α} [filter.ne_bot b] (hf : filter.tendsto f b (nhds a₁)) (hg : filter.tendsto g b (nhds a₂)) (h : ∀ (x : β), f x ≤ g x) : a₁ ≤ a₂ := le_of_tendsto_of_tendsto hf hg (filter.eventually_of_forall h) theorem le_of_tendsto {α : Type u} {β : Type v} [topological_space α] [preorder α] [t : order_closed_topology α] {f : β → α} {a : α} {b : α} {x : filter β} [filter.ne_bot x] (lim : filter.tendsto f x (nhds a)) (h : filter.eventually (fun (c : β) => f c ≤ b) x) : a ≤ b := le_of_tendsto_of_tendsto lim tendsto_const_nhds h theorem le_of_tendsto' {α : Type u} {β : Type v} [topological_space α] [preorder α] [t : order_closed_topology α] {f : β → α} {a : α} {b : α} {x : filter β} [filter.ne_bot x] (lim : filter.tendsto f x (nhds a)) (h : ∀ (c : β), f c ≤ b) : a ≤ b := le_of_tendsto lim (filter.eventually_of_forall h) theorem ge_of_tendsto {α : Type u} {β : Type v} [topological_space α] [preorder α] [t : order_closed_topology α] {f : β → α} {a : α} {b : α} {x : filter β} [filter.ne_bot x] (lim : filter.tendsto f x (nhds a)) (h : filter.eventually (fun (c : β) => b ≤ f c) x) : b ≤ a := le_of_tendsto_of_tendsto tendsto_const_nhds lim h theorem ge_of_tendsto' {α : Type u} {β : Type v} [topological_space α] [preorder α] [t : order_closed_topology α] {f : β → α} {a : α} {b : α} {x : filter β} [filter.ne_bot x] (lim : filter.tendsto f x (nhds a)) (h : ∀ (c : β), b ≤ f c) : b ≤ a := ge_of_tendsto lim (filter.eventually_of_forall h) @[simp] theorem closure_le_eq {α : Type u} {β : Type v} [topological_space α] [preorder α] [t : order_closed_topology α] [topological_space β] {f : β → α} {g : β → α} (hf : continuous f) (hg : continuous g) : closure (set_of fun (b : β) => f b ≤ g b) = set_of fun (b : β) => f b ≤ g b := is_closed.closure_eq (is_closed_le hf hg) theorem closure_lt_subset_le {α : Type u} {β : Type v} [topological_space α] [preorder α] [t : order_closed_topology α] [topological_space β] {f : β → α} {g : β → α} (hf : continuous f) (hg : continuous g) : closure (set_of fun (b : β) => f b < g b) ⊆ set_of fun (b : β) => f b ≤ g b := sorry theorem continuous_within_at.closure_le {α : Type u} {β : Type v} [topological_space α] [preorder α] [t : order_closed_topology α] [topological_space β] {f : β → α} {g : β → α} {s : set β} {x : β} (hx : x ∈ closure s) (hf : continuous_within_at f s x) (hg : continuous_within_at g s x) (h : ∀ (y : β), y ∈ s → f y ≤ g y) : f x ≤ g x := (fun (this : (f x, g x) ∈ set_of fun (p : α × α) => prod.fst p ≤ prod.snd p) => this) (is_closed.closure_subset order_closed_topology.is_closed_le' (continuous_within_at.mem_closure (continuous_within_at.prod hf hg) hx h)) /-- If `s` is a closed set and two functions `f` and `g` are continuous on `s`, then the set `{x ∈ s | f x ≤ g x}` is a closed set. -/ theorem is_closed.is_closed_le {α : Type u} {β : Type v} [topological_space α] [preorder α] [t : order_closed_topology α] [topological_space β] {f : β → α} {g : β → α} {s : set β} (hs : is_closed s) (hf : continuous_on f s) (hg : continuous_on g s) : is_closed (has_sep.sep (fun (x : β) => f x ≤ g x) s) := continuous_on.preimage_closed_of_closed (continuous_on.prod hf hg) hs order_closed_topology.is_closed_le' theorem nhds_within_Ici_ne_bot {α : Type u} [topological_space α] [preorder α] {a : α} {b : α} (H₂ : a ≤ b) : filter.ne_bot (nhds_within b (set.Ici a)) := nhds_within_ne_bot_of_mem H₂ instance nhds_within_Ici_self_ne_bot {α : Type u} [topological_space α] [preorder α] (a : α) : filter.ne_bot (nhds_within a (set.Ici a)) := nhds_within_Ici_ne_bot (le_refl a) theorem nhds_within_Iic_ne_bot {α : Type u} [topological_space α] [preorder α] {a : α} {b : α} (H : a ≤ b) : filter.ne_bot (nhds_within a (set.Iic b)) := nhds_within_ne_bot_of_mem H instance nhds_within_Iic_self_ne_bot {α : Type u} [topological_space α] [preorder α] (a : α) : filter.ne_bot (nhds_within a (set.Iic a)) := nhds_within_Iic_ne_bot (le_refl a) protected instance order_closed_topology.to_t2_space {α : Type u} [topological_space α] [partial_order α] [t : order_closed_topology α] : t2_space α := t2_space.mk ((fun (this : is_open (set_of fun (p : α × α) => prod.fst p ≠ prod.snd p)) (a b : α) (h : a ≠ b) => sorry) is_closed_eq) theorem is_open_lt_prod {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] : is_open (set_of fun (p : α × α) => prod.fst p < prod.snd p) := sorry theorem is_open_lt {α : Type u} {β : Type v} [topological_space α] [linear_order α] [order_closed_topology α] [topological_space β] {f : β → α} {g : β → α} (hf : continuous f) (hg : continuous g) : is_open (set_of fun (b : β) => f b < g b) := sorry theorem is_open_Iio {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} : is_open (set.Iio a) := is_open_lt continuous_id continuous_const theorem is_open_Ioi {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} : is_open (set.Ioi a) := is_open_lt continuous_const continuous_id theorem is_open_Ioo {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} {b : α} : is_open (set.Ioo a b) := is_open_inter is_open_Ioi is_open_Iio @[simp] theorem interior_Ioi {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} : interior (set.Ioi a) = set.Ioi a := is_open.interior_eq is_open_Ioi @[simp] theorem interior_Iio {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} : interior (set.Iio a) = set.Iio a := is_open.interior_eq is_open_Iio @[simp] theorem interior_Ioo {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} {b : α} : interior (set.Ioo a b) = set.Ioo a b := is_open.interior_eq is_open_Ioo /-- Intermediate value theorem for two functions: if `f` and `g` are two continuous functions on a preconnected space and `f a ≤ g a` and `g b ≤ f b`, then for some `x` we have `f x = g x`. -/ theorem intermediate_value_univ₂ {α : Type u} {γ : Type w} [topological_space α] [linear_order α] [order_closed_topology α] [topological_space γ] [preconnected_space γ] {a : γ} {b : γ} {f : γ → α} {g : γ → α} (hf : continuous f) (hg : continuous g) (ha : f a ≤ g a) (hb : g b ≤ f b) : ∃ (x : γ), f x = g x := sorry /-- Intermediate value theorem for two functions: if `f` and `g` are two functions continuous on a preconnected set `s` and for some `a b ∈ s` we have `f a ≤ g a` and `g b ≤ f b`, then for some `x ∈ s` we have `f x = g x`. -/ theorem is_preconnected.intermediate_value₂ {α : Type u} {γ : Type w} [topological_space α] [linear_order α] [order_closed_topology α] [topological_space γ] {s : set γ} (hs : is_preconnected s) {a : γ} {b : γ} (ha : a ∈ s) (hb : b ∈ s) {f : γ → α} {g : γ → α} (hf : continuous_on f s) (hg : continuous_on g s) (ha' : f a ≤ g a) (hb' : g b ≤ f b) : ∃ (x : γ), ∃ (H : x ∈ s), f x = g x := sorry /-- Intermediate Value Theorem for continuous functions on connected sets. -/ theorem is_preconnected.intermediate_value {α : Type u} {γ : Type w} [topological_space α] [linear_order α] [order_closed_topology α] [topological_space γ] {s : set γ} (hs : is_preconnected s) {a : γ} {b : γ} (ha : a ∈ s) (hb : b ∈ s) {f : γ → α} (hf : continuous_on f s) : set.Icc (f a) (f b) ⊆ f '' s := fun (x : α) (hx : x ∈ set.Icc (f a) (f b)) => iff.mpr set.mem_image_iff_bex (is_preconnected.intermediate_value₂ hs ha hb hf continuous_on_const (and.left hx) (and.right hx)) /-- Intermediate Value Theorem for continuous functions on connected spaces. -/ theorem intermediate_value_univ {α : Type u} {γ : Type w} [topological_space α] [linear_order α] [order_closed_topology α] [topological_space γ] [preconnected_space γ] (a : γ) (b : γ) {f : γ → α} (hf : continuous f) : set.Icc (f a) (f b) ⊆ set.range f := fun (x : α) (hx : x ∈ set.Icc (f a) (f b)) => intermediate_value_univ₂ hf continuous_const (and.left hx) (and.right hx) /-- Intermediate Value Theorem for continuous functions on connected spaces. -/ theorem mem_range_of_exists_le_of_exists_ge {α : Type u} {γ : Type w} [topological_space α] [linear_order α] [order_closed_topology α] [topological_space γ] [preconnected_space γ] {c : α} {f : γ → α} (hf : continuous f) (h₁ : ∃ (a : γ), f a ≤ c) (h₂ : ∃ (b : γ), c ≤ f b) : c ∈ set.range f := sorry /-- If a preconnected set contains endpoints of an interval, then it includes the whole interval. -/ theorem is_preconnected.Icc_subset {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {s : set α} (hs : is_preconnected s) {a : α} {b : α} (ha : a ∈ s) (hb : b ∈ s) : set.Icc a b ⊆ s := sorry /-- If a preconnected set contains endpoints of an interval, then it includes the whole interval. -/ theorem is_connected.Icc_subset {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {s : set α} (hs : is_connected s) {a : α} {b : α} (ha : a ∈ s) (hb : b ∈ s) : set.Icc a b ⊆ s := is_preconnected.Icc_subset (and.right hs) ha hb /-- If preconnected set in a linear order space is unbounded below and above, then it is the whole space. -/ theorem is_preconnected.eq_univ_of_unbounded {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {s : set α} (hs : is_preconnected s) (hb : ¬bdd_below s) (ha : ¬bdd_above s) : s = set.univ := sorry /-! ### Neighborhoods to the left and to the right on an `order_closed_topology` Limits to the left and to the right of real functions are defined in terms of neighborhoods to the left and to the right, either open or closed, i.e., members of `𝓝[Ioi a] a` and `𝓝[Ici a] a` on the right, and similarly on the left. Here we simply prove that all right-neighborhoods of a point are equal, and we'll prove later other useful characterizations which require the stronger hypothesis `order_topology α` -/ /-! #### Right neighborhoods, point excluded -/ theorem Ioo_mem_nhds_within_Ioi {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} {b : α} {c : α} (H : b ∈ set.Ico a c) : set.Ioo a c ∈ nhds_within b (set.Ioi b) := sorry theorem Ioc_mem_nhds_within_Ioi {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} {b : α} {c : α} (H : b ∈ set.Ico a c) : set.Ioc a c ∈ nhds_within b (set.Ioi b) := filter.mem_sets_of_superset (Ioo_mem_nhds_within_Ioi H) set.Ioo_subset_Ioc_self theorem Ico_mem_nhds_within_Ioi {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} {b : α} {c : α} (H : b ∈ set.Ico a c) : set.Ico a c ∈ nhds_within b (set.Ioi b) := filter.mem_sets_of_superset (Ioo_mem_nhds_within_Ioi H) set.Ioo_subset_Ico_self theorem Icc_mem_nhds_within_Ioi {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} {b : α} {c : α} (H : b ∈ set.Ico a c) : set.Icc a c ∈ nhds_within b (set.Ioi b) := filter.mem_sets_of_superset (Ioo_mem_nhds_within_Ioi H) set.Ioo_subset_Icc_self @[simp] theorem nhds_within_Ioc_eq_nhds_within_Ioi {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} {b : α} (h : a < b) : nhds_within a (set.Ioc a b) = nhds_within a (set.Ioi a) := le_antisymm (nhds_within_mono a set.Ioc_subset_Ioi_self) (nhds_within_le_of_mem (Ioc_mem_nhds_within_Ioi (iff.mpr set.left_mem_Ico h))) @[simp] theorem nhds_within_Ioo_eq_nhds_within_Ioi {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} {b : α} (h : a < b) : nhds_within a (set.Ioo a b) = nhds_within a (set.Ioi a) := le_antisymm (nhds_within_mono a set.Ioo_subset_Ioi_self) (nhds_within_le_of_mem (Ioo_mem_nhds_within_Ioi (iff.mpr set.left_mem_Ico h))) @[simp] theorem continuous_within_at_Ioc_iff_Ioi {α : Type u} {β : Type v} [topological_space α] [linear_order α] [order_closed_topology α] [topological_space β] {a : α} {b : α} {f : α → β} (h : a < b) : continuous_within_at f (set.Ioc a b) a ↔ continuous_within_at f (set.Ioi a) a := sorry @[simp] theorem continuous_within_at_Ioo_iff_Ioi {α : Type u} {β : Type v} [topological_space α] [linear_order α] [order_closed_topology α] [topological_space β] {a : α} {b : α} {f : α → β} (h : a < b) : continuous_within_at f (set.Ioo a b) a ↔ continuous_within_at f (set.Ioi a) a := sorry /-! #### Left neighborhoods, point excluded -/ theorem Ioo_mem_nhds_within_Iio {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} {b : α} {c : α} (H : b ∈ set.Ioc a c) : set.Ioo a c ∈ nhds_within b (set.Iio b) := sorry theorem Ico_mem_nhds_within_Iio {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} {b : α} {c : α} (H : b ∈ set.Ioc a c) : set.Ico a c ∈ nhds_within b (set.Iio b) := filter.mem_sets_of_superset (Ioo_mem_nhds_within_Iio H) set.Ioo_subset_Ico_self theorem Ioc_mem_nhds_within_Iio {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} {b : α} {c : α} (H : b ∈ set.Ioc a c) : set.Ioc a c ∈ nhds_within b (set.Iio b) := filter.mem_sets_of_superset (Ioo_mem_nhds_within_Iio H) set.Ioo_subset_Ioc_self theorem Icc_mem_nhds_within_Iio {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} {b : α} {c : α} (H : b ∈ set.Ioc a c) : set.Icc a c ∈ nhds_within b (set.Iio b) := filter.mem_sets_of_superset (Ioo_mem_nhds_within_Iio H) set.Ioo_subset_Icc_self @[simp] theorem nhds_within_Ico_eq_nhds_within_Iio {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} {b : α} (h : a < b) : nhds_within b (set.Ico a b) = nhds_within b (set.Iio b) := sorry @[simp] theorem nhds_within_Ioo_eq_nhds_within_Iio {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} {b : α} (h : a < b) : nhds_within b (set.Ioo a b) = nhds_within b (set.Iio b) := sorry @[simp] theorem continuous_within_at_Ico_iff_Iio {α : Type u} {γ : Type w} [topological_space α] [linear_order α] [order_closed_topology α] [topological_space γ] {a : α} {b : α} {f : α → γ} (h : a < b) : continuous_within_at f (set.Ico a b) b ↔ continuous_within_at f (set.Iio b) b := sorry @[simp] theorem continuous_within_at_Ioo_iff_Iio {α : Type u} {γ : Type w} [topological_space α] [linear_order α] [order_closed_topology α] [topological_space γ] {a : α} {b : α} {f : α → γ} (h : a < b) : continuous_within_at f (set.Ioo a b) b ↔ continuous_within_at f (set.Iio b) b := sorry /-! #### Right neighborhoods, point included -/ theorem Ioo_mem_nhds_within_Ici {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} {b : α} {c : α} (H : b ∈ set.Ioo a c) : set.Ioo a c ∈ nhds_within b (set.Ici b) := mem_nhds_within_of_mem_nhds (mem_nhds_sets is_open_Ioo H) theorem Ioc_mem_nhds_within_Ici {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} {b : α} {c : α} (H : b ∈ set.Ioo a c) : set.Ioc a c ∈ nhds_within b (set.Ici b) := filter.mem_sets_of_superset (Ioo_mem_nhds_within_Ici H) set.Ioo_subset_Ioc_self theorem Ico_mem_nhds_within_Ici {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} {b : α} {c : α} (H : b ∈ set.Ico a c) : set.Ico a c ∈ nhds_within b (set.Ici b) := sorry theorem Icc_mem_nhds_within_Ici {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} {b : α} {c : α} (H : b ∈ set.Ico a c) : set.Icc a c ∈ nhds_within b (set.Ici b) := filter.mem_sets_of_superset (Ico_mem_nhds_within_Ici H) set.Ico_subset_Icc_self @[simp] theorem nhds_within_Icc_eq_nhds_within_Ici {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} {b : α} (h : a < b) : nhds_within a (set.Icc a b) = nhds_within a (set.Ici a) := le_antisymm (nhds_within_mono a set.Icc_subset_Ici_self) (nhds_within_le_of_mem (Icc_mem_nhds_within_Ici (iff.mpr set.left_mem_Ico h))) @[simp] theorem nhds_within_Ico_eq_nhds_within_Ici {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} {b : α} (h : a < b) : nhds_within a (set.Ico a b) = nhds_within a (set.Ici a) := le_antisymm (nhds_within_mono a fun (x : α) => and.left) (nhds_within_le_of_mem (Ico_mem_nhds_within_Ici (iff.mpr set.left_mem_Ico h))) @[simp] theorem continuous_within_at_Icc_iff_Ici {α : Type u} {β : Type v} [topological_space α] [linear_order α] [order_closed_topology α] [topological_space β] {a : α} {b : α} {f : α → β} (h : a < b) : continuous_within_at f (set.Icc a b) a ↔ continuous_within_at f (set.Ici a) a := sorry @[simp] theorem continuous_within_at_Ico_iff_Ici {α : Type u} {β : Type v} [topological_space α] [linear_order α] [order_closed_topology α] [topological_space β] {a : α} {b : α} {f : α → β} (h : a < b) : continuous_within_at f (set.Ico a b) a ↔ continuous_within_at f (set.Ici a) a := sorry /-! #### Left neighborhoods, point included -/ theorem Ioo_mem_nhds_within_Iic {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} {b : α} {c : α} (H : b ∈ set.Ioo a c) : set.Ioo a c ∈ nhds_within b (set.Iic b) := mem_nhds_within_of_mem_nhds (mem_nhds_sets is_open_Ioo H) theorem Ico_mem_nhds_within_Iic {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} {b : α} {c : α} (H : b ∈ set.Ioo a c) : set.Ico a c ∈ nhds_within b (set.Iic b) := filter.mem_sets_of_superset (Ioo_mem_nhds_within_Iic H) set.Ioo_subset_Ico_self theorem Ioc_mem_nhds_within_Iic {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} {b : α} {c : α} (H : b ∈ set.Ioc a c) : set.Ioc a c ∈ nhds_within b (set.Iic b) := sorry theorem Icc_mem_nhds_within_Iic {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} {b : α} {c : α} (H : b ∈ set.Ioc a c) : set.Icc a c ∈ nhds_within b (set.Iic b) := filter.mem_sets_of_superset (Ioc_mem_nhds_within_Iic H) set.Ioc_subset_Icc_self @[simp] theorem nhds_within_Icc_eq_nhds_within_Iic {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} {b : α} (h : a < b) : nhds_within b (set.Icc a b) = nhds_within b (set.Iic b) := sorry @[simp] theorem nhds_within_Ioc_eq_nhds_within_Iic {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] {a : α} {b : α} (h : a < b) : nhds_within b (set.Ioc a b) = nhds_within b (set.Iic b) := sorry @[simp] theorem continuous_within_at_Icc_iff_Iic {α : Type u} {β : Type v} [topological_space α] [linear_order α] [order_closed_topology α] [topological_space β] {a : α} {b : α} {f : α → β} (h : a < b) : continuous_within_at f (set.Icc a b) b ↔ continuous_within_at f (set.Iic b) b := sorry @[simp] theorem continuous_within_at_Ioc_iff_Iic {α : Type u} {β : Type v} [topological_space α] [linear_order α] [order_closed_topology α] [topological_space β] {a : α} {b : α} {f : α → β} (h : a < b) : continuous_within_at f (set.Ioc a b) b ↔ continuous_within_at f (set.Iic b) b := sorry theorem frontier_le_subset_eq {α : Type u} {β : Type v} [topological_space α] [linear_order α] [order_closed_topology α] {f : β → α} {g : β → α} [topological_space β] (hf : continuous f) (hg : continuous g) : frontier (set_of fun (b : β) => f b ≤ g b) ⊆ set_of fun (b : β) => f b = g b := sorry theorem frontier_lt_subset_eq {α : Type u} {β : Type v} [topological_space α] [linear_order α] [order_closed_topology α] {f : β → α} {g : β → α} [topological_space β] (hf : continuous f) (hg : continuous g) : frontier (set_of fun (b : β) => f b < g b) ⊆ set_of fun (b : β) => f b = g b := sorry theorem continuous.min {α : Type u} {β : Type v} [topological_space α] [linear_order α] [order_closed_topology α] {f : β → α} {g : β → α} [topological_space β] (hf : continuous f) (hg : continuous g) : continuous fun (b : β) => min (f b) (g b) := (fun (this : ∀ (b : β), b ∈ frontier (set_of fun (b : β) => f b ≤ g b) → f b = g b) => continuous_if this hf hg) fun (b : β) (hb : b ∈ frontier (set_of fun (b : β) => f b ≤ g b)) => frontier_le_subset_eq hf hg hb theorem continuous.max {α : Type u} {β : Type v} [topological_space α] [linear_order α] [order_closed_topology α] {f : β → α} {g : β → α} [topological_space β] (hf : continuous f) (hg : continuous g) : continuous fun (b : β) => max (f b) (g b) := continuous.min hf hg theorem continuous_min {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] : continuous fun (p : α × α) => min (prod.fst p) (prod.snd p) := continuous.min continuous_fst continuous_snd theorem continuous_max {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] : continuous fun (p : α × α) => max (prod.fst p) (prod.snd p) := continuous.max continuous_fst continuous_snd theorem tendsto.max {α : Type u} {β : Type v} [topological_space α] [linear_order α] [order_closed_topology α] {f : β → α} {g : β → α} {b : filter β} {a₁ : α} {a₂ : α} (hf : filter.tendsto f b (nhds a₁)) (hg : filter.tendsto g b (nhds a₂)) : filter.tendsto (fun (b : β) => max (f b) (g b)) b (nhds (max a₁ a₂)) := filter.tendsto.comp (continuous.tendsto continuous_max (a₁, a₂)) (filter.tendsto.prod_mk_nhds hf hg) theorem tendsto.min {α : Type u} {β : Type v} [topological_space α] [linear_order α] [order_closed_topology α] {f : β → α} {g : β → α} {b : filter β} {a₁ : α} {a₂ : α} (hf : filter.tendsto f b (nhds a₁)) (hg : filter.tendsto g b (nhds a₂)) : filter.tendsto (fun (b : β) => min (f b) (g b)) b (nhds (min a₁ a₂)) := filter.tendsto.comp (continuous.tendsto continuous_min (a₁, a₂)) (filter.tendsto.prod_mk_nhds hf hg) /-- The order topology on an ordered type is the topology generated by open intervals. We register it on a preorder, but it is mostly interesting in linear orders, where it is also order-closed. We define it as a mixin. If you want to introduce the order topology on a preorder, use `preorder.topology`. -/ class order_topology (α : Type u_1) [t : topological_space α] [preorder α] where topology_eq_generate_intervals : t = topological_space.generate_from (set_of fun (s : set α) => ∃ (a : α), s = set.Ioi a ∨ s = set.Iio a) /-- (Order) topology on a partial order `α` generated by the subbase of open intervals `(a, ∞) = { x ∣ a < x }, (-∞ , b) = {x ∣ x < b}` for all `a, b` in `α`. We do not register it as an instance as many ordered sets are already endowed with the same topology, most often in a non-defeq way though. Register as a local instance when necessary. -/ def preorder.topology (α : Type u_1) [preorder α] : topological_space α := topological_space.generate_from (set_of fun (s : set α) => ∃ (a : α), (s = set_of fun (b : α) => a < b) ∨ s = set_of fun (b : α) => b < a) protected instance order_dual.order_topology {α : Type u_1} [topological_space α] [partial_order α] [order_topology α] : order_topology (order_dual α) := sorry theorem is_open_iff_generate_intervals {α : Type u} [topological_space α] [partial_order α] [t : order_topology α] {s : set α} : is_open s ↔ topological_space.generate_open (set_of fun (s : set α) => ∃ (a : α), s = set.Ioi a ∨ s = set.Iio a) s := sorry theorem is_open_lt' {α : Type u} [topological_space α] [partial_order α] [t : order_topology α] (a : α) : is_open (set_of fun (b : α) => a < b) := eq.mpr (id (Eq._oldrec (Eq.refl (is_open (set_of fun (b : α) => a < b))) (propext is_open_iff_generate_intervals))) (topological_space.generate_open.basic (set_of fun (b : α) => a < b) (Exists.intro a (Or.inl rfl))) theorem is_open_gt' {α : Type u} [topological_space α] [partial_order α] [t : order_topology α] (a : α) : is_open (set_of fun (b : α) => b < a) := eq.mpr (id (Eq._oldrec (Eq.refl (is_open (set_of fun (b : α) => b < a))) (propext is_open_iff_generate_intervals))) (topological_space.generate_open.basic (set_of fun (b : α) => b < a) (Exists.intro a (Or.inr rfl))) theorem lt_mem_nhds {α : Type u} [topological_space α] [partial_order α] [t : order_topology α] {a : α} {b : α} (h : a < b) : filter.eventually (fun (x : α) => a < x) (nhds b) := mem_nhds_sets (is_open_lt' a) h theorem le_mem_nhds {α : Type u} [topological_space α] [partial_order α] [t : order_topology α] {a : α} {b : α} (h : a < b) : filter.eventually (fun (x : α) => a ≤ x) (nhds b) := filter.sets_of_superset (nhds b) (lt_mem_nhds h) fun (b : α) (hb : b ∈ set_of fun (x : α) => (fun (x : α) => a < x) x) => le_of_lt hb theorem gt_mem_nhds {α : Type u} [topological_space α] [partial_order α] [t : order_topology α] {a : α} {b : α} (h : a < b) : filter.eventually (fun (x : α) => x < b) (nhds a) := mem_nhds_sets (is_open_gt' b) h theorem ge_mem_nhds {α : Type u} [topological_space α] [partial_order α] [t : order_topology α] {a : α} {b : α} (h : a < b) : filter.eventually (fun (x : α) => x ≤ b) (nhds a) := filter.sets_of_superset (nhds a) (gt_mem_nhds h) fun (b_1 : α) (hb : b_1 ∈ set_of fun (x : α) => (fun (x : α) => x < b) x) => le_of_lt hb theorem nhds_eq_order {α : Type u} [topological_space α] [partial_order α] [t : order_topology α] (a : α) : nhds a = (infi fun (b : α) => infi fun (H : b ∈ set.Iio a) => filter.principal (set.Ioi b)) ⊓ infi fun (b : α) => infi fun (H : b ∈ set.Ioi a) => filter.principal (set.Iio b) := sorry theorem tendsto_order {α : Type u} {β : Type v} [topological_space α] [partial_order α] [t : order_topology α] {f : β → α} {a : α} {x : filter β} : filter.tendsto f x (nhds a) ↔ (∀ (a' : α), a' < a → filter.eventually (fun (b : β) => a' < f b) x) ∧ ∀ (a' : α), a' > a → filter.eventually (fun (b : β) => f b < a') x := sorry protected instance tendsto_Icc_class_nhds {α : Type u} [topological_space α] [partial_order α] [t : order_topology α] (a : α) : filter.tendsto_Ixx_class set.Icc (nhds a) (nhds a) := sorry protected instance tendsto_Ico_class_nhds {α : Type u} [topological_space α] [partial_order α] [t : order_topology α] (a : α) : filter.tendsto_Ixx_class set.Ico (nhds a) (nhds a) := filter.tendsto_Ixx_class_of_subset fun (_x _x_1 : α) => set.Ico_subset_Icc_self protected instance tendsto_Ioc_class_nhds {α : Type u} [topological_space α] [partial_order α] [t : order_topology α] (a : α) : filter.tendsto_Ixx_class set.Ioc (nhds a) (nhds a) := filter.tendsto_Ixx_class_of_subset fun (_x _x_1 : α) => set.Ioc_subset_Icc_self protected instance tendsto_Ioo_class_nhds {α : Type u} [topological_space α] [partial_order α] [t : order_topology α] (a : α) : filter.tendsto_Ixx_class set.Ioo (nhds a) (nhds a) := filter.tendsto_Ixx_class_of_subset fun (_x _x_1 : α) => set.Ioo_subset_Icc_self /-- Also known as squeeze or sandwich theorem. This version assumes that inequalities hold eventually for the filter. -/ theorem tendsto_of_tendsto_of_tendsto_of_le_of_le' {α : Type u} {β : Type v} [topological_space α] [partial_order α] [t : order_topology α] {f : β → α} {g : β → α} {h : β → α} {b : filter β} {a : α} (hg : filter.tendsto g b (nhds a)) (hh : filter.tendsto h b (nhds a)) (hgf : filter.eventually (fun (b : β) => g b ≤ f b) b) (hfh : filter.eventually (fun (b : β) => f b ≤ h b) b) : filter.tendsto f b (nhds a) := sorry /-- Also known as squeeze or sandwich theorem. This version assumes that inequalities hold everywhere. -/ theorem tendsto_of_tendsto_of_tendsto_of_le_of_le {α : Type u} {β : Type v} [topological_space α] [partial_order α] [t : order_topology α] {f : β → α} {g : β → α} {h : β → α} {b : filter β} {a : α} (hg : filter.tendsto g b (nhds a)) (hh : filter.tendsto h b (nhds a)) (hgf : g ≤ f) (hfh : f ≤ h) : filter.tendsto f b (nhds a) := tendsto_of_tendsto_of_tendsto_of_le_of_le' hg hh (filter.eventually_of_forall hgf) (filter.eventually_of_forall hfh) theorem nhds_order_unbounded {α : Type u} [topological_space α] [partial_order α] [t : order_topology α] {a : α} (hu : ∃ (u : α), a < u) (hl : ∃ (l : α), l < a) : nhds a = infi fun (l : α) => infi fun (h₂ : l < a) => infi fun (u : α) => infi fun (h₂ : a < u) => filter.principal (set.Ioo l u) := sorry theorem tendsto_order_unbounded {α : Type u} {β : Type v} [topological_space α] [partial_order α] [t : order_topology α] {f : β → α} {a : α} {x : filter β} (hu : ∃ (u : α), a < u) (hl : ∃ (l : α), l < a) (h : ∀ (l u : α), l < a → a < u → filter.eventually (fun (b : β) => l < f b ∧ f b < u) x) : filter.tendsto f x (nhds a) := sorry protected instance tendsto_Ixx_nhds_within {α : Type u_1} [preorder α] [topological_space α] (a : α) {s : set α} {t : set α} {Ixx : α → α → set α} [filter.tendsto_Ixx_class Ixx (nhds a) (nhds a)] [filter.tendsto_Ixx_class Ixx (filter.principal s) (filter.principal t)] : filter.tendsto_Ixx_class Ixx (nhds_within a s) (nhds_within a t) := filter.tendsto_Ixx_class_inf protected instance tendsto_Icc_class_nhds_pi {ι : Type u_1} {α : ι → Type u_2} [Nonempty ι] [(i : ι) → partial_order (α i)] [(i : ι) → topological_space (α i)] [∀ (i : ι), order_topology (α i)] (f : (i : ι) → α i) : filter.tendsto_Ixx_class set.Icc (nhds f) (nhds f) := sorry theorem induced_order_topology' {α : Type u} {β : Type v} [partial_order α] [ta : topological_space β] [partial_order β] [order_topology β] (f : α → β) (hf : ∀ {x y : α}, f x < f y ↔ x < y) (H₁ : ∀ {a : α} {x : β}, x < f a → ∃ (b : α), ∃ (H : b < a), x ≤ f b) (H₂ : ∀ {a : α} {x : β}, f a < x → ∃ (b : α), ∃ (H : b > a), f b ≤ x) : order_topology α := sorry theorem induced_order_topology {α : Type u} {β : Type v} [partial_order α] [ta : topological_space β] [partial_order β] [order_topology β] (f : α → β) (hf : ∀ {x y : α}, f x < f y ↔ x < y) (H : ∀ {x y : β}, x < y → ∃ (a : α), x < f a ∧ f a < y) : order_topology α := sorry /-- On an `ord_connected` subset of a linear order, the order topology for the restriction of the order is the same as the restriction to the subset of the order topology. -/ protected instance order_topology_of_ord_connected {α : Type u} [ta : topological_space α] [linear_order α] [order_topology α] {t : set α} [ht : set.ord_connected t] : order_topology ↥t := sorry theorem nhds_top_order {α : Type u} [topological_space α] [order_top α] [order_topology α] : nhds ⊤ = infi fun (l : α) => infi fun (h₂ : l < ⊤) => filter.principal (set.Ioi l) := sorry theorem nhds_bot_order {α : Type u} [topological_space α] [order_bot α] [order_topology α] : nhds ⊥ = infi fun (l : α) => infi fun (h₂ : ⊥ < l) => filter.principal (set.Iio l) := sorry theorem tendsto_nhds_top_mono {α : Type u} {β : Type v} [topological_space β] [order_top β] [order_topology β] {l : filter α} {f : α → β} {g : α → β} (hf : filter.tendsto f l (nhds ⊤)) (hg : filter.eventually_le l f g) : filter.tendsto g l (nhds ⊤) := sorry theorem tendsto_nhds_bot_mono {α : Type u} {β : Type v} [topological_space β] [order_bot β] [order_topology β] {l : filter α} {f : α → β} {g : α → β} (hf : filter.tendsto f l (nhds ⊥)) (hg : filter.eventually_le l g f) : filter.tendsto g l (nhds ⊥) := tendsto_nhds_top_mono hf hg theorem tendsto_nhds_top_mono' {α : Type u} {β : Type v} [topological_space β] [order_top β] [order_topology β] {l : filter α} {f : α → β} {g : α → β} (hf : filter.tendsto f l (nhds ⊤)) (hg : f ≤ g) : filter.tendsto g l (nhds ⊤) := tendsto_nhds_top_mono hf (filter.eventually_of_forall hg) theorem tendsto_nhds_bot_mono' {α : Type u} {β : Type v} [topological_space β] [order_bot β] [order_topology β] {l : filter α} {f : α → β} {g : α → β} (hf : filter.tendsto f l (nhds ⊥)) (hg : g ≤ f) : filter.tendsto g l (nhds ⊥) := tendsto_nhds_bot_mono hf (filter.eventually_of_forall hg) theorem exists_Ioc_subset_of_mem_nhds' {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {s : set α} (hs : s ∈ nhds a) {l : α} (hl : l < a) : ∃ (l' : α), ∃ (H : l' ∈ set.Ico l a), set.Ioc l' a ⊆ s := sorry theorem exists_Ico_subset_of_mem_nhds' {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {s : set α} (hs : s ∈ nhds a) {u : α} (hu : a < u) : ∃ (u' : α), ∃ (H : u' ∈ set.Ioc a u), set.Ico a u' ⊆ s := sorry theorem exists_Ioc_subset_of_mem_nhds {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {s : set α} (hs : s ∈ nhds a) (h : ∃ (l : α), l < a) : ∃ (l : α), ∃ (H : l < a), set.Ioc l a ⊆ s := sorry theorem exists_Ico_subset_of_mem_nhds {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {s : set α} (hs : s ∈ nhds a) (h : ∃ (u : α), a < u) : ∃ (u : α), ∃ (_x : a < u), set.Ico a u ⊆ s := sorry theorem order_separated {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a₁ : α} {a₂ : α} (h : a₁ < a₂) : ∃ (u : set α), ∃ (v : set α), is_open u ∧ is_open v ∧ a₁ ∈ u ∧ a₂ ∈ v ∧ ∀ (b₁ : α), b₁ ∈ u → ∀ (b₂ : α), b₂ ∈ v → b₁ < b₂ := sorry protected instance order_topology.to_order_closed_topology {α : Type u} [topological_space α] [linear_order α] [order_topology α] : order_closed_topology α := order_closed_topology.mk (iff.mpr is_open_prod_iff fun (a₁ a₂ : α) (h : ¬a₁ ≤ a₂) => (fun (h : a₂ < a₁) => sorry) (lt_of_not_ge h)) theorem order_topology.t2_space {α : Type u} [topological_space α] [linear_order α] [order_topology α] : t2_space α := order_closed_topology.to_t2_space protected instance order_topology.regular_space {α : Type u} [topological_space α] [linear_order α] [order_topology α] : regular_space α := regular_space.mk fun (s : set α) (a : α) (hs : is_closed s) (ha : ¬a ∈ s) => (fun (hs' : sᶜ ∈ nhds a) => (fun (this : ∃ (t : set α), is_open t ∧ (∀ (l : α), l ∈ s → l < a → l ∈ t) ∧ nhds_within a t = ⊥) => sorry) (classical.by_cases (fun (h : ∃ (l : α), l < a) => sorry) fun (this : ¬∃ (l : α), l < a) => Exists.intro ∅ { left := is_open_empty, right := { left := fun (l : α) (_x : l ∈ s) (hl : l < a) => false.elim (this (Exists.intro l hl)), right := nhds_within_empty a } })) (mem_nhds_sets hs ha) /-- A set is a neighborhood of `a` if and only if it contains an interval `(l, u)` containing `a`, provided `a` is neither a bottom element nor a top element. -/ theorem mem_nhds_iff_exists_Ioo_subset' {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {s : set α} (hl : ∃ (l : α), l < a) (hu : ∃ (u : α), a < u) : s ∈ nhds a ↔ ∃ (l : α), ∃ (u : α), a ∈ set.Ioo l u ∧ set.Ioo l u ⊆ s := sorry /-- A set is a neighborhood of `a` if and only if it contains an interval `(l, u)` containing `a`. -/ theorem mem_nhds_iff_exists_Ioo_subset {α : Type u} [topological_space α] [linear_order α] [order_topology α] [no_top_order α] [no_bot_order α] {a : α} {s : set α} : s ∈ nhds a ↔ ∃ (l : α), ∃ (u : α), a ∈ set.Ioo l u ∧ set.Ioo l u ⊆ s := mem_nhds_iff_exists_Ioo_subset' (no_bot a) (no_top a) theorem nhds_basis_Ioo' {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} (hl : ∃ (l : α), l < a) (hu : ∃ (u : α), a < u) : filter.has_basis (nhds a) (fun (b : α × α) => prod.fst b < a ∧ a < prod.snd b) fun (b : α × α) => set.Ioo (prod.fst b) (prod.snd b) := sorry theorem nhds_basis_Ioo {α : Type u} [topological_space α] [linear_order α] [order_topology α] [no_top_order α] [no_bot_order α] {a : α} : filter.has_basis (nhds a) (fun (b : α × α) => prod.fst b < a ∧ a < prod.snd b) fun (b : α × α) => set.Ioo (prod.fst b) (prod.snd b) := nhds_basis_Ioo' (no_bot a) (no_top a) theorem filter.eventually.exists_Ioo_subset {α : Type u} [topological_space α] [linear_order α] [order_topology α] [no_top_order α] [no_bot_order α] {a : α} {p : α → Prop} (hp : filter.eventually (fun (x : α) => p x) (nhds a)) : ∃ (l : α), ∃ (u : α), a ∈ set.Ioo l u ∧ set.Ioo l u ⊆ set_of fun (x : α) => p x := iff.mp mem_nhds_iff_exists_Ioo_subset hp theorem Iio_mem_nhds {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {b : α} (h : a < b) : set.Iio b ∈ nhds a := mem_nhds_sets is_open_Iio h theorem Ioi_mem_nhds {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {b : α} (h : a < b) : set.Ioi a ∈ nhds b := mem_nhds_sets is_open_Ioi h theorem Iic_mem_nhds {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {b : α} (h : a < b) : set.Iic b ∈ nhds a := filter.mem_sets_of_superset (Iio_mem_nhds h) set.Iio_subset_Iic_self theorem Ici_mem_nhds {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {b : α} (h : a < b) : set.Ici a ∈ nhds b := filter.mem_sets_of_superset (Ioi_mem_nhds h) set.Ioi_subset_Ici_self theorem Ioo_mem_nhds {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {b : α} {x : α} (ha : a < x) (hb : x < b) : set.Ioo a b ∈ nhds x := mem_nhds_sets is_open_Ioo { left := ha, right := hb } theorem Ioc_mem_nhds {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {b : α} {x : α} (ha : a < x) (hb : x < b) : set.Ioc a b ∈ nhds x := filter.mem_sets_of_superset (Ioo_mem_nhds ha hb) set.Ioo_subset_Ioc_self theorem Ico_mem_nhds {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {b : α} {x : α} (ha : a < x) (hb : x < b) : set.Ico a b ∈ nhds x := filter.mem_sets_of_superset (Ioo_mem_nhds ha hb) set.Ioo_subset_Ico_self theorem Icc_mem_nhds {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {b : α} {x : α} (ha : a < x) (hb : x < b) : set.Icc a b ∈ nhds x := filter.mem_sets_of_superset (Ioo_mem_nhds ha hb) set.Ioo_subset_Icc_self /-! ### Intervals in `Π i, π i` belong to `𝓝 x` For each leamma `pi_Ixx_mem_nhds` we add a non-dependent version `pi_Ixx_mem_nhds'` because sometimes Lean fails to unify different instances while trying to apply the dependent version to, e.g., `ι → ℝ`. -/ theorem pi_Iic_mem_nhds {ι : Type u_1} {π : ι → Type u_2} [fintype ι] [(i : ι) → linear_order (π i)] [(i : ι) → topological_space (π i)] [∀ (i : ι), order_topology (π i)] {a : (i : ι) → π i} {x : (i : ι) → π i} (ha : ∀ (i : ι), x i < a i) : set.Iic a ∈ nhds x := set.pi_univ_Iic a ▸ set_pi_mem_nhds (set.finite.of_fintype set.univ) fun (i : ι) (_x : i ∈ set.univ) => Iic_mem_nhds (ha i) theorem pi_Iic_mem_nhds' {α : Type u} [topological_space α] [linear_order α] [order_topology α] {ι : Type u_1} [fintype ι] {a' : ι → α} {x' : ι → α} (ha : ∀ (i : ι), x' i < a' i) : set.Iic a' ∈ nhds x' := pi_Iic_mem_nhds ha theorem pi_Ici_mem_nhds {ι : Type u_1} {π : ι → Type u_2} [fintype ι] [(i : ι) → linear_order (π i)] [(i : ι) → topological_space (π i)] [∀ (i : ι), order_topology (π i)] {a : (i : ι) → π i} {x : (i : ι) → π i} (ha : ∀ (i : ι), a i < x i) : set.Ici a ∈ nhds x := set.pi_univ_Ici a ▸ set_pi_mem_nhds (set.finite.of_fintype set.univ) fun (i : ι) (_x : i ∈ set.univ) => Ici_mem_nhds (ha i) theorem pi_Ici_mem_nhds' {α : Type u} [topological_space α] [linear_order α] [order_topology α] {ι : Type u_1} [fintype ι] {a' : ι → α} {x' : ι → α} (ha : ∀ (i : ι), a' i < x' i) : set.Ici a' ∈ nhds x' := pi_Ici_mem_nhds ha theorem pi_Icc_mem_nhds {ι : Type u_1} {π : ι → Type u_2} [fintype ι] [(i : ι) → linear_order (π i)] [(i : ι) → topological_space (π i)] [∀ (i : ι), order_topology (π i)] {a : (i : ι) → π i} {b : (i : ι) → π i} {x : (i : ι) → π i} (ha : ∀ (i : ι), a i < x i) (hb : ∀ (i : ι), x i < b i) : set.Icc a b ∈ nhds x := set.pi_univ_Icc a b ▸ set_pi_mem_nhds (set.finite.of_fintype set.univ) fun (i : ι) (_x : i ∈ set.univ) => Icc_mem_nhds (ha i) (hb i) theorem pi_Icc_mem_nhds' {α : Type u} [topological_space α] [linear_order α] [order_topology α] {ι : Type u_1} [fintype ι] {a' : ι → α} {b' : ι → α} {x' : ι → α} (ha : ∀ (i : ι), a' i < x' i) (hb : ∀ (i : ι), x' i < b' i) : set.Icc a' b' ∈ nhds x' := pi_Icc_mem_nhds ha hb theorem pi_Iio_mem_nhds {ι : Type u_1} {π : ι → Type u_2} [fintype ι] [(i : ι) → linear_order (π i)] [(i : ι) → topological_space (π i)] [∀ (i : ι), order_topology (π i)] {a : (i : ι) → π i} {x : (i : ι) → π i} [Nonempty ι] (ha : ∀ (i : ι), x i < a i) : set.Iio a ∈ nhds x := filter.mem_sets_of_superset (set_pi_mem_nhds (set.finite.of_fintype set.univ) fun (i : ι) (_x : i ∈ set.univ) => Iio_mem_nhds (ha i)) (set.pi_univ_Iio_subset a) theorem pi_Iio_mem_nhds' {α : Type u} [topological_space α] [linear_order α] [order_topology α] {ι : Type u_1} [fintype ι] {a' : ι → α} {x' : ι → α} [Nonempty ι] (ha : ∀ (i : ι), x' i < a' i) : set.Iio a' ∈ nhds x' := pi_Iio_mem_nhds ha theorem pi_Ioi_mem_nhds {ι : Type u_1} {π : ι → Type u_2} [fintype ι] [(i : ι) → linear_order (π i)] [(i : ι) → topological_space (π i)] [∀ (i : ι), order_topology (π i)] {a : (i : ι) → π i} {x : (i : ι) → π i} [Nonempty ι] (ha : ∀ (i : ι), a i < x i) : set.Ioi a ∈ nhds x := pi_Iio_mem_nhds ha theorem pi_Ioi_mem_nhds' {α : Type u} [topological_space α] [linear_order α] [order_topology α] {ι : Type u_1} [fintype ι] {a' : ι → α} {x' : ι → α} [Nonempty ι] (ha : ∀ (i : ι), a' i < x' i) : set.Ioi a' ∈ nhds x' := pi_Ioi_mem_nhds ha theorem pi_Ioc_mem_nhds {ι : Type u_1} {π : ι → Type u_2} [fintype ι] [(i : ι) → linear_order (π i)] [(i : ι) → topological_space (π i)] [∀ (i : ι), order_topology (π i)] {a : (i : ι) → π i} {b : (i : ι) → π i} {x : (i : ι) → π i} [Nonempty ι] (ha : ∀ (i : ι), a i < x i) (hb : ∀ (i : ι), x i < b i) : set.Ioc a b ∈ nhds x := filter.mem_sets_of_superset (set_pi_mem_nhds (set.finite.of_fintype set.univ) fun (i : ι) (_x : i ∈ set.univ) => Ioc_mem_nhds (ha i) (hb i)) (set.pi_univ_Ioc_subset a b) theorem pi_Ioc_mem_nhds' {α : Type u} [topological_space α] [linear_order α] [order_topology α] {ι : Type u_1} [fintype ι] {a' : ι → α} {b' : ι → α} {x' : ι → α} [Nonempty ι] (ha : ∀ (i : ι), a' i < x' i) (hb : ∀ (i : ι), x' i < b' i) : set.Ioc a' b' ∈ nhds x' := pi_Ioc_mem_nhds ha hb theorem pi_Ico_mem_nhds {ι : Type u_1} {π : ι → Type u_2} [fintype ι] [(i : ι) → linear_order (π i)] [(i : ι) → topological_space (π i)] [∀ (i : ι), order_topology (π i)] {a : (i : ι) → π i} {b : (i : ι) → π i} {x : (i : ι) → π i} [Nonempty ι] (ha : ∀ (i : ι), a i < x i) (hb : ∀ (i : ι), x i < b i) : set.Ico a b ∈ nhds x := filter.mem_sets_of_superset (set_pi_mem_nhds (set.finite.of_fintype set.univ) fun (i : ι) (_x : i ∈ set.univ) => Ico_mem_nhds (ha i) (hb i)) (set.pi_univ_Ico_subset a b) theorem pi_Ico_mem_nhds' {α : Type u} [topological_space α] [linear_order α] [order_topology α] {ι : Type u_1} [fintype ι] {a' : ι → α} {b' : ι → α} {x' : ι → α} [Nonempty ι] (ha : ∀ (i : ι), a' i < x' i) (hb : ∀ (i : ι), x' i < b' i) : set.Ico a' b' ∈ nhds x' := pi_Ico_mem_nhds ha hb theorem pi_Ioo_mem_nhds {ι : Type u_1} {π : ι → Type u_2} [fintype ι] [(i : ι) → linear_order (π i)] [(i : ι) → topological_space (π i)] [∀ (i : ι), order_topology (π i)] {a : (i : ι) → π i} {b : (i : ι) → π i} {x : (i : ι) → π i} [Nonempty ι] (ha : ∀ (i : ι), a i < x i) (hb : ∀ (i : ι), x i < b i) : set.Ioo a b ∈ nhds x := filter.mem_sets_of_superset (set_pi_mem_nhds (set.finite.of_fintype set.univ) fun (i : ι) (_x : i ∈ set.univ) => Ioo_mem_nhds (ha i) (hb i)) (set.pi_univ_Ioo_subset a b) theorem pi_Ioo_mem_nhds' {α : Type u} [topological_space α] [linear_order α] [order_topology α] {ι : Type u_1} [fintype ι] {a' : ι → α} {b' : ι → α} {x' : ι → α} [Nonempty ι] (ha : ∀ (i : ι), a' i < x' i) (hb : ∀ (i : ι), x' i < b' i) : set.Ioo a' b' ∈ nhds x' := pi_Ioo_mem_nhds ha hb theorem disjoint_nhds_at_top {α : Type u} [topological_space α] [linear_order α] [order_topology α] [no_top_order α] (x : α) : disjoint (nhds x) filter.at_top := sorry @[simp] theorem inf_nhds_at_top {α : Type u} [topological_space α] [linear_order α] [order_topology α] [no_top_order α] (x : α) : nhds x ⊓ filter.at_top = ⊥ := iff.mp disjoint_iff (disjoint_nhds_at_top x) theorem disjoint_nhds_at_bot {α : Type u} [topological_space α] [linear_order α] [order_topology α] [no_bot_order α] (x : α) : disjoint (nhds x) filter.at_bot := disjoint_nhds_at_top x @[simp] theorem inf_nhds_at_bot {α : Type u} [topological_space α] [linear_order α] [order_topology α] [no_bot_order α] (x : α) : nhds x ⊓ filter.at_bot = ⊥ := inf_nhds_at_top x theorem not_tendsto_nhds_of_tendsto_at_top {α : Type u} {β : Type v} [topological_space α] [linear_order α] [order_topology α] [no_top_order α] {F : filter β} [filter.ne_bot F] {f : β → α} (hf : filter.tendsto f F filter.at_top) (x : α) : ¬filter.tendsto f F (nhds x) := filter.tendsto.not_tendsto hf (disjoint.symm (disjoint_nhds_at_top x)) theorem not_tendsto_at_top_of_tendsto_nhds {α : Type u} {β : Type v} [topological_space α] [linear_order α] [order_topology α] [no_top_order α] {F : filter β} [filter.ne_bot F] {f : β → α} {x : α} (hf : filter.tendsto f F (nhds x)) : ¬filter.tendsto f F filter.at_top := filter.tendsto.not_tendsto hf (disjoint_nhds_at_top x) theorem not_tendsto_nhds_of_tendsto_at_bot {α : Type u} {β : Type v} [topological_space α] [linear_order α] [order_topology α] [no_bot_order α] {F : filter β} [filter.ne_bot F] {f : β → α} (hf : filter.tendsto f F filter.at_bot) (x : α) : ¬filter.tendsto f F (nhds x) := filter.tendsto.not_tendsto hf (disjoint.symm (disjoint_nhds_at_bot x)) theorem not_tendsto_at_bot_of_tendsto_nhds {α : Type u} {β : Type v} [topological_space α] [linear_order α] [order_topology α] [no_bot_order α] {F : filter β} [filter.ne_bot F] {f : β → α} {x : α} (hf : filter.tendsto f F (nhds x)) : ¬filter.tendsto f F filter.at_bot := filter.tendsto.not_tendsto hf (disjoint_nhds_at_bot x) /-! ### Neighborhoods to the left and to the right on an `order_topology` We've seen some properties of left and right neighborhood of a point in an `order_closed_topology`. In an `order_topology`, such neighborhoods can be characterized as the sets containing suitable intervals to the right or to the left of `a`. We give now these characterizations. -/ -- NB: If you extend the list, append to the end please to avoid breaking the API /-- The following statements are equivalent: 0. `s` is a neighborhood of `a` within `(a, +∞)` 1. `s` is a neighborhood of `a` within `(a, b]` 2. `s` is a neighborhood of `a` within `(a, b)` 3. `s` includes `(a, u)` for some `u ∈ (a, b]` 4. `s` includes `(a, u)` for some `u > a` -/ theorem tfae_mem_nhds_within_Ioi {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {b : α} (hab : a < b) (s : set α) : tfae [s ∈ nhds_within a (set.Ioi a), s ∈ nhds_within a (set.Ioc a b), s ∈ nhds_within a (set.Ioo a b), ∃ (u : α), ∃ (H : u ∈ set.Ioc a b), set.Ioo a u ⊆ s, ∃ (u : α), ∃ (H : u ∈ set.Ioi a), set.Ioo a u ⊆ s] := sorry theorem mem_nhds_within_Ioi_iff_exists_mem_Ioc_Ioo_subset {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {u' : α} {s : set α} (hu' : a < u') : s ∈ nhds_within a (set.Ioi a) ↔ ∃ (u : α), ∃ (H : u ∈ set.Ioc a u'), set.Ioo a u ⊆ s := list.tfae.out (tfae_mem_nhds_within_Ioi hu' s) 0 (bit1 1) /-- A set is a neighborhood of `a` within `(a, +∞)` if and only if it contains an interval `(a, u)` with `a < u < u'`, provided `a` is not a top element. -/ theorem mem_nhds_within_Ioi_iff_exists_Ioo_subset' {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {u' : α} {s : set α} (hu' : a < u') : s ∈ nhds_within a (set.Ioi a) ↔ ∃ (u : α), ∃ (H : u ∈ set.Ioi a), set.Ioo a u ⊆ s := list.tfae.out (tfae_mem_nhds_within_Ioi hu' s) 0 (bit0 (bit0 1)) /-- A set is a neighborhood of `a` within `(a, +∞)` if and only if it contains an interval `(a, u)` with `a < u`. -/ theorem mem_nhds_within_Ioi_iff_exists_Ioo_subset {α : Type u} [topological_space α] [linear_order α] [order_topology α] [no_top_order α] {a : α} {s : set α} : s ∈ nhds_within a (set.Ioi a) ↔ ∃ (u : α), ∃ (H : u ∈ set.Ioi a), set.Ioo a u ⊆ s := sorry /-- A set is a neighborhood of `a` within `(a, +∞)` if and only if it contains an interval `(a, u]` with `a < u`. -/ theorem mem_nhds_within_Ioi_iff_exists_Ioc_subset {α : Type u} [topological_space α] [linear_order α] [order_topology α] [no_top_order α] [densely_ordered α] {a : α} {s : set α} : s ∈ nhds_within a (set.Ioi a) ↔ ∃ (u : α), ∃ (H : u ∈ set.Ioi a), set.Ioc a u ⊆ s := sorry /-- The following statements are equivalent: 0. `s` is a neighborhood of `b` within `(-∞, b)` 1. `s` is a neighborhood of `b` within `[a, b)` 2. `s` is a neighborhood of `b` within `(a, b)` 3. `s` includes `(l, b)` for some `l ∈ [a, b)` 4. `s` includes `(l, b)` for some `l < b` -/ theorem tfae_mem_nhds_within_Iio {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {b : α} (h : a < b) (s : set α) : tfae [s ∈ nhds_within b (set.Iio b), s ∈ nhds_within b (set.Ico a b), s ∈ nhds_within b (set.Ioo a b), ∃ (l : α), ∃ (H : l ∈ set.Ico a b), set.Ioo l b ⊆ s, ∃ (l : α), ∃ (H : l ∈ set.Iio b), set.Ioo l b ⊆ s] := sorry theorem mem_nhds_within_Iio_iff_exists_mem_Ico_Ioo_subset {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {l' : α} {s : set α} (hl' : l' < a) : s ∈ nhds_within a (set.Iio a) ↔ ∃ (l : α), ∃ (H : l ∈ set.Ico l' a), set.Ioo l a ⊆ s := list.tfae.out (tfae_mem_nhds_within_Iio hl' s) 0 (bit1 1) /-- A set is a neighborhood of `a` within `(-∞, a)` if and only if it contains an interval `(l, a)` with `l < a`, provided `a` is not a bottom element. -/ theorem mem_nhds_within_Iio_iff_exists_Ioo_subset' {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {l' : α} {s : set α} (hl' : l' < a) : s ∈ nhds_within a (set.Iio a) ↔ ∃ (l : α), ∃ (H : l ∈ set.Iio a), set.Ioo l a ⊆ s := list.tfae.out (tfae_mem_nhds_within_Iio hl' s) 0 (bit0 (bit0 1)) /-- A set is a neighborhood of `a` within `(-∞, a)` if and only if it contains an interval `(l, a)` with `l < a`. -/ theorem mem_nhds_within_Iio_iff_exists_Ioo_subset {α : Type u} [topological_space α] [linear_order α] [order_topology α] [no_bot_order α] {a : α} {s : set α} : s ∈ nhds_within a (set.Iio a) ↔ ∃ (l : α), ∃ (H : l ∈ set.Iio a), set.Ioo l a ⊆ s := sorry /-- A set is a neighborhood of `a` within `(-∞, a)` if and only if it contains an interval `[l, a)` with `l < a`. -/ theorem mem_nhds_within_Iio_iff_exists_Ico_subset {α : Type u} [topological_space α] [linear_order α] [order_topology α] [no_bot_order α] [densely_ordered α] {a : α} {s : set α} : s ∈ nhds_within a (set.Iio a) ↔ ∃ (l : α), ∃ (H : l ∈ set.Iio a), set.Ico l a ⊆ s := sorry /-- The following statements are equivalent: 0. `s` is a neighborhood of `a` within `[a, +∞)` 1. `s` is a neighborhood of `a` within `[a, b]` 2. `s` is a neighborhood of `a` within `[a, b)` 3. `s` includes `[a, u)` for some `u ∈ (a, b]` 4. `s` includes `[a, u)` for some `u > a` -/ theorem tfae_mem_nhds_within_Ici {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {b : α} (hab : a < b) (s : set α) : tfae [s ∈ nhds_within a (set.Ici a), s ∈ nhds_within a (set.Icc a b), s ∈ nhds_within a (set.Ico a b), ∃ (u : α), ∃ (H : u ∈ set.Ioc a b), set.Ico a u ⊆ s, ∃ (u : α), ∃ (H : u ∈ set.Ioi a), set.Ico a u ⊆ s] := sorry theorem mem_nhds_within_Ici_iff_exists_mem_Ioc_Ico_subset {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {u' : α} {s : set α} (hu' : a < u') : s ∈ nhds_within a (set.Ici a) ↔ ∃ (u : α), ∃ (H : u ∈ set.Ioc a u'), set.Ico a u ⊆ s := list.tfae.out (tfae_mem_nhds_within_Ici hu' s) 0 (bit1 1) /-- A set is a neighborhood of `a` within `[a, +∞)` if and only if it contains an interval `[a, u)` with `a < u < u'`, provided `a` is not a top element. -/ theorem mem_nhds_within_Ici_iff_exists_Ico_subset' {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {u' : α} {s : set α} (hu' : a < u') : s ∈ nhds_within a (set.Ici a) ↔ ∃ (u : α), ∃ (H : u ∈ set.Ioi a), set.Ico a u ⊆ s := list.tfae.out (tfae_mem_nhds_within_Ici hu' s) 0 (bit0 (bit0 1)) /-- A set is a neighborhood of `a` within `[a, +∞)` if and only if it contains an interval `[a, u)` with `a < u`. -/ theorem mem_nhds_within_Ici_iff_exists_Ico_subset {α : Type u} [topological_space α] [linear_order α] [order_topology α] [no_top_order α] {a : α} {s : set α} : s ∈ nhds_within a (set.Ici a) ↔ ∃ (u : α), ∃ (H : u ∈ set.Ioi a), set.Ico a u ⊆ s := sorry /-- A set is a neighborhood of `a` within `[a, +∞)` if and only if it contains an interval `[a, u]` with `a < u`. -/ theorem mem_nhds_within_Ici_iff_exists_Icc_subset' {α : Type u} [topological_space α] [linear_order α] [order_topology α] [no_top_order α] [densely_ordered α] {a : α} {s : set α} : s ∈ nhds_within a (set.Ici a) ↔ ∃ (u : α), ∃ (H : u ∈ set.Ioi a), set.Icc a u ⊆ s := sorry /-- The following statements are equivalent: 0. `s` is a neighborhood of `b` within `(-∞, b]` 1. `s` is a neighborhood of `b` within `[a, b]` 2. `s` is a neighborhood of `b` within `(a, b]` 3. `s` includes `(l, b]` for some `l ∈ [a, b)` 4. `s` includes `(l, b]` for some `l < b` -/ theorem tfae_mem_nhds_within_Iic {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {b : α} (h : a < b) (s : set α) : tfae [s ∈ nhds_within b (set.Iic b), s ∈ nhds_within b (set.Icc a b), s ∈ nhds_within b (set.Ioc a b), ∃ (l : α), ∃ (H : l ∈ set.Ico a b), set.Ioc l b ⊆ s, ∃ (l : α), ∃ (H : l ∈ set.Iio b), set.Ioc l b ⊆ s] := sorry theorem mem_nhds_within_Iic_iff_exists_mem_Ico_Ioc_subset {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {l' : α} {s : set α} (hl' : l' < a) : s ∈ nhds_within a (set.Iic a) ↔ ∃ (l : α), ∃ (H : l ∈ set.Ico l' a), set.Ioc l a ⊆ s := list.tfae.out (tfae_mem_nhds_within_Iic hl' s) 0 (bit1 1) /-- A set is a neighborhood of `a` within `(-∞, a]` if and only if it contains an interval `(l, a]` with `l < a`, provided `a` is not a bottom element. -/ theorem mem_nhds_within_Iic_iff_exists_Ioc_subset' {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {l' : α} {s : set α} (hl' : l' < a) : s ∈ nhds_within a (set.Iic a) ↔ ∃ (l : α), ∃ (H : l ∈ set.Iio a), set.Ioc l a ⊆ s := list.tfae.out (tfae_mem_nhds_within_Iic hl' s) 0 (bit0 (bit0 1)) /-- A set is a neighborhood of `a` within `(-∞, a]` if and only if it contains an interval `(l, a]` with `l < a`. -/ theorem mem_nhds_within_Iic_iff_exists_Ioc_subset {α : Type u} [topological_space α] [linear_order α] [order_topology α] [no_bot_order α] {a : α} {s : set α} : s ∈ nhds_within a (set.Iic a) ↔ ∃ (l : α), ∃ (H : l ∈ set.Iio a), set.Ioc l a ⊆ s := sorry /-- A set is a neighborhood of `a` within `(-∞, a]` if and only if it contains an interval `[l, a]` with `l < a`. -/ theorem mem_nhds_within_Iic_iff_exists_Icc_subset' {α : Type u} [topological_space α] [linear_order α] [order_topology α] [no_bot_order α] [densely_ordered α] {a : α} {s : set α} : s ∈ nhds_within a (set.Iic a) ↔ ∃ (l : α), ∃ (H : l ∈ set.Iio a), set.Icc l a ⊆ s := sorry /-- A set is a neighborhood of `a` within `[a, +∞)` if and only if it contains an interval `[a, u]` with `a < u`. -/ theorem mem_nhds_within_Ici_iff_exists_Icc_subset {α : Type u} [topological_space α] [linear_order α] [order_topology α] [no_top_order α] [densely_ordered α] {a : α} {s : set α} : s ∈ nhds_within a (set.Ici a) ↔ ∃ (u : α), a < u ∧ set.Icc a u ⊆ s := sorry /-- A set is a neighborhood of `a` within `(-∞, a]` if and only if it contains an interval `[l, a]` with `l < a`. -/ theorem mem_nhds_within_Iic_iff_exists_Icc_subset {α : Type u} [topological_space α] [linear_order α] [order_topology α] [no_bot_order α] [densely_ordered α] {a : α} {s : set α} : s ∈ nhds_within a (set.Iic a) ↔ ∃ (l : α), l < a ∧ set.Icc l a ⊆ s := sorry theorem nhds_eq_infi_abs_sub {α : Type u} [topological_space α] [linear_ordered_add_comm_group α] [order_topology α] (a : α) : nhds a = infi fun (r : α) => infi fun (H : r > 0) => filter.principal (set_of fun (b : α) => abs (a - b) < r) := sorry theorem order_topology_of_nhds_abs {α : Type u_1} [topological_space α] [linear_ordered_add_comm_group α] (h_nhds : ∀ (a : α), nhds a = infi fun (r : α) => infi fun (H : r > 0) => filter.principal (set_of fun (b : α) => abs (a - b) < r)) : order_topology α := sorry theorem linear_ordered_add_comm_group.tendsto_nhds {α : Type u} {β : Type v} [topological_space α] [linear_ordered_add_comm_group α] [order_topology α] {f : β → α} {x : filter β} {a : α} : filter.tendsto f x (nhds a) ↔ ∀ (ε : α), ε > 0 → filter.eventually (fun (b : β) => abs (f b - a) < ε) x := sorry theorem eventually_abs_sub_lt {α : Type u} [topological_space α] [linear_ordered_add_comm_group α] [order_topology α] (a : α) {ε : α} (hε : 0 < ε) : filter.eventually (fun (x : α) => abs (x - a) < ε) (nhds a) := sorry protected instance linear_ordered_add_comm_group.topological_add_group {α : Type u} [topological_space α] [linear_ordered_add_comm_group α] [order_topology α] : topological_add_group α := topological_add_group.mk (iff.mpr continuous_iff_continuous_at fun (a : α) => iff.mpr linear_ordered_add_comm_group.tendsto_nhds fun (ε : α) (ε0 : ε > 0) => filter.eventually.mono (eventually_abs_sub_lt a ε0) fun (x : α) (hx : abs (x - a) < ε) => eq.mpr (id (Eq._oldrec (Eq.refl (abs (-x - (fun (a : α) => -a) a) < ε)) (neg_sub_neg x a))) (eq.mpr (id (Eq._oldrec (Eq.refl (abs (a - x) < ε)) (abs_sub a x))) hx)) theorem continuous_abs {α : Type u} [topological_space α] [linear_ordered_add_comm_group α] [order_topology α] : continuous abs := continuous.max continuous_id continuous_neg theorem filter.tendsto.abs {α : Type u} {β : Type v} [topological_space α] [linear_ordered_add_comm_group α] [order_topology α] {f : β → α} {a : α} {l : filter β} (h : filter.tendsto f l (nhds a)) : filter.tendsto (fun (x : β) => abs (f x)) l (nhds (abs a)) := filter.tendsto.comp (continuous.tendsto continuous_abs a) h theorem continuous.abs {α : Type u} {β : Type v} [topological_space α] [linear_ordered_add_comm_group α] [order_topology α] {f : β → α} [topological_space β] (h : continuous f) : continuous fun (x : β) => abs (f x) := continuous.comp continuous_abs h theorem continuous_at.abs {α : Type u} {β : Type v} [topological_space α] [linear_ordered_add_comm_group α] [order_topology α] {f : β → α} [topological_space β] {b : β} (h : continuous_at f b) : continuous_at (fun (x : β) => abs (f x)) b := filter.tendsto.abs h theorem continuous_within_at.abs {α : Type u} {β : Type v} [topological_space α] [linear_ordered_add_comm_group α] [order_topology α] {f : β → α} [topological_space β] {b : β} {s : set β} (h : continuous_within_at f s b) : continuous_within_at (fun (x : β) => abs (f x)) s b := filter.tendsto.abs h theorem continuous_on.abs {α : Type u} {β : Type v} [topological_space α] [linear_ordered_add_comm_group α] [order_topology α] {f : β → α} [topological_space β] {s : set β} (h : continuous_on f s) : continuous_on (fun (x : β) => abs (f x)) s := fun (x : β) (hx : x ∈ s) => continuous_within_at.abs (h x hx) theorem tendsto_abs_nhds_within_zero {α : Type u} [topological_space α] [linear_ordered_add_comm_group α] [order_topology α] : filter.tendsto abs (nhds_within 0 (singleton 0ᶜ)) (nhds_within 0 (set.Ioi 0)) := filter.tendsto.inf (continuous.tendsto' continuous_abs 0 0 abs_zero) (iff.mpr filter.tendsto_principal_principal fun (x : α) => iff.mpr abs_pos) /-- In a linearly ordered additive commutative group with the order topology, if `f` tends to `C` and `g` tends to `at_top` then `f + g` tends to `at_top`. -/ theorem filter.tendsto.add_at_top {α : Type u} {β : Type v} [topological_space α] [linear_ordered_add_comm_group α] [order_topology α] {l : filter β} {f : β → α} {g : β → α} {C : α} (hf : filter.tendsto f l (nhds C)) (hg : filter.tendsto g l filter.at_top) : filter.tendsto (fun (x : β) => f x + g x) l filter.at_top := sorry /-- In a linearly ordered additive commutative group with the order topology, if `f` tends to `C` and `g` tends to `at_bot` then `f + g` tends to `at_bot`. -/ theorem filter.tendsto.add_at_bot {α : Type u} {β : Type v} [topological_space α] [linear_ordered_add_comm_group α] [order_topology α] {l : filter β} {f : β → α} {g : β → α} {C : α} (hf : filter.tendsto f l (nhds C)) (hg : filter.tendsto g l filter.at_bot) : filter.tendsto (fun (x : β) => f x + g x) l filter.at_bot := filter.tendsto.add_at_top hf hg /-- In a linearly ordered additive commutative group with the order topology, if `f` tends to `at_top` and `g` tends to `C` then `f + g` tends to `at_top`. -/ theorem filter.tendsto.at_top_add {α : Type u} {β : Type v} [topological_space α] [linear_ordered_add_comm_group α] [order_topology α] {l : filter β} {f : β → α} {g : β → α} {C : α} (hf : filter.tendsto f l filter.at_top) (hg : filter.tendsto g l (nhds C)) : filter.tendsto (fun (x : β) => f x + g x) l filter.at_top := sorry /-- In a linearly ordered additive commutative group with the order topology, if `f` tends to `at_bot` and `g` tends to `C` then `f + g` tends to `at_bot`. -/ theorem filter.tendsto.at_bot_add {α : Type u} {β : Type v} [topological_space α] [linear_ordered_add_comm_group α] [order_topology α] {l : filter β} {f : β → α} {g : β → α} {C : α} (hf : filter.tendsto f l filter.at_bot) (hg : filter.tendsto g l (nhds C)) : filter.tendsto (fun (x : β) => f x + g x) l filter.at_bot := sorry /-- In a linearly ordered field with the order topology, if `f` tends to `at_top` and `g` tends to a positive constant `C` then `f * g` tends to `at_top`. -/ theorem filter.tendsto.at_top_mul {α : Type u} {β : Type v} [linear_ordered_field α] [topological_space α] [order_topology α] {l : filter β} {f : β → α} {g : β → α} {C : α} (hC : 0 < C) (hf : filter.tendsto f l filter.at_top) (hg : filter.tendsto g l (nhds C)) : filter.tendsto (fun (x : β) => f x * g x) l filter.at_top := sorry /-- In a linearly ordered field with the order topology, if `f` tends to a positive constant `C` and `g` tends to `at_top` then `f * g` tends to `at_top`. -/ theorem filter.tendsto.mul_at_top {α : Type u} {β : Type v} [linear_ordered_field α] [topological_space α] [order_topology α] {l : filter β} {f : β → α} {g : β → α} {C : α} (hC : 0 < C) (hf : filter.tendsto f l (nhds C)) (hg : filter.tendsto g l filter.at_top) : filter.tendsto (fun (x : β) => f x * g x) l filter.at_top := sorry /-- In a linearly ordered field with the order topology, if `f` tends to `at_top` and `g` tends to a negative constant `C` then `f * g` tends to `at_bot`. -/ theorem filter.tendsto.at_top_mul_neg {α : Type u} {β : Type v} [linear_ordered_field α] [topological_space α] [order_topology α] {l : filter β} {f : β → α} {g : β → α} {C : α} (hC : C < 0) (hf : filter.tendsto f l filter.at_top) (hg : filter.tendsto g l (nhds C)) : filter.tendsto (fun (x : β) => f x * g x) l filter.at_bot := sorry /-- In a linearly ordered field with the order topology, if `f` tends to a negative constant `C` and `g` tends to `at_top` then `f * g` tends to `at_bot`. -/ theorem filter.tendsto.neg_mul_at_top {α : Type u} {β : Type v} [linear_ordered_field α] [topological_space α] [order_topology α] {l : filter β} {f : β → α} {g : β → α} {C : α} (hC : C < 0) (hf : filter.tendsto f l (nhds C)) (hg : filter.tendsto g l filter.at_top) : filter.tendsto (fun (x : β) => f x * g x) l filter.at_bot := sorry /-- In a linearly ordered field with the order topology, if `f` tends to `at_bot` and `g` tends to a positive constant `C` then `f * g` tends to `at_bot`. -/ theorem filter.tendsto.at_bot_mul {α : Type u} {β : Type v} [linear_ordered_field α] [topological_space α] [order_topology α] {l : filter β} {f : β → α} {g : β → α} {C : α} (hC : 0 < C) (hf : filter.tendsto f l filter.at_bot) (hg : filter.tendsto g l (nhds C)) : filter.tendsto (fun (x : β) => f x * g x) l filter.at_bot := sorry /-- In a linearly ordered field with the order topology, if `f` tends to `at_bot` and `g` tends to a negative constant `C` then `f * g` tends to `at_top`. -/ theorem filter.tendsto.at_bot_mul_neg {α : Type u} {β : Type v} [linear_ordered_field α] [topological_space α] [order_topology α] {l : filter β} {f : β → α} {g : β → α} {C : α} (hC : C < 0) (hf : filter.tendsto f l filter.at_bot) (hg : filter.tendsto g l (nhds C)) : filter.tendsto (fun (x : β) => f x * g x) l filter.at_top := sorry /-- In a linearly ordered field with the order topology, if `f` tends to a positive constant `C` and `g` tends to `at_bot` then `f * g` tends to `at_bot`. -/ theorem filter.tendsto.mul_at_bot {α : Type u} {β : Type v} [linear_ordered_field α] [topological_space α] [order_topology α] {l : filter β} {f : β → α} {g : β → α} {C : α} (hC : 0 < C) (hf : filter.tendsto f l (nhds C)) (hg : filter.tendsto g l filter.at_bot) : filter.tendsto (fun (x : β) => f x * g x) l filter.at_bot := sorry /-- In a linearly ordered field with the order topology, if `f` tends to a negative constant `C` and `g` tends to `at_bot` then `f * g` tends to `at_top`. -/ theorem filter.tendsto.neg_mul_at_bot {α : Type u} {β : Type v} [linear_ordered_field α] [topological_space α] [order_topology α] {l : filter β} {f : β → α} {g : β → α} {C : α} (hC : C < 0) (hf : filter.tendsto f l (nhds C)) (hg : filter.tendsto g l filter.at_bot) : filter.tendsto (fun (x : β) => f x * g x) l filter.at_top := sorry /-- The function `x ↦ x⁻¹` tends to `+∞` on the right of `0`. -/ theorem tendsto_inv_zero_at_top {α : Type u} [linear_ordered_field α] [topological_space α] [order_topology α] : filter.tendsto (fun (x : α) => x⁻¹) (nhds_within 0 (set.Ioi 0)) filter.at_top := sorry /-- The function `r ↦ r⁻¹` tends to `0` on the right as `r → +∞`. -/ theorem tendsto_inv_at_top_zero' {α : Type u} [linear_ordered_field α] [topological_space α] [order_topology α] : filter.tendsto (fun (r : α) => r⁻¹) filter.at_top (nhds_within 0 (set.Ioi 0)) := sorry theorem tendsto_inv_at_top_zero {α : Type u} [linear_ordered_field α] [topological_space α] [order_topology α] : filter.tendsto (fun (r : α) => r⁻¹) filter.at_top (nhds 0) := filter.tendsto.mono_right tendsto_inv_at_top_zero' inf_le_left theorem filter.tendsto.div_at_top {α : Type u} {β : Type v} [linear_ordered_field α] [topological_space α] [order_topology α] [has_continuous_mul α] {f : β → α} {g : β → α} {l : filter β} {a : α} (h : filter.tendsto f l (nhds a)) (hg : filter.tendsto g l filter.at_top) : filter.tendsto (fun (x : β) => f x / g x) l (nhds 0) := sorry theorem tendsto.inv_tendsto_at_top {α : Type u} {β : Type v} [linear_ordered_field α] [topological_space α] [order_topology α] {l : filter β} {f : β → α} (h : filter.tendsto f l filter.at_top) : filter.tendsto (f⁻¹) l (nhds 0) := filter.tendsto.comp tendsto_inv_at_top_zero h theorem tendsto.inv_tendsto_zero {α : Type u} {β : Type v} [linear_ordered_field α] [topological_space α] [order_topology α] {l : filter β} {f : β → α} (h : filter.tendsto f l (nhds_within 0 (set.Ioi 0))) : filter.tendsto (f⁻¹) l filter.at_top := filter.tendsto.comp tendsto_inv_zero_at_top h /-- The function `x^(-n)` tends to `0` at `+∞` for any positive natural `n`. A version for positive real powers exists as `tendsto_rpow_neg_at_top`. -/ theorem tendsto_pow_neg_at_top {α : Type u} [linear_ordered_field α] [topological_space α] [order_topology α] {n : ℕ} (hn : 1 ≤ n) : filter.tendsto (fun (x : α) => x ^ (-↑n)) filter.at_top (nhds 0) := filter.tendsto.congr (fun (x : α) => Eq.symm (fpow_neg x ↑n)) (tendsto.inv_tendsto_at_top (filter.tendsto_pow_at_top hn)) theorem preimage_neg {α : Type u} [add_group α] : set.preimage Neg.neg = set.image Neg.neg := Eq.symm (set.image_eq_preimage_of_inverse neg_neg neg_neg) theorem filter.map_neg {α : Type u} [add_group α] : filter.map Neg.neg = filter.comap Neg.neg := funext fun (f : filter α) => filter.map_eq_comap_of_inverse (funext neg_neg) (funext neg_neg) theorem is_lub.nhds_within_ne_bot {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {s : set α} (ha : is_lub s a) (hs : set.nonempty s) : filter.ne_bot (nhds_within a s) := sorry theorem is_glb.nhds_within_ne_bot {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {s : set α} : is_glb s a → set.nonempty s → filter.ne_bot (nhds_within a s) := is_lub.nhds_within_ne_bot theorem is_lub_of_mem_nhds {α : Type u} [topological_space α] [linear_order α] [order_topology α] {s : set α} {a : α} {f : filter α} (hsa : a ∈ upper_bounds s) (hsf : s ∈ f) [filter.ne_bot (f ⊓ nhds a)] : is_lub s a := sorry theorem is_glb_of_mem_nhds {α : Type u} [topological_space α] [linear_order α] [order_topology α] {s : set α} {a : α} {f : filter α} : a ∈ lower_bounds s → s ∈ f → filter.ne_bot (f ⊓ nhds a) → is_glb s a := is_lub_of_mem_nhds theorem is_lub_of_is_lub_of_tendsto {α : Type u} {β : Type v} [topological_space α] [topological_space β] [linear_order α] [linear_order β] [order_topology α] [order_topology β] {f : α → β} {s : set α} {a : α} {b : β} (hf : ∀ (x : α), x ∈ s → ∀ (y : α), y ∈ s → x ≤ y → f x ≤ f y) (ha : is_lub s a) (hs : set.nonempty s) (hb : filter.tendsto f (nhds_within a s) (nhds b)) : is_lub (f '' s) b := sorry theorem is_glb_of_is_glb_of_tendsto {α : Type u} {β : Type v} [topological_space α] [topological_space β] [linear_order α] [linear_order β] [order_topology α] [order_topology β] {f : α → β} {s : set α} {a : α} {b : β} (hf : ∀ (x : α), x ∈ s → ∀ (y : α), y ∈ s → x ≤ y → f x ≤ f y) : is_glb s a → set.nonempty s → filter.tendsto f (nhds_within a s) (nhds b) → is_glb (f '' s) b := is_lub_of_is_lub_of_tendsto fun (x : order_dual α) (hx : x ∈ s) (y : order_dual α) (hy : y ∈ s) => hf y hy x hx theorem is_glb_of_is_lub_of_tendsto {α : Type u} {β : Type v} [topological_space α] [topological_space β] [linear_order α] [linear_order β] [order_topology α] [order_topology β] {f : α → β} {s : set α} {a : α} {b : β} : (∀ (x : α), x ∈ s → ∀ (y : α), y ∈ s → x ≤ y → f y ≤ f x) → is_lub s a → set.nonempty s → filter.tendsto f (nhds_within a s) (nhds b) → is_glb (f '' s) b := is_lub_of_is_lub_of_tendsto theorem is_lub_of_is_glb_of_tendsto {α : Type u} {β : Type v} [topological_space α] [topological_space β] [linear_order α] [linear_order β] [order_topology α] [order_topology β] {f : α → β} {s : set α} {a : α} {b : β} : (∀ (x : α), x ∈ s → ∀ (y : α), y ∈ s → x ≤ y → f y ≤ f x) → is_glb s a → set.nonempty s → filter.tendsto f (nhds_within a s) (nhds b) → is_lub (f '' s) b := is_glb_of_is_glb_of_tendsto theorem mem_closure_of_is_lub {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {s : set α} (ha : is_lub s a) (hs : set.nonempty s) : a ∈ closure s := eq.mpr (id (Eq._oldrec (Eq.refl (a ∈ closure s)) closure_eq_cluster_pts)) (is_lub.nhds_within_ne_bot ha hs) theorem mem_of_is_lub_of_is_closed {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {s : set α} (ha : is_lub s a) (hs : set.nonempty s) (sc : is_closed s) : a ∈ s := eq.mpr (id (Eq._oldrec (Eq.refl (a ∈ s)) (Eq.symm (is_closed.closure_eq sc)))) (mem_closure_of_is_lub ha hs) theorem mem_closure_of_is_glb {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {s : set α} (ha : is_glb s a) (hs : set.nonempty s) : a ∈ closure s := eq.mpr (id (Eq._oldrec (Eq.refl (a ∈ closure s)) closure_eq_cluster_pts)) (is_glb.nhds_within_ne_bot ha hs) theorem mem_of_is_glb_of_is_closed {α : Type u} [topological_space α] [linear_order α] [order_topology α] {a : α} {s : set α} (ha : is_glb s a) (hs : set.nonempty s) (sc : is_closed s) : a ∈ s := eq.mpr (id (Eq._oldrec (Eq.refl (a ∈ s)) (Eq.symm (is_closed.closure_eq sc)))) (mem_closure_of_is_glb ha hs) /-- A compact set is bounded below -/ theorem is_compact.bdd_below {α : Type u} [topological_space α] [linear_order α] [order_closed_topology α] [Nonempty α] {s : set α} (hs : is_compact s) : bdd_below s := sorry /-- A compact set is bounded above -/ theorem is_compact.bdd_above {α : Type u} [topological_space α] [linear_order α] [order_topology α] [Nonempty α] {s : set α} : is_compact s → bdd_above s := is_compact.bdd_below /-- The closure of the interval `(a, +∞)` is the closed interval `[a, +∞)`, unless `a` is a top element. -/ theorem closure_Ioi' {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] {a : α} {b : α} (hab : a < b) : closure (set.Ioi a) = set.Ici a := sorry /-- The closure of the interval `(a, +∞)` is the closed interval `[a, +∞)`. -/ @[simp] theorem closure_Ioi {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] (a : α) [no_top_order α] : closure (set.Ioi a) = set.Ici a := (fun (_a : ∃ (a' : α), a < a') => Exists.dcases_on _a fun (w : α) (h : a < w) => idRhs (closure (set.Ioi a) = set.Ici a) (closure_Ioi' h)) (no_top a) /-- The closure of the interval `(-∞, a)` is the closed interval `(-∞, a]`, unless `a` is a bottom element. -/ theorem closure_Iio' {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] {a : α} {b : α} (hab : b < a) : closure (set.Iio a) = set.Iic a := sorry /-- The closure of the interval `(-∞, a)` is the interval `(-∞, a]`. -/ @[simp] theorem closure_Iio {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] (a : α) [no_bot_order α] : closure (set.Iio a) = set.Iic a := (fun (_a : ∃ (a' : α), a' < a) => Exists.dcases_on _a fun (w : α) (h : w < a) => idRhs (closure (set.Iio a) = set.Iic a) (closure_Iio' h)) (no_bot a) /-- The closure of the open interval `(a, b)` is the closed interval `[a, b]`. -/ @[simp] theorem closure_Ioo {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] {a : α} {b : α} (hab : a < b) : closure (set.Ioo a b) = set.Icc a b := sorry /-- The closure of the interval `(a, b]` is the closed interval `[a, b]`. -/ @[simp] theorem closure_Ioc {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] {a : α} {b : α} (hab : a < b) : closure (set.Ioc a b) = set.Icc a b := sorry /-- The closure of the interval `[a, b)` is the closed interval `[a, b]`. -/ @[simp] theorem closure_Ico {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] {a : α} {b : α} (hab : a < b) : closure (set.Ico a b) = set.Icc a b := sorry @[simp] theorem interior_Ici {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] [no_bot_order α] {a : α} : interior (set.Ici a) = set.Ioi a := sorry @[simp] theorem interior_Iic {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] [no_top_order α] {a : α} : interior (set.Iic a) = set.Iio a := sorry @[simp] theorem interior_Icc {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] [no_bot_order α] [no_top_order α] {a : α} {b : α} : interior (set.Icc a b) = set.Ioo a b := sorry @[simp] theorem interior_Ico {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] [no_bot_order α] {a : α} {b : α} : interior (set.Ico a b) = set.Ioo a b := sorry @[simp] theorem interior_Ioc {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] [no_top_order α] {a : α} {b : α} : interior (set.Ioc a b) = set.Ioo a b := sorry @[simp] theorem frontier_Ici {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] [no_bot_order α] {a : α} : frontier (set.Ici a) = singleton a := sorry @[simp] theorem frontier_Iic {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] [no_top_order α] {a : α} : frontier (set.Iic a) = singleton a := sorry @[simp] theorem frontier_Ioi {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] [no_top_order α] {a : α} : frontier (set.Ioi a) = singleton a := sorry @[simp] theorem frontier_Iio {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] [no_bot_order α] {a : α} : frontier (set.Iio a) = singleton a := sorry @[simp] theorem frontier_Icc {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] [no_bot_order α] [no_top_order α] {a : α} {b : α} (h : a < b) : frontier (set.Icc a b) = insert a (singleton b) := sorry @[simp] theorem frontier_Ioo {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] {a : α} {b : α} (h : a < b) : frontier (set.Ioo a b) = insert a (singleton b) := sorry @[simp] theorem frontier_Ico {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] [no_bot_order α] {a : α} {b : α} (h : a < b) : frontier (set.Ico a b) = insert a (singleton b) := sorry @[simp] theorem frontier_Ioc {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] [no_top_order α] {a : α} {b : α} (h : a < b) : frontier (set.Ioc a b) = insert a (singleton b) := sorry theorem nhds_within_Ioi_ne_bot' {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] {a : α} {b : α} {c : α} (H₁ : a < c) (H₂ : a ≤ b) : filter.ne_bot (nhds_within b (set.Ioi a)) := iff.mp mem_closure_iff_nhds_within_ne_bot (eq.mpr (id (Eq._oldrec (Eq.refl (b ∈ closure (set.Ioi a))) (closure_Ioi' H₁))) H₂) theorem nhds_within_Ioi_ne_bot {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] [no_top_order α] {a : α} {b : α} (H : a ≤ b) : filter.ne_bot (nhds_within b (set.Ioi a)) := sorry theorem nhds_within_Ioi_self_ne_bot' {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] {a : α} {b : α} (H : a < b) : filter.ne_bot (nhds_within a (set.Ioi a)) := nhds_within_Ioi_ne_bot' H (le_refl a) instance nhds_within_Ioi_self_ne_bot {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] [no_top_order α] (a : α) : filter.ne_bot (nhds_within a (set.Ioi a)) := nhds_within_Ioi_ne_bot (le_refl a) theorem nhds_within_Iio_ne_bot' {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] {a : α} {b : α} {c : α} (H₁ : a < c) (H₂ : b ≤ c) : filter.ne_bot (nhds_within b (set.Iio c)) := iff.mp mem_closure_iff_nhds_within_ne_bot (eq.mpr (id (Eq._oldrec (Eq.refl (b ∈ closure (set.Iio c))) (closure_Iio' H₁))) H₂) theorem nhds_within_Iio_ne_bot {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] [no_bot_order α] {a : α} {b : α} (H : a ≤ b) : filter.ne_bot (nhds_within a (set.Iio b)) := sorry theorem nhds_within_Iio_self_ne_bot' {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] {a : α} {b : α} (H : a < b) : filter.ne_bot (nhds_within b (set.Iio b)) := nhds_within_Iio_ne_bot' H (le_refl b) instance nhds_within_Iio_self_ne_bot {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] [no_bot_order α] (a : α) : filter.ne_bot (nhds_within a (set.Iio a)) := nhds_within_Iio_ne_bot (le_refl a) theorem comap_coe_nhds_within_Iio_of_Ioo_subset {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] {b : α} {s : set α} (hb : s ⊆ set.Iio b) (hs : set.nonempty s → ∃ (a : α), ∃ (H : a < b), set.Ioo a b ⊆ s) : filter.comap coe (nhds_within b (set.Iio b)) = filter.at_top := sorry theorem comap_coe_nhds_within_Ioi_of_Ioo_subset {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] {a : α} {s : set α} (ha : s ⊆ set.Ioi a) (hs : set.nonempty s → ∃ (b : α), ∃ (H : b > a), set.Ioo a b ⊆ s) : filter.comap coe (nhds_within a (set.Ioi a)) = filter.at_bot := sorry theorem map_coe_at_top_of_Ioo_subset {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] {b : α} {s : set α} (hb : s ⊆ set.Iio b) (hs : ∀ (a' : α) (H : a' < b), ∃ (a : α), ∃ (H : a < b), set.Ioo a b ⊆ s) : filter.map coe filter.at_top = nhds_within b (set.Iio b) := sorry theorem map_coe_at_bot_of_Ioo_subset {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] {a : α} {s : set α} (ha : s ⊆ set.Ioi a) (hs : ∀ (b' : α) (H : b' > a), ∃ (b : α), ∃ (H : b > a), set.Ioo a b ⊆ s) : filter.map coe filter.at_bot = nhds_within a (set.Ioi a) := sorry /-- The `at_top` filter for an open interval `Ioo a b` comes from the left-neighbourhoods filter at the right endpoint in the ambient order. -/ theorem comap_coe_Ioo_nhds_within_Iio {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] (a : α) (b : α) : filter.comap coe (nhds_within b (set.Iio b)) = filter.at_top := comap_coe_nhds_within_Iio_of_Ioo_subset set.Ioo_subset_Iio_self fun (h : set.nonempty (set.Ioo a b)) => Exists.intro a (Exists.intro (iff.mp set.nonempty_Ioo h) (set.subset.refl (set.Ioo a b))) /-- The `at_bot` filter for an open interval `Ioo a b` comes from the right-neighbourhoods filter at the left endpoint in the ambient order. -/ theorem comap_coe_Ioo_nhds_within_Ioi {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] (a : α) (b : α) : filter.comap coe (nhds_within a (set.Ioi a)) = filter.at_bot := comap_coe_nhds_within_Ioi_of_Ioo_subset set.Ioo_subset_Ioi_self fun (h : set.nonempty (set.Ioo a b)) => Exists.intro b (Exists.intro (iff.mp set.nonempty_Ioo h) (set.subset.refl (set.Ioo a b))) theorem comap_coe_Ioi_nhds_within_Ioi {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] (a : α) : filter.comap coe (nhds_within a (set.Ioi a)) = filter.at_bot := sorry theorem comap_coe_Iio_nhds_within_Iio {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] (a : α) : filter.comap coe (nhds_within a (set.Iio a)) = filter.at_top := comap_coe_Ioi_nhds_within_Ioi a @[simp] theorem map_coe_Ioo_at_top {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] {a : α} {b : α} (h : a < b) : filter.map coe filter.at_top = nhds_within b (set.Iio b) := map_coe_at_top_of_Ioo_subset set.Ioo_subset_Iio_self fun (_x : α) (_x : _x < b) => Exists.intro a (Exists.intro h (set.subset.refl (set.Ioo a b))) @[simp] theorem map_coe_Ioo_at_bot {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] {a : α} {b : α} (h : a < b) : filter.map coe filter.at_bot = nhds_within a (set.Ioi a) := map_coe_at_bot_of_Ioo_subset set.Ioo_subset_Ioi_self fun (_x : α) (_x : _x > a) => Exists.intro b (Exists.intro h (set.subset.refl (set.Ioo a b))) @[simp] theorem map_coe_Ioi_at_bot {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] (a : α) : filter.map coe filter.at_bot = nhds_within a (set.Ioi a) := map_coe_at_bot_of_Ioo_subset (set.subset.refl (set.Ioi a)) fun (b : α) (hb : b > a) => Exists.intro b (Exists.intro hb set.Ioo_subset_Ioi_self) @[simp] theorem map_coe_Iio_at_top {α : Type u} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] (a : α) : filter.map coe filter.at_top = nhds_within a (set.Iio a) := map_coe_Ioi_at_bot a @[simp] theorem tendsto_comp_coe_Ioo_at_top {α : Type u} {β : Type v} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] {a : α} {b : α} {l : filter β} {f : α → β} (h : a < b) : filter.tendsto (fun (x : ↥(set.Ioo a b)) => f ↑x) filter.at_top l ↔ filter.tendsto f (nhds_within b (set.Iio b)) l := sorry @[simp] theorem tendsto_comp_coe_Ioo_at_bot {α : Type u} {β : Type v} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] {a : α} {b : α} {l : filter β} {f : α → β} (h : a < b) : filter.tendsto (fun (x : ↥(set.Ioo a b)) => f ↑x) filter.at_bot l ↔ filter.tendsto f (nhds_within a (set.Ioi a)) l := sorry @[simp] theorem tendsto_comp_coe_Ioi_at_bot {α : Type u} {β : Type v} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] {a : α} {l : filter β} {f : α → β} : filter.tendsto (fun (x : ↥(set.Ioi a)) => f ↑x) filter.at_bot l ↔ filter.tendsto f (nhds_within a (set.Ioi a)) l := sorry @[simp] theorem tendsto_comp_coe_Iio_at_top {α : Type u} {β : Type v} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] {a : α} {l : filter β} {f : α → β} : filter.tendsto (fun (x : ↥(set.Iio a)) => f ↑x) filter.at_top l ↔ filter.tendsto f (nhds_within a (set.Iio a)) l := sorry @[simp] theorem tendsto_Ioo_at_top {α : Type u} {β : Type v} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] {a : α} {b : α} {l : filter β} {f : β → ↥(set.Ioo a b)} : filter.tendsto f l filter.at_top ↔ filter.tendsto (fun (x : β) => ↑(f x)) l (nhds_within b (set.Iio b)) := sorry @[simp] theorem tendsto_Ioo_at_bot {α : Type u} {β : Type v} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] {a : α} {b : α} {l : filter β} {f : β → ↥(set.Ioo a b)} : filter.tendsto f l filter.at_bot ↔ filter.tendsto (fun (x : β) => ↑(f x)) l (nhds_within a (set.Ioi a)) := sorry @[simp] theorem tendsto_Ioi_at_bot {α : Type u} {β : Type v} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] {a : α} {l : filter β} {f : β → ↥(set.Ioi a)} : filter.tendsto f l filter.at_bot ↔ filter.tendsto (fun (x : β) => ↑(f x)) l (nhds_within a (set.Ioi a)) := sorry @[simp] theorem tendsto_Iio_at_top {α : Type u} {β : Type v} [topological_space α] [linear_order α] [order_topology α] [densely_ordered α] {a : α} {l : filter β} {f : β → ↥(set.Iio a)} : filter.tendsto f l filter.at_top ↔ filter.tendsto (fun (x : β) => ↑(f x)) l (nhds_within a (set.Iio a)) := sorry theorem Sup_mem_closure {α : Type u} [topological_space α] [complete_linear_order α] [order_topology α] {s : set α} (hs : set.nonempty s) : Sup s ∈ closure s := mem_closure_of_is_lub (is_lub_Sup s) hs theorem Inf_mem_closure {α : Type u} [topological_space α] [complete_linear_order α] [order_topology α] {s : set α} (hs : set.nonempty s) : Inf s ∈ closure s := mem_closure_of_is_glb (is_glb_Inf s) hs theorem is_closed.Sup_mem {α : Type u} [topological_space α] [complete_linear_order α] [order_topology α] {s : set α} (hs : set.nonempty s) (hc : is_closed s) : Sup s ∈ s := mem_of_is_lub_of_is_closed (is_lub_Sup s) hs hc theorem is_closed.Inf_mem {α : Type u} [topological_space α] [complete_linear_order α] [order_topology α] {s : set α} (hs : set.nonempty s) (hc : is_closed s) : Inf s ∈ s := mem_of_is_glb_of_is_closed (is_glb_Inf s) hs hc /-- A monotone function continuous at the supremum of a nonempty set sends this supremum to the supremum of the image of this set. -/ theorem map_Sup_of_continuous_at_of_monotone' {α : Type u} {β : Type v} [complete_linear_order α] [topological_space α] [order_topology α] [complete_linear_order β] [topological_space β] [order_topology β] {f : α → β} {s : set α} (Cf : continuous_at f (Sup s)) (Mf : monotone f) (hs : set.nonempty s) : f (Sup s) = Sup (f '' s) := sorry --This is a particular case of the more general is_lub_of_is_lub_of_tendsto /-- A monotone function `s` sending `bot` to `bot` and continuous at the supremum of a set sends this supremum to the supremum of the image of this set. -/ theorem map_Sup_of_continuous_at_of_monotone {α : Type u} {β : Type v} [complete_linear_order α] [topological_space α] [order_topology α] [complete_linear_order β] [topological_space β] [order_topology β] {f : α → β} {s : set α} (Cf : continuous_at f (Sup s)) (Mf : monotone f) (fbot : f ⊥ = ⊥) : f (Sup s) = Sup (f '' s) := sorry /-- A monotone function continuous at the indexed supremum over a nonempty `Sort` sends this indexed supremum to the indexed supremum of the composition. -/ theorem map_supr_of_continuous_at_of_monotone' {α : Type u} {β : Type v} [complete_linear_order α] [topological_space α] [order_topology α] [complete_linear_order β] [topological_space β] [order_topology β] {ι : Sort u_1} [Nonempty ι] {f : α → β} {g : ι → α} (Cf : continuous_at f (supr g)) (Mf : monotone f) : f (supr fun (i : ι) => g i) = supr fun (i : ι) => f (g i) := sorry /-- If a monotone function sending `bot` to `bot` is continuous at the indexed supremum over a `Sort`, then it sends this indexed supremum to the indexed supremum of the composition. -/ theorem map_supr_of_continuous_at_of_monotone {α : Type u} {β : Type v} [complete_linear_order α] [topological_space α] [order_topology α] [complete_linear_order β] [topological_space β] [order_topology β] {ι : Sort u_1} {f : α → β} {g : ι → α} (Cf : continuous_at f (supr g)) (Mf : monotone f) (fbot : f ⊥ = ⊥) : f (supr fun (i : ι) => g i) = supr fun (i : ι) => f (g i) := sorry /-- A monotone function continuous at the infimum of a nonempty set sends this infimum to the infimum of the image of this set. -/ theorem map_Inf_of_continuous_at_of_monotone' {α : Type u} {β : Type v} [complete_linear_order α] [topological_space α] [order_topology α] [complete_linear_order β] [topological_space β] [order_topology β] {f : α → β} {s : set α} (Cf : continuous_at f (Inf s)) (Mf : monotone f) (hs : set.nonempty s) : f (Inf s) = Inf (f '' s) := map_Sup_of_continuous_at_of_monotone' Cf (monotone.order_dual Mf) hs /-- A monotone function `s` sending `top` to `top` and continuous at the infimum of a set sends this infimum to the infimum of the image of this set. -/ theorem map_Inf_of_continuous_at_of_monotone {α : Type u} {β : Type v} [complete_linear_order α] [topological_space α] [order_topology α] [complete_linear_order β] [topological_space β] [order_topology β] {f : α → β} {s : set α} (Cf : continuous_at f (Inf s)) (Mf : monotone f) (ftop : f ⊤ = ⊤) : f (Inf s) = Inf (f '' s) := map_Sup_of_continuous_at_of_monotone Cf (monotone.order_dual Mf) ftop /-- A monotone function continuous at the indexed infimum over a nonempty `Sort` sends this indexed infimum to the indexed infimum of the composition. -/ theorem map_infi_of_continuous_at_of_monotone' {α : Type u} {β : Type v} [complete_linear_order α] [topological_space α] [order_topology α] [complete_linear_order β] [topological_space β] [order_topology β] {ι : Sort u_1} [Nonempty ι] {f : α → β} {g : ι → α} (Cf : continuous_at f (infi g)) (Mf : monotone f) : f (infi fun (i : ι) => g i) = infi fun (i : ι) => f (g i) := map_supr_of_continuous_at_of_monotone' Cf (monotone.order_dual Mf) /-- If a monotone function sending `top` to `top` is continuous at the indexed infimum over a `Sort`, then it sends this indexed infimum to the indexed infimum of the composition. -/ theorem map_infi_of_continuous_at_of_monotone {α : Type u} {β : Type v} [complete_linear_order α] [topological_space α] [order_topology α] [complete_linear_order β] [topological_space β] [order_topology β] {ι : Sort u_1} {f : α → β} {g : ι → α} (Cf : continuous_at f (infi g)) (Mf : monotone f) (ftop : f ⊤ = ⊤) : f (infi g) = infi (f ∘ g) := map_supr_of_continuous_at_of_monotone Cf (monotone.order_dual Mf) ftop theorem cSup_mem_closure {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] {s : set α} (hs : set.nonempty s) (B : bdd_above s) : Sup s ∈ closure s := mem_closure_of_is_lub (is_lub_cSup hs B) hs theorem cInf_mem_closure {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] {s : set α} (hs : set.nonempty s) (B : bdd_below s) : Inf s ∈ closure s := mem_closure_of_is_glb (is_glb_cInf hs B) hs theorem is_closed.cSup_mem {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] {s : set α} (hc : is_closed s) (hs : set.nonempty s) (B : bdd_above s) : Sup s ∈ s := mem_of_is_lub_of_is_closed (is_lub_cSup hs B) hs hc theorem is_closed.cInf_mem {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] {s : set α} (hc : is_closed s) (hs : set.nonempty s) (B : bdd_below s) : Inf s ∈ s := mem_of_is_glb_of_is_closed (is_glb_cInf hs B) hs hc /-- If a monotone function is continuous at the supremum of a nonempty bounded above set `s`, then it sends this supremum to the supremum of the image of `s`. -/ theorem map_cSup_of_continuous_at_of_monotone {α : Type u} {β : Type v} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] [conditionally_complete_linear_order β] [topological_space β] [order_topology β] {f : α → β} {s : set α} (Cf : continuous_at f (Sup s)) (Mf : monotone f) (ne : set.nonempty s) (H : bdd_above s) : f (Sup s) = Sup (f '' s) := sorry /-- If a monotone function is continuous at the indexed supremum of a bounded function on a nonempty `Sort`, then it sends this supremum to the supremum of the composition. -/ theorem map_csupr_of_continuous_at_of_monotone {α : Type u} {β : Type v} {γ : Type w} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] [conditionally_complete_linear_order β] [topological_space β] [order_topology β] [Nonempty γ] {f : α → β} {g : γ → α} (Cf : continuous_at f (supr fun (i : γ) => g i)) (Mf : monotone f) (H : bdd_above (set.range g)) : f (supr fun (i : γ) => g i) = supr fun (i : γ) => f (g i) := sorry /-- If a monotone function is continuous at the infimum of a nonempty bounded below set `s`, then it sends this infimum to the infimum of the image of `s`. -/ theorem map_cInf_of_continuous_at_of_monotone {α : Type u} {β : Type v} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] [conditionally_complete_linear_order β] [topological_space β] [order_topology β] {f : α → β} {s : set α} (Cf : continuous_at f (Inf s)) (Mf : monotone f) (ne : set.nonempty s) (H : bdd_below s) : f (Inf s) = Inf (f '' s) := map_cSup_of_continuous_at_of_monotone Cf (monotone.order_dual Mf) ne H /-- A continuous monotone function sends indexed infimum to indexed infimum in conditionally complete linear order, under a boundedness assumption. -/ theorem map_cinfi_of_continuous_at_of_monotone {α : Type u} {β : Type v} {γ : Type w} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] [conditionally_complete_linear_order β] [topological_space β] [order_topology β] [Nonempty γ] {f : α → β} {g : γ → α} (Cf : continuous_at f (infi fun (i : γ) => g i)) (Mf : monotone f) (H : bdd_below (set.range g)) : f (infi fun (i : γ) => g i) = infi fun (i : γ) => f (g i) := map_csupr_of_continuous_at_of_monotone Cf (monotone.order_dual Mf) H /-- A bounded connected subset of a conditionally complete linear order includes the open interval `(Inf s, Sup s)`. -/ theorem is_connected.Ioo_cInf_cSup_subset {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] {s : set α} (hs : is_connected s) (hb : bdd_below s) (ha : bdd_above s) : set.Ioo (Inf s) (Sup s) ⊆ s := sorry theorem eq_Icc_cInf_cSup_of_connected_bdd_closed {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] {s : set α} (hc : is_connected s) (hb : bdd_below s) (ha : bdd_above s) (hcl : is_closed s) : s = set.Icc (Inf s) (Sup s) := set.subset.antisymm (subset_Icc_cInf_cSup hb ha) (is_connected.Icc_subset hc (is_closed.cInf_mem hcl (is_connected.nonempty hc) hb) (is_closed.cSup_mem hcl (is_connected.nonempty hc) ha)) theorem is_preconnected.Ioi_cInf_subset {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] {s : set α} (hs : is_preconnected s) (hb : bdd_below s) (ha : ¬bdd_above s) : set.Ioi (Inf s) ⊆ s := sorry theorem is_preconnected.Iio_cSup_subset {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] {s : set α} (hs : is_preconnected s) (hb : ¬bdd_below s) (ha : bdd_above s) : set.Iio (Sup s) ⊆ s := is_preconnected.Ioi_cInf_subset hs ha hb /-- A preconnected set in a conditionally complete linear order is either one of the intervals `[Inf s, Sup s]`, `[Inf s, Sup s)`, `(Inf s, Sup s]`, `(Inf s, Sup s)`, `[Inf s, +∞)`, `(Inf s, +∞)`, `(-∞, Sup s]`, `(-∞, Sup s)`, `(-∞, +∞)`, or `∅`. The converse statement requires `α` to be densely ordererd. -/ theorem is_preconnected.mem_intervals {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] {s : set α} (hs : is_preconnected s) : s ∈ insert (set.Icc (Inf s) (Sup s)) (insert (set.Ico (Inf s) (Sup s)) (insert (set.Ioc (Inf s) (Sup s)) (insert (set.Ioo (Inf s) (Sup s)) (insert (set.Ici (Inf s)) (insert (set.Ioi (Inf s)) (insert (set.Iic (Sup s)) (insert (set.Iio (Sup s)) (insert set.univ (singleton ∅))))))))) := sorry /-- A preconnected set is either one of the intervals `Icc`, `Ico`, `Ioc`, `Ioo`, `Ici`, `Ioi`, `Iic`, `Iio`, or `univ`, or `∅`. The converse statement requires `α` to be densely ordererd. Though one can represent `∅` as `(Inf s, Inf s)`, we include it into the list of possible cases to improve readability. -/ theorem set_of_is_preconnected_subset_of_ordered {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] : (set_of fun (s : set α) => is_preconnected s) ⊆ set.range (function.uncurry set.Icc) ∪ set.range (function.uncurry set.Ico) ∪ set.range (function.uncurry set.Ioc) ∪ set.range (function.uncurry set.Ioo) ∪ (set.range set.Ici ∪ set.range set.Ioi ∪ set.range set.Iic ∪ set.range set.Iio ∪ insert set.univ (singleton ∅)) := sorry /-- A "continuous induction principle" for a closed interval: if a set `s` meets `[a, b]` on a closed subset, contains `a`, and the set `s ∩ [a, b)` has no maximal point, then `b ∈ s`. -/ theorem is_closed.mem_of_ge_of_forall_exists_gt {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] {a : α} {b : α} {s : set α} (hs : is_closed (s ∩ set.Icc a b)) (ha : a ∈ s) (hab : a ≤ b) (hgt : ∀ (x : α), x ∈ s ∩ set.Ico a b → set.nonempty (s ∩ set.Ioc x b)) : b ∈ s := sorry /-- A "continuous induction principle" for a closed interval: if a set `s` meets `[a, b]` on a closed subset, contains `a`, and for any `a ≤ x < y ≤ b`, `x ∈ s`, the set `s ∩ (x, y]` is not empty, then `[a, b] ⊆ s`. -/ theorem is_closed.Icc_subset_of_forall_exists_gt {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] {a : α} {b : α} {s : set α} (hs : is_closed (s ∩ set.Icc a b)) (ha : a ∈ s) (hgt : ∀ (x : α), x ∈ s ∩ set.Ico a b → ∀ (y : α), y ∈ set.Ioi x → set.nonempty (s ∩ set.Ioc x y)) : set.Icc a b ⊆ s := sorry /-- A "continuous induction principle" for a closed interval: if a set `s` meets `[a, b]` on a closed subset, contains `a`, and for any `x ∈ s ∩ [a, b)` the set `s` includes some open neighborhood of `x` within `(x, +∞)`, then `[a, b] ⊆ s`. -/ theorem is_closed.Icc_subset_of_forall_mem_nhds_within {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] [densely_ordered α] {a : α} {b : α} {s : set α} (hs : is_closed (s ∩ set.Icc a b)) (ha : a ∈ s) (hgt : ∀ (x : α), x ∈ s ∩ set.Ico a b → s ∈ nhds_within x (set.Ioi x)) : set.Icc a b ⊆ s := sorry /-- A closed interval in a densely ordered conditionally complete linear order is preconnected. -/ theorem is_preconnected_Icc {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] [densely_ordered α] {a : α} {b : α} : is_preconnected (set.Icc a b) := sorry theorem is_preconnected_interval {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] [densely_ordered α] {a : α} {b : α} : is_preconnected (set.interval a b) := is_preconnected_Icc theorem is_preconnected_iff_ord_connected {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] [densely_ordered α] {s : set α} : is_preconnected s ↔ set.ord_connected s := sorry theorem is_preconnected.ord_connected {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] [densely_ordered α] {s : set α} : is_preconnected s → set.ord_connected s := iff.mp is_preconnected_iff_ord_connected theorem is_preconnected_Ici {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] [densely_ordered α] {a : α} : is_preconnected (set.Ici a) := set.ord_connected.is_preconnected set.ord_connected_Ici theorem is_preconnected_Iic {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] [densely_ordered α] {a : α} : is_preconnected (set.Iic a) := set.ord_connected.is_preconnected set.ord_connected_Iic theorem is_preconnected_Iio {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] [densely_ordered α] {a : α} : is_preconnected (set.Iio a) := set.ord_connected.is_preconnected set.ord_connected_Iio theorem is_preconnected_Ioi {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] [densely_ordered α] {a : α} : is_preconnected (set.Ioi a) := set.ord_connected.is_preconnected set.ord_connected_Ioi theorem is_preconnected_Ioo {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] [densely_ordered α] {a : α} {b : α} : is_preconnected (set.Ioo a b) := set.ord_connected.is_preconnected set.ord_connected_Ioo theorem is_preconnected_Ioc {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] [densely_ordered α] {a : α} {b : α} : is_preconnected (set.Ioc a b) := set.ord_connected.is_preconnected set.ord_connected_Ioc theorem is_preconnected_Ico {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] [densely_ordered α] {a : α} {b : α} : is_preconnected (set.Ico a b) := set.ord_connected.is_preconnected set.ord_connected_Ico protected instance ordered_connected_space {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] [densely_ordered α] : preconnected_space α := preconnected_space.mk (set.ord_connected.is_preconnected set.ord_connected_univ) /-- In a dense conditionally complete linear order, the set of preconnected sets is exactly the set of the intervals `Icc`, `Ico`, `Ioc`, `Ioo`, `Ici`, `Ioi`, `Iic`, `Iio`, `(-∞, +∞)`, or `∅`. Though one can represent `∅` as `(Inf s, Inf s)`, we include it into the list of possible cases to improve readability. -/ theorem set_of_is_preconnected_eq_of_ordered {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] [densely_ordered α] : (set_of fun (s : set α) => is_preconnected s) = set.range (function.uncurry set.Icc) ∪ set.range (function.uncurry set.Ico) ∪ set.range (function.uncurry set.Ioc) ∪ set.range (function.uncurry set.Ioo) ∪ (set.range set.Ici ∪ set.range set.Ioi ∪ set.range set.Iic ∪ set.range set.Iio ∪ insert set.univ (singleton ∅)) := sorry /--Intermediate Value Theorem for continuous functions on closed intervals, case `f a ≤ t ≤ f b`.-/ theorem intermediate_value_Icc {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] [densely_ordered α] {δ : Type u_1} [linear_order δ] [topological_space δ] [order_closed_topology δ] {a : α} {b : α} (hab : a ≤ b) {f : α → δ} (hf : continuous_on f (set.Icc a b)) : set.Icc (f a) (f b) ⊆ f '' set.Icc a b := is_preconnected.intermediate_value is_preconnected_Icc (iff.mpr set.left_mem_Icc hab) (iff.mpr set.right_mem_Icc hab) hf /--Intermediate Value Theorem for continuous functions on closed intervals, case `f a ≥ t ≥ f b`.-/ theorem intermediate_value_Icc' {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] [densely_ordered α] {δ : Type u_1} [linear_order δ] [topological_space δ] [order_closed_topology δ] {a : α} {b : α} (hab : a ≤ b) {f : α → δ} (hf : continuous_on f (set.Icc a b)) : set.Icc (f b) (f a) ⊆ f '' set.Icc a b := is_preconnected.intermediate_value is_preconnected_Icc (iff.mpr set.right_mem_Icc hab) (iff.mpr set.left_mem_Icc hab) hf /-- A continuous function which tendsto `at_top` `at_top` and to `at_bot` `at_bot` is surjective. -/ theorem continuous.surjective {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] [densely_ordered α] {δ : Type u_1} [linear_order δ] [topological_space δ] [order_closed_topology δ] {f : α → δ} (hf : continuous f) (h_top : filter.tendsto f filter.at_top filter.at_top) (h_bot : filter.tendsto f filter.at_bot filter.at_bot) : function.surjective f := sorry /-- A continuous function which tendsto `at_bot` `at_top` and to `at_top` `at_bot` is surjective. -/ theorem continuous.surjective' {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] [densely_ordered α] {δ : Type u_1} [linear_order δ] [topological_space δ] [order_closed_topology δ] {f : α → δ} (hf : continuous f) (h_top : filter.tendsto f filter.at_bot filter.at_top) (h_bot : filter.tendsto f filter.at_top filter.at_bot) : function.surjective f := continuous.surjective hf h_top h_bot /-- If a function `f : α → β` is continuous on a nonempty interval `s`, its restriction to `s` tends to `at_bot : filter β` along `at_bot : filter ↥s` and tends to `at_top : filter β` along `at_top : filter ↥s`, then the restriction of `f` to `s` is surjective. We formulate the conclusion as `surj_on f s univ`. -/ theorem continuous_on.surj_on_of_tendsto {α : Type u} {β : Type v} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] [conditionally_complete_linear_order β] [topological_space β] [order_topology β] [densely_ordered α] {f : α → β} {s : set α} [set.ord_connected s] (hs : set.nonempty s) (hf : continuous_on f s) (hbot : filter.tendsto (fun (x : ↥s) => f ↑x) filter.at_bot filter.at_bot) (htop : filter.tendsto (fun (x : ↥s) => f ↑x) filter.at_top filter.at_top) : set.surj_on f s set.univ := iff.mpr set.surj_on_iff_surjective (continuous.surjective (iff.mp continuous_on_iff_continuous_restrict hf) htop hbot) /-- If a function `f : α → β` is continuous on a nonempty interval `s`, its restriction to `s` tends to `at_top : filter β` along `at_bot : filter ↥s` and tends to `at_bot : filter β` along `at_top : filter ↥s`, then the restriction of `f` to `s` is surjective. We formulate the conclusion as `surj_on f s univ`. -/ theorem continuous_on.surj_on_of_tendsto' {α : Type u} {β : Type v} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] [conditionally_complete_linear_order β] [topological_space β] [order_topology β] [densely_ordered α] {f : α → β} {s : set α} [set.ord_connected s] (hs : set.nonempty s) (hf : continuous_on f s) (hbot : filter.tendsto (fun (x : ↥s) => f ↑x) filter.at_bot filter.at_top) (htop : filter.tendsto (fun (x : ↥s) => f ↑x) filter.at_top filter.at_bot) : set.surj_on f s set.univ := continuous_on.surj_on_of_tendsto hs hf hbot htop theorem is_compact.Inf_mem {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] {s : set α} (hs : is_compact s) (ne_s : set.nonempty s) : Inf s ∈ s := is_closed.cInf_mem (is_compact.is_closed hs) ne_s (is_compact.bdd_below hs) theorem is_compact.Sup_mem {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] {s : set α} (hs : is_compact s) (ne_s : set.nonempty s) : Sup s ∈ s := is_compact.Inf_mem hs ne_s theorem is_compact.is_glb_Inf {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] {s : set α} (hs : is_compact s) (ne_s : set.nonempty s) : is_glb s (Inf s) := is_glb_cInf ne_s (is_compact.bdd_below hs) theorem is_compact.is_lub_Sup {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] {s : set α} (hs : is_compact s) (ne_s : set.nonempty s) : is_lub s (Sup s) := is_compact.is_glb_Inf hs ne_s theorem is_compact.is_least_Inf {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] {s : set α} (hs : is_compact s) (ne_s : set.nonempty s) : is_least s (Inf s) := { left := is_compact.Inf_mem hs ne_s, right := and.left (is_compact.is_glb_Inf hs ne_s) } theorem is_compact.is_greatest_Sup {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] {s : set α} (hs : is_compact s) (ne_s : set.nonempty s) : is_greatest s (Sup s) := is_compact.is_least_Inf hs ne_s theorem is_compact.exists_is_least {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] {s : set α} (hs : is_compact s) (ne_s : set.nonempty s) : ∃ (x : α), is_least s x := Exists.intro (Inf s) (is_compact.is_least_Inf hs ne_s) theorem is_compact.exists_is_greatest {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] {s : set α} (hs : is_compact s) (ne_s : set.nonempty s) : ∃ (x : α), is_greatest s x := Exists.intro (Sup s) (is_compact.is_greatest_Sup hs ne_s) theorem is_compact.exists_is_glb {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] {s : set α} (hs : is_compact s) (ne_s : set.nonempty s) : ∃ (x : α), ∃ (H : x ∈ s), is_glb s x := Exists.intro (Inf s) (Exists.intro (is_compact.Inf_mem hs ne_s) (is_compact.is_glb_Inf hs ne_s)) theorem is_compact.exists_is_lub {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] {s : set α} (hs : is_compact s) (ne_s : set.nonempty s) : ∃ (x : α), ∃ (H : x ∈ s), is_lub s x := Exists.intro (Sup s) (Exists.intro (is_compact.Sup_mem hs ne_s) (is_compact.is_lub_Sup hs ne_s)) theorem is_compact.exists_Inf_image_eq {β : Type v} [conditionally_complete_linear_order β] [topological_space β] [order_topology β] {α : Type u} [topological_space α] {s : set α} (hs : is_compact s) (ne_s : set.nonempty s) {f : α → β} (hf : continuous_on f s) : ∃ (x : α), ∃ (H : x ∈ s), Inf (f '' s) = f x := sorry theorem is_compact.exists_Sup_image_eq {β : Type v} [conditionally_complete_linear_order β] [topological_space β] [order_topology β] {α : Type u} [topological_space α] {s : set α} : is_compact s → set.nonempty s → ∀ {f : α → β}, continuous_on f s → ∃ (x : α), ∃ (H : x ∈ s), Sup (f '' s) = f x := is_compact.exists_Inf_image_eq theorem eq_Icc_of_connected_compact {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] {s : set α} (h₁ : is_connected s) (h₂ : is_compact s) : s = set.Icc (Inf s) (Sup s) := eq_Icc_cInf_cSup_of_connected_bdd_closed h₁ (is_compact.bdd_below h₂) (is_compact.bdd_above h₂) (is_compact.is_closed h₂) /-- The extreme value theorem: a continuous function realizes its minimum on a compact set -/ theorem is_compact.exists_forall_le {β : Type v} [conditionally_complete_linear_order β] [topological_space β] [order_topology β] {α : Type u} [topological_space α] {s : set α} (hs : is_compact s) (ne_s : set.nonempty s) {f : α → β} (hf : continuous_on f s) : ∃ (x : α), ∃ (H : x ∈ s), ∀ (y : α), y ∈ s → f x ≤ f y := sorry /-- The extreme value theorem: a continuous function realizes its maximum on a compact set -/ theorem is_compact.exists_forall_ge {β : Type v} [conditionally_complete_linear_order β] [topological_space β] [order_topology β] {α : Type u} [topological_space α] {s : set α} : is_compact s → set.nonempty s → ∀ {f : α → β}, continuous_on f s → ∃ (x : α), ∃ (H : x ∈ s), ∀ (y : α), y ∈ s → f y ≤ f x := is_compact.exists_forall_le /-- The extreme value theorem: if a continuous function `f` tends to infinity away from compact sets, then it has a global minimum. -/ theorem continuous.exists_forall_le {β : Type v} [conditionally_complete_linear_order β] [topological_space β] [order_topology β] {α : Type u_1} [topological_space α] [Nonempty α] {f : α → β} (hf : continuous f) (hlim : filter.tendsto f (filter.cocompact α) filter.at_top) : ∃ (x : α), ∀ (y : α), f x ≤ f y := sorry /-- The extreme value theorem: if a continuous function `f` tends to negative infinity away from compactx sets, then it has a global maximum. -/ theorem continuous.exists_forall_ge {β : Type v} [conditionally_complete_linear_order β] [topological_space β] [order_topology β] {α : Type u_1} [topological_space α] [Nonempty α] {f : α → β} (hf : continuous f) (hlim : filter.tendsto f (filter.cocompact α) filter.at_bot) : ∃ (x : α), ∀ (y : α), f y ≤ f x := continuous.exists_forall_le hf hlim theorem is_bounded_le_nhds {α : Type u} [semilattice_sup α] [topological_space α] [order_topology α] (a : α) : filter.is_bounded LessEq (nhds a) := sorry theorem filter.tendsto.is_bounded_under_le {α : Type u} {β : Type v} [semilattice_sup α] [topological_space α] [order_topology α] {f : filter β} {u : β → α} {a : α} (h : filter.tendsto u f (nhds a)) : filter.is_bounded_under LessEq f u := filter.is_bounded.mono h (is_bounded_le_nhds a) theorem is_cobounded_ge_nhds {α : Type u} [semilattice_sup α] [topological_space α] [order_topology α] (a : α) : filter.is_cobounded ge (nhds a) := filter.is_bounded.is_cobounded_flip (is_bounded_le_nhds a) theorem filter.tendsto.is_cobounded_under_ge {α : Type u} {β : Type v} [semilattice_sup α] [topological_space α] [order_topology α] {f : filter β} {u : β → α} {a : α} [filter.ne_bot f] (h : filter.tendsto u f (nhds a)) : filter.is_cobounded_under ge f u := filter.is_bounded.is_cobounded_flip (filter.tendsto.is_bounded_under_le h) theorem is_bounded_ge_nhds {α : Type u} [semilattice_inf α] [topological_space α] [order_topology α] (a : α) : filter.is_bounded ge (nhds a) := is_bounded_le_nhds a theorem filter.tendsto.is_bounded_under_ge {α : Type u} {β : Type v} [semilattice_inf α] [topological_space α] [order_topology α] {f : filter β} {u : β → α} {a : α} (h : filter.tendsto u f (nhds a)) : filter.is_bounded_under ge f u := filter.is_bounded.mono h (is_bounded_ge_nhds a) theorem is_cobounded_le_nhds {α : Type u} [semilattice_inf α] [topological_space α] [order_topology α] (a : α) : filter.is_cobounded LessEq (nhds a) := filter.is_bounded.is_cobounded_flip (is_bounded_ge_nhds a) theorem filter.tendsto.is_cobounded_under_le {α : Type u} {β : Type v} [semilattice_inf α] [topological_space α] [order_topology α] {f : filter β} {u : β → α} {a : α} [filter.ne_bot f] (h : filter.tendsto u f (nhds a)) : filter.is_cobounded_under LessEq f u := filter.is_bounded.is_cobounded_flip (filter.tendsto.is_bounded_under_ge h) theorem lt_mem_sets_of_Limsup_lt {α : Type u} [conditionally_complete_linear_order α] {f : filter α} {b : α} (h : filter.is_bounded LessEq f) (l : filter.Limsup f < b) : filter.eventually (fun (a : α) => a < b) f := sorry theorem gt_mem_sets_of_Liminf_gt {α : Type u} [conditionally_complete_linear_order α] {f : filter α} {b : α} : filter.is_bounded ge f → b < filter.Liminf f → filter.eventually (fun (a : α) => b < a) f := lt_mem_sets_of_Limsup_lt /-- If the liminf and the limsup of a filter coincide, then this filter converges to their common value, at least if the filter is eventually bounded above and below. -/ theorem le_nhds_of_Limsup_eq_Liminf {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] {f : filter α} {a : α} (hl : filter.is_bounded LessEq f) (hg : filter.is_bounded ge f) (hs : filter.Limsup f = a) (hi : filter.Liminf f = a) : f ≤ nhds a := iff.mpr tendsto_order { left := fun (b : α) (hb : b < a) => gt_mem_sets_of_Liminf_gt hg (Eq.symm hi ▸ hb), right := fun (b : α) (hb : b > a) => lt_mem_sets_of_Limsup_lt hl (Eq.symm hs ▸ hb) } theorem Limsup_nhds {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] (a : α) : filter.Limsup (nhds a) = a := sorry theorem Liminf_nhds {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] (a : α) : filter.Liminf (nhds a) = a := Limsup_nhds /-- If a filter is converging, its limsup coincides with its limit. -/ theorem Liminf_eq_of_le_nhds {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] {f : filter α} {a : α} [filter.ne_bot f] (h : f ≤ nhds a) : filter.Liminf f = a := sorry /-- If a filter is converging, its liminf coincides with its limit. -/ theorem Limsup_eq_of_le_nhds {α : Type u} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] {f : filter α} {a : α} [filter.ne_bot f] : f ≤ nhds a → filter.Limsup f = a := Liminf_eq_of_le_nhds /-- If a function has a limit, then its limsup coincides with its limit. -/ theorem filter.tendsto.limsup_eq {α : Type u} {β : Type v} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] {f : filter β} {u : β → α} {a : α} [filter.ne_bot f] (h : filter.tendsto u f (nhds a)) : filter.limsup f u = a := Limsup_eq_of_le_nhds h /-- If a function has a limit, then its liminf coincides with its limit. -/ theorem filter.tendsto.liminf_eq {α : Type u} {β : Type v} [conditionally_complete_linear_order α] [topological_space α] [order_topology α] {f : filter β} {u : β → α} {a : α} [filter.ne_bot f] (h : filter.tendsto u f (nhds a)) : filter.liminf f u = a := Liminf_eq_of_le_nhds h -- In complete_linear_order, the above theorems take a simpler form /-- If the liminf and the limsup of a function coincide, then the limit of the function exists and has the same value -/ theorem tendsto_of_liminf_eq_limsup {α : Type u} {β : Type v} [complete_linear_order α] [topological_space α] [order_topology α] {f : filter β} {u : β → α} {a : α} (hinf : filter.liminf f u = a) (hsup : filter.limsup f u = a) : filter.tendsto u f (nhds a) := le_nhds_of_Limsup_eq_Liminf filter.is_bounded_le_of_top filter.is_bounded_ge_of_bot hsup hinf /-- If a number `a` is less than or equal to the `liminf` of a function `f` at some filter and is greater than or equal to the `limsup` of `f`, then `f` tends to `a` along this filter. -/ theorem tendsto_of_le_liminf_of_limsup_le {α : Type u} {β : Type v} [complete_linear_order α] [topological_space α] [order_topology α] {f : filter β} {u : β → α} {a : α} (hinf : a ≤ filter.liminf f u) (hsup : filter.limsup f u ≤ a) : filter.tendsto u f (nhds a) := sorry /-! Here is a counter-example to a version of the following with `conditionally_complete_lattice α`. Take `α = [0, 1) → ℝ` with the natural lattice structure, `ι = ℕ`. Put `f n x = -x^n`. Then `⨆ n, f n = 0` while none of `f n` is strictly greater than the constant function `-0.5`. -/ theorem tendsto_at_top_csupr {ι : Type u_1} {α : Type u_2} [preorder ι] [topological_space α] [conditionally_complete_linear_order α] [order_topology α] {f : ι → α} (h_mono : monotone f) (hbdd : bdd_above (set.range f)) : filter.tendsto f filter.at_top (nhds (supr fun (i : ι) => f i)) := sorry theorem tendsto_at_top_cinfi {ι : Type u_1} {α : Type u_2} [preorder ι] [topological_space α] [conditionally_complete_linear_order α] [order_topology α] {f : ι → α} (h_mono : ∀ {i j : ι}, i ≤ j → f j ≤ f i) (hbdd : bdd_below (set.range f)) : filter.tendsto f filter.at_top (nhds (infi fun (i : ι) => f i)) := tendsto_at_top_csupr h_mono hbdd theorem tendsto_at_top_supr {ι : Type u_1} {α : Type u_2} [preorder ι] [topological_space α] [complete_linear_order α] [order_topology α] {f : ι → α} (h_mono : monotone f) : filter.tendsto f filter.at_top (nhds (supr fun (i : ι) => f i)) := tendsto_at_top_csupr h_mono (order_top.bdd_above (set.range f)) theorem tendsto_at_top_infi {ι : Type u_1} {α : Type u_2} [preorder ι] [topological_space α] [complete_linear_order α] [order_topology α] {f : ι → α} (h_mono : ∀ {i j : ι}, i ≤ j → f j ≤ f i) : filter.tendsto f filter.at_top (nhds (infi fun (i : ι) => f i)) := tendsto_at_top_cinfi h_mono (order_bot.bdd_below (set.range f)) theorem tendsto_of_monotone {ι : Type u_1} {α : Type u_2} [preorder ι] [topological_space α] [conditionally_complete_linear_order α] [order_topology α] {f : ι → α} (h_mono : monotone f) : filter.tendsto f filter.at_top filter.at_top ∨ ∃ (l : α), filter.tendsto f filter.at_top (nhds l) := dite (bdd_above (set.range f)) (fun (H : bdd_above (set.range f)) => Or.inr (Exists.intro (supr fun (i : ι) => f i) (tendsto_at_top_csupr h_mono H))) fun (H : ¬bdd_above (set.range f)) => Or.inl (filter.tendsto_at_top_at_top_of_monotone' h_mono H) theorem supr_eq_of_tendsto {α : Type u_1} {β : Type u_2} [topological_space α] [complete_linear_order α] [order_topology α] [Nonempty β] [semilattice_sup β] {f : β → α} {a : α} (hf : monotone f) : filter.tendsto f filter.at_top (nhds a) → supr f = a := tendsto_nhds_unique (tendsto_at_top_supr hf) theorem infi_eq_of_tendsto {β : Type v} {α : Type u_1} [topological_space α] [complete_linear_order α] [order_topology α] [Nonempty β] [semilattice_sup β] {f : β → α} {a : α} (hf : ∀ (n m : β), n ≤ m → f m ≤ f n) : filter.tendsto f filter.at_top (nhds a) → infi f = a := tendsto_nhds_unique (tendsto_at_top_infi hf) theorem tendsto_neg_nhds_within_Ioi {α : Type u} [ordered_add_comm_group α] [topological_space α] [topological_add_group α] {a : α} : filter.tendsto Neg.neg (nhds_within a (set.Ioi a)) (nhds_within (-a) (set.Iio (-a))) := sorry theorem tendsto_inv_nhds_within_Iio {α : Type u} [ordered_comm_group α] [topological_space α] [topological_group α] {a : α} : filter.tendsto has_inv.inv (nhds_within a (set.Iio a)) (nhds_within (a⁻¹) (set.Ioi (a⁻¹))) := sorry theorem tendsto_neg_nhds_within_Ioi_neg {α : Type u} [ordered_add_comm_group α] [topological_space α] [topological_add_group α] {a : α} : filter.tendsto Neg.neg (nhds_within (-a) (set.Ioi (-a))) (nhds_within a (set.Iio a)) := sorry theorem tendsto_neg_nhds_within_Iio_neg {α : Type u} [ordered_add_comm_group α] [topological_space α] [topological_add_group α] {a : α} : filter.tendsto Neg.neg (nhds_within (-a) (set.Iio (-a))) (nhds_within a (set.Ioi a)) := sorry theorem tendsto_neg_nhds_within_Ici {α : Type u} [ordered_add_comm_group α] [topological_space α] [topological_add_group α] {a : α} : filter.tendsto Neg.neg (nhds_within a (set.Ici a)) (nhds_within (-a) (set.Iic (-a))) := sorry theorem tendsto_inv_nhds_within_Iic {α : Type u} [ordered_comm_group α] [topological_space α] [topological_group α] {a : α} : filter.tendsto has_inv.inv (nhds_within a (set.Iic a)) (nhds_within (a⁻¹) (set.Ici (a⁻¹))) := sorry theorem tendsto_inv_nhds_within_Ici_inv {α : Type u} [ordered_comm_group α] [topological_space α] [topological_group α] {a : α} : filter.tendsto has_inv.inv (nhds_within (a⁻¹) (set.Ici (a⁻¹))) (nhds_within a (set.Iic a)) := sorry theorem tendsto_inv_nhds_within_Iic_inv {α : Type u} [ordered_comm_group α] [topological_space α] [topological_group α] {a : α} : filter.tendsto has_inv.inv (nhds_within (a⁻¹) (set.Iic (a⁻¹))) (nhds_within a (set.Ici a)) := sorry theorem nhds_left_sup_nhds_right {α : Type u} (a : α) [topological_space α] [linear_order α] : nhds_within a (set.Iic a) ⊔ nhds_within a (set.Ici a) = nhds a := sorry theorem nhds_left'_sup_nhds_right {α : Type u} (a : α) [topological_space α] [linear_order α] : nhds_within a (set.Iio a) ⊔ nhds_within a (set.Ici a) = nhds a := sorry theorem nhds_left_sup_nhds_right' {α : Type u} (a : α) [topological_space α] [linear_order α] : nhds_within a (set.Iic a) ⊔ nhds_within a (set.Ioi a) = nhds a := sorry theorem continuous_at_iff_continuous_left_right {α : Type u} {β : Type v} [topological_space α] [linear_order α] [topological_space β] {a : α} {f : α → β} : continuous_at f a ↔ continuous_within_at f (set.Iic a) a ∧ continuous_within_at f (set.Ici a) a := sorry theorem continuous_on_Icc_extend_from_Ioo {α : Type u} {β : Type v} [topological_space α] [linear_order α] [densely_ordered α] [order_topology α] [topological_space β] [regular_space β] {f : α → β} {a : α} {b : α} {la : β} {lb : β} (hab : a < b) (hf : continuous_on f (set.Ioo a b)) (ha : filter.tendsto f (nhds_within a (set.Ioi a)) (nhds la)) (hb : filter.tendsto f (nhds_within b (set.Iio b)) (nhds lb)) : continuous_on (extend_from (set.Ioo a b) f) (set.Icc a b) := sorry theorem eq_lim_at_left_extend_from_Ioo {α : Type u} {β : Type v} [topological_space α] [linear_order α] [densely_ordered α] [order_topology α] [topological_space β] [t2_space β] {f : α → β} {a : α} {b : α} {la : β} (hab : a < b) (ha : filter.tendsto f (nhds_within a (set.Ioi a)) (nhds la)) : extend_from (set.Ioo a b) f a = la := sorry theorem eq_lim_at_right_extend_from_Ioo {α : Type u} {β : Type v} [topological_space α] [linear_order α] [densely_ordered α] [order_topology α] [topological_space β] [t2_space β] {f : α → β} {a : α} {b : α} {lb : β} (hab : a < b) (hb : filter.tendsto f (nhds_within b (set.Iio b)) (nhds lb)) : extend_from (set.Ioo a b) f b = lb := sorry theorem continuous_on_Ico_extend_from_Ioo {α : Type u} {β : Type v} [topological_space α] [linear_order α] [densely_ordered α] [order_topology α] [topological_space β] [regular_space β] {f : α → β} {a : α} {b : α} {la : β} (hab : a < b) (hf : continuous_on f (set.Ioo a b)) (ha : filter.tendsto f (nhds_within a (set.Ioi a)) (nhds la)) : continuous_on (extend_from (set.Ioo a b) f) (set.Ico a b) := sorry theorem continuous_on_Ioc_extend_from_Ioo {α : Type u} {β : Type v} [topological_space α] [linear_order α] [densely_ordered α] [order_topology α] [topological_space β] [regular_space β] {f : α → β} {a : α} {b : α} {lb : β} (hab : a < b) (hf : continuous_on f (set.Ioo a b)) (hb : filter.tendsto f (nhds_within b (set.Iio b)) (nhds lb)) : continuous_on (extend_from (set.Ioo a b) f) (set.Ioc a b) := sorry theorem continuous_within_at_Ioi_iff_Ici {α : Type u_1} {β : Type u_2} [topological_space α] [partial_order α] [topological_space β] {a : α} {f : α → β} : continuous_within_at f (set.Ioi a) a ↔ continuous_within_at f (set.Ici a) a := sorry theorem continuous_within_at_Iio_iff_Iic {α : Type u_1} {β : Type u_2} [topological_space α] [linear_order α] [topological_space β] {a : α} {f : α → β} : continuous_within_at f (set.Iio a) a ↔ continuous_within_at f (set.Iic a) a := sorry theorem continuous_at_iff_continuous_left'_right' {α : Type u} {β : Type v} [topological_space α] [linear_order α] [topological_space β] {a : α} {f : α → β} : continuous_at f a ↔ continuous_within_at f (set.Iio a) a ∧ continuous_within_at f (set.Ioi a) a := sorry /-! ### Continuity of monotone functions In this section we prove the following fact: if `f` is a monotone function on a neighborhood of `a` and the image of this neighborhood is a neighborhood of `f a`, then `f` is continuous at `a`, see `continuous_at_of_mono_incr_on_of_image_mem_nhds`, as well as several similar facts. -/ /-- If `f` is a function strictly monotonically increasing on a right neighborhood of `a` and the image of this neighborhood under `f` meets every interval `(f a, b]`, `b > f a`, then `f` is continuous at `a` from the right. The assumption `hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioc (f a) b` is required because otherwise the function `f : ℝ → ℝ` given by `f x = if x ≤ 0 then x else x + 1` would be a counter-example at `a = 0`. -/ theorem strict_mono_incr_on.continuous_at_right_of_exists_between {α : Type u} {β : Type v} [linear_order α] [topological_space α] [order_topology α] [linear_order β] [topological_space β] [order_topology β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_incr_on f s) (hs : s ∈ nhds_within a (set.Ici a)) (hfs : ∀ (b : β) (H : b > f a), ∃ (c : α), ∃ (H : c ∈ s), f c ∈ set.Ioc (f a) b) : continuous_within_at f (set.Ici a) a := sorry /-- If `f` is a function monotonically increasing function on a right neighborhood of `a` and the image of this neighborhood under `f` meets every interval `(f a, b)`, `b > f a`, then `f` is continuous at `a` from the right. The assumption `hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioo (f a) b` cannot be replaced by the weaker assumption `hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioc (f a) b` we use for strictly monotone functions because otherwise the function `ceil : ℝ → ℤ` would be a counter-example at `a = 0`. -/ theorem continuous_at_right_of_mono_incr_on_of_exists_between {α : Type u} {β : Type v} [linear_order α] [topological_space α] [order_topology α] [linear_order β] [topological_space β] [order_topology β] {f : α → β} {s : set α} {a : α} (h_mono : ∀ (x : α), x ∈ s → ∀ (y : α), y ∈ s → x ≤ y → f x ≤ f y) (hs : s ∈ nhds_within a (set.Ici a)) (hfs : ∀ (b : β) (H : b > f a), ∃ (c : α), ∃ (H : c ∈ s), f c ∈ set.Ioo (f a) b) : continuous_within_at f (set.Ici a) a := sorry /-- If a function `f` with a densely ordered codomain is monotonically increasing on a right neighborhood of `a` and the closure of the image of this neighborhood under `f` is a right neighborhood of `f a`, then `f` is continuous at `a` from the right. -/ theorem continuous_at_right_of_mono_incr_on_of_closure_image_mem_nhds_within {α : Type u} {β : Type v} [linear_order α] [topological_space α] [order_topology α] [linear_order β] [topological_space β] [order_topology β] [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : ∀ (x : α), x ∈ s → ∀ (y : α), y ∈ s → x ≤ y → f x ≤ f y) (hs : s ∈ nhds_within a (set.Ici a)) (hfs : closure (f '' s) ∈ nhds_within (f a) (set.Ici (f a))) : continuous_within_at f (set.Ici a) a := sorry /-- If a function `f` with a densely ordered codomain is monotonically increasing on a right neighborhood of `a` and the image of this neighborhood under `f` is a right neighborhood of `f a`, then `f` is continuous at `a` from the right. -/ theorem continuous_at_right_of_mono_incr_on_of_image_mem_nhds_within {α : Type u} {β : Type v} [linear_order α] [topological_space α] [order_topology α] [linear_order β] [topological_space β] [order_topology β] [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : ∀ (x : α), x ∈ s → ∀ (y : α), y ∈ s → x ≤ y → f x ≤ f y) (hs : s ∈ nhds_within a (set.Ici a)) (hfs : f '' s ∈ nhds_within (f a) (set.Ici (f a))) : continuous_within_at f (set.Ici a) a := continuous_at_right_of_mono_incr_on_of_closure_image_mem_nhds_within h_mono hs (filter.mem_sets_of_superset hfs subset_closure) /-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a right neighborhood of `a` and the closure of the image of this neighborhood under `f` is a right neighborhood of `f a`, then `f` is continuous at `a` from the right. -/ theorem strict_mono_incr_on.continuous_at_right_of_closure_image_mem_nhds_within {α : Type u} {β : Type v} [linear_order α] [topological_space α] [order_topology α] [linear_order β] [topological_space β] [order_topology β] [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_incr_on f s) (hs : s ∈ nhds_within a (set.Ici a)) (hfs : closure (f '' s) ∈ nhds_within (f a) (set.Ici (f a))) : continuous_within_at f (set.Ici a) a := continuous_at_right_of_mono_incr_on_of_closure_image_mem_nhds_within (fun (x : α) (hx : x ∈ s) (y : α) (hy : y ∈ s) => iff.mpr (strict_mono_incr_on.le_iff_le h_mono hx hy)) hs hfs /-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a right neighborhood of `a` and the image of this neighborhood under `f` is a right neighborhood of `f a`, then `f` is continuous at `a` from the right. -/ theorem strict_mono_incr_on.continuous_at_right_of_image_mem_nhds_within {α : Type u} {β : Type v} [linear_order α] [topological_space α] [order_topology α] [linear_order β] [topological_space β] [order_topology β] [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_incr_on f s) (hs : s ∈ nhds_within a (set.Ici a)) (hfs : f '' s ∈ nhds_within (f a) (set.Ici (f a))) : continuous_within_at f (set.Ici a) a := strict_mono_incr_on.continuous_at_right_of_closure_image_mem_nhds_within h_mono hs (filter.mem_sets_of_superset hfs subset_closure) /-- If a function `f` is strictly monotonically increasing on a right neighborhood of `a` and the image of this neighborhood under `f` includes `Ioi (f a)`, then `f` is continuous at `a` from the right. -/ theorem strict_mono_incr_on.continuous_at_right_of_surj_on {α : Type u} {β : Type v} [linear_order α] [topological_space α] [order_topology α] [linear_order β] [topological_space β] [order_topology β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_incr_on f s) (hs : s ∈ nhds_within a (set.Ici a)) (hfs : set.surj_on f s (set.Ioi (f a))) : continuous_within_at f (set.Ici a) a := sorry /-- If `f` is a function strictly monotonically increasing on a left neighborhood of `a` and the image of this neighborhood under `f` meets every interval `[b, f a)`, `b < f a`, then `f` is continuous at `a` from the left. The assumption `hfs : ∀ b < f a, ∃ c ∈ s, f c ∈ Ico b (f a)` is required because otherwise the function `f : ℝ → ℝ` given by `f x = if x < 0 then x else x + 1` would be a counter-example at `a = 0`. -/ theorem strict_mono_incr_on.continuous_at_left_of_exists_between {α : Type u} {β : Type v} [linear_order α] [topological_space α] [order_topology α] [linear_order β] [topological_space β] [order_topology β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_incr_on f s) (hs : s ∈ nhds_within a (set.Iic a)) (hfs : ∀ (b : β) (H : b < f a), ∃ (c : α), ∃ (H : c ∈ s), f c ∈ set.Ico b (f a)) : continuous_within_at f (set.Iic a) a := sorry /-- If `f` is a function monotonically increasing function on a left neighborhood of `a` and the image of this neighborhood under `f` meets every interval `(b, f a)`, `b < f a`, then `f` is continuous at `a` from the left. The assumption `hfs : ∀ b < f a, ∃ c ∈ s, f c ∈ Ioo b (f a)` cannot be replaced by the weaker assumption `hfs : ∀ b < f a, ∃ c ∈ s, f c ∈ Ico b (f a)` we use for strictly monotone functions because otherwise the function `floor : ℝ → ℤ` would be a counter-example at `a = 0`. -/ theorem continuous_at_left_of_mono_incr_on_of_exists_between {α : Type u} {β : Type v} [linear_order α] [topological_space α] [order_topology α] [linear_order β] [topological_space β] [order_topology β] {f : α → β} {s : set α} {a : α} (h_mono : ∀ (x : α), x ∈ s → ∀ (y : α), y ∈ s → x ≤ y → f x ≤ f y) (hs : s ∈ nhds_within a (set.Iic a)) (hfs : ∀ (b : β) (H : b < f a), ∃ (c : α), ∃ (H : c ∈ s), f c ∈ set.Ioo b (f a)) : continuous_within_at f (set.Iic a) a := sorry /-- If a function `f` with a densely ordered codomain is monotonically increasing on a left neighborhood of `a` and the closure of the image of this neighborhood under `f` is a left neighborhood of `f a`, then `f` is continuous at `a` from the left -/ theorem continuous_at_left_of_mono_incr_on_of_closure_image_mem_nhds_within {α : Type u} {β : Type v} [linear_order α] [topological_space α] [order_topology α] [linear_order β] [topological_space β] [order_topology β] [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : ∀ (x : α), x ∈ s → ∀ (y : α), y ∈ s → x ≤ y → f x ≤ f y) (hs : s ∈ nhds_within a (set.Iic a)) (hfs : closure (f '' s) ∈ nhds_within (f a) (set.Iic (f a))) : continuous_within_at f (set.Iic a) a := continuous_at_right_of_mono_incr_on_of_closure_image_mem_nhds_within (fun (x : order_dual α) (hx : x ∈ s) (y : order_dual α) (hy : y ∈ s) => h_mono y hy x hx) hs hfs /-- If a function `f` with a densely ordered codomain is monotonically increasing on a left neighborhood of `a` and the image of this neighborhood under `f` is a left neighborhood of `f a`, then `f` is continuous at `a` from the left. -/ theorem continuous_at_left_of_mono_incr_on_of_image_mem_nhds_within {α : Type u} {β : Type v} [linear_order α] [topological_space α] [order_topology α] [linear_order β] [topological_space β] [order_topology β] [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : ∀ (x : α), x ∈ s → ∀ (y : α), y ∈ s → x ≤ y → f x ≤ f y) (hs : s ∈ nhds_within a (set.Iic a)) (hfs : f '' s ∈ nhds_within (f a) (set.Iic (f a))) : continuous_within_at f (set.Iic a) a := continuous_at_left_of_mono_incr_on_of_closure_image_mem_nhds_within h_mono hs (filter.mem_sets_of_superset hfs subset_closure) /-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a left neighborhood of `a` and the closure of the image of this neighborhood under `f` is a left neighborhood of `f a`, then `f` is continuous at `a` from the left. -/ theorem strict_mono_incr_on.continuous_at_left_of_closure_image_mem_nhds_within {α : Type u} {β : Type v} [linear_order α] [topological_space α] [order_topology α] [linear_order β] [topological_space β] [order_topology β] [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_incr_on f s) (hs : s ∈ nhds_within a (set.Iic a)) (hfs : closure (f '' s) ∈ nhds_within (f a) (set.Iic (f a))) : continuous_within_at f (set.Iic a) a := strict_mono_incr_on.continuous_at_right_of_closure_image_mem_nhds_within (strict_mono_incr_on.dual h_mono) hs hfs /-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a left neighborhood of `a` and the image of this neighborhood under `f` is a left neighborhood of `f a`, then `f` is continuous at `a` from the left. -/ theorem strict_mono_incr_on.continuous_at_left_of_image_mem_nhds_within {α : Type u} {β : Type v} [linear_order α] [topological_space α] [order_topology α] [linear_order β] [topological_space β] [order_topology β] [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_incr_on f s) (hs : s ∈ nhds_within a (set.Iic a)) (hfs : f '' s ∈ nhds_within (f a) (set.Iic (f a))) : continuous_within_at f (set.Iic a) a := strict_mono_incr_on.continuous_at_right_of_image_mem_nhds_within (strict_mono_incr_on.dual h_mono) hs hfs /-- If a function `f` is strictly monotonically increasing on a left neighborhood of `a` and the image of this neighborhood under `f` includes `Iio (f a)`, then `f` is continuous at `a` from the left. -/ theorem strict_mono_incr_on.continuous_at_left_of_surj_on {α : Type u} {β : Type v} [linear_order α] [topological_space α] [order_topology α] [linear_order β] [topological_space β] [order_topology β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_incr_on f s) (hs : s ∈ nhds_within a (set.Iic a)) (hfs : set.surj_on f s (set.Iio (f a))) : continuous_within_at f (set.Iic a) a := strict_mono_incr_on.continuous_at_right_of_surj_on (strict_mono_incr_on.dual h_mono) hs hfs /-- If a function `f` is strictly monotonically increasing on a neighborhood of `a` and the image of this neighborhood under `f` meets every interval `[b, f a)`, `b < f a`, and every interval `(f a, b]`, `b > f a`, then `f` is continuous at `a`. -/ theorem strict_mono_incr_on.continuous_at_of_exists_between {α : Type u} {β : Type v} [linear_order α] [topological_space α] [order_topology α] [linear_order β] [topological_space β] [order_topology β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_incr_on f s) (hs : s ∈ nhds a) (hfs_l : ∀ (b : β) (H : b < f a), ∃ (c : α), ∃ (H : c ∈ s), f c ∈ set.Ico b (f a)) (hfs_r : ∀ (b : β) (H : b > f a), ∃ (c : α), ∃ (H : c ∈ s), f c ∈ set.Ioc (f a) b) : continuous_at f a := iff.mpr continuous_at_iff_continuous_left_right { left := strict_mono_incr_on.continuous_at_left_of_exists_between h_mono (mem_nhds_within_of_mem_nhds hs) hfs_l, right := strict_mono_incr_on.continuous_at_right_of_exists_between h_mono (mem_nhds_within_of_mem_nhds hs) hfs_r } /-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a neighborhood of `a` and the closure of the image of this neighborhood under `f` is a neighborhood of `f a`, then `f` is continuous at `a`. -/ theorem strict_mono_incr_on.continuous_at_of_closure_image_mem_nhds {α : Type u} {β : Type v} [linear_order α] [topological_space α] [order_topology α] [linear_order β] [topological_space β] [order_topology β] [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_incr_on f s) (hs : s ∈ nhds a) (hfs : closure (f '' s) ∈ nhds (f a)) : continuous_at f a := sorry /-- If a function `f` with a densely ordered codomain is strictly monotonically increasing on a neighborhood of `a` and the image of this set under `f` is a neighborhood of `f a`, then `f` is continuous at `a`. -/ theorem strict_mono_incr_on.continuous_at_of_image_mem_nhds {α : Type u} {β : Type v} [linear_order α] [topological_space α] [order_topology α] [linear_order β] [topological_space β] [order_topology β] [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_incr_on f s) (hs : s ∈ nhds a) (hfs : f '' s ∈ nhds (f a)) : continuous_at f a := strict_mono_incr_on.continuous_at_of_closure_image_mem_nhds h_mono hs (filter.mem_sets_of_superset hfs subset_closure) /-- If `f` is a function monotonically increasing function on a neighborhood of `a` and the image of this neighborhood under `f` meets every interval `(b, f a)`, `b < f a`, and every interval `(f a, b)`, `b > f a`, then `f` is continuous at `a`. -/ theorem continuous_at_of_mono_incr_on_of_exists_between {α : Type u} {β : Type v} [linear_order α] [topological_space α] [order_topology α] [linear_order β] [topological_space β] [order_topology β] {f : α → β} {s : set α} {a : α} (h_mono : ∀ (x : α), x ∈ s → ∀ (y : α), y ∈ s → x ≤ y → f x ≤ f y) (hs : s ∈ nhds a) (hfs_l : ∀ (b : β) (H : b < f a), ∃ (c : α), ∃ (H : c ∈ s), f c ∈ set.Ioo b (f a)) (hfs_r : ∀ (b : β) (H : b > f a), ∃ (c : α), ∃ (H : c ∈ s), f c ∈ set.Ioo (f a) b) : continuous_at f a := iff.mpr continuous_at_iff_continuous_left_right { left := continuous_at_left_of_mono_incr_on_of_exists_between h_mono (mem_nhds_within_of_mem_nhds hs) hfs_l, right := continuous_at_right_of_mono_incr_on_of_exists_between h_mono (mem_nhds_within_of_mem_nhds hs) hfs_r } /-- If a function `f` with a densely ordered codomain is monotonically increasing on a neighborhood of `a` and the closure of the image of this neighborhood under `f` is a neighborhood of `f a`, then `f` is continuous at `a`. -/ theorem continuous_at_of_mono_incr_on_of_closure_image_mem_nhds {α : Type u} {β : Type v} [linear_order α] [topological_space α] [order_topology α] [linear_order β] [topological_space β] [order_topology β] [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : ∀ (x : α), x ∈ s → ∀ (y : α), y ∈ s → x ≤ y → f x ≤ f y) (hs : s ∈ nhds a) (hfs : closure (f '' s) ∈ nhds (f a)) : continuous_at f a := sorry /-- If a function `f` with a densely ordered codomain is monotonically increasing on a neighborhood of `a` and the image of this neighborhood under `f` is a neighborhood of `f a`, then `f` is continuous at `a`. -/ theorem continuous_at_of_mono_incr_on_of_image_mem_nhds {α : Type u} {β : Type v} [linear_order α] [topological_space α] [order_topology α] [linear_order β] [topological_space β] [order_topology β] [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : ∀ (x : α), x ∈ s → ∀ (y : α), y ∈ s → x ≤ y → f x ≤ f y) (hs : s ∈ nhds a) (hfs : f '' s ∈ nhds (f a)) : continuous_at f a := continuous_at_of_mono_incr_on_of_closure_image_mem_nhds h_mono hs (filter.mem_sets_of_superset hfs subset_closure) /-- A monotone function with densely ordered codomain and a dense range is continuous. -/ theorem monotone.continuous_of_dense_range {α : Type u} {β : Type v} [linear_order α] [topological_space α] [order_topology α] [linear_order β] [topological_space β] [order_topology β] [densely_ordered β] {f : α → β} (h_mono : monotone f) (h_dense : dense_range f) : continuous f := sorry /-- A monotone surjective function with a densely ordered codomain is surjective. -/ theorem monotone.continuous_of_surjective {α : Type u} {β : Type v} [linear_order α] [topological_space α] [order_topology α] [linear_order β] [topological_space β] [order_topology β] [densely_ordered β] {f : α → β} (h_mono : monotone f) (h_surj : function.surjective f) : continuous f := monotone.continuous_of_dense_range h_mono (function.surjective.dense_range h_surj) /-! ### Continuity of order isomorphisms In this section we prove that an `order_iso` is continuous, hence it is a `homeomorph`. We prove this for an `order_iso` between to partial orders with order topology. -/ namespace order_iso protected theorem continuous {α : Type u} {β : Type v} [partial_order α] [partial_order β] [topological_space α] [topological_space β] [order_topology α] [order_topology β] (e : α ≃o β) : continuous ⇑e := sorry /-- An order isomorphism between two linear order `order_topology` spaces is a homeomorphism. -/ def to_homeomorph {α : Type u} {β : Type v} [partial_order α] [partial_order β] [topological_space α] [topological_space β] [order_topology α] [order_topology β] (e : α ≃o β) : α ≃ₜ β := homeomorph.mk (rel_iso.to_equiv e) @[simp] theorem coe_to_homeomorph {α : Type u} {β : Type v} [partial_order α] [partial_order β] [topological_space α] [topological_space β] [order_topology α] [order_topology β] (e : α ≃o β) : ⇑(to_homeomorph e) = ⇑e := rfl @[simp] theorem coe_to_homeomorph_symm {α : Type u} {β : Type v} [partial_order α] [partial_order β] [topological_space α] [topological_space β] [order_topology α] [order_topology β] (e : α ≃o β) : ⇑(homeomorph.symm (to_homeomorph e)) = ⇑(symm e) := rfl
af25bf4e15497d39710e37981fadc544061c0833
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/dynamics/ergodic/conservative.lean
19c15969371659f0b90f684fd4953de2cbd3e4b3
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
10,146
lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import measure_theory.constructions.borel_space.basic import dynamics.ergodic.measure_preserving import combinatorics.pigeonhole /-! # Conservative systems > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we define `f : α → α` to be a *conservative* system w.r.t a measure `μ` if `f` is non-singular (`measure_theory.quasi_measure_preserving`) and for every measurable set `s` of positive measure at least one point `x ∈ s` returns back to `s` after some number of iterations of `f`. There are several properties that look like they are stronger than this one but actually follow from it: * `measure_theory.conservative.frequently_measure_inter_ne_zero`, `measure_theory.conservative.exists_gt_measure_inter_ne_zero`: if `μ s ≠ 0`, then for infinitely many `n`, the measure of `s ∩ (f^[n]) ⁻¹' s` is positive. * `measure_theory.conservative.measure_mem_forall_ge_image_not_mem_eq_zero`, `measure_theory.conservative.ae_mem_imp_frequently_image_mem`: a.e. every point of `s` visits `s` infinitely many times (Poincaré recurrence theorem). We also prove the topological Poincaré recurrence theorem `measure_theory.conservative.ae_frequently_mem_of_mem_nhds`. Let `f : α → α` be a conservative dynamical system on a topological space with second countable topology and measurable open sets. Then almost every point `x : α` is recurrent: it visits every neighborhood `s ∈ 𝓝 x` infinitely many times. ## Tags conservative dynamical system, Poincare recurrence theorem -/ noncomputable theory open classical set filter measure_theory finset function topological_space open_locale classical topology variables {ι : Type*} {α : Type*} [measurable_space α] {f : α → α} {s : set α} {μ : measure α} namespace measure_theory open measure /-- We say that a non-singular (`measure_theory.quasi_measure_preserving`) self-map is *conservative* if for any measurable set `s` of positive measure there exists `x ∈ s` such that `x` returns back to `s` under some iteration of `f`. -/ structure conservative (f : α → α) (μ : measure α . volume_tac) extends quasi_measure_preserving f μ μ : Prop := (exists_mem_image_mem : ∀ ⦃s⦄, measurable_set s → μ s ≠ 0 → ∃ (x ∈ s) (m ≠ 0), f^[m] x ∈ s) /-- A self-map preserving a finite measure is conservative. -/ protected lemma measure_preserving.conservative [is_finite_measure μ] (h : measure_preserving f μ μ) : conservative f μ := ⟨h.quasi_measure_preserving, λ s hsm h0, h.exists_mem_image_mem hsm h0⟩ namespace conservative /-- The identity map is conservative w.r.t. any measure. -/ protected lemma id (μ : measure α) : conservative id μ := { to_quasi_measure_preserving := quasi_measure_preserving.id μ, exists_mem_image_mem := λ s hs h0, let ⟨x, hx⟩ := nonempty_of_measure_ne_zero h0 in ⟨x, hx, 1, one_ne_zero, hx⟩ } /-- If `f` is a conservative map and `s` is a measurable set of nonzero measure, then for infinitely many values of `m` a positive measure of points `x ∈ s` returns back to `s` after `m` iterations of `f`. -/ lemma frequently_measure_inter_ne_zero (hf : conservative f μ) (hs : measurable_set s) (h0 : μ s ≠ 0) : ∃ᶠ m in at_top, μ (s ∩ (f^[m]) ⁻¹' s) ≠ 0 := begin by_contra H, simp only [not_frequently, eventually_at_top, ne.def, not_not] at H, rcases H with ⟨N, hN⟩, induction N with N ihN, { apply h0, simpa using hN 0 le_rfl }, rw [imp_false] at ihN, push_neg at ihN, rcases ihN with ⟨n, hn, hμn⟩, set T := s ∩ ⋃ n ≥ N + 1, (f^[n]) ⁻¹' s, have hT : measurable_set T, from hs.inter (measurable_set.bUnion (to_countable _) (λ _ _, hf.measurable.iterate _ hs)), have hμT : μ T = 0, { convert (measure_bUnion_null_iff $ to_countable _).2 hN, rw ←inter_Union₂, refl }, have : μ ((s ∩ (f^[n]) ⁻¹' s) \ T) ≠ 0, by rwa [measure_diff_null hμT], rcases hf.exists_mem_image_mem ((hs.inter (hf.measurable.iterate n hs)).diff hT) this with ⟨x, ⟨⟨hxs, hxn⟩, hxT⟩, m, hm0, ⟨hxms, hxm⟩, hxx⟩, refine hxT ⟨hxs, mem_Union₂.2 ⟨n + m, _, _⟩⟩, { exact add_le_add hn (nat.one_le_of_lt $ pos_iff_ne_zero.2 hm0) }, { rwa [set.mem_preimage, ← iterate_add_apply] at hxm } end /-- If `f` is a conservative map and `s` is a measurable set of nonzero measure, then for an arbitrarily large `m` a positive measure of points `x ∈ s` returns back to `s` after `m` iterations of `f`. -/ lemma exists_gt_measure_inter_ne_zero (hf : conservative f μ) (hs : measurable_set s) (h0 : μ s ≠ 0) (N : ℕ) : ∃ m > N, μ (s ∩ (f^[m]) ⁻¹' s) ≠ 0 := let ⟨m, hm, hmN⟩ := ((hf.frequently_measure_inter_ne_zero hs h0).and_eventually (eventually_gt_at_top N)).exists in ⟨m, hmN, hm⟩ /-- Poincaré recurrence theorem: given a conservative map `f` and a measurable set `s`, the set of points `x ∈ s` such that `x` does not return to `s` after `≥ n` iterations has measure zero. -/ lemma measure_mem_forall_ge_image_not_mem_eq_zero (hf : conservative f μ) (hs : measurable_set s) (n : ℕ) : μ {x ∈ s | ∀ m ≥ n, f^[m] x ∉ s} = 0 := begin by_contradiction H, have : measurable_set (s ∩ {x | ∀ m ≥ n, f^[m] x ∉ s}), { simp only [set_of_forall, ← compl_set_of], exact hs.inter (measurable_set.bInter (to_countable _) (λ m _, hf.measurable.iterate m hs.compl)) }, rcases (hf.exists_gt_measure_inter_ne_zero this H) n with ⟨m, hmn, hm⟩, rcases nonempty_of_measure_ne_zero hm with ⟨x, ⟨hxs, hxn⟩, hxm, -⟩, exact hxn m hmn.lt.le hxm end /-- Poincaré recurrence theorem: given a conservative map `f` and a measurable set `s`, almost every point `x ∈ s` returns back to `s` infinitely many times. -/ lemma ae_mem_imp_frequently_image_mem (hf : conservative f μ) (hs : measurable_set s) : ∀ᵐ x ∂μ, x ∈ s → ∃ᶠ n in at_top, (f^[n] x) ∈ s := begin simp only [frequently_at_top, @forall_swap (_ ∈ s), ae_all_iff], intro n, filter_upwards [measure_zero_iff_ae_nmem.1 (hf.measure_mem_forall_ge_image_not_mem_eq_zero hs n)], simp, end lemma inter_frequently_image_mem_ae_eq (hf : conservative f μ) (hs : measurable_set s) : (s ∩ {x | ∃ᶠ n in at_top, f^[n] x ∈ s} : set α) =ᵐ[μ] s := inter_eventually_eq_left.2 $ hf.ae_mem_imp_frequently_image_mem hs lemma measure_inter_frequently_image_mem_eq (hf : conservative f μ) (hs : measurable_set s) : μ (s ∩ {x | ∃ᶠ n in at_top, f^[n] x ∈ s}) = μ s := measure_congr (hf.inter_frequently_image_mem_ae_eq hs) /-- Poincaré recurrence theorem: if `f` is a conservative dynamical system and `s` is a measurable set, then for `μ`-a.e. `x`, if the orbit of `x` visits `s` at least once, then it visits `s` infinitely many times. -/ lemma ae_forall_image_mem_imp_frequently_image_mem (hf : conservative f μ) (hs : measurable_set s) : ∀ᵐ x ∂μ, ∀ k, f^[k] x ∈ s → ∃ᶠ n in at_top, (f^[n] x) ∈ s := begin refine ae_all_iff.2 (λ k, _), refine (hf.ae_mem_imp_frequently_image_mem (hf.measurable.iterate k hs)).mono (λ x hx hk, _), rw [← map_add_at_top_eq_nat k, frequently_map], refine (hx hk).mono (λ n hn, _), rwa [add_comm, iterate_add_apply] end /-- If `f` is a conservative self-map and `s` is a measurable set of positive measure, then `μ.ae`-frequently we have `x ∈ s` and `s` returns to `s` under infinitely many iterations of `f`. -/ lemma frequently_ae_mem_and_frequently_image_mem (hf : conservative f μ) (hs : measurable_set s) (h0 : μ s ≠ 0) : ∃ᵐ x ∂μ, x ∈ s ∧ ∃ᶠ n in at_top, (f^[n] x) ∈ s := ((frequently_ae_mem_iff.2 h0).and_eventually (hf.ae_mem_imp_frequently_image_mem hs)).mono $ λ x hx, ⟨hx.1, hx.2 hx.1⟩ /-- Poincaré recurrence theorem. Let `f : α → α` be a conservative dynamical system on a topological space with second countable topology and measurable open sets. Then almost every point `x : α` is recurrent: it visits every neighborhood `s ∈ 𝓝 x` infinitely many times. -/ lemma ae_frequently_mem_of_mem_nhds [topological_space α] [second_countable_topology α] [opens_measurable_space α] {f : α → α} {μ : measure α} (h : conservative f μ) : ∀ᵐ x ∂μ, ∀ s ∈ 𝓝 x, ∃ᶠ n in at_top, f^[n] x ∈ s := begin have : ∀ s ∈ countable_basis α, ∀ᵐ x ∂μ, x ∈ s → ∃ᶠ n in at_top, (f^[n] x) ∈ s, from λ s hs, h.ae_mem_imp_frequently_image_mem (is_open_of_mem_countable_basis hs).measurable_set, refine ((ae_ball_iff $ countable_countable_basis α).2 this).mono (λ x hx s hs, _), rcases (is_basis_countable_basis α).mem_nhds_iff.1 hs with ⟨o, hoS, hxo, hos⟩, exact (hx o hoS hxo).mono (λ n hn, hos hn) end /-- Iteration of a conservative system is a conservative system. -/ protected lemma iterate (hf : conservative f μ) (n : ℕ) : conservative (f^[n]) μ := begin cases n, { exact conservative.id μ }, -- Discharge the trivial case `n = 0` refine ⟨hf.1.iterate _, λ s hs hs0, _⟩, rcases (hf.frequently_ae_mem_and_frequently_image_mem hs hs0).exists with ⟨x, hxs, hx⟩, /- We take a point `x ∈ s` such that `f^[k] x ∈ s` for infinitely many values of `k`, then we choose two of these values `k < l` such that `k ≡ l [MOD (n + 1)]`. Then `f^[k] x ∈ s` and `(f^[n + 1])^[(l - k) / (n + 1)] (f^[k] x) = f^[l] x ∈ s`. -/ rw nat.frequently_at_top_iff_infinite at hx, rcases nat.exists_lt_modeq_of_infinite hx n.succ_pos with ⟨k, hk, l, hl, hkl, hn⟩, set m := (l - k) / (n + 1), have : (n + 1) * m = l - k, { apply nat.mul_div_cancel', exact (nat.modeq_iff_dvd' hkl.le).1 hn }, refine ⟨f^[k] x, hk, m, _, _⟩, { intro hm, rw [hm, mul_zero, eq_comm, tsub_eq_zero_iff_le] at this, exact this.not_lt hkl }, { rwa [← iterate_mul, this, ← iterate_add_apply, tsub_add_cancel_of_le], exact hkl.le } end end conservative end measure_theory
eef4b967d8f7a1f0c185bad43302ba09f3c72a5a
63abd62053d479eae5abf4951554e1064a4c45b4
/src/algebra/algebra/subalgebra.lean
0979d85dd478fbd6fc981722fa7e4942b6188008
[ "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
18,475
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Yury Kudryashov -/ import algebra.algebra.basic /-! # Subalgebras over Commutative Semiring In this file we define `subalgebra`s and the usual operations on them (`map`, `comap`). More lemmas about `adjoin` can be found in `ring_theory.adjoin`. -/ universes u v w open_locale tensor_product big_operators set_option old_structure_cmd true /-- A subalgebra is a sub(semi)ring that includes the range of `algebra_map`. -/ structure subalgebra (R : Type u) (A : Type v) [comm_semiring R] [semiring A] [algebra R A] extends subsemiring A : Type v := (algebra_map_mem' : ∀ r, algebra_map R A r ∈ carrier) /-- Reinterpret a `subalgebra` as a `subsemiring`. -/ add_decl_doc subalgebra.to_subsemiring namespace subalgebra variables {R : Type u} {A : Type v} {B : Type w} variables [comm_semiring R] [semiring A] [algebra R A] [semiring B] [algebra R B] include R instance : has_coe (subalgebra R A) (subsemiring A) := ⟨λ S, { ..S }⟩ instance : has_mem A (subalgebra R A) := ⟨λ x S, x ∈ (S : set A)⟩ variables {A} theorem mem_coe {x : A} {s : subalgebra R A} : x ∈ (s : set A) ↔ x ∈ s := iff.rfl @[ext] theorem ext {S T : subalgebra R A} (h : ∀ x : A, x ∈ S ↔ x ∈ T) : S = T := by cases S; cases T; congr; ext x; exact h x theorem ext_iff {S T : subalgebra R A} : S = T ↔ ∀ x : A, x ∈ S ↔ x ∈ T := ⟨λ h x, by rw h, ext⟩ variables (S : subalgebra R A) theorem algebra_map_mem (r : R) : algebra_map R A r ∈ S := S.algebra_map_mem' r theorem srange_le : (algebra_map R A).srange ≤ S := λ x ⟨r, _, hr⟩, hr ▸ S.algebra_map_mem r theorem range_subset : set.range (algebra_map R A) ⊆ S := λ x ⟨r, hr⟩, hr ▸ S.algebra_map_mem r theorem range_le : set.range (algebra_map R A) ≤ S := S.range_subset theorem one_mem : (1 : A) ∈ S := subsemiring.one_mem S theorem mul_mem {x y : A} (hx : x ∈ S) (hy : y ∈ S) : x * y ∈ S := subsemiring.mul_mem S hx hy theorem smul_mem {x : A} (hx : x ∈ S) (r : R) : r • x ∈ S := (algebra.smul_def r x).symm ▸ S.mul_mem (S.algebra_map_mem r) hx theorem pow_mem {x : A} (hx : x ∈ S) (n : ℕ) : x ^ n ∈ S := subsemiring.pow_mem S hx n theorem zero_mem : (0 : A) ∈ S := subsemiring.zero_mem S theorem add_mem {x y : A} (hx : x ∈ S) (hy : y ∈ S) : x + y ∈ S := subsemiring.add_mem S hx hy theorem neg_mem {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) {x : A} (hx : x ∈ S) : -x ∈ S := neg_one_smul R x ▸ S.smul_mem hx _ theorem sub_mem {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) {x y : A} (hx : x ∈ S) (hy : y ∈ S) : x - y ∈ S := S.add_mem hx $ S.neg_mem hy theorem nsmul_mem {x : A} (hx : x ∈ S) (n : ℕ) : n •ℕ x ∈ S := subsemiring.nsmul_mem S hx n theorem gsmul_mem {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) {x : A} (hx : x ∈ S) (n : ℤ) : n •ℤ x ∈ S := int.cases_on n (λ i, S.nsmul_mem hx i) (λ i, S.neg_mem $ S.nsmul_mem hx _) theorem coe_nat_mem (n : ℕ) : (n : A) ∈ S := subsemiring.coe_nat_mem S n theorem coe_int_mem {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) (n : ℤ) : (n : A) ∈ S := int.cases_on n (λ i, S.coe_nat_mem i) (λ i, S.neg_mem $ S.coe_nat_mem $ i + 1) theorem list_prod_mem {L : list A} (h : ∀ x ∈ L, x ∈ S) : L.prod ∈ S := subsemiring.list_prod_mem S h theorem list_sum_mem {L : list A} (h : ∀ x ∈ L, x ∈ S) : L.sum ∈ S := subsemiring.list_sum_mem S h theorem multiset_prod_mem {R : Type u} {A : Type v} [comm_semiring R] [comm_semiring A] [algebra R A] (S : subalgebra R A) {m : multiset A} (h : ∀ x ∈ m, x ∈ S) : m.prod ∈ S := subsemiring.multiset_prod_mem S m h theorem multiset_sum_mem {m : multiset A} (h : ∀ x ∈ m, x ∈ S) : m.sum ∈ S := subsemiring.multiset_sum_mem S m h theorem prod_mem {R : Type u} {A : Type v} [comm_semiring R] [comm_semiring A] [algebra R A] (S : subalgebra R A) {ι : Type w} {t : finset ι} {f : ι → A} (h : ∀ x ∈ t, f x ∈ S) : ∏ x in t, f x ∈ S := subsemiring.prod_mem S h theorem sum_mem {ι : Type w} {t : finset ι} {f : ι → A} (h : ∀ x ∈ t, f x ∈ S) : ∑ x in t, f x ∈ S := subsemiring.sum_mem S h instance {R : Type u} {A : Type v} [comm_semiring R] [semiring A] [algebra R A] (S : subalgebra R A) : is_add_submonoid (S : set A) := { zero_mem := S.zero_mem, add_mem := λ _ _, S.add_mem } instance {R : Type u} {A : Type v} [comm_semiring R] [semiring A] [algebra R A] (S : subalgebra R A) : is_submonoid (S : set A) := { one_mem := S.one_mem, mul_mem := λ _ _, S.mul_mem } /-- A subalgebra over a ring is also a `subring`. -/ def to_subring {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) : subring A := { neg_mem' := λ _, S.neg_mem, .. S.to_subsemiring } instance {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) : is_subring (S : set A) := { neg_mem := λ _, S.neg_mem } instance : inhabited S := ⟨0⟩ instance (R : Type u) (A : Type v) [comm_semiring R] [semiring A] [algebra R A] (S : subalgebra R A) : semiring S := subsemiring.to_semiring S instance (R : Type u) (A : Type v) [comm_semiring R] [comm_semiring A] [algebra R A] (S : subalgebra R A) : comm_semiring S := subsemiring.to_comm_semiring S instance (R : Type u) (A : Type v) [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) : ring S := @@subtype.ring _ S.is_subring instance (R : Type u) (A : Type v) [comm_ring R] [comm_ring A] [algebra R A] (S : subalgebra R A) : comm_ring S := @@subtype.comm_ring _ S.is_subring instance algebra : algebra R S := { smul := λ (c:R) x, ⟨c • x.1, S.smul_mem x.2 c⟩, commutes' := λ c x, subtype.eq $ algebra.commutes _ _, smul_def' := λ c x, subtype.eq $ algebra.smul_def _ _, .. (algebra_map R A).cod_srestrict S $ λ x, S.range_le ⟨x, rfl⟩ } instance to_algebra {R A B : Type*} [comm_semiring R] [comm_semiring A] [semiring B] [algebra R A] [algebra A B] (A₀ : subalgebra R A) : algebra A₀ B := algebra.of_subsemiring A₀ instance nontrivial [nontrivial A] : nontrivial S := subsemiring.nontrivial S -- todo: standardize on the names these morphisms -- compare with submodule.subtype /-- Embedding of a subalgebra into the algebra. -/ def val : S →ₐ[R] A := by refine_struct { to_fun := (coe : S → A) }; intros; refl @[simp] lemma coe_val : (S.val : S → A) = coe := rfl lemma val_apply (x : S) : S.val x = (x : A) := rfl /-- Convert a `subalgebra` to `submodule` -/ def to_submodule : submodule R A := { carrier := S, zero_mem' := (0:S).2, add_mem' := λ x y hx hy, (⟨x, hx⟩ + ⟨y, hy⟩ : S).2, smul_mem' := λ c x hx, (algebra.smul_def c x).symm ▸ (⟨algebra_map R A c, S.range_le ⟨c, rfl⟩⟩ * ⟨x, hx⟩:S).2 } instance coe_to_submodule : has_coe (subalgebra R A) (submodule R A) := ⟨to_submodule⟩ instance to_submodule.is_subring {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] (S : subalgebra R A) : is_subring ((S : submodule R A) : set A) := S.is_subring @[simp] lemma mem_to_submodule {x} : x ∈ (S : submodule R A) ↔ x ∈ S := iff.rfl theorem to_submodule_injective {S U : subalgebra R A} (h : (S : submodule R A) = U) : S = U := ext $ λ x, by rw [← mem_to_submodule, ← mem_to_submodule, h] theorem to_submodule_inj {S U : subalgebra R A} : (S : submodule R A) = U ↔ S = U := ⟨to_submodule_injective, congr_arg _⟩ /-- Linear equivalence between `S : submodule R A` and `S`. Though these types are equal, we define it as a `linear_equiv` to avoid type equalities. -/ def to_submodule_equiv (S : subalgebra R A) : (S : submodule R A) ≃ₗ[R] S := linear_equiv.of_eq _ _ rfl instance : partial_order (subalgebra R A) := { le := λ S T, (S : set A) ⊆ (T : set A), le_refl := λ S, set.subset.refl S, le_trans := λ _ _ _, set.subset.trans, le_antisymm := λ S T hst hts, ext $ λ x, ⟨@hst x, @hts x⟩ } /-- Reinterpret an `S`-subalgebra as an `R`-subalgebra in `comap R S A`. -/ def comap {R : Type u} {S : Type v} {A : Type w} [comm_semiring R] [comm_semiring S] [semiring A] [algebra R S] [algebra S A] (iSB : subalgebra S A) : subalgebra R (algebra.comap R S A) := { algebra_map_mem' := λ r, iSB.algebra_map_mem (algebra_map R S r), .. iSB } /-- If `S` is an `R`-subalgebra of `A` and `T` is an `S`-subalgebra of `A`, then `T` is an `R`-subalgebra of `A`. -/ def under {R : Type u} {A : Type v} [comm_semiring R] [comm_semiring A] {i : algebra R A} (S : subalgebra R A) (T : subalgebra S A) : subalgebra R A := { algebra_map_mem' := λ r, T.algebra_map_mem ⟨algebra_map R A r, S.algebra_map_mem r⟩, .. T } /-- Transport a subalgebra via an algebra homomorphism. -/ def map (S : subalgebra R A) (f : A →ₐ[R] B) : subalgebra R B := { algebra_map_mem' := λ r, f.commutes r ▸ set.mem_image_of_mem _ (S.algebra_map_mem r), .. subsemiring.map (f : A →+* B) S,} /-- Preimage of a subalgebra under an algebra homomorphism. -/ def comap' (S : subalgebra R B) (f : A →ₐ[R] B) : subalgebra R A := { algebra_map_mem' := λ r, show f (algebra_map R A r) ∈ S, from (f.commutes r).symm ▸ S.algebra_map_mem r, .. subsemiring.comap (f : A →+* B) S,} lemma map_mono {S₁ S₂ : subalgebra R A} {f : A →ₐ[R] B} : S₁ ≤ S₂ → S₁.map f ≤ S₂.map f := set.image_subset f theorem map_le {S : subalgebra R A} {f : A →ₐ[R] B} {U : subalgebra R B} : map S f ≤ U ↔ S ≤ comap' U f := set.image_subset_iff lemma map_injective {S₁ S₂ : subalgebra R A} (f : A →ₐ[R] B) (hf : function.injective f) (ih : S₁.map f = S₂.map f) : S₁ = S₂ := ext $ set.ext_iff.1 $ set.image_injective.2 hf $ set.ext $ ext_iff.1 ih lemma mem_map {S : subalgebra R A} {f : A →ₐ[R] B} {y : B} : y ∈ map S f ↔ ∃ x ∈ S, f x = y := subsemiring.mem_map instance integral_domain {R A : Type*} [comm_ring R] [integral_domain A] [algebra R A] (S : subalgebra R A) : integral_domain S := @subring.domain A _ S _ end subalgebra namespace alg_hom variables {R : Type u} {A : Type v} {B : Type w} variables [comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B] variables (φ : A →ₐ[R] B) /-- Range of an `alg_hom` as a subalgebra. -/ protected def range (φ : A →ₐ[R] B) : subalgebra R B := { algebra_map_mem' := λ r, ⟨algebra_map R A r, set.mem_univ _, φ.commutes r⟩, .. φ.to_ring_hom.srange } @[simp] lemma mem_range (φ : A →ₐ[R] B) {y : B} : y ∈ φ.range ↔ ∃ x, φ x = y := ring_hom.mem_srange @[simp] lemma coe_range (φ : A →ₐ[R] B) : (φ.range : set B) = set.range φ := by { ext, rw [subalgebra.mem_coe, mem_range], refl } /-- Restrict the codomain of an algebra homomorphism. -/ def cod_restrict (f : A →ₐ[R] B) (S : subalgebra R B) (hf : ∀ x, f x ∈ S) : A →ₐ[R] S := { commutes' := λ r, subtype.eq $ f.commutes r, .. ring_hom.cod_srestrict (f : A →+* B) S hf } theorem injective_cod_restrict (f : A →ₐ[R] B) (S : subalgebra R B) (hf : ∀ x, f x ∈ S) : function.injective (f.cod_restrict S hf) ↔ function.injective f := ⟨λ H x y hxy, H $ subtype.eq hxy, λ H x y hxy, H (congr_arg subtype.val hxy : _)⟩ /-- The equalizer of two R-algebra homomorphisms -/ def equalizer (ϕ ψ : A →ₐ[R] B) : subalgebra R A := { carrier := {a | ϕ a = ψ a}, zero_mem' := by { change ϕ 0 = ψ 0, rw [alg_hom.map_zero, alg_hom.map_zero] }, add_mem' := λ x y hx hy, by { change ϕ x = ψ x at hx, change ϕ y = ψ y at hy, change ϕ (x + y) = ψ (x + y), rw [alg_hom.map_add, alg_hom.map_add, hx, hy] }, one_mem' := by { change ϕ 1 = ψ 1, rw [alg_hom.map_one, alg_hom.map_one] }, mul_mem' := λ x y hx hy, by { change ϕ x = ψ x at hx, change ϕ y = ψ y at hy, change ϕ (x * y) = ψ (x * y), rw [alg_hom.map_mul, alg_hom.map_mul, hx, hy] }, algebra_map_mem' := λ x, by { change ϕ (algebra_map R A x) = ψ (algebra_map R A x), rw [alg_hom.commutes, alg_hom.commutes] } } @[simp] lemma mem_equalizer (ϕ ψ : A →ₐ[R] B) (x : A) : x ∈ ϕ.equalizer ψ ↔ ϕ x = ψ x := iff.rfl end alg_hom namespace algebra variables (R : Type u) {A : Type v} {B : Type w} variables [comm_semiring R] [semiring A] [algebra R A] [semiring B] [algebra R B] /-- The minimal subalgebra that includes `s`. -/ def adjoin (s : set A) : subalgebra R A := { algebra_map_mem' := λ r, subsemiring.subset_closure $ or.inl ⟨r, rfl⟩, .. subsemiring.closure (set.range (algebra_map R A) ∪ s) } variables {R} protected lemma gc : galois_connection (adjoin R : set A → subalgebra R A) coe := λ s S, ⟨λ H, le_trans (le_trans (set.subset_union_right _ _) subsemiring.subset_closure) H, λ H, subsemiring.closure_le.2 $ set.union_subset S.range_subset H⟩ /-- Galois insertion between `adjoin` and `coe`. -/ protected def gi : galois_insertion (adjoin R : set A → subalgebra R A) coe := { choice := λ s hs, adjoin R s, gc := algebra.gc, le_l_u := λ S, (algebra.gc (S : set A) (adjoin R S)).1 $ le_refl _, choice_eq := λ _ _, rfl } instance : complete_lattice (subalgebra R A) := galois_insertion.lift_complete_lattice algebra.gi instance : inhabited (subalgebra R A) := ⟨⊥⟩ theorem mem_bot {x : A} : x ∈ (⊥ : subalgebra R A) ↔ x ∈ set.range (algebra_map R A) := suffices (of_id R A).range = (⊥ : subalgebra R A), by { rw [← this, ← subalgebra.mem_coe, alg_hom.coe_range], refl }, le_bot_iff.mp (λ x hx, subalgebra.range_le _ ((of_id R A).coe_range ▸ hx)) theorem to_submodule_bot : ((⊥ : subalgebra R A) : submodule R A) = submodule.span R {1} := by { ext x, simp [mem_bot, -set.singleton_one, submodule.mem_span_singleton, algebra.smul_def] } @[simp] theorem mem_top {x : A} : x ∈ (⊤ : subalgebra R A) := subsemiring.subset_closure $ or.inr trivial @[simp] theorem coe_top : ((⊤ : subalgebra R A) : submodule R A) = ⊤ := submodule.ext $ λ x, iff_of_true mem_top trivial @[simp] theorem coe_bot : ((⊥ : subalgebra R A) : set A) = set.range (algebra_map R A) := by simp [set.ext_iff, algebra.mem_bot] theorem eq_top_iff {S : subalgebra R A} : S = ⊤ ↔ ∀ x : A, x ∈ S := ⟨λ h x, by rw h; exact mem_top, λ h, by ext x; exact ⟨λ _, mem_top, λ _, h x⟩⟩ @[simp] theorem map_top (f : A →ₐ[R] B) : subalgebra.map (⊤ : subalgebra R A) f = f.range := subalgebra.ext $ λ x, ⟨λ ⟨y, _, hy⟩, ⟨y, set.mem_univ _, hy⟩, λ ⟨y, mem, hy⟩, ⟨y, algebra.mem_top, hy⟩⟩ @[simp] theorem map_bot (f : A →ₐ[R] B) : subalgebra.map (⊥ : subalgebra R A) f = ⊥ := eq_bot_iff.2 $ λ x ⟨y, hy, hfy⟩, let ⟨r, hr⟩ := mem_bot.1 hy in subalgebra.range_le _ ⟨r, by rwa [← f.commutes, hr]⟩ @[simp] theorem comap_top (f : A →ₐ[R] B) : subalgebra.comap' (⊤ : subalgebra R B) f = ⊤ := eq_top_iff.2 $ λ x, mem_top /-- `alg_hom` to `⊤ : subalgebra R A`. -/ def to_top : A →ₐ[R] (⊤ : subalgebra R A) := by refine_struct { to_fun := λ x, (⟨x, mem_top⟩ : (⊤ : subalgebra R A)) }; intros; refl theorem surjective_algebra_map_iff : function.surjective (algebra_map R A) ↔ (⊤ : subalgebra R A) = ⊥ := ⟨λ h, eq_bot_iff.2 $ λ y _, let ⟨x, hx⟩ := h y in hx ▸ subalgebra.algebra_map_mem _ _, λ h y, algebra.mem_bot.1 $ eq_bot_iff.1 h (algebra.mem_top : y ∈ _)⟩ theorem bijective_algebra_map_iff {R A : Type*} [field R] [semiring A] [nontrivial A] [algebra R A] : function.bijective (algebra_map R A) ↔ (⊤ : subalgebra R A) = ⊥ := ⟨λ h, surjective_algebra_map_iff.1 h.2, λ h, ⟨(algebra_map R A).injective, surjective_algebra_map_iff.2 h⟩⟩ /-- The bottom subalgebra is isomorphic to the base ring. -/ noncomputable def bot_equiv_of_injective (h : function.injective (algebra_map R A)) : (⊥ : subalgebra R A) ≃ₐ[R] R := alg_equiv.symm $ alg_equiv.of_bijective (algebra.of_id R _) ⟨λ x y hxy, h (congr_arg subtype.val hxy : _), λ ⟨y, hy⟩, let ⟨x, hx⟩ := algebra.mem_bot.1 hy in ⟨x, subtype.eq hx⟩⟩ /-- The bottom subalgebra is isomorphic to the field. -/ noncomputable def bot_equiv (F R : Type*) [field F] [semiring R] [nontrivial R] [algebra F R] : (⊥ : subalgebra F R) ≃ₐ[F] F := bot_equiv_of_injective (ring_hom.injective _) /-- The top subalgebra is isomorphic to the field. -/ noncomputable def top_equiv : (⊤ : subalgebra R A) ≃ₐ[R] A := (alg_equiv.of_bijective to_top ⟨λ _ _, subtype.mk.inj, λ x, ⟨x.val, by { ext, refl }⟩⟩ : A ≃ₐ[R] (⊤ : subalgebra R A)).symm end algebra namespace subalgebra open algebra variables {R : Type u} {A : Type v} variables [comm_semiring R] [semiring A] [algebra R A] variables (S : subalgebra R A) lemma range_val : S.val.range = S := ext $ set.ext_iff.1 $ S.val.coe_range.trans subtype.range_val instance : unique (subalgebra R R) := { uniq := begin intro S, refine le_antisymm (λ r hr, _) bot_le, simp only [set.mem_range, coe_bot, id.map_eq_self, exists_apply_eq_apply, default], end .. algebra.subalgebra.inhabited } end subalgebra section nat variables {R : Type*} [semiring R] /-- A subsemiring is a `ℕ`-subalgebra. -/ def subalgebra_of_subsemiring (S : subsemiring R) : subalgebra ℕ R := { algebra_map_mem' := λ i, S.coe_nat_mem i, .. S } @[simp] lemma mem_subalgebra_of_subsemiring {x : R} {S : subsemiring R} : x ∈ subalgebra_of_subsemiring S ↔ x ∈ S := iff.rfl end nat section int variables {R : Type*} [ring R] /-- A subring is a `ℤ`-subalgebra. -/ def subalgebra_of_subring (S : subring R) : subalgebra ℤ R := { algebra_map_mem' := λ i, int.induction_on i S.zero_mem (λ i ih, S.add_mem ih S.one_mem) (λ i ih, show ((-i - 1 : ℤ) : R) ∈ S, by { rw [int.cast_sub, int.cast_one], exact S.sub_mem ih S.one_mem }), .. S } /-- A subset closed under the ring operations is a `ℤ`-subalgebra. -/ def subalgebra_of_is_subring (S : set R) [is_subring S] : subalgebra ℤ R := subalgebra_of_subring S.to_subring variables {S : Type*} [semiring S] @[simp] lemma mem_subalgebra_of_subring {x : R} {S : subring R} : x ∈ subalgebra_of_subring S ↔ x ∈ S := iff.rfl @[simp] lemma mem_subalgebra_of_is_subring {x : R} {S : set R} [is_subring S] : x ∈ subalgebra_of_is_subring S ↔ x ∈ S := iff.rfl end int
5b24a26cb99593f28e9f0e11cab17d0be876a12d
08bd4ba4ca87dba1f09d2c96a26f5d65da81f4b4
/src/Lean/Parser.lean
8b3683a99885fcdae431988ac6c3a104ad8be7e0
[ "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "Apache-2.0", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
gebner/lean4
d51c4922640a52a6f7426536ea669ef18a1d9af5
8cd9ce06843c9d42d6d6dc43d3e81e3b49dfc20f
refs/heads/master
1,685,732,780,391
1,672,962,627,000
1,673,459,398,000
373,307,283
0
0
Apache-2.0
1,691,316,730,000
1,622,669,271,000
Lean
UTF-8
Lean
false
false
7,638
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ import Lean.Parser.Basic import Lean.Parser.Level import Lean.Parser.Term import Lean.Parser.Tactic import Lean.Parser.Command import Lean.Parser.Module import Lean.Parser.Syntax import Lean.Parser.Do namespace Lean namespace Parser open Lean.PrettyPrinter open Lean.PrettyPrinter.Parenthesizer open Lean.PrettyPrinter.Formatter builtin_initialize register_parser_alias "ws" checkWsBefore { stackSz? := some 0 } register_parser_alias "noWs" checkNoWsBefore { stackSz? := some 0 } register_parser_alias "linebreak" checkLinebreakBefore { stackSz? := some 0 } register_parser_alias (kind := numLitKind) "num" numLit register_parser_alias (kind := strLitKind) "str" strLit register_parser_alias (kind := charLitKind) "char" charLit register_parser_alias (kind := nameLitKind) "name" nameLit register_parser_alias (kind := scientificLitKind) "scientific" scientificLit register_parser_alias (kind := identKind) "ident" ident register_parser_alias "colGt" checkColGt { stackSz? := some 0 } register_parser_alias "colGe" checkColGe { stackSz? := some 0 } register_parser_alias "colEq" checkColEq { stackSz? := some 0 } register_parser_alias "lineEq" checkLineEq { stackSz? := some 0 } register_parser_alias lookahead { stackSz? := some 0 } register_parser_alias atomic { stackSz? := none } register_parser_alias many register_parser_alias many1 register_parser_alias manyIndent register_parser_alias many1Indent register_parser_alias optional { autoGroupArgs := false } register_parser_alias withPosition { stackSz? := none } register_parser_alias withoutPosition { stackSz? := none } register_parser_alias withoutForbidden { stackSz? := none } register_parser_alias (kind := interpolatedStrKind) interpolatedStr register_parser_alias orelse register_parser_alias andthen { stackSz? := none } registerAlias "notFollowedBy" ``notFollowedBy (notFollowedBy · "element") Parenthesizer.registerAlias "notFollowedBy" notFollowedBy.parenthesizer Formatter.registerAlias "notFollowedBy" notFollowedBy.formatter end Parser namespace PrettyPrinter namespace Parenthesizer -- Close the mutual recursion loop; see corresponding `[extern]` in the parenthesizer. @[export lean_mk_antiquot_parenthesizer] def mkAntiquot.parenthesizer (name : String) (kind : SyntaxNodeKind) (anonymous := true) (isPseudoKind := true) : Parenthesizer := Parser.mkAntiquot.parenthesizer name kind anonymous isPseudoKind -- The parenthesizer auto-generated these instances correctly, but tagged them with the wrong kind, since the actual kind -- (e.g. `ident`) is not equal to the parser name `Lean.Parser.Term.ident`. @[builtin_parenthesizer ident] def ident.parenthesizer : Parenthesizer := Parser.Term.ident.parenthesizer @[builtin_parenthesizer num] def numLit.parenthesizer : Parenthesizer := Parser.Term.num.parenthesizer @[builtin_parenthesizer scientific] def scientificLit.parenthesizer : Parenthesizer := Parser.Term.scientific.parenthesizer @[builtin_parenthesizer char] def charLit.parenthesizer : Parenthesizer := Parser.Term.char.parenthesizer @[builtin_parenthesizer str] def strLit.parenthesizer : Parenthesizer := Parser.Term.str.parenthesizer open Lean.Parser @[export lean_pretty_printer_parenthesizer_interpret_parser_descr] unsafe def interpretParserDescr : ParserDescr → CoreM Parenthesizer | ParserDescr.const n => getConstAlias parenthesizerAliasesRef n | ParserDescr.unary n d => return (← getUnaryAlias parenthesizerAliasesRef n) (← interpretParserDescr d) | ParserDescr.binary n d₁ d₂ => return (← getBinaryAlias parenthesizerAliasesRef n) (← interpretParserDescr d₁) (← interpretParserDescr d₂) | ParserDescr.node k prec d => return leadingNode.parenthesizer k prec (← interpretParserDescr d) | ParserDescr.nodeWithAntiquot n k d => return withAntiquot.parenthesizer (mkAntiquot.parenthesizer' n k (anonymous := true)) <| node.parenthesizer k (← interpretParserDescr d) | ParserDescr.sepBy p sep psep trail => return sepBy.parenthesizer (← interpretParserDescr p) sep (← interpretParserDescr psep) trail | ParserDescr.sepBy1 p sep psep trail => return sepBy1.parenthesizer (← interpretParserDescr p) sep (← interpretParserDescr psep) trail | ParserDescr.trailingNode k prec lhsPrec d => return trailingNode.parenthesizer k prec lhsPrec (← interpretParserDescr d) | ParserDescr.symbol tk => return symbol.parenthesizer tk | ParserDescr.nonReservedSymbol tk includeIdent => return nonReservedSymbol.parenthesizer tk includeIdent | ParserDescr.parser constName => combinatorParenthesizerAttribute.runDeclFor constName | ParserDescr.cat catName prec => return categoryParser.parenthesizer catName prec end Parenthesizer namespace Formatter @[export lean_mk_antiquot_formatter] def mkAntiquot.formatter (name : String) (kind : SyntaxNodeKind) (anonymous := true) (isPseudoKind := true) : Formatter := Parser.mkAntiquot.formatter name kind anonymous isPseudoKind @[builtin_formatter ident] def ident.formatter : Formatter := Parser.Term.ident.formatter @[builtin_formatter num] def numLit.formatter : Formatter := Parser.Term.num.formatter @[builtin_formatter scientific] def scientificLit.formatter : Formatter := Parser.Term.scientific.formatter @[builtin_formatter char] def charLit.formatter : Formatter := Parser.Term.char.formatter @[builtin_formatter str] def strLit.formatter : Formatter := Parser.Term.str.formatter open Lean.Parser @[export lean_pretty_printer_formatter_interpret_parser_descr] unsafe def interpretParserDescr : ParserDescr → CoreM Formatter | ParserDescr.const n => getConstAlias formatterAliasesRef n | ParserDescr.unary n d => return (← getUnaryAlias formatterAliasesRef n) (← interpretParserDescr d) | ParserDescr.binary n d₁ d₂ => return (← getBinaryAlias formatterAliasesRef n) (← interpretParserDescr d₁) (← interpretParserDescr d₂) | ParserDescr.node k _ d => return node.formatter k (← interpretParserDescr d) | ParserDescr.nodeWithAntiquot n k d => return withAntiquot.formatter (mkAntiquot.formatter' n k (anonymous := true)) <| node.formatter k (← interpretParserDescr d) | ParserDescr.sepBy p sep psep trail => return sepBy.formatter (← interpretParserDescr p) sep (← interpretParserDescr psep) trail | ParserDescr.sepBy1 p sep psep trail => return sepBy1.formatter (← interpretParserDescr p) sep (← interpretParserDescr psep) trail | ParserDescr.trailingNode k prec lhsPrec d => return trailingNode.formatter k prec lhsPrec (← interpretParserDescr d) | ParserDescr.symbol tk => return symbol.formatter tk | ParserDescr.nonReservedSymbol tk _ => return nonReservedSymbol.formatter tk | ParserDescr.parser constName => combinatorFormatterAttribute.runDeclFor constName | ParserDescr.cat catName _ => return categoryParser.formatter catName end Formatter end PrettyPrinter end Lean
2dfdb2b61e1ac9f0a3d19944ec8ab7275586880d
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/data/polynomial/ring_division.lean
c0a140b181594bb118b6ce3745f1273ac78685ee
[ "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
27,021
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker, Johan Commelin -/ import data.polynomial.basic import data.polynomial.div import data.polynomial.algebra_map import data.set.finite /-! # Theory of univariate polynomials This file starts looking like the ring theory of $ R[X] $ -/ noncomputable theory local attribute [instance, priority 100] classical.prop_decidable open finset namespace polynomial universes u v w z variables {R : Type u} {S : Type v} {T : Type w} {A : Type z} {a b : R} {n : ℕ} section comm_ring variables [comm_ring R] {p q : polynomial R} variables [comm_ring S] lemma nat_degree_pos_of_aeval_root [algebra R S] {p : polynomial R} (hp : p ≠ 0) {z : S} (hz : aeval z p = 0) (inj : ∀ (x : R), algebra_map R S x = 0 → x = 0) : 0 < p.nat_degree := nat_degree_pos_of_eval₂_root hp (algebra_map R S) hz inj lemma degree_pos_of_aeval_root [algebra R S] {p : polynomial R} (hp : p ≠ 0) {z : S} (hz : aeval z p = 0) (inj : ∀ (x : R), algebra_map R S x = 0 → x = 0) : 0 < p.degree := nat_degree_pos_iff_degree_pos.mp (nat_degree_pos_of_aeval_root hp hz inj) lemma aeval_mod_by_monic_eq_self_of_root [algebra R S] {p q : polynomial R} (hq : q.monic) {x : S} (hx : aeval x q = 0) : aeval x (p %ₘ q) = aeval x p := eval₂_mod_by_monic_eq_self_of_root hq hx end comm_ring section no_zero_divisors variables [comm_ring R] [no_zero_divisors R] {p q : polynomial R} instance : no_zero_divisors (polynomial R) := { eq_zero_or_eq_zero_of_mul_eq_zero := λ a b h, begin rw [← leading_coeff_eq_zero, ← leading_coeff_eq_zero], refine eq_zero_or_eq_zero_of_mul_eq_zero _, rw [← leading_coeff_zero, ← leading_coeff_mul, h], end } lemma nat_degree_mul (hp : p ≠ 0) (hq : q ≠ 0) : nat_degree (p * q) = nat_degree p + nat_degree q := by rw [← with_bot.coe_eq_coe, ← degree_eq_nat_degree (mul_ne_zero hp hq), with_bot.coe_add, ← degree_eq_nat_degree hp, ← degree_eq_nat_degree hq, degree_mul] @[simp] lemma nat_degree_pow (p : polynomial R) (n : ℕ) : nat_degree (p ^ n) = n * nat_degree p := if hp0 : p = 0 then if hn0 : n = 0 then by simp [hp0, hn0] else by rw [hp0, zero_pow (nat.pos_of_ne_zero hn0)]; simp else nat_degree_pow' (by rw [← leading_coeff_pow, ne.def, leading_coeff_eq_zero]; exact pow_ne_zero _ hp0) lemma root_mul : is_root (p * q) a ↔ is_root p a ∨ is_root q a := by simp_rw [is_root, eval_mul, mul_eq_zero] lemma root_or_root_of_root_mul (h : is_root (p * q) a) : is_root p a ∨ is_root q a := root_mul.1 h lemma degree_le_mul_left (p : polynomial R) (hq : q ≠ 0) : degree p ≤ degree (p * q) := if hp : p = 0 then by simp only [hp, zero_mul, le_refl] else by rw [degree_mul, degree_eq_nat_degree hp, degree_eq_nat_degree hq]; exact with_bot.coe_le_coe.2 (nat.le_add_right _ _) theorem nat_degree_le_of_dvd {p q : polynomial R} (h1 : p ∣ q) (h2 : q ≠ 0) : p.nat_degree ≤ q.nat_degree := begin rcases h1 with ⟨q, rfl⟩, rw mul_ne_zero_iff at h2, rw [nat_degree_mul h2.1 h2.2], exact nat.le_add_right _ _ end end no_zero_divisors section integral_domain variables [integral_domain R] {p q : polynomial R} instance : integral_domain (polynomial R) := { ..polynomial.no_zero_divisors, ..polynomial.nontrivial, ..polynomial.comm_ring } lemma nat_trailing_degree_mul (hp : p ≠ 0) (hq : q ≠ 0) : (p * q).nat_trailing_degree = p.nat_trailing_degree + q.nat_trailing_degree := begin simp only [←nat.sub_eq_of_eq_add (nat_degree_eq_reverse_nat_degree_add_nat_trailing_degree _)], rw [reverse_mul_of_domain, nat_degree_mul hp hq, nat_degree_mul (mt reverse_eq_zero.mp hp) (mt reverse_eq_zero.mp hq), reverse_nat_degree, reverse_nat_degree, ←nat.sub_sub, nat.add_comm, nat.add_sub_assoc (nat.sub_le _ _), add_comm, nat.add_sub_assoc (nat.sub_le _ _)], end section roots open multiset local attribute [reducible] with_zero lemma degree_eq_zero_of_is_unit (h : is_unit p) : degree p = 0 := let ⟨q, hq⟩ := is_unit_iff_dvd_one.1 h in have hp0 : p ≠ 0, from λ hp0, by simpa [hp0] using hq, have hq0 : q ≠ 0, from λ hp0, by simpa [hp0] using hq, have nat_degree (1 : polynomial R) = nat_degree (p * q), from congr_arg _ hq, by rw [nat_degree_one, nat_degree_mul hp0 hq0, eq_comm, _root_.add_eq_zero_iff, ← with_bot.coe_eq_coe, ← degree_eq_nat_degree hp0] at this; exact this.1 @[simp] lemma degree_coe_units (u : units (polynomial R)) : degree (u : polynomial R) = 0 := degree_eq_zero_of_is_unit ⟨u, rfl⟩ theorem prime_X_sub_C {r : R} : prime (X - C r) := ⟨X_sub_C_ne_zero r, not_is_unit_X_sub_C, λ _ _, by { simp_rw [dvd_iff_is_root, is_root.def, eval_mul, mul_eq_zero], exact id }⟩ theorem prime_X : prime (X : polynomial R) := by { convert (prime_X_sub_C : prime (X - C 0 : polynomial R)), simp } lemma prime_of_degree_eq_one_of_monic (hp1 : degree p = 1) (hm : monic p) : prime p := have p = X - C (- p.coeff 0), by simpa [hm.leading_coeff] using eq_X_add_C_of_degree_eq_one hp1, this.symm ▸ prime_X_sub_C theorem irreducible_X_sub_C (r : R) : irreducible (X - C r) := irreducible_of_prime prime_X_sub_C theorem irreducible_X : irreducible (X : polynomial R) := irreducible_of_prime prime_X lemma irreducible_of_degree_eq_one_of_monic (hp1 : degree p = 1) (hm : monic p) : irreducible p := irreducible_of_prime (prime_of_degree_eq_one_of_monic hp1 hm) theorem eq_of_monic_of_associated (hp : p.monic) (hq : q.monic) (hpq : associated p q) : p = q := begin obtain ⟨u, hu⟩ := hpq, unfold monic at hp hq, rw eq_C_of_degree_le_zero (le_of_eq $ degree_coe_units _) at hu, rw [← hu, leading_coeff_mul, hp, one_mul, leading_coeff_C] at hq, rwa [hq, C_1, mul_one] at hu end @[simp] lemma root_multiplicity_zero {x : R} : root_multiplicity x 0 = 0 := dif_pos rfl lemma root_multiplicity_eq_zero {p : polynomial R} {x : R} (h : ¬ is_root p x) : root_multiplicity x p = 0 := begin rw root_multiplicity_eq_multiplicity, split_ifs, { refl }, rw [← enat.coe_inj, enat.coe_get, multiplicity.multiplicity_eq_zero_of_not_dvd, enat.coe_zero], intro hdvd, exact h (dvd_iff_is_root.mp hdvd) end lemma root_multiplicity_pos {p : polynomial R} (hp : p ≠ 0) {x : R} : 0 < root_multiplicity x p ↔ is_root p x := begin rw [← dvd_iff_is_root, root_multiplicity_eq_multiplicity, dif_neg hp, ← enat.coe_lt_coe, enat.coe_get], exact multiplicity.dvd_iff_multiplicity_pos end lemma root_multiplicity_mul {p q : polynomial R} {x : R} (hpq : p * q ≠ 0) : root_multiplicity x (p * q) = root_multiplicity x p + root_multiplicity x q := begin have hp : p ≠ 0 := left_ne_zero_of_mul hpq, have hq : q ≠ 0 := right_ne_zero_of_mul hpq, rw [root_multiplicity_eq_multiplicity (p * q), dif_neg hpq, root_multiplicity_eq_multiplicity p, dif_neg hp, root_multiplicity_eq_multiplicity q, dif_neg hq, @multiplicity.mul' _ _ _ (X - C x) _ _ prime_X_sub_C], end lemma root_multiplicity_X_sub_C_self {x : R} : root_multiplicity x (X - C x) = 1 := by rw [root_multiplicity_eq_multiplicity, dif_neg (X_sub_C_ne_zero x), multiplicity.get_multiplicity_self] lemma root_multiplicity_X_sub_C {x y : R} : root_multiplicity x (X - C y) = if x = y then 1 else 0 := begin split_ifs with hxy, { rw hxy, exact root_multiplicity_X_sub_C_self }, exact root_multiplicity_eq_zero (mt root_X_sub_C.mp (ne.symm hxy)) end /-- The multiplicity of `a` as root of `(X - a) ^ n` is `n`. -/ lemma root_multiplicity_X_sub_C_pow (a : R) (n : ℕ) : root_multiplicity a ((X - C a) ^ n) = n := begin induction n with n hn, { refine root_multiplicity_eq_zero _, simp only [eval_one, is_root.def, not_false_iff, one_ne_zero, pow_zero] }, have hzero := (ne_zero_of_monic (monic_pow (monic_X_sub_C a) n.succ)), rw pow_succ (X - C a) n at hzero ⊢, simp only [root_multiplicity_mul hzero, root_multiplicity_X_sub_C_self, hn, nat.one_add] end /-- If `(X - a) ^ n` divides a polynomial `p` then the multiplicity of `a` as root of `p` is at least `n`. -/ lemma root_multiplicity_of_dvd {p : polynomial R} {a : R} {n : ℕ} (hzero : p ≠ 0) (h : (X - C a) ^ n ∣ p) : n ≤ root_multiplicity a p := begin obtain ⟨q, hq⟩ := exists_eq_mul_right_of_dvd h, rw hq at hzero, simp only [hq, root_multiplicity_mul hzero, root_multiplicity_X_sub_C_pow, ge_iff_le, _root_.zero_le, le_add_iff_nonneg_right], end /-- The multiplicity of `p + q` is at least the minimum of the multiplicities. -/ lemma root_multiplicity_add {p q : polynomial R} (a : R) (hzero : p + q ≠ 0) : min (root_multiplicity a p) (root_multiplicity a q) ≤ root_multiplicity a (p + q) := begin refine root_multiplicity_of_dvd hzero _, have hdivp : (X - C a) ^ root_multiplicity a p ∣ p := pow_root_multiplicity_dvd p a, have hdivq : (X - C a) ^ root_multiplicity a q ∣ q := pow_root_multiplicity_dvd q a, exact min_pow_dvd_add hdivp hdivq end lemma exists_multiset_roots : ∀ {p : polynomial R} (hp : p ≠ 0), ∃ s : multiset R, (s.card : with_bot ℕ) ≤ degree p ∧ ∀ a, s.count a = root_multiplicity a p | p := λ hp, by haveI := classical.prop_decidable (∃ x, is_root p x); exact if h : ∃ x, is_root p x then let ⟨x, hx⟩ := h in have hpd : 0 < degree p := degree_pos_of_root hp hx, have hd0 : p /ₘ (X - C x) ≠ 0 := λ h, by rw [← mul_div_by_monic_eq_iff_is_root.2 hx, h, mul_zero] at hp; exact hp rfl, have wf : degree (p /ₘ _) < degree p := degree_div_by_monic_lt _ (monic_X_sub_C x) hp ((degree_X_sub_C x).symm ▸ dec_trivial), let ⟨t, htd, htr⟩ := @exists_multiset_roots (p /ₘ (X - C x)) hd0 in have hdeg : degree (X - C x) ≤ degree p := begin rw [degree_X_sub_C, degree_eq_nat_degree hp], rw degree_eq_nat_degree hp at hpd, exact with_bot.coe_le_coe.2 (with_bot.coe_lt_coe.1 hpd) end, have hdiv0 : p /ₘ (X - C x) ≠ 0 := mt (div_by_monic_eq_zero_iff (monic_X_sub_C x) (ne_zero_of_monic (monic_X_sub_C x))).1 $ not_lt.2 hdeg, ⟨x ::ₘ t, calc (card (x ::ₘ t) : with_bot ℕ) = t.card + 1 : by exact_mod_cast card_cons _ _ ... ≤ degree p : by rw [← degree_add_div_by_monic (monic_X_sub_C x) hdeg, degree_X_sub_C, add_comm]; exact add_le_add (le_refl (1 : with_bot ℕ)) htd, begin assume a, conv_rhs { rw ← mul_div_by_monic_eq_iff_is_root.mpr hx }, rw [root_multiplicity_mul (mul_ne_zero (X_sub_C_ne_zero _) hdiv0), root_multiplicity_X_sub_C, ← htr a], split_ifs with ha, { rw [ha, count_cons_self, nat.succ_eq_add_one, add_comm] }, { rw [count_cons_of_ne ha, zero_add] }, end⟩ else ⟨0, (degree_eq_nat_degree hp).symm ▸ with_bot.coe_le_coe.2 (nat.zero_le _), by { intro a, rw [count_zero, root_multiplicity_eq_zero (not_exists.mp h a)] }⟩ using_well_founded {dec_tac := tactic.assumption} /-- `roots p` noncomputably gives a multiset containing all the roots of `p`, including their multiplicities. -/ noncomputable def roots (p : polynomial R) : multiset R := if h : p = 0 then ∅ else classical.some (exists_multiset_roots h) @[simp] lemma roots_zero : (0 : polynomial R).roots = 0 := dif_pos rfl lemma card_roots (hp0 : p ≠ 0) : ((roots p).card : with_bot ℕ) ≤ degree p := begin unfold roots, rw dif_neg hp0, exact (classical.some_spec (exists_multiset_roots hp0)).1 end lemma card_roots' {p : polynomial R} (hp0 : p ≠ 0) : p.roots.card ≤ nat_degree p := with_bot.coe_le_coe.1 (le_trans (card_roots hp0) (le_of_eq $ degree_eq_nat_degree hp0)) lemma card_roots_sub_C {p : polynomial R} {a : R} (hp0 : 0 < degree p) : ((p - C a).roots.card : with_bot ℕ) ≤ degree p := calc ((p - C a).roots.card : with_bot ℕ) ≤ degree (p - C a) : card_roots $ mt sub_eq_zero.1 $ λ h, not_le_of_gt hp0 $ h.symm ▸ degree_C_le ... = degree p : by rw [sub_eq_add_neg, ← C_neg]; exact degree_add_C hp0 lemma card_roots_sub_C' {p : polynomial R} {a : R} (hp0 : 0 < degree p) : (p - C a).roots.card ≤ nat_degree p := with_bot.coe_le_coe.1 (le_trans (card_roots_sub_C hp0) (le_of_eq $ degree_eq_nat_degree (λ h, by simp [*, lt_irrefl] at *))) @[simp] lemma count_roots (hp : p ≠ 0) : p.roots.count a = root_multiplicity a p := by { rw [roots, dif_neg hp], exact (classical.some_spec (exists_multiset_roots hp)).2 a } @[simp] lemma mem_roots (hp : p ≠ 0) : a ∈ p.roots ↔ is_root p a := by rw [← count_pos, count_roots hp, root_multiplicity_pos hp] lemma eq_zero_of_infinite_is_root (p : polynomial R) (h : set.infinite {x | is_root p x}) : p = 0 := begin by_contradiction hp, apply h, convert p.roots.to_finset.finite_to_set using 1, ext1 r, simp only [mem_roots hp, multiset.mem_to_finset, set.mem_set_of_eq, finset.mem_coe] end lemma eq_of_infinite_eval_eq {R : Type*} [integral_domain R] (p q : polynomial R) (h : set.infinite {x | eval x p = eval x q}) : p = q := begin rw [← sub_eq_zero], apply eq_zero_of_infinite_is_root, simpa only [is_root, eval_sub, sub_eq_zero] end lemma roots_mul {p q : polynomial R} (hpq : p * q ≠ 0) : (p * q).roots = p.roots + q.roots := multiset.ext.mpr $ λ r, by rw [count_add, count_roots hpq, count_roots (left_ne_zero_of_mul hpq), count_roots (right_ne_zero_of_mul hpq), root_multiplicity_mul hpq] @[simp] lemma mem_roots_sub_C {p : polynomial R} {a x : R} (hp0 : 0 < degree p) : x ∈ (p - C a).roots ↔ p.eval x = a := (mem_roots (show p - C a ≠ 0, from mt sub_eq_zero.1 $ λ h, not_le_of_gt hp0 $ h.symm ▸ degree_C_le)).trans (by rw [is_root.def, eval_sub, eval_C, sub_eq_zero]) @[simp] lemma roots_X_sub_C (r : R) : roots (X - C r) = r ::ₘ 0 := begin ext s, rw [count_roots (X_sub_C_ne_zero r), root_multiplicity_X_sub_C], split_ifs with h, { rw [h, count_singleton] }, { rw [count_cons_of_ne h, count_zero] } end @[simp] lemma roots_C (x : R) : (C x).roots = 0 := if H : x = 0 then by rw [H, C_0, roots_zero] else multiset.ext.mpr $ λ r, have h : C x ≠ 0, from λ h, H $ C_inj.1 $ h.symm ▸ C_0.symm, have not_root : ¬ is_root (C x) r := mt (λ (h : eval r (C x) = 0), trans eval_C.symm h) H, by rw [count_roots h, count_zero, root_multiplicity_eq_zero not_root] @[simp] lemma roots_one : (1 : polynomial R).roots = ∅ := roots_C 1 lemma roots_list_prod (L : list (polynomial R)) : ((0 : polynomial R) ∉ L) → L.prod.roots = (L : multiset (polynomial R)).bind roots := list.rec_on L (λ _, roots_one) $ λ hd tl ih H, begin rw [list.mem_cons_iff, not_or_distrib] at H, rw [list.prod_cons, roots_mul (mul_ne_zero (ne.symm H.1) $ list.prod_ne_zero H.2), ← multiset.cons_coe, multiset.cons_bind, ih H.2] end lemma roots_multiset_prod (m : multiset (polynomial R)) : (0 : polynomial R) ∉ m → m.prod.roots = m.bind roots := by { rcases m with ⟨L⟩, simpa only [coe_prod, quot_mk_to_coe''] using roots_list_prod L } lemma roots_prod {ι : Type*} (f : ι → polynomial R) (s : finset ι) : s.prod f ≠ 0 → (s.prod f).roots = s.val.bind (λ i, roots (f i)) := begin rcases s with ⟨m, hm⟩, simpa [multiset.prod_eq_zero_iff, bind_map] using roots_multiset_prod (m.map f) end lemma roots_prod_X_sub_C (s : finset R) : (s.prod (λ a, X - C a)).roots = s.val := (roots_prod (λ a, X - C a) s (prod_ne_zero_iff.mpr (λ a _, X_sub_C_ne_zero a))).trans (by simp_rw [roots_X_sub_C, bind_cons, bind_zero, add_zero, multiset.map_id']) lemma card_roots_X_pow_sub_C {n : ℕ} (hn : 0 < n) (a : R) : (roots ((X : polynomial R) ^ n - C a)).card ≤ n := with_bot.coe_le_coe.1 $ calc ((roots ((X : polynomial R) ^ n - C a)).card : with_bot ℕ) ≤ degree ((X : polynomial R) ^ n - C a) : card_roots (X_pow_sub_C_ne_zero hn a) ... = n : degree_X_pow_sub_C hn a section nth_roots /-- `nth_roots n a` noncomputably returns the solutions to `x ^ n = a`-/ def nth_roots (n : ℕ) (a : R) : multiset R := roots ((X : polynomial R) ^ n - C a) @[simp] lemma mem_nth_roots {n : ℕ} (hn : 0 < n) {a x : R} : x ∈ nth_roots n a ↔ x ^ n = a := by rw [nth_roots, mem_roots (X_pow_sub_C_ne_zero hn a), is_root.def, eval_sub, eval_C, eval_pow, eval_X, sub_eq_zero] @[simp] lemma nth_roots_zero (r : R) : nth_roots 0 r = 0 := by simp only [empty_eq_zero, pow_zero, nth_roots, ← C_1, ← C_sub, roots_C] lemma card_nth_roots (n : ℕ) (a : R) : (nth_roots n a).card ≤ n := if hn : n = 0 then if h : (X : polynomial R) ^ n - C a = 0 then by simp only [nat.zero_le, nth_roots, roots, h, dif_pos rfl, empty_eq_zero, card_zero] else with_bot.coe_le_coe.1 (le_trans (card_roots h) (by rw [hn, pow_zero, ← C_1, ← @is_ring_hom.map_sub _ _ _ _ (@C R _)]; exact degree_C_le)) else by rw [← with_bot.coe_le_coe, ← degree_X_pow_sub_C (nat.pos_of_ne_zero hn) a]; exact card_roots (X_pow_sub_C_ne_zero (nat.pos_of_ne_zero hn) a) /-- The multiset `nth_roots ↑n (1 : R)` as a finset. -/ def nth_roots_finset (n : ℕ) (R : Type*) [integral_domain R] : finset R := multiset.to_finset (nth_roots n (1 : R)) @[simp] lemma mem_nth_roots_finset {n : ℕ} (h : 0 < n) {x : R} : x ∈ nth_roots_finset n R ↔ x ^ (n : ℕ) = 1 := by rw [nth_roots_finset, mem_to_finset, mem_nth_roots h] end nth_roots lemma coeff_comp_degree_mul_degree (hqd0 : nat_degree q ≠ 0) : coeff (p.comp q) (nat_degree p * nat_degree q) = leading_coeff p * leading_coeff q ^ nat_degree p := if hp0 : p = 0 then by simp [hp0] else calc coeff (p.comp q) (nat_degree p * nat_degree q) = p.sum (λ n a, coeff (C a * q ^ n) (nat_degree p * nat_degree q)) : by rw [comp, eval₂, coeff_sum] ... = coeff (C (leading_coeff p) * q ^ nat_degree p) (nat_degree p * nat_degree q) : finset.sum_eq_single _ begin assume b hbs hbp, have hq0 : q ≠ 0, from λ hq0, hqd0 (by rw [hq0, nat_degree_zero]), have : coeff p b ≠ 0, by rwa finsupp.mem_support_iff at hbs, refine coeff_eq_zero_of_degree_lt _, erw [degree_mul, degree_C this, degree_pow, zero_add, degree_eq_nat_degree hq0, ← with_bot.coe_nsmul, nsmul_eq_mul, with_bot.coe_lt_coe, nat.cast_id, mul_lt_mul_right (pos_iff_ne_zero.mpr hqd0)], exact lt_of_le_of_ne (le_nat_degree_of_ne_zero this) hbp, end begin intro h, contrapose! hp0, rw finsupp.mem_support_iff at h, push_neg at h, rwa ← leading_coeff_eq_zero, end ... = _ : have coeff (q ^ nat_degree p) (nat_degree p * nat_degree q) = leading_coeff (q ^ nat_degree p), by rw [leading_coeff, nat_degree_pow], by rw [coeff_C_mul, this, leading_coeff_pow] lemma nat_degree_comp : nat_degree (p.comp q) = nat_degree p * nat_degree q := le_antisymm nat_degree_comp_le (if hp0 : p = 0 then by rw [hp0, zero_comp, nat_degree_zero, zero_mul] else if hqd0 : nat_degree q = 0 then have degree q ≤ 0, by rw [← with_bot.coe_zero, ← hqd0]; exact degree_le_nat_degree, by rw [eq_C_of_degree_le_zero this]; simp else le_nat_degree_of_ne_zero $ have hq0 : q ≠ 0, from λ hq0, hqd0 $ by rw [hq0, nat_degree_zero], calc coeff (p.comp q) (nat_degree p * nat_degree q) = leading_coeff p * leading_coeff q ^ nat_degree p : coeff_comp_degree_mul_degree hqd0 ... ≠ 0 : mul_ne_zero (mt leading_coeff_eq_zero.1 hp0) (pow_ne_zero _ (mt leading_coeff_eq_zero.1 hq0))) lemma leading_coeff_comp (hq : nat_degree q ≠ 0) : leading_coeff (p.comp q) = leading_coeff p * leading_coeff q ^ nat_degree p := by rw [← coeff_comp_degree_mul_degree hq, ← nat_degree_comp]; refl lemma units_coeff_zero_smul (c : units (polynomial R)) (p : polynomial R) : (c : polynomial R).coeff 0 • p = c * p := by rw [←polynomial.C_mul', ←polynomial.eq_C_of_degree_eq_zero (degree_coe_units c)] @[simp] lemma nat_degree_coe_units (u : units (polynomial R)) : nat_degree (u : polynomial R) = 0 := nat_degree_eq_of_degree_eq_some (degree_coe_units u) lemma comp_eq_zero_iff : p.comp q = 0 ↔ p = 0 ∨ (p.eval (q.coeff 0) = 0 ∧ q = C (q.coeff 0)) := begin split, { intro h, have key : p.nat_degree = 0 ∨ q.nat_degree = 0, { rw [←mul_eq_zero, ←nat_degree_comp, h, nat_degree_zero] }, replace key := or.imp eq_C_of_nat_degree_eq_zero eq_C_of_nat_degree_eq_zero key, cases key, { rw [key, C_comp] at h, exact or.inl (key.trans h) }, { rw [key, comp_C, C_eq_zero] at h, exact or.inr ⟨h, key⟩ }, }, { exact λ h, or.rec (λ h, by rw [h, zero_comp]) (λ h, by rw [h.2, comp_C, h.1, C_0]) h }, end lemma zero_of_eval_zero [infinite R] (p : polynomial R) (h : ∀ x, p.eval x = 0) : p = 0 := by classical; by_contradiction hp; exact infinite.not_fintype ⟨p.roots.to_finset, λ x, multiset.mem_to_finset.mpr ((mem_roots hp).mpr (h _))⟩ lemma funext [infinite R] {p q : polynomial R} (ext : ∀ r : R, p.eval r = q.eval r) : p = q := begin rw ← sub_eq_zero, apply zero_of_eval_zero, intro x, rw [eval_sub, sub_eq_zero, ext], end /-- The set of distinct roots of `p` in `E`. If you have a non-separable polynomial, use `polynomial.roots` for the multiset where multiple roots have the appropriate multiplicity. -/ def root_set (p : polynomial R) (S) [integral_domain S] [algebra R S] : set S := (p.map (algebra_map R S)).roots.to_finset lemma root_set_def (p : polynomial R) (S) [integral_domain S] [algebra R S] : p.root_set S = (p.map (algebra_map R S)).roots.to_finset := rfl @[simp] lemma root_set_zero (S) [integral_domain S] [algebra R S] : (0 : polynomial R).root_set S = ∅ := by rw [root_set_def, polynomial.map_zero, roots_zero, to_finset_zero, finset.coe_empty] end roots theorem is_unit_iff {f : polynomial R} : is_unit f ↔ ∃ r : R, is_unit r ∧ C r = f := ⟨λ hf, ⟨f.coeff 0, is_unit_C.1 $ eq_C_of_degree_eq_zero (degree_eq_zero_of_is_unit hf) ▸ hf, (eq_C_of_degree_eq_zero (degree_eq_zero_of_is_unit hf)).symm⟩, λ ⟨r, hr, hrf⟩, hrf ▸ is_unit_C.2 hr⟩ lemma coeff_coe_units_zero_ne_zero (u : units (polynomial R)) : coeff (u : polynomial R) 0 ≠ 0 := begin conv in (0) {rw [← nat_degree_coe_units u]}, rw [← leading_coeff, ne.def, leading_coeff_eq_zero], exact units.ne_zero _ end lemma degree_eq_degree_of_associated (h : associated p q) : degree p = degree q := let ⟨u, hu⟩ := h in by simp [hu.symm] lemma degree_eq_one_of_irreducible_of_root (hi : irreducible p) {x : R} (hx : is_root p x) : degree p = 1 := let ⟨g, hg⟩ := dvd_iff_is_root.2 hx in have is_unit (X - C x) ∨ is_unit g, from hi.is_unit_or_is_unit hg, this.elim (λ h, have h₁ : degree (X - C x) = 1, from degree_X_sub_C x, have h₂ : degree (X - C x) = 0, from degree_eq_zero_of_is_unit h, by rw h₁ at h₂; exact absurd h₂ dec_trivial) (λ hgu, by rw [hg, degree_mul, degree_X_sub_C, degree_eq_zero_of_is_unit hgu, add_zero]) /-- Division by a monic polynomial doesn't change the leading coefficient. -/ lemma leading_coeff_div_by_monic_of_monic {R : Type u} [integral_domain R] {p q : polynomial R} (hmonic : q.monic) (hdegree : q.degree ≤ p.degree) : (p /ₘ q).leading_coeff = p.leading_coeff := begin have hp := mod_by_monic_add_div p hmonic, have hzero : (p /ₘ q) ≠ 0, { intro h, exact not_lt_of_le hdegree ((div_by_monic_eq_zero_iff hmonic (monic.ne_zero hmonic)).1 h) }, have deglt : (p %ₘ q).degree < (q * (p /ₘ q)).degree, { rw degree_mul, refine lt_of_lt_of_le (degree_mod_by_monic_lt p hmonic (monic.ne_zero hmonic)) _, rw [degree_eq_nat_degree (monic.ne_zero hmonic), degree_eq_nat_degree hzero], norm_cast, simp only [zero_le, le_add_iff_nonneg_right] }, have hrew := (leading_coeff_add_of_degree_lt deglt), rw leading_coeff_mul q (p /ₘ q) at hrew, simp only [hmonic, one_mul, monic.leading_coeff] at hrew, nth_rewrite 1 ← hp, exact hrew.symm end lemma eq_of_monic_of_dvd_of_nat_degree_le (hp : p.monic) (hq : q.monic) (hdiv : p ∣ q) (hdeg : q.nat_degree ≤ p.nat_degree) : q = p := begin obtain ⟨r, hr⟩ := hdiv, have rzero : r ≠ 0, { intro h, simpa [h, monic.ne_zero hq] using hr }, rw [hr, nat_degree_mul (monic.ne_zero hp) rzero] at hdeg, have hdegeq : p.nat_degree + r.nat_degree = p.nat_degree, { suffices hdegle : p.nat_degree ≤ p.nat_degree + r.nat_degree, { exact le_antisymm hdeg hdegle }, exact nat.le.intro rfl }, replace hdegeq := eq_C_of_nat_degree_eq_zero (((@add_right_inj _ _ p.nat_degree) _ 0).1 hdegeq), suffices hlead : 1 = r.leading_coeff, { have hcoeff := leading_coeff_C (r.coeff 0), rw [← hdegeq, ← hlead] at hcoeff, rw [← hcoeff, C_1] at hdegeq, rwa [hdegeq, mul_one] at hr }, have hprod : q.leading_coeff = p.leading_coeff * r.leading_coeff, { simp only [hr, leading_coeff_mul] }, rwa [monic.leading_coeff hp, monic.leading_coeff hq, one_mul] at hprod end end integral_domain section variables [semiring R] [integral_domain S] (φ : R →+* S) lemma is_unit_of_is_unit_leading_coeff_of_is_unit_map (f : polynomial R) (hf : is_unit (leading_coeff f)) (H : is_unit (map φ f)) : is_unit f := begin have dz := degree_eq_zero_of_is_unit H, rw degree_map_eq_of_leading_coeff_ne_zero at dz, { rw eq_C_of_degree_eq_zero dz, apply is_unit.map', convert hf, rw (degree_eq_iff_nat_degree_eq _).1 dz, rintro rfl, simpa using H, }, { intro h, have u : is_unit (φ f.leading_coeff) := is_unit.map' _ hf, rw h at u, simpa using u, } end end section variables [integral_domain R] [integral_domain S] (φ : R →+* S) /-- A polynomial over an integral domain `R` is irreducible if it is monic and irreducible after mapping into an integral domain `S`. A special case of this lemma is that a polynomial over `ℤ` is irreducible if it is monic and irreducible over `ℤ/pℤ` for some prime `p`. -/ lemma monic.irreducible_of_irreducible_map (f : polynomial R) (h_mon : monic f) (h_irr : irreducible (map φ f)) : irreducible f := begin fsplit, { intro h, exact h_irr.not_unit (is_unit.map (monoid_hom.of (map φ)) h), }, { intros a b h, have q := (leading_coeff_mul a b).symm, rw ←h at q, dsimp [monic] at h_mon, rw h_mon at q, have au : is_unit a.leading_coeff := is_unit_of_mul_eq_one _ _ q, rw mul_comm at q, have bu : is_unit b.leading_coeff := is_unit_of_mul_eq_one _ _ q, clear q h_mon, have h' := congr_arg (map φ) h, simp only [map_mul] at h', cases h_irr.is_unit_or_is_unit h' with w w, { left, exact is_unit_of_is_unit_leading_coeff_of_is_unit_map _ _ au w, }, { right, exact is_unit_of_is_unit_leading_coeff_of_is_unit_map _ _ bu w, }, } end end end polynomial namespace is_integral_domain variables {R : Type*} [comm_ring R] /-- Lift evidence that `is_integral_domain R` to `is_integral_domain (polynomial R)`. -/ lemma polynomial (h : is_integral_domain R) : is_integral_domain (polynomial R) := @integral_domain.to_is_integral_domain _ (@polynomial.integral_domain _ (h.to_integral_domain _)) end is_integral_domain
6cdb0ebbad700ef94674c8eec946bd5733f51988
8e6cad62ec62c6c348e5faaa3c3f2079012bdd69
/src/linear_algebra/char_poly/coeff.lean
cef0b705904aef750f396a79c19a9b4708de46af
[ "Apache-2.0" ]
permissive
benjamindavidson/mathlib
8cc81c865aa8e7cf4462245f58d35ae9a56b150d
fad44b9f670670d87c8e25ff9cdf63af87ad731e
refs/heads/master
1,679,545,578,362
1,615,343,014,000
1,615,343,014,000
312,926,983
0
0
Apache-2.0
1,615,360,301,000
1,605,399,418,000
Lean
UTF-8
Lean
false
false
8,953
lean
/- Copyright (c) 2020 Aaron Anderson, Jalex Stark. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Aaron Anderson, Jalex Stark. -/ import data.matrix.char_p import linear_algebra.char_poly.basic import linear_algebra.matrix import ring_theory.polynomial.basic import algebra.polynomial.big_operators import group_theory.perm.cycles import field_theory.finite.basic /-! # Characteristic polynomials We give methods for computing coefficients of the characteristic polynomial. ## Main definitions - `char_poly_degree_eq_dim` proves that the degree of the characteristic polynomial over a nonzero ring is the dimension of the matrix - `det_eq_sign_char_poly_coeff` proves that the determinant is the constant term of the characteristic polynomial, up to sign. - `trace_eq_neg_char_poly_coeff` proves that the trace is the negative of the (d-1)th coefficient of the characteristic polynomial, where d is the dimension of the matrix. For a nonzero ring, this is the second-highest coefficient. -/ noncomputable theory universes u v w z open polynomial matrix open_locale big_operators variables {R : Type u} [comm_ring R] variables {n G : Type v} [decidable_eq n] [fintype n] variables {α β : Type v} [decidable_eq α] open finset open polynomial variable {M : matrix n n R} lemma char_matrix_apply_nat_degree [nontrivial R] (i j : n) : (char_matrix M i j).nat_degree = ite (i = j) 1 0 := by { by_cases i = j; simp [h, ← degree_eq_iff_nat_degree_eq_of_pos (nat.succ_pos 0)], } lemma char_matrix_apply_nat_degree_le (i j : n) : (char_matrix M i j).nat_degree ≤ ite (i = j) 1 0 := by split_ifs; simp [h, nat_degree_X_sub_C_le] variable (M) lemma char_poly_sub_diagonal_degree_lt : (char_poly M - ∏ (i : n), (X - C (M i i))).degree < ↑(fintype.card n - 1) := begin rw [char_poly, det, ← insert_erase (mem_univ (equiv.refl n)), sum_insert (not_mem_erase (equiv.refl n) univ), add_comm], simp only [char_matrix_apply_eq, one_mul, equiv.perm.sign_refl, id.def, int.cast_one, units.coe_one, add_sub_cancel, equiv.coe_refl], rw ← mem_degree_lt, apply submodule.sum_mem (degree_lt R (fintype.card n - 1)), intros c hc, rw [← C_eq_int_cast, C_mul'], apply submodule.smul_mem (degree_lt R (fintype.card n - 1)) ↑↑(equiv.perm.sign c), rw mem_degree_lt, apply lt_of_le_of_lt degree_le_nat_degree _, rw with_bot.coe_lt_coe, apply lt_of_le_of_lt _ (equiv.perm.fixed_point_card_lt_of_ne_one (ne_of_mem_erase hc)), apply le_trans (polynomial.nat_degree_prod_le univ (λ i : n, (char_matrix M (c i) i))) _, rw card_eq_sum_ones, rw sum_filter, apply sum_le_sum, intros, apply char_matrix_apply_nat_degree_le, end lemma char_poly_coeff_eq_prod_coeff_of_le {k : ℕ} (h : fintype.card n - 1 ≤ k) : (char_poly M).coeff k = (∏ i : n, (X - C (M i i))).coeff k := begin apply eq_of_sub_eq_zero, rw ← coeff_sub, apply polynomial.coeff_eq_zero_of_degree_lt, apply lt_of_lt_of_le (char_poly_sub_diagonal_degree_lt M) _, rw with_bot.coe_le_coe, apply h, end lemma det_of_card_zero (h : fintype.card n = 0) (M : matrix n n R) : M.det = 1 := by { rw fintype.card_eq_zero_iff at h, suffices : M = 1, { simp [this] }, ext, tauto } theorem char_poly_degree_eq_dim [nontrivial R] (M : matrix n n R) : (char_poly M).degree = fintype.card n := begin by_cases fintype.card n = 0, { rw h, unfold char_poly, rw det_of_card_zero, {simp}, {assumption} }, rw ← sub_add_cancel (char_poly M) (∏ (i : n), (X - C (M i i))), have h1 : (∏ (i : n), (X - C (M i i))).degree = fintype.card n, { rw degree_eq_iff_nat_degree_eq_of_pos, swap, apply nat.pos_of_ne_zero h, rw nat_degree_prod', simp_rw nat_degree_X_sub_C, unfold fintype.card, simp, simp_rw (monic_X_sub_C _).leading_coeff, simp, }, rw degree_add_eq_right_of_degree_lt, exact h1, rw h1, apply lt_trans (char_poly_sub_diagonal_degree_lt M), rw with_bot.coe_lt_coe, rw ← nat.pred_eq_sub_one, apply nat.pred_lt, apply h, end theorem char_poly_nat_degree_eq_dim [nontrivial R] (M : matrix n n R) : (char_poly M).nat_degree = fintype.card n := nat_degree_eq_of_degree_eq_some (char_poly_degree_eq_dim M) lemma char_poly_monic (M : matrix n n R) : monic (char_poly M) := begin nontriviality, by_cases fintype.card n = 0, {rw [char_poly, det_of_card_zero h], apply monic_one}, have mon : (∏ (i : n), (X - C (M i i))).monic, { apply monic_prod_of_monic univ (λ i : n, (X - C (M i i))), simp [monic_X_sub_C], }, rw ← sub_add_cancel (∏ (i : n), (X - C (M i i))) (char_poly M) at mon, rw monic at *, rw leading_coeff_add_of_degree_lt at mon, rw ← mon, rw char_poly_degree_eq_dim, rw ← neg_sub, rw degree_neg, apply lt_trans (char_poly_sub_diagonal_degree_lt M), rw with_bot.coe_lt_coe, rw ← nat.pred_eq_sub_one, apply nat.pred_lt, apply h, end theorem trace_eq_neg_char_poly_coeff [nonempty n] (M : matrix n n R) : (matrix.trace n R R) M = -(char_poly M).coeff (fintype.card n - 1) := begin nontriviality, rw char_poly_coeff_eq_prod_coeff_of_le, swap, refl, rw [fintype.card, prod_X_sub_C_coeff_card_pred univ (λ i : n, M i i)], simp, rw [← fintype.card, fintype.card_pos_iff], apply_instance, end -- I feel like this should use polynomial.alg_hom_eval₂_algebra_map lemma mat_poly_equiv_eval (M : matrix n n (polynomial R)) (r : R) (i j : n) : (mat_poly_equiv M).eval ((scalar n) r) i j = (M i j).eval r := begin unfold polynomial.eval, unfold eval₂, transitivity finsupp.sum (mat_poly_equiv M) (λ (e : ℕ) (a : matrix n n R), (a * (scalar n) r ^ e) i j), { unfold finsupp.sum, rw sum_apply, rw sum_apply, dsimp, refl, }, { simp_rw ← (scalar n).map_pow, simp_rw ← (matrix.scalar.commute _ _).eq, simp only [coe_scalar, matrix.one_mul, ring_hom.id_apply, smul_apply, mul_eq_mul, algebra.smul_mul_assoc], have h : ∀ x : ℕ, (λ (e : ℕ) (a : R), r ^ e * a) x 0 = 0 := by simp, symmetry, rw ← finsupp.sum_map_range_index h, swap, refl, refine congr (congr rfl _) (by {ext, rw mul_comm}), ext, rw finsupp.map_range_apply, simp [apply_eq_coeff], } end lemma eval_det (M : matrix n n (polynomial R)) (r : R) : polynomial.eval r M.det = (polynomial.eval (matrix.scalar n r) (mat_poly_equiv M)).det := begin rw [polynomial.eval, ← coe_eval₂_ring_hom, ring_hom.map_det], apply congr_arg det, ext, symmetry, convert mat_poly_equiv_eval _ _ _ _, end theorem det_eq_sign_char_poly_coeff (M : matrix n n R) : M.det = (-1)^(fintype.card n) * (char_poly M).coeff 0:= begin rw [coeff_zero_eq_eval_zero, char_poly, eval_det, mat_poly_equiv_char_matrix, ← det_smul], simp end variables {p : ℕ} [fact p.prime] @[simp] lemma finite_field.char_poly_pow_card {K : Type*} [field K] [fintype K] (M : matrix n n K) : char_poly (M ^ (fintype.card K)) = char_poly M := begin by_cases hn : nonempty n, { letI := hn, cases char_p.exists K with p hp, letI := hp, rcases finite_field.card K p with ⟨⟨k, kpos⟩, ⟨hp, hk⟩⟩, letI : fact p.prime := hp, dsimp at hk, rw hk at *, apply (frobenius_inj (polynomial K) p).iterate k, repeat { rw iterate_frobenius, rw ← hk }, rw ← finite_field.expand_card, unfold char_poly, rw [alg_hom.map_det, ← is_monoid_hom.map_pow], apply congr_arg det, apply mat_poly_equiv.injective, swap, { apply_instance }, rw [← mat_poly_equiv.coe_alg_hom, alg_hom.map_pow, mat_poly_equiv.coe_alg_hom, mat_poly_equiv_char_matrix, hk, sub_pow_char_pow_of_commute, ← C_pow], swap, { apply polynomial.commute_X }, -- the following is a nasty case bash that should be abstracted as a lemma -- (and maybe it can be proven more... algebraically?) ext, rw [coeff_sub, coeff_C], by_cases hij : i = j; simp [char_matrix, hij, coeff_X_pow]; simp only [coeff_C]; split_ifs; simp *, }, { congr, apply @subsingleton.elim _ (subsingleton_of_empty_left hn) _ _, }, end @[simp] lemma zmod.char_poly_pow_card (M : matrix n n (zmod p)) : char_poly (M ^ p) = char_poly M := by { have h := finite_field.char_poly_pow_card M, rwa zmod.card at h, } lemma finite_field.trace_pow_card {K : Type*} [field K] [fintype K] [nonempty n] (M : matrix n n K) : trace n K K (M ^ (fintype.card K)) = (trace n K K M) ^ (fintype.card K) := by rw [trace_eq_neg_char_poly_coeff, trace_eq_neg_char_poly_coeff, finite_field.char_poly_pow_card, finite_field.pow_card] lemma zmod.trace_pow_card {p:ℕ} [fact p.prime] [nonempty n] (M : matrix n n (zmod p)) : trace n (zmod p) (zmod p) (M ^ p) = (trace n (zmod p) (zmod p) M)^p := by { have h := finite_field.trace_pow_card M, rwa zmod.card at h, } namespace matrix theorem is_integral : is_integral R M := ⟨char_poly M, ⟨char_poly_monic M, aeval_self_char_poly M⟩⟩ theorem min_poly_dvd_char_poly {K : Type*} [field K] (M : matrix n n K) : (minpoly K M) ∣ char_poly M := minpoly.dvd _ _ (aeval_self_char_poly M) end matrix
a79f054d3305d0c85ac1df3d9bbbcdeb6f8ea430
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/tests/lean/run/decClassical.lean
3a7c2f9f949aa5ab30455be5efddec4353c825a5
[ "Apache-2.0" ]
permissive
williamdemeo/lean4
72161c58fe65c3ad955d6a3050bb7d37c04c0d54
6d00fcf1d6d873e195f9220c668ef9c58e9c4a35
refs/heads/master
1,678,305,356,877
1,614,708,995,000
1,614,708,995,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
314
lean
open Classical theorem ex : if (fun x => x + 1) = (fun x => x + 2) then False else True := by have (fun x => x + 1) ≠ (fun x => x + 2) by intro h have 1 = 2 from congrFun h 0 exact absurd this decide! rw ifNeg this exact True.intro def tst (x : Nat) : Bool := if 1 < 2 then true else false
947683a9fce04091fc95398e4a17088c5cd5db9c
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/linear_algebra/basic.lean
d3b20ba062aa09b453be9bb7f79c6c18c4632e74
[ "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
106,555
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Kevin Buzzard, Yury Kudryashov, Frédéric Dupuis, Heather Macbeth -/ import algebra.big_operators.pi import algebra.module.hom import algebra.module.prod import algebra.module.submodule_lattice import data.dfinsupp import data.finsupp.basic import order.compactly_generated import order.omega_complete_partial_order /-! # Linear algebra This file defines the basics of linear algebra. It sets up the "categorical/lattice structure" of modules over a ring, submodules, and linear maps. Many of the relevant definitions, including `module`, `submodule`, and `linear_map`, are found in `src/algebra/module`. ## Main definitions * Many constructors for (semi)linear maps * `submodule.span s` is defined to be the smallest submodule containing the set `s`. * The kernel `ker` and range `range` of a linear map are submodules of the domain and codomain respectively. * The general linear group is defined to be the group of invertible linear maps from `M` to itself. See `linear_algebra.quotient` for quotients by submodules. ## Main theorems See `linear_algebra.isomorphisms` for Noether's three isomorphism theorems for modules. ## Notations * We continue to use the notations `M →ₛₗ[σ] M₂` and `M →ₗ[R] M₂` for the type of semilinear (resp. linear) maps from `M` to `M₂` over the ring homomorphism `σ` (resp. over the ring `R`). * We introduce the notation `R ∙ v` for the span of a singleton, `submodule.span R {v}`. This is `\.`, not the same as the scalar multiplication `•`/`\bub`. ## Implementation notes We note that, when constructing linear maps, it is convenient to use operations defined on bundled maps (`linear_map.prod`, `linear_map.coprod`, arithmetic operations like `+`) instead of defining a function and proving it is linear. ## TODO * Parts of this file have not yet been generalized to semilinear maps ## Tags linear algebra, vector space, module -/ open function open_locale big_operators pointwise variables {R : Type*} {R₁ : Type*} {R₂ : Type*} {R₃ : Type*} {R₄ : Type*} variables {K : Type*} {K₂ : Type*} variables {M : Type*} {M' : Type*} {M₁ : Type*} {M₂ : Type*} {M₃ : Type*} {M₄ : Type*} variables {N : Type*} {N₂ : Type*} variables {ι : Type*} variables {V : Type*} {V₂ : Type*} namespace finsupp lemma smul_sum {α : Type*} {β : Type*} {R : Type*} {M : Type*} [has_zero β] [monoid R] [add_comm_monoid M] [distrib_mul_action R M] {v : α →₀ β} {c : R} {h : α → β → M} : c • (v.sum h) = v.sum (λa b, c • h a b) := finset.smul_sum @[simp] lemma sum_smul_index_linear_map' {α : Type*} {R : Type*} {M : Type*} {M₂ : Type*} [semiring R] [add_comm_monoid M] [module R M] [add_comm_monoid M₂] [module R M₂] {v : α →₀ M} {c : R} {h : α → M →ₗ[R] M₂} : (c • v).sum (λ a, h a) = c • (v.sum (λ a, h a)) := begin rw [finsupp.sum_smul_index', finsupp.smul_sum], { simp only [linear_map.map_smul], }, { intro i, exact (h i).map_zero }, end variables (α : Type*) [fintype α] variables (R M) [add_comm_monoid M] [semiring R] [module R M] /-- Given `fintype α`, `linear_equiv_fun_on_fintype R` is the natural `R`-linear equivalence between `α →₀ β` and `α → β`. -/ @[simps apply] noncomputable def linear_equiv_fun_on_fintype : (α →₀ M) ≃ₗ[R] (α → M) := { to_fun := coe_fn, map_add' := λ f g, by { ext, refl }, map_smul' := λ c f, by { ext, refl }, .. equiv_fun_on_fintype } @[simp] lemma linear_equiv_fun_on_fintype_single [decidable_eq α] (x : α) (m : M) : (linear_equiv_fun_on_fintype R M α) (single x m) = pi.single x m := begin ext a, change (equiv_fun_on_fintype (single x m)) a = _, convert _root_.congr_fun (equiv_fun_on_fintype_single x m) a, end @[simp] lemma linear_equiv_fun_on_fintype_symm_single [decidable_eq α] (x : α) (m : M) : (linear_equiv_fun_on_fintype R M α).symm (pi.single x m) = single x m := begin ext a, change (equiv_fun_on_fintype.symm (pi.single x m)) a = _, convert congr_fun (equiv_fun_on_fintype_symm_single x m) a, end @[simp] lemma linear_equiv_fun_on_fintype_symm_coe (f : α →₀ M) : (linear_equiv_fun_on_fintype R M α).symm f = f := by { ext, simp [linear_equiv_fun_on_fintype], } end finsupp section open_locale classical /-- decomposing `x : ι → R` as a sum along the canonical basis -/ lemma pi_eq_sum_univ {ι : Type*} [fintype ι] {R : Type*} [semiring R] (x : ι → R) : x = ∑ i, x i • (λj, if i = j then 1 else 0) := by { ext, simp } end /-! ### Properties of linear maps -/ namespace linear_map section add_comm_monoid variables [semiring R] [semiring R₂] [semiring R₃] [semiring R₄] variables [add_comm_monoid M] [add_comm_monoid M₁] [add_comm_monoid M₂] variables [add_comm_monoid M₃] [add_comm_monoid M₄] variables [module R M] [module R M₁] [module R₂ M₂] [module R₃ M₃] [module R₄ M₄] variables {σ₁₂ : R →+* R₂} {σ₂₃ : R₂ →+* R₃} {σ₃₄ : R₃ →+* R₄} variables {σ₁₃ : R →+* R₃} {σ₂₄ : R₂ →+* R₄} {σ₁₄ : R →+* R₄} variables [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] [ring_hom_comp_triple σ₂₃ σ₃₄ σ₂₄] variables [ring_hom_comp_triple σ₁₃ σ₃₄ σ₁₄] [ring_hom_comp_triple σ₁₂ σ₂₄ σ₁₄] variables (f : M →ₛₗ[σ₁₂] M₂) (g : M₂ →ₛₗ[σ₂₃] M₃) include R R₂ theorem comp_assoc (h : M₃ →ₛₗ[σ₃₄] M₄) : ((h.comp g : M₂ →ₛₗ[σ₂₄] M₄).comp f : M →ₛₗ[σ₁₄] M₄) = h.comp (g.comp f : M →ₛₗ[σ₁₃] M₃) := rfl omit R R₂ /-- The restriction of a linear map `f : M → M₂` to a submodule `p ⊆ M` gives a linear map `p → M₂`. -/ def dom_restrict (f : M →ₛₗ[σ₁₂] M₂) (p : submodule R M) : p →ₛₗ[σ₁₂] M₂ := f.comp p.subtype @[simp] lemma dom_restrict_apply (f : M →ₛₗ[σ₁₂] M₂) (p : submodule R M) (x : p) : f.dom_restrict p x = f x := rfl /-- A linear map `f : M₂ → M` whose values lie in a submodule `p ⊆ M` can be restricted to a linear map M₂ → p. -/ def cod_restrict (p : submodule R₂ M₂) (f : M →ₛₗ[σ₁₂] M₂) (h : ∀c, f c ∈ p) : M →ₛₗ[σ₁₂] p := by refine {to_fun := λc, ⟨f c, h c⟩, ..}; intros; apply set_coe.ext; simp @[simp] theorem cod_restrict_apply (p : submodule R₂ M₂) (f : M →ₛₗ[σ₁₂] M₂) {h} (x : M) : (cod_restrict p f h x : M₂) = f x := rfl @[simp] lemma comp_cod_restrict (p : submodule R₃ M₃) (h : ∀b, g b ∈ p) : ((cod_restrict p g h).comp f : M →ₛₗ[σ₁₃] p) = cod_restrict p (g.comp f) (assume b, h _) := ext $ assume b, rfl @[simp] lemma subtype_comp_cod_restrict (p : submodule R₂ M₂) (h : ∀b, f b ∈ p) : p.subtype.comp (cod_restrict p f h) = f := ext $ assume b, rfl /-- Restrict domain and codomain of an endomorphism. -/ def restrict (f : M →ₗ[R] M) {p : submodule R M} (hf : ∀ x ∈ p, f x ∈ p) : p →ₗ[R] p := (f.dom_restrict p).cod_restrict p $ set_like.forall.2 hf lemma restrict_apply {f : M →ₗ[R] M} {p : submodule R M} (hf : ∀ x ∈ p, f x ∈ p) (x : p) : f.restrict hf x = ⟨f x, hf x.1 x.2⟩ := rfl lemma subtype_comp_restrict {f : M →ₗ[R] M} {p : submodule R M} (hf : ∀ x ∈ p, f x ∈ p) : p.subtype.comp (f.restrict hf) = f.dom_restrict p := rfl lemma restrict_eq_cod_restrict_dom_restrict {f : M →ₗ[R] M} {p : submodule R M} (hf : ∀ x ∈ p, f x ∈ p) : f.restrict hf = (f.dom_restrict p).cod_restrict p (λ x, hf x.1 x.2) := rfl lemma restrict_eq_dom_restrict_cod_restrict {f : M →ₗ[R] M} {p : submodule R M} (hf : ∀ x, f x ∈ p) : f.restrict (λ x _, hf x) = (f.cod_restrict p hf).dom_restrict p := rfl instance unique_of_left [subsingleton M] : unique (M →ₛₗ[σ₁₂] M₂) := { uniq := λ f, ext $ λ x, by rw [subsingleton.elim x 0, map_zero, map_zero], .. linear_map.inhabited } instance unique_of_right [subsingleton M₂] : unique (M →ₛₗ[σ₁₂] M₂) := coe_injective.unique /-- Evaluation of a `σ₁₂`-linear map at a fixed `a`, as an `add_monoid_hom`. -/ def eval_add_monoid_hom (a : M) : (M →ₛₗ[σ₁₂] M₂) →+ M₂ := { to_fun := λ f, f a, map_add' := λ f g, linear_map.add_apply f g a, map_zero' := rfl } /-- `linear_map.to_add_monoid_hom` promoted to an `add_monoid_hom` -/ def to_add_monoid_hom' : (M →ₛₗ[σ₁₂] M₂) →+ (M →+ M₂) := { to_fun := to_add_monoid_hom, map_zero' := by ext; refl, map_add' := by intros; ext; refl } lemma sum_apply (t : finset ι) (f : ι → M →ₛₗ[σ₁₂] M₂) (b : M) : (∑ d in t, f d) b = ∑ d in t, f d b := add_monoid_hom.map_sum ((add_monoid_hom.eval b).comp to_add_monoid_hom') f _ section smul_right variables {S : Type*} [semiring S] [module R S] [module S M] [is_scalar_tower R S M] /-- When `f` is an `R`-linear map taking values in `S`, then `λb, f b • x` is an `R`-linear map. -/ def smul_right (f : M₁ →ₗ[R] S) (x : M) : M₁ →ₗ[R] M := { to_fun := λb, f b • x, map_add' := λ x y, by rw [f.map_add, add_smul], map_smul' := λ b y, by dsimp; rw [f.map_smul, smul_assoc] } @[simp] theorem coe_smul_right (f : M₁ →ₗ[R] S) (x : M) : (smul_right f x : M₁ → M) = λ c, f c • x := rfl theorem smul_right_apply (f : M₁ →ₗ[R] S) (x : M) (c : M₁) : smul_right f x c = f c • x := rfl end smul_right instance [nontrivial M] : nontrivial (module.End R M) := begin obtain ⟨m, ne⟩ := (nontrivial_iff_exists_ne (0 : M)).mp infer_instance, exact nontrivial_of_ne 1 0 (λ p, ne (linear_map.congr_fun p m)), end @[simp, norm_cast] lemma coe_fn_sum {ι : Type*} (t : finset ι) (f : ι → M →ₛₗ[σ₁₂] M₂) : ⇑(∑ i in t, f i) = ∑ i in t, (f i : M → M₂) := add_monoid_hom.map_sum ⟨@to_fun R R₂ _ _ σ₁₂ M M₂ _ _ _ _, rfl, λ x y, rfl⟩ _ _ @[simp] lemma pow_apply (f : M →ₗ[R] M) (n : ℕ) (m : M) : (f^n) m = (f^[n] m) := begin induction n with n ih, { refl, }, { simp only [function.comp_app, function.iterate_succ, linear_map.mul_apply, pow_succ, ih], exact (function.commute.iterate_self _ _ m).symm, }, end lemma pow_map_zero_of_le {f : module.End R M} {m : M} {k l : ℕ} (hk : k ≤ l) (hm : (f^k) m = 0) : (f^l) m = 0 := by rw [← tsub_add_cancel_of_le hk, pow_add, mul_apply, hm, map_zero] lemma commute_pow_left_of_commute {f : M →ₛₗ[σ₁₂] M₂} {g : module.End R M} {g₂ : module.End R₂ M₂} (h : g₂.comp f = f.comp g) (k : ℕ) : (g₂^k).comp f = f.comp (g^k) := begin induction k with k ih, { simpa only [pow_zero], }, { rw [pow_succ, pow_succ, linear_map.mul_eq_comp, linear_map.comp_assoc, ih, ← linear_map.comp_assoc, h, linear_map.comp_assoc, linear_map.mul_eq_comp], }, end lemma submodule_pow_eq_zero_of_pow_eq_zero {N : submodule R M} {g : module.End R N} {G : module.End R M} (h : G.comp N.subtype = N.subtype.comp g) {k : ℕ} (hG : G^k = 0) : g^k = 0 := begin ext m, have hg : N.subtype.comp (g^k) m = 0, { rw [← commute_pow_left_of_commute h, hG, zero_comp, zero_apply], }, simp only [submodule.subtype_apply, comp_app, submodule.coe_eq_zero, coe_comp] at hg, rw [hg, linear_map.zero_apply], end lemma coe_pow (f : M →ₗ[R] M) (n : ℕ) : ⇑(f^n) = (f^[n]) := by { ext m, apply pow_apply, } @[simp] lemma id_pow (n : ℕ) : (id : M →ₗ[R] M)^n = id := one_pow n section variables {f' : M →ₗ[R] M} lemma iterate_succ (n : ℕ) : (f' ^ (n + 1)) = comp (f' ^ n) f' := by rw [pow_succ', mul_eq_comp] lemma iterate_surjective (h : surjective f') : ∀ n : ℕ, surjective ⇑(f' ^ n) | 0 := surjective_id | (n + 1) := by { rw [iterate_succ], exact surjective.comp (iterate_surjective n) h, } lemma iterate_injective (h : injective f') : ∀ n : ℕ, injective ⇑(f' ^ n) | 0 := injective_id | (n + 1) := by { rw [iterate_succ], exact injective.comp (iterate_injective n) h, } lemma iterate_bijective (h : bijective f') : ∀ n : ℕ, bijective ⇑(f' ^ n) | 0 := bijective_id | (n + 1) := by { rw [iterate_succ], exact bijective.comp (iterate_bijective n) h, } lemma injective_of_iterate_injective {n : ℕ} (hn : n ≠ 0) (h : injective ⇑(f' ^ n)) : injective f' := begin rw [← nat.succ_pred_eq_of_pos (pos_iff_ne_zero.mpr hn), iterate_succ, coe_comp] at h, exact injective.of_comp h, end lemma surjective_of_iterate_surjective {n : ℕ} (hn : n ≠ 0) (h : surjective ⇑(f' ^ n)) : surjective f' := begin rw [← nat.succ_pred_eq_of_pos (pos_iff_ne_zero.mpr hn), nat.succ_eq_add_one, add_comm, pow_add] at h, exact surjective.of_comp h, end end section open_locale classical /-- A linear map `f` applied to `x : ι → R` can be computed using the image under `f` of elements of the canonical basis. -/ lemma pi_apply_eq_sum_univ [fintype ι] (f : (ι → R) →ₗ[R] M) (x : ι → R) : f x = ∑ i, x i • (f (λj, if i = j then 1 else 0)) := begin conv_lhs { rw [pi_eq_sum_univ x, f.map_sum] }, apply finset.sum_congr rfl (λl hl, _), rw f.map_smul end end end add_comm_monoid section module variables {S : Type*} [semiring R] [semiring S] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [module R M] [module R M₂] [module R M₃] [module S M₂] [module S M₃] [smul_comm_class R S M₂] [smul_comm_class R S M₃] (f : M →ₗ[R] M₂) variable (S) /-- Applying a linear map at `v : M`, seen as `S`-linear map from `M →ₗ[R] M₂` to `M₂`. See `linear_map.applyₗ` for a version where `S = R`. -/ @[simps] def applyₗ' : M →+ (M →ₗ[R] M₂) →ₗ[S] M₂ := { to_fun := λ v, { to_fun := λ f, f v, map_add' := λ f g, f.add_apply g v, map_smul' := λ x f, f.smul_apply x v }, map_zero' := linear_map.ext $ λ f, f.map_zero, map_add' := λ x y, linear_map.ext $ λ f, f.map_add _ _ } section variables (R M) /-- The equivalence between R-linear maps from `R` to `M`, and points of `M` itself. This says that the forgetful functor from `R`-modules to types is representable, by `R`. This as an `S`-linear equivalence, under the assumption that `S` acts on `M` commuting with `R`. When `R` is commutative, we can take this to be the usual action with `S = R`. Otherwise, `S = ℕ` shows that the equivalence is additive. See note [bundled maps over different rings]. -/ @[simps] def ring_lmap_equiv_self [module S M] [smul_comm_class R S M] : (R →ₗ[R] M) ≃ₗ[S] M := { to_fun := λ f, f 1, inv_fun := smul_right (1 : R →ₗ[R] R), left_inv := λ f, by { ext, simp }, right_inv := λ x, by simp, .. applyₗ' S (1 : R) } end end module section comm_semiring variables [comm_semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] variables [module R M] [module R M₂] [module R M₃] variables (f g : M →ₗ[R] M₂) include R /-- Composition by `f : M₂ → M₃` is a linear map from the space of linear maps `M → M₂` to the space of linear maps `M₂ → M₃`. -/ def comp_right (f : M₂ →ₗ[R] M₃) : (M →ₗ[R] M₂) →ₗ[R] (M →ₗ[R] M₃) := { to_fun := f.comp, map_add' := λ _ _, linear_map.ext $ λ _, f.map_add _ _, map_smul' := λ _ _, linear_map.ext $ λ _, f.map_smul _ _ } /-- Applying a linear map at `v : M`, seen as a linear map from `M →ₗ[R] M₂` to `M₂`. See also `linear_map.applyₗ'` for a version that works with two different semirings. This is the `linear_map` version of `add_monoid_hom.eval`. -/ @[simps] def applyₗ : M →ₗ[R] (M →ₗ[R] M₂) →ₗ[R] M₂ := { to_fun := λ v, { to_fun := λ f, f v, ..applyₗ' R v }, map_smul' := λ x y, linear_map.ext $ λ f, f.map_smul _ _, ..applyₗ' R } /-- Alternative version of `dom_restrict` as a linear map. -/ def dom_restrict' (p : submodule R M) : (M →ₗ[R] M₂) →ₗ[R] (p →ₗ[R] M₂) := { to_fun := λ φ, φ.dom_restrict p, map_add' := by simp [linear_map.ext_iff], map_smul' := by simp [linear_map.ext_iff] } @[simp] lemma dom_restrict'_apply (f : M →ₗ[R] M₂) (p : submodule R M) (x : p) : dom_restrict' p f x = f x := rfl end comm_semiring section comm_ring variables [comm_ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [module R M] [module R M₂] [module R M₃] /-- The family of linear maps `M₂ → M` parameterised by `f ∈ M₂ → R`, `x ∈ M`, is linear in `f`, `x`. -/ def smul_rightₗ : (M₂ →ₗ[R] R) →ₗ[R] M →ₗ[R] M₂ →ₗ[R] M := { to_fun := λ f, { to_fun := linear_map.smul_right f, map_add' := λ m m', by { ext, apply smul_add, }, map_smul' := λ c m, by { ext, apply smul_comm, } }, map_add' := λ f f', by { ext, apply add_smul, }, map_smul' := λ c f, by { ext, apply mul_smul, } } @[simp] lemma smul_rightₗ_apply (f : M₂ →ₗ[R] R) (x : M) (c : M₂) : (smul_rightₗ : (M₂ →ₗ[R] R) →ₗ[R] M →ₗ[R] M₂ →ₗ[R] M) f x c = (f c) • x := rfl end comm_ring end linear_map /-- The `R`-linear equivalence between additive morphisms `A →+ B` and `ℕ`-linear morphisms `A →ₗ[ℕ] B`. -/ @[simps] def add_monoid_hom_lequiv_nat {A B : Type*} (R : Type*) [semiring R] [add_comm_monoid A] [add_comm_monoid B] [module R B] : (A →+ B) ≃ₗ[R] (A →ₗ[ℕ] B) := { to_fun := add_monoid_hom.to_nat_linear_map, inv_fun := linear_map.to_add_monoid_hom, map_add' := by { intros, ext, refl }, map_smul' := by { intros, ext, refl }, left_inv := by { intros f, ext, refl }, right_inv := by { intros f, ext, refl } } /-- The `R`-linear equivalence between additive morphisms `A →+ B` and `ℤ`-linear morphisms `A →ₗ[ℤ] B`. -/ @[simps] def add_monoid_hom_lequiv_int {A B : Type*} (R : Type*) [semiring R] [add_comm_group A] [add_comm_group B] [module R B] : (A →+ B) ≃ₗ[R] (A →ₗ[ℤ] B) := { to_fun := add_monoid_hom.to_int_linear_map, inv_fun := linear_map.to_add_monoid_hom, map_add' := by { intros, ext, refl }, map_smul' := by { intros, ext, refl }, left_inv := by { intros f, ext, refl }, right_inv := by { intros f, ext, refl } } /-! ### Properties of submodules -/ namespace submodule section add_comm_monoid variables [semiring R] [semiring R₂] [semiring R₃] variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M'] variables [module R M] [module R M'] [module R₂ M₂] [module R₃ M₃] variables {σ₁₂ : R →+* R₂} {σ₂₃ : R₂ →+* R₃} {σ₁₃ : R →+* R₃} variables {σ₂₁ : R₂ →+* R} variables [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂] variables [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] variables (p p' : submodule R M) (q q' : submodule R₂ M₂) variables (q₁ q₁' : submodule R M') variables {r : R} {x y : M} open set variables {p p'} /-- If two submodules `p` and `p'` satisfy `p ⊆ p'`, then `of_le p p'` is the linear map version of this inclusion. -/ def of_le (h : p ≤ p') : p →ₗ[R] p' := p.subtype.cod_restrict p' $ λ ⟨x, hx⟩, h hx @[simp] theorem coe_of_le (h : p ≤ p') (x : p) : (of_le h x : M) = x := rfl theorem of_le_apply (h : p ≤ p') (x : p) : of_le h x = ⟨x, h x.2⟩ := rfl theorem of_le_injective (h : p ≤ p') : function.injective (of_le h) := λ x y h, subtype.val_injective (subtype.mk.inj h) variables (p p') lemma subtype_comp_of_le (p q : submodule R M) (h : p ≤ q) : q.subtype.comp (of_le h) = p.subtype := by { ext ⟨b, hb⟩, refl } variables (R) @[simp] lemma subsingleton_iff : subsingleton (submodule R M) ↔ subsingleton M := have h : subsingleton (submodule R M) ↔ subsingleton (add_submonoid M), { rw [←subsingleton_iff_bot_eq_top, ←subsingleton_iff_bot_eq_top], convert to_add_submonoid_eq.symm; refl, }, h.trans add_submonoid.subsingleton_iff @[simp] lemma nontrivial_iff : nontrivial (submodule R M) ↔ nontrivial M := not_iff_not.mp ( (not_nontrivial_iff_subsingleton.trans $ subsingleton_iff R).trans not_nontrivial_iff_subsingleton.symm) variables {R} instance [subsingleton M] : unique (submodule R M) := ⟨⟨⊥⟩, λ a, @subsingleton.elim _ ((subsingleton_iff R).mpr ‹_›) a _⟩ instance unique' [subsingleton R] : unique (submodule R M) := by haveI := module.subsingleton R M; apply_instance instance [nontrivial M] : nontrivial (submodule R M) := (nontrivial_iff R).mpr ‹_› theorem disjoint_def {p p' : submodule R M} : disjoint p p' ↔ ∀ x ∈ p, x ∈ p' → x = (0:M) := show (∀ x, x ∈ p ∧ x ∈ p' → x ∈ ({0} : set M)) ↔ _, by simp theorem disjoint_def' {p p' : submodule R M} : disjoint p p' ↔ ∀ (x ∈ p) (y ∈ p'), x = y → x = (0:M) := disjoint_def.trans ⟨λ h x hx y hy hxy, h x hx $ hxy.symm ▸ hy, λ h x hx hx', h _ hx x hx' rfl⟩ theorem mem_right_iff_eq_zero_of_disjoint {p p' : submodule R M} (h : disjoint p p') {x : p} : (x:M) ∈ p' ↔ x = 0 := ⟨λ hx, coe_eq_zero.1 $ disjoint_def.1 h x x.2 hx, λ h, h.symm ▸ p'.zero_mem⟩ theorem mem_left_iff_eq_zero_of_disjoint {p p' : submodule R M} (h : disjoint p p') {x : p'} : (x:M) ∈ p ↔ x = 0 := ⟨λ hx, coe_eq_zero.1 $ disjoint_def.1 h x hx x.2, λ h, h.symm ▸ p.zero_mem⟩ section variables [ring_hom_surjective σ₁₂] /-- The pushforward of a submodule `p ⊆ M` by `f : M → M₂` -/ def map (f : M →ₛₗ[σ₁₂] M₂) (p : submodule R M) : submodule R₂ M₂ := { carrier := f '' p, smul_mem' := begin rintro c x ⟨y, hy, rfl⟩, obtain ⟨a, rfl⟩ := σ₁₂.is_surjective c, exact ⟨_, p.smul_mem a hy, f.map_smulₛₗ _ _⟩, end, .. p.to_add_submonoid.map f.to_add_monoid_hom } @[simp] lemma map_coe (f : M →ₛₗ[σ₁₂] M₂) (p : submodule R M) : (map f p : set M₂) = f '' p := rfl @[simp] lemma mem_map {f : M →ₛₗ[σ₁₂] M₂} {p : submodule R M} {x : M₂} : x ∈ map f p ↔ ∃ y, y ∈ p ∧ f y = x := iff.rfl theorem mem_map_of_mem {f : M →ₛₗ[σ₁₂] M₂} {p : submodule R M} {r} (h : r ∈ p) : f r ∈ map f p := set.mem_image_of_mem _ h lemma apply_coe_mem_map (f : M →ₛₗ[σ₁₂] M₂) {p : submodule R M} (r : p) : f r ∈ map f p := mem_map_of_mem r.prop @[simp] lemma map_id : map (linear_map.id : M →ₗ[R] M) p = p := submodule.ext $ λ a, by simp lemma map_comp [ring_hom_surjective σ₂₃] [ring_hom_surjective σ₁₃] (f : M →ₛₗ[σ₁₂] M₂) (g : M₂ →ₛₗ[σ₂₃] M₃) (p : submodule R M) : map (g.comp f : M →ₛₗ[σ₁₃] M₃) p = map g (map f p) := set_like.coe_injective $ by simp [map_coe]; rw ← image_comp lemma map_mono {f : M →ₛₗ[σ₁₂] M₂} {p p' : submodule R M} : p ≤ p' → map f p ≤ map f p' := image_subset _ @[simp] lemma map_zero : map (0 : M →ₛₗ[σ₁₂] M₂) p = ⊥ := have ∃ (x : M), x ∈ p := ⟨0, p.zero_mem⟩, ext $ by simp [this, eq_comm] lemma map_add_le (f g : M →ₛₗ[σ₁₂] M₂) : map (f + g) p ≤ map f p ⊔ map g p := begin rintros x ⟨m, hm, rfl⟩, exact add_mem_sup (mem_map_of_mem hm) (mem_map_of_mem hm), end lemma range_map_nonempty (N : submodule R M) : (set.range (λ ϕ, submodule.map ϕ N : (M →ₛₗ[σ₁₂] M₂) → submodule R₂ M₂)).nonempty := ⟨_, set.mem_range.mpr ⟨0, rfl⟩⟩ end include σ₂₁ /-- The pushforward of a submodule by an injective linear map is linearly equivalent to the original submodule. -/ noncomputable def equiv_map_of_injective (f : M →ₛₗ[σ₁₂] M₂) (i : injective f) (p : submodule R M) : p ≃ₛₗ[σ₁₂] p.map f := { map_add' := by { intros, simp, refl, }, map_smul' := by { intros, simp, refl, }, ..(equiv.set.image f p i) } @[simp] lemma coe_equiv_map_of_injective_apply (f : M →ₛₗ[σ₁₂] M₂) (i : injective f) (p : submodule R M) (x : p) : (equiv_map_of_injective f i p x : M₂) = f x := rfl omit σ₂₁ /-- The pullback of a submodule `p ⊆ M₂` along `f : M → M₂` -/ def comap (f : M →ₛₗ[σ₁₂] M₂) (p : submodule R₂ M₂) : submodule R M := { carrier := f ⁻¹' p, smul_mem' := λ a x h, by simp [p.smul_mem _ h], .. p.to_add_submonoid.comap f.to_add_monoid_hom } @[simp] lemma comap_coe (f : M →ₛₗ[σ₁₂] M₂) (p : submodule R₂ M₂) : (comap f p : set M) = f ⁻¹' p := rfl @[simp] lemma mem_comap {f : M →ₛₗ[σ₁₂] M₂} {p : submodule R₂ M₂} : x ∈ comap f p ↔ f x ∈ p := iff.rfl lemma comap_id : comap linear_map.id p = p := set_like.coe_injective rfl lemma comap_comp (f : M →ₛₗ[σ₁₂] M₂) (g : M₂ →ₛₗ[σ₂₃] M₃) (p : submodule R₃ M₃) : comap (g.comp f : M →ₛₗ[σ₁₃] M₃) p = comap f (comap g p) := rfl lemma comap_mono {f : M →ₛₗ[σ₁₂] M₂} {q q' : submodule R₂ M₂} : q ≤ q' → comap f q ≤ comap f q' := preimage_mono section variables [ring_hom_surjective σ₁₂] lemma map_le_iff_le_comap {f : M →ₛₗ[σ₁₂] M₂} {p : submodule R M} {q : submodule R₂ M₂} : map f p ≤ q ↔ p ≤ comap f q := image_subset_iff lemma gc_map_comap (f : M →ₛₗ[σ₁₂] M₂) : galois_connection (map f) (comap f) | p q := map_le_iff_le_comap @[simp] lemma map_bot (f : M →ₛₗ[σ₁₂] M₂) : map f ⊥ = ⊥ := (gc_map_comap f).l_bot @[simp] lemma map_sup (f : M →ₛₗ[σ₁₂] M₂) : map f (p ⊔ p') = map f p ⊔ map f p' := (gc_map_comap f).l_sup @[simp] lemma map_supr {ι : Sort*} (f : M →ₛₗ[σ₁₂] M₂) (p : ι → submodule R M) : map f (⨆i, p i) = (⨆i, map f (p i)) := (gc_map_comap f).l_supr end @[simp] lemma comap_top (f : M →ₛₗ[σ₁₂] M₂) : comap f ⊤ = ⊤ := rfl @[simp] lemma comap_inf (f : M →ₛₗ[σ₁₂] M₂) : comap f (q ⊓ q') = comap f q ⊓ comap f q' := rfl @[simp] lemma comap_infi [ring_hom_surjective σ₁₂] {ι : Sort*} (f : M →ₛₗ[σ₁₂] M₂) (p : ι → submodule R₂ M₂) : comap f (⨅i, p i) = (⨅i, comap f (p i)) := (gc_map_comap f).u_infi @[simp] lemma comap_zero : comap (0 : M →ₛₗ[σ₁₂] M₂) q = ⊤ := ext $ by simp lemma map_comap_le [ring_hom_surjective σ₁₂] (f : M →ₛₗ[σ₁₂] M₂) (q : submodule R₂ M₂) : map f (comap f q) ≤ q := (gc_map_comap f).l_u_le _ lemma le_comap_map [ring_hom_surjective σ₁₂] (f : M →ₛₗ[σ₁₂] M₂) (p : submodule R M) : p ≤ comap f (map f p) := (gc_map_comap f).le_u_l _ section galois_insertion variables {f : M →ₛₗ[σ₁₂] M₂} (hf : surjective f) variables [ring_hom_surjective σ₁₂] include hf /-- `map f` and `comap f` form a `galois_insertion` when `f` is surjective. -/ def gi_map_comap : galois_insertion (map f) (comap f) := (gc_map_comap f).to_galois_insertion (λ S x hx, begin rcases hf x with ⟨y, rfl⟩, simp only [mem_map, mem_comap], exact ⟨y, hx, rfl⟩ end) lemma map_comap_eq_of_surjective (p : submodule R₂ M₂) : (p.comap f).map f = p := (gi_map_comap hf).l_u_eq _ lemma map_surjective_of_surjective : function.surjective (map f) := (gi_map_comap hf).l_surjective lemma comap_injective_of_surjective : function.injective (comap f) := (gi_map_comap hf).u_injective lemma map_sup_comap_of_surjective (p q : submodule R₂ M₂) : (p.comap f ⊔ q.comap f).map f = p ⊔ q := (gi_map_comap hf).l_sup_u _ _ lemma map_supr_comap_of_sujective (S : ι → submodule R₂ M₂) : (⨆ i, (S i).comap f).map f = supr S := (gi_map_comap hf).l_supr_u _ lemma map_inf_comap_of_surjective (p q : submodule R₂ M₂) : (p.comap f ⊓ q.comap f).map f = p ⊓ q := (gi_map_comap hf).l_inf_u _ _ lemma map_infi_comap_of_surjective (S : ι → submodule R₂ M₂) : (⨅ i, (S i).comap f).map f = infi S := (gi_map_comap hf).l_infi_u _ lemma comap_le_comap_iff_of_surjective (p q : submodule R₂ M₂) : p.comap f ≤ q.comap f ↔ p ≤ q := (gi_map_comap hf).u_le_u_iff lemma comap_strict_mono_of_surjective : strict_mono (comap f) := (gi_map_comap hf).strict_mono_u end galois_insertion section galois_coinsertion variables [ring_hom_surjective σ₁₂] {f : M →ₛₗ[σ₁₂] M₂} (hf : injective f) include hf /-- `map f` and `comap f` form a `galois_coinsertion` when `f` is injective. -/ def gci_map_comap : galois_coinsertion (map f) (comap f) := (gc_map_comap f).to_galois_coinsertion (λ S x, by simp [mem_comap, mem_map, hf.eq_iff]) lemma comap_map_eq_of_injective (p : submodule R M) : (p.map f).comap f = p := (gci_map_comap hf).u_l_eq _ lemma comap_surjective_of_injective : function.surjective (comap f) := (gci_map_comap hf).u_surjective lemma map_injective_of_injective : function.injective (map f) := (gci_map_comap hf).l_injective lemma comap_inf_map_of_injective (p q : submodule R M) : (p.map f ⊓ q.map f).comap f = p ⊓ q := (gci_map_comap hf).u_inf_l _ _ lemma comap_infi_map_of_injective (S : ι → submodule R M) : (⨅ i, (S i).map f).comap f = infi S := (gci_map_comap hf).u_infi_l _ lemma comap_sup_map_of_injective (p q : submodule R M) : (p.map f ⊔ q.map f).comap f = p ⊔ q := (gci_map_comap hf).u_sup_l _ _ lemma comap_supr_map_of_injective (S : ι → submodule R M) : (⨆ i, (S i).map f).comap f = supr S := (gci_map_comap hf).u_supr_l _ lemma map_le_map_iff_of_injective (p q : submodule R M) : p.map f ≤ q.map f ↔ p ≤ q := (gci_map_comap hf).l_le_l_iff lemma map_strict_mono_of_injective : strict_mono (map f) := (gci_map_comap hf).strict_mono_l end galois_coinsertion --TODO(Mario): is there a way to prove this from order properties? lemma map_inf_eq_map_inf_comap [ring_hom_surjective σ₁₂] {f : M →ₛₗ[σ₁₂] M₂} {p : submodule R M} {p' : submodule R₂ M₂} : map f p ⊓ p' = map f (p ⊓ comap f p') := le_antisymm (by rintro _ ⟨⟨x, h₁, rfl⟩, h₂⟩; exact ⟨_, ⟨h₁, h₂⟩, rfl⟩) (le_inf (map_mono inf_le_left) (map_le_iff_le_comap.2 inf_le_right)) lemma map_comap_subtype : map p.subtype (comap p.subtype p') = p ⊓ p' := ext $ λ x, ⟨by rintro ⟨⟨_, h₁⟩, h₂, rfl⟩; exact ⟨h₁, h₂⟩, λ ⟨h₁, h₂⟩, ⟨⟨_, h₁⟩, h₂, rfl⟩⟩ lemma eq_zero_of_bot_submodule : ∀(b : (⊥ : submodule R M)), b = 0 | ⟨b', hb⟩ := subtype.eq $ show b' = 0, from (mem_bot R).1 hb section variables (R) /-- The span of a set `s ⊆ M` is the smallest submodule of M that contains `s`. -/ def span (s : set M) : submodule R M := Inf {p | s ⊆ p} end variables {s t : set M} lemma mem_span : x ∈ span R s ↔ ∀ p : submodule R M, s ⊆ p → x ∈ p := mem_bInter_iff lemma subset_span : s ⊆ span R s := λ x h, mem_span.2 $ λ p hp, hp h lemma span_le {p} : span R s ≤ p ↔ s ⊆ p := ⟨subset.trans subset_span, λ ss x h, mem_span.1 h _ ss⟩ lemma span_mono (h : s ⊆ t) : span R s ≤ span R t := span_le.2 $ subset.trans h subset_span lemma span_eq_of_le (h₁ : s ⊆ p) (h₂ : p ≤ span R s) : span R s = p := le_antisymm (span_le.2 h₁) h₂ @[simp] lemma span_eq : span R (p : set M) = p := span_eq_of_le _ (subset.refl _) subset_span lemma map_span [ring_hom_surjective σ₁₂] (f : M →ₛₗ[σ₁₂] M₂) (s : set M) : (span R s).map f = span R₂ (f '' s) := eq.symm $ span_eq_of_le _ (set.image_subset f subset_span) $ map_le_iff_le_comap.2 $ span_le.2 $ λ x hx, subset_span ⟨x, hx, rfl⟩ alias submodule.map_span ← linear_map.map_span lemma map_span_le {R M M₂ : Type*} [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [module R M] [module R M₂] (f : M →ₗ[R] M₂) (s : set M) (N : submodule R M₂) : map f (span R s) ≤ N ↔ ∀ m ∈ s, f m ∈ N := begin rw [f.map_span, span_le, set.image_subset_iff], exact iff.rfl end @[simp] lemma span_insert_zero : span R (insert (0 : M) s) = span R s := begin refine le_antisymm _ (submodule.span_mono (set.subset_insert 0 s)), rw [span_le, set.insert_subset], exact ⟨by simp only [set_like.mem_coe, submodule.zero_mem], submodule.subset_span⟩, end alias submodule.map_span_le ← linear_map.map_span_le /- See also `span_preimage_eq` below. -/ lemma span_preimage_le (f : M →ₛₗ[σ₁₂] M₂) (s : set M₂) : span R (f ⁻¹' s) ≤ (span R₂ s).comap f := by { rw [span_le, comap_coe], exact preimage_mono (subset_span), } alias submodule.span_preimage_le ← linear_map.span_preimage_le /-- An induction principle for span membership. If `p` holds for 0 and all elements of `s`, and is preserved under addition and scalar multiplication, then `p` holds for all elements of the span of `s`. -/ @[elab_as_eliminator] lemma span_induction {p : M → Prop} (h : x ∈ span R s) (Hs : ∀ x ∈ s, p x) (H0 : p 0) (H1 : ∀ x y, p x → p y → p (x + y)) (H2 : ∀ (a:R) x, p x → p (a • x)) : p x := (@span_le _ _ _ _ _ _ ⟨p, H0, H1, H2⟩).2 Hs h lemma span_nat_eq_add_submonoid_closure (s : set M) : (span ℕ s).to_add_submonoid = add_submonoid.closure s := begin refine eq.symm (add_submonoid.closure_eq_of_le subset_span _), apply add_submonoid.to_nat_submodule.symm.to_galois_connection.l_le _, rw span_le, exact add_submonoid.subset_closure, end @[simp] lemma span_nat_eq (s : add_submonoid M) : (span ℕ (s : set M)).to_add_submonoid = s := by rw [span_nat_eq_add_submonoid_closure, s.closure_eq] lemma span_int_eq_add_subgroup_closure {M : Type*} [add_comm_group M] (s : set M) : (span ℤ s).to_add_subgroup = add_subgroup.closure s := eq.symm $ add_subgroup.closure_eq_of_le _ subset_span $ λ x hx, span_induction hx (λ x hx, add_subgroup.subset_closure hx) (add_subgroup.zero_mem _) (λ _ _, add_subgroup.add_mem _) (λ _ _ _, add_subgroup.gsmul_mem _ ‹_› _) @[simp] lemma span_int_eq {M : Type*} [add_comm_group M] (s : add_subgroup M) : (span ℤ (s : set M)).to_add_subgroup = s := by rw [span_int_eq_add_subgroup_closure, s.closure_eq] section variables (R M) /-- `span` forms a Galois insertion with the coercion from submodule to set. -/ protected def gi : galois_insertion (@span R M _ _ _) coe := { choice := λ s _, span R s, gc := λ s t, span_le, le_l_u := λ s, subset_span, choice_eq := λ s h, rfl } end @[simp] lemma span_empty : span R (∅ : set M) = ⊥ := (submodule.gi R M).gc.l_bot @[simp] lemma span_univ : span R (univ : set M) = ⊤ := eq_top_iff.2 $ set_like.le_def.2 $ subset_span lemma span_union (s t : set M) : span R (s ∪ t) = span R s ⊔ span R t := (submodule.gi R M).gc.l_sup lemma span_Union {ι} (s : ι → set M) : span R (⋃ i, s i) = ⨆ i, span R (s i) := (submodule.gi R M).gc.l_supr lemma span_eq_supr_of_singleton_spans (s : set M) : span R s = ⨆ x ∈ s, span R {x} := by simp only [←span_Union, set.bUnion_of_singleton s] @[simp] theorem coe_supr_of_directed {ι} [hι : nonempty ι] (S : ι → submodule R M) (H : directed (≤) S) : ((supr S : submodule R M) : set M) = ⋃ i, S i := begin refine subset.antisymm _ (Union_subset $ le_supr S), suffices : (span R (⋃ i, (S i : set M)) : set M) ⊆ ⋃ (i : ι), ↑(S i), by simpa only [span_Union, span_eq] using this, refine (λ x hx, span_induction hx (λ _, id) _ _ _); simp only [mem_Union, exists_imp_distrib], { exact hι.elim (λ i, ⟨i, (S i).zero_mem⟩) }, { intros x y i hi j hj, rcases H i j with ⟨k, ik, jk⟩, exact ⟨k, add_mem _ (ik hi) (jk hj)⟩ }, { exact λ a x i hi, ⟨i, smul_mem _ a hi⟩ }, end @[simp] theorem mem_supr_of_directed {ι} [nonempty ι] (S : ι → submodule R M) (H : directed (≤) S) {x} : x ∈ supr S ↔ ∃ i, x ∈ S i := by { rw [← set_like.mem_coe, coe_supr_of_directed S H, mem_Union], refl } theorem mem_Sup_of_directed {s : set (submodule R M)} {z} (hs : s.nonempty) (hdir : directed_on (≤) s) : z ∈ Sup s ↔ ∃ y ∈ s, z ∈ y := begin haveI : nonempty s := hs.to_subtype, simp only [Sup_eq_supr', mem_supr_of_directed _ hdir.directed_coe, set_coe.exists, subtype.coe_mk] end @[norm_cast, simp] lemma coe_supr_of_chain (a : ℕ →ₘ submodule R M) : (↑(⨆ k, a k) : set M) = ⋃ k, (a k : set M) := coe_supr_of_directed a a.monotone.directed_le /-- We can regard `coe_supr_of_chain` as the statement that `coe : (submodule R M) → set M` is Scott continuous for the ω-complete partial order induced by the complete lattice structures. -/ lemma coe_scott_continuous : omega_complete_partial_order.continuous' (coe : submodule R M → set M) := ⟨set_like.coe_mono, coe_supr_of_chain⟩ @[simp] lemma mem_supr_of_chain (a : ℕ →ₘ submodule R M) (m : M) : m ∈ (⨆ k, a k) ↔ ∃ k, m ∈ a k := mem_supr_of_directed a a.monotone.directed_le section variables {p p'} lemma mem_sup : x ∈ p ⊔ p' ↔ ∃ (y ∈ p) (z ∈ p'), y + z = x := ⟨λ h, begin rw [← span_eq p, ← span_eq p', ← span_union] at h, apply span_induction h, { rintro y (h | h), { exact ⟨y, h, 0, by simp, by simp⟩ }, { exact ⟨0, by simp, y, h, by simp⟩ } }, { exact ⟨0, by simp, 0, by simp⟩ }, { rintro _ _ ⟨y₁, hy₁, z₁, hz₁, rfl⟩ ⟨y₂, hy₂, z₂, hz₂, rfl⟩, exact ⟨_, add_mem _ hy₁ hy₂, _, add_mem _ hz₁ hz₂, by simp [add_assoc]; cc⟩ }, { rintro a _ ⟨y, hy, z, hz, rfl⟩, exact ⟨_, smul_mem _ a hy, _, smul_mem _ a hz, by simp [smul_add]⟩ } end, by rintro ⟨y, hy, z, hz, rfl⟩; exact add_mem _ ((le_sup_left : p ≤ p ⊔ p') hy) ((le_sup_right : p' ≤ p ⊔ p') hz)⟩ lemma mem_sup' : x ∈ p ⊔ p' ↔ ∃ (y : p) (z : p'), (y:M) + z = x := mem_sup.trans $ by simp only [set_like.exists, coe_mk] lemma coe_sup : ↑(p ⊔ p') = (p + p' : set M) := by { ext, rw [set_like.mem_coe, mem_sup, set.mem_add], simp, } end /- This is the character `∙`, with escape sequence `\.`, and is thus different from the scalar multiplication character `•`, with escape sequence `\bub`. -/ notation R`∙`:1000 x := span R (@singleton _ _ set.has_singleton x) lemma mem_span_singleton_self (x : M) : x ∈ R ∙ x := subset_span rfl lemma nontrivial_span_singleton {x : M} (h : x ≠ 0) : nontrivial (R ∙ x) := ⟨begin use [0, x, submodule.mem_span_singleton_self x], intros H, rw [eq_comm, submodule.mk_eq_zero] at H, exact h H end⟩ lemma mem_span_singleton {y : M} : x ∈ (R ∙ y) ↔ ∃ a:R, a • y = x := ⟨λ h, begin apply span_induction h, { rintro y (rfl|⟨⟨⟩⟩), exact ⟨1, by simp⟩ }, { exact ⟨0, by simp⟩ }, { rintro _ _ ⟨a, rfl⟩ ⟨b, rfl⟩, exact ⟨a + b, by simp [add_smul]⟩ }, { rintro a _ ⟨b, rfl⟩, exact ⟨a * b, by simp [smul_smul]⟩ } end, by rintro ⟨a, y, rfl⟩; exact smul_mem _ _ (subset_span $ by simp)⟩ lemma le_span_singleton_iff {s : submodule R M} {v₀ : M} : s ≤ (R ∙ v₀) ↔ ∀ v ∈ s, ∃ r : R, r • v₀ = v := by simp_rw [set_like.le_def, mem_span_singleton] lemma span_singleton_eq_top_iff (x : M) : (R ∙ x) = ⊤ ↔ ∀ v, ∃ r : R, r • x = v := begin rw [eq_top_iff, le_span_singleton_iff], finish, end @[simp] lemma span_zero_singleton : (R ∙ (0:M)) = ⊥ := by { ext, simp [mem_span_singleton, eq_comm] } lemma span_singleton_eq_range (y : M) : ↑(R ∙ y) = range ((• y) : R → M) := set.ext $ λ x, mem_span_singleton lemma span_singleton_smul_le (r : R) (x : M) : (R ∙ (r • x)) ≤ R ∙ x := begin rw [span_le, set.singleton_subset_iff, set_like.mem_coe], exact smul_mem _ _ (mem_span_singleton_self _) end lemma span_singleton_smul_eq {K E : Type*} [division_ring K] [add_comm_group E] [module K E] {r : K} (x : E) (hr : r ≠ 0) : (K ∙ (r • x)) = K ∙ x := begin refine le_antisymm (span_singleton_smul_le r x) _, convert span_singleton_smul_le r⁻¹ (r • x), exact (inv_smul_smul₀ hr _).symm end lemma disjoint_span_singleton {K E : Type*} [division_ring K] [add_comm_group E] [module K E] {s : submodule K E} {x : E} : disjoint s (K ∙ x) ↔ (x ∈ s → x = 0) := begin refine disjoint_def.trans ⟨λ H hx, H x hx $ subset_span $ mem_singleton x, _⟩, assume H y hy hyx, obtain ⟨c, hc⟩ := mem_span_singleton.1 hyx, subst y, classical, by_cases hc : c = 0, by simp only [hc, zero_smul], rw [s.smul_mem_iff hc] at hy, rw [H hy, smul_zero] end lemma disjoint_span_singleton' {K E : Type*} [division_ring K] [add_comm_group E] [module K E] {p : submodule K E} {x : E} (x0 : x ≠ 0) : disjoint p (K ∙ x) ↔ x ∉ p := disjoint_span_singleton.trans ⟨λ h₁ h₂, x0 (h₁ h₂), λ h₁ h₂, (h₁ h₂).elim⟩ lemma mem_span_insert {y} : x ∈ span R (insert y s) ↔ ∃ (a:R) (z ∈ span R s), x = a • y + z := begin simp only [← union_singleton, span_union, mem_sup, mem_span_singleton, exists_prop, exists_exists_eq_and], rw [exists_comm], simp only [eq_comm, add_comm, exists_and_distrib_left] end lemma span_insert (x) (s : set M) : span R (insert x s) = span R ({x} : set M) ⊔ span R s := by rw [insert_eq, span_union] lemma span_insert_eq_span (h : x ∈ span R s) : span R (insert x s) = span R s := span_eq_of_le _ (set.insert_subset.mpr ⟨h, subset_span⟩) (span_mono $ subset_insert _ _) lemma span_span : span R (span R s : set M) = span R s := span_eq _ lemma span_eq_bot : span R (s : set M) = ⊥ ↔ ∀ x ∈ s, (x:M) = 0 := eq_bot_iff.trans ⟨ λ H x h, (mem_bot R).1 $ H $ subset_span h, λ H, span_le.2 (λ x h, (mem_bot R).2 $ H x h)⟩ @[simp] lemma span_singleton_eq_bot : (R ∙ x) = ⊥ ↔ x = 0 := span_eq_bot.trans $ by simp @[simp] lemma span_zero : span R (0 : set M) = ⊥ := by rw [←singleton_zero, span_singleton_eq_bot] @[simp] lemma span_image [ring_hom_surjective σ₁₂] (f : M →ₛₗ[σ₁₂] M₂) : span R₂ (f '' s) = map f (span R s) := span_eq_of_le _ (image_subset _ subset_span) $ map_le_iff_le_comap.2 $ span_le.2 $ image_subset_iff.1 subset_span lemma apply_mem_span_image_of_mem_span [ring_hom_surjective σ₁₂] (f : M →ₛₗ[σ₁₂] M₂) {x : M} {s : set M} (h : x ∈ submodule.span R s) : f x ∈ submodule.span R₂ (f '' s) := begin rw submodule.span_image, exact submodule.mem_map_of_mem h end /-- `f` is an explicit argument so we can `apply` this theorem and obtain `h` as a new goal. -/ lemma not_mem_span_of_apply_not_mem_span_image [ring_hom_surjective σ₁₂] (f : M →ₛₗ[σ₁₂] M₂) {x : M} {s : set M} (h : f x ∉ submodule.span R₂ (f '' s)) : x ∉ submodule.span R s := not.imp h (apply_mem_span_image_of_mem_span f) lemma supr_eq_span {ι : Sort*} (p : ι → submodule R M) : (⨆ (i : ι), p i) = submodule.span R (⋃ (i : ι), ↑(p i)) := le_antisymm (supr_le $ assume i, subset.trans (assume m hm, set.mem_Union.mpr ⟨i, hm⟩) subset_span) (span_le.mpr $ Union_subset_iff.mpr $ assume i m hm, mem_supr_of_mem i hm) lemma span_singleton_le_iff_mem (m : M) (p : submodule R M) : (R ∙ m) ≤ p ↔ m ∈ p := by rw [span_le, singleton_subset_iff, set_like.mem_coe] lemma singleton_span_is_compact_element (x : M) : complete_lattice.is_compact_element (span R {x} : submodule R M) := begin rw complete_lattice.is_compact_element_iff_le_of_directed_Sup_le, intros d hemp hdir hsup, have : x ∈ Sup d, from (set_like.le_def.mp hsup) (mem_span_singleton_self x), obtain ⟨y, ⟨hyd, hxy⟩⟩ := (mem_Sup_of_directed hemp hdir).mp this, exact ⟨y, ⟨hyd, by simpa only [span_le, singleton_subset_iff]⟩⟩, end instance : is_compactly_generated (submodule R M) := ⟨λ s, ⟨(λ x, span R {x}) '' s, ⟨λ t ht, begin rcases (set.mem_image _ _ _).1 ht with ⟨x, hx, rfl⟩, apply singleton_span_is_compact_element, end, by rw [Sup_eq_supr, supr_image, ←span_eq_supr_of_singleton_spans, span_eq]⟩⟩⟩ lemma lt_sup_iff_not_mem {I : submodule R M} {a : M} : I < I ⊔ (R ∙ a) ↔ a ∉ I := begin split, { intro h, by_contra akey, have h1 : I ⊔ (R ∙ a) ≤ I, { simp only [sup_le_iff], split, { exact le_refl I, }, { exact (span_singleton_le_iff_mem a I).mpr akey, } }, have h2 := gt_of_ge_of_gt h1 h, exact lt_irrefl I h2, }, { intro h, apply set_like.lt_iff_le_and_exists.mpr, split, simp only [le_sup_left], use a, split, swap, { assumption, }, { have : (R ∙ a) ≤ I ⊔ (R ∙ a) := le_sup_right, exact this (mem_span_singleton_self a), } }, end lemma mem_supr {ι : Sort*} (p : ι → submodule R M) {m : M} : (m ∈ ⨆ i, p i) ↔ (∀ N, (∀ i, p i ≤ N) → m ∈ N) := begin rw [← span_singleton_le_iff_mem, le_supr_iff], simp only [span_singleton_le_iff_mem], end section open_locale classical /-- For every element in the span of a set, there exists a finite subset of the set such that the element is contained in the span of the subset. -/ lemma mem_span_finite_of_mem_span {S : set M} {x : M} (hx : x ∈ span R S) : ∃ T : finset M, ↑T ⊆ S ∧ x ∈ span R (T : set M) := begin refine span_induction hx (λ x hx, _) _ _ _, { refine ⟨{x}, _, _⟩, { rwa [finset.coe_singleton, set.singleton_subset_iff] }, { rw finset.coe_singleton, exact submodule.mem_span_singleton_self x } }, { use ∅, simp }, { rintros x y ⟨X, hX, hxX⟩ ⟨Y, hY, hyY⟩, refine ⟨X ∪ Y, _, _⟩, { rw finset.coe_union, exact set.union_subset hX hY }, rw [finset.coe_union, span_union, mem_sup], exact ⟨x, hxX, y, hyY, rfl⟩, }, { rintros a x ⟨T, hT, h2⟩, exact ⟨T, hT, smul_mem _ _ h2⟩ } end end /-- The product of two submodules is a submodule. -/ def prod : submodule R (M × M') := { carrier := set.prod p q₁, smul_mem' := by rintro a ⟨x, y⟩ ⟨hx, hy⟩; exact ⟨smul_mem _ a hx, smul_mem _ a hy⟩, .. p.to_add_submonoid.prod q₁.to_add_submonoid } @[simp] lemma prod_coe : (prod p q₁ : set (M × M')) = set.prod p q₁ := rfl @[simp] lemma mem_prod {p : submodule R M} {q : submodule R M'} {x : M × M'} : x ∈ prod p q ↔ x.1 ∈ p ∧ x.2 ∈ q := set.mem_prod lemma span_prod_le (s : set M) (t : set M') : span R (set.prod s t) ≤ prod (span R s) (span R t) := span_le.2 $ set.prod_mono subset_span subset_span @[simp] lemma prod_top : (prod ⊤ ⊤ : submodule R (M × M')) = ⊤ := by ext; simp @[simp] lemma prod_bot : (prod ⊥ ⊥ : submodule R (M × M')) = ⊥ := by ext ⟨x, y⟩; simp [prod.zero_eq_mk] lemma prod_mono {p p' : submodule R M} {q q' : submodule R M'} : p ≤ p' → q ≤ q' → prod p q ≤ prod p' q' := prod_mono @[simp] lemma prod_inf_prod : prod p q₁ ⊓ prod p' q₁' = prod (p ⊓ p') (q₁ ⊓ q₁') := set_like.coe_injective set.prod_inter_prod @[simp] lemma prod_sup_prod : prod p q₁ ⊔ prod p' q₁' = prod (p ⊔ p') (q₁ ⊔ q₁') := begin refine le_antisymm (sup_le (prod_mono le_sup_left le_sup_left) (prod_mono le_sup_right le_sup_right)) _, simp [set_like.le_def], intros xx yy hxx hyy, rcases mem_sup.1 hxx with ⟨x, hx, x', hx', rfl⟩, rcases mem_sup.1 hyy with ⟨y, hy, y', hy', rfl⟩, refine mem_sup.2 ⟨(x, y), ⟨hx, hy⟩, (x', y'), ⟨hx', hy'⟩, rfl⟩ end end add_comm_monoid variables [ring R] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [module R M] [module R M₂] [module R M₃] variables (p p' : submodule R M) (q q' : submodule R M₂) variables {r : R} {x y : M} open set @[simp] lemma neg_coe : -(p : set M) = p := set.ext $ λ x, p.neg_mem_iff @[simp] protected lemma map_neg (f : M →ₗ[R] M₂) : map (-f) p = map f p := ext $ λ y, ⟨λ ⟨x, hx, hy⟩, hy ▸ ⟨-x, neg_mem _ hx, f.map_neg x⟩, λ ⟨x, hx, hy⟩, hy ▸ ⟨-x, neg_mem _ hx, ((-f).map_neg _).trans (neg_neg (f x))⟩⟩ @[simp] lemma span_neg (s : set M) : span R (-s) = span R s := calc span R (-s) = span R ((-linear_map.id : M →ₗ[R] M) '' s) : by simp ... = map (-linear_map.id) (span R s) : ((-linear_map.id).map_span _).symm ... = span R s : by simp lemma mem_span_insert' {y} {s : set M} : x ∈ span R (insert y s) ↔ ∃(a:R), x + a • y ∈ span R s := begin rw mem_span_insert, split, { rintro ⟨a, z, hz, rfl⟩, exact ⟨-a, by simp [hz, add_assoc]⟩ }, { rintro ⟨a, h⟩, exact ⟨-a, _, h, by simp [add_comm, add_left_comm]⟩ } end end submodule namespace submodule variables [field K] variables [add_comm_group V] [module K V] variables [add_comm_group V₂] [module K V₂] lemma comap_smul (f : V →ₗ[K] V₂) (p : submodule K V₂) (a : K) (h : a ≠ 0) : p.comap (a • f) = p.comap f := by ext b; simp only [submodule.mem_comap, p.smul_mem_iff h, linear_map.smul_apply] lemma map_smul (f : V →ₗ[K] V₂) (p : submodule K V) (a : K) (h : a ≠ 0) : p.map (a • f) = p.map f := le_antisymm begin rw [map_le_iff_le_comap, comap_smul f _ a h, ← map_le_iff_le_comap], exact le_refl _ end begin rw [map_le_iff_le_comap, ← comap_smul f _ a h, ← map_le_iff_le_comap], exact le_refl _ end lemma comap_smul' (f : V →ₗ[K] V₂) (p : submodule K V₂) (a : K) : p.comap (a • f) = (⨅ h : a ≠ 0, p.comap f) := by classical; by_cases a = 0; simp [h, comap_smul] lemma map_smul' (f : V →ₗ[K] V₂) (p : submodule K V) (a : K) : p.map (a • f) = (⨆ h : a ≠ 0, p.map f) := by classical; by_cases a = 0; simp [h, map_smul] end submodule /-! ### Properties of linear maps -/ namespace linear_map section add_comm_monoid variables [semiring R] [semiring R₂] [semiring R₃] variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] variables {σ₁₂ : R →+* R₂} {σ₂₃ : R₂ →+* R₃} {σ₁₃ : R →+* R₃} variables [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] variables [module R M] [module R₂ M₂] [module R₃ M₃] include R open submodule /-- If two linear maps are equal on a set `s`, then they are equal on `submodule.span s`. See also `linear_map.eq_on_span'` for a version using `set.eq_on`. -/ lemma eq_on_span {s : set M} {f g : M →ₛₗ[σ₁₂] M₂} (H : set.eq_on f g s) ⦃x⦄ (h : x ∈ span R s) : f x = g x := by apply span_induction h H; simp {contextual := tt} /-- If two linear maps are equal on a set `s`, then they are equal on `submodule.span s`. This version uses `set.eq_on`, and the hidden argument will expand to `h : x ∈ (span R s : set M)`. See `linear_map.eq_on_span` for a version that takes `h : x ∈ span R s` as an argument. -/ lemma eq_on_span' {s : set M} {f g : M →ₛₗ[σ₁₂] M₂} (H : set.eq_on f g s) : set.eq_on f g (span R s : set M) := eq_on_span H /-- If `s` generates the whole module and linear maps `f`, `g` are equal on `s`, then they are equal. -/ lemma ext_on {s : set M} {f g : M →ₛₗ[σ₁₂] M₂} (hv : span R s = ⊤) (h : set.eq_on f g s) : f = g := linear_map.ext (λ x, eq_on_span h (eq_top_iff'.1 hv _)) /-- If the range of `v : ι → M` generates the whole module and linear maps `f`, `g` are equal at each `v i`, then they are equal. -/ lemma ext_on_range {v : ι → M} {f g : M →ₛₗ[σ₁₂] M₂} (hv : span R (set.range v) = ⊤) (h : ∀i, f (v i) = g (v i)) : f = g := ext_on hv (set.forall_range_iff.2 h) section finsupp variables {γ : Type*} [has_zero γ] @[simp] lemma map_finsupp_sum (f : M →ₛₗ[σ₁₂] M₂) {t : ι →₀ γ} {g : ι → γ → M} : f (t.sum g) = t.sum (λ i d, f (g i d)) := f.map_sum lemma coe_finsupp_sum (t : ι →₀ γ) (g : ι → γ → M →ₛₗ[σ₁₂] M₂) : ⇑(t.sum g) = t.sum (λ i d, g i d) := coe_fn_sum _ _ @[simp] lemma finsupp_sum_apply (t : ι →₀ γ) (g : ι → γ → M →ₛₗ[σ₁₂] M₂) (b : M) : (t.sum g) b = t.sum (λ i d, g i d b) := sum_apply _ _ _ end finsupp section dfinsupp open dfinsupp variables {γ : ι → Type*} [decidable_eq ι] section sum variables [Π i, has_zero (γ i)] [Π i (x : γ i), decidable (x ≠ 0)] @[simp] lemma map_dfinsupp_sum (f : M →ₛₗ[σ₁₂] M₂) {t : Π₀ i, γ i} {g : Π i, γ i → M} : f (t.sum g) = t.sum (λ i d, f (g i d)) := f.map_sum lemma coe_dfinsupp_sum (t : Π₀ i, γ i) (g : Π i, γ i → M →ₛₗ[σ₁₂] M₂) : ⇑(t.sum g) = t.sum (λ i d, g i d) := coe_fn_sum _ _ @[simp] lemma dfinsupp_sum_apply (t : Π₀ i, γ i) (g : Π i, γ i → M →ₛₗ[σ₁₂] M₂) (b : M) : (t.sum g) b = t.sum (λ i d, g i d b) := sum_apply _ _ _ end sum section sum_add_hom variables [Π i, add_zero_class (γ i)] @[simp] lemma map_dfinsupp_sum_add_hom (f : M →ₛₗ[σ₁₂] M₂) {t : Π₀ i, γ i} {g : Π i, γ i →+ M} : f (sum_add_hom g t) = sum_add_hom (λ i, f.to_add_monoid_hom.comp (g i)) t := f.to_add_monoid_hom.map_dfinsupp_sum_add_hom _ _ end sum_add_hom end dfinsupp variables {σ₂₁ : R₂ →+* R} {τ₁₂ : R →+* R₂} {τ₂₃ : R₂ →+* R₃} {τ₁₃ : R →+* R₃} variables [ring_hom_comp_triple τ₁₂ τ₂₃ τ₁₃] theorem map_cod_restrict [ring_hom_surjective σ₂₁] (p : submodule R M) (f : M₂ →ₛₗ[σ₂₁] M) (h p') : submodule.map (cod_restrict p f h) p' = comap p.subtype (p'.map f) := submodule.ext $ λ ⟨x, hx⟩, by simp [subtype.ext_iff_val] theorem comap_cod_restrict (p : submodule R M) (f : M₂ →ₛₗ[σ₂₁] M) (hf p') : submodule.comap (cod_restrict p f hf) p' = submodule.comap f (map p.subtype p') := submodule.ext $ λ x, ⟨λ h, ⟨⟨_, hf x⟩, h, rfl⟩, by rintro ⟨⟨_, _⟩, h, ⟨⟩⟩; exact h⟩ section /-- The range of a linear map `f : M → M₂` is a submodule of `M₂`. See Note [range copy pattern]. -/ def range [ring_hom_surjective τ₁₂] (f : M →ₛₗ[τ₁₂] M₂) : submodule R₂ M₂ := (map f ⊤).copy (set.range f) set.image_univ.symm theorem range_coe [ring_hom_surjective τ₁₂] (f : M →ₛₗ[τ₁₂] M₂) : (range f : set M₂) = set.range f := rfl @[simp] theorem mem_range [ring_hom_surjective τ₁₂] {f : M →ₛₗ[τ₁₂] M₂} {x} : x ∈ range f ↔ ∃ y, f y = x := iff.rfl lemma range_eq_map [ring_hom_surjective τ₁₂] (f : M →ₛₗ[τ₁₂] M₂) : f.range = map f ⊤ := by { ext, simp } theorem mem_range_self [ring_hom_surjective τ₁₂] (f : M →ₛₗ[τ₁₂] M₂) (x : M) : f x ∈ f.range := ⟨x, rfl⟩ @[simp] theorem range_id : range (linear_map.id : M →ₗ[R] M) = ⊤ := set_like.coe_injective set.range_id theorem range_comp [ring_hom_surjective τ₁₂] [ring_hom_surjective τ₂₃] [ring_hom_surjective τ₁₃] (f : M →ₛₗ[τ₁₂] M₂) (g : M₂ →ₛₗ[τ₂₃] M₃) : range (g.comp f : M →ₛₗ[τ₁₃] M₃) = map g (range f) := set_like.coe_injective (set.range_comp g f) theorem range_comp_le_range [ring_hom_surjective τ₂₃] [ring_hom_surjective τ₁₃] (f : M →ₛₗ[τ₁₂] M₂) (g : M₂ →ₛₗ[τ₂₃] M₃) : range (g.comp f : M →ₛₗ[τ₁₃] M₃) ≤ range g := set_like.coe_mono (set.range_comp_subset_range f g) theorem range_eq_top [ring_hom_surjective τ₁₂] {f : M →ₛₗ[τ₁₂] M₂} : range f = ⊤ ↔ surjective f := by rw [set_like.ext'_iff, range_coe, top_coe, set.range_iff_surjective] lemma range_le_iff_comap [ring_hom_surjective τ₁₂] {f : M →ₛₗ[τ₁₂] M₂} {p : submodule R₂ M₂} : range f ≤ p ↔ comap f p = ⊤ := by rw [range_eq_map, map_le_iff_le_comap, eq_top_iff] lemma map_le_range [ring_hom_surjective τ₁₂] {f : M →ₛₗ[τ₁₂] M₂} {p : submodule R M} : map f p ≤ range f := set_like.coe_mono (set.image_subset_range f p) end /-- The decreasing sequence of submodules consisting of the ranges of the iterates of a linear map. -/ @[simps] def iterate_range {R M} [ring R] [add_comm_group M] [module R M] (f : M →ₗ[R] M) : ℕ →ₘ order_dual (submodule R M) := ⟨λ n, (f ^ n).range, λ n m w x h, begin obtain ⟨c, rfl⟩ := le_iff_exists_add.mp w, rw linear_map.mem_range at h, obtain ⟨m, rfl⟩ := h, rw linear_map.mem_range, use (f ^ c) m, rw [pow_add, linear_map.mul_apply], end⟩ /-- Restrict the codomain of a linear map `f` to `f.range`. This is the bundled version of `set.range_factorization`. -/ @[reducible] def range_restrict [ring_hom_surjective τ₁₂] (f : M →ₛₗ[τ₁₂] M₂) : M →ₛₗ[τ₁₂] f.range := f.cod_restrict f.range f.mem_range_self --set_option trace.class_instances true /-- The range of a linear map is finite if the domain is finite. Note: this instance can form a diamond with `subtype.fintype` in the presence of `fintype M₂`. -/ instance fintype_range [fintype M] [decidable_eq M₂] [ring_hom_surjective τ₁₂] (f : M →ₛₗ[τ₁₂] M₂) : fintype (range f) := set.fintype_range f section variables (R) (M) /-- Given an element `x` of a module `M` over `R`, the natural map from `R` to scalar multiples of `x`.-/ @[simps] def to_span_singleton (x : M) : R →ₗ[R] M := linear_map.id.smul_right x /-- The range of `to_span_singleton x` is the span of `x`.-/ lemma span_singleton_eq_range (x : M) : (R ∙ x) = (to_span_singleton R M x).range := submodule.ext $ λ y, by {refine iff.trans _ mem_range.symm, exact mem_span_singleton } lemma to_span_singleton_one (x : M) : to_span_singleton R M x 1 = x := one_smul _ _ end /-- The kernel of a linear map `f : M → M₂` is defined to be `comap f ⊥`. This is equivalent to the set of `x : M` such that `f x = 0`. The kernel is a submodule of `M`. -/ def ker (f : M →ₛₗ[τ₁₂] M₂) : submodule R M := comap f ⊥ @[simp] theorem mem_ker {f : M →ₛₗ[τ₁₂] M₂} {y} : y ∈ ker f ↔ f y = 0 := mem_bot R₂ @[simp] theorem ker_id : ker (linear_map.id : M →ₗ[R] M) = ⊥ := rfl @[simp] theorem map_coe_ker (f : M →ₛₗ[τ₁₂] M₂) (x : ker f) : f x = 0 := mem_ker.1 x.2 lemma comp_ker_subtype (f : M →ₛₗ[τ₁₂] M₂) : f.comp f.ker.subtype = 0 := linear_map.ext $ λ x, suffices f x = 0, by simp [this], mem_ker.1 x.2 theorem ker_comp (f : M →ₛₗ[τ₁₂] M₂) (g : M₂ →ₛₗ[τ₂₃] M₃) : ker (g.comp f : M →ₛₗ[τ₁₃] M₃) = comap f (ker g) := rfl theorem ker_le_ker_comp (f : M →ₛₗ[τ₁₂] M₂) (g : M₂ →ₛₗ[τ₂₃] M₃) : ker f ≤ ker (g.comp f : M →ₛₗ[τ₁₃] M₃) := by rw ker_comp; exact comap_mono bot_le theorem disjoint_ker {f : M →ₛₗ[τ₁₂] M₂} {p : submodule R M} : disjoint p (ker f) ↔ ∀ x ∈ p, f x = 0 → x = 0 := by simp [disjoint_def] theorem ker_eq_bot' {f : M →ₛₗ[τ₁₂] M₂} : ker f = ⊥ ↔ (∀ m, f m = 0 → m = 0) := by simpa [disjoint] using @disjoint_ker _ _ _ _ _ _ _ _ _ _ _ f ⊤ theorem ker_eq_bot_of_inverse {τ₂₁ : R₂ →+* R} [ring_hom_inv_pair τ₁₂ τ₂₁] {f : M →ₛₗ[τ₁₂] M₂} {g : M₂ →ₛₗ[τ₂₁] M} (h : (g.comp f : M →ₗ[R] M) = id) : ker f = ⊥ := ker_eq_bot'.2 $ λ m hm, by rw [← id_apply m, ← h, comp_apply, hm, g.map_zero] lemma le_ker_iff_map [ring_hom_surjective τ₁₂] {f : M →ₛₗ[τ₁₂] M₂} {p : submodule R M} : p ≤ ker f ↔ map f p = ⊥ := by rw [ker, eq_bot_iff, map_le_iff_le_comap] lemma ker_cod_restrict {τ₂₁ : R₂ →+* R} (p : submodule R M) (f : M₂ →ₛₗ[τ₂₁] M) (hf) : ker (cod_restrict p f hf) = ker f := by rw [ker, comap_cod_restrict, map_bot]; refl lemma range_cod_restrict {τ₂₁ : R₂ →+* R} [ring_hom_surjective τ₂₁] (p : submodule R M) (f : M₂ →ₛₗ[τ₂₁] M) (hf) : range (cod_restrict p f hf) = comap p.subtype f.range := by simpa only [range_eq_map] using map_cod_restrict _ _ _ _ lemma ker_restrict {p : submodule R M} {f : M →ₗ[R] M} (hf : ∀ x : M, x ∈ p → f x ∈ p) : ker (f.restrict hf) = (f.dom_restrict p).ker := by rw [restrict_eq_cod_restrict_dom_restrict, ker_cod_restrict] lemma _root_.submodule.map_comap_eq [ring_hom_surjective τ₁₂] (f : M →ₛₗ[τ₁₂] M₂) (q : submodule R₂ M₂) : map f (comap f q) = range f ⊓ q := le_antisymm (le_inf map_le_range (map_comap_le _ _)) $ by rintro _ ⟨⟨x, _, rfl⟩, hx⟩; exact ⟨x, hx, rfl⟩ lemma _root_.submodule.map_comap_eq_self [ring_hom_surjective τ₁₂] {f : M →ₛₗ[τ₁₂] M₂} {q : submodule R₂ M₂} (h : q ≤ range f) : map f (comap f q) = q := by rwa [submodule.map_comap_eq, inf_eq_right] @[simp] theorem ker_zero : ker (0 : M →ₛₗ[τ₁₂] M₂) = ⊤ := eq_top_iff'.2 $ λ x, by simp @[simp] theorem range_zero [ring_hom_surjective τ₁₂] : range (0 : M →ₛₗ[τ₁₂] M₂) = ⊥ := by simpa only [range_eq_map] using submodule.map_zero _ theorem ker_eq_top {f : M →ₛₗ[τ₁₂] M₂} : ker f = ⊤ ↔ f = 0 := ⟨λ h, ext $ λ x, mem_ker.1 $ h.symm ▸ trivial, λ h, h.symm ▸ ker_zero⟩ section variables [ring_hom_surjective τ₁₂] lemma range_le_bot_iff (f : M →ₛₗ[τ₁₂] M₂) : range f ≤ ⊥ ↔ f = 0 := by rw [range_le_iff_comap]; exact ker_eq_top theorem range_eq_bot {f : M →ₛₗ[τ₁₂] M₂} : range f = ⊥ ↔ f = 0 := by rw [← range_le_bot_iff, le_bot_iff] lemma range_le_ker_iff {f : M →ₛₗ[τ₁₂] M₂} {g : M₂ →ₛₗ[τ₂₃] M₃} : range f ≤ ker g ↔ (g.comp f : M →ₛₗ[τ₁₃] M₃) = 0 := ⟨λ h, ker_eq_top.1 $ eq_top_iff'.2 $ λ x, h $ ⟨_, rfl⟩, λ h x hx, mem_ker.2 $ exists.elim hx $ λ y hy, by rw [←hy, ←comp_apply, h, zero_apply]⟩ theorem comap_le_comap_iff {f : M →ₛₗ[τ₁₂] M₂} (hf : range f = ⊤) {p p'} : comap f p ≤ comap f p' ↔ p ≤ p' := ⟨λ H x hx, by rcases range_eq_top.1 hf x with ⟨y, hy, rfl⟩; exact H hx, comap_mono⟩ theorem comap_injective {f : M →ₛₗ[τ₁₂] M₂} (hf : range f = ⊤) : injective (comap f) := λ p p' h, le_antisymm ((comap_le_comap_iff hf).1 (le_of_eq h)) ((comap_le_comap_iff hf).1 (ge_of_eq h)) end theorem ker_eq_bot_of_injective {f : M →ₛₗ[τ₁₂] M₂} (hf : injective f) : ker f = ⊥ := begin have : disjoint ⊤ f.ker, by { rw [disjoint_ker, ← map_zero f], exact λ x hx H, hf H }, simpa [disjoint] end /-- The increasing sequence of submodules consisting of the kernels of the iterates of a linear map. -/ @[simps] def iterate_ker {R M} [ring R] [add_comm_group M] [module R M] (f : M →ₗ[R] M) : ℕ →ₘ submodule R M := ⟨λ n, (f ^ n).ker, λ n m w x h, begin obtain ⟨c, rfl⟩ := le_iff_exists_add.mp w, rw linear_map.mem_ker at h, rw [linear_map.mem_ker, add_comm, pow_add, linear_map.mul_apply, h, linear_map.map_zero], end⟩ end add_comm_monoid section add_comm_group variables [semiring R] [semiring R₂] [semiring R₃] variables [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [module R M] [module R₂ M₂] [module R₃ M₃] variables {τ₁₂ : R →+* R₂} {τ₂₃ : R₂ →+* R₃} {τ₁₃ : R →+* R₃} variables [ring_hom_comp_triple τ₁₂ τ₂₃ τ₁₃] [ring_hom_surjective τ₁₂] include R open submodule lemma _root_.submodule.comap_map_eq (f : M →ₛₗ[τ₁₂] M₂) (p : submodule R M) : comap f (map f p) = p ⊔ ker f := begin refine le_antisymm _ (sup_le (le_comap_map _ _) (comap_mono bot_le)), rintro x ⟨y, hy, e⟩, exact mem_sup.2 ⟨y, hy, x - y, by simpa using sub_eq_zero.2 e.symm, by simp⟩ end lemma _root_.submodule.comap_map_eq_self {f : M →ₛₗ[τ₁₂] M₂} {p : submodule R M} (h : ker f ≤ p) : comap f (map f p) = p := by rw [submodule.comap_map_eq, sup_of_le_left h] theorem map_le_map_iff (f : M →ₛₗ[τ₁₂] M₂) {p p'} : map f p ≤ map f p' ↔ p ≤ p' ⊔ ker f := by rw [map_le_iff_le_comap, submodule.comap_map_eq] theorem map_le_map_iff' {f : M →ₛₗ[τ₁₂] M₂} (hf : ker f = ⊥) {p p'} : map f p ≤ map f p' ↔ p ≤ p' := by rw [map_le_map_iff, hf, sup_bot_eq] theorem map_injective {f : M →ₛₗ[τ₁₂] M₂} (hf : ker f = ⊥) : injective (map f) := λ p p' h, le_antisymm ((map_le_map_iff' hf).1 (le_of_eq h)) ((map_le_map_iff' hf).1 (ge_of_eq h)) theorem map_eq_top_iff {f : M →ₛₗ[τ₁₂] M₂} (hf : range f = ⊤) {p : submodule R M} : p.map f = ⊤ ↔ p ⊔ f.ker = ⊤ := by simp_rw [← top_le_iff, ← hf, range_eq_map, map_le_map_iff] end add_comm_group section ring variables [ring R] [ring R₂] [ring R₃] variables [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [module R M] [module R₂ M₂] [module R₃ M₃] variables {τ₁₂ : R →+* R₂} {τ₂₃ : R₂ →+* R₃} {τ₁₃ : R →+* R₃} variables [ring_hom_comp_triple τ₁₂ τ₂₃ τ₁₃] variables {f : M →ₛₗ[τ₁₂] M₂} include R open submodule theorem sub_mem_ker_iff {x y} : x - y ∈ f.ker ↔ f x = f y := by rw [mem_ker, map_sub, sub_eq_zero] theorem disjoint_ker' {p : submodule R M} : disjoint p (ker f) ↔ ∀ x y ∈ p, f x = f y → x = y := disjoint_ker.trans ⟨λ H x y hx hy h, eq_of_sub_eq_zero $ H _ (sub_mem _ hx hy) (by simp [h]), λ H x h₁ h₂, H x 0 h₁ (zero_mem _) (by simpa using h₂)⟩ theorem inj_of_disjoint_ker {p : submodule R M} {s : set M} (h : s ⊆ p) (hd : disjoint p (ker f)) : ∀ x y ∈ s, f x = f y → x = y := λ x y hx hy, disjoint_ker'.1 hd _ _ (h hx) (h hy) theorem ker_eq_bot : ker f = ⊥ ↔ injective f := by simpa [disjoint] using @disjoint_ker' _ _ _ _ _ _ _ _ _ _ _ f ⊤ lemma ker_le_iff [ring_hom_surjective τ₁₂] {p : submodule R M} : ker f ≤ p ↔ ∃ (y ∈ range f), f ⁻¹' {y} ⊆ p := begin split, { intros h, use 0, rw [← set_like.mem_coe, f.range_coe], exact ⟨⟨0, map_zero f⟩, h⟩, }, { rintros ⟨y, h₁, h₂⟩, rw set_like.le_def, intros z hz, simp only [mem_ker, set_like.mem_coe] at hz, rw [← set_like.mem_coe, f.range_coe, set.mem_range] at h₁, obtain ⟨x, hx⟩ := h₁, have hx' : x ∈ p, { exact h₂ hx, }, have hxz : z + x ∈ p, { apply h₂, simp [hx, hz], }, suffices : z + x - x ∈ p, { simpa only [this, add_sub_cancel], }, exact p.sub_mem hxz hx', }, end end ring section field variables [field K] [field K₂] variables [add_comm_group V] [module K V] variables [add_comm_group V₂] [module K V₂] lemma ker_smul (f : V →ₗ[K] V₂) (a : K) (h : a ≠ 0) : ker (a • f) = ker f := submodule.comap_smul f _ a h lemma ker_smul' (f : V →ₗ[K] V₂) (a : K) : ker (a • f) = ⨅(h : a ≠ 0), ker f := submodule.comap_smul' f _ a lemma range_smul (f : V →ₗ[K] V₂) (a : K) (h : a ≠ 0) : range (a • f) = range f := by simpa only [range_eq_map] using submodule.map_smul f _ a h lemma range_smul' (f : V →ₗ[K] V₂) (a : K) : range (a • f) = ⨆(h : a ≠ 0), range f := by simpa only [range_eq_map] using submodule.map_smul' f _ a lemma span_singleton_sup_ker_eq_top (f : V →ₗ[K] K) {x : V} (hx : f x ≠ 0) : (K ∙ x) ⊔ f.ker = ⊤ := eq_top_iff.2 (λ y hy, submodule.mem_sup.2 ⟨(f y * (f x)⁻¹) • x, submodule.mem_span_singleton.2 ⟨f y * (f x)⁻¹, rfl⟩, ⟨y - (f y * (f x)⁻¹) • x, by rw [linear_map.mem_ker, f.map_sub, f.map_smul, smul_eq_mul, mul_assoc, inv_mul_cancel hx, mul_one, sub_self], by simp only [add_sub_cancel'_right]⟩⟩) end field end linear_map namespace is_linear_map lemma is_linear_map_add [semiring R] [add_comm_monoid M] [module R M] : is_linear_map R (λ (x : M × M), x.1 + x.2) := begin apply is_linear_map.mk, { intros x y, simp, cc }, { intros x y, simp [smul_add] } end lemma is_linear_map_sub {R M : Type*} [semiring R] [add_comm_group M] [module R M]: is_linear_map R (λ (x : M × M), x.1 - x.2) := begin apply is_linear_map.mk, { intros x y, simp [add_comm, add_left_comm, sub_eq_add_neg] }, { intros x y, simp [smul_sub] } end end is_linear_map namespace submodule section add_comm_monoid variables [semiring R] [semiring R₂] [add_comm_monoid M] [add_comm_monoid M₂] variables [module R M] [module R₂ M₂] variables (p p' : submodule R M) (q : submodule R₂ M₂) variables {τ₁₂ : R →+* R₂} open linear_map @[simp] theorem map_top [ring_hom_surjective τ₁₂] (f : M →ₛₗ[τ₁₂] M₂) : map f ⊤ = range f := f.range_eq_map.symm @[simp] theorem comap_bot (f : M →ₛₗ[τ₁₂] M₂) : comap f ⊥ = ker f := rfl @[simp] theorem ker_subtype : p.subtype.ker = ⊥ := ker_eq_bot_of_injective $ λ x y, subtype.ext_val @[simp] theorem range_subtype : p.subtype.range = p := by simpa using map_comap_subtype p ⊤ lemma map_subtype_le (p' : submodule R p) : map p.subtype p' ≤ p := by simpa using (map_le_range : map p.subtype p' ≤ p.subtype.range) /-- Under the canonical linear map from a submodule `p` to the ambient space `M`, the image of the maximal submodule of `p` is just `p `. -/ @[simp] lemma map_subtype_top : map p.subtype (⊤ : submodule R p) = p := by simp @[simp] lemma comap_subtype_eq_top {p p' : submodule R M} : comap p.subtype p' = ⊤ ↔ p ≤ p' := eq_top_iff.trans $ map_le_iff_le_comap.symm.trans $ by rw [map_subtype_top] @[simp] lemma comap_subtype_self : comap p.subtype p = ⊤ := comap_subtype_eq_top.2 (le_refl _) @[simp] theorem ker_of_le (p p' : submodule R M) (h : p ≤ p') : (of_le h).ker = ⊥ := by rw [of_le, ker_cod_restrict, ker_subtype] lemma range_of_le (p q : submodule R M) (h : p ≤ q) : (of_le h).range = comap q.subtype p := by rw [← map_top, of_le, linear_map.map_cod_restrict, map_top, range_subtype] end add_comm_monoid section ring variables [ring R] [ring R₂] [add_comm_group M] [add_comm_group M₂] [module R M] [module R₂ M₂] variables (p p' : submodule R M) (q : submodule R₂ M₂) variables {τ₁₂ : R →+* R₂} open linear_map lemma disjoint_iff_comap_eq_bot {p q : submodule R M} : disjoint p q ↔ comap p.subtype q = ⊥ := by rw [eq_bot_iff, ← map_le_map_iff' p.ker_subtype, map_bot, map_comap_subtype, disjoint] /-- If `N ⊆ M` then submodules of `N` are the same as submodules of `M` contained in `N` -/ def map_subtype.rel_iso : submodule R p ≃o {p' : submodule R M // p' ≤ p} := { to_fun := λ p', ⟨map p.subtype p', map_subtype_le p _⟩, inv_fun := λ q, comap p.subtype q, left_inv := λ p', comap_map_eq_self $ by simp, right_inv := λ ⟨q, hq⟩, subtype.ext_val $ by simp [map_comap_subtype p, inf_of_le_right hq], map_rel_iff' := λ p₁ p₂, map_le_map_iff' (ker_subtype p) } /-- If `p ⊆ M` is a submodule, the ordering of submodules of `p` is embedded in the ordering of submodules of `M`. -/ def map_subtype.order_embedding : submodule R p ↪o submodule R M := (rel_iso.to_rel_embedding $ map_subtype.rel_iso p).trans (subtype.rel_embedding _ _) @[simp] lemma map_subtype_embedding_eq (p' : submodule R p) : map_subtype.order_embedding p p' = map p.subtype p' := rfl end ring end submodule namespace linear_map section semiring variables [semiring R] [semiring R₂] [semiring R₃] variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] variables [module R M] [module R₂ M₂] [module R₃ M₃] variables {τ₁₂ : R →+* R₂} {τ₂₃ : R₂ →+* R₃} {τ₁₃ : R →+* R₃} variables [ring_hom_comp_triple τ₁₂ τ₂₃ τ₁₃] /-- A monomorphism is injective. -/ lemma ker_eq_bot_of_cancel {f : M →ₛₗ[τ₁₂] M₂} (h : ∀ (u v : f.ker →ₗ[R] M), f.comp u = f.comp v → u = v) : f.ker = ⊥ := begin have h₁ : f.comp (0 : f.ker →ₗ[R] M) = 0 := comp_zero _, rw [←submodule.range_subtype f.ker, ←h 0 f.ker.subtype (eq.trans h₁ (comp_ker_subtype f).symm)], exact range_zero end lemma range_comp_of_range_eq_top [ring_hom_surjective τ₁₂] [ring_hom_surjective τ₂₃] [ring_hom_surjective τ₁₃] {f : M →ₛₗ[τ₁₂] M₂} (g : M₂ →ₛₗ[τ₂₃] M₃) (hf : range f = ⊤) : range (g.comp f : M →ₛₗ[τ₁₃] M₃) = range g := by rw [range_comp, hf, submodule.map_top] lemma ker_comp_of_ker_eq_bot (f : M →ₛₗ[τ₁₂] M₂) {g : M₂ →ₛₗ[τ₂₃] M₃} (hg : ker g = ⊥) : ker (g.comp f : M →ₛₗ[τ₁₃] M₃) = ker f := by rw [ker_comp, hg, submodule.comap_bot] section image /-- If `O` is a submodule of `M`, and `Φ : O →ₗ M'` is a linear map, then `(ϕ : O →ₗ M').submodule_image N` is `ϕ(N)` as a submodule of `M'` -/ def submodule_image {M' : Type*} [add_comm_monoid M'] [module R M'] {O : submodule R M} (ϕ : O →ₗ[R] M') (N : submodule R M) : submodule R M' := (N.comap O.subtype).map ϕ @[simp] lemma mem_submodule_image {M' : Type*} [add_comm_monoid M'] [module R M'] {O : submodule R M} {ϕ : O →ₗ[R] M'} {N : submodule R M} {x : M'} : x ∈ ϕ.submodule_image N ↔ ∃ y (yO : y ∈ O) (yN : y ∈ N), ϕ ⟨y, yO⟩ = x := begin refine submodule.mem_map.trans ⟨_, _⟩; simp_rw submodule.mem_comap, { rintro ⟨⟨y, yO⟩, (yN : y ∈ N), h⟩, exact ⟨y, yO, yN, h⟩ }, { rintro ⟨y, yO, yN, h⟩, exact ⟨⟨y, yO⟩, yN, h⟩ } end lemma mem_submodule_image_of_le {M' : Type*} [add_comm_monoid M'] [module R M'] {O : submodule R M} {ϕ : O →ₗ[R] M'} {N : submodule R M} (hNO : N ≤ O) {x : M'} : x ∈ ϕ.submodule_image N ↔ ∃ y (yN : y ∈ N), ϕ ⟨y, hNO yN⟩ = x := begin refine mem_submodule_image.trans ⟨_, _⟩, { rintro ⟨y, yO, yN, h⟩, exact ⟨y, yN, h⟩ }, { rintro ⟨y, yN, h⟩, exact ⟨y, hNO yN, yN, h⟩ } end lemma submodule_image_apply_of_le {M' : Type*} [add_comm_group M'] [module R M'] {O : submodule R M} (ϕ : O →ₗ[R] M') (N : submodule R M) (hNO : N ≤ O) : ϕ.submodule_image N = (ϕ.comp (submodule.of_le hNO)).range := by rw [submodule_image, range_comp, submodule.range_of_le] end image end semiring end linear_map @[simp] lemma linear_map.range_range_restrict [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [module R M] [module R M₂] (f : M →ₗ[R] M₂) : f.range_restrict.range = ⊤ := by simp [f.range_cod_restrict _] /-! ### Linear equivalences -/ namespace linear_equiv section add_comm_monoid section subsingleton variables [semiring R] [semiring R₂] [semiring R₃] [semiring R₄] variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M₄] variables [module R M] [module R₂ M₂] variables [subsingleton M] [subsingleton M₂] variables {σ₁₂ : R →+* R₂} {σ₂₁ : R₂ →+* R} variables [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂] include σ₂₁ /-- Between two zero modules, the zero map is an equivalence. -/ instance : has_zero (M ≃ₛₗ[σ₁₂] M₂) := ⟨{ to_fun := 0, inv_fun := 0, right_inv := λ x, subsingleton.elim _ _, left_inv := λ x, subsingleton.elim _ _, ..(0 : M →ₛₗ[σ₁₂] M₂)}⟩ omit σ₂₁ -- Even though these are implied by `subsingleton.elim` via the `unique` instance below, they're -- nice to have as `rfl`-lemmas for `dsimp`. include σ₂₁ @[simp] lemma zero_symm : (0 : M ≃ₛₗ[σ₁₂] M₂).symm = 0 := rfl @[simp] lemma coe_zero : ⇑(0 : M ≃ₛₗ[σ₁₂] M₂) = 0 := rfl lemma zero_apply (x : M) : (0 : M ≃ₛₗ[σ₁₂] M₂) x = 0 := rfl /-- Between two zero modules, the zero map is the only equivalence. -/ instance : unique (M ≃ₛₗ[σ₁₂] M₂) := { uniq := λ f, to_linear_map_injective (subsingleton.elim _ _), default := 0 } omit σ₂₁ end subsingleton section variables [semiring R] [semiring R₂] [semiring R₃] [semiring R₄] variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M₄] variables {module_M : module R M} {module_M₂ : module R₂ M₂} variables {σ₁₂ : R →+* R₂} {σ₂₁ : R₂ →+* R} variables {re₁₂ : ring_hom_inv_pair σ₁₂ σ₂₁} {re₂₁ : ring_hom_inv_pair σ₂₁ σ₁₂} variables (e e' : M ≃ₛₗ[σ₁₂] M₂) lemma map_eq_comap {p : submodule R M} : (p.map (e : M →ₛₗ[σ₁₂] M₂) : submodule R₂ M₂) = p.comap (e.symm : M₂ →ₛₗ[σ₂₁] M) := set_like.coe_injective $ by simp [e.image_eq_preimage] /-- A linear equivalence of two modules restricts to a linear equivalence from any submodule `p` of the domain onto the image of that submodule. This is `linear_equiv.of_submodule'` but with `map` on the right instead of `comap` on the left. -/ def of_submodule (p : submodule R M) : p ≃ₛₗ[σ₁₂] ↥(p.map (e : M →ₛₗ[σ₁₂] M₂) : submodule R₂ M₂) := { inv_fun := λ y, ⟨(e.symm : M₂ →ₛₗ[σ₂₁] M) y, by { rcases y with ⟨y', hy⟩, rw submodule.mem_map at hy, rcases hy with ⟨x, hx, hxy⟩, subst hxy, simp only [symm_apply_apply, submodule.coe_mk, coe_coe, hx], }⟩, left_inv := λ x, by simp, right_inv := λ y, by { apply set_coe.ext, simp, }, ..((e : M →ₛₗ[σ₁₂] M₂).dom_restrict p).cod_restrict (p.map (e : M →ₛₗ[σ₁₂] M₂)) (λ x, ⟨x, by simp⟩) } include σ₂₁ @[simp] lemma of_submodule_apply (p : submodule R M) (x : p) : ↑(e.of_submodule p x) = e x := rfl @[simp] lemma of_submodule_symm_apply (p : submodule R M) (x : (p.map (e : M →ₛₗ[σ₁₂] M₂) : submodule R₂ M₂)) : ↑((e.of_submodule p).symm x) = e.symm x := rfl omit σ₂₁ end section finsupp variables {γ : Type*} variables [semiring R] [semiring R₂] variables [add_comm_monoid M] [add_comm_monoid M₂] variables [module R M] [module R₂ M₂] [has_zero γ] variables {τ₁₂ : R →+* R₂} {τ₂₁ : R₂ →+* R} variables [ring_hom_inv_pair τ₁₂ τ₂₁] [ring_hom_inv_pair τ₂₁ τ₁₂] include τ₂₁ @[simp] lemma map_finsupp_sum (f : M ≃ₛₗ[τ₁₂] M₂) {t : ι →₀ γ} {g : ι → γ → M} : f (t.sum g) = t.sum (λ i d, f (g i d)) := f.map_sum _ omit τ₂₁ end finsupp section dfinsupp open dfinsupp variables [semiring R] [semiring R₂] variables [add_comm_monoid M] [add_comm_monoid M₂] variables [module R M] [module R₂ M₂] variables {τ₁₂ : R →+* R₂} {τ₂₁ : R₂ →+* R} variables [ring_hom_inv_pair τ₁₂ τ₂₁] [ring_hom_inv_pair τ₂₁ τ₁₂] variables {γ : ι → Type*} [decidable_eq ι] include τ₂₁ @[simp] lemma map_dfinsupp_sum [Π i, has_zero (γ i)] [Π i (x : γ i), decidable (x ≠ 0)] (f : M ≃ₛₗ[τ₁₂] M₂) (t : Π₀ i, γ i) (g : Π i, γ i → M) : f (t.sum g) = t.sum (λ i d, f (g i d)) := f.map_sum _ @[simp] lemma map_dfinsupp_sum_add_hom [Π i, add_zero_class (γ i)] (f : M ≃ₛₗ[τ₁₂] M₂) (t : Π₀ i, γ i) (g : Π i, γ i →+ M) : f (sum_add_hom g t) = sum_add_hom (λ i, f.to_add_equiv.to_add_monoid_hom.comp (g i)) t := f.to_add_equiv.map_dfinsupp_sum_add_hom _ _ end dfinsupp section uncurry variables [semiring R] [semiring R₂] [semiring R₃] [semiring R₄] variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M₄] variables (V V₂ R) /-- Linear equivalence between a curried and uncurried function. Differs from `tensor_product.curry`. -/ protected def curry : (V × V₂ → R) ≃ₗ[R] (V → V₂ → R) := { map_add' := λ _ _, by { ext, refl }, map_smul' := λ _ _, by { ext, refl }, .. equiv.curry _ _ _ } @[simp] lemma coe_curry : ⇑(linear_equiv.curry R V V₂) = curry := rfl @[simp] lemma coe_curry_symm : ⇑(linear_equiv.curry R V V₂).symm = uncurry := rfl end uncurry section variables [semiring R] [semiring R₂] [semiring R₃] [semiring R₄] variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M₄] variables {module_M : module R M} {module_M₂ : module R₂ M₂} {module_M₃ : module R₃ M₃} variables {σ₁₂ : R →+* R₂} {σ₂₁ : R₂ →+* R} variables {σ₂₃ : R₂ →+* R₃} {σ₁₃ : R →+* R₃} [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] variables {σ₃₂ : R₃ →+* R₂} variables {re₁₂ : ring_hom_inv_pair σ₁₂ σ₂₁} {re₂₁ : ring_hom_inv_pair σ₂₁ σ₁₂} variables {re₂₃ : ring_hom_inv_pair σ₂₃ σ₃₂} {re₃₂ : ring_hom_inv_pair σ₃₂ σ₂₃} variables (f : M →ₛₗ[σ₁₂] M₂) (g : M₂ →ₛₗ[σ₂₁] M) (e : M ≃ₛₗ[σ₁₂] M₂) (h : M₂ →ₛₗ[σ₂₃] M₃) variables (e'' : M₂ ≃ₛₗ[σ₂₃] M₃) variables (p q : submodule R M) /-- Linear equivalence between two equal submodules. -/ def of_eq (h : p = q) : p ≃ₗ[R] q := { map_smul' := λ _ _, rfl, map_add' := λ _ _, rfl, .. equiv.set.of_eq (congr_arg _ h) } variables {p q} @[simp] lemma coe_of_eq_apply (h : p = q) (x : p) : (of_eq p q h x : M) = x := rfl @[simp] lemma of_eq_symm (h : p = q) : (of_eq p q h).symm = of_eq q p h.symm := rfl include σ₂₁ /-- A linear equivalence which maps a submodule of one module onto another, restricts to a linear equivalence of the two submodules. -/ def of_submodules (p : submodule R M) (q : submodule R₂ M₂) (h : p.map (e : M →ₛₗ[σ₁₂] M₂) = q) : p ≃ₛₗ[σ₁₂] q := (e.of_submodule p).trans (linear_equiv.of_eq _ _ h) @[simp] lemma of_submodules_apply {p : submodule R M} {q : submodule R₂ M₂} (h : p.map ↑e = q) (x : p) : ↑(e.of_submodules p q h x) = e x := rfl @[simp] lemma of_submodules_symm_apply {p : submodule R M} {q : submodule R₂ M₂} (h : p.map ↑e = q) (x : q) : ↑((e.of_submodules p q h).symm x) = e.symm x := rfl include re₁₂ re₂₁ /-- A linear equivalence of two modules restricts to a linear equivalence from the preimage of any submodule to that submodule. This is `linear_equiv.of_submodule` but with `comap` on the left instead of `map` on the right. -/ def of_submodule' [module R M] [module R₂ M₂] (f : M ≃ₛₗ[σ₁₂] M₂) (U : submodule R₂ M₂) : U.comap (f : M →ₛₗ[σ₁₂] M₂) ≃ₛₗ[σ₁₂] U := (f.symm.of_submodules _ _ f.symm.map_eq_comap).symm lemma of_submodule'_to_linear_map [module R M] [module R₂ M₂] (f : M ≃ₛₗ[σ₁₂] M₂) (U : submodule R₂ M₂) : (f.of_submodule' U).to_linear_map = (f.to_linear_map.dom_restrict _).cod_restrict _ subtype.prop := by { ext, refl } @[simp] lemma of_submodule'_apply [module R M] [module R₂ M₂] (f : M ≃ₛₗ[σ₁₂] M₂) (U : submodule R₂ M₂) (x : U.comap (f : M →ₛₗ[σ₁₂] M₂)) : (f.of_submodule' U x : M₂) = f (x : M) := rfl @[simp] lemma of_submodule'_symm_apply [module R M] [module R₂ M₂] (f : M ≃ₛₗ[σ₁₂] M₂) (U : submodule R₂ M₂) (x : U) : ((f.of_submodule' U).symm x : M) = f.symm (x : M₂) := rfl variable (p) omit σ₂₁ re₁₂ re₂₁ /-- The top submodule of `M` is linearly equivalent to `M`. -/ def of_top (h : p = ⊤) : p ≃ₗ[R] M := { inv_fun := λ x, ⟨x, h.symm ▸ trivial⟩, left_inv := λ ⟨x, h⟩, rfl, right_inv := λ x, rfl, .. p.subtype } @[simp] theorem of_top_apply {h} (x : p) : of_top p h x = x := rfl @[simp] theorem coe_of_top_symm_apply {h} (x : M) : ((of_top p h).symm x : M) = x := rfl theorem of_top_symm_apply {h} (x : M) : (of_top p h).symm x = ⟨x, h.symm ▸ trivial⟩ := rfl include σ₂₁ re₁₂ re₂₁ /-- If a linear map has an inverse, it is a linear equivalence. -/ def of_linear (h₁ : f.comp g = linear_map.id) (h₂ : g.comp f = linear_map.id) : M ≃ₛₗ[σ₁₂] M₂ := { inv_fun := g, left_inv := linear_map.ext_iff.1 h₂, right_inv := linear_map.ext_iff.1 h₁, ..f } omit σ₂₁ re₁₂ re₂₁ include σ₂₁ re₁₂ re₂₁ @[simp] theorem of_linear_apply {h₁ h₂} (x : M) : of_linear f g h₁ h₂ x = f x := rfl omit σ₂₁ re₁₂ re₂₁ include σ₂₁ re₁₂ re₂₁ @[simp] theorem of_linear_symm_apply {h₁ h₂} (x : M₂) : (of_linear f g h₁ h₂).symm x = g x := rfl omit σ₂₁ re₁₂ re₂₁ @[simp] protected theorem range : (e : M →ₛₗ[σ₁₂] M₂).range = ⊤ := linear_map.range_eq_top.2 e.to_equiv.surjective include σ₂₁ re₁₂ re₂₁ lemma eq_bot_of_equiv [module R₂ M₂] (e : p ≃ₛₗ[σ₁₂] (⊥ : submodule R₂ M₂)) : p = ⊥ := begin refine bot_unique (set_like.le_def.2 $ assume b hb, (submodule.mem_bot R).2 _), rw [← p.mk_eq_zero hb, ← e.map_eq_zero_iff], apply submodule.eq_zero_of_bot_submodule end omit σ₂₁ re₁₂ re₂₁ @[simp] protected theorem ker : (e : M →ₛₗ[σ₁₂] M₂).ker = ⊥ := linear_map.ker_eq_bot_of_injective e.to_equiv.injective @[simp] theorem range_comp [ring_hom_surjective σ₁₂] [ring_hom_surjective σ₂₃] [ring_hom_surjective σ₁₃] : (h.comp (e : M →ₛₗ[σ₁₂] M₂) : M →ₛₗ[σ₁₃] M₃).range = h.range := linear_map.range_comp_of_range_eq_top _ e.range include module_M @[simp] theorem ker_comp (l : M →ₛₗ[σ₁₂] M₂) : (((e'' : M₂ →ₛₗ[σ₂₃] M₃).comp l : M →ₛₗ[σ₁₃] M₃) : M →ₛₗ[σ₁₃] M₃).ker = l.ker := linear_map.ker_comp_of_ker_eq_bot _ e''.ker omit module_M variables {f g} include σ₂₁ /-- An linear map `f : M →ₗ[R] M₂` with a left-inverse `g : M₂ →ₗ[R] M` defines a linear equivalence between `M` and `f.range`. This is a computable alternative to `linear_equiv.of_injective`, and a bidirectional version of `linear_map.range_restrict`. -/ def of_left_inverse [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂] {g : M₂ → M} (h : function.left_inverse g f) : M ≃ₛₗ[σ₁₂] f.range := { to_fun := f.range_restrict, inv_fun := g ∘ f.range.subtype, left_inv := h, right_inv := λ x, subtype.ext $ let ⟨x', hx'⟩ := linear_map.mem_range.mp x.prop in show f (g x) = x, by rw [←hx', h x'], .. f.range_restrict } omit σ₂₁ @[simp] lemma of_left_inverse_apply [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂] (h : function.left_inverse g f) (x : M) : ↑(of_left_inverse h x) = f x := rfl include σ₂₁ @[simp] lemma of_left_inverse_symm_apply [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂] (h : function.left_inverse g f) (x : f.range) : (of_left_inverse h).symm x = g x := rfl omit σ₂₁ variables (f) /-- An `injective` linear map `f : M →ₗ[R] M₂` defines a linear equivalence between `M` and `f.range`. See also `linear_map.of_left_inverse`. -/ noncomputable def of_injective [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂] (h : injective f) : M ≃ₛₗ[σ₁₂] f.range := of_left_inverse $ classical.some_spec h.has_left_inverse @[simp] theorem of_injective_apply [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂] {h : injective f} (x : M) : ↑(of_injective f h x) = f x := rfl /-- A bijective linear map is a linear equivalence. -/ noncomputable def of_bijective [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂] (hf₁ : injective f) (hf₂ : surjective f) : M ≃ₛₗ[σ₁₂] M₂ := (of_injective f hf₁).trans (of_top _ $ linear_map.range_eq_top.2 hf₂) @[simp] theorem of_bijective_apply [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂] {hf₁ hf₂} (x : M) : of_bijective f hf₁ hf₂ x = f x := rfl end end add_comm_monoid section add_comm_group variables [semiring R] [semiring R₂] [semiring R₃] [semiring R₄] variables [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] [add_comm_group M₄] variables {module_M : module R M} {module_M₂ : module R₂ M₂} variables {module_M₃ : module R₃ M₃} {module_M₄ : module R₄ M₄} variables {σ₁₂ : R →+* R₂} {σ₃₄ : R₃ →+* R₄} variables {σ₂₁ : R₂ →+* R} {σ₄₃ : R₄ →+* R₃} variables {re₁₂ : ring_hom_inv_pair σ₁₂ σ₂₁} {re₂₁ : ring_hom_inv_pair σ₂₁ σ₁₂} variables {re₃₄ : ring_hom_inv_pair σ₃₄ σ₄₃} {re₄₃ : ring_hom_inv_pair σ₄₃ σ₃₄} variables (e e₁ : M ≃ₛₗ[σ₁₂] M₂) (e₂ : M₃ ≃ₛₗ[σ₃₄] M₄) @[simp] theorem map_neg (a : M) : e (-a) = -e a := e.to_linear_map.map_neg a @[simp] theorem map_sub (a b : M) : e (a - b) = e a - e b := e.to_linear_map.map_sub a b end add_comm_group section neg variables (R) [semiring R] [add_comm_group M] [module R M] /-- `x ↦ -x` as a `linear_equiv` -/ def neg : M ≃ₗ[R] M := { .. equiv.neg M, .. (-linear_map.id : M →ₗ[R] M) } variable {R} @[simp] lemma coe_neg : ⇑(neg R : M ≃ₗ[R] M) = -id := rfl lemma neg_apply (x : M) : neg R x = -x := by simp @[simp] lemma symm_neg : (neg R : M ≃ₗ[R] M).symm = neg R := rfl end neg section comm_semiring variables [comm_semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] variables [module R M] [module R M₂] [module R M₃] open _root_.linear_map /-- Multiplying by a unit `a` of the ring `R` is a linear equivalence. -/ def smul_of_unit (a : units R) : M ≃ₗ[R] M := of_linear ((a:R) • 1 : M →ₗ[R] M) (((a⁻¹ : units R) : R) • 1 : M →ₗ[R] M) (by rw [smul_comp, comp_smul, smul_smul, units.mul_inv, one_smul]; refl) (by rw [smul_comp, comp_smul, smul_smul, units.inv_mul, one_smul]; refl) /-- A linear isomorphism between the domains and codomains of two spaces of linear maps gives a linear isomorphism between the two function spaces. -/ def arrow_congr {R M₁ M₂ M₂₁ M₂₂ : Sort*} [comm_semiring R] [add_comm_monoid M₁] [add_comm_monoid M₂] [add_comm_monoid M₂₁] [add_comm_monoid M₂₂] [module R M₁] [module R M₂] [module R M₂₁] [module R M₂₂] (e₁ : M₁ ≃ₗ[R] M₂) (e₂ : M₂₁ ≃ₗ[R] M₂₂) : (M₁ →ₗ[R] M₂₁) ≃ₗ[R] (M₂ →ₗ[R] M₂₂) := { to_fun := λ f : M₁ →ₗ[R] M₂₁, (e₂ : M₂₁ →ₗ[R] M₂₂).comp $ f.comp (e₁.symm : M₂ →ₗ[R] M₁), inv_fun := λ f, (e₂.symm : M₂₂ →ₗ[R] M₂₁).comp $ f.comp (e₁ : M₁ →ₗ[R] M₂), left_inv := λ f, by { ext x, simp only [symm_apply_apply, comp_app, coe_comp, coe_coe]}, right_inv := λ f, by { ext x, simp only [comp_app, apply_symm_apply, coe_comp, coe_coe]}, map_add' := λ f g, by { ext x, simp only [map_add, add_apply, comp_app, coe_comp, coe_coe]}, map_smul' := λ c f, by { ext x, simp only [smul_apply, comp_app, coe_comp, map_smulₛₗ, coe_coe]} } @[simp] lemma arrow_congr_apply {R M₁ M₂ M₂₁ M₂₂ : Sort*} [comm_semiring R] [add_comm_monoid M₁] [add_comm_monoid M₂] [add_comm_monoid M₂₁] [add_comm_monoid M₂₂] [module R M₁] [module R M₂] [module R M₂₁] [module R M₂₂] (e₁ : M₁ ≃ₗ[R] M₂) (e₂ : M₂₁ ≃ₗ[R] M₂₂) (f : M₁ →ₗ[R] M₂₁) (x : M₂) : arrow_congr e₁ e₂ f x = e₂ (f (e₁.symm x)) := rfl @[simp] lemma arrow_congr_symm_apply {R M₁ M₂ M₂₁ M₂₂ : Sort*} [comm_semiring R] [add_comm_monoid M₁] [add_comm_monoid M₂] [add_comm_monoid M₂₁] [add_comm_monoid M₂₂] [module R M₁] [module R M₂] [module R M₂₁] [module R M₂₂] (e₁ : M₁ ≃ₗ[R] M₂) (e₂ : M₂₁ ≃ₗ[R] M₂₂) (f : M₂ →ₗ[R] M₂₂) (x : M₁) : (arrow_congr e₁ e₂).symm f x = e₂.symm (f (e₁ x)) := rfl lemma arrow_congr_comp {N N₂ N₃ : Sort*} [add_comm_monoid N] [add_comm_monoid N₂] [add_comm_monoid N₃] [module R N] [module R N₂] [module R N₃] (e₁ : M ≃ₗ[R] N) (e₂ : M₂ ≃ₗ[R] N₂) (e₃ : M₃ ≃ₗ[R] N₃) (f : M →ₗ[R] M₂) (g : M₂ →ₗ[R] M₃) : arrow_congr e₁ e₃ (g.comp f) = (arrow_congr e₂ e₃ g).comp (arrow_congr e₁ e₂ f) := by { ext, simp only [symm_apply_apply, arrow_congr_apply, linear_map.comp_apply], } lemma arrow_congr_trans {M₁ M₂ M₃ N₁ N₂ N₃ : Sort*} [add_comm_monoid M₁] [module R M₁] [add_comm_monoid M₂] [module R M₂] [add_comm_monoid M₃] [module R M₃] [add_comm_monoid N₁] [module R N₁] [add_comm_monoid N₂] [module R N₂] [add_comm_monoid N₃] [module R N₃] (e₁ : M₁ ≃ₗ[R] M₂) (e₂ : N₁ ≃ₗ[R] N₂) (e₃ : M₂ ≃ₗ[R] M₃) (e₄ : N₂ ≃ₗ[R] N₃) : (arrow_congr e₁ e₂).trans (arrow_congr e₃ e₄) = arrow_congr (e₁.trans e₃) (e₂.trans e₄) := rfl /-- If `M₂` and `M₃` are linearly isomorphic then the two spaces of linear maps from `M` into `M₂` and `M` into `M₃` are linearly isomorphic. -/ def congr_right (f : M₂ ≃ₗ[R] M₃) : (M →ₗ[R] M₂) ≃ₗ[R] (M →ₗ[R] M₃) := arrow_congr (linear_equiv.refl R M) f /-- If `M` and `M₂` are linearly isomorphic then the two spaces of linear maps from `M` and `M₂` to themselves are linearly isomorphic. -/ def conj (e : M ≃ₗ[R] M₂) : (module.End R M) ≃ₗ[R] (module.End R M₂) := arrow_congr e e lemma conj_apply (e : M ≃ₗ[R] M₂) (f : module.End R M) : e.conj f = ((↑e : M →ₗ[R] M₂).comp f).comp (e.symm : M₂ →ₗ[R] M) := rfl lemma symm_conj_apply (e : M ≃ₗ[R] M₂) (f : module.End R M₂) : e.symm.conj f = ((↑e.symm : M₂ →ₗ[R] M).comp f).comp (e : M →ₗ[R] M₂) := rfl lemma conj_comp (e : M ≃ₗ[R] M₂) (f g : module.End R M) : e.conj (g.comp f) = (e.conj g).comp (e.conj f) := arrow_congr_comp e e e f g lemma conj_trans (e₁ : M ≃ₗ[R] M₂) (e₂ : M₂ ≃ₗ[R] M₃) : e₁.conj.trans e₂.conj = (e₁.trans e₂).conj := by { ext f x, refl, } @[simp] lemma conj_id (e : M ≃ₗ[R] M₂) : e.conj linear_map.id = linear_map.id := by { ext, simp [conj_apply], } end comm_semiring section field variables [field K] [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [module K M] [module K M₂] [module K M₃] variables (K) (M) open _root_.linear_map /-- Multiplying by a nonzero element `a` of the field `K` is a linear equivalence. -/ def smul_of_ne_zero (a : K) (ha : a ≠ 0) : M ≃ₗ[K] M := smul_of_unit $ units.mk0 a ha section noncomputable theory open_locale classical lemma ker_to_span_singleton {x : M} (h : x ≠ 0) : (to_span_singleton K M x).ker = ⊥ := begin ext c, split, { intros hc, rw submodule.mem_bot, rw mem_ker at hc, by_contra hc', have : x = 0, calc x = c⁻¹ • (c • x) : by rw [← mul_smul, inv_mul_cancel hc', one_smul] ... = c⁻¹ • ((to_span_singleton K M x) c) : rfl ... = 0 : by rw [hc, smul_zero], tauto }, { rw [mem_ker, submodule.mem_bot], intros h, rw h, simp } end /-- Given a nonzero element `x` of a vector space `M` over a field `K`, the natural map from `K` to the span of `x`, with invertibility check to consider it as an isomorphism.-/ def to_span_nonzero_singleton (x : M) (h : x ≠ 0) : K ≃ₗ[K] (K ∙ x) := linear_equiv.trans (linear_equiv.of_injective (to_span_singleton K M x) (ker_eq_bot.1 $ ker_to_span_singleton K M h)) (of_eq (to_span_singleton K M x).range (K ∙ x) (span_singleton_eq_range K M x).symm) lemma to_span_nonzero_singleton_one (x : M) (h : x ≠ 0) : to_span_nonzero_singleton K M x h 1 = (⟨x, submodule.mem_span_singleton_self x⟩ : K ∙ x) := begin apply set_like.coe_eq_coe.mp, have : ↑(to_span_nonzero_singleton K M x h 1) = to_span_singleton K M x 1 := rfl, rw [this, to_span_singleton_one, submodule.coe_mk], end /-- Given a nonzero element `x` of a vector space `M` over a field `K`, the natural map from the span of `x` to `K`.-/ abbreviation coord (x : M) (h : x ≠ 0) : (K ∙ x) ≃ₗ[K] K := (to_span_nonzero_singleton K M x h).symm lemma coord_self (x : M) (h : x ≠ 0) : (coord K M x h) (⟨x, submodule.mem_span_singleton_self x⟩ : K ∙ x) = 1 := by rw [← to_span_nonzero_singleton_one K M x h, symm_apply_apply] end end field end linear_equiv namespace submodule section module variables [semiring R] [add_comm_monoid M] [module R M] /-- Given `p` a submodule of the module `M` and `q` a submodule of `p`, `p.equiv_subtype_map q` is the natural `linear_equiv` between `q` and `q.map p.subtype`. -/ def equiv_subtype_map (p : submodule R M) (q : submodule R p) : q ≃ₗ[R] q.map p.subtype := { inv_fun := begin rintro ⟨x, hx⟩, refine ⟨⟨x, _⟩, _⟩; rcases hx with ⟨⟨_, h⟩, _, rfl⟩; assumption end, left_inv := λ ⟨⟨_, _⟩, _⟩, rfl, right_inv := λ ⟨x, ⟨_, h⟩, _, rfl⟩, rfl, .. (p.subtype.dom_restrict q).cod_restrict _ begin rintro ⟨x, hx⟩, refine ⟨x, hx, rfl⟩, end } @[simp] lemma equiv_subtype_map_apply {p : submodule R M} {q : submodule R p} (x : q) : (p.equiv_subtype_map q x : M) = p.subtype.dom_restrict q x := rfl @[simp] lemma equiv_subtype_map_symm_apply {p : submodule R M} {q : submodule R p} (x : q.map p.subtype) : ((p.equiv_subtype_map q).symm x : M) = x := by { cases x, refl } /-- If `s ≤ t`, then we can view `s` as a submodule of `t` by taking the comap of `t.subtype`. -/ @[simps] def comap_subtype_equiv_of_le {p q : submodule R M} (hpq : p ≤ q) : comap q.subtype p ≃ₗ[R] p := { to_fun := λ x, ⟨x, x.2⟩, inv_fun := λ x, ⟨⟨x, hpq x.2⟩, x.2⟩, left_inv := λ x, by simp only [coe_mk, set_like.eta, coe_coe], right_inv := λ x, by simp only [subtype.coe_mk, set_like.eta, coe_coe], map_add' := λ x y, rfl, map_smul' := λ c x, rfl } end module end submodule namespace submodule variables [comm_ring R] [comm_ring R₂] variables [add_comm_group M] [add_comm_group M₂] [module R M] [module R₂ M₂] variables [add_comm_group N] [add_comm_group N₂] [module R N] [module R N₂] variables {τ₁₂ : R →+* R₂} {τ₂₁ : R₂ →+* R} variables [ring_hom_inv_pair τ₁₂ τ₂₁] [ring_hom_inv_pair τ₂₁ τ₁₂] variables (p : submodule R M) (q : submodule R₂ M₂) variables (pₗ : submodule R N) (qₗ : submodule R N₂) include τ₂₁ @[simp] lemma mem_map_equiv {e : M ≃ₛₗ[τ₁₂] M₂} {x : M₂} : x ∈ p.map (e : M →ₛₗ[τ₁₂] M₂) ↔ e.symm x ∈ p := begin rw submodule.mem_map, split, { rintros ⟨y, hy, hx⟩, simp [←hx, hy], }, { intros hx, refine ⟨e.symm x, hx, by simp⟩, }, end omit τ₂₁ lemma map_equiv_eq_comap_symm (e : M ≃ₛₗ[τ₁₂] M₂) (K : submodule R M) : K.map (e : M →ₛₗ[τ₁₂] M₂) = K.comap (e.symm : M₂ →ₛₗ[τ₂₁] M) := submodule.ext (λ _, by rw [mem_map_equiv, mem_comap, linear_equiv.coe_coe]) lemma comap_equiv_eq_map_symm (e : M ≃ₛₗ[τ₁₂] M₂) (K : submodule R₂ M₂) : K.comap (e : M →ₛₗ[τ₁₂] M₂) = K.map (e.symm : M₂ →ₛₗ[τ₂₁] M) := (map_equiv_eq_comap_symm e.symm K).symm lemma comap_le_comap_smul (fₗ : N →ₗ[R] N₂) (c : R) : comap fₗ qₗ ≤ comap (c • fₗ) qₗ := begin rw set_like.le_def, intros m h, change c • (fₗ m) ∈ qₗ, change fₗ m ∈ qₗ at h, apply qₗ.smul_mem _ h, end lemma inf_comap_le_comap_add (f₁ f₂ : M →ₛₗ[τ₁₂] M₂) : comap f₁ q ⊓ comap f₂ q ≤ comap (f₁ + f₂) q := begin rw set_like.le_def, intros m h, change f₁ m + f₂ m ∈ q, change f₁ m ∈ q ∧ f₂ m ∈ q at h, apply q.add_mem h.1 h.2, end /-- Given modules `M`, `M₂` over a commutative ring, together with submodules `p ⊆ M`, `q ⊆ M₂`, the set of maps $\{f ∈ Hom(M, M₂) | f(p) ⊆ q \}$ is a submodule of `Hom(M, M₂)`. -/ def compatible_maps : submodule R (N →ₗ[R] N₂) := { carrier := {fₗ | pₗ ≤ comap fₗ qₗ}, zero_mem' := by { change pₗ ≤ comap 0 qₗ, rw comap_zero, refine le_top, }, add_mem' := λ f₁ f₂ h₁ h₂, by { apply le_trans _ (inf_comap_le_comap_add qₗ f₁ f₂), rw le_inf_iff, exact ⟨h₁, h₂⟩, }, smul_mem' := λ c fₗ h, le_trans h (comap_le_comap_smul qₗ fₗ c), } end submodule namespace equiv variables [semiring R] [add_comm_monoid M] [module R M] [add_comm_monoid M₂] [module R M₂] /-- An equivalence whose underlying function is linear is a linear equivalence. -/ def to_linear_equiv (e : M ≃ M₂) (h : is_linear_map R (e : M → M₂)) : M ≃ₗ[R] M₂ := { .. e, .. h.mk' e} end equiv namespace add_equiv variables [semiring R] [add_comm_monoid M] [module R M] [add_comm_monoid M₂] [module R M₂] /-- An additive equivalence whose underlying function preserves `smul` is a linear equivalence. -/ def to_linear_equiv (e : M ≃+ M₂) (h : ∀ (c : R) x, e (c • x) = c • e x) : M ≃ₗ[R] M₂ := { map_smul' := h, .. e, } @[simp] lemma coe_to_linear_equiv (e : M ≃+ M₂) (h : ∀ (c : R) x, e (c • x) = c • e x) : ⇑(e.to_linear_equiv h) = e := rfl @[simp] lemma coe_to_linear_equiv_symm (e : M ≃+ M₂) (h : ∀ (c : R) x, e (c • x) = c • e x) : ⇑(e.to_linear_equiv h).symm = e.symm := rfl end add_equiv section fun_left variables (R M) [semiring R] [add_comm_monoid M] [module R M] variables {m n p : Type*} namespace linear_map /-- Given an `R`-module `M` and a function `m → n` between arbitrary types, construct a linear map `(n → M) →ₗ[R] (m → M)` -/ def fun_left (f : m → n) : (n → M) →ₗ[R] (m → M) := { to_fun := (∘ f), map_add' := λ _ _, rfl, map_smul' := λ _ _, rfl } @[simp] theorem fun_left_apply (f : m → n) (g : n → M) (i : m) : fun_left R M f g i = g (f i) := rfl @[simp] theorem fun_left_id (g : n → M) : fun_left R M _root_.id g = g := rfl theorem fun_left_comp (f₁ : n → p) (f₂ : m → n) : fun_left R M (f₁ ∘ f₂) = (fun_left R M f₂).comp (fun_left R M f₁) := rfl theorem fun_left_surjective_of_injective (f : m → n) (hf : injective f) : surjective (fun_left R M f) := begin classical, intro g, refine ⟨λ x, if h : ∃ y, f y = x then g h.some else 0, _⟩, { ext, dsimp only [fun_left_apply], split_ifs with w, { congr, exact hf w.some_spec, }, { simpa only [not_true, exists_apply_eq_apply] using w } }, end theorem fun_left_injective_of_surjective (f : m → n) (hf : surjective f) : injective (fun_left R M f) := begin obtain ⟨g, hg⟩ := hf.has_right_inverse, suffices : left_inverse (fun_left R M g) (fun_left R M f), { exact this.injective }, intro x, rw [←linear_map.comp_apply, ← fun_left_comp, hg.id, fun_left_id], end end linear_map namespace linear_equiv open _root_.linear_map /-- Given an `R`-module `M` and an equivalence `m ≃ n` between arbitrary types, construct a linear equivalence `(n → M) ≃ₗ[R] (m → M)` -/ def fun_congr_left (e : m ≃ n) : (n → M) ≃ₗ[R] (m → M) := linear_equiv.of_linear (fun_left R M e) (fun_left R M e.symm) (linear_map.ext $ λ x, funext $ λ i, by rw [id_apply, ← fun_left_comp, equiv.symm_comp_self, fun_left_id]) (linear_map.ext $ λ x, funext $ λ i, by rw [id_apply, ← fun_left_comp, equiv.self_comp_symm, fun_left_id]) @[simp] theorem fun_congr_left_apply (e : m ≃ n) (x : n → M) : fun_congr_left R M e x = fun_left R M e x := rfl @[simp] theorem fun_congr_left_id : fun_congr_left R M (equiv.refl n) = linear_equiv.refl R (n → M) := rfl @[simp] theorem fun_congr_left_comp (e₁ : m ≃ n) (e₂ : n ≃ p) : fun_congr_left R M (equiv.trans e₁ e₂) = linear_equiv.trans (fun_congr_left R M e₂) (fun_congr_left R M e₁) := rfl @[simp] lemma fun_congr_left_symm (e : m ≃ n) : (fun_congr_left R M e).symm = fun_congr_left R M e.symm := rfl end linear_equiv end fun_left namespace linear_equiv variables [semiring R] [add_comm_monoid M] [module R M] variables (R M) instance automorphism_group : group (M ≃ₗ[R] M) := { mul := λ f g, g.trans f, one := linear_equiv.refl R M, inv := λ f, f.symm, mul_assoc := λ f g h, by {ext, refl}, mul_one := λ f, by {ext, refl}, one_mul := λ f, by {ext, refl}, mul_left_inv := λ f, by {ext, exact f.left_inv x} } /-- Restriction from `R`-linear automorphisms of `M` to `R`-linear endomorphisms of `M`, promoted to a monoid hom. -/ def automorphism_group.to_linear_map_monoid_hom : (M ≃ₗ[R] M) →* (M →ₗ[R] M) := { to_fun := coe, map_one' := rfl, map_mul' := λ _ _, rfl } /-- The tautological action by `M ≃ₗ[R] M` on `M`. This generalizes `function.End.apply_mul_action`. -/ instance apply_distrib_mul_action : distrib_mul_action (M ≃ₗ[R] M) M := { smul := ($), smul_zero := linear_equiv.map_zero, smul_add := linear_equiv.map_add, one_smul := λ _, rfl, mul_smul := λ _ _ _, rfl } @[simp] protected lemma smul_def (f : M ≃ₗ[R] M) (a : M) : f • a = f a := rfl /-- `linear_equiv.apply_distrib_mul_action` is faithful. -/ instance apply_has_faithful_scalar : has_faithful_scalar (M ≃ₗ[R] M) M := ⟨λ _ _, linear_equiv.ext⟩ instance apply_smul_comm_class : smul_comm_class R (M ≃ₗ[R] M) M := { smul_comm := λ r e m, (e.map_smul r m).symm } instance apply_smul_comm_class' : smul_comm_class (M ≃ₗ[R] M) R M := { smul_comm := linear_equiv.map_smul } end linear_equiv namespace linear_map variables [semiring R] [add_comm_monoid M] [module R M] variables (R M) /-- The group of invertible linear maps from `M` to itself -/ @[reducible] def general_linear_group := units (M →ₗ[R] M) namespace general_linear_group variables {R M} instance : has_coe_to_fun (general_linear_group R M) (λ _, M → M) := by apply_instance /-- An invertible linear map `f` determines an equivalence from `M` to itself. -/ def to_linear_equiv (f : general_linear_group R M) : (M ≃ₗ[R] M) := { inv_fun := f.inv.to_fun, left_inv := λ m, show (f.inv * f.val) m = m, by erw f.inv_val; simp, right_inv := λ m, show (f.val * f.inv) m = m, by erw f.val_inv; simp, ..f.val } /-- An equivalence from `M` to itself determines an invertible linear map. -/ def of_linear_equiv (f : (M ≃ₗ[R] M)) : general_linear_group R M := { val := f, inv := (f.symm : M →ₗ[R] M), val_inv := linear_map.ext $ λ _, f.apply_symm_apply _, inv_val := linear_map.ext $ λ _, f.symm_apply_apply _ } variables (R M) /-- The general linear group on `R` and `M` is multiplicatively equivalent to the type of linear equivalences between `M` and itself. -/ def general_linear_equiv : general_linear_group R M ≃* (M ≃ₗ[R] M) := { to_fun := to_linear_equiv, inv_fun := of_linear_equiv, left_inv := λ f, by { ext, refl }, right_inv := λ f, by { ext, refl }, map_mul' := λ x y, by {ext, refl} } @[simp] lemma general_linear_equiv_to_linear_map (f : general_linear_group R M) : (general_linear_equiv R M f : M →ₗ[R] M) = f := by {ext, refl} end general_linear_group end linear_map namespace submodule variables [ring R] [add_comm_group M] [module R M] instance : is_modular_lattice (submodule R M) := ⟨λ x y z xz a ha, begin rw [mem_inf, mem_sup] at ha, rcases ha with ⟨⟨b, hb, c, hc, rfl⟩, haz⟩, rw mem_sup, refine ⟨b, hb, c, mem_inf.2 ⟨hc, _⟩, rfl⟩, rw [← add_sub_cancel c b, add_comm], apply z.sub_mem haz (xz hb), end⟩ end submodule
67fbb7febdd4761863733c343ad01d9300b8239c
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/data/fin/default_auto.lean
2f71c059644dc2e97ec7956da47b1e55fb4e8bee
[]
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
303
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.data.fin.basic import Mathlib.Lean3Lib.init.data.fin.ops namespace Mathlib end Mathlib
9c1cee49862128c2b09ed5bf8fe5102ef2b266da
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/geometry/manifold/diffeomorph.lean
5c1acb8ca8f4c81b56e1fcfb58b21a1a14049d16
[ "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
19,771
lean
/- Copyright © 2020 Nicolò Cavalleri. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Nicolò Cavalleri, Yury Kudryashov -/ import geometry.manifold.times_cont_mdiff_map /-! # Diffeomorphisms This file implements diffeomorphisms. ## Definitions * `diffeomorph I I' M M' n`: `n`-times continuously differentiable diffeomorphism between `M` and `M'` with respect to I and I'; we do not introduce a separate definition for the case `n = ∞`; we use notation instead. * `diffeomorph.to_homeomorph`: reinterpret a diffeomorphism as a homeomorphism. * `continuous_linear_equiv.to_diffeomorph`: reinterpret a continuous equivalence as a diffeomorphism. * `model_with_corners.trans_diffeomorph`: compose a given `model_with_corners` with a diffeomorphism between the old and the new target spaces. Useful, e.g, to turn any finite dimensional manifold into a manifold modelled on a Euclidean space. * `diffeomorph.to_trans_diffeomorph`: the identity diffeomorphism between `M` with model `I` and `M` with model `I.trans_diffeomorph e`. ## Notations * `M ≃ₘ^n⟮I, I'⟯ M'` := `diffeomorph I J M N n` * `M ≃ₘ⟮I, I'⟯ M'` := `diffeomorph I J M N ⊤` * `E ≃ₘ^n[𝕜] E'` := `E ≃ₘ^n⟮𝓘(𝕜, E), 𝓘(𝕜, E')⟯ E'` * `E ≃ₘ[𝕜] E'` := `E ≃ₘ⟮𝓘(𝕜, E), 𝓘(𝕜, E')⟯ E'` ## Implementation notes This notion of diffeomorphism is needed although there is already a notion of structomorphism because structomorphisms do not allow the model spaces `H` and `H'` of the two manifolds to be different, i.e. for a structomorphism one has to impose `H = H'` which is often not the case in practice. ## Keywords diffeomorphism, manifold -/ open_locale manifold topological_space open function set variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {F : Type*} [normed_group F] [normed_space 𝕜 F] {H : Type*} [topological_space H] {H' : Type*} [topological_space H'] {G : Type*} [topological_space G] {I : model_with_corners 𝕜 E H} {I' : model_with_corners 𝕜 E' H'} {J : model_with_corners 𝕜 F G} variables {M : Type*} [topological_space M] [charted_space H M] {M' : Type*} [topological_space M'] [charted_space H' M'] {N : Type*} [topological_space N] [charted_space G N] {n : with_top ℕ} section defs variables (I I' M M' n) /-- `n`-times continuously differentiable diffeomorphism between `M` and `M'` with respect to I and I' -/ @[protect_proj, nolint has_inhabited_instance] structure diffeomorph extends M ≃ M' := (times_cont_mdiff_to_fun : times_cont_mdiff I I' n to_equiv) (times_cont_mdiff_inv_fun : times_cont_mdiff I' I n to_equiv.symm) end defs localized "notation M ` ≃ₘ^` n:1000 `⟮`:50 I `,` J `⟯ ` N := diffeomorph I J M N n" in manifold localized "notation M ` ≃ₘ⟮` I `,` J `⟯ ` N := diffeomorph I J M N ⊤" in manifold localized "notation E ` ≃ₘ^` n:1000 `[`:50 𝕜 `] ` E' := diffeomorph (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') E E' n" in manifold localized "notation E ` ≃ₘ[` 𝕜 `] ` E' := diffeomorph (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') E E' ⊤" in manifold namespace diffeomorph instance : has_coe_to_fun (M ≃ₘ^n⟮I, I'⟯ M') (λ _, M → M') := ⟨λe, e.to_equiv⟩ instance : has_coe (M ≃ₘ^n⟮I, I'⟯ M') C^n⟮I, M; I', M'⟯ := ⟨λ Φ, ⟨Φ, Φ.times_cont_mdiff_to_fun⟩⟩ @[continuity] protected lemma continuous (h : M ≃ₘ^n⟮I, I'⟯ M') : continuous h := h.times_cont_mdiff_to_fun.continuous protected lemma times_cont_mdiff (h : M ≃ₘ^n⟮I, I'⟯ M') : times_cont_mdiff I I' n h := h.times_cont_mdiff_to_fun protected lemma times_cont_mdiff_at (h : M ≃ₘ^n⟮I, I'⟯ M') {x} : times_cont_mdiff_at I I' n h x := h.times_cont_mdiff.times_cont_mdiff_at protected lemma times_cont_mdiff_within_at (h : M ≃ₘ^n⟮I, I'⟯ M') {s x} : times_cont_mdiff_within_at I I' n h s x := h.times_cont_mdiff_at.times_cont_mdiff_within_at protected lemma times_cont_diff (h : E ≃ₘ^n[𝕜] E') : times_cont_diff 𝕜 n h := h.times_cont_mdiff.times_cont_diff protected lemma smooth (h : M ≃ₘ⟮I, I'⟯ M') : smooth I I' h := h.times_cont_mdiff_to_fun protected lemma mdifferentiable (h : M ≃ₘ^n⟮I, I'⟯ M') (hn : 1 ≤ n) : mdifferentiable I I' h := h.times_cont_mdiff.mdifferentiable hn protected lemma mdifferentiable_on (h : M ≃ₘ^n⟮I, I'⟯ M') (s : set M) (hn : 1 ≤ n) : mdifferentiable_on I I' h s := (h.mdifferentiable hn).mdifferentiable_on @[simp] lemma coe_to_equiv (h : M ≃ₘ^n⟮I, I'⟯ M') : ⇑h.to_equiv = h := rfl @[simp, norm_cast] lemma coe_coe (h : M ≃ₘ^n⟮I, I'⟯ M') : ⇑(h : C^n⟮I, M; I', M'⟯) = h := rfl lemma to_equiv_injective : injective (diffeomorph.to_equiv : (M ≃ₘ^n⟮I, I'⟯ M') → (M ≃ M')) | ⟨e, _, _⟩ ⟨e', _, _⟩ rfl := rfl @[simp] lemma to_equiv_inj {h h' : M ≃ₘ^n⟮I, I'⟯ M'} : h.to_equiv = h'.to_equiv ↔ h = h' := to_equiv_injective.eq_iff /-- Coercion to function `λ h : M ≃ₘ^n⟮I, I'⟯ M', (h : M → M')` is injective. -/ lemma coe_fn_injective : injective (λ (h : M ≃ₘ^n⟮I, I'⟯ M') (x : M), h x) := equiv.coe_fn_injective.comp to_equiv_injective @[ext] lemma ext {h h' : M ≃ₘ^n⟮I, I'⟯ M'} (Heq : ∀ x, h x = h' x) : h = h' := coe_fn_injective $ funext Heq section variables (M I n) /-- Identity map as a diffeomorphism. -/ protected def refl : M ≃ₘ^n⟮I, I⟯ M := { times_cont_mdiff_to_fun := times_cont_mdiff_id, times_cont_mdiff_inv_fun := times_cont_mdiff_id, to_equiv := equiv.refl M } @[simp] lemma refl_to_equiv : (diffeomorph.refl I M n).to_equiv = equiv.refl _ := rfl @[simp] lemma coe_refl : ⇑(diffeomorph.refl I M n) = id := rfl end /-- Composition of two diffeomorphisms. -/ protected def trans (h₁ : M ≃ₘ^n⟮I, I'⟯ M') (h₂ : M' ≃ₘ^n⟮I', J⟯ N) : M ≃ₘ^n⟮I, J⟯ N := { times_cont_mdiff_to_fun := h₂.times_cont_mdiff_to_fun.comp h₁.times_cont_mdiff_to_fun, times_cont_mdiff_inv_fun := h₁.times_cont_mdiff_inv_fun.comp h₂.times_cont_mdiff_inv_fun, to_equiv := h₁.to_equiv.trans h₂.to_equiv } @[simp] lemma trans_refl (h : M ≃ₘ^n⟮I, I'⟯ M') : h.trans (diffeomorph.refl I' M' n) = h := ext $ λ _, rfl @[simp] lemma refl_trans (h : M ≃ₘ^n⟮I, I'⟯ M') : (diffeomorph.refl I M n).trans h = h := ext $ λ _, rfl @[simp] lemma coe_trans (h₁ : M ≃ₘ^n⟮I, I'⟯ M') (h₂ : M' ≃ₘ^n⟮I', J⟯ N) : ⇑(h₁.trans h₂) = h₂ ∘ h₁ := rfl /-- Inverse of a diffeomorphism. -/ protected def symm (h : M ≃ₘ^n⟮I, J⟯ N) : N ≃ₘ^n⟮J, I⟯ M := { times_cont_mdiff_to_fun := h.times_cont_mdiff_inv_fun, times_cont_mdiff_inv_fun := h.times_cont_mdiff_to_fun, to_equiv := h.to_equiv.symm } @[simp] lemma apply_symm_apply (h : M ≃ₘ^n⟮I, J⟯ N) (x : N) : h (h.symm x) = x := h.to_equiv.apply_symm_apply x @[simp] lemma symm_apply_apply (h : M ≃ₘ^n⟮I, J⟯ N) (x : M) : h.symm (h x) = x := h.to_equiv.symm_apply_apply x @[simp] lemma symm_refl : (diffeomorph.refl I M n).symm = diffeomorph.refl I M n := ext $ λ _, rfl @[simp] lemma self_trans_symm (h : M ≃ₘ^n⟮I, J⟯ N) : h.trans h.symm = diffeomorph.refl I M n := ext h.symm_apply_apply @[simp] lemma symm_trans_self (h : M ≃ₘ^n⟮I, J⟯ N) : h.symm.trans h = diffeomorph.refl J N n := ext h.apply_symm_apply @[simp] lemma symm_trans' (h₁ : M ≃ₘ^n⟮I, I'⟯ M') (h₂ : M' ≃ₘ^n⟮I', J⟯ N) : (h₁.trans h₂).symm = h₂.symm.trans h₁.symm := rfl @[simp] lemma symm_to_equiv (h : M ≃ₘ^n⟮I, J⟯ N) : h.symm.to_equiv = h.to_equiv.symm := rfl @[simp, mfld_simps] lemma to_equiv_coe_symm (h : M ≃ₘ^n⟮I, J⟯ N) : ⇑h.to_equiv.symm = h.symm := rfl lemma image_eq_preimage (h : M ≃ₘ^n⟮I, J⟯ N) (s : set M) : h '' s = h.symm ⁻¹' s := h.to_equiv.image_eq_preimage s lemma symm_image_eq_preimage (h : M ≃ₘ^n⟮I, J⟯ N) (s : set N) : h.symm '' s = h ⁻¹' s := h.symm.image_eq_preimage s @[simp, mfld_simps] lemma range_comp {α} (h : M ≃ₘ^n⟮I, J⟯ N) (f : α → M) : range (h ∘ f) = h.symm ⁻¹' (range f) := by rw [range_comp, image_eq_preimage] @[simp] lemma image_symm_image (h : M ≃ₘ^n⟮I, J⟯ N) (s : set N) : h '' (h.symm '' s) = s := h.to_equiv.image_symm_image s @[simp] lemma symm_image_image (h : M ≃ₘ^n⟮I, J⟯ N) (s : set M) : h.symm '' (h '' s) = s := h.to_equiv.symm_image_image s /-- A diffeomorphism is a homeomorphism. -/ def to_homeomorph (h : M ≃ₘ^n⟮I, J⟯ N) : M ≃ₜ N := ⟨h.to_equiv, h.continuous, h.symm.continuous⟩ @[simp] lemma to_homeomorph_to_equiv (h : M ≃ₘ^n⟮I, J⟯ N) : h.to_homeomorph.to_equiv = h.to_equiv := rfl @[simp] lemma symm_to_homeomorph (h : M ≃ₘ^n⟮I, J⟯ N) : h.symm.to_homeomorph = h.to_homeomorph.symm := rfl @[simp] lemma coe_to_homeomorph (h : M ≃ₘ^n⟮I, J⟯ N) : ⇑h.to_homeomorph = h := rfl @[simp] lemma coe_to_homeomorph_symm (h : M ≃ₘ^n⟮I, J⟯ N) : ⇑h.to_homeomorph.symm = h.symm := rfl @[simp] lemma times_cont_mdiff_within_at_comp_diffeomorph_iff {m} (h : M ≃ₘ^n⟮I, J⟯ N) {f : N → M'} {s x} (hm : m ≤ n) : times_cont_mdiff_within_at I I' m (f ∘ h) s x ↔ times_cont_mdiff_within_at J I' m f (h.symm ⁻¹' s) (h x) := begin split, { intro Hfh, rw [← h.symm_apply_apply x] at Hfh, simpa only [(∘), h.apply_symm_apply] using Hfh.comp (h x) (h.symm.times_cont_mdiff_within_at.of_le hm) (maps_to_preimage _ _) }, { rw ← h.image_eq_preimage, exact λ hf, hf.comp x (h.times_cont_mdiff_within_at.of_le hm) (maps_to_image _ _) } end @[simp] lemma times_cont_mdiff_on_comp_diffeomorph_iff {m} (h : M ≃ₘ^n⟮I, J⟯ N) {f : N → M'} {s} (hm : m ≤ n) : times_cont_mdiff_on I I' m (f ∘ h) s ↔ times_cont_mdiff_on J I' m f (h.symm ⁻¹' s) := h.to_equiv.forall_congr $ λ x, by simp only [hm, coe_to_equiv, symm_apply_apply, times_cont_mdiff_within_at_comp_diffeomorph_iff, mem_preimage] @[simp] lemma times_cont_mdiff_at_comp_diffeomorph_iff {m} (h : M ≃ₘ^n⟮I, J⟯ N) {f : N → M'} {x} (hm : m ≤ n) : times_cont_mdiff_at I I' m (f ∘ h) x ↔ times_cont_mdiff_at J I' m f (h x) := h.times_cont_mdiff_within_at_comp_diffeomorph_iff hm @[simp] lemma times_cont_mdiff_comp_diffeomorph_iff {m} (h : M ≃ₘ^n⟮I, J⟯ N) {f : N → M'} (hm : m ≤ n) : times_cont_mdiff I I' m (f ∘ h) ↔ times_cont_mdiff J I' m f := h.to_equiv.forall_congr $ λ x, (h.times_cont_mdiff_at_comp_diffeomorph_iff hm) @[simp] lemma times_cont_mdiff_within_at_diffeomorph_comp_iff {m} (h : M ≃ₘ^n⟮I, J⟯ N) {f : M' → M} (hm : m ≤ n) {s x} : times_cont_mdiff_within_at I' J m (h ∘ f) s x ↔ times_cont_mdiff_within_at I' I m f s x := ⟨λ Hhf, by simpa only [(∘), h.symm_apply_apply] using (h.symm.times_cont_mdiff_at.of_le hm).comp_times_cont_mdiff_within_at _ Hhf, λ Hf, (h.times_cont_mdiff_at.of_le hm).comp_times_cont_mdiff_within_at _ Hf⟩ @[simp] lemma times_cont_mdiff_at_diffeomorph_comp_iff {m} (h : M ≃ₘ^n⟮I, J⟯ N) {f : M' → M} (hm : m ≤ n) {x} : times_cont_mdiff_at I' J m (h ∘ f) x ↔ times_cont_mdiff_at I' I m f x := h.times_cont_mdiff_within_at_diffeomorph_comp_iff hm @[simp] lemma times_cont_mdiff_on_diffeomorph_comp_iff {m} (h : M ≃ₘ^n⟮I, J⟯ N) {f : M' → M} (hm : m ≤ n) {s} : times_cont_mdiff_on I' J m (h ∘ f) s ↔ times_cont_mdiff_on I' I m f s := forall₂_congr $ λ x hx, h.times_cont_mdiff_within_at_diffeomorph_comp_iff hm @[simp] lemma times_cont_mdiff_diffeomorph_comp_iff {m} (h : M ≃ₘ^n⟮I, J⟯ N) {f : M' → M} (hm : m ≤ n) : times_cont_mdiff I' J m (h ∘ f) ↔ times_cont_mdiff I' I m f := forall_congr $ λ x, h.times_cont_mdiff_within_at_diffeomorph_comp_iff hm lemma to_local_homeomorph_mdifferentiable (h : M ≃ₘ^n⟮I, J⟯ N) (hn : 1 ≤ n) : h.to_homeomorph.to_local_homeomorph.mdifferentiable I J := ⟨h.mdifferentiable_on _ hn, h.symm.mdifferentiable_on _ hn⟩ variables [smooth_manifold_with_corners I M] [smooth_manifold_with_corners J N] lemma unique_mdiff_on_image_aux (h : M ≃ₘ^n⟮I, J⟯ N) (hn : 1 ≤ n) {s : set M} (hs : unique_mdiff_on I s) : unique_mdiff_on J (h '' s) := begin convert hs.unique_mdiff_on_preimage (h.to_local_homeomorph_mdifferentiable hn), simp [h.image_eq_preimage] end @[simp] lemma unique_mdiff_on_image (h : M ≃ₘ^n⟮I, J⟯ N) (hn : 1 ≤ n) {s : set M} : unique_mdiff_on J (h '' s) ↔ unique_mdiff_on I s := ⟨λ hs, h.symm_image_image s ▸ h.symm.unique_mdiff_on_image_aux hn hs, h.unique_mdiff_on_image_aux hn⟩ @[simp] lemma unique_mdiff_on_preimage (h : M ≃ₘ^n⟮I, J⟯ N) (hn : 1 ≤ n) {s : set N} : unique_mdiff_on I (h ⁻¹' s) ↔ unique_mdiff_on J s := h.symm_image_eq_preimage s ▸ h.symm.unique_mdiff_on_image hn @[simp] lemma unique_diff_on_image (h : E ≃ₘ^n[𝕜] F) (hn : 1 ≤ n) {s : set E} : unique_diff_on 𝕜 (h '' s) ↔ unique_diff_on 𝕜 s := by simp only [← unique_mdiff_on_iff_unique_diff_on, unique_mdiff_on_image, hn] @[simp] lemma unique_diff_on_preimage (h : E ≃ₘ^n[𝕜] F) (hn : 1 ≤ n) {s : set F} : unique_diff_on 𝕜 (h ⁻¹' s) ↔ unique_diff_on 𝕜 s := h.symm_image_eq_preimage s ▸ h.symm.unique_diff_on_image hn end diffeomorph namespace continuous_linear_equiv variable (e : E ≃L[𝕜] E') /-- A continuous linear equivalence between normed spaces is a diffeomorphism. -/ def to_diffeomorph : E ≃ₘ[𝕜] E' := { times_cont_mdiff_to_fun := e.times_cont_diff.times_cont_mdiff, times_cont_mdiff_inv_fun := e.symm.times_cont_diff.times_cont_mdiff, to_equiv := e.to_linear_equiv.to_equiv } @[simp] lemma coe_to_diffeomorph : ⇑e.to_diffeomorph = e := rfl @[simp] lemma symm_to_diffeomorph : e.symm.to_diffeomorph = e.to_diffeomorph.symm := rfl @[simp] lemma coe_to_diffeomorph_symm : ⇑e.to_diffeomorph.symm = e.symm := rfl end continuous_linear_equiv namespace model_with_corners variables (I) (e : E ≃ₘ[𝕜] E') /-- Apply a diffeomorphism (e.g., a continuous linear equivalence) to the model vector space. -/ def trans_diffeomorph (I : model_with_corners 𝕜 E H) (e : E ≃ₘ[𝕜] E') : model_with_corners 𝕜 E' H := { to_local_equiv := I.to_local_equiv.trans e.to_equiv.to_local_equiv, source_eq := by simp, unique_diff' := by simp [range_comp e, I.unique_diff], continuous_to_fun := e.continuous.comp I.continuous, continuous_inv_fun := I.continuous_symm.comp e.symm.continuous } @[simp, mfld_simps] lemma coe_trans_diffeomorph : ⇑(I.trans_diffeomorph e) = e ∘ I := rfl @[simp, mfld_simps] lemma coe_trans_diffeomorph_symm : ⇑(I.trans_diffeomorph e).symm = I.symm ∘ e.symm := rfl lemma trans_diffeomorph_range : range (I.trans_diffeomorph e) = e '' (range I) := range_comp e I lemma coe_ext_chart_at_trans_diffeomorph (x : M) : ⇑(ext_chart_at (I.trans_diffeomorph e) x) = e ∘ ext_chart_at I x := rfl lemma coe_ext_chart_at_trans_diffeomorph_symm (x : M) : ⇑(ext_chart_at (I.trans_diffeomorph e) x).symm = (ext_chart_at I x).symm ∘ e.symm := rfl lemma ext_chart_at_trans_diffeomorph_target (x : M) : (ext_chart_at (I.trans_diffeomorph e) x).target = e.symm ⁻¹' (ext_chart_at I x).target := by simp only [range_comp e, e.image_eq_preimage, preimage_preimage] with mfld_simps end model_with_corners namespace diffeomorph variables (e : E ≃ₘ[𝕜] F) instance smooth_manifold_with_corners_trans_diffeomorph [smooth_manifold_with_corners I M] : smooth_manifold_with_corners (I.trans_diffeomorph e) M := begin refine smooth_manifold_with_corners_of_times_cont_diff_on _ _ (λ e₁ e₂ h₁ h₂, _), refine e.times_cont_diff.comp_times_cont_diff_on (((times_cont_diff_groupoid ⊤ I).compatible h₁ h₂).1.comp e.symm.times_cont_diff.times_cont_diff_on _), mfld_set_tac end variables (I M) /-- The identity diffeomorphism between a manifold with model `I` and the same manifold with model `I.trans_diffeomorph e`. -/ def to_trans_diffeomorph (e : E ≃ₘ[𝕜] F) : M ≃ₘ⟮I, I.trans_diffeomorph e⟯ M := { to_equiv := equiv.refl M, times_cont_mdiff_to_fun := λ x, begin refine times_cont_mdiff_within_at_iff.2 ⟨continuous_within_at_id, _⟩, refine e.times_cont_diff.times_cont_diff_within_at.congr' (λ y hy, _) _, { simp only [equiv.coe_refl, id, (∘), I.coe_ext_chart_at_trans_diffeomorph, (ext_chart_at I x).right_inv hy.1] }, exact ⟨(ext_chart_at I x).map_source (mem_ext_chart_source I x), trivial, by simp only with mfld_simps⟩ end, times_cont_mdiff_inv_fun := λ x, begin refine times_cont_mdiff_within_at_iff.2 ⟨continuous_within_at_id, _⟩, refine e.symm.times_cont_diff.times_cont_diff_within_at.congr' (λ y hy, _) _, { simp only [mem_inter_eq, I.ext_chart_at_trans_diffeomorph_target] at hy, simp only [equiv.coe_refl, equiv.refl_symm, id, (∘), I.coe_ext_chart_at_trans_diffeomorph_symm, (ext_chart_at I x).right_inv hy.1] }, exact ⟨(ext_chart_at _ x).map_source (mem_ext_chart_source _ x), trivial, by simp only [e.symm_apply_apply, equiv.refl_symm, equiv.coe_refl] with mfld_simps⟩ end } variables {I M} @[simp] lemma times_cont_mdiff_within_at_trans_diffeomorph_right {f : M' → M} {x s} : times_cont_mdiff_within_at I' (I.trans_diffeomorph e) n f s x ↔ times_cont_mdiff_within_at I' I n f s x := (to_trans_diffeomorph I M e).times_cont_mdiff_within_at_diffeomorph_comp_iff le_top @[simp] lemma times_cont_mdiff_at_trans_diffeomorph_right {f : M' → M} {x} : times_cont_mdiff_at I' (I.trans_diffeomorph e) n f x ↔ times_cont_mdiff_at I' I n f x := (to_trans_diffeomorph I M e).times_cont_mdiff_at_diffeomorph_comp_iff le_top @[simp] lemma times_cont_mdiff_on_trans_diffeomorph_right {f : M' → M} {s} : times_cont_mdiff_on I' (I.trans_diffeomorph e) n f s ↔ times_cont_mdiff_on I' I n f s := (to_trans_diffeomorph I M e).times_cont_mdiff_on_diffeomorph_comp_iff le_top @[simp] lemma times_cont_mdiff_trans_diffeomorph_right {f : M' → M} : times_cont_mdiff I' (I.trans_diffeomorph e) n f ↔ times_cont_mdiff I' I n f := (to_trans_diffeomorph I M e).times_cont_mdiff_diffeomorph_comp_iff le_top @[simp] lemma smooth_trans_diffeomorph_right {f : M' → M} : smooth I' (I.trans_diffeomorph e) f ↔ smooth I' I f := times_cont_mdiff_trans_diffeomorph_right e @[simp] lemma times_cont_mdiff_within_at_trans_diffeomorph_left {f : M → M'} {x s} : times_cont_mdiff_within_at (I.trans_diffeomorph e) I' n f s x ↔ times_cont_mdiff_within_at I I' n f s x := ((to_trans_diffeomorph I M e).times_cont_mdiff_within_at_comp_diffeomorph_iff le_top).symm @[simp] lemma times_cont_mdiff_at_trans_diffeomorph_left {f : M → M'} {x} : times_cont_mdiff_at (I.trans_diffeomorph e) I' n f x ↔ times_cont_mdiff_at I I' n f x := ((to_trans_diffeomorph I M e).times_cont_mdiff_at_comp_diffeomorph_iff le_top).symm @[simp] lemma times_cont_mdiff_on_trans_diffeomorph_left {f : M → M'} {s} : times_cont_mdiff_on (I.trans_diffeomorph e) I' n f s ↔ times_cont_mdiff_on I I' n f s := ((to_trans_diffeomorph I M e).times_cont_mdiff_on_comp_diffeomorph_iff le_top).symm @[simp] lemma times_cont_mdiff_trans_diffeomorph_left {f : M → M'} : times_cont_mdiff (I.trans_diffeomorph e) I' n f ↔ times_cont_mdiff I I' n f := ((to_trans_diffeomorph I M e).times_cont_mdiff_comp_diffeomorph_iff le_top).symm @[simp] lemma smooth_trans_diffeomorph_left {f : M → M'} : smooth (I.trans_diffeomorph e) I' f ↔ smooth I I' f := e.times_cont_mdiff_trans_diffeomorph_left end diffeomorph
dd19e26dc0ce83ac7881f75ca91b0eb081a039ae
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/def3.lean
8377bf1e5292c0c9cefe1222f8b87457ab0a3175
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
193
lean
definition f (a b : nat) : nat := nat.cases_on a (a + b + a + a + b) (λ a', a + a + b) definition g (a b : nat) := f (f a b) a set_option trace.compiler true vm_eval g (g (f 2 3) 2) 3
3a3e9af843e7e562741f0517e60dd4e628ea1a4f
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/topology/local_extr.lean
eef9fac277ef4b33f32c44a09aaa4b8dbe452248
[ "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
14,809
lean
/- Copyright (c) 2019 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import order.filter.extr topology.continuous_on /-! # Local extrema of functions on topological spaces ## Main definitions This file defines special versions of `is_*_filter f a l`, `*=min/max/extr`, from `order/filter/extr` for two kinds of filters: `nhds_within` and `nhds`. These versions are called `is_local_*_on` and `is_local_*`, respectively. ## Main statements Many lemmas in this file restate those from `order/filter/extr`, and you can find a detailed documentation there. These convenience lemmas are provided only to make the dot notation return propositions of expected types, not just `is_*_filter`. Here is the list of statements specific to these two types of filters: * `is_local_*.on`, `is_local_*_on.on_subset`: restrict to a subset; * `is_local_*_on.inter` : intersect the set with another one; * `is_*_on.localize` : a global extremum is a local extremum too. * `is_[local_]*_on.is_local_*` : if we have `is_local_*_on f s a` and `s ∈ 𝓝 a`, then we have `is_local_* f a`. -/ universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} [topological_space α] open set filter open_locale topological_space section preorder variables [preorder β] [preorder γ] (f : α → β) (s : set α) (a : α) /-- `is_local_min_on f s a` means that `f a ≤ f x` for all `x ∈ s` in some neighborhood of `a`. -/ def is_local_min_on := is_min_filter f (nhds_within a s) a /-- `is_local_max_on f s a` means that `f x ≤ f a` for all `x ∈ s` in some neighborhood of `a`. -/ def is_local_max_on := is_max_filter f (nhds_within a s) a /-- `is_local_extr_on f s a` means `is_local_min_on f s a ∨ is_local_max_on f s a`. -/ def is_local_extr_on := is_extr_filter f (nhds_within a s) a /-- `is_local_min f a` means that `f a ≤ f x` for all `x` in some neighborhood of `a`. -/ def is_local_min := is_min_filter f (𝓝 a) a /-- `is_local_max f a` means that `f x ≤ f a` for all `x ∈ s` in some neighborhood of `a`. -/ def is_local_max := is_max_filter f (𝓝 a) a /-- `is_local_extr_on f s a` means `is_local_min_on f s a ∨ is_local_max_on f s a`. -/ def is_local_extr := is_extr_filter f (𝓝 a) a variables {f s a} lemma is_local_extr_on.elim {p : Prop} : is_local_extr_on f s a → (is_local_min_on f s a → p) → (is_local_max_on f s a → p) → p := or.elim lemma is_local_extr.elim {p : Prop} : is_local_extr f a → (is_local_min f a → p) → (is_local_max f a → p) → p := or.elim /-! ### Restriction to (sub)sets -/ lemma is_local_min.on (h : is_local_min f a) (s) : is_local_min_on f s a := h.filter_inf _ lemma is_local_max.on (h : is_local_max f a) (s) : is_local_max_on f s a := h.filter_inf _ lemma is_local_extr.on (h : is_local_extr f a) (s) : is_local_extr_on f s a := h.filter_inf _ lemma is_local_min_on.on_subset {t : set α} (hf : is_local_min_on f t a) (h : s ⊆ t) : is_local_min_on f s a := hf.filter_mono $ nhds_within_mono a h lemma is_local_max_on.on_subset {t : set α} (hf : is_local_max_on f t a) (h : s ⊆ t) : is_local_max_on f s a := hf.filter_mono $ nhds_within_mono a h lemma is_local_extr_on.on_subset {t : set α} (hf : is_local_extr_on f t a) (h : s ⊆ t) : is_local_extr_on f s a := hf.filter_mono $ nhds_within_mono a h lemma is_local_min_on.inter (hf : is_local_min_on f s a) (t) : is_local_min_on f (s ∩ t) a := hf.on_subset (inter_subset_left s t) lemma is_local_max_on.inter (hf : is_local_max_on f s a) (t) : is_local_max_on f (s ∩ t) a := hf.on_subset (inter_subset_left s t) lemma is_local_extr_on.inter (hf : is_local_extr_on f s a) (t) : is_local_extr_on f (s ∩ t) a := hf.on_subset (inter_subset_left s t) lemma is_min_on.localize (hf : is_min_on f s a) : is_local_min_on f s a := hf.filter_mono $ inf_le_right lemma is_max_on.localize (hf : is_max_on f s a) : is_local_max_on f s a := hf.filter_mono $ inf_le_right lemma is_extr_on.localize (hf : is_extr_on f s a) : is_local_extr_on f s a := hf.filter_mono $ inf_le_right lemma is_local_min_on.is_local_min (hf : is_local_min_on f s a) (hs : s ∈ 𝓝 a) : is_local_min f a := have 𝓝 a ≤ principal s, from le_principal_iff.2 hs, hf.filter_mono $ le_inf (le_refl _) this lemma is_local_max_on.is_local_max (hf : is_local_max_on f s a) (hs : s ∈ 𝓝 a) : is_local_max f a := have 𝓝 a ≤ principal s, from le_principal_iff.2 hs, hf.filter_mono $ le_inf (le_refl _) this lemma is_local_extr_on.is_local_extr (hf : is_local_extr_on f s a) (hs : s ∈ 𝓝 a) : is_local_extr f a := hf.elim (λ hf, (hf.is_local_min hs).is_extr) (λ hf, (hf.is_local_max hs).is_extr) lemma is_min_on.is_local_min (hf : is_min_on f s a) (hs : s ∈ 𝓝 a) : is_local_min f a := hf.localize.is_local_min hs lemma is_max_on.is_local_max (hf : is_max_on f s a) (hs : s ∈ 𝓝 a) : is_local_max f a := hf.localize.is_local_max hs lemma is_extr_on.is_local_extr (hf : is_extr_on f s a) (hs : s ∈ 𝓝 a) : is_local_extr f a := hf.localize.is_local_extr hs /-! ### Constant -/ lemma is_local_min_on_const {b : β} : is_local_min_on (λ _, b) s a := is_min_filter_const lemma is_local_max_on_const {b : β} : is_local_max_on (λ _, b) s a := is_max_filter_const lemma is_local_extr_on_const {b : β} : is_local_extr_on (λ _, b) s a := is_extr_filter_const lemma is_local_min_const {b : β} : is_local_min (λ _, b) a := is_min_filter_const lemma is_local_max_const {b : β} : is_local_max (λ _, b) a := is_max_filter_const lemma is_local_extr_const {b : β} : is_local_extr (λ _, b) a := is_extr_filter_const /-! ### Composition with (anti)monotone functions -/ lemma is_local_min.comp_mono (hf : is_local_min f a) {g : β → γ} (hg : monotone g) : is_local_min (g ∘ f) a := hf.comp_mono hg lemma is_local_max.comp_mono (hf : is_local_max f a) {g : β → γ} (hg : monotone g) : is_local_max (g ∘ f) a := hf.comp_mono hg lemma is_local_extr.comp_mono (hf : is_local_extr f a) {g : β → γ} (hg : monotone g) : is_local_extr (g ∘ f) a := hf.comp_mono hg lemma is_local_min.comp_antimono (hf : is_local_min f a) {g : β → γ} (hg : ∀ ⦃x y⦄, x ≤ y → g y ≤ g x) : is_local_max (g ∘ f) a := hf.comp_antimono hg lemma is_local_max.comp_antimono (hf : is_local_max f a) {g : β → γ} (hg : ∀ ⦃x y⦄, x ≤ y → g y ≤ g x) : is_local_min (g ∘ f) a := hf.comp_antimono hg lemma is_local_extr.comp_antimono (hf : is_local_extr f a) {g : β → γ} (hg : ∀ ⦃x y⦄, x ≤ y → g y ≤ g x) : is_local_extr (g ∘ f) a := hf.comp_antimono hg lemma is_local_min_on.comp_mono (hf : is_local_min_on f s a) {g : β → γ} (hg : monotone g) : is_local_min_on (g ∘ f) s a := hf.comp_mono hg lemma is_local_max_on.comp_mono (hf : is_local_max_on f s a) {g : β → γ} (hg : monotone g) : is_local_max_on (g ∘ f) s a := hf.comp_mono hg lemma is_local_extr_on.comp_mono (hf : is_local_extr_on f s a) {g : β → γ} (hg : monotone g) : is_local_extr_on (g ∘ f) s a := hf.comp_mono hg lemma is_local_min_on.comp_antimono (hf : is_local_min_on f s a) {g : β → γ} (hg : ∀ ⦃x y⦄, x ≤ y → g y ≤ g x) : is_local_max_on (g ∘ f) s a := hf.comp_antimono hg lemma is_local_max_on.comp_antimono (hf : is_local_max_on f s a) {g : β → γ} (hg : ∀ ⦃x y⦄, x ≤ y → g y ≤ g x) : is_local_min_on (g ∘ f) s a := hf.comp_antimono hg lemma is_local_extr_on.comp_antimono (hf : is_local_extr_on f s a) {g : β → γ} (hg : ∀ ⦃x y⦄, x ≤ y → g y ≤ g x) : is_local_extr_on (g ∘ f) s a := hf.comp_antimono hg lemma is_local_min.bicomp_mono [preorder δ] {op : β → γ → δ} (hop : ((≤) ⇒ (≤) ⇒ (≤)) op op) (hf : is_local_min f a) {g : α → γ} (hg : is_local_min g a) : is_local_min (λ x, op (f x) (g x)) a := hf.bicomp_mono hop hg lemma is_local_max.bicomp_mono [preorder δ] {op : β → γ → δ} (hop : ((≤) ⇒ (≤) ⇒ (≤)) op op) (hf : is_local_max f a) {g : α → γ} (hg : is_local_max g a) : is_local_max (λ x, op (f x) (g x)) a := hf.bicomp_mono hop hg -- No `extr` version because we need `hf` and `hg` to be of the same kind lemma is_local_min_on.bicomp_mono [preorder δ] {op : β → γ → δ} (hop : ((≤) ⇒ (≤) ⇒ (≤)) op op) (hf : is_local_min_on f s a) {g : α → γ} (hg : is_local_min_on g s a) : is_local_min_on (λ x, op (f x) (g x)) s a := hf.bicomp_mono hop hg lemma is_local_max_on.bicomp_mono [preorder δ] {op : β → γ → δ} (hop : ((≤) ⇒ (≤) ⇒ (≤)) op op) (hf : is_local_max_on f s a) {g : α → γ} (hg : is_local_max_on g s a) : is_local_max_on (λ x, op (f x) (g x)) s a := hf.bicomp_mono hop hg /-! ### Composition with `continuous_at` -/ lemma is_local_min.comp_continuous [topological_space δ] {g : δ → α} {b : δ} (hf : is_local_min f (g b)) (hg : continuous_at g b) : is_local_min (f ∘ g) b := hg hf lemma is_local_max.comp_continuous [topological_space δ] {g : δ → α} {b : δ} (hf : is_local_max f (g b)) (hg : continuous_at g b) : is_local_max (f ∘ g) b := hg hf lemma is_local_extr.comp_continuous [topological_space δ] {g : δ → α} {b : δ} (hf : is_local_extr f (g b)) (hg : continuous_at g b) : is_local_extr (f ∘ g) b := hf.comp_tendsto hg lemma is_local_min.comp_continuous_on [topological_space δ] {s : set δ} {g : δ → α} {b : δ} (hf : is_local_min f (g b)) (hg : continuous_on g s) (hb : b ∈ s) : is_local_min_on (f ∘ g) s b := hf.comp_tendsto (hg b hb) lemma is_local_max.comp_continuous_on [topological_space δ] {s : set δ} {g : δ → α} {b : δ} (hf : is_local_max f (g b)) (hg : continuous_on g s) (hb : b ∈ s) : is_local_max_on (f ∘ g) s b := hf.comp_tendsto (hg b hb) lemma is_local_extr.comp_continuous_on [topological_space δ] {s : set δ} (g : δ → α) {b : δ} (hf : is_local_extr f (g b)) (hg : continuous_on g s) (hb : b ∈ s) : is_local_extr_on (f ∘ g) s b := hf.elim (λ hf, (hf.comp_continuous_on hg hb).is_extr) (λ hf, (is_local_max.comp_continuous_on hf hg hb).is_extr) end preorder /-! ### Pointwise addition -/ section ordered_comm_monoid variables [ordered_comm_monoid β] {f g : α → β} {a : α} {s : set α} {l : filter α} lemma is_local_min.add (hf : is_local_min f a) (hg : is_local_min g a) : is_local_min (λ x, f x + g x) a := hf.add hg lemma is_local_max.add (hf : is_local_max f a) (hg : is_local_max g a) : is_local_max (λ x, f x + g x) a := hf.add hg lemma is_local_min_on.add (hf : is_local_min_on f s a) (hg : is_local_min_on g s a) : is_local_min_on (λ x, f x + g x) s a := hf.add hg lemma is_local_max_on.add (hf : is_local_max_on f s a) (hg : is_local_max_on g s a) : is_local_max_on (λ x, f x + g x) s a := hf.add hg end ordered_comm_monoid /-! ### Pointwise negation and subtraction -/ section ordered_comm_group variables [ordered_comm_group β] {f g : α → β} {a : α} {s : set α} {l : filter α} lemma is_local_min.neg (hf : is_local_min f a) : is_local_max (λ x, -f x) a := hf.neg lemma is_local_max.neg (hf : is_local_max f a) : is_local_min (λ x, -f x) a := hf.neg lemma is_local_extr.neg (hf : is_local_extr f a) : is_local_extr (λ x, -f x) a := hf.neg lemma is_local_min_on.neg (hf : is_local_min_on f s a) : is_local_max_on (λ x, -f x) s a := hf.neg lemma is_local_max_on.neg (hf : is_local_max_on f s a) : is_local_min_on (λ x, -f x) s a := hf.neg lemma is_local_extr_on.neg (hf : is_local_extr_on f s a) : is_local_extr_on (λ x, -f x) s a := hf.neg lemma is_local_min.sub (hf : is_local_min f a) (hg : is_local_max g a) : is_local_min (λ x, f x - g x) a := hf.sub hg lemma is_local_max.sub (hf : is_local_max f a) (hg : is_local_min g a) : is_local_max (λ x, f x - g x) a := hf.sub hg lemma is_local_min_on.sub (hf : is_local_min_on f s a) (hg : is_local_max_on g s a) : is_local_min_on (λ x, f x - g x) s a := hf.sub hg lemma is_local_max_on.sub (hf : is_local_max_on f s a) (hg : is_local_min_on g s a) : is_local_max_on (λ x, f x - g x) s a := hf.sub hg end ordered_comm_group /-! ### Pointwise `sup`/`inf` -/ section semilattice_sup variables [semilattice_sup β] {f g : α → β} {a : α} {s : set α} {l : filter α} lemma is_local_min.sup (hf : is_local_min f a) (hg : is_local_min g a) : is_local_min (λ x, f x ⊔ g x) a := hf.sup hg lemma is_local_max.sup (hf : is_local_max f a) (hg : is_local_max g a) : is_local_max (λ x, f x ⊔ g x) a := hf.sup hg lemma is_local_min_on.sup (hf : is_local_min_on f s a) (hg : is_local_min_on g s a) : is_local_min_on (λ x, f x ⊔ g x) s a := hf.sup hg lemma is_local_max_on.sup (hf : is_local_max_on f s a) (hg : is_local_max_on g s a) : is_local_max_on (λ x, f x ⊔ g x) s a := hf.sup hg end semilattice_sup section semilattice_inf variables [semilattice_inf β] {f g : α → β} {a : α} {s : set α} {l : filter α} lemma is_local_min.inf (hf : is_local_min f a) (hg : is_local_min g a) : is_local_min (λ x, f x ⊓ g x) a := hf.inf hg lemma is_local_max.inf (hf : is_local_max f a) (hg : is_local_max g a) : is_local_max (λ x, f x ⊓ g x) a := hf.inf hg lemma is_local_min_on.inf (hf : is_local_min_on f s a) (hg : is_local_min_on g s a) : is_local_min_on (λ x, f x ⊓ g x) s a := hf.inf hg lemma is_local_max_on.inf (hf : is_local_max_on f s a) (hg : is_local_max_on g s a) : is_local_max_on (λ x, f x ⊓ g x) s a := hf.inf hg end semilattice_inf /-! ### Pointwise `min`/`max` -/ section decidable_linear_order variables [decidable_linear_order β] {f g : α → β} {a : α} {s : set α} {l : filter α} lemma is_local_min.min (hf : is_local_min f a) (hg : is_local_min g a) : is_local_min (λ x, min (f x) (g x)) a := hf.min hg lemma is_local_max.min (hf : is_local_max f a) (hg : is_local_max g a) : is_local_max (λ x, min (f x) (g x)) a := hf.min hg lemma is_local_min_on.min (hf : is_local_min_on f s a) (hg : is_local_min_on g s a) : is_local_min_on (λ x, min (f x) (g x)) s a := hf.min hg lemma is_local_max_on.min (hf : is_local_max_on f s a) (hg : is_local_max_on g s a) : is_local_max_on (λ x, min (f x) (g x)) s a := hf.min hg lemma is_local_min.max (hf : is_local_min f a) (hg : is_local_min g a) : is_local_min (λ x, max (f x) (g x)) a := hf.max hg lemma is_local_max.max (hf : is_local_max f a) (hg : is_local_max g a) : is_local_max (λ x, max (f x) (g x)) a := hf.max hg lemma is_local_min_on.max (hf : is_local_min_on f s a) (hg : is_local_min_on g s a) : is_local_min_on (λ x, max (f x) (g x)) s a := hf.max hg lemma is_local_max_on.max (hf : is_local_max_on f s a) (hg : is_local_max_on g s a) : is_local_max_on (λ x, max (f x) (g x)) s a := hf.max hg end decidable_linear_order
43fbe219bbe610342ebcfa72efd3b8b9a0b27661
a4673261e60b025e2c8c825dfa4ab9108246c32e
/tests/lean/server/init_exit.lean
93a946155d58553060aaa566f81a366c472e0b25
[ "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
101
lean
#lang lean4 import Lean.Server #eval Lean.Server.Test.runWithInputFile "./init_exit_client.log" none
faaa8cf72da76ba9c8bfce66e06c8c829a3e8ab0
bae21755a4a03bbe0a5c22e258db8633407711ad
/library/init/data/array/basic.lean
6aa5e5b71f73f3074fed4382f2ab7a26a1138f62
[ "Apache-2.0" ]
permissive
nor-code/lean
f437357a8f85db0f06f186fa50fcb1bc75f6b122
aa306af3d7c47de3c7937c98d3aa919eb8da6f34
refs/heads/master
1,662,613,329,886
1,586,696,014,000
1,586,696,014,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,710
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Mario Carneiro -/ prelude import init.data.nat init.data.bool init.ite_simp universes u v w /-- In the VM, d_array is implemented as a persistent array. -/ structure d_array (n : nat) (α : fin n → Type u) := (data : Π i : fin n, α i) namespace d_array variables {n : nat} {α : fin n → Type u} {β : Type v} /-- The empty array. -/ def nil {α} : d_array 0 α := {data := λ ⟨x, h⟩, absurd h (nat.not_lt_zero x)} /-- `read a i` reads the `i`th member of `a`. Has builtin VM implementation. -/ def read (a : d_array n α) (i : fin n) : α i := a.data i /-- `write a i v` sets the `i`th member of `a` to be `v`. Has builtin VM implementation. -/ def write (a : d_array n α) (i : fin n) (v : α i) : d_array n α := {data := λ j, if h : i = j then eq.rec_on h v else a.read j} def iterate_aux (a : d_array n α) (f : Π i : fin n, α i → β → β) : Π (i : nat), i ≤ n → β → β | 0 h b := b | (j+1) h b := let i : fin n := ⟨j, h⟩ in f i (a.read i) (iterate_aux j (le_of_lt h) b) /-- Fold over the elements of the given array in ascending order. Has builtin VM implementation. -/ def iterate (a : d_array n α) (b : β) (f : Π i : fin n, α i → β → β) : β := iterate_aux a f n (le_refl _) b /-- Map the array. Has builtin VM implementation. -/ def foreach (a : d_array n α) (f : Π i : fin n, α i → α i) : d_array n α := iterate a a $ λ i v a', a'.write i (f i v) def map (f : Π i : fin n, α i → α i) (a : d_array n α) : d_array n α := foreach a f def map₂ (f : Π i : fin n, α i → α i → α i) (a b : d_array n α) : d_array n α := foreach b (λ i, f i (a.read i)) def foldl (a : d_array n α) (b : β) (f : Π i : fin n, α i → β → β) : β := iterate a b f def rev_iterate_aux (a : d_array n α) (f : Π i : fin n, α i → β → β) : Π (i : nat), i ≤ n → β → β | 0 h b := b | (j+1) h b := let i : fin n := ⟨j, h⟩ in rev_iterate_aux j (le_of_lt h) (f i (a.read i) b) def rev_iterate (a : d_array n α) (b : β) (f : Π i : fin n, α i → β → β) : β := rev_iterate_aux a f n (le_refl _) b @[simp] lemma read_write (a : d_array n α) (i : fin n) (v : α i) : read (write a i v) i = v := by simp [read, write] @[simp] lemma read_write_of_ne (a : d_array n α) {i j : fin n} (v : α i) : i ≠ j → read (write a i v) j = read a j := by intro h; simp [read, write, h] protected lemma ext {a b : d_array n α} (h : ∀ i, read a i = read b i) : a = b := by cases a; cases b; congr; exact funext h protected lemma ext' {a b : d_array n α} (h : ∀ (i : nat) (h : i < n), read a ⟨i, h⟩ = read b ⟨i, h⟩) : a = b := begin cases a, cases b, congr, funext i, cases i, apply h end protected def beq_aux [∀ i, decidable_eq (α i)] (a b : d_array n α) : Π (i : nat), i ≤ n → bool | 0 h := tt | (i+1) h := if a.read ⟨i, h⟩ = b.read ⟨i, h⟩ then beq_aux i (le_of_lt h) else ff /-- Boolean element-wise equality check. -/ protected def beq [∀ i, decidable_eq (α i)] (a b : d_array n α) : bool := d_array.beq_aux a b n (le_refl _) lemma of_beq_aux_eq_tt [∀ i, decidable_eq (α i)] {a b : d_array n α} : ∀ (i : nat) (h : i ≤ n), d_array.beq_aux a b i h = tt → ∀ (j : nat) (h' : j < i), a.read ⟨j, lt_of_lt_of_le h' h⟩ = b.read ⟨j, lt_of_lt_of_le h' h⟩ | 0 h₁ h₂ j h₃ := absurd h₃ (nat.not_lt_zero _) | (i+1) h₁ h₂ j h₃ := begin have h₂' : read a ⟨i, h₁⟩ = read b ⟨i, h₁⟩ ∧ d_array.beq_aux a b i _ = tt, {simp [d_array.beq_aux] at h₂, assumption}, have h₁' : i ≤ n, from le_of_lt h₁, have ih : ∀ (j : nat) (h' : j < i), a.read ⟨j, lt_of_lt_of_le h' h₁'⟩ = b.read ⟨j, lt_of_lt_of_le h' h₁'⟩, from of_beq_aux_eq_tt i h₁' h₂'.2, by_cases hji : j = i, { subst hji, exact h₂'.1 }, { have j_lt_i : j < i, from lt_of_le_of_ne (nat.le_of_lt_succ h₃) hji, exact ih j j_lt_i } end lemma of_beq_eq_tt [∀ i, decidable_eq (α i)] {a b : d_array n α} : d_array.beq a b = tt → a = b := begin unfold d_array.beq, intro h, have : ∀ (j : nat) (h : j < n), a.read ⟨j, h⟩ = b.read ⟨j, h⟩, from of_beq_aux_eq_tt n (le_refl _) h, apply d_array.ext' this end lemma of_beq_aux_eq_ff [∀ i, decidable_eq (α i)] {a b : d_array n α} : ∀ (i : nat) (h : i ≤ n), d_array.beq_aux a b i h = ff → ∃ (j : nat) (h' : j < i), a.read ⟨j, lt_of_lt_of_le h' h⟩ ≠ b.read ⟨j, lt_of_lt_of_le h' h⟩ | 0 h₁ h₂ := begin simp [d_array.beq_aux] at h₂, contradiction end | (i+1) h₁ h₂ := begin have h₂' : read a ⟨i, h₁⟩ ≠ read b ⟨i, h₁⟩ ∨ d_array.beq_aux a b i _ = ff, {simp [d_array.beq_aux] at h₂, assumption}, cases h₂' with h h, { existsi i, existsi (nat.lt_succ_self _), exact h }, { have h₁' : i ≤ n, from le_of_lt h₁, have ih : ∃ (j : nat) (h' : j < i), a.read ⟨j, lt_of_lt_of_le h' h₁'⟩ ≠ b.read ⟨j, lt_of_lt_of_le h' h₁'⟩, from of_beq_aux_eq_ff i h₁' h, cases ih with j ih, cases ih with h' ih, existsi j, existsi (nat.lt_succ_of_lt h'), exact ih } end lemma of_beq_eq_ff [∀ i, decidable_eq (α i)] {a b : d_array n α} : d_array.beq a b = ff → a ≠ b := begin unfold d_array.beq, intros h hne, have : ∃ (j : nat) (h' : j < n), a.read ⟨j, h'⟩ ≠ b.read ⟨j, h'⟩, from of_beq_aux_eq_ff n (le_refl _) h, cases this with j this, cases this with h' this, subst hne, contradiction end instance [∀ i, decidable_eq (α i)] : decidable_eq (d_array n α) := λ a b, if h : d_array.beq a b = tt then is_true (of_beq_eq_tt h) else is_false (of_beq_eq_ff (eq_ff_of_not_eq_tt h)) end d_array /-- A non-dependent array (see `d_array`). Implemented in the VM as a persistent array. -/ def array (n : nat) (α : Type u) : Type u := d_array n (λ _, α) /-- `mk_array n v` creates a new array of length `n` where each element is `v`. Has builtin VM implementation. -/ def mk_array {α} (n) (v : α) : array n α := {data := λ _, v} namespace array variables {n : nat} {α : Type u} {β : Type v} def nil {α} : array 0 α := d_array.nil @[inline] def read (a : array n α) (i : fin n) : α := d_array.read a i @[inline] def write (a : array n α) (i : fin n) (v : α) : array n α := d_array.write a i v /-- Fold array starting from 0, folder function includes an index argument. -/ @[inline] def iterate (a : array n α) (b : β) (f : fin n → α → β → β) : β := d_array.iterate a b f /-- Map each element of the given array with an index argument. -/ @[inline] def foreach (a : array n α) (f : fin n → α → α) : array n α := d_array.foreach a f @[inline] def map (f : α → α) (a : array n α) : array n α := foreach a (λ _, f) @[inline] def map₂ (f : α → α → α) (a b : array n α) : array n α := foreach b (λ i, f (a.read i)) @[inline] def foldl (a : array n α) (b : β) (f : α → β → β) : β := iterate a b (λ _, f) def rev_list (a : array n α) : list α := a.foldl [] (::) def rev_iterate (a : array n α) (b : β) (f : fin n → α → β → β) : β := d_array.rev_iterate a b f def rev_foldl (a : array n α) (b : β) (f : α → β → β) : β := rev_iterate a b (λ _, f) def to_list (a : array n α) : list α := a.rev_foldl [] (::) lemma push_back_idx {j n} (h₁ : j < n + 1) (h₂ : j ≠ n) : j < n := nat.lt_of_le_and_ne (nat.le_of_lt_succ h₁) h₂ /-- `push_back a v` pushes value `v` to the end of the array. Has builtin VM implementation. -/ def push_back (a : array n α) (v : α) : array (n+1) α := {data := λ ⟨j, h₁⟩, if h₂ : j = n then v else a.read ⟨j, push_back_idx h₁ h₂⟩} lemma pop_back_idx {j n} (h : j < n) : j < n + 1 := nat.lt.step h /-- Discard _last_ element in the array. Has builtin VM implementation. -/ def pop_back (a : array (n+1) α) : array n α := {data := λ ⟨j, h⟩, a.read ⟨j, pop_back_idx h⟩} protected def mem (v : α) (a : array n α) : Prop := ∃ i : fin n, read a i = v instance : has_mem α (array n α) := ⟨array.mem⟩ theorem read_mem (a : array n α) (i) : read a i ∈ a := exists.intro i rfl instance [has_repr α] : has_repr (array n α) := ⟨repr ∘ to_list⟩ meta instance [has_to_format α] : has_to_format (array n α) := ⟨to_fmt ∘ to_list⟩ meta instance [has_to_tactic_format α] : has_to_tactic_format (array n α) := ⟨tactic.pp ∘ to_list⟩ @[simp] lemma read_write (a : array n α) (i : fin n) (v : α) : read (write a i v) i = v := d_array.read_write a i v @[simp] lemma read_write_of_ne (a : array n α) {i j : fin n} (v : α) : i ≠ j → read (write a i v) j = read a j := d_array.read_write_of_ne a v def read' [inhabited β] (a : array n β) (i : nat) : β := if h : i < n then a.read ⟨i,h⟩ else default β def write' (a : array n α) (i : nat) (v : α) : array n α := if h : i < n then a.write ⟨i, h⟩ v else a lemma read_eq_read' [inhabited α] (a : array n α) {i : nat} (h : i < n) : read a ⟨i, h⟩ = read' a i := by simp [read', h] lemma write_eq_write' (a : array n α) {i : nat} (h : i < n) (v : α) : write a ⟨i, h⟩ v = write' a i v := by simp [write', h] protected lemma ext {a b : array n α} (h : ∀ i, read a i = read b i) : a = b := d_array.ext h protected lemma ext' {a b : array n α} (h : ∀ (i : nat) (h : i < n), read a ⟨i, h⟩ = read b ⟨i, h⟩) : a = b := d_array.ext' h instance [decidable_eq α] : decidable_eq (array n α) := begin unfold array, apply_instance end end array
5d3e6cdfc404bbc216427832a99ec059be658ef7
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/field_theory/subfield_auto.lean
f1c065e55e11351c283ffb0af10219afe127c1fc
[]
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
27,431
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors : Anne Baanen -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.algebra.basic import Mathlib.PostPort universes u l u_1 v w namespace Mathlib /-! # Subfields Let `K` be a field. This file defines the "bundled" subfield type `subfield K`, a type whose terms correspond to subfields of `K`. This is the preferred way to talk about subfields in mathlib. Unbundled subfields (`s : set K` and `is_subfield s`) are not in this file, and they will ultimately be deprecated. We prove that subfields 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 `subfield R`, sending a subset of `R` to the subfield it generates, and prove that it is a Galois insertion. ## Main definitions Notation used here: `(K : Type u) [field K] (L : Type u) [field L] (f g : K →+* L)` `(A : subfield K) (B : subfield L) (s : set K)` * `subfield R` : the type of subfields of a ring `R`. * `instance : complete_lattice (subfield R)` : the complete lattice structure on the subfields. * `subfield.closure` : subfield closure of a set, i.e., the smallest subfield that includes the set. * `subfield.gi` : `closure : set M → subfield M` and coercion `coe : subfield M → set M` form a `galois_insertion`. * `comap f B : subfield K` : the preimage of a subfield `B` along the ring homomorphism `f` * `map f A : subfield L` : the image of a subfield `A` along the ring homomorphism `f`. * `prod A B : subfield (K × L)` : the product of subfields * `f.field_range : subfield B` : the range of the ring homomorphism `f`. * `eq_locus_field f g : subfield K` : given ring homomorphisms `f g : K →+* R`, the subfield of `K` where `f x = g x` ## Implementation notes A subfield is implemented as a subring which is is closed under `⁻¹`. Lattice inclusion (e.g. `≤` and `⊓`) is used rather than set notation (`⊆` and `∩`), although `∈` is defined as membership of a subfield's underlying set. ## Tags subfield, subfields -/ /-- `subfield R` is the type of subfields of `R`. A subfield 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 subfield (K : Type u) [field K] extends subring K where inv_mem' : ∀ (x : K), x ∈ carrier → x⁻¹ ∈ carrier /-- Reinterpret a `subfield` as a `subring`. -/ namespace subfield /-- The underlying `add_subgroup` of a subfield. -/ def to_add_subgroup {K : Type u} [field K] (s : subfield K) : add_subgroup K := add_subgroup.mk (add_subgroup.carrier (subring.to_add_subgroup (to_subring s))) sorry sorry sorry /-- The underlying submonoid of a subfield. -/ def to_submonoid {K : Type u} [field K] (s : subfield K) : submonoid K := submonoid.mk (submonoid.carrier (subring.to_submonoid (to_subring s))) sorry sorry protected instance set.has_coe {K : Type u} [field K] : has_coe (subfield K) (set K) := has_coe.mk carrier @[simp] theorem coe_to_subring {K : Type u} [field K] (s : subfield K) : ↑(to_subring s) = ↑s := rfl protected instance has_coe_to_sort {K : Type u} [field K] : has_coe_to_sort (subfield K) := has_coe_to_sort.mk (Type u) fun (S : subfield K) => ↥(carrier S) protected instance has_mem {K : Type u} [field K] : has_mem K (subfield K) := has_mem.mk fun (m : K) (S : subfield K) => m ∈ ↑S @[simp] theorem mem_mk {K : Type u} [field K] (s : set K) (ho : 1 ∈ s) (hm : ∀ {a b : K}, a ∈ s → b ∈ s → a * b ∈ s) (hz : 0 ∈ s) (ha : ∀ {a b : K}, a ∈ s → b ∈ s → a + b ∈ s) (hn : ∀ {x : K}, x ∈ s → -x ∈ s) (hi : ∀ (x : K), x ∈ s → x⁻¹ ∈ s) (x : K) : x ∈ mk s ho hm hz ha hn hi ↔ x ∈ s := iff.rfl @[simp] theorem mem_to_subring {K : Type u} [field K] (s : subfield K) (x : K) : x ∈ to_subring s ↔ x ∈ s := iff.rfl end subfield protected theorem subfield.exists {K : Type u} [field K] {s : subfield K} {p : ↥s → Prop} : (∃ (x : ↥s), p x) ↔ ∃ (x : K), ∃ (H : x ∈ s), p { val := x, property := H } := set_coe.exists protected theorem subfield.forall {K : Type u} [field K] {s : subfield K} {p : ↥s → Prop} : (∀ (x : ↥s), p x) ↔ ∀ (x : K) (H : x ∈ s), p { val := x, property := H } := set_coe.forall /-- A `subring` containing inverses is a `subfield`. -/ def subring.to_subfield {K : Type u} [field K] (s : subring K) (hinv : ∀ (x : K), x ∈ s → x⁻¹ ∈ s) : subfield K := subfield.mk (subring.carrier s) sorry sorry sorry sorry sorry hinv namespace subfield /-- Two subfields are equal if the underlying subsets are equal. -/ theorem ext' {K : Type u} [field K] {s : subfield K} {t : subfield K} (h : ↑s = ↑t) : s = t := sorry /-- Two subfields are equal if and only if the underlying subsets are equal. -/ protected theorem ext'_iff {K : Type u} [field K] {s : subfield K} {t : subfield K} : s = t ↔ ↑s = ↑t := { mp := fun (h : s = t) => h ▸ rfl, mpr := fun (h : ↑s = ↑t) => ext' h } /-- Two subfields are equal if they have the same elements. -/ theorem ext {K : Type u} [field K] {S : subfield K} {T : subfield K} (h : ∀ (x : K), x ∈ S ↔ x ∈ T) : S = T := ext' (set.ext h) /-- A subfield contains the ring's 1. -/ theorem one_mem {K : Type u} [field K] (s : subfield K) : 1 ∈ s := one_mem' s /-- A subfield contains the ring's 0. -/ theorem zero_mem {K : Type u} [field K] (s : subfield K) : 0 ∈ s := zero_mem' s /-- A subfield is closed under multiplication. -/ theorem mul_mem {K : Type u} [field K] (s : subfield K) {x : K} {y : K} : x ∈ s → y ∈ s → x * y ∈ s := mul_mem' s /-- A subfield is closed under addition. -/ theorem add_mem {K : Type u} [field K] (s : subfield K) {x : K} {y : K} : x ∈ s → y ∈ s → x + y ∈ s := add_mem' s /-- A subfield is closed under negation. -/ theorem neg_mem {K : Type u} [field K] (s : subfield K) {x : K} : x ∈ s → -x ∈ s := neg_mem' s /-- A subfield is closed under subtraction. -/ theorem sub_mem {K : Type u} [field K] (s : subfield K) {x : K} {y : K} : x ∈ s → y ∈ s → x - y ∈ s := subring.sub_mem (to_subring s) /-- A subfield is closed under inverses. -/ theorem inv_mem {K : Type u} [field K] (s : subfield K) {x : K} : x ∈ s → x⁻¹ ∈ s := inv_mem' s /-- A subfield is closed under division. -/ theorem div_mem {K : Type u} [field K] (s : subfield K) {x : K} {y : K} (hx : x ∈ s) (hy : y ∈ s) : x / y ∈ s := eq.mpr (id (Eq._oldrec (Eq.refl (x / y ∈ s)) (div_eq_mul_inv x y))) (mul_mem s hx (inv_mem s hy)) /-- Product of a list of elements in a subfield is in the subfield. -/ theorem list_prod_mem {K : Type u} [field K] (s : subfield K) {l : List K} : (∀ (x : K), x ∈ l → x ∈ s) → list.prod l ∈ s := submonoid.list_prod_mem (to_submonoid s) /-- Sum of a list of elements in a subfield is in the subfield. -/ theorem list_sum_mem {K : Type u} [field K] (s : subfield K) {l : List K} : (∀ (x : K), x ∈ l → x ∈ s) → list.sum l ∈ s := add_subgroup.list_sum_mem (to_add_subgroup s) /-- Product of a multiset of elements in a subfield is in the subfield. -/ theorem multiset_prod_mem {K : Type u} [field K] (s : subfield K) (m : multiset K) : (∀ (a : K), a ∈ m → a ∈ s) → multiset.prod m ∈ s := submonoid.multiset_prod_mem (to_submonoid s) m /-- Sum of a multiset of elements in a `subfield` is in the `subfield`. -/ theorem multiset_sum_mem {K : Type u} [field K] (s : subfield K) (m : multiset K) : (∀ (a : K), a ∈ m → a ∈ s) → multiset.sum m ∈ s := add_subgroup.multiset_sum_mem (to_add_subgroup s) m /-- Product of elements of a subfield indexed by a `finset` is in the subfield. -/ theorem prod_mem {K : Type u} [field K] (s : subfield K) {ι : Type u_1} {t : finset ι} {f : ι → K} (h : ∀ (c : ι), c ∈ t → f c ∈ s) : (finset.prod t fun (i : ι) => f i) ∈ s := submonoid.prod_mem (to_submonoid s) h /-- Sum of elements in a `subfield` indexed by a `finset` is in the `subfield`. -/ theorem sum_mem {K : Type u} [field K] (s : subfield K) {ι : Type u_1} {t : finset ι} {f : ι → K} (h : ∀ (c : ι), c ∈ t → f c ∈ s) : (finset.sum t fun (i : ι) => f i) ∈ s := add_subgroup.sum_mem (to_add_subgroup s) h theorem pow_mem {K : Type u} [field K] (s : subfield K) {x : K} (hx : x ∈ s) (n : ℕ) : x ^ n ∈ s := submonoid.pow_mem (to_submonoid s) hx n theorem gsmul_mem {K : Type u} [field K] (s : subfield K) {x : K} (hx : x ∈ s) (n : ℤ) : n •ℤ x ∈ s := add_subgroup.gsmul_mem (to_add_subgroup s) hx n theorem coe_int_mem {K : Type u} [field K] (s : subfield K) (n : ℤ) : ↑n ∈ s := sorry /-- A subfield inherits a field structure -/ protected instance to_field {K : Type u} [field K] (s : subfield K) : field ↥s := field.mk integral_domain.add sorry integral_domain.zero sorry sorry integral_domain.neg integral_domain.sub sorry sorry integral_domain.mul sorry integral_domain.one sorry sorry sorry sorry sorry (fun (x : ↥s) => { val := ↑x⁻¹, property := sorry }) sorry sorry sorry @[simp] theorem coe_add {K : Type u} [field K] (s : subfield K) (x : ↥s) (y : ↥s) : ↑(x + y) = ↑x + ↑y := rfl @[simp] theorem coe_neg {K : Type u} [field K] (s : subfield K) (x : ↥s) : ↑(-x) = -↑x := rfl @[simp] theorem coe_mul {K : Type u} [field K] (s : subfield K) (x : ↥s) (y : ↥s) : ↑(x * y) = ↑x * ↑y := rfl @[simp] theorem coe_inv {K : Type u} [field K] (s : subfield K) (x : ↥s) : ↑(x⁻¹) = (↑x⁻¹) := rfl @[simp] theorem coe_zero {K : Type u} [field K] (s : subfield K) : ↑0 = 0 := rfl @[simp] theorem coe_one {K : Type u} [field K] (s : subfield K) : ↑1 = 1 := rfl /-- The embedding from a subfield of the field `K` to `K`. -/ def subtype {K : Type u} [field K] (s : subfield K) : ↥s →+* K := ring_hom.mk coe sorry sorry sorry sorry protected instance to_algebra {K : Type u} [field K] (s : subfield K) : algebra (↥s) K := ring_hom.to_algebra (subtype s) @[simp] theorem coe_subtype {K : Type u} [field K] (s : subfield K) : ⇑(subtype s) = coe := rfl /-! # Partial order -/ protected instance partial_order {K : Type u} [field K] : partial_order (subfield K) := partial_order.mk (fun (s t : subfield K) => ∀ {x : K}, x ∈ s → x ∈ t) partial_order.lt sorry sorry sorry theorem le_def {K : Type u} [field K] {s : subfield K} {t : subfield K} : s ≤ t ↔ ∀ {x : K}, x ∈ s → x ∈ t := iff.rfl @[simp] theorem coe_subset_coe {K : Type u} [field K] {s : subfield K} {t : subfield K} : ↑s ⊆ ↑t ↔ s ≤ t := iff.rfl @[simp] theorem coe_ssubset_coe {K : Type u} [field K] {s : subfield K} {t : subfield K} : ↑s ⊂ ↑t ↔ s < t := iff.rfl @[simp] theorem mem_coe {K : Type u} [field K] {s : subfield K} {m : K} : m ∈ ↑s ↔ m ∈ s := iff.rfl @[simp] theorem coe_coe {K : Type u} [field K] (s : subfield K) : ↥↑s = ↥s := rfl @[simp] theorem mem_to_submonoid {K : Type u} [field K] {s : subfield K} {x : K} : x ∈ to_submonoid s ↔ x ∈ s := iff.rfl @[simp] theorem coe_to_submonoid {K : Type u} [field K] (s : subfield K) : ↑(to_submonoid s) = ↑s := rfl @[simp] theorem mem_to_add_subgroup {K : Type u} [field K] {s : subfield K} {x : K} : x ∈ to_add_subgroup s ↔ x ∈ s := iff.rfl @[simp] theorem coe_to_add_subgroup {K : Type u} [field K] (s : subfield K) : ↑(to_add_subgroup s) = ↑s := rfl /-! # top -/ /-- The subfield of `K` containing all elements of `K`. -/ protected instance has_top {K : Type u} [field K] : has_top (subfield K) := has_top.mk (mk (subring.carrier ⊤) sorry sorry sorry sorry sorry sorry) protected instance inhabited {K : Type u} [field K] : Inhabited (subfield K) := { default := ⊤ } @[simp] theorem mem_top {K : Type u} [field K] (x : K) : x ∈ ⊤ := set.mem_univ x @[simp] theorem coe_top {K : Type u} [field K] : ↑⊤ = set.univ := rfl /-! # comap -/ /-- The preimage of a subfield along a ring homomorphism is a subfield. -/ def comap {K : Type u} {L : Type v} [field K] [field L] (f : K →+* L) (s : subfield L) : subfield K := mk (subring.carrier (subring.comap f (to_subring s))) sorry sorry sorry sorry sorry sorry @[simp] theorem coe_comap {K : Type u} {L : Type v} [field K] [field L] (f : K →+* L) (s : subfield L) : ↑(comap f s) = ⇑f ⁻¹' ↑s := rfl @[simp] theorem mem_comap {K : Type u} {L : Type v} [field K] [field L] {s : subfield L} {f : K →+* L} {x : K} : x ∈ comap f s ↔ coe_fn f x ∈ s := iff.rfl theorem comap_comap {K : Type u} {L : Type v} {M : Type w} [field K] [field L] [field M] (s : subfield M) (g : L →+* M) (f : K →+* L) : comap f (comap g s) = comap (ring_hom.comp g f) s := rfl /-! # map -/ /-- The image of a subfield along a ring homomorphism is a subfield. -/ def map {K : Type u} {L : Type v} [field K] [field L] (f : K →+* L) (s : subfield K) : subfield L := mk (subring.carrier (subring.map f (to_subring s))) sorry sorry sorry sorry sorry sorry @[simp] theorem coe_map {K : Type u} {L : Type v} [field K] [field L] (s : subfield K) (f : K →+* L) : ↑(map f s) = ⇑f '' ↑s := rfl @[simp] theorem mem_map {K : Type u} {L : Type v} [field K] [field L] {f : K →+* L} {s : subfield K} {y : L} : y ∈ map f s ↔ ∃ (x : K), ∃ (H : x ∈ s), coe_fn f x = y := set.mem_image_iff_bex theorem map_map {K : Type u} {L : Type v} {M : Type w} [field K] [field L] [field M] (s : subfield K) (g : L →+* M) (f : K →+* L) : map g (map f s) = map (ring_hom.comp g f) s := ext' (set.image_image (fun (a : L) => coe_fn g a) (fun (a : K) => coe_fn f a) (subring.carrier (to_subring s))) theorem map_le_iff_le_comap {K : Type u} {L : Type v} [field K] [field L] {f : K →+* L} {s : subfield K} {t : subfield L} : map f s ≤ t ↔ s ≤ comap f t := set.image_subset_iff theorem gc_map_comap {K : Type u} {L : Type v} [field K] [field L] (f : K →+* L) : galois_connection (map f) (comap f) := fun (S : subfield K) (T : subfield L) => map_le_iff_le_comap end subfield namespace ring_hom /-! # range -/ /-- The range of a ring homomorphism, as a subfield of the target. -/ def field_range {K : Type u} {L : Type v} [field K] [field L] (f : K →+* L) : subfield L := subfield.map f ⊤ @[simp] theorem coe_field_range {K : Type u} {L : Type v} [field K] [field L] (f : K →+* L) : ↑(field_range f) = set.range ⇑f := set.image_univ @[simp] theorem mem_field_range {K : Type u} {L : Type v} [field K] [field L] {f : K →+* L} {y : L} : y ∈ range f ↔ ∃ (x : K), coe_fn f x = y := sorry theorem map_field_range {K : Type u} {L : Type v} {M : Type w} [field K] [field L] [field M] (g : L →+* M) (f : K →+* L) : subfield.map g (field_range f) = field_range (comp g f) := subfield.map_map ⊤ g f end ring_hom namespace subfield /-! # inf -/ /-- The inf of two subfields is their intersection. -/ protected instance has_inf {K : Type u} [field K] : has_inf (subfield K) := has_inf.mk fun (s t : subfield K) => mk (subring.carrier (to_subring s ⊓ to_subring t)) sorry sorry sorry sorry sorry sorry @[simp] theorem coe_inf {K : Type u} [field K] (p : subfield K) (p' : subfield K) : ↑(p ⊓ p') = ↑p ∩ ↑p' := rfl @[simp] theorem mem_inf {K : Type u} [field K] {p : subfield K} {p' : subfield K} {x : K} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl protected instance has_Inf {K : Type u} [field K] : has_Inf (subfield K) := has_Inf.mk fun (S : set (subfield K)) => mk (subring.carrier (Inf (to_subring '' S))) sorry sorry sorry sorry sorry sorry @[simp] theorem coe_Inf {K : Type u} [field K] (S : set (subfield K)) : ↑(Inf S) = set.Inter fun (s : subfield K) => set.Inter fun (H : s ∈ S) => ↑s := sorry theorem mem_Inf {K : Type u} [field K] {S : set (subfield K)} {x : K} : x ∈ Inf S ↔ ∀ (p : subfield K), p ∈ S → x ∈ p := sorry @[simp] theorem Inf_to_subring {K : Type u} [field K] (s : set (subfield K)) : to_subring (Inf s) = infi fun (t : subfield K) => infi fun (H : t ∈ s) => to_subring t := sorry theorem is_glb_Inf {K : Type u} [field K] (S : set (subfield K)) : is_glb S (Inf S) := sorry /-- Subfields of a ring form a complete lattice. -/ protected instance complete_lattice {K : Type u} [field K] : complete_lattice (subfield K) := complete_lattice.mk complete_lattice.sup complete_lattice.le complete_lattice.lt sorry sorry sorry sorry sorry sorry has_inf.inf sorry sorry sorry ⊤ sorry complete_lattice.bot sorry complete_lattice.Sup complete_lattice.Inf sorry sorry sorry sorry /-! # subfield closure of a subset -/ /-- The `subfield` generated by a set. -/ def closure {K : Type u} [field K] (s : set K) : subfield K := mk (set_of fun (_x : K) => ∃ (x : K), ∃ (H : x ∈ subring.closure s), ∃ (y : K), ∃ (H : y ∈ subring.closure s), x / y = _x) sorry sorry sorry sorry sorry sorry theorem mem_closure_iff {K : Type u} [field K] {s : set K} {x : K} : x ∈ closure s ↔ ∃ (y : K), ∃ (H : y ∈ subring.closure s), ∃ (z : K), ∃ (H : z ∈ subring.closure s), y / z = x := iff.rfl theorem subring_closure_le {K : Type u} [field K] (s : set K) : subring.closure s ≤ to_subring (closure s) := fun (x : K) (hx : x ∈ subring.closure s) => Exists.intro x (Exists.intro hx (Exists.intro 1 (Exists.intro (subring.one_mem (subring.closure s)) (div_one x)))) /-- The subfield generated by a set includes the set. -/ @[simp] theorem subset_closure {K : Type u} [field K] {s : set K} : s ⊆ ↑(closure s) := set.subset.trans subring.subset_closure (subring_closure_le s) theorem mem_closure {K : Type u} [field K] {x : K} {s : set K} : x ∈ closure s ↔ ∀ (S : subfield K), s ⊆ ↑S → x ∈ S := sorry /-- A subfield `t` includes `closure s` if and only if it includes `s`. -/ @[simp] theorem closure_le {K : Type u} [field K] {s : set K} {t : subfield K} : closure s ≤ t ↔ s ⊆ ↑t := { mp := set.subset.trans subset_closure, mpr := fun (h : s ⊆ ↑t) (x : K) (hx : x ∈ closure s) => iff.mp mem_closure hx t h } /-- Subfield closure of a set is monotone in its argument: if `s ⊆ t`, then `closure s ≤ closure t`. -/ theorem closure_mono {K : Type u} [field K] {s : set K} {t : set K} (h : s ⊆ t) : closure s ≤ closure t := iff.mpr closure_le (set.subset.trans h subset_closure) theorem closure_eq_of_le {K : Type u} [field K] {s : set K} {t : subfield K} (h₁ : s ⊆ ↑t) (h₂ : t ≤ closure s) : closure s = t := le_antisymm (iff.mpr closure_le h₁) h₂ /-- An induction principle for closure membership. If `p` holds for `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`. -/ theorem closure_induction {K : Type u} [field K] {s : set K} {p : K → Prop} {x : K} (h : x ∈ closure s) (Hs : ∀ (x : K), x ∈ s → p x) (H1 : p 1) (Hadd : ∀ (x y : K), p x → p y → p (x + y)) (Hneg : ∀ (x : K), p x → p (-x)) (Hinv : ∀ (x : K), p x → p (x⁻¹)) (Hmul : ∀ (x y : K), p x → p y → p (x * y)) : p x := iff.mpr closure_le Hs x h /-- `closure` forms a Galois insertion with the coercion to set. -/ protected def gi (K : Type u) [field K] : galois_insertion closure coe := galois_insertion.mk (fun (s : set K) (_x : ↑(closure s) ≤ s) => closure s) sorry sorry sorry /-- Closure of a subfield `S` equals `S`. -/ theorem closure_eq {K : Type u} [field K] (s : subfield K) : closure ↑s = s := galois_insertion.l_u_eq (subfield.gi K) s @[simp] theorem closure_empty {K : Type u} [field K] : closure ∅ = ⊥ := galois_connection.l_bot (galois_insertion.gc (subfield.gi K)) @[simp] theorem closure_univ {K : Type u} [field K] : closure set.univ = ⊤ := coe_top ▸ closure_eq ⊤ theorem closure_union {K : Type u} [field K] (s : set K) (t : set K) : closure (s ∪ t) = closure s ⊔ closure t := galois_connection.l_sup (galois_insertion.gc (subfield.gi K)) theorem closure_Union {K : Type u} [field K] {ι : Sort u_1} (s : ι → set K) : closure (set.Union fun (i : ι) => s i) = supr fun (i : ι) => closure (s i) := galois_connection.l_supr (galois_insertion.gc (subfield.gi K)) theorem closure_sUnion {K : Type u} [field K] (s : set (set K)) : closure (⋃₀s) = supr fun (t : set K) => supr fun (H : t ∈ s) => closure t := galois_connection.l_Sup (galois_insertion.gc (subfield.gi K)) theorem map_sup {K : Type u} {L : Type v} [field K] [field L] (s : subfield K) (t : subfield K) (f : K →+* L) : map f (s ⊔ t) = map f s ⊔ map f t := galois_connection.l_sup (gc_map_comap f) theorem map_supr {K : Type u} {L : Type v} [field K] [field L] {ι : Sort u_1} (f : K →+* L) (s : ι → subfield K) : map f (supr s) = supr fun (i : ι) => map f (s i) := galois_connection.l_supr (gc_map_comap f) theorem comap_inf {K : Type u} {L : Type v} [field K] [field L] (s : subfield L) (t : subfield L) (f : K →+* L) : comap f (s ⊓ t) = comap f s ⊓ comap f t := galois_connection.u_inf (gc_map_comap f) theorem comap_infi {K : Type u} {L : Type v} [field K] [field L] {ι : Sort u_1} (f : K →+* L) (s : ι → subfield L) : comap f (infi s) = infi fun (i : ι) => comap f (s i) := galois_connection.u_infi (gc_map_comap f) @[simp] theorem map_bot {K : Type u} {L : Type v} [field K] [field L] (f : K →+* L) : map f ⊥ = ⊥ := galois_connection.l_bot (gc_map_comap f) @[simp] theorem comap_top {K : Type u} {L : Type v} [field K] [field L] (f : K →+* L) : comap f ⊤ = ⊤ := galois_connection.u_top (gc_map_comap f) /-- The underlying set of a non-empty directed Sup of subfields is just a union of the subfields. Note that this fails without the directedness assumption (the union of two subfields is typically not a subfield) -/ theorem mem_supr_of_directed {K : Type u} [field K] {ι : Sort u_1} [hι : Nonempty ι] {S : ι → subfield K} (hS : directed LessEq S) {x : K} : (x ∈ supr fun (i : ι) => S i) ↔ ∃ (i : ι), x ∈ S i := sorry theorem coe_supr_of_directed {K : Type u} [field K] {ι : Sort u_1} [hι : Nonempty ι] {S : ι → subfield K} (hS : directed LessEq S) : ↑(supr fun (i : ι) => S i) = set.Union fun (i : ι) => ↑(S i) := sorry theorem mem_Sup_of_directed_on {K : Type u} [field K] {S : set (subfield K)} (Sne : set.nonempty S) (hS : directed_on LessEq S) {x : K} : x ∈ Sup S ↔ ∃ (s : subfield K), ∃ (H : s ∈ S), x ∈ s := sorry theorem coe_Sup_of_directed_on {K : Type u} [field K] {S : set (subfield K)} (Sne : set.nonempty S) (hS : directed_on LessEq S) : ↑(Sup S) = set.Union fun (s : subfield K) => set.Union fun (H : s ∈ S) => ↑s := sorry end subfield namespace ring_hom /-- Restrict the codomain of a ring homomorphism to a subfield that includes the range. -/ def cod_restrict_field {K : Type u} {L : Type v} [field K] [field L] (f : K →+* L) (s : subfield L) (h : ∀ (x : K), coe_fn f x ∈ s) : K →+* ↥s := mk (fun (x : K) => { val := coe_fn f x, property := h x }) sorry sorry sorry sorry /-- Restriction of a ring homomorphism to a subfield of the domain. -/ def restrict_field {K : Type u} {L : Type v} [field K] [field L] (f : K →+* L) (s : subfield K) : ↥s →+* L := comp f (subfield.subtype s) @[simp] theorem restrict_field_apply {K : Type u} {L : Type v} [field K] [field L] {s : subfield K} (f : K →+* L) (x : ↥s) : coe_fn (restrict_field f s) x = coe_fn f ↑x := rfl /-- Restriction of a ring homomorphism to its range interpreted as a subfield. -/ def range_restrict_field {K : Type u} {L : Type v} [field K] [field L] (f : K →+* L) : K →+* ↥(range f) := cod_restrict' f (range f) sorry @[simp] theorem coe_range_restrict_field {K : Type u} {L : Type v} [field K] [field L] (f : K →+* L) (x : K) : ↑(coe_fn (range_restrict_field f) x) = coe_fn f x := rfl /-- The subfield of elements `x : R` such that `f x = g x`, i.e., the equalizer of f and g as a subfield of R -/ def eq_locus_field {K : Type u} {L : Type v} [field K] [field L] (f : K →+* L) (g : K →+* L) : subfield K := subfield.mk (set_of fun (x : K) => coe_fn f x = coe_fn g x) sorry sorry sorry sorry sorry sorry /-- If two ring homomorphisms are equal on a set, then they are equal on its subfield closure. -/ theorem eq_on_field_closure {K : Type u} {L : Type v} [field K] [field L] {f : K →+* L} {g : K →+* L} {s : set K} (h : set.eq_on (⇑f) (⇑g) s) : set.eq_on ⇑f ⇑g ↑(subfield.closure s) := (fun (this : subfield.closure s ≤ eq_locus_field f g) => this) (iff.mpr subfield.closure_le h) theorem eq_of_eq_on_subfield_top {K : Type u} {L : Type v} [field K] [field L] {f : K →+* L} {g : K →+* L} (h : set.eq_on ⇑f ⇑g ↑⊤) : f = g := ext fun (x : K) => h trivial theorem eq_of_eq_on_of_field_closure_eq_top {K : Type u} {L : Type v} [field K] [field L] {s : set K} (hs : subfield.closure s = ⊤) {f : K →+* L} {g : K →+* L} (h : set.eq_on (⇑f) (⇑g) s) : f = g := eq_of_eq_on_subfield_top (hs ▸ eq_on_field_closure h) theorem field_closure_preimage_le {K : Type u} {L : Type v} [field K] [field L] (f : K →+* L) (s : set L) : subfield.closure (⇑f ⁻¹' s) ≤ subfield.comap f (subfield.closure s) := iff.mpr subfield.closure_le fun (x : K) (hx : x ∈ ⇑f ⁻¹' s) => iff.mpr subfield.mem_coe (iff.mpr subfield.mem_comap (subfield.subset_closure hx)) /-- The image under a ring homomorphism of the subfield generated by a set equals the subfield generated by the image of the set. -/ theorem map_field_closure {K : Type u} {L : Type v} [field K] [field L] (f : K →+* L) (s : set K) : subfield.map f (subfield.closure s) = subfield.closure (⇑f '' s) := sorry end ring_hom namespace subfield /-- The ring homomorphism associated to an inclusion of subfields. -/ def inclusion {K : Type u} [field K] {S : subfield K} {T : subfield K} (h : S ≤ T) : ↥S →+* ↥T := ring_hom.cod_restrict_field (subtype S) T sorry @[simp] theorem field_range_subtype {K : Type u} [field K] (s : subfield K) : ring_hom.field_range (subtype s) = s := ext' (Eq.trans (ring_hom.coe_srange (subtype s)) subtype.range_coe) end subfield namespace ring_equiv /-- Makes the identity isomorphism from a proof two subfields of a multiplicative monoid are equal. -/ def subfield_congr {K : Type u} [field K] {s : subfield K} {t : subfield K} (h : s = t) : ↥s ≃+* ↥t := mk (equiv.to_fun (equiv.set_congr sorry)) (equiv.inv_fun (equiv.set_congr sorry)) sorry sorry sorry sorry end ring_equiv namespace subfield theorem closure_preimage_le {K : Type u} {L : Type v} [field K] [field L] (f : K →+* L) (s : set L) : closure (⇑f ⁻¹' s) ≤ comap f (closure s) := iff.mpr closure_le fun (x : K) (hx : x ∈ ⇑f ⁻¹' s) => iff.mpr mem_coe (iff.mpr mem_comap (subset_closure hx)) end Mathlib
c1ec7ebbd7052ca6b9b8a46be61a7ef2f53e4198
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/topology/locally_constant/algebra.lean
36d369c02ffcf97f97a68b4b00893bb5b76b202c
[ "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
7,512
lean
/- Copyright (c) 2021 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import algebra.algebra.basic import topology.locally_constant.basic /-! # Algebraic structure on locally constant functions This file puts algebraic structure (`add_group`, etc) on the type of locally constant functions. -/ namespace locally_constant variables {X Y : Type*} [topological_space X] @[to_additive] instance [has_one Y] : has_one (locally_constant X Y) := { one := const X 1 } @[simp, to_additive] lemma coe_one [has_one Y] : ⇑(1 : locally_constant X Y) = (1 : X → Y) := rfl @[to_additive] lemma one_apply [has_one Y] (x : X) : (1 : locally_constant X Y) x = 1 := rfl @[to_additive] instance [has_inv Y] : has_inv (locally_constant X Y) := { inv := λ f, ⟨f⁻¹ , f.is_locally_constant.inv⟩ } @[simp, to_additive] lemma coe_inv [has_inv Y] (f : locally_constant X Y) : ⇑(f⁻¹) = f⁻¹ := rfl @[to_additive] lemma inv_apply [has_inv Y] (f : locally_constant X Y) (x : X) : f⁻¹ x = (f x)⁻¹ := rfl @[to_additive] instance [has_mul Y] : has_mul (locally_constant X Y) := { mul := λ f g, ⟨f * g, f.is_locally_constant.mul g.is_locally_constant⟩ } @[simp, to_additive] lemma coe_mul [has_mul Y] (f g : locally_constant X Y) : ⇑(f * g) = f * g := rfl @[to_additive] lemma mul_apply [has_mul Y] (f g : locally_constant X Y) (x : X) : (f * g) x = f x * g x := rfl @[to_additive] instance [mul_one_class Y] : mul_one_class (locally_constant X Y) := { one_mul := by { intros, ext, simp only [mul_apply, one_apply, one_mul] }, mul_one := by { intros, ext, simp only [mul_apply, one_apply, mul_one] }, .. locally_constant.has_one, .. locally_constant.has_mul } /-- `coe_fn` is a `monoid_hom`. -/ @[to_additive "`coe_fn` is an `add_monoid_hom`.", simps] def coe_fn_monoid_hom [mul_one_class Y] : locally_constant X Y →* (X → Y) := { to_fun := coe_fn, map_one' := rfl, map_mul' := λ _ _, rfl } /-- The constant-function embedding, as a multiplicative monoid hom. -/ @[to_additive "The constant-function embedding, as an additive monoid hom.", simps] def const_monoid_hom [mul_one_class Y] : Y →* locally_constant X Y := { to_fun := const X, map_one' := rfl, map_mul' := λ _ _, rfl, } instance [mul_zero_class Y] : mul_zero_class (locally_constant X Y) := { zero_mul := by { intros, ext, simp only [mul_apply, zero_apply, zero_mul] }, mul_zero := by { intros, ext, simp only [mul_apply, zero_apply, mul_zero] }, .. locally_constant.has_zero, .. locally_constant.has_mul } instance [mul_zero_one_class Y] : mul_zero_one_class (locally_constant X Y) := { .. locally_constant.mul_zero_class, .. locally_constant.mul_one_class } @[to_additive] instance [has_div Y] : has_div (locally_constant X Y) := { div := λ f g, ⟨f / g, f.is_locally_constant.div g.is_locally_constant⟩ } @[to_additive] lemma coe_div [has_div Y] (f g : locally_constant X Y) : ⇑(f / g) = f / g := rfl @[to_additive] lemma div_apply [has_div Y] (f g : locally_constant X Y) (x : X) : (f / g) x = f x / g x := rfl @[to_additive] instance [semigroup Y] : semigroup (locally_constant X Y) := { mul_assoc := by { intros, ext, simp only [mul_apply, mul_assoc] }, .. locally_constant.has_mul } instance [semigroup_with_zero Y] : semigroup_with_zero (locally_constant X Y) := { .. locally_constant.mul_zero_class, .. locally_constant.semigroup } @[to_additive] instance [comm_semigroup Y] : comm_semigroup (locally_constant X Y) := { mul_comm := by { intros, ext, simp only [mul_apply, mul_comm] }, .. locally_constant.semigroup } @[to_additive] instance [monoid Y] : monoid (locally_constant X Y) := { mul := (*), .. locally_constant.semigroup, .. locally_constant.mul_one_class } @[to_additive] instance [comm_monoid Y] : comm_monoid (locally_constant X Y) := { .. locally_constant.comm_semigroup, .. locally_constant.monoid } @[to_additive] instance [group Y] : group (locally_constant X Y) := { mul_left_inv := by { intros, ext, simp only [mul_apply, inv_apply, one_apply, mul_left_inv] }, div_eq_mul_inv := by { intros, ext, simp only [mul_apply, inv_apply, div_apply, div_eq_mul_inv] }, .. locally_constant.monoid, .. locally_constant.has_inv, .. locally_constant.has_div } @[to_additive] instance [comm_group Y] : comm_group (locally_constant X Y) := { .. locally_constant.comm_monoid, .. locally_constant.group } instance [distrib Y] : distrib (locally_constant X Y) := { left_distrib := by { intros, ext, simp only [mul_apply, add_apply, mul_add] }, right_distrib := by { intros, ext, simp only [mul_apply, add_apply, add_mul] }, .. locally_constant.has_add, .. locally_constant.has_mul } instance [non_unital_non_assoc_semiring Y] : non_unital_non_assoc_semiring (locally_constant X Y) := { .. locally_constant.add_comm_monoid, .. locally_constant.has_mul, .. locally_constant.distrib, .. locally_constant.mul_zero_class } instance [non_unital_semiring Y] : non_unital_semiring (locally_constant X Y) := { .. locally_constant.semigroup, .. locally_constant.non_unital_non_assoc_semiring } instance [non_assoc_semiring Y] : non_assoc_semiring (locally_constant X Y) := { .. locally_constant.mul_one_class, .. locally_constant.non_unital_non_assoc_semiring } /-- The constant-function embedding, as a ring hom. -/ @[simps] def const_ring_hom [non_assoc_semiring Y] : Y →+* locally_constant X Y := { to_fun := const X, .. const_monoid_hom, .. const_add_monoid_hom, } instance [semiring Y] : semiring (locally_constant X Y) := { .. locally_constant.add_comm_monoid, .. locally_constant.monoid, .. locally_constant.distrib, .. locally_constant.mul_zero_class } instance [comm_semiring Y] : comm_semiring (locally_constant X Y) := { .. locally_constant.semiring, .. locally_constant.comm_monoid } instance [ring Y] : ring (locally_constant X Y) := { .. locally_constant.semiring, .. locally_constant.add_comm_group } instance [comm_ring Y] : comm_ring (locally_constant X Y) := { .. locally_constant.comm_semiring, .. locally_constant.ring } variables {R : Type*} instance [has_scalar R Y] : has_scalar R (locally_constant X Y) := { smul := λ r f, { to_fun := r • f, is_locally_constant := ((is_locally_constant f).comp ((•) r) : _), } } @[simp] lemma coe_smul [has_scalar R Y] (r : R) (f : locally_constant X Y) : ⇑(r • f) = r • f := rfl lemma smul_apply [has_scalar R Y] (r : R) (f : locally_constant X Y) (x : X) : (r • f) x = r • (f x) := rfl instance [monoid R] [mul_action R Y] : mul_action R (locally_constant X Y) := function.injective.mul_action _ coe_injective (λ _ _, rfl) instance [monoid R] [add_monoid Y] [distrib_mul_action R Y] : distrib_mul_action R (locally_constant X Y) := function.injective.distrib_mul_action coe_fn_add_monoid_hom coe_injective (λ _ _, rfl) instance [semiring R] [add_comm_monoid Y] [module R Y] : module R (locally_constant X Y) := function.injective.module R coe_fn_add_monoid_hom coe_injective (λ _ _, rfl) section algebra variables [comm_semiring R] [semiring Y] [algebra R Y] instance : algebra R (locally_constant X Y) := { to_ring_hom := const_ring_hom.comp $ algebra_map R Y, commutes' := by { intros, ext, exact algebra.commutes' _ _, }, smul_def' := by { intros, ext, exact algebra.smul_def' _ _, }, } @[simp] lemma coe_algebra_map (r : R) : ⇑(algebra_map R (locally_constant X Y) r) = algebra_map R (X → Y) r := rfl end algebra end locally_constant
4716632ec00be80acdf3595fe11f514f1b49d402
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/hott/algebra/category/limits/limits.hlean
286d13fb94e0f216657e02c2aa9239e2dbcd58d4
[ "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
17,452
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn Limits in a category -/ import ..constructions.cone ..constructions.discrete ..constructions.product ..constructions.finite_cats ..category ..constructions.functor open is_trunc functor nat_trans eq namespace category variables {ob : Type} [C : precategory ob] {c c' : ob} (D I : Precategory) include C definition is_terminal [class] (c : ob) := Πd, is_contr (d ⟶ c) definition is_contr_of_is_terminal (c d : ob) [H : is_terminal d] : is_contr (c ⟶ d) := H c local attribute is_contr_of_is_terminal [instance] definition terminal_morphism (c c' : ob) [H : is_terminal c'] : c ⟶ c' := !center definition hom_terminal_eq [H : is_terminal c'] (f f' : c ⟶ c') : f = f' := !is_hprop.elim definition eq_terminal_morphism [H : is_terminal c'] (f : c ⟶ c') : f = terminal_morphism c c' := !is_hprop.elim definition terminal_iso_terminal (c c' : ob) [H : is_terminal c] [K : is_terminal c'] : c ≅ c' := iso.MK !terminal_morphism !terminal_morphism !hom_terminal_eq !hom_terminal_eq local attribute is_terminal [reducible] theorem is_hprop_is_terminal [instance] : is_hprop (is_terminal c) := _ omit C structure has_terminal_object [class] (D : Precategory) := (d : D) (is_terminal : is_terminal d) definition terminal_object [reducible] [unfold 2] := @has_terminal_object.d attribute has_terminal_object.is_terminal [instance] variable {D} definition terminal_object_iso_terminal_object (H₁ H₂ : has_terminal_object D) : @terminal_object D H₁ ≅ @terminal_object D H₂ := !terminal_iso_terminal theorem is_hprop_has_terminal_object [instance] (D : Category) : is_hprop (has_terminal_object D) := begin apply is_hprop.mk, intro t₁ t₂, induction t₁ with d₁ H₁, induction t₂ with d₂ H₂, assert p : d₁ = d₂, { apply eq_of_iso, apply terminal_iso_terminal}, induction p, exact ap _ !is_hprop.elim end variable (D) definition has_limits_of_shape [class] := Π(F : I ⇒ D), has_terminal_object (cone F) /- The next definitions states that a category is complete with respect to diagrams in a certain universe. "is_complete.{o₁ h₁ o₂ h₂}" means that D is complete with respect to diagrams with shape in Precategory.{o₂ h₂} -/ definition is_complete.{o₁ h₁ o₂ h₂} [class] (D : Precategory.{o₁ h₁}) := Π(I : Precategory.{o₂ h₂}), has_limits_of_shape D I definition has_limits_of_shape_of_is_complete [instance] [H : is_complete D] (I : Precategory) : has_limits_of_shape D I := H I section open pi theorem is_hprop_has_limits_of_shape [instance] (D : Category) (I : Precategory) : is_hprop (has_limits_of_shape D I) := by apply is_trunc_pi; intro F; exact is_hprop_has_terminal_object (Category_cone F) local attribute is_complete [reducible] theorem is_hprop_is_complete [instance] (D : Category) : is_hprop (is_complete D) := _ end variables {D I} definition has_terminal_object_cone [H : has_limits_of_shape D I] (F : I ⇒ D) : has_terminal_object (cone F) := H F local attribute has_terminal_object_cone [instance] variables (F : I ⇒ D) [H : has_limits_of_shape D I] {i j : I} include H definition limit_cone : cone F := !terminal_object definition is_terminal_limit_cone [instance] : is_terminal (limit_cone F) := has_terminal_object.is_terminal _ section specific_limit omit H variable {F} variables (x : cone_obj F) [K : is_terminal x] include K definition to_limit_object : D := cone_to_obj x definition to_limit_nat_trans : constant_functor I (to_limit_object x) ⟹ F := cone_to_nat x definition to_limit_morphism (i : I) : to_limit_object x ⟶ F i := to_limit_nat_trans x i theorem to_limit_commute {i j : I} (f : i ⟶ j) : to_fun_hom F f ∘ to_limit_morphism x i = to_limit_morphism x j := naturality (to_limit_nat_trans x) f ⬝ !id_right definition to_limit_cone_obj [constructor] {d : D} {η : Πi, d ⟶ F i} (p : Π⦃i j : I⦄ (f : i ⟶ j), to_fun_hom F f ∘ η i = η j) : cone_obj F := cone_obj.mk d (nat_trans.mk η (λa b f, p f ⬝ !id_right⁻¹)) definition to_hom_limit {d : D} (η : Πi, d ⟶ F i) (p : Π⦃i j : I⦄ (f : i ⟶ j), to_fun_hom F f ∘ η i = η j) : d ⟶ to_limit_object x := cone_to_hom (terminal_morphism (to_limit_cone_obj x p) x) theorem to_hom_limit_commute {d : D} (η : Πi, d ⟶ F i) (p : Π⦃i j : I⦄ (f : i ⟶ j), to_fun_hom F f ∘ η i = η j) (i : I) : to_limit_morphism x i ∘ to_hom_limit x η p = η i := cone_to_eq (terminal_morphism (to_limit_cone_obj x p) x) i definition to_limit_cone_hom [constructor] {d : D} {η : Πi, d ⟶ F i} (p : Π⦃i j : I⦄ (f : i ⟶ j), to_fun_hom F f ∘ η i = η j) {h : d ⟶ to_limit_object x} (q : Πi, to_limit_morphism x i ∘ h = η i) : cone_hom (to_limit_cone_obj x p) x := cone_hom.mk h q variable {x} theorem to_eq_hom_limit {d : D} {η : Πi, d ⟶ F i} (p : Π⦃i j : I⦄ (f : i ⟶ j), to_fun_hom F f ∘ η i = η j) {h : d ⟶ to_limit_object x} (q : Πi, to_limit_morphism x i ∘ h = η i) : h = to_hom_limit x η p := ap cone_to_hom (eq_terminal_morphism (to_limit_cone_hom x p q)) theorem to_limit_cone_unique {d : D} {η : Πi, d ⟶ F i} (p : Π⦃i j : I⦄ (f : i ⟶ j), to_fun_hom F f ∘ η i = η j) {h₁ : d ⟶ to_limit_object x} (q₁ : Πi, to_limit_morphism x i ∘ h₁ = η i) {h₂ : d ⟶ to_limit_object x} (q₂ : Πi, to_limit_morphism x i ∘ h₂ = η i): h₁ = h₂ := to_eq_hom_limit p q₁ ⬝ (to_eq_hom_limit p q₂)⁻¹ omit K definition to_limit_object_iso_to_limit_object [constructor] (x y : cone_obj F) [K : is_terminal x] [L : is_terminal y] : to_limit_object x ≅ to_limit_object y := cone_iso_pr1 !terminal_iso_terminal end specific_limit /- TODO: relate below definitions to above definitions. However, type class resolution seems to fail... -/ definition limit_object : D := cone_to_obj (limit_cone F) definition limit_nat_trans : constant_functor I (limit_object F) ⟹ F := cone_to_nat (limit_cone F) definition limit_morphism (i : I) : limit_object F ⟶ F i := limit_nat_trans F i variable {H} theorem limit_commute {i j : I} (f : i ⟶ j) : to_fun_hom F f ∘ limit_morphism F i = limit_morphism F j := naturality (limit_nat_trans F) f ⬝ !id_right variable [H] definition limit_cone_obj [constructor] {d : D} {η : Πi, d ⟶ F i} (p : Π⦃i j : I⦄ (f : i ⟶ j), to_fun_hom F f ∘ η i = η j) : cone_obj F := cone_obj.mk d (nat_trans.mk η (λa b f, p f ⬝ !id_right⁻¹)) variable {H} definition hom_limit {d : D} (η : Πi, d ⟶ F i) (p : Π⦃i j : I⦄ (f : i ⟶ j), to_fun_hom F f ∘ η i = η j) : d ⟶ limit_object F := cone_to_hom (@(terminal_morphism (limit_cone_obj F p) _) (is_terminal_limit_cone _)) theorem hom_limit_commute {d : D} (η : Πi, d ⟶ F i) (p : Π⦃i j : I⦄ (f : i ⟶ j), to_fun_hom F f ∘ η i = η j) (i : I) : limit_morphism F i ∘ hom_limit F η p = η i := cone_to_eq (@(terminal_morphism (limit_cone_obj F p) _) (is_terminal_limit_cone _)) i definition limit_cone_hom [constructor] {d : D} {η : Πi, d ⟶ F i} (p : Π⦃i j : I⦄ (f : i ⟶ j), to_fun_hom F f ∘ η i = η j) {h : d ⟶ limit_object F} (q : Πi, limit_morphism F i ∘ h = η i) : cone_hom (limit_cone_obj F p) (limit_cone F) := cone_hom.mk h q variable {F} theorem eq_hom_limit {d : D} {η : Πi, d ⟶ F i} (p : Π⦃i j : I⦄ (f : i ⟶ j), to_fun_hom F f ∘ η i = η j) {h : d ⟶ limit_object F} (q : Πi, limit_morphism F i ∘ h = η i) : h = hom_limit F η p := ap cone_to_hom (@eq_terminal_morphism _ _ _ _ (is_terminal_limit_cone _) (limit_cone_hom F p q)) theorem limit_cone_unique {d : D} {η : Πi, d ⟶ F i} (p : Π⦃i j : I⦄ (f : i ⟶ j), to_fun_hom F f ∘ η i = η j) {h₁ : d ⟶ limit_object F} (q₁ : Πi, limit_morphism F i ∘ h₁ = η i) {h₂ : d ⟶ limit_object F} (q₂ : Πi, limit_morphism F i ∘ h₂ = η i): h₁ = h₂ := eq_hom_limit p q₁ ⬝ (eq_hom_limit p q₂)⁻¹ definition limit_hom_limit {F G : I ⇒ D} (η : F ⟹ G) : limit_object F ⟶ limit_object G := hom_limit _ (λi, η i ∘ limit_morphism F i) abstract by intro i j f; rewrite [assoc,naturality,-assoc,limit_commute] end theorem limit_hom_limit_commute {F G : I ⇒ D} (η : F ⟹ G) : limit_morphism G i ∘ limit_hom_limit η = η i ∘ limit_morphism F i := !hom_limit_commute -- theorem hom_limit_commute {d : D} (η : Πi, d ⟶ F i) -- (p : Π⦃i j : I⦄ (f : i ⟶ j), to_fun_hom F f ∘ η i = η j) (i : I) -- : limit_morphism F i ∘ hom_limit F η p = η i := -- cone_to_eq (@(terminal_morphism (limit_cone_obj F p) _) (is_terminal_limit_cone _)) i omit H variable (F) definition limit_object_iso_limit_object [constructor] (H₁ H₂ : has_limits_of_shape D I) : @(limit_object F) H₁ ≅ @(limit_object F) H₂ := cone_iso_pr1 !terminal_object_iso_terminal_object definition limit_functor [constructor] (D I : Precategory) [H : has_limits_of_shape D I] : D ^c I ⇒ D := begin fapply functor.mk: esimp, { intro F, exact limit_object F}, { apply @limit_hom_limit}, { intro F, unfold limit_hom_limit, refine (eq_hom_limit _ _)⁻¹, intro i, apply comp_id_eq_id_comp}, { intro F G H η θ, unfold limit_hom_limit, refine (eq_hom_limit _ _)⁻¹, intro i, rewrite [assoc, hom_limit_commute, -assoc, hom_limit_commute, assoc]} end section bin_products open bool prod.ops definition has_binary_products [reducible] (D : Precategory) := has_limits_of_shape D c2 variables [K : has_binary_products D] (d d' : D) include K definition product_object : D := limit_object (c2_functor D d d') infixr ` ×l `:75 := product_object definition pr1 : d ×l d' ⟶ d := limit_morphism (c2_functor D d d') ff definition pr2 : d ×l d' ⟶ d' := limit_morphism (c2_functor D d d') tt variables {d d'} definition hom_product {x : D} (f : x ⟶ d) (g : x ⟶ d') : x ⟶ d ×l d' := hom_limit (c2_functor D d d') (bool.rec f g) (by intro b₁ b₂ f; induction b₁: induction b₂: esimp at *; try contradiction: apply id_left) theorem pr1_hom_product {x : D} (f : x ⟶ d) (g : x ⟶ d') : !pr1 ∘ hom_product f g = f := hom_limit_commute (c2_functor D d d') (bool.rec f g) _ ff theorem pr2_hom_product {x : D} (f : x ⟶ d) (g : x ⟶ d') : !pr2 ∘ hom_product f g = g := hom_limit_commute (c2_functor D d d') (bool.rec f g) _ tt theorem eq_hom_product {x : D} {f : x ⟶ d} {g : x ⟶ d'} {h : x ⟶ d ×l d'} (p : !pr1 ∘ h = f) (q : !pr2 ∘ h = g) : h = hom_product f g := eq_hom_limit _ (bool.rec p q) theorem product_cone_unique {x : D} {f : x ⟶ d} {g : x ⟶ d'} {h₁ : x ⟶ d ×l d'} (p₁ : !pr1 ∘ h₁ = f) (q₁ : !pr2 ∘ h₁ = g) {h₂ : x ⟶ d ×l d'} (p₂ : !pr1 ∘ h₂ = f) (q₂ : !pr2 ∘ h₂ = g) : h₁ = h₂ := eq_hom_product p₁ q₁ ⬝ (eq_hom_product p₂ q₂)⁻¹ variable (D) -- TODO: define this in terms of limit_functor and functor_two_left (in exponential_laws) definition product_functor [constructor] : D ×c D ⇒ D := functor.mk (λx, product_object x.1 x.2) (λx y f, hom_product (f.1 ∘ !pr1) (f.2 ∘ !pr2)) abstract begin intro x, symmetry, apply eq_hom_product: apply comp_id_eq_id_comp end end abstract begin intro x y z g f, symmetry, apply eq_hom_product, rewrite [assoc,pr1_hom_product,-assoc,pr1_hom_product,assoc], rewrite [assoc,pr2_hom_product,-assoc,pr2_hom_product,assoc] end end omit K variables {D} (d d') definition product_object_iso_product_object [constructor] (H₁ H₂ : has_binary_products D) : @product_object D H₁ d d' ≅ @product_object D H₂ d d' := limit_object_iso_limit_object _ H₁ H₂ end bin_products section equalizers open bool prod.ops sum equalizer_category_hom definition has_equalizers [reducible] (D : Precategory) := has_limits_of_shape D equalizer_category variables [K : has_equalizers D] include K variables {d d' x : D} (f g : d ⟶ d') definition equalizer_object : D := limit_object (equalizer_category_functor D f g) definition equalizer : equalizer_object f g ⟶ d := limit_morphism (equalizer_category_functor D f g) ff theorem equalizes : f ∘ equalizer f g = g ∘ equalizer f g := limit_commute (equalizer_category_functor D f g) (inl f1) ⬝ (limit_commute (equalizer_category_functor D f g) (inl f2))⁻¹ variables {f g} definition hom_equalizer (h : x ⟶ d) (p : f ∘ h = g ∘ h) : x ⟶ equalizer_object f g := hom_limit (equalizer_category_functor D f g) (bool.rec h (g ∘ h)) begin intro b₁ b₂ i; induction i with j j: induction j, -- report(?) "esimp" is super slow here exact p, reflexivity, apply id_left end theorem equalizer_hom_equalizer (h : x ⟶ d) (p : f ∘ h = g ∘ h) : equalizer f g ∘ hom_equalizer h p = h := hom_limit_commute (equalizer_category_functor D f g) (bool.rec h (g ∘ h)) _ ff theorem eq_hom_equalizer {h : x ⟶ d} (p : f ∘ h = g ∘ h) {i : x ⟶ equalizer_object f g} (q : equalizer f g ∘ i = h) : i = hom_equalizer h p := eq_hom_limit _ (bool.rec q begin refine ap (λx, x ∘ i) (limit_commute (equalizer_category_functor D f g) (inl f2))⁻¹ ⬝ _, refine !assoc⁻¹ ⬝ _, exact ap (λx, _ ∘ x) q end) theorem equalizer_cone_unique {h : x ⟶ d} (p : f ∘ h = g ∘ h) {i₁ : x ⟶ equalizer_object f g} (q₁ : equalizer f g ∘ i₁ = h) {i₂ : x ⟶ equalizer_object f g} (q₂ : equalizer f g ∘ i₂ = h) : i₁ = i₂ := eq_hom_equalizer p q₁ ⬝ (eq_hom_equalizer p q₂)⁻¹ omit K variables (f g) definition equalizer_object_iso_equalizer_object [constructor] (H₁ H₂ : has_equalizers D) : @equalizer_object D H₁ _ _ f g ≅ @equalizer_object D H₂ _ _ f g := limit_object_iso_limit_object _ H₁ H₂ end equalizers section pullbacks open sum prod.ops pullback_category_ob pullback_category_hom definition has_pullbacks [reducible] (D : Precategory) := has_limits_of_shape D pullback_category variables [K : has_pullbacks D] include K variables {d₁ d₂ d₃ x : D} (f : d₁ ⟶ d₃) (g : d₂ ⟶ d₃) definition pullback_object : D := limit_object (pullback_category_functor D f g) definition pullback : pullback_object f g ⟶ d₂ := limit_morphism (pullback_category_functor D f g) BL definition pullback_rev : pullback_object f g ⟶ d₁ := limit_morphism (pullback_category_functor D f g) TR theorem pullback_commutes : f ∘ pullback_rev f g = g ∘ pullback f g := limit_commute (pullback_category_functor D f g) (inl f1) ⬝ (limit_commute (pullback_category_functor D f g) (inl f2))⁻¹ variables {f g} definition hom_pullback (h₁ : x ⟶ d₁) (h₂ : x ⟶ d₂) (p : f ∘ h₁ = g ∘ h₂) : x ⟶ pullback_object f g := hom_limit (pullback_category_functor D f g) (pullback_category_ob.rec h₁ h₂ (g ∘ h₂)) begin intro i₁ i₂ k; induction k with j j: induction j, exact p, reflexivity, apply id_left end theorem pullback_hom_pullback (h₁ : x ⟶ d₁) (h₂ : x ⟶ d₂) (p : f ∘ h₁ = g ∘ h₂) : pullback f g ∘ hom_pullback h₁ h₂ p = h₂ := hom_limit_commute (pullback_category_functor D f g) (pullback_category_ob.rec h₁ h₂ (g ∘ h₂)) _ BL theorem pullback_rev_hom_pullback (h₁ : x ⟶ d₁) (h₂ : x ⟶ d₂) (p : f ∘ h₁ = g ∘ h₂) : pullback_rev f g ∘ hom_pullback h₁ h₂ p = h₁ := hom_limit_commute (pullback_category_functor D f g) (pullback_category_ob.rec h₁ h₂ (g ∘ h₂)) _ TR theorem eq_hom_pullback {h₁ : x ⟶ d₁} {h₂ : x ⟶ d₂} (p : f ∘ h₁ = g ∘ h₂) {k : x ⟶ pullback_object f g} (q : pullback f g ∘ k = h₂) (r : pullback_rev f g ∘ k = h₁) : k = hom_pullback h₁ h₂ p := eq_hom_limit _ (pullback_category_ob.rec r q begin refine ap (λx, x ∘ k) (limit_commute (pullback_category_functor D f g) (inl f2))⁻¹ ⬝ _, refine !assoc⁻¹ ⬝ _, exact ap (λx, _ ∘ x) q end) theorem pullback_cone_unique {h₁ : x ⟶ d₁} {h₂ : x ⟶ d₂} (p : f ∘ h₁ = g ∘ h₂) {k₁ : x ⟶ pullback_object f g} (q₁ : pullback f g ∘ k₁ = h₂) (r₁ : pullback_rev f g ∘ k₁ = h₁) {k₂ : x ⟶ pullback_object f g} (q₂ : pullback f g ∘ k₂ = h₂) (r₂ : pullback_rev f g ∘ k₂ = h₁) : k₁ = k₂ := (eq_hom_pullback p q₁ r₁) ⬝ (eq_hom_pullback p q₂ r₂)⁻¹ variables (f g) definition pullback_object_iso_pullback_object [constructor] (H₁ H₂ : has_pullbacks D) : @pullback_object D H₁ _ _ _ f g ≅ @pullback_object D H₂ _ _ _ f g := limit_object_iso_limit_object _ H₁ H₂ end pullbacks namespace ops infixr ×l := product_object end ops end category
11355b1f9f918e0e978d75db9404f3864c78f8ba
9dc8cecdf3c4634764a18254e94d43da07142918
/src/group_theory/schur_zassenhaus.lean
a425f1600e0d7fbbe26d93b7813188207a574b55
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
14,707
lean
/- Copyright (c) 2021 Thomas Browning. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Thomas Browning -/ import group_theory.sylow import group_theory.transfer /-! # The Schur-Zassenhaus Theorem In this file we prove the Schur-Zassenhaus theorem. ## Main results - `exists_right_complement'_of_coprime` : The **Schur-Zassenhaus** theorem: If `H : subgroup G` is normal and has order coprime to its index, then there exists a subgroup `K` which is a (right) complement of `H`. - `exists_left_complement'_of_coprime` The **Schur-Zassenhaus** theorem: If `H : subgroup G` is normal and has order coprime to its index, then there exists a subgroup `K` which is a (left) complement of `H`. -/ open_locale big_operators namespace subgroup section schur_zassenhaus_abelian open mul_opposite mul_action subgroup.left_transversals mem_left_transversals variables {G : Type*} [group G] (H : subgroup G) [is_commutative H] [fintype (G ⧸ H)] (α β : left_transversals (H : set G)) /-- The quotient of the transversals of an abelian normal `N` by the `diff` relation. -/ def quotient_diff := quotient (setoid.mk (λ α β, diff (monoid_hom.id H) α β = 1) ⟨λ α, diff_self (monoid_hom.id H) α, λ α β h, by rw [←diff_inv, h, inv_one], λ α β γ h h', by rw [←diff_mul_diff, h, h', one_mul]⟩) instance : inhabited H.quotient_diff := quotient.inhabited _ lemma smul_diff_smul' [hH : normal H] (g : Gᵐᵒᵖ) : diff (monoid_hom.id H) (g • α) (g • β) = ⟨g.unop⁻¹ * (diff (monoid_hom.id H) α β : H) * g.unop, hH.mem_comm ((congr_arg (∈ H) (mul_inv_cancel_left _ _)).mpr (set_like.coe_mem _))⟩ := begin let ϕ : H →* H := { to_fun := λ h, ⟨g.unop⁻¹ * h * g.unop, hH.mem_comm ((congr_arg (∈ H) (mul_inv_cancel_left _ _)).mpr (set_like.coe_mem _))⟩, map_one' := by rw [subtype.ext_iff, coe_mk, coe_one, mul_one, inv_mul_self], map_mul' := λ h₁ h₂, by rw [subtype.ext_iff, coe_mk, coe_mul, coe_mul, coe_mk, coe_mk, mul_assoc, mul_assoc, mul_assoc, mul_assoc, mul_assoc, mul_inv_cancel_left] }, refine eq.trans (finset.prod_bij' (λ q _, g⁻¹ • q) (λ q _, finset.mem_univ _) (λ q _, subtype.ext _) (λ q _, g • q) (λ q _, finset.mem_univ _) (λ q _, smul_inv_smul g q) (λ q _, inv_smul_smul g q)) (map_prod ϕ _ _).symm, simp_rw [monoid_hom.id_apply, monoid_hom.coe_mk, coe_mk, smul_apply_eq_smul_apply_inv_smul, smul_eq_mul_unop, mul_inv_rev, mul_assoc], end variables {H} [normal H] instance : mul_action G H.quotient_diff := { smul := λ g, quotient.map' (λ α, op g⁻¹ • α) (λ α β h, subtype.ext (by rwa [smul_diff_smul', coe_mk, coe_one, mul_eq_one_iff_eq_inv, mul_right_eq_self, ←coe_one, ←subtype.ext_iff])), mul_smul := λ g₁ g₂ q, quotient.induction_on' q (λ T, congr_arg quotient.mk' (by rw mul_inv_rev; exact mul_smul (op g₁⁻¹) (op g₂⁻¹) T)), one_smul := λ q, quotient.induction_on' q (λ T, congr_arg quotient.mk' (by rw inv_one; apply one_smul Gᵐᵒᵖ T)) } lemma smul_diff' (h : H) : diff (monoid_hom.id H) α ((op (h : G)) • β) = diff (monoid_hom.id H) α β * h ^ H.index := begin rw [diff, diff, index_eq_card, ←finset.card_univ, ←finset.prod_const, ←finset.prod_mul_distrib], refine finset.prod_congr rfl (λ q _, _), simp_rw [subtype.ext_iff, monoid_hom.id_apply, coe_mul, coe_mk, mul_assoc, mul_right_inj], rw [smul_apply_eq_smul_apply_inv_smul, smul_eq_mul_unop, unop_op, mul_left_inj, ←subtype.ext_iff, equiv.apply_eq_iff_eq, inv_smul_eq_iff], exact self_eq_mul_right.mpr ((quotient_group.eq_one_iff _).mpr h.2), end variables [fintype H] lemma eq_one_of_smul_eq_one (hH : nat.coprime (fintype.card H) H.index) (α : H.quotient_diff) (h : H) : h • α = α → h = 1 := quotient.induction_on' α $ λ α hα, (pow_coprime hH).injective $ calc h ^ H.index = diff (monoid_hom.id H) ((op ((h⁻¹ : H) : G)) • α) α : by rw [←diff_inv, smul_diff', diff_self, one_mul, inv_pow, inv_inv] ... = 1 ^ H.index : (quotient.exact' hα).trans (one_pow H.index).symm lemma exists_smul_eq (hH : nat.coprime (fintype.card H) H.index) (α β : H.quotient_diff) : ∃ h : H, h • α = β := quotient.induction_on' α (quotient.induction_on' β (λ β α, exists_imp_exists (λ n, quotient.sound') ⟨(pow_coprime hH).symm (diff (monoid_hom.id H) β α), (diff_inv _ _ _).symm.trans (inv_eq_one.mpr ((smul_diff' β α ((pow_coprime hH).symm (diff (monoid_hom.id H) β α))⁻¹).trans (by rw [inv_pow, ←pow_coprime_apply hH, equiv.apply_symm_apply, mul_inv_self])))⟩)) lemma is_complement'_stabilizer_of_coprime {α : H.quotient_diff} (hH : nat.coprime (fintype.card H) H.index) : is_complement' H (stabilizer G α) := is_complement'_stabilizer α (eq_one_of_smul_eq_one hH α) (λ g, exists_smul_eq hH (g • α) α) /-- Do not use this lemma: It is made obsolete by `exists_right_complement'_of_coprime` -/ private lemma exists_right_complement'_of_coprime_aux (hH : nat.coprime (fintype.card H) H.index) : ∃ K : subgroup G, is_complement' H K := nonempty_of_inhabited.elim (λ α, ⟨stabilizer G α, is_complement'_stabilizer_of_coprime hH⟩) end schur_zassenhaus_abelian open_locale classical universe u namespace schur_zassenhaus_induction /-! ## Proof of the Schur-Zassenhaus theorem In this section, we prove the Schur-Zassenhaus theorem. The proof is by contradiction. We assume that `G` is a minimal counterexample to the theorem. -/ variables {G : Type u} [group G] [fintype G] {N : subgroup G} [normal N] (h1 : nat.coprime (fintype.card N) N.index) (h2 : ∀ (G' : Type u) [group G'] [fintype G'], by exactI ∀ (hG'3 : fintype.card G' < fintype.card G) {N' : subgroup G'} [N'.normal] (hN : nat.coprime (fintype.card N') N'.index), ∃ H' : subgroup G', is_complement' N' H') (h3 : ∀ H : subgroup G, ¬ is_complement' N H) include h1 h2 h3 /-! We will arrive at a contradiction via the following steps: * step 0: `N` (the normal Hall subgroup) is nontrivial. * step 1: If `K` is a subgroup of `G` with `K ⊔ N = ⊤`, then `K = ⊤`. * step 2: `N` is a minimal normal subgroup, phrased in terms of subgroups of `G`. * step 3: `N` is a minimal normal subgroup, phrased in terms of subgroups of `N`. * step 4: `p` (`min_fact (fintype.card N)`) is prime (follows from step0). * step 5: `P` (a Sylow `p`-subgroup of `N`) is nontrivial. * step 6: `N` is a `p`-group (applies step 1 to the normalizer of `P` in `G`). * step 7: `N` is abelian (applies step 3 to the center of `N`). -/ /-- Do not use this lemma: It is made obsolete by `exists_right_complement'_of_coprime` -/ @[nolint unused_arguments] private lemma step0 : N ≠ ⊥ := begin unfreezingI { rintro rfl }, exact h3 ⊤ is_complement'_bot_top, end /-- Do not use this lemma: It is made obsolete by `exists_right_complement'_of_coprime` -/ private lemma step1 (K : subgroup G) (hK : K ⊔ N = ⊤) : K = ⊤ := begin contrapose! h3, have h4 : (N.comap K.subtype).index = N.index, { rw [←N.relindex_top_right, ←hK], exact relindex_eq_relindex_sup K N }, have h5 : fintype.card K < fintype.card G, { rw ← K.index_mul_card, exact lt_mul_of_one_lt_left fintype.card_pos (one_lt_index_of_ne_top h3) }, have h6 : nat.coprime (fintype.card (N.comap K.subtype)) (N.comap K.subtype).index, { rw h4, exact h1.coprime_dvd_left (card_comap_dvd_of_injective N K.subtype subtype.coe_injective) }, obtain ⟨H, hH⟩ := h2 K h5 h6, replace hH : fintype.card (H.map K.subtype) = N.index := ((set.card_image_of_injective _ subtype.coe_injective).trans (nat.mul_left_injective fintype.card_pos (hH.symm.card_mul.trans (N.comap K.subtype).index_mul_card.symm))).trans h4, have h7 : fintype.card N * fintype.card (H.map K.subtype) = fintype.card G, { rw [hH, ←N.index_mul_card, mul_comm] }, have h8 : (fintype.card N).coprime (fintype.card (H.map K.subtype)), { rwa hH }, exact ⟨H.map K.subtype, is_complement'_of_coprime h7 h8⟩, end /-- Do not use this lemma: It is made obsolete by `exists_right_complement'_of_coprime` -/ private lemma step2 (K : subgroup G) [K.normal] (hK : K ≤ N) : K = ⊥ ∨ K = N := begin have : function.surjective (quotient_group.mk' K) := quotient.surjective_quotient_mk', have h4 := step1 h1 h2 h3, contrapose! h4, have h5 : fintype.card (G ⧸ K) < fintype.card G, { rw [←index_eq_card, ←K.index_mul_card], refine lt_mul_of_one_lt_right (nat.pos_of_ne_zero index_ne_zero_of_finite) (K.one_lt_card_iff_ne_bot.mpr h4.1) }, have h6 : nat.coprime (fintype.card (N.map (quotient_group.mk' K))) (N.map (quotient_group.mk' K)).index, { have index_map := N.index_map_eq this (by rwa quotient_group.ker_mk), have index_pos : 0 < N.index := nat.pos_of_ne_zero index_ne_zero_of_finite, rw index_map, refine h1.coprime_dvd_left _, rw [←nat.mul_dvd_mul_iff_left index_pos, index_mul_card, ←index_map, index_mul_card], exact K.card_quotient_dvd_card }, obtain ⟨H, hH⟩ := h2 (G ⧸ K) h5 h6, refine ⟨H.comap (quotient_group.mk' K), _, _⟩, { have key : (N.map (quotient_group.mk' K)).comap (quotient_group.mk' K) = N, { refine comap_map_eq_self _, rwa quotient_group.ker_mk }, rwa [←key, comap_sup_eq, hH.symm.sup_eq_top, comap_top] }, { rw ← comap_top (quotient_group.mk' K), intro hH', rw [comap_injective this hH', is_complement'_top_right, map_eq_bot_iff, quotient_group.ker_mk] at hH, { exact h4.2 (le_antisymm hK hH) } }, end /-- Do not use this lemma: It is made obsolete by `exists_right_complement'_of_coprime` -/ private lemma step3 (K : subgroup N) [(K.map N.subtype).normal] : K = ⊥ ∨ K = ⊤ := begin have key := step2 h1 h2 h3 (K.map N.subtype) K.map_subtype_le, rw ← map_bot N.subtype at key, conv at key { congr, skip, to_rhs, rw [←N.subtype_range, N.subtype.range_eq_map] }, have inj := map_injective (show function.injective N.subtype, from subtype.coe_injective), rwa [inj.eq_iff, inj.eq_iff] at key, end /-- Do not use this lemma: It is made obsolete by `exists_right_complement'_of_coprime` -/ private lemma step4 : (fintype.card N).min_fac.prime := (nat.min_fac_prime (N.one_lt_card_iff_ne_bot.mpr (step0 h1 h2 h3)).ne') /-- Do not use this lemma: It is made obsolete by `exists_right_complement'_of_coprime` -/ private lemma step5 {P : sylow (fintype.card N).min_fac N} : P.1 ≠ ⊥ := begin haveI : fact ((fintype.card N).min_fac.prime) := ⟨step4 h1 h2 h3⟩, exact P.ne_bot_of_dvd_card (fintype.card N).min_fac_dvd, end /-- Do not use this lemma: It is made obsolete by `exists_right_complement'_of_coprime` -/ private lemma step6 : is_p_group (fintype.card N).min_fac N := begin haveI : fact ((fintype.card N).min_fac.prime) := ⟨step4 h1 h2 h3⟩, refine sylow.nonempty.elim (λ P, P.2.of_surjective P.1.subtype _), rw [←monoid_hom.range_top_iff_surjective, subtype_range], haveI : (P.1.map N.subtype).normal := normalizer_eq_top.mp (step1 h1 h2 h3 (P.1.map N.subtype).normalizer P.normalizer_sup_eq_top), exact (step3 h1 h2 h3 P.1).resolve_left (step5 h1 h2 h3), end /-- Do not use this lemma: It is made obsolete by `exists_right_complement'_of_coprime` -/ lemma step7 : is_commutative N := begin haveI := N.bot_or_nontrivial.resolve_left (step0 h1 h2 h3), haveI : fact ((fintype.card N).min_fac.prime) := ⟨step4 h1 h2 h3⟩, exact ⟨⟨λ g h, eq_top_iff.mp ((step3 h1 h2 h3 N.center).resolve_left (step6 h1 h2 h3).bot_lt_center.ne') (mem_top h) g⟩⟩, end end schur_zassenhaus_induction variables {n : ℕ} {G : Type u} [group G] /-- Do not use this lemma: It is made obsolete by `exists_right_complement'_of_coprime` -/ private lemma exists_right_complement'_of_coprime_aux' [fintype G] (hG : fintype.card G = n) {N : subgroup G} [N.normal] (hN : nat.coprime (fintype.card N) N.index) : ∃ H : subgroup G, is_complement' N H := begin unfreezingI { revert G }, apply nat.strong_induction_on n, rintros n ih G _ _ rfl N _ hN, refine not_forall_not.mp (λ h3, _), haveI := by exactI schur_zassenhaus_induction.step7 hN (λ G' _ _ hG', by { apply ih _ hG', refl }) h3, exact not_exists_of_forall_not h3 (exists_right_complement'_of_coprime_aux hN), end /-- **Schur-Zassenhaus** for normal subgroups: If `H : subgroup G` is normal, and has order coprime to its index, then there exists a subgroup `K` which is a (right) complement of `H`. -/ theorem exists_right_complement'_of_coprime_of_fintype [fintype G] {N : subgroup G} [N.normal] (hN : nat.coprime (fintype.card N) N.index) : ∃ H : subgroup G, is_complement' N H := exists_right_complement'_of_coprime_aux' rfl hN /-- **Schur-Zassenhaus** for normal subgroups: If `H : subgroup G` is normal, and has order coprime to its index, then there exists a subgroup `K` which is a (right) complement of `H`. -/ theorem exists_right_complement'_of_coprime {N : subgroup G} [N.normal] (hN : nat.coprime (nat.card N) N.index) : ∃ H : subgroup G, is_complement' N H := begin by_cases hN1 : nat.card N = 0, { rw [hN1, nat.coprime_zero_left, index_eq_one] at hN, rw hN, exact ⟨⊥, is_complement'_top_bot⟩ }, by_cases hN2 : N.index = 0, { rw [hN2, nat.coprime_zero_right] at hN, haveI := (cardinal.to_nat_eq_one_iff_unique.mp hN).1, rw N.eq_bot_of_subsingleton, exact ⟨⊤, is_complement'_bot_top⟩ }, have hN3 : nat.card G ≠ 0, { rw ← N.card_mul_index, exact mul_ne_zero hN1 hN2 }, haveI := (cardinal.lt_aleph_0_iff_fintype.mp (lt_of_not_ge (mt cardinal.to_nat_apply_of_aleph_0_le hN3))).some, rw nat.card_eq_fintype_card at hN, exact exists_right_complement'_of_coprime_of_fintype hN, end /-- **Schur-Zassenhaus** for normal subgroups: If `H : subgroup G` is normal, and has order coprime to its index, then there exists a subgroup `K` which is a (left) complement of `H`. -/ theorem exists_left_complement'_of_coprime_of_fintype [fintype G] {N : subgroup G} [N.normal] (hN : nat.coprime (fintype.card N) N.index) : ∃ H : subgroup G, is_complement' H N := Exists.imp (λ _, is_complement'.symm) (exists_right_complement'_of_coprime_of_fintype hN) /-- **Schur-Zassenhaus** for normal subgroups: If `H : subgroup G` is normal, and has order coprime to its index, then there exists a subgroup `K` which is a (left) complement of `H`. -/ theorem exists_left_complement'_of_coprime {N : subgroup G} [N.normal] (hN : nat.coprime (nat.card N) N.index) : ∃ H : subgroup G, is_complement' H N := Exists.imp (λ _, is_complement'.symm) (exists_right_complement'_of_coprime hN) end subgroup
ce4b5b19e7b918e57f6bedec271d6a22625597ba
9028d228ac200bbefe3a711342514dd4e4458bff
/src/data/fintype/basic.lean
d9221488df9b417414fc078729e404dd51ccd73e
[ "Apache-2.0" ]
permissive
mcncm/mathlib
8d25099344d9d2bee62822cb9ed43aa3e09fa05e
fde3d78cadeec5ef827b16ae55664ef115e66f57
refs/heads/master
1,672,743,316,277
1,602,618,514,000
1,602,618,514,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
51,510
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 tactic.wlog import data.finset.powerset import data.finset.lattice import data.finset.pi import data.array.lemmas import order.well_founded open_locale nat universes u v variables {α : Type*} {β : Type*} {γ : Type*} /-- `fintype α` means that `α` is finite, i.e. there are only finitely many distinct elements of type `α`. The evidence of this is a finset `elems` (a list up to permutation without duplicates), together with a proof that everything of type `α` is in the list. -/ class fintype (α : Type*) := (elems [] : finset α) (complete : ∀ x : α, x ∈ elems) namespace finset variable [fintype α] /-- `univ` is the universal finite set of type `finset α` implied from the assumption `fintype α`. -/ def univ : finset α := fintype.elems α @[simp] theorem mem_univ (x : α) : x ∈ (univ : finset α) := fintype.complete x @[simp] theorem mem_univ_val : ∀ x, x ∈ (univ : finset α).1 := mem_univ @[simp] lemma coe_univ : ↑(univ : finset α) = (set.univ : set α) := by ext; simp lemma univ_nonempty_iff : (univ : finset α).nonempty ↔ nonempty α := by rw [← coe_nonempty, coe_univ, set.nonempty_iff_univ_nonempty] lemma univ_nonempty [nonempty α] : (univ : finset α).nonempty := univ_nonempty_iff.2 ‹_› lemma univ_eq_empty : (univ : finset α) = ∅ ↔ ¬nonempty α := by rw [← univ_nonempty_iff, nonempty_iff_ne_empty, ne.def, not_not] theorem subset_univ (s : finset α) : s ⊆ univ := λ a _, mem_univ a instance : order_top (finset α) := { top := univ, le_top := subset_univ, .. finset.partial_order } instance [decidable_eq α] : boolean_algebra (finset α) := { compl := λ s, univ \ s, sdiff_eq := λ s t, by simp [ext_iff], inf_compl_le_bot := λ s x hx, by simpa using hx, top_le_sup_compl := λ s x hx, by simp, ..finset.distrib_lattice, ..finset.semilattice_inf_bot, ..finset.order_top, ..finset.has_sdiff } lemma compl_eq_univ_sdiff [decidable_eq α] (s : finset α) : sᶜ = univ \ s := rfl @[simp] lemma mem_compl [decidable_eq α] {s : finset α} {x : α} : x ∈ sᶜ ↔ x ∉ s := by simp [compl_eq_univ_sdiff] @[simp, norm_cast] lemma coe_compl [decidable_eq α] (s : finset α) : ↑(sᶜ) = (↑s : set α)ᶜ := set.ext $ λ x, mem_compl theorem eq_univ_iff_forall {s : finset α} : s = univ ↔ ∀ x, x ∈ s := by simp [ext_iff] @[simp] lemma univ_inter [decidable_eq α] (s : finset α) : univ ∩ s = s := ext $ λ a, by simp @[simp] lemma inter_univ [decidable_eq α] (s : finset α) : s ∩ univ = s := by rw [inter_comm, univ_inter] @[simp] lemma piecewise_univ [∀i : α, decidable (i ∈ (univ : finset α))] {δ : α → Sort*} (f g : Πi, δ i) : univ.piecewise f g = f := by { ext i, simp [piecewise] } lemma univ_map_equiv_to_embedding {α β : Type*} [fintype α] [fintype β] (e : α ≃ β) : univ.map e.to_embedding = univ := begin apply eq_univ_iff_forall.mpr, intro b, rw [mem_map], use e.symm b, simp, end 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 (α) [decidable_eq α] [fintype α] : trunc (α ≃ fin (card α)) := by unfold card finset.card; exact quot.rec_on_subsingleton (@univ α _).1 (λ l (h : ∀ x:α, x ∈ l) (nd : l.nodup), trunc.mk (equiv_fin_of_forall_mem_list h nd)) mem_univ_val univ.2 theorem exists_equiv_fin (α) [fintype α] : ∃ n, nonempty (α ≃ fin n) := by haveI := classical.dec_eq α; exact ⟨card α, nonempty_of_trunc (equiv_fin α)⟩ instance (α : Type*) : subsingleton (fintype α) := ⟨λ ⟨s₁, h₁⟩ ⟨s₂, h₂⟩, by congr; simp [finset.ext_iff, h₁, h₂]⟩ /-- Given a predicate that can be represented by a finset, the subtype associated to the predicate is a fintype. -/ protected def subtype {p : α → Prop} (s : finset α) (H : ∀ x : α, x ∈ s ↔ p x) : fintype {x // p x} := ⟨⟨multiset.pmap subtype.mk s.1 (λ x, (H x).1), multiset.nodup_pmap (λ a _ b _, congr_arg subtype.val) s.2⟩, λ ⟨x, px⟩, multiset.mem_pmap.2 ⟨x, (H x).2 px, rfl⟩⟩ theorem subtype_card {p : α → Prop} (s : finset α) (H : ∀ x : α, x ∈ s ↔ p x) : @card {x // p x} (fintype.subtype s H) = s.card := multiset.card_pmap _ _ _ theorem card_of_subtype {p : α → Prop} (s : finset α) (H : ∀ x : α, x ∈ s ↔ p x) [fintype {x // p x}] : card {x // p x} = s.card := by { rw ← subtype_card s H, congr } /-- Construct a fintype from a finset with the same elements. -/ def of_finset {p : set α} (s : finset α) (H : ∀ x, x ∈ s ↔ x ∈ p) : fintype p := fintype.subtype s H @[simp] theorem card_of_finset {p : set α} (s : finset α) (H : ∀ x, x ∈ s ↔ x ∈ p) : @fintype.card p (of_finset s H) = s.card := fintype.subtype_card s H theorem card_of_finset' {p : set α} (s : finset α) (H : ∀ x, x ∈ s ↔ x ∈ p) [fintype p] : fintype.card p = s.card := by rw ← card_of_finset s H; congr /-- If `f : α → β` is a bijection and `α` is a fintype, then `β` is also a fintype. -/ def of_bijective [fintype α] (f : α → β) (H : function.bijective f) : fintype β := ⟨univ.map ⟨f, H.1⟩, λ b, let ⟨a, e⟩ := H.2 b in e ▸ mem_map_of_mem _ (mem_univ _)⟩ /-- If `f : α → β` is a surjection and `α` is a fintype, then `β` is also a fintype. -/ def of_surjective [decidable_eq β] [fintype α] (f : α → β) (H : function.surjective f) : fintype β := ⟨univ.image f, λ b, let ⟨a, e⟩ := H b in e ▸ mem_image_of_mem _ (mem_univ _)⟩ /-- Given an injective function to a fintype, the domain is also a fintype. This is noncomputable because injectivity alone cannot be used to construct preimages. -/ noncomputable def of_injective [fintype β] (f : α → β) (H : function.injective f) : fintype α := by letI := classical.dec; exact if hα : nonempty α then by letI := classical.inhabited_of_nonempty hα; exact of_surjective (inv_fun f) (inv_fun_surjective H) else ⟨∅, λ x, (hα ⟨x⟩).elim⟩ /-- If `f : α ≃ β` and `α` is a fintype, then `β` is also a fintype. -/ def of_equiv (α : Type*) [fintype α] (f : α ≃ β) : fintype β := of_bijective _ f.bijective theorem of_equiv_card [fintype α] (f : α ≃ β) : @card β (of_equiv α f) = card α := multiset.card_map _ _ theorem card_congr {α β} [fintype α] [fintype β] (f : α ≃ β) : card α = card β := by rw ← of_equiv_card f; congr 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⟩ /-- Subsingleton types are fintypes (with zero or one terms). -/ def of_subsingleton (a : α) [subsingleton α] : fintype α := ⟨{a}, λ b, finset.mem_singleton.2 (subsingleton.elim _ _)⟩ @[simp] theorem univ_of_subsingleton (a : α) [subsingleton α] : @univ _ (of_subsingleton a) = {a} := rfl @[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 -- We use an arbitrary `[fintype s]` instance here, -- not necessarily coming from a `[fintype α]`. @[simp] lemma to_finset_card {α : Type*} (s : set α) [fintype s] : s.to_finset.card = fintype.card s := multiset.card_map subtype.val finset.univ.val @[simp] theorem coe_to_finset (s : set α) [fintype s] : (↑s.to_finset : set α) = s := set.ext $ λ _, mem_to_finset @[simp] theorem to_finset_inj {s t : set α} [fintype s] [fintype t] : s.to_finset = t.to_finset ↔ s = t := ⟨λ h, by rw [← s.coe_to_finset, h, t.coe_to_finset], λ h, by simp [h]; congr⟩ end set lemma finset.card_univ [fintype α] : (finset.univ : finset α).card = fintype.card α := rfl lemma finset.eq_univ_of_card [fintype α] (s : finset α) (hs : s.card = fintype.card α) : s = univ := eq_of_subset_of_card_le (subset_univ _) $ by rw [hs, finset.card_univ] lemma finset.card_le_univ [fintype α] (s : finset α) : s.card ≤ fintype.card α := card_le_of_subset (subset_univ s) lemma finset.card_univ_diff [decidable_eq α] [fintype α] (s : finset α) : (finset.univ \ s).card = fintype.card α - s.card := finset.card_sdiff (subset_univ s) lemma finset.card_compl [decidable_eq α] [fintype α] (s : finset α) : sᶜ.card = fintype.card α - s.card := finset.card_univ_diff s instance (n : ℕ) : fintype (fin n) := ⟨finset.fin_range n, finset.mem_fin_range⟩ lemma fin.univ_def (n : ℕ) : (univ : finset (fin n)) = finset.fin_range n := rfl @[simp] theorem fintype.card_fin (n : ℕ) : fintype.card (fin n) = n := list.length_fin_range n @[simp] lemma finset.card_fin (n : ℕ) : finset.card (finset.univ : finset (fin n)) = n := by rw [finset.card_univ, fintype.card_fin] /-- Embed `fin n` into `fin (n + 1)` by prepending zero to the `univ` -/ lemma fin.univ_succ (n : ℕ) : (univ : finset (fin (n + 1))) = insert 0 (univ.image fin.succ) := begin ext m, simp only [mem_univ, mem_insert, true_iff, mem_image, exists_prop], exact fin.cases (or.inl rfl) (λ i, or.inr ⟨i, trivial, rfl⟩) m end /-- Embed `fin n` into `fin (n + 1)` by appending a new `fin.last n` to the `univ` -/ lemma fin.univ_cast_succ (n : ℕ) : (univ : finset (fin (n + 1))) = insert (fin.last n) (univ.image fin.cast_succ) := begin ext m, simp only [mem_univ, mem_insert, true_iff, mem_image, exists_prop, true_and], by_cases h : m.val < n, { right, use fin.cast_lt m h, rw fin.cast_succ_cast_lt }, { left, exact fin.eq_last_of_not_lt h } end /-- Embed `fin n` into `fin (n + 1)` by inserting around a specified pivot `p : fin (n + 1)` into the `univ` -/ lemma fin.univ_succ_above (n : ℕ) (p : fin (n + 1)) : (univ : finset (fin (n + 1))) = insert p (univ.image (fin.succ_above p)) := begin rcases lt_or_eq_of_le (fin.le_last p) with hl|rfl, { ext m, simp only [finset.mem_univ, finset.mem_insert, true_iff, finset.mem_image, exists_prop], refine or_iff_not_imp_left.mpr _, { intro h, use p.pred_above m h, simp only [eq_self_iff_true, fin.succ_above_descend, and_self] } }, { rw fin.succ_above_last, exact fin.univ_cast_succ n } end @[instance, priority 10] def unique.fintype {α : Type*} [unique α] : fintype α := fintype.of_subsingleton (default α) @[simp] lemma univ_unique {α : Type*} [unique α] [f : fintype α] : @finset.univ α _ = {default α} := by rw [subsingleton.elim f (@unique.fintype α _)]; refl 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 _ = {tt, ff} := 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 /-- Given a finset on `α`, lift it to being a finset on `option α` using `option.some` and then insert `option.none`. -/ def finset.insert_none (s : finset α) : finset (option α) := ⟨none ::ₘ s.1.map some, multiset.nodup_cons.2 ⟨by simp, multiset.nodup_map (λ a b, option.some.inj) s.2⟩⟩ @[simp] theorem finset.mem_insert_none {s : finset α} : ∀ {o : option α}, o ∈ s.insert_none ↔ ∀ a ∈ o, a ∈ s | none := iff_of_true (multiset.mem_cons_self _ _) (λ a h, by cases h) | (some a) := multiset.mem_cons.trans $ by simp; refl theorem finset.some_mem_insert_none {s : finset α} {a : α} : some a ∈ s.insert_none ↔ a ∈ s := by simp instance {α : Type*} [fintype α] : fintype (option α) := ⟨univ.insert_none, λ a, by simp⟩ @[simp] theorem fintype.card_option {α : Type*} [fintype α] : fintype.card (option α) = fintype.card α + 1 := (multiset.card_cons _ _).trans (by rw multiset.card_map; refl) instance {α : Type*} (β : α → Type*) [fintype α] [∀ a, fintype (β a)] : fintype (sigma β) := ⟨univ.sigma (λ _, univ), λ ⟨a, b⟩, by simp⟩ @[simp] lemma finset.univ_sigma_univ {α : Type*} {β : α → Type*} [fintype α] [∀ a, fintype (β a)] : (univ : finset α).sigma (λ a, (univ : finset (β a))) = univ := rfl instance (α β : Type*) [fintype α] [fintype β] : fintype (α × β) := ⟨univ.product univ, λ ⟨a, b⟩, by simp⟩ @[simp] lemma finset.univ_product_univ {α β : Type*} [fintype α] [fintype β] : (univ : finset α).product (univ : finset β) = univ := rfl @[simp] theorem fintype.card_prod (α β : Type*) [fintype α] [fintype β] : fintype.card (α × β) = fintype.card α * fintype.card β := card_product _ _ /-- Given that `α × β` is a fintype, `α` is also a fintype. -/ def fintype.fintype_prod_left {α β} [decidable_eq α] [fintype (α × β)] [nonempty β] : fintype α := ⟨(fintype.elems (α × β)).image prod.fst, assume a, let ⟨b⟩ := ‹nonempty β› in by simp; exact ⟨b, fintype.complete _⟩⟩ /-- Given that `α × β` is a fintype, `β` is also a fintype. -/ def fintype.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)) namespace fintype variables [fintype α] [fintype β] lemma card_le_of_injective (f : α → β) (hf : function.injective f) : card α ≤ card β := finset.card_le_card_of_inj_on f (λ _ _, finset.mem_univ _) (λ _ _ _ _ h, hf h) /-- The pigeonhole principle for finitely many pigeons and pigeonholes. This is the `fintype` version of `finset.exists_ne_map_eq_of_card_lt_of_maps_to`. -/ lemma exists_ne_map_eq_of_card_lt (f : α → β) (h : fintype.card β < fintype.card α) : ∃ x y, x ≠ y ∧ f x = f y := let ⟨x, _, y, _, h⟩ := finset.exists_ne_map_eq_of_card_lt_of_maps_to h (λ x _, mem_univ (f x)) in ⟨x, y, h⟩ lemma card_eq_one_iff : card α = 1 ↔ (∃ x : α, ∀ y, y = x) := by rw [← card_unit, card_eq]; exact ⟨λ ⟨a⟩, ⟨a.symm (), λ y, a.injective (subsingleton.elim _ _)⟩, λ ⟨x, hx⟩, ⟨⟨λ _, (), λ _, x, λ _, (hx _).trans (hx _).symm, λ _, subsingleton.elim _ _⟩⟩⟩ lemma card_eq_zero_iff : card α = 0 ↔ (α → false) := ⟨λ h a, have e : α ≃ empty := classical.choice (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 [card_congr e]⟩ /-- A `fintype` with cardinality zero is (constructively) equivalent to `pempty`. -/ def card_eq_zero_equiv_equiv_pempty : card α = 0 ≃ (α ≃ pempty.{v+1}) := { to_fun := λ h, { to_fun := λ a, false.elim (card_eq_zero_iff.1 h a), inv_fun := λ a, pempty.elim a, left_inv := λ a, false.elim (card_eq_zero_iff.1 h a), right_inv := λ a, pempty.elim a, }, inv_fun := λ e, by { simp only [←of_equiv_card e], convert card_pempty, }, left_inv := λ h, rfl, right_inv := λ e, by { ext x, cases e x, } } lemma card_pos_iff : 0 < card α ↔ nonempty α := ⟨λ h, classical.by_contradiction (λ h₁, have card α = 0 := card_eq_zero_iff.2 (λ a, h₁ ⟨a⟩), lt_irrefl 0 $ by rwa this at h), λ ⟨a⟩, nat.pos_of_ne_zero (mt card_eq_zero_iff.1 (λ h, h a))⟩ lemma card_le_one_iff : card α ≤ 1 ↔ (∀ a b : α, a = b) := let n := card α in have hn : n = card α := rfl, match n, hn with | 0 := λ ha, ⟨λ h, λ a, (card_eq_zero_iff.1 ha.symm a).elim, λ _, ha ▸ nat.le_succ _⟩ | 1 := λ ha, ⟨λ h, λ a b, let ⟨x, hx⟩ := card_eq_one_iff.1 ha.symm in by rw [hx a, hx b], λ _, ha ▸ le_refl _⟩ | (n+2) := λ ha, ⟨λ h, by rw ← ha at h; exact absurd h dec_trivial, (λ h, card_unit ▸ card_le_of_injective (λ _, ()) (λ _ _ _, h _ _))⟩ end lemma card_le_one_iff_subsingleton : card α ≤ 1 ↔ subsingleton α := iff.trans card_le_one_iff subsingleton_iff.symm lemma one_lt_card_iff_nontrivial : 1 < card α ↔ nontrivial α := begin classical, rw ← not_iff_not, push_neg, rw [not_nontrivial_iff_subsingleton, card_le_one_iff_subsingleton] end lemma exists_ne_of_one_lt_card (h : 1 < card α) (a : α) : ∃ b : α, b ≠ a := by { haveI : nontrivial α := one_lt_card_iff_nontrivial.1 h, exact exists_ne a } lemma exists_pair_of_one_lt_card (h : 1 < card α) : ∃ (a b : α), a ≠ b := by { haveI : nontrivial α := one_lt_card_iff_nontrivial.1 h, exact exists_pair_ne α } lemma injective_iff_surjective {f : α → α} : injective f ↔ surjective f := by haveI := classical.prop_decidable; exact have ∀ {f : α → α}, injective f → surjective f, from λ f hinj x, have h₁ : image f univ = univ := eq_of_subset_of_card_le (subset_univ _) ((card_image_of_injective univ hinj).symm ▸ le_refl _), have h₂ : x ∈ image f univ := h₁.symm ▸ mem_univ _, exists_of_bex (mem_image.1 h₂), ⟨this, λ hsurj, has_left_inverse.injective ⟨surj_inv hsurj, left_inverse_of_surjective_of_right_inverse (this (injective_surj_inv _)) (right_inverse_surj_inv _)⟩⟩ lemma injective_iff_bijective {f : α → α} : injective f ↔ bijective f := by simp [bijective, injective_iff_surjective] lemma surjective_iff_bijective {f : α → α} : surjective f ↔ bijective f := by simp [bijective, injective_iff_surjective] lemma injective_iff_surjective_of_equiv {β : Type*} {f : α → β} (e : α ≃ β) : injective f ↔ surjective f := have injective (e.symm ∘ f) ↔ surjective (e.symm ∘ f), from injective_iff_surjective, ⟨λ hinj, by simpa [function.comp] using e.surjective.comp (this.1 (e.symm.injective.comp hinj)), λ hsurj, by simpa [function.comp] using e.injective.comp (this.2 (e.symm.surjective.comp hsurj))⟩ lemma nonempty_equiv_of_card_eq (h : card α = card β) : nonempty (α ≃ β) := begin obtain ⟨m, ⟨f⟩⟩ := exists_equiv_fin α, obtain ⟨n, ⟨g⟩⟩ := exists_equiv_fin β, suffices : m = n, { subst this, exact ⟨f.trans g.symm⟩ }, calc m = card (fin m) : (card_fin m).symm ... = card α : card_congr f.symm ... = card β : h ... = card (fin n) : card_congr g ... = n : card_fin n end lemma bijective_iff_injective_and_card (f : α → β) : bijective f ↔ injective f ∧ card α = card β := begin split, { intro h, exact ⟨h.1, card_congr (equiv.of_bijective f h)⟩ }, { rintro ⟨hf, h⟩, refine ⟨hf, _⟩, obtain ⟨e⟩ : nonempty (α ≃ β) := nonempty_equiv_of_card_eq h, rwa ← injective_iff_surjective_of_equiv e } end lemma bijective_iff_surjective_and_card (f : α → β) : bijective f ↔ surjective f ∧ card α = card β := begin split, { intro h, exact ⟨h.2, card_congr (equiv.of_bijective f h)⟩, }, { rintro ⟨hf, h⟩, refine ⟨_, hf⟩, obtain ⟨e⟩ : nonempty (α ≃ β) := nonempty_equiv_of_card_eq h, rwa injective_iff_surjective_of_equiv e } end end fintype lemma fintype.coe_image_univ [fintype α] [decidable_eq β] {f : α → β} : ↑(finset.image f finset.univ) = set.range f := by { ext x, simp } instance list.subtype.fintype [decidable_eq α] (l : list α) : fintype {x // x ∈ l} := fintype.of_list l.attach l.mem_attach instance multiset.subtype.fintype [decidable_eq α] (s : multiset α) : fintype {x // x ∈ s} := fintype.of_multiset s.attach s.mem_attach instance finset.subtype.fintype (s : finset α) : fintype {x // x ∈ s} := ⟨s.attach, s.mem_attach⟩ instance finset_coe.fintype (s : finset α) : fintype (↑s : set α) := finset.subtype.fintype s @[simp] lemma fintype.card_coe (s : finset α) : fintype.card (↑s : set α) = s.card := card_attach lemma finset.attach_eq_univ {s : finset α} : s.attach = finset.univ := rfl 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 /-- A `finset` of a subsingleton type has cardinality at most one. -/ lemma finset.card_le_one_of_subsingleton [subsingleton α] (s : finset α) : s.card ≤ 1 := finset.card_le_one_iff.2 $ λ _ _ _ _, subsingleton.elim _ _ 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 [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 {⟨h⟩} else ∅, λ ⟨h⟩, by simp [h]⟩ instance Prop.fintype : fintype Prop := ⟨⟨true ::ₘ false ::ₘ 0, by simp [true_ne_false]⟩, classical.cases (by simp) (by simp)⟩ instance subtype.fintype (p : α → Prop) [decidable_pred p] [fintype α] : fintype {x // p x} := fintype.subtype (univ.filter p) (by simp) /-- A set on a fintype, when coerced to a type, is a fintype. -/ def set_fintype {α} [fintype α] (s : set α) [decidable_pred s] : fintype s := subtype.fintype (λ x, x ∈ s) namespace function.embedding /-- An embedding from a `fintype` to itself can be promoted to an equivalence. -/ noncomputable def equiv_of_fintype_self_embedding {α : Type*} [fintype α] (e : α ↪ α) : α ≃ α := equiv.of_bijective e (fintype.injective_iff_bijective.1 e.2) @[simp] lemma equiv_of_fintype_self_embedding_to_embedding {α : Type*} [fintype α] (e : α ↪ α) : e.equiv_of_fintype_self_embedding.to_embedding = e := by { ext, refl, } end function.embedding @[simp] lemma finset.univ_map_embedding {α : Type*} [fintype α] (e : α ↪ α) : univ.map e = univ := by rw [← e.equiv_of_fintype_self_embedding_to_embedding, univ_map_equiv_to_embedding] namespace fintype variables [decidable_eq α] [fintype α] {δ : α → Type*} /-- Given for all `a : α` a finset `t a` of `δ a`, then one can define the finset `fintype.pi_finset t` of all functions taking values in `t a` for all `a`. This is the analogue of `finset.pi` where the base finset is `univ` (but formally they are not the same, as there is an additional condition `i ∈ finset.univ` in the `finset.pi` definition). -/ def pi_finset (t : Πa, finset (δ a)) : finset (Πa, δ a) := (finset.univ.pi t).map ⟨λ f a, f a (mem_univ a), λ _ _, by simp [function.funext_iff]⟩ @[simp] lemma mem_pi_finset {t : Πa, finset (δ a)} {f : Πa, δ a} : f ∈ pi_finset t ↔ (∀a, f a ∈ t a) := begin split, { simp only [pi_finset, mem_map, and_imp, forall_prop_of_true, exists_prop, mem_univ, exists_imp_distrib, mem_pi], assume g hg hgf a, rw ← hgf, exact hg a }, { simp only [pi_finset, mem_map, forall_prop_of_true, exists_prop, mem_univ, mem_pi], assume hf, exact ⟨λ a ha, f a, hf, rfl⟩ } end lemma pi_finset_subset (t₁ t₂ : Πa, finset (δ a)) (h : ∀ a, t₁ a ⊆ t₂ a) : pi_finset t₁ ⊆ pi_finset t₂ := λ g hg, mem_pi_finset.2 $ λ a, h a $ mem_pi_finset.1 hg a lemma pi_finset_disjoint_of_disjoint [∀ a, decidable_eq (δ a)] (t₁ t₂ : Πa, finset (δ a)) {a : α} (h : disjoint (t₁ a) (t₂ a)) : disjoint (pi_finset t₁) (pi_finset t₂) := disjoint_iff_ne.2 $ λ f₁ hf₁ f₂ hf₂ eq₁₂, disjoint_iff_ne.1 h (f₁ a) (mem_pi_finset.1 hf₁ a) (f₂ a) (mem_pi_finset.1 hf₂ a) (congr_fun eq₁₂ a) end fintype /-! ### pi -/ /-- A dependent product of fintypes, indexed by a fintype, is a fintype. -/ instance pi.fintype {α : Type*} {β : α → Type*} [decidable_eq α] [fintype α] [∀a, fintype (β a)] : fintype (Πa, β a) := ⟨fintype.pi_finset (λ _, univ), by simp⟩ @[simp] lemma fintype.pi_finset_univ {α : Type*} {β : α → Type*} [decidable_eq α] [fintype α] [∀a, fintype (β a)] : fintype.pi_finset (λ a : α, (finset.univ : finset (β a))) = (finset.univ : finset (Π a, β a)) := rfl instance d_array.fintype {n : ℕ} {α : fin n → Type*} [∀n, fintype (α n)] : fintype (d_array n α) := fintype.of_equiv _ (equiv.d_array_equiv_fin _).symm instance array.fintype {n : ℕ} {α : Type*} [fintype α] : fintype (array n α) := d_array.fintype instance vector.fintype {α : Type*} [fintype α] {n : ℕ} : fintype (vector α n) := fintype.of_equiv _ (equiv.vector_equiv_fin _ _).symm instance quotient.fintype [fintype α] (s : setoid α) [decidable_rel ((≈) : α → α → Prop)] : fintype (quotient s) := fintype.of_surjective quotient.mk (λ x, quotient.induction_on x (λ x, ⟨x, rfl⟩)) instance finset.fintype [fintype α] : fintype (finset α) := ⟨univ.powerset, λ x, finset.mem_powerset.2 (finset.subset_univ _)⟩ @[simp] lemma fintype.card_finset [fintype α] : fintype.card (finset α) = 2 ^ (fintype.card α) := finset.card_powerset finset.univ @[simp] lemma set.to_finset_univ [fintype α] : (set.univ : set α).to_finset = finset.univ := by { ext, simp only [set.mem_univ, mem_univ, set.mem_to_finset] } @[simp] lemma set.to_finset_empty [fintype α] : (∅ : set α).to_finset = ∅ := by { ext, simp only [set.mem_empty_eq, set.mem_to_finset, not_mem_empty] } 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 _, not_forall.2 ⟨x, by simp [hx]⟩⟩ instance psigma.fintype {α : Type*} {β : α → Type*} [fintype α] [∀ a, fintype (β a)] : fintype (Σ' a, β a) := fintype.of_equiv _ (equiv.psigma_equiv_sigma _).symm instance psigma.fintype_prop_left {α : Prop} {β : α → Type*} [decidable α] [∀ a, fintype (β a)] : fintype (Σ' a, β a) := if h : α then fintype.of_equiv (β h) ⟨λ x, ⟨h, x⟩, psigma.snd, λ _, rfl, λ ⟨_, _⟩, rfl⟩ else ⟨∅, λ x, h x.1⟩ instance psigma.fintype_prop_right {α : Type*} {β : α → Prop} [∀ a, decidable (β a)] [fintype α] : fintype (Σ' a, β a) := fintype.of_equiv {a // β a} ⟨λ ⟨x, y⟩, ⟨x, y⟩, λ ⟨x, y⟩, ⟨x, y⟩, λ ⟨x, y⟩, rfl, λ ⟨x, y⟩, rfl⟩ instance psigma.fintype_prop_prop {α : Prop} {β : α → Prop} [decidable α] [∀ a, decidable (β a)] : fintype (Σ' a, β a) := if h : ∃ a, β a then ⟨{⟨h.fst, h.snd⟩}, λ ⟨_, _⟩, by simp⟩ else ⟨∅, λ ⟨x, y⟩, h ⟨x, y⟩⟩ instance set.fintype [fintype α] : fintype (set α) := ⟨(@finset.univ α _).powerset.map ⟨coe, coe_injective⟩, λ s, begin classical, refine mem_map.2 ⟨finset.univ.filter s, mem_powerset.2 (subset_univ _), _⟩, apply (coe_filter _).trans, rw [coe_univ, set.sep_univ], refl end⟩ instance pfun_fintype (p : Prop) [decidable p] (α : p → Type*) [Π hp, fintype (α hp)] : fintype (Π hp : p, α hp) := if hp : p then fintype.of_equiv (α hp) ⟨λ a _, a, λ f, f hp, λ _, rfl, λ _, rfl⟩ else ⟨singleton (λ h, (hp h).elim), by simp [hp, function.funext_iff]⟩ @[simp] lemma finset.univ_pi_univ {α : Type*} {β : α → Type*} [decidable_eq α] [fintype α] [∀a, fintype (β a)] : finset.univ.pi (λ a : α, (finset.univ : finset (β a))) = finset.univ := by { ext, simp } lemma mem_image_univ_iff_mem_range {α β : Type*} [fintype α] [decidable_eq β] {f : α → β} {b : β} : b ∈ univ.image f ↔ b ∈ set.range f := by simp lemma card_lt_card_of_injective_of_not_mem {α β : Type*} [fintype α] [fintype β] (f : α → β) (h : function.injective f) {b : β} (w : b ∉ set.range f) : fintype.card α < fintype.card β := begin classical, calc fintype.card α = (univ : finset α).card : rfl ... = (image f univ).card : (card_image_of_injective univ h).symm ... < (insert b (image f univ)).card : card_lt_card (ssubset_insert (mt mem_image_univ_iff_mem_range.mp w)) ... ≤ (univ : finset β).card : card_le_of_subset (subset_univ _) ... = fintype.card β : rfl end /-- An auxiliary function for `quotient.fin_choice`. Given a collection of setoids indexed by a type `ι`, a (finite) list `l` of indices, and a function that for each `i ∈ l` gives a term of the corresponding quotient type, then there is a corresponding term in the quotient of the product of the setoids indexed by `l`. -/ def quotient.fin_choice_aux {ι : Type*} [decidable_eq ι] {α : ι → Type*} [S : ∀ i, setoid (α i)] : Π (l : list ι), (Π i ∈ l, quotient (S i)) → @quotient (Π i ∈ l, α i) (by apply_instance) | [] f := ⟦λ i, false.elim⟧ | (i::l) f := begin refine quotient.lift_on₂ (f i (list.mem_cons_self _ _)) (quotient.fin_choice_aux l (λ j h, f j (list.mem_cons_of_mem _ h))) _ _, exact λ a l, ⟦λ j h, if e : j = i then by rw e; exact a else l _ (h.resolve_left e)⟧, refine λ a₁ l₁ a₂ l₂ h₁ h₂, quotient.sound (λ j h, _), by_cases e : j = i; simp [e], { subst j, exact h₁ }, { exact h₂ _ _ } end theorem quotient.fin_choice_aux_eq {ι : Type*} [decidable_eq ι] {α : ι → Type*} [S : ∀ i, setoid (α i)] : ∀ (l : list ι) (f : Π i ∈ l, α i), quotient.fin_choice_aux l (λ i h, ⟦f i h⟧) = ⟦f⟧ | [] f := quotient.sound (λ i h, h.elim) | (i::l) f := begin simp [quotient.fin_choice_aux, quotient.fin_choice_aux_eq l], refine quotient.sound (λ j h, _), by_cases e : j = i; simp [e], subst j, refl end /-- Given a collection of setoids indexed by a fintype `ι` and a function that for each `i : ι` gives a term of the corresponding quotient type, then there is corresponding term in the quotient of the product of the setoids. -/ def quotient.fin_choice {ι : Type*} [decidable_eq ι] [fintype ι] {α : ι → Type*} [S : ∀ i, setoid (α i)] (f : Π i, quotient (S i)) : @quotient (Π i, α i) (by apply_instance) := quotient.lift_on (@quotient.rec_on _ _ (λ l : multiset ι, @quotient (Π i ∈ l, α i) (by apply_instance)) finset.univ.1 (λ l, quotient.fin_choice_aux l (λ i _, f i)) (λ a b h, begin have := λ a, quotient.fin_choice_aux_eq a (λ i h, quotient.out (f i)), simp [quotient.out_eq] at this, simp [this], let g := λ a:multiset ι, ⟦λ (i : ι) (h : i ∈ a), quotient.out (f i)⟧, refine eq_of_heq ((eq_rec_heq _ _).trans (_ : g a == g b)), congr' 1, exact quotient.sound h, end)) (λ f, ⟦λ i, f i (finset.mem_univ _)⟧) (λ a b h, quotient.sound $ λ i, h _ _) theorem quotient.fin_choice_eq {ι : Type*} [decidable_eq ι] [fintype ι] {α : ι → Type*} [∀ i, setoid (α i)] (f : Π i, α i) : quotient.fin_choice (λ i, ⟦f i⟧) = ⟦f⟧ := begin let q, swap, change quotient.lift_on q _ _ = _, have : q = ⟦λ i h, f i⟧, { dsimp [q], exact quotient.induction_on (@finset.univ ι _).1 (λ l, quotient.fin_choice_aux_eq _ _) }, simp [this], exact setoid.refl _ end section equiv open list equiv equiv.perm variables [decidable_eq α] [decidable_eq β] /-- Given a list, produce a list of all permutations of its elements. -/ def perms_of_list : list α → list (perm α) | [] := [1] | (a :: l) := perms_of_list l ++ l.bind (λ b, (perms_of_list l).map (λ f, swap a b * f)) lemma length_perms_of_list : ∀ l : list α, length (perms_of_list l) = l.length! | [] := rfl | (a :: l) := begin rw [length_cons, nat.factorial_succ], simp [perms_of_list, length_bind, length_perms_of_list, function.comp, nat.succ_mul], cc end lemma mem_perms_of_list_of_mem {l : list α} {f : perm α} (h : ∀ x, f x ≠ x → x ∈ l) : f ∈ perms_of_list l := begin induction l with a l IH generalizing f h, { exact list.mem_singleton.2 (equiv.ext $ λ x, decidable.by_contradiction $ h _) }, by_cases hfa : f a = a, { refine mem_append_left _ (IH (λ x hx, mem_of_ne_of_mem _ (h x hx))), rintro rfl, exact hx hfa }, { have hfa' : f (f a) ≠ f a := mt (λ h, f.injective h) hfa, have : ∀ (x : α), (swap a (f a) * f) x ≠ x → x ∈ l, { intros x hx, have hxa : x ≠ a, { rintro rfl, apply hx, simp only [mul_apply, swap_apply_right] }, refine list.mem_of_ne_of_mem hxa (h x (λ h, _)), simp only [h, mul_apply, swap_apply_def, mul_apply, ne.def, apply_eq_iff_eq] at hx; split_ifs at hx, exacts [hxa (h.symm.trans h_1), hx h] }, suffices : f ∈ perms_of_list l ∨ ∃ (b ∈ l) (g ∈ perms_of_list l), swap a b * g = f, { simpa only [perms_of_list, exists_prop, list.mem_map, mem_append, list.mem_bind] }, refine or_iff_not_imp_left.2 (λ hfl, ⟨f a, _, swap a (f a) * f, IH this, _⟩), { by_cases hffa : f (f a) = a, { exact mem_of_ne_of_mem hfa (h _ (mt (λ h, f.injective h) hfa)) }, { apply this, simp only [mul_apply, swap_apply_def, mul_apply, ne.def, apply_eq_iff_eq], split_ifs; cc } }, { rw [← mul_assoc, mul_def (swap a (f a)) (swap a (f a)), swap_swap, ← equiv.perm.one_def, one_mul] } } end lemma mem_of_mem_perms_of_list : ∀ {l : list α} {f : perm α}, f ∈ perms_of_list l → ∀ {x}, f x ≠ x → x ∈ l | [] f h := have f = 1 := by simpa [perms_of_list] using h, by rw this; simp | (a::l) f h := (mem_append.1 h).elim (λ h x hx, mem_cons_of_mem _ (mem_of_mem_perms_of_list h hx)) (λ h x hx, let ⟨y, hy, hy'⟩ := list.mem_bind.1 h in let ⟨g, hg₁, hg₂⟩ := list.mem_map.1 hy' in if hxa : x = a then by simp [hxa] else if hxy : x = y then mem_cons_of_mem _ $ by rwa hxy else mem_cons_of_mem _ $ mem_of_mem_perms_of_list hg₁ $ by rw [eq_inv_mul_iff_mul_eq.2 hg₂, mul_apply, swap_inv, swap_apply_def]; split_ifs; cc) lemma mem_perms_of_list_iff {l : list α} {f : perm α} : f ∈ perms_of_list l ↔ ∀ {x}, f x ≠ x → x ∈ l := ⟨mem_of_mem_perms_of_list, mem_perms_of_list_of_mem⟩ lemma nodup_perms_of_list : ∀ {l : list α} (hl : l.nodup), (perms_of_list l).nodup | [] hl := by simp [perms_of_list] | (a::l) hl := have hl' : l.nodup, from nodup_of_nodup_cons hl, have hln' : (perms_of_list l).nodup, from nodup_perms_of_list hl', have hmeml : ∀ {f : perm α}, f ∈ perms_of_list l → f a = a, from λ f hf, not_not.1 (mt (mem_of_mem_perms_of_list hf) (nodup_cons.1 hl).1), by rw [perms_of_list, list.nodup_append, list.nodup_bind, pairwise_iff_nth_le]; exact ⟨hln', ⟨λ _ _, nodup_map (λ _ _, mul_left_cancel) hln', λ i j hj hij x hx₁ hx₂, let ⟨f, hf⟩ := list.mem_map.1 hx₁ in let ⟨g, hg⟩ := list.mem_map.1 hx₂ in have hix : x a = nth_le l i (lt_trans hij hj), by rw [← hf.2, mul_apply, hmeml hf.1, swap_apply_left], have hiy : x a = nth_le l j hj, by rw [← hg.2, mul_apply, hmeml hg.1, swap_apply_left], absurd (hf.2.trans (hg.2.symm)) $ λ h, ne_of_lt hij $ nodup_iff_nth_le_inj.1 hl' i j (lt_trans hij hj) hj $ by rw [← hix, hiy]⟩, λ f hf₁ hf₂, let ⟨x, hx, hx'⟩ := list.mem_bind.1 hf₂ in let ⟨g, hg⟩ := list.mem_map.1 hx' in have hgxa : g⁻¹ x = a, from f.injective $ by rw [hmeml hf₁, ← hg.2]; simp, have hxa : x ≠ a, from λ h, (list.nodup_cons.1 hl).1 (h ▸ hx), (list.nodup_cons.1 hl).1 $ hgxa ▸ mem_of_mem_perms_of_list hg.1 (by rwa [apply_inv_self, hgxa])⟩ /-- Given a finset, produce the finset of all permutations of its elements. -/ def perms_of_finset (s : finset α) : finset (perm α) := quotient.hrec_on s.1 (λ l hl, ⟨perms_of_list l, nodup_perms_of_list hl⟩) (λ a b hab, hfunext (congr_arg _ (quotient.sound hab)) (λ ha hb _, heq_of_eq $ finset.ext $ by simp [mem_perms_of_list_iff, hab.mem_iff])) s.2 lemma mem_perms_of_finset_iff : ∀ {s : finset α} {f : perm α}, f ∈ perms_of_finset s ↔ ∀ {x}, f x ≠ x → x ∈ s := by rintros ⟨⟨l⟩, hs⟩ f; exact mem_perms_of_list_iff lemma card_perms_of_finset : ∀ (s : finset α), (perms_of_finset s).card = s.card! := by rintros ⟨⟨l⟩, hs⟩; exact length_perms_of_list l /-- The collection of permutations of a fintype is a fintype. -/ def fintype_perm [fintype α] : fintype (perm α) := ⟨perms_of_finset (@finset.univ α _), by simp [mem_perms_of_finset_iff]⟩ instance [fintype α] [fintype β] : fintype (α ≃ β) := if h : fintype.card β = fintype.card α then trunc.rec_on_subsingleton (fintype.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 α)! := subsingleton.elim (@fintype_perm α _ _) (@equiv.fintype α α _ _ _ _) ▸ card_perms_of_finset _ lemma fintype.card_equiv [fintype α] [fintype β] (e : α ≃ β) : fintype.card (α ≃ β) = (fintype.card α)! := fintype.card_congr (equiv_congr (equiv.refl α) e) ▸ fintype.card_perm lemma univ_eq_singleton_of_card_one {α} [fintype α] (x : α) (h : fintype.card α = 1) : (univ : finset α) = {x} := begin apply symm, apply eq_of_subset_of_card_le (subset_univ ({x})), apply le_of_eq, simp [h, finset.card_univ] end end equiv namespace fintype section choose open fintype open equiv variables [fintype α] (p : α → Prop) [decidable_pred p] /-- Given a fintype `α` and a predicate `p`, associate to a proof that there is a unique element of `α` satisfying `p` this unique element, as an element of the corresponding subtype. -/ def choose_x (hp : ∃! a : α, p a) : {a // p a} := ⟨finset.choose p univ (by simp; exact hp), finset.choose_property _ _ _⟩ /-- Given a fintype `α` and a predicate `p`, associate to a proof that there is a unique element of `α` satisfying `p` this unique element, as an element of `α`. -/ def choose (hp : ∃! a, p a) : α := choose_x p hp lemma choose_spec (hp : ∃! a, p a) : p (choose p hp) := (choose_x p hp).property end choose section bijection_inverse open function variables [fintype α] variables [decidable_eq β] variables {f : α → β} /-- ` `bij_inv f` is the unique inverse to a bijection `f`. This acts as a computable alternative to `function.inv_fun`. -/ def bij_inv (f_bij : bijective f) (b : β) : α := fintype.choose (λ a, f a = b) begin rcases f_bij.right b with ⟨a', fa_eq_b⟩, rw ← fa_eq_b, exact ⟨a', ⟨rfl, (λ a h, f_bij.left h)⟩⟩ end lemma left_inverse_bij_inv (f_bij : bijective f) : left_inverse (bij_inv f_bij) f := λ a, f_bij.left (choose_spec (λ a', f a' = f a) _) lemma right_inverse_bij_inv (f_bij : bijective f) : right_inverse (bij_inv f_bij) f := λ b, choose_spec (λ a', f a' = b) _ lemma bijective_bij_inv (f_bij : bijective f) : bijective (bij_inv f_bij) := ⟨(right_inverse_bij_inv _).injective, (left_inverse_bij_inv _).surjective⟩ end bijection_inverse lemma well_founded_of_trans_of_irrefl [fintype α] (r : α → α → Prop) [is_trans α r] [is_irrefl α r] : well_founded r := by classical; exact have ∀ x y, r x y → (univ.filter (λ z, r z x)).card < (univ.filter (λ z, r z y)).card, from λ x y hxy, finset.card_lt_card $ by simp only [finset.lt_iff_ssubset.symm, lt_iff_le_not_le, finset.le_iff_subset, finset.subset_iff, mem_filter, true_and, mem_univ, hxy]; exact ⟨λ z hzx, trans hzx hxy, not_forall_of_exists_not ⟨x, not_imp.2 ⟨hxy, irrefl x⟩⟩⟩, subrelation.wf this (measure_wf _) lemma preorder.well_founded [fintype α] [preorder α] : well_founded ((<) : α → α → Prop) := well_founded_of_trans_of_irrefl _ @[instance, priority 10] lemma linear_order.is_well_order [fintype α] [linear_order α] : is_well_order α (<) := { wf := preorder.well_founded } end fintype /-- A type is said to be infinite if it has no fintype instance. -/ 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⟩ lemma finset.exists_minimal {α : Type*} [preorder α] (s : finset α) (h : s.nonempty) : ∃ m ∈ s, ∀ x ∈ s, ¬ (x < m) := begin obtain ⟨c, hcs : c ∈ s⟩ := h, have : well_founded (@has_lt.lt {x // x ∈ s} _) := fintype.well_founded_of_trans_of_irrefl _, obtain ⟨⟨m, hms : m ∈ s⟩, -, H⟩ := this.has_min set.univ ⟨⟨c, hcs⟩, trivial⟩, exact ⟨m, hms, λ x hx hxm, H ⟨x, hx⟩ trivial hxm⟩, end lemma finset.exists_maximal {α : Type*} [preorder α] (s : finset α) (h : s.nonempty) : ∃ m ∈ s, ∀ x ∈ s, ¬ (m < x) := @finset.exists_minimal (order_dual α) _ s h namespace infinite lemma exists_not_mem_finset [infinite α] (s : finset α) : ∃ x, x ∉ s := not_forall.1 $ λ h, 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 /-- Embedding of `ℕ` into an infinite type. -/ noncomputable def nat_embedding (α : Type*) [infinite α] : ℕ ↪ α := ⟨_, nat_embedding_aux_injective α⟩ lemma exists_subset_card_eq (α : Type*) [infinite α] (n : ℕ) : ∃ s : finset α, s.card = n := ⟨(range n).map (nat_embedding α), by rw [card_map, card_range]⟩ end infinite lemma not_injective_infinite_fintype [infinite α] [fintype β] (f : α → β) : ¬ injective f := assume (hf : injective f), have H : fintype α := fintype.of_injective f hf, infinite.not_fintype H /-- The pigeonhole principle for infinitely many pigeons in finitely many pigeonholes. If there are infinitely many pigeons in finitely many pigeonholes, then there are at least two pigeons in the same pigeonhole. See also: `fintype.exists_ne_map_eq_of_card_lt`, `fintype.exists_infinite_fiber`. -/ lemma fintype.exists_ne_map_eq_of_infinite [infinite α] [fintype β] (f : α → β) : ∃ x y : α, x ≠ y ∧ f x = f y := begin classical, by_contra hf, push_neg at hf, apply not_injective_infinite_fintype f, intros x y, contrapose, apply hf, end /-- The strong pigeonhole principle for infinitely many pigeons in finitely many pigeonholes. If there are infinitely many pigeons in finitely many pigeonholes, then there is a pigeonhole with infinitely many pigeons. See also: `fintype.exists_ne_map_eq_of_infinite` -/ lemma fintype.exists_infinite_fiber [infinite α] [fintype β] (f : α → β) : ∃ y : β, infinite (f ⁻¹' {y}) := begin classical, by_contra hf, push_neg at hf, haveI h' : ∀ (y : β), fintype (f ⁻¹' {y}) := begin intro y, specialize hf y, rw [←not_nonempty_fintype, not_not] at hf, exact classical.choice hf, end, let key : fintype α := { elems := univ.bind (λ (y : β), (f ⁻¹' {y}).to_finset), complete := by simp }, exact infinite.not_fintype key, end 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) section trunc /-- For `s : multiset α`, we can lift the existential statement that `∃ x, x ∈ s` to a `trunc α`. -/ def trunc_of_multiset_exists_mem {α} (s : multiset α) : (∃ x, x ∈ s) → trunc α := quotient.rec_on_subsingleton s $ λ l h, match l, h with | [], _ := false.elim (by tauto) | (a :: _), _ := trunc.mk a end /-- A `nonempty` `fintype` constructively contains an element. -/ def trunc_of_nonempty_fintype (α) [nonempty α] [fintype α] : trunc α := trunc_of_multiset_exists_mem finset.univ.val (by simp) /-- A `fintype` with positive cardinality constructively contains an element. -/ def trunc_of_card_pos {α} [fintype α] (h : 0 < fintype.card α) : trunc α := by { letI := (fintype.card_pos_iff.mp h), exact trunc_of_nonempty_fintype α } /-- By iterating over the elements of a fintype, we can lift an existential statement `∃ a, P a` to `trunc (Σ' a, P a)`, containing data. -/ def trunc_sigma_of_exists {α} [fintype α] {P : α → Prop} [decidable_pred P] (h : ∃ a, P a) : trunc (Σ' a, P a) := @trunc_of_nonempty_fintype (Σ' a, P a) (exists.elim h $ λ a ha, ⟨⟨a, ha⟩⟩) _ end trunc
79c975f84df6124619891d63b6370ec0bda10e40
fe25de614feb5587799621c41487aaee0d083b08
/tests/lean/run/toFromJson.lean
0d6b30ee661ff0214ad7dbcee99f8175ee3c1ea5
[ "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
2,581
lean
import Lean open Lean open Lean Parser Term declare_syntax_cat json syntax strLit : json syntax numLit : json syntax "{" (Lean.Parser.ident ": " json),* "}" : json syntax "[" json,* "]" : json syntax "json " json : term /- declare a micro json parser, so we can write our tests in a more legible way. -/ open Json in macro_rules | `(json $s:strLit) => s | `(json $n:numLit) => n | `(json { $[$ns : $js],* }) => do let mut fields := #[] for n in ns, j in js do fields := fields.push (← `(($(quote n.getId.getString!), json $j))) `(mkObj [$fields,*]) | `(json [ $[$js],* ]) => do let mut fields := #[] for j in js do fields := fields.push (← `(json $j)) `(Json.arr #[$fields,*]) def checkToJson [ToJson α] (obj : α) (rhs : Json) : MetaM Unit := let lhs := (obj |> toJson).pretty if lhs == rhs.pretty then () else throwError "{lhs} ≟ {rhs}" def checkRoundTrip [Repr α] [BEq α] [ToJson α] [FromJson α] (obj : α) : MetaM Unit := let roundTripped := obj |> toJson |> fromJson? if let some roundTripped := roundTripped then if obj == roundTripped then () else throwError "{repr obj} ≟ {repr roundTripped}" else throwError "couldn't parse: {repr obj} ≟ {obj |> toJson}" -- set_option trace.Meta.debug true in structure Foo where x : Nat y : String deriving ToJson, FromJson, Repr, BEq #eval checkToJson { x := 1, y := "bla" : Foo} (json { y : "bla", x : 1 }) #eval checkRoundTrip { x := 1, y := "bla" : Foo } -- set_option trace.Elab.command true structure WInfo where a : Nat b : Nat deriving ToJson, FromJson, Repr, BEq -- set_option trace.Elab.command true inductive E | W : WInfo → E | WAlt (a b : Nat) | X : Nat → Nat → E | Y : Nat → E | YAlt (a : Nat) | Z deriving ToJson, FromJson, Repr, BEq #eval checkToJson (E.W { a := 2, b := 3}) (json { W : { a : 2, b : 3 } }) #eval checkRoundTrip (E.W { a := 2, b := 3 }) #eval checkToJson (E.WAlt 2 3) (json { WAlt : { a : 2, b : 3 } }) #eval checkRoundTrip (E.WAlt 2 3) #eval checkToJson (E.X 2 3) (json { X : [2, 3] }) #eval checkRoundTrip (E.X 2 3) #eval checkToJson (E.Y 4) (json { Y : 4 }) #eval checkRoundTrip (E.Y 4) #eval checkToJson (E.YAlt 5) (json { YAlt : { a : 5 } }) #eval checkRoundTrip (E.YAlt 5) #eval checkToJson E.Z (json "Z") #eval checkRoundTrip E.Z inductive ERec | mk : Nat → ERec | W : ERec → ERec deriving ToJson, FromJson, Repr, BEq #eval checkToJson (ERec.W (ERec.mk 6)) (json { W : { mk : 6 }}) #eval checkRoundTrip (ERec.mk 7) #eval checkRoundTrip (ERec.W (ERec.mk 8))
b61b7984102de4afe89023f41630a08458cd61b6
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/t2.lean
fb21d6e8b16e857b98e14bd8db1106459ca16b65
[ "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
38
lean
#print "hello world" #print "testing"
6884e9d2815bd8e12b94f388cb27e8cb40563b6d
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/06_Inductive_Types.org.31.lean
a10580074a94b1bf9425908eb94f54fa49ff6611
[]
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
1,012
lean
/- page 87 -/ import standard namespace hide inductive nat : Type := | zero : nat | succ : nat → nat namespace nat definition add (m n : nat) : nat := nat.rec_on n m (fun n add_m_n, succ add_m_n) notation 0 := zero infix `+` := add theorem add_zero (m : nat) : m + 0 = m := rfl theorem add_succ (m n : nat) : m + succ n = succ (m + n) := rfl local abbreviation induction_on := @nat.induction_on theorem zero_add (n : nat) : 0 + n = n := induction_on n (show 0 + 0 = 0, from rfl) (take n, assume IH : 0 + n = n, show 0 + succ n = succ n, from calc 0 + succ n = succ (0 + n) : rfl ... = succ n : IH) attribute add [reducible] theorem add_assoc (m n k : nat) : m + n + k = m + (n + k) := induction_on k rfl (fun k IH, eq.subst IH rfl) -- BEGIN theorem add_comm (m n : nat) : m + n = n + m := induction_on n (show m + 0 = 0 + m, from eq.symm (zero_add m)) (take n, assume IH : m + n = n + m, calc m + succ n = succ (m + n) : rfl ... = succ (n + m) : IH ... = succ n + m : sorry) -- END end nat end hide
7e1698c7cfed766adc2dd82401d107af7e76a9e9
efa51dd2edbbbbd6c34bd0ce436415eb405832e7
/20150803_CADE/examples/lua.lean
e64007048406122fd77d131390f1e3efb0d466b0
[ "Apache-2.0" ]
permissive
leanprover/presentations
dd031a05bcb12c8855676c77e52ed84246bd889a
3ce2d132d299409f1de269fa8e95afa1333d644e
refs/heads/master
1,688,703,388,796
1,686,838,383,000
1,687,465,742,000
29,750,158
12
9
Apache-2.0
1,540,211,670,000
1,422,042,683,000
Lean
UTF-8
Lean
false
false
680
lean
import data.nat open nat definition f (a : nat) : nat := a + 10 (* local env = get_env() local f_decl = env:get("f") -- Traverse subterms of f f_decl:value():for_each(function (a) print(">> " .. tostring(a)) end) local nat = Const("nat") -- Create the term λ x, f x + f x local add = Const({"nat","add"}) local f = Const("f") local x = Local("x", nat) local val = Fun(x, add(f(x), f(x))) -- Create a new definition using the term above local g_decl = mk_definition("g", mk_arrow(nat, nat), val) -- Type check it and get a "certified declaration" local g_cdecl = type_check(env, g_decl) set_env(env:add(g_decl)) *) check g eval g 1
875c73aea05a3f1d968d6ecc8d7e4ef5531b720f
367134ba5a65885e863bdc4507601606690974c1
/src/ring_theory/ideal/prod.lean
c776343778ae972b9b1cc51ffca9377e49189f6f
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
7,515
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 ring_theory.ideal.operations /-! # Ideals in product rings For commutative rings `R` and `S` and ideals `I ≤ R`, `J ≤ S`, we define `ideal.prod I J` as the product `I × J`, viewed as an ideal of `R × S`. In `ideal_prod_eq` we show that every ideal of `R × S` is of this form. Furthermore, we show that every prime ideal of `R × S` is of the form `p × S` or `R × p`, where `p` is a prime ideal. -/ universes u v variables {R : Type u} {S : Type v} [comm_ring R] [comm_ring S] (I I' : ideal R) (J J' : ideal S) namespace ideal /-- `I × J` as an ideal of `R × S`. -/ def prod : ideal (R × S) := { carrier := { x | x.fst ∈ I ∧ x.snd ∈ J }, zero_mem' := by simp, add_mem' := begin rintros ⟨a₁, a₂⟩ ⟨b₁, b₂⟩ ⟨ha₁, ha₂⟩ ⟨hb₁, hb₂⟩, exact ⟨I.add_mem ha₁ hb₁, J.add_mem ha₂ hb₂⟩ end, smul_mem' := begin rintros ⟨a₁, a₂⟩ ⟨b₁, b₂⟩ ⟨hb₁, hb₂⟩, exact ⟨I.mul_mem_left _ hb₁, J.mul_mem_left _ hb₂⟩, end } @[simp] lemma mem_prod {r : R} {s : S} : (⟨r, s⟩ : R × S) ∈ prod I J ↔ r ∈ I ∧ s ∈ J := iff.rfl @[simp] lemma prod_top_top : prod (⊤ : ideal R) (⊤ : ideal S) = ⊤ := ideal.ext $ by simp /-- Every ideal of the product ring is of the form `I × J`, where `I` and `J` can be explicitly given as the image under the projection maps. -/ theorem ideal_prod_eq (I : ideal (R × S)) : I = ideal.prod (map (ring_hom.fst R S) I) (map (ring_hom.snd R S) I) := begin apply ideal.ext, rintro ⟨r, s⟩, rw [mem_prod, mem_map_iff_of_surjective (ring_hom.fst R S) prod.fst_surjective, mem_map_iff_of_surjective (ring_hom.snd R S) prod.snd_surjective], refine ⟨λ h, ⟨⟨_, ⟨h, rfl⟩⟩, ⟨_, ⟨h, rfl⟩⟩⟩, _⟩, rintro ⟨⟨⟨r, s'⟩, ⟨h₁, rfl⟩⟩, ⟨⟨r', s⟩, ⟨h₂, rfl⟩⟩⟩, simpa using I.add_mem (I.mul_mem_right (1, 0) h₁) (I.mul_mem_right (0, 1) h₂) end @[simp] lemma map_fst_prod (I : ideal R) (J : ideal S) : map (ring_hom.fst R S) (prod I J) = I := begin ext, rw mem_map_iff_of_surjective (ring_hom.fst R S) prod.fst_surjective, exact ⟨by { rintro ⟨x, ⟨h, rfl⟩⟩, exact h.1 }, λ h, ⟨⟨x, 0⟩, ⟨⟨h, ideal.zero_mem _⟩, rfl⟩⟩⟩ end @[simp] lemma map_snd_prod (I : ideal R) (J : ideal S) : map (ring_hom.snd R S) (prod I J) = J := begin ext, rw mem_map_iff_of_surjective (ring_hom.snd R S) prod.snd_surjective, exact ⟨by { rintro ⟨x, ⟨h, rfl⟩⟩, exact h.2 }, λ h, ⟨⟨0, x⟩, ⟨⟨ideal.zero_mem _, h⟩, rfl⟩⟩⟩ end @[simp] lemma map_prod_comm_prod : map ↑(ring_equiv.prod_comm : R × S ≃+* S × R) (prod I J) = prod J I := begin refine trans (ideal_prod_eq _) _, simp [map_map], end /-- Ideals of `R × S` are in one-to-one correspondence with pairs of ideals of `R` and ideals of `S`. -/ def ideal_prod_equiv : ideal (R × S) ≃ ideal R × ideal S := { to_fun := λ I, ⟨map (ring_hom.fst R S) I, map (ring_hom.snd R S) I⟩, inv_fun := λ I, prod I.1 I.2, left_inv := λ I, (ideal_prod_eq I).symm, right_inv := λ ⟨I, J⟩, by simp } @[simp] lemma ideal_prod_equiv_symm_apply (I : ideal R) (J : ideal S) : ideal_prod_equiv.symm ⟨I, J⟩ = prod I J := rfl lemma prod.ext_iff {I I' : ideal R} {J J' : ideal S} : prod I J = prod I' J' ↔ I = I' ∧ J = J' := by simp only [←ideal_prod_equiv_symm_apply, ideal_prod_equiv.symm.injective.eq_iff, prod.mk.inj_iff] lemma is_prime_of_is_prime_prod_top {I : ideal R} (h : (ideal.prod I (⊤ : ideal S)).is_prime) : I.is_prime := begin split, { unfreezingI { contrapose! h }, simp [is_prime_iff, h] }, { intros x y hxy, have : (⟨x, 1⟩ : R × S) * ⟨y, 1⟩ ∈ prod I ⊤, { rw [prod.mk_mul_mk, mul_one, mem_prod], exact ⟨hxy, trivial⟩ }, simpa using h.mem_or_mem this } end lemma is_prime_of_is_prime_prod_top' {I : ideal S} (h : (ideal.prod (⊤ : ideal R) I).is_prime) : I.is_prime := begin apply @is_prime_of_is_prime_prod_top _ R, rw ←map_prod_comm_prod, exact map_is_prime_of_equiv _ end lemma is_prime_ideal_prod_top {I : ideal R} [h : I.is_prime] : (prod I (⊤ : ideal S)).is_prime := begin split, { unfreezingI { rcases h with ⟨h, -⟩, contrapose! h }, rw [←prod_top_top, prod.ext_iff] at h, exact h.1 }, rintros ⟨r₁, s₁⟩ ⟨r₂, s₂⟩ ⟨h₁, h₂⟩, cases h.mem_or_mem h₁ with h h, { exact or.inl ⟨h, trivial⟩ }, { exact or.inr ⟨h, trivial⟩ } end lemma is_prime_ideal_prod_top' {I : ideal S} [h : I.is_prime] : (prod (⊤ : ideal R) I).is_prime := begin rw ←map_prod_comm_prod, apply map_is_prime_of_equiv _, exact is_prime_ideal_prod_top, end lemma ideal_prod_prime_aux {I : ideal R} {J : ideal S} : (ideal.prod I J).is_prime → I = ⊤ ∨ J = ⊤ := begin contrapose!, simp only [ne_top_iff_one, is_prime_iff, not_and, not_forall, not_or_distrib], exact λ ⟨hI, hJ⟩ hIJ, ⟨⟨0, 1⟩, ⟨1, 0⟩, by simp, by simp [hJ], by simp [hI]⟩ end /-- Classification of prime ideals in product rings: the prime ideals of `R × S` are precisely the ideals of the form `p × S` or `R × p`, where `p` is a prime ideal of `R` or `S`. -/ theorem ideal_prod_prime (I : ideal (R × S)) : I.is_prime ↔ ((∃ p : ideal R, p.is_prime ∧ I = ideal.prod p ⊤) ∨ (∃ p : ideal S, p.is_prime ∧ I = ideal.prod ⊤ p)) := begin split, { rw ideal_prod_eq I, introsI hI, rcases ideal_prod_prime_aux hI with (h|h), { right, rw h at hI ⊢, exact ⟨_, ⟨is_prime_of_is_prime_prod_top' hI, rfl⟩⟩ }, { left, rw h at hI ⊢, exact ⟨_, ⟨is_prime_of_is_prime_prod_top hI, rfl⟩⟩ } }, { rintro (⟨p, ⟨h, rfl⟩⟩|⟨p, ⟨h, rfl⟩⟩), { exactI is_prime_ideal_prod_top }, { exactI is_prime_ideal_prod_top' } } end @[simp] private def prime_ideals_equiv_impl : { I : ideal R // I.is_prime } ⊕ { J : ideal S // J.is_prime } → { K : ideal (R × S) // K.is_prime } | (sum.inl ⟨I, hI⟩) := ⟨ideal.prod I ⊤, by exactI is_prime_ideal_prod_top⟩ | (sum.inr ⟨J, hJ⟩) := ⟨ideal.prod ⊤ J, by exactI is_prime_ideal_prod_top'⟩ section variables (R S) /-- The prime ideals of `R × S` are in bijection with the disjoint union of the prime ideals of `R` and the prime ideals of `S`. -/ noncomputable def prime_ideals_equiv : { K : ideal (R × S) // K.is_prime } ≃ { I : ideal R // I.is_prime } ⊕ { J : ideal S // J.is_prime } := equiv.symm $ equiv.of_bijective prime_ideals_equiv_impl begin split, { rintros (⟨I, hI⟩|⟨J, hJ⟩) (⟨I', hI'⟩|⟨J', hJ'⟩) h; simp [prod.ext_iff] at h, { simp [h] }, { exact false.elim (hI.ne_top h.1) }, { exact false.elim (hJ.ne_top h.2) }, { simp [h] } }, { rintro ⟨I, hI⟩, rcases (ideal_prod_prime I).1 hI with (⟨p, ⟨hp, rfl⟩⟩|⟨p, ⟨hp, rfl⟩⟩), { exact ⟨sum.inl ⟨p, hp⟩, rfl⟩ }, { exact ⟨sum.inr ⟨p, hp⟩, rfl⟩ } } end end @[simp] lemma prime_ideals_equiv_symm_inl (h : I.is_prime) : (prime_ideals_equiv R S).symm (sum.inl ⟨I, h⟩) = ⟨prod I ⊤, by exactI is_prime_ideal_prod_top⟩ := rfl @[simp] lemma prime_ideals_equiv_symm_inr (h : J.is_prime) : (prime_ideals_equiv R S).symm (sum.inr ⟨J, h⟩) = ⟨prod ⊤ J, by exactI is_prime_ideal_prod_top'⟩ := rfl end ideal
ab07f4f0152937fde2199046dfa5b0f7aa392787
aa2345b30d710f7e75f13157a35845ee6d48c017
/order/bounded_lattice.lean
2a361f3336f90a3b3721a91f8e5cba6b94561054
[ "Apache-2.0" ]
permissive
CohenCyril/mathlib
5241b20a3fd0ac0133e48e618a5fb7761ca7dcbe
a12d5a192f5923016752f638d19fc1a51610f163
refs/heads/master
1,586,031,957,957
1,541,432,824,000
1,541,432,824,000
156,246,337
0
0
Apache-2.0
1,541,434,514,000
1,541,434,513,000
null
UTF-8
Lean
false
false
23,909
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 Defines bounded lattice type class hierarchy. Includes the Prop and fun instances. -/ import order.lattice data.option tactic.pi_instances set_option old_structure_cmd true universes u v namespace lattice variable {α : Type u} /-- Typeclass for the `⊤` (`\top`) notation -/ class has_top (α : Type u) := (top : α) /-- Typeclass for the `⊥` (`\bot`) notation -/ class has_bot (α : Type u) := (bot : α) notation `⊤` := has_top.top _ notation `⊥` := has_bot.bot _ /-- An `order_top` is a partial order with a maximal element. (We could state this on preorders, but then it wouldn't be unique so distinguishing one would seem odd.) -/ class order_top (α : Type u) extends has_top α, partial_order α := (le_top : ∀ a : α, a ≤ ⊤) section order_top variables [order_top α] {a b : α} @[simp] theorem le_top : a ≤ ⊤ := order_top.le_top a theorem top_unique (h : ⊤ ≤ a) : a = ⊤ := le_antisymm le_top h -- TODO: delete in favor of the next? theorem eq_top_iff : a = ⊤ ↔ ⊤ ≤ a := ⟨assume eq, eq.symm ▸ le_refl ⊤, top_unique⟩ @[simp] theorem top_le_iff : ⊤ ≤ a ↔ a = ⊤ := ⟨top_unique, λ h, h.symm ▸ le_refl ⊤⟩ @[simp] theorem not_top_lt : ¬ ⊤ < a := assume h, lt_irrefl a (lt_of_le_of_lt le_top h) theorem eq_top_mono (h : a ≤ b) (h₂ : a = ⊤) : b = ⊤ := top_le_iff.1 $ h₂ ▸ h end order_top theorem order_top.ext_top {α} {A B : order_top α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : (by haveI := A; exact ⊤ : α) = ⊤ := top_unique $ by rw ← H; apply le_top theorem order_top.ext {α} {A B : order_top α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : A = B := begin haveI this := partial_order.ext H, have tt := order_top.ext_top H, cases A; cases B; injection this; congr' end /-- An `order_bot` is a partial order with a minimal element. (We could state this on preorders, but then it wouldn't be unique so distinguishing one would seem odd.) -/ class order_bot (α : Type u) extends has_bot α, partial_order α := (bot_le : ∀ a : α, ⊥ ≤ a) section order_bot variables [order_bot α] {a b : α} @[simp] theorem bot_le : ⊥ ≤ a := order_bot.bot_le a theorem bot_unique (h : a ≤ ⊥) : a = ⊥ := le_antisymm h bot_le -- TODO: delete? theorem eq_bot_iff : a = ⊥ ↔ a ≤ ⊥ := ⟨assume eq, eq.symm ▸ le_refl ⊥, bot_unique⟩ @[simp] theorem le_bot_iff : a ≤ ⊥ ↔ a = ⊥ := ⟨bot_unique, assume h, h.symm ▸ le_refl ⊥⟩ @[simp] theorem not_lt_bot : ¬ a < ⊥ := assume h, lt_irrefl a (lt_of_lt_of_le h bot_le) theorem neq_bot_of_le_neq_bot {a b : α} (hb : b ≠ ⊥) (hab : b ≤ a) : a ≠ ⊥ := assume ha, hb $ bot_unique $ ha ▸ hab theorem eq_bot_mono (h : a ≤ b) (h₂ : b = ⊥) : a = ⊥ := le_bot_iff.1 $ h₂ ▸ h end order_bot theorem order_bot.ext_bot {α} {A B : order_bot α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : (by haveI := A; exact ⊥ : α) = ⊥ := bot_unique $ by rw ← H; apply bot_le theorem order_bot.ext {α} {A B : order_bot α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : A = B := begin haveI this := partial_order.ext H, have tt := order_bot.ext_bot H, cases A; cases B; injection this; congr' end /-- A `semilattice_sup_top` is a semilattice with top and join. -/ class semilattice_sup_top (α : Type u) extends order_top α, semilattice_sup α section semilattice_sup_top variables [semilattice_sup_top α] {a : α} @[simp] theorem top_sup_eq : ⊤ ⊔ a = ⊤ := sup_of_le_left le_top @[simp] theorem sup_top_eq : a ⊔ ⊤ = ⊤ := sup_of_le_right le_top end semilattice_sup_top /-- A `semilattice_sup_bot` is a semilattice with bottom and join. -/ class semilattice_sup_bot (α : Type u) extends order_bot α, semilattice_sup α section semilattice_sup_bot variables [semilattice_sup_bot α] {a b : α} @[simp] theorem bot_sup_eq : ⊥ ⊔ a = a := sup_of_le_right bot_le @[simp] theorem sup_bot_eq : a ⊔ ⊥ = a := sup_of_le_left bot_le @[simp] theorem sup_eq_bot_iff : a ⊔ b = ⊥ ↔ (a = ⊥ ∧ b = ⊥) := by rw [eq_bot_iff, sup_le_iff]; simp end semilattice_sup_bot instance nat.semilattice_sup_bot : semilattice_sup_bot ℕ := { bot := 0, bot_le := nat.zero_le, .. nat.distrib_lattice } /-- A `semilattice_inf_top` is a semilattice with top and meet. -/ class semilattice_inf_top (α : Type u) extends order_top α, semilattice_inf α section semilattice_inf_top variables [semilattice_inf_top α] {a b : α} @[simp] theorem top_inf_eq : ⊤ ⊓ a = a := inf_of_le_right le_top @[simp] theorem inf_top_eq : a ⊓ ⊤ = a := inf_of_le_left le_top @[simp] theorem inf_eq_top_iff : a ⊓ b = ⊤ ↔ (a = ⊤ ∧ b = ⊤) := by rw [eq_top_iff, le_inf_iff]; simp end semilattice_inf_top /-- A `semilattice_inf_bot` is a semilattice with bottom and meet. -/ class semilattice_inf_bot (α : Type u) extends order_bot α, semilattice_inf α section semilattice_inf_bot variables [semilattice_inf_bot α] {a : α} @[simp] theorem bot_inf_eq : ⊥ ⊓ a = ⊥ := inf_of_le_left bot_le @[simp] theorem inf_bot_eq : a ⊓ ⊥ = ⊥ := inf_of_le_right bot_le end semilattice_inf_bot /- Bounded lattices -/ /-- A bounded lattice is a lattice with a top and bottom element, denoted `⊤` and `⊥` respectively. This allows for the interpretation of all finite suprema and infima, taking `inf ∅ = ⊤` and `sup ∅ = ⊥`. -/ class bounded_lattice (α : Type u) extends lattice α, order_top α, order_bot α instance semilattice_inf_top_of_bounded_lattice (α : Type u) [bl : bounded_lattice α] : semilattice_inf_top α := { le_top := assume x, @le_top α _ x, ..bl } instance semilattice_inf_bot_of_bounded_lattice (α : Type u) [bl : bounded_lattice α] : semilattice_inf_bot α := { bot_le := assume x, @bot_le α _ x, ..bl } instance semilattice_sup_top_of_bounded_lattice (α : Type u) [bl : bounded_lattice α] : semilattice_sup_top α := { le_top := assume x, @le_top α _ x, ..bl } instance semilattice_sup_bot_of_bounded_lattice (α : Type u) [bl : bounded_lattice α] : semilattice_sup_bot α := { bot_le := assume x, @bot_le α _ x, ..bl } theorem bounded_lattice.ext {α} {A B : bounded_lattice α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : A = B := begin haveI H1 : @bounded_lattice.to_lattice α A = @bounded_lattice.to_lattice α B := lattice.ext H, haveI H2 := order_bot.ext H, haveI H3 : @bounded_lattice.to_order_top α A = @bounded_lattice.to_order_top α B := order_top.ext H, have tt := order_bot.ext_bot H, cases A; cases B; injection H1; injection H2; injection H3; congr' end /-- A bounded distributive lattice is exactly what it sounds like. -/ class bounded_distrib_lattice α extends distrib_lattice α, bounded_lattice α lemma inf_eq_bot_iff_le_compl {α : Type u} [bounded_distrib_lattice α] {a b c : α} (h₁ : b ⊔ c = ⊤) (h₂ : b ⊓ c = ⊥) : a ⊓ b = ⊥ ↔ a ≤ c := ⟨assume : a ⊓ b = ⊥, calc a ≤ a ⊓ (b ⊔ c) : by simp [h₁] ... = (a ⊓ b) ⊔ (a ⊓ c) : by simp [inf_sup_left] ... ≤ c : by simp [this, inf_le_right], assume : a ≤ c, bot_unique $ calc a ⊓ b ≤ b ⊓ c : by rw [inf_comm]; exact inf_le_inf (le_refl _) this ... = ⊥ : h₂⟩ /- Prop instance -/ instance bounded_lattice_Prop : bounded_lattice Prop := { lattice.bounded_lattice . le := λa b, a → b, le_refl := assume _, id, le_trans := assume a b c f g, g ∘ f, le_antisymm := assume a b Hab Hba, propext ⟨Hab, Hba⟩, sup := or, le_sup_left := @or.inl, le_sup_right := @or.inr, sup_le := assume a b c, or.rec, inf := and, inf_le_left := @and.left, inf_le_right := @and.right, le_inf := assume a b c Hab Hac Ha, and.intro (Hab Ha) (Hac Ha), top := true, le_top := assume a Ha, true.intro, bot := false, bot_le := @false.elim } section logic variable [preorder α] theorem monotone_and {p q : α → Prop} (m_p : monotone p) (m_q : monotone q) : monotone (λx, p x ∧ q x) := assume a b h, and.imp (m_p h) (m_q h) -- Note: by finish [monotone] doesn't work theorem monotone_or {p q : α → Prop} (m_p : monotone p) (m_q : monotone q) : monotone (λx, p x ∨ q x) := assume a b h, or.imp (m_p h) (m_q h) end logic /- Function lattices -/ /- TODO: * build up the lattice hierarchy for `fun`-functor piecewise. semilattic_*, bounded_lattice, lattice ... * can this be generalized to the dependent function space? -/ instance pi.bounded_lattice {α : Type u} {β : Type v} [bounded_lattice β] : bounded_lattice (α → β) := by pi_instance end lattice def with_bot (α : Type*) := option α namespace with_bot variable {α : Type u} open lattice instance : has_coe_t α (with_bot α) := ⟨some⟩ instance has_bot : has_bot (with_bot α) := ⟨none⟩ lemma none_eq_bot : (none : with_bot α) = (⊥ : with_bot α) := rfl lemma some_eq_coe (a : α) : (some a : with_bot α) = (↑a : with_bot α) := rfl theorem coe_eq_coe {a b : α} : (a : with_bot α) = b ↔ a = b := by rw [← option.some.inj_eq a b]; refl instance partial_order [partial_order α] : partial_order (with_bot α) := { le := λ o₁ o₂ : option α, ∀ a ∈ o₁, ∃ b ∈ o₂, a ≤ b, le_refl := λ o a ha, ⟨a, ha, le_refl _⟩, le_trans := λ o₁ o₂ o₃ h₁ h₂ a ha, let ⟨b, hb, ab⟩ := h₁ a ha, ⟨c, hc, bc⟩ := h₂ b hb in ⟨c, hc, le_trans ab bc⟩, le_antisymm := λ o₁ o₂ h₁ h₂, begin cases o₁ with a, { cases o₂ with b, {refl}, rcases h₂ b rfl with ⟨_, ⟨⟩, _⟩ }, { rcases h₁ a rfl with ⟨b, ⟨⟩, h₁'⟩, rcases h₂ b rfl with ⟨_, ⟨⟩, h₂'⟩, rw le_antisymm h₁' h₂' } end } instance order_bot [partial_order α] : order_bot (with_bot α) := { bot_le := λ a a' h, option.no_confusion h, ..with_bot.partial_order, ..with_bot.has_bot } @[simp] theorem coe_le_coe [partial_order α] {a b : α} : (a : with_bot α) ≤ b ↔ a ≤ b := ⟨λ h, by rcases h a rfl with ⟨_, ⟨⟩, h⟩; exact h, λ h a' e, option.some_inj.1 e ▸ ⟨b, rfl, h⟩⟩ @[simp] theorem some_le_some [partial_order α] {a b : α} : @has_le.le (with_bot α) _ (some a) (some b) ↔ a ≤ b := coe_le_coe theorem coe_le [partial_order α] {a b : α} : ∀ {o : option α}, b ∈ o → ((a : with_bot α) ≤ o ↔ a ≤ b) | _ rfl := coe_le_coe @[simp] theorem some_lt_some [partial_order α] {a b : α} : @has_lt.lt (with_bot α) _ (some a) (some b) ↔ a < b := (and_congr some_le_some (not_congr some_le_some)) .trans lt_iff_le_not_le.symm lemma coe_lt_coe [partial_order α] {a b : α} : (a : with_bot α) < b ↔ a < b := some_lt_some lemma bot_lt_some [partial_order α] (a : α) : (⊥ : with_bot α) < some a := lt_of_le_of_ne bot_le (λ h, option.no_confusion h) lemma bot_lt_coe [partial_order α] (a : α) : (⊥ : with_bot α) < a := bot_lt_some a instance linear_order [linear_order α] : linear_order (with_bot α) := { le_total := λ o₁ o₂, begin cases o₁ with a, {exact or.inl bot_le}, cases o₂ with b, {exact or.inr bot_le}, simp [le_total] end, ..with_bot.partial_order } instance decidable_linear_order [decidable_linear_order α] : decidable_linear_order (with_bot α) := { decidable_le := λ a b, begin cases a with a, { exact is_true bot_le }, cases b with b, { exact is_false (mt (le_antisymm bot_le) (by simp)) }, { exact decidable_of_iff _ some_le_some } end, ..with_bot.linear_order } instance semilattice_sup [semilattice_sup α] : semilattice_sup_bot (with_bot α) := { sup := option.lift_or_get (⊔), le_sup_left := λ o₁ o₂ a ha, by cases ha; cases o₂; simp [option.lift_or_get], le_sup_right := λ o₁ o₂ a ha, by cases ha; cases o₁; simp [option.lift_or_get], sup_le := λ o₁ o₂ o₃ h₁ h₂ a ha, begin cases o₁ with b; cases o₂ with c; cases ha, { exact h₂ a rfl }, { exact h₁ a rfl }, { rcases h₁ b rfl with ⟨d, ⟨⟩, h₁'⟩, simp at h₂, exact ⟨d, rfl, sup_le h₁' h₂⟩ } end, ..with_bot.order_bot } instance semilattice_inf [semilattice_inf α] : semilattice_inf_bot (with_bot α) := { inf := λ o₁ o₂, o₁.bind (λ a, o₂.map (λ b, a ⊓ b)), inf_le_left := λ o₁ o₂ a ha, begin simp at ha, rcases ha with ⟨b, rfl, c, rfl, rfl⟩, exact ⟨_, rfl, inf_le_left⟩ end, inf_le_right := λ o₁ o₂ a ha, begin simp at ha, rcases ha with ⟨b, rfl, c, rfl, rfl⟩, exact ⟨_, rfl, inf_le_right⟩ end, le_inf := λ o₁ o₂ o₃ h₁ h₂ a ha, begin cases ha, rcases h₁ a rfl with ⟨b, ⟨⟩, ab⟩, rcases h₂ a rfl with ⟨c, ⟨⟩, ac⟩, exact ⟨_, rfl, le_inf ab ac⟩ end, ..with_bot.order_bot } instance lattice [lattice α] : lattice (with_bot α) := { ..with_bot.semilattice_sup, ..with_bot.semilattice_inf } theorem lattice_eq_DLO [decidable_linear_order α] : lattice.lattice_of_decidable_linear_order = @with_bot.lattice α _ := lattice.ext $ λ x y, iff.rfl theorem sup_eq_max [decidable_linear_order α] (x y : with_bot α) : x ⊔ y = max x y := by rw [← sup_eq_max, lattice_eq_DLO] theorem inf_eq_min [decidable_linear_order α] (x y : with_bot α) : x ⊓ y = min x y := by rw [← inf_eq_min, lattice_eq_DLO] instance order_top [order_top α] : order_top (with_bot α) := { top := some ⊤, le_top := λ o a ha, by cases ha; exact ⟨_, rfl, le_top⟩, ..with_bot.partial_order } instance bounded_lattice [bounded_lattice α] : bounded_lattice (with_bot α) := { ..with_bot.lattice, ..with_bot.order_top, ..with_bot.order_bot } lemma well_founded_lt [partial_order α] (h : well_founded ((<) : α → α → Prop)) : well_founded ((<) : with_bot α → with_bot α → Prop) := have acc_bot : acc ((<) : with_bot α → with_bot α → Prop) ⊥ := acc.intro _ (λ a ha, (not_le_of_gt ha bot_le).elim), ⟨λ a, option.rec_on a acc_bot (λ a, acc.intro _ (λ b, option.rec_on b (λ _, acc_bot) (λ b, well_founded.induction h b (show ∀ b : α, (∀ c, c < b → (c : with_bot α) < a → acc ((<) : with_bot α → with_bot α → Prop) c) → (b : with_bot α) < a → acc ((<) : with_bot α → with_bot α → Prop) b, from λ b ih hba, acc.intro _ (λ c, option.rec_on c (λ _, acc_bot) (λ c hc, ih _ (some_lt_some.1 hc) (lt_trans hc hba)))))))⟩ instance densely_ordered [partial_order α] [densely_ordered α] [no_bot_order α] : densely_ordered (with_bot α) := ⟨ assume a b, match a, b with | a, none := assume h : a < ⊥, (not_lt_bot h).elim | none, some b := assume h, let ⟨a, ha⟩ := no_bot b in ⟨a, bot_lt_coe a, coe_lt_coe.2 ha⟩ | some a, some b := assume h, let ⟨a, ha₁, ha₂⟩ := dense (coe_lt_coe.1 h) in ⟨a, coe_lt_coe.2 ha₁, coe_lt_coe.2 ha₂⟩ end⟩ end with_bot --TODO(Mario): Construct using order dual on with_bot def with_top (α : Type*) := option α namespace with_top variable {α : Type u} open lattice instance : has_coe_t α (with_top α) := ⟨some⟩ instance has_top : has_top (with_top α) := ⟨none⟩ lemma none_eq_top : (none : with_top α) = (⊤ : with_top α) := rfl lemma some_eq_coe (a : α) : (some a : with_top α) = (↑a : with_top α) := rfl theorem coe_eq_coe {a b : α} : (a : with_top α) = b ↔ a = b := by rw [← option.some.inj_eq a b]; refl @[simp] theorem top_ne_coe [partial_order α] {a : α} : ⊤ ≠ (a : with_top α) . @[simp] theorem coe_ne_top [partial_order α] {a : α} : (a : with_top α) ≠ ⊤ . instance partial_order [partial_order α] : partial_order (with_top α) := { le := λ o₁ o₂ : option α, ∀ b ∈ o₂, ∃ a ∈ o₁, a ≤ b, le_refl := λ o a ha, ⟨a, ha, le_refl _⟩, le_trans := λ o₁ o₂ o₃ h₁ h₂ c hc, let ⟨b, hb, bc⟩ := h₂ c hc, ⟨a, ha, ab⟩ := h₁ b hb in ⟨a, ha, le_trans ab bc⟩, le_antisymm := λ o₁ o₂ h₁ h₂, begin cases o₂ with b, { cases o₁ with a, {refl}, rcases h₂ a rfl with ⟨_, ⟨⟩, _⟩ }, { rcases h₁ b rfl with ⟨a, ⟨⟩, h₁'⟩, rcases h₂ a rfl with ⟨_, ⟨⟩, h₂'⟩, rw le_antisymm h₁' h₂' } end } instance order_top [partial_order α] : order_top (with_top α) := { le_top := λ a a' h, option.no_confusion h, ..with_top.partial_order, .. with_top.has_top } @[simp] theorem coe_le_coe [partial_order α] {a b : α} : (a : with_top α) ≤ b ↔ a ≤ b := ⟨λ h, by rcases h b rfl with ⟨_, ⟨⟩, h⟩; exact h, λ h a' e, option.some_inj.1 e ▸ ⟨a, rfl, h⟩⟩ @[simp] theorem some_le_some [partial_order α] {a b : α} : @has_le.le (with_top α) _ (some a) (some b) ↔ a ≤ b := coe_le_coe theorem le_coe [partial_order α] {a b : α} : ∀ {o : option α}, a ∈ o → (@has_le.le (with_top α) _ o b ↔ a ≤ b) | _ rfl := coe_le_coe theorem le_coe_iff [partial_order α] (b : α) : ∀(x : with_top α), x ≤ b ↔ (∃a:α, x = a ∧ a ≤ b) | (some a) := by simp [some_eq_coe, coe_eq_coe] | none := by simp [none_eq_top] theorem coe_le_iff [partial_order α] (a : α) : ∀(x : with_top α), ↑a ≤ x ↔ (∀b:α, x = ↑b → a ≤ b) | (some b) := by simp [some_eq_coe, coe_eq_coe] | none := by simp [none_eq_top] theorem lt_iff_exists_coe [partial_order α] : ∀(a b : with_top α), a < b ↔ (∃p:α, a = p ∧ ↑p < b) | (some a) b := by simp [some_eq_coe, coe_eq_coe] | none b := by simp [none_eq_top] @[simp] theorem some_lt_some [partial_order α] {a b : α} : @has_lt.lt (with_top α) _ (some a) (some b) ↔ a < b := (and_congr some_le_some (not_congr some_le_some)) .trans lt_iff_le_not_le.symm lemma coe_lt_coe [partial_order α] {a b : α} : (a : with_top α) < b ↔ a < b := some_lt_some lemma coe_lt_top [partial_order α] (a : α) : (a : with_top α) < ⊤ := lt_of_le_of_ne le_top (λ h, option.no_confusion h) lemma not_top_le_coe [partial_order α] (a : α) : ¬ (⊤:with_top α) ≤ ↑a := assume h, (lt_irrefl ⊤ (lt_of_le_of_lt h (coe_lt_top a))).elim instance linear_order [linear_order α] : linear_order (with_top α) := { le_total := λ o₁ o₂, begin cases o₁ with a, {exact or.inr le_top}, cases o₂ with b, {exact or.inl le_top}, simp [le_total] end, ..with_top.partial_order } instance decidable_linear_order [decidable_linear_order α] : decidable_linear_order (with_top α) := { decidable_le := λ a b, begin cases b with b, { exact is_true le_top }, cases a with a, { exact is_false (mt (le_antisymm le_top) (by simp)) }, { exact decidable_of_iff _ some_le_some } end, ..with_top.linear_order } instance semilattice_inf [semilattice_inf α] : semilattice_inf_top (with_top α) := { inf := option.lift_or_get (⊓), inf_le_left := λ o₁ o₂ a ha, by cases ha; cases o₂; simp [option.lift_or_get], inf_le_right := λ o₁ o₂ a ha, by cases ha; cases o₁; simp [option.lift_or_get], le_inf := λ o₁ o₂ o₃ h₁ h₂ a ha, begin cases o₂ with b; cases o₃ with c; cases ha, { exact h₂ a rfl }, { exact h₁ a rfl }, { rcases h₁ b rfl with ⟨d, ⟨⟩, h₁'⟩, simp at h₂, exact ⟨d, rfl, le_inf h₁' h₂⟩ } end, ..with_top.order_top } lemma coe_inf [semilattice_inf α] (a b : α) : ((a ⊓ b : α) : with_top α) = a ⊓ b := rfl instance semilattice_sup [semilattice_sup α] : semilattice_sup_top (with_top α) := { sup := λ o₁ o₂, o₁.bind (λ a, o₂.map (λ b, a ⊔ b)), le_sup_left := λ o₁ o₂ a ha, begin simp at ha, rcases ha with ⟨b, rfl, c, rfl, rfl⟩, exact ⟨_, rfl, le_sup_left⟩ end, le_sup_right := λ o₁ o₂ a ha, begin simp at ha, rcases ha with ⟨b, rfl, c, rfl, rfl⟩, exact ⟨_, rfl, le_sup_right⟩ end, sup_le := λ o₁ o₂ o₃ h₁ h₂ a ha, begin cases ha, rcases h₁ a rfl with ⟨b, ⟨⟩, ab⟩, rcases h₂ a rfl with ⟨c, ⟨⟩, ac⟩, exact ⟨_, rfl, sup_le ab ac⟩ end, ..with_top.order_top } lemma coe_sup [semilattice_sup α] (a b : α) : ((a ⊔ b : α) : with_top α) = a ⊔ b := rfl instance lattice [lattice α] : lattice (with_top α) := { ..with_top.semilattice_sup, ..with_top.semilattice_inf } theorem lattice_eq_DLO [decidable_linear_order α] : lattice.lattice_of_decidable_linear_order = @with_top.lattice α _ := lattice.ext $ λ x y, iff.rfl theorem sup_eq_max [decidable_linear_order α] (x y : with_top α) : x ⊔ y = max x y := by rw [← sup_eq_max, lattice_eq_DLO] theorem inf_eq_min [decidable_linear_order α] (x y : with_top α) : x ⊓ y = min x y := by rw [← inf_eq_min, lattice_eq_DLO] instance order_bot [order_bot α] : order_bot (with_top α) := { bot := some ⊥, bot_le := λ o a ha, by cases ha; exact ⟨_, rfl, bot_le⟩, ..with_top.partial_order } instance bounded_lattice [bounded_lattice α] : bounded_lattice (with_top α) := { ..with_top.lattice, ..with_top.order_top, ..with_top.order_bot } lemma well_founded_lt {α : Type*} [partial_order α] (h : well_founded ((<) : α → α → Prop)) : well_founded ((<) : with_top α → with_top α → Prop) := have acc_some : ∀ a : α, acc ((<) : with_top α → with_top α → Prop) (some a) := λ a, acc.intro _ (well_founded.induction h a (show ∀ b, (∀ c, c < b → ∀ d : with_top α, d < some c → acc (<) d) → ∀ y : with_top α, y < some b → acc (<) y, from λ b ih c, option.rec_on c (λ hc, (not_lt_of_ge lattice.le_top hc).elim) (λ c hc, acc.intro _ (ih _ (some_lt_some.1 hc))))), ⟨λ a, option.rec_on a (acc.intro _ (λ y, option.rec_on y (λ h, (lt_irrefl _ h).elim) (λ _ _, acc_some _))) acc_some⟩ instance densely_ordered [partial_order α] [densely_ordered α] [no_top_order α] : densely_ordered (with_top α) := ⟨ assume a b, match a, b with | none, a := assume h : ⊤ < a, (not_top_lt h).elim | some a, none := assume h, let ⟨b, hb⟩ := no_top a in ⟨b, coe_lt_coe.2 hb, coe_lt_top b⟩ | some a, some b := assume h, let ⟨a, ha₁, ha₂⟩ := dense (coe_lt_coe.1 h) in ⟨a, coe_lt_coe.2 ha₁, coe_lt_coe.2 ha₂⟩ end⟩ end with_top namespace order_dual open lattice variable (α : Type*) instance [has_bot α] : has_top (order_dual α) := ⟨(⊥ : α)⟩ instance [has_top α] : has_bot (order_dual α) := ⟨(⊤ : α)⟩ instance [order_bot α] : order_top (order_dual α) := { le_top := @bot_le α _, .. order_dual.partial_order α, .. order_dual.lattice.has_top α } instance [order_top α] : order_bot (order_dual α) := { bot_le := @le_top α _, .. order_dual.partial_order α, .. order_dual.lattice.has_bot α } instance [semilattice_inf_bot α] : semilattice_sup_top (order_dual α) := { .. order_dual.lattice.semilattice_sup α, .. order_dual.lattice.order_top α } instance [semilattice_inf_top α] : semilattice_sup_bot (order_dual α) := { .. order_dual.lattice.semilattice_sup α, .. order_dual.lattice.order_bot α } instance [semilattice_sup_bot α] : semilattice_inf_top (order_dual α) := { .. order_dual.lattice.semilattice_inf α, .. order_dual.lattice.order_top α } instance [semilattice_sup_top α] : semilattice_inf_bot (order_dual α) := { .. order_dual.lattice.semilattice_inf α, .. order_dual.lattice.order_bot α } instance [bounded_lattice α] : bounded_lattice (order_dual α) := { .. order_dual.lattice.lattice α, .. order_dual.lattice.order_top α, .. order_dual.lattice.order_bot α } end order_dual
bf1efe9112f97da7259d516031802d1b6f044737
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/star/pointwise.lean
0b27e91d4062808fd0e14b5638e76dc565b57a2d
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
4,266
lean
/- Copyright (c) 2022 Jireh Loreaux. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jireh Loreaux -/ import algebra.star.basic import data.set.finite import data.set.pointwise.basic /-! # Pointwise star operation on sets This file defines the star operation pointwise on sets and provides the basic API. Besides basic facts about about how the star operation acts on sets (e.g., `(s ∩ t)⋆ = s⋆ ∩ t⋆`), if `s t : set α`, then under suitable assumption on `α`, it is shown * `(s + t)⋆ = s⋆ + t⋆` * `(s * t)⋆ = t⋆ + s⋆` * `(s⁻¹)⋆ = (s⋆)⁻¹` -/ namespace set open_locale pointwise local postfix `⋆`:std.prec.max_plus := star variables {α : Type*} {s t : set α} {a : α} /-- The set `(star s : set α)` is defined as `{x | star x ∈ s}` in locale `pointwise`. In the usual case where `star` is involutive, it is equal to `{star s | x ∈ s}`, see `set.image_star`. -/ protected def has_star [has_star α] : has_star (set α) := ⟨preimage has_star.star⟩ localized "attribute [instance] set.has_star" in pointwise @[simp] lemma star_empty [has_star α] : (∅ : set α)⋆ = ∅ := rfl @[simp] lemma star_univ [has_star α] : (univ : set α)⋆ = univ := rfl @[simp] lemma nonempty_star [has_involutive_star α] {s : set α} : (s⋆).nonempty ↔ s.nonempty := star_involutive.surjective.nonempty_preimage lemma nonempty.star [has_involutive_star α] {s : set α} (h : s.nonempty) : (s⋆).nonempty := nonempty_star.2 h @[simp] lemma mem_star [has_star α] : a ∈ s⋆ ↔ a⋆ ∈ s := iff.rfl lemma star_mem_star [has_involutive_star α] : a⋆ ∈ s⋆ ↔ a ∈ s := by simp only [mem_star, star_star] @[simp] lemma star_preimage [has_star α] : has_star.star ⁻¹' s = s⋆ := rfl @[simp] lemma image_star [has_involutive_star α] : has_star.star '' s = s⋆ := by { simp only [← star_preimage], rw [image_eq_preimage_of_inverse]; intro; simp only [star_star] } @[simp] lemma inter_star [has_star α] : (s ∩ t)⋆ = s⋆ ∩ t⋆ := preimage_inter @[simp] lemma union_star [has_star α] : (s ∪ t)⋆ = s⋆ ∪ t⋆ := preimage_union @[simp] lemma Inter_star {ι : Sort*} [has_star α] (s : ι → set α) : (⋂ i, s i)⋆ = ⋂ i, (s i)⋆ := preimage_Inter @[simp] lemma Union_star {ι : Sort*} [has_star α] (s : ι → set α) : (⋃ i, s i)⋆ = ⋃ i, (s i)⋆ := preimage_Union @[simp] lemma compl_star [has_star α] : (sᶜ)⋆ = (s⋆)ᶜ := preimage_compl @[simp] instance [has_involutive_star α] : has_involutive_star (set α) := { star := has_star.star, star_involutive := λ s, by { simp only [← star_preimage, preimage_preimage, star_star, preimage_id'] } } @[simp] lemma star_subset_star [has_involutive_star α] {s t : set α} : s⋆ ⊆ t⋆ ↔ s ⊆ t := equiv.star.surjective.preimage_subset_preimage_iff lemma star_subset [has_involutive_star α] {s t : set α} : s⋆ ⊆ t ↔ s ⊆ t⋆ := by { rw [← star_subset_star, star_star] } lemma finite.star [has_involutive_star α] {s : set α} (hs : s.finite) : s⋆.finite := hs.preimage $ star_injective.inj_on _ lemma star_singleton {β : Type*} [has_involutive_star β] (x : β) : ({x} : set β)⋆ = {x⋆} := by { ext1 y, rw [mem_star, mem_singleton_iff, mem_singleton_iff, star_eq_iff_star_eq, eq_comm], } protected lemma star_mul [monoid α] [star_semigroup α] (s t : set α) : (s * t)⋆ = t⋆ * s⋆ := by simp_rw [←image_star, ←image2_mul, image_image2, image2_image_left, image2_image_right, star_mul, image2_swap _ s t] protected lemma star_add [add_monoid α] [star_add_monoid α] (s t : set α) : (s + t)⋆ = s⋆ + t⋆ := by simp_rw [←image_star, ←image2_add, image_image2, image2_image_left, image2_image_right, star_add] @[simp] instance [has_star α] [has_trivial_star α] : has_trivial_star (set α) := { star_trivial := λ s, by { rw [←star_preimage], ext1, simp [star_trivial] } } protected lemma star_inv [group α] [star_semigroup α] (s : set α) : (s⁻¹)⋆ = (s⋆)⁻¹ := by { ext, simp only [mem_star, mem_inv, star_inv] } protected lemma star_inv' [division_ring α] [star_ring α] (s : set α) : (s⁻¹)⋆ = (s⋆)⁻¹ := by { ext, simp only [mem_star, mem_inv, star_inv'] } end set
0d93f21d6d9f2204b2ccec3f000af9e702b713bd
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/data/equiv/denumerable.lean
37be42432f6a943a4962ae625950b99d5af43bd3
[ "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
9,044
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro Denumerable (countably infinite) types, as a typeclass extending encodable. This is used to provide explicit encode/decode functions from nat, where the functions are known inverses of each other. -/ import data.equiv.encodable data.sigma data.fintype data.list.min_max open nat section prio set_option default_priority 100 -- see Note [default priority] /-- A denumerable type is one which is (constructively) bijective with ℕ. Although we already have a name for this property, namely `α ≃ ℕ`, we are here interested in using it as a typeclass. -/ class denumerable (α : Type*) extends encodable α := (decode_inv : ∀ n, ∃ a ∈ decode n, encode a = n) end prio namespace denumerable section variables {α : Type*} {β : Type*} [denumerable α] [denumerable β] open encodable theorem decode_is_some (α) [denumerable α] (n : ℕ) : (decode α n).is_some := option.is_some_iff_exists.2 $ (decode_inv α n).imp $ λ a, Exists.fst def of_nat (α) [f : denumerable α] (n : ℕ) : α := option.get (decode_is_some α n) @[simp, priority 900] theorem decode_eq_of_nat (α) [denumerable α] (n : ℕ) : decode α n = some (of_nat α n) := option.eq_some_of_is_some _ @[simp] theorem of_nat_of_decode {n b} (h : decode α n = some b) : of_nat α n = b := option.some.inj $ (decode_eq_of_nat _ _).symm.trans h @[simp] theorem encode_of_nat (n) : encode (of_nat α n) = n := let ⟨a, h, e⟩ := decode_inv α n in by rwa [of_nat_of_decode h] @[simp] theorem of_nat_encode (a) : of_nat α (encode a) = a := of_nat_of_decode (encodek _) def eqv (α) [denumerable α] : α ≃ ℕ := ⟨encode, of_nat α, of_nat_encode, encode_of_nat⟩ def mk' {α} (e : α ≃ ℕ) : denumerable α := { encode := e, decode := some ∘ e.symm, encodek := λ a, congr_arg some (e.symm_apply_apply _), decode_inv := λ n, ⟨_, rfl, e.apply_symm_apply _⟩ } def of_equiv (α) {β} [denumerable α] (e : β ≃ α) : denumerable β := { decode_inv := λ n, by simp, ..encodable.of_equiv _ e } @[simp] theorem of_equiv_of_nat (α) {β} [denumerable α] (e : β ≃ α) (n) : @of_nat β (of_equiv _ e) n = e.symm (of_nat α n) := by apply of_nat_of_decode; show option.map _ _ = _; simp def equiv₂ (α β) [denumerable α] [denumerable β] : α ≃ β := (eqv α).trans (eqv β).symm instance nat : denumerable nat := ⟨λ n, ⟨_, rfl, rfl⟩⟩ @[simp] theorem of_nat_nat (n) : of_nat ℕ n = n := rfl instance option : denumerable (option α) := ⟨λ n, by cases n; simp⟩ instance sum : denumerable (α ⊕ β) := ⟨λ n, begin suffices : ∃ a ∈ @decode_sum α β _ _ n, encode_sum a = bit (bodd n) (div2 n), {simpa [bit_decomp]}, simp [decode_sum]; cases bodd n; simp [decode_sum, bit, encode_sum] end⟩ section sigma variables {γ : α → Type*} [∀ a, denumerable (γ a)] instance sigma : denumerable (sigma γ) := ⟨λ n, by simp [decode_sigma]; exact ⟨_, _, ⟨rfl, heq.rfl⟩, by simp⟩⟩ @[simp] theorem sigma_of_nat_val (n : ℕ) : of_nat (sigma γ) n = ⟨of_nat α (unpair n).1, of_nat (γ _) (unpair n).2⟩ := option.some.inj $ by rw [← decode_eq_of_nat, decode_sigma_val]; simp; refl end sigma instance prod : denumerable (α × β) := of_equiv _ (equiv.sigma_equiv_prod α β).symm @[simp] theorem prod_of_nat_val (n : ℕ) : of_nat (α × β) n = (of_nat α (unpair n).1, of_nat β (unpair n).2) := by simp; refl @[simp] theorem prod_nat_of_nat : of_nat (ℕ × ℕ) = unpair := by funext; simp instance int : denumerable ℤ := denumerable.mk' equiv.int_equiv_nat instance pnat : denumerable ℕ+ := denumerable.mk' equiv.pnat_equiv_nat instance ulift : denumerable (ulift α) := of_equiv _ equiv.ulift instance plift : denumerable (plift α) := of_equiv _ equiv.plift def pair : α × α ≃ α := equiv₂ _ _ end end denumerable namespace nat.subtype open function encodable variables {s : set ℕ} [decidable_pred s] [infinite s] lemma exists_succ (x : s) : ∃ n, x.1 + n + 1 ∈ s := classical.by_contradiction $ λ h, have ∀ (a : ℕ) (ha : a ∈ s), a < x.val.succ, from λ a ha, lt_of_not_ge (λ hax, h ⟨a - (x.1 + 1), by rwa [add_right_comm, nat.add_sub_cancel' hax]⟩), infinite.not_fintype ⟨(((multiset.range x.1.succ).filter (∈ s)).pmap (λ (y : ℕ) (hy : y ∈ s), subtype.mk y hy) (by simp [-multiset.range_succ])).to_finset, by simpa [subtype.ext, multiset.mem_filter, -multiset.range_succ]⟩ def succ (x : s) : s := have h : ∃ m, x.1 + m + 1 ∈ s, from exists_succ x, ⟨x.1 + nat.find h + 1, nat.find_spec h⟩ lemma succ_le_of_lt {x y : s} (h : y < x) : succ y ≤ x := have hx : ∃ m, y.1 + m + 1 ∈ s, from exists_succ _, let ⟨k, hk⟩ := nat.exists_eq_add_of_lt h in have nat.find hx ≤ k, from nat.find_min' _ (hk ▸ x.2), show y.1 + nat.find hx + 1 ≤ x.1, by rw hk; exact add_le_add_right (add_le_add_left this _) _ lemma le_succ_of_forall_lt_le {x y : s} (h : ∀ z < x, z ≤ y) : x ≤ succ y := have hx : ∃ m, y.1 + m + 1 ∈ s, from exists_succ _, show x.1 ≤ y.1 + nat.find hx + 1, from le_of_not_gt $ λ hxy, have y.1 + nat.find hx + 1 ≤ y.1 := h ⟨_, nat.find_spec hx⟩ hxy, not_lt_of_le this $ calc y.1 ≤ y.1 + nat.find hx : le_add_of_nonneg_right (nat.zero_le _) ... < y.1 + nat.find hx + 1 : nat.lt_succ_self _ lemma lt_succ_self (x : s) : x < succ x := calc x.1 ≤ x.1 + _ : le_add_right (le_refl _) ... < succ x : nat.lt_succ_self (x.1 + _) lemma lt_succ_iff_le {x y : s} : x < succ y ↔ x ≤ y := ⟨λ h, le_of_not_gt (λ h', not_le_of_gt h (succ_le_of_lt h')), λ h, lt_of_le_of_lt h (lt_succ_self _)⟩ def of_nat (s : set ℕ) [decidable_pred s] [infinite s] : ℕ → s | 0 := ⊥ | (n+1) := succ (of_nat n) lemma of_nat_surjective_aux : ∀ {x : ℕ} (hx : x ∈ s), ∃ n, of_nat s n = ⟨x, hx⟩ | x := λ hx, let t : list s := ((list.range x).filter (λ y, y ∈ s)).pmap (λ (y : ℕ) (hy : y ∈ s), ⟨y, hy⟩) (by simp) in have hmt : ∀ {y : s}, y ∈ t ↔ y < ⟨x, hx⟩, by simp [list.mem_filter, subtype.ext, t]; intros; refl, have wf : ∀ m : s, list.maximum t = m → m.1 < x, from λ m hmax, by simpa [hmt] using list.maximum_mem hmax, begin cases hmax : list.maximum t with m, { exact ⟨0, le_antisymm (@bot_le s _ _) (le_of_not_gt (λ h, list.not_mem_nil (⊥ : s) $ by rw [← list.maximum_eq_none.1 hmax, hmt]; exact h))⟩ }, { cases of_nat_surjective_aux m.2 with a ha, exact ⟨a + 1, le_antisymm (by rw of_nat; exact succ_le_of_lt (by rw ha; exact wf _ hmax)) $ by rw of_nat; exact le_succ_of_forall_lt_le (λ z hz, by rw ha; cases m; exact list.le_maximum_of_mem (hmt.2 hz) hmax)⟩ } end using_well_founded {dec_tac := `[tauto]} lemma of_nat_surjective : surjective (of_nat s) := λ ⟨x, hx⟩, of_nat_surjective_aux hx private def to_fun_aux (x : s) : ℕ := (list.range x).countp s private lemma to_fun_aux_eq (x : s) : to_fun_aux x = ((finset.range x).filter s).card := by rw [to_fun_aux, list.countp_eq_length_filter]; refl open finset private lemma right_inverse_aux : ∀ n, to_fun_aux (of_nat s n) = n | 0 := begin rw [to_fun_aux_eq, card_eq_zero, eq_empty_iff_forall_not_mem], assume n, rw [mem_filter, of_nat, mem_range], assume h, exact not_lt_of_le bot_le (show (⟨n, h.2⟩ : s) < ⊥, from h.1) end | (n+1) := have ih : to_fun_aux (of_nat s n) = n, from right_inverse_aux n, have h₁ : (of_nat s n : ℕ) ∉ (range (of_nat s n)).filter s, by simp, have h₂ : (range (succ (of_nat s n))).filter s = insert (of_nat s n) ((range (of_nat s n)).filter s), begin simp only [finset.ext, mem_insert, mem_range, mem_filter], assume m, exact ⟨λ h, by simp only [h.2, and_true]; exact or.symm (lt_or_eq_of_le ((@lt_succ_iff_le _ _ _ ⟨m, h.2⟩ _).1 h.1)), λ h, h.elim (λ h, h.symm ▸ ⟨lt_succ_self _, subtype.property _⟩) (λ h, ⟨lt_of_le_of_lt (le_of_lt h.1) (lt_succ_self _), h.2⟩)⟩ end, begin clear_aux_decl, simp only [to_fun_aux_eq, of_nat, range_succ] at *, conv {to_rhs, rw [← ih, ← card_insert_of_not_mem h₁, ← h₂] }, end def denumerable (s : set ℕ) [decidable_pred s] [infinite s] : denumerable s := denumerable.of_equiv ℕ { to_fun := to_fun_aux, inv_fun := of_nat s, left_inv := left_inverse_of_surjective_of_right_inverse of_nat_surjective right_inverse_aux, right_inv := right_inverse_aux } end nat.subtype namespace denumerable open encodable def of_encodable_of_infinite (α : Type*) [encodable α] [infinite α] : denumerable α := begin letI := @decidable_range_encode α _; letI : infinite (set.range (@encode α _)) := infinite.of_injective _ (equiv.set.range _ encode_injective).injective, letI := nat.subtype.denumerable (set.range (@encode α _)), exact denumerable.of_equiv (set.range (@encode α _)) (equiv_range_encode α) end end denumerable
931671432e3c733b29c09d18b2905e608935f4e5
ce6917c5bacabee346655160b74a307b4a5ab620
/src/ch5/ex0306.lean
ae390fca24bd732752b0c03dd18272601f44af45
[]
no_license
Ailrun/Theorem_Proving_in_Lean
ae6a23f3c54d62d401314d6a771e8ff8b4132db2
2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68
refs/heads/master
1,609,838,270,467
1,586,846,743,000
1,586,846,743,000
240,967,761
1
0
null
null
null
null
UTF-8
Lean
false
false
182
lean
example (p q : ℕ → Prop) : (∃ x, p x ∧ q x) → ∃ x, q x ∧ p x := begin intro h, cases h with x hpqx, cases hpqx with hpx hqx, existsi x, split; assumption end
019c0411df22e8cbb6e5593c6b0d04289962992a
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/src/Init/Control/Basic.lean
b644e4c553b2c12008a630cd5dcc127ef60b0cbb
[ "Apache-2.0" ]
permissive
WojciechKarpiel/lean4
7f89706b8e3c1f942b83a2c91a3a00b05da0e65b
f6e1314fa08293dea66a329e05b6c196a0189163
refs/heads/master
1,686,633,402,214
1,625,821,189,000
1,625,821,258,000
384,640,886
0
0
Apache-2.0
1,625,903,617,000
1,625,903,026,000
null
UTF-8
Lean
false
false
3,804
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura, Sebastian Ullrich -/ prelude import Init.Core universe u v w @[reducible] def Functor.mapRev {f : Type u → Type v} [Functor f] {α β : Type u} : f α → (α → β) → f β := fun a f => f <$> a infixr:100 " <&> " => Functor.mapRev @[inline] def Functor.discard {f : Type u → Type v} {α : Type u} [Functor f] (x : f α) : f PUnit := Functor.mapConst PUnit.unit x export Functor (discard) class Alternative (f : Type u → Type v) extends Applicative f : Type (max (u+1) v) where failure : {α : Type u} → f α orElse : {α : Type u} → f α → f α → f α instance (f : Type u → Type v) (α : Type u) [Alternative f] : OrElse (f α) := ⟨Alternative.orElse⟩ variable {f : Type u → Type v} [Alternative f] {α : Type u} export Alternative (failure) @[inline] def guard {f : Type → Type v} [Alternative f] (p : Prop) [Decidable p] : f Unit := if p then pure () else failure @[inline] def optional (x : f α) : f (Option α) := some <$> x <|> pure none class ToBool (α : Type u) where toBool : α → Bool export ToBool (toBool) instance : ToBool Bool where toBool b := b @[macroInline] def bool {β : Type u} {α : Type v} [ToBool β] (f t : α) (b : β) : α := match toBool b with | true => t | false => f @[macroInline] def orM {m : Type u → Type v} {β : Type u} [Monad m] [ToBool β] (x y : m β) : m β := do let b ← x match toBool b with | true => pure b | false => y infixr:30 " <||> " => orM @[macroInline] def andM {m : Type u → Type v} {β : Type u} [Monad m] [ToBool β] (x y : m β) : m β := do let b ← x match toBool b with | true => y | false => pure b infixr:35 " <&&> " => andM @[macroInline] def notM {m : Type → Type v} [Applicative m] (x : m Bool) : m Bool := not <$> x class MonadControl (m : Type u → Type v) (n : Type u → Type w) where stM : Type u → Type u liftWith : {α : Type u} → (({β : Type u} → n β → m (stM β)) → m α) → n α restoreM : {α : Type u} → m (stM α) → n α class MonadControlT (m : Type u → Type v) (n : Type u → Type w) where stM : Type u → Type u liftWith : {α : Type u} → (({β : Type u} → n β → m (stM β)) → m α) → n α restoreM {α : Type u} : stM α → n α export MonadControlT (stM liftWith restoreM) instance (m n o) [MonadControl n o] [MonadControlT m n] : MonadControlT m o where stM α := stM m n (MonadControl.stM n o α) liftWith f := MonadControl.liftWith fun x₂ => liftWith fun x₁ => f (x₁ ∘ x₂) restoreM := MonadControl.restoreM ∘ restoreM instance (m : Type u → Type v) [Pure m] : MonadControlT m m where stM α := α liftWith f := f fun x => x restoreM x := pure x @[inline] def controlAt (m : Type u → Type v) {n : Type u → Type w} [s1 : MonadControlT m n] [s2 : Bind n] {α : Type u} (f : ({β : Type u} → n β → m (stM m n β)) → m (stM m n α)) : n α := liftWith f >>= restoreM @[inline] def control {m : Type u → Type v} {n : Type u → Type w} [MonadControlT m n] [Bind n] {α : Type u} (f : ({β : Type u} → n β → m (stM m n β)) → m (stM m n α)) : n α := controlAt m f /- Typeclass for the polymorphic `forM` operation described in the "do unchained" paper. Remark: - `γ` is a "container" type of elements of type `α`. - `α` is treated as an output parameter by the typeclass resolution procedure. That is, it tries to find an instance using only `m` and `γ`. -/ class ForM (m : Type u → Type v) (γ : Type w₁) (α : outParam (Type w₂)) where forM [Monad m] : γ → (α → m PUnit) → m PUnit export ForM (forM)
76bf83512cdce605c19f3a920dd531942c4c90b6
9028d228ac200bbefe3a711342514dd4e4458bff
/src/topology/subset_properties.lean
a0255bde163073cd8f8a4e80f4776b1d15967aec
[ "Apache-2.0" ]
permissive
mcncm/mathlib
8d25099344d9d2bee62822cb9ed43aa3e09fa05e
fde3d78cadeec5ef827b16ae55664ef115e66f57
refs/heads/master
1,672,743,316,277
1,602,618,514,000
1,602,618,514,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
58,157
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Yury Kudryashov -/ import topology.continuous_on import data.finset.order /-! # Properties of subsets of topological spaces ## Main definitions `compact`, `is_clopen`, `is_irreducible`, `is_connected`, `is_totally_disconnected`, `is_totally_separated` TODO: write better docs ## On the definition of irreducible and connected sets/spaces In informal mathematics, irreducible and connected spaces are assumed to be nonempty. We formalise the predicate without that assumption as `is_preirreducible` and `is_preconnected` respectively. In other words, the only difference is whether the empty space counts as irreducible and/or connected. There are good reasons to consider the empty space to be “too simple to be simple” See also https://ncatlab.org/nlab/show/too+simple+to+be+simple, and in particular https://ncatlab.org/nlab/show/too+simple+to+be+simple#relationship_to_biased_definitions. -/ open set filter classical open_locale classical topological_space filter universes u v variables {α : Type u} {β : Type v} [topological_space α] {s t : set α} /- compact sets -/ section compact /-- A set `s` is compact if for every filter `f` that contains `s`, every set of `f` also meets every neighborhood of some `a ∈ s`. -/ def is_compact (s : set α) := ∀ ⦃f⦄ [ne_bot f], f ≤ 𝓟 s → ∃a∈s, cluster_pt a f /-- The complement to a compact set belongs to a filter `f` if it belongs to each filter `𝓝 a ⊓ f`, `a ∈ s`. -/ lemma is_compact.compl_mem_sets (hs : is_compact s) {f : filter α} (hf : ∀ a ∈ s, sᶜ ∈ 𝓝 a ⊓ f) : sᶜ ∈ f := begin contrapose! hf, simp only [mem_iff_inf_principal_compl, compl_compl, inf_assoc, ← exists_prop] at hf ⊢, exact @hs _ hf inf_le_right end /-- The complement to a compact set belongs to a filter `f` if each `a ∈ s` has a neighborhood `t` within `s` such that `tᶜ` belongs to `f`. -/ lemma is_compact.compl_mem_sets_of_nhds_within (hs : is_compact s) {f : filter α} (hf : ∀ a ∈ s, ∃ t ∈ 𝓝[s] a, tᶜ ∈ f) : sᶜ ∈ f := begin refine hs.compl_mem_sets (λ a ha, _), rcases hf a ha with ⟨t, ht, hst⟩, replace ht := mem_inf_principal.1 ht, refine mem_inf_sets.2 ⟨_, ht, _, hst, _⟩, rintros x ⟨h₁, h₂⟩ hs, exact h₂ (h₁ hs) end /-- If `p : set α → Prop` is stable under restriction and union, and each point `x of a compact set `s` has a neighborhood `t` within `s` such that `p t`, then `p s` holds. -/ @[elab_as_eliminator] lemma is_compact.induction_on {s : set α} (hs : is_compact s) {p : set α → Prop} (he : p ∅) (hmono : ∀ ⦃s t⦄, s ⊆ t → p t → p s) (hunion : ∀ ⦃s t⦄, p s → p t → p (s ∪ t)) (hnhds : ∀ x ∈ s, ∃ t ∈ 𝓝[s] x, p t) : p s := let f : filter α := { sets := {t | p tᶜ}, univ_sets := by simpa, sets_of_superset := λ t₁ t₂ ht₁ ht, hmono (compl_subset_compl.2 ht) ht₁, inter_sets := λ t₁ t₂ ht₁ ht₂, by simp [compl_inter, hunion ht₁ ht₂] } in have sᶜ ∈ f, from hs.compl_mem_sets_of_nhds_within (by simpa using hnhds), by simpa /-- The intersection of a compact set and a closed set is a compact set. -/ lemma is_compact.inter_right (hs : is_compact s) (ht : is_closed t) : is_compact (s ∩ t) := begin introsI f hnf hstf, obtain ⟨a, hsa, ha⟩ : ∃ a ∈ s, cluster_pt a f := hs (le_trans hstf (le_principal_iff.2 (inter_subset_left _ _))), have : a ∈ t := (ht.mem_of_nhds_within_ne_bot $ ha.mono $ le_trans hstf (le_principal_iff.2 (inter_subset_right _ _))), exact ⟨a, ⟨hsa, this⟩, ha⟩ end /-- The intersection of a closed set and a compact set is a compact set. -/ lemma is_compact.inter_left (ht : is_compact t) (hs : is_closed s) : is_compact (s ∩ t) := inter_comm t s ▸ ht.inter_right hs /-- The set difference of a compact set and an open set is a compact set. -/ lemma compact_diff (hs : is_compact s) (ht : is_open t) : is_compact (s \ t) := hs.inter_right (is_closed_compl_iff.mpr ht) /-- A closed subset of a compact set is a compact set. -/ lemma compact_of_is_closed_subset (hs : is_compact s) (ht : is_closed t) (h : t ⊆ s) : is_compact t := inter_eq_self_of_subset_right h ▸ hs.inter_right ht lemma is_compact.adherence_nhdset {f : filter α} (hs : is_compact s) (hf₂ : f ≤ 𝓟 s) (ht₁ : is_open t) (ht₂ : ∀a∈s, cluster_pt a f → a ∈ t) : t ∈ f := classical.by_cases mem_sets_of_eq_bot $ assume : f ⊓ 𝓟 tᶜ ≠ ⊥, let ⟨a, ha, (hfa : cluster_pt a $ f ⊓ 𝓟 tᶜ)⟩ := @@hs this $ inf_le_left_of_le hf₂ in have a ∈ t, from ht₂ a ha (hfa.of_inf_left), have tᶜ ∩ t ∈ 𝓝[tᶜ] a, from inter_mem_nhds_within _ (mem_nhds_sets ht₁ this), have A : 𝓝[tᶜ] a = ⊥, from empty_in_sets_eq_bot.1 $ compl_inter_self t ▸ this, have 𝓝[tᶜ] a ≠ ⊥, from hfa.of_inf_right, absurd A this lemma compact_iff_ultrafilter_le_nhds : is_compact s ↔ (∀f, is_ultrafilter f → f ≤ 𝓟 s → ∃a∈s, f ≤ 𝓝 a) := ⟨assume hs : is_compact s, assume f hf hfs, let ⟨a, ha, h⟩ := @hs _ hf.left hfs in ⟨a, ha, le_of_ultrafilter hf h⟩, assume hs : (∀f, is_ultrafilter f → f ≤ 𝓟 s → ∃a∈s, f ≤ 𝓝 a), assume f hf hfs, let ⟨a, ha, (h : ultrafilter_of f ≤ 𝓝 a)⟩ := hs (ultrafilter_of f) (ultrafilter_ultrafilter_of' hf) (le_trans ultrafilter_of_le hfs) in have cluster_pt a (ultrafilter_of f), from cluster_pt.of_le_nhds' h (ultrafilter_ultrafilter_of' hf).left, ⟨a, ha, this.mono ultrafilter_of_le⟩⟩ /-- For every open cover of a compact set, there exists a finite subcover. -/ lemma is_compact.elim_finite_subcover {ι : Type v} (hs : is_compact s) (U : ι → set α) (hUo : ∀i, is_open (U i)) (hsU : s ⊆ ⋃ i, U i) : ∃ t : finset ι, s ⊆ ⋃ i ∈ t, U i := is_compact.induction_on hs ⟨∅, empty_subset _⟩ (λ s₁ s₂ hs ⟨t, hs₂⟩, ⟨t, subset.trans hs hs₂⟩) (λ s₁ s₂ ⟨t₁, ht₁⟩ ⟨t₂, ht₂⟩, ⟨t₁ ∪ t₂, by { rw [finset.bUnion_union], exact union_subset_union ht₁ ht₂ }⟩) (λ x hx, let ⟨i, hi⟩ := mem_Union.1 (hsU hx) in ⟨U i, mem_nhds_within.2 ⟨U i, hUo i, hi, inter_subset_left _ _⟩, {i}, by simp⟩) /-- For every family of closed sets whose intersection avoids a compact set, there exists a finite subfamily whose intersection avoids this compact set. -/ lemma is_compact.elim_finite_subfamily_closed {s : set α} {ι : Type v} (hs : is_compact s) (Z : ι → set α) (hZc : ∀i, is_closed (Z i)) (hsZ : s ∩ (⋂ i, Z i) = ∅) : ∃ t : finset ι, s ∩ (⋂ i ∈ t, Z i) = ∅ := let ⟨t, ht⟩ := hs.elim_finite_subcover (λ i, (Z i)ᶜ) hZc (by simpa only [subset_def, not_forall, eq_empty_iff_forall_not_mem, set.mem_Union, exists_prop, set.mem_inter_eq, not_and, iff_self, set.mem_Inter, set.mem_compl_eq] using hsZ) in ⟨t, by simpa only [subset_def, not_forall, eq_empty_iff_forall_not_mem, set.mem_Union, exists_prop, set.mem_inter_eq, not_and, iff_self, set.mem_Inter, set.mem_compl_eq] using ht⟩ /-- To show that a compact set intersects the intersection of a family of closed sets, it is sufficient to show that it intersects every finite subfamily. -/ lemma is_compact.inter_Inter_nonempty {s : set α} {ι : Type v} (hs : is_compact s) (Z : ι → set α) (hZc : ∀i, is_closed (Z i)) (hsZ : ∀ t : finset ι, (s ∩ ⋂ i ∈ t, Z i).nonempty) : (s ∩ ⋂ i, Z i).nonempty := begin simp only [← ne_empty_iff_nonempty] at hsZ ⊢, apply mt (hs.elim_finite_subfamily_closed Z hZc), push_neg, exact hsZ end /-- Cantor's intersection theorem: the intersection of a directed family of nonempty compact closed sets is nonempty. -/ lemma is_compact.nonempty_Inter_of_directed_nonempty_compact_closed {ι : Type v} [hι : nonempty ι] (Z : ι → set α) (hZd : directed (⊇) Z) (hZn : ∀ i, (Z i).nonempty) (hZc : ∀ i, is_compact (Z i)) (hZcl : ∀ i, is_closed (Z i)) : (⋂ i, Z i).nonempty := begin apply hι.elim, intro i₀, let Z' := λ i, Z i ∩ Z i₀, suffices : (⋂ i, Z' i).nonempty, { exact nonempty.mono (Inter_subset_Inter $ assume i, inter_subset_left (Z i) (Z i₀)) this }, rw ← ne_empty_iff_nonempty, intro H, obtain ⟨t, ht⟩ : ∃ (t : finset ι), ((Z i₀) ∩ ⋂ (i ∈ t), Z' i) = ∅, from (hZc i₀).elim_finite_subfamily_closed Z' (assume i, is_closed_inter (hZcl i) (hZcl i₀)) (by rw [H, inter_empty]), obtain ⟨i₁, hi₁⟩ : ∃ i₁ : ι, Z i₁ ⊆ Z i₀ ∧ ∀ i ∈ t, Z i₁ ⊆ Z' i, { rcases directed.finset_le hZd t with ⟨i, hi⟩, rcases hZd i i₀ with ⟨i₁, hi₁, hi₁₀⟩, use [i₁, hi₁₀], intros j hj, exact subset_inter (subset.trans hi₁ (hi j hj)) hi₁₀ }, suffices : ((Z i₀) ∩ ⋂ (i ∈ t), Z' i).nonempty, { rw ← ne_empty_iff_nonempty at this, contradiction }, refine nonempty.mono _ (hZn i₁), exact subset_inter hi₁.left (subset_bInter hi₁.right) end /-- Cantor's intersection theorem for sequences indexed by `ℕ`: the intersection of a decreasing sequence of nonempty compact closed sets is nonempty. -/ lemma is_compact.nonempty_Inter_of_sequence_nonempty_compact_closed (Z : ℕ → set α) (hZd : ∀ i, Z (i+1) ⊆ Z i) (hZn : ∀ i, (Z i).nonempty) (hZ0 : is_compact (Z 0)) (hZcl : ∀ i, is_closed (Z i)) : (⋂ i, Z i).nonempty := have Zmono : _, from @monotone_of_monotone_nat (order_dual _) _ Z hZd, have hZd : directed (⊇) Z, from directed_of_sup Zmono, have ∀ i, Z i ⊆ Z 0, from assume i, Zmono $ zero_le i, have hZc : ∀ i, is_compact (Z i), from assume i, compact_of_is_closed_subset hZ0 (hZcl i) (this i), is_compact.nonempty_Inter_of_directed_nonempty_compact_closed Z hZd hZn hZc hZcl /-- For every open cover of a compact set, there exists a finite subcover. -/ lemma is_compact.elim_finite_subcover_image {b : set β} {c : β → set α} (hs : is_compact s) (hc₁ : ∀i∈b, is_open (c i)) (hc₂ : s ⊆ ⋃i∈b, c i) : ∃b'⊆b, finite b' ∧ s ⊆ ⋃i∈b', c i := begin rcases hs.elim_finite_subcover (λ i, c i.1 : b → set α) _ _ with ⟨d, hd⟩, refine ⟨↑(d.image subtype.val), _, finset.finite_to_set _, _⟩, { intros i hi, erw finset.mem_image at hi, rcases hi with ⟨s, hsd, rfl⟩, exact s.property }, { refine subset.trans hd _, rintros x ⟨_, ⟨s, rfl⟩, ⟨_, ⟨hsd, rfl⟩, H⟩⟩, refine ⟨c s.val, ⟨s.val, _⟩, H⟩, simp [finset.mem_image_of_mem subtype.val hsd] }, { rintro ⟨i, hi⟩, exact hc₁ i hi }, { refine subset.trans hc₂ _, rintros x ⟨_, ⟨i, rfl⟩, ⟨_, ⟨hib, rfl⟩, H⟩⟩, exact ⟨_, ⟨⟨i, hib⟩, rfl⟩, H⟩ }, end /-- A set `s` is compact if for every family of closed sets whose intersection avoids `s`, there exists a finite subfamily whose intersection avoids `s`. -/ theorem compact_of_finite_subfamily_closed (h : Π {ι : Type u} (Z : ι → (set α)), (∀ i, is_closed (Z i)) → s ∩ (⋂ i, Z i) = ∅ → (∃ (t : finset ι), s ∩ (⋂ i ∈ t, Z i) = ∅)) : is_compact s := assume f hfn hfs, classical.by_contradiction $ assume : ¬ (∃x∈s, cluster_pt x f), have hf : ∀x∈s, 𝓝 x ⊓ f = ⊥, by simpa only [cluster_pt, not_exists, not_not, ne_bot], have ¬ ∃x∈s, ∀t∈f.sets, x ∈ closure t, from assume ⟨x, hxs, hx⟩, have ∅ ∈ 𝓝 x ⊓ f, by rw [empty_in_sets_eq_bot, hf x hxs], let ⟨t₁, ht₁, t₂, ht₂, ht⟩ := by rw [mem_inf_sets] at this; exact this in have ∅ ∈ 𝓝[t₂] x, from (𝓝[t₂] x).sets_of_superset (inter_mem_inf_sets ht₁ (subset.refl t₂)) ht, have 𝓝[t₂] x = ⊥, by rwa [empty_in_sets_eq_bot] at this, by simp only [closure_eq_cluster_pts] at hx; exact hx t₂ ht₂ this, let ⟨t, ht⟩ := h (λ i : f.sets, closure i.1) (λ i, is_closed_closure) (by simpa [eq_empty_iff_forall_not_mem, not_exists]) in have (⋂i∈t, subtype.val i) ∈ f, from Inter_mem_sets t.finite_to_set $ assume i hi, i.2, have s ∩ (⋂i∈t, subtype.val i) ∈ f, from inter_mem_sets (le_principal_iff.1 hfs) this, have ∅ ∈ f, from mem_sets_of_superset this $ assume x ⟨hxs, hx⟩, let ⟨i, hit, hxi⟩ := (show ∃i ∈ t, x ∉ closure (subtype.val i), by { rw [eq_empty_iff_forall_not_mem] at ht, simpa [hxs, not_forall] using ht x }) in have x ∈ closure i.val, from subset_closure (mem_bInter_iff.mp hx i hit), show false, from hxi this, hfn $ by rwa [empty_in_sets_eq_bot] at this /-- A set `s` is compact if for every open cover of `s`, there exists a finite subcover. -/ lemma compact_of_finite_subcover (h : Π {ι : Type u} (U : ι → (set α)), (∀ i, is_open (U i)) → s ⊆ (⋃ i, U i) → (∃ (t : finset ι), s ⊆ (⋃ i ∈ t, U i))) : is_compact s := compact_of_finite_subfamily_closed $ assume ι Z hZc hsZ, let ⟨t, ht⟩ := h (λ i, (Z i)ᶜ) (assume i, is_open_compl_iff.mpr $ hZc i) (by simpa only [subset_def, not_forall, eq_empty_iff_forall_not_mem, set.mem_Union, exists_prop, set.mem_inter_eq, not_and, iff_self, set.mem_Inter, set.mem_compl_eq] using hsZ) in ⟨t, by simpa only [subset_def, not_forall, eq_empty_iff_forall_not_mem, set.mem_Union, exists_prop, set.mem_inter_eq, not_and, iff_self, set.mem_Inter, set.mem_compl_eq] using ht⟩ /-- A set `s` is compact if and only if for every open cover of `s`, there exists a finite subcover. -/ lemma compact_iff_finite_subcover : is_compact s ↔ (Π {ι : Type u} (U : ι → (set α)), (∀ i, is_open (U i)) → s ⊆ (⋃ i, U i) → (∃ (t : finset ι), s ⊆ (⋃ i ∈ t, U i))) := ⟨assume hs ι, hs.elim_finite_subcover, compact_of_finite_subcover⟩ /-- A set `s` is compact if and only if for every family of closed sets whose intersection avoids `s`, there exists a finite subfamily whose intersection avoids `s`. -/ theorem compact_iff_finite_subfamily_closed : is_compact s ↔ (Π {ι : Type u} (Z : ι → (set α)), (∀ i, is_closed (Z i)) → s ∩ (⋂ i, Z i) = ∅ → (∃ (t : finset ι), s ∩ (⋂ i ∈ t, Z i) = ∅)) := ⟨assume hs ι, hs.elim_finite_subfamily_closed, compact_of_finite_subfamily_closed⟩ @[simp] lemma compact_empty : is_compact (∅ : set α) := assume f hnf hsf, not.elim hnf $ empty_in_sets_eq_bot.1 $ le_principal_iff.1 hsf @[simp] lemma compact_singleton {a : α} : is_compact ({a} : set α) := compact_of_finite_subcover $ assume ι U hUo hsU, let ⟨i, hai⟩ := (show ∃i : ι, a ∈ U i, from mem_Union.1 $ singleton_subset_iff.1 hsU) in ⟨{i}, singleton_subset_iff.2 (by simpa only [finset.bUnion_singleton])⟩ lemma set.finite.compact_bUnion {s : set β} {f : β → set α} (hs : finite s) (hf : ∀i ∈ s, is_compact (f i)) : is_compact (⋃i ∈ s, f i) := compact_of_finite_subcover $ assume ι U hUo hsU, have ∀i : subtype s, ∃t : finset ι, f i ⊆ (⋃ j ∈ t, U j), from assume ⟨i, hi⟩, (hf i hi).elim_finite_subcover _ hUo (calc f i ⊆ ⋃i ∈ s, f i : subset_bUnion_of_mem hi ... ⊆ ⋃j, U j : hsU), let ⟨finite_subcovers, h⟩ := axiom_of_choice this in by haveI : fintype (subtype s) := hs.fintype; exact let t := finset.bind finset.univ finite_subcovers in have (⋃i ∈ s, f i) ⊆ (⋃ i ∈ t, U i), from bUnion_subset $ assume i hi, calc f i ⊆ (⋃ j ∈ finite_subcovers ⟨i, hi⟩, U j) : (h ⟨i, hi⟩) ... ⊆ (⋃ j ∈ t, U j) : bUnion_subset_bUnion_left $ assume j hj, finset.mem_bind.mpr ⟨_, finset.mem_univ _, hj⟩, ⟨t, this⟩ lemma compact_Union {f : β → set α} [fintype β] (h : ∀i, is_compact (f i)) : is_compact (⋃i, f i) := by rw ← bUnion_univ; exact finite_univ.compact_bUnion (λ i _, h i) lemma set.finite.is_compact (hs : finite s) : is_compact s := bUnion_of_singleton s ▸ hs.compact_bUnion (λ _ _, compact_singleton) lemma is_compact.union (hs : is_compact s) (ht : is_compact t) : is_compact (s ∪ t) := by rw union_eq_Union; exact compact_Union (λ b, by cases b; assumption) section tube_lemma variables [topological_space β] /-- `nhds_contain_boxes s t` means that any open neighborhood of `s × t` in `α × β` includes a product of an open neighborhood of `s` by an open neighborhood of `t`. -/ def nhds_contain_boxes (s : set α) (t : set β) : Prop := ∀ (n : set (α × β)) (hn : is_open n) (hp : set.prod s t ⊆ n), ∃ (u : set α) (v : set β), is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ set.prod u v ⊆ n lemma nhds_contain_boxes.symm {s : set α} {t : set β} : nhds_contain_boxes s t → nhds_contain_boxes t s := assume H n hn hp, let ⟨u, v, uo, vo, su, tv, p⟩ := H (prod.swap ⁻¹' n) (continuous_swap n hn) (by rwa [←image_subset_iff, image_swap_prod]) in ⟨v, u, vo, uo, tv, su, by rwa [←image_subset_iff, image_swap_prod] at p⟩ lemma nhds_contain_boxes.comm {s : set α} {t : set β} : nhds_contain_boxes s t ↔ nhds_contain_boxes t s := iff.intro nhds_contain_boxes.symm nhds_contain_boxes.symm lemma nhds_contain_boxes_of_singleton {x : α} {y : β} : nhds_contain_boxes ({x} : set α) ({y} : set β) := assume n hn hp, let ⟨u, v, uo, vo, xu, yv, hp'⟩ := is_open_prod_iff.mp hn x y (hp $ by simp) in ⟨u, v, uo, vo, by simpa, by simpa, hp'⟩ lemma nhds_contain_boxes_of_compact {s : set α} (hs : is_compact s) (t : set β) (H : ∀ x ∈ s, nhds_contain_boxes ({x} : set α) t) : nhds_contain_boxes s t := assume n hn hp, have ∀x : subtype s, ∃uv : set α × set β, is_open uv.1 ∧ is_open uv.2 ∧ {↑x} ⊆ uv.1 ∧ t ⊆ uv.2 ∧ set.prod uv.1 uv.2 ⊆ n, from assume ⟨x, hx⟩, have set.prod {x} t ⊆ n, from subset.trans (prod_mono (by simpa) (subset.refl _)) hp, let ⟨ux,vx,H1⟩ := H x hx n hn this in ⟨⟨ux,vx⟩,H1⟩, let ⟨uvs, h⟩ := classical.axiom_of_choice this in have us_cover : s ⊆ ⋃i, (uvs i).1, from assume x hx, set.subset_Union _ ⟨x,hx⟩ (by simpa using (h ⟨x,hx⟩).2.2.1), let ⟨s0, s0_cover⟩ := hs.elim_finite_subcover _ (λi, (h i).1) us_cover in let u := ⋃(i ∈ s0), (uvs i).1 in let v := ⋂(i ∈ s0), (uvs i).2 in have is_open u, from is_open_bUnion (λi _, (h i).1), have is_open v, from is_open_bInter s0.finite_to_set (λi _, (h i).2.1), have t ⊆ v, from subset_bInter (λi _, (h i).2.2.2.1), have set.prod u v ⊆ n, from assume ⟨x',y'⟩ ⟨hx',hy'⟩, have ∃i ∈ s0, x' ∈ (uvs i).1, by simpa using hx', let ⟨i,is0,hi⟩ := this in (h i).2.2.2.2 ⟨hi, (bInter_subset_of_mem is0 : v ⊆ (uvs i).2) hy'⟩, ⟨u, v, ‹is_open u›, ‹is_open v›, s0_cover, ‹t ⊆ v›, ‹set.prod u v ⊆ n›⟩ lemma generalized_tube_lemma {s : set α} (hs : is_compact s) {t : set β} (ht : is_compact t) {n : set (α × β)} (hn : is_open n) (hp : set.prod s t ⊆ n) : ∃ (u : set α) (v : set β), is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ set.prod u v ⊆ n := have _, from nhds_contain_boxes_of_compact hs t $ assume x _, nhds_contain_boxes.symm $ nhds_contain_boxes_of_compact ht {x} $ assume y _, nhds_contain_boxes_of_singleton, this n hn hp end tube_lemma /-- Type class for compact spaces. Separation is sometimes included in the definition, especially in the French literature, but we do not include it here. -/ class compact_space (α : Type*) [topological_space α] : Prop := (compact_univ : is_compact (univ : set α)) lemma compact_univ [h : compact_space α] : is_compact (univ : set α) := h.compact_univ lemma cluster_point_of_compact [compact_space α] (f : filter α) [ne_bot f] : ∃ x, cluster_pt x f := by simpa using compact_univ (show f ≤ 𝓟 univ, by simp) theorem compact_space_of_finite_subfamily_closed {α : Type u} [topological_space α] (h : Π {ι : Type u} (Z : ι → (set α)), (∀ i, is_closed (Z i)) → (⋂ i, Z i) = ∅ → (∃ (t : finset ι), (⋂ i ∈ t, Z i) = ∅)) : compact_space α := { compact_univ := begin apply compact_of_finite_subfamily_closed, intros ι Z, specialize h Z, simpa using h end } lemma is_closed.compact [compact_space α] {s : set α} (h : is_closed s) : is_compact s := compact_of_is_closed_subset compact_univ h (subset_univ _) variables [topological_space β] lemma is_compact.image_of_continuous_on {f : α → β} (hs : is_compact s) (hf : continuous_on f s) : is_compact (f '' s) := begin intros l lne ls, have : ne_bot (l.comap f ⊓ 𝓟 s) := comap_inf_principal_ne_bot_of_image_mem lne (le_principal_iff.1 ls), obtain ⟨a, has, ha⟩ : ∃ a ∈ s, cluster_pt a (l.comap f ⊓ 𝓟 s) := @@hs this inf_le_right, use [f a, mem_image_of_mem f has], have : tendsto f (𝓝 a ⊓ (comap f l ⊓ 𝓟 s)) (𝓝 (f a) ⊓ l), { convert (hf a has).inf (@tendsto_comap _ _ f l) using 1, rw nhds_within, ac_refl }, exact @@tendsto.ne_bot _ this ha, end lemma is_compact.image {f : α → β} (hs : is_compact s) (hf : continuous f) : is_compact (f '' s) := hs.image_of_continuous_on hf.continuous_on lemma compact_range [compact_space α] {f : α → β} (hf : continuous f) : is_compact (range f) := by rw ← image_univ; exact compact_univ.image hf /-- If X is is_compact then pr₂ : X × Y → Y is a closed map -/ theorem is_closed_proj_of_compact {X : Type*} [topological_space X] [compact_space X] {Y : Type*} [topological_space Y] : is_closed_map (prod.snd : X × Y → Y) := begin set πX := (prod.fst : X × Y → X), set πY := (prod.snd : X × Y → Y), assume C (hC : is_closed C), rw is_closed_iff_cluster_pt at hC ⊢, assume y (y_closure : cluster_pt y $ 𝓟 (πY '' C)), have : ne_bot (map πX (comap πY (𝓝 y) ⊓ 𝓟 C)), { suffices : ne_bot (map πY (comap πY (𝓝 y) ⊓ 𝓟 C)), by simpa only [map_ne_bot_iff], calc map πY (comap πY (𝓝 y) ⊓ 𝓟 C) = 𝓝 y ⊓ map πY (𝓟 C) : filter.push_pull' _ _ _ ... = 𝓝 y ⊓ 𝓟 (πY '' C) : by rw map_principal ... ≠ ⊥ : y_closure }, resetI, obtain ⟨x, hx⟩ : ∃ x, cluster_pt x (map πX (comap πY (𝓝 y) ⊓ 𝓟 C)), from cluster_point_of_compact _, refine ⟨⟨x, y⟩, _, by simp [πY]⟩, apply hC, rw [cluster_pt, ← filter.map_ne_bot_iff πX], calc map πX (𝓝 (x, y) ⊓ 𝓟 C) = map πX (comap πX (𝓝 x) ⊓ comap πY (𝓝 y) ⊓ 𝓟 C) : by rw [nhds_prod_eq, filter.prod] ... = map πX (comap πY (𝓝 y) ⊓ 𝓟 C ⊓ comap πX (𝓝 x)) : by ac_refl ... = map πX (comap πY (𝓝 y) ⊓ 𝓟 C) ⊓ 𝓝 x : by rw filter.push_pull ... = 𝓝 x ⊓ map πX (comap πY (𝓝 y) ⊓ 𝓟 C) : by rw inf_comm ... ≠ ⊥ : hx, end lemma embedding.compact_iff_compact_image {f : α → β} (hf : embedding f) : is_compact s ↔ is_compact (f '' s) := iff.intro (assume h, h.image hf.continuous) $ assume h, begin rw compact_iff_ultrafilter_le_nhds at ⊢ h, intros u hu us', let u' : filter β := map f u, have : u' ≤ 𝓟 (f '' s), begin rw [map_le_iff_le_comap, comap_principal], convert us', exact preimage_image_eq _ hf.inj end, rcases h u' (ultrafilter_map hu) this with ⟨_, ⟨a, ha, ⟨⟩⟩, _⟩, refine ⟨a, ha, _⟩, rwa [hf.induced, nhds_induced, ←map_le_iff_le_comap] end lemma compact_iff_compact_in_subtype {p : α → Prop} {s : set {a // p a}} : is_compact s ↔ is_compact ((coe : _ → α) '' s) := embedding_subtype_coe.compact_iff_compact_image lemma compact_iff_compact_univ {s : set α} : is_compact s ↔ is_compact (univ : set s) := by rw [compact_iff_compact_in_subtype, image_univ, subtype.range_coe]; refl lemma compact_iff_compact_space {s : set α} : is_compact s ↔ compact_space s := compact_iff_compact_univ.trans ⟨λ h, ⟨h⟩, @compact_space.compact_univ _ _⟩ lemma is_compact.prod {s : set α} {t : set β} (hs : is_compact s) (ht : is_compact t) : is_compact (set.prod s t) := begin rw compact_iff_ultrafilter_le_nhds at hs ht ⊢, intros f hf hfs, rw le_principal_iff at hfs, rcases hs (map prod.fst f) (ultrafilter_map hf) (le_principal_iff.2 (mem_map_sets_iff.2 ⟨_, hfs, image_subset_iff.2 (λ s h, h.1)⟩)) with ⟨a, sa, ha⟩, rcases ht (map prod.snd f) (ultrafilter_map hf) (le_principal_iff.2 (mem_map_sets_iff.2 ⟨_, hfs, image_subset_iff.2 (λ s h, h.2)⟩)) with ⟨b, tb, hb⟩, rw map_le_iff_le_comap at ha hb, refine ⟨⟨a, b⟩, ⟨sa, tb⟩, _⟩, rw nhds_prod_eq, exact le_inf ha hb end /-- Finite topological spaces are compact. -/ @[priority 100] instance fintype.compact_space [fintype α] : compact_space α := { compact_univ := set.finite_univ.is_compact } /-- The product of two compact spaces is compact. -/ instance [compact_space α] [compact_space β] : compact_space (α × β) := ⟨by { rw ← univ_prod_univ, exact compact_univ.prod compact_univ }⟩ /-- The disjoint union of two compact spaces is compact. -/ instance [compact_space α] [compact_space β] : compact_space (α ⊕ β) := ⟨begin rw ← range_inl_union_range_inr, exact (compact_range continuous_inl).union (compact_range continuous_inr) end⟩ section tychonoff variables {ι : Type*} {π : ι → Type*} [∀i, topological_space (π i)] /-- Tychonoff's theorem -/ lemma compact_pi_infinite {s : Πi:ι, set (π i)} : (∀i, is_compact (s i)) → is_compact {x : Πi:ι, π i | ∀i, x i ∈ s i} := begin simp only [compact_iff_ultrafilter_le_nhds, nhds_pi, exists_prop, mem_set_of_eq, le_infi_iff, le_principal_iff], intros h f hf hfs, have : ∀i:ι, ∃a, a∈s i ∧ tendsto (λx:Πi:ι, π i, x i) f (𝓝 a), { refine λ i, h i _ (ultrafilter_map hf) (mem_map.2 _), exact mem_sets_of_superset hfs (λ x hx, hx i) }, choose a ha, exact ⟨a, assume i, (ha i).left, assume i, (ha i).right.le_comap⟩ end /-- A version of Tychonoff's theorem that uses `set.pi`. -/ lemma compact_univ_pi {s : Πi:ι, set (π i)} (h : ∀i, is_compact (s i)) : is_compact (set.pi set.univ s) := by { convert compact_pi_infinite h, simp only [pi, forall_prop_of_true, mem_univ] } instance pi.compact [∀i:ι, compact_space (π i)] : compact_space (Πi, π i) := ⟨begin have A : is_compact {x : Πi:ι, π i | ∀i, x i ∈ (univ : set (π i))} := compact_pi_infinite (λi, compact_univ), have : {x : Πi:ι, π i | ∀i, x i ∈ (univ : set (π i))} = univ := by ext; simp, rwa this at A, end⟩ end tychonoff instance quot.compact_space {r : α → α → Prop} [compact_space α] : compact_space (quot r) := ⟨by { rw ← range_quot_mk, exact compact_range continuous_quot_mk }⟩ instance quotient.compact_space {s : setoid α} [compact_space α] : compact_space (quotient s) := quot.compact_space /-- There are various definitions of "locally compact space" in the literature, which agree for Hausdorff spaces but not in general. This one is the precise condition on X needed for the evaluation `map C(X, Y) × X → Y` to be continuous for all `Y` when `C(X, Y)` is given the compact-open topology. -/ class locally_compact_space (α : Type*) [topological_space α] : Prop := (local_compact_nhds : ∀ (x : α) (n ∈ 𝓝 x), ∃ s ∈ 𝓝 x, s ⊆ n ∧ is_compact s) /-- A reformulation of the definition of locally compact space: In a locally compact space, every open set containing `x` has a compact subset containing `x` in its interior. -/ lemma exists_compact_subset [locally_compact_space α] {x : α} {U : set α} (hU : is_open U) (hx : x ∈ U) : ∃ (K : set α), is_compact K ∧ x ∈ interior K ∧ K ⊆ U := begin rcases locally_compact_space.local_compact_nhds x U _ with ⟨K, h1K, h2K, h3K⟩, { refine ⟨K, h3K, _, h2K⟩, rwa [ mem_interior_iff_mem_nhds] }, rwa [← mem_interior_iff_mem_nhds, hU.interior_eq] end lemma is_ultrafilter.le_nhds_Lim [compact_space α] (F : ultrafilter α) : F.1 ≤ nhds (@Lim _ _ F.1.nonempty_of_ne_bot F.1) := begin rcases compact_iff_ultrafilter_le_nhds.mp compact_univ F.1 F.2 (by simp) with ⟨x, -, h⟩, exact le_nhds_Lim ⟨x,h⟩, end end compact section clopen /-- A set is clopen if it is both open and closed. -/ def is_clopen (s : set α) : Prop := is_open s ∧ is_closed s theorem is_clopen_union {s t : set α} (hs : is_clopen s) (ht : is_clopen t) : is_clopen (s ∪ t) := ⟨is_open_union hs.1 ht.1, is_closed_union hs.2 ht.2⟩ theorem is_clopen_inter {s t : set α} (hs : is_clopen s) (ht : is_clopen t) : is_clopen (s ∩ t) := ⟨is_open_inter hs.1 ht.1, is_closed_inter hs.2 ht.2⟩ @[simp] theorem is_clopen_empty : is_clopen (∅ : set α) := ⟨is_open_empty, is_closed_empty⟩ @[simp] theorem is_clopen_univ : is_clopen (univ : set α) := ⟨is_open_univ, is_closed_univ⟩ theorem is_clopen_compl {s : set α} (hs : is_clopen s) : is_clopen sᶜ := ⟨hs.2, is_closed_compl_iff.2 hs.1⟩ @[simp] theorem is_clopen_compl_iff {s : set α} : is_clopen sᶜ ↔ is_clopen s := ⟨λ h, compl_compl s ▸ is_clopen_compl h, is_clopen_compl⟩ theorem is_clopen_diff {s t : set α} (hs : is_clopen s) (ht : is_clopen t) : is_clopen (s \ t) := is_clopen_inter hs (is_clopen_compl ht) end clopen section preirreducible /-- A preirreducible set `s` is one where there is no non-trivial pair of disjoint opens on `s`. -/ def is_preirreducible (s : set α) : Prop := ∀ (u v : set α), is_open u → is_open v → (s ∩ u).nonempty → (s ∩ v).nonempty → (s ∩ (u ∩ v)).nonempty /-- An irreducible set `s` is one that is nonempty and where there is no non-trivial pair of disjoint opens on `s`. -/ def is_irreducible (s : set α) : Prop := s.nonempty ∧ is_preirreducible s lemma is_irreducible.nonempty {s : set α} (h : is_irreducible s) : s.nonempty := h.1 lemma is_irreducible.is_preirreducible {s : set α} (h : is_irreducible s) : is_preirreducible s := h.2 theorem is_preirreducible_empty : is_preirreducible (∅ : set α) := λ _ _ _ _ _ ⟨x, h1, h2⟩, h1.elim theorem is_irreducible_singleton {x} : is_irreducible ({x} : set α) := ⟨singleton_nonempty x, λ u v _ _ ⟨y, h1, h2⟩ ⟨z, h3, h4⟩, by rw mem_singleton_iff at h1 h3; substs y z; exact ⟨x, rfl, h2, h4⟩⟩ theorem is_preirreducible.closure {s : set α} (H : is_preirreducible s) : is_preirreducible (closure s) := λ u v hu hv ⟨y, hycs, hyu⟩ ⟨z, hzcs, hzv⟩, let ⟨p, hpu, hps⟩ := mem_closure_iff.1 hycs u hu hyu in let ⟨q, hqv, hqs⟩ := mem_closure_iff.1 hzcs v hv hzv in let ⟨r, hrs, hruv⟩ := H u v hu hv ⟨p, hps, hpu⟩ ⟨q, hqs, hqv⟩ in ⟨r, subset_closure hrs, hruv⟩ lemma is_irreducible.closure {s : set α} (h : is_irreducible s) : is_irreducible (closure s) := ⟨h.nonempty.closure, h.is_preirreducible.closure⟩ theorem exists_preirreducible (s : set α) (H : is_preirreducible s) : ∃ t : set α, is_preirreducible t ∧ s ⊆ t ∧ ∀ u, is_preirreducible u → t ⊆ u → u = t := let ⟨m, hm, hsm, hmm⟩ := zorn.zorn_subset₀ {t : set α | is_preirreducible t} (λ c hc hcc hcn, let ⟨t, htc⟩ := hcn in ⟨⋃₀ c, λ u v hu hv ⟨y, hy, hyu⟩ ⟨z, hz, hzv⟩, let ⟨p, hpc, hyp⟩ := mem_sUnion.1 hy, ⟨q, hqc, hzq⟩ := mem_sUnion.1 hz in or.cases_on (zorn.chain.total hcc hpc hqc) (assume hpq : p ⊆ q, let ⟨x, hxp, hxuv⟩ := hc hqc u v hu hv ⟨y, hpq hyp, hyu⟩ ⟨z, hzq, hzv⟩ in ⟨x, mem_sUnion_of_mem hxp hqc, hxuv⟩) (assume hqp : q ⊆ p, let ⟨x, hxp, hxuv⟩ := hc hpc u v hu hv ⟨y, hyp, hyu⟩ ⟨z, hqp hzq, hzv⟩ in ⟨x, mem_sUnion_of_mem hxp hpc, hxuv⟩), λ x hxc, set.subset_sUnion_of_mem hxc⟩) s H in ⟨m, hm, hsm, λ u hu hmu, hmm _ hu hmu⟩ /-- A maximal irreducible set that contains a given point. -/ def irreducible_component (x : α) : set α := classical.some (exists_preirreducible {x} is_irreducible_singleton.is_preirreducible) lemma irreducible_component_property (x : α) : is_preirreducible (irreducible_component x) ∧ {x} ⊆ (irreducible_component x) ∧ ∀ u, is_preirreducible u → (irreducible_component x) ⊆ u → u = (irreducible_component x) := classical.some_spec (exists_preirreducible {x} is_irreducible_singleton.is_preirreducible) theorem mem_irreducible_component {x : α} : x ∈ irreducible_component x := singleton_subset_iff.1 (irreducible_component_property x).2.1 theorem is_irreducible_irreducible_component {x : α} : is_irreducible (irreducible_component x) := ⟨⟨x, mem_irreducible_component⟩, (irreducible_component_property x).1⟩ theorem eq_irreducible_component {x : α} : ∀ {s : set α}, is_preirreducible s → irreducible_component x ⊆ s → s = irreducible_component x := (irreducible_component_property x).2.2 theorem is_closed_irreducible_component {x : α} : is_closed (irreducible_component x) := closure_eq_iff_is_closed.1 $ eq_irreducible_component is_irreducible_irreducible_component.is_preirreducible.closure subset_closure /-- A preirreducible space is one where there is no non-trivial pair of disjoint opens. -/ class preirreducible_space (α : Type u) [topological_space α] : Prop := (is_preirreducible_univ [] : is_preirreducible (univ : set α)) /-- An irreducible space is one that is nonempty and where there is no non-trivial pair of disjoint opens. -/ class irreducible_space (α : Type u) [topological_space α] extends preirreducible_space α : Prop := (to_nonempty [] : nonempty α) attribute [instance, priority 50] irreducible_space.to_nonempty -- see Note [lower instance priority] theorem nonempty_preirreducible_inter [preirreducible_space α] {s t : set α} : is_open s → is_open t → s.nonempty → t.nonempty → (s ∩ t).nonempty := by simpa only [univ_inter, univ_subset_iff] using @preirreducible_space.is_preirreducible_univ α _ _ s t theorem is_preirreducible.image [topological_space β] {s : set α} (H : is_preirreducible s) (f : α → β) (hf : continuous_on f s) : is_preirreducible (f '' s) := begin rintros u v hu hv ⟨_, ⟨⟨x, hx, rfl⟩, hxu⟩⟩ ⟨_, ⟨⟨y, hy, rfl⟩, hyv⟩⟩, rw ← set.mem_preimage at hxu hyv, rcases continuous_on_iff'.1 hf u hu with ⟨u', hu', u'_eq⟩, rcases continuous_on_iff'.1 hf v hv with ⟨v', hv', v'_eq⟩, have := H u' v' hu' hv', rw [set.inter_comm s u', ← u'_eq] at this, rw [set.inter_comm s v', ← v'_eq] at this, rcases this ⟨x, hxu, hx⟩ ⟨y, hyv, hy⟩ with ⟨z, hzs, hzu', hzv'⟩, refine ⟨f z, mem_image_of_mem f hzs, _, _⟩, all_goals { rw ← set.mem_preimage, apply set.mem_of_mem_inter_left, show z ∈ _ ∩ s, simp [*] } end theorem is_irreducible.image [topological_space β] {s : set α} (H : is_irreducible s) (f : α → β) (hf : continuous_on f s) : is_irreducible (f '' s) := ⟨nonempty_image_iff.mpr H.nonempty, H.is_preirreducible.image f hf⟩ lemma subtype.preirreducible_space {s : set α} (h : is_preirreducible s) : preirreducible_space s := { is_preirreducible_univ := begin intros u v hu hv hsu hsv, rw is_open_induced_iff at hu hv, rcases hu with ⟨u, hu, rfl⟩, rcases hv with ⟨v, hv, rfl⟩, rcases hsu with ⟨⟨x, hxs⟩, hxs', hxu⟩, rcases hsv with ⟨⟨y, hys⟩, hys', hyv⟩, rcases h u v hu hv ⟨x, hxs, hxu⟩ ⟨y, hys, hyv⟩ with ⟨z, hzs, ⟨hzu, hzv⟩⟩, exact ⟨⟨z, hzs⟩, ⟨set.mem_univ _, ⟨hzu, hzv⟩⟩⟩ end } lemma subtype.irreducible_space {s : set α} (h : is_irreducible s) : irreducible_space s := { is_preirreducible_univ := (subtype.preirreducible_space h.is_preirreducible).is_preirreducible_univ, to_nonempty := h.nonempty.to_subtype } /-- A set `s` is irreducible if and only if for every finite collection of open sets all of whose members intersect `s`, `s` also intersects the intersection of the entire collection (i.e., there is an element of `s` contained in every member of the collection). -/ lemma is_irreducible_iff_sInter {s : set α} : is_irreducible s ↔ ∀ (U : finset (set α)) (hU : ∀ u ∈ U, is_open u) (H : ∀ u ∈ U, (s ∩ u).nonempty), (s ∩ ⋂₀ ↑U).nonempty := begin split; intro h, { intro U, apply finset.induction_on U, { intros, simpa using h.nonempty }, { intros u U hu IH hU H, rw [finset.coe_insert, sInter_insert], apply h.2, { solve_by_elim [finset.mem_insert_self] }, { apply is_open_sInter (finset.finite_to_set U), intros, solve_by_elim [finset.mem_insert_of_mem] }, { solve_by_elim [finset.mem_insert_self] }, { apply IH, all_goals { intros, solve_by_elim [finset.mem_insert_of_mem] } } } }, { split, { simpa using h ∅ _ _; intro u; simp }, intros u v hu hv hu' hv', simpa using h {u,v} _ _, all_goals { intro t, rw [finset.mem_insert, finset.mem_singleton], rintro (rfl|rfl); assumption } } end /-- A set is preirreducible if and only if for every cover by two closed sets, it is contained in one of the two covering sets. -/ lemma is_preirreducible_iff_closed_union_closed {s : set α} : is_preirreducible s ↔ ∀ (z₁ z₂ : set α), is_closed z₁ → is_closed z₂ → s ⊆ z₁ ∪ z₂ → s ⊆ z₁ ∨ s ⊆ z₂ := begin split, all_goals { intros h t₁ t₂ ht₁ ht₂, specialize h t₁ᶜ t₂ᶜ, simp only [is_open_compl_iff, is_closed_compl_iff] at h, specialize h ht₁ ht₂ }, { contrapose!, simp only [not_subset], rintro ⟨⟨x, hx, hx'⟩, ⟨y, hy, hy'⟩⟩, rcases h ⟨x, hx, hx'⟩ ⟨y, hy, hy'⟩ with ⟨z, hz, hz'⟩, rw ← compl_union at hz', exact ⟨z, hz, hz'⟩ }, { rintro ⟨x, hx, hx'⟩ ⟨y, hy, hy'⟩, rw ← compl_inter at h, delta set.nonempty, rw imp_iff_not_or at h, contrapose! h, split, { intros z hz hz', exact h z ⟨hz, hz'⟩ }, { split; intro H; refine H _ ‹_›; assumption } } end /-- A set is irreducible if and only if for every cover by a finite collection of closed sets, it is contained in one of the members of the collection. -/ lemma is_irreducible_iff_sUnion_closed {s : set α} : is_irreducible s ↔ ∀ (Z : finset (set α)) (hZ : ∀ z ∈ Z, is_closed z) (H : s ⊆ ⋃₀ ↑Z), ∃ z ∈ Z, s ⊆ z := begin rw [is_irreducible, is_preirreducible_iff_closed_union_closed], split; intro h, { intro Z, apply finset.induction_on Z, { intros, rw [finset.coe_empty, sUnion_empty] at H, rcases h.1 with ⟨x, hx⟩, exfalso, tauto }, { intros z Z hz IH hZ H, cases h.2 z (⋃₀ ↑Z) _ _ _ with h' h', { exact ⟨z, finset.mem_insert_self _ _, h'⟩ }, { rcases IH _ h' with ⟨z', hz', hsz'⟩, { exact ⟨z', finset.mem_insert_of_mem hz', hsz'⟩ }, { intros, solve_by_elim [finset.mem_insert_of_mem] } }, { solve_by_elim [finset.mem_insert_self] }, { rw sUnion_eq_bUnion, apply is_closed_bUnion (finset.finite_to_set Z), { intros, solve_by_elim [finset.mem_insert_of_mem] } }, { simpa using H } } }, { split, { by_contradiction hs, simpa using h ∅ _ _, { intro z, simp }, { simpa [set.nonempty] using hs } }, intros z₁ z₂ hz₁ hz₂ H, have := h {z₁, z₂} _ _, simp only [exists_prop, finset.mem_insert, finset.mem_singleton] at this, { rcases this with ⟨z, rfl|rfl, hz⟩; tauto }, { intro t, rw [finset.mem_insert, finset.mem_singleton], rintro (rfl|rfl); assumption }, { simpa using H } } end end preirreducible section preconnected /-- A preconnected set is one where there is no non-trivial open partition. -/ def is_preconnected (s : set α) : Prop := ∀ (u v : set α), is_open u → is_open v → s ⊆ u ∪ v → (s ∩ u).nonempty → (s ∩ v).nonempty → (s ∩ (u ∩ v)).nonempty /-- A connected set is one that is nonempty and where there is no non-trivial open partition. -/ def is_connected (s : set α) : Prop := s.nonempty ∧ is_preconnected s lemma is_connected.nonempty {s : set α} (h : is_connected s) : s.nonempty := h.1 lemma is_connected.is_preconnected {s : set α} (h : is_connected s) : is_preconnected s := h.2 theorem is_preirreducible.is_preconnected {s : set α} (H : is_preirreducible s) : is_preconnected s := λ _ _ hu hv _, H _ _ hu hv theorem is_irreducible.is_connected {s : set α} (H : is_irreducible s) : is_connected s := ⟨H.nonempty, H.is_preirreducible.is_preconnected⟩ theorem is_preconnected_empty : is_preconnected (∅ : set α) := is_preirreducible_empty.is_preconnected theorem is_connected_singleton {x} : is_connected ({x} : set α) := is_irreducible_singleton.is_connected /-- If any point of a set is joined to a fixed point by a preconnected subset, then the original set is preconnected as well. -/ theorem is_preconnected_of_forall {s : set α} (x : α) (H : ∀ y ∈ s, ∃ t ⊆ s, x ∈ t ∧ y ∈ t ∧ is_preconnected t) : is_preconnected s := begin rintros u v hu hv hs ⟨z, zs, zu⟩ ⟨y, ys, yv⟩, have xs : x ∈ s, by { rcases H y ys with ⟨t, ts, xt, yt, ht⟩, exact ts xt }, wlog xu : x ∈ u := hs xs using [u v y z, v u z y], rcases H y ys with ⟨t, ts, xt, yt, ht⟩, have := ht u v hu hv(subset.trans ts hs) ⟨x, xt, xu⟩ ⟨y, yt, yv⟩, exact this.imp (λ z hz, ⟨ts hz.1, hz.2⟩) end /-- If any two points of a set are contained in a preconnected subset, then the original set is preconnected as well. -/ theorem is_preconnected_of_forall_pair {s : set α} (H : ∀ x y ∈ s, ∃ t ⊆ s, x ∈ t ∧ y ∈ t ∧ is_preconnected t) : is_preconnected s := begin rintros u v hu hv hs ⟨x, xs, xu⟩ ⟨y, ys, yv⟩, rcases H x y xs ys with ⟨t, ts, xt, yt, ht⟩, have := ht u v hu hv(subset.trans ts hs) ⟨x, xt, xu⟩ ⟨y, yt, yv⟩, exact this.imp (λ z hz, ⟨ts hz.1, hz.2⟩) end /-- A union of a family of preconnected sets with a common point is preconnected as well. -/ theorem is_preconnected_sUnion (x : α) (c : set (set α)) (H1 : ∀ s ∈ c, x ∈ s) (H2 : ∀ s ∈ c, is_preconnected s) : is_preconnected (⋃₀ c) := begin apply is_preconnected_of_forall x, rintros y ⟨s, sc, ys⟩, exact ⟨s, subset_sUnion_of_mem sc, H1 s sc, ys, H2 s sc⟩ end theorem is_preconnected.union (x : α) {s t : set α} (H1 : x ∈ s) (H2 : x ∈ t) (H3 : is_preconnected s) (H4 : is_preconnected t) : is_preconnected (s ∪ t) := sUnion_pair s t ▸ is_preconnected_sUnion x {s, t} (by rintro r (rfl | rfl | h); assumption) (by rintro r (rfl | rfl | h); assumption) theorem is_connected.union {s t : set α} (H : (s ∩ t).nonempty) (Hs : is_connected s) (Ht : is_connected t) : is_connected (s ∪ t) := begin rcases H with ⟨x, hx⟩, refine ⟨⟨x, mem_union_left t (mem_of_mem_inter_left hx)⟩, _⟩, exact is_preconnected.union x (mem_of_mem_inter_left hx) (mem_of_mem_inter_right hx) Hs.is_preconnected Ht.is_preconnected end theorem is_preconnected.closure {s : set α} (H : is_preconnected s) : is_preconnected (closure s) := λ u v hu hv hcsuv ⟨y, hycs, hyu⟩ ⟨z, hzcs, hzv⟩, let ⟨p, hpu, hps⟩ := mem_closure_iff.1 hycs u hu hyu in let ⟨q, hqv, hqs⟩ := mem_closure_iff.1 hzcs v hv hzv in let ⟨r, hrs, hruv⟩ := H u v hu hv (subset.trans subset_closure hcsuv) ⟨p, hps, hpu⟩ ⟨q, hqs, hqv⟩ in ⟨r, subset_closure hrs, hruv⟩ theorem is_connected.closure {s : set α} (H : is_connected s) : is_connected (closure s) := ⟨H.nonempty.closure, H.is_preconnected.closure⟩ theorem is_preconnected.image [topological_space β] {s : set α} (H : is_preconnected s) (f : α → β) (hf : continuous_on f s) : is_preconnected (f '' s) := begin -- Unfold/destruct definitions in hypotheses rintros u v hu hv huv ⟨_, ⟨x, xs, rfl⟩, xu⟩ ⟨_, ⟨y, ys, rfl⟩, yv⟩, rcases continuous_on_iff'.1 hf u hu with ⟨u', hu', u'_eq⟩, rcases continuous_on_iff'.1 hf v hv with ⟨v', hv', v'_eq⟩, -- Reformulate `huv : f '' s ⊆ u ∪ v` in terms of `u'` and `v'` replace huv : s ⊆ u' ∪ v', { rw [image_subset_iff, preimage_union] at huv, replace huv := subset_inter huv (subset.refl _), rw [inter_distrib_right, u'_eq, v'_eq, ← inter_distrib_right] at huv, exact (subset_inter_iff.1 huv).1 }, -- Now `s ⊆ u' ∪ v'`, so we can apply `‹is_preconnected s›` obtain ⟨z, hz⟩ : (s ∩ (u' ∩ v')).nonempty, { refine H u' v' hu' hv' huv ⟨x, _⟩ ⟨y, _⟩; rw inter_comm, exacts [u'_eq ▸ ⟨xu, xs⟩, v'_eq ▸ ⟨yv, ys⟩] }, rw [← inter_self s, inter_assoc, inter_left_comm s u', ← inter_assoc, inter_comm s, inter_comm s, ← u'_eq, ← v'_eq] at hz, exact ⟨f z, ⟨z, hz.1.2, rfl⟩, hz.1.1, hz.2.1⟩ end theorem is_connected.image [topological_space β] {s : set α} (H : is_connected s) (f : α → β) (hf : continuous_on f s) : is_connected (f '' s) := ⟨nonempty_image_iff.mpr H.nonempty, H.is_preconnected.image f hf⟩ theorem is_preconnected_closed_iff {s : set α} : is_preconnected s ↔ ∀ t t', is_closed t → is_closed t' → s ⊆ t ∪ t' → (s ∩ t).nonempty → (s ∩ t').nonempty → (s ∩ (t ∩ t')).nonempty := ⟨begin rintros h t t' ht ht' htt' ⟨x, xs, xt⟩ ⟨y, ys, yt'⟩, by_contradiction h', rw [← ne_empty_iff_nonempty, ne.def, not_not, ← subset_compl_iff_disjoint, compl_inter] at h', have xt' : x ∉ t', from (h' xs).elim (absurd xt) id, have yt : y ∉ t, from (h' ys).elim id (absurd yt'), have := ne_empty_iff_nonempty.2 (h tᶜ t'ᶜ (is_open_compl_iff.2 ht) (is_open_compl_iff.2 ht') h' ⟨y, ys, yt⟩ ⟨x, xs, xt'⟩), rw [ne.def, ← compl_union, ← subset_compl_iff_disjoint, compl_compl] at this, contradiction end, begin rintros h u v hu hv huv ⟨x, xs, xu⟩ ⟨y, ys, yv⟩, by_contradiction h', rw [← ne_empty_iff_nonempty, ne.def, not_not, ← subset_compl_iff_disjoint, compl_inter] at h', have xv : x ∉ v, from (h' xs).elim (absurd xu) id, have yu : y ∉ u, from (h' ys).elim id (absurd yv), have := ne_empty_iff_nonempty.2 (h uᶜ vᶜ (is_closed_compl_iff.2 hu) (is_closed_compl_iff.2 hv) h' ⟨y, ys, yu⟩ ⟨x, xs, xv⟩), rw [ne.def, ← compl_union, ← subset_compl_iff_disjoint, compl_compl] at this, contradiction end⟩ /-- The connected component of a point is the maximal connected set that contains this point. -/ def connected_component (x : α) : set α := ⋃₀ { s : set α | is_preconnected s ∧ x ∈ s } /-- The connected component of a point inside a set. -/ def connected_component_in (F : set α) (x : F) : set α := coe '' (connected_component x) theorem mem_connected_component {x : α} : x ∈ connected_component x := mem_sUnion_of_mem (mem_singleton x) ⟨is_connected_singleton.is_preconnected, mem_singleton x⟩ theorem is_connected_connected_component {x : α} : is_connected (connected_component x) := ⟨⟨x, mem_connected_component⟩, is_preconnected_sUnion x _ (λ _, and.right) (λ _, and.left)⟩ theorem subset_connected_component {x : α} {s : set α} (H1 : is_preconnected s) (H2 : x ∈ s) : s ⊆ connected_component x := λ z hz, mem_sUnion_of_mem hz ⟨H1, H2⟩ theorem is_closed_connected_component {x : α} : is_closed (connected_component x) := closure_eq_iff_is_closed.1 $ subset.antisymm (subset_connected_component is_connected_connected_component.closure.is_preconnected (subset_closure mem_connected_component)) subset_closure theorem irreducible_component_subset_connected_component {x : α} : irreducible_component x ⊆ connected_component x := subset_connected_component is_irreducible_irreducible_component.is_connected.is_preconnected mem_irreducible_component /-- A preconnected space is one where there is no non-trivial open partition. -/ class preconnected_space (α : Type u) [topological_space α] : Prop := (is_preconnected_univ : is_preconnected (univ : set α)) export preconnected_space (is_preconnected_univ) /-- A connected space is a nonempty one where there is no non-trivial open partition. -/ class connected_space (α : Type u) [topological_space α] extends preconnected_space α : Prop := (to_nonempty : nonempty α) attribute [instance, priority 50] connected_space.to_nonempty -- see Note [lower instance priority] lemma is_connected_range [topological_space β] [connected_space α] {f : α → β} (h : continuous f) : is_connected (range f) := begin inhabit α, rw ← image_univ, exact ⟨⟨f (default α), mem_image_of_mem _ (mem_univ _)⟩, is_preconnected.image is_preconnected_univ _ h.continuous_on⟩ end lemma connected_space_iff_connected_component : connected_space α ↔ ∃ x : α, connected_component x = univ := begin split, { rintros ⟨h, ⟨x⟩⟩, exactI ⟨x, eq_univ_of_univ_subset $ subset_connected_component is_preconnected_univ (mem_univ x)⟩ }, { rintros ⟨x, h⟩, haveI : preconnected_space α := ⟨by {rw ← h, exact is_connected_connected_component.2 }⟩, exact ⟨⟨x⟩⟩ } end @[priority 100] -- see Note [lower instance priority] instance preirreducible_space.preconnected_space (α : Type u) [topological_space α] [preirreducible_space α] : preconnected_space α := ⟨(preirreducible_space.is_preirreducible_univ α).is_preconnected⟩ @[priority 100] -- see Note [lower instance priority] instance irreducible_space.connected_space (α : Type u) [topological_space α] [irreducible_space α] : connected_space α := { to_nonempty := irreducible_space.to_nonempty α } theorem nonempty_inter [preconnected_space α] {s t : set α} : is_open s → is_open t → s ∪ t = univ → s.nonempty → t.nonempty → (s ∩ t).nonempty := by simpa only [univ_inter, univ_subset_iff] using @preconnected_space.is_preconnected_univ α _ _ s t theorem is_clopen_iff [preconnected_space α] {s : set α} : is_clopen s ↔ s = ∅ ∨ s = univ := ⟨λ hs, classical.by_contradiction $ λ h, have h1 : s ≠ ∅ ∧ sᶜ ≠ ∅, from ⟨mt or.inl h, mt (λ h2, or.inr $ (by rw [← compl_compl s, h2, compl_empty] : s = univ)) h⟩, let ⟨_, h2, h3⟩ := nonempty_inter hs.1 hs.2 (union_compl_self s) (ne_empty_iff_nonempty.1 h1.1) (ne_empty_iff_nonempty.1 h1.2) in h3 h2, by rintro (rfl | rfl); [exact is_clopen_empty, exact is_clopen_univ]⟩ lemma eq_univ_of_nonempty_clopen [preconnected_space α] {s : set α} (h : s.nonempty) (h' : is_clopen s) : s = univ := by { rw is_clopen_iff at h', finish [h.ne_empty] } lemma subtype.preconnected_space {s : set α} (h : is_preconnected s) : preconnected_space s := { is_preconnected_univ := begin intros u v hu hv hs hsu hsv, rw is_open_induced_iff at hu hv, rcases hu with ⟨u, hu, rfl⟩, rcases hv with ⟨v, hv, rfl⟩, rcases hsu with ⟨⟨x, hxs⟩, hxs', hxu⟩, rcases hsv with ⟨⟨y, hys⟩, hys', hyv⟩, rcases h u v hu hv _ ⟨x, hxs, hxu⟩ ⟨y, hys, hyv⟩ with ⟨z, hzs, ⟨hzu, hzv⟩⟩, exact ⟨⟨z, hzs⟩, ⟨set.mem_univ _, ⟨hzu, hzv⟩⟩⟩, intros z hz, rcases hs (set.mem_univ ⟨z, hz⟩) with hzu|hzv, { left, assumption }, { right, assumption } end } lemma subtype.connected_space {s : set α} (h : is_connected s) : connected_space s := { is_preconnected_univ := (subtype.preconnected_space h.is_preconnected).is_preconnected_univ, to_nonempty := h.nonempty.to_subtype } lemma is_preconnected_iff_preconnected_space {s : set α} : is_preconnected s ↔ preconnected_space s := ⟨subtype.preconnected_space, begin introI, simpa using is_preconnected_univ.image (coe : s → α) continuous_subtype_coe.continuous_on end⟩ lemma is_connected_iff_connected_space {s : set α} : is_connected s ↔ connected_space s := ⟨subtype.connected_space, λ h, ⟨nonempty_subtype.mp h.2, is_preconnected_iff_preconnected_space.mpr h.1⟩⟩ /-- A set `s` is preconnected if and only if for every cover by two open sets that are disjoint on `s`, it is contained in one of the two covering sets. -/ lemma is_preconnected_iff_subset_of_disjoint {s : set α} : is_preconnected s ↔ ∀ (u v : set α) (hu : is_open u) (hv : is_open v) (hs : s ⊆ u ∪ v) (huv : s ∩ (u ∩ v) = ∅), s ⊆ u ∨ s ⊆ v := begin split; intro h, { intros u v hu hv hs huv, specialize h u v hu hv hs, contrapose! huv, rw ne_empty_iff_nonempty, simp [not_subset] at huv, rcases huv with ⟨⟨x, hxs, hxu⟩, ⟨y, hys, hyv⟩⟩, have hxv : x ∈ v := or_iff_not_imp_left.mp (hs hxs) hxu, have hyu : y ∈ u := or_iff_not_imp_right.mp (hs hys) hyv, exact h ⟨y, hys, hyu⟩ ⟨x, hxs, hxv⟩ }, { intros u v hu hv hs hsu hsv, rw ← ne_empty_iff_nonempty, intro H, specialize h u v hu hv hs H, contrapose H, apply ne_empty_iff_nonempty.mpr, cases h, { rcases hsv with ⟨x, hxs, hxv⟩, exact ⟨x, hxs, ⟨h hxs, hxv⟩⟩ }, { rcases hsu with ⟨x, hxs, hxu⟩, exact ⟨x, hxs, ⟨hxu, h hxs⟩⟩ } } end /-- A set `s` is connected if and only if for every cover by a finite collection of open sets that are pairwise disjoint on `s`, it is contained in one of the members of the collection. -/ lemma is_connected_iff_sUnion_disjoint_open {s : set α} : is_connected s ↔ ∀ (U : finset (set α)) (H : ∀ (u v : set α), u ∈ U → v ∈ U → (s ∩ (u ∩ v)).nonempty → u = v) (hU : ∀ u ∈ U, is_open u) (hs : s ⊆ ⋃₀ ↑U), ∃ u ∈ U, s ⊆ u := begin rw [is_connected, is_preconnected_iff_subset_of_disjoint], split; intro h, { intro U, apply finset.induction_on U, { rcases h.left, suffices : s ⊆ ∅ → false, { simpa }, intro, solve_by_elim }, { intros u U hu IH hs hU H, rw [finset.coe_insert, sUnion_insert] at H, cases h.2 u (⋃₀ ↑U) _ _ H _ with hsu hsU, { exact ⟨u, finset.mem_insert_self _ _, hsu⟩ }, { rcases IH _ _ hsU with ⟨v, hvU, hsv⟩, { exact ⟨v, finset.mem_insert_of_mem hvU, hsv⟩ }, { intros, apply hs; solve_by_elim [finset.mem_insert_of_mem] }, { intros, solve_by_elim [finset.mem_insert_of_mem] } }, { solve_by_elim [finset.mem_insert_self] }, { apply is_open_sUnion, intros, solve_by_elim [finset.mem_insert_of_mem] }, { apply eq_empty_of_subset_empty, rintro x ⟨hxs, hxu, hxU⟩, rw mem_sUnion at hxU, rcases hxU with ⟨v, hvU, hxv⟩, rcases hs u v (finset.mem_insert_self _ _) (finset.mem_insert_of_mem hvU) _ with rfl, { contradiction }, { exact ⟨x, hxs, hxu, hxv⟩ } } } }, { split, { rw ← ne_empty_iff_nonempty, by_contradiction hs, push_neg at hs, subst hs, simpa using h ∅ _ _ _; simp }, intros u v hu hv hs hsuv, rcases h {u, v} _ _ _ with ⟨t, ht, ht'⟩, { rw [finset.mem_insert, finset.mem_singleton] at ht, rcases ht with rfl|rfl; tauto }, { intros t₁ t₂ ht₁ ht₂ hst, rw ← ne_empty_iff_nonempty at hst, rw [finset.mem_insert, finset.mem_singleton] at ht₁ ht₂, rcases ht₁ with rfl|rfl; rcases ht₂ with rfl|rfl, all_goals { refl <|> contradiction <|> skip }, rw inter_comm t₁ at hst, contradiction }, { intro t, rw [finset.mem_insert, finset.mem_singleton], rintro (rfl|rfl); assumption }, { simpa using hs } } end end preconnected section totally_disconnected /-- A set is called totally disconnected if all of its connected components are singletons. -/ def is_totally_disconnected (s : set α) : Prop := ∀ t, t ⊆ s → is_preconnected t → subsingleton t theorem is_totally_disconnected_empty : is_totally_disconnected (∅ : set α) := λ t ht _, ⟨λ ⟨_, h⟩, (ht h).elim⟩ theorem is_totally_disconnected_singleton {x} : is_totally_disconnected ({x} : set α) := λ t ht _, ⟨λ ⟨p, hp⟩ ⟨q, hq⟩, subtype.eq $ show p = q, from (eq_of_mem_singleton (ht hp)).symm ▸ (eq_of_mem_singleton (ht hq)).symm⟩ /-- A space is totally disconnected if all of its connected components are singletons. -/ class totally_disconnected_space (α : Type u) [topological_space α] : Prop := (is_totally_disconnected_univ : is_totally_disconnected (univ : set α)) end totally_disconnected section totally_separated /-- A set `s` is called totally separated if any two points of this set can be separated by two disjoint open sets covering `s`. -/ def is_totally_separated (s : set α) : Prop := ∀ x ∈ s, ∀ y ∈ s, x ≠ y → ∃ u v : set α, is_open u ∧ is_open v ∧ x ∈ u ∧ y ∈ v ∧ s ⊆ u ∪ v ∧ u ∩ v = ∅ theorem is_totally_separated_empty : is_totally_separated (∅ : set α) := λ x, false.elim theorem is_totally_separated_singleton {x} : is_totally_separated ({x} : set α) := λ p hp q hq hpq, (hpq $ (eq_of_mem_singleton hp).symm ▸ (eq_of_mem_singleton hq).symm).elim theorem is_totally_disconnected_of_is_totally_separated {s : set α} (H : is_totally_separated s) : is_totally_disconnected s := λ t hts ht, ⟨λ ⟨x, hxt⟩ ⟨y, hyt⟩, subtype.eq $ classical.by_contradiction $ assume hxy : x ≠ y, let ⟨u, v, hu, hv, hxu, hyv, hsuv, huv⟩ := H x (hts hxt) y (hts hyt) hxy in let ⟨r, hrt, hruv⟩ := ht u v hu hv (subset.trans hts hsuv) ⟨x, hxt, hxu⟩ ⟨y, hyt, hyv⟩ in (ext_iff.1 huv r).1 hruv⟩ /-- A space is totally separated if any two points can be separated by two disjoint open sets covering the whole space. -/ class totally_separated_space (α : Type u) [topological_space α] : Prop := (is_totally_separated_univ [] : is_totally_separated (univ : set α)) @[priority 100] -- see Note [lower instance priority] instance totally_separated_space.totally_disconnected_space (α : Type u) [topological_space α] [totally_separated_space α] : totally_disconnected_space α := ⟨is_totally_disconnected_of_is_totally_separated $ totally_separated_space.is_totally_separated_univ α⟩ end totally_separated
aa56ae23fc33266dd580fbf5fc194aa9d4a381f4
f3849be5d845a1cb97680f0bbbe03b85518312f0
/library/tools/super/simp.lean
9663d42f7556b8928db7cc5b05408d17b1044da4
[ "Apache-2.0" ]
permissive
bjoeris/lean
0ed95125d762b17bfcb54dad1f9721f953f92eeb
4e496b78d5e73545fa4f9a807155113d8e6b0561
refs/heads/master
1,611,251,218,281
1,495,337,658,000
1,495,337,658,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,496
lean
/- Copyright (c) 2016 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ import .clause_ops .prover_state open tactic monad namespace super meta def prove_using_assumption : tactic unit := do tgt ← target, ass ← mk_local_def `h tgt, exact ass meta def simplify_capturing_assumptions (type : expr) : tactic (expr × expr × list expr) := do S ← simp_lemmas.mk_default, (type', heq) ← simplify S type, hyps ← return $ contained_lconsts type, hyps' ← return $ contained_lconsts_list [type', heq], add_hyps ← return $ list.filter (λn : expr, ¬hyps.contains n.local_uniq_name) hyps'.values, return (type', heq, add_hyps) meta def try_simplify_left (c : clause) (i : ℕ) : tactic (list clause) := on_left_at c i $ λtype, do (type', heq, add_hyps) ← simplify_capturing_assumptions type, hyp ← mk_local_def `h type', prf ← mk_eq_mpr heq hyp, return [(hyp::add_hyps, prf)] meta def try_simplify_right (c : clause) (i : ℕ) : tactic (list clause) := on_right_at' c i $ λhyp, do (type', heq, add_hyps) ← simplify_capturing_assumptions hyp.local_type, heqtype ← infer_type heq, heqsymm ← mk_eq_symm heq, prf ← mk_eq_mpr heqsymm hyp, return [(add_hyps, prf)] meta def simp_inf : inf_decl := inf_decl.mk 40 $ take given, sequence' $ do r ← [try_simplify_right, try_simplify_left], i ← list.range given.c.num_lits, [inf_if_successful 2 given (r given.c i)] end super
8ea58cfa849af3fc8b6cd1962b14cd07e819e03a
947b78d97130d56365ae2ec264df196ce769371a
/src/Lean/Elab/Syntax.lean
a685f2ab7e477648c03a611285aa5b1a0404050c
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
23,106
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.Elab.Command import Lean.Elab.Quotation namespace Lean namespace Elab namespace Term /- Expand `optional «precedence»` where «precedence» := parser! " : " >> precedenceLit precedenceLit : Parser := numLit <|> maxSymbol maxSymbol := parser! nonReservedSymbol "max" -/ def expandOptPrecedence (stx : Syntax) : Option Nat := if stx.isNone then none else match ((stx.getArg 0).getArg 1).isNatLit? with | some v => some v | _ => some Parser.maxPrec private def mkParserSeq (ds : Array Syntax) : TermElabM Syntax := if ds.size == 0 then throwUnsupportedSyntax else if ds.size == 1 then pure $ ds.get! 0 else ds.foldlFromM (fun r d => `(ParserDescr.andthen $r $d)) (ds.get! 0) 1 structure ToParserDescrContext := (catName : Name) (first : Bool) (leftRec : Bool) -- true iff left recursion is allowed /- When `leadingIdentAsSymbol == true` we convert `Lean.Parser.Syntax.atom` into `Lean.ParserDescr.nonReservedSymbol` See comment at `Parser.ParserCategory`. -/ (leadingIdentAsSymbol : Bool) abbrev ToParserDescrM := ReaderT ToParserDescrContext (StateRefT Bool TermElabM) private def markAsTrailingParser : ToParserDescrM Unit := set true @[inline] private def withNotFirst {α} (x : ToParserDescrM α) : ToParserDescrM α := adaptReader (fun (ctx : ToParserDescrContext) => { ctx with first := false }) x @[inline] private def withoutLeftRec {α} (x : ToParserDescrM α) : ToParserDescrM α := adaptReader (fun (ctx : ToParserDescrContext) => { ctx with leftRec := false }) x def checkLeftRec (stx : Syntax) : ToParserDescrM Bool := do ctx ← read; if ctx.first && stx.getKind == `Lean.Parser.Syntax.cat then do let cat := (stx.getIdAt 0).eraseMacroScopes; if cat == ctx.catName then do let prec? : Option Nat := expandOptPrecedence (stx.getArg 1); unless prec?.isNone $ throwErrorAt (stx.getArg 1) ("invalid occurrence of ':<num>' modifier in head"); unless ctx.leftRec $ throwErrorAt (stx.getArg 3) ("invalid occurrence of '" ++ cat ++ "', parser algorithm does not allow this form of left recursion"); markAsTrailingParser; -- mark as trailing par pure true else pure false else pure false partial def toParserDescrAux : Syntax → ToParserDescrM Syntax | stx => let kind := stx.getKind; if kind == nullKind then do let args := stx.getArgs; condM (checkLeftRec (stx.getArg 0)) (do when (args.size == 1) $ throwErrorAt stx "invalid atomic left recursive syntax"; let args := args.eraseIdx 0; args ← args.mapIdxM $ fun i arg => withNotFirst $ toParserDescrAux arg; liftM $ mkParserSeq args) (do args ← args.mapIdxM $ fun i arg => withNotFirst $ toParserDescrAux arg; liftM $ mkParserSeq args) else if kind == choiceKind then do toParserDescrAux (stx.getArg 0) else if kind == `Lean.Parser.Syntax.paren then toParserDescrAux (stx.getArg 1) else if kind == `Lean.Parser.Syntax.cat then do let cat := (stx.getIdAt 0).eraseMacroScopes; ctx ← read; if ctx.first && cat == ctx.catName then throwErrorAt stx "invalid atomic left recursive syntax" else do let prec? : Option Nat := expandOptPrecedence (stx.getArg 1); env ← getEnv; if Parser.isParserCategory env cat then let prec := prec?.getD 0; `(ParserDescr.cat $(quote cat) $(quote prec)) else do -- `cat` is not a valid category name. Thus, we test whether it is a valid constant candidates ← liftM $ resolveGlobalConst cat; let candidates := candidates.filter fun c => match env.find? c with | none => false | some info => match info.type with | Expr.const `Lean.Parser.TrailingParser _ _ => true | Expr.const `Lean.Parser.Parser _ _ => true | Expr.const `Lean.ParserDescr _ _ => true | Expr.const `Lean.TrailingParserDescr _ _ => true | _ => false; match candidates with | [] => throwErrorAt (stx.getArg 3) ("unknown category '" ++ cat ++ "' or parser declaration") | [c] => do unless prec?.isNone $ throwErrorAt (stx.getArg 3) "unexpected precedence"; `(ParserDescr.parser $(quote c)) | cs => throwErrorAt (stx.getArg 3) ("ambiguous parser declaration " ++ toString cs) else if kind == `Lean.Parser.Syntax.atom then do match (stx.getArg 0).isStrLit? with | some atom => do ctx ← read; if ctx.leadingIdentAsSymbol then `(ParserDescr.nonReservedSymbol $(quote atom) false) else `(ParserDescr.symbol $(quote atom)) | none => throwUnsupportedSyntax else if kind == `Lean.Parser.Syntax.num then `(ParserDescr.numLit) else if kind == `Lean.Parser.Syntax.str then `(ParserDescr.strLit) else if kind == `Lean.Parser.Syntax.char then `(ParserDescr.charLit) else if kind == `Lean.Parser.Syntax.ident then `(ParserDescr.ident) else if kind == `Lean.Parser.Syntax.try then do d ← withoutLeftRec $ toParserDescrAux (stx.getArg 1); `(ParserDescr.try $d) else if kind == `Lean.Parser.Syntax.notFollowedBy then do d ← withoutLeftRec $ toParserDescrAux (stx.getArg 1); `(ParserDescr.notFollowedBy $d) else if kind == `Lean.Parser.Syntax.lookahead then do d ← withoutLeftRec $ toParserDescrAux (stx.getArg 1); `(ParserDescr.lookahead $d) else if kind == `Lean.Parser.Syntax.sepBy then do d₁ ← withoutLeftRec $ toParserDescrAux (stx.getArg 1); d₂ ← withoutLeftRec $ toParserDescrAux (stx.getArg 2); `(ParserDescr.sepBy $d₁ $d₂) else if kind == `Lean.Parser.Syntax.sepBy1 then do d₁ ← withoutLeftRec $ toParserDescrAux (stx.getArg 1); d₂ ← withoutLeftRec $ toParserDescrAux (stx.getArg 2); `(ParserDescr.sepBy1 $d₁ $d₂) else if kind == `Lean.Parser.Syntax.many then do d ← withoutLeftRec $ toParserDescrAux (stx.getArg 0); `(ParserDescr.many $d) else if kind == `Lean.Parser.Syntax.many1 then do d ← withoutLeftRec $ toParserDescrAux (stx.getArg 0); `(ParserDescr.many1 $d) else if kind == `Lean.Parser.Syntax.optional then do d ← withoutLeftRec $ toParserDescrAux (stx.getArg 0); `(ParserDescr.optional $d) else if kind == `Lean.Parser.Syntax.orelse then do d₁ ← withoutLeftRec $ toParserDescrAux (stx.getArg 0); d₂ ← withoutLeftRec $ toParserDescrAux (stx.getArg 2); `(ParserDescr.orelse $d₁ $d₂) else do stxNew? ← liftM (liftMacroM (expandMacro? stx) : TermElabM _); match stxNew? with | some stxNew => toParserDescrAux stxNew | none => throwErrorAt stx $ "unexpected syntax kind of category `syntax`: " ++ kind /-- Given a `stx` of category `syntax`, return a pair `(newStx, trailingParser)`, where `newStx` is of category `term`. After elaboration, `newStx` should have type `TrailingParserDescr` if `trailingParser == true`, and `ParserDescr` otherwise. -/ def toParserDescr (stx : Syntax) (catName : Name) : TermElabM (Syntax × Bool) := do env ← getEnv; let leadingIdentAsSymbol := Parser.leadingIdentAsSymbol env catName; (toParserDescrAux stx { catName := catName, first := true, leftRec := true, leadingIdentAsSymbol := leadingIdentAsSymbol }).run false end Term namespace Command private def getCatSuffix (catName : Name) : String := match catName with | Name.str _ s _ => s | _ => unreachable! private def declareSyntaxCatQuotParser (catName : Name) : CommandElabM Unit := do let quotSymbol := "`(" ++ getCatSuffix catName ++ "|"; let kind := catName ++ `quot; cmd ← `(@[termParser] def $(mkIdent kind) : Lean.ParserDescr := Lean.ParserDescr.node $(quote kind) $(quote Lean.Parser.maxPrec) (Lean.ParserDescr.andthen (Lean.ParserDescr.symbol $(quote quotSymbol)) (Lean.ParserDescr.andthen (Lean.ParserDescr.cat $(quote catName) 0) (Lean.ParserDescr.symbol ")")))); elabCommand cmd @[builtinCommandElab syntaxCat] def elabDeclareSyntaxCat : CommandElab := fun stx => do let catName := stx.getIdAt 1; let attrName := catName.appendAfter "Parser"; env ← getEnv; env ← liftIO $ Parser.registerParserCategory env attrName catName; setEnv env; declareSyntaxCatQuotParser catName def mkKindName (catName : Name) : Name := `_kind ++ catName def mkFreshKind (catName : Name) : CommandElabM Name := do scp ← getCurrMacroScope; mainModule ← getMainModule; pure $ Lean.addMacroScope mainModule (mkKindName catName) scp def Macro.mkFreshKind (catName : Name) : MacroM Name := Macro.addMacroScope (mkKindName catName) private def elabKindPrio (stx : Syntax) (catName : Name) : CommandElabM (Name × Nat) := do if stx.isNone then do k ← mkFreshKind catName; pure (k, 0) else let mkKind (stx : Syntax) : CommandElabM Name := do { let kind := stx.getId; if kind.hasMacroScopes then pure kind else do currNamespace ← getCurrNamespace; pure (currNamespace ++ kind) }; let arg := stx.getArg 1; if arg.getKind == `Lean.Parser.Command.parserKind then do k ← mkKind (arg.getArg 0); pure (k, 0) else if arg.getKind == `Lean.Parser.Command.parserPrio then do k ← mkFreshKind catName; let prio := (arg.getArg 0).isNatLit?.getD 0; pure (k, prio) else if arg.getKind == `Lean.Parser.Command.parserKindPrio then do k ← mkKind (arg.getArg 0); let prio := (arg.getArg 2).isNatLit?.getD 0; pure (k, prio) else throwError "unexpected syntax kind/priority" /- We assume a new syntax can be treated as an atom when it starts and ends with a token. Here are examples of atom-like syntax. ``` syntax "(" term ")" : term syntax "[" (sepBy term ",") "]" : term syntax "foo" : term ``` -/ private partial def isAtomLikeSyntax : Syntax → Bool | stx => let kind := stx.getKind; if kind == nullKind then isAtomLikeSyntax (stx.getArg 0) && isAtomLikeSyntax (stx.getArg (stx.getNumArgs - 1)) else if kind == choiceKind then isAtomLikeSyntax (stx.getArg 0) -- see toParserDescrAux else if kind == `Lean.Parser.Syntax.paren then isAtomLikeSyntax (stx.getArg 1) else kind == `Lean.Parser.Syntax.atom /- def «syntax» := parser! "syntax " >> optPrecedence >> optKindPrio >> many1 syntaxParser >> " : " >> ident -/ @[builtinCommandElab «syntax»] def elabSyntax : CommandElab := fun stx => do env ← getEnv; let cat := (stx.getIdAt 5).eraseMacroScopes; unless (Parser.isParserCategory env cat) $ throwErrorAt (stx.getArg 5) ("unknown category '" ++ cat ++ "'"); let syntaxParser := stx.getArg 3; -- If the user did not provide an explicit precedence, we assign `maxPrec` to atom-like syntax and `leadPrec` otherwise. let precDefault := if isAtomLikeSyntax syntaxParser then Parser.maxPrec else Parser.leadPrec; let prec := (Term.expandOptPrecedence (stx.getArg 1)).getD precDefault; (kind, prio) ← elabKindPrio (stx.getArg 2) cat; let catParserId := mkIdentFrom stx (cat.appendAfter "Parser"); (val, trailingParser) ← runTermElabM none $ fun _ => Term.toParserDescr syntaxParser cat; d ← if trailingParser then `(@[$catParserId:ident $(quote prio):numLit] def $(mkIdentFrom stx kind) : Lean.TrailingParserDescr := ParserDescr.trailingNode $(quote kind) $(quote prec) $val) else `(@[$catParserId:ident $(quote prio):numLit] def $(mkIdentFrom stx kind) : Lean.ParserDescr := ParserDescr.node $(quote kind) $(quote prec) $val); trace `Elab fun _ => d; withMacroExpansion stx d $ elabCommand d /- def syntaxAbbrev := parser! "syntax " >> ident >> " := " >> many1 syntaxParser -/ @[builtinCommandElab «syntaxAbbrev»] def elabSyntaxAbbrev : CommandElab := fun stx => do let declName := stx.getArg 1; (val, _) ← runTermElabM none $ fun _ => Term.toParserDescr (stx.getArg 3) Name.anonymous; stx' ← `(def $declName : Lean.ParserDescr := $val); withMacroExpansion stx stx' $ elabCommand stx' def elabMacroRulesAux (k : SyntaxNodeKind) (alts : Array Syntax) : CommandElabM Syntax := do alts ← alts.mapSepElemsM $ fun alt => do { let lhs := alt.getArg 0; let pat := lhs.getArg 0; when (!pat.isQuot) $ throwUnsupportedSyntax; let quot := pat.getArg 1; let k' := quot.getKind; if k' == k then pure alt else if k' == choiceKind then do match quot.getArgs.find? $ fun quotAlt => quotAlt.getKind == k with | none => throwErrorAt alt ("invalid macro_rules alternative, expected syntax node kind '" ++ k ++ "'") | some quot => do let pat := pat.setArg 1 quot; let lhs := lhs.setArg 0 pat; pure $ alt.setArg 0 lhs else throwErrorAt alt ("invalid macro_rules alternative, unexpected syntax node kind '" ++ k' ++ "'") }; `(@[macro $(Lean.mkIdent k)] def myMacro : Macro := fun stx => match_syntax stx with $alts:matchAlt* | _ => throw Lean.Macro.Exception.unsupportedSyntax) def inferMacroRulesAltKind (alt : Syntax) : CommandElabM SyntaxNodeKind := do let lhs := alt.getArg 0; let pat := lhs.getArg 0; when (!pat.isQuot) $ throwUnsupportedSyntax; let quot := pat.getArg 1; pure quot.getKind def elabNoKindMacroRulesAux (alts : Array Syntax) : CommandElabM Syntax := do k ← inferMacroRulesAltKind (alts.get! 0); if k == choiceKind then throwErrorAt (alts.get! 0) "invalid macro_rules alternative, multiple interpretations for pattern (solution: specify node kind using `macro_rules [<kind>] ...`)" else do altsK ← alts.filterSepElemsM (fun alt => do k' ← inferMacroRulesAltKind alt; pure $ k == k'); altsNotK ← alts.filterSepElemsM (fun alt => do k' ← inferMacroRulesAltKind alt; pure $ k != k'); defCmd ← elabMacroRulesAux k altsK; if altsNotK.isEmpty then pure defCmd else `($defCmd:command macro_rules $altsNotK:matchAlt*) @[builtinCommandElab «macro_rules»] def elabMacroRules : CommandElab := adaptExpander $ fun stx => match_syntax stx with | `(macro_rules $alts:matchAlt*) => elabNoKindMacroRulesAux alts | `(macro_rules | $alts:matchAlt*) => elabNoKindMacroRulesAux alts | `(macro_rules [$kind] $alts:matchAlt*) => elabMacroRulesAux kind.getId alts | `(macro_rules [$kind] | $alts:matchAlt*) => elabMacroRulesAux kind.getId alts | _ => throwUnsupportedSyntax @[builtinMacro Lean.Parser.Command.mixfix] def expandMixfix : Macro := fun stx => match_syntax stx with | `(infix:$prec $op => $f) => `(infixl:$prec $op => $f) | `(infixr:$prec $op => $f) => `(notation:$prec lhs $op:strLit rhs:$prec => $f lhs rhs) | `(infixl:$prec $op => $f) => let prec1 : Syntax := quote (prec.toNat+1); `(notation:$prec lhs $op:strLit rhs:$prec1 => $f lhs rhs) | `(prefix:$prec $op => $f) => `(notation:$prec $op:strLit arg:$prec => $f arg) | `(postfix:$prec $op => $f) => `(notation:$prec arg $op:strLit => $f arg) | _ => Macro.throwUnsupported /- Wrap all occurrences of the given `ident` nodes in antiquotations -/ private partial def antiquote (vars : Array Syntax) : Syntax → Syntax | stx => match_syntax stx with | `($id:ident) => if (vars.findIdx? (fun var => var.getId == id.getId)).isSome then Syntax.node `antiquot #[mkAtom "$", mkNullNode, id, mkNullNode, mkNullNode] else stx | _ => match stx with | Syntax.node k args => Syntax.node k (args.map antiquote) | stx => stx /- Convert `notation` command lhs item into a `syntax` command item -/ def expandNotationItemIntoSyntaxItem (stx : Syntax) : MacroM Syntax := let k := stx.getKind; if k == `Lean.Parser.Command.identPrec then pure $ Syntax.node `Lean.Parser.Syntax.cat #[mkIdentFrom stx `term, stx.getArg 1] else if k == quotedSymbolKind then match stx.getArg 1 with | Syntax.atom info val => pure $ Syntax.node `Lean.Parser.Syntax.atom #[mkStxStrLit val info] | _ => Macro.throwUnsupported else if k == strLitKind then pure $ Syntax.node `Lean.Parser.Syntax.atom #[stx] else Macro.throwUnsupported def strLitToPattern (stx: Syntax) : MacroM Syntax := match stx.isStrLit? with | some str => pure $ mkAtomFrom stx str | none => Macro.throwUnsupported /- Convert `notation` command lhs item a pattern element -/ def expandNotationItemIntoPattern (stx : Syntax) : MacroM Syntax := let k := stx.getKind; if k == `Lean.Parser.Command.identPrec then let item := stx.getArg 0; pure $ mkNode `antiquot #[mkAtom "$", mkNullNode, item, mkNullNode, mkNullNode] else if k == quotedSymbolKind then pure $ stx.getArg 1 else if k == strLitKind then strLitToPattern stx else Macro.throwUnsupported private def expandNotationAux (ref : Syntax) (prec? : Option Syntax) (items : Array Syntax) (rhs : Syntax) : MacroM Syntax := do kind ← Macro.mkFreshKind `term; -- build parser syntaxParts ← items.mapM expandNotationItemIntoSyntaxItem; let cat := mkIdentFrom ref `term; -- build macro rules let vars := items.filter $ fun item => item.getKind == `Lean.Parser.Command.identPrec; let vars := vars.map $ fun var => var.getArg 0; let rhs := antiquote vars rhs; patArgs ← items.mapM expandNotationItemIntoPattern; let pat := Syntax.node kind patArgs; match prec? with | none => `(syntax [$(mkIdentFrom ref kind):ident] $syntaxParts* : $cat macro_rules | `($pat) => `($rhs)) | some prec => `(syntax:$prec [$(mkIdentFrom ref kind):ident] $syntaxParts* : $cat macro_rules | `($pat) => `($rhs)) @[builtinMacro Lean.Parser.Command.notation] def expandNotation : Macro := fun stx => match_syntax stx with | `(notation:$prec $items* => $rhs) => expandNotationAux stx prec items rhs | `(notation $items:notationItem* => $rhs) => expandNotationAux stx none items rhs | _ => Macro.throwUnsupported /- Convert `macro` argument into a `syntax` command item -/ def expandMacroArgIntoSyntaxItem (stx : Syntax) : MacroM Syntax := let k := stx.getKind; if k == `Lean.Parser.Command.macroArgSimple then pure $ stx.getArg 2 else if k == strLitKind then pure $ Syntax.node `Lean.Parser.Syntax.atom #[stx] else Macro.throwUnsupported /- Convert `macro` head into a `syntax` command item -/ def expandMacroHeadIntoSyntaxItem (stx : Syntax) : MacroM Syntax := if stx.isIdent then let info := stx.getHeadInfo.getD {}; let id := stx.getId; pure $ Syntax.node `Lean.Parser.Syntax.atom #[mkStxStrLit (toString id) info] else expandMacroArgIntoSyntaxItem stx /- Convert `macro` arg into a pattern element -/ def expandMacroArgIntoPattern (stx : Syntax) : MacroM Syntax := let k := stx.getKind; if k == `Lean.Parser.Command.macroArgSimple then let item := stx.getArg 0; pure $ mkNode `antiquot #[mkAtom "$", mkNullNode, item, mkNullNode, mkNullNode] else if k == strLitKind then strLitToPattern stx else Macro.throwUnsupported /- Convert `macro` head into a pattern element -/ def expandMacroHeadIntoPattern (stx : Syntax) : MacroM Syntax := if stx.isIdent then pure $ mkAtomFrom stx (toString stx.getId) else expandMacroArgIntoPattern stx @[builtinMacro Lean.Parser.Command.macro] def expandMacro : Macro := fun stx => do let prec := (stx.getArg 1).getArgs; let head := stx.getArg 2; let args := (stx.getArg 3).getArgs; let cat := stx.getArg 5; kind ← Macro.mkFreshKind (cat.getId).eraseMacroScopes; -- build parser stxPart ← expandMacroHeadIntoSyntaxItem head; stxParts ← args.mapM expandMacroArgIntoSyntaxItem; let stxParts := #[stxPart] ++ stxParts; -- build macro rules patHead ← expandMacroHeadIntoPattern head; patArgs ← args.mapM expandMacroArgIntoPattern; let pat := Syntax.node kind (#[patHead] ++ patArgs); if stx.getArgs.size == 8 then -- `stx` is of the form `macro $head $args* : $cat => term` let rhs := stx.getArg 7; `(syntax $prec* [$(mkIdentFrom stx kind):ident] $stxParts* : $cat macro_rules | `($pat) => $rhs) else -- `stx` is of the form `macro $head $args* : $cat => `( $body )` let rhsBody := stx.getArg 8; `(syntax $prec* [$(mkIdentFrom stx kind):ident] $stxParts* : $cat macro_rules | `($pat) => `($rhsBody)) @[init] private def regTraceClasses : IO Unit := do registerTraceClass `Elab.syntax; pure () @[inline] def withExpectedType (expectedType? : Option Expr) (x : Expr → TermElabM Expr) : TermElabM Expr := do Term.tryPostponeIfNoneOrMVar expectedType?; some expectedType ← pure expectedType? | throwError "expected type must be known"; x expectedType /- def elabTail := try (" : " >> ident) >> darrow >> termParser parser! "elab " >> optPrecedence >> elabHead >> many elabArg >> elabTail -/ @[builtinMacro Lean.Parser.Command.elab] def expandElab : Macro := fun stx => do let ref := stx; let prec := (stx.getArg 1).getArgs; let head := stx.getArg 2; let args := (stx.getArg 3).getArgs; let cat := stx.getArg 5; let expectedTypeSpec := stx.getArg 6; let rhs := stx.getArg 8; let catName := cat.getId; kind ← Macro.mkFreshKind catName.eraseMacroScopes; -- build parser stxPart ← expandMacroHeadIntoSyntaxItem head; stxParts ← args.mapM expandMacroArgIntoSyntaxItem; let stxParts := #[stxPart] ++ stxParts; -- build pattern for `martch_syntax patHead ← expandMacroHeadIntoPattern head; patArgs ← args.mapM expandMacroArgIntoPattern; let pat := Syntax.node kind (#[patHead] ++ patArgs); let kindId := mkIdentFrom ref kind; if expectedTypeSpec.hasArgs then if catName == `term then let expId := expectedTypeSpec.getArg 1; `(syntax $prec* [$kindId:ident] $stxParts* : $cat @[termElab $kindId:ident] def elabFn : Lean.Elab.Term.TermElab := fun stx expectedType? => match_syntax stx with | `($pat) => Lean.Elab.Command.withExpectedType expectedType? fun $expId => $rhs | _ => throwUnsupportedSyntax) else Macro.throwError expectedTypeSpec ("syntax category '" ++ toString catName ++ "' does not support expected type specification") else if catName == `term then `(syntax $prec* [$kindId:ident] $stxParts* : $cat @[termElab $kindId:ident] def elabFn : Lean.Elab.Term.TermElab := fun stx _ => match_syntax stx with | `($pat) => $rhs | _ => throwUnsupportedSyntax) else if catName == `command then `(syntax $prec* [$kindId:ident] $stxParts* : $cat @[commandElab $kindId:ident] def elabFn : Lean.Elab.Command.CommandElab := fun stx => match_syntax stx with | `($pat) => $rhs | _ => throwUnsupportedSyntax) else if catName == `tactic then `(syntax $prec* [$kindId:ident] $stxParts* : $cat @[tactic $kindId:ident] def elabFn : Lean.Elab.Tactic.Tactic := fun stx => match_syntax stx with | `(tactic|$pat) => $rhs | _ => throwUnsupportedSyntax) else -- We considered making the command extensible and support new user-defined categories. We think it is unnecessary. -- If users want this feature, they add their own `elab` macro that uses this one as a fallback. Macro.throwError ref ("unsupported syntax category '" ++ toString catName ++ "'") end Command end Elab end Lean
758d1517a80d12285b8da43cdfa5516b6b081fc5
626e312b5c1cb2d88fca108f5933076012633192
/src/algebra/algebra/operations.lean
1821096d95e01b9dce48cc3a8c739dcc821221dd
[ "Apache-2.0" ]
permissive
Bioye97/mathlib
9db2f9ee54418d29dd06996279ba9dc874fd6beb
782a20a27ee83b523f801ff34efb1a9557085019
refs/heads/master
1,690,305,956,488
1,631,067,774,000
1,631,067,774,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,884
lean
/- Copyright (c) 2019 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import algebra.algebra.basic /-! # Multiplication and division of submodules of an algebra. An interface for multiplication and division of sub-R-modules of an R-algebra A is developed. ## Main definitions Let `R` be a commutative ring (or semiring) and aet `A` be an `R`-algebra. * `1 : submodule R A` : the R-submodule R of the R-algebra A * `has_mul (submodule R A)` : multiplication of two sub-R-modules M and N of A is defined to be the smallest submodule containing all the products `m * n`. * `has_div (submodule R A)` : `I / J` is defined to be the submodule consisting of all `a : A` such that `a • J ⊆ I` It is proved that `submodule R A` is a semiring, and also an algebra over `set A`. ## Tags multiplication of submodules, division of subodules, submodule semiring -/ universes uι u v open algebra set open_locale pointwise namespace submodule variables {ι : Sort uι} variables {R : Type u} [comm_semiring R] section ring variables {A : Type v} [semiring A] [algebra R A] variables (S T : set A) {M N P Q : submodule R A} {m n : A} /-- `1 : submodule R A` is the submodule R of A. -/ instance : has_one (submodule R A) := ⟨(algebra.linear_map R A).range⟩ theorem one_eq_range : (1 : submodule R A) = (algebra.linear_map R A).range := rfl lemma algebra_map_mem (r : R) : algebra_map R A r ∈ (1 : submodule R A) := linear_map.mem_range_self _ _ @[simp] lemma mem_one {x : A} : x ∈ (1 : submodule R A) ↔ ∃ y, algebra_map R A y = x := by simp only [one_eq_range, linear_map.mem_range, algebra.linear_map_apply] theorem one_eq_span : (1 : submodule R A) = R ∙ 1 := begin apply submodule.ext, intro a, simp only [mem_one, mem_span_singleton, algebra.smul_def, mul_one] end theorem one_le : (1 : submodule R A) ≤ P ↔ (1 : A) ∈ P := by simpa only [one_eq_span, span_le, set.singleton_subset_iff] /-- Multiplication of sub-R-modules of an R-algebra A. The submodule `M * N` is the smallest R-submodule of `A` containing the elements `m * n` for `m ∈ M` and `n ∈ N`. -/ instance : has_mul (submodule R A) := ⟨λ M N, ⨆ s : M, N.map $ algebra.lmul R A s.1⟩ theorem mul_mem_mul (hm : m ∈ M) (hn : n ∈ N) : m * n ∈ M * N := (le_supr _ ⟨m, hm⟩ : _ ≤ M * N) ⟨n, hn, rfl⟩ theorem mul_le : M * N ≤ P ↔ ∀ (m ∈ M) (n ∈ N), m * n ∈ P := ⟨λ H m hm n hn, H $ mul_mem_mul hm hn, λ H, supr_le $ λ ⟨m, hm⟩, map_le_iff_le_comap.2 $ λ n hn, H m hm n hn⟩ @[elab_as_eliminator] protected theorem mul_induction_on {C : A → Prop} {r : A} (hr : r ∈ M * N) (hm : ∀ (m ∈ M) (n ∈ N), C (m * n)) (h0 : C 0) (ha : ∀ x y, C x → C y → C (x + y)) (hs : ∀ (r : R) x, C x → C (r • x)) : C r := (@mul_le _ _ _ _ _ _ _ ⟨C, h0, ha, hs⟩).2 hm hr variables R theorem span_mul_span : span R S * span R T = span R (S * T) := begin apply le_antisymm, { rw mul_le, intros a ha b hb, apply span_induction ha, work_on_goal 0 { intros, apply span_induction hb, work_on_goal 0 { intros, exact subset_span ⟨_, _, ‹_›, ‹_›, rfl⟩ } }, all_goals { intros, simp only [mul_zero, zero_mul, zero_mem, left_distrib, right_distrib, mul_smul_comm, smul_mul_assoc], try {apply add_mem _ _ _}, try {apply smul_mem _ _ _} }, assumption' }, { rw span_le, rintros _ ⟨a, b, ha, hb, rfl⟩, exact mul_mem_mul (subset_span ha) (subset_span hb) } end variables {R} variables (M N P Q) protected theorem mul_assoc : (M * N) * P = M * (N * P) := le_antisymm (mul_le.2 $ λ mn hmn p hp, suffices M * N ≤ (M * (N * P)).comap (algebra.lmul_right R p), from this hmn, mul_le.2 $ λ m hm n hn, show m * n * p ∈ M * (N * P), from (mul_assoc m n p).symm ▸ mul_mem_mul hm (mul_mem_mul hn hp)) (mul_le.2 $ λ m hm np hnp, suffices N * P ≤ (M * N * P).comap (algebra.lmul_left R m), from this hnp, mul_le.2 $ λ n hn p hp, show m * (n * p) ∈ M * N * P, from mul_assoc m n p ▸ mul_mem_mul (mul_mem_mul hm hn) hp) @[simp] theorem mul_bot : M * ⊥ = ⊥ := eq_bot_iff.2 $ mul_le.2 $ λ m hm n hn, by rw [submodule.mem_bot] at hn ⊢; rw [hn, mul_zero] @[simp] theorem bot_mul : ⊥ * M = ⊥ := eq_bot_iff.2 $ mul_le.2 $ λ m hm n hn, by rw [submodule.mem_bot] at hm ⊢; rw [hm, zero_mul] @[simp] protected theorem one_mul : (1 : submodule R A) * M = M := by { conv_lhs { rw [one_eq_span, ← span_eq M] }, erw [span_mul_span, one_mul, span_eq] } @[simp] protected theorem mul_one : M * 1 = M := by { conv_lhs { rw [one_eq_span, ← span_eq M] }, erw [span_mul_span, mul_one, span_eq] } variables {M N P Q} @[mono] theorem mul_le_mul (hmp : M ≤ P) (hnq : N ≤ Q) : M * N ≤ P * Q := mul_le.2 $ λ m hm n hn, mul_mem_mul (hmp hm) (hnq hn) theorem mul_le_mul_left (h : M ≤ N) : M * P ≤ N * P := mul_le_mul h (le_refl P) theorem mul_le_mul_right (h : N ≤ P) : M * N ≤ M * P := mul_le_mul (le_refl M) h variables (M N P) theorem mul_sup : M * (N ⊔ P) = M * N ⊔ M * P := le_antisymm (mul_le.2 $ λ m hm np hnp, let ⟨n, hn, p, hp, hnp⟩ := mem_sup.1 hnp in mem_sup.2 ⟨_, mul_mem_mul hm hn, _, mul_mem_mul hm hp, hnp ▸ (mul_add m n p).symm⟩) (sup_le (mul_le_mul_right le_sup_left) (mul_le_mul_right le_sup_right)) theorem sup_mul : (M ⊔ N) * P = M * P ⊔ N * P := le_antisymm (mul_le.2 $ λ mn hmn p hp, let ⟨m, hm, n, hn, hmn⟩ := mem_sup.1 hmn in mem_sup.2 ⟨_, mul_mem_mul hm hp, _, mul_mem_mul hn hp, hmn ▸ (add_mul m n p).symm⟩) (sup_le (mul_le_mul_left le_sup_left) (mul_le_mul_left le_sup_right)) lemma mul_subset_mul : (↑M : set A) * (↑N : set A) ⊆ (↑(M * N) : set A) := by { rintros _ ⟨i, j, hi, hj, rfl⟩, exact mul_mem_mul hi hj } lemma map_mul {A'} [semiring A'] [algebra R A'] (f : A →ₐ[R] A') : map f.to_linear_map (M * N) = map f.to_linear_map M * map f.to_linear_map N := calc map f.to_linear_map (M * N) = ⨆ (i : M), (N.map (lmul R A i)).map f.to_linear_map : map_supr _ _ ... = map f.to_linear_map M * map f.to_linear_map N : begin apply congr_arg Sup, ext S, split; rintros ⟨y, hy⟩, { use [f y, mem_map.mpr ⟨y.1, y.2, rfl⟩], refine trans _ hy, ext, simp }, { obtain ⟨y', hy', fy_eq⟩ := mem_map.mp y.2, use [y', hy'], refine trans _ hy, rw f.to_linear_map_apply at fy_eq, ext, simp [fy_eq] } end section decidable_eq open_locale classical lemma mem_span_mul_finite_of_mem_span_mul {S : set A} {S' : set A} {x : A} (hx : x ∈ span R (S * S')) : ∃ (T T' : finset A), ↑T ⊆ S ∧ ↑T' ⊆ S' ∧ x ∈ span R (T * T' : set A) := begin obtain ⟨U, h, hU⟩ := mem_span_finite_of_mem_span hx, obtain ⟨T, T', hS, hS', h⟩ := finset.subset_mul h, use [T, T', hS, hS'], have h' : (U : set A) ⊆ T * T', { assumption_mod_cast, }, have h'' := span_mono h' hU, assumption, end end decidable_eq lemma mul_eq_span_mul_set (s t : submodule R A) : s * t = span R ((s : set A) * (t : set A)) := by rw [← span_mul_span, span_eq, span_eq] lemma supr_mul (s : ι → submodule R A) (t : submodule R A) : (⨆ i, s i) * t = ⨆ i, s i * t := begin suffices : (⨆ i, span R (s i : set A)) * span R t = (⨆ i, span R (s i) * span R t), { simpa only [span_eq] using this }, simp_rw [span_mul_span, ← span_Union, span_mul_span, set.Union_mul], end lemma mul_supr (t : submodule R A) (s : ι → submodule R A) : t * (⨆ i, s i) = ⨆ i, t * s i := begin suffices : span R (t : set A) * (⨆ i, span R (s i)) = (⨆ i, span R t * span R (s i)), { simpa only [span_eq] using this }, simp_rw [span_mul_span, ← span_Union, span_mul_span, set.mul_Union], end lemma mem_span_mul_finite_of_mem_mul {P Q : submodule R A} {x : A} (hx : x ∈ P * Q) : ∃ (T T' : finset A), (T : set A) ⊆ P ∧ (T' : set A) ⊆ Q ∧ x ∈ span R (T * T' : set A) := submodule.mem_span_mul_finite_of_mem_span_mul (by rwa [← submodule.span_eq P, ← submodule.span_eq Q, submodule.span_mul_span] at hx) variables {M N P} /-- Sub-R-modules of an R-algebra form a semiring. -/ instance : semiring (submodule R A) := { one_mul := submodule.one_mul, mul_one := submodule.mul_one, mul_assoc := submodule.mul_assoc, zero_mul := bot_mul, mul_zero := mul_bot, left_distrib := mul_sup, right_distrib := sup_mul, ..submodule.add_comm_monoid_submodule, ..submodule.has_one, ..submodule.has_mul } variables (M) lemma pow_subset_pow {n : ℕ} : (↑M : set A)^n ⊆ ↑(M^n : submodule R A) := begin induction n with n ih, { erw [pow_zero, pow_zero, set.singleton_subset_iff], rw [set_like.mem_coe, ← one_le], exact le_refl _ }, { rw [pow_succ, pow_succ], refine set.subset.trans (set.mul_subset_mul (subset.refl _) ih) _, apply mul_subset_mul } end /-- `span` is a semiring homomorphism (recall multiplication is pointwise multiplication of subsets on either side). -/ def span.ring_hom : set_semiring A →+* submodule R A := { to_fun := submodule.span R, map_zero' := span_empty, map_one' := one_eq_span.symm, map_add' := span_union, map_mul' := λ s t, by erw [span_mul_span, ← image_mul_prod] } end ring section comm_ring variables {A : Type v} [comm_semiring A] [algebra R A] variables {M N : submodule R A} {m n : A} theorem mul_mem_mul_rev (hm : m ∈ M) (hn : n ∈ N) : n * m ∈ M * N := mul_comm m n ▸ mul_mem_mul hm hn variables (M N) protected theorem mul_comm : M * N = N * M := le_antisymm (mul_le.2 $ λ r hrm s hsn, mul_mem_mul_rev hsn hrm) (mul_le.2 $ λ r hrn s hsm, mul_mem_mul_rev hsm hrn) /-- Sub-R-modules of an R-algebra A form a semiring. -/ instance : comm_semiring (submodule R A) := { mul_comm := submodule.mul_comm, .. submodule.semiring } variables (R A) /-- R-submodules of the R-algebra A are a module over `set A`. -/ instance module_set : module (set_semiring A) (submodule R A) := { smul := λ s P, span R s * P, smul_add := λ _ _ _, mul_add _ _ _, add_smul := λ s t P, show span R (s ⊔ t) * P = _, by { erw [span_union, right_distrib] }, mul_smul := λ s t P, show _ = _ * (_ * _), by { rw [← mul_assoc, span_mul_span, ← image_mul_prod] }, one_smul := λ P, show span R {(1 : A)} * P = _, by { conv_lhs {erw ← span_eq P}, erw [span_mul_span, one_mul, span_eq] }, zero_smul := λ P, show span R ∅ * P = ⊥, by erw [span_empty, bot_mul], smul_zero := λ _, mul_bot _ } variables {R A} lemma smul_def {s : set_semiring A} {P : submodule R A} : s • P = span R s * P := rfl lemma smul_le_smul {s t : set_semiring A} {M N : submodule R A} (h₁ : s.down ≤ t.down) (h₂ : M ≤ N) : s • M ≤ t • N := mul_le_mul (span_mono h₁) h₂ lemma smul_singleton (a : A) (M : submodule R A) : ({a} : set A).up • M = M.map (lmul_left _ a) := begin conv_lhs {rw ← span_eq M}, change span _ _ * span _ _ = _, rw [span_mul_span], apply le_antisymm, { rw span_le, rintros _ ⟨b, m, hb, hm, rfl⟩, rw [set_like.mem_coe, mem_map, set.mem_singleton_iff.mp hb], exact ⟨m, hm, rfl⟩ }, { rintros _ ⟨m, hm, rfl⟩, exact subset_span ⟨a, m, set.mem_singleton a, hm, rfl⟩ } end section quotient /-- The elements of `I / J` are the `x` such that `x • J ⊆ I`. In fact, we define `x ∈ I / J` to be `∀ y ∈ J, x * y ∈ I` (see `mem_div_iff_forall_mul_mem`), which is equivalent to `x • J ⊆ I` (see `mem_div_iff_smul_subset`), but nicer to use in proofs. This is the general form of the ideal quotient, traditionally written $I : J$. -/ instance : has_div (submodule R A) := ⟨ λ I J, { carrier := { x | ∀ y ∈ J, x * y ∈ I }, zero_mem' := λ y hy, by { rw zero_mul, apply submodule.zero_mem }, add_mem' := λ a b ha hb y hy, by { rw add_mul, exact submodule.add_mem _ (ha _ hy) (hb _ hy) }, smul_mem' := λ r x hx y hy, by { rw algebra.smul_mul_assoc, exact submodule.smul_mem _ _ (hx _ hy) } } ⟩ lemma mem_div_iff_forall_mul_mem {x : A} {I J : submodule R A} : x ∈ I / J ↔ ∀ y ∈ J, x * y ∈ I := iff.refl _ lemma mem_div_iff_smul_subset {x : A} {I J : submodule R A} : x ∈ I / J ↔ x • (J : set A) ⊆ I := ⟨ λ h y ⟨y', hy', xy'_eq_y⟩, by { rw ← xy'_eq_y, apply h, assumption }, λ h y hy, h (set.smul_mem_smul_set hy) ⟩ lemma le_div_iff {I J K : submodule R A} : I ≤ J / K ↔ ∀ (x ∈ I) (z ∈ K), x * z ∈ J := iff.refl _ lemma le_div_iff_mul_le {I J K : submodule R A} : I ≤ J / K ↔ I * K ≤ J := by rw [le_div_iff, mul_le] @[simp] lemma one_le_one_div {I : submodule R A} : 1 ≤ 1 / I ↔ I ≤ 1 := begin split, all_goals {intro hI}, {rwa [le_div_iff_mul_le, one_mul] at hI}, {rwa [le_div_iff_mul_le, one_mul]}, end lemma le_self_mul_one_div {I : submodule R A} (hI : I ≤ 1) : I ≤ I * (1 / I) := begin rw [← mul_one I] {occs := occurrences.pos [1]}, apply mul_le_mul_right (one_le_one_div.mpr hI), end lemma mul_one_div_le_one {I : submodule R A} : I * (1 / I) ≤ 1 := begin rw submodule.mul_le, intros m hm n hn, rw [submodule.mem_div_iff_forall_mul_mem] at hn, rw mul_comm, exact hn m hm, end @[simp] lemma map_div {B : Type*} [comm_ring B] [algebra R B] (I J : submodule R A) (h : A ≃ₐ[R] B) : (I / J).map h.to_linear_map = I.map h.to_linear_map / J.map h.to_linear_map := begin ext x, simp only [mem_map, mem_div_iff_forall_mul_mem], split, { rintro ⟨x, hx, rfl⟩ _ ⟨y, hy, rfl⟩, exact ⟨x * y, hx _ hy, h.map_mul x y⟩ }, { rintro hx, refine ⟨h.symm x, λ z hz, _, h.apply_symm_apply x⟩, obtain ⟨xz, xz_mem, hxz⟩ := hx (h z) ⟨z, hz, rfl⟩, convert xz_mem, apply h.injective, erw [h.map_mul, h.apply_symm_apply, hxz] } end end quotient end comm_ring end submodule
d42acdeec5f0613a6f9ef1edfb80e75d9e3f5da0
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/data/list/qsort.lean
460bed4aa454eb1a92ccbb9b6871fb9a553514fd
[]
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,242
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.data.list.lemmas import Mathlib.Lean3Lib.init.wf universes u_1 namespace Mathlib namespace list -- Note: we can't use the equation compiler here because -- init.meta.well_founded_tactics uses this file def qsort.F {α : Type u_1} (lt : α → α → Bool) (x : List α) : ((y : List α) → length y < length x → List α) → List α := sorry /- This is based on the minimalist Haskell "quicksort". Remark: this is *not* really quicksort since it doesn't partition the elements in-place -/ def qsort {α : Type u_1} (lt : α → α → Bool) : List α → List α := well_founded.fix sorry sorry @[simp] theorem qsort_nil {α : Type u_1} (lt : α → α → Bool) : qsort lt [] = [] := sorry @[simp] theorem qsort_cons {α : Type u_1} (lt : α → α → Bool) (h : α) (t : List α) : qsort lt (h :: t) = (fun (_a : List α × List α) => prod.cases_on _a fun (fst snd : List α) => idRhs (List α) (qsort lt snd ++ h :: qsort lt fst)) (partition (fun (x : α) => lt h x = tt) t) := sorry
2256247421c2199ccf319d1e365f32041dbe0d83
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/tactic/linear_combination.lean
d5437549a2bfb144fe67f85118895fdfca6ed29a
[ "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,498
lean
/- Copyright (c) 2022 Abby J. Goldberg. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Abby J. Goldberg -/ import tactic.ring /-! # linear_combination Tactic In this file, the `linear_combination` tactic is created. This tactic, which works over `ring`s, attempts to prove the target by creating and applying a linear combination of a list of equalities. This file also includes a definition for `linear_combination_config`. A `linear_combination_config` object can be passed into the tactic, allowing the user to specify a normalization tactic. ## Implementation Notes This tactic works by creating a weighted sum of the given equations with the given coefficients. Then, it subtracts the right side of the weighted sum from the left side so that the right side equals 0, and it does the same with the target. Afterwards, it sets the goal to be the equality between the lefthand side of the new goal and the lefthand side of the new weighted sum. Lastly, it uses a normalization tactic to see if the weighted sum is equal to the target. ## References * <https://leanprover.zulipchat.com/#narrow/stream/239415-metaprogramming-.2F.20tactics/topic/Linear.20algebra.20tactic/near/213928196> -/ namespace linear_combo open tactic /-! ### Lemmas -/ lemma left_mul_both_sides {α} [h : has_mul α] {x y : α} (z : α) (h1 : x = y) : z * x = z * y := congr_arg (has_mul.mul z) h1 lemma sum_two_equations {α} [h : has_add α] {x1 y1 x2 y2 : α} (h1 : x1 = y1) (h2: x2 = y2) : x1 + x2 = y1 + y2 := congr (congr_arg has_add.add h1) h2 lemma left_minus_right {α} [h : add_group α] {x y : α} (h1 : x = y) : x - y = 0 := sub_eq_zero.mpr h1 lemma all_on_left_equiv {α} [h : add_group α] (x y : α) : (x = y) = (x - y = 0) := propext (⟨left_minus_right, sub_eq_zero.mp⟩) lemma replace_eq_expr {α} [h : has_zero α] {x y : α} (h1 : x = 0) (h2 : y = x) : y = 0 := by rwa h2 /-! ### Configuration -/ /-- A configuration object for `linear_combination`. `normalize` describes whether or not the normalization step should be used. `normalization_tactic` describes the tactic used for normalization when checking if the weighted sum is equivalent to the goal (when `normalize` is `tt`). -/ meta structure linear_combination_config : Type := (normalize : bool := tt) (normalization_tactic : tactic unit := `[ring1]) /-! ### Part 1: Multiplying Equations by Constants and Adding Them Together -/ /-- Given that `lhs = rhs`, this tactic returns an `expr` proving that `coeff * lhs = coeff * rhs`. * Input: * `h_equality` : an `expr`, whose type should be an equality between terms of type `α`, where there is an instance of `has_mul α` * `coeff` : a `pexpr`, which should be a value of type `α` * Output: an `expr`, which proves that the result of multiplying both sides of `h_equality` by the `coeff` holds -/ meta def mul_equality_expr (h_equality : expr) (coeff : pexpr) : tactic expr := do `(%%lhs = %%rhs) ← infer_type h_equality, -- Mark the coefficient as having the same type as the sides of `h_equality` - -- this is necessary in order to use the left_mul_both_sides lemma left_type ← infer_type lhs, coeff_expr ← to_expr ``(%%coeff : %%left_type), mk_app ``left_mul_both_sides [coeff_expr, h_equality] /-- Given two hypotheses that `a = b` and `c = d`, this tactic returns an `expr` proving that `a + c = b + d`. * Input: * `h_equality1` : an `expr`, whose type should be an equality between terms of type `α`, where there is an instance of `has_add α` * `h_equality2` : an `expr`, whose type should be an equality between terms of type `α` * Output: an `expr`, which proves that the result of adding the two equalities holds -/ meta def sum_equalities (h_equality1 h_equality2 : expr) : tactic expr := mk_app ``sum_two_equations [h_equality1, h_equality2] /-- Given that `a = b` and `c = d`, along with a coefficient, this tactic returns an `expr` proving that `a + coeff * c = b + coeff * d`. * Input: * `h_equality1` : an `expr`, whose type should be an equality between terms of type `α`, where there are instances of `has_add α` and `has_mul α` * `h_equality2` : an `expr`, whose type should be an equality between terms of type `α` * `coeff_for_eq2` : a `pexpr`, which should be a value of type `α` * Output: an `expr`, which proves that the result of adding the first equality to the result of multiplying `coeff_for_eq2` by the second equality holds -/ meta def sum_two_hyps_one_mul_helper (h_equality1 h_equality2 : expr) (coeff_for_eq2 : pexpr) : tactic expr := mul_equality_expr h_equality2 coeff_for_eq2 >>= sum_equalities h_equality1 /-- Given that `l_sum1 = r_sum1`, `l_h1 = r_h1`, ..., `l_hn = r_hn`, and given coefficients `c_1`, ..., `c_n`, this tactic returns an `expr` proving that `l_sum1 + (c_1 * l_h1) + ... + (c_n * l_hn)` `= r_sum1 + (c_1 * r_h1) + ... + (c_n * r_hn)` * Input: * an `option (tactic expr)` : `none`, if there is no sum to add to yet, or `some` containing the base summation equation * a `list name` : a list of names, referring to equalities in the local context * a `list pexpr` : a list of coefficients to be multiplied with the corresponding equalities in the list of names * Output: an `expr`, which proves that the weighted sum of the given equalities added to the base equation holds -/ meta def make_sum_of_hyps_helper : option (tactic expr) → list name → list pexpr → tactic expr | none [] [] := do fail "There are no hypotheses to add" | (some tactic_hcombo) [] [] := do tactic_hcombo | none (h_equality_nam :: h_eqs_names) (coeff :: coeffs) := do -- This is the first equality, and we do not have anything to add to it h_equality ← get_local h_equality_nam, make_sum_of_hyps_helper (some (mul_equality_expr h_equality coeff)) h_eqs_names coeffs | (some tactic_hcombo) (h_equality_nam :: h_eqs_names) (coeff :: coeffs) := do h_equality ← get_local h_equality_nam, hcombo ← tactic_hcombo, -- We want to add this weighted equality to the current equality in -- the hypothesis make_sum_of_hyps_helper (some (sum_two_hyps_one_mul_helper hcombo h_equality coeff)) h_eqs_names coeffs | _ _ _ := do fail ("The length of the input list of equalities should be the " ++ "same as the length of the input list of coefficients") /-- Given a list of names referencing equalities and a list of pexprs representing coefficients, this tactic proves that a weighted sum of the equalities (where each equation is multiplied by the corresponding coefficient) holds. * Input: * `h_eqs_names` : a list of names, referring to equalities in the local context * `coeffs` : a list of coefficients to be multiplied with the corresponding equalities in the list of names * Output: an `expr`, which proves that the weighted sum of the equalities holds -/ meta def make_sum_of_hyps (h_eqs_names : list name) (coeffs : list pexpr) : tactic expr := make_sum_of_hyps_helper none h_eqs_names coeffs /-! ### Part 2: Simplifying -/ /-- This tactic proves that the result of moving all the terms in an equality to the left side of the equals sign by subtracting the right side of the equation from the left side holds. In other words, given `lhs = rhs`, this tactic proves that `lhs - rhs = 0`. * Input: * `h_equality` : an `expr`, whose type should be an equality between terms of type `α`, where there is an instance of `add_group α` * Output: an `expr`, which proves that `lhs - rhs = 0`, where `lhs` and `rhs` are the left and right sides of `h_equality` respectively -/ meta def move_to_left_side (h_equality : expr) : tactic expr := mk_app ``left_minus_right [h_equality] /-- This tactic replaces the target with the result of moving all the terms in the target to the left side of the equals sign by subtracting the right side of the equation from the left side. In other words, when the target is lhs = rhs, this tactic proves that `lhs - rhs = 0` and replaces the target with this new equality. Note: The target must be an equality when this tactic is called, and the equality must have some type `α` on each side, where there is an instance of `add_group α`. * Input: N/A * Output: N/A -/ meta def move_target_to_left_side : tactic unit := do -- Move all the terms in the target equality to the left side target ← target, (targ_lhs, targ_rhs) ← match_eq target, target_left_eq ← to_expr ``(%%targ_lhs - %%targ_rhs = 0), mk_app ``all_on_left_equiv [targ_lhs, targ_rhs] >>= replace_target target_left_eq /-! ### Part 3: Matching the Linear Combination to the Target -/ /-- This tactic changes the goal to be that the lefthand side of the target is equal to the lefthand side of the given expression. For example, if `hsum_on_left` is `5*x - 5*y = 0`, and the target is `-5*y + 5*x = 0`, this tactic will change the target to be `-5*y + 5*x = 5*x - 5*y`. This tactic only should be used when the target's type an equality whose right side is 0. * Input: * `hsum_on_left` : expr, whose type should be an equality with 0 on the right side of the equals sign * Output: N/A -/ meta def set_goal_to_hleft_eq_tleft (hsum_on_left : expr) : tactic unit := do to_expr ``(replace_eq_expr %%hsum_on_left) >>= apply, skip /-- This tactic attempts to prove the goal by normalizing the target if the `normalize` field of the given configuration is true. * Input: * `config` : a `linear_combination_config`, which determines the tactic used for normalization if normalization is done * Output: N/A -/ meta def prove_equal_if_desired (config : linear_combination_config) : tactic unit := when config.normalize config.normalization_tactic /-! ### Part 4: Completed Tactic -/ /-- This is a tactic that attempts to prove the target by creating and applying a linear combination of a list of equalities. (If the `normalize` field of the configuration is set to ff, then the tactic will simply set the user up to prove their target using the linear combination instead of attempting to finish the proof.) Note: The left and right sides of all the equalities should have the same ring type, and the coefficients should also have this type. There must be instances of `has_mul` and `add_group` for this type. Also note that the target must involve at least one variable. * Input: * `h_eqs_names` : a list of names, referring to equations in the local context * `coeffs` : a list of coefficients to be multiplied with the corresponding equations in the list of names * `config` : a `linear_combination_config`, which determines the tactic used for normalization; by default, this value is the standard configuration for a `linear_combination_config` * Output: N/A -/ meta def linear_combination (h_eqs_names : list name) (coeffs : list pexpr) (config : linear_combination_config := {}) : tactic unit := do hsum ← make_sum_of_hyps h_eqs_names coeffs, hsum_on_left ← move_to_left_side hsum, move_target_to_left_side, set_goal_to_hleft_eq_tleft hsum_on_left, prove_equal_if_desired config section interactive_mode setup_tactic_parser /-- A parser that matches a pair in parentheses (where the first item in the pair is an identifier and the second item in the pair is a `pexpr`) or an identifier by itself. If the identifier is by itself, this parser behaves as though it was given a `pexpr ` of ``(1) along with the identifier. * Input: None * Output: a `lean.parser (name × pexpr)` -/ meta def parse_name_pexpr_pair : lean.parser (name × pexpr) := (do tk "(", id ← ident, tk ",", coeff ← parser.pexpr 0, tk ")", pure (id, coeff)) <|> (do id ← ident, pure (id, ``(1))) /-- `linear_combination` attempts to prove the target by creating and applying a linear combination of a list of equalities. The tactic will create a linear combination by adding the equalities together from left to right, so the order of the input hypotheses does matter. If the `normalize` field of the configuration is set to false, then the tactic will simply set the user up to prove their target using the linear combination instead of attempting to finish the proof. Note: The left and right sides of all the equalities should have the same type, and the coefficients should also have this type. There must be instances of `has_mul` and `add_group` for this type. * Input: * `input` : the pairs of hypotheses and their corresponding coefficients. If no coefficient is given with a hypothesis, then the coefficient for that hypothesis will be set to 1. * `config` : a linear_combination_config, which determines the tactic used for normalization; by default, this value is the standard configuration for a linear_combination_config. In the standard configuration, `normalize` is set to tt (meaning this tactic is set to use normalization), and `normalization_tactic` is set to `ring1`. Example Usage: ``` example (x y : ℤ) (h1 : x*y + 2*x = 1) (h2 : x = y) : x*y = -2*y + 1 := by linear_combination (h1, 1) (h2, -2) example (x y : ℤ) (h1 : x*y + 2*x = 1) (h2 : x = y) : x*y = -2*y + 1 := by linear_combination h1 (h2, -2) example (x y z : ℝ) (ha : x + 2*y - z = 4) (hb : 2*x + y + z = -2) (hc : x + 2*y + z = 2) : -3*x - 3*y - 4*z = 2 := by linear_combination (ha, 1) (hb, -1) (hc, -2) example (x y : ℚ) (h1 : x + y = 3) (h2 : 3*x = 7) : x*x*y + y*x*y + 6*x = 3*x*y + 14 := by linear_combination (h1, x*y) (h2, 2) example (x y : ℤ) (h1 : x = -3) (h2 : y = 10) : 2*x = -6 := begin linear_combination (h1, 2) {normalize := ff}, simp, norm_cast end ``` -/ meta def _root_.tactic.interactive.linear_combination (input : parse parse_name_pexpr_pair*) (config : linear_combination_config := {}) : tactic unit := let (h_eqs_names, coeffs) := list.unzip input in linear_combination h_eqs_names coeffs config add_tactic_doc { name := "linear_combination", category := doc_category.tactic, decl_names := [`tactic.interactive.linear_combination], tags := [] } end interactive_mode end linear_combo
27241c8304683f960555336418370116dd7dcb93
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/meta/smt/interactive_auto.lean
3eeb51af3b7cb487c6a4cf54f18716731ec6d61e
[]
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
385
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.meta.smt.smt_tactic import Mathlib.Lean3Lib.init.meta.interactive_base import Mathlib.Lean3Lib.init.meta.smt.rsimp namespace Mathlib namespace smt_tactic end Mathlib
cf39754fb21e6e85539d8d3926544f73eda6ff48
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/dsimp_options.lean
464b4ac31cbf377650c136a4ca06de613543002b
[ "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
1,741
lean
example (b c : nat) : c = @bool.cases_on (λ _, nat) tt b c := begin fail_if_success {dsimp {iota := ff}}, dsimp {iota := tt}, guard_target c = c, reflexivity end example (b c : nat) : c = @bool.cases_on (λ _, nat) tt b c := begin dsimp, -- iota is tt by default guard_target c = c, reflexivity end example (b c : nat) : c = (λ x : nat, x) c := begin fail_if_success {dsimp {beta := ff}}, dsimp {beta := tt}, guard_target c = c, reflexivity end example (b c : nat) : c = (λ x : nat, x) c := begin dsimp, -- beta is tt by default guard_target c = c, reflexivity end example (b c : nat) : c = (b, c).2 := begin fail_if_success {dsimp {proj := ff}}, dsimp {proj := tt}, guard_target c = c, reflexivity end example (b c : nat) : c = (b, c).2 := begin dsimp, -- projection reduction is true by default IF argument is a constructor guard_target c = c, reflexivity end example (f g : nat → nat) : f ∘ g = λ x, f (g x) := begin fail_if_success {dsimp}, -- reducible definition (e.g., function.comp) are not unfolded by default dsimp [(∘)], guard_target (λ x, f (g x)) = λ x, f (g x), reflexivity end example (f g : nat → nat) : f ∘ g = λ x, f (g x) := begin dsimp [function.comp], guard_target (λ x, f (g x)) = λ x, f (g x), reflexivity end example (a : nat) : (let x := a in x) = a := begin fail_if_success{dsimp {zeta := ff}}, dsimp {zeta := tt}, guard_target a = a, reflexivity end example (a : nat) : (let x := a in x) = a := begin dsimp, -- zeta is tt by default guard_target a = a, reflexivity end example (a b : nat) : a + b = b + a := begin fail_if_success{dsimp}, -- will not unfold has_add.add applications apply nat.add_comm end
4738a1021e5c4d919f912743bcb67c6ee698f3e8
da23b545e1653cafd4ab88b3a42b9115a0b1355f
/test/forwards_reasoning.lean
7ab420359b06468457e4f06e812f43e9359b21e4
[]
no_license
minchaowu/lean-tidy
137f5058896e0e81dae84bf8d02b74101d21677a
2d4c52d66cf07c59f8746e405ba861b4fa0e3835
refs/heads/master
1,585,283,406,120
1,535,094,033,000
1,535,094,033,000
145,945,792
0
0
null
null
null
null
UTF-8
Lean
false
false
987
lean
import tidy.forwards_reasoning lemma G (n : ℕ) : list ℕ := [n] lemma F : ℕ := 0 section local attribute [forward] G example : 1 = 1 := begin success_if_fail { forwards_library_reasoning }, refl end local attribute [forward] F example : 1 = 1 := begin forwards_library_reasoning, forwards_library_reasoning, success_if_fail { forwards_library_reasoning }, refl end example : 1 = 1 := begin have p := [0], forwards_library_reasoning, success_if_fail { forwards_library_reasoning }, refl end end section inductive T (n : ℕ) | t : ℕ → T @[forward] lemma H.H {n : ℕ} (v : T n) : string := "hello" example : 1 = 1 := begin success_if_fail { forwards_library_reasoning }, have p : T 3 := T.t 3 5, forwards_library_reasoning, guard_hyp H_p := string, -- check that we drop namespaces refl end example (P Q : Prop) (p : P) (h : P → Q): Q := begin forwards_reasoning, success_if_fail { forwards_reasoning }, exact h_p end end
3075918fd7ec6751b3c45c38a798a6847010b10d
37683ecbb27d7c2037bfd9ad7e06d662f460a005
/spectrum/basic.hlean
defccbeb2bd5bf5fbd531d46b751e48390553017
[ "Apache-2.0" ]
permissive
GRSEB9S/Spectral-1
b2443b09cae7aac1247b1f88c846c532ac802b8e
dd14277e0bfc6270a488eb3b9ec231484065b9d8
refs/heads/master
1,631,315,269,407
1,522,048,315,000
1,522,799,803,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
37,750
hlean
/- Copyright (c) 2016 Michael Shulman. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Michael Shulman, Floris van Doorn, Egbert Rijke, Stefano Piceghello, Yuri Sulyma -/ import homotopy.LES_of_homotopy_groups ..algebra.splice ..algebra.seq_colim ..homotopy.EM ..homotopy.fwedge ..pointed_cubes open eq nat int susp pointed pmap sigma is_equiv equiv fiber algebra trunc trunc_index pi group succ_str EM EM.ops function unit lift is_trunc /--------------------- Basic definitions ---------------------/ /- The basic definitions of spectra and prespectra make sense for any successor-structure. -/ structure gen_prespectrum (N : succ_str) := (deloop : N → Type*) (glue : Π(n:N), (deloop n) →* (Ω (deloop (S n)))) attribute gen_prespectrum.deloop [coercion] structure is_spectrum [class] {N : succ_str} (E : gen_prespectrum N) := (is_equiv_glue : Πn, is_equiv (gen_prespectrum.glue E n)) attribute is_spectrum.is_equiv_glue [instance] structure gen_spectrum (N : succ_str) := (to_prespectrum : gen_prespectrum N) (to_is_spectrum : is_spectrum to_prespectrum) attribute gen_spectrum.to_prespectrum [coercion] attribute gen_spectrum.to_is_spectrum [instance] attribute gen_spectrum._trans_of_to_prespectrum [unfold 2] -- Classically, spectra and prespectra use the successor structure +ℕ. -- But we will use +ℤ instead, to reduce case analysis later on. abbreviation prespectrum := gen_prespectrum +ℤ definition prespectrum.mk (Y : ℤ → Type*) (e : Π(n : ℤ), Y n →* Ω (Y (n+1))) : prespectrum := gen_prespectrum.mk Y e abbreviation spectrum := gen_spectrum +ℤ abbreviation spectrum.mk (Y : prespectrum) (e : is_spectrum Y) : spectrum := gen_spectrum.mk Y e namespace spectrum definition glue [unfold 2] {{N : succ_str}} := @gen_prespectrum.glue N --definition glue := (@gen_prespectrum.glue +ℤ) definition equiv_glue {N : succ_str} (E : gen_prespectrum N) [H : is_spectrum E] (n:N) : (E n) ≃* (Ω (E (S n))) := pequiv_of_pmap (glue E n) (is_spectrum.is_equiv_glue E n) definition equiv_glue2 (Y : spectrum) (n : ℤ) : Ω (Ω (Y (n+2))) ≃* Y n := begin refine (!equiv_glue ⬝e* loop_pequiv_loop (!equiv_glue ⬝e* loop_pequiv_loop _))⁻¹ᵉ*, refine pequiv_of_eq (ap Y _), exact add.assoc n 1 1 end definition gluen {N : succ_str} (X : gen_prespectrum N) (n : N) (k : ℕ) : X n →* Ω[k] (X (n +' k)) := by induction k with k f; reflexivity; exact !loopn_succ_in⁻¹ᵉ* ∘* Ω→[k] (glue X (n +' k)) ∘* f -- note: the forward map is (currently) not definitionally equal to gluen. Is that a problem? definition equiv_gluen {N : succ_str} (X : gen_spectrum N) (n : N) (k : ℕ) : X n ≃* Ω[k] (X (n +' k)) := by induction k with k f; reflexivity; exact f ⬝e* (loopn_pequiv_loopn k (equiv_glue X (n +' k)) ⬝e* !loopn_succ_in⁻¹ᵉ*) definition equiv_gluen_inv_succ {N : succ_str} (X : gen_spectrum N) (n : N) (k : ℕ) : (equiv_gluen X n (k+1))⁻¹ᵉ* ~* (equiv_gluen X n k)⁻¹ᵉ* ∘* Ω→[k] (equiv_glue X (n +' k))⁻¹ᵉ* ∘* !loopn_succ_in := begin refine !trans_pinv ⬝* pwhisker_left _ _, refine !trans_pinv ⬝* _, refine pwhisker_left _ !pinv_pinv end definition succ_str_add_eq_int_add (n : ℤ) (m : ℕ) : @succ_str.add sint n m = n + m := begin induction m with m IH, { symmetry, exact add_zero n }, { exact ap int.succ IH ⬝ add.assoc n m 1 } end -- a square when we compose glue with transporting over a path in N definition glue_ptransport {N : succ_str} (X : gen_prespectrum N) {n n' : N} (p : n = n') : glue X n' ∘* ptransport X p ~* Ω→ (ptransport X (ap S p)) ∘* glue X n := by induction p; exact !pcompose_pid ⬝* !pid_pcompose⁻¹* ⬝* pwhisker_right _ !ap1_pid⁻¹* -- Sometimes an ℕ-indexed version does arise naturally, however, so -- we give a standard way to extend an ℕ-indexed (pre)spectrum to a -- ℤ-indexed one. definition psp_of_nat_indexed [constructor] (E : gen_prespectrum +ℕ) : gen_prespectrum +ℤ := gen_prespectrum.mk (λ(n:ℤ), match n with | of_nat k := E k | neg_succ_of_nat k := Ω[succ k] (E 0) end) begin intros n, cases n with n n: esimp, { exact (gen_prespectrum.glue E n) }, cases n with n, { exact (pid _) }, { exact (pid _) } end definition is_spectrum_of_nat_indexed [instance] (E : gen_prespectrum +ℕ) [H : is_spectrum E] : is_spectrum (psp_of_nat_indexed E) := begin apply is_spectrum.mk, intros n, cases n with n n: esimp, { apply is_spectrum.is_equiv_glue }, cases n with n: apply is_equiv_id end protected definition of_nat_indexed (E : gen_prespectrum +ℕ) [H : is_spectrum E] : spectrum := spectrum.mk (psp_of_nat_indexed E) (is_spectrum_of_nat_indexed E) -- In fact, a (pre)spectrum indexed on any pointed successor structure -- gives rise to one indexed on +ℕ, so in this sense +ℤ is a -- "universal" successor structure for indexing spectra. definition succ_str.of_nat {N : succ_str} (z : N) : ℕ → N | succ_str.of_nat zero := z | succ_str.of_nat (succ k) := S (succ_str.of_nat k) definition psp_of_gen_indexed [constructor] {N : succ_str} (z : N) (E : gen_prespectrum N) : prespectrum := psp_of_nat_indexed (gen_prespectrum.mk (λn, E (succ_str.of_nat z n)) (λn, gen_prespectrum.glue E (succ_str.of_nat z n))) definition is_spectrum_of_gen_indexed [instance] {N : succ_str} (z : N) (E : gen_prespectrum N) [H : is_spectrum E] : is_spectrum (psp_of_gen_indexed z E) := begin apply is_spectrum_of_nat_indexed, apply is_spectrum.mk, intros n, esimp, apply is_spectrum.is_equiv_glue end protected definition of_gen_indexed [constructor] {N : succ_str} (z : N) (E : gen_spectrum N) : spectrum := gen_spectrum.mk (psp_of_gen_indexed z E) (is_spectrum_of_gen_indexed z E) -- Generally it's easiest to define a spectrum by giving 'equiv's -- directly. This works for any indexing succ_str. protected definition MK [constructor] {N : succ_str} (deloop : N → Type*) (glue : Π(n:N), (deloop n) ≃* (Ω (deloop (S n)))) : gen_spectrum N := gen_spectrum.mk (gen_prespectrum.mk deloop (λ(n:N), glue n)) (begin apply is_spectrum.mk, intros n, esimp, apply pequiv.to_is_equiv -- Why doesn't typeclass inference find this? end) -- Finally, we combine them and give a way to produce a (ℤ-)spectrum from a ℕ-indexed family of 'equiv's. protected definition Mk [constructor] (deloop : ℕ → Type*) (glue : Π(n:ℕ), (deloop n) ≃* (Ω (deloop (nat.succ n)))) : spectrum := spectrum.of_nat_indexed (spectrum.MK deloop glue) ------------------------------ -- Maps and homotopies of (pre)spectra ------------------------------ -- These make sense for any succ_str. structure smap {N : succ_str} (E F : gen_prespectrum N) := (to_fun : Π(n:N), E n →* F n) (glue_square : Π(n:N), psquare (to_fun n) (Ω→ (to_fun (S n))) (glue E n) (glue F n) ) definition smap_sigma {N : succ_str} (X Y : gen_prespectrum N) : Type := Σ (to_fun : Π(n:N), X n →* Y n), Π(n:N), psquare (to_fun n) (Ω→ (to_fun (S n))) (glue X n) (glue Y n) open smap infix ` →ₛ `:30 := smap attribute smap.to_fun [coercion] definition smap_to_sigma [unfold 4] {N : succ_str} {X Y : gen_prespectrum N} (f : X →ₛ Y) : smap_sigma X Y := begin induction f with f fsq, exact sigma.mk f fsq, end definition smap_to_struc [unfold 4] {N : succ_str} {X Y : gen_prespectrum N} (f : smap_sigma X Y) : X →ₛ Y := begin induction f with f fsq, exact smap.mk f fsq, end definition smap_to_sigma_isretr {N : succ_str} {X Y : gen_prespectrum N} (f : smap_sigma X Y) : smap_to_sigma (smap_to_struc f) = f := begin induction f, reflexivity end definition smap_to_sigma_issec {N : succ_str} {X Y : gen_prespectrum N} (f : X →ₛ Y) : smap_to_struc (smap_to_sigma f) = f := begin induction f, reflexivity end definition smap_sigma_equiv [constructor] {N : succ_str} (X Y : gen_prespectrum N) : (smap_sigma X Y) ≃ (X →ₛ Y) := begin fapply equiv.mk, exact smap_to_struc, fapply adjointify, exact smap_to_sigma, exact smap_to_sigma_issec, exact smap_to_sigma_isretr end -- A version of 'glue_square' in the spectrum case that uses 'equiv_glue' definition sglue_square {N : succ_str} {E F : gen_spectrum N} (f : E →ₛ F) (n : N) : psquare (f n) (Ω→ (f (S n))) (equiv_glue E n) (equiv_glue F n) := glue_square f n definition sid [constructor] [refl] {N : succ_str} (E : gen_prespectrum N) : E →ₛ E := smap.mk (λ n, pid (E n)) (λ n, psquare_of_phtpy_bot (ap1_pid) (psquare_of_pid_top_bot (phomotopy.rfl))) --print sid -- smap.mk (λn, pid (E n)) -- (λn, calc glue E n ∘* pid (E n) ~* glue E n : pcompose_pid -- ... ~* pid (Ω(E (S n))) ∘* glue E n : pid_pcompose -- ... ~* Ω→(pid (E (S n))) ∘* glue E n : pwhisker_right (glue E n) ap1_pid⁻¹*) definition scompose [trans] {N : succ_str} {X Y Z : gen_prespectrum N} (g : Y →ₛ Z) (f : X →ₛ Y) : X →ₛ Z := smap.mk (λn, g n ∘* f n) (λ n, psquare_of_phtpy_bot (ap1_pcompose (g (S n)) (f (S n))) (psquare_hcompose (glue_square f n) (glue_square g n))) /- (λn, calc glue Z n ∘* to_fun g n ∘* to_fun f n ~* (glue Z n ∘* to_fun g n) ∘* to_fun f n : passoc ... ~* (Ω→(to_fun g (S n)) ∘* glue Y n) ∘* to_fun f n : pwhisker_right (to_fun f n) (glue_square g n) ... ~* Ω→(to_fun g (S n)) ∘* (glue Y n ∘* to_fun f n) : passoc ... ~* Ω→(to_fun g (S n)) ∘* (Ω→ (f (S n)) ∘* glue X n) : pwhisker_left (Ω→(to_fun g (S n))) (glue_square f n) ... ~* (Ω→(to_fun g (S n)) ∘* Ω→(f (S n))) ∘* glue X n : passoc ... ~* Ω→(to_fun g (S n) ∘* to_fun f (S n)) ∘* glue X n : pwhisker_right (glue X n) (ap1_pcompose _ _)) -/ infixr ` ∘ₛ `:60 := scompose definition szero [constructor] {N : succ_str} (E F : gen_prespectrum N) : E →ₛ F := smap.mk (λn, pconst (E n) (F n)) (λn, psquare_of_phtpy_bot (ap1_pconst (E (S n)) (F (S n))) (psquare_of_pconst_top_bot (glue E n) (glue F n))) /- (λn, calc glue F n ∘* pconst (E n) (F n) ~* pconst (E n) (Ω(F (S n))) : pcompose_pconst ... ~* pconst (Ω(E (S n))) (Ω(F (S n))) ∘* glue E n : pconst_pcompose ... ~* Ω→(pconst (E (S n)) (F (S n))) ∘* glue E n : pwhisker_right (glue E n) (ap1_pconst _ _)) -/ definition stransport [constructor] {N : succ_str} {A : Type} {a a' : A} (p : a = a') (E : A → gen_prespectrum N) : E a →ₛ E a' := smap.mk (λn, ptransport (λa, E a n) p) begin intro n, induction p, exact !pcompose_pid ⬝* !pid_pcompose⁻¹* ⬝* pwhisker_right _ !ap1_pid⁻¹*, end structure shomotopy {N : succ_str} {E F : gen_prespectrum N} (f g : E →ₛ F) := (to_phomotopy : Πn, f n ~* g n) (glue_homotopy : Πn, ptube_v (to_phomotopy n) (ap1_phomotopy (to_phomotopy (S n))) (glue_square f n) (glue_square g n)) /- (glue_homotopy : Πn, phsquare (pwhisker_left (glue F n) (to_phomotopy n)) (pwhisker_right (glue E n) (ap1_phomotopy (to_phomotopy (S n)))) (glue_square f n) (glue_square g n)) -/ infix ` ~ₛ `:50 := shomotopy definition shomotopy_compose {N : succ_str} {E F : gen_prespectrum N} {f g h : E →ₛ F} (p : g ~ₛ h) (q : f ~ₛ g) : f ~ₛ h := shomotopy.mk (λn, (shomotopy.to_phomotopy q n) ⬝* (shomotopy.to_phomotopy p n)) begin intro n, unfold [ptube_v], rewrite (pwhisker_left_trans _), rewrite ap1_phomotopy_trans, rewrite (pwhisker_right_trans _), exact phhconcat ((shomotopy.glue_homotopy q) n) ((shomotopy.glue_homotopy p) n) end definition shomotopy_inverse {N : succ_str} {E F : gen_prespectrum N} {f g : E →ₛ F} (p : f ~ₛ g) : g ~ₛ f := shomotopy.mk (λn, (shomotopy.to_phomotopy p n)⁻¹*) begin intro n, unfold [ptube_v], rewrite (pwhisker_left_symm _ _), rewrite [-ap1_phomotopy_symm], rewrite (pwhisker_right_symm _ _), exact phhinverse ((shomotopy.glue_homotopy p) n) end /- Comparing the structure of shomotopy with a Σ-type -/ definition shomotopy_sigma {N : succ_str} {X Y : gen_prespectrum N} (f g : X →ₛ Y) : Type := Σ (phtpy : Π (n : N), f n ~* g n), Πn, ptube_v (phtpy n) (ap1_phomotopy (phtpy (S n))) (glue_square f n) (glue_square g n) definition shomotopy_to_sigma [unfold 6] {N : succ_str} {X Y : gen_prespectrum N} {f g : X →ₛ Y} (H : f ~ₛ g) : shomotopy_sigma f g := begin induction H with H Hsq, exact sigma.mk H Hsq, end definition shomotopy_to_struct [unfold 6] {N : succ_str} {X Y : gen_prespectrum N} {f g : X →ₛ Y} (H : shomotopy_sigma f g) : f ~ₛ g := begin induction H with H Hsq, exact shomotopy.mk H Hsq, end definition shomotopy_to_sigma_isretr {N : succ_str} {X Y : gen_prespectrum N} {f g : X →ₛ Y} (H : shomotopy_sigma f g) : shomotopy_to_sigma (shomotopy_to_struct H) = H := begin induction H with H Hsq, reflexivity end definition shomotopy_to_sigma_issec {N : succ_str} {X Y : gen_prespectrum N} {f g : X →ₛ Y} (H : f ~ₛ g) : shomotopy_to_struct (shomotopy_to_sigma H) = H := begin induction H, reflexivity end definition shomotopy_sigma_equiv [constructor] {N : succ_str} {X Y : gen_prespectrum N} (f g : X →ₛ Y) : shomotopy_sigma f g ≃ (f ~ₛ g) := begin fapply equiv.mk, exact shomotopy_to_struct, fapply adjointify, exact shomotopy_to_sigma, exact shomotopy_to_sigma_issec, exact shomotopy_to_sigma_isretr, end /- equivalence of shomotopy and eq -/ /- definition eq_of_shomotopy_pfun {N : succ_str} {X Y : gen_prespectrum N} {f g : X →ₛ Y} (H : f ~ₛ g) (n : N) : f n = g n := begin fapply eq_of_fn_eq_fn (smap_sigma_equiv X Y), repeat exact sorry end-/ definition fam_phomotopy_of_eq {N : Type} {X Y: N → Type*} (f g : Π n, X n →* Y n) : (f = g) ≃ (Π n, f n ~* g n) := (eq.eq_equiv_homotopy) ⬝e pi_equiv_pi_right (λ n, pmap_eq_equiv (f n) (g n)) /- definition phomotopy_rec_on_eq [recursor] {k' : ppi B x₀} {Q : (k ~* k') → Type} (p : k ~* k') (H : Π(q : k = k'), Q (phomotopy_of_eq q)) : Q p := phomotopy_of_eq_of_phomotopy p ▸ H (eq_of_phomotopy p) -/ definition fam_phomotopy_rec_on_eq {N : Type} {X Y : N → Type*} (f g : Π n, X n →* Y n) {Q : (Π n, f n ~* g n) → Type} (p : Π n, f n ~* g n) (H : Π (q : f = g), Q (fam_phomotopy_of_eq f g q)) : Q p := begin refine _ ▸ H ((fam_phomotopy_of_eq f g)⁻¹ᵉ p), have q : to_fun (fam_phomotopy_of_eq f g) (to_fun (fam_phomotopy_of_eq f g)⁻¹ᵉ p) = p, from right_inv (fam_phomotopy_of_eq f g) p, krewrite q end /- definition phomotopy_rec_idp [recursor] {Q : Π {k' : ppi B x₀}, (k ~* k') → Type} (q : Q (phomotopy.refl k)) {k' : ppi B x₀} (H : k ~* k') : Q H := begin induction H using phomotopy_rec_on_eq with t, induction t, exact eq_phomotopy_refl_phomotopy_of_eq_refl k ▸ q, end -/ --set_option pp.coercions true definition fam_phomotopy_rec_idp {N : Type} {X Y : N → Type*} (f : Π n, X n →* Y n) (Q : Π (g : Π n, X n →* Y n) (H : Π n, f n ~* g n), Type) (q : Q f (λ n, phomotopy.rfl)) (g : Π n, X n →* Y n) (H : Π n, f n ~* g n) : Q g H := begin fapply fam_phomotopy_rec_on_eq, refine λ(p : f = g), _, --ugly trick intro p, induction p, exact q, end definition eq_of_shomotopy {N : succ_str} {X Y : gen_prespectrum N} {f g : X →ₛ Y} (H : f ~ₛ g) : f = g := begin fapply eq_of_fn_eq_fn (smap_sigma_equiv X Y)⁻¹ᵉ, induction f with f fsq, induction g with g gsq, induction H with H Hsq, fapply sigma_eq, fapply eq_of_homotopy, intro n, fapply eq_of_phomotopy, exact H n, fapply pi_pathover_constant, intro n, esimp at *, revert g H gsq Hsq n, refine fam_phomotopy_rec_idp f _ _, intro gsq Hsq n, refine change_path _ _, -- have p : eq_of_homotopy (λ n, eq_of_phomotopy phomotopy.rfl) = refl f, reflexivity, refine (eq_of_homotopy_eta rfl)⁻¹ ⬝ _, fapply ap (eq_of_homotopy), fapply eq_of_homotopy, intro n, refine (eq_of_phomotopy_refl _)⁻¹, -- fapply eq_of_phomotopy, fapply pathover_idp_of_eq, note Hsq' := ptube_v_eq_bot phomotopy.rfl (ap1_phomotopy_refl _) (fsq n) (gsq n) (Hsq n), unfold ptube_v at *, unfold phsquare at *, refine _ ⬝ Hsq'⁻¹ ⬝ _, refine (trans_refl (fsq n))⁻¹ ⬝ _, exact idp ◾** (pwhisker_right_refl _ _)⁻¹, refine _ ⬝ (refl_trans (gsq n)), refine _ ◾** idp, exact pwhisker_left_refl _ _, end ------------------------------ -- Equivalences of prespectra ------------------------------ definition spectrum_pequiv_of_pequiv_succ {E F : spectrum} (n : ℤ) (e : E (n + 1) ≃* F (n + 1)) : E n ≃* F n := equiv_glue E n ⬝e* loop_pequiv_loop e ⬝e* (equiv_glue F n)⁻¹ᵉ* definition spectrum_pequiv_of_nat {E F : spectrum} (e : Π(n : ℕ), E n ≃* F n) (n : ℤ) : E n ≃* F n := begin induction n with n n, exact e n, induction n with n IH, { exact spectrum_pequiv_of_pequiv_succ -[1+0] (e 0) }, { exact spectrum_pequiv_of_pequiv_succ -[1+succ n] IH } end definition spectrum_pequiv_of_nat_add {E F : spectrum} (m : ℕ) (e : Π(n : ℕ), E (n + m) ≃* F (n + m)) : Π(n : ℤ), E n ≃* F n := begin apply spectrum_pequiv_of_nat, refine nat.rec_down _ m e _, intro n f k, cases k with k, exact spectrum_pequiv_of_pequiv_succ _ (f 0), exact pequiv_ap E (ap of_nat (succ_add k n)) ⬝e* f k ⬝e* pequiv_ap F (ap of_nat (succ_add k n))⁻¹ end definition is_contr_spectrum_of_nat {E : spectrum} (e : Π(n : ℕ), is_contr (E n)) (n : ℤ) : is_contr (E n) := begin have Πn, is_contr (E (n + 1)) → is_contr (E n), from λn H, @(is_trunc_equiv_closed_rev -2 !equiv_glue) (is_contr_loop_of_is_contr H), induction n with n n, exact e n, induction n with n IH, { exact this -[1+0] (e 0) }, { exact this -[1+succ n] IH } end structure is_sequiv {N : succ_str} {E F : gen_prespectrum N} (f : E →ₛ F) : Type := (to_linv : F →ₛ E) (is_retr : to_linv ∘ₛf ~ₛ sid E) (to_rinv : F →ₛ E) (is_sec : f ∘ₛ to_rinv ~ₛ sid F) structure sequiv {N : succ_str} (E F : gen_prespectrum N) : Type := (to_fun : E →ₛ F) (to_is_sequiv : is_sequiv to_fun) infix ` ≃ₛ ` : 25 := sequiv definition is_sequiv_smap {N : succ_str} {E F : gen_prespectrum N} (f : E →ₛ F) : Type := Π (n: N), is_equiv (f n) definition is_sequiv_of_smap_pequiv {N : succ_str} {E F : gen_prespectrum N} (f : E →ₛ F) (H : is_sequiv_smap f) (n : N) : E n ≃* F n := begin fapply pequiv_of_pmap, exact f n, fapply H, end definition is_sequiv_of_smap_inv {N : succ_str} {E F : gen_prespectrum N} (f : E →ₛ F) (H : is_sequiv_smap f) : F →ₛ E := begin fapply smap.mk, intro n, exact (is_sequiv_of_smap_pequiv f H n)⁻¹ᵉ*, intro n, refine _ ⬝vp* (to_pinv_loopn_pequiv_loopn 1 (is_sequiv_of_smap_pequiv f H (S n)))⁻¹*, fapply phinverse, exact glue_square f n, end local postfix `⁻¹ˢ` : (max + 1) := is_sequiv_of_smap_inv definition is_sequiv_of_smap_isretr {N : succ_str} {E F : gen_prespectrum N} (f : E →ₛ F) (H : is_sequiv_smap f) : is_sequiv_of_smap_inv f H ∘ₛ f ~ₛ sid E := begin fapply shomotopy.mk, intro n, fapply pleft_inv, intro n, refine _ ⬝hp** _, repeat exact sorry, end definition is_sequiv_of_smap_issec {N : succ_str} {E F : gen_prespectrum N} (f : E →ₛ F) (H : is_sequiv_smap f) : f ∘ₛ is_sequiv_of_smap_inv f H ~ₛ sid F := begin repeat exact sorry end definition is_sequiv_of_smap {N : succ_str} {E F : gen_prespectrum N} (f : E →ₛ F) : is_sequiv_smap f → is_sequiv f := begin intro H, fapply is_sequiv.mk, fapply is_sequiv_of_smap_inv f H, fapply is_sequiv_of_smap_isretr f H, fapply is_sequiv_of_smap_inv f H, fapply is_sequiv_of_smap_issec f H, end /--------- Fibers ----------/ definition sfiber [constructor] {N : succ_str} {X Y : gen_spectrum N} (f : X →ₛ Y) : gen_spectrum N := spectrum.MK (λn, pfiber (f n)) (λn, (loop_pfiber (f (S n)))⁻¹ᵉ* ∘*ᵉ pfiber_pequiv_of_square _ _ (sglue_square f n)) /- the map from the fiber to the domain -/ definition spoint {N : succ_str} {X Y : gen_spectrum N} (f : X →ₛ Y) : sfiber f →ₛ X := smap.mk (λn, ppoint (f n)) begin intro n, refine _ ⬝* !passoc, refine _ ⬝* pwhisker_right _ !ppoint_loop_pfiber_inv⁻¹*, rexact (pfiber_pequiv_of_square_ppoint (equiv_glue X n) (equiv_glue Y n) (sglue_square f n))⁻¹* end definition scompose_spoint {N : succ_str} {X Y : gen_spectrum N} (f : X →ₛ Y) : f ∘ₛ spoint f ~ₛ !szero := begin fapply shomotopy.mk, { intro n, exact pcompose_ppoint (f n) }, { intro n, exact sorry } end /--------------------- Homotopy groups ---------------------/ -- Here we start to reap the rewards of using ℤ-indexing: we can -- read off the homotopy groups without any tedious case-analysis of -- n. We increment by 2 in order to ensure that they are all -- automatically abelian groups. definition shomotopy_group (n : ℤ) (E : spectrum) : AbGroup := πag[2] (E (2 - n)) notation `πₛ[`:95 n:0 `]`:0 := shomotopy_group n definition shomotopy_group_fun (n : ℤ) {E F : spectrum} (f : E →ₛ F) : πₛ[n] E →g πₛ[n] F := proof π→g[2] (f (2 - n)) qed definition shomotopy_group_isomorphism_of_pequiv (n : ℤ) {E F : spectrum} (f : Πn, E n ≃* F n) : πₛ[n] E ≃g πₛ[n] F := proof homotopy_group_isomorphism_of_pequiv 1 (f (2 - n)) qed definition shomotopy_group_isomorphism_of_pequiv_nat (n : ℕ) {E F : spectrum} (f : Πn, E n ≃* F n) : πₛ[n] E ≃g πₛ[n] F := shomotopy_group_isomorphism_of_pequiv n (spectrum_pequiv_of_nat f) notation `πₛ→[`:95 n:0 `]`:0 := shomotopy_group_fun n /- properties about homotopy groups -/ definition equiv_glue_neg (X : spectrum) (n : ℤ) : X (2 - succ n) ≃* Ω (X (2 - n)) := have H : succ (2 - succ n) = 2 - n, from ap succ !sub_sub⁻¹ ⬝ sub_add_cancel (2-n) 1, equiv_glue X (2 - succ n) ⬝e* loop_pequiv_loop (pequiv_of_eq (ap X H)) definition π_glue (X : spectrum) (n : ℤ) : π[2] (X (2 - succ n)) ≃* π[3] (X (2 - n)) := homotopy_group_pequiv 2 (equiv_glue_neg X n) definition πg_glue (X : spectrum) (n : ℤ) : πg[2] (X (2 - succ n)) ≃g πg[3] (X (2 - n)) := begin change πg[2] (X (2 - succ n)) ≃g πg[2] (Ω (X (2 - n))), apply homotopy_group_isomorphism_of_pequiv, exact equiv_glue_neg X n end definition πg_glue_homotopy_π_glue (X : spectrum) (n : ℤ) : πg_glue X n ~ π_glue X n := by reflexivity definition π_glue_square {X Y : spectrum} (f : X →ₛ Y) (n : ℤ) : π_glue Y n ∘* π→[2] (f (2 - succ n)) ~* π→[3] (f (2 - n)) ∘* π_glue X n := begin change π→[2] (equiv_glue_neg Y n) ∘* π→[2] (f (2 - succ n)) ~* π→[2] (Ω→ (f (2 - n))) ∘* π→[2] (equiv_glue_neg X n), refine homotopy_group_functor_psquare 2 _, refine !sglue_square ⬝v* ap1_psquare !pequiv_of_eq_commute end definition homotopy_group_spectrum_irrel_one {n m : ℤ} {k : ℕ} (E : spectrum) (p : n + 1 = m + k) [Hk : is_succ k] : πg[k] (E n) ≃g π₁ (E m) := begin induction Hk with k, change π₁ (Ω[k] (E n)) ≃g π₁ (E m), apply homotopy_group_isomorphism_of_pequiv 0, symmetry, have m + k = n, from (pred_succ (m + k))⁻¹ ⬝ ap pred (add.assoc m k 1 ⬝ p⁻¹) ⬝ pred_succ n, induction (succ_str_add_eq_int_add m k ⬝ this), exact equiv_gluen E m k end definition homotopy_group_spectrum_irrel {n m : ℤ} {l k : ℕ} (E : spectrum) (p : n + l = m + k) [Hk : is_succ k] [Hl : is_succ l] : πg[k] (E n) ≃g πg[l] (E m) := proof have Πa b c : ℤ, a + (b + c) = c + (b + a), from λa b c, !add.assoc⁻¹ ⬝ add.comm (a + b) c ⬝ ap (λx, c + x) (add.comm a b), have n + 1 = m + 1 - l + k, from ap succ (add_sub_cancel n l)⁻¹ ⬝ !add.assoc ⬝ ap (λx, x + (-l + 1)) p ⬝ !add.assoc ⬝ ap (λx, m + x) (this k (-l) 1) ⬝ !add.assoc⁻¹ ⬝ !add.assoc⁻¹, homotopy_group_spectrum_irrel_one E this ⬝g (homotopy_group_spectrum_irrel_one E (sub_add_cancel (m+1) l)⁻¹)⁻¹ᵍ qed definition shomotopy_group_isomorphism_homotopy_group {n m : ℤ} {l : ℕ} (E : spectrum) (p : n + m = l) [H : is_succ l] : πₛ[n] E ≃g πg[l] (E m) := have 2 - n + l = m + 2, from ap (λx, 2 - n + x) p⁻¹ ⬝ !add.assoc⁻¹ ⬝ ap (λx, x + m) (sub_add_cancel 2 n) ⬝ add.comm 2 m, homotopy_group_spectrum_irrel E this definition shomotopy_group_pequiv_homotopy_group_ab {n m : ℤ} {l : ℕ} (E : spectrum) (p : n + m = l) [H : is_at_least_two l] : πₛ[n] E ≃g πag[l] (E m) := begin induction H with l, exact shomotopy_group_isomorphism_homotopy_group E p end definition shomotopy_group_pequiv_homotopy_group {n m : ℤ} {l : ℕ} (E : spectrum) (p : n + m = l) : πₛ[n] E ≃* π[l] (E m) := begin cases l with l, { apply ptrunc_pequiv_ptrunc, symmetry, change E m ≃* Ω (Ω (E (2 - n))), refine !equiv_glue ⬝e* loop_pequiv_loop _, refine !equiv_glue ⬝e* loop_pequiv_loop _, apply pequiv_ap E, have -n = m, from neg_eq_of_add_eq_zero p, induction this, rexact add.assoc (-n) 1 1 ⬝ add.comm (-n) 2 }, { exact pequiv_of_isomorphism (shomotopy_group_isomorphism_homotopy_group E p) } end /- the long exact sequence of homotopy groups for spectra -/ section LES open chain_complex prod fin group universe variable u parameters {X Y : spectrum.{u}} (f : X →ₛ Y) definition LES_of_shomotopy_groups : chain_complex +3ℤ := splice (λ(n : ℤ), LES_of_homotopy_groups (f (2 - n))) (2, 0) (π_glue Y) (π_glue X) (π_glue_square f) -- This LES is definitionally what we want: example (n : ℤ) : LES_of_shomotopy_groups (n, 0) = πₛ[n] Y := idp example (n : ℤ) : LES_of_shomotopy_groups (n, 1) = πₛ[n] X := idp example (n : ℤ) : LES_of_shomotopy_groups (n, 2) = πₛ[n] (sfiber f) := idp example (n : ℤ) : cc_to_fn LES_of_shomotopy_groups (n, 0) = πₛ→[n] f := idp example (n : ℤ) : cc_to_fn LES_of_shomotopy_groups (n, 1) = πₛ→[n] (spoint f) := idp -- the maps are ugly for (n, 2) definition ab_group_LES_of_shomotopy_groups : Π(v : +3ℤ), ab_group (LES_of_shomotopy_groups v) | (n, fin.mk 0 H) := proof AbGroup.struct (πₛ[n] Y) qed | (n, fin.mk 1 H) := proof AbGroup.struct (πₛ[n] X) qed | (n, fin.mk 2 H) := proof AbGroup.struct (πₛ[n] (sfiber f)) qed | (n, fin.mk (k+3) H) := begin exfalso, apply lt_le_antisymm H, apply le_add_left end local attribute ab_group_LES_of_shomotopy_groups [instance] definition is_mul_hom_LES_of_shomotopy_groups : Π(v : +3ℤ), is_mul_hom (cc_to_fn LES_of_shomotopy_groups v) | (n, fin.mk 0 H) := proof homomorphism.struct (πₛ→[n] f) qed | (n, fin.mk 1 H) := proof homomorphism.struct (πₛ→[n] (spoint f)) qed | (n, fin.mk 2 H) := proof homomorphism.struct (homomorphism_LES_of_homotopy_groups_fun (f (2 - n)) (1, 2) ∘g πg_glue Y n) qed | (n, fin.mk (k+3) H) := begin exfalso, apply lt_le_antisymm H, apply le_add_left end definition is_exact_LES_of_shomotopy_groups : is_exact LES_of_shomotopy_groups := begin apply is_exact_splice, intro n, apply is_exact_LES_of_homotopy_groups, end -- In the comments below is a start on an explicit description of the LES for spectra -- Maybe it's slightly nicer to work with than the above version definition shomotopy_groups [reducible] : +3ℤ → AbGroup | (n, fin.mk 0 H) := πₛ[n] Y | (n, fin.mk 1 H) := πₛ[n] X | (n, fin.mk k H) := πₛ[n] (sfiber f) definition shomotopy_groups_fun : Π(v : +3ℤ), shomotopy_groups (S v) →g shomotopy_groups v | (n, fin.mk 0 H) := proof πₛ→[n] f qed | (n, fin.mk 1 H) := proof πₛ→[n] (spoint f) qed | (n, fin.mk 2 H) := proof homomorphism_LES_of_homotopy_groups_fun (f (2 - n)) (nat.succ nat.zero, 2) ∘g πg_glue Y n ∘g (by reflexivity) qed | (n, fin.mk (k+3) H) := begin exfalso, apply lt_le_antisymm H, apply le_add_left end --(homomorphism_LES_of_homotopy_groups_fun (f (2 - n)) (1, 2) ∘g πg_glue Y n) end LES /- homotopy group of a prespectrum -/ definition pshomotopy_group_hom (n : ℤ) (E : prespectrum) (k : ℕ) : πag[k + 2] (E (-n + 2 + k)) →g πag[k + 3] (E (-n + 2 + (k + 1))) := begin refine _ ∘g π→g[k+2] (glue E _), refine (ghomotopy_group_succ_in _ (k+1))⁻¹ᵍ ∘g _, refine homotopy_group_isomorphism_of_pequiv (k+1) (loop_pequiv_loop (pequiv_of_eq (ap E (add.assoc (-n + 2) k 1)))) end definition pshomotopy_group (n : ℤ) (E : prespectrum) : AbGroup := group.seq_colim (λ(k : ℕ), πag[k+2] (E (-n + 2 + k))) (pshomotopy_group_hom n E) notation `πₚₛ[`:95 n:0 `]`:0 := pshomotopy_group n definition pshomotopy_group_fun (n : ℤ) {E F : prespectrum} (f : E →ₛ F) : πₚₛ[n] E →g πₚₛ[n] F := proof group.seq_colim_functor (λk, π→g[k+2] (f (-n + 2 +[ℤ] k))) begin intro k, note sq1 := homotopy_group_homomorphism_psquare (k+2) (ptranspose (smap.glue_square f (-n + 2 +[ℤ] k))), note sq2 := homotopy_group_functor_psquare (k+2) (ap1_psquare (ptransport_natural E F f (add.assoc (-n + 2) k 1))), note sq3 := (homotopy_group_succ_in_natural (k+2) (f (-n + 2 +[ℤ] (k+1))))⁻¹ʰᵗʸʰ, note sq4 := hsquare_of_psquare sq2, note rect := sq1 ⬝htyh sq4 ⬝htyh sq3, exact sorry --sq1 ⬝htyh sq4 ⬝htyh sq3, end qed notation `πₚₛ→[`:95 n:0 `]`:0 := pshomotopy_group_fun n /- a chain complex of spectra (not yet used anywhere) -/ structure sp_chain_complex (N : succ_str) : Type := (car : N → spectrum) (fn : Π(n : N), car (S n) →ₛ car n) (is_chain_complex : Πn, fn n ∘ₛ fn (S n) ~ₛ szero _ _) section variables {N : succ_str} (X : sp_chain_complex N) (n : N) definition scc_to_car [unfold 2] [coercion] := @sp_chain_complex.car definition scc_to_fn [unfold 2] : X (S n) →ₛ X n := sp_chain_complex.fn X n definition scc_is_chain_complex [unfold 2] : scc_to_fn X n ∘ₛ scc_to_fn X (S n) ~ₛ szero _ _ := sp_chain_complex.is_chain_complex X n end ------------------------------ -- Suspension prespectra ------------------------------ -- Suspension prespectra are one that's naturally indexed on the natural numbers definition psp_susp (X : Type*) : gen_prespectrum +ℕ := gen_prespectrum.mk (λn, iterate_susp n X) (λn, loop_susp_unit (iterate_susp n X)) -- The sphere prespectrum definition psp_sphere : gen_prespectrum +ℕ := psp_susp bool.pbool /------------------------------- Cotensor of spectra by types -------------------------------/ -- Makes sense for any indexing succ_str. Could be done for -- prespectra too, but as with truncation, why bother? definition sp_cotensor [constructor] {N : succ_str} (A : Type*) (B : gen_spectrum N) : gen_spectrum N := spectrum.MK (λn, ppmap A (B n)) (λn, (loop_ppmap_commute A (B (S n)))⁻¹ᵉ* ∘*ᵉ (pequiv_ppcompose_left (equiv_glue B n))) /- unpointed cotensor -/ definition sp_ucotensor [constructor] {N : succ_str} (A : Type) (B : gen_spectrum N) : gen_spectrum N := spectrum.MK (λn, A →ᵘ* B n) (λn, pumap_pequiv_right A (equiv_glue B n) ⬝e* (loop_pumap A (B (S n)))⁻¹ᵉ*) ---------------------------------------- -- Sections of parametrized spectra ---------------------------------------- definition spi [constructor] {N : succ_str} (A : Type*) (E : A → gen_spectrum N) : gen_spectrum N := spectrum.MK (λn, Π*a, E a n) (λn, !loop_pppi_pequiv⁻¹ᵉ* ∘*ᵉ ppi_pequiv_right (λa, equiv_glue (E a) n)) definition spi_compose_left [constructor] {N : succ_str} {A : Type*} {E F : A -> gen_spectrum N} (f : Πa, E a →ₛ F a) : spi A E →ₛ spi A F := smap.mk (λn, pppi_compose_left (λa, f a n)) begin intro n, exact psquare_pppi_compose_left (λa, (glue_square (f a) n)) ⬝v* !loop_pppi_pequiv_natural⁻¹ᵛ* end -- unpointed spi definition supi [constructor] {N : succ_str} (A : Type) (E : A → gen_spectrum N) : gen_spectrum N := spectrum.MK (λn, Πᵘ*a, E a n) (λn, pupi_pequiv_right (λa, equiv_glue (E a) n) ⬝e* (loop_pupi (λa, E a (S n)))⁻¹ᵉ*) /- Mapping spectra -/ -- note: see also cotensor above /- suspension of a spectrum this is just a shift. We could call a shift in the other direction loopn, though it might be more convenient to just take a negative suspension -/ definition ssusp [constructor] {N : succ_str} (X : gen_spectrum N) : gen_spectrum N := spectrum.MK (λn, X (S n)) (λn, equiv_glue X (S n)) definition ssuspn [constructor] (k : ℤ) (X : spectrum) : spectrum := spectrum.MK (λn, X (n + k)) (λn, equiv_glue X (n + k) ⬝e* loop_pequiv_loop (pequiv_ap X !add.right_comm)) definition shomotopy_group_ssuspn (k : ℤ) (X : spectrum) (n : ℤ) : πₛ[k] (ssuspn n X) ≃g πₛ[k - n] X := have k - n + (2 - k + n) = 2, from !add.comm ⬝ ap (λx, x + (k - n)) (!add.assoc ⬝ ap (λx, 2 + x) (ap (λx, -k + x) !neg_neg⁻¹ ⬝ !neg_add⁻¹)) ⬝ sub_add_cancel 2 (k - n), (shomotopy_group_isomorphism_homotopy_group X this)⁻¹ᵍ /- Tensor by spaces -/ /- Cofibers and stability -/ ------------------------------ -- Contractible spectrum ------------------------------ definition sunit.{u} [constructor] : spectrum.{u} := spectrum.MK (λn, plift punit) (λn, pequiv_of_is_contr _ _ _ _) definition shomotopy_group_sunit.{u} (n : ℤ) : πₛ[n] sunit.{u} ≃g trivial_ab_group_lift.{u} := phomotopy_group_plift_punit 2 definition add_point_spectrum [constructor] {X : Type} (Y : X → spectrum) (x : X₊) : spectrum := spectrum.MK (λn, add_point_over (λx, Y x n) x) begin intro n, induction x with x, apply pequiv_of_is_contr, apply is_trunc_lift, apply is_contr_loop_of_is_contr, apply is_trunc_lift, exact equiv_glue (Y x) n end open option definition shomotopy_group_add_point_spectrum {X : Type} (Y : X → spectrum) (n : ℤ) : Π(x : X₊), πₛ[n] (add_point_spectrum Y x) ≃g add_point_AbGroup (λ (x : X), πₛ[n] (Y x)) x | (some x) := by reflexivity | none := proof phomotopy_group_plift_punit 2 qed /- The Eilenberg-MacLane spectrum -/ definition EM_spectrum /-[constructor]-/ (G : AbGroup) : spectrum := spectrum.Mk (K G) (λn, (loop_EM G n)⁻¹ᵉ*) definition EM_spectrum_pequiv {G H : AbGroup} (e : G ≃g H) (n : ℤ) : EM_spectrum G n ≃* EM_spectrum H n := spectrum_pequiv_of_nat (λk, EM_pequiv_EM k e) n definition EM_spectrum_trivial.{u} (n : ℤ) : EM_spectrum trivial_ab_group_lift.{u} n ≃* trivial_ab_group_lift.{u} := pequiv_of_is_contr _ _ (is_contr_spectrum_of_nat (λk, is_contr_EM k !is_trunc_lift) n) !is_trunc_lift definition is_contr_EM_spectrum_neg (G : AbGroup) (n : ℕ) : is_contr (EM_spectrum G (-[1+n])) := begin induction n with n IH, { apply is_contr_loop, exact is_trunc_EM G 0 }, { apply is_contr_loop_of_is_contr, exact IH } end definition is_contr_EM_spectrum (G : AbGroup) (n : ℤ) (H : is_contr G) : is_contr (EM_spectrum G n) := begin cases n with n n, { apply is_contr_EM n H }, { apply is_contr_EM_spectrum_neg G n } end /- K(πₗ(Aₖ),l) ≃* K(πₙ(A),l) for l = n + k -/ definition EM_type_pequiv_EM (A : spectrum) {n k : ℤ} {l : ℕ} (p : n + k = l) : EM_type (A k) l ≃* EM (πₛ[n] A) l := begin symmetry, cases l with l, { exact shomotopy_group_pequiv_homotopy_group A p }, { cases l with l, { apply EM1_pequiv_EM1, exact shomotopy_group_isomorphism_homotopy_group A p }, { apply EMadd1_pequiv_EMadd1 (l+1), exact shomotopy_group_isomorphism_homotopy_group A p }} end /- Wedge of prespectra -/ open fwedge definition fwedge_prespectrum.{u v} {I : Type.{v}} (X : I -> prespectrum.{u}) : prespectrum.{max u v} := begin fconstructor, { intro n, exact fwedge (λ i, X i n) }, { intro n, fapply fwedge_pmap, intro i, exact Ω→ !pinl ∘* !glue } end end spectrum
ad126ad4f537a6c1851b7d23774c9983449bf685
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Meta/Basic.lean
2ed7c55f2de6d4c053d36dc49dc8383ca8f24e04
[ "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
71,828
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.Data.LOption import Lean.Environment import Lean.Class import Lean.ReducibilityAttrs import Lean.Util.ReplaceExpr import Lean.Util.MonadBacktrack import Lean.Compiler.InlineAttrs import Lean.Meta.TransparencyMode /-! This module provides four (mutually dependent) goodies that are needed for building the elaborator and tactic frameworks. 1- Weak head normal form computation with support for metavariables and transparency modes. 2- Definitionally equality checking with support for metavariables (aka unification modulo definitional equality). 3- Type inference. 4- Type class resolution. They are packed into the MetaM monad. -/ namespace Lean.Meta builtin_initialize isDefEqStuckExceptionId : InternalExceptionId ← registerInternalExceptionId `isDefEqStuck /-- Configuration flags for the `MetaM` monad. Many of them are used to control the `isDefEq` function that checks whether two terms are definitionally equal or not. Recall that when `isDefEq` is trying to check whether `?m@C a₁ ... aₙ` and `t` are definitionally equal (`?m@C a₁ ... aₙ =?= t`), where `?m@C` as a shorthand for `C |- ?m : t` where `t` is the type of `?m`. We solve it using the assignment `?m := fun a₁ ... aₙ => t` if 1) `a₁ ... aₙ` are pairwise distinct free variables that are ​*not*​ let-variables. 2) `a₁ ... aₙ` are not in `C` 3) `t` only contains free variables in `C` and/or `{a₁, ..., aₙ}` 4) For every metavariable `?m'@C'` occurring in `t`, `C'` is a subprefix of `C` 5) `?m` does not occur in `t` -/ structure Config where /-- If `foApprox` is set to true, and some `aᵢ` is not a free variable, then we use first-order unification ``` ?m a_1 ... a_i a_{i+1} ... a_{i+k} =?= f b_1 ... b_k ``` reduces to ``` ?m a_1 ... a_i =?= f a_{i+1} =?= b_1 ... a_{i+k} =?= b_k ``` -/ foApprox : Bool := false /-- When `ctxApprox` is set to true, we relax condition 4, by creating an auxiliary metavariable `?n'` with a smaller context than `?m'`. -/ ctxApprox : Bool := false /-- When `quasiPatternApprox` is set to true, we ignore condition 2. -/ quasiPatternApprox : Bool := false /-- When `constApprox` is set to true, we solve `?m t =?= c` using `?m := fun _ => c` when `?m t` is not a higher-order pattern and `c` is not an application as -/ constApprox : Bool := false /-- When the following flag is set, `isDefEq` throws the exeption `Exeption.isDefEqStuck` whenever it encounters a constraint `?m ... =?= t` where `?m` is read only. This feature is useful for type class resolution where we may want to notify the caller that the TC problem may be solveable later after it assigns `?m`. -/ isDefEqStuckEx : Bool := false /-- Controls which definitions and theorems can be unfolded by `isDefEq` and `whnf`. -/ transparency : TransparencyMode := TransparencyMode.default /-- If zetaNonDep == false, then non dependent let-decls are not zeta expanded. -/ zetaNonDep : Bool := true /-- When `trackZeta == true`, we store zetaFVarIds all free variables that have been zeta-expanded. -/ trackZeta : Bool := false /-- Enable/disable the unification hints feature. -/ unificationHints : Bool := true /-- Enables proof irrelevance at `isDefEq` -/ proofIrrelevance : Bool := true /-- By default synthetic opaque metavariables are not assigned by `isDefEq`. Motivation: we want to make sure typing constraints resolved during elaboration should not "fill" holes that are supposed to be filled using tactics. However, this restriction is too restrictive for tactics such as `exact t`. When elaborating `t`, we dot not fill named holes when solving typing constraints or TC resolution. But, we ignore the restriction when we try to unify the type of `t` with the goal target type. We claim this is not a hack and is defensible behavior because this last unification step is not really part of the term elaboration. -/ assignSyntheticOpaque : Bool := false /-- Enable/Disable support for offset constraints such as `?x + 1 =?= e` -/ offsetCnstrs : Bool := true /-- Eta for structures configuration mode. -/ etaStruct : EtaStructMode := .all /-- Function parameter information cache. -/ structure ParamInfo where /-- The binder annotation for the parameter. -/ binderInfo : BinderInfo := BinderInfo.default /-- `hasFwdDeps` is true if there is another parameter whose type depends on this one. -/ hasFwdDeps : Bool := false /-- `backDeps` contains the backwards dependencies. That is, the (0-indexed) position of previous parameters that this one depends on. -/ backDeps : Array Nat := #[] /-- `isProp` is true if the parameter is always a proposition. -/ isProp : Bool := false /-- `isDecInst` is true if the parameter's type is of the form `Decidable ...`. This information affects the generation of congruence theorems. -/ isDecInst : Bool := false /-- `higherOrderOutParam` is true if this parameter is a higher-order output parameter of local instance. Example: ``` getElem : {cont : Type u_1} → {idx : Type u_2} → {elem : Type u_3} → {dom : cont → idx → Prop} → [self : GetElem cont idx elem dom] → (xs : cont) → (i : idx) → dom xs i → elem ``` This flag is true for the parameter `dom` because it is output parameter of `[self : GetElem cont idx elem dom]` -/ higherOrderOutParam : Bool := false /-- `dependsOnHigherOrderOutParam` is true if the type of this parameter depends on the higher-order output parameter of a previous local instance. Example: ``` getElem : {cont : Type u_1} → {idx : Type u_2} → {elem : Type u_3} → {dom : cont → idx → Prop} → [self : GetElem cont idx elem dom] → (xs : cont) → (i : idx) → dom xs i → elem ``` This flag is true for the parameter with type `dom xs i` since `dom` is an output parameter of the instance `[self : GetElem cont idx elem dom]` -/ dependsOnHigherOrderOutParam : Bool := false deriving Inhabited def ParamInfo.isImplicit (p : ParamInfo) : Bool := p.binderInfo == BinderInfo.implicit def ParamInfo.isInstImplicit (p : ParamInfo) : Bool := p.binderInfo == BinderInfo.instImplicit def ParamInfo.isStrictImplicit (p : ParamInfo) : Bool := p.binderInfo == BinderInfo.strictImplicit def ParamInfo.isExplicit (p : ParamInfo) : Bool := p.binderInfo == BinderInfo.default /-- Function information cache. See `ParamInfo`. -/ structure FunInfo where /-- Parameter information cache. -/ paramInfo : Array ParamInfo := #[] /-- `resultDeps` contains the function result type backwards dependencies. That is, the (0-indexed) position of parameters that the result type depends on. -/ resultDeps : Array Nat := #[] /-- Key for the function information cache. -/ structure InfoCacheKey where /-- The transparency mode used to compute the `FunInfo`. -/ transparency : TransparencyMode /-- The function being cached information about. It is quite often an `Expr.const`. -/ expr : Expr /-- `nargs? = some n` if the cached information was computed assuming the function has arity `n`. If `nargs? = none`, then the cache information consumed the arrow type as much as possible unsing the current transparency setting. X-/ nargs? : Option Nat deriving Inhabited, BEq namespace InfoCacheKey instance : Hashable InfoCacheKey := ⟨fun ⟨transparency, expr, nargs⟩ => mixHash (hash transparency) <| mixHash (hash expr) (hash nargs)⟩ end InfoCacheKey abbrev SynthInstanceCache := PersistentHashMap (LocalInstances × Expr) (Option Expr) abbrev InferTypeCache := PersistentExprStructMap Expr abbrev FunInfoCache := PersistentHashMap InfoCacheKey FunInfo abbrev WhnfCache := PersistentExprStructMap Expr /-- A mapping `(s, t) ↦ isDefEq s t`. TODO: consider more efficient representations (e.g., a proper set) and caching policies (e.g., imperfect cache). We should also investigate the impact on memory consumption. -/ abbrev DefEqCache := PersistentHashMap (Expr × Expr) Bool /-- Cache datastructures for type inference, type class resolution, whnf, and definitional equality. -/ structure Cache where inferType : InferTypeCache := {} funInfo : FunInfoCache := {} synthInstance : SynthInstanceCache := {} whnfDefault : WhnfCache := {} -- cache for closed terms and `TransparencyMode.default` whnfAll : WhnfCache := {} -- cache for closed terms and `TransparencyMode.all` defEq : DefEqCache := {} deriving Inhabited /-- "Context" for a postponed universe constraint. `lhs` and `rhs` are the surrounding `isDefEq` call when the postponed constraint was created. -/ structure DefEqContext where lhs : Expr rhs : Expr lctx : LocalContext localInstances : LocalInstances /-- Auxiliary structure for representing postponed universe constraints. Remark: the fields `ref` and `rootDefEq?` are used for error message generation only. Remark: we may consider improving the error message generation in the future. -/ structure PostponedEntry where /-- We save the `ref` at entry creation time. This is used for reporting errors back to the user. -/ ref : Syntax lhs : Level rhs : Level /-- Context for the surrounding `isDefEq` call when entry was created. -/ ctx? : Option DefEqContext deriving Inhabited /-- `MetaM` monad state. -/ structure State where mctx : MetavarContext := {} cache : Cache := {} /-- When `trackZeta == true`, then any let-decl free variable that is zeta expansion performed by `MetaM` is stored in `zetaFVarIds`. -/ zetaFVarIds : FVarIdSet := {} /-- Array of postponed universe level constraints -/ postponed : PersistentArray PostponedEntry := {} deriving Inhabited /-- Backtrackable state for the `MetaM` monad. -/ structure SavedState where core : Core.State meta : State deriving Nonempty /-- Contextual information for the `MetaM` monad. -/ structure Context where config : Config := {} /-- Local context -/ lctx : LocalContext := {} /-- Local instances in `lctx`. -/ localInstances : LocalInstances := #[] /-- Not `none` when inside of an `isDefEq` test. See `PostponedEntry`. -/ defEqCtx? : Option DefEqContext := none /-- Track the number of nested `synthPending` invocations. Nested invocations can happen when the type class resolution invokes `synthPending`. Remark: in the current implementation, `synthPending` fails if `synthPendingDepth > 0`. We will add a configuration option if necessary. -/ synthPendingDepth : Nat := 0 /-- A predicate to control whether a constant can be unfolded or not at `whnf`. Note that we do not cache results at `whnf` when `canUnfold?` is not `none`. -/ canUnfold? : Option (Config → ConstantInfo → CoreM Bool) := none abbrev MetaM := ReaderT Context $ StateRefT State CoreM -- Make the compiler generate specialized `pure`/`bind` so we do not have to optimize through the -- whole monad stack at every use site. May eventually be covered by `deriving`. @[always_inline] instance : Monad MetaM := let i := inferInstanceAs (Monad MetaM); { pure := i.pure, bind := i.bind } instance : Inhabited (MetaM α) where default := fun _ _ => default instance : MonadLCtx MetaM where getLCtx := return (← read).lctx instance : MonadMCtx MetaM where getMCtx := return (← get).mctx modifyMCtx f := modify fun s => { s with mctx := f s.mctx } instance : MonadEnv MetaM where getEnv := return (← getThe Core.State).env modifyEnv f := do modifyThe Core.State fun s => { s with env := f s.env, cache := {} }; modify fun s => { s with cache := {} } instance : AddMessageContext MetaM where addMessageContext := addMessageContextFull protected def saveState : MetaM SavedState := return { core := (← getThe Core.State), meta := (← get) } /-- Restore backtrackable parts of the state. -/ def SavedState.restore (b : SavedState) : MetaM Unit := do Core.restore b.core modify fun s => { s with mctx := b.meta.mctx, zetaFVarIds := b.meta.zetaFVarIds, postponed := b.meta.postponed } instance : MonadBacktrack SavedState MetaM where saveState := Meta.saveState restoreState s := s.restore @[inline] def MetaM.run (x : MetaM α) (ctx : Context := {}) (s : State := {}) : CoreM (α × State) := x ctx |>.run s @[inline] def MetaM.run' (x : MetaM α) (ctx : Context := {}) (s : State := {}) : CoreM α := Prod.fst <$> x.run ctx s @[inline] def MetaM.toIO (x : MetaM α) (ctxCore : Core.Context) (sCore : Core.State) (ctx : Context := {}) (s : State := {}) : IO (α × Core.State × State) := do let ((a, s), sCore) ← (x.run ctx s).toIO ctxCore sCore pure (a, sCore, s) instance [MetaEval α] : MetaEval (MetaM α) := ⟨fun env opts x _ => MetaEval.eval env opts x.run' true⟩ protected def throwIsDefEqStuck : MetaM α := throw <| Exception.internal isDefEqStuckExceptionId builtin_initialize registerTraceClass `Meta registerTraceClass `Meta.debug export Core (instantiateTypeLevelParams instantiateValueLevelParams) @[inline] def liftMetaM [MonadLiftT MetaM m] (x : MetaM α) : m α := liftM x @[inline] def mapMetaM [MonadControlT MetaM m] [Monad m] (f : forall {α}, MetaM α → MetaM α) {α} (x : m α) : m α := controlAt MetaM fun runInBase => f <| runInBase x @[inline] def map1MetaM [MonadControlT MetaM m] [Monad m] (f : forall {α}, (β → MetaM α) → MetaM α) {α} (k : β → m α) : m α := controlAt MetaM fun runInBase => f fun b => runInBase <| k b @[inline] def map2MetaM [MonadControlT MetaM m] [Monad m] (f : forall {α}, (β → γ → MetaM α) → MetaM α) {α} (k : β → γ → m α) : m α := controlAt MetaM fun runInBase => f fun b c => runInBase <| k b c section Methods variable [MonadControlT MetaM n] [Monad n] @[inline] def modifyCache (f : Cache → Cache) : MetaM Unit := modify fun ⟨mctx, cache, zetaFVarIds, postponed⟩ => ⟨mctx, f cache, zetaFVarIds, postponed⟩ @[inline] def modifyInferTypeCache (f : InferTypeCache → InferTypeCache) : MetaM Unit := modifyCache fun ⟨ic, c1, c2, c3, c4, c5⟩ => ⟨f ic, c1, c2, c3, c4, c5⟩ @[inline] def modifyDefEqCache (f : DefEqCache → DefEqCache) : MetaM Unit := modifyCache fun ⟨c1, c2, c3, c4, c5, defeq⟩ => ⟨c1, c2, c3, c4, c5, f defeq⟩ def getLocalInstances : MetaM LocalInstances := return (← read).localInstances def getConfig : MetaM Config := return (← read).config def resetZetaFVarIds : MetaM Unit := modify fun s => { s with zetaFVarIds := {} } def getZetaFVarIds : MetaM FVarIdSet := return (← get).zetaFVarIds /-- Return the array of postponed universe level constraints. -/ def getPostponed : MetaM (PersistentArray PostponedEntry) := return (← get).postponed /-- Set the array of postponed universe level constraints. -/ def setPostponed (postponed : PersistentArray PostponedEntry) : MetaM Unit := modify fun s => { s with postponed := postponed } /-- Modify the array of postponed universe level constraints. -/ @[inline] def modifyPostponed (f : PersistentArray PostponedEntry → PersistentArray PostponedEntry) : MetaM Unit := modify fun s => { s with postponed := f s.postponed } /-- `useEtaStruct inductName` return `true` if we eta for structures is enabled for for the inductive datatype `inductName`. Recall we have three different settings: `.none` (never use it), `.all` (always use it), `.notClasses` (enabled only for structure-like inductive types that are not classes). The parameter `inductName` affects the result only if the current setting is `.notClasses`. -/ def useEtaStruct (inductName : Name) : MetaM Bool := do match (← getConfig).etaStruct with | .none => return false | .all => return true | .notClasses => return !isClass (← getEnv) inductName /-! WARNING: The following 4 constants are a hack for simulating forward declarations. They are defined later using the `export` attribute. This is hackish because we have to hard-code the true arity of these definitions here, and make sure the C names match. We have used another hack based on `IO.Ref`s in the past, it was safer but less efficient. -/ /-- Reduces an expression to its Weak Head Normal Form. This is when the topmost expression has been fully reduced, but may contain subexpressions which have not been reduced. -/ @[extern 6 "lean_whnf"] opaque whnf : Expr → MetaM Expr /-- Returns the inferred type of the given expression, or fails if it is not type-correct. -/ @[extern 6 "lean_infer_type"] opaque inferType : Expr → MetaM Expr @[extern 7 "lean_is_expr_def_eq"] opaque isExprDefEqAux : Expr → Expr → MetaM Bool @[extern 7 "lean_is_level_def_eq"] opaque isLevelDefEqAux : Level → Level → MetaM Bool @[extern 6 "lean_synth_pending"] protected opaque synthPending : MVarId → MetaM Bool def whnfForall (e : Expr) : MetaM Expr := do let e' ← whnf e if e'.isForall then pure e' else pure e -- withIncRecDepth for a monad `n` such that `[MonadControlT MetaM n]` protected def withIncRecDepth (x : n α) : n α := mapMetaM (withIncRecDepth (m := MetaM)) x private def mkFreshExprMVarAtCore (mvarId : MVarId) (lctx : LocalContext) (localInsts : LocalInstances) (type : Expr) (kind : MetavarKind) (userName : Name) (numScopeArgs : Nat) : MetaM Expr := do modifyMCtx fun mctx => mctx.addExprMVarDecl mvarId userName lctx localInsts type kind numScopeArgs; return mkMVar mvarId def mkFreshExprMVarAt (lctx : LocalContext) (localInsts : LocalInstances) (type : Expr) (kind : MetavarKind := MetavarKind.natural) (userName : Name := Name.anonymous) (numScopeArgs : Nat := 0) : MetaM Expr := do mkFreshExprMVarAtCore (← mkFreshMVarId) lctx localInsts type kind userName numScopeArgs def mkFreshLevelMVar : MetaM Level := do let mvarId ← mkFreshLMVarId modifyMCtx fun mctx => mctx.addLevelMVarDecl mvarId; return mkLevelMVar mvarId private def mkFreshExprMVarCore (type : Expr) (kind : MetavarKind) (userName : Name) : MetaM Expr := do mkFreshExprMVarAt (← getLCtx) (← getLocalInstances) type kind userName private def mkFreshExprMVarImpl (type? : Option Expr) (kind : MetavarKind) (userName : Name) : MetaM Expr := match type? with | some type => mkFreshExprMVarCore type kind userName | none => do let u ← mkFreshLevelMVar let type ← mkFreshExprMVarCore (mkSort u) MetavarKind.natural Name.anonymous mkFreshExprMVarCore type kind userName def mkFreshExprMVar (type? : Option Expr) (kind := MetavarKind.natural) (userName := Name.anonymous) : MetaM Expr := mkFreshExprMVarImpl type? kind userName def mkFreshTypeMVar (kind := MetavarKind.natural) (userName := Name.anonymous) : MetaM Expr := do let u ← mkFreshLevelMVar mkFreshExprMVar (mkSort u) kind userName /-- Low-level version of `MkFreshExprMVar` which allows users to create/reserve a `mvarId` using `mkFreshId`, and then later create the metavar using this method. -/ private def mkFreshExprMVarWithIdCore (mvarId : MVarId) (type : Expr) (kind : MetavarKind := MetavarKind.natural) (userName : Name := Name.anonymous) (numScopeArgs : Nat := 0) : MetaM Expr := do mkFreshExprMVarAtCore mvarId (← getLCtx) (← getLocalInstances) type kind userName numScopeArgs def mkFreshExprMVarWithId (mvarId : MVarId) (type? : Option Expr := none) (kind : MetavarKind := MetavarKind.natural) (userName := Name.anonymous) : MetaM Expr := match type? with | some type => mkFreshExprMVarWithIdCore mvarId type kind userName | none => do let u ← mkFreshLevelMVar let type ← mkFreshExprMVar (mkSort u) mkFreshExprMVarWithIdCore mvarId type kind userName def mkFreshLevelMVars (num : Nat) : MetaM (List Level) := num.foldM (init := []) fun _ us => return (← mkFreshLevelMVar)::us def mkFreshLevelMVarsFor (info : ConstantInfo) : MetaM (List Level) := mkFreshLevelMVars info.numLevelParams /-- Create a constant with the given name and new universe metavariables. Example: ``mkConstWithFreshMVarLevels `Monad`` returns `@Monad.{?u, ?v}` -/ def mkConstWithFreshMVarLevels (declName : Name) : MetaM Expr := do let info ← getConstInfo declName return mkConst declName (← mkFreshLevelMVarsFor info) /-- Return current transparency setting/mode. -/ def getTransparency : MetaM TransparencyMode := return (← getConfig).transparency def shouldReduceAll : MetaM Bool := return (← getTransparency) == TransparencyMode.all def shouldReduceReducibleOnly : MetaM Bool := return (← getTransparency) == TransparencyMode.reducible /-- Return `some mvarDecl` where `mvarDecl` is `mvarId` declaration in the current metavariable context. Return `none` if `mvarId` has no declaration in the current metavariable context. -/ def _root_.Lean.MVarId.findDecl? (mvarId : MVarId) : MetaM (Option MetavarDecl) := return (← getMCtx).findDecl? mvarId @[deprecated MVarId.findDecl?] def findMVarDecl? (mvarId : MVarId) : MetaM (Option MetavarDecl) := mvarId.findDecl? /-- Return `mvarId` declaration in the current metavariable context. Throw an exception if `mvarId` is not declarated in the current metavariable context. -/ def _root_.Lean.MVarId.getDecl (mvarId : MVarId) : MetaM MetavarDecl := do match (← mvarId.findDecl?) with | some d => pure d | none => throwError "unknown metavariable '?{mvarId.name}'" @[deprecated MVarId.getDecl] def getMVarDecl (mvarId : MVarId) : MetaM MetavarDecl := do mvarId.getDecl /-- Return `mvarId` kind. Throw an exception if `mvarId` is not declarated in the current metavariable context. -/ def _root_.Lean.MVarId.getKind (mvarId : MVarId) : MetaM MetavarKind := return (← mvarId.getDecl).kind @[deprecated MVarId.getKind] def getMVarDeclKind (mvarId : MVarId) : MetaM MetavarKind := mvarId.getKind /-- Reture `true` if `e` is a synthetic (or synthetic opaque) metavariable -/ def isSyntheticMVar (e : Expr) : MetaM Bool := do if e.isMVar then return (← e.mvarId!.getKind) matches .synthetic | .syntheticOpaque else return false /-- Set `mvarId` kind in the current metavariable context. -/ def _root_.Lean.MVarId.setKind (mvarId : MVarId) (kind : MetavarKind) : MetaM Unit := modifyMCtx fun mctx => mctx.setMVarKind mvarId kind @[deprecated MVarId.setKind] def setMVarKind (mvarId : MVarId) (kind : MetavarKind) : MetaM Unit := mvarId.setKind kind /-- Update the type of the given metavariable. This function assumes the new type is definitionally equal to the current one -/ def _root_.Lean.MVarId.setType (mvarId : MVarId) (type : Expr) : MetaM Unit := do modifyMCtx fun mctx => mctx.setMVarType mvarId type @[deprecated MVarId.setType] def setMVarType (mvarId : MVarId) (type : Expr) : MetaM Unit := do mvarId.setType type /-- Return true if the given metavariable is "read-only". That is, its `depth` is different from the current metavariable context depth. -/ def _root_.Lean.MVarId.isReadOnly (mvarId : MVarId) : MetaM Bool := do return (← mvarId.getDecl).depth != (← getMCtx).depth @[deprecated MVarId.isReadOnly] def isReadOnlyExprMVar (mvarId : MVarId) : MetaM Bool := do mvarId.isReadOnly /-- Return true if `mvarId.isReadOnly` return true or if `mvarId` is a synthetic opaque metavariable. Recall `isDefEq` will not assign a value to `mvarId` if `mvarId.isReadOnlyOrSyntheticOpaque`. -/ def _root_.Lean.MVarId.isReadOnlyOrSyntheticOpaque (mvarId : MVarId) : MetaM Bool := do let mvarDecl ← mvarId.getDecl match mvarDecl.kind with | MetavarKind.syntheticOpaque => return !(← getConfig).assignSyntheticOpaque | _ => return mvarDecl.depth != (← getMCtx).depth @[deprecated MVarId.isReadOnlyOrSyntheticOpaque] def isReadOnlyOrSyntheticOpaqueExprMVar (mvarId : MVarId) : MetaM Bool := do mvarId.isReadOnlyOrSyntheticOpaque /-- Return the level of the given universe level metavariable. -/ def _root_.Lean.LMVarId.getLevel (mvarId : LMVarId) : MetaM Nat := do match (← getMCtx).findLevelDepth? mvarId with | some depth => return depth | _ => throwError "unknown universe metavariable '?{mvarId.name}'" @[deprecated LMVarId.getLevel] def getLevelMVarDepth (mvarId : LMVarId) : MetaM Nat := mvarId.getLevel /-- Return true if the given universe metavariable is "read-only". That is, its `depth` is different from the current metavariable context depth. -/ def _root_.Lean.LMVarId.isReadOnly (mvarId : LMVarId) : MetaM Bool := return (← mvarId.getLevel) < (← getMCtx).levelAssignDepth @[deprecated LMVarId.isReadOnly] def isReadOnlyLevelMVar (mvarId : LMVarId) : MetaM Bool := do mvarId.isReadOnly /-- Set the user-facing name for the given metavariable. -/ def _root_.Lean.MVarId.setUserName (mvarId : MVarId) (newUserName : Name) : MetaM Unit := modifyMCtx fun mctx => mctx.setMVarUserName mvarId newUserName @[deprecated MVarId.setUserName] def setMVarUserName (mvarId : MVarId) (userNameNew : Name) : MetaM Unit := mvarId.setUserName userNameNew /-- Throw an exception saying `fvarId` is not declared in the current local context. -/ def _root_.Lean.FVarId.throwUnknown (fvarId : FVarId) : CoreM α := throwError "unknown free variable '{mkFVar fvarId}'" @[deprecated FVarId.throwUnknown] def throwUnknownFVar (fvarId : FVarId) : MetaM α := fvarId.throwUnknown /-- Return `some decl` if `fvarId` is declared in the current local context. -/ def _root_.Lean.FVarId.findDecl? (fvarId : FVarId) : MetaM (Option LocalDecl) := return (← getLCtx).find? fvarId @[deprecated FVarId.findDecl?] def findLocalDecl? (fvarId : FVarId) : MetaM (Option LocalDecl) := fvarId.findDecl? /-- Return the local declaration for the given free variable. Throw an exception if local declaration is not in the current local context. -/ def _root_.Lean.FVarId.getDecl (fvarId : FVarId) : MetaM LocalDecl := do match (← getLCtx).find? fvarId with | some d => return d | none => fvarId.throwUnknown @[deprecated FVarId.getDecl] def getLocalDecl (fvarId : FVarId) : MetaM LocalDecl := do fvarId.getDecl /-- Return the type of the given free variable. -/ def _root_.Lean.FVarId.getType (fvarId : FVarId) : MetaM Expr := return (← fvarId.getDecl).type /-- Return the binder information for the given free variable. -/ def _root_.Lean.FVarId.getBinderInfo (fvarId : FVarId) : MetaM BinderInfo := return (← fvarId.getDecl).binderInfo /-- Return `some value` if the given free variable is a let-declaration, and `none` otherwise. -/ def _root_.Lean.FVarId.getValue? (fvarId : FVarId) : MetaM (Option Expr) := return (← fvarId.getDecl).value? /-- Return the user-facing name for the given free variable. -/ def _root_.Lean.FVarId.getUserName (fvarId : FVarId) : MetaM Name := return (← fvarId.getDecl).userName /-- Return `true` is the free variable is a let-variable. -/ def _root_.Lean.FVarId.isLetVar (fvarId : FVarId) : MetaM Bool := return (← fvarId.getDecl).isLet /-- Get the local declaration associated to the given `Expr` in the current local context. Fails if the given expression is not a fvar or if no such declaration exists. -/ def getFVarLocalDecl (fvar : Expr) : MetaM LocalDecl := fvar.fvarId!.getDecl /-- Given a user-facing name for a free variable, return its declaration in the current local context. Throw an exception if free variable is not declared. -/ def getLocalDeclFromUserName (userName : Name) : MetaM LocalDecl := do match (← getLCtx).findFromUserName? userName with | some d => pure d | none => throwError "unknown local declaration '{userName}'" /-- Given a user-facing name for a free variable, return the free variable or throw if not declared. -/ def getFVarFromUserName (userName : Name) : MetaM Expr := do let d ← getLocalDeclFromUserName userName return Expr.fvar d.fvarId /-- Lift a `MkBindingM` monadic action `x` to `MetaM`. -/ @[inline] def liftMkBindingM (x : MetavarContext.MkBindingM α) : MetaM α := do match x { lctx := (← getLCtx), mainModule := (← getEnv).mainModule } { mctx := (← getMCtx), ngen := (← getNGen), nextMacroScope := (← getThe Core.State).nextMacroScope } with | .ok e sNew => do setMCtx sNew.mctx modifyThe Core.State fun s => { s with ngen := sNew.ngen, nextMacroScope := sNew.nextMacroScope } pure e | .error (.revertFailure ..) sNew => do setMCtx sNew.mctx modifyThe Core.State fun s => { s with ngen := sNew.ngen, nextMacroScope := sNew.nextMacroScope } throwError "failed to create binder due to failure when reverting variable dependencies" /-- Similar to `abstracM` but consider only the first `min n xs.size` entries in `xs` It is also similar to `Expr.abstractRange`, but handles metavariables correctly. It uses `elimMVarDeps` to ensure `e` and the type of the free variables `xs` do not contain a metavariable `?m` s.t. local context of `?m` contains a free variable in `xs`. -/ def _root_.Lean.Expr.abstractRangeM (e : Expr) (n : Nat) (xs : Array Expr) : MetaM Expr := liftMkBindingM <| MetavarContext.abstractRange e n xs @[deprecated Expr.abstractRangeM] def abstractRange (e : Expr) (n : Nat) (xs : Array Expr) : MetaM Expr := e.abstractRangeM n xs /-- Replace free (or meta) variables `xs` with loose bound variables. Similar to `Expr.abstract`, but handles metavariables correctly. -/ def _root_.Lean.Expr.abstractM (e : Expr) (xs : Array Expr) : MetaM Expr := e.abstractRangeM xs.size xs @[deprecated Expr.abstractM] def abstract (e : Expr) (xs : Array Expr) : MetaM Expr := e.abstractM xs /-- Collect forward dependencies for the free variables in `toRevert`. Recall that when reverting free variables `xs`, we must also revert their forward dependencies. -/ def collectForwardDeps (toRevert : Array Expr) (preserveOrder : Bool) : MetaM (Array Expr) := do liftMkBindingM <| MetavarContext.collectForwardDeps toRevert preserveOrder /-- Takes an array `xs` of free variables or metavariables and a term `e` that may contain those variables, and abstracts and binds them as universal quantifiers. - if `usedOnly = true` then only variables that the expression body depends on will appear. - if `usedLetOnly = true` same as `usedOnly` except for let-bound variables. (That is, local constants which have been assigned a value.) -/ def mkForallFVars (xs : Array Expr) (e : Expr) (usedOnly : Bool := false) (usedLetOnly : Bool := true) (binderInfoForMVars := BinderInfo.implicit) : MetaM Expr := if xs.isEmpty then return e else liftMkBindingM <| MetavarContext.mkForall xs e usedOnly usedLetOnly binderInfoForMVars /-- Takes an array `xs` of free variables and metavariables and a body term `e` and creates `fun ..xs => e`, suitably abstracting `e` and the types in `xs`. -/ def mkLambdaFVars (xs : Array Expr) (e : Expr) (usedOnly : Bool := false) (usedLetOnly : Bool := true) (binderInfoForMVars := BinderInfo.implicit) : MetaM Expr := if xs.isEmpty then return e else liftMkBindingM <| MetavarContext.mkLambda xs e usedOnly usedLetOnly binderInfoForMVars def mkLetFVars (xs : Array Expr) (e : Expr) (usedLetOnly := true) (binderInfoForMVars := BinderInfo.implicit) : MetaM Expr := mkLambdaFVars xs e (usedLetOnly := usedLetOnly) (binderInfoForMVars := binderInfoForMVars) /-- `fun _ : Unit => a` -/ def mkFunUnit (a : Expr) : MetaM Expr := return Lean.mkLambda (← mkFreshUserName `x) BinderInfo.default (mkConst ``Unit) a def elimMVarDeps (xs : Array Expr) (e : Expr) (preserveOrder : Bool := false) : MetaM Expr := if xs.isEmpty then pure e else liftMkBindingM <| MetavarContext.elimMVarDeps xs e preserveOrder /-- `withConfig f x` executes `x` using the updated configuration object obtained by applying `f`. -/ @[inline] def withConfig (f : Config → Config) : n α → n α := mapMetaM <| withReader (fun ctx => { ctx with config := f ctx.config }) @[inline] def withTrackingZeta (x : n α) : n α := withConfig (fun cfg => { cfg with trackZeta := true }) x @[inline] def withoutProofIrrelevance (x : n α) : n α := withConfig (fun cfg => { cfg with proofIrrelevance := false }) x @[inline] def withTransparency (mode : TransparencyMode) : n α → n α := mapMetaM <| withConfig (fun config => { config with transparency := mode }) /-- `withDefault x` excutes `x` using the default transparency setting. -/ @[inline] def withDefault (x : n α) : n α := withTransparency TransparencyMode.default x /-- `withReducible x` excutes `x` using the reducible transparency setting. In this setting only definitions tagged as `[reducible]` are unfolded. -/ @[inline] def withReducible (x : n α) : n α := withTransparency TransparencyMode.reducible x /-- `withReducibleAndInstances x` excutes `x` using the `.instances` transparency setting. In this setting only definitions tagged as `[reducible]` or type class instances are unfolded. -/ @[inline] def withReducibleAndInstances (x : n α) : n α := withTransparency TransparencyMode.instances x /-- Execute `x` ensuring the transparency setting is at least `mode`. Recall that `.all > .default > .instances > .reducible`. -/ @[inline] def withAtLeastTransparency (mode : TransparencyMode) (x : n α) : n α := withConfig (fun config => let oldMode := config.transparency let mode := if oldMode.lt mode then mode else oldMode { config with transparency := mode }) x /-- Execute `x` allowing `isDefEq` to assign synthetic opaque metavariables. -/ @[inline] def withAssignableSyntheticOpaque (x : n α) : n α := withConfig (fun config => { config with assignSyntheticOpaque := true }) x /-- Save cache, execute `x`, restore cache -/ @[inline] private def savingCacheImpl (x : MetaM α) : MetaM α := do let savedCache := (← get).cache try x finally modify fun s => { s with cache := savedCache } @[inline] def savingCache : n α → n α := mapMetaM savingCacheImpl def getTheoremInfo (info : ConstantInfo) : MetaM (Option ConstantInfo) := do if (← shouldReduceAll) then return some info else return none private def getDefInfoTemp (info : ConstantInfo) : MetaM (Option ConstantInfo) := do match (← getTransparency) with | TransparencyMode.all => return some info | TransparencyMode.default => return some info | _ => if (← isReducible info.name) then return some info else return none /-- Remark: we later define `getUnfoldableConst?` at `GetConst.lean` after we define `Instances.lean`. This method is only used to implement `isClassQuickConst?`. It is very similar to `getUnfoldableConst?`, but it returns none when `TransparencyMode.instances` and `constName` is an instance. This difference should be irrelevant for `isClassQuickConst?`. -/ private def getConstTemp? (constName : Name) : MetaM (Option ConstantInfo) := do match (← getEnv).find? constName with | some (info@(ConstantInfo.thmInfo _)) => getTheoremInfo info | some (info@(ConstantInfo.defnInfo _)) => getDefInfoTemp info | some info => pure (some info) | none => throwUnknownConstant constName private def isClassQuickConst? (constName : Name) : MetaM (LOption Name) := do if isClass (← getEnv) constName then return .some constName else match (← getConstTemp? constName) with | some (.defnInfo ..) => return .undef -- We may be able to unfold the definition | _ => return .none private partial def isClassQuick? : Expr → MetaM (LOption Name) | .bvar .. => return .none | .lit .. => return .none | .fvar .. => return .none | .sort .. => return .none | .lam .. => return .none | .letE .. => return .undef | .proj .. => return .undef | .forallE _ _ b _ => isClassQuick? b | .mdata _ e => isClassQuick? e | .const n _ => isClassQuickConst? n | .mvar mvarId => do let some val ← getExprMVarAssignment? mvarId | return .none isClassQuick? val | .app f _ => do match f.getAppFn with | .const n .. => isClassQuickConst? n | .lam .. => return .undef | .mvar mvarId => let some val ← getExprMVarAssignment? mvarId | return .none match val.getAppFn with | .const n .. => isClassQuickConst? n | _ => return .undef | _ => return .none private def withNewLocalInstanceImp (className : Name) (fvar : Expr) (k : MetaM α) : MetaM α := do let localDecl ← getFVarLocalDecl fvar if localDecl.isImplementationDetail then k else withReader (fun ctx => { ctx with localInstances := ctx.localInstances.push { className := className, fvar := fvar } }) k /-- Add entry `{ className := className, fvar := fvar }` to localInstances, and then execute continuation `k`. -/ def withNewLocalInstance (className : Name) (fvar : Expr) : n α → n α := mapMetaM <| withNewLocalInstanceImp className fvar private def fvarsSizeLtMaxFVars (fvars : Array Expr) (maxFVars? : Option Nat) : Bool := match maxFVars? with | some maxFVars => fvars.size < maxFVars | none => true mutual /-- `withNewLocalInstances isClassExpensive fvars j k` updates the vector or local instances using free variables `fvars[j] ... fvars.back`, and execute `k`. - `isClassExpensive` is defined later. - `isClassExpensive` uses `whnf` which depends (indirectly) on the set of local instances. -/ private partial def withNewLocalInstancesImp (fvars : Array Expr) (i : Nat) (k : MetaM α) : MetaM α := do if h : i < fvars.size then let fvar := fvars.get ⟨i, h⟩ let decl ← getFVarLocalDecl fvar match (← isClassQuick? decl.type) with | .none => withNewLocalInstancesImp fvars (i+1) k | .undef => match (← isClassExpensive? decl.type) with | none => withNewLocalInstancesImp fvars (i+1) k | some c => withNewLocalInstance c fvar <| withNewLocalInstancesImp fvars (i+1) k | .some c => withNewLocalInstance c fvar <| withNewLocalInstancesImp fvars (i+1) k else k /-- `forallTelescopeAuxAux lctx fvars j type` Remarks: - `lctx` is the `MetaM` local context extended with declarations for `fvars`. - `type` is the type we are computing the telescope for. It contains only dangling bound variables in the range `[j, fvars.size)` - if `reducing? == true` and `type` is not `forallE`, we use `whnf`. - when `type` is not a `forallE` nor it can't be reduced to one, we excute the continuation `k`. Here is an example that demonstrates the `reducing?`. Suppose we have ``` abbrev StateM s a := s -> Prod a s ``` Now, assume we are trying to build the telescope for ``` forall (x : Nat), StateM Int Bool ``` if `reducing == true`, the function executes `k #[(x : Nat) (s : Int)] Bool`. if `reducing == false`, the function executes `k #[(x : Nat)] (StateM Int Bool)` if `maxFVars?` is `some max`, then we interrupt the telescope construction when `fvars.size == max` -/ private partial def forallTelescopeReducingAuxAux (reducing : Bool) (maxFVars? : Option Nat) (type : Expr) (k : Array Expr → Expr → MetaM α) : MetaM α := do let rec process (lctx : LocalContext) (fvars : Array Expr) (j : Nat) (type : Expr) : MetaM α := do match type with | .forallE n d b bi => if fvarsSizeLtMaxFVars fvars maxFVars? then let d := d.instantiateRevRange j fvars.size fvars let fvarId ← mkFreshFVarId let lctx := lctx.mkLocalDecl fvarId n d bi let fvar := mkFVar fvarId let fvars := fvars.push fvar process lctx fvars j b else let type := type.instantiateRevRange j fvars.size fvars; withReader (fun ctx => { ctx with lctx := lctx }) do withNewLocalInstancesImp fvars j do k fvars type | _ => let type := type.instantiateRevRange j fvars.size fvars; withReader (fun ctx => { ctx with lctx := lctx }) do withNewLocalInstancesImp fvars j do if reducing && fvarsSizeLtMaxFVars fvars maxFVars? then let newType ← whnf type if newType.isForall then process lctx fvars fvars.size newType else k fvars type else k fvars type process (← getLCtx) #[] 0 type private partial def forallTelescopeReducingAux (type : Expr) (maxFVars? : Option Nat) (k : Array Expr → Expr → MetaM α) : MetaM α := do match maxFVars? with | some 0 => k #[] type | _ => do let newType ← whnf type if newType.isForall then forallTelescopeReducingAuxAux true maxFVars? newType k else k #[] type -- Helper method for isClassExpensive? private partial def isClassApp? (type : Expr) (instantiated := false) : MetaM (Option Name) := do match type.getAppFn with | .const c _ => let env ← getEnv if isClass env c then return some c else -- Use whnf to make sure abbreviations are unfolded match (← whnf type).getAppFn with | .const c _ => if isClass env c then return some c else return none | _ => return none | .mvar .. => if instantiated then return none isClassApp? (← instantiateMVars type) true | _ => return none private partial def isClassExpensive? (type : Expr) : MetaM (Option Name) := withReducible do -- when testing whether a type is a type class, we only unfold reducible constants. forallTelescopeReducingAux type none fun _ type => isClassApp? type private partial def isClassImp? (type : Expr) : MetaM (Option Name) := do match (← isClassQuick? type) with | .none => return none | .some c => return (some c) | .undef => isClassExpensive? type end /-- `isClass? type` return `some ClsName` if `type` is an instance of the class `ClsName`. Example: ``` #eval do let x ← mkAppM ``Inhabited #[mkConst ``Nat] IO.println (← isClass? x) -- (some Inhabited) ``` -/ def isClass? (type : Expr) : MetaM (Option Name) := try isClassImp? type catch _ => return none private def withNewLocalInstancesImpAux (fvars : Array Expr) (j : Nat) : n α → n α := mapMetaM <| withNewLocalInstancesImp fvars j partial def withNewLocalInstances (fvars : Array Expr) (j : Nat) : n α → n α := mapMetaM <| withNewLocalInstancesImpAux fvars j @[inline] private def forallTelescopeImp (type : Expr) (k : Array Expr → Expr → MetaM α) : MetaM α := do forallTelescopeReducingAuxAux (reducing := false) (maxFVars? := none) type k /-- Given `type` of the form `forall xs, A`, execute `k xs A`. This combinator will declare local declarations, create free variables for them, execute `k` with updated local context, and make sure the cache is restored after executing `k`. -/ def forallTelescope (type : Expr) (k : Array Expr → Expr → n α) : n α := map2MetaM (fun k => forallTelescopeImp type k) k private def forallTelescopeReducingImp (type : Expr) (k : Array Expr → Expr → MetaM α) : MetaM α := forallTelescopeReducingAux type (maxFVars? := none) k /-- Similar to `forallTelescope`, but given `type` of the form `forall xs, A`, it reduces `A` and continues bulding the telescope if it is a `forall`. -/ def forallTelescopeReducing (type : Expr) (k : Array Expr → Expr → n α) : n α := map2MetaM (fun k => forallTelescopeReducingImp type k) k private def forallBoundedTelescopeImp (type : Expr) (maxFVars? : Option Nat) (k : Array Expr → Expr → MetaM α) : MetaM α := forallTelescopeReducingAux type maxFVars? k /-- Similar to `forallTelescopeReducing`, stops constructing the telescope when it reaches size `maxFVars`. -/ def forallBoundedTelescope (type : Expr) (maxFVars? : Option Nat) (k : Array Expr → Expr → n α) : n α := map2MetaM (fun k => forallBoundedTelescopeImp type maxFVars? k) k private partial def lambdaTelescopeImp (e : Expr) (consumeLet : Bool) (k : Array Expr → Expr → MetaM α) : MetaM α := do process consumeLet (← getLCtx) #[] 0 e where process (consumeLet : Bool) (lctx : LocalContext) (fvars : Array Expr) (j : Nat) (e : Expr) : MetaM α := do match consumeLet, e with | _, .lam n d b bi => let d := d.instantiateRevRange j fvars.size fvars let fvarId ← mkFreshFVarId let lctx := lctx.mkLocalDecl fvarId n d bi let fvar := mkFVar fvarId process consumeLet lctx (fvars.push fvar) j b | true, .letE n t v b _ => do let t := t.instantiateRevRange j fvars.size fvars let v := v.instantiateRevRange j fvars.size fvars let fvarId ← mkFreshFVarId let lctx := lctx.mkLetDecl fvarId n t v let fvar := mkFVar fvarId process true lctx (fvars.push fvar) j b | _, e => let e := e.instantiateRevRange j fvars.size fvars withReader (fun ctx => { ctx with lctx := lctx }) do withNewLocalInstancesImp fvars j do k fvars e /-- Similar to `lambdaTelescope` but for lambda and let expressions. -/ def lambdaLetTelescope (e : Expr) (k : Array Expr → Expr → n α) : n α := map2MetaM (fun k => lambdaTelescopeImp e true k) k /-- Given `e` of the form `fun ..xs => A`, execute `k xs A`. This combinator will declare local declarations, create free variables for them, execute `k` with updated local context, and make sure the cache is restored after executing `k`. -/ def lambdaTelescope (e : Expr) (k : Array Expr → Expr → n α) : n α := map2MetaM (fun k => lambdaTelescopeImp e false k) k /-- Return the parameter names for the given global declaration. -/ def getParamNames (declName : Name) : MetaM (Array Name) := do forallTelescopeReducing (← getConstInfo declName).type fun xs _ => do xs.mapM fun x => do let localDecl ← x.fvarId!.getDecl return localDecl.userName -- `kind` specifies the metavariable kind for metavariables not corresponding to instance implicit `[ ... ]` arguments. private partial def forallMetaTelescopeReducingAux (e : Expr) (reducing : Bool) (maxMVars? : Option Nat) (kind : MetavarKind) : MetaM (Array Expr × Array BinderInfo × Expr) := process #[] #[] 0 e where process (mvars : Array Expr) (bis : Array BinderInfo) (j : Nat) (type : Expr) : MetaM (Array Expr × Array BinderInfo × Expr) := do if maxMVars?.isEqSome mvars.size then let type := type.instantiateRevRange j mvars.size mvars; return (mvars, bis, type) else match type with | .forallE n d b bi => let d := d.instantiateRevRange j mvars.size mvars let k := if bi.isInstImplicit then MetavarKind.synthetic else kind let mvar ← mkFreshExprMVar d k n let mvars := mvars.push mvar let bis := bis.push bi process mvars bis j b | _ => let type := type.instantiateRevRange j mvars.size mvars; if reducing then do let newType ← whnf type; if newType.isForall then process mvars bis mvars.size newType else return (mvars, bis, type) else return (mvars, bis, type) /-- Given `e` of the form `forall ..xs, A`, this combinator will create a new metavariable for each `x` in `xs` and instantiate `A` with these. Returns a product containing - the new metavariables - the binder info for the `xs` - the instantiated `A` -/ def forallMetaTelescope (e : Expr) (kind := MetavarKind.natural) : MetaM (Array Expr × Array BinderInfo × Expr) := forallMetaTelescopeReducingAux e (reducing := false) (maxMVars? := none) kind /-- Similar to `forallMetaTelescope`, but if `e = forall ..xs, A` it will reduce `A` to construct further mvars. -/ def forallMetaTelescopeReducing (e : Expr) (maxMVars? : Option Nat := none) (kind := MetavarKind.natural) : MetaM (Array Expr × Array BinderInfo × Expr) := forallMetaTelescopeReducingAux e (reducing := true) maxMVars? kind /-- Similar to `forallMetaTelescopeReducing`, stops constructing the telescope when it reaches size `maxMVars`. -/ def forallMetaBoundedTelescope (e : Expr) (maxMVars : Nat) (kind : MetavarKind := MetavarKind.natural) : MetaM (Array Expr × Array BinderInfo × Expr) := forallMetaTelescopeReducingAux e (reducing := true) (maxMVars? := some maxMVars) (kind := kind) /-- Similar to `forallMetaTelescopeReducingAux` but for lambda expressions. -/ partial def lambdaMetaTelescope (e : Expr) (maxMVars? : Option Nat := none) : MetaM (Array Expr × Array BinderInfo × Expr) := process #[] #[] 0 e where process (mvars : Array Expr) (bis : Array BinderInfo) (j : Nat) (type : Expr) : MetaM (Array Expr × Array BinderInfo × Expr) := do let finalize : Unit → MetaM (Array Expr × Array BinderInfo × Expr) := fun _ => do let type := type.instantiateRevRange j mvars.size mvars return (mvars, bis, type) if maxMVars?.isEqSome mvars.size then finalize () else match type with | .lam _ d b bi => let d := d.instantiateRevRange j mvars.size mvars let mvar ← mkFreshExprMVar d let mvars := mvars.push mvar let bis := bis.push bi process mvars bis j b | _ => finalize () private def withNewFVar (n : Name) (fvar fvarType : Expr) (k : Expr → MetaM α) : MetaM α := do if let some c ← isClass? fvarType then withNewLocalInstance c fvar <| k fvar else k fvar private def withLocalDeclImp (n : Name) (bi : BinderInfo) (type : Expr) (k : Expr → MetaM α) (kind : LocalDeclKind) : MetaM α := do let fvarId ← mkFreshFVarId let ctx ← read let lctx := ctx.lctx.mkLocalDecl fvarId n type bi kind let fvar := mkFVar fvarId withReader (fun ctx => { ctx with lctx := lctx }) do withNewFVar n fvar type k /-- Create a free variable `x` with name, binderInfo and type, add it to the context and run in `k`. Then revert the context. -/ def withLocalDecl (name : Name) (bi : BinderInfo) (type : Expr) (k : Expr → n α) (kind : LocalDeclKind := .default) : n α := map1MetaM (fun k => withLocalDeclImp name bi type k kind) k def withLocalDeclD (name : Name) (type : Expr) (k : Expr → n α) : n α := withLocalDecl name BinderInfo.default type k /-- Append an array of free variables `xs` to the local context and execute `k xs`. declInfos takes the form of an array consisting of: - the name of the variable - the binder info of the variable - a type constructor for the variable, where the array consists of all of the free variables defined prior to this one. This is needed because the type of the variable may depend on prior variables. -/ partial def withLocalDecls [Inhabited α] (declInfos : Array (Name × BinderInfo × (Array Expr → n Expr))) (k : (xs : Array Expr) → n α) : n α := loop #[] where loop [Inhabited α] (acc : Array Expr) : n α := do if acc.size < declInfos.size then let (name, bi, typeCtor) := declInfos[acc.size]! withLocalDecl name bi (←typeCtor acc) fun x => loop (acc.push x) else k acc def withLocalDeclsD [Inhabited α] (declInfos : Array (Name × (Array Expr → n Expr))) (k : (xs : Array Expr) → n α) : n α := withLocalDecls (declInfos.map (fun (name, typeCtor) => (name, BinderInfo.default, typeCtor))) k private def withNewBinderInfosImp (bs : Array (FVarId × BinderInfo)) (k : MetaM α) : MetaM α := do let lctx := bs.foldl (init := (← getLCtx)) fun lctx (fvarId, bi) => lctx.setBinderInfo fvarId bi withReader (fun ctx => { ctx with lctx := lctx }) k def withNewBinderInfos (bs : Array (FVarId × BinderInfo)) (k : n α) : n α := mapMetaM (fun k => withNewBinderInfosImp bs k) k /-- Execute `k` using a local context where any `x` in `xs` that is tagged as instance implicit is treated as a regular implicit. -/ def withInstImplicitAsImplict (xs : Array Expr) (k : MetaM α) : MetaM α := do let newBinderInfos ← xs.filterMapM fun x => do let bi ← x.fvarId!.getBinderInfo if bi == .instImplicit then return some (x.fvarId!, .implicit) else return none withNewBinderInfos newBinderInfos k private def withLetDeclImp (n : Name) (type : Expr) (val : Expr) (k : Expr → MetaM α) (kind : LocalDeclKind) : MetaM α := do let fvarId ← mkFreshFVarId let ctx ← read let lctx := ctx.lctx.mkLetDecl fvarId n type val (nonDep := false) kind let fvar := mkFVar fvarId withReader (fun ctx => { ctx with lctx := lctx }) do withNewFVar n fvar type k /-- Add the local declaration `<name> : <type> := <val>` to the local context and execute `k x`, where `x` is a new free variable corresponding to the `let`-declaration. After executing `k x`, the local context is restored. -/ def withLetDecl (name : Name) (type : Expr) (val : Expr) (k : Expr → n α) (kind : LocalDeclKind := .default) : n α := map1MetaM (fun k => withLetDeclImp name type val k kind) k def withLocalInstancesImp (decls : List LocalDecl) (k : MetaM α) : MetaM α := do let mut localInsts := (← read).localInstances let size := localInsts.size for decl in decls do unless decl.isImplementationDetail do if let some className ← isClass? decl.type then localInsts := localInsts.push { className, fvar := decl.toExpr } if localInsts.size == size then k else withReader (fun ctx => { ctx with localInstances := localInsts }) k /-- Register any local instance in `decls` -/ def withLocalInstances (decls : List LocalDecl) : n α → n α := mapMetaM <| withLocalInstancesImp decls private def withExistingLocalDeclsImp (decls : List LocalDecl) (k : MetaM α) : MetaM α := do let ctx ← read let lctx := decls.foldl (fun (lctx : LocalContext) decl => lctx.addDecl decl) ctx.lctx withReader (fun ctx => { ctx with lctx := lctx }) do withLocalInstancesImp decls k /-- `withExistingLocalDecls decls k`, adds the given local declarations to the local context, and then executes `k`. This method assumes declarations in `decls` have valid `FVarId`s. After executing `k`, the local context is restored. Remark: this method is used, for example, to implement the `match`-compiler. Each `match`-alternative commes with a local declarations (corresponding to pattern variables), and we use `withExistingLocalDecls` to add them to the local context before we process them. -/ def withExistingLocalDecls (decls : List LocalDecl) : n α → n α := mapMetaM <| withExistingLocalDeclsImp decls private def withNewMCtxDepthImp (allowLevelAssignments : Bool) (x : MetaM α) : MetaM α := do let saved ← get modify fun s => { s with mctx := s.mctx.incDepth allowLevelAssignments, postponed := {} } try x finally modify fun s => { s with mctx := saved.mctx, postponed := saved.postponed } /-- `withNewMCtxDepth k` executes `k` with a higher metavariable context depth, where metavariables created outside the `withNewMCtxDepth` (with a lower depth) cannot be assigned. If `allowLevelAssignments` is set to true, then the level metavariable depth is not increased, and level metavariables from the outer scope can be assigned. (This is used by TC synthesis.) -/ def withNewMCtxDepth (k : n α) (allowLevelAssignments := false) : n α := mapMetaM (withNewMCtxDepthImp allowLevelAssignments) k private def withLocalContextImp (lctx : LocalContext) (localInsts : LocalInstances) (x : MetaM α) : MetaM α := do withReader (fun ctx => { ctx with lctx := lctx, localInstances := localInsts }) do x /-- `withLCtx lctx localInsts k` replaces the local context and local instances, and then executes `k`. The local context and instances are restored after executing `k`. This method assumes that the local instances in `localInsts` are in the local context `lctx`. -/ def withLCtx (lctx : LocalContext) (localInsts : LocalInstances) : n α → n α := mapMetaM <| withLocalContextImp lctx localInsts private def withMVarContextImp (mvarId : MVarId) (x : MetaM α) : MetaM α := do let mvarDecl ← mvarId.getDecl withLocalContextImp mvarDecl.lctx mvarDecl.localInstances x /-- Execute `x` using the given metavariable `LocalContext` and `LocalInstances`. The type class resolution cache is flushed when executing `x` if its `LocalInstances` are different from the current ones. -/ def _root_.Lean.MVarId.withContext (mvarId : MVarId) : n α → n α := mapMetaM <| withMVarContextImp mvarId @[deprecated MVarId.withContext] def withMVarContext (mvarId : MVarId) : n α → n α := mvarId.withContext private def withMCtxImp (mctx : MetavarContext) (x : MetaM α) : MetaM α := do let mctx' ← getMCtx setMCtx mctx try x finally setMCtx mctx' /-- `withMCtx mctx k` replaces the metavariable context and then executes `k`. The metavariable context is restored after executing `k`. This method is used to implement the type class resolution procedure. -/ def withMCtx (mctx : MetavarContext) : n α → n α := mapMetaM <| withMCtxImp mctx @[inline] private def approxDefEqImp (x : MetaM α) : MetaM α := withConfig (fun config => { config with foApprox := true, ctxApprox := true, quasiPatternApprox := true}) x /-- Execute `x` using approximate unification: `foApprox`, `ctxApprox` and `quasiPatternApprox`. -/ @[inline] def approxDefEq : n α → n α := mapMetaM approxDefEqImp @[inline] private def fullApproxDefEqImp (x : MetaM α) : MetaM α := withConfig (fun config => { config with foApprox := true, ctxApprox := true, quasiPatternApprox := true, constApprox := true }) x /-- Similar to `approxDefEq`, but uses all available approximations. We don't use `constApprox` by default at `approxDefEq` because it often produces undesirable solution for monadic code. For example, suppose we have `pure (x > 0)` which has type `?m Prop`. We also have the goal `[Pure ?m]`. Now, assume the expected type is `IO Bool`. Then, the unification constraint `?m Prop =?= IO Bool` could be solved as `?m := fun _ => IO Bool` using `constApprox`, but this spurious solution would generate a failure when we try to solve `[Pure (fun _ => IO Bool)]` -/ @[inline] def fullApproxDefEq : n α → n α := mapMetaM fullApproxDefEqImp /-- Instantiate assigned universe metavariables in `u`, and then normalize it. -/ def normalizeLevel (u : Level) : MetaM Level := do let u ← instantiateLevelMVars u pure u.normalize /-- `whnf` with reducible transparency.-/ def whnfR (e : Expr) : MetaM Expr := withTransparency TransparencyMode.reducible <| whnf e /-- `whnf` with default transparency.-/ def whnfD (e : Expr) : MetaM Expr := withTransparency TransparencyMode.default <| whnf e /-- `whnf` with instances transparency.-/ def whnfI (e : Expr) : MetaM Expr := withTransparency TransparencyMode.instances <| whnf e /-- Mark declaration `declName` with the attribute `[inline]`. This method does not check whether the given declaration is a definition. Recall that this attribute can only be set in the same module where `declName` has been declared. -/ def setInlineAttribute (declName : Name) (kind := Compiler.InlineAttributeKind.inline): MetaM Unit := do let env ← getEnv match Compiler.setInlineAttribute env declName kind with | .ok env => setEnv env | .error msg => throwError msg private partial def instantiateForallAux (ps : Array Expr) (i : Nat) (e : Expr) : MetaM Expr := do if h : i < ps.size then let p := ps.get ⟨i, h⟩ match (← whnf e) with | .forallE _ _ b _ => instantiateForallAux ps (i+1) (b.instantiate1 p) | _ => throwError "invalid instantiateForall, too many parameters" else return e /-- Given `e` of the form `forall (a_1 : A_1) ... (a_n : A_n), B[a_1, ..., a_n]` and `p_1 : A_1, ... p_n : A_n`, return `B[p_1, ..., p_n]`. -/ def instantiateForall (e : Expr) (ps : Array Expr) : MetaM Expr := instantiateForallAux ps 0 e private partial def instantiateLambdaAux (ps : Array Expr) (i : Nat) (e : Expr) : MetaM Expr := do if h : i < ps.size then let p := ps.get ⟨i, h⟩ match (← whnf e) with | .lam _ _ b _ => instantiateLambdaAux ps (i+1) (b.instantiate1 p) | _ => throwError "invalid instantiateLambda, too many parameters" else return e /-- Given `e` of the form `fun (a_1 : A_1) ... (a_n : A_n) => t[a_1, ..., a_n]` and `p_1 : A_1, ... p_n : A_n`, return `t[p_1, ..., p_n]`. It uses `whnf` to reduce `e` if it is not a lambda -/ def instantiateLambda (e : Expr) (ps : Array Expr) : MetaM Expr := instantiateLambdaAux ps 0 e /-- Pretty-print the given expression. -/ def ppExprWithInfos (e : Expr) : MetaM FormatWithInfos := do let ctxCore ← readThe Core.Context Lean.ppExprWithInfos { env := (← getEnv), mctx := (← getMCtx), lctx := (← getLCtx), opts := (← getOptions), currNamespace := ctxCore.currNamespace, openDecls := ctxCore.openDecls } e /-- Pretty-print the given expression. -/ def ppExpr (e : Expr) : MetaM Format := (·.fmt) <$> ppExprWithInfos e @[inline] protected def orElse (x : MetaM α) (y : Unit → MetaM α) : MetaM α := do let s ← saveState try x catch _ => s.restore; y () instance : OrElse (MetaM α) := ⟨Meta.orElse⟩ instance : Alternative MetaM where failure := fun {_} => throwError "failed" orElse := Meta.orElse @[inline] private def orelseMergeErrorsImp (x y : MetaM α) (mergeRef : Syntax → Syntax → Syntax := fun r₁ _ => r₁) (mergeMsg : MessageData → MessageData → MessageData := fun m₁ m₂ => m₁ ++ Format.line ++ m₂) : MetaM α := do let env ← getEnv let mctx ← getMCtx try x catch ex => setEnv env setMCtx mctx match ex with | Exception.error ref₁ m₁ => try y catch | Exception.error ref₂ m₂ => throw <| Exception.error (mergeRef ref₁ ref₂) (mergeMsg m₁ m₂) | ex => throw ex | ex => throw ex /-- Similar to `orelse`, but merge errors. Note that internal errors are not caught. The default `mergeRef` uses the `ref` (position information) for the first message. The default `mergeMsg` combines error messages using `Format.line ++ Format.line` as a separator. -/ @[inline] def orelseMergeErrors [MonadControlT MetaM m] [Monad m] (x y : m α) (mergeRef : Syntax → Syntax → Syntax := fun r₁ _ => r₁) (mergeMsg : MessageData → MessageData → MessageData := fun m₁ m₂ => m₁ ++ Format.line ++ Format.line ++ m₂) : m α := do controlAt MetaM fun runInBase => orelseMergeErrorsImp (runInBase x) (runInBase y) mergeRef mergeMsg /-- Execute `x`, and apply `f` to the produced error message -/ def mapErrorImp (x : MetaM α) (f : MessageData → MessageData) : MetaM α := do try x catch | Exception.error ref msg => throw <| Exception.error ref <| f msg | ex => throw ex @[inline] def mapError [MonadControlT MetaM m] [Monad m] (x : m α) (f : MessageData → MessageData) : m α := controlAt MetaM fun runInBase => mapErrorImp (runInBase x) f /-- Sort free variables using an order `x < y` iff `x` was defined before `y`. If a free variable is not in the local context, we use their id. -/ def sortFVarIds (fvarIds : Array FVarId) : MetaM (Array FVarId) := do let lctx ← getLCtx return fvarIds.qsort fun fvarId₁ fvarId₂ => match lctx.find? fvarId₁, lctx.find? fvarId₂ with | some d₁, some d₂ => d₁.index < d₂.index | some _, none => false | none, some _ => true | none, none => Name.quickLt fvarId₁.name fvarId₂.name end Methods /-- Return `true` if `declName` is an inductive predicate. That is, `inductive` type in `Prop`. -/ def isInductivePredicate (declName : Name) : MetaM Bool := do match (← getEnv).find? declName with | some (.inductInfo { type := type, ..}) => forallTelescopeReducing type fun _ type => do match (← whnfD type) with | .sort u .. => return u == levelZero | _ => return false | _ => return false def isListLevelDefEqAux : List Level → List Level → MetaM Bool | [], [] => return true | u::us, v::vs => isLevelDefEqAux u v <&&> isListLevelDefEqAux us vs | _, _ => return false def getNumPostponed : MetaM Nat := do return (← getPostponed).size def getResetPostponed : MetaM (PersistentArray PostponedEntry) := do let ps ← getPostponed setPostponed {} return ps /-- Annotate any constant and sort in `e` that satisfies `p` with `pp.universes true` -/ private def exposeRelevantUniverses (e : Expr) (p : Level → Bool) : Expr := e.replace fun | .const _ us => if us.any p then some (e.setPPUniverses true) else none | .sort u => if p u then some (e.setPPUniverses true) else none | _ => none private def mkLeveErrorMessageCore (header : String) (entry : PostponedEntry) : MetaM MessageData := do match entry.ctx? with | none => return m!"{header}{indentD m!"{entry.lhs} =?= {entry.rhs}"}" | some ctx => withLCtx ctx.lctx ctx.localInstances do let s := entry.lhs.collectMVars entry.rhs.collectMVars /- `p u` is true if it contains a universe metavariable in `s` -/ let p (u : Level) := u.any fun | .mvar m => s.contains m | _ => false let lhs := exposeRelevantUniverses (← instantiateMVars ctx.lhs) p let rhs := exposeRelevantUniverses (← instantiateMVars ctx.rhs) p try addMessageContext m!"{header}{indentD m!"{entry.lhs} =?= {entry.rhs}"}\nwhile trying to unify{indentD m!"{lhs} : {← inferType lhs}"}\nwith{indentD m!"{rhs} : {← inferType rhs}"}" catch _ => addMessageContext m!"{header}{indentD m!"{entry.lhs} =?= {entry.rhs}"}\nwhile trying to unify{indentD lhs}\nwith{indentD rhs}" def mkLevelStuckErrorMessage (entry : PostponedEntry) : MetaM MessageData := do mkLeveErrorMessageCore "stuck at solving universe constraint" entry def mkLevelErrorMessage (entry : PostponedEntry) : MetaM MessageData := do mkLeveErrorMessageCore "failed to solve universe constraint" entry private def processPostponedStep (exceptionOnFailure : Bool) : MetaM Bool := do let ps ← getResetPostponed for p in ps do unless (← withReader (fun ctx => { ctx with defEqCtx? := p.ctx? }) <| isLevelDefEqAux p.lhs p.rhs) do if exceptionOnFailure then withRef p.ref do throwError (← mkLevelErrorMessage p) else return false return true partial def processPostponed (mayPostpone : Bool := true) (exceptionOnFailure := false) : MetaM Bool := do if (← getNumPostponed) == 0 then return true else let numPostponedBegin ← getNumPostponed withTraceNode `Meta.isLevelDefEq.postponed (fun _ => return m!"processing #{numPostponedBegin} postponed is-def-eq level constraints") do let rec loop : MetaM Bool := do let numPostponed ← getNumPostponed if numPostponed == 0 then return true else if !(← processPostponedStep exceptionOnFailure) then return false else let numPostponed' ← getNumPostponed if numPostponed' == 0 then return true else if numPostponed' < numPostponed then loop else trace[Meta.isLevelDefEq.postponed] "no progress solving pending is-def-eq level constraints" return mayPostpone loop /-- `checkpointDefEq x` executes `x` and process all postponed universe level constraints produced by `x`. We keep the modifications only if `processPostponed` return true and `x` returned `true`. If `mayPostpone == false`, all new postponed universe level constraints must be solved before returning. We currently try to postpone universe constraints as much as possible, even when by postponing them we are not sure whether `x` really succeeded or not. -/ @[specialize] def checkpointDefEq (x : MetaM Bool) (mayPostpone : Bool := true) : MetaM Bool := do let s ← saveState /- It is not safe to use the `isDefEq` cache between different `isDefEq` calls. Reason: different configuration settings, and result depends on the state of the `MetavarContext` We have tried in the past to track when the result was independent of the `MetavarContext` state but it was not effective. It is more important to cache aggressively inside of a single `isDefEq` call because some of the heuristics create many similar subproblems. See issue #1102 for an example that triggers an exponential blowup if we don't use this more aggresive form of caching. -/ modifyDefEqCache fun _ => {} let postponed ← getResetPostponed try if (← x) then if (← processPostponed mayPostpone) then let newPostponed ← getPostponed setPostponed (postponed ++ newPostponed) return true else s.restore return false else s.restore return false catch ex => s.restore throw ex /-- Determines whether two universe level expressions are definitionally equal to each other. -/ def isLevelDefEq (u v : Level) : MetaM Bool := checkpointDefEq (mayPostpone := true) <| Meta.isLevelDefEqAux u v /-- See `isDefEq`. -/ def isExprDefEq (t s : Expr) : MetaM Bool := withReader (fun ctx => { ctx with defEqCtx? := some { lhs := t, rhs := s, lctx := ctx.lctx, localInstances := ctx.localInstances } }) do checkpointDefEq (mayPostpone := true) <| Meta.isExprDefEqAux t s /-- Determines whether two expressions are definitionally equal to each other. To control how metavariables are assigned and unified, metavariables and their context have a "depth". Given a metavariable `?m` and a `MetavarContext` `mctx`, `?m` is not assigned if `?m.depth != mctx.depth`. The combinator `withNewMCtxDepth x` will bump the depth while executing `x`. So, `withNewMCtxDepth (isDefEq a b)` is `isDefEq` without any mvar assignment happening whereas `isDefEq a b` will assign any metavariables of the current depth in `a` and `b` to unify them. For matching (where only mvars in `b` should be assigned), we create the term inside the `withNewMCtxDepth`. For an example, see [Lean.Meta.Simp.tryTheoremWithExtraArgs?](https://github.com/leanprover/lean4/blob/master/src/Lean/Meta/Tactic/Simp/Rewrite.lean#L100-L106) -/ abbrev isDefEq (t s : Expr) : MetaM Bool := isExprDefEq t s def isExprDefEqGuarded (a b : Expr) : MetaM Bool := do try isExprDefEq a b catch _ => return false /-- Similar to `isDefEq`, but returns `false` if an exception has been thrown. -/ abbrev isDefEqGuarded (t s : Expr) : MetaM Bool := isExprDefEqGuarded t s def isDefEqNoConstantApprox (t s : Expr) : MetaM Bool := approxDefEq <| isDefEq t s /-- Eta expand the given expression. Example: ``` etaExpand (mkConst ``Nat.add) ``` produces `fun x y => Nat.add x y` -/ def etaExpand (e : Expr) : MetaM Expr := withDefault do forallTelescopeReducing (← inferType e) fun xs _ => mkLambdaFVars xs (mkAppN e xs) end Meta builtin_initialize registerTraceClass `Meta.isLevelDefEq.postponed export Meta (MetaM) end Lean
cec18bf037b5ca29284b69567eac450d40f0737c
5719a16e23dfc08cdea7a5bf035b81690f307965
/src/Init/Control/Applicative.lean
8e80b9c5c97e82a2e28e64554f2e5bf70b3e693b
[ "Apache-2.0" ]
permissive
postmasters/lean4
488b03969a371e1507e1e8a4df9ebf63c7cbe7ac
f3976fc53a883ac7606fc59357d43f4b51016ca7
refs/heads/master
1,655,582,707,480
1,588,682,595,000
1,588,682,595,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,358
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ prelude import Init.Control.Functor open Function universes u v class HasPure (f : Type u → Type v) := (pure {} {α : Type u} : α → f α) export HasPure (pure) class HasSeq (f : Type u → Type v) : Type (max (u+1) v) := (seq : ∀ {α β : Type u}, f (α → β) → f α → f β) infixl <*> := HasSeq.seq class HasSeqLeft (f : Type u → Type v) : Type (max (u+1) v) := (seqLeft : ∀ {α : Type u}, f α → f PUnit → f α) infixl <* := HasSeqLeft.seqLeft class HasSeqRight (f : Type u → Type v) : Type (max (u+1) v) := (seqRight : ∀ {β : Type u}, f PUnit → f β → f β) infixr *> := HasSeqRight.seqRight class Applicative (f : Type u → Type v) extends Functor f, HasPure f, HasSeq f, HasSeqLeft f, HasSeqRight f := (map := fun _ _ x y => pure x <*> y) (seqLeft := fun α a b => const _ <$> a <*> b) (seqRight := fun β a b => const _ id <$> a <*> b) @[macroInline] def when {m : Type → Type u} [Applicative m] (c : Prop) [h : Decidable c] (t : m Unit) : m Unit := if c then t else pure () @[macroInline] def unless {m : Type → Type u} [Applicative m] (c : Prop) [h : Decidable c] (e : m Unit) : m Unit := if c then pure () else e
9527576f6458533647de167abbc8c0c1e972bd52
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/data/polynomial/basic.lean
e605e09fe9cf33405fe20c1f3e8f37ee6add6aec
[ "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
27,461
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import algebra.monoid_algebra.basic /-! # Theory of univariate polynomials This file defines `polynomial R`, the type of univariate polynomials over the semiring `R`, builds a semiring structure on it, and gives basic definitions that are expanded in other files in this directory. ## Main definitions * `monomial n a` is the polynomial `a X^n`. Note that `monomial n` is defined as an `R`-linear map. * `C a` is the constant polynomial `a`. Note that `C` is defined as a ring homomorphism. * `X` is the polynomial `X`, i.e., `monomial 1 1`. * `p.sum f` is `∑ n in p.support, f n (p.coeff n)`, i.e., one sums the values of functions applied to coefficients of the polynomial `p`. * `p.erase n` is the polynomial `p` in which one removes the `c X^n` term. There are often two natural variants of lemmas involving sums, depending on whether one acts on the polynomials, or on the function. The naming convention is that one adds `index` when acting on the polynomials. For instance, * `sum_add_index` states that `(p + q).sum f = p.sum f + q.sum f`; * `sum_add` states that `p.sum (λ n x, f n x + g n x) = p.sum f + p.sum g`. ## Implementation Polynomials are defined using `add_monoid_algebra R ℕ`, where `R` is a commutative semiring, but through a structure to make them irreducible from the point of view of the kernel. Most operations are irreducible since Lean can not compute anyway with `add_monoid_algebra`. There are two exceptions that we make semireducible: * The zero polynomial, so that its coefficients are definitionally equal to `0`. * The scalar action, to permit typeclass search to unfold it to resolve potential instance diamonds. The raw implementation of the equivalence between `polynomial R` and `add_monoid_algebra R ℕ` is done through `of_finsupp` and `to_finsupp` (or, equivalently, `rcases p` when `p` is a polynomial gives an element `q` of `add_monoid_algebra R ℕ`, and conversely `⟨q⟩` gives back `p`). The equivalence is also registered as a ring equiv in `polynomial.to_finsupp_iso`. These should in general not be used once the basic API for polynomials is constructed. -/ noncomputable theory /-- `polynomial R` is the type of univariate polynomials over `R`. Polynomials should be seen as (semi-)rings with the additional constructor `X`. The embedding from `R` is called `C`. -/ structure polynomial (R : Type*) [semiring R] := of_finsupp :: (to_finsupp : add_monoid_algebra R ℕ) open finsupp add_monoid_algebra open_locale big_operators namespace polynomial universes u variables {R : Type u} {a b : R} {m n : ℕ} section semiring variables [semiring R] {p q : polynomial R} lemma forall_iff_forall_finsupp (P : polynomial R → Prop) : (∀ p, P p) ↔ ∀ (q : add_monoid_algebra R ℕ), P ⟨q⟩ := ⟨λ h q, h ⟨q⟩, λ h ⟨p⟩, h p⟩ lemma exists_iff_exists_finsupp (P : polynomial R → Prop) : (∃ p, P p) ↔ ∃ (q : add_monoid_algebra R ℕ), P ⟨q⟩ := ⟨λ ⟨⟨p⟩, hp⟩, ⟨p, hp⟩, λ ⟨q, hq⟩, ⟨⟨q⟩, hq⟩ ⟩ /-- The function version of `monomial`. Use `monomial` instead of this one. -/ @[irreducible] def monomial_fun (n : ℕ) (a : R) : polynomial R := ⟨finsupp.single n a⟩ @[irreducible] private def add : polynomial R → polynomial R → polynomial R | ⟨a⟩ ⟨b⟩ := ⟨a + b⟩ @[irreducible] private def neg {R : Type u} [ring R] : polynomial R → polynomial R | ⟨a⟩ := ⟨-a⟩ @[irreducible] private def mul : polynomial R → polynomial R → polynomial R | ⟨a⟩ ⟨b⟩ := ⟨a * b⟩ instance : has_zero (polynomial R) := ⟨⟨0⟩⟩ instance : has_one (polynomial R) := ⟨monomial_fun 0 (1 : R)⟩ instance : has_add (polynomial R) := ⟨add⟩ instance {R : Type u} [ring R] : has_neg (polynomial R) := ⟨neg⟩ instance : has_mul (polynomial R) := ⟨mul⟩ instance {S : Type*} [monoid S] [distrib_mul_action S R] : has_scalar S (polynomial R) := ⟨λ r p, ⟨r • p.to_finsupp⟩⟩ lemma zero_to_finsupp : (⟨0⟩ : polynomial R) = 0 := rfl lemma one_to_finsupp : (⟨1⟩ : polynomial R) = 1 := begin change (⟨1⟩ : polynomial R) = monomial_fun 0 (1 : R), rw [monomial_fun], refl end lemma add_to_finsupp {a b} : (⟨a⟩ + ⟨b⟩ : polynomial R) = ⟨a + b⟩ := show add _ _ = _, by rw add lemma neg_to_finsupp {R : Type u} [ring R] {a} : (-⟨a⟩ : polynomial R) = ⟨-a⟩ := show neg _ = _, by rw neg lemma mul_to_finsupp {a b} : (⟨a⟩ * ⟨b⟩ : polynomial R) = ⟨a * b⟩ := show mul _ _ = _, by rw mul lemma smul_to_finsupp {S : Type*} [monoid S] [distrib_mul_action S R] {a : S} {b} : (a • ⟨b⟩ : polynomial R) = ⟨a • b⟩ := rfl lemma _root_.is_smul_regular.polynomial {S : Type*} [monoid S] [distrib_mul_action S R] {a : S} (ha : is_smul_regular R a) : is_smul_regular (polynomial R) a | ⟨x⟩ ⟨y⟩ h := congr_arg _ $ ha.finsupp (polynomial.of_finsupp.inj h) instance : inhabited (polynomial R) := ⟨0⟩ instance : semiring (polynomial R) := by refine_struct { zero := (0 : polynomial R), one := 1, mul := (*), add := (+), nsmul := (•), npow := npow_rec, npow_zero' := λ x, rfl, npow_succ' := λ n x, rfl }; { repeat { rintro ⟨_⟩, }; simp [← zero_to_finsupp, ← one_to_finsupp, add_to_finsupp, mul_to_finsupp, mul_assoc, mul_add, add_mul, smul_to_finsupp, nat.succ_eq_one_add]; abel } instance {S} [monoid S] [distrib_mul_action S R] : distrib_mul_action S (polynomial R) := { smul := (•), one_smul := by { rintros ⟨⟩, simp [smul_to_finsupp] }, mul_smul := by { rintros _ _ ⟨⟩, simp [smul_to_finsupp, mul_smul], }, smul_add := by { rintros _ ⟨⟩ ⟨⟩, simp [smul_to_finsupp, add_to_finsupp] }, smul_zero := by { rintros _, simp [← zero_to_finsupp, smul_to_finsupp] } } instance {S} [monoid S] [distrib_mul_action S R] [has_faithful_scalar S R] : has_faithful_scalar S (polynomial R) := { eq_of_smul_eq_smul := λ s₁ s₂ h, eq_of_smul_eq_smul $ λ a : ℕ →₀ R, congr_arg to_finsupp (h ⟨a⟩) } instance {S} [semiring S] [module S R] : module S (polynomial R) := { smul := (•), add_smul := by { rintros _ _ ⟨⟩, simp [smul_to_finsupp, add_to_finsupp, add_smul] }, zero_smul := by { rintros ⟨⟩, simp [smul_to_finsupp, ← zero_to_finsupp] }, ..polynomial.distrib_mul_action } instance {S₁ S₂} [monoid S₁] [monoid S₂] [distrib_mul_action S₁ R] [distrib_mul_action S₂ R] [smul_comm_class S₁ S₂ R] : smul_comm_class S₁ S₂ (polynomial R) := ⟨by { rintros _ _ ⟨⟩, simp [smul_to_finsupp, smul_comm] }⟩ instance {S₁ S₂} [has_scalar S₁ S₂] [monoid S₁] [monoid S₂] [distrib_mul_action S₁ R] [distrib_mul_action S₂ R] [is_scalar_tower S₁ S₂ R] : is_scalar_tower S₁ S₂ (polynomial R) := ⟨by { rintros _ _ ⟨⟩, simp [smul_to_finsupp] }⟩ instance {S} [monoid S] [distrib_mul_action S R] [distrib_mul_action Sᵐᵒᵖ R] [is_central_scalar S R] : is_central_scalar S (polynomial R) := ⟨by { rintros _ ⟨⟩, simp [smul_to_finsupp, op_smul_eq_smul] }⟩ instance [subsingleton R] : unique (polynomial R) := { uniq := by { rintros ⟨x⟩, change (⟨x⟩ : polynomial R) = 0, rw [← zero_to_finsupp], simp }, .. polynomial.inhabited } variable (R) /-- Ring isomorphism between `polynomial R` and `add_monoid_algebra R ℕ`. This is just an implementation detail, but it can be useful to transfer results from `finsupp` to polynomials. -/ @[simps] def to_finsupp_iso : polynomial R ≃+* add_monoid_algebra R ℕ := { to_fun := λ p, p.to_finsupp, inv_fun := λ p, ⟨p⟩, left_inv := λ ⟨p⟩, rfl, right_inv := λ p, rfl, map_mul' := by { rintros ⟨⟩ ⟨⟩, simp [mul_to_finsupp] }, map_add' := by { rintros ⟨⟩ ⟨⟩, simp [add_to_finsupp] } } /-- Ring isomorphism between `(polynomial R)ᵐᵒᵖ` and `polynomial Rᵐᵒᵖ`. -/ @[simps] def op_ring_equiv : (polynomial R)ᵐᵒᵖ ≃+* polynomial Rᵐᵒᵖ := ((to_finsupp_iso R).op.trans add_monoid_algebra.op_ring_equiv).trans (to_finsupp_iso _).symm variable {R} lemma sum_to_finsupp {ι : Type*} (s : finset ι) (f : ι → add_monoid_algebra R ℕ) : ∑ i in s, (⟨f i⟩ : polynomial R) = ⟨∑ i in s, f i⟩ := ((to_finsupp_iso R).symm.to_add_monoid_hom.map_sum f s).symm /-- The set of all `n` such that `X^n` has a non-zero coefficient. -/ def support : polynomial R → finset ℕ | ⟨p⟩ := p.support @[simp] lemma support_zero : (0 : polynomial R).support = ∅ := rfl @[simp] lemma support_eq_empty : p.support = ∅ ↔ p = 0 := by { rcases p, simp [support, ← zero_to_finsupp] } lemma card_support_eq_zero : p.support.card = 0 ↔ p = 0 := by simp /-- `monomial s a` is the monomial `a * X^s` -/ def monomial (n : ℕ) : R →ₗ[R] polynomial R := { to_fun := monomial_fun n, map_add' := by simp [monomial_fun, add_to_finsupp], map_smul' := by simp [monomial_fun, smul_to_finsupp] } @[simp] lemma monomial_zero_right (n : ℕ) : monomial n (0 : R) = 0 := (monomial n).map_zero -- This is not a `simp` lemma as `monomial_zero_left` is more general. lemma monomial_zero_one : monomial 0 (1 : R) = 1 := rfl -- TODO: can't we just delete this one? lemma monomial_add (n : ℕ) (r s : R) : monomial n (r + s) = monomial n r + monomial n s := (monomial n).map_add _ _ lemma monomial_mul_monomial (n m : ℕ) (r s : R) : monomial n r * monomial m s = monomial (n + m) (r * s) := by simp only [monomial, monomial_fun, linear_map.coe_mk, mul_to_finsupp, add_monoid_algebra.single_mul_single] @[simp] lemma monomial_pow (n : ℕ) (r : R) (k : ℕ) : (monomial n r)^k = monomial (n*k) (r^k) := begin induction k with k ih, { simp [pow_zero, monomial_zero_one], }, { simp [pow_succ, ih, monomial_mul_monomial, nat.succ_eq_add_one, mul_add, add_comm] }, end lemma smul_monomial {S} [monoid S] [distrib_mul_action S R] (a : S) (n : ℕ) (b : R) : a • monomial n b = monomial n (a • b) := by simp [monomial, monomial_fun, smul_to_finsupp] @[simp] lemma to_finsupp_iso_monomial : (to_finsupp_iso R) (monomial n a) = single n a := by simp [to_finsupp_iso, monomial, monomial_fun] @[simp] lemma to_finsupp_iso_symm_single : (to_finsupp_iso R).symm (single n a) = monomial n a := by simp [to_finsupp_iso, monomial, monomial_fun] lemma monomial_injective (n : ℕ) : function.injective (monomial n : R → polynomial R) := begin convert (to_finsupp_iso R).symm.injective.comp (single_injective n), ext, simp end @[simp] lemma monomial_eq_zero_iff (t : R) (n : ℕ) : monomial n t = 0 ↔ t = 0 := linear_map.map_eq_zero_iff _ (polynomial.monomial_injective n) lemma support_add : (p + q).support ⊆ p.support ∪ q.support := begin rcases p, rcases q, simp only [add_to_finsupp, support], exact support_add end /-- `C a` is the constant polynomial `a`. `C` is provided as a ring homomorphism. -/ def C : R →+* polynomial R := { map_one' := by simp [monomial_zero_one], map_mul' := by simp [monomial_mul_monomial], map_zero' := by simp, .. monomial 0 } @[simp] lemma monomial_zero_left (a : R) : monomial 0 a = C a := rfl lemma C_0 : C (0 : R) = 0 := by simp lemma C_1 : C (1 : R) = 1 := rfl lemma C_mul : C (a * b) = C a * C b := C.map_mul a b lemma C_add : C (a + b) = C a + C b := C.map_add a b @[simp] lemma smul_C {S} [monoid S] [distrib_mul_action S R] (s : S) (r : R) : s • C r = C (s • r) := smul_monomial _ _ r @[simp] lemma C_bit0 : C (bit0 a) = bit0 (C a) := C_add @[simp] lemma C_bit1 : C (bit1 a) = bit1 (C a) := by simp [bit1, C_bit0] lemma C_pow : C (a ^ n) = C a ^ n := C.map_pow a n @[simp] lemma C_eq_nat_cast (n : ℕ) : C (n : R) = (n : polynomial R) := map_nat_cast C n @[simp] lemma C_mul_monomial : C a * monomial n b = monomial n (a * b) := by simp only [←monomial_zero_left, monomial_mul_monomial, zero_add] @[simp] lemma monomial_mul_C : monomial n a * C b = monomial n (a * b) := by simp only [←monomial_zero_left, monomial_mul_monomial, add_zero] /-- `X` is the polynomial variable (aka indeterminate). -/ def X : polynomial R := monomial 1 1 lemma monomial_one_one_eq_X : monomial 1 (1 : R) = X := rfl lemma monomial_one_right_eq_X_pow (n : ℕ) : monomial n (1 : R) = X^n := begin induction n with n ih, { simp [monomial_zero_one], }, { rw [pow_succ, ←ih, ←monomial_one_one_eq_X, monomial_mul_monomial, add_comm, one_mul], } end /-- `X` commutes with everything, even when the coefficients are noncommutative. -/ lemma X_mul : X * p = p * X := begin rcases p, simp only [X, monomial, monomial_fun, mul_to_finsupp, linear_map.coe_mk], ext, simp [add_monoid_algebra.mul_apply, sum_single_index, add_comm], congr; ext; congr, end lemma X_pow_mul {n : ℕ} : X^n * p = p * X^n := begin induction n with n ih, { simp, }, { conv_lhs { rw pow_succ', }, rw [mul_assoc, X_mul, ←mul_assoc, ih, mul_assoc, ←pow_succ'], } end lemma X_pow_mul_assoc {n : ℕ} : (p * X^n) * q = (p * q) * X^n := by rw [mul_assoc, X_pow_mul, ←mul_assoc] lemma commute_X (p : polynomial R) : commute X p := X_mul lemma commute_X_pow (p : polynomial R) (n : ℕ) : commute (X ^ n) p := X_pow_mul @[simp] lemma monomial_mul_X (n : ℕ) (r : R) : monomial n r * X = monomial (n+1) r := by erw [monomial_mul_monomial, mul_one] @[simp] lemma monomial_mul_X_pow (n : ℕ) (r : R) (k : ℕ) : monomial n r * X^k = monomial (n+k) r := begin induction k with k ih, { simp, }, { simp [ih, pow_succ', ←mul_assoc, add_assoc], }, end @[simp] lemma X_mul_monomial (n : ℕ) (r : R) : X * monomial n r = monomial (n+1) r := by rw [X_mul, monomial_mul_X] @[simp] lemma X_pow_mul_monomial (k n : ℕ) (r : R) : X^k * monomial n r = monomial (n+k) r := by rw [X_pow_mul, monomial_mul_X_pow] /-- `coeff p n` (often denoted `p.coeff n`) is the coefficient of `X^n` in `p`. -/ def coeff : polynomial R → ℕ → R | ⟨p⟩ n := p n lemma coeff_monomial : coeff (monomial n a) m = if n = m then a else 0 := by { simp only [monomial, monomial_fun, coeff, linear_map.coe_mk], rw finsupp.single_apply } @[simp] lemma coeff_zero (n : ℕ) : coeff (0 : polynomial R) n = 0 := rfl @[simp] lemma coeff_one_zero : coeff (1 : polynomial R) 0 = 1 := by { rw [← monomial_zero_one, coeff_monomial], simp } @[simp] lemma coeff_X_one : coeff (X : polynomial R) 1 = 1 := coeff_monomial @[simp] lemma coeff_X_zero : coeff (X : polynomial R) 0 = 0 := coeff_monomial @[simp] lemma coeff_monomial_succ : coeff (monomial (n+1) a) 0 = 0 := by simp [coeff_monomial] lemma coeff_X : coeff (X : polynomial R) n = if 1 = n then 1 else 0 := coeff_monomial lemma coeff_X_of_ne_one {n : ℕ} (hn : n ≠ 1) : coeff (X : polynomial R) n = 0 := by rw [coeff_X, if_neg hn.symm] @[simp] lemma mem_support_iff : n ∈ p.support ↔ p.coeff n ≠ 0 := by { rcases p, simp [support, coeff] } lemma not_mem_support_iff : n ∉ p.support ↔ p.coeff n = 0 := by simp lemma coeff_C : coeff (C a) n = ite (n = 0) a 0 := by { convert coeff_monomial using 2, simp [eq_comm], } @[simp] lemma coeff_C_zero : coeff (C a) 0 = a := coeff_monomial lemma coeff_C_ne_zero (h : n ≠ 0) : (C a).coeff n = 0 := by rw [coeff_C, if_neg h] theorem nontrivial.of_polynomial_ne (h : p ≠ q) : nontrivial R := ⟨⟨0, 1, λ h01 : 0 = 1, h $ by rw [← mul_one p, ← mul_one q, ← C_1, ← h01, C_0, mul_zero, mul_zero] ⟩⟩ lemma monomial_eq_C_mul_X : ∀{n}, monomial n a = C a * X^n | 0 := (mul_one _).symm | (n+1) := calc monomial (n + 1) a = monomial n a * X : by { rw [X, monomial_mul_monomial, mul_one], } ... = (C a * X^n) * X : by rw [monomial_eq_C_mul_X] ... = C a * X^(n+1) : by simp only [pow_add, mul_assoc, pow_one] @[simp] lemma C_inj : C a = C b ↔ a = b := ⟨λ h, coeff_C_zero.symm.trans (h.symm ▸ coeff_C_zero), congr_arg C⟩ @[simp] lemma C_eq_zero : C a = 0 ↔ a = 0 := calc C a = 0 ↔ C a = C 0 : by rw C_0 ... ↔ a = 0 : C_inj theorem ext_iff {p q : polynomial R} : p = q ↔ ∀ n, coeff p n = coeff q n := by { rcases p, rcases q, simp [coeff, finsupp.ext_iff] } @[ext] lemma ext {p q : polynomial R} : (∀ n, coeff p n = coeff q n) → p = q := ext_iff.2 /-- Monomials generate the additive monoid of polynomials. -/ lemma add_submonoid_closure_set_of_eq_monomial : add_submonoid.closure {p : polynomial R | ∃ n a, p = monomial n a} = ⊤ := begin apply top_unique, rw [← add_submonoid.map_equiv_top (to_finsupp_iso R).symm.to_add_equiv, ← finsupp.add_closure_set_of_eq_single, add_monoid_hom.map_mclosure], refine add_submonoid.closure_mono (set.image_subset_iff.2 _), rintro _ ⟨n, a, rfl⟩, exact ⟨n, a, polynomial.to_finsupp_iso_symm_single⟩, end lemma add_hom_ext {M : Type*} [add_monoid M] {f g : polynomial R →+ M} (h : ∀ n a, f (monomial n a) = g (monomial n a)) : f = g := add_monoid_hom.eq_of_eq_on_mdense add_submonoid_closure_set_of_eq_monomial $ by { rintro p ⟨n, a, rfl⟩, exact h n a } @[ext] lemma add_hom_ext' {M : Type*} [add_monoid M] {f g : polynomial R →+ M} (h : ∀ n, f.comp (monomial n).to_add_monoid_hom = g.comp (monomial n).to_add_monoid_hom) : f = g := add_hom_ext (λ n, add_monoid_hom.congr_fun (h n)) @[ext] lemma lhom_ext' {M : Type*} [add_comm_monoid M] [module R M] {f g : polynomial R →ₗ[R] M} (h : ∀ n, f.comp (monomial n) = g.comp (monomial n)) : f = g := linear_map.to_add_monoid_hom_injective $ add_hom_ext $ λ n, linear_map.congr_fun (h n) -- this has the same content as the subsingleton lemma eq_zero_of_eq_zero (h : (0 : R) = (1 : R)) (p : polynomial R) : p = 0 := by rw [←one_smul R p, ←h, zero_smul] lemma support_monomial (n) (a : R) (H : a ≠ 0) : (monomial n a).support = singleton n := by simp [monomial, monomial_fun, support, finsupp.support_single_ne_zero H] lemma support_monomial' (n) (a : R) : (monomial n a).support ⊆ singleton n := by simp [monomial, monomial_fun, support, finsupp.support_single_subset] lemma X_pow_eq_monomial (n) : X ^ n = monomial n (1:R) := begin induction n with n hn, { rw [pow_zero, monomial_zero_one] }, { rw [pow_succ', hn, X, monomial_mul_monomial, one_mul] }, end lemma monomial_eq_smul_X {n} : monomial n (a : R) = a • X^n := calc monomial n a = monomial n (a * 1) : by simp ... = a • monomial n 1 : by simp [monomial, monomial_fun, smul_to_finsupp] ... = a • X^n : by rw X_pow_eq_monomial lemma support_X_pow (H : ¬ (1:R) = 0) (n : ℕ) : (X^n : polynomial R).support = singleton n := begin convert support_monomial n 1 H, exact X_pow_eq_monomial n, end lemma support_X_empty (H : (1:R)=0) : (X : polynomial R).support = ∅ := begin rw [X, H, monomial_zero_right, support_zero], end lemma support_X (H : ¬ (1 : R) = 0) : (X : polynomial R).support = singleton 1 := begin rw [← pow_one X, support_X_pow H 1], end lemma monomial_left_inj {R : Type*} [semiring R] {a : R} (ha : a ≠ 0) {i j : ℕ} : (monomial i a) = (monomial j a) ↔ i = j := by simp [monomial, monomial_fun, finsupp.single_left_inj ha] lemma nat_cast_mul {R : Type*} [semiring R] (n : ℕ) (p : polynomial R) : (n : polynomial R) * p = n • p := (nsmul_eq_mul _ _).symm /-- Summing the values of a function applied to the coefficients of a polynomial -/ def sum {S : Type*} [add_comm_monoid S] (p : polynomial R) (f : ℕ → R → S) : S := ∑ n in p.support, f n (p.coeff n) lemma sum_def {S : Type*} [add_comm_monoid S] (p : polynomial R) (f : ℕ → R → S) : p.sum f = ∑ n in p.support, f n (p.coeff n) := rfl lemma sum_eq_of_subset {S : Type*} [add_comm_monoid S] (p : polynomial R) (f : ℕ → R → S) (hf : ∀ i, f i 0 = 0) (s : finset ℕ) (hs : p.support ⊆ s) : p.sum f = ∑ n in s, f n (p.coeff n) := begin apply finset.sum_subset hs (λ n hn h'n, _), rw not_mem_support_iff at h'n, simp [h'n, hf] end /-- Expressing the product of two polynomials as a double sum. -/ lemma mul_eq_sum_sum : p * q = ∑ i in p.support, q.sum (λ j a, (monomial (i + j)) (p.coeff i * a)) := begin rcases p, rcases q, simp [mul_to_finsupp, support, monomial, sum, monomial_fun, coeff, sum_to_finsupp], refl end @[simp] lemma sum_zero_index {S : Type*} [add_comm_monoid S] (f : ℕ → R → S) : (0 : polynomial R).sum f = 0 := by simp [sum] @[simp] lemma sum_monomial_index {S : Type*} [add_comm_monoid S] (n : ℕ) (a : R) (f : ℕ → R → S) (hf : f n 0 = 0) : (monomial n a : polynomial R).sum f = f n a := begin by_cases h : a = 0, { simp [h, hf] }, { simp [sum, support_monomial, h, coeff_monomial] } end @[simp] lemma sum_C_index {a} {β} [add_comm_monoid β] {f : ℕ → R → β} (h : f 0 0 = 0) : (C a).sum f = f 0 a := sum_monomial_index 0 a f h -- the assumption `hf` is only necessary when the ring is trivial @[simp] lemma sum_X_index {S : Type*} [add_comm_monoid S] {f : ℕ → R → S} (hf : f 1 0 = 0) : (X : polynomial R).sum f = f 1 1 := sum_monomial_index 1 1 f hf lemma sum_add_index {S : Type*} [add_comm_monoid S] (p q : polynomial R) (f : ℕ → R → S) (hf : ∀ i, f i 0 = 0) (h_add : ∀a b₁ b₂, f a (b₁ + b₂) = f a b₁ + f a b₂) : (p + q).sum f = p.sum f + q.sum f := begin rcases p, rcases q, simp only [add_to_finsupp, sum, support, coeff, pi.add_apply, coe_add], exact finsupp.sum_add_index hf h_add, end lemma sum_add' {S : Type*} [add_comm_monoid S] (p : polynomial R) (f g : ℕ → R → S) : p.sum (f + g) = p.sum f + p.sum g := by simp [sum_def, finset.sum_add_distrib] lemma sum_add {S : Type*} [add_comm_monoid S] (p : polynomial R) (f g : ℕ → R → S) : p.sum (λ n x, f n x + g n x) = p.sum f + p.sum g := sum_add' _ _ _ lemma sum_smul_index {S : Type*} [add_comm_monoid S] (p : polynomial R) (b : R) (f : ℕ → R → S) (hf : ∀ i, f i 0 = 0) : (b • p).sum f = p.sum (λ n a, f n (b * a)) := begin rcases p, simp [smul_to_finsupp, sum, support, coeff], exact finsupp.sum_smul_index hf, end /-- `erase p n` is the polynomial `p` in which the `X^n` term has been erased. -/ @[irreducible] definition erase (n : ℕ) : polynomial R → polynomial R | ⟨p⟩ := ⟨p.erase n⟩ @[simp] lemma support_erase (p : polynomial R) (n : ℕ) : support (p.erase n) = (support p).erase n := by { rcases p, simp only [support, erase, support_erase] } lemma monomial_add_erase (p : polynomial R) (n : ℕ) : monomial n (coeff p n) + p.erase n = p := begin rcases p, simp [add_to_finsupp, monomial, monomial_fun, coeff, erase], exact finsupp.single_add_erase _ _ end lemma coeff_erase (p : polynomial R) (n i : ℕ) : (p.erase n).coeff i = if i = n then 0 else p.coeff i := begin rcases p, simp only [erase, coeff], convert rfl end @[simp] lemma erase_zero (n : ℕ) : (0 : polynomial R).erase n = 0 := by simp [← zero_to_finsupp, erase] @[simp] lemma erase_monomial {n : ℕ} {a : R} : erase n (monomial n a) = 0 := by simp [monomial, monomial_fun, erase, ← zero_to_finsupp] @[simp] lemma erase_same (p : polynomial R) (n : ℕ) : coeff (p.erase n) n = 0 := by simp [coeff_erase] @[simp] lemma erase_ne (p : polynomial R) (n i : ℕ) (h : i ≠ n) : coeff (p.erase n) i = coeff p i := by simp [coeff_erase, h] section update /-- Replace the coefficient of a `p : polynomial p` at a given degree `n : ℕ` by a given value `a : R`. If `a = 0`, this is equal to `p.erase n` If `p.nat_degree < n` and `a ≠ 0`, this increases the degree to `n`. -/ def update (p : polynomial R) (n : ℕ) (a : R) : polynomial R := polynomial.of_finsupp (p.to_finsupp.update n a) lemma coeff_update (p : polynomial R) (n : ℕ) (a : R) : (p.update n a).coeff = function.update p.coeff n a := begin ext, cases p, simp only [coeff, update, function.update_apply, coe_update], end lemma coeff_update_apply (p : polynomial R) (n : ℕ) (a : R) (i : ℕ) : (p.update n a).coeff i = if (i = n) then a else p.coeff i := by rw [coeff_update, function.update_apply] @[simp] lemma coeff_update_same (p : polynomial R) (n : ℕ) (a : R) : (p.update n a).coeff n = a := by rw [p.coeff_update_apply, if_pos rfl] lemma coeff_update_ne (p : polynomial R) {n : ℕ} (a : R) {i : ℕ} (h : i ≠ n) : (p.update n a).coeff i = p.coeff i := by rw [p.coeff_update_apply, if_neg h] @[simp] lemma update_zero_eq_erase (p : polynomial R) (n : ℕ) : p.update n 0 = p.erase n := by { ext, rw [coeff_update_apply, coeff_erase] } lemma support_update (p : polynomial R) (n : ℕ) (a : R) [decidable (a = 0)] : support (p.update n a) = if a = 0 then p.support.erase n else insert n p.support := by { cases p, simp only [support, update, support_update], congr } lemma support_update_zero (p : polynomial R) (n : ℕ) : support (p.update n 0) = p.support.erase n := by rw [update_zero_eq_erase, support_erase] lemma support_update_ne_zero (p : polynomial R) (n : ℕ) {a : R} (ha : a ≠ 0) : support (p.update n a) = insert n p.support := by classical; rw [support_update, if_neg ha] end update end semiring section comm_semiring variables [comm_semiring R] instance : comm_semiring (polynomial R) := { mul_comm := by { rintros ⟨⟩ ⟨⟩, simp [mul_to_finsupp, mul_comm] }, .. polynomial.semiring } end comm_semiring section ring variables [ring R] instance : ring (polynomial R) := { neg := has_neg.neg, add_left_neg := by { rintros ⟨⟩, simp [neg_to_finsupp, add_to_finsupp, ← zero_to_finsupp] }, zsmul := (•), zsmul_zero' := by { rintro ⟨⟩, simp [smul_to_finsupp, ← zero_to_finsupp] }, zsmul_succ' := by { rintros n ⟨⟩, simp [smul_to_finsupp, add_to_finsupp, add_smul, add_comm] }, zsmul_neg' := by { rintros n ⟨⟩, simp only [smul_to_finsupp, neg_to_finsupp], simp [add_smul, add_mul] }, .. polynomial.semiring } @[simp] lemma coeff_neg (p : polynomial R) (n : ℕ) : coeff (-p) n = -coeff p n := by { rcases p, simp [coeff, neg_to_finsupp] } @[simp] lemma coeff_sub (p q : polynomial R) (n : ℕ) : coeff (p - q) n = coeff p n - coeff q n := by { rcases p, rcases q, simp [coeff, sub_eq_add_neg, add_to_finsupp, neg_to_finsupp] } @[simp] lemma monomial_neg (n : ℕ) (a : R) : monomial n (-a) = -(monomial n a) := by rw [eq_neg_iff_add_eq_zero, ←monomial_add, neg_add_self, monomial_zero_right] @[simp] lemma support_neg {p : polynomial R} : (-p).support = p.support := by { rcases p, simp [support, neg_to_finsupp] } end ring instance [comm_ring R] : comm_ring (polynomial R) := { .. polynomial.comm_semiring, .. polynomial.ring } section nonzero_semiring variables [semiring R] [nontrivial R] instance : nontrivial (polynomial R) := begin have h : nontrivial (add_monoid_algebra R ℕ) := by apply_instance, rcases h.exists_pair_ne with ⟨x, y, hxy⟩, refine ⟨⟨⟨x⟩, ⟨y⟩, _⟩⟩, simp [hxy], end lemma X_ne_zero : (X : polynomial R) ≠ 0 := mt (congr_arg (λ p, coeff p 1)) (by simp) end nonzero_semiring section repr variables [semiring R] open_locale classical instance [has_repr R] : has_repr (polynomial R) := ⟨λ p, if p = 0 then "0" else (p.support.sort (≤)).foldr (λ n a, a ++ (if a = "" then "" else " + ") ++ if n = 0 then "C (" ++ repr (coeff p n) ++ ")" else if n = 1 then if (coeff p n) = 1 then "X" else "C (" ++ repr (coeff p n) ++ ") * X" else if (coeff p n) = 1 then "X ^ " ++ repr n else "C (" ++ repr (coeff p n) ++ ") * X ^ " ++ repr n) ""⟩ end repr end polynomial
a8b19f1fd0bdb435c700a15e6b97773d88f8d53e
26bff4ed296b8373c92b6b025f5d60cdf02104b9
/tests/lean/hott/cases.hlean
7e2b5268812f02d593081c6b54cab1418932c21d
[ "Apache-2.0" ]
permissive
guiquanz/lean
b8a878ea24f237b84b0e6f6be2f300e8bf028229
242f8ba0486860e53e257c443e965a82ee342db3
refs/heads/master
1,526,680,092,098
1,427,492,833,000
1,427,493,281,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
479
hlean
open nat inductive vec (A : Type) : nat → Type := | nil {} : vec A zero | cons : Π {n}, A → vec A n → vec A (succ n) namespace vec variables {A B C : Type} variables {n m : nat} notation a :: b := cons a b protected definition destruct (v : vec A (succ n)) {P : Π {n : nat}, vec A (succ n) → Type} (H : Π {n : nat} (h : A) (t : vec A n), P (h :: t)) : P v := begin cases v with (n', h', t'), apply (H h' t') end end vec
d7cd158d1871714f24266574e1a146e055ab0561
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/ring_theory/artinian.lean
9c7732e99860f7b16b75bf28430008c01a1aaaa1
[ "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
15,105
lean
/- Copyright (c) 2021 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import linear_algebra.basic import linear_algebra.prod import linear_algebra.pi import data.set_like.fintype import linear_algebra.linear_independent import tactic.linarith import algebra.algebra.basic import ring_theory.noetherian /-! # Artinian rings and modules A module satisfying these equivalent conditions is said to be an *Artinian* R-module if every decreasing chain of submodules is eventually constant, or equivalently, if the relation `<` on submodules is well founded. A ring is an *Artinian ring* if it is Artinian as a module over itself. (Note that we do not assume yet that our rings are commutative, so perhaps this should be called "left Artinian". To avoid cumbersome names once we specialize to the commutative case, we don't make this explicit in the declaration names.) ## Main definitions Let `R` be a ring and let `M` and `P` be `R`-modules. Let `N` be an `R`-submodule of `M`. * `is_artinian R M` is the proposition that `M` is a Artinian `R`-module. It is a class, implemented as the predicate that the `<` relation on submodules is well founded. ## References * [M. F. Atiyah and I. G. Macdonald, *Introduction to commutative algebra*][atiyah-macdonald] * [samuel] ## Tags Artinian, artinian, Artinian ring, Artinian module, artinian ring, artinian module -/ open set open_locale big_operators pointwise /-- `is_artinian R M` is the proposition that `M` is an Artinian `R`-module, implemented as the well-foundedness of submodule inclusion. -/ class is_artinian (R M) [semiring R] [add_comm_monoid M] [module R M] : Prop := (well_founded_submodule_lt [] : well_founded ((<) : submodule R M → submodule R M → Prop)) section variables {R : Type*} {M : Type*} {P : Type*} {N : Type*} variables [ring R] [add_comm_group M] [add_comm_group P] [add_comm_group N] variables [module R M] [module R P] [module R N] open is_artinian include R theorem is_artinian_of_injective (f : M →ₗ[R] P) (h : function.injective f) [is_artinian R P] : is_artinian R M := ⟨subrelation.wf (λ A B hAB, show A.map f < B.map f, from submodule.map_strict_mono_of_injective h hAB) (inv_image.wf (submodule.map f) (is_artinian.well_founded_submodule_lt R P))⟩ instance is_artinian_submodule' [is_artinian R M] (N : submodule R M) : is_artinian R N := is_artinian_of_injective N.subtype subtype.val_injective lemma is_artinian_of_le {s t : submodule R M} [ht : is_artinian R t] (h : s ≤ t) : is_artinian R s := is_artinian_of_injective (submodule.of_le h) (submodule.of_le_injective h) variable (M) theorem is_artinian_of_surjective (f : M →ₗ[R] P) (hf : function.surjective f) [is_artinian R M] : is_artinian R P := ⟨subrelation.wf (λ A B hAB, show A.comap f < B.comap f, from submodule.comap_strict_mono_of_surjective hf hAB) (inv_image.wf (submodule.comap f) (is_artinian.well_founded_submodule_lt _ _))⟩ variable {M} theorem is_artinian_of_linear_equiv (f : M ≃ₗ[R] P) [is_artinian R M] : is_artinian R P := is_artinian_of_surjective _ f.to_linear_map f.to_equiv.surjective theorem is_artinian_of_range_eq_ker [is_artinian R M] [is_artinian R P] (f : M →ₗ[R] N) (g : N →ₗ[R] P) (hf : function.injective f) (hg : function.surjective g) (h : f.range = g.ker) : is_artinian R N := ⟨well_founded_lt_exact_sequence (is_artinian.well_founded_submodule_lt _ _) (is_artinian.well_founded_submodule_lt _ _) f.range (submodule.map f) (submodule.comap f) (submodule.comap g) (submodule.map g) (submodule.gci_map_comap hf) (submodule.gi_map_comap hg) (by simp [submodule.map_comap_eq, inf_comm]) (by simp [submodule.comap_map_eq, h])⟩ instance is_artinian_prod [is_artinian R M] [is_artinian R P] : is_artinian R (M × P) := is_artinian_of_range_eq_ker (linear_map.inl R M P) (linear_map.snd R M P) linear_map.inl_injective linear_map.snd_surjective (linear_map.range_inl R M P) @[instance, priority 100] lemma is_artinian_of_fintype [fintype M] : is_artinian R M := ⟨fintype.well_founded_of_trans_of_irrefl _⟩ local attribute [elab_as_eliminator] fintype.induction_empty_option instance is_artinian_pi {R ι : Type*} [fintype ι] : Π {M : ι → Type*} [ring R] [Π i, add_comm_group (M i)], by exactI Π [Π i, module R (M i)], by exactI Π [∀ i, is_artinian R (M i)], is_artinian R (Π i, M i) := fintype.induction_empty_option (begin introsI α β e hα M _ _ _ _, exact is_artinian_of_linear_equiv (linear_equiv.Pi_congr_left R M e) end) (by { introsI M _ _ _ _, apply_instance }) (begin introsI α _ ih M _ _ _ _, exact is_artinian_of_linear_equiv (linear_equiv.pi_option_equiv_prod R).symm, end) ι /-- A version of `is_artinian_pi` for non-dependent functions. We need this instance because sometimes Lean fails to apply the dependent version in non-dependent settings (e.g., it fails to prove that `ι → ℝ` is finite dimensional over `ℝ`). -/ instance is_artinian_pi' {R ι M : Type*} [ring R] [add_comm_group M] [module R M] [fintype ι] [is_artinian R M] : is_artinian R (ι → M) := is_artinian_pi end open is_artinian submodule function section variables {R M : Type*} [ring R] [add_comm_group M] [module R M] theorem is_artinian_iff_well_founded : is_artinian R M ↔ well_founded ((<) : submodule R M → submodule R M → Prop) := ⟨λ h, h.1, is_artinian.mk⟩ variables {R M} lemma is_artinian.finite_of_linear_independent [nontrivial R] [is_artinian R M] {s : set M} (hs : linear_independent R (coe : s → M)) : s.finite := begin refine classical.by_contradiction (λ hf, (rel_embedding.well_founded_iff_no_descending_seq.1 (well_founded_submodule_lt R M)).elim' _), have f : ℕ ↪ s, from @infinite.nat_embedding s ⟨λ f, hf ⟨f⟩⟩, have : ∀ n, (coe ∘ f) '' {m | n ≤ m} ⊆ s, { rintros n x ⟨y, hy₁, hy₂⟩, subst hy₂, exact (f y).2 }, have : ∀ a b : ℕ, a ≤ b ↔ span R ((coe ∘ f) '' {m | b ≤ m}) ≤ span R ((coe ∘ f) '' {m | a ≤ m}), { assume a b, rw [span_le_span_iff hs (this b) (this a), set.image_subset_image_iff (subtype.coe_injective.comp f.injective), set.subset_def], simp only [set.mem_set_of_eq], exact ⟨λ hab x, le_trans hab, λ h, (h _ (le_refl _))⟩ }, exact ⟨⟨λ n, span R ((coe ∘ f) '' {m | n ≤ m}), λ x y, by simp [le_antisymm_iff, (this _ _).symm] {contextual := tt}⟩, begin intros a b, conv_rhs { rw [gt, lt_iff_le_not_le, this, this, ← lt_iff_le_not_le] }, simp end⟩ end /-- A module is Artinian iff every nonempty set of submodules has a minimal submodule among them. -/ theorem set_has_minimal_iff_artinian : (∀ a : set $ submodule R M, a.nonempty → ∃ M' ∈ a, ∀ I ∈ a, I ≤ M' → I = M') ↔ is_artinian R M := by rw [is_artinian_iff_well_founded, well_founded.well_founded_iff_has_min'] /-- A module is Noetherian iff every decreasing chain of submodules stabilizes. -/ theorem monotone_stabilizes_iff_artinian : (∀ (f : ℕ →ₘ order_dual (submodule R M)), ∃ n, ∀ m, n ≤ m → f n = f m) ↔ is_artinian R M := by rw [is_artinian_iff_well_founded]; exact (well_founded.monotone_chain_condition (order_dual (submodule R M))).symm /-- If `∀ I > J, P I` implies `P J`, then `P` holds for all submodules. -/ lemma is_artinian.induction [is_artinian R M] {P : submodule R M → Prop} (hgt : ∀ I, (∀ J < I, P J) → P I) (I : submodule R M) : P I := well_founded.recursion (well_founded_submodule_lt R M) I hgt /-- For any endomorphism of a Artinian module, there is some nontrivial iterate with disjoint kernel and range. -/ theorem is_artinian.exists_endomorphism_iterate_ker_sup_range_eq_top [I : is_artinian R M] (f : M →ₗ[R] M) : ∃ n : ℕ, n ≠ 0 ∧ (f ^ n).ker ⊔ (f ^ n).range = ⊤ := begin obtain ⟨n, w⟩ := monotone_stabilizes_iff_artinian.mpr I (f.iterate_range.comp ⟨λ n, n+1, λ n m w, by linarith⟩), specialize w ((n + 1) + n) (by linarith), dsimp at w, refine ⟨n + 1, nat.succ_ne_zero _, _⟩, simp_rw [eq_top_iff', mem_sup], intro x, have : (f^(n + 1)) x ∈ (f ^ ((n + 1) + n + 1)).range, { rw ← w, exact mem_range_self _ }, rcases this with ⟨y, hy⟩, use x - (f ^ (n+1)) y, split, { rw [linear_map.mem_ker, linear_map.map_sub, ← hy, sub_eq_zero, pow_add], simp [iterate_add_apply], }, { use (f^ (n+1)) y, simp } end /-- Any injective endomorphism of an Artinian module is surjective. -/ theorem is_artinian.surjective_of_injective_endomorphism [is_artinian R M] (f : M →ₗ[R] M) (s : injective f) : surjective f := begin obtain ⟨n, ne, w⟩ := is_artinian.exists_endomorphism_iterate_ker_sup_range_eq_top f, rw [linear_map.ker_eq_bot.mpr (linear_map.iterate_injective s n), bot_sup_eq, linear_map.range_eq_top] at w, exact linear_map.surjective_of_iterate_surjective ne w, end /-- Any injective endomorphism of an Artinian module is bijective. -/ theorem is_artinian.bijective_of_injective_endomorphism [is_artinian R M] (f : M →ₗ[R] M) (s : injective f) : bijective f := ⟨s, is_artinian.surjective_of_injective_endomorphism f s⟩ /-- A sequence `f` of submodules of a artinian module, with the supremum `f (n+1)` and the infinum of `f 0`, ..., `f n` being ⊤, is eventually ⊤. -/ lemma is_artinian.disjoint_partial_infs_eventually_top [I : is_artinian R M] (f : ℕ → submodule R M) (h : ∀ n, disjoint (partial_sups (order_dual.to_dual ∘ f) n) (order_dual.to_dual (f (n+1)))) : ∃ n : ℕ, ∀ m, n ≤ m → f m = ⊤ := begin -- A little off-by-one cleanup first: suffices t : ∃ n : ℕ, ∀ m, n ≤ m → order_dual.to_dual f (m+1) = ⊤, { obtain ⟨n, w⟩ := t, use n+1, rintros (_|m) p, { cases p, }, { apply w, exact nat.succ_le_succ_iff.mp p }, }, obtain ⟨n, w⟩ := monotone_stabilizes_iff_artinian.mpr I (partial_sups (order_dual.to_dual ∘ f)), exact ⟨n, (λ m p, eq_bot_of_disjoint_absorbs (h m) ((eq.symm (w (m + 1) (le_add_right p))).trans (w m p)))⟩ end universe w variables {N : Type w} [add_comm_group N] [module R N] -- TODO: Prove this for artinian modules -- /-- -- If `M ⊕ N` embeds into `M`, for `M` noetherian over `R`, then `N` is trivial. -- -/ -- noncomputable def is_noetherian.equiv_punit_of_prod_injective [is_noetherian R M] -- (f : M × N →ₗ[R] M) (i : injective f) : N ≃ₗ[R] punit.{w+1} := -- begin -- apply nonempty.some, -- obtain ⟨n, w⟩ := is_noetherian.disjoint_partial_sups_eventually_bot (f.tailing i) -- (f.tailings_disjoint_tailing i), -- specialize w n (le_refl n), -- apply nonempty.intro, -- refine (f.tailing_linear_equiv i n).symm.trans _, -- rw w, -- exact submodule.bot_equiv_punit, -- end end /-- A ring is Artinian if it is Artinian as a module over itself. -/ class is_artinian_ring (R) [ring R] extends is_artinian R R : Prop theorem is_artinian_ring_iff {R} [ring R] : is_artinian_ring R ↔ is_artinian R R := ⟨λ h, h.1, @is_artinian_ring.mk _ _⟩ theorem ring.is_artinian_of_zero_eq_one {R} [ring R] (h01 : (0 : R) = 1) : is_artinian_ring R := by haveI := subsingleton_of_zero_eq_one h01; haveI := fintype.of_subsingleton (0:R); split; apply_instance theorem is_artinian_of_submodule_of_artinian (R M) [ring R] [add_comm_group M] [module R M] (N : submodule R M) (h : is_artinian R M) : is_artinian R N := by apply_instance theorem is_artinian_of_quotient_of_artinian (R) [ring R] (M) [add_comm_group M] [module R M] (N : submodule R M) (h : is_artinian R M) : is_artinian R N.quotient := is_artinian_of_surjective M (submodule.mkq N) (submodule.quotient.mk_surjective N) /-- If `M / S / R` is a scalar tower, and `M / R` is Artinian, then `M / S` is also Artinian. -/ theorem is_artinian_of_tower (R) {S M} [comm_ring R] [ring S] [add_comm_group M] [algebra R S] [module S M] [module R M] [is_scalar_tower R S M] (h : is_artinian R M) : is_artinian S M := begin rw is_artinian_iff_well_founded at h ⊢, refine (submodule.restrict_scalars_embedding R S M).well_founded h end theorem is_artinian_of_fg_of_artinian {R M} [ring R] [add_comm_group M] [module R M] (N : submodule R M) [is_artinian_ring R] (hN : N.fg) : is_artinian R N := let ⟨s, hs⟩ := hN in begin haveI := classical.dec_eq M, haveI := classical.dec_eq R, letI : is_artinian R R := by apply_instance, have : ∀ x ∈ s, x ∈ N, from λ x hx, hs ▸ submodule.subset_span hx, refine @@is_artinian_of_surjective ((↑s : set M) → R) _ _ _ (pi.module _ _ _) _ _ _ is_artinian_pi, { fapply linear_map.mk, { exact λ f, ⟨∑ i in s.attach, f i • i.1, N.sum_mem (λ c _, N.smul_mem _ $ this _ c.2)⟩ }, { intros f g, apply subtype.eq, change ∑ i in s.attach, (f i + g i) • _ = _, simp only [add_smul, finset.sum_add_distrib], refl }, { intros c f, apply subtype.eq, change ∑ i in s.attach, (c • f i) • _ = _, simp only [smul_eq_mul, mul_smul], exact finset.smul_sum.symm } }, rintro ⟨n, hn⟩, change n ∈ N at hn, rw [← hs, ← set.image_id ↑s, finsupp.mem_span_image_iff_total] at hn, rcases hn with ⟨l, hl1, hl2⟩, refine ⟨λ x, l x, subtype.ext _⟩, change ∑ i in s.attach, l i • (i : M) = n, rw [@finset.sum_attach M M s _ (λ i, l i • i), ← hl2, finsupp.total_apply, finsupp.sum, eq_comm], refine finset.sum_subset hl1 (λ x _ hx, _), rw [finsupp.not_mem_support_iff.1 hx, zero_smul] end lemma is_artinian_of_fg_of_artinian' {R M} [ring R] [add_comm_group M] [module R M] [is_artinian_ring R] (h : (⊤ : submodule R M).fg) : is_artinian R M := have is_artinian R (⊤ : submodule R M), from is_artinian_of_fg_of_artinian _ h, by exactI is_artinian_of_linear_equiv (linear_equiv.of_top (⊤ : submodule R M) rfl) /-- In a module over a artinian ring, the submodule generated by finitely many vectors is artinian. -/ theorem is_artinian_span_of_finite (R) {M} [ring R] [add_comm_group M] [module R M] [is_artinian_ring R] {A : set M} (hA : finite A) : is_artinian R (submodule.span R A) := is_artinian_of_fg_of_artinian _ (submodule.fg_def.mpr ⟨A, hA, rfl⟩) theorem is_artinian_ring_of_surjective (R) [comm_ring R] (S) [comm_ring S] (f : R →+* S) (hf : function.surjective f) [H : is_artinian_ring R] : is_artinian_ring S := begin rw [is_artinian_ring_iff, is_artinian_iff_well_founded] at H ⊢, exact order_embedding.well_founded (ideal.order_embedding_of_surjective f hf) H, end instance is_artinian_ring_range {R} [comm_ring R] {S} [comm_ring S] (f : R →+* S) [is_artinian_ring R] : is_artinian_ring f.range := is_artinian_ring_of_surjective R f.range f.range_restrict f.range_restrict_surjective theorem is_artinian_ring_of_ring_equiv (R) [comm_ring R] {S} [comm_ring S] (f : R ≃+* S) [is_artinian_ring R] : is_artinian_ring S := is_artinian_ring_of_surjective R S f.to_ring_hom f.to_equiv.surjective
a3a66e94c30be90a516ade9a8cc9652843277dba
4727251e0cd73359b15b664c3170e5d754078599
/src/order/antisymmetrization.lean
4e8098fb5a811389dc2a8ad7be29aeec9e4249e8
[ "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
8,365
lean
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import order.hom.basic /-! # Turning a preorder into a partial order This file allows to make a preorder into a partial order by quotienting out the elements `a`, `b` such that `a ≤ b` and `b ≤ a`. `antisymmetrization` is a functor from `Preorder` to `PartialOrder`. See `Preorder_to_PartialOrder`. ## Main declarations * `antisymm_rel`: The antisymmetrization relation. `antisymm_rel r a b` means that `a` and `b` are related both ways by `r`. * `antisymmetrization α r`: The quotient of `α` by `antisymm_rel r`. Even when `r` is just a preorder, `antisymmetrization α` is a partial order. -/ open function order_dual variables {α β : Type*} section relation variables (r : α → α → Prop) /-- The antisymmetrization relation. -/ def antisymm_rel (a b : α) : Prop := r a b ∧ r b a lemma antisymm_rel_swap : antisymm_rel (swap r) = antisymm_rel r := funext $ λ _, funext $ λ _, propext and.comm @[refl] lemma antisymm_rel_refl [is_refl α r] (a : α) : antisymm_rel r a a := ⟨refl _, refl _⟩ variables {r} @[symm] lemma antisymm_rel.symm {a b : α} : antisymm_rel r a b → antisymm_rel r b a := and.symm @[trans] lemma antisymm_rel.trans [is_trans α r] {a b c : α} (hab : antisymm_rel r a b) (hbc : antisymm_rel r b c) : antisymm_rel r a c := ⟨trans hab.1 hbc.1, trans hbc.2 hab.2⟩ instance antisymm_rel.decidable_rel [decidable_rel r] : decidable_rel (antisymm_rel r) := λ _ _, and.decidable @[simp] lemma antisymm_rel_iff_eq [is_refl α r] [is_antisymm α r] {a b : α} : antisymm_rel r a b ↔ a = b := antisymm_iff alias antisymm_rel_iff_eq ↔ antisymm_rel.eq _ end relation section is_preorder variables (α) (r : α → α → Prop) [is_preorder α r] /-- The antisymmetrization relation as an equivalence relation. -/ @[simps] def antisymm_rel.setoid : setoid α := ⟨antisymm_rel r, antisymm_rel_refl _, λ _ _, antisymm_rel.symm, λ _ _ _, antisymm_rel.trans⟩ /-- The partial order derived from a preorder by making pairwise comparable elements equal. This is the quotient by `λ a b, a ≤ b ∧ b ≤ a`. -/ def antisymmetrization : Type* := quotient $ antisymm_rel.setoid α r variables {α} /-- Turn an element into its antisymmetrization. -/ def to_antisymmetrization : α → antisymmetrization α r := quotient.mk' /-- Get a representative from the antisymmetrization. -/ noncomputable def of_antisymmetrization : antisymmetrization α r → α := quotient.out' instance [inhabited α] : inhabited (antisymmetrization α r) := quotient.inhabited _ @[elab_as_eliminator] protected lemma antisymmetrization.ind {p : antisymmetrization α r → Prop} : (∀ a, p $ to_antisymmetrization r a) → ∀ q, p q := quot.ind @[elab_as_eliminator] protected lemma antisymmetrization.induction_on {p : antisymmetrization α r → Prop} (a : antisymmetrization α r) (h : ∀ a, p $ to_antisymmetrization r a) : p a := quotient.induction_on' a h @[simp] lemma to_antisymmetrization_of_antisymmetrization (a : antisymmetrization α r) : to_antisymmetrization r (of_antisymmetrization r a) = a := quotient.out_eq' _ end is_preorder section preorder variables {α} [preorder α] [preorder β] {a b : α} lemma antisymm_rel.image {a b : α} (h : antisymm_rel (≤) a b) {f : α → β} (hf : monotone f) : antisymm_rel (≤) (f a) (f b) := ⟨hf h.1, hf h.2⟩ instance : partial_order (antisymmetrization α (≤)) := { le := λ a b, quotient.lift_on₂' a b (≤) $ λ (a₁ a₂ b₁ b₂ : α) h₁ h₂, propext ⟨λ h, h₁.2.trans $ h.trans h₂.1, λ h, h₁.1.trans $ h.trans h₂.2⟩, lt := λ a b, quotient.lift_on₂' a b (<) $ λ (a₁ a₂ b₁ b₂ : α) h₁ h₂, propext ⟨λ h, h₁.2.trans_lt $ h.trans_le h₂.1, λ h, h₁.1.trans_lt $ h.trans_le h₂.2⟩, le_refl := λ a, quotient.induction_on' a $ le_refl, le_trans := λ a b c, quotient.induction_on₃' a b c $ λ a b c, le_trans, lt_iff_le_not_le := λ a b, quotient.induction_on₂' a b $ λ a b, lt_iff_le_not_le, le_antisymm := λ a b, quotient.induction_on₂' a b $ λ a b hab hba, quotient.sound' ⟨hab, hba⟩ } instance [@decidable_rel α (≤)] [@decidable_rel α (<)] [is_total α (≤)] : linear_order (antisymmetrization α (≤)) := { le_total := λ a b, quotient.induction_on₂' a b $ total_of (≤), decidable_eq := @quotient.decidable_eq _ (antisymm_rel.setoid _ (≤)) antisymm_rel.decidable_rel, decidable_le := λ _ _, quotient.lift_on₂'.decidable _ _ _ _, decidable_lt := λ _ _, quotient.lift_on₂'.decidable _ _ _ _, ..antisymmetrization.partial_order } @[simp] lemma to_antisymmetrization_le_to_antisymmetrization_iff : to_antisymmetrization (≤) a ≤ to_antisymmetrization (≤) b ↔ a ≤ b := iff.rfl @[simp] lemma to_antisymmetrization_lt_to_antisymmetrization_iff : to_antisymmetrization (≤) a < to_antisymmetrization (≤) b ↔ a < b := iff.rfl @[simp] lemma of_antisymmetrization_le_of_antisymmetrization_iff {a b : antisymmetrization α (≤)} : of_antisymmetrization (≤) a ≤ of_antisymmetrization (≤) b ↔ a ≤ b := by convert to_antisymmetrization_le_to_antisymmetrization_iff.symm; exact (to_antisymmetrization_of_antisymmetrization _ _).symm @[simp] lemma of_antisymmetrization_lt_of_antisymmetrization_iff {a b : antisymmetrization α (≤)} : of_antisymmetrization (≤) a < of_antisymmetrization (≤) b ↔ a < b := by convert to_antisymmetrization_lt_to_antisymmetrization_iff.symm; exact (to_antisymmetrization_of_antisymmetrization _ _).symm @[mono] lemma to_antisymmetrization_mono : monotone (@to_antisymmetrization α (≤) _) := λ a b, id /-- `to_antisymmetrization` as an order homomorphism. -/ @[simps] def order_hom.to_antisymmetrization : α →o antisymmetrization α (≤) := ⟨to_antisymmetrization (≤), λ a b, id⟩ private lemma lift_fun_antisymm_rel (f : α →o β) : ((antisymm_rel.setoid α (≤)).r ⇒ (antisymm_rel.setoid β (≤)).r) f f := λ a b h, ⟨f.mono h.1, f.mono h.2⟩ /-- Turns an order homomorphism from `α` to `β` into one from `antisymmetrization α` to `antisymmetrization β`. `antisymmetrization` is actually a functor. See `Preorder_to_PartialOrder`. -/ protected def order_hom.antisymmetrization (f : α →o β) : antisymmetrization α (≤) →o antisymmetrization β (≤) := ⟨quotient.map' f $ lift_fun_antisymm_rel f, λ a b, quotient.induction_on₂' a b $ f.mono⟩ @[simp] lemma order_hom.coe_antisymmetrization (f : α →o β) : ⇑f.antisymmetrization = quotient.map' f (lift_fun_antisymm_rel f) := rfl @[simp] lemma order_hom.antisymmetrization_apply (f : α →o β) (a : antisymmetrization α (≤)) : f.antisymmetrization a = quotient.map' f (lift_fun_antisymm_rel f) a := rfl @[simp] lemma order_hom.antisymmetrization_apply_mk (f : α →o β) (a : α) : f.antisymmetrization (to_antisymmetrization _ a) = (to_antisymmetrization _ (f a)) := quotient.map'_mk' f (lift_fun_antisymm_rel f) _ variables (α) /-- `of_antisymmetrization` as an order embedding. -/ @[simps] noncomputable def order_embedding.of_antisymmetrization : antisymmetrization α (≤) ↪o α := { to_fun := of_antisymmetrization _, inj' := λ _ _, quotient.out_inj.1, map_rel_iff' := λ a b, of_antisymmetrization_le_of_antisymmetrization_iff } /-- `antisymmetrization` and `order_dual` commute. -/ def order_iso.dual_antisymmetrization : (antisymmetrization α (≤))ᵒᵈ ≃o antisymmetrization αᵒᵈ (≤) := { to_fun := quotient.map' id $ λ _ _, and.symm, inv_fun := quotient.map' id $ λ _ _, and.symm, left_inv := λ a, quotient.induction_on' a $ λ a, by simp_rw [quotient.map'_mk', id], right_inv := λ a, quotient.induction_on' a $ λ a, by simp_rw [quotient.map'_mk', id], map_rel_iff' := λ a b, quotient.induction_on₂' a b $ λ a b, iff.rfl } @[simp] lemma order_iso.dual_antisymmetrization_apply (a : α) : order_iso.dual_antisymmetrization _ (to_dual $ to_antisymmetrization _ a) = to_antisymmetrization _ (to_dual a) := rfl @[simp] lemma order_iso.dual_antisymmetrization_symm_apply (a : α) : (order_iso.dual_antisymmetrization _).symm (to_antisymmetrization _ $ to_dual a) = to_dual (to_antisymmetrization _ a) := rfl end preorder
e6408155365b48bc3d46c54ee28ba9925b59fd78
1a61aba1b67cddccce19532a9596efe44be4285f
/library/init/reserved_notation.lean
6af3efffadae9ce9295b2df19262ad79a3c9a4ec
[ "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
2,268
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad -/ prelude import init.datatypes notation `assume` binders `,` r:(scoped f, f) := r notation `take` binders `,` r:(scoped f, f) := r /- Global declarations of right binding strength If a module reassigns these, it will be incompatible with other modules that adhere to these conventions. When hovering over a symbol, use "C-c C-k" to see how to input it. -/ definition std.prec.max : num := 1024 -- the strength of application, identifiers, (, [, etc. definition std.prec.arrow : num := 25 /- The next definition is "max + 10". It can be used e.g. for postfix operations that should be stronger than application. -/ definition std.prec.max_plus := num.succ (num.succ (num.succ (num.succ (num.succ (num.succ (num.succ (num.succ (num.succ (num.succ std.prec.max))))))))) /- Logical operations and relations -/ reserve prefix `¬`:40 reserve prefix `~`:40 reserve infixr `∧`:35 reserve infixr `/\`:35 reserve infixr `\/`:30 reserve infixr `∨`:30 reserve infix `<->`:20 reserve infix `↔`:20 reserve infix `=`:50 reserve infix `≠`:50 reserve infix `≈`:50 reserve infix `~`:50 reserve infix `≡`:50 reserve infixr `∘`:60 -- input with \comp reserve postfix `⁻¹`:std.prec.max_plus -- input with \sy or \-1 or \inv reserve infixl `⬝`:75 reserve infixr `▸`:75 reserve infixr `▹`:75 /- types and type constructors -/ reserve infixl `⊎`:25 reserve infixl `×`:30 /- arithmetic operations -/ reserve infixl `+`:65 reserve infixl `-`:65 reserve infixl `*`:70 reserve infixl `div`:70 reserve infixl `mod`:70 reserve infixl `/`:70 reserve prefix `-`:100 reserve infix `^`:80 reserve infix `<=`:50 reserve infix `≤`:50 reserve infix `<`:50 reserve infix `>=`:50 reserve infix `≥`:50 reserve infix `>`:50 /- boolean operations -/ reserve infixl `&&`:70 reserve infixl `||`:65 /- set operations -/ reserve infix `∈`:50 reserve infix `∉`:50 reserve infixl `∩`:70 reserve infixl `∪`:65 reserve infix `⊆`:50 reserve infix `⊇`:50 /- other symbols -/ reserve infix `∣`:50 reserve infixl `++`:65 reserve infixr `::`:65
2c7aad0a6514f727b98d4b35c320c2d7d2b4e9f7
9dc8cecdf3c4634764a18254e94d43da07142918
/src/topology/shrinking_lemma.lean
cc359edf4b3cedc3326c15434c2c121168529b29
[ "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,195
lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Reid Barton -/ import topology.separation /-! # The shrinking lemma In this file we prove a few versions of the shrinking lemma. The lemma says that in a normal topological space a point finite open covering can be “shrunk”: for a point finite open covering `u : ι → set X` there exists a refinement `v : ι → set X` such that `closure (v i) ⊆ u i`. For finite or countable coverings this lemma can be proved without the axiom of choice, see [ncatlab](https://ncatlab.org/nlab/show/shrinking+lemma) for details. We only formalize the most general result that works for any covering but needs the axiom of choice. We prove two versions of the lemma: * `exists_subset_Union_closure_subset` deals with a covering of a closed set in a normal space; * `exists_Union_eq_closure_subset` deals with a covering of the whole space. ## Tags normal space, shrinking lemma -/ open set function open_locale classical noncomputable theory variables {ι X : Type*} [topological_space X] [normal_space X] namespace shrinking_lemma /-- Auxiliary definition for the proof of `shrinking_lemma`. A partial refinement of a covering `⋃ i, u i` of a set `s` is a map `v : ι → set X` and a set `carrier : set ι` such that * `s ⊆ ⋃ i, v i`; * all `v i` are open; * if `i ∈ carrier v`, then `closure (v i) ⊆ u i`; * if `i ∉ carrier`, then `v i = u i`. This type is equipped with the folowing partial order: `v ≤ v'` if `v.carrier ⊆ v'.carrier` and `v i = v' i` for `i ∈ v.carrier`. We will use Zorn's lemma to prove that this type has a maximal element, then show that the maximal element must have `carrier = univ`. -/ @[nolint has_nonempty_instance] -- the trivial refinement needs `u` to be a covering structure partial_refinement (u : ι → set X) (s : set X) := (to_fun : ι → set X) (carrier : set ι) (is_open' : ∀ i, is_open (to_fun i)) (subset_Union' : s ⊆ ⋃ i, to_fun i) (closure_subset' : ∀ i ∈ carrier, closure (to_fun i) ⊆ (u i)) (apply_eq' : ∀ i ∉ carrier, to_fun i = u i) namespace partial_refinement variables {u : ι → set X} {s : set X} instance : has_coe_to_fun (partial_refinement u s) (λ _, ι → set X) := ⟨to_fun⟩ lemma subset_Union (v : partial_refinement u s) : s ⊆ ⋃ i, v i := v.subset_Union' lemma closure_subset (v : partial_refinement u s) {i : ι} (hi : i ∈ v.carrier) : closure (v i) ⊆ (u i) := v.closure_subset' i hi lemma apply_eq (v : partial_refinement u s) {i : ι} (hi : i ∉ v.carrier) : v i = u i := v.apply_eq' i hi protected lemma is_open (v : partial_refinement u s) (i : ι) : is_open (v i) := v.is_open' i protected lemma subset (v : partial_refinement u s) (i : ι) : v i ⊆ u i := if h : i ∈ v.carrier then subset.trans subset_closure (v.closure_subset h) else (v.apply_eq h).le attribute [ext] partial_refinement instance : partial_order (partial_refinement u s) := { le := λ v₁ v₂, v₁.carrier ⊆ v₂.carrier ∧ ∀ i ∈ v₁.carrier, v₁ i = v₂ i, le_refl := λ v, ⟨subset.refl _, λ _ _, rfl⟩, le_trans := λ v₁ v₂ v₃ h₁₂ h₂₃, ⟨subset.trans h₁₂.1 h₂₃.1, λ i hi, (h₁₂.2 i hi).trans (h₂₃.2 i $ h₁₂.1 hi)⟩, le_antisymm := λ v₁ v₂ h₁₂ h₂₁, have hc : v₁.carrier = v₂.carrier, from subset.antisymm h₁₂.1 h₂₁.1, ext _ _ (funext $ λ x, if hx : x ∈ v₁.carrier then h₁₂.2 _ hx else (v₁.apply_eq hx).trans (eq.symm $ v₂.apply_eq $ hc ▸ hx)) hc } /-- If two partial refinements `v₁`, `v₂` belong to a chain (hence, they are comparable) and `i` belongs to the carriers of both partial refinements, then `v₁ i = v₂ i`. -/ lemma apply_eq_of_chain {c : set (partial_refinement u s)} (hc : is_chain (≤) c) {v₁ v₂} (h₁ : v₁ ∈ c) (h₂ : v₂ ∈ c) {i} (hi₁ : i ∈ v₁.carrier) (hi₂ : i ∈ v₂.carrier) : v₁ i = v₂ i := begin wlog hle : v₁ ≤ v₂ := hc.total h₁ h₂ using [v₁ v₂, v₂ v₁], exact hle.2 _ hi₁, end /-- The carrier of the least upper bound of a non-empty chain of partial refinements is the union of their carriers. -/ def chain_Sup_carrier (c : set (partial_refinement u s)) : set ι := ⋃ v ∈ c, carrier v /-- Choice of an element of a nonempty chain of partial refinements. If `i` belongs to one of `carrier v`, `v ∈ c`, then `find c ne i` is one of these partial refinements. -/ def find (c : set (partial_refinement u s)) (ne : c.nonempty) (i : ι) : partial_refinement u s := if hi : ∃ v ∈ c, i ∈ carrier v then hi.some else ne.some lemma find_mem {c : set (partial_refinement u s)} (i : ι) (ne : c.nonempty) : find c ne i ∈ c := by { rw find, split_ifs, exacts [h.some_spec.fst, ne.some_spec] } lemma mem_find_carrier_iff {c : set (partial_refinement u s)} {i : ι} (ne : c.nonempty) : i ∈ (find c ne i).carrier ↔ i ∈ chain_Sup_carrier c := begin rw find, split_ifs, { have : i ∈ h.some.carrier ∧ i ∈ chain_Sup_carrier c, from ⟨h.some_spec.snd, mem_Union₂.2 h⟩, simp only [this] }, { have : i ∉ ne.some.carrier ∧ i ∉ chain_Sup_carrier c, from ⟨λ hi, h ⟨_, ne.some_spec, hi⟩, mt mem_Union₂.1 h⟩, simp only [this] } end lemma find_apply_of_mem {c : set (partial_refinement u s)} (hc : is_chain (≤) c) (ne : c.nonempty) {i v} (hv : v ∈ c) (hi : i ∈ carrier v) : find c ne i i = v i := apply_eq_of_chain hc (find_mem _ _) hv ((mem_find_carrier_iff _).2 $ mem_Union₂.2 ⟨v, hv, hi⟩) hi /-- Least upper bound of a nonempty chain of partial refinements. -/ def chain_Sup (c : set (partial_refinement u s)) (hc : is_chain (≤) c) (ne : c.nonempty) (hfin : ∀ x ∈ s, {i | x ∈ u i}.finite) (hU : s ⊆ ⋃ i, u i) : partial_refinement u s := begin refine ⟨λ i, find c ne i i, chain_Sup_carrier c, λ i, (find _ _ _).is_open i, λ x hxs, mem_Union.2 _, λ i hi, (find c ne i).closure_subset ((mem_find_carrier_iff _).2 hi), λ i hi, (find c ne i).apply_eq (mt (mem_find_carrier_iff _).1 hi)⟩, rcases em (∃ i ∉ chain_Sup_carrier c, x ∈ u i) with ⟨i, hi, hxi⟩|hx, { use i, rwa (find c ne i).apply_eq (mt (mem_find_carrier_iff _).1 hi) }, { simp_rw [not_exists, not_imp_not, chain_Sup_carrier, mem_Union₂] at hx, haveI : nonempty (partial_refinement u s) := ⟨ne.some⟩, choose! v hvc hiv using hx, rcases (hfin x hxs).exists_maximal_wrt v _ (mem_Union.1 (hU hxs)) with ⟨i, hxi : x ∈ u i, hmax : ∀ j, x ∈ u j → v i ≤ v j → v i = v j⟩, rcases mem_Union.1 ((v i).subset_Union hxs) with ⟨j, hj⟩, use j, have hj' : x ∈ u j := (v i).subset _ hj, have : v j ≤ v i, from (hc.total (hvc _ hxi) (hvc _ hj')).elim (λ h, (hmax j hj' h).ge) id, rwa find_apply_of_mem hc ne (hvc _ hxi) (this.1 $ hiv _ hj') } end /-- `chain_Sup hu c hc ne hfin hU` is an upper bound of the chain `c`. -/ lemma le_chain_Sup {c : set (partial_refinement u s)} (hc : is_chain (≤) c) (ne : c.nonempty) (hfin : ∀ x ∈ s, {i | x ∈ u i}.finite) (hU : s ⊆ ⋃ i, u i) {v} (hv : v ∈ c) : v ≤ chain_Sup c hc ne hfin hU := ⟨λ i hi, mem_bUnion hv hi, λ i hi, (find_apply_of_mem hc _ hv hi).symm⟩ /-- If `s` is a closed set, `v` is a partial refinement, and `i` is an index such that `i ∉ v.carrier`, then there exists a partial refinement that is strictly greater than `v`. -/ lemma exists_gt (v : partial_refinement u s) (hs : is_closed s) (i : ι) (hi : i ∉ v.carrier) : ∃ v' : partial_refinement u s, v < v' := begin have I : s ∩ (⋂ j ≠ i, (v j)ᶜ) ⊆ v i, { simp only [subset_def, mem_inter_eq, mem_Inter, and_imp], intros x hxs H, rcases mem_Union.1 (v.subset_Union hxs) with ⟨j, hj⟩, exact (em (j = i)).elim (λ h, h ▸ hj) (λ h, (H j h hj).elim) }, have C : is_closed (s ∩ (⋂ j ≠ i, (v j)ᶜ)), from is_closed.inter hs (is_closed_bInter $ λ _ _, is_closed_compl_iff.2 $ v.is_open _), rcases normal_exists_closure_subset C (v.is_open i) I with ⟨vi, ovi, hvi, cvi⟩, refine ⟨⟨update v i vi, insert i v.carrier, _, _, _, _⟩, _, _⟩, { intro j, by_cases h : j = i; simp [h, ovi, v.is_open] }, { refine λ x hx, mem_Union.2 _, rcases em (∃ j ≠ i, x ∈ v j) with ⟨j, hji, hj⟩|h, { use j, rwa update_noteq hji }, { push_neg at h, use i, rw update_same, exact hvi ⟨hx, mem_bInter h⟩ } }, { rintro j (rfl|hj), { rwa [update_same, ← v.apply_eq hi] }, { rw update_noteq (ne_of_mem_of_not_mem hj hi), exact v.closure_subset hj } }, { intros j hj, rw [mem_insert_iff, not_or_distrib] at hj, rw [update_noteq hj.1, v.apply_eq hj.2] }, { refine ⟨subset_insert _ _, λ j hj, _⟩, exact (update_noteq (ne_of_mem_of_not_mem hj hi) _ _).symm }, { exact λ hle, hi (hle.1 $ mem_insert _ _) } end end partial_refinement end shrinking_lemma open shrinking_lemma variables {u : ι → set X} {s : set X} /-- Shrinking lemma. A point-finite open cover of a closed subset of a normal space can be "shrunk" to a new open cover so that the closure of each new open set is contained in the corresponding original open set. -/ lemma exists_subset_Union_closure_subset (hs : is_closed s) (uo : ∀ i, is_open (u i)) (uf : ∀ x ∈ s, {i | x ∈ u i}.finite) (us : s ⊆ ⋃ i, u i) : ∃ v : ι → set X, s ⊆ Union v ∧ (∀ i, is_open (v i)) ∧ ∀ i, closure (v i) ⊆ u i := begin classical, haveI : nonempty (partial_refinement u s) := ⟨⟨u, ∅, uo, us, λ _, false.elim, λ _ _, rfl⟩⟩, have : ∀ c : set (partial_refinement u s), is_chain (≤) c → c.nonempty → ∃ ub, ∀ v ∈ c, v ≤ ub, from λ c hc ne, ⟨partial_refinement.chain_Sup c hc ne uf us, λ v hv, partial_refinement.le_chain_Sup _ _ _ _ hv⟩, rcases zorn_nonempty_partial_order this with ⟨v, hv⟩, suffices : ∀ i, i ∈ v.carrier, from ⟨v, v.subset_Union, λ i, v.is_open _, λ i, v.closure_subset (this i)⟩, contrapose! hv, rcases hv with ⟨i, hi⟩, rcases v.exists_gt hs i hi with ⟨v', hlt⟩, exact ⟨v', hlt.le, hlt.ne'⟩ end /-- Shrinking lemma. A point-finite open cover of a closed subset of a normal space can be "shrunk" to a new closed cover so that each new closed set is contained in the corresponding original open set. See also `exists_subset_Union_closure_subset` for a stronger statement. -/ lemma exists_subset_Union_closed_subset (hs : is_closed s) (uo : ∀ i, is_open (u i)) (uf : ∀ x ∈ s, {i | x ∈ u i}.finite) (us : s ⊆ ⋃ i, u i) : ∃ v : ι → set X, s ⊆ Union v ∧ (∀ i, is_closed (v i)) ∧ ∀ i, v i ⊆ u i := let ⟨v, hsv, hvo, hv⟩ := exists_subset_Union_closure_subset hs uo uf us in ⟨λ i, closure (v i), subset.trans hsv (Union_mono $ λ i, subset_closure), λ i, is_closed_closure, hv⟩ /-- Shrinking lemma. A point-finite open cover of a closed subset of a normal space can be "shrunk" to a new open cover so that the closure of each new open set is contained in the corresponding original open set. -/ lemma exists_Union_eq_closure_subset (uo : ∀ i, is_open (u i)) (uf : ∀ x, {i | x ∈ u i}.finite) (uU : (⋃ i, u i) = univ) : ∃ v : ι → set X, Union v = univ ∧ (∀ i, is_open (v i)) ∧ ∀ i, closure (v i) ⊆ u i := let ⟨v, vU, hv⟩ := exists_subset_Union_closure_subset is_closed_univ uo (λ x _, uf x) uU.ge in ⟨v, univ_subset_iff.1 vU, hv⟩ /-- Shrinking lemma. A point-finite open cover of a closed subset of a normal space can be "shrunk" to a new closed cover so that each of the new closed sets is contained in the corresponding original open set. See also `exists_Union_eq_closure_subset` for a stronger statement. -/ lemma exists_Union_eq_closed_subset (uo : ∀ i, is_open (u i)) (uf : ∀ x, {i | x ∈ u i}.finite) (uU : (⋃ i, u i) = univ) : ∃ v : ι → set X, Union v = univ ∧ (∀ i, is_closed (v i)) ∧ ∀ i, v i ⊆ u i := let ⟨v, vU, hv⟩ := exists_subset_Union_closed_subset is_closed_univ uo (λ x _, uf x) uU.ge in ⟨v, univ_subset_iff.1 vU, hv⟩
828d733344be737aec67b07cfcfaf9f7d397aaeb
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/1886.lean
d26d74ed7742029e4cb57c9c070f35d3db3df5ad
[ "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
105
lean
structure U (α : Type) where a : α theorem mk_inj (w : U.mk a = U.mk b) : a = b := by injection w
b95ba275374be28ffaa9ac69aac554ca0b22bac9
737dc4b96c97368cb66b925eeea3ab633ec3d702
/src/Lean/Meta/Tactic/Simp/Main.lean
b3020ea64b8cbd41818ad6cca77b6e73f73a2e9c
[ "Apache-2.0" ]
permissive
Bioye97/lean4
1ace34638efd9913dc5991443777b01a08983289
bc3900cbb9adda83eed7e6affeaade7cfd07716d
refs/heads/master
1,690,589,820,211
1,631,051,000,000
1,631,067,598,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
17,491
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.Transform import Lean.Meta.Tactic.Replace import Lean.Meta.Tactic.Util import Lean.Meta.Tactic.Clear import Lean.Meta.Tactic.Simp.Types import Lean.Meta.Tactic.Simp.Rewrite namespace Lean.Meta namespace Simp builtin_initialize congrHypothesisExceptionId : InternalExceptionId ← registerInternalExceptionId `congrHypothesisFailed def throwCongrHypothesisFailed : MetaM α := throw <| Exception.internal congrHypothesisExceptionId def Result.getProof (r : Result) : MetaM Expr := do match r.proof? with | some p => return p | none => mkEqRefl r.expr private def mkEqTrans (r₁ r₂ : Result) : MetaM Result := do match r₁.proof? with | none => return r₂ | some p₁ => match r₂.proof? with | none => return { r₂ with proof? := r₁.proof? } | some p₂ => return { r₂ with proof? := (← Meta.mkEqTrans p₁ p₂) } def mkCongrFun (r : Result) (a : Expr) : MetaM Result := match r.proof? with | none => return { expr := mkApp r.expr a, proof? := none } | some h => return { expr := mkApp r.expr a, proof? := (← Meta.mkCongrFun h a) } def mkCongr (r₁ r₂ : Result) : MetaM Result := let e := mkApp r₁.expr r₂.expr match r₁.proof?, r₂.proof? with | none, none => return { expr := e, proof? := none } | some h, none => return { expr := e, proof? := (← Meta.mkCongrFun h r₂.expr) } | none, some h => return { expr := e, proof? := (← Meta.mkCongrArg r₁.expr h) } | some h₁, some h₂ => return { expr := e, proof? := (← Meta.mkCongr h₁ h₂) } private def mkImpCongr (r₁ r₂ : Result) : MetaM Result := do let e ← mkArrow r₁.expr r₂.expr match r₁.proof?, r₂.proof? with | none, none => return { expr := e, proof? := none } | _, _ => return { expr := e, proof? := (← Meta.mkImpCongr (← r₁.getProof) (← r₂.getProof)) } -- TODO specialize if bootleneck private def reduceProj (e : Expr) : MetaM Expr := do match (← reduceProj? e) with | some e => return e | _ => return e private def reduceProjFn? (e : Expr) : SimpM (Option Expr) := do matchConst e.getAppFn (fun _ => pure none) fun cinfo _ => do match (← getProjectionFnInfo? cinfo.name) with | none => return none | some projInfo => if projInfo.fromClass then if (← read).simpLemmas.isDeclToUnfold cinfo.name then -- We only unfold class projections when the user explicitly requested them to be unfolded. -- Recall that `unfoldDefinition?` has support for unfolding this kind of projection. withReducibleAndInstances <| unfoldDefinition? e else return none else -- `structure` projection match (← unfoldDefinition? e) with | none => pure none | some e => match (← reduceProj? e.getAppFn) with | some f => return some (mkAppN f e.getAppArgs) | none => return none private def reduceFVar (cfg : Config) (e : Expr) : MetaM Expr := do if cfg.zeta then match (← getFVarLocalDecl e).value? with | some v => return v | none => return e else return e private def unfold? (e : Expr) : SimpM (Option Expr) := do let f := e.getAppFn if !f.isConst then return none let fName := f.constName! if (← isProjectionFn fName) then return none -- should be reduced by `reduceProjFn?` if (← read).simpLemmas.isDeclToUnfold e.getAppFn.constName! then withDefault <| unfoldDefinition? e else return none private partial def reduce (e : Expr) : SimpM Expr := withIncRecDepth do let cfg := (← read).config if cfg.beta then let e' := e.headBeta if e' != e then return (← reduce e') -- TODO: eta reduction if cfg.proj then match (← reduceProjFn? e) with | some e => return (← reduce e) | none => pure () if cfg.iota then match (← reduceRecMatcher? e) with | some e => return (← reduce e) | none => pure () match (← unfold? e) with | some e => reduce e | none => return e private partial def dsimp (e : Expr) : M Expr := do transform e (post := fun e => return TransformStep.done (← reduce e)) partial def simp (e : Expr) : M Result := withIncRecDepth do let cfg ← getConfig if (← isProof e) then return { expr := e } if cfg.memoize then if let some result := (← get).cache.find? e then return result simpLoop { expr := e } where simpLoop (r : Result) : M Result := do let cfg ← getConfig if (← get).numSteps > cfg.maxSteps then throwError "simp failed, maximum number of steps exceeded" else let init := r.expr modify fun s => { s with numSteps := s.numSteps + 1 } match (← pre r.expr) with | Step.done r => cacheResult cfg r | Step.visit r' => let r ← mkEqTrans r r' let r ← mkEqTrans r (← simpStep r.expr) match (← post r.expr) with | Step.done r' => cacheResult cfg (← mkEqTrans r r') | Step.visit r' => let r ← mkEqTrans r r' if cfg.singlePass || init == r.expr then cacheResult cfg r else simpLoop r simpStep (e : Expr) : M Result := do match e with | Expr.mdata _ e _ => simp e | Expr.proj .. => pure { expr := (← reduceProj e) } | Expr.app .. => simpApp e | Expr.lam .. => simpLambda e | Expr.forallE .. => simpForall e | Expr.letE .. => simpLet e | Expr.const .. => simpConst e | Expr.bvar .. => unreachable! | Expr.sort .. => pure { expr := e } | Expr.lit .. => pure { expr := e } | Expr.mvar .. => pure { expr := (← instantiateMVars e) } | Expr.fvar .. => pure { expr := (← reduceFVar (← getConfig) e) } congrDefault (e : Expr) : M Result := withParent e <| e.withApp fun f args => do let infos := (← getFunInfoNArgs f args.size).paramInfo let mut r ← simp f let mut i := 0 for arg in args do trace[Debug.Meta.Tactic.simp] "app [{i}] {infos.size} {arg} hasFwdDeps: {infos[i].hasFwdDeps}" if i < infos.size && !infos[i].hasFwdDeps then r ← mkCongr r (← simp arg) else if (← whnfD (← inferType r.expr)).isArrow then r ← mkCongr r (← simp arg) else r ← mkCongrFun r (← dsimp arg) i := i + 1 return r /- Return true iff processing the given congruence lemma hypothesis produced a non-refl proof. -/ processCongrHypothesis (h : Expr) : M Bool := do forallTelescopeReducing (← inferType h) fun xs hType => withNewLemmas xs do let lhs ← instantiateMVars hType.appFn!.appArg! let r ← simp lhs let rhs := hType.appArg! rhs.withApp fun m zs => do let val ← mkLambdaFVars zs r.expr unless (← isDefEq m val) do throwCongrHypothesisFailed unless (← isDefEq h (← mkLambdaFVars xs (← r.getProof))) do throwCongrHypothesisFailed return r.proof?.isSome /- Try to rewrite `e` children using the given congruence lemma -/ tryCongrLemma? (c : CongrLemma) (e : Expr) : M (Option Result) := withNewMCtxDepth do trace[Debug.Meta.Tactic.simp.congr] "{c.theoremName}, {e}" let lemma ← mkConstWithFreshMVarLevels c.theoremName let (xs, bis, type) ← forallMetaTelescopeReducing (← inferType lemma) if c.hypothesesPos.any (· ≥ xs.size) then return none let lhs := type.appFn!.appArg! let rhs := type.appArg! if (← isDefEq lhs e) then let mut modified := false for i in c.hypothesesPos do let x := xs[i] try if (← processCongrHypothesis x) then modified := true catch _ => trace[Meta.Tactic.simp.congr] "processCongrHypothesis {c.theoremName} failed {← inferType x}" return none unless modified do trace[Meta.Tactic.simp.congr] "{c.theoremName} not modified" return none unless (← synthesizeArgs c.theoremName xs bis (← read).discharge?) do trace[Meta.Tactic.simp.congr] "{c.theoremName} synthesizeArgs failed" return none let eNew ← instantiateMVars rhs let proof ← instantiateMVars (mkAppN lemma xs) return some { expr := eNew, proof? := proof } else return none congr (e : Expr) : M Result := do let f := e.getAppFn if f.isConst then let congrLemmas ← getCongrLemmas let cs := congrLemmas.get f.constName! for c in cs do match (← tryCongrLemma? c e) with | none => pure () | some r => return r congrDefault e else congrDefault e simpApp (e : Expr) : M Result := do let e ← reduce e if !e.isApp then simp e else congr e simpConst (e : Expr) : M Result := return { expr := (← reduce e) } withNewLemmas {α} (xs : Array Expr) (f : M α) : M α := do if (← getConfig).contextual then let mut s ← getSimpLemmas let mut updated := false for x in xs do if (← isProof x) then s ← s.add #[] x updated := true if updated then withSimpLemmas s f else f else f simpLambda (e : Expr) : M Result := withParent e <| lambdaTelescope e fun xs e => withNewLemmas xs do let r ← simp e let eNew ← mkLambdaFVars xs r.expr match r.proof? with | none => return { expr := eNew } | some h => let p ← xs.foldrM (init := h) fun x h => do mkFunExt (← mkLambdaFVars #[x] h) return { expr := eNew, proof? := p } simpArrow (e : Expr) : M Result := do trace[Debug.Meta.Tactic.simp] "arrow {e}" let p := e.bindingDomain! let q := e.bindingBody! let rp ← simp p trace[Debug.Meta.Tactic.simp] "arrow [{(← getConfig).contextual}] {p} [{← isProp p}] -> {q} [{← isProp q}]" if (← (← getConfig).contextual <&&> isProp p <&&> isProp q) then trace[Debug.Meta.Tactic.simp] "ctx arrow {rp.expr} -> {q}" withLocalDeclD e.bindingName! rp.expr fun h => do let s ← getSimpLemmas let s ← s.add #[] h withSimpLemmas s do let rq ← simp q match rq.proof? with | none => mkImpCongr rp rq | some hq => let hq ← mkLambdaFVars #[h] hq return { expr := (← mkArrow rp.expr rq.expr), proof? := (← mkImpCongrCtx (← rp.getProof) hq) } else mkImpCongr rp (← simp q) simpForall (e : Expr) : M Result := withParent e do trace[Debug.Meta.Tactic.simp] "forall {e}" if e.isArrow then simpArrow e else if (← isProp e) then withLocalDecl e.bindingName! e.bindingInfo! e.bindingDomain! fun x => withNewLemmas #[x] do let b := e.bindingBody!.instantiate1 x let rb ← simp b let eNew ← mkForallFVars #[x] rb.expr match rb.proof? with | none => return { expr := eNew } | some h => return { expr := eNew, proof? := (← mkForallCongr (← mkLambdaFVars #[x] h)) } else return { expr := (← dsimp e) } simpLet (e : Expr) : M Result := do if (← getConfig).zeta then match e with | Expr.letE _ _ v b _ => return { expr := b.instantiate1 v } | _ => unreachable! else -- TODO: simplify nondependent let-decls return { expr := (← dsimp e) } cacheResult (cfg : Config) (r : Result) : M Result := do if cfg.memoize then modify fun s => { s with cache := s.cache.insert e r } return r def main (e : Expr) (ctx : Context) (methods : Methods := {}) : MetaM Result := do withReducible do simp e methods ctx |>.run' {} abbrev Discharge := Expr → SimpM (Option Expr) namespace DefaultMethods mutual partial def discharge? (e : Expr) : SimpM (Option Expr) := do let ctx ← read if ctx.dischargeDepth >= ctx.config.maxDischargeDepth then trace[Meta.Tactic.simp.discharge] "maximum discharge depth has been reached" return none else withReader (fun ctx => { ctx with dischargeDepth := ctx.dischargeDepth + 1 }) do let r ← simp e methods if r.expr.isConstOf ``True then try return some (← mkOfEqTrue (← r.getProof)) catch _ => return none else return none partial def pre (e : Expr) : SimpM Step := preDefault e discharge? partial def post (e : Expr) : SimpM Step := postDefault e discharge? partial def methods : Methods := { pre := pre, post := post, discharge? := discharge? } end end DefaultMethods end Simp def simp (e : Expr) (ctx : Simp.Context) (discharge? : Option Simp.Discharge := none) : MetaM Simp.Result := do profileitM Exception "simp" (← getOptions) do match discharge? with | none => Simp.main e ctx (methods := Simp.DefaultMethods.methods) | some d => Simp.main e ctx (methods := { pre := (Simp.preDefault . d), post := (Simp.postDefault . d), discharge? := d }) /-- See `simpTarget`. This method assumes `mvarId` is not assigned, and we are already using `mvarId`s local context. -/ def simpTargetCore (mvarId : MVarId) (ctx : Simp.Context) (discharge? : Option Simp.Discharge := none) : MetaM (Option MVarId) := do let target ← instantiateMVars (← getMVarType mvarId) let r ← simp target ctx discharge? if r.expr.isConstOf ``True then match r.proof? with | some proof => assignExprMVar mvarId (← mkOfEqTrue proof) | none => assignExprMVar mvarId (mkConst ``True.intro) return none else match r.proof? with | some proof => replaceTargetEq mvarId r.expr proof | none => if target != r.expr then replaceTargetDefEq mvarId r.expr else return mvarId /-- Simplify the given goal target (aka type). Return `none` if the goal was closed. Return `some mvarId'` otherwise, where `mvarId'` is the simplified new goal. -/ def simpTarget (mvarId : MVarId) (ctx : Simp.Context) (discharge? : Option Simp.Discharge := none) : MetaM (Option MVarId) := withMVarContext mvarId do checkNotAssigned mvarId `simp simpTargetCore mvarId ctx discharge? /-- Simplify `prop` (which is inhabited by `proof`). Return `none` if the goal was closed. Return `some (proof', prop')` otherwise, where `proof' : prop'` and `prop'` is the simplified `prop`. This method assumes `mvarId` is not assigned, and we are already using `mvarId`s local context. -/ def simpStep (mvarId : MVarId) (proof : Expr) (prop : Expr) (ctx : Simp.Context) (discharge? : Option Simp.Discharge := none) : MetaM (Option (Expr × Expr)) := do let r ← simp prop ctx discharge? if r.expr.isConstOf ``False then match r.proof? with | some eqProof => assignExprMVar mvarId (← mkFalseElim (← getMVarType mvarId) (← mkEqMP eqProof proof)) | none => assignExprMVar mvarId (← mkFalseElim (← getMVarType mvarId) proof) return none else match r.proof? with | some eqProof => return some ((← mkEqMP eqProof proof), r.expr) | none => if r.expr != prop then return some ((← mkExpectedTypeHint proof r.expr), r.expr) else return some (proof, r.expr) def simpLocalDecl (mvarId : MVarId) (fvarId : FVarId) (ctx : Simp.Context) (discharge? : Option Simp.Discharge := none) : MetaM (Option (FVarId × MVarId)) := do withMVarContext mvarId do checkNotAssigned mvarId `simp let localDecl ← getLocalDecl fvarId let type ← instantiateMVars localDecl.type match (← simpStep mvarId (mkFVar fvarId) type ctx discharge?) with | none => return none | some (value, type') => if type != type' then let mvarId ← assert mvarId localDecl.userName type' value let mvarId ← tryClear mvarId localDecl.fvarId let (fvarId, mvarId) ← intro1P mvarId return some (fvarId, mvarId) else return some (fvarId, mvarId) abbrev FVarIdToLemmaId := FVarIdMap Name def simpGoal (mvarId : MVarId) (ctx : Simp.Context) (discharge? : Option Simp.Discharge := none) (simplifyTarget : Bool := true) (fvarIdsToSimp : Array FVarId := #[]) (fvarIdToLemmaId : FVarIdToLemmaId := {}) : MetaM (Option (Array FVarId × MVarId)) := do withMVarContext mvarId do checkNotAssigned mvarId `simp let mut mvarId := mvarId let mut toAssert : Array Hypothesis := #[] for fvarId in fvarIdsToSimp do let localDecl ← getLocalDecl fvarId let type ← instantiateMVars localDecl.type let ctx ← match fvarIdToLemmaId.find? localDecl.fvarId with | none => pure ctx | some lemmaId => pure { ctx with simpLemmas := (← ctx.simpLemmas.eraseCore lemmaId) } match (← simpStep mvarId (mkFVar fvarId) type ctx discharge?) with | none => return none | some (value, type) => toAssert := toAssert.push { userName := localDecl.userName, type := type, value := value } if simplifyTarget then match (← simpTarget mvarId ctx discharge?) with | none => return none | some mvarIdNew => mvarId := mvarIdNew let (fvarIdsNew, mvarIdNew) ← assertHypotheses mvarId toAssert let mvarIdNew ← tryClearMany mvarIdNew fvarIdsToSimp return (fvarIdsNew, mvarIdNew) end Lean.Meta
f534716104e9913aeb7ce4670228558c5225a905
9dc8cecdf3c4634764a18254e94d43da07142918
/src/analysis/convex/basic.lean
baea8f43976b95b79ab6f4a6b00b21f466c63680
[ "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
21,713
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, Yaël Dillies -/ import algebra.order.module import analysis.convex.star import linear_algebra.affine_space.affine_subspace /-! # Convex sets and functions in vector spaces In a 𝕜-vector space, we define the following objects and properties. * `convex 𝕜 s`: A set `s` is convex if for any two points `x y ∈ s` it includes `segment 𝕜 x y`. * `std_simplex 𝕜 ι`: The standard simplex in `ι → 𝕜` (currently requires `fintype ι`). It is the intersection of the positive quadrant with the hyperplane `s.sum = 1`. We also provide various equivalent versions of the definitions above, prove that some specific sets are convex. ## TODO Generalize all this file to affine spaces. -/ variables {𝕜 E F β : Type*} open linear_map set open_locale big_operators classical convex pointwise /-! ### Convexity of sets -/ section ordered_semiring variables [ordered_semiring 𝕜] section add_comm_monoid variables [add_comm_monoid E] [add_comm_monoid F] section has_smul variables (𝕜) [has_smul 𝕜 E] [has_smul 𝕜 F] (s : set E) {x : E} /-- Convexity of sets. -/ def convex : Prop := ∀ ⦃x : E⦄, x ∈ s → star_convex 𝕜 x s variables {𝕜 s} lemma convex.star_convex (hs : convex 𝕜 s) (hx : x ∈ s) : star_convex 𝕜 x s := hs hx lemma convex_iff_segment_subset : convex 𝕜 s ↔ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → [x -[𝕜] y] ⊆ s := forall₂_congr $ λ x hx, star_convex_iff_segment_subset 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 lemma convex.open_segment_subset (h : convex 𝕜 s) {x y : E} (hx : x ∈ s) (hy : y ∈ s) : open_segment 𝕜 x y ⊆ s := (open_segment_subset_segment 𝕜 x y).trans (h.segment_subset 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 rintro 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 hx y hy a b ha hb hab, (h ha hb hab) (set.add_mem_add ⟨_, hx, rfl⟩ ⟨_, hy, rfl⟩)) alias convex_iff_pointwise_add_subset ↔ convex.set_combo_subset _ lemma convex_empty : convex 𝕜 (∅ : set E) := λ x, false.elim lemma convex_univ : convex 𝕜 (set.univ : set E) := λ _ _, star_convex_univ _ lemma convex.inter {t : set E} (hs : convex 𝕜 s) (ht : convex 𝕜 t) : convex 𝕜 (s ∩ t) := λ x hx, (hs hx.1).inter (ht hx.2) lemma convex_sInter {S : set (set E)} (h : ∀ s ∈ S, convex 𝕜 s) : convex 𝕜 (⋂₀ S) := λ x hx, star_convex_sInter $ λ s hs, h _ hs $ hx _ hs 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_Inter₂ {ι : Sort*} {κ : ι → Sort*} {s : Π i, κ i → set E} (h : ∀ i j, convex 𝕜 (s i j)) : convex 𝕜 (⋂ i j, s i j) := convex_Inter $ λ i, convex_Inter $ h i lemma convex.prod {s : set E} {t : set F} (hs : convex 𝕜 s) (ht : convex 𝕜 t) : convex 𝕜 (s ×ˢ t) := λ x hx, (hs hx.1).prod (ht hx.2) lemma convex_pi {ι : Type*} {E : ι → Type*} [Π i, add_comm_monoid (E i)] [Π i, has_smul 𝕜 (E i)] {s : set ι} {t : Π i, set (E i)} (ht : ∀ ⦃i⦄, i ∈ s → convex 𝕜 (t i)) : convex 𝕜 (s.pi t) := λ x hx, star_convex_pi $ λ i hi, ht hi $ hx _ hi lemma directed.convex_Union {ι : Sort*} {s : ι → set E} (hdir : directed (⊆) s) (hc : ∀ ⦃i : ι⦄, convex 𝕜 (s i)) : convex 𝕜 (⋃ i, s i) := begin rintro x hx y hy a b ha hb hab, rw mem_Union at ⊢ hx hy, obtain ⟨i, hx⟩ := hx, obtain ⟨j, hy⟩ := hy, obtain ⟨k, hik, hjk⟩ := hdir i j, exact ⟨k, hc (hik hx) (hjk hy) ha hb hab⟩, end lemma directed_on.convex_sUnion {c : set (set E)} (hdir : directed_on (⊆) c) (hc : ∀ ⦃A : set E⦄, A ∈ c → convex 𝕜 A) : convex 𝕜 (⋃₀c) := begin rw sUnion_eq_Union, exact (directed_on_iff_directed.1 hdir).convex_Union (λ A, hc A.2), end end has_smul section module variables [module 𝕜 E] [module 𝕜 F] {s : set E} {x : E} lemma convex_iff_open_segment_subset : convex 𝕜 s ↔ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → open_segment 𝕜 x y ⊆ s := forall₂_congr $ λ x, star_convex_iff_open_segment_subset lemma convex_iff_forall_pos : convex 𝕜 s ↔ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → a • x + b • y ∈ s := forall₂_congr $ λ x, star_convex_iff_forall_pos lemma convex_iff_pairwise_pos : convex 𝕜 s ↔ s.pairwise (λ x y, ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → a • x + b • y ∈ s) := begin refine convex_iff_forall_pos.trans ⟨λ h x hx y hy _, h hx hy, _⟩, intros h x hx y hy a b ha hb hab, obtain rfl | hxy := eq_or_ne x y, { rwa convex.combo_self hab }, { exact h hx hy hxy ha hb hab }, end lemma convex.star_convex_iff (hs : convex 𝕜 s) (h : s.nonempty) : star_convex 𝕜 x s ↔ x ∈ s := ⟨λ hxs, hxs.mem h, hs.star_convex⟩ protected lemma set.subsingleton.convex {s : set E} (h : s.subsingleton) : convex 𝕜 s := convex_iff_pairwise_pos.mpr (h.pairwise _) lemma convex_singleton (c : E) : convex 𝕜 ({c} : set E) := subsingleton_singleton.convex lemma convex_segment (x y : E) : convex 𝕜 [x -[𝕜] y] := begin rintro p ⟨ap, bp, hap, hbp, habp, rfl⟩ q ⟨aq, bq, haq, hbq, habq, rfl⟩ a b ha hb hab, refine ⟨a * ap + b * aq, a * bp + b * bq, add_nonneg (mul_nonneg ha hap) (mul_nonneg hb haq), add_nonneg (mul_nonneg ha hbp) (mul_nonneg hb hbq), _, _⟩, { rw [add_add_add_comm, ←mul_add, ←mul_add, habp, habq, mul_one, mul_one, hab] }, { simp_rw [add_smul, mul_smul, smul_add], exact add_add_add_comm _ _ _ _ } end lemma convex_open_segment (a b : E) : convex 𝕜 (open_segment 𝕜 a b) := begin rw convex_iff_open_segment_subset, rintro p ⟨ap, bp, hap, hbp, habp, rfl⟩ q ⟨aq, bq, haq, hbq, habq, rfl⟩ z ⟨a, b, ha, hb, hab, rfl⟩, refine ⟨a * ap + b * aq, a * bp + b * bq, add_pos (mul_pos ha hap) (mul_pos hb haq), add_pos (mul_pos ha hbp) (mul_pos hb hbq), _, _⟩, { rw [add_add_add_comm, ←mul_add, ←mul_add, habp, habq, mul_one, mul_one, hab] }, { simp_rw [add_smul, mul_smul, smul_add], exact add_add_add_comm _ _ _ _ } end lemma convex.linear_image (hs : convex 𝕜 s) (f : E →ₗ[𝕜] F) : convex 𝕜 (f '' s) := begin intros x hx y hy a b ha hb hab, obtain ⟨x', hx', rfl⟩ := mem_image_iff_bex.1 hx, obtain ⟨y', hy', rfl⟩ := mem_image_iff_bex.1 hy, exact ⟨a • x' + b • y', hs hx' hy' ha hb hab, by rw [f.map_add, f.map_smul, f.map_smul]⟩, end 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 𝕜 (f ⁻¹' s) := begin intros x hx y hy a b ha hb hab, rw [mem_preimage, f.map_add, f.map_smul, f.map_smul], exact hs hx hy ha hb hab, end lemma convex.is_linear_preimage {s : set F} (hs : convex 𝕜 s) {f : E → F} (hf : is_linear_map 𝕜 f) : convex 𝕜 (f ⁻¹' s) := hs.linear_preimage $ hf.mk' f 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.vadd (hs : convex 𝕜 s) (z : E) : convex 𝕜 (z +ᵥ s) := by { simp_rw [←image_vadd, vadd_eq_add, ←singleton_add], exact (convex_singleton _).add hs } lemma convex.translate (hs : convex 𝕜 s) (z : E) : convex 𝕜 ((λ x, z + x) '' s) := hs.vadd _ /-- The translation of a convex set is also convex. -/ lemma convex.translate_preimage_right (hs : convex 𝕜 s) (z : E) : convex 𝕜 ((λ x, z + x) ⁻¹' s) := begin intros x hx y hy a b ha hb hab, have h := hs hx hy ha hb hab, rwa [smul_add, smul_add, add_add_add_comm, ←add_smul, hab, one_smul] at h, end /-- The translation of a convex set is also convex. -/ lemma convex.translate_preimage_left (hs : convex 𝕜 s) (z : E) : convex 𝕜 ((λ x, x + z) ⁻¹' s) := by simpa only [add_comm] using hs.translate_preimage_right z section ordered_add_comm_monoid variables [ordered_add_comm_monoid β] [module 𝕜 β] [ordered_smul 𝕜 β] lemma convex_Iic (r : β) : convex 𝕜 (Iic r) := λ x hx y hy a b ha hb hab, calc a • x + b • y ≤ a • r + b • r : add_le_add (smul_le_smul_of_nonneg hx ha) (smul_le_smul_of_nonneg hy hb) ... = r : convex.combo_self hab _ lemma convex_Ici (r : β) : convex 𝕜 (Ici r) := @convex_Iic 𝕜 βᵒᵈ _ _ _ _ r lemma convex_Icc (r s : β) : convex 𝕜 (Icc r s) := Ici_inter_Iic.subst ((convex_Ici r).inter $ convex_Iic s) 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_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 simp_rw le_antisymm_iff, exact (convex_halfspace_le h r).inter (convex_halfspace_ge h r), end end ordered_add_comm_monoid section ordered_cancel_add_comm_monoid variables [ordered_cancel_add_comm_monoid β] [module 𝕜 β] [ordered_smul 𝕜 β] lemma convex_Iio (r : β) : convex 𝕜 (Iio r) := begin intros x hx y hy a b ha hb hab, obtain rfl | ha' := ha.eq_or_lt, { rw zero_add at hab, rwa [zero_smul, zero_add, hab, one_smul] }, rw mem_Iio at hx hy, calc a • x + b • y < a • r + b • r : add_lt_add_of_lt_of_le (smul_lt_smul_of_pos hx ha') (smul_le_smul_of_nonneg hy.le hb) ... = r : convex.combo_self hab _ end lemma convex_Ioi (r : β) : convex 𝕜 (Ioi r) := @convex_Iio 𝕜 βᵒᵈ _ _ _ _ r lemma convex_Ioo (r s : β) : convex 𝕜 (Ioo r s) := Ioi_inter_Iio.subst ((convex_Ioi r).inter $ convex_Iio s) lemma convex_Ico (r s : β) : convex 𝕜 (Ico r s) := Ici_inter_Iio.subst ((convex_Ici r).inter $ convex_Iio s) lemma convex_Ioc (r s : β) : convex 𝕜 (Ioc r s) := Ioi_inter_Iic.subst ((convex_Ioi r).inter $ convex_Iic s) 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_gt {f : E → β} (h : is_linear_map 𝕜 f) (r : β) : convex 𝕜 {w | r < f w} := (convex_Ioi r).is_linear_preimage h end ordered_cancel_add_comm_monoid section linear_ordered_add_comm_monoid variables [linear_ordered_add_comm_monoid β] [module 𝕜 β] [ordered_smul 𝕜 β] lemma convex_interval (r s : β) : convex 𝕜 (interval r s) := convex_Icc _ _ end linear_ordered_add_comm_monoid end module end add_comm_monoid section linear_ordered_add_comm_monoid variables [linear_ordered_add_comm_monoid E] [ordered_add_comm_monoid β] [module 𝕜 E] [ordered_smul 𝕜 E] {s : set E} {f : E → β} lemma monotone_on.convex_le (hf : monotone_on f s) (hs : convex 𝕜 s) (r : β) : convex 𝕜 {x ∈ s | f x ≤ r} := λ x hx y hy a b ha hb hab, ⟨hs hx.1 hy.1 ha hb hab, (hf (hs hx.1 hy.1 ha hb hab) (max_rec' s hx.1 hy.1) (convex.combo_le_max x y ha hb hab)).trans (max_rec' _ hx.2 hy.2)⟩ lemma monotone_on.convex_lt (hf : monotone_on f s) (hs : convex 𝕜 s) (r : β) : convex 𝕜 {x ∈ s | f x < r} := λ x hx y hy a b ha hb hab, ⟨hs hx.1 hy.1 ha hb hab, (hf (hs hx.1 hy.1 ha hb hab) (max_rec' s hx.1 hy.1) (convex.combo_le_max x y ha hb hab)).trans_lt (max_rec' _ hx.2 hy.2)⟩ lemma monotone_on.convex_ge (hf : monotone_on f s) (hs : convex 𝕜 s) (r : β) : convex 𝕜 {x ∈ s | r ≤ f x} := @monotone_on.convex_le 𝕜 Eᵒᵈ βᵒᵈ _ _ _ _ _ _ _ hf.dual hs r lemma monotone_on.convex_gt (hf : monotone_on f s) (hs : convex 𝕜 s) (r : β) : convex 𝕜 {x ∈ s | r < f x} := @monotone_on.convex_lt 𝕜 Eᵒᵈ βᵒᵈ _ _ _ _ _ _ _ hf.dual hs r lemma antitone_on.convex_le (hf : antitone_on f s) (hs : convex 𝕜 s) (r : β) : convex 𝕜 {x ∈ s | f x ≤ r} := @monotone_on.convex_ge 𝕜 E βᵒᵈ _ _ _ _ _ _ _ hf hs r lemma antitone_on.convex_lt (hf : antitone_on f s) (hs : convex 𝕜 s) (r : β) : convex 𝕜 {x ∈ s | f x < r} := @monotone_on.convex_gt 𝕜 E βᵒᵈ _ _ _ _ _ _ _ hf hs r lemma antitone_on.convex_ge (hf : antitone_on f s) (hs : convex 𝕜 s) (r : β) : convex 𝕜 {x ∈ s | r ≤ f x} := @monotone_on.convex_le 𝕜 E βᵒᵈ _ _ _ _ _ _ _ hf hs r lemma antitone_on.convex_gt (hf : antitone_on f s) (hs : convex 𝕜 s) (r : β) : convex 𝕜 {x ∈ s | r < f x} := @monotone_on.convex_lt 𝕜 E βᵒᵈ _ _ _ _ _ _ _ hf hs r lemma monotone.convex_le (hf : monotone f) (r : β) : convex 𝕜 {x | f x ≤ r} := set.sep_univ.subst ((hf.monotone_on univ).convex_le convex_univ r) lemma monotone.convex_lt (hf : monotone f) (r : β) : convex 𝕜 {x | f x ≤ r} := set.sep_univ.subst ((hf.monotone_on univ).convex_le convex_univ r) lemma monotone.convex_ge (hf : monotone f ) (r : β) : convex 𝕜 {x | r ≤ f x} := set.sep_univ.subst ((hf.monotone_on univ).convex_ge convex_univ r) lemma monotone.convex_gt (hf : monotone f) (r : β) : convex 𝕜 {x | f x ≤ r} := set.sep_univ.subst ((hf.monotone_on univ).convex_le convex_univ r) lemma antitone.convex_le (hf : antitone f) (r : β) : convex 𝕜 {x | f x ≤ r} := set.sep_univ.subst ((hf.antitone_on univ).convex_le convex_univ r) lemma antitone.convex_lt (hf : antitone f) (r : β) : convex 𝕜 {x | f x < r} := set.sep_univ.subst ((hf.antitone_on univ).convex_lt convex_univ r) lemma antitone.convex_ge (hf : antitone f) (r : β) : convex 𝕜 {x | r ≤ f x} := set.sep_univ.subst ((hf.antitone_on univ).convex_ge convex_univ r) lemma antitone.convex_gt (hf : antitone f) (r : β) : convex 𝕜 {x | r < f x} := set.sep_univ.subst ((hf.antitone_on univ).convex_gt convex_univ r) end linear_ordered_add_comm_monoid end ordered_semiring section ordered_comm_semiring variables [ordered_comm_semiring 𝕜] section add_comm_monoid variables [add_comm_monoid E] [add_comm_monoid F] [module 𝕜 E] [module 𝕜 F] {s : set E} lemma convex.smul (hs : convex 𝕜 s) (c : 𝕜) : convex 𝕜 (c • s) := hs.linear_image (linear_map.lsmul _ _ c) lemma convex.smul_preimage (hs : convex 𝕜 s) (c : 𝕜) : convex 𝕜 ((λ z, c • z) ⁻¹' s) := hs.linear_preimage (linear_map.lsmul _ _ c) lemma convex.affinity (hs : convex 𝕜 s) (z : E) (c : 𝕜) : convex 𝕜 ((λ x, z + c • x) '' s) := by simpa only [←image_smul, ←image_vadd, image_image] using (hs.smul c).vadd z end add_comm_monoid end ordered_comm_semiring section ordered_ring variables [ordered_ring 𝕜] section add_comm_group variables [add_comm_group E] [add_comm_group F] [module 𝕜 E] [module 𝕜 F] {s t : set E} lemma convex.add_smul_mem (hs : convex 𝕜 s) {x y : E} (hx : x ∈ s) (hy : x + y ∈ s) {t : 𝕜} (ht : t ∈ Icc (0 : 𝕜) 1) : x + t • y ∈ s := begin have h : x + t • y = (1 - t) • x + t • (x + y), { rw [smul_add, ←add_assoc, ←add_smul, sub_add_cancel, one_smul] }, rw h, exact hs hx hy (sub_nonneg_of_le ht.2) ht.1 (sub_add_cancel _ _), end lemma convex.smul_mem_of_zero_mem (hs : convex 𝕜 s) {x : E} (zero_mem : (0 : E) ∈ s) (hx : x ∈ s) {t : 𝕜} (ht : t ∈ Icc (0 : 𝕜) 1) : t • x ∈ s := by simpa using hs.add_smul_mem zero_mem (by simpa using hx) ht lemma convex.add_smul_sub_mem (h : convex 𝕜 s) {x y : E} (hx : x ∈ s) (hy : y ∈ s) {t : 𝕜} (ht : t ∈ Icc (0 : 𝕜) 1) : x + t • (y - x) ∈ s := begin apply h.segment_subset hx hy, rw segment_eq_image', exact mem_image_of_mem _ ht, end /-- Affine subspaces are convex. -/ lemma affine_subspace.convex (Q : affine_subspace 𝕜 E) : convex 𝕜 (Q : set E) := begin intros x hx y hy a b ha hb hab, rw [eq_sub_of_add_eq hab, ← affine_map.line_map_apply_module], exact affine_map.line_map_mem b hx hy, 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) := λ x hx, (hs hx).affine_preimage _ /-- The image of a convex set under an affine map is convex. -/ lemma convex.affine_image (f : E →ᵃ[𝕜] F) (hs : convex 𝕜 s) : convex 𝕜 (f '' s) := by { rintro _ ⟨x, hx, rfl⟩, exact (hs hx).affine_image _ } lemma convex.neg (hs : convex 𝕜 s) : convex 𝕜 (-s) := hs.is_linear_preimage is_linear_map.is_linear_map_neg lemma convex.sub (hs : convex 𝕜 s) (ht : convex 𝕜 t) : convex 𝕜 (s - t) := by { rw sub_eq_add_neg, exact hs.add ht.neg } end add_comm_group end ordered_ring section linear_ordered_field variables [linear_ordered_field 𝕜] section add_comm_group variables [add_comm_group E] [add_comm_group F] [module 𝕜 E] [module 𝕜 F] {s : set E} /-- Alternative definition of set convexity, using division. -/ lemma convex_iff_div : convex 𝕜 s ↔ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 ≤ a → 0 ≤ b → 0 < a + b → (a / (a + b)) • x + (b / (a + b)) • y ∈ s := forall₂_congr $ λ x hx, star_convex_iff_div lemma convex.mem_smul_of_zero_mem (h : convex 𝕜 s) {x : E} (zero_mem : (0 : E) ∈ s) (hx : x ∈ s) {t : 𝕜} (ht : 1 ≤ t) : x ∈ t • s := begin rw mem_smul_set_iff_inv_smul_mem₀ (zero_lt_one.trans_le ht).ne', exact h.smul_mem_of_zero_mem zero_mem hx ⟨inv_nonneg.2 (zero_le_one.trans ht), inv_le_one ht⟩, end lemma convex.add_smul (h_conv : convex 𝕜 s) {p q : 𝕜} (hp : 0 ≤ p) (hq : 0 ≤ q) : (p + q) • s = p • s + q • s := begin obtain rfl | hs := s.eq_empty_or_nonempty, { simp_rw [smul_set_empty, add_empty] }, obtain rfl | hp' := hp.eq_or_lt, { rw [zero_add, zero_smul_set hs, zero_add] }, obtain rfl | hq' := hq.eq_or_lt, { rw [add_zero, zero_smul_set hs, add_zero] }, ext, split, { rintro ⟨v, hv, rfl⟩, exact ⟨p • v, q • v, smul_mem_smul_set hv, smul_mem_smul_set hv, (add_smul _ _ _).symm⟩ }, { rintro ⟨v₁, v₂, ⟨v₁₁, h₁₂, rfl⟩, ⟨v₂₁, h₂₂, rfl⟩, rfl⟩, have hpq := add_pos hp' hq', refine mem_smul_set.2 ⟨_, h_conv h₁₂ h₂₂ _ _ (by rw [←div_self hpq.ne', add_div] : p / (p + q) + q / (p + q) = 1), by simp only [← mul_smul, smul_add, mul_div_cancel' _ hpq.ne']⟩; positivity } end end add_comm_group end linear_ordered_field /-! #### Convex sets in an ordered space Relates `convex` and `ord_connected`. -/ section lemma set.ord_connected.convex_of_chain [ordered_semiring 𝕜] [ordered_add_comm_monoid E] [module 𝕜 E] [ordered_smul 𝕜 E] {s : set E} (hs : s.ord_connected) (h : is_chain (≤) s) : convex 𝕜 s := begin refine convex_iff_segment_subset.mpr (λ x hx y hy, _), obtain hxy | hyx := h.total hx hy, { exact (segment_subset_Icc hxy).trans (hs.out hx hy) }, { rw segment_symm, exact (segment_subset_Icc hyx).trans (hs.out hy hx) } end lemma set.ord_connected.convex [ordered_semiring 𝕜] [linear_ordered_add_comm_monoid E] [module 𝕜 E] [ordered_smul 𝕜 E] {s : set E} (hs : s.ord_connected) : convex 𝕜 s := hs.convex_of_chain $ is_chain_of_trichotomous s lemma convex_iff_ord_connected [linear_ordered_field 𝕜] {s : set 𝕜} : convex 𝕜 s ↔ s.ord_connected := by simp_rw [convex_iff_segment_subset, segment_eq_interval, ord_connected_iff_interval_subset] alias convex_iff_ord_connected ↔ convex.ord_connected _ end /-! #### Convexity of submodules/subspaces -/ namespace submodule variables [ordered_semiring 𝕜] [add_comm_monoid E] [module 𝕜 E] protected lemma convex (K : submodule 𝕜 E) : convex 𝕜 (↑K : set E) := by { repeat {intro}, refine add_mem (smul_mem _ _ _) (smul_mem _ _ _); assumption } protected lemma star_convex (K : submodule 𝕜 E) : star_convex 𝕜 (0 : E) K := K.convex K.zero_mem end submodule /-! ### Simplex -/ section simplex variables (𝕜) (ι : Type*) [ordered_semiring 𝕜] [fintype ι] /-- The standard simplex in the space of functions `ι → 𝕜` is the set of vectors with non-negative coordinates with total sum `1`. This is the free object in the category of convex spaces. -/ def std_simplex : 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 hf g 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 _)]⟩ end simplex
7b4ddd75e86066620c0dec6a87c3b5d143432b95
4727251e0cd73359b15b664c3170e5d754078599
/src/data/finset/prod.lean
9b4c0756134960f798108d6d4d5a1c501537fda4
[ "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
10,292
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Oliver Nash -/ import data.finset.card /-! # Finsets in product types This file defines finset constructions on the product type `α × β`. Beware not to confuse with the `finset.prod` operation which computes the multiplicative product. ## Main declarations * `finset.product`: Turns `s : finset α`, `t : finset β` into their product in `finset (α × β)`. * `finset.diag`: For `s : finset α`, `s.diag` is the `finset (α × α)` of pairs `(a, a)` with `a ∈ s`. * `finset.off_diag`: For `s : finset α`, `s.off_diag` is the `finset (α × α)` of pairs `(a, b)` with `a, b ∈ s` and `a ≠ b`. -/ open multiset variables {α β γ : Type*} namespace finset /-! ### prod -/ section prod variables {s s' : finset α} {t t' : finset β} {a : α} {b : β} /-- `product s t` is the set of pairs `(a, b)` such that `a ∈ s` and `b ∈ t`. -/ protected def product (s : finset α) (t : finset β) : finset (α × β) := ⟨_, s.nodup.product t.nodup⟩ @[simp] lemma product_val : (s.product t).1 = s.1.product t.1 := rfl @[simp] lemma mem_product {p : α × β} : p ∈ s.product t ↔ p.1 ∈ s ∧ p.2 ∈ t := mem_product lemma mk_mem_product (ha : a ∈ s) (hb : b ∈ t) : (a, b) ∈ s.product t := mem_product.2 ⟨ha, hb⟩ @[simp, norm_cast] lemma coe_product (s : finset α) (t : finset β) : (s.product t : set (α × β)) = (s : set α) ×ˢ (t : set β) := set.ext $ λ x, finset.mem_product lemma subset_product [decidable_eq α] [decidable_eq β] {s : finset (α × β)} : s ⊆ (s.image prod.fst).product (s.image prod.snd) := λ p hp, mem_product.2 ⟨mem_image_of_mem _ hp, mem_image_of_mem _ hp⟩ lemma product_subset_product (hs : s ⊆ s') (ht : t ⊆ t') : s.product t ⊆ s'.product t' := λ ⟨x,y⟩ h, mem_product.2 ⟨hs (mem_product.1 h).1, ht (mem_product.1 h).2⟩ lemma product_subset_product_left (hs : s ⊆ s') : s.product t ⊆ s'.product t := product_subset_product hs (subset.refl _) lemma product_subset_product_right (ht : t ⊆ t') : s.product t ⊆ s.product t' := product_subset_product (subset.refl _) ht lemma product_eq_bUnion [decidable_eq α] [decidable_eq β] (s : finset α) (t : finset β) : s.product t = s.bUnion (λa, t.image $ λb, (a, b)) := ext $ λ ⟨x, y⟩, by simp only [mem_product, mem_bUnion, mem_image, exists_prop, prod.mk.inj_iff, and.left_comm, exists_and_distrib_left, exists_eq_right, exists_eq_left] lemma product_eq_bUnion_right [decidable_eq α] [decidable_eq β] (s : finset α) (t : finset β) : s.product t = t.bUnion (λ b, s.image $ λ a, (a, b)) := ext $ λ ⟨x, y⟩, by simp only [mem_product, mem_bUnion, mem_image, exists_prop, prod.mk.inj_iff, and.left_comm, exists_and_distrib_left, exists_eq_right, exists_eq_left] /-- See also `finset.sup_product_left`. -/ @[simp] lemma product_bUnion [decidable_eq γ] (s : finset α) (t : finset β) (f : α × β → finset γ) : (s.product t).bUnion f = s.bUnion (λ a, t.bUnion (λ b, f (a, b))) := by { classical, simp_rw [product_eq_bUnion, bUnion_bUnion, image_bUnion] } @[simp] lemma card_product (s : finset α) (t : finset β) : card (s.product t) = card s * card t := multiset.card_product _ _ lemma filter_product (p : α → Prop) (q : β → Prop) [decidable_pred p] [decidable_pred q] : (s.product t).filter (λ (x : α × β), p x.1 ∧ q x.2) = (s.filter p).product (t.filter q) := by { ext ⟨a, b⟩, simp only [mem_filter, mem_product], exact and_and_and_comm (a ∈ s) (b ∈ t) (p a) (q b) } lemma filter_product_card (s : finset α) (t : finset β) (p : α → Prop) (q : β → Prop) [decidable_pred p] [decidable_pred q] : ((s.product t).filter (λ (x : α × β), p x.1 ↔ q x.2)).card = (s.filter p).card * (t.filter q).card + (s.filter (not ∘ p)).card * (t.filter (not ∘ q)).card := begin classical, rw [← card_product, ← card_product, ← filter_product, ← filter_product, ← card_union_eq], { apply congr_arg, ext ⟨a, b⟩, simp only [filter_union_right, mem_filter, mem_product], split; intros h; use h.1, simp only [function.comp_app, and_self, h.2, em (q b)], cases h.2; { try { simp at h_1 }, simp [h_1] } }, { rw disjoint_iff, change _ ∩ _ = ∅, ext ⟨a, b⟩, rw mem_inter, simp only [and_imp, mem_filter, not_and, not_not, function.comp_app, iff_false, mem_product, not_mem_empty], intros, assumption } end lemma empty_product (t : finset β) : (∅ : finset α).product t = ∅ := rfl lemma product_empty (s : finset α) : s.product (∅ : finset β) = ∅ := eq_empty_of_forall_not_mem (λ x h, (finset.mem_product.1 h).2) lemma nonempty.product (hs : s.nonempty) (ht : t.nonempty) : (s.product t).nonempty := let ⟨x, hx⟩ := hs, ⟨y, hy⟩ := ht in ⟨(x, y), mem_product.2 ⟨hx, hy⟩⟩ lemma nonempty.fst (h : (s.product t).nonempty) : s.nonempty := let ⟨xy, hxy⟩ := h in ⟨xy.1, (mem_product.1 hxy).1⟩ lemma nonempty.snd (h : (s.product t).nonempty) : t.nonempty := let ⟨xy, hxy⟩ := h in ⟨xy.2, (mem_product.1 hxy).2⟩ @[simp] lemma nonempty_product : (s.product t).nonempty ↔ s.nonempty ∧ t.nonempty := ⟨λ h, ⟨h.fst, h.snd⟩, λ h, h.1.product h.2⟩ @[simp] lemma product_eq_empty {s : finset α} {t : finset β} : s.product t = ∅ ↔ s = ∅ ∨ t = ∅ := by rw [←not_nonempty_iff_eq_empty, nonempty_product, not_and_distrib, not_nonempty_iff_eq_empty, not_nonempty_iff_eq_empty] @[simp] lemma singleton_product {a : α} : ({a} : finset α).product t = t.map ⟨prod.mk a, prod.mk.inj_left _⟩ := by { ext ⟨x, y⟩, simp [and.left_comm, eq_comm] } @[simp] lemma product_singleton {b : β} : s.product {b} = s.map ⟨λ i, (i, b), prod.mk.inj_right _⟩ := by { ext ⟨x, y⟩, simp [and.left_comm, eq_comm] } lemma singleton_product_singleton {a : α} {b : β} : ({a} : finset α).product ({b} : finset β) = {(a, b)} := by simp only [product_singleton, function.embedding.coe_fn_mk, map_singleton] @[simp] lemma union_product [decidable_eq α] [decidable_eq β] : (s ∪ s').product t = s.product t ∪ s'.product t := by { ext ⟨x, y⟩, simp only [or_and_distrib_right, mem_union, mem_product] } @[simp] lemma product_union [decidable_eq α] [decidable_eq β] : s.product (t ∪ t') = s.product t ∪ s.product t' := by { ext ⟨x, y⟩, simp only [and_or_distrib_left, mem_union, mem_product] } end prod section diag variables (s t : finset α) [decidable_eq α] /-- Given a finite set `s`, the diagonal, `s.diag` is the set of pairs of the form `(a, a)` for `a ∈ s`. -/ def diag := (s.product s).filter (λ (a : α × α), a.fst = a.snd) /-- Given a finite set `s`, the off-diagonal, `s.off_diag` is the set of pairs `(a, b)` with `a ≠ b` for `a, b ∈ s`. -/ def off_diag := (s.product s).filter (λ (a : α × α), a.fst ≠ a.snd) @[simp] lemma mem_diag (x : α × α) : x ∈ s.diag ↔ x.1 ∈ s ∧ x.1 = x.2 := by { simp only [diag, mem_filter, mem_product], split; intros h; simp only [h, and_true, eq_self_iff_true, and_self], rw ←h.2, exact h.1 } @[simp] lemma mem_off_diag (x : α × α) : x ∈ s.off_diag ↔ x.1 ∈ s ∧ x.2 ∈ s ∧ x.1 ≠ x.2 := by { simp only [off_diag, mem_filter, mem_product], split; intros h; simp only [h, ne.def, not_false_iff, and_self] } @[simp] lemma diag_card : (diag s).card = s.card := begin suffices : diag s = s.image (λ a, (a, a)), { rw this, apply card_image_of_inj_on, exact λ x1 h1 x2 h2 h3, (prod.mk.inj h3).1 }, ext ⟨a₁, a₂⟩, rw mem_diag, split; intros h; rw finset.mem_image at *, { use [a₁, h.1, prod.mk.inj_iff.mpr ⟨rfl, h.2⟩] }, { rcases h with ⟨a, h1, h2⟩, have h := prod.mk.inj h2, rw [←h.1, ←h.2], use h1 }, end @[simp] lemma off_diag_card : (off_diag s).card = s.card * s.card - s.card := begin suffices : (diag s).card + (off_diag s).card = s.card * s.card, { nth_rewrite 2 ← s.diag_card, simp only [diag_card] at *, rw tsub_eq_of_eq_add_rev, rw this }, rw ← card_product, apply filter_card_add_filter_neg_card_eq_card, end @[simp] lemma diag_empty : (∅ : finset α).diag = ∅ := rfl @[simp] lemma off_diag_empty : (∅ : finset α).off_diag = ∅ := rfl @[simp] lemma diag_union_off_diag : s.diag ∪ s.off_diag = s.product s := filter_union_filter_neg_eq _ _ @[simp] lemma disjoint_diag_off_diag : disjoint s.diag s.off_diag := disjoint_filter_filter_neg _ _ lemma product_sdiff_diag : s.product s \ s.diag = s.off_diag := by rw [←diag_union_off_diag, union_comm, union_sdiff_self, sdiff_eq_self_of_disjoint (disjoint_diag_off_diag _).symm] lemma product_sdiff_off_diag : s.product s \ s.off_diag = s.diag := by rw [←diag_union_off_diag, union_sdiff_self, sdiff_eq_self_of_disjoint (disjoint_diag_off_diag _)] lemma diag_union : (s ∪ t).diag = s.diag ∪ t.diag := by { ext ⟨i, j⟩, simp only [mem_diag, mem_union, or_and_distrib_right] } variables {s t} lemma off_diag_union (h : disjoint s t) : (s ∪ t).off_diag = s.off_diag ∪ t.off_diag ∪ s.product t ∪ t.product s := begin rw [off_diag, union_product, product_union, product_union, union_comm _ (t.product t), union_assoc, union_left_comm (s.product t), ←union_assoc, filter_union, filter_union, ←off_diag, ←off_diag, filter_true_of_mem, ←union_assoc], simp only [mem_union, mem_product, ne.def, prod.forall], rintro i j (⟨hi, hj⟩ | ⟨hi, hj⟩), { exact h.forall_ne_finset hi hj }, { exact h.symm.forall_ne_finset hi hj }, end variables (a : α) @[simp] lemma off_diag_singleton : ({a} : finset α).off_diag = ∅ := by simp [←finset.card_eq_zero] lemma diag_singleton : ({a} : finset α).diag = {(a, a)} := by rw [←product_sdiff_off_diag, off_diag_singleton, sdiff_empty, singleton_product_singleton] lemma diag_insert : (insert a s).diag = insert (a, a) s.diag := by rw [insert_eq, insert_eq, diag_union, diag_singleton] lemma off_diag_insert (has : a ∉ s) : (insert a s).off_diag = s.off_diag ∪ ({a} : finset α).product s ∪ s.product {a} := by rw [insert_eq, union_comm, off_diag_union (disjoint_singleton_right.2 has), off_diag_singleton, union_empty, union_right_comm] end diag end finset
6ab9a471d614750bc7da8aedf988d306a2224a09
217bb195841a8be2d1b4edd2084d6b69ccd62f50
/library/init/lean/parser/basic.lean
d540f3bd708e84c412e98076af28cbbd9fd22706
[ "Apache-2.0" ]
permissive
frank-lesser/lean4
717f56c9bacd5bf3a67542d2f5cea721d4743a30
79e2abe33f73162f773ea731265e456dbfe822f9
refs/heads/master
1,589,741,267,933
1,556,424,200,000
1,556,424,281,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,503
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Sebastian Ullrich Parser for the Lean language -/ prelude import init.lean.parser.parsec init.lean.parser.syntax init.lean.parser.rec import init.lean.parser.trie import init.lean.parser.identifier init.data.rbmap init.lean.message namespace Lean namespace Parser /- Maximum standard precedence. This is the precedence of Function application. In the standard Lean language, only the token `.` has a left-binding power greater than `maxPrec` (so that field accesses like `g (h x).f` are parsed as `g ((h x).f)`, not `(g (h x)).f`). -/ def maxPrec : Nat := 1024 structure TokenConfig := («prefix» : String) /- Left-binding power used by the Term Parser. The Term Parser operates in the context of a right-binding power between 0 (used by parentheses and on the top-Level) and (usually) `maxPrec` (used by Function application). After parsing an initial Term, it continues parsing and expanding that Term only when the left-binding power of the next token is greater than the current right-binding power. For example, it never continues parsing an argument after the initial parse, unless a token with lbp > maxPrec is encountered. Conversely, the Term Parser will always continue parsing inside parentheses until it finds a token with lbp 0 (such as `)`). -/ (lbp : Nat := 0) -- reading a token should not need any State /- An optional Parser that is activated after matching `prefix`. It should return a Syntax tree with a "hole" for the `SourceInfo` surrounding the token, which will be supplied by the `token` Parser. Remark: `suffixParser` has many applications for example for parsing hexdecimal numbers, `prefix` is `0x` and `suffixParser` is the Parser `digit*`. We also use it to parse String literals: here `prefix` is just `"`. -/ (suffixParser : Option (Parsec' (SourceInfo → Syntax)) := none) -- Backtrackable State structure ParserState := (messages : MessageLog) structure TokenCacheEntry := (startIt stopIt : String.OldIterator) (tk : Syntax) -- Non-backtrackable State structure ParserCache := (tokenCache : Option TokenCacheEntry := none) -- for profiling (hit miss : Nat := 0) structure FrontendConfig := (filename : String) (input : String) (fileMap : FileMap) /- Remark: if we have a Node in the Trie with `some TokenConfig`, the String induced by the path is equal to the `TokenConfig.prefix`. -/ structure ParserConfig extends FrontendConfig := (tokens : Trie TokenConfig) instance parserConfigCoe : HasCoe ParserConfig FrontendConfig := ⟨ParserConfig.toFrontendConfig⟩ @[derive Monad Alternative MonadParsec MonadExcept] def parserCoreT (m : Type → Type) [Monad m] := ParsecT Syntax $ StateT ParserCache $ m @[derive Monad Alternative MonadReader MonadParsec MonadExcept] def ParserT (ρ : Type) (m : Type → Type) [Monad m] := ReaderT ρ $ parserCoreT m @[derive Monad Alternative MonadReader MonadParsec MonadExcept] def BasicParserM := ParserT ParserConfig Id abbrev basicParser := BasicParserM Syntax abbrev monadBasicParser := HasMonadLiftT BasicParserM section local attribute [reducible] BasicParserM ParserT parserCoreT @[inline] def getCache : BasicParserM ParserCache := monadLift (get : StateT ParserCache Id _) @[inline] def putCache : ParserCache → BasicParserM PUnit := λ c, monadLift (set c : StateT ParserCache Id _) end -- an arbitrary `Parser` Type; parsers are usually some Monad stack based on `BasicParserM` returning `Syntax` variable {ρ : Type} class HasTokens (r : ρ) := mk {} :: (tokens : List TokenConfig) @[noinline, nospecialize] def tokens (r : ρ) [HasTokens r] := HasTokens.tokens r instance HasTokens.Inhabited (r : ρ) : Inhabited (HasTokens r) := ⟨⟨[]⟩⟩ instance List.nil.tokens : Parser.HasTokens ([] : List ρ) := default _ instance List.cons.tokens (r : ρ) (rs : List ρ) [Parser.HasTokens r] [Parser.HasTokens rs] : Parser.HasTokens (r::rs) := ⟨tokens r ++ tokens rs⟩ class HasView (α : outParam Type) (r : ρ) := (view : Syntax → α) (review : α → Syntax) export HasView (view review) def tryView {α : Type} (k : SyntaxNodeKind) [HasView α k] (stx : Syntax) : Option α := if stx.isOfKind k then some (HasView.view k stx) else none instance HasView.default (r : ρ) : Inhabited (Parser.HasView Syntax r) := ⟨{ view := id, review := id }⟩ class HasViewDefault (r : ρ) (α : outParam Type) [HasView α r] (default : α) := mk {} def messageOfParsecMessage {μ : Type} (cfg : FrontendConfig) (msg : Parsec.Message μ) : Message := {filename := cfg.filename, pos := cfg.fileMap.toPosition msg.it.offset, text := msg.text} /-- Run Parser stack, returning a partial Syntax tree in case of a fatal error -/ protected def run {m : Type → Type} {α ρ : Type} [Monad m] [HasCoeT ρ FrontendConfig] (cfg : ρ) (s : String) (r : StateT ParserState (ParserT ρ m) α) : m (Sum α Syntax × MessageLog) := do (r, _) ← (((r.run {messages:=MessageLog.empty}).run cfg).parse s).run {}, pure $ match r with | Except.ok (a, st) := (Sum.inl a, st.messages) | Except.error msg := (Sum.inr msg.custom.get, MessageLog.empty.add (messageOfParsecMessage cfg msg)) open MonadParsec open Parser.HasView variables {α : Type} {m : Type → Type} local notation `Parser` := m Syntax def logMessage {μ : Type} [Monad m] [MonadReader ρ m] [HasLiftT ρ FrontendConfig] [MonadState ParserState m] (msg : Parsec.Message μ) : m Unit := do cfg ← read, modify (λ st, {st with messages := st.messages.add (messageOfParsecMessage ↑cfg msg)}) def mkTokenTrie (tokens : List TokenConfig) : Except String (Trie TokenConfig) := do -- the only hardcoded tokens, because they are never directly mentioned by a `Parser` let builtinTokens : List TokenConfig := [{«prefix» := "/-"}, {«prefix» := "--"}], t ← (builtinTokens ++ tokens).mfoldl (λ (t : Trie TokenConfig) tk, match t.find tk.prefix with | some tk' := (match tk.lbp, tk'.lbp with | l, 0 := pure $ t.insert tk.prefix tk | 0, _ := pure t | l, l' := if l = l' then pure t else throw $ "invalid token '" ++ tk.prefix ++ "', has been defined with precedences " ++ toString l ++ " and " ++ toString l') | none := pure $ t.insert tk.prefix tk) Trie.empty, pure t /- Monad stacks used in multiple files -/ /- NOTE: We move `RecT` under `ParserT`'s `ReaderT` so that `termParser`, which does not have access to `commandParser`'s ρ (=`CommandParserConfig`) can still recurse into it (for command quotations). This means that the `CommandParserConfig` will be reset on a recursive call to `command.Parser`, i.e. it forgets about locally registered parsers, but that's not an issue for our intended uses of it. -/ @[derive Monad Alternative MonadReader MonadParsec MonadExcept MonadRec] def CommandParserM (ρ : Type) := ReaderT ρ $ RecT Unit Syntax $ parserCoreT Id section local attribute [reducible] ParserT CommandParserM instance CommandParserM.MonadReaderAdapter (ρ ρ' : Type) : MonadReaderAdapter ρ ρ' (CommandParserM ρ) (CommandParserM ρ') := inferInstance instance CommandParserM.basicParser (ρ : Type) [HasLiftT ρ ParserConfig] : monadBasicParser (CommandParserM ρ) := ⟨λ _ x cfg rec, x.run ↑cfg⟩ end /- The `Nat` at `RecT` is the lbp` -/ @[derive Monad Alternative MonadReader MonadParsec MonadExcept MonadRec monadBasicParser] def TermParserM := RecT Nat Syntax $ CommandParserM ParserConfig abbrev termParser := TermParserM Syntax /-- A Term Parser for a suffix or infix notation that accepts a preceding Term. -/ @[derive Monad Alternative MonadReader MonadParsec MonadExcept MonadRec monadBasicParser] def TrailingTermParserM := ReaderT Syntax TermParserM abbrev trailingTermParser := TrailingTermParserM Syntax instance trailingTermParserCoe : HasCoe termParser trailingTermParser := ⟨λ x _, x⟩ /-- A multimap indexed by tokens. Used for indexing parsers by their leading token. -/ def TokenMap (α : Type) := RBMap Name (List α) Name.quickLt def TokenMap.insert {α : Type} (map : TokenMap α) (k : Name) (v : α) : TokenMap α := match map.find k with | none := map.insert k [v] | some vs := map.insert k (v::vs) def TokenMap.ofList {α : Type} : List (Name × α) → TokenMap α | [] := mkRBMap _ _ _ | (⟨k,v⟩::xs) := (TokenMap.ofList xs).insert k v instance tokenMapNil.tokens : Parser.HasTokens $ @TokenMap.ofList ρ [] := default _ instance tokenMapCons.tokens (k : Name) (r : ρ) (rs : List (Name × ρ)) [Parser.HasTokens r] [Parser.HasTokens $ TokenMap.ofList rs] : Parser.HasTokens $ TokenMap.ofList ((k,r)::rs) := ⟨tokens r ++ tokens (TokenMap.ofList rs)⟩ -- This needs to be a separate structure since `termParser`s cannot contain themselves in their config structure CommandParserConfig extends ParserConfig := (leadingTermParsers : TokenMap termParser) (trailingTermParsers : TokenMap trailingTermParser) -- local Term parsers (such as from `local notation`) hide previous parsers instead of overloading them (localLeadingTermParsers : TokenMap termParser := mkRBMap _ _ _) (localTrailingTermParsers : TokenMap trailingTermParser := mkRBMap _ _ _) instance commandParserConfigCoeParserConfig : HasCoe CommandParserConfig ParserConfig := ⟨CommandParserConfig.toParserConfig⟩ abbrev commandParser := CommandParserM CommandParserConfig Syntax end «Parser» end Lean
d1caf985e18a6480b4e136c9c57083c6c2499ba7
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/number_theory/ADE_inequality.lean
5ba14070ab98cfe537adf82187201cfc4a50ee0b
[ "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,742
lean
/- Copyright (c) 2021 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import data.multiset.sort import data.pnat.interval import data.rat.order import data.pnat.basic import tactic.norm_num import tactic.field_simp import tactic.interval_cases /-! # The inequality `p⁻¹ + q⁻¹ + r⁻¹ > 1` > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we classify solutions to the inequality `(p⁻¹ + q⁻¹ + r⁻¹ : ℚ) > 1`, for positive natural numbers `p`, `q`, and `r`. The solutions are exactly of the form. * `A' q r := {1,q,r}` * `D' r := {2,2,r}` * `E6 := {2,3,3}`, or `E7 := {2,3,4}`, or `E8 := {2,3,5}` This inequality shows up in Lie theory, in the classification of Dynkin diagrams, root systems, and semisimple Lie algebras. ## Main declarations * `pqr.A' q r`, the multiset `{1,q,r}` * `pqr.D' r`, the multiset `{2,2,r}` * `pqr.E6`, the multiset `{2,3,3}` * `pqr.E7`, the multiset `{2,3,4}` * `pqr.E8`, the multiset `{2,3,5}` * `pqr.classification`, the classification of solutions to `p⁻¹ + q⁻¹ + r⁻¹ > 1` -/ namespace ADE_inequality open multiset /-- `A' q r := {1,q,r}` is a `multiset ℕ+` that is a solution to the inequality `(p⁻¹ + q⁻¹ + r⁻¹ : ℚ) > 1`. -/ def A' (q r : ℕ+) : multiset ℕ+ := {1,q,r} /-- `A r := {1,1,r}` is a `multiset ℕ+` that is a solution to the inequality `(p⁻¹ + q⁻¹ + r⁻¹ : ℚ) > 1`. These solutions are related to the Dynkin diagrams $A_r$. -/ def A (r : ℕ+) : multiset ℕ+ := A' 1 r /-- `D' r := {2,2,r}` is a `multiset ℕ+` that is a solution to the inequality `(p⁻¹ + q⁻¹ + r⁻¹ : ℚ) > 1`. These solutions are related to the Dynkin diagrams $D_{r+2}$. -/ def D' (r : ℕ+) : multiset ℕ+ := {2,2,r} /-- `E' r := {2,3,r}` is a `multiset ℕ+`. For `r ∈ {3,4,5}` is a solution to the inequality `(p⁻¹ + q⁻¹ + r⁻¹ : ℚ) > 1`. These solutions are related to the Dynkin diagrams $E_{r+3}$. -/ def E' (r : ℕ+) : multiset ℕ+ := {2,3,r} /-- `E6 := {2,3,3}` is a `multiset ℕ+` that is a solution to the inequality `(p⁻¹ + q⁻¹ + r⁻¹ : ℚ) > 1`. This solution is related to the Dynkin diagrams $E_6$. -/ def E6 : multiset ℕ+ := E' 3 /-- `E7 := {2,3,4}` is a `multiset ℕ+` that is a solution to the inequality `(p⁻¹ + q⁻¹ + r⁻¹ : ℚ) > 1`. This solution is related to the Dynkin diagrams $E_7$. -/ def E7 : multiset ℕ+ := E' 4 /-- `E8 := {2,3,5}` is a `multiset ℕ+` that is a solution to the inequality `(p⁻¹ + q⁻¹ + r⁻¹ : ℚ) > 1`. This solution is related to the Dynkin diagrams $E_8$. -/ def E8 : multiset ℕ+ := E' 5 /-- `sum_inv pqr` for a `pqr : multiset ℕ+` is the sum of the inverses of the elements of `pqr`, as rational number. The intended argument is a multiset `{p,q,r}` of cardinality `3`. -/ def sum_inv (pqr : multiset ℕ+) : ℚ := multiset.sum $ pqr.map $ λ x, x⁻¹ lemma sum_inv_pqr (p q r : ℕ+) : sum_inv {p,q,r} = p⁻¹ + q⁻¹ + r⁻¹ := by simp only [sum_inv, coe_coe, add_zero, insert_eq_cons, add_assoc, map_cons, sum_cons, map_singleton, sum_singleton] /-- A multiset `pqr` of positive natural numbers is `admissible` if it is equal to `A' q r`, or `D' r`, or one of `E6`, `E7`, or `E8`. -/ def admissible (pqr : multiset ℕ+) : Prop := (∃ q r, A' q r = pqr) ∨ (∃ r, D' r = pqr) ∨ (E' 3 = pqr ∨ E' 4 = pqr ∨ E' 5 = pqr) lemma admissible_A' (q r : ℕ+) : admissible (A' q r) := or.inl ⟨q, r, rfl⟩ lemma admissible_D' (n : ℕ+) : admissible (D' n) := or.inr $ or.inl ⟨n, rfl⟩ lemma admissible_E'3 : admissible (E' 3) := or.inr $ or.inr $ or.inl rfl lemma admissible_E'4 : admissible (E' 4) := or.inr $ or.inr $ or.inr $ or.inl rfl lemma admissible_E'5 : admissible (E' 5) := or.inr $ or.inr $ or.inr $ or.inr rfl lemma admissible_E6 : admissible (E6) := admissible_E'3 lemma admissible_E7 : admissible (E7) := admissible_E'4 lemma admissible_E8 : admissible (E8) := admissible_E'5 lemma admissible.one_lt_sum_inv {pqr : multiset ℕ+} : admissible pqr → 1 < sum_inv pqr := begin rw [admissible], rintro (⟨p', q', H⟩|⟨n, H⟩|H|H|H), { rw [← H, A', sum_inv_pqr, add_assoc], simp only [lt_add_iff_pos_right, pnat.one_coe, inv_one, nat.cast_one, coe_coe], apply add_pos; simp only [pnat.pos, nat.cast_pos, inv_pos] }, { rw [← H, D', sum_inv_pqr], simp only [lt_add_iff_pos_right, pnat.one_coe, inv_one, nat.cast_one, coe_coe, pnat.coe_bit0, nat.cast_bit0], norm_num }, all_goals { rw [← H, E', sum_inv_pqr], norm_num } end lemma lt_three {p q r : ℕ+} (hpq : p ≤ q) (hqr : q ≤ r) (H : 1 < sum_inv {p, q, r}) : p < 3 := begin have h3 : (0:ℚ) < 3, by norm_num, contrapose! H, rw sum_inv_pqr, have h3q := H.trans hpq, have h3r := h3q.trans hqr, calc (p⁻¹ + q⁻¹ + r⁻¹ : ℚ) ≤ 3⁻¹ + 3⁻¹ + 3⁻¹ : add_le_add (add_le_add _ _) _ ... = 1 : by norm_num, all_goals { rw inv_le_inv _ h3; [assumption_mod_cast, norm_num] } end lemma lt_four {q r : ℕ+} (hqr : q ≤ r) (H : 1 < sum_inv {2, q, r}) : q < 4 := begin have h4 : (0:ℚ) < 4, by norm_num, contrapose! H, rw sum_inv_pqr, have h4r := H.trans hqr, simp only [pnat.coe_bit0, nat.cast_bit0, pnat.one_coe, nat.cast_one, coe_coe], calc (2⁻¹ + q⁻¹ + r⁻¹ : ℚ) ≤ 2⁻¹ + 4⁻¹ + 4⁻¹ : add_le_add (add_le_add le_rfl _) _ ... = 1 : by norm_num, all_goals { rw inv_le_inv _ h4; [assumption_mod_cast, norm_num] } end lemma lt_six {r : ℕ+} (H : 1 < sum_inv {2, 3, r}) : r < 6 := begin have h6 : (0:ℚ) < 6, by norm_num, contrapose! H, rw sum_inv_pqr, simp only [pnat.coe_bit0, nat.cast_bit0, pnat.one_coe, nat.cast_bit1, nat.cast_one, pnat.coe_bit1, coe_coe], calc (2⁻¹ + 3⁻¹ + r⁻¹ : ℚ) ≤ 2⁻¹ + 3⁻¹ + 6⁻¹ : add_le_add (add_le_add le_rfl le_rfl) _ ... = 1 : by norm_num, rw inv_le_inv _ h6; [assumption_mod_cast, norm_num] end lemma admissible_of_one_lt_sum_inv_aux' {p q r : ℕ+} (hpq : p ≤ q) (hqr : q ≤ r) (H : 1 < sum_inv {p,q,r}) : admissible {p,q,r} := begin have hp3 : p < 3 := lt_three hpq hqr H, interval_cases p, { exact admissible_A' q r }, have hq4 : q < 4 := lt_four hqr H, interval_cases q, { exact admissible_D' r }, have hr6 : r < 6 := lt_six H, interval_cases r, { exact admissible_E6 }, { exact admissible_E7 }, { exact admissible_E8 } end lemma admissible_of_one_lt_sum_inv_aux : ∀ {pqr : list ℕ+} (hs : pqr.sorted (≤)) (hl : pqr.length = 3) (H : 1 < sum_inv pqr), admissible pqr | [p,q,r] hs hl H := begin obtain ⟨⟨hpq, -⟩, hqr⟩ : (p ≤ q ∧ p ≤ r) ∧ q ≤ r, simpa using hs, exact admissible_of_one_lt_sum_inv_aux' hpq hqr H, end lemma admissible_of_one_lt_sum_inv {p q r : ℕ+} (H : 1 < sum_inv {p,q,r}) : admissible {p,q,r} := begin simp only [admissible], let S := sort ((≤) : ℕ+ → ℕ+ → Prop) {p,q,r}, have hS : S.sorted (≤) := sort_sorted _ _, have hpqr : ({p,q,r} : multiset ℕ+) = S := (sort_eq has_le.le {p, q, r}).symm, simp only [hpqr] at *, apply admissible_of_one_lt_sum_inv_aux hS _ H, simp only [S, length_sort], dec_trivial, end /-- A multiset `{p,q,r}` of positive natural numbers is a solution to `(p⁻¹ + q⁻¹ + r⁻¹ : ℚ) > 1` if and only if it is `admissible` which means it is one of: * `A' q r := {1,q,r}` * `D' r := {2,2,r}` * `E6 := {2,3,3}`, or `E7 := {2,3,4}`, or `E8 := {2,3,5}` -/ lemma classification (p q r : ℕ+) : 1 < sum_inv {p,q,r} ↔ admissible {p,q,r} := ⟨admissible_of_one_lt_sum_inv, admissible.one_lt_sum_inv⟩ end ADE_inequality
5fa4d250c0b34fc29597cfb0edf96556a70cee95
592ee40978ac7604005a4e0d35bbc4b467389241
/Library/generated/mathscheme-lean/LeftCancellativeMagma.lean
218d56da700147de7ea7c640e8fb4ba00e931ad9
[]
no_license
ysharoda/Deriving-Definitions
3e149e6641fae440badd35ac110a0bd705a49ad2
dfecb27572022de3d4aa702cae8db19957523a59
refs/heads/master
1,679,127,857,700
1,615,939,007,000
1,615,939,007,000
229,785,731
4
0
null
null
null
null
UTF-8
Lean
false
false
6,508
lean
import init.data.nat.basic import init.data.fin.basic import data.vector import .Prelude open Staged open nat open fin open vector section LeftCancellativeMagma structure LeftCancellativeMagma (A : Type) : Type := (op : (A → (A → A))) (leftCancellative : (∀ {x y z : A} , ((op z x) = (op z y) → x = y))) open LeftCancellativeMagma structure Sig (AS : Type) : Type := (opS : (AS → (AS → AS))) structure Product (A : Type) : Type := (opP : ((Prod A A) → ((Prod A A) → (Prod A A)))) (leftCancellativeP : (∀ {xP yP zP : (Prod A A)} , ((opP zP xP) = (opP zP yP) → xP = yP))) structure Hom {A1 : Type} {A2 : Type} (Le1 : (LeftCancellativeMagma A1)) (Le2 : (LeftCancellativeMagma A2)) : Type := (hom : (A1 → A2)) (pres_op : (∀ {x1 x2 : A1} , (hom ((op Le1) x1 x2)) = ((op Le2) (hom x1) (hom x2)))) structure RelInterp {A1 : Type} {A2 : Type} (Le1 : (LeftCancellativeMagma A1)) (Le2 : (LeftCancellativeMagma A2)) : Type 1 := (interp : (A1 → (A2 → Type))) (interp_op : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((op Le1) x1 x2) ((op Le2) y1 y2)))))) inductive LeftCancellativeMagmaTerm : Type | opL : (LeftCancellativeMagmaTerm → (LeftCancellativeMagmaTerm → LeftCancellativeMagmaTerm)) open LeftCancellativeMagmaTerm inductive ClLeftCancellativeMagmaTerm (A : Type) : Type | sing : (A → ClLeftCancellativeMagmaTerm) | opCl : (ClLeftCancellativeMagmaTerm → (ClLeftCancellativeMagmaTerm → ClLeftCancellativeMagmaTerm)) open ClLeftCancellativeMagmaTerm inductive OpLeftCancellativeMagmaTerm (n : ℕ) : Type | v : ((fin n) → OpLeftCancellativeMagmaTerm) | opOL : (OpLeftCancellativeMagmaTerm → (OpLeftCancellativeMagmaTerm → OpLeftCancellativeMagmaTerm)) open OpLeftCancellativeMagmaTerm inductive OpLeftCancellativeMagmaTerm2 (n : ℕ) (A : Type) : Type | v2 : ((fin n) → OpLeftCancellativeMagmaTerm2) | sing2 : (A → OpLeftCancellativeMagmaTerm2) | opOL2 : (OpLeftCancellativeMagmaTerm2 → (OpLeftCancellativeMagmaTerm2 → OpLeftCancellativeMagmaTerm2)) open OpLeftCancellativeMagmaTerm2 def simplifyCl {A : Type} : ((ClLeftCancellativeMagmaTerm A) → (ClLeftCancellativeMagmaTerm A)) | (opCl x1 x2) := (opCl (simplifyCl x1) (simplifyCl x2)) | (sing x1) := (sing x1) def simplifyOpB {n : ℕ} : ((OpLeftCancellativeMagmaTerm n) → (OpLeftCancellativeMagmaTerm n)) | (opOL x1 x2) := (opOL (simplifyOpB x1) (simplifyOpB x2)) | (v x1) := (v x1) def simplifyOp {n : ℕ} {A : Type} : ((OpLeftCancellativeMagmaTerm2 n A) → (OpLeftCancellativeMagmaTerm2 n A)) | (opOL2 x1 x2) := (opOL2 (simplifyOp x1) (simplifyOp x2)) | (v2 x1) := (v2 x1) | (sing2 x1) := (sing2 x1) def evalB {A : Type} : ((LeftCancellativeMagma A) → (LeftCancellativeMagmaTerm → A)) | Le (opL x1 x2) := ((op Le) (evalB Le x1) (evalB Le x2)) def evalCl {A : Type} : ((LeftCancellativeMagma A) → ((ClLeftCancellativeMagmaTerm A) → A)) | Le (sing x1) := x1 | Le (opCl x1 x2) := ((op Le) (evalCl Le x1) (evalCl Le x2)) def evalOpB {A : Type} {n : ℕ} : ((LeftCancellativeMagma A) → ((vector A n) → ((OpLeftCancellativeMagmaTerm n) → A))) | Le vars (v x1) := (nth vars x1) | Le vars (opOL x1 x2) := ((op Le) (evalOpB Le vars x1) (evalOpB Le vars x2)) def evalOp {A : Type} {n : ℕ} : ((LeftCancellativeMagma A) → ((vector A n) → ((OpLeftCancellativeMagmaTerm2 n A) → A))) | Le vars (v2 x1) := (nth vars x1) | Le vars (sing2 x1) := x1 | Le vars (opOL2 x1 x2) := ((op Le) (evalOp Le vars x1) (evalOp Le vars x2)) def inductionB {P : (LeftCancellativeMagmaTerm → Type)} : ((∀ (x1 x2 : LeftCancellativeMagmaTerm) , ((P x1) → ((P x2) → (P (opL x1 x2))))) → (∀ (x : LeftCancellativeMagmaTerm) , (P x))) | popl (opL x1 x2) := (popl _ _ (inductionB popl x1) (inductionB popl x2)) def inductionCl {A : Type} {P : ((ClLeftCancellativeMagmaTerm A) → Type)} : ((∀ (x1 : A) , (P (sing x1))) → ((∀ (x1 x2 : (ClLeftCancellativeMagmaTerm A)) , ((P x1) → ((P x2) → (P (opCl x1 x2))))) → (∀ (x : (ClLeftCancellativeMagmaTerm A)) , (P x)))) | psing popcl (sing x1) := (psing x1) | psing popcl (opCl x1 x2) := (popcl _ _ (inductionCl psing popcl x1) (inductionCl psing popcl x2)) def inductionOpB {n : ℕ} {P : ((OpLeftCancellativeMagmaTerm n) → Type)} : ((∀ (fin : (fin n)) , (P (v fin))) → ((∀ (x1 x2 : (OpLeftCancellativeMagmaTerm n)) , ((P x1) → ((P x2) → (P (opOL x1 x2))))) → (∀ (x : (OpLeftCancellativeMagmaTerm n)) , (P x)))) | pv popol (v x1) := (pv x1) | pv popol (opOL x1 x2) := (popol _ _ (inductionOpB pv popol x1) (inductionOpB pv popol x2)) def inductionOp {n : ℕ} {A : Type} {P : ((OpLeftCancellativeMagmaTerm2 n A) → Type)} : ((∀ (fin : (fin n)) , (P (v2 fin))) → ((∀ (x1 : A) , (P (sing2 x1))) → ((∀ (x1 x2 : (OpLeftCancellativeMagmaTerm2 n A)) , ((P x1) → ((P x2) → (P (opOL2 x1 x2))))) → (∀ (x : (OpLeftCancellativeMagmaTerm2 n A)) , (P x))))) | pv2 psing2 popol2 (v2 x1) := (pv2 x1) | pv2 psing2 popol2 (sing2 x1) := (psing2 x1) | pv2 psing2 popol2 (opOL2 x1 x2) := (popol2 _ _ (inductionOp pv2 psing2 popol2 x1) (inductionOp pv2 psing2 popol2 x2)) def stageB : (LeftCancellativeMagmaTerm → (Staged LeftCancellativeMagmaTerm)) | (opL x1 x2) := (stage2 opL (codeLift2 opL) (stageB x1) (stageB x2)) def stageCl {A : Type} : ((ClLeftCancellativeMagmaTerm A) → (Staged (ClLeftCancellativeMagmaTerm A))) | (sing x1) := (Now (sing x1)) | (opCl x1 x2) := (stage2 opCl (codeLift2 opCl) (stageCl x1) (stageCl x2)) def stageOpB {n : ℕ} : ((OpLeftCancellativeMagmaTerm n) → (Staged (OpLeftCancellativeMagmaTerm n))) | (v x1) := (const (code (v x1))) | (opOL x1 x2) := (stage2 opOL (codeLift2 opOL) (stageOpB x1) (stageOpB x2)) def stageOp {n : ℕ} {A : Type} : ((OpLeftCancellativeMagmaTerm2 n A) → (Staged (OpLeftCancellativeMagmaTerm2 n A))) | (sing2 x1) := (Now (sing2 x1)) | (v2 x1) := (const (code (v2 x1))) | (opOL2 x1 x2) := (stage2 opOL2 (codeLift2 opOL2) (stageOp x1) (stageOp x2)) structure StagedRepr (A : Type) (Repr : (Type → Type)) : Type := (opT : ((Repr A) → ((Repr A) → (Repr A)))) end LeftCancellativeMagma
b2a689033229a01f280ff7e9592b75fa9234e426
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/topology/metric_space/premetric_space.lean
17ab8051abfc2c347ae0671bc59dc60ec98bbb4f
[ "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
3,619
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Premetric spaces. Author: Sébastien Gouëzel -/ import topology.metric_space.basic tactic.linarith /-! # Premetric spaces Metric spaces are often defined as quotients of spaces endowed with a "distance" function satisfying the triangular inequality, but for which `dist x y = 0` does not imply x = y. We call such a space a premetric space. `dist x y = 0` defines an equivalence relation, and the quotient is canonically a metric space. -/ noncomputable theory universes u v variables {α : Type u} section prio set_option default_priority 100 -- see Note [default priority] class premetric_space (α : Type u) extends has_dist α : Type u := (dist_self : ∀ x : α, dist x x = 0) (dist_comm : ∀ x y : α, dist x y = dist y x) (dist_triangle : ∀ x y z : α, dist x z ≤ dist x y + dist y z) end prio namespace premetric section protected lemma dist_nonneg {α : Type u} [premetric_space α] {x y : α} : 0 ≤ dist x y := begin have := calc 0 = dist x x : (premetric_space.dist_self _).symm ... ≤ dist x y + dist y x : premetric_space.dist_triangle _ _ _ ... = dist x y + dist x y : by simp [premetric_space.dist_comm], by linarith end /-- The canonical equivalence relation on a premetric space. -/ def dist_setoid (α : Type u) [premetric_space α] : setoid α := setoid.mk (λx y, dist x y = 0) begin unfold equivalence, repeat { split }, { exact premetric_space.dist_self }, { assume x y h, rwa premetric_space.dist_comm }, { assume x y z hxy hyz, refine le_antisymm _ premetric.dist_nonneg, calc dist x z ≤ dist x y + dist y z : premetric_space.dist_triangle _ _ _ ... = 0 + 0 : by rw [hxy, hyz] ... = 0 : by simp } end local attribute [instance] dist_setoid /-- The canonical quotient of a premetric space, identifying points at distance 0. -/ @[reducible] definition metric_quot (α : Type u) [premetric_space α] : Type* := quotient (premetric.dist_setoid α) instance has_dist_metric_quot {α : Type u} [premetric_space α] : has_dist (metric_quot α) := { dist := quotient.lift₂ (λp q : α, dist p q) begin assume x y x' y' hxx' hyy', have Hxx' : dist x x' = 0 := hxx', have Hyy' : dist y y' = 0 := hyy', have A : dist x y ≤ dist x' y' := calc dist x y ≤ dist x x' + dist x' y : premetric_space.dist_triangle _ _ _ ... = dist x' y : by simp [Hxx'] ... ≤ dist x' y' + dist y' y : premetric_space.dist_triangle _ _ _ ... = dist x' y' : by simp [premetric_space.dist_comm, Hyy'], have B : dist x' y' ≤ dist x y := calc dist x' y' ≤ dist x' x + dist x y' : premetric_space.dist_triangle _ _ _ ... = dist x y' : by simp [premetric_space.dist_comm, Hxx'] ... ≤ dist x y + dist y y' : premetric_space.dist_triangle _ _ _ ... = dist x y : by simp [Hyy'], exact le_antisymm A B end } lemma metric_quot_dist_eq {α : Type u} [premetric_space α] (p q : α) : dist ⟦p⟧ ⟦q⟧ = dist p q := rfl instance metric_space_quot {α : Type u} [premetric_space α] : metric_space (metric_quot α) := { dist_self := begin refine quotient.ind (λy, _), exact premetric_space.dist_self _ end, eq_of_dist_eq_zero := λxc yc, quotient.induction_on₂ xc yc (λx y H, quotient.sound H), dist_comm := λxc yc, quotient.induction_on₂ xc yc (λx y, premetric_space.dist_comm _ _), dist_triangle := λxc yc zc, quotient.induction_on₃ xc yc zc (λx y z, premetric_space.dist_triangle _ _ _) } end --section end premetric --namespace
43c5c3ff039457d363c99e09bfa7b1d58d0f8823
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/data/set/intervals/basic.lean
74116008d59c50355c1667a84f885f77a983a401
[ "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
28,615
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot, Yury Kudryashov -/ import order.lattice algebra.order_functions algebra.ordered_field tactic.tauto /-! # Intervals In any preorder `α`, we define intervals (which on each side can be either infinite, open, or closed) using the following naming conventions: - `i`: infinite - `o`: open - `c`: closed Each interval has the name `I` + letter for left side + letter for right side. For instance, `Ioc a b` denotes the inverval `(a, b]`. This file contains these definitions, and basic facts on inclusion, intersection, difference of intervals (where the precise statements may depend on the properties of the order, in particular for some statements it should be `linear_order` or `densely_ordered`). This file also contains statements on lower and upper bounds of intervals. TODO: This is just the beginning; a lot of rules are missing -/ universe u namespace set open set section intervals variables {α : Type u} [preorder α] {a a₁ a₂ b b₁ b₂ x : α} /-- Left-open right-open interval -/ def Ioo (a b : α) := {x | a < x ∧ x < b} /-- Left-closed right-open interval -/ def Ico (a b : α) := {x | a ≤ x ∧ x < b} /-- Left-infinite right-open interval -/ def Iio (a : α) := {x | x < a} /-- Left-closed right-closed interval -/ def Icc (a b : α) := {x | a ≤ x ∧ x ≤ b} /-- Left-infinite right-closed interval -/ def Iic (b : α) := {x | x ≤ b} /-- Left-open right-closed interval -/ def Ioc (a b : α) := {x | a < x ∧ x ≤ b} /-- Left-closed right-infinite interval -/ def Ici (a : α) := {x | a ≤ x} /-- Left-open right-infinite interval -/ def Ioi (a : α) := {x | a < x} @[simp] lemma mem_Ioo : x ∈ Ioo a b ↔ a < x ∧ x < b := iff.rfl @[simp] lemma mem_Ico : x ∈ Ico a b ↔ a ≤ x ∧ x < b := iff.rfl @[simp] lemma mem_Iio : x ∈ Iio b ↔ x < b := iff.rfl @[simp] lemma mem_Icc : x ∈ Icc a b ↔ a ≤ x ∧ x ≤ b := iff.rfl @[simp] lemma mem_Iic : x ∈ Iic b ↔ x ≤ b := iff.rfl @[simp] lemma mem_Ioc : x ∈ Ioc a b ↔ a < x ∧ x ≤ b := iff.rfl @[simp] lemma mem_Ici : x ∈ Ici a ↔ a ≤ x := iff.rfl @[simp] lemma mem_Ioi : x ∈ Ioi a ↔ a < x := iff.rfl @[simp] lemma left_mem_Ioo : a ∈ Ioo a b ↔ false := by simp [lt_irrefl] @[simp] lemma left_mem_Ico : a ∈ Ico a b ↔ a < b := by simp [le_refl] @[simp] lemma left_mem_Icc : a ∈ Icc a b ↔ a ≤ b := by simp [le_refl] @[simp] lemma left_mem_Ioc : a ∈ Ioc a b ↔ false := by simp [lt_irrefl] lemma left_mem_Ici : a ∈ Ici a := by simp @[simp] lemma right_mem_Ioo : b ∈ Ioo a b ↔ false := by simp [lt_irrefl] @[simp] lemma right_mem_Ico : b ∈ Ico a b ↔ false := by simp [lt_irrefl] @[simp] lemma right_mem_Icc : b ∈ Icc a b ↔ a ≤ b := by simp [le_refl] @[simp] lemma right_mem_Ioc : b ∈ Ioc a b ↔ a < b := by simp [le_refl] lemma right_mem_Iic : a ∈ Iic a := by simp @[simp] lemma dual_Ici : @Ici (order_dual α) _ a = @Iic α _ a := rfl @[simp] lemma dual_Iic : @Iic (order_dual α) _ a = @Ici α _ a := rfl @[simp] lemma dual_Ioi : @Ioi (order_dual α) _ a = @Iio α _ a := rfl @[simp] lemma dual_Iio : @Iio (order_dual α) _ a = @Ioi α _ a := rfl @[simp] lemma dual_Icc : @Icc (order_dual α) _ a b = @Icc α _ b a := set.ext $ λ x, and_comm _ _ @[simp] lemma dual_Ioc : @Ioc (order_dual α) _ a b = @Ico α _ b a := set.ext $ λ x, and_comm _ _ @[simp] lemma dual_Ico : @Ico (order_dual α) _ a b = @Ioc α _ b a := set.ext $ λ x, and_comm _ _ @[simp] lemma dual_Ioo : @Ioo (order_dual α) _ a b = @Ioo α _ b a := set.ext $ λ x, and_comm _ _ @[simp] lemma nonempty_Icc : (Icc a b).nonempty ↔ a ≤ b := ⟨λ ⟨x, hx⟩, le_trans hx.1 hx.2, λ h, ⟨a, left_mem_Icc.2 h⟩⟩ @[simp] lemma nonempty_Ico : (Ico a b).nonempty ↔ a < b := ⟨λ ⟨x, hx⟩, lt_of_le_of_lt hx.1 hx.2, λ h, ⟨a, left_mem_Ico.2 h⟩⟩ @[simp] lemma nonempty_Ioc : (Ioc a b).nonempty ↔ a < b := ⟨λ ⟨x, hx⟩, lt_of_lt_of_le hx.1 hx.2, λ h, ⟨b, right_mem_Ioc.2 h⟩⟩ @[simp] lemma nonempty_Ici : (Ici a).nonempty := ⟨a, left_mem_Ici⟩ @[simp] lemma nonempty_Iic : (Iic a).nonempty := ⟨a, right_mem_Iic⟩ @[simp] lemma nonempty_Ioo [densely_ordered α] : (Ioo a b).nonempty ↔ a < b := ⟨λ ⟨x, ha, hb⟩, lt_trans ha hb, dense⟩ @[simp] lemma nonempty_Ioi [no_top_order α] : (Ioi a).nonempty := no_top a @[simp] lemma nonempty_Iio [no_bot_order α] : (Iio a).nonempty := no_bot a @[simp] lemma Ioo_eq_empty (h : b ≤ a) : Ioo a b = ∅ := eq_empty_iff_forall_not_mem.2 $ λ x ⟨h₁, h₂⟩, not_le_of_lt (lt_trans h₁ h₂) h @[simp] lemma Ico_eq_empty (h : b ≤ a) : Ico a b = ∅ := eq_empty_iff_forall_not_mem.2 $ λ x ⟨h₁, h₂⟩, not_le_of_lt (lt_of_le_of_lt h₁ h₂) h @[simp] lemma Icc_eq_empty (h : b < a) : Icc a b = ∅ := eq_empty_iff_forall_not_mem.2 $ λ x ⟨h₁, h₂⟩, not_lt_of_le (le_trans h₁ h₂) h @[simp] lemma Ioc_eq_empty (h : b ≤ a) : Ioc a b = ∅ := eq_empty_iff_forall_not_mem.2 $ λ x ⟨h₁, h₂⟩, not_lt_of_le (le_trans h₂ h) h₁ @[simp] lemma Ioo_self (a : α) : Ioo a a = ∅ := Ioo_eq_empty $ le_refl _ @[simp] lemma Ico_self (a : α) : Ico a a = ∅ := Ico_eq_empty $ le_refl _ @[simp] lemma Ioc_self (a : α) : Ioc a a = ∅ := Ioc_eq_empty $ le_refl _ lemma Ici_subset_Ici : Ici a ⊆ Ici b ↔ b ≤ a := ⟨λ h, h $ left_mem_Ici, λ h x hx, le_trans h hx⟩ lemma Iic_subset_Iic : Iic a ⊆ Iic b ↔ a ≤ b := @Ici_subset_Ici (order_dual α) _ _ _ lemma Ici_subset_Ioi : Ici a ⊆ Ioi b ↔ b < a := ⟨λ h, h left_mem_Ici, λ h x hx, lt_of_lt_of_le h hx⟩ lemma Iic_subset_Iio : Iic a ⊆ Iio b ↔ a < b := ⟨λ h, h right_mem_Iic, λ h x hx, lt_of_le_of_lt hx h⟩ lemma Ioo_subset_Ioo (h₁ : a₂ ≤ a₁) (h₂ : b₁ ≤ b₂) : Ioo a₁ b₁ ⊆ Ioo a₂ b₂ := λ x ⟨hx₁, hx₂⟩, ⟨lt_of_le_of_lt h₁ hx₁, lt_of_lt_of_le hx₂ h₂⟩ lemma Ioo_subset_Ioo_left (h : a₁ ≤ a₂) : Ioo a₂ b ⊆ Ioo a₁ b := Ioo_subset_Ioo h (le_refl _) lemma Ioo_subset_Ioo_right (h : b₁ ≤ b₂) : Ioo a b₁ ⊆ Ioo a b₂ := Ioo_subset_Ioo (le_refl _) h lemma Ico_subset_Ico (h₁ : a₂ ≤ a₁) (h₂ : b₁ ≤ b₂) : Ico a₁ b₁ ⊆ Ico a₂ b₂ := λ x ⟨hx₁, hx₂⟩, ⟨le_trans h₁ hx₁, lt_of_lt_of_le hx₂ h₂⟩ lemma Ico_subset_Ico_left (h : a₁ ≤ a₂) : Ico a₂ b ⊆ Ico a₁ b := Ico_subset_Ico h (le_refl _) lemma Ico_subset_Ico_right (h : b₁ ≤ b₂) : Ico a b₁ ⊆ Ico a b₂ := Ico_subset_Ico (le_refl _) h lemma Icc_subset_Icc (h₁ : a₂ ≤ a₁) (h₂ : b₁ ≤ b₂) : Icc a₁ b₁ ⊆ Icc a₂ b₂ := λ x ⟨hx₁, hx₂⟩, ⟨le_trans h₁ hx₁, le_trans hx₂ h₂⟩ lemma Icc_subset_Icc_left (h : a₁ ≤ a₂) : Icc a₂ b ⊆ Icc a₁ b := Icc_subset_Icc h (le_refl _) lemma Icc_subset_Icc_right (h : b₁ ≤ b₂) : Icc a b₁ ⊆ Icc a b₂ := Icc_subset_Icc (le_refl _) h lemma Ioc_subset_Ioc (h₁ : a₂ ≤ a₁) (h₂ : b₁ ≤ b₂) : Ioc a₁ b₁ ⊆ Ioc a₂ b₂ := λ x ⟨hx₁, hx₂⟩, ⟨lt_of_le_of_lt h₁ hx₁, le_trans hx₂ h₂⟩ lemma Ioc_subset_Ioc_left (h : a₁ ≤ a₂) : Ioc a₂ b ⊆ Ioc a₁ b := Ioc_subset_Ioc h (le_refl _) lemma Ioc_subset_Ioc_right (h : b₁ ≤ b₂) : Ioc a b₁ ⊆ Ioc a b₂ := Ioc_subset_Ioc (le_refl _) h lemma Ico_subset_Ioo_left (h₁ : a₁ < a₂) : Ico a₂ b ⊆ Ioo a₁ b := λ x, and.imp_left $ lt_of_lt_of_le h₁ lemma Icc_subset_Ico_right (h₁ : b₁ < b₂) : Icc a b₁ ⊆ Ico a b₂ := λ x, and.imp_right $ λ h₂, lt_of_le_of_lt h₂ h₁ lemma Ioo_subset_Ico_self : Ioo a b ⊆ Ico a b := λ x, and.imp_left le_of_lt lemma Ioo_subset_Ioc_self : Ioo a b ⊆ Ioc a b := λ x, and.imp_right le_of_lt lemma Ico_subset_Icc_self : Ico a b ⊆ Icc a b := λ x, and.imp_right le_of_lt lemma Ioc_subset_Icc_self : Ioc a b ⊆ Icc a b := λ x, and.imp_left le_of_lt lemma Ioo_subset_Icc_self : Ioo a b ⊆ Icc a b := subset.trans Ioo_subset_Ico_self Ico_subset_Icc_self lemma Ico_subset_Iio_self : Ico a b ⊆ Iio b := λ x, and.right lemma Ioo_subset_Iio_self : Ioo a b ⊆ Iio b := λ x, and.right lemma Ioc_subset_Ioi_self : Ioc a b ⊆ Ioi a := λ x, and.left lemma Ioo_subset_Ioi_self : Ioo a b ⊆ Ioi a := λ x, and.left lemma Ioi_subset_Ici_self : Ioi a ⊆ Ici a := λx hx, le_of_lt hx lemma Iio_subset_Iic_self : Iio a ⊆ Iic a := λx hx, le_of_lt hx lemma Icc_subset_Icc_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Icc a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ ≤ b₂ := ⟨λ h, ⟨(h ⟨le_refl _, h₁⟩).1, (h ⟨h₁, le_refl _⟩).2⟩, λ ⟨h, h'⟩ x ⟨hx, hx'⟩, ⟨le_trans h hx, le_trans hx' h'⟩⟩ lemma Icc_subset_Ioo_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ioo a₂ b₂ ↔ a₂ < a₁ ∧ b₁ < b₂ := ⟨λ h, ⟨(h ⟨le_refl _, h₁⟩).1, (h ⟨h₁, le_refl _⟩).2⟩, λ ⟨h, h'⟩ x ⟨hx, hx'⟩, ⟨lt_of_lt_of_le h hx, lt_of_le_of_lt hx' h'⟩⟩ lemma Icc_subset_Ico_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ico a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ < b₂ := ⟨λ h, ⟨(h ⟨le_refl _, h₁⟩).1, (h ⟨h₁, le_refl _⟩).2⟩, λ ⟨h, h'⟩ x ⟨hx, hx'⟩, ⟨le_trans h hx, lt_of_le_of_lt hx' h'⟩⟩ lemma Icc_subset_Ioc_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ioc a₂ b₂ ↔ a₂ < a₁ ∧ b₁ ≤ b₂ := ⟨λ h, ⟨(h ⟨le_refl _, h₁⟩).1, (h ⟨h₁, le_refl _⟩).2⟩, λ ⟨h, h'⟩ x ⟨hx, hx'⟩, ⟨lt_of_lt_of_le h hx, le_trans hx' h'⟩⟩ lemma Icc_subset_Iio_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Iio b₂ ↔ b₁ < b₂ := ⟨λ h, h ⟨h₁, le_refl _⟩, λ h x ⟨hx, hx'⟩, lt_of_le_of_lt hx' h⟩ lemma Icc_subset_Ioi_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ioi a₂ ↔ a₂ < a₁ := ⟨λ h, h ⟨le_refl _, h₁⟩, λ h x ⟨hx, hx'⟩, lt_of_lt_of_le h hx⟩ lemma Icc_subset_Iic_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Iic b₂ ↔ b₁ ≤ b₂ := ⟨λ h, h ⟨h₁, le_refl _⟩, λ h x ⟨hx, hx'⟩, le_trans hx' h⟩ lemma Icc_subset_Ici_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ici a₂ ↔ a₂ ≤ a₁ := ⟨λ h, h ⟨le_refl _, h₁⟩, λ h x ⟨hx, hx'⟩, le_trans h hx⟩ /-- If `a ≤ b`, then `(b, +∞) ⊆ (a, +∞)`. In preorders, this is just an implication. If you need the equivalence in linear orders, use `Ioi_subset_Ioi_iff`. -/ lemma Ioi_subset_Ioi (h : a ≤ b) : Ioi b ⊆ Ioi a := λx hx, lt_of_le_of_lt h hx /-- If `a ≤ b`, then `(b, +∞) ⊆ [a, +∞)`. In preorders, this is just an implication. If you need the equivalence in dense linear orders, use `Ioi_subset_Ici_iff`. -/ lemma Ioi_subset_Ici (h : a ≤ b) : Ioi b ⊆ Ici a := subset.trans (Ioi_subset_Ioi h) Ioi_subset_Ici_self /-- If `a ≤ b`, then `(-∞, a) ⊆ (-∞, b)`. In preorders, this is just an implication. If you need the equivalence in linear orders, use `Iio_subset_Iio_iff`. -/ lemma Iio_subset_Iio (h : a ≤ b) : Iio a ⊆ Iio b := λx hx, lt_of_lt_of_le hx h /-- If `a ≤ b`, then `(-∞, a) ⊆ (-∞, b]`. In preorders, this is just an implication. If you need the equivalence in dense linear orders, use `Iio_subset_Iic_iff`. -/ lemma Iio_subset_Iic (h : a ≤ b) : Iio a ⊆ Iic b := subset.trans (Iio_subset_Iio h) Iio_subset_Iic_self lemma Ici_inter_Iic : Ici a ∩ Iic b = Icc a b := rfl lemma Ici_inter_Iio : Ici a ∩ Iio b = Ico a b := rfl lemma Ioi_inter_Iic : Ioi a ∩ Iic b = Ioc a b := rfl lemma Ioi_inter_Iio : Ioi a ∩ Iio b = Ioo a b := rfl end intervals section partial_order variables {α : Type u} [partial_order α] {a b : α} @[simp] lemma Icc_self (a : α) : Icc a a = {a} := set.ext $ by simp [Icc, le_antisymm_iff, and_comm] lemma Ico_diff_Ioo_eq_singleton (h : a < b) : Ico a b \ Ioo a b = {a} := set.ext $ λ x, begin simp [not_and'], split, { rintro ⟨⟨ax, xb⟩, hne⟩, exact (eq_or_lt_of_le ax).elim eq.symm (λ h', absurd h' (hne xb)) }, { rintro rfl, exact ⟨⟨le_refl _, h⟩, λ _, lt_irrefl x⟩ } end lemma Ioc_diff_Ioo_eq_singleton (h : a < b) : Ioc a b \ Ioo a b = {b} := set.ext $ λ x, begin simp, split, { rintro ⟨⟨ax, xb⟩, hne⟩, exact (eq_or_lt_of_le xb).elim id (λ h', absurd h' (hne ax)) }, { rintro rfl, exact ⟨⟨h, le_refl _⟩, λ _, lt_irrefl x⟩ } end lemma Icc_diff_Ico_eq_singleton (h : a ≤ b) : Icc a b \ Ico a b = {b} := set.ext $ λ x, begin simp, split, { rintro ⟨⟨ax, xb⟩, h⟩, exact classical.by_contradiction (λ ne, h ax (lt_of_le_of_ne xb ne)) }, { rintro rfl, exact ⟨⟨h, le_refl _⟩, λ _, lt_irrefl x⟩ } end lemma Icc_diff_Ioc_eq_singleton (h : a ≤ b) : Icc a b \ Ioc a b = {a} := set.ext $ λ x, begin simp [not_and'], split, { rintro ⟨⟨ax, xb⟩, h⟩, exact classical.by_contradiction (λ hne, h xb (lt_of_le_of_ne ax (ne.symm hne))) }, { rintro rfl, exact ⟨⟨le_refl _, h⟩, λ _, lt_irrefl x⟩ } end end partial_order section linear_order variables {α : Type u} [linear_order α] {a a₁ a₂ b b₁ b₂ : α} lemma compl_Iic : -(Iic a) = Ioi a := ext $ λ _, not_le lemma compl_Ici : -(Ici a) = Iio a := ext $ λ _, not_le lemma compl_Iio : -(Iio a) = Ici a := ext $ λ _, not_lt lemma compl_Ioi : -(Ioi a) = Iic a := ext $ λ _, not_lt lemma Ioo_eq_empty_iff [densely_ordered α] : Ioo a b = ∅ ↔ b ≤ a := ⟨λ eq, le_of_not_lt $ λ h, let ⟨x, h₁, h₂⟩ := dense h in eq_empty_iff_forall_not_mem.1 eq x ⟨h₁, h₂⟩, Ioo_eq_empty⟩ lemma Ico_eq_empty_iff : Ico a b = ∅ ↔ b ≤ a := ⟨λ eq, le_of_not_lt $ λ h, eq_empty_iff_forall_not_mem.1 eq a ⟨le_refl _, h⟩, Ico_eq_empty⟩ lemma Icc_eq_empty_iff : Icc a b = ∅ ↔ b < a := ⟨λ eq, lt_of_not_ge $ λ h, eq_empty_iff_forall_not_mem.1 eq a ⟨le_refl _, h⟩, Icc_eq_empty⟩ lemma Ico_subset_Ico_iff (h₁ : a₁ < b₁) : Ico a₁ b₁ ⊆ Ico a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ ≤ b₂ := ⟨λ h, have a₂ ≤ a₁ ∧ a₁ < b₂ := h ⟨le_refl _, h₁⟩, ⟨this.1, le_of_not_lt $ λ h', lt_irrefl b₂ (h ⟨le_of_lt this.2, h'⟩).2⟩, λ ⟨h₁, h₂⟩, Ico_subset_Ico h₁ h₂⟩ lemma Ioo_subset_Ioo_iff [densely_ordered α] (h₁ : a₁ < b₁) : Ioo a₁ b₁ ⊆ Ioo a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ ≤ b₂ := ⟨λ h, begin rcases dense h₁ with ⟨x, xa, xb⟩, split; refine le_of_not_lt (λ h', _), { have ab := lt_trans (h ⟨xa, xb⟩).1 xb, exact lt_irrefl _ (h ⟨h', ab⟩).1 }, { have ab := lt_trans xa (h ⟨xa, xb⟩).2, exact lt_irrefl _ (h ⟨ab, h'⟩).2 } end, λ ⟨h₁, h₂⟩, Ioo_subset_Ioo h₁ h₂⟩ lemma Ico_eq_Ico_iff (h : a₁ < b₁ ∨ a₂ < b₂) : Ico a₁ b₁ = Ico a₂ b₂ ↔ a₁ = a₂ ∧ b₁ = b₂ := ⟨λ e, begin simp [subset.antisymm_iff] at e, simp [le_antisymm_iff], cases h; simp [Ico_subset_Ico_iff h] at e; [ rcases e with ⟨⟨h₁, h₂⟩, e'⟩, rcases e with ⟨e', ⟨h₁, h₂⟩⟩ ]; have := (Ico_subset_Ico_iff (lt_of_le_of_lt h₁ $ lt_of_lt_of_le h h₂)).1 e'; tauto end, λ ⟨h₁, h₂⟩, by rw [h₁, h₂]⟩ open_locale classical @[simp] lemma Ioi_subset_Ioi_iff : Ioi b ⊆ Ioi a ↔ a ≤ b := begin refine ⟨λh, _, λh, Ioi_subset_Ioi h⟩, by_contradiction ba, exact lt_irrefl _ (h (not_le.mp ba)) end @[simp] lemma Ioi_subset_Ici_iff [densely_ordered α] : Ioi b ⊆ Ici a ↔ a ≤ b := begin refine ⟨λh, _, λh, Ioi_subset_Ici h⟩, by_contradiction ba, obtain ⟨c, bc, ca⟩ : ∃c, b < c ∧ c < a := dense (not_le.mp ba), exact lt_irrefl _ (lt_of_lt_of_le ca (h bc)) end @[simp] lemma Iio_subset_Iio_iff : Iio a ⊆ Iio b ↔ a ≤ b := begin refine ⟨λh, _, λh, Iio_subset_Iio h⟩, by_contradiction ab, exact lt_irrefl _ (h (not_le.mp ab)) end @[simp] lemma Iio_subset_Iic_iff [densely_ordered α] : Iio a ⊆ Iic b ↔ a ≤ b := begin refine ⟨λh, _, λh, Iio_subset_Iic h⟩, by_contradiction ba, obtain ⟨c, bc, ca⟩ : ∃c, b < c ∧ c < a := dense (not_le.mp ba), exact lt_irrefl _ (lt_of_lt_of_le bc (h ca)) end /-! ### Unions of adjacent intervals -/ /-! #### Two infinite intervals -/ @[simp] lemma Iic_union_Ici : Iic a ∪ Ici a = univ := eq_univ_of_forall (λ x, le_total x a) @[simp] lemma Iio_union_Ici : Iio a ∪ Ici a = univ := eq_univ_of_forall (λ x, lt_or_le x a) @[simp] lemma Iic_union_Ioi : Iic a ∪ Ioi a = univ := eq_univ_of_forall (λ x, le_or_lt x a) /-! #### A finite and an infinite interval -/ @[simp] lemma Ioc_union_Ici_eq_Ioi (h : a < b) : Ioc a b ∪ Ici b = Ioi a := ext $ λ x, ⟨λ hx, hx.elim and.left (lt_of_lt_of_le h), λ hx, (le_total x b).elim (λ hxb, or.inl ⟨hx, hxb⟩) (λ hxb, or.inr hxb)⟩ @[simp] lemma Icc_union_Ici_eq_Ioi (h : a ≤ b) : Icc a b ∪ Ici b = Ici a := ext $ λ x, ⟨λ hx, hx.elim and.left (le_trans h), λ hx, (le_total x b).elim (λ hxb, or.inl ⟨hx, hxb⟩) (λ hxb, or.inr hxb)⟩ @[simp] lemma Ioo_union_Ici_eq_Ioi (h : a < b) : Ioo a b ∪ Ici b = Ioi a := ext $ λ x, ⟨λ hx, hx.elim and.left (lt_of_lt_of_le h), λ hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx, hxb⟩) (λ hxb, or.inr hxb)⟩ @[simp] lemma Ico_union_Ici_eq_Ioi (h : a ≤ b) : Ico a b ∪ Ici b = Ici a := ext $ λ x, ⟨λ hx, hx.elim and.left (le_trans h), λ hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx, hxb⟩) (λ hxb, or.inr hxb)⟩ @[simp] lemma Ioc_union_Ioi_eq_Ioi (h : a ≤ b) : Ioc a b ∪ Ioi b = Ioi a := ext $ λ x, ⟨λ hx, hx.elim and.left (lt_of_le_of_lt h), λ hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx, hxb⟩) (λ hxb, or.inr hxb)⟩ @[simp] lemma Icc_union_Ioi_eq_Ioi (h : a ≤ b) : Icc a b ∪ Ioi b = Ici a := ext $ λ x, ⟨λ hx, hx.elim and.left (λ hx, le_trans h (le_of_lt hx)), λ hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx, hxb⟩) (λ hxb, or.inr hxb)⟩ /-! #### An infinite and a finite interval -/ @[simp] lemma Iic_union_Icc_eq_Iic (h : a ≤ b) : Iic a ∪ Icc a b = Iic b := ext $ λ x, ⟨λ hx, hx.elim (λ hx, le_trans hx h) and.right, λ hx, (le_total x a).elim (λ hxa, or.inl hxa) (λ hxa, or.inr ⟨hxa, hx⟩)⟩ @[simp] lemma Iic_union_Ico_eq_Iio (h : a < b) : Iic a ∪ Ico a b = Iio b := ext $ λ x, ⟨λ hx, hx.elim (λ hx, lt_of_le_of_lt hx h) and.right, λ hx, (le_total x a).elim (λ hxa, or.inl hxa) (λ hxa, or.inr ⟨hxa, hx⟩)⟩ @[simp] lemma Iio_union_Icc_eq_Iic (h : a ≤ b) : Iio a ∪ Icc a b = Iic b := ext $ λ x, ⟨λ hx, hx.elim (λ hx, le_trans (le_of_lt hx) h) and.right, λ hx, (lt_or_le x a).elim (λ hxa, or.inl hxa) (λ hxa, or.inr ⟨hxa, hx⟩)⟩ @[simp] lemma Iio_union_Ico_eq_Iio (h : a ≤ b) : Iio a ∪ Ico a b = Iio b := ext $ λ x, ⟨λ hx, hx.elim (λ hx, lt_of_lt_of_le hx h) and.right, λ hx, (lt_or_le x a).elim (λ hxa, or.inl hxa) (λ hxa, or.inr ⟨hxa, hx⟩)⟩ @[simp] lemma Iic_union_Ioc_eq_Iic (h : a ≤ b) : Iic a ∪ Ioc a b = Iic b := ext $ λ x, ⟨λ hx, hx.elim (λ hx, le_trans hx h) and.right, λ hx, (le_or_lt x a).elim (λ hxa, or.inl hxa) (λ hxa, or.inr ⟨hxa, hx⟩)⟩ @[simp] lemma Iic_union_Ioo_eq_Iio (h : a < b) : Iic a ∪ Ioo a b = Iio b := ext $ λ x, ⟨λ hx, hx.elim (λ hx, lt_of_le_of_lt hx h) and.right, λ hx, (le_or_lt x a).elim (λ hxa, or.inl hxa) (λ hxa, or.inr ⟨hxa, hx⟩)⟩ /-! #### Two finite intervals with a common point -/ @[simp] lemma Ioc_union_Ico_eq_Ioo {c} (h₁ : a < b) (h₂ : b < c) : Ioc a b ∪ Ico b c = Ioo a c := ext $ λ x, ⟨λ hx, hx.elim (λ hx, ⟨hx.1, lt_of_le_of_lt hx.2 h₂⟩) (λ hx, ⟨lt_of_lt_of_le h₁ hx.1, hx.2⟩), λ hx, (le_total x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)⟩ @[simp] lemma Icc_union_Ico_eq_Ico {c} (h₁ : a ≤ b) (h₂ : b < c) : Icc a b ∪ Ico b c = Ico a c := ext $ λ x, ⟨λ hx, hx.elim (λ hx, ⟨hx.1, lt_of_le_of_lt hx.2 h₂⟩) (λ hx, ⟨le_trans h₁ hx.1, hx.2⟩), λ hx, (le_total x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)⟩ @[simp] lemma Icc_union_Icc_eq_Icc {c} (h₁ : a ≤ b) (h₂ : b ≤ c) : Icc a b ∪ Icc b c = Icc a c := ext $ λ x, ⟨λ hx, hx.elim (λ hx, ⟨hx.1, le_trans hx.2 h₂⟩) (λ hx, ⟨le_trans h₁ hx.1, hx.2⟩), λ hx, (le_total x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)⟩ @[simp] lemma Ioc_union_Icc_eq_Ioc {c} (h₁ : a < b) (h₂ : b ≤ c) : Ioc a b ∪ Icc b c = Ioc a c := ext $ λ x, ⟨λ hx, hx.elim (λ hx, ⟨hx.1, le_trans hx.2 h₂⟩) (λ hx, ⟨lt_of_lt_of_le h₁ hx.1, hx.2⟩), λ hx, (le_total x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)⟩ /-! #### Two finite intervals, `I?o` and `Ic?` -/ @[simp] lemma Ioo_union_Ico_eq_Ioo {c} (h₁ : a < b) (h₂ : b ≤ c) : Ioo a b ∪ Ico b c = Ioo a c := ext $ λ x, ⟨λ hx, hx.elim (λ hx, ⟨hx.1, lt_of_lt_of_le hx.2 h₂⟩) (λ hx, ⟨lt_of_lt_of_le h₁ hx.1, hx.2⟩), λ hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)⟩ @[simp] lemma Ico_union_Ico_eq_Ico {c} (h₁ : a ≤ b) (h₂ : b ≤ c) : Ico a b ∪ Ico b c = Ico a c := ext $ λ x, ⟨λ hx, hx.elim (λ hx, ⟨hx.1, lt_of_lt_of_le hx.2 h₂⟩) (λ hx, ⟨le_trans h₁ hx.1, hx.2⟩), λ hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)⟩ @[simp] lemma Ico_union_Icc_eq_Icc {c} (h₁ : a ≤ b) (h₂ : b ≤ c) : Ico a b ∪ Icc b c = Icc a c := ext $ λ x, ⟨λ hx, hx.elim (λ hx, ⟨hx.1, le_trans (le_of_lt hx.2) h₂⟩) (λ hx, ⟨le_trans h₁ hx.1, hx.2⟩), λ hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)⟩ @[simp] lemma Ioo_union_Icc_eq_Ioc {c} (h₁ : a < b) (h₂ : b ≤ c) : Ioo a b ∪ Icc b c = Ioc a c := ext $ λ x, ⟨λ hx, hx.elim (λ hx, ⟨hx.1, le_trans (le_of_lt hx.2) h₂⟩) (λ hx, ⟨lt_of_lt_of_le h₁ hx.1, hx.2⟩), λ hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)⟩ /-! #### Two finite intervals, `I?c` and `Io?` -/ @[simp] lemma Ioc_union_Ioo_eq_Ioo {c} (h₁ : a ≤ b) (h₂ : b < c) : Ioc a b ∪ Ioo b c = Ioo a c := ext $ λ x, ⟨λ hx, hx.elim (λ hx, ⟨hx.1, lt_of_le_of_lt hx.2 h₂⟩) (λ hx, ⟨lt_of_le_of_lt h₁ hx.1, hx.2⟩), λ hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)⟩ @[simp] lemma Icc_union_Ioo_eq_Ico {c} (h₁ : a ≤ b) (h₂ : b < c) : Icc a b ∪ Ioo b c = Ico a c := ext $ λ x, ⟨λ hx, hx.elim (λ hx, ⟨hx.1, lt_of_le_of_lt hx.2 h₂⟩) (λ hx, ⟨le_trans h₁ (le_of_lt hx.1), hx.2⟩), λ hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)⟩ @[simp] lemma Icc_union_Ioc_eq_Icc {c} (h₁ : a ≤ b) (h₂ : b ≤ c) : Icc a b ∪ Ioc b c = Icc a c := ext $ λ x, ⟨λ hx, hx.elim (λ hx, ⟨hx.1, le_trans hx.2 h₂⟩) (λ hx, ⟨le_trans h₁ (le_of_lt hx.1), hx.2⟩), λ hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)⟩ @[simp] lemma Ioc_union_Ioc_eq_Ioc {c} (h₁ : a ≤ b) (h₂ : b ≤ c) : Ioc a b ∪ Ioc b c = Ioc a c := ext $ λ x, ⟨λ hx, hx.elim (λ hx, ⟨hx.1, le_trans hx.2 h₂⟩) (λ hx, ⟨lt_of_le_of_lt h₁ hx.1, hx.2⟩), λ hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩)⟩ end linear_order section lattice section inf variables {α : Type u} [semilattice_inf α] @[simp] lemma Iic_inter_Iic {a b : α} : Iic a ∩ Iic b = Iic (a ⊓ b) := by { ext x, simp [Iic] } @[simp] lemma Iio_inter_Iio [is_total α (≤)] {a b : α} : Iio a ∩ Iio b = Iio (a ⊓ b) := by { ext x, simp [Iio] } end inf section sup variables {α : Type u} [semilattice_sup α] @[simp] lemma Ici_inter_Ici {a b : α} : Ici a ∩ Ici b = Ici (a ⊔ b) := by { ext x, simp [Ici] } @[simp] lemma Ioi_inter_Ioi [is_total α (≤)] {a b : α} : Ioi a ∩ Ioi b = Ioi (a ⊔ b) := by { ext x, simp [Ioi] } end sup section both variables {α : Type u} [lattice α] [ht : is_total α (≤)] {a b c a₁ a₂ b₁ b₂ : α} lemma Icc_inter_Icc : Icc a₁ b₁ ∩ Icc a₂ b₂ = Icc (a₁ ⊔ a₂) (b₁ ⊓ b₂) := by simp only [Ici_inter_Iic.symm, Ici_inter_Ici.symm, Iic_inter_Iic.symm]; ac_refl @[simp] lemma Icc_inter_Icc_eq_singleton (hab : a ≤ b) (hbc : b ≤ c) : Icc a b ∩ Icc b c = {b} := begin rw [Icc_inter_Icc], convert Icc_self b, exact sup_of_le_right hab, exact inf_of_le_left hbc end include ht lemma Ico_inter_Ico : Ico a₁ b₁ ∩ Ico a₂ b₂ = Ico (a₁ ⊔ a₂) (b₁ ⊓ b₂) := by simp only [Ici_inter_Iio.symm, Ici_inter_Ici.symm, Iio_inter_Iio.symm]; ac_refl lemma Ioc_inter_Ioc : Ioc a₁ b₁ ∩ Ioc a₂ b₂ = Ioc (a₁ ⊔ a₂) (b₁ ⊓ b₂) := by simp only [Ioi_inter_Iic.symm, Ioi_inter_Ioi.symm, Iic_inter_Iic.symm]; ac_refl lemma Ioo_inter_Ioo : Ioo a₁ b₁ ∩ Ioo a₂ b₂ = Ioo (a₁ ⊔ a₂) (b₁ ⊓ b₂) := by simp only [Ioi_inter_Iio.symm, Ioi_inter_Ioi.symm, Iio_inter_Iio.symm]; ac_refl end both end lattice section decidable_linear_order variables {α : Type u} [decidable_linear_order α] {a a₁ a₂ b b₁ b₂ : α} @[simp] lemma Ico_diff_Iio {a b c : α} : Ico a b \ Iio c = Ico (max a c) b := set.ext $ by simp [Ico, Iio, iff_def, max_le_iff] {contextual:=tt} @[simp] lemma Ico_inter_Iio {a b c : α} : Ico a b ∩ Iio c = Ico a (min b c) := set.ext $ by simp [Ico, Iio, iff_def, lt_min_iff] {contextual:=tt} end decidable_linear_order section ordered_add_comm_group variables {α : Type u} [ordered_add_comm_group α] lemma image_add_left_Icc (a b c : α) : ((+) a) '' Icc b c = Icc (a + b) (a + c) := begin ext x, split, { rintros ⟨x, hx, rfl⟩, exact ⟨add_le_add_left hx.1 a, add_le_add_left hx.2 a⟩}, { intro hx, refine ⟨-a + x, _, add_neg_cancel_left _ _⟩, exact ⟨le_neg_add_iff_add_le.2 hx.1, neg_add_le_iff_le_add.2 hx.2⟩ } end lemma image_add_right_Icc (a b c : α) : (λ x, x + c) '' Icc a b = Icc (a + c) (b + c) := by convert image_add_left_Icc c a b using 1; simp only [add_comm _ c] lemma image_neg_Iio (r : α) : image (λz, -z) (Iio r) = Ioi (-r) := begin ext z, apply iff.intro, { intros hz, apply exists.elim hz, intros z' hz', rw [←hz'.2], simp only [mem_Ioi, neg_lt_neg_iff], exact hz'.1 }, { intros hz, simp only [mem_image, mem_Iio], use -z, simp [hz], exact neg_lt.1 hz } end lemma image_neg_Iic (r : α) : image (λz, -z) (Iic r) = Ici (-r) := begin apply set.ext, intros z, apply iff.intro, { intros hz, apply exists.elim hz, intros z' hz', rw [←hz'.2], simp only [neg_le_neg_iff, mem_Ici], exact hz'.1 }, { intros hz, simp only [mem_image, mem_Iic], use -z, simp [hz], exact neg_le.1 hz } end end ordered_add_comm_group section decidable_linear_ordered_add_comm_group variables {α : Type u} [decidable_linear_ordered_add_comm_group α] /-- If we remove a smaller interval from a larger, the result is nonempty -/ lemma nonempty_Ico_sdiff {x dx y dy : α} (h : dy < dx) (hx : 0 < dx) : nonempty ↥(Ico x (x + dx) \ Ico y (y + dy)) := begin cases lt_or_le x y with h' h', { use x, simp* }, { use max x (x + dy), simp [*, le_refl] } end end decidable_linear_ordered_add_comm_group section linear_ordered_field variables {α : Type u} [linear_ordered_field α] lemma image_mul_right_Icc' (a b : α) {c : α} (h : 0 < c) : (λ x, x * c) '' Icc a b = Icc (a * c) (b * c) := begin ext x, split, { rintros ⟨x, hx, rfl⟩, exact ⟨mul_le_mul_of_nonneg_right hx.1 (le_of_lt h), mul_le_mul_of_nonneg_right hx.2 (le_of_lt h)⟩ }, { intro hx, refine ⟨x / c, _, div_mul_cancel x (ne_of_gt h)⟩, exact ⟨le_div_of_mul_le h hx.1, div_le_of_le_mul h (mul_comm b c ▸ hx.2)⟩ } end lemma image_mul_right_Icc {a b c : α} (hab : a ≤ b) (hc : 0 ≤ c) : (λ x, x * c) '' Icc a b = Icc (a * c) (b * c) := begin cases eq_or_lt_of_le hc, { subst c, simp [(nonempty_Icc.2 hab).image_const] }, exact image_mul_right_Icc' a b ‹0 < c› end lemma image_mul_left_Icc' {a : α} (h : 0 < a) (b c : α) : ((*) a) '' Icc b c = Icc (a * b) (a * c) := by { convert image_mul_right_Icc' b c h using 1; simp only [mul_comm _ a] } lemma image_mul_left_Icc {a b c : α} (ha : 0 ≤ a) (hbc : b ≤ c) : ((*) a) '' Icc b c = Icc (a * b) (a * c) := by { convert image_mul_right_Icc hbc ha using 1; simp only [mul_comm _ a] } end linear_ordered_field end set
6b8843ca32999dbb30362b7e03f5d44795c02684
82e44445c70db0f03e30d7be725775f122d72f3e
/src/data/list/duplicate.lean
21b0e8339cb9f5a6dc52d6e463b2662478dc4812
[ "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
4,339
lean
/- Copyright (c) 2021 Yakov Pechersky. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yakov Pechersky, Chris Hughes -/ import data.list.nodup import data.fin /-! # List duplicates ## Main definitions * `list.duplicate x l : Prop` is an inductive property that holds when `x` is a duplicate in `l` ## Implementation details In this file, `x ∈+ l` notation is shorthand for `list.duplicate x l`. -/ variable {α : Type*} namespace list /-- Property that an element `x : α` of `l : list α` can be found in the list more than once. -/ inductive duplicate (x : α) : list α → Prop | cons_mem {l : list α} : x ∈ l → duplicate (x :: l) | cons_duplicate {y : α} {l : list α} : duplicate l → duplicate (y :: l) local infix ` ∈+ `:50 := list.duplicate variables {l : list α} {x : α} lemma mem.duplicate_cons_self (h : x ∈ l) : x ∈+ x :: l := duplicate.cons_mem h lemma duplicate.duplicate_cons (h : x ∈+ l) (y : α) : x ∈+ y :: l := duplicate.cons_duplicate h lemma duplicate.mem (h : x ∈+ l) : x ∈ l := begin induction h with l' h y l' h hm, { exact mem_cons_self _ _ }, { exact mem_cons_of_mem _ hm } end lemma duplicate.mem_cons_self (h : x ∈+ x :: l) : x ∈ l := begin cases h with _ h _ _ h, { exact h }, { exact h.mem } end @[simp] lemma duplicate_cons_self_iff : x ∈+ x :: l ↔ x ∈ l := ⟨duplicate.mem_cons_self, mem.duplicate_cons_self⟩ lemma duplicate.ne_nil (h : x ∈+ l) : l ≠ [] := λ H, (mem_nil_iff x).mp (H ▸ h.mem) @[simp] lemma not_duplicate_nil (x : α) : ¬ x ∈+ [] := λ H, H.ne_nil rfl lemma duplicate.ne_singleton (h : x ∈+ l) (y : α) : l ≠ [y] := begin induction h with l' h z l' h hm, { simp [ne_nil_of_mem h] }, { simp [ne_nil_of_mem h.mem] } end @[simp] lemma not_duplicate_singleton (x y : α) : ¬ x ∈+ [y] := λ H, H.ne_singleton _ rfl lemma duplicate.elim_nil (h : x ∈+ []) : false := not_duplicate_nil x h lemma duplicate.elim_singleton {y : α} (h : x ∈+ [y]) : false := not_duplicate_singleton x y h lemma duplicate_cons_iff {y : α} : x ∈+ y :: l ↔ (y = x ∧ x ∈ l) ∨ x ∈+ l := begin refine ⟨λ h, _, λ h, _⟩, { cases h with _ hm _ _ hm, { exact or.inl ⟨rfl, hm⟩ }, { exact or.inr hm } }, { rcases h with ⟨rfl|h⟩|h, { simpa }, { exact h.cons_duplicate } } end lemma duplicate.of_duplicate_cons {y : α} (h : x ∈+ y :: l) (hx : x ≠ y) : x ∈+ l := by simpa [duplicate_cons_iff, hx.symm] using h lemma duplicate_cons_iff_of_ne {y : α} (hne : x ≠ y) : x ∈+ y :: l ↔ x ∈+ l := by simp [duplicate_cons_iff, hne.symm] lemma duplicate.mono_sublist {l' : list α} (hx : x ∈+ l) (h : l <+ l') : x ∈+ l' := begin induction h with l₁ l₂ y h IH l₁ l₂ y h IH, { exact hx }, { exact (IH hx).duplicate_cons _ }, { rw duplicate_cons_iff at hx ⊢, rcases hx with ⟨rfl, hx⟩|hx, { simp [h.subset hx] }, { simp [IH hx] } } end /-- The contrapositive of `list.nodup_iff_sublist`. -/ lemma duplicate_iff_sublist : x ∈+ l ↔ [x, x] <+ l := begin induction l with y l IH, { simp }, { by_cases hx : x = y, { simp [hx, cons_sublist_cons_iff, singleton_sublist] }, { rw [duplicate_cons_iff_of_ne hx, IH], refine ⟨sublist_cons_of_sublist y, λ h, _⟩, cases h, { assumption }, { contradiction } } } end lemma nodup_iff_forall_not_duplicate : nodup l ↔ ∀ (x : α), ¬ x ∈+ l := by simp_rw [nodup_iff_sublist, duplicate_iff_sublist] lemma exists_duplicate_iff_not_nodup : (∃ (x : α), x ∈+ l) ↔ ¬ nodup l := by simp [nodup_iff_forall_not_duplicate] lemma duplicate.not_nodup (h : x ∈+ l) : ¬ nodup l := λ H, nodup_iff_forall_not_duplicate.mp H _ h lemma duplicate_iff_two_le_count [decidable_eq α] : (x ∈+ l) ↔ 2 ≤ count x l := by simp [duplicate_iff_sublist, le_count_iff_repeat_sublist] instance decidable_duplicate [decidable_eq α] (x : α) : ∀ (l : list α), decidable (x ∈+ l) | [] := is_false (not_duplicate_nil x) | (y :: l) := match decidable_duplicate l with | is_true h := is_true (h.duplicate_cons y) | is_false h := if hx : y = x ∧ x ∈ l then is_true (hx.left.symm ▸ hx.right.duplicate_cons_self) else is_false (by simpa [duplicate_cons_iff, h] using hx) end end list
0d5f0aaacd6301f2e4674afd68a1b8c6913062a9
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/hott/types/nat/order.hlean
71fac0d3b7569efcbccd58ce63e121406540204d
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
18,486
hlean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Leonardo de Moura, Jeremy Avigad The order relation on the natural numbers. -/ import .basic algebra.ordered_ring open eq eq.ops algebra algebra namespace nat /- lt and le -/ protected theorem le_of_lt_sum_eq {m n : ℕ} (H : m < n ⊎ m = n) : m ≤ n := nat.le_of_eq_sum_lt (sum.swap H) protected theorem lt_sum_eq_of_le {m n : ℕ} (H : m ≤ n) : m < n ⊎ m = n := sum.swap (nat.eq_sum_lt_of_le H) protected theorem le_iff_lt_sum_eq (m n : ℕ) : m ≤ n ↔ m < n ⊎ m = n := iff.intro nat.lt_sum_eq_of_le nat.le_of_lt_sum_eq protected theorem lt_of_le_prod_ne {m n : ℕ} (H1 : m ≤ n) : m ≠ n → m < n := sum_resolve_right (nat.eq_sum_lt_of_le H1) protected theorem lt_iff_le_prod_ne (m n : ℕ) : m < n ↔ m ≤ n × m ≠ n := iff.intro (take H, pair (nat.le_of_lt H) (take H1, !nat.lt_irrefl (H1 ▸ H))) (prod.rec nat.lt_of_le_prod_ne) theorem le_add_right (n k : ℕ) : n ≤ n + k := nat.rec !nat.le_refl (λ k, le_succ_of_le) k theorem le_add_left (n m : ℕ): n ≤ m + n := !add.comm ▸ !le_add_right theorem le.intro {n m k : ℕ} (h : n + k = m) : n ≤ m := h ▸ !le_add_right theorem le.elim {n m : ℕ} : n ≤ m → Σ k, n + k = m := le.rec (sigma.mk 0 rfl) (λm h, sigma.rec (λ k H, sigma.mk (succ k) (H ▸ rfl))) protected theorem le_total {m n : ℕ} : m ≤ n ⊎ n ≤ m := sum.imp_left nat.le_of_lt !nat.lt_sum_ge /- addition -/ protected theorem add_le_add_left {n m : ℕ} (H : n ≤ m) (k : ℕ) : k + n ≤ k + m := obtain l Hl, from le.elim H, le.intro (Hl ▸ !add.assoc) protected theorem add_le_add_right {n m : ℕ} (H : n ≤ m) (k : ℕ) : n + k ≤ m + k := !add.comm ▸ !add.comm ▸ nat.add_le_add_left H k protected theorem le_of_add_le_add_left {k n m : ℕ} (H : k + n ≤ k + m) : n ≤ m := obtain l Hl, from le.elim H, le.intro (nat.add_left_cancel (!add.assoc⁻¹ ⬝ Hl)) protected theorem lt_of_add_lt_add_left {k n m : ℕ} (H : k + n < k + m) : n < m := let H' := nat.le_of_lt H in nat.lt_of_le_prod_ne (nat.le_of_add_le_add_left H') (assume Heq, !nat.lt_irrefl (Heq ▸ H)) protected theorem add_lt_add_left {n m : ℕ} (H : n < m) (k : ℕ) : k + n < k + m := lt_of_succ_le (!add_succ ▸ nat.add_le_add_left (succ_le_of_lt H) k) protected theorem add_lt_add_right {n m : ℕ} (H : n < m) (k : ℕ) : n + k < m + k := !add.comm ▸ !add.comm ▸ nat.add_lt_add_left H k protected theorem lt_add_of_pos_right {n k : ℕ} (H : k > 0) : n < n + k := !add_zero ▸ nat.add_lt_add_left H n /- multiplication -/ theorem mul_le_mul_left {n m : ℕ} (k : ℕ) (H : n ≤ m) : k * n ≤ k * m := obtain (l : ℕ) (Hl : n + l = m), from le.elim H, have k * n + k * l = k * m, by rewrite [-left_distrib, Hl], le.intro this theorem mul_le_mul_right {n m : ℕ} (k : ℕ) (H : n ≤ m) : n * k ≤ m * k := !mul.comm ▸ !mul.comm ▸ !mul_le_mul_left H protected theorem mul_le_mul {n m k l : ℕ} (H1 : n ≤ k) (H2 : m ≤ l) : n * m ≤ k * l := nat.le_trans (!nat.mul_le_mul_right H1) (!nat.mul_le_mul_left H2) protected theorem mul_lt_mul_of_pos_left {n m k : ℕ} (H : n < m) (Hk : k > 0) : k * n < k * m := nat.lt_of_lt_of_le (nat.lt_add_of_pos_right Hk) (!mul_succ ▸ nat.mul_le_mul_left k (succ_le_of_lt H)) protected theorem mul_lt_mul_of_pos_right {n m k : ℕ} (H : n < m) (Hk : k > 0) : n * k < m * k := !mul.comm ▸ !mul.comm ▸ nat.mul_lt_mul_of_pos_left H Hk /- nat is an instance of a linearly ordered semiring and a lattice -/ protected definition decidable_linear_ordered_semiring [trans_instance] : decidable_linear_ordered_semiring nat := ⦃ decidable_linear_ordered_semiring, nat.comm_semiring, add_left_cancel := @nat.add_left_cancel, add_right_cancel := @nat.add_right_cancel, lt := nat.lt, le := nat.le, le_refl := nat.le_refl, le_trans := @nat.le_trans, le_antisymm := @nat.le_antisymm, le_total := @nat.le_total, le_iff_lt_sum_eq := @nat.le_iff_lt_sum_eq, le_of_lt := @nat.le_of_lt, lt_irrefl := @nat.lt_irrefl, lt_of_lt_of_le := @nat.lt_of_lt_of_le, lt_of_le_of_lt := @nat.lt_of_le_of_lt, lt_of_add_lt_add_left := @nat.lt_of_add_lt_add_left, add_lt_add_left := @nat.add_lt_add_left, add_le_add_left := @nat.add_le_add_left, le_of_add_le_add_left := @nat.le_of_add_le_add_left, zero_lt_one := zero_lt_succ 0, mul_le_mul_of_nonneg_left := (take a b c H1 H2, nat.mul_le_mul_left c H1), mul_le_mul_of_nonneg_right := (take a b c H1 H2, nat.mul_le_mul_right c H1), mul_lt_mul_of_pos_left := @nat.mul_lt_mul_of_pos_left, mul_lt_mul_of_pos_right := @nat.mul_lt_mul_of_pos_right, decidable_lt := nat.decidable_lt ⦄ definition nat_has_dvd [instance] [priority nat.prio] : has_dvd nat := has_dvd.mk has_dvd.dvd theorem add_pos_left {a : ℕ} (H : 0 < a) (b : ℕ) : 0 < a + b := @add_pos_of_pos_of_nonneg _ _ a b H !zero_le theorem add_pos_right {a : ℕ} (H : 0 < a) (b : ℕ) : 0 < b + a := by rewrite add.comm; apply add_pos_left H b theorem add_eq_zero_iff_eq_zero_prod_eq_zero {a b : ℕ} : a + b = 0 ↔ a = 0 × b = 0 := @add_eq_zero_iff_eq_zero_prod_eq_zero_of_nonneg_of_nonneg _ _ a b !zero_le !zero_le theorem le_add_of_le_left {a b c : ℕ} (H : b ≤ c) : b ≤ a + c := @le_add_of_nonneg_of_le _ _ a b c !zero_le H theorem le_add_of_le_right {a b c : ℕ} (H : b ≤ c) : b ≤ c + a := @le_add_of_le_of_nonneg _ _ a b c H !zero_le theorem lt_add_of_lt_left {b c : ℕ} (H : b < c) (a : ℕ) : b < a + c := @lt_add_of_nonneg_of_lt _ _ a b c !zero_le H theorem lt_add_of_lt_right {b c : ℕ} (H : b < c) (a : ℕ) : b < c + a := @lt_add_of_lt_of_nonneg _ _ a b c H !zero_le theorem lt_of_mul_lt_mul_left {a b c : ℕ} (H : c * a < c * b) : a < b := @lt_of_mul_lt_mul_left _ _ a b c H !zero_le theorem lt_of_mul_lt_mul_right {a b c : ℕ} (H : a * c < b * c) : a < b := @lt_of_mul_lt_mul_right _ _ a b c H !zero_le theorem pos_of_mul_pos_left {a b : ℕ} (H : 0 < a * b) : 0 < b := @pos_of_mul_pos_left _ _ a b H !zero_le theorem pos_of_mul_pos_right {a b : ℕ} (H : 0 < a * b) : 0 < a := @pos_of_mul_pos_right _ _ a b H !zero_le theorem zero_le_one : (0:nat) ≤ 1 := dec_star /- properties specific to nat -/ theorem lt_intro {n m k : ℕ} (H : succ n + k = m) : n < m := lt_of_succ_le (le.intro H) theorem lt_elim {n m : ℕ} (H : n < m) : Σk, succ n + k = m := le.elim (succ_le_of_lt H) theorem lt_add_succ (n m : ℕ) : n < n + succ m := lt_intro !succ_add_eq_succ_add theorem eq_zero_of_le_zero {n : ℕ} (H : n ≤ 0) : n = 0 := obtain (k : ℕ) (Hk : n + k = 0), from le.elim H, eq_zero_of_add_eq_zero_right Hk /- succ and pred -/ theorem le_of_lt_succ {m n : nat} : m < succ n → m ≤ n := le_of_succ_le_succ theorem lt_iff_succ_le (m n : nat) : m < n ↔ succ m ≤ n := iff.rfl theorem lt_succ_iff_le (m n : nat) : m < succ n ↔ m ≤ n := iff.intro le_of_lt_succ lt_succ_of_le theorem self_le_succ (n : ℕ) : n ≤ succ n := le.intro !add_one theorem succ_le_sum_eq_of_le {n m : ℕ} : n ≤ m → succ n ≤ m ⊎ n = m := lt_sum_eq_of_le theorem pred_le_of_le_succ {n m : ℕ} : n ≤ succ m → pred n ≤ m := pred_le_pred theorem succ_le_of_le_pred {n m : ℕ} : succ n ≤ m → n ≤ pred m := pred_le_pred theorem pred_le_pred_of_le {n m : ℕ} : n ≤ m → pred n ≤ pred m := pred_le_pred theorem pre_lt_of_lt {n m : ℕ} : n < m → pred n < m := lt_of_le_of_lt !pred_le theorem lt_of_pred_lt_pred {n m : ℕ} (H : pred n < pred m) : n < m := lt_of_not_ge (suppose m ≤ n, not_lt_of_ge (pred_le_pred_of_le this) H) theorem le_sum_eq_succ_of_le_succ {n m : ℕ} (H : n ≤ succ m) : n ≤ m ⊎ n = succ m := sum.imp_left le_of_succ_le_succ (succ_le_sum_eq_of_le H) theorem le_pred_self (n : ℕ) : pred n ≤ n := !pred_le theorem succ_pos (n : ℕ) : 0 < succ n := !zero_lt_succ theorem succ_pred_of_pos {n : ℕ} (H : n > 0) : succ (pred n) = n := (sum_resolve_right (eq_zero_sum_eq_succ_pred n) (ne.symm (ne_of_lt H)))⁻¹ theorem exists_eq_succ_of_lt {n : ℕ} : Π {m : ℕ}, n < m → Σk, m = succ k | 0 H := absurd H !not_lt_zero | (succ k) H := sigma.mk k rfl theorem lt_succ_self (n : ℕ) : n < succ n := lt.base n lemma lt_succ_of_lt {i j : nat} : i < j → i < succ j := assume Plt, lt.trans Plt (self_lt_succ j) /- other forms of induction -/ protected definition strong_rec_on {P : nat → Type} (n : ℕ) (H : Πn, (Πm, m < n → P m) → P n) : P n := nat.rec (λm h, absurd h !not_lt_zero) (λn' (IH : Π {m : ℕ}, m < n' → P m) m l, sum.elim (lt_sum_eq_of_le (le_of_lt_succ l)) IH (λ e, eq.rec (H n' @IH) e⁻¹)) (succ n) n !lt_succ_self protected theorem case_strong_rec_on {P : nat → Type} (a : nat) (H0 : P 0) (Hind : Π(n : nat), (Πm, m ≤ n → P m) → P (succ n)) : P a := nat.strong_rec_on a (take n, show (Π m, m < n → P m) → P n, from nat.cases_on n (suppose (Π m, m < 0 → P m), show P 0, from H0) (take n, suppose (Π m, m < succ n → P m), show P (succ n), from Hind n (take m, assume H1 : m ≤ n, this _ (lt_succ_of_le H1)))) /- pos -/ theorem by_cases_zero_pos {P : ℕ → Type} (y : ℕ) (H0 : P 0) (H1 : Π {y : nat}, y > 0 → P y) : P y := nat.cases_on y H0 (take y, H1 !succ_pos) theorem eq_zero_sum_pos (n : ℕ) : n = 0 ⊎ n > 0 := sum_of_sum_of_imp_left (sum.swap (lt_sum_eq_of_le !zero_le)) (suppose 0 = n, by subst n) theorem pos_of_ne_zero {n : ℕ} (H : n ≠ 0) : n > 0 := sum.elim !eq_zero_sum_pos (take H2 : n = 0, by contradiction) (take H2 : n > 0, H2) theorem ne_zero_of_pos {n : ℕ} (H : n > 0) : n ≠ 0 := ne.symm (ne_of_lt H) theorem exists_eq_succ_of_pos {n : ℕ} (H : n > 0) : Σl, n = succ l := exists_eq_succ_of_lt H theorem pos_of_dvd_of_pos {m n : ℕ} (H1 : m ∣ n) (H2 : n > 0) : m > 0 := pos_of_ne_zero (suppose m = 0, have n = 0, from eq_zero_of_zero_dvd (this ▸ H1), ne_of_lt H2 (by subst n)) /- multiplication -/ theorem mul_lt_mul_of_le_of_lt {n m k l : ℕ} (Hk : k > 0) (H1 : n ≤ k) (H2 : m < l) : n * m < k * l := lt_of_le_of_lt (mul_le_mul_right m H1) (mul_lt_mul_of_pos_left H2 Hk) theorem mul_lt_mul_of_lt_of_le {n m k l : ℕ} (Hl : l > 0) (H1 : n < k) (H2 : m ≤ l) : n * m < k * l := lt_of_le_of_lt (mul_le_mul_left n H2) (mul_lt_mul_of_pos_right H1 Hl) theorem mul_lt_mul_of_le_of_le {n m k l : ℕ} (H1 : n < k) (H2 : m < l) : n * m < k * l := have H3 : n * m ≤ k * m, from mul_le_mul_right m (le_of_lt H1), have H4 : k * m < k * l, from mul_lt_mul_of_pos_left H2 (lt_of_le_of_lt !zero_le H1), lt_of_le_of_lt H3 H4 theorem eq_of_mul_eq_mul_left {m k n : ℕ} (Hn : n > 0) (H : n * m = n * k) : m = k := have n * m ≤ n * k, by rewrite H, have m ≤ k, from le_of_mul_le_mul_left this Hn, have n * k ≤ n * m, by rewrite H, have k ≤ m, from le_of_mul_le_mul_left this Hn, le.antisymm `m ≤ k` this theorem eq_of_mul_eq_mul_right {n m k : ℕ} (Hm : m > 0) (H : n * m = k * m) : n = k := eq_of_mul_eq_mul_left Hm (!mul.comm ▸ !mul.comm ▸ H) theorem eq_zero_sum_eq_of_mul_eq_mul_left {n m k : ℕ} (H : n * m = n * k) : n = 0 ⊎ m = k := sum_of_sum_of_imp_right !eq_zero_sum_pos (assume Hn : n > 0, eq_of_mul_eq_mul_left Hn H) theorem eq_zero_sum_eq_of_mul_eq_mul_right {n m k : ℕ} (H : n * m = k * m) : m = 0 ⊎ n = k := eq_zero_sum_eq_of_mul_eq_mul_left (!mul.comm ▸ !mul.comm ▸ H) theorem eq_one_of_mul_eq_one_right {n m : ℕ} (H : n * m = 1) : n = 1 := have H2 : n * m > 0, by rewrite H; apply succ_pos, sum.elim (le_sum_gt n 1) (suppose n ≤ 1, have n > 0, from pos_of_mul_pos_right H2, show n = 1, from le.antisymm `n ≤ 1` (succ_le_of_lt this)) (suppose n > 1, have m > 0, from pos_of_mul_pos_left H2, have n * m ≥ 2 * 1, from nat.mul_le_mul (succ_le_of_lt `n > 1`) (succ_le_of_lt this), have 1 ≥ 2, from !mul_one ▸ H ▸ this, absurd !lt_succ_self (not_lt_of_ge this)) theorem eq_one_of_mul_eq_one_left {n m : ℕ} (H : n * m = 1) : m = 1 := eq_one_of_mul_eq_one_right (!mul.comm ▸ H) theorem eq_one_of_mul_eq_self_left {n m : ℕ} (Hpos : n > 0) (H : m * n = n) : m = 1 := eq_of_mul_eq_mul_right Hpos (H ⬝ !one_mul⁻¹) theorem eq_one_of_mul_eq_self_right {n m : ℕ} (Hpos : m > 0) (H : m * n = m) : n = 1 := eq_one_of_mul_eq_self_left Hpos (!mul.comm ▸ H) theorem eq_one_of_dvd_one {n : ℕ} (H : n ∣ 1) : n = 1 := dvd.elim H (take m, suppose 1 = n * m, eq_one_of_mul_eq_one_right this⁻¹) /- min and max -/ open decidable theorem min_zero [simp] (a : ℕ) : min a 0 = 0 := by rewrite [min_eq_right !zero_le] theorem zero_min [simp] (a : ℕ) : min 0 a = 0 := by rewrite [min_eq_left !zero_le] theorem max_zero [simp] (a : ℕ) : max a 0 = a := by rewrite [max_eq_left !zero_le] theorem zero_max [simp] (a : ℕ) : max 0 a = a := by rewrite [max_eq_right !zero_le] theorem min_succ_succ [simp] (a b : ℕ) : min (succ a) (succ b) = succ (min a b) := sum.elim !lt_sum_ge (suppose a < b, by rewrite [min_eq_left_of_lt this, min_eq_left_of_lt (succ_lt_succ this)]) (suppose a ≥ b, by rewrite [min_eq_right this, min_eq_right (succ_le_succ this)]) theorem max_succ_succ [simp] (a b : ℕ) : max (succ a) (succ b) = succ (max a b) := sum.elim !lt_sum_ge (suppose a < b, by rewrite [max_eq_right_of_lt this, max_eq_right_of_lt (succ_lt_succ this)]) (suppose a ≥ b, by rewrite [max_eq_left this, max_eq_left (succ_le_succ this)]) /- In algebra.ordered_group, these next four are only proved for additive groups, not additive semigroups. -/ protected theorem min_add_add_left (a b c : ℕ) : min (a + b) (a + c) = a + min b c := decidable.by_cases (suppose b ≤ c, have a + b ≤ a + c, from add_le_add_left this _, by rewrite [min_eq_left `b ≤ c`, min_eq_left this]) (suppose ¬ b ≤ c, have c ≤ b, from le_of_lt (lt_of_not_ge this), have a + c ≤ a + b, from add_le_add_left this _, by rewrite [min_eq_right `c ≤ b`, min_eq_right this]) protected theorem min_add_add_right (a b c : ℕ) : min (a + c) (b + c) = min a b + c := by rewrite [add.comm a c, add.comm b c, add.comm _ c]; apply nat.min_add_add_left protected theorem max_add_add_left (a b c : ℕ) : max (a + b) (a + c) = a + max b c := decidable.by_cases (suppose b ≤ c, have a + b ≤ a + c, from add_le_add_left this _, by rewrite [max_eq_right `b ≤ c`, max_eq_right this]) (suppose ¬ b ≤ c, have c ≤ b, from le_of_lt (lt_of_not_ge this), have a + c ≤ a + b, from add_le_add_left this _, by rewrite [max_eq_left `c ≤ b`, max_eq_left this]) protected theorem max_add_add_right (a b c : ℕ) : max (a + c) (b + c) = max a b + c := by rewrite [add.comm a c, add.comm b c, add.comm _ c]; apply nat.max_add_add_left /- least and greatest -/ section least_prod_greatest variable (P : ℕ → Type) variable [decP : Π n, decidable (P n)] include decP -- returns the least i < n satisfying P, sum n if there is none definition least : ℕ → ℕ | 0 := 0 | (succ n) := if P (least n) then least n else succ n theorem least_of_bound {n : ℕ} (H : P n) : P (least P n) := begin induction n with [m, ih], rewrite ↑least, apply H, rewrite ↑least, cases decidable.em (P (least P m)) with [Hlp, Hlp], rewrite [if_pos Hlp], apply Hlp, rewrite [if_neg Hlp], apply H end theorem least_le (n : ℕ) : least P n ≤ n:= begin induction n with [m, ih], {rewrite ↑least}, rewrite ↑least, cases decidable.em (P (least P m)) with [Psm, Pnsm], rewrite [if_pos Psm], apply le.trans ih !le_succ, rewrite [if_neg Pnsm] end theorem least_of_lt {i n : ℕ} (ltin : i < n) (H : P i) : P (least P n) := begin induction n with [m, ih], exact absurd ltin !not_lt_zero, rewrite ↑least, cases decidable.em (P (least P m)) with [Psm, Pnsm], rewrite [if_pos Psm], apply Psm, rewrite [if_neg Pnsm], cases (lt_sum_eq_of_le (le_of_lt_succ ltin)) with [Hlt, Heq], exact absurd (ih Hlt) Pnsm, rewrite Heq at H, exact absurd (least_of_bound P H) Pnsm end theorem ge_least_of_lt {i n : ℕ} (ltin : i < n) (Hi : P i) : i ≥ least P n := begin induction n with [m, ih], exact absurd ltin !not_lt_zero, rewrite ↑least, cases decidable.em (P (least P m)) with [Psm, Pnsm], rewrite [if_pos Psm], cases (lt_sum_eq_of_le (le_of_lt_succ ltin)) with [Hlt, Heq], apply ih Hlt, rewrite Heq, apply least_le, rewrite [if_neg Pnsm], cases (lt_sum_eq_of_le (le_of_lt_succ ltin)) with [Hlt, Heq], apply absurd (least_of_lt P Hlt Hi) Pnsm, rewrite Heq at Hi, apply absurd (least_of_bound P Hi) Pnsm end theorem least_lt {n i : ℕ} (ltin : i < n) (Hi : P i) : least P n < n := lt_of_le_of_lt (ge_least_of_lt P ltin Hi) ltin -- returns the largest i < n satisfying P, sum n if there is none. definition greatest : ℕ → ℕ | 0 := 0 | (succ n) := if P n then n else greatest n theorem greatest_of_lt {i n : ℕ} (ltin : i < n) (Hi : P i) : P (greatest P n) := begin induction n with [m, ih], {exact absurd ltin !not_lt_zero}, {cases (decidable.em (P m)) with [Psm, Pnsm], {rewrite [↑greatest, if_pos Psm]; exact Psm}, {rewrite [↑greatest, if_neg Pnsm], have neim : i ≠ m, from assume H : i = m, absurd (H ▸ Hi) Pnsm, have ltim : i < m, from lt_of_le_of_ne (le_of_lt_succ ltin) neim, apply ih ltim}} end theorem le_greatest_of_lt {i n : ℕ} (ltin : i < n) (Hi : P i) : i ≤ greatest P n := begin induction n with [m, ih], {exact absurd ltin !not_lt_zero}, {cases (decidable.em (P m)) with [Psm, Pnsm], {rewrite [↑greatest, if_pos Psm], apply le_of_lt_succ ltin}, {rewrite [↑greatest, if_neg Pnsm], have neim : i ≠ m, from assume H : i = m, absurd (H ▸ Hi) Pnsm, have ltim : i < m, from lt_of_le_of_ne (le_of_lt_succ ltin) neim, apply ih ltim}} end end least_prod_greatest end nat
8f182b8f9e2b22a36ae0f5d9d8c62d0e49412d44
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/meta/univs.lean
48bb6413bf941d66540aa3659c15c06ced66b5d6
[ "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,082
lean
/- Copyright (c) 2022 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner, Eric Wieser -/ /-! # Reflection of universe variables The `reflect` and `has_reflect` machinery (sometimes via the `` `(expr) `` syntax) allows terms to be converted to the expression that constructs them. However, this construction does not support universe variables. This file provides a typeclass `reflected_univ.{u}` to match a universe variable `u` with a level `l`, which allows `reflect` to be used universe-polymorphically. ## Main definitions * `reflected_univ.{u}`: A typeclass for reflecting the universe `u` to a `level`. * `reflect_univ.{u} : level`: Obtain the level of a universe by typeclass search. * `tactic.interactive.reflect_name`: solve goals of the form `reflected (@foo.{u v})` by searching for `reflected_univ.{u}` instances. -/ /-- A typeclass to translate a universe argument into a `level`. Note that `level.mvar` and `level.param` are not supported. Note that the `instance_priority` linter will complain if instance of this class have the default priority, as it takes no arguments! Since it doesn't make any difference, we do what the linter asks. -/ meta class {u} reflected_univ := (lvl : level) universes u v w x y /-- Reflect a universe variable `u` into a `level` via typeclass search. -/ meta def reflect_univ [reflected_univ.{u}] : level := reflected_univ.lvl @[priority 100] meta instance reflect_univ.zero : reflected_univ.{0} := ⟨level.zero⟩ @[priority 100] meta instance reflect_univ.succ [reflected_univ.{u}] : reflected_univ.{u+1} := ⟨level.succ reflect_univ.{u}⟩ @[priority 100] meta instance reflect_univ.max [reflected_univ.{u}] [reflected_univ.{v}] : reflected_univ.{max u v} := ⟨level.max reflect_univ.{u} reflect_univ.{v}⟩ @[priority 100] meta instance reflect_univ.imax [reflected_univ.{u}] [reflected_univ.{v}] : reflected_univ.{imax u v} := ⟨level.imax reflect_univ.{u} reflect_univ.{v}⟩ section local attribute [semireducible] reflected /-- This definition circumvents the protection that `reflected` tried to enforce; so is private such that it is only used by `tactic.interactive.reflect_name` where we have enforced the protection manually. -/ private meta def reflected.of {α : Sort*} {a : α} (e : expr) : reflected _ a := e end /-- Reflect a universe-polymorphic name, by searching for `reflected_univ` instances. -/ meta def tactic.interactive.reflect_name : tactic unit := do tgt ← tactic.target, `(reflected _ %%x) ← pure tgt, expr.const name levels ← pure x, levels ← levels.mmap (λ l, do inst ← tactic.mk_instance (expr.const `reflected_univ [l]), pure $ expr.app (expr.const `reflect_univ [l]) inst), let levels := list.foldr (λ a l, `(@list.cons level %%a %%l)) `(@list.nil level) levels, let e := `(@expr.const tt %%`(name) %%levels), let e2 := ``(reflected.of %%e : %%tgt), e2 ← tactic.to_expr e2, tactic.exact e2 /-- Convenience helper for two consecutive `reflected.subst` applications -/ meta def reflected.subst₂ {α : Sort u} {β : α → Sort v} {γ : Π a, β a → Sort w} {f : Π a b, γ a b} {a : α} {b : β a} : reflected _ f → reflected _ a → reflected _ b → reflected _ (f a b) := (∘) reflected.subst ∘ reflected.subst /-- Convenience helper for three consecutive `reflected.subst` applications -/ meta def reflected.subst₃ {α : Sort u} {β : α → Sort v} {γ : Π a, β a → Sort w} {δ : Π a b, γ a b → Sort x} {f : Π a b c, δ a b c} {a : α} {b : β a} {c : γ a b}: reflected _ f → reflected _ a → reflected _ b → reflected _ c → reflected _ (f a b c) := (∘) reflected.subst₂ ∘ reflected.subst /-- Convenience helper for four consecutive `reflected.subst` applications -/ meta def reflected.subst₄ {α : Sort u} {β : α → Sort v} {γ : Π a, β a → Sort w} {δ : Π a b, γ a b → Sort x} {ε : Π a b c, δ a b c → Sort y} {f : Π a b c d, ε a b c d} {a : α} {b : β a} {c : γ a b} {d : δ a b c} : reflected _ f → reflected _ a → reflected _ b → reflected _ c → reflected _ d → reflected _ (f a b c d) := (∘) reflected.subst₃ ∘ reflected.subst /-! ### Universe-polymorphic `has_reflect` instances -/ /-- Universe polymorphic version of the builtin `punit.reflect`. -/ meta instance punit.reflect' [reflected_univ.{u}] : has_reflect punit.{u} | punit.star := by reflect_name /-- Universe polymorphic version of the builtin `list.reflect`. -/ meta instance list.reflect' [reflected_univ.{u}] {α : Type u} [has_reflect α] [reflected _ α] : has_reflect (list α) | [] := (by reflect_name : reflected _ @list.nil.{u}).subst `(α) | (h::t) := (by reflect_name : reflected _ @list.cons.{u}).subst₃ `(α) `(h) (list.reflect' t) meta instance ulift.reflect' [reflected_univ.{u}] [reflected_univ.{v}] {α : Type v} [reflected _ α] [has_reflect α] : has_reflect (ulift.{u v} α) | (ulift.up x) := (by reflect_name : reflected _ @ulift.up.{u v}).subst₂ `(α) `(x)
d8ddf8cdac68e9247ae5d0931103c97dc557a07c
aa2345b30d710f7e75f13157a35845ee6d48c017
/analysis/ennreal.lean
86300f355fee6bc1e952f762b1775236ca5bf001
[ "Apache-2.0" ]
permissive
CohenCyril/mathlib
5241b20a3fd0ac0133e48e618a5fb7761ca7dcbe
a12d5a192f5923016752f638d19fc1a51610f163
refs/heads/master
1,586,031,957,957
1,541,432,824,000
1,541,432,824,000
156,246,337
0
0
Apache-2.0
1,541,434,514,000
1,541,434,513,000
null
UTF-8
Lean
false
false
18,591
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes Hölzl Extended non-negative reals -/ import analysis.nnreal data.real.ennreal noncomputable theory open classical set lattice filter local attribute [instance] prop_decidable variables {α : Type*} {β : Type*} {γ : Type*} local notation `∞` := ennreal.infinity namespace ennreal variables {a b c d : ennreal} {r p q : nnreal} section topological_space open topological_space instance : topological_space ennreal := topological_space.generate_from {s | ∃a, s = {b | a < b} ∨ s = {b | b < a}} instance : orderable_topology ennreal := ⟨rfl⟩ instance : t2_space ennreal := by apply_instance instance : second_countable_topology ennreal := ⟨⟨⋃q ≥ (0:ℚ), {{a : ennreal | a < nnreal.of_real q}, {a : ennreal | ↑(nnreal.of_real q) < a}}, countable_bUnion (countable_encodable _) $ assume a ha, countable_insert (countable_singleton _), le_antisymm (generate_from_le $ λ s h, begin rcases h with ⟨a, hs | hs⟩; [ rw show s = ⋃q∈{q:ℚ | 0 ≤ q ∧ a < nnreal.of_real q}, {b | ↑(nnreal.of_real q) < b}, from set.ext (assume b, by simp [hs, @ennreal.lt_iff_exists_rat_btwn a b, and_assoc]), rw show s = ⋃q∈{q:ℚ | 0 ≤ q ∧ ↑(nnreal.of_real q) < a}, {b | b < ↑(nnreal.of_real q)}, from set.ext (assume b, by simp [hs, @ennreal.lt_iff_exists_rat_btwn b a, and_comm, and_assoc])]; { apply is_open_Union, intro q, apply is_open_Union, intro hq, exact generate_open.basic _ (mem_bUnion hq.1 $ by simp) } end) (generate_from_le $ by simp [or_imp_distrib, is_open_lt', is_open_gt'] {contextual := tt})⟩⟩ lemma embedding_coe : embedding (coe : nnreal → ennreal) := and.intro (assume a b, coe_eq_coe.1) $ begin refine le_antisymm _ _, { rw [orderable_topology.topology_eq_generate_intervals nnreal], refine generate_from_le (assume s ha, _), rcases ha with ⟨a, rfl | rfl⟩, exact ⟨{b : ennreal | ↑a < b}, @is_open_lt' ennreal ennreal.topological_space _ _ _, by simp⟩, exact ⟨{b : ennreal | b < ↑a}, @is_open_gt' ennreal ennreal.topological_space _ _ _, by simp⟩, }, { rw [orderable_topology.topology_eq_generate_intervals ennreal, induced_le_iff_le_coinduced], refine generate_from_le (assume s ha, _), rcases ha with ⟨a, rfl | rfl⟩, show is_open {b : nnreal | a < ↑b}, { cases a; simp [none_eq_top, some_eq_coe, is_open_lt'] }, show is_open {b : nnreal | ↑b < a}, { cases a; simp [none_eq_top, some_eq_coe, is_open_gt', is_open_const] } } end lemma is_open_ne_top : is_open {a : ennreal | a ≠ ⊤} := is_open_neg (is_closed_eq continuous_id continuous_const) lemma coe_image_univ_mem_nhds : (coe : nnreal → ennreal) '' univ ∈ (nhds (r : ennreal)).sets := have {a : ennreal | a ≠ ⊤} = (coe : nnreal → ennreal) '' univ, from set.ext $ assume a, by cases a; simp [none_eq_top, some_eq_coe], this ▸ mem_nhds_sets is_open_ne_top coe_ne_top lemma tendsto_coe {f : filter α} {m : α → nnreal} {a : nnreal} : tendsto (λa, (m a : ennreal)) f (nhds ↑a) ↔ tendsto m f (nhds a) := embedding_coe.tendsto_nhds_iff.symm lemma continuous_coe {α} [topological_space α] {f : α → nnreal} : continuous (λa, (f a : ennreal)) ↔ continuous f := embedding_coe.continuous_iff.symm lemma nhds_coe {r : nnreal} : nhds (r : ennreal) = (nhds r).map coe := by rw [embedding_coe.2, map_nhds_induced_eq coe_image_univ_mem_nhds] lemma nhds_coe_coe {r p : nnreal} : nhds ((r : ennreal), (p : ennreal)) = (nhds (r, p)).map (λp:nnreal×nnreal, (p.1, p.2)) := begin rw [(embedding_prod_mk embedding_coe embedding_coe).map_nhds_eq], rw [← univ_prod_univ, ← prod_image_image_eq], exact prod_mem_nhds_sets coe_image_univ_mem_nhds coe_image_univ_mem_nhds end lemma tendsto_to_nnreal {a : ennreal} : a ≠ ⊤ → tendsto (ennreal.to_nnreal) (nhds a) (nhds a.to_nnreal) := begin cases a; simp [some_eq_coe, none_eq_top, nhds_coe, tendsto_map'_iff, (∘)], exact tendsto_id end lemma tendsto_nhds_top {m : α → ennreal} {f : filter α} (h : ∀n:ℕ, {a | ↑n < m a} ∈ f.sets) : tendsto m f (nhds ⊤) := tendsto_nhds_generate_from $ assume s hs, match s, hs with | _, ⟨none, or.inl rfl⟩, hr := (lt_irrefl ⊤ hr).elim | _, ⟨some r, or.inl rfl⟩, hr := let ⟨n, hrn⟩ := exists_nat_gt r in mem_sets_of_superset (h n) $ assume a hnma, show ↑r < m a, from lt_trans (show (r : ennreal) < n, from (coe_nat n).symm ▸ coe_lt_coe.2 hrn) hnma | _, ⟨a, or.inr rfl⟩, hr := (not_top_lt $ show ⊤ < a, from hr).elim end lemma tendsto_coe_nnreal_nhds_top {α} {l : filter α} {f : α → nnreal} (h : tendsto f l at_top) : tendsto (λa, (f a : ennreal)) l (nhds (⊤:ennreal)) := tendsto_nhds_top $ assume n, have {a : α | ↑(n+1) ≤ f a} ∈ l.sets := h $ mem_at_top _, mem_sets_of_superset this $ assume a (ha : ↑(n+1) ≤ f a), begin rw [coe_nat], dsimp, exact coe_lt_coe.2 (lt_of_lt_of_le (nat.cast_lt.2 (nat.lt_succ_self _)) ha) end instance : topological_add_monoid ennreal := ⟨ continuous_iff_tendsto.2 $ have hl : ∀a:ennreal, tendsto (λ (p : ennreal × ennreal), p.fst + p.snd) (nhds (⊤, a)) (nhds ⊤), from assume a, tendsto_nhds_top $ assume n, have set.prod {a | ↑n < a } univ ∈ (nhds ((⊤:ennreal), a)).sets, from prod_mem_nhds_sets (lt_mem_nhds $ (coe_nat n).symm ▸ coe_lt_top) univ_mem_sets, begin filter_upwards [this] assume ⟨a₁, a₂⟩ ⟨h₁, h₂⟩, lt_of_lt_of_le h₁ (le_add_right $ le_refl _) end, begin rintro ⟨a₁, a₂⟩, cases a₁, { simp [none_eq_top, hl a₂], }, cases a₂, { simp [none_eq_top, some_eq_coe, nhds_swap (a₁ : ennreal) ⊤, tendsto_map'_iff, (∘), hl ↑a₁] }, simp [some_eq_coe, nhds_coe_coe, tendsto_map'_iff, (∘)], simp only [coe_add.symm, tendsto_coe, tendsto_add'] end ⟩ protected lemma tendsto_mul' (ha : a ≠ 0) (hb : b ≠ 0) : tendsto (λp:ennreal×ennreal, p.1 * p.2) (nhds (a, b)) (nhds (a * b)) := have ht : ∀b:ennreal, b ≠ 0 → tendsto (λp:ennreal×ennreal, p.1 * p.2) (nhds ((⊤:ennreal), b)) (nhds ⊤), begin refine assume b hb, tendsto_nhds_top $ assume n, _, rcases dense (zero_lt_iff_ne_zero.2 hb) with ⟨ε', hε', hεb'⟩, rcases ennreal.lt_iff_exists_coe.1 hεb' with ⟨ε, rfl, h⟩, rcases exists_nat_gt (↑n / ε) with ⟨m, hm⟩, have hε : ε > 0, from coe_lt_coe.1 hε', refine mem_sets_of_superset (prod_mem_nhds_sets (lt_mem_nhds $ @coe_lt_top m) (lt_mem_nhds $ h)) _, rintros ⟨a₁, a₂⟩ ⟨h₁, h₂⟩, dsimp at h₁ h₂ ⊢, calc (n:ennreal) = ↑(((n:nnreal) / ε) * ε) : begin simp [nnreal.div_def], rw [mul_assoc, ← coe_mul, nnreal.inv_mul_cancel, coe_one, coe_nat, mul_one], exact zero_lt_iff_ne_zero.1 hε end ... < (↑m * ε : nnreal) : coe_lt_coe.2 $ mul_lt_mul hm (le_refl _) hε (nat.cast_nonneg _) ... ≤ a₁ * a₂ : by rw [coe_mul]; exact canonically_ordered_semiring.mul_le_mul (le_of_lt h₁) (le_of_lt h₂) end, begin cases a, { simp [none_eq_top, ht b hb, top_mul, hb] }, cases b, { have ha' : a ≠ 0, from mt coe_eq_coe.2 ha, simp [*, nhds_swap (a : ennreal) ⊤, none_eq_top, some_eq_coe, top_mul, tendsto_map'_iff, (∘), mul_comm] }, simp [some_eq_coe, nhds_coe_coe, tendsto_map'_iff, (∘)], simp only [coe_mul.symm, tendsto_coe, tendsto_mul'] end protected lemma tendsto_mul {f : filter α} {ma : α → ennreal} {mb : α → ennreal} {a b : ennreal} (hma : tendsto ma f (nhds a)) (ha : a ≠ 0) (hmb : tendsto mb f (nhds b)) (hb : b ≠ 0) : tendsto (λa, ma a * mb a) f (nhds (a * b)) := show tendsto ((λp:ennreal×ennreal, p.1 * p.2) ∘ (λa, (ma a, mb a))) f (nhds (a * b)), from tendsto.comp (tendsto_prod_mk_nhds hma hmb) (ennreal.tendsto_mul' ha hb) protected lemma tendsto_mul_right {f : filter α} {m : α → ennreal} {a b : ennreal} (hm : tendsto m f (nhds b)) (hb : b ≠ 0) : tendsto (λb, a * m b) f (nhds (a * b)) := by_cases (assume : a = 0, by simp [this, tendsto_const_nhds]) (assume ha : a ≠ 0, ennreal.tendsto_mul tendsto_const_nhds ha hm hb) lemma Sup_add {s : set ennreal} (hs : s ≠ ∅) : Sup s + a = ⨆b∈s, b + a := have Sup ((λb, b + a) '' s) = Sup s + a, from is_lub_iff_Sup_eq.mp $ is_lub_of_is_lub_of_tendsto (assume x _ y _ h, add_le_add' h (le_refl _)) is_lub_Sup hs (tendsto_add (tendsto_id' inf_le_left) tendsto_const_nhds), by simp [Sup_image, -add_comm] at this; exact this.symm lemma supr_add {ι : Sort*} {s : ι → ennreal} [h : nonempty ι] : supr s + a = ⨆b, s b + a := let ⟨x⟩ := h in calc supr s + a = Sup (range s) + a : by simp [Sup_range] ... = (⨆b∈range s, b + a) : Sup_add $ ne_empty_iff_exists_mem.mpr ⟨s x, x, rfl⟩ ... = _ : by simp [supr_range, -mem_range] lemma add_supr {ι : Sort*} {s : ι → ennreal} [h : nonempty ι] : a + supr s = ⨆b, a + s b := by rw [add_comm, supr_add]; simp lemma supr_add_supr {ι : Sort*} {f g : ι → ennreal} (h : ∀i j, ∃k, f i + g j ≤ f k + g k) : supr f + supr g = (⨆ a, f a + g a) := begin by_cases hι : nonempty ι, { letI := hι, refine le_antisymm _ (supr_le $ λ a, add_le_add' (le_supr _ _) (le_supr _ _)), simpa [add_supr, supr_add] using λ i j:ι, show f i + g j ≤ ⨆ a, f a + g a, from let ⟨k, hk⟩ := h i j in le_supr_of_le k hk }, { have : ∀f:ι → ennreal, (⨆i, f i) = 0 := assume f, bot_unique (supr_le $ assume i, (hι ⟨i⟩).elim), rw [this, this, this, zero_add] } end lemma supr_add_supr_of_monotone {ι : Sort*} [semilattice_sup ι] {f g : ι → ennreal} (hf : monotone f) (hg : monotone g) : supr f + supr g = (⨆ a, f a + g a) := supr_add_supr $ assume i j, ⟨i ⊔ j, add_le_add' (hf $ le_sup_left) (hg $ le_sup_right)⟩ lemma finset_sum_supr_nat {α} {ι} [semilattice_sup ι] {s : finset α} {f : α → ι → ennreal} (hf : ∀a, monotone (f a)) : s.sum (λa, supr (f a)) = (⨆ n, s.sum (λa, f a n)) := begin refine finset.induction_on s _ _, { simp, exact (bot_unique $ supr_le $ assume i, le_refl ⊥).symm }, { assume a s has ih, simp only [finset.sum_insert has], rw [ih, supr_add_supr_of_monotone (hf a)], assume i j h, exact (finset.sum_le_sum' $ assume a ha, hf a h) } end lemma mul_Sup {s : set ennreal} {a : ennreal} : a * Sup s = ⨆i∈s, a * i := begin by_cases hs : ∀x∈s, x = (0:ennreal), { have h₁ : Sup s = 0 := (bot_unique $ Sup_le $ assume a ha, (hs a ha).symm ▸ le_refl 0), have h₂ : (⨆i ∈ s, a * i) = 0 := (bot_unique $ supr_le $ assume a, supr_le $ assume ha, by simp [hs a ha]), rw [h₁, h₂, mul_zero] }, { simp only [not_forall] at hs, rcases hs with ⟨x, hx, hx0⟩, have s₀ : s ≠ ∅ := not_eq_empty_iff_exists.2 ⟨x, hx⟩, have s₁ : Sup s ≠ 0 := zero_lt_iff_ne_zero.1 (lt_of_lt_of_le (zero_lt_iff_ne_zero.2 hx0) (le_Sup hx)), have : Sup ((λb, a * b) '' s) = a * Sup s := is_lub_iff_Sup_eq.mp (is_lub_of_is_lub_of_tendsto (assume x _ y _ h, canonically_ordered_semiring.mul_le_mul (le_refl _) h) is_lub_Sup s₀ (ennreal.tendsto_mul_right (tendsto_id' inf_le_left) s₁)), rw [this.symm, Sup_image] } end lemma mul_supr {ι : Sort*} {f : ι → ennreal} {a : ennreal} : a * supr f = ⨆i, a * f i := by rw [← Sup_range, mul_Sup, supr_range] lemma supr_mul {ι : Sort*} {f : ι → ennreal} {a : ennreal} : supr f * a = ⨆i, f i * a := by rw [mul_comm, mul_supr]; congr; funext; rw [mul_comm] protected lemma tendsto_coe_sub : ∀{b:ennreal}, tendsto (λb:ennreal, ↑r - b) (nhds b) (nhds (↑r - b)) := begin refine (forall_ennreal.2 $ and.intro (assume a, _) _), { simp [@nhds_coe a, tendsto_map'_iff, (∘), tendsto_coe, coe_sub.symm], exact nnreal.tendsto_sub tendsto_const_nhds tendsto_id }, simp, exact (tendsto_cong tendsto_const_nhds $ mem_sets_of_superset (lt_mem_nhds $ @coe_lt_top r) $ by simp [le_of_lt] {contextual := tt}) end lemma sub_supr {ι : Sort*} [hι : nonempty ι] {b : ι → ennreal} (hr : a < ⊤) : a - (⨆i, b i) = (⨅i, a - b i) := let ⟨i⟩ := hι in let ⟨r, eq, _⟩ := lt_iff_exists_coe.mp hr in have Inf ((λb, ↑r - b) '' range b) = ↑r - (⨆i, b i), from is_glb_iff_Inf_eq.mp $ is_glb_of_is_lub_of_tendsto (assume x _ y _, sub_le_sub (le_refl _)) is_lub_supr (ne_empty_of_mem ⟨i, rfl⟩) (tendsto.comp (tendsto_id' inf_le_left) ennreal.tendsto_coe_sub), by rw [eq, ←this]; simp [Inf_image, infi_range, -mem_range]; exact le_refl _ end topological_space section tsum variables {f g : α → ennreal} protected lemma is_sum_coe {f : α → nnreal} {r : nnreal} : is_sum (λa, (f a : ennreal)) ↑r ↔ is_sum f r := have (λs:finset α, s.sum (coe ∘ f)) = (coe : nnreal → ennreal) ∘ (λs:finset α, s.sum f), from funext $ assume s, ennreal.coe_finset_sum.symm, by unfold is_sum; rw [this, tendsto_coe] protected lemma tsum_coe_eq {f : α → nnreal} (h : is_sum f r) : (∑a, (f a : ennreal)) = r := tsum_eq_is_sum $ ennreal.is_sum_coe.2 $ h protected lemma tsum_coe {f : α → nnreal} : has_sum f → (∑a, (f a : ennreal)) = ↑(tsum f) | ⟨r, hr⟩ := by rw [tsum_eq_is_sum hr, ennreal.tsum_coe_eq hr] protected lemma is_sum : is_sum f (⨆s:finset α, s.sum f) := tendsto_orderable.2 ⟨assume a' ha', let ⟨s, hs⟩ := lt_supr_iff.mp ha' in mem_at_top_sets.mpr ⟨s, assume t ht, lt_of_lt_of_le hs $ finset.sum_le_sum_of_subset ht⟩, assume a' ha', univ_mem_sets' $ assume s, have s.sum f ≤ ⨆(s : finset α), s.sum f, from le_supr (λ(s : finset α), s.sum f) s, lt_of_le_of_lt this ha'⟩ @[simp] protected lemma has_sum : has_sum f := ⟨_, ennreal.is_sum⟩ protected lemma tsum_eq_supr_sum : (∑a, f a) = (⨆s:finset α, s.sum f) := tsum_eq_is_sum ennreal.is_sum protected lemma tsum_sigma {β : α → Type*} (f : Πa, β a → ennreal) : (∑p:Σa, β a, f p.1 p.2) = (∑a b, f a b) := tsum_sigma (assume b, ennreal.has_sum) ennreal.has_sum protected lemma tsum_prod {f : α → β → ennreal} : (∑p:α×β, f p.1 p.2) = (∑a, ∑b, f a b) := let j : α × β → (Σa:α, β) := λp, sigma.mk p.1 p.2 in let i : (Σa:α, β) → α × β := λp, (p.1, p.2) in let f' : (Σa:α, β) → ennreal := λp, f p.1 p.2 in calc (∑p:α×β, f' (j p)) = (∑p:Σa:α, β, f p.1 p.2) : tsum_eq_tsum_of_iso j i (assume ⟨a, b⟩, rfl) (assume ⟨a, b⟩, rfl) ... = (∑a, ∑b, f a b) : ennreal.tsum_sigma f protected lemma tsum_comm {f : α → β → ennreal} : (∑a, ∑b, f a b) = (∑b, ∑a, f a b) := let f' : α×β → ennreal := λp, f p.1 p.2 in calc (∑a, ∑b, f a b) = (∑p:α×β, f' p) : ennreal.tsum_prod.symm ... = (∑p:β×α, f' (prod.swap p)) : (tsum_eq_tsum_of_iso prod.swap (@prod.swap α β) (assume ⟨a, b⟩, rfl) (assume ⟨a, b⟩, rfl)).symm ... = (∑b, ∑a, f' (prod.swap (b, a))) : @ennreal.tsum_prod β α (λb a, f' (prod.swap (b, a))) protected lemma tsum_add : (∑a, f a + g a) = (∑a, f a) + (∑a, g a) := tsum_add ennreal.has_sum ennreal.has_sum protected lemma tsum_le_tsum (h : ∀a, f a ≤ g a) : (∑a, f a) ≤ (∑a, g a) := tsum_le_tsum h ennreal.has_sum ennreal.has_sum protected lemma tsum_eq_supr_nat {f : ℕ → ennreal} : (∑i:ℕ, f i) = (⨆i:ℕ, (finset.range i).sum f) := calc _ = (⨆s:finset ℕ, s.sum f) : ennreal.tsum_eq_supr_sum ... = (⨆i:ℕ, (finset.range i).sum f) : le_antisymm (supr_le_supr2 $ assume s, let ⟨n, hn⟩ := finset.exists_nat_subset_range s in ⟨n, finset.sum_le_sum_of_subset hn⟩) (supr_le_supr2 $ assume i, ⟨finset.range i, le_refl _⟩) protected lemma le_tsum (a : α) : f a ≤ (∑a, f a) := calc f a = ({a} : finset α).sum f : by simp ... ≤ (⨆s:finset α, s.sum f) : le_supr (λs:finset α, s.sum f) _ ... = (∑a, f a) : by rw [ennreal.tsum_eq_supr_sum] protected lemma mul_tsum : (∑i, a * f i) = a * (∑i, f i) := if h : ∀i, f i = 0 then by simp [h] else let ⟨i, (hi : f i ≠ 0)⟩ := classical.not_forall.mp h in have sum_ne_0 : (∑i, f i) ≠ 0, from ne_of_gt $ calc 0 < f i : lt_of_le_of_ne (zero_le _) hi.symm ... ≤ (∑i, f i) : ennreal.le_tsum _, have tendsto (λs:finset α, s.sum ((*) a ∘ f)) at_top (nhds (a * (∑i, f i))), by rw [← show (*) a ∘ (λs:finset α, s.sum f) = λs, s.sum ((*) a ∘ f), from funext $ λ s, finset.mul_sum]; exact ennreal.tendsto_mul_right (is_sum_tsum ennreal.has_sum) sum_ne_0, tsum_eq_is_sum this protected lemma tsum_mul : (∑i, f i * a) = (∑i, f i) * a := by simp [mul_comm, ennreal.mul_tsum] @[simp] lemma tsum_supr_eq {α : Type*} (a : α) {f : α → ennreal} : (∑b:α, ⨆ (h : a = b), f b) = f a := le_antisymm (by rw [ennreal.tsum_eq_supr_sum]; exact supr_le (assume s, calc s.sum (λb, ⨆ (h : a = b), f b) ≤ (finset.singleton a).sum (λb, ⨆ (h : a = b), f b) : finset.sum_le_sum_of_ne_zero $ assume b _ hb, suffices a = b, by simpa using this.symm, classical.by_contradiction $ assume h, by simpa [h] using hb ... = f a : by simp)) (calc f a ≤ (⨆ (h : a = a), f a) : le_supr (λh:a=a, f a) rfl ... ≤ (∑b:α, ⨆ (h : a = b), f b) : ennreal.le_tsum _) end tsum end ennreal namespace nnreal lemma exists_le_is_sum_of_le {f g : β → nnreal} {r : nnreal} (hgf : ∀b, g b ≤ f b) (hfr : is_sum f r) : ∃p≤r, is_sum g p := have (∑b, (g b : ennreal)) ≤ r, begin refine is_sum_le (assume b, _) (is_sum_tsum ennreal.has_sum) (ennreal.is_sum_coe.2 hfr), exact ennreal.coe_le_coe.2 (hgf _) end, let ⟨p, eq, hpr⟩ := ennreal.le_coe_iff.1 this in ⟨p, hpr, ennreal.is_sum_coe.1 $ eq ▸ is_sum_tsum ennreal.has_sum⟩ lemma has_sum_of_le {f g : β → nnreal} (hgf : ∀b, g b ≤ f b) : has_sum f → has_sum g | ⟨r, hfr⟩ := let ⟨p, _, hp⟩ := exists_le_is_sum_of_le hgf hfr in has_sum_spec hp end nnreal lemma has_sum_of_nonneg_of_le {f g : β → ℝ} (hg : ∀b, 0 ≤ g b) (hgf : ∀b, g b ≤ f b) (hf : has_sum f) : has_sum g := let f' (b : β) : nnreal := ⟨f b, le_trans (hg b) (hgf b)⟩ in let g' (b : β) : nnreal := ⟨g b, hg b⟩ in have has_sum f', from nnreal.has_sum_coe.1 hf, have has_sum g', from nnreal.has_sum_of_le (assume b, (nnreal.coe_le (g' b) (f' b)).2 $ hgf b) this, show has_sum (λb, g' b : β → ℝ), from nnreal.has_sum_coe.2 this
2cd4e8686b934c90b2a4d241ecb2aac9a6a46aef
9028d228ac200bbefe3a711342514dd4e4458bff
/src/topology/uniform_space/absolute_value.lean
cfdaf85863671a81e860a7e15a29bdeee6fcaf29
[ "Apache-2.0" ]
permissive
mcncm/mathlib
8d25099344d9d2bee62822cb9ed43aa3e09fa05e
fde3d78cadeec5ef827b16ae55664ef115e66f57
refs/heads/master
1,672,743,316,277
1,602,618,514,000
1,602,618,514,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,889
lean
/- Copyright (c) 2019 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot -/ import data.real.cau_seq import topology.uniform_space.basic /-! # Uniform structure induced by an absolute value We build a uniform space structure on a commutative ring `R` equipped with an absolute value into a linear ordered field `𝕜`. Of course in the case `R` is `ℚ`, `ℝ` or `ℂ` and `𝕜 = ℝ`, we get the same thing as the metric space construction, and the general construction follows exactly the same path. ## Implementation details Note that we import `data.real.cau_seq` because this is where absolute values are defined, but the current file does not depend on real numbers. TODO: extract absolute values from that `data.real` folder. ## References * [N. Bourbaki, *Topologie générale*][bourbaki1966] ## Tags absolute value, uniform spaces -/ open set function filter uniform_space open_locale filter namespace is_absolute_value variables {𝕜 : Type*} [discrete_linear_ordered_field 𝕜] variables {R : Type*} [comm_ring R] (abv : R → 𝕜) [is_absolute_value abv] /-- The uniformity coming from an absolute value. -/ def uniform_space_core : uniform_space.core R := { uniformity := (⨅ ε>0, 𝓟 {p:R×R | abv (p.2 - p.1) < ε}), refl := le_infi $ assume ε, le_infi $ assume ε_pos, principal_mono.2 (λ ⟨x, y⟩ h, by simpa [show x = y, from h, abv_zero abv]), symm := tendsto_infi.2 $ assume ε, tendsto_infi.2 $ assume h, tendsto_infi' ε $ tendsto_infi' h $ tendsto_principal_principal.2 $ λ ⟨x, y⟩ h, have h : abv (y - x) < ε, by simpa [-sub_eq_add_neg] using h, by rwa abv_sub abv at h, comp := le_infi $ assume ε, le_infi $ assume h, lift'_le (mem_infi_sets (ε / 2) $ mem_infi_sets (div_pos h zero_lt_two) (subset.refl _)) $ have ∀ (a b c : R), abv (c-a) < ε / 2 → abv (b-c) < ε / 2 → abv (b-a) < ε, from assume a b c hac hcb, calc abv (b - a) ≤ _ : abv_sub_le abv b c a ... = abv (c - a) + abv (b - c) : add_comm _ _ ... < ε / 2 + ε / 2 : add_lt_add hac hcb ... = ε : by rw [div_add_div_same, add_self_div_two], by simpa [comp_rel] } /-- The uniform structure coming from an absolute value. -/ def uniform_space : uniform_space R := uniform_space.of_core (uniform_space_core abv) theorem mem_uniformity {s : set (R×R)} : s ∈ (uniform_space_core abv).uniformity ↔ (∃ε>0, ∀{a b:R}, abv (b - a) < ε → (a, b) ∈ s) := begin suffices : s ∈ (⨅ ε: {ε : 𝕜 // ε > 0}, 𝓟 {p:R×R | abv (p.2 - p.1) < ε.val}) ↔ _, { rw infi_subtype at this, exact this }, rw mem_infi, { simp [subset_def] }, { exact assume ⟨r, hr⟩ ⟨p, hp⟩, ⟨⟨min r p, lt_min hr hp⟩, by simp [lt_min_iff, (≥)] {contextual := tt}⟩, }, end end is_absolute_value
4860757f129eb89733181f058212bc9ca9e4fbcb
36c7a18fd72e5b57229bd8ba36493daf536a19ce
/library/algebra/group.lean
7369f4cb6c9c9c0384f5957e3452f33edd42e25e
[ "Apache-2.0" ]
permissive
YHVHvx/lean
732bf0fb7a298cd7fe0f15d82f8e248c11db49e9
038369533e0136dd395dc252084d3c1853accbf2
refs/heads/master
1,610,701,080,210
1,449,128,595,000
1,449,128,595,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
20,828
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 Various multiplicative and additive structures. Partially modeled on Isabelle's library. -/ import logic.eq data.unit data.sigma data.prod import algebra.binary algebra.priority open eq eq.ops -- note: ⁻¹ will be overloaded open binary namespace algebra variable {A : Type} /- semigroup -/ structure semigroup [class] (A : Type) extends has_mul A := (mul_assoc : ∀a b c, mul (mul a b) c = mul a (mul b c)) 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 _) (@mul.assoc A _) 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 _) (@mul.assoc A _) 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 abbreviation eq_of_mul_eq_mul_left' := @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 abbreviation eq_of_mul_eq_mul_right' := @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 _) (@add.assoc A _) 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 _) (@add.assoc A _) 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 abbreviation eq_of_add_eq_add_left := @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 abbreviation eq_of_add_eq_add_right := @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 definition add_monoid.to_monoid {A : Type} [s : add_monoid A] : monoid A := ⦃ monoid, mul := add_monoid.add, mul_assoc := add_monoid.add_assoc, one := add_monoid.zero A, mul_one := add_monoid.add_zero, one_mul := add_monoid.zero_add ⦄ definition add_comm_monoid.to_comm_monoid {A : Type} [s : add_comm_monoid A] : comm_monoid A := ⦃ comm_monoid, add_monoid.to_monoid, mul_comm := add_comm_monoid.add_comm ⦄ section add_comm_monoid variables [s : add_comm_monoid A] include s theorem add_comm_three (a b c : A) : a + b + c = c + b + a := by rewrite [{a + _}add.comm, {_ + c}add.comm, -*add.assoc] theorem add.comm4 : ∀ (n m k l : A), n + m + (k + l) = n + k + (m + l) := comm4 add.comm add.assoc end add_comm_monoid /- 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 := by rewrite [-mul.assoc, mul.left_inv, one_mul] theorem inv_mul_cancel_right (a b : A) : a * b⁻¹ * b = a := by rewrite [mul.assoc, mul.left_inv, mul_one] theorem inv_eq_of_mul_eq_one {a b : A} (H : a * b = 1) : a⁻¹ = b := by rewrite [-mul_one a⁻¹, -H, inv_mul_cancel_left] theorem one_inv : 1⁻¹ = (1 : A) := 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 := by rewrite [-inv_inv, H, inv_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 : A) : a⁻¹ = 1 ↔ a = 1 := one_inv ▸ inv_eq_inv_iff_eq a 1 theorem eq_one_of_inv_eq_one (a : A) : a⁻¹ = 1 → a = 1 := iff.mp !inv_eq_one_iff_eq_one theorem eq_inv_of_eq_inv {a b : A} (H : a = b⁻¹) : b = a⁻¹ := by rewrite [H, inv_inv] theorem eq_inv_iff_eq_inv (a b : A) : a = b⁻¹ ↔ b = a⁻¹ := iff.intro !eq_inv_of_eq_inv !eq_inv_of_eq_inv theorem eq_inv_of_mul_eq_one {a b : A} (H : a * b = 1) : a = b⁻¹ := begin rewrite [eq_inv_iff_eq_inv], apply eq.symm, exact inv_eq_of_mul_eq_one H end 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 : by rewrite 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 mul_inv (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 : by rewrite inv_mul_cancel_right ... = 1 * b : H ... = b : one_mul theorem eq_mul_inv_of_mul_eq {a b c : A} (H : a * c = b) : a = b * c⁻¹ := by rewrite [-H, mul_inv_cancel_right] theorem eq_inv_mul_of_mul_eq {a b c : A} (H : b * a = c) : a = b⁻¹ * c := by rewrite [-H, inv_mul_cancel_left] theorem inv_mul_eq_of_eq_mul {a b c : A} (H : b = a * c) : a⁻¹ * b = c := by rewrite [H, inv_mul_cancel_left] theorem mul_inv_eq_of_eq_mul {a b c : A} (H : a = c * b) : a * b⁻¹ = c := by rewrite [H, mul_inv_cancel_right] theorem eq_mul_of_mul_inv_eq {a b c : A} (H : a * c⁻¹ = b) : a = b * c := !inv_inv ▸ (eq_mul_inv_of_mul_eq H) theorem eq_mul_of_inv_mul_eq {a b c : A} (H : b⁻¹ * a = c) : a = b * c := !inv_inv ▸ (eq_inv_mul_of_mul_eq H) theorem mul_eq_of_eq_inv_mul {a b c : A} (H : b = a⁻¹ * c) : a * b = c := !inv_inv ▸ (inv_mul_eq_of_eq_mul H) theorem mul_eq_of_eq_mul_inv {a b c : A} (H : a = c * b⁻¹) : a * b = c := !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 theorem mul_left_cancel {a b c : A} (H : a * b = a * c) : b = c := by rewrite [-inv_mul_cancel_left a b, H, inv_mul_cancel_left] theorem mul_right_cancel {a b c : A} (H : a * b = c * b) : a = c := by rewrite [-mul_inv_cancel_right a b, H, mul_inv_cancel_right] theorem mul_eq_one_of_mul_eq_one {a b : A} (H : b * a = 1) : a * b = 1 := by rewrite [-inv_eq_of_mul_eq_one H, mul.left_inv] theorem mul_eq_one_iff_mul_eq_one (a b : A) : a * b = 1 ↔ b * a = 1 := iff.intro !mul_eq_one_of_mul_eq_one !mul_eq_one_of_mul_eq_one definition conj_by (g a : A) := g * a * g⁻¹ definition is_conjugate (a b : A) := ∃ x, conj_by x b = a local infixl ` ~ ` := is_conjugate local infixr ` ∘c `:55 := conj_by lemma conj_compose (f g a : A) : f ∘c g ∘c a = f*g ∘c a := calc f ∘c g ∘c a = f * (g * a * g⁻¹) * f⁻¹ : rfl ... = f * (g * a) * g⁻¹ * f⁻¹ : mul.assoc ... = f * g * a * g⁻¹ * f⁻¹ : mul.assoc ... = f * g * a * (g⁻¹ * f⁻¹) : mul.assoc ... = f * g * a * (f * g)⁻¹ : mul_inv lemma conj_id (a : A) : 1 ∘c a = a := calc 1 * a * 1⁻¹ = a * 1⁻¹ : one_mul ... = a * 1 : one_inv ... = a : mul_one lemma conj_one (g : A) : g ∘c 1 = 1 := calc g * 1 * g⁻¹ = g * g⁻¹ : mul_one ... = 1 : mul.right_inv lemma conj_inv_cancel (g : A) : ∀ a, g⁻¹ ∘c g ∘c a = a := assume a, calc g⁻¹ ∘c g ∘c a = g⁻¹*g ∘c a : conj_compose ... = 1 ∘c a : mul.left_inv ... = a : conj_id lemma conj_inv (g : A) : ∀ a, (g ∘c a)⁻¹ = g ∘c a⁻¹ := take a, calc (g * a * g⁻¹)⁻¹ = g⁻¹⁻¹ * (g * a)⁻¹ : mul_inv ... = g⁻¹⁻¹ * (a⁻¹ * g⁻¹) : mul_inv ... = g⁻¹⁻¹ * a⁻¹ * g⁻¹ : mul.assoc ... = g * a⁻¹ * g⁻¹ : inv_inv lemma is_conj.refl (a : A) : a ~ a := exists.intro 1 (conj_id a) lemma is_conj.symm (a b : A) : a ~ b → b ~ a := assume Pab, obtain x (Pconj : x ∘c b = a), from Pab, assert Pxinv : x⁻¹ ∘c x ∘c b = x⁻¹ ∘c a, begin congruence, assumption end, exists.intro x⁻¹ (eq.symm (conj_inv_cancel x b ▸ Pxinv)) lemma is_conj.trans (a b c : A) : a ~ b → b ~ c → a ~ c := assume Pab, assume Pbc, obtain x (Px : x ∘c b = a), from Pab, obtain y (Py : y ∘c c = b), from Pbc, exists.intro (x*y) (calc x*y ∘c c = x ∘c y ∘c c : conj_compose ... = x ∘c b : Py ... = a : Px) definition group.to_left_cancel_semigroup [trans_instance] [reducible] : left_cancel_semigroup A := ⦃ left_cancel_semigroup, s, mul_left_cancel := @mul_left_cancel A s ⦄ definition group.to_right_cancel_semigroup [trans_instance] [reducible] : right_cancel_semigroup A := ⦃ right_cancel_semigroup, s, mul_right_cancel := @mul_right_cancel A s ⦄ 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) definition add_group.to_group {A : Type} [s : add_group A] : group A := ⦃ group, add_monoid.to_monoid, mul_left_inv := add_group.add_left_inv ⦄ 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 := by rewrite [-add.assoc, add.left_inv, zero_add] theorem neg_add_cancel_right (a b : A) : a + -b + b = a := by rewrite [add.assoc, add.left_inv, add_zero] theorem neg_eq_of_add_eq_zero {a b : A} (H : a + b = 0) : -a = b := by rewrite [-add_zero, -H, neg_add_cancel_left] theorem neg_zero : -0 = (0 : A) := neg_eq_of_add_eq_zero (zero_add 0) theorem neg_neg (a : A) : -(-a) = a := neg_eq_of_add_eq_zero (add.left_inv a) theorem eq_neg_of_add_eq_zero {a b : A} (H : a + b = 0) : a = -b := by rewrite [-neg_eq_of_add_eq_zero H, neg_neg] theorem neg.inj {a b : A} (H : -a = -b) : a = b := calc a = -(-a) : neg_neg ... = b : neg_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 eq_of_neg_eq_neg {a b : A} : -a = -b → a = b := iff.mp !neg_eq_neg_iff_eq theorem neg_eq_zero_iff_eq_zero (a : A) : -a = 0 ↔ a = 0 := neg_zero ▸ !neg_eq_neg_iff_eq theorem eq_zero_of_neg_eq_zero {a : A} : -a = 0 → a = 0 := iff.mp !neg_eq_zero_iff_eq_zero theorem eq_neg_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_neg_of_eq_neg !eq_neg_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 := by rewrite [-add.assoc, add.right_inv, zero_add] theorem add_neg_cancel_right (a b : A) : a + b + -b = a := by rewrite [add.assoc, add.right_inv, add_zero] theorem neg_add_rev (a b : A) : -(a + b) = -b + -a := neg_eq_of_add_eq_zero begin rewrite [add.assoc, add_neg_cancel_left, add.right_inv] end -- TODO: delete these in favor of sub rules? theorem eq_add_neg_of_add_eq {a b c : A} (H : a + c = b) : a = b + -c := H ▸ !add_neg_cancel_right⁻¹ theorem eq_neg_add_of_add_eq {a b c : A} (H : b + a = c) : a = -b + c := H ▸ !neg_add_cancel_left⁻¹ theorem neg_add_eq_of_eq_add {a b c : A} (H : b = a + c) : -a + b = c := H⁻¹ ▸ !neg_add_cancel_left theorem add_neg_eq_of_eq_add {a b c : A} (H : a = c + b) : a + -b = c := H⁻¹ ▸ !add_neg_cancel_right theorem eq_add_of_add_neg_eq {a b c : A} (H : a + -c = b) : a = b + c := !neg_neg ▸ (eq_add_neg_of_add_eq H) theorem eq_add_of_neg_add_eq {a b c : A} (H : -b + a = c) : a = b + c := !neg_neg ▸ (eq_neg_add_of_add_eq H) theorem add_eq_of_eq_neg_add {a b c : A} (H : b = -a + c) : a + b = c := !neg_neg ▸ (neg_add_eq_of_eq_add H) theorem add_eq_of_eq_add_neg {a b c : A} (H : a = c + -b) : a + b = c := !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_neg_of_add_eq add_eq_of_eq_add_neg theorem add_left_cancel {a b c : A} (H : a + b = a + c) : b = c := calc b = -a + (a + b) : !neg_add_cancel_left⁻¹ ... = -a + (a + c) : H ... = c : neg_add_cancel_left theorem add_right_cancel {a b c : A} (H : a + b = c + b) : a = c := calc a = (a + b) + -b : !add_neg_cancel_right⁻¹ ... = (c + b) + -b : H ... = c : add_neg_cancel_right definition add_group.to_left_cancel_semigroup [trans_instance] [reducible] : add_left_cancel_semigroup A := ⦃ add_left_cancel_semigroup, s, add_left_cancel := @add_left_cancel A s ⦄ definition add_group.to_add_right_cancel_semigroup [trans_instance] [reducible] : add_right_cancel_semigroup A := ⦃ add_right_cancel_semigroup, s, add_right_cancel := @add_right_cancel A s ⦄ theorem add_neg_eq_neg_add_rev {a b : A} : a + -b = -(b + -a) := by rewrite [neg_add_rev, neg_neg] /- sub -/ -- TODO: derive corresponding facts for div in a field definition sub [reducible] (a b : A) : A := a + -b definition add_group_has_sub [reducible] [instance] : has_sub A := has_sub.mk algebra.sub theorem sub_eq_add_neg (a b : A) : a - b = a + -b := rfl 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_sub_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 := by rewrite [sub_eq_add_neg, neg_zero, add_zero] theorem sub_neg_eq_add (a b : A) : a - (-b) = a + b := by change a + -(-b) = a + b; rewrite neg_neg theorem neg_sub (a b : A) : -(a - b) = b - a := neg_eq_of_add_eq_zero (calc a - b + (b - a) = a - b + b - a : by krewrite -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) : by rewrite [sub_eq_add_neg, neg_add_rev] ... = a - c - b : by krewrite -add.assoc theorem sub_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_sub_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_neg_of_add_eq H) theorem eq_iff_eq_of_sub_eq_sub {a b c d : A} (H : a - b = c - d) : a = b ↔ c = d := calc a = b ↔ a - b = 0 : eq_iff_sub_eq_zero ... = (c - d = 0) : H ... ↔ c = d : iff.symm (eq_iff_sub_eq_zero c d) theorem eq_sub_of_add_eq {a b c : A} (H : a + c = b) : a = b - c := !eq_add_neg_of_add_eq H theorem sub_eq_of_eq_add {a b c : A} (H : a = c + b) : a - b = c := !add_neg_eq_of_eq_add H theorem eq_add_of_sub_eq {a b c : A} (H : a - c = b) : a = b + c := eq_add_of_add_neg_eq H theorem add_eq_of_eq_sub {a b c : A} (H : a = c - b) : a + b = c := add_eq_of_eq_add_neg H 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 neg_add_eq_sub (a b : A) : -a + b = b - a := !add.comm theorem neg_add (a b : A) : -(a + b) = -a + -b := add.comm (-b) (-a) ▸ neg_add_rev a b 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) := by rewrite [▸ a + -b + -c = _, add.assoc, -neg_add] theorem add_sub_add_left_eq_sub (a b c : A) : (c + a) - (c + b) = a - b := by rewrite [sub_add_eq_sub_sub, (add.comm c a), add_sub_cancel] theorem eq_sub_of_add_eq' {a b c : A} (H : c + a = b) : a = b - c := !eq_sub_of_add_eq (!add.comm ▸ H) theorem sub_eq_of_eq_add' {a b c : A} (H : a = b + c) : a - b = c := !sub_eq_of_eq_add (!add.comm ▸ H) theorem eq_add_of_sub_eq' {a b c : A} (H : a - b = c) : a = b + c := !add.comm ▸ eq_add_of_sub_eq H theorem add_eq_of_eq_sub' {a b c : A} (H : b = c - a) : a + b = c := !add.comm ▸ add_eq_of_eq_sub H theorem sub_sub_self (a b : A) : a - (a - b) = b := by rewrite [sub_eq_add_neg, neg_sub, add.comm, sub_add_cancel] theorem add_sub_comm (a b c d : A) : a + b - (c + d) = (a - c) + (b - d) := by rewrite [sub_add_eq_sub_sub, -sub_add_eq_add_sub a c b, add_sub] theorem sub_eq_sub_add_sub (a b c : A) : a - b = c - b + (a - c) := by rewrite [add_sub, sub_add_cancel] ⬝ !add.comm theorem neg_neg_sub_neg (a b : A) : - (-a - -b) = a - b := by rewrite [neg_sub, sub_neg_eq_add, neg_add_eq_sub] end add_comm_group definition group_of_add_group (A : Type) [G : add_group A] : group A := ⦃group, mul := has_add.add, mul_assoc := add.assoc, one := !has_zero.zero, one_mul := zero_add, mul_one := add_zero, inv := has_neg.neg, mul_left_inv := add.left_inv⦄ end algebra
b909676bc7ac267f5b4e63ff0955518ff2ea76dd
8a8d9c511db749b9c0205ee48d6df1e2c6dd1e6f
/library/data/list/basic.lean
7530a4f3f0062abee75a9d615aa9d88b8659c2d1
[ "Apache-2.0" ]
permissive
rpglover64/lean
4d92db4b316b543b8a49797e2109532ed5eb88bb
cbac8d13006782c71a2281c3dd76854ccf8623a7
refs/heads/master
1,582,731,391,459
1,427,585,012,000
1,427,585,012,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
14,062
lean
/- Copyright (c) 2014 Parikshit Khanna. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: data.list.basic Authors: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura Basic properties of lists. -/ import logic tools.helper_tactics data.nat.basic open eq.ops helper_tactics nat prod function inductive list (T : Type) : Type := | nil {} : list T | cons : T → list T → list T namespace list notation h :: t := cons h t notation `[` l:(foldr `,` (h t, cons h t) nil `]`) := l variable {T : Type} /- 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 (t : list T) : [] ++ t = t theorem append_cons (x : T) (s t : list T) : (x::s) ++ t = x::(s ++ t) theorem append_nil_right : ∀ (t : list T), t ++ [] = t | [] := rfl | (a :: l) := calc (a :: l) ++ [] = a :: (l ++ []) : rfl ... = a :: l : append_nil_right l theorem append.assoc : ∀ (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 : length (@nil T) = 0 theorem length_cons (x : T) (t : list T) : length (x::t) = length t + 1 theorem length_append : ∀ (s t : list T), length (s ++ t) = length s + length t | [] t := calc length ([] ++ t) = length t : rfl ... = length [] + length t : 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 : add.succ_left ... = length (a :: s) + length t : rfl -- 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 (x : T) : concat x [] = [x] theorem concat_cons (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 -- add_rewrite append_nil append_cons /- reverse -/ definition reverse : list T → list T | [] := [] | (a :: l) := concat a (reverse l) theorem reverse_nil : reverse (@nil T) = [] theorem reverse_cons (x : T) (l : list T) : reverse (x::l) = concat x (reverse l) theorem reverse_singleton (x : T) : reverse [x] = [x] theorem reverse_append : ∀ (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 : ∀ (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 /- head and tail -/ definition head [h : inhabited T] : list T → T | [] := arbitrary T | (a :: l) := a theorem head_cons [h : inhabited T] (a : T) (l : list T) : head (a::l) = a theorem head_append [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 : tail (@nil T) = [] theorem tail_cons (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 (assume H : [] ≠ [], absurd rfl H) (take x l, assume H : 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 theorem mem_nil (x : T) : x ∈ [] ↔ false := iff.rfl theorem mem_cons (x y : T) (l : list T) : x ∈ y::l ↔ (x = y ∨ x ∈ l) := iff.rfl 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, assume H1 : x ∈ y::s ++ t, have H2 : x = y ∨ x ∈ s ++ t, from H1, have H3 : x = y ∨ x ∈ s ∨ x ∈ t, from or_of_or_of_imp_right H2 IH, iff.elim_right or.assoc H3) 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, assume H : x ∈ y::s ∨ x ∈ t, or.elim H (assume H1, or.elim H1 (take H2 : x = y, or.inl H2) (take H2 : x ∈ s, or.inr (IH (or.inl H2)))) (assume H1 : x ∈ t, or.inr (IH (or.inr H1)))) 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 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 (take H : x ∈ [], false.elim (iff.elim_left !mem_nil H)) (take y l, assume IH : x ∈ l → ∃s t : list T, l = s ++ (x::t), assume H : x ∈ y::l, or.elim H (assume H1 : x = y, exists.intro [] (!exists.intro (H1 ▸ rfl))) (assume H1 : x ∈ l, obtain s (H2 : ∃t : list T, l = s ++ (x::t)), from IH H1, obtain t (H3 : l = s ++ (x::t)), from H2, have H4 : y :: l = (y::s) ++ (x::t), from H3 ▸ rfl, !exists.intro (!exists.intro H4))) 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)) (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) (assume Heq : x = h, decidable.inl (or.inl Heq)) (assume Hne : x ≠ h, decidable.inl (or.inr Hp))) (assume Hn : ¬x ∈ l, decidable.rec_on (H x h) (assume Heq : x = h, decidable.inl (or.inl Heq)) (assume Hne : x ≠ h, have H1 : ¬(x = h ∨ x ∈ l), from assume H2 : x = h ∨ x ∈ l, or.elim H2 (assume Heq, absurd Heq Hne) (assume Hp, absurd Hp Hn), have H2 : ¬x ∈ h::l, from iff.elim_right (not_iff_not_of_iff !mem_cons) H1, decidable.inr H2))) /- 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 (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.not_mem {l : list T} {x : T} : ¬x ∈ l → find x l = length l := list.rec_on l (assume P₁ : ¬x ∈ [], _) (take y l, assume iH : ¬x ∈ l → find x l = length l, assume P₁ : ¬x ∈ y::l, have P₂ : ¬(x = y ∨ x ∈ l), from iff.elim_right (not_iff_not_of_iff !mem_cons) P₁, have P₃ : ¬x = y ∧ ¬x ∈ l, from (iff.elim_left not_or_iff_not_and_not P₂), 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 P₃) ... = succ (length l) : {iH (and.elim_right P₃)} ... = length (y::l) : !length_cons⁻¹) end /- nth element -/ definition nth [h : inhabited T] : list T → nat → T | [] n := arbitrary T | (a :: l) 0 := a | (a :: l) (n+1) := nth l n theorem nth_zero [h : inhabited T] (a : T) (l : list T) : nth (a :: l) 0 = a theorem nth_succ [h : inhabited T] (a : T) (l : list T) (n : nat) : nth (a::l) (n+1) = nth l n open decidable definition decidable_eq {A : Type} [H : decidable_eq A] : ∀ l₁ l₂ : list A, decidable (l₁ = l₂) | [] [] := inl rfl | [] (b::l₂) := inr (λ H, list.no_confusion H) | (a::l₁) [] := inr (λ H, list.no_confusion H) | (a::l₁) (b::l₂) := match H a b with | inl Hab := match decidable_eq l₁ l₂ with | inl He := inl (eq.rec_on Hab (eq.rec_on He rfl)) | inr Hn := inr (λ H, list.no_confusion H (λ Hab Ht, absurd Ht Hn)) end | inr Hnab := inr (λ H, list.no_confusion H (λ Hab Ht, absurd Hab Hnab)) end section combinators variables {A B C : Type} definition map (f : A → B) : list A → list B | [] := [] | (a :: l) := f a :: map l theorem map_nil (f : A → B) : map f [] = [] theorem map_cons (f : A → B) (a : A) (l : list A) : map f (a :: l) = f a :: map f l theorem map_map (g : B → C) (f : A → B) : ∀ l : list A, map g (map f l) = map (g ∘ f) l | [] := rfl | (a :: l) := show (g ∘ f) a :: map g (map f l) = map (g ∘ f) (a :: l), by rewrite (map_map l) theorem len_map (f : A → B) : ∀ l : list A, length (map f l) = length l | [] := rfl | (a :: l) := show length (map f l) + 1 = length l + 1, by rewrite (len_map l) definition map₂ (f : A → B → C) : list A → list B → list C | [] _ := [] | _ [] := [] | (x::xs) (y::ys) := f x y :: map₂ xs ys definition foldl (f : A → B → A) : A → list B → A | a [] := a | a (b :: l) := foldl (f a b) l definition foldr (f : A → B → B) : B → list A → B | b [] := b | b (a :: l) := f a (foldr b l) section foldl_eq_foldr -- foldl and foldr coincide when f is commutative and associative parameters {α : Type} {f : α → α → α} hypothesis (Hcomm : ∀ a b, f a b = f b a) hypothesis (Hassoc : ∀ a b c, f a (f b c) = f (f a b) c) include Hcomm Hassoc theorem foldl_eq_of_comm_of_assoc : ∀ a b l, foldl f a (b::l) = f b (foldl f a l) | a b nil := Hcomm a b | a b (c::l) := begin change (foldl f (f (f a b) c) l = f b (foldl f (f a c) l)), rewrite -foldl_eq_of_comm_of_assoc, change (foldl f (f (f a b) c) l = foldl f (f (f a c) b) l), have H₁ : f (f a b) c = f (f a c) b, by rewrite [-Hassoc, -Hassoc, Hcomm b c], rewrite H₁ end theorem foldl_eq_foldr : ∀ a l, foldl f a l = foldr f a l | a nil := rfl | a (b :: l) := begin rewrite foldl_eq_of_comm_of_assoc, esimp, change (f b (foldl f a l) = f b (foldr f a l)), rewrite foldl_eq_foldr end end foldl_eq_foldr definition all (p : A → Prop) (l : list A) : Prop := foldr (λ a r, p a ∧ r) true l definition any (p : A → Prop) (l : list A) : Prop := foldr (λ a r, p a ∨ r) false l definition decidable_all (p : A → Prop) [H : decidable_pred p] : ∀ l, decidable (all p l) | [] := decidable_true | (a :: l) := match H a with | inl Hp₁ := match decidable_all l with | inl Hp₂ := inl (and.intro Hp₁ Hp₂) | inr Hn₂ := inr (not_and_of_not_right (p a) Hn₂) end | inr Hn := inr (not_and_of_not_left (all p l) Hn) end definition decidable_any (p : A → Prop) [H : decidable_pred p] : ∀ l, decidable (any p l) | [] := decidable_false | (a :: l) := match H a with | inl Hp := inl (or.inl Hp) | inr Hn₁ := match decidable_any l with | inl Hp₂ := inl (or.inr Hp₂) | inr Hn₂ := inr (not_or Hn₁ Hn₂) end end definition zip (l₁ : list A) (l₂ : list B) : list (A × B) := map₂ (λ a b, (a, b)) l₁ l₂ definition unzip : list (A × B) → list A × list B | [] := ([], []) | ((a, b) :: l) := match unzip l with | (la, lb) := (a :: la, b :: lb) end theorem unzip_nil : unzip (@nil (A × B)) = ([], []) theorem unzip_cons (a : A) (b : B) (l : list (A × B)) : unzip ((a, b) :: l) = match unzip l with (la, lb) := (a :: la, b :: lb) end theorem zip_unzip : ∀ (l : list (A × B)), zip (pr₁ (unzip l)) (pr₂ (unzip l)) = l | [] := rfl | ((a, b) :: l) := begin rewrite unzip_cons, have r : zip (pr₁ (unzip l)) (pr₂ (unzip l)) = l, from zip_unzip l, revert r, apply (prod.cases_on (unzip l)), intros [la, lb, r], rewrite -r end end combinators /- flat -/ variable {A : Type} definition flat (l : list (list A)) : list A := foldl append nil l end list attribute list.decidable_eq [instance] attribute list.decidable_mem [instance] attribute list.decidable_any [instance] attribute list.decidable_all [instance]
d7c0bdee9664b55798ee86c74218f20d8f38e606
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/algebra/associated.lean
1d95d26a69cfd5104ea098b077c4b26264f04ae2
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
30,609
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Jens Wagemaker -/ import algebra.big_operators.basic import algebra.divisibility import algebra.invertible /-! # Associated, prime, and irreducible elements. -/ variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} theorem is_unit_iff_dvd_one [comm_monoid α] {x : α} : is_unit x ↔ x ∣ 1 := ⟨by rintro ⟨u, rfl⟩; exact ⟨_, u.mul_inv.symm⟩, λ ⟨y, h⟩, ⟨⟨x, y, h.symm, by rw [h, mul_comm]⟩, rfl⟩⟩ theorem is_unit_iff_forall_dvd [comm_monoid α] {x : α} : is_unit x ↔ ∀ y, x ∣ y := is_unit_iff_dvd_one.trans ⟨λ h y, h.trans (one_dvd _), λ h, h _⟩ theorem is_unit_of_dvd_unit {α} [comm_monoid α] {x y : α} (xy : x ∣ y) (hu : is_unit y) : is_unit x := is_unit_iff_dvd_one.2 $ xy.trans $ is_unit_iff_dvd_one.1 hu lemma is_unit_of_dvd_one [comm_monoid α] : ∀a ∣ 1, is_unit (a:α) | a ⟨b, eq⟩ := ⟨units.mk_of_mul_eq_one a b eq.symm, rfl⟩ lemma dvd_and_not_dvd_iff [comm_cancel_monoid_with_zero α] {x y : α} : x ∣ y ∧ ¬y ∣ x ↔ dvd_not_unit x y := ⟨λ ⟨⟨d, hd⟩, hyx⟩, ⟨λ hx0, by simpa [hx0] using hyx, ⟨d, mt is_unit_iff_dvd_one.1 (λ ⟨e, he⟩, hyx ⟨e, by rw [hd, mul_assoc, ← he, mul_one]⟩), hd⟩⟩, λ ⟨hx0, d, hdu, hdx⟩, ⟨⟨d, hdx⟩, λ ⟨e, he⟩, hdu (is_unit_of_dvd_one _ ⟨e, mul_left_cancel₀ hx0 $ by conv {to_lhs, rw [he, hdx]};simp [mul_assoc]⟩)⟩⟩ lemma pow_dvd_pow_iff [comm_cancel_monoid_with_zero α] {x : α} {n m : ℕ} (h0 : x ≠ 0) (h1 : ¬ is_unit x) : x ^ n ∣ x ^ m ↔ n ≤ m := begin split, { intro h, rw [← not_lt], intro hmn, apply h1, have : x ^ m * x ∣ x ^ m * 1, { rw [← pow_succ', mul_one], exact (pow_dvd_pow _ (nat.succ_le_of_lt hmn)).trans h }, rwa [mul_dvd_mul_iff_left, ← is_unit_iff_dvd_one] at this, apply pow_ne_zero m h0 }, { apply pow_dvd_pow } end section prime variables [comm_monoid_with_zero α] /-- prime element of a `comm_monoid_with_zero` -/ def prime (p : α) : Prop := p ≠ 0 ∧ ¬ is_unit p ∧ (∀a b, p ∣ a * b → p ∣ a ∨ p ∣ b) namespace prime variables {p : α} (hp : prime p) include hp lemma ne_zero : p ≠ 0 := hp.1 lemma not_unit : ¬ is_unit p := hp.2.1 lemma not_dvd_one : ¬ p ∣ 1 := mt (is_unit_of_dvd_one _) hp.not_unit lemma ne_one : p ≠ 1 := λ h, hp.2.1 (h.symm ▸ is_unit_one) lemma dvd_or_dvd (hp : prime p) {a b : α} (h : p ∣ a * b) : p ∣ a ∨ p ∣ b := hp.2.2 a b h lemma dvd_of_dvd_pow (hp : prime p) {a : α} {n : ℕ} (h : p ∣ a^n) : p ∣ a := begin induction n with n ih, { rw pow_zero at h, have := is_unit_of_dvd_one _ h, have := not_unit hp, contradiction }, rw pow_succ at h, cases dvd_or_dvd hp h with dvd_a dvd_pow, { assumption }, exact ih dvd_pow end lemma exists_mem_multiset_dvd {s : multiset α} : p ∣ s.prod → ∃ a ∈ s, p ∣ a := multiset.induction_on s (λ h, (hp.not_dvd_one h).elim) $ λ a s ih h, have p ∣ a * s.prod, by simpa using h, match hp.dvd_or_dvd this with | or.inl h := ⟨a, multiset.mem_cons_self a s, h⟩ | or.inr h := let ⟨a, has, h⟩ := ih h in ⟨a, multiset.mem_cons_of_mem has, h⟩ end lemma exists_mem_multiset_map_dvd {s : multiset β} {f : β → α} : p ∣ (s.map f).prod → ∃ a ∈ s, p ∣ f a := λ h, by simpa only [exists_prop, multiset.mem_map, exists_exists_and_eq_and] using hp.exists_mem_multiset_dvd h lemma exists_mem_finset_dvd {s : finset β} {f : β → α} : p ∣ s.prod f → ∃ i ∈ s, p ∣ f i := hp.exists_mem_multiset_map_dvd end prime @[simp] lemma not_prime_zero : ¬ prime (0 : α) := λ h, h.ne_zero rfl @[simp] lemma not_prime_one : ¬ prime (1 : α) := λ h, h.not_unit is_unit_one end prime lemma prime.left_dvd_or_dvd_right_of_dvd_mul [comm_cancel_monoid_with_zero α] {p : α} (hp : prime p) {a b : α} : a ∣ p * b → p ∣ a ∨ a ∣ b := begin rintro ⟨c, hc⟩, rcases hp.2.2 a c (hc ▸ dvd_mul_right _ _) with h | ⟨x, rfl⟩, { exact or.inl h }, { rw [mul_left_comm, mul_right_inj' hp.ne_zero] at hc, exact or.inr (hc.symm ▸ dvd_mul_right _ _) } end /-- `irreducible p` states that `p` is non-unit and only factors into units. We explicitly avoid stating that `p` is non-zero, this would require a semiring. Assuming only a monoid allows us to reuse irreducible for associated elements. -/ class irreducible [monoid α] (p : α) : Prop := (not_unit' : ¬ is_unit p) (is_unit_or_is_unit' : ∀a b, p = a * b → is_unit a ∨ is_unit b) namespace irreducible lemma not_unit [monoid α] {p : α} (hp : irreducible p) : ¬ is_unit p := hp.1 lemma is_unit_or_is_unit [monoid α] {p : α} (hp : irreducible p) {a b : α} (h : p = a * b) : is_unit a ∨ is_unit b := irreducible.is_unit_or_is_unit' a b h end irreducible lemma irreducible_iff [monoid α] {p : α} : irreducible p ↔ ¬ is_unit p ∧ ∀a b, p = a * b → is_unit a ∨ is_unit b := ⟨λ h, ⟨h.1, h.2⟩, λ h, ⟨h.1, h.2⟩⟩ @[simp] theorem not_irreducible_one [monoid α] : ¬ irreducible (1 : α) := by simp [irreducible_iff] @[simp] theorem not_irreducible_zero [monoid_with_zero α] : ¬ irreducible (0 : α) | ⟨hn0, h⟩ := have is_unit (0:α) ∨ is_unit (0:α), from h 0 0 ((mul_zero 0).symm), this.elim hn0 hn0 theorem irreducible.ne_zero [monoid_with_zero α] : ∀ {p:α}, irreducible p → p ≠ 0 | _ hp rfl := not_irreducible_zero hp theorem of_irreducible_mul {α} [monoid α] {x y : α} : irreducible (x * y) → is_unit x ∨ is_unit y | ⟨_, h⟩ := h _ _ rfl theorem irreducible_or_factor {α} [monoid α] (x : α) (h : ¬ is_unit x) : irreducible x ∨ ∃ a b, ¬ is_unit a ∧ ¬ is_unit b ∧ a * b = x := begin haveI := classical.dec, refine or_iff_not_imp_right.2 (λ H, _), simp [h, irreducible_iff] at H ⊢, refine λ a b h, classical.by_contradiction $ λ o, _, simp [not_or_distrib] at o, exact H _ o.1 _ o.2 h.symm end protected lemma prime.irreducible [comm_cancel_monoid_with_zero α] {p : α} (hp : prime p) : irreducible p := ⟨hp.not_unit, λ a b hab, (show a * b ∣ a ∨ a * b ∣ b, from hab ▸ hp.dvd_or_dvd (hab ▸ dvd_rfl)).elim (λ ⟨x, hx⟩, or.inr (is_unit_iff_dvd_one.2 ⟨x, mul_right_cancel₀ (show a ≠ 0, from λ h, by simp [*, prime] at *) $ by conv {to_lhs, rw hx}; simp [mul_comm, mul_assoc, mul_left_comm]⟩)) (λ ⟨x, hx⟩, or.inl (is_unit_iff_dvd_one.2 ⟨x, mul_right_cancel₀ (show b ≠ 0, from λ h, by simp [*, prime] at *) $ by conv {to_lhs, rw hx}; simp [mul_comm, mul_assoc, mul_left_comm]⟩))⟩ lemma succ_dvd_or_succ_dvd_of_succ_sum_dvd_mul [comm_cancel_monoid_with_zero α] {p : α} (hp : prime p) {a b : α} {k l : ℕ} : p ^ k ∣ a → p ^ l ∣ b → p ^ ((k + l) + 1) ∣ a * b → p ^ (k + 1) ∣ a ∨ p ^ (l + 1) ∣ b := λ ⟨x, hx⟩ ⟨y, hy⟩ ⟨z, hz⟩, have h : p ^ (k + l) * (x * y) = p ^ (k + l) * (p * z), by simpa [mul_comm, pow_add, hx, hy, mul_assoc, mul_left_comm] using hz, have hp0: p ^ (k + l) ≠ 0, from pow_ne_zero _ hp.ne_zero, have hpd : p ∣ x * y, from ⟨z, by rwa [mul_right_inj' hp0] at h⟩, (hp.dvd_or_dvd hpd).elim (λ ⟨d, hd⟩, or.inl ⟨d, by simp [*, pow_succ, mul_comm, mul_left_comm, mul_assoc]⟩) (λ ⟨d, hd⟩, or.inr ⟨d, by simp [*, pow_succ, mul_comm, mul_left_comm, mul_assoc]⟩) /-- If `p` and `q` are irreducible, then `p ∣ q` implies `q ∣ p`. -/ lemma irreducible.dvd_symm [monoid α] {p q : α} (hp : irreducible p) (hq : irreducible q) : p ∣ q → q ∣ p := begin tactic.unfreeze_local_instances, rintros ⟨q', rfl⟩, rw is_unit.mul_right_dvd (or.resolve_left (of_irreducible_mul hq) hp.not_unit), end lemma irreducible.dvd_comm [monoid α] {p q : α} (hp : irreducible p) (hq : irreducible q) : p ∣ q ↔ q ∣ p := ⟨hp.dvd_symm hq, hq.dvd_symm hp⟩ /-- Two elements of a `monoid` are `associated` if one of them is another one multiplied by a unit on the right. -/ def associated [monoid α] (x y : α) : Prop := ∃u:units α, x * u = y local infix ` ~ᵤ ` : 50 := associated namespace associated @[refl] protected theorem refl [monoid α] (x : α) : x ~ᵤ x := ⟨1, by simp⟩ @[symm] protected theorem symm [monoid α] : ∀{x y : α}, x ~ᵤ y → y ~ᵤ x | x _ ⟨u, rfl⟩ := ⟨u⁻¹, by rw [mul_assoc, units.mul_inv, mul_one]⟩ @[trans] protected theorem trans [monoid α] : ∀{x y z : α}, x ~ᵤ y → y ~ᵤ z → x ~ᵤ z | x _ _ ⟨u, rfl⟩ ⟨v, rfl⟩ := ⟨u * v, by rw [units.coe_mul, mul_assoc]⟩ /-- The setoid of the relation `x ~ᵤ y` iff there is a unit `u` such that `x * u = y` -/ protected def setoid (α : Type*) [monoid α] : setoid α := { r := associated, iseqv := ⟨associated.refl, λa b, associated.symm, λa b c, associated.trans⟩ } end associated local attribute [instance] associated.setoid theorem unit_associated_one [monoid α] {u : units α} : (u : α) ~ᵤ 1 := ⟨u⁻¹, units.mul_inv u⟩ theorem associated_one_iff_is_unit [monoid α] {a : α} : (a : α) ~ᵤ 1 ↔ is_unit a := iff.intro (assume h, let ⟨c, h⟩ := h.symm in h ▸ ⟨c, (one_mul _).symm⟩) (assume ⟨c, h⟩, associated.symm ⟨c, by simp [h]⟩) theorem associated_zero_iff_eq_zero [monoid_with_zero α] (a : α) : a ~ᵤ 0 ↔ a = 0 := iff.intro (assume h, let ⟨u, h⟩ := h.symm in by simpa using h.symm) (assume h, h ▸ associated.refl a) theorem associated_one_of_mul_eq_one [comm_monoid α] {a : α} (b : α) (hab : a * b = 1) : a ~ᵤ 1 := show (units.mk_of_mul_eq_one a b hab : α) ~ᵤ 1, from unit_associated_one theorem associated_one_of_associated_mul_one [comm_monoid α] {a b : α} : a * b ~ᵤ 1 → a ~ᵤ 1 | ⟨u, h⟩ := associated_one_of_mul_eq_one (b * u) $ by simpa [mul_assoc] using h lemma associated_mul_unit_left {β : Type*} [monoid β] (a u : β) (hu : is_unit u) : associated (a * u) a := let ⟨u', hu⟩ := hu in ⟨u'⁻¹, hu ▸ units.mul_inv_cancel_right _ _⟩ lemma associated_unit_mul_left {β : Type*} [comm_monoid β] (a u : β) (hu : is_unit u) : associated (u * a) a := begin rw mul_comm, exact associated_mul_unit_left _ _ hu end lemma associated_mul_unit_right {β : Type*} [monoid β] (a u : β) (hu : is_unit u) : associated a (a * u) := (associated_mul_unit_left a u hu).symm lemma associated_unit_mul_right {β : Type*} [comm_monoid β] (a u : β) (hu : is_unit u) : associated a (u * a) := (associated_unit_mul_left a u hu).symm lemma associated.mul_mul [comm_monoid α] {a₁ a₂ b₁ b₂ : α} : a₁ ~ᵤ b₁ → a₂ ~ᵤ b₂ → (a₁ * a₂) ~ᵤ (b₁ * b₂) | ⟨c₁, h₁⟩ ⟨c₂, h₂⟩ := ⟨c₁ * c₂, by simp [h₁.symm, h₂.symm, mul_assoc, mul_comm, mul_left_comm]⟩ lemma associated.mul_left [comm_monoid α] (a : α) {b c : α} (h : b ~ᵤ c) : (a * b) ~ᵤ (a * c) := (associated.refl a).mul_mul h lemma associated.mul_right [comm_monoid α] {a b : α} (h : a ~ᵤ b) (c : α) : (a * c) ~ᵤ (b * c) := h.mul_mul (associated.refl c) lemma associated.pow_pow [comm_monoid α] {a b : α} {n : ℕ} (h : a ~ᵤ b) : a ^ n ~ᵤ b ^ n := begin induction n with n ih, { simp [h] }, convert h.mul_mul ih; rw pow_succ end protected lemma associated.dvd [monoid α] {a b : α} : a ~ᵤ b → a ∣ b := λ ⟨u, hu⟩, ⟨u, hu.symm⟩ protected lemma associated.dvd_dvd [monoid α] {a b : α} (h : a ~ᵤ b) : a ∣ b ∧ b ∣ a := ⟨h.dvd, h.symm.dvd⟩ theorem associated_of_dvd_dvd [cancel_monoid_with_zero α] {a b : α} (hab : a ∣ b) (hba : b ∣ a) : a ~ᵤ b := begin rcases hab with ⟨c, rfl⟩, rcases hba with ⟨d, a_eq⟩, by_cases ha0 : a = 0, { simp [*] at * }, have hac0 : a * c ≠ 0, { intro con, rw [con, zero_mul] at a_eq, apply ha0 a_eq, }, have : a * (c * d) = a * 1 := by rw [← mul_assoc, ← a_eq, mul_one], have hcd : (c * d) = 1, from mul_left_cancel₀ ha0 this, have : a * c * (d * c) = a * c * 1 := by rw [← mul_assoc, ← a_eq, mul_one], have hdc : d * c = 1, from mul_left_cancel₀ hac0 this, exact ⟨⟨c, d, hcd, hdc⟩, rfl⟩ end theorem dvd_dvd_iff_associated [cancel_monoid_with_zero α] {a b : α} : a ∣ b ∧ b ∣ a ↔ a ~ᵤ b := ⟨λ ⟨h1, h2⟩, associated_of_dvd_dvd h1 h2, associated.dvd_dvd⟩ lemma exists_associated_mem_of_dvd_prod [comm_cancel_monoid_with_zero α] {p : α} (hp : prime p) {s : multiset α} : (∀ r ∈ s, prime r) → p ∣ s.prod → ∃ q ∈ s, p ~ᵤ q := multiset.induction_on s (by simp [mt is_unit_iff_dvd_one.2 hp.not_unit]) (λ a s ih hs hps, begin rw [multiset.prod_cons] at hps, cases hp.dvd_or_dvd hps with h h, { use [a, by simp], cases h with u hu, cases (((hs a (multiset.mem_cons.2 (or.inl rfl))).irreducible) .is_unit_or_is_unit hu).resolve_left hp.not_unit with v hv, exact ⟨v, by simp [hu, hv]⟩ }, { rcases ih (λ r hr, hs _ (multiset.mem_cons.2 (or.inr hr))) h with ⟨q, hq₁, hq₂⟩, exact ⟨q, multiset.mem_cons.2 (or.inr hq₁), hq₂⟩ } end) lemma associated.dvd_iff_dvd_left [monoid α] {a b c : α} (h : a ~ᵤ b) : a ∣ c ↔ b ∣ c := let ⟨u, hu⟩ := h in hu ▸ units.mul_right_dvd.symm lemma associated.dvd_iff_dvd_right [monoid α] {a b c : α} (h : b ~ᵤ c) : a ∣ b ↔ a ∣ c := let ⟨u, hu⟩ := h in hu ▸ units.dvd_mul_right.symm lemma associated.eq_zero_iff [monoid_with_zero α] {a b : α} (h : a ~ᵤ b) : a = 0 ↔ b = 0 := ⟨λ ha, let ⟨u, hu⟩ := h in by simp [hu.symm, ha], λ hb, let ⟨u, hu⟩ := h.symm in by simp [hu.symm, hb]⟩ lemma associated.ne_zero_iff [monoid_with_zero α] {a b : α} (h : a ~ᵤ b) : a ≠ 0 ↔ b ≠ 0 := not_congr h.eq_zero_iff protected lemma associated.prime [comm_monoid_with_zero α] {p q : α} (h : p ~ᵤ q) (hp : prime p) : prime q := ⟨h.ne_zero_iff.1 hp.ne_zero, let ⟨u, hu⟩ := h in ⟨λ ⟨v, hv⟩, hp.not_unit ⟨v * u⁻¹, by simp [hv, hu.symm]⟩, hu ▸ by { simp [units.mul_right_dvd], intros a b, exact hp.dvd_or_dvd }⟩⟩ lemma irreducible.associated_of_dvd [cancel_monoid_with_zero α] {p q : α} (p_irr : irreducible p) (q_irr : irreducible q) (dvd : p ∣ q) : associated p q := associated_of_dvd_dvd dvd (p_irr.dvd_symm q_irr dvd) lemma irreducible.dvd_irreducible_iff_associated [cancel_monoid_with_zero α] {p q : α} (pp : irreducible p) (qp : irreducible q) : p ∣ q ↔ associated p q := ⟨irreducible.associated_of_dvd pp qp, associated.dvd⟩ lemma prime.associated_of_dvd [comm_cancel_monoid_with_zero α] {p q : α} (p_prime : prime p) (q_prime : prime q) (dvd : p ∣ q) : associated p q := p_prime.irreducible.associated_of_dvd q_prime.irreducible dvd theorem prime.dvd_prime_iff_associated [comm_cancel_monoid_with_zero α] {p q : α} (pp : prime p) (qp : prime q) : p ∣ q ↔ associated p q := pp.irreducible.dvd_irreducible_iff_associated qp.irreducible lemma associated.prime_iff [comm_monoid_with_zero α] {p q : α} (h : p ~ᵤ q) : prime p ↔ prime q := ⟨h.prime, h.symm.prime⟩ protected lemma associated.is_unit [monoid α] {a b : α} (h : a ~ᵤ b) : is_unit a → is_unit b := let ⟨u, hu⟩ := h in λ ⟨v, hv⟩, ⟨v * u, by simp [hv, hu.symm]⟩ lemma associated.is_unit_iff [monoid α] {a b : α} (h : a ~ᵤ b) : is_unit a ↔ is_unit b := ⟨h.is_unit, h.symm.is_unit⟩ protected lemma associated.irreducible [monoid α] {p q : α} (h : p ~ᵤ q) (hp : irreducible p) : irreducible q := ⟨mt h.symm.is_unit hp.1, let ⟨u, hu⟩ := h in λ a b hab, have hpab : p = a * (b * (u⁻¹ : units α)), from calc p = (p * u) * (u ⁻¹ : units α) : by simp ... = _ : by rw hu; simp [hab, mul_assoc], (hp.is_unit_or_is_unit hpab).elim or.inl (λ ⟨v, hv⟩, or.inr ⟨v * u, by simp [hv]⟩)⟩ protected lemma associated.irreducible_iff [monoid α] {p q : α} (h : p ~ᵤ q) : irreducible p ↔ irreducible q := ⟨h.irreducible, h.symm.irreducible⟩ lemma associated.of_mul_left [comm_cancel_monoid_with_zero α] {a b c d : α} (h : a * b ~ᵤ c * d) (h₁ : a ~ᵤ c) (ha : a ≠ 0) : b ~ᵤ d := let ⟨u, hu⟩ := h in let ⟨v, hv⟩ := associated.symm h₁ in ⟨u * (v : units α), mul_left_cancel₀ ha begin rw [← hv, mul_assoc c (v : α) d, mul_left_comm c, ← hu], simp [hv.symm, mul_assoc, mul_comm, mul_left_comm] end⟩ lemma associated.of_mul_right [comm_cancel_monoid_with_zero α] {a b c d : α} : a * b ~ᵤ c * d → b ~ᵤ d → b ≠ 0 → a ~ᵤ c := by rw [mul_comm a, mul_comm c]; exact associated.of_mul_left section unique_units variables [monoid α] [unique (units α)] lemma units_eq_one (u : units α) : u = 1 := subsingleton.elim u 1 theorem associated_iff_eq {x y : α} : x ~ᵤ y ↔ x = y := begin split, { rintro ⟨c, rfl⟩, rw [units_eq_one c, units.coe_one, mul_one] }, { rintro rfl, refl }, end theorem associated_eq_eq : (associated : α → α → Prop) = eq := by { ext, rw associated_iff_eq } end unique_units /-- The quotient of a monoid by the `associated` relation. Two elements `x` and `y` are associated iff there is a unit `u` such that `x * u = y`. There is a natural monoid structure on `associates α`. -/ def associates (α : Type*) [monoid α] : Type* := quotient (associated.setoid α) namespace associates open associated /-- The canonical quotient map from a monoid `α` into the `associates` of `α` -/ protected def mk {α : Type*} [monoid α] (a : α) : associates α := ⟦ a ⟧ instance [monoid α] : inhabited (associates α) := ⟨⟦1⟧⟩ theorem mk_eq_mk_iff_associated [monoid α] {a b : α} : associates.mk a = associates.mk b ↔ a ~ᵤ b := iff.intro quotient.exact quot.sound theorem quotient_mk_eq_mk [monoid α] (a : α) : ⟦ a ⟧ = associates.mk a := rfl theorem quot_mk_eq_mk [monoid α] (a : α) : quot.mk setoid.r a = associates.mk a := rfl theorem forall_associated [monoid α] {p : associates α → Prop} : (∀a, p a) ↔ (∀a, p (associates.mk a)) := iff.intro (assume h a, h _) (assume h a, quotient.induction_on a h) theorem mk_surjective [monoid α] : function.surjective (@associates.mk α _) := forall_associated.2 (λ a, ⟨a, rfl⟩) instance [monoid α] : has_one (associates α) := ⟨⟦ 1 ⟧⟩ theorem one_eq_mk_one [monoid α] : (1 : associates α) = associates.mk 1 := rfl instance [monoid α] : has_bot (associates α) := ⟨1⟩ lemma exists_rep [monoid α] (a : associates α) : ∃ a0 : α, associates.mk a0 = a := quot.exists_rep a section comm_monoid variable [comm_monoid α] instance : has_mul (associates α) := ⟨λa' b', quotient.lift_on₂ a' b' (λa b, ⟦ a * b ⟧) $ assume a₁ a₂ b₁ b₂ ⟨c₁, h₁⟩ ⟨c₂, h₂⟩, quotient.sound $ ⟨c₁ * c₂, by simp [h₁.symm, h₂.symm, mul_assoc, mul_comm, mul_left_comm]⟩⟩ theorem mk_mul_mk {x y : α} : associates.mk x * associates.mk y = associates.mk (x * y) := rfl instance : comm_monoid (associates α) := { one := 1, mul := (*), mul_one := assume a', quotient.induction_on a' $ assume a, show ⟦a * 1⟧ = ⟦ a ⟧, by simp, one_mul := assume a', quotient.induction_on a' $ assume a, show ⟦1 * a⟧ = ⟦ a ⟧, by simp, mul_assoc := assume a' b' c', quotient.induction_on₃ a' b' c' $ assume a b c, show ⟦a * b * c⟧ = ⟦a * (b * c)⟧, by rw [mul_assoc], mul_comm := assume a' b', quotient.induction_on₂ a' b' $ assume a b, show ⟦a * b⟧ = ⟦b * a⟧, by rw [mul_comm] } instance : preorder (associates α) := { le := has_dvd.dvd, le_refl := dvd_refl, le_trans := λ a b c, dvd_trans} @[simp] lemma mk_one : associates.mk (1 : α) = 1 := rfl /-- `associates.mk` as a `monoid_hom`. -/ protected def mk_monoid_hom : α →* (associates α) := ⟨associates.mk, mk_one, λ x y, mk_mul_mk⟩ @[simp] lemma mk_monoid_hom_apply (a : α) : associates.mk_monoid_hom a = associates.mk a := rfl lemma associated_map_mk {f : associates α →* α} (hinv : function.right_inverse f associates.mk) (a : α) : a ~ᵤ f (associates.mk a) := associates.mk_eq_mk_iff_associated.1 (hinv (associates.mk a)).symm lemma mk_pow (a : α) (n : ℕ) : associates.mk (a ^ n) = (associates.mk a) ^ n := by induction n; simp [*, pow_succ, associates.mk_mul_mk.symm] lemma dvd_eq_le : ((∣) : associates α → associates α → Prop) = (≤) := rfl theorem prod_mk {p : multiset α} : (p.map associates.mk).prod = associates.mk p.prod := multiset.induction_on p (by simp; refl) $ assume a s ih, by simp [ih]; refl theorem rel_associated_iff_map_eq_map {p q : multiset α} : multiset.rel associated p q ↔ p.map associates.mk = q.map associates.mk := by { rw [← multiset.rel_eq, multiset.rel_map], simp only [mk_eq_mk_iff_associated] } theorem mul_eq_one_iff {x y : associates α} : x * y = 1 ↔ (x = 1 ∧ y = 1) := iff.intro (quotient.induction_on₂ x y $ assume a b h, have a * b ~ᵤ 1, from quotient.exact h, ⟨quotient.sound $ associated_one_of_associated_mul_one this, quotient.sound $ associated_one_of_associated_mul_one $ by rwa [mul_comm] at this⟩) (by simp {contextual := tt}) theorem prod_eq_one_iff {p : multiset (associates α)} : p.prod = 1 ↔ (∀a ∈ p, (a:associates α) = 1) := multiset.induction_on p (by simp) (by simp [mul_eq_one_iff, or_imp_distrib, forall_and_distrib] {contextual := tt}) theorem units_eq_one (u : units (associates α)) : u = 1 := units.ext (mul_eq_one_iff.1 u.val_inv).1 instance unique_units : unique (units (associates α)) := { default := 1, uniq := associates.units_eq_one } theorem coe_unit_eq_one (u : units (associates α)): (u : associates α) = 1 := by simp theorem is_unit_iff_eq_one (a : associates α) : is_unit a ↔ a = 1 := iff.intro (assume ⟨u, h⟩, h ▸ coe_unit_eq_one _) (assume h, h.symm ▸ is_unit_one) theorem is_unit_mk {a : α} : is_unit (associates.mk a) ↔ is_unit a := calc is_unit (associates.mk a) ↔ a ~ᵤ 1 : by rw [is_unit_iff_eq_one, one_eq_mk_one, mk_eq_mk_iff_associated] ... ↔ is_unit a : associated_one_iff_is_unit section order theorem mul_mono {a b c d : associates α} (h₁ : a ≤ b) (h₂ : c ≤ d) : a * c ≤ b * d := let ⟨x, hx⟩ := h₁, ⟨y, hy⟩ := h₂ in ⟨x * y, by simp [hx, hy, mul_comm, mul_assoc, mul_left_comm]⟩ theorem one_le {a : associates α} : 1 ≤ a := dvd.intro _ (one_mul a) theorem prod_le_prod {p q : multiset (associates α)} (h : p ≤ q) : p.prod ≤ q.prod := begin haveI := classical.dec_eq (associates α), haveI := classical.dec_eq α, suffices : p.prod ≤ (p + (q - p)).prod, { rwa [add_tsub_cancel_of_le h] at this }, suffices : p.prod * 1 ≤ p.prod * (q - p).prod, { simpa }, exact mul_mono (le_refl p.prod) one_le end theorem le_mul_right {a b : associates α} : a ≤ a * b := ⟨b, rfl⟩ theorem le_mul_left {a b : associates α} : a ≤ b * a := by rw [mul_comm]; exact le_mul_right instance : order_bot (associates α) := { bot := 1, bot_le := assume a, one_le } end order end comm_monoid instance [has_zero α] [monoid α] : has_zero (associates α) := ⟨⟦ 0 ⟧⟩ instance [has_zero α] [monoid α] : has_top (associates α) := ⟨0⟩ section comm_monoid_with_zero variables [comm_monoid_with_zero α] @[simp] theorem mk_eq_zero {a : α} : associates.mk a = 0 ↔ a = 0 := ⟨assume h, (associated_zero_iff_eq_zero a).1 $ quotient.exact h, assume h, h.symm ▸ rfl⟩ theorem mk_ne_zero {a : α} : associates.mk a ≠ 0 ↔ a ≠ 0 := not_congr mk_eq_zero instance : comm_monoid_with_zero (associates α) := { zero_mul := by { rintro ⟨a⟩, show associates.mk (0 * a) = associates.mk 0, rw [zero_mul] }, mul_zero := by { rintro ⟨a⟩, show associates.mk (a * 0) = associates.mk 0, rw [mul_zero] }, .. associates.comm_monoid, .. associates.has_zero } instance : order_top (associates α) := { top := 0, le_top := assume a, ⟨0, (mul_zero a).symm⟩ } instance [nontrivial α] : nontrivial (associates α) := ⟨⟨0, 1, assume h, have (0 : α) ~ᵤ 1, from quotient.exact h, have (0 : α) = 1, from ((associated_zero_iff_eq_zero 1).1 this.symm).symm, zero_ne_one this⟩⟩ lemma exists_non_zero_rep {a : associates α} : a ≠ 0 → ∃ a0 : α, a0 ≠ 0 ∧ associates.mk a0 = a := quotient.induction_on a (λ b nz, ⟨b, mt (congr_arg quotient.mk) nz, rfl⟩) theorem dvd_of_mk_le_mk {a b : α} : associates.mk a ≤ associates.mk b → a ∣ b | ⟨c', hc'⟩ := (quotient.induction_on c' $ assume c hc, let ⟨d, hd⟩ := (quotient.exact hc).symm in ⟨(↑d) * c, calc b = (a * c) * ↑d : hd.symm ... = a * (↑d * c) : by ac_refl⟩) hc' theorem mk_le_mk_of_dvd {a b : α} : a ∣ b → associates.mk a ≤ associates.mk b := assume ⟨c, hc⟩, ⟨associates.mk c, by simp [hc]; refl⟩ theorem mk_le_mk_iff_dvd_iff {a b : α} : associates.mk a ≤ associates.mk b ↔ a ∣ b := iff.intro dvd_of_mk_le_mk mk_le_mk_of_dvd theorem mk_dvd_mk {a b : α} : associates.mk a ∣ associates.mk b ↔ a ∣ b := iff.intro dvd_of_mk_le_mk mk_le_mk_of_dvd lemma prime.le_or_le {p : associates α} (hp : prime p) {a b : associates α} (h : p ≤ a * b) : p ≤ a ∨ p ≤ b := hp.2.2 a b h lemma exists_mem_multiset_le_of_prime {s : multiset (associates α)} {p : associates α} (hp : prime p) : p ≤ s.prod → ∃a∈s, p ≤ a := multiset.induction_on s (assume ⟨d, eq⟩, (hp.ne_one (mul_eq_one_iff.1 eq.symm).1).elim) $ assume a s ih h, have p ≤ a * s.prod, by simpa using h, match prime.le_or_le hp this with | or.inl h := ⟨a, multiset.mem_cons_self a s, h⟩ | or.inr h := let ⟨a, has, h⟩ := ih h in ⟨a, multiset.mem_cons_of_mem has, h⟩ end lemma prime_mk (p : α) : prime (associates.mk p) ↔ _root_.prime p := begin rw [prime, _root_.prime, forall_associated], transitivity, { apply and_congr, refl, apply and_congr, refl, apply forall_congr, assume a, exact forall_associated }, apply and_congr mk_ne_zero, apply and_congr, { rw [is_unit_mk], }, apply forall_congr, assume a, apply forall_congr, assume b, rw [mk_mul_mk, mk_dvd_mk, mk_dvd_mk, mk_dvd_mk], end theorem irreducible_mk (a : α) : irreducible (associates.mk a) ↔ irreducible a := begin simp only [irreducible_iff, is_unit_mk], apply and_congr iff.rfl, split, { rintro h x y rfl, simpa [is_unit_mk] using h (associates.mk x) (associates.mk y) rfl }, { intros h x y, refine quotient.induction_on₂ x y (assume x y a_eq, _), rcases quotient.exact a_eq.symm with ⟨u, a_eq⟩, rw mul_assoc at a_eq, show is_unit (associates.mk x) ∨ is_unit (associates.mk y), simpa [is_unit_mk] using h _ _ a_eq.symm } end theorem mk_dvd_not_unit_mk_iff {a b : α} : dvd_not_unit (associates.mk a) (associates.mk b) ↔ dvd_not_unit a b := begin rw [dvd_not_unit, dvd_not_unit, mk_ne_zero], apply and_congr_right, intro ane0, split, { contrapose!, rw forall_associated, intros h x hx hbax, rw [mk_mul_mk, mk_eq_mk_iff_associated] at hbax, cases hbax with u hu, apply h (x * ↑u⁻¹), { rw is_unit_mk at hx, rw associated.is_unit_iff, apply hx, use u, simp, }, simp [← mul_assoc, ← hu] }, { rintro ⟨x, ⟨hx, rfl⟩⟩, use associates.mk x, simp [is_unit_mk, mk_mul_mk, hx], } end theorem dvd_not_unit_of_lt {a b : associates α} (hlt : a < b) : dvd_not_unit a b := begin split, { rintro rfl, apply not_lt_of_le _ hlt, apply dvd_zero }, rcases hlt with ⟨⟨x, rfl⟩, ndvd⟩, refine ⟨x, _, rfl⟩, contrapose! ndvd, rcases ndvd with ⟨u, rfl⟩, simp, end end comm_monoid_with_zero section comm_cancel_monoid_with_zero variable [comm_cancel_monoid_with_zero α] instance : partial_order (associates α) := { le_antisymm := λ a' b', quotient.induction_on₂ a' b' (λ a b hab hba, quot.sound $ associated_of_dvd_dvd (dvd_of_mk_le_mk hab) (dvd_of_mk_le_mk hba)) .. associates.preorder } instance : no_zero_divisors (associates α) := ⟨λ x y, (quotient.induction_on₂ x y $ assume a b h, have a * b = 0, from (associated_zero_iff_eq_zero _).1 (quotient.exact h), have a = 0 ∨ b = 0, from mul_eq_zero.1 this, this.imp (assume h, h.symm ▸ rfl) (assume h, h.symm ▸ rfl))⟩ theorem irreducible_iff_prime_iff : (∀ a : α, irreducible a ↔ prime a) ↔ (∀ a : (associates α), irreducible a ↔ prime a) := begin rw forall_associated, split; intros h a; have ha := h a; rw irreducible_mk at *; rw prime_mk at *; exact ha, end lemma eq_of_mul_eq_mul_left : ∀(a b c : associates α), a ≠ 0 → a * b = a * c → b = c := begin rintros ⟨a⟩ ⟨b⟩ ⟨c⟩ ha h, rcases quotient.exact' h with ⟨u, hu⟩, have hu : a * (b * ↑u) = a * c, { rwa [← mul_assoc] }, exact quotient.sound' ⟨u, mul_left_cancel₀ (mk_ne_zero.1 ha) hu⟩ end lemma eq_of_mul_eq_mul_right : ∀(a b c : associates α), b ≠ 0 → a * b = c * b → a = c := λ a b c bne0, (mul_comm b a) ▸ (mul_comm b c) ▸ (eq_of_mul_eq_mul_left b a c bne0) lemma le_of_mul_le_mul_left (a b c : associates α) (ha : a ≠ 0) : a * b ≤ a * c → b ≤ c | ⟨d, hd⟩ := ⟨d, eq_of_mul_eq_mul_left a _ _ ha $ by rwa ← mul_assoc⟩ lemma one_or_eq_of_le_of_prime : ∀(p m : associates α), prime p → m ≤ p → (m = 1 ∨ m = p) | _ m ⟨hp0, hp1, h⟩ ⟨d, rfl⟩ := match h m d dvd_rfl with | or.inl h := classical.by_cases (assume : m = 0, by simp [this]) $ assume : m ≠ 0, have m * d ≤ m * 1, by simpa using h, have d ≤ 1, from associates.le_of_mul_le_mul_left m d 1 ‹m ≠ 0› this, have d = 1, from bot_unique this, by simp [this] | or.inr h := classical.by_cases (assume : d = 0, by simp [this] at hp0; contradiction) $ assume : d ≠ 0, have d * m ≤ d * 1, by simpa [mul_comm] using h, or.inl $ bot_unique $ associates.le_of_mul_le_mul_left d m 1 ‹d ≠ 0› this end instance : comm_cancel_monoid_with_zero (associates α) := { mul_left_cancel_of_ne_zero := eq_of_mul_eq_mul_left, mul_right_cancel_of_ne_zero := eq_of_mul_eq_mul_right, .. (infer_instance : comm_monoid_with_zero (associates α)) } theorem dvd_not_unit_iff_lt {a b : associates α} : dvd_not_unit a b ↔ a < b := dvd_and_not_dvd_iff.symm end comm_cancel_monoid_with_zero end associates namespace multiset lemma prod_ne_zero_of_prime [comm_cancel_monoid_with_zero α] [nontrivial α] (s : multiset α) (h : ∀ x ∈ s, prime x) : s.prod ≠ 0 := multiset.prod_ne_zero (λ h0, prime.ne_zero (h 0 h0) rfl) end multiset
713a7379e84ebfeadadfe2b18d7450212bf4ce7f
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/issue332.lean
e22e0ed0533ae67a33f40ac33122b9287a45343e
[ "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
150
lean
import logic.eq variable {a : Type} definition foo {A : Type} : A → A := begin intro a, exact a end check @foo example : foo 10 = (10:num) := rfl
c079b43e9778f77dd14aa21ffb6c21d26ccdfe90
f57749ca63d6416f807b770f67559503fdb21001
/tests/lean/run/new_obtain3.lean
54466897fedd2f10be1b040e6ad4283dd55227d2
[ "Apache-2.0" ]
permissive
aliassaf/lean
bd54e85bed07b1ff6f01396551867b2677cbc6ac
f9b069b6a50756588b309b3d716c447004203152
refs/heads/master
1,610,982,152,948
1,438,916,029,000
1,438,916,029,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
607
lean
import data.set open set function eq.ops variables {X Y Z : Type} lemma image_compose (f : Y → X) (g : X → Y) (a : set X) : (f ∘ g) '[a] = f '[g '[a]] := setext (take z, iff.intro (assume Hz : z ∈ (f ∘ g) '[a], obtain x (Hx₁ : x ∈ a) (Hx₂ : f (g x) = z), from Hz, have Hgx : g x ∈ g '[a], from mem_image Hx₁ rfl, show z ∈ f '[g '[a]], from mem_image Hgx Hx₂) (assume Hz : z ∈ f '[g '[a]], obtain y [x (Hz₁ : x ∈ a) (Hz₂ : g x = y)] (Hy₂ : f y = z), from Hz, show z ∈ (f ∘ g) '[a], from mem_image Hz₁ (Hz₂⁻¹ ▸ Hy₂)))
1284b7791e3ef9c508bd5baefce15ffeba0486d0
6094e25ea0b7699e642463b48e51b2ead6ddc23f
/library/theories/group_theory/cyclic.lean
9b490220fd24e173275b6d7ea407752708125c57
[ "Apache-2.0" ]
permissive
gbaz/lean
a7835c4e3006fbbb079e8f8ffe18aacc45adebfb
a501c308be3acaa50a2c0610ce2e0d71becf8032
refs/heads/master
1,611,198,791,433
1,451,339,111,000
1,451,339,111,000
48,713,797
0
0
null
1,451,338,939,000
1,451,338,939,000
null
UTF-8
Lean
false
false
15,858
lean
/- Copyright (c) 2015 Haitao Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author : Haitao Zhang -/ import data algebra.group algebra.group_power .finsubg .hom .perm open function finset open eq.ops namespace group_theory section cyclic open nat fin list local attribute madd [reducible] variable {A : Type} variable [ambG : group A] include ambG lemma pow_mod {a : A} {n m : nat} : a ^ m = 1 → a ^ n = a ^ (n % m) := assume Pid, assert a ^ (n / m * m) = 1, from calc a ^ (n / m * m) = a ^ (m * (n / m)) : by rewrite (mul.comm (n / m) m) ... = (a ^ m) ^ (n / m) : by rewrite pow_mul ... = 1 ^ (n / m) : by rewrite Pid ... = 1 : one_pow (n / m), calc a ^ n = a ^ (n / m * m + n % m) : by rewrite -(eq_div_mul_add_mod n m) ... = a ^ (n / m * m) * a ^ (n % m) : by rewrite pow_add ... = 1 * a ^ (n % m) : by rewrite this ... = a ^ (n % m) : by rewrite one_mul lemma pow_sub_eq_one_of_pow_eq {a : A} {i j : nat} : a^i = a^j → a^(i - j) = 1 := assume Pe, or.elim (lt_or_ge i j) (assume Piltj, begin rewrite [sub_eq_zero_of_le (nat.le_of_lt Piltj)] end) (assume Pigej, begin rewrite [pow_sub a Pigej, Pe, mul.right_inv] end) lemma pow_dist_eq_one_of_pow_eq {a : A} {i j : nat} : a^i = a^j → a^(dist i j) = 1 := assume Pe, or.elim (lt_or_ge i j) (suppose i < j, by rewrite [dist_eq_sub_of_lt this]; exact pow_sub_eq_one_of_pow_eq (eq.symm Pe)) (suppose i ≥ j, by rewrite [dist_eq_sub_of_ge this]; exact pow_sub_eq_one_of_pow_eq Pe) lemma pow_madd {a : A} {n : nat} {i j : fin (succ n)} : a^(succ n) = 1 → a^(val (i + j)) = a^i * a^j := assume Pe, calc a^(val (i + j)) = a^((i + j) % (succ n)) : rfl ... = a^(val i + val j) : by rewrite [-pow_mod Pe] ... = a^i * a^j : by rewrite pow_add lemma mk_pow_mod {a : A} {n m : nat} : a ^ (succ m) = 1 → a ^ n = a ^ (mk_mod m n) := assume Pe, pow_mod Pe variable [finA : fintype A] include finA open fintype variable [deceqA : decidable_eq A] include deceqA lemma exists_pow_eq_one (a : A) : ∃ n, n < card A ∧ a ^ (succ n) = 1 := let f := (λ i : fin (succ (card A)), a ^ i) in assert Pninj : ¬(injective f), from assume Pinj, absurd (card_le_of_inj _ _ (exists.intro f Pinj)) (begin rewrite [card_fin], apply not_succ_le_self end), obtain i₁ P₁, from exists_not_of_not_forall Pninj, obtain i₂ P₂, from exists_not_of_not_forall P₁, obtain Pfe Pne, from iff.elim_left not_implies_iff_and_not P₂, assert Pvne : val i₁ ≠ val i₂, from assume Pveq, absurd (eq_of_veq Pveq) Pne, exists.intro (pred (dist i₁ i₂)) (begin rewrite [succ_pred_of_pos (dist_pos_of_ne Pvne)], apply and.intro, apply lt_of_succ_lt_succ, rewrite [succ_pred_of_pos (dist_pos_of_ne Pvne)], apply nat.lt_of_le_of_lt dist_le_max (max_lt i₁ i₂), apply pow_dist_eq_one_of_pow_eq Pfe end) -- Another possibility is to generate a list of powers and use find to get the first -- unity. -- The bound on bex is arbitrary as long as it is large enough (at least card A). Making -- it larger simplifies some proofs, such as a ∈ cyc a. definition cyc (a : A) : finset A := {x ∈ univ | bex (succ (card A)) (λ n, a ^ n = x)} definition order (a : A) := card (cyc a) definition pow_fin (a : A) (n : nat) (i : fin (order a)) := a ^ (i + n) definition cyc_pow_fin (a : A) (n : nat) : finset A := image (pow_fin a n) univ lemma order_le_group_order {a : A} : order a ≤ card A := card_le_card_of_subset !subset_univ lemma cyc_has_one (a : A) : 1 ∈ cyc a := begin apply mem_sep_of_mem !mem_univ, existsi 0, apply and.intro, apply zero_lt_succ, apply pow_zero end lemma order_pos (a : A) : 0 < order a := length_pos_of_mem (cyc_has_one a) lemma cyc_mul_closed (a : A) : finset_mul_closed_on (cyc a) := take g h, assume Pgin Phin, obtain n Plt Pe, from exists_pow_eq_one a, obtain i Pilt Pig, from of_mem_sep Pgin, obtain j Pjlt Pjh, from of_mem_sep Phin, begin rewrite [-Pig, -Pjh, -pow_add, pow_mod Pe], apply mem_sep_of_mem !mem_univ, existsi ((i + j) % (succ n)), apply and.intro, apply nat.lt_trans (mod_lt (i+j) !zero_lt_succ) (succ_lt_succ Plt), apply rfl end lemma cyc_has_inv (a : A) : finset_has_inv (cyc a) := take g, assume Pgin, obtain n Plt Pe, from exists_pow_eq_one a, obtain i Pilt Pig, from of_mem_sep Pgin, let ni := -(mk_mod n i) in assert Pinv : g*a^ni = 1, by rewrite [-Pig, mk_pow_mod Pe, -(pow_madd Pe), add.right_inv], begin rewrite [inv_eq_of_mul_eq_one Pinv], apply mem_sep_of_mem !mem_univ, existsi ni, apply and.intro, apply nat.lt_trans (is_lt ni) (succ_lt_succ Plt), apply rfl end lemma self_mem_cyc (a : A) : a ∈ cyc a := mem_sep_of_mem !mem_univ (exists.intro (1 : nat) (and.intro (succ_lt_succ card_pos) !pow_one)) lemma mem_cyc (a : A) : ∀ {n : nat}, a^n ∈ cyc a | 0 := cyc_has_one a | (succ n) := begin rewrite pow_succ', apply cyc_mul_closed a, exact mem_cyc, apply self_mem_cyc end lemma order_le {a : A} {n : nat} : a^(succ n) = 1 → order a ≤ succ n := assume Pe, let s := image (pow_nat a) (upto (succ n)) in assert Psub: cyc a ⊆ s, from subset_of_forall (take g, assume Pgin, obtain i Pilt Pig, from of_mem_sep Pgin, begin rewrite [-Pig, pow_mod Pe], apply mem_image, apply mem_upto_of_lt (mod_lt i !zero_lt_succ), exact rfl end), #nat calc order a ≤ card s : card_le_card_of_subset Psub ... ≤ card (upto (succ n)) : !card_image_le ... = succ n : card_upto (succ n) lemma pow_ne_of_lt_order {a : A} {n : nat} : succ n < order a → a^(succ n) ≠ 1 := assume Plt, not_imp_not_of_imp order_le (not_le_of_gt Plt) lemma eq_zero_of_pow_eq_one {a : A} : ∀ {n : nat}, a^n = 1 → n < order a → n = 0 | 0 := assume Pe Plt, rfl | (succ n) := assume Pe Plt, absurd Pe (pow_ne_of_lt_order Plt) lemma pow_fin_inj (a : A) (n : nat) : injective (pow_fin a n) := take i j : fin (order a), suppose a^(i + n) = a^(j + n), have a^(dist i j) = 1, begin apply !dist_add_add_right ▸ (pow_dist_eq_one_of_pow_eq this) end, have dist i j = 0, from eq_zero_of_pow_eq_one this (nat.lt_of_le_of_lt dist_le_max (max_lt i j)), eq_of_veq (eq_of_dist_eq_zero this) lemma cyc_eq_cyc (a : A) (n : nat) : cyc_pow_fin a n = cyc a := assert Psub : cyc_pow_fin a n ⊆ cyc a, from subset_of_forall (take g, assume Pgin, obtain i Pin Pig, from exists_of_mem_image Pgin, by rewrite [-Pig]; apply mem_cyc), eq_of_card_eq_of_subset (begin apply eq.trans, apply card_image_eq_of_inj_on, rewrite [to_set_univ, -set.injective_iff_inj_on_univ], exact pow_fin_inj a n, rewrite [card_fin] end) Psub lemma pow_order (a : A) : a^(order a) = 1 := obtain i Pin Pone, from exists_of_mem_image (eq.symm (cyc_eq_cyc a 1) ▸ cyc_has_one a), or.elim (eq_or_lt_of_le (succ_le_of_lt (is_lt i))) (assume P, P ▸ Pone) (assume P, absurd Pone (pow_ne_of_lt_order P)) lemma eq_one_of_order_eq_one {a : A} : order a = 1 → a = 1 := assume Porder, calc a = a^1 : by rewrite (pow_one a) ... = a^(order a) : by rewrite Porder ... = 1 : by rewrite pow_order lemma order_of_min_pow {a : A} {n : nat} (Pone : a^(succ n) = 1) (Pmin : ∀ i, i < n → a^(succ i) ≠ 1) : order a = succ n := or.elim (eq_or_lt_of_le (order_le Pone)) (λ P, P) (λ P : order a < succ n, begin assert Pn : a^(order a) ≠ 1, rewrite [-(succ_pred_of_pos (order_pos a))], apply Pmin, apply nat.lt_of_succ_lt_succ, rewrite [succ_pred_of_pos !order_pos], assumption, exact absurd (pow_order a) Pn end) lemma order_dvd_of_pow_eq_one {a : A} {n : nat} (Pone : a^n = 1) : order a ∣ n := assert Pe : a^(n % order a) = 1, from begin revert Pone, rewrite [eq_div_mul_add_mod n (order a) at {1}, pow_add, mul.comm _ (order a), pow_mul, pow_order, one_pow, one_mul], intros, assumption end, dvd_of_mod_eq_zero (eq_zero_of_pow_eq_one Pe (mod_lt n !order_pos)) definition cyc_is_finsubg [instance] (a : A) : is_finsubg (cyc a) := is_finsubg.mk (cyc_has_one a) (cyc_mul_closed a) (cyc_has_inv a) lemma order_dvd_group_order (a : A) : order a ∣ card A := dvd.intro (eq.symm (!mul.comm ▸ lagrange_theorem (subset_univ (cyc a)))) definition pow_fin' (a : A) (i : fin (succ (pred (order a)))) := pow_nat a i local attribute group_of_add_group [instance] lemma pow_fin_hom (a : A) : homomorphic (pow_fin' a) := take i j : fin (succ (pred (order a))), begin rewrite [↑pow_fin'], apply pow_madd, rewrite [succ_pred_of_pos !order_pos], exact pow_order a end definition pow_fin_is_iso (a : A) : is_iso_class (pow_fin' a) := is_iso_class.mk (pow_fin_hom a) (have H : injective (λ (i : fin (order a)), a ^ (val i + 0)), from pow_fin_inj a 0, begin+ rewrite [↑pow_fin', succ_pred_of_pos !order_pos]; exact H end) end cyclic section rot open nat list open fin fintype list section local attribute group_of_add_group [instance] lemma pow_eq_mul {n : nat} {i : fin (succ n)} : ∀ {k : nat}, i^k = mk_mod n (i*k) | 0 := by rewrite [pow_zero] | (succ k) := begin assert Psucc : i^(succ k) = madd (i^k) i, apply pow_succ', rewrite [Psucc, pow_eq_mul], apply eq_of_veq, rewrite [mul_succ, val_madd, ↑mk_mod, mod_add_mod] end end definition rotl : ∀ {n : nat} m : nat, fin n → fin n | 0 := take m i, elim0 i | (succ n) := take m, madd (mk_mod n (n*m)) definition rotr : ∀ {n : nat} m : nat, fin n → fin n | (0:nat) := take m i, elim0 i | (nat.succ n) := take m, madd (-(mk_mod n (n*m))) lemma rotl_succ' {n m : nat} : rotl m = madd (mk_mod n (n*m)) := rfl lemma rotl_zero : ∀ {n : nat}, @rotl n 0 = id | 0 := funext take i, elim0 i | (nat.succ n) := funext take i, begin rewrite [↑rotl, mul_zero, mk_mod_zero_eq, zero_madd] end lemma rotl_id : ∀ {n : nat}, @rotl n n = id | 0 := funext take i, elim0 i | (nat.succ n) := assert P : mk_mod n (n * succ n) = mk_mod n 0, from eq_of_veq (by rewrite [↑mk_mod, mul_mod_left]), begin rewrite [rotl_succ', P], apply rotl_zero end lemma rotl_to_zero {n i : nat} : rotl i (mk_mod n i) = 0 := eq_of_veq begin rewrite [↑rotl, val_madd], esimp [mk_mod], rewrite [ mod_add_mod, add_mod_mod, -succ_mul, mul_mod_right] end lemma rotl_compose : ∀ {n : nat} {j k : nat}, (@rotl n j) ∘ (rotl k) = rotl (j + k) | 0 := take j k, funext take i, elim0 i | (succ n) := take j k, funext take i, eq.symm begin rewrite [*rotl_succ', left_distrib, -(@madd_mk_mod n (n*j)), madd_assoc], end lemma rotr_rotl : ∀ {n : nat} (m : nat) {i : fin n}, rotr m (rotl m i) = i | 0 := take m i, elim0 i | (nat.succ n) := take m i, calc (-(mk_mod n (n*m))) + ((mk_mod n (n*m)) + i) = i : by rewrite neg_add_cancel_left lemma rotl_rotr : ∀ {n : nat} (m : nat), (@rotl n m) ∘ (rotr m) = id | 0 := take m, funext take i, elim0 i | (nat.succ n) := take m, funext take i, calc (mk_mod n (n*m)) + (-(mk_mod n (n*m)) + i) = i : add_neg_cancel_left lemma rotl_succ {n : nat} : (rotl 1) ∘ (@succ n) = lift_succ := funext (take i, eq_of_veq (begin rewrite [↑compose, ↑rotl, ↑madd, mul_one n, ↑mk_mod, mod_add_mod, ↑lift_succ, val_succ, -succ_add_eq_succ_add, add_mod_self_left, mod_eq_of_lt (lt.trans (is_lt i) !lt_succ_self), -val_lift] end)) definition list.rotl {A : Type} : ∀ l : list A, list A | [] := [] | (a::l) := l++[a] lemma rotl_cons {A : Type} {a : A} {l} : list.rotl (a::l) = l++[a] := rfl lemma rotl_map {A B : Type} {f : A → B} : ∀ {l : list A}, list.rotl (map f l) = map f (list.rotl l) | [] := rfl | (a::l) := begin rewrite [map_cons, *rotl_cons, map_append] end lemma rotl_eq_rotl : ∀ {n : nat}, map (rotl 1) (upto n) = list.rotl (upto n) | 0 := rfl | (succ n) := begin rewrite [upto_step at {1}, upto_succ, rotl_cons, map_append], congruence, rewrite [map_map], congruence, exact rotl_succ, rewrite [map_singleton], congruence, rewrite [↑rotl, mul_one n, ↑mk_mod, ↑maxi, ↑madd], congruence, rewrite [ mod_add_mod, val_zero, add_zero, mod_eq_of_lt !lt_succ_self ] end definition seq [reducible] (A : Type) (n : nat) := fin n → A variable {A : Type} definition rotl_fun {n : nat} (m : nat) (f : seq A n) : seq A n := f ∘ (rotl m) definition rotr_fun {n : nat} (m : nat) (f : seq A n) : seq A n := f ∘ (rotr m) lemma rotl_seq_zero {n : nat} : rotl_fun 0 = @id (seq A n) := funext take f, begin rewrite [↑rotl_fun, rotl_zero] end lemma rotl_seq_ne_id : ∀ {n : nat}, (∃ a b : A, a ≠ b) → ∀ i, i < n → rotl_fun (succ i) ≠ (@id (seq A (succ n))) | 0 := assume Pex, take i, assume Piltn, absurd Piltn !not_lt_zero | (nat.succ n) := assume Pex, obtain a b Pne, from Pex, take i, assume Pilt, let f := (λ j : fin (succ (succ n)), if j = 0 then a else b), fi := mk_mod (succ n) (succ i) in have Pfne : rotl_fun (succ i) f fi ≠ f fi, from begin rewrite [↑rotl_fun, rotl_to_zero, mk_mod_of_lt (succ_lt_succ Pilt), if_pos rfl, if_neg mk_succ_ne_zero], assumption end, have P : rotl_fun (succ i) f ≠ f, from assume Peq, absurd (congr_fun Peq fi) Pfne, assume Peq, absurd (congr_fun Peq f) P lemma rotr_rotl_fun {n : nat} (m : nat) (f : seq A n) : rotr_fun m (rotl_fun m f) = f := calc f ∘ (rotl m) ∘ (rotr m) = f ∘ ((rotl m) ∘ (rotr m)) : by rewrite -compose.assoc ... = f ∘ id : by rewrite (rotl_rotr m) lemma rotl_fun_inj {n : nat} {m : nat} : @injective (seq A n) (seq A n) (rotl_fun m) := injective_of_has_left_inverse (exists.intro (rotr_fun m) (rotr_rotl_fun m)) lemma seq_rotl_eq_list_rotl {n : nat} (f : seq A n) : fun_to_list (rotl_fun 1 f) = list.rotl (fun_to_list f) := begin rewrite [↑fun_to_list, ↑rotl_fun, -map_map, rotl_map], congruence, exact rotl_eq_rotl end end rot section rotg open nat fin fintype definition rotl_perm [reducible] (A : Type) [finA : fintype A] [deceqA : decidable_eq A] (n : nat) (m : nat) : perm (seq A n) := perm.mk (rotl_fun m) rotl_fun_inj variable {A : Type} variable [finA : fintype A] variable [deceqA : decidable_eq A] variable {n : nat} include finA deceqA lemma rotl_perm_mul {i j : nat} : (rotl_perm A n i) * (rotl_perm A n j) = rotl_perm A n (j+i) := eq_of_feq (funext take f, calc f ∘ (rotl j) ∘ (rotl i) = f ∘ ((rotl j) ∘ (rotl i)) : by rewrite -compose.assoc ... = f ∘ (rotl (j+i)) : by rewrite rotl_compose) lemma rotl_perm_pow_eq : ∀ {i : nat}, (rotl_perm A n 1) ^ i = rotl_perm A n i | 0 := begin rewrite [pow_zero, ↑rotl_perm, perm_one, -eq_iff_feq], esimp, rewrite rotl_seq_zero end | (succ i) := begin rewrite [pow_succ', rotl_perm_pow_eq, rotl_perm_mul, one_add] end lemma rotl_perm_pow_eq_one : (rotl_perm A n 1) ^ n = 1 := eq.trans rotl_perm_pow_eq (eq_of_feq begin esimp [rotl_perm], rewrite [↑rotl_fun, rotl_id] end) lemma rotl_perm_mod {i : nat} : rotl_perm A n i = rotl_perm A n (i % n) := calc rotl_perm A n i = (rotl_perm A n 1) ^ i : by rewrite rotl_perm_pow_eq ... = (rotl_perm A n 1) ^ (i % n) : by rewrite (pow_mod rotl_perm_pow_eq_one) ... = rotl_perm A n (i % n) : by rewrite rotl_perm_pow_eq -- needs A to have at least two elements! lemma rotl_perm_pow_ne_one (Pex : ∃ a b : A, a ≠ b) : ∀ i, i < n → (rotl_perm A (succ n) 1)^(succ i) ≠ 1 := take i, assume Piltn, begin intro P, revert P, rewrite [rotl_perm_pow_eq, -eq_iff_feq, perm_one, *perm.f_mk], intro P, exact absurd P (rotl_seq_ne_id Pex i Piltn) end lemma rotl_perm_order (Pex : ∃ a b : A, a ≠ b) : order (rotl_perm A (succ n) 1) = (succ n) := order_of_min_pow rotl_perm_pow_eq_one (rotl_perm_pow_ne_one Pex) end rotg end group_theory