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
4efc8d6aa091dbb0c6dded93633e810637e67766
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/list/chain_auto.lean
f91af5e36c90818b2a4b49e9ef9a406e585d501e
[]
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
11,837
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Kenny Lau, Yury Kudryashov -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.list.pairwise import Mathlib.logic.relation import Mathlib.PostPort universes u v namespace Mathlib namespace list /- chain relation (conjunction of R a b ∧ R b c ∧ R c d ...) -/ theorem chain_iff {α : Type u} (R : α → α → Prop) : ∀ (ᾰ : α) (ᾰ_1 : List α), chain R ᾰ ᾰ_1 ↔ ᾰ_1 = [] ∨ Exists fun {b : α} => Exists fun {l : List α} => R ᾰ b ∧ chain R b l ∧ ᾰ_1 = b :: l := sorry theorem rel_of_chain_cons {α : Type u} {R : α → α → Prop} {a : α} {b : α} {l : List α} (p : chain R a (b :: l)) : R a b := and.left (iff.mp chain_cons p) theorem chain_of_chain_cons {α : Type u} {R : α → α → Prop} {a : α} {b : α} {l : List α} (p : chain R a (b :: l)) : chain R b l := and.right (iff.mp chain_cons p) theorem chain.imp' {α : Type u} {R : α → α → Prop} {S : α → α → Prop} (HRS : ∀ {a b : α}, R a b → S a b) {a : α} {b : α} (Hab : ∀ {c : α}, R a c → S b c) {l : List α} (p : chain R a l) : chain S b l := sorry theorem chain.imp {α : Type u} {R : α → α → Prop} {S : α → α → Prop} (H : ∀ (a b : α), R a b → S a b) {a : α} {l : List α} (p : chain R a l) : chain S a l := chain.imp' H (H a) p theorem chain.iff {α : Type u} {R : α → α → Prop} {S : α → α → Prop} (H : ∀ (a b : α), R a b ↔ S a b) {a : α} {l : List α} : chain R a l ↔ chain S a l := { mp := chain.imp fun (a b : α) => iff.mp (H a b), mpr := chain.imp fun (a b : α) => iff.mpr (H a b) } theorem chain.iff_mem {α : Type u} {R : α → α → Prop} {a : α} {l : List α} : chain R a l ↔ chain (fun (x y : α) => x ∈ a :: l ∧ y ∈ l ∧ R x y) a l := sorry theorem chain_singleton {α : Type u} {R : α → α → Prop} {a : α} {b : α} : chain R a [b] ↔ R a b := sorry theorem chain_split {α : Type u} {R : α → α → Prop} {a : α} {b : α} {l₁ : List α} {l₂ : List α} : chain R a (l₁ ++ b :: l₂) ↔ chain R a (l₁ ++ [b]) ∧ chain R b l₂ := sorry theorem chain_map {α : Type u} {β : Type v} {R : α → α → Prop} (f : β → α) {b : β} {l : List β} : chain R (f b) (map f l) ↔ chain (fun (a b : β) => R (f a) (f b)) b l := sorry theorem chain_of_chain_map {α : Type u} {β : Type v} {R : α → α → Prop} {S : β → β → Prop} (f : α → β) (H : ∀ (a b : α), S (f a) (f b) → R a b) {a : α} {l : List α} (p : chain S (f a) (map f l)) : chain R a l := chain.imp H (iff.mp (chain_map f) p) theorem chain_map_of_chain {α : Type u} {β : Type v} {R : α → α → Prop} {S : β → β → Prop} (f : α → β) (H : ∀ (a b : α), R a b → S (f a) (f b)) {a : α} {l : List α} (p : chain R a l) : chain S (f a) (map f l) := iff.mpr (chain_map f) (chain.imp H p) theorem chain_pmap_of_chain {α : Type u} {β : Type v} {R : α → α → Prop} {S : β → β → Prop} {p : α → Prop} {f : (a : α) → p a → β} (H : ∀ (a b : α) (ha : p a) (hb : p b), R a b → S (f a ha) (f b hb)) {a : α} {l : List α} (hl₁ : chain R a l) (ha : p a) (hl₂ : ∀ (a : α), a ∈ l → p a) : chain S (f a ha) (pmap f l hl₂) := sorry theorem chain_of_chain_pmap {α : Type u} {β : Type v} {R : α → α → Prop} {S : β → β → Prop} {p : α → Prop} (f : (a : α) → p a → β) {l : List α} (hl₁ : ∀ (a : α), a ∈ l → p a) {a : α} (ha : p a) (hl₂ : chain S (f a ha) (pmap f l hl₁)) (H : ∀ (a b : α) (ha : p a) (hb : p b), S (f a ha) (f b hb) → R a b) : chain R a l := sorry theorem chain_of_pairwise {α : Type u} {R : α → α → Prop} {a : α} {l : List α} (p : pairwise R (a :: l)) : chain R a l := sorry theorem chain_iff_pairwise {α : Type u} {R : α → α → Prop} (tr : transitive R) {a : α} {l : List α} : chain R a l ↔ pairwise R (a :: l) := sorry theorem chain_iff_nth_le {α : Type u} {R : α → α → Prop} {a : α} {l : List α} : chain R a l ↔ (∀ (h : 0 < length l), R a (nth_le l 0 h)) ∧ ∀ (i : ℕ) (h : i < length l - 1), R (nth_le l i (nat.lt_of_lt_pred h)) (nth_le l (i + 1) (iff.mp nat.lt_pred_iff h)) := sorry theorem chain'.imp {α : Type u} {R : α → α → Prop} {S : α → α → Prop} (H : ∀ (a b : α), R a b → S a b) {l : List α} (p : chain' R l) : chain' S l := list.cases_on l (fun (p : chain' R []) => trivial) (fun (l_hd : α) (l_tl : List α) (p : chain' R (l_hd :: l_tl)) => chain.imp H p) p theorem chain'.iff {α : Type u} {R : α → α → Prop} {S : α → α → Prop} (H : ∀ (a b : α), R a b ↔ S a b) {l : List α} : chain' R l ↔ chain' S l := { mp := chain'.imp fun (a b : α) => iff.mp (H a b), mpr := chain'.imp fun (a b : α) => iff.mpr (H a b) } theorem chain'.iff_mem {α : Type u} {R : α → α → Prop} {l : List α} : chain' R l ↔ chain' (fun (x y : α) => x ∈ l ∧ y ∈ l ∧ R x y) l := sorry @[simp] theorem chain'_nil {α : Type u} {R : α → α → Prop} : chain' R [] := trivial @[simp] theorem chain'_singleton {α : Type u} {R : α → α → Prop} (a : α) : chain' R [a] := chain.nil theorem chain'_split {α : Type u} {R : α → α → Prop} {a : α} {l₁ : List α} {l₂ : List α} : chain' R (l₁ ++ a :: l₂) ↔ chain' R (l₁ ++ [a]) ∧ chain' R (a :: l₂) := sorry theorem chain'_map {α : Type u} {β : Type v} {R : α → α → Prop} (f : β → α) {l : List β} : chain' R (map f l) ↔ chain' (fun (a b : β) => R (f a) (f b)) l := list.cases_on l (iff.refl (chain' R (map f []))) fun (l_hd : β) (l_tl : List β) => chain_map f theorem chain'_of_chain'_map {α : Type u} {β : Type v} {R : α → α → Prop} {S : β → β → Prop} (f : α → β) (H : ∀ (a b : α), S (f a) (f b) → R a b) {l : List α} (p : chain' S (map f l)) : chain' R l := chain'.imp H (iff.mp (chain'_map f) p) theorem chain'_map_of_chain' {α : Type u} {β : Type v} {R : α → α → Prop} {S : β → β → Prop} (f : α → β) (H : ∀ (a b : α), R a b → S (f a) (f b)) {l : List α} (p : chain' R l) : chain' S (map f l) := iff.mpr (chain'_map f) (chain'.imp H p) theorem pairwise.chain' {α : Type u} {R : α → α → Prop} {l : List α} : pairwise R l → chain' R l := sorry theorem chain'_iff_pairwise {α : Type u} {R : α → α → Prop} (tr : transitive R) {l : List α} : chain' R l ↔ pairwise R l := list.cases_on l (idRhs (True ↔ pairwise R []) (iff.symm (iff_true_intro pairwise.nil))) fun (l_hd : α) (l_tl : List α) => idRhs (chain R l_hd l_tl ↔ pairwise R (l_hd :: l_tl)) (chain_iff_pairwise tr) @[simp] theorem chain'_cons {α : Type u} {R : α → α → Prop} {x : α} {y : α} {l : List α} : chain' R (x :: y :: l) ↔ R x y ∧ chain' R (y :: l) := chain_cons theorem chain'.cons {α : Type u} {R : α → α → Prop} {x : α} {y : α} {l : List α} (h₁ : R x y) (h₂ : chain' R (y :: l)) : chain' R (x :: y :: l) := iff.mpr chain'_cons { left := h₁, right := h₂ } theorem chain'.tail {α : Type u} {R : α → α → Prop} {l : List α} (h : chain' R l) : chain' R (tail l) := sorry theorem chain'.rel_head {α : Type u} {R : α → α → Prop} {x : α} {y : α} {l : List α} (h : chain' R (x :: y :: l)) : R x y := rel_of_chain_cons h theorem chain'.rel_head' {α : Type u} {R : α → α → Prop} {x : α} {l : List α} (h : chain' R (x :: l)) {y : α} (hy : y ∈ head' l) : R x y := chain'.rel_head (eq.mp (Eq._oldrec (Eq.refl (chain' R (x :: l))) (Eq.symm (cons_head'_tail hy))) h) theorem chain'.cons' {α : Type u} {R : α → α → Prop} {x : α} {l : List α} : chain' R l → (∀ (y : α), y ∈ head' l → R x y) → chain' R (x :: l) := sorry theorem chain'_cons' {α : Type u} {R : α → α → Prop} {x : α} {l : List α} : chain' R (x :: l) ↔ (∀ (y : α), y ∈ head' l → R x y) ∧ chain' R l := sorry theorem chain'.append {α : Type u} {R : α → α → Prop} {l₁ : List α} {l₂ : List α} (h₁ : chain' R l₁) (h₂ : chain' R l₂) (h : ∀ (x : α), x ∈ last' l₁ → ∀ (y : α), y ∈ head' l₂ → R x y) : chain' R (l₁ ++ l₂) := sorry theorem chain'_pair {α : Type u} {R : α → α → Prop} {x : α} {y : α} : chain' R [x, y] ↔ R x y := sorry theorem chain'.imp_head {α : Type u} {R : α → α → Prop} {x : α} {y : α} (h : ∀ {z : α}, R x z → R y z) {l : List α} (hl : chain' R (x :: l)) : chain' R (y :: l) := chain'.cons' (chain'.tail hl) fun (z : α) (hz : z ∈ head' (tail (x :: l))) => h (chain'.rel_head' hl hz) theorem chain'_reverse {α : Type u} {R : α → α → Prop} {l : List α} : chain' R (reverse l) ↔ chain' (flip R) l := sorry theorem chain'_iff_nth_le {α : Type u} {R : α → α → Prop} {l : List α} : chain' R l ↔ ∀ (i : ℕ) (h : i < length l - 1), R (nth_le l i (nat.lt_of_lt_pred h)) (nth_le l (i + 1) (iff.mp nat.lt_pred_iff h)) := sorry /-- If `l₁ l₂` and `l₃` are lists and `l₁ ++ l₂` and `l₂ ++ l₃` both satisfy `chain' R`, then so does `l₁ ++ l₂ ++ l₃` provided `l₂ ≠ []` -/ theorem chain'.append_overlap {α : Type u} {R : α → α → Prop} {l₁ : List α} {l₂ : List α} {l₃ : List α} (h₁ : chain' R (l₁ ++ l₂)) (h₂ : chain' R (l₂ ++ l₃)) (hn : l₂ ≠ []) : chain' R (l₁ ++ l₂ ++ l₃) := sorry /-- If `a` and `b` are related by the reflexive transitive closure of `r`, then there is a `r`-chain starting from `a` and ending on `b`. The converse of `relation_refl_trans_gen_of_exists_chain`. -/ theorem exists_chain_of_relation_refl_trans_gen {α : Type u} {r : α → α → Prop} {a : α} {b : α} (h : relation.refl_trans_gen r a b) : ∃ (l : List α), chain r a l ∧ last (a :: l) (cons_ne_nil a l) = b := sorry /-- Given a chain from `a` to `b`, and a predicate true at `b`, if `r x y → p y → p x` then the predicate is true everywhere in the chain and at `a`. That is, we can propagate the predicate up the chain. -/ theorem chain.induction {α : Type u} {r : α → α → Prop} {a : α} {b : α} (p : α → Prop) (l : List α) (h : chain r a l) (hb : last (a :: l) (cons_ne_nil a l) = b) (carries : ∀ {x y : α}, r x y → p y → p x) (final : p b) (i : α) (H : i ∈ a :: l) : p i := sorry /-- Given a chain from `a` to `b`, and a predicate true at `b`, if `r x y → p y → p x` then the predicate is true at `a`. That is, we can propagate the predicate all the way up the chain. -/ theorem chain.induction_head {α : Type u} {r : α → α → Prop} {a : α} {b : α} (p : α → Prop) (l : List α) (h : chain r a l) (hb : last (a :: l) (cons_ne_nil a l) = b) (carries : ∀ {x y : α}, r x y → p y → p x) (final : p b) : p a := chain.induction p l h hb carries final a (mem_cons_self a l) /-- If there is an `r`-chain starting from `a` and ending at `b`, then `a` and `b` are related by the reflexive transitive closure of `r`. The converse of `exists_chain_of_relation_refl_trans_gen`. -/ theorem relation_refl_trans_gen_of_exists_chain {α : Type u} {r : α → α → Prop} {a : α} {b : α} (l : List α) (hl₁ : chain r a l) (hl₂ : last (a :: l) (cons_ne_nil a l) = b) : relation.refl_trans_gen r a b := chain.induction_head (fun (_x : α) => relation.refl_trans_gen r _x b) l hl₁ hl₂ (fun (x y : α) => relation.refl_trans_gen.head) relation.refl_trans_gen.refl end Mathlib
4589371e5bf89e0b92752ee3078e26f73d226c4f
4727251e0cd73359b15b664c3170e5d754078599
/src/category_theory/monoidal/End.lean
f12b646e85e4cd11f98c4c6ccd3d85dad757f572
[ "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,625
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Andrew Yang -/ import category_theory.monoidal.functor /-! # Endofunctors as a monoidal category. We give the monoidal category structure on `C ⥤ C`, and show that when `C` itself is monoidal, it embeds via a monoidal functor into `C ⥤ C`. ## TODO Can we use this to show coherence results, e.g. a cheap proof that `λ_ (𝟙_ C) = ρ_ (𝟙_ C)`? I suspect this is harder than is usually made out. -/ universes v u namespace category_theory variables (C : Type u) [category.{v} C] /-- The category of endofunctors of any category is a monoidal category, with tensor product given by composition of functors (and horizontal composition of natural transformations). -/ def endofunctor_monoidal_category : monoidal_category (C ⥤ C) := { tensor_obj := λ F G, F ⋙ G, tensor_hom := λ F G F' G' α β, α ◫ β, tensor_unit := 𝟭 C, associator := λ F G H, functor.associator F G H, left_unitor := λ F, functor.left_unitor F, right_unitor := λ F, functor.right_unitor F, }. open category_theory.monoidal_category local attribute [instance] endofunctor_monoidal_category local attribute [reducible] endofunctor_monoidal_category /-- Tensoring on the right gives a monoidal functor from `C` into endofunctors of `C`. -/ @[simps] def tensoring_right_monoidal [monoidal_category.{v} C] : monoidal_functor C (C ⥤ C) := { ε := (right_unitor_nat_iso C).inv, μ := λ X Y, { app := λ Z, (α_ Z X Y).hom, naturality' := λ Z Z' f, by { dsimp, rw associator_naturality, simp, } }, μ_natural' := λ X Y X' Y' f g, by { ext Z, dsimp, simp only [←id_tensor_comp_tensor_id g f, id_tensor_comp, ←tensor_id, category.assoc, associator_naturality, associator_naturality_assoc], }, associativity' := λ X Y Z, by { ext W, dsimp, simp [pentagon], }, left_unitality' := λ X, by { ext Y, dsimp, rw [category.id_comp, triangle, ←tensor_comp], simp, }, right_unitality' := λ X, begin ext Y, dsimp, rw [tensor_id, category.comp_id, right_unitor_tensor_inv, category.assoc, iso.inv_hom_id_assoc, ←id_tensor_comp, iso.inv_hom_id, tensor_id], end, ε_is_iso := by apply_instance, μ_is_iso := λ X Y, -- We could avoid needing to do this explicitly by -- constructing a partially applied analogue of `associator_nat_iso`. ⟨⟨{ app := λ Z, (α_ Z X Y).inv, naturality' := λ Z Z' f, by { dsimp, rw ←associator_inv_naturality, simp, } }, by tidy⟩⟩, ..tensoring_right C }. variable {C} variables {M : Type*} [category M] [monoidal_category M] (F : monoidal_functor M (C ⥤ C)) @[simp, reassoc] lemma μ_hom_inv_app (i j : M) (X : C) : (F.μ i j).app X ≫ (F.μ_iso i j).inv.app X = 𝟙 _ := (F.μ_iso i j).hom_inv_id_app X @[simp, reassoc] lemma μ_inv_hom_app (i j : M) (X : C) : (F.μ_iso i j).inv.app X ≫ (F.μ i j).app X = 𝟙 _ := (F.μ_iso i j).inv_hom_id_app X @[simp, reassoc] lemma ε_hom_inv_app (X : C) : F.ε.app X ≫ F.ε_iso.inv.app X = 𝟙 _ := F.ε_iso.hom_inv_id_app X @[simp, reassoc] lemma ε_inv_hom_app (X : C) : F.ε_iso.inv.app X ≫ F.ε.app X = 𝟙 _ := F.ε_iso.inv_hom_id_app X @[simp, reassoc] lemma ε_naturality {X Y : C} (f : X ⟶ Y) : F.ε.app X ≫ (F.obj (𝟙_M)).map f = f ≫ F.ε.app Y := (F.ε.naturality f).symm @[simp, reassoc] lemma ε_inv_naturality {X Y : C} (f : X ⟶ Y) : (F.obj (𝟙_M)).map f ≫ F.ε_iso.inv.app Y = F.ε_iso.inv.app X ≫ f := F.ε_iso.inv.naturality f @[simp, reassoc] lemma μ_naturality {m n : M} {X Y : C} (f : X ⟶ Y) : (F.obj n).map ((F.obj m).map f) ≫ (F.μ m n).app Y = (F.μ m n).app X ≫ (F.obj _).map f := (F.to_lax_monoidal_functor.μ m n).naturality f -- This is a simp lemma in the reverse direction via `nat_trans.naturality`. @[reassoc] lemma μ_inv_naturality {m n : M} {X Y : C} (f : X ⟶ Y) : (F.μ_iso m n).inv.app X ≫ (F.obj n).map ((F.obj m).map f) = (F.obj _).map f ≫ (F.μ_iso m n).inv.app Y := ((F.μ_iso m n).inv.naturality f).symm -- This is not a simp lemma since it could be proved by the lemmas later. @[reassoc] lemma μ_naturality₂ {m n m' n' : M} (f : m ⟶ m') (g : n ⟶ n') (X : C) : (F.map g).app ((F.obj m).obj X) ≫ (F.obj n').map ((F.map f).app X) ≫ (F.μ m' n').app X = (F.μ m n).app X ≫ (F.map (f ⊗ g)).app X := begin have := congr_app (F.to_lax_monoidal_functor.μ_natural f g) X, dsimp at this, simpa using this, end @[simp, reassoc] lemma μ_naturalityₗ {m n m' : M} (f : m ⟶ m') (X : C) : (F.obj n).map ((F.map f).app X) ≫ (F.μ m' n).app X = (F.μ m n).app X ≫ (F.map (f ⊗ 𝟙 n)).app X := begin rw ← μ_naturality₂ F f (𝟙 n) X, simp, end @[simp, reassoc] lemma μ_naturalityᵣ {m n n' : M} (g : n ⟶ n') (X : C) : (F.map g).app ((F.obj m).obj X) ≫ (F.μ m n').app X = (F.μ m n).app X ≫ (F.map (𝟙 m ⊗ g)).app X := begin rw ← μ_naturality₂ F (𝟙 m) g X, simp, end @[simp, reassoc] lemma μ_inv_naturalityₗ {m n m' : M} (f : m ⟶ m') (X : C) : (F.μ_iso m n).inv.app X ≫ (F.obj n).map ((F.map f).app X) = (F.map (f ⊗ 𝟙 n)).app X ≫ (F.μ_iso m' n).inv.app X := begin rw [← is_iso.comp_inv_eq, category.assoc, ← is_iso.eq_inv_comp], simp, end @[simp, reassoc] lemma μ_inv_naturalityᵣ {m n n' : M} (g : n ⟶ n') (X : C) : (F.μ_iso m n).inv.app X ≫ (F.map g).app ((F.obj m).obj X) = (F.map (𝟙 m ⊗ g)).app X ≫ (F.μ_iso m n').inv.app X := begin rw [← is_iso.comp_inv_eq, category.assoc, ← is_iso.eq_inv_comp], simp, end @[reassoc] lemma left_unitality_app (n : M) (X : C) : (F.obj n).map (F.ε.app X) ≫ (F.μ (𝟙_M) n).app X ≫ (F.map (λ_ n).hom).app X = 𝟙 _ := begin have := congr_app (F.to_lax_monoidal_functor.left_unitality n) X, dsimp at this, simpa using this.symm, end @[reassoc, simp] lemma obj_ε_app (n : M) (X : C) : (F.obj n).map (F.ε.app X) = (F.map (λ_ n).inv).app X ≫ (F.μ_iso (𝟙_M) n).inv.app X := begin refine eq.trans _ (category.id_comp _), rw [← category.assoc, ← is_iso.comp_inv_eq, ← is_iso.comp_inv_eq, category.assoc], convert left_unitality_app F n X, { simp }, { ext, simpa } end @[reassoc, simp] lemma obj_ε_inv_app (n : M) (X : C) : (F.obj n).map (F.ε_iso.inv.app X) = (F.μ (𝟙_M) n).app X ≫ (F.map (λ_ n).hom).app X := begin rw [← cancel_mono ((F.obj n).map (F.ε.app X)), ← functor.map_comp], simpa, end @[reassoc] lemma right_unitality_app (n : M) (X : C) : F.ε.app ((F.obj n).obj X) ≫ (F.μ n (𝟙_M)).app X ≫ (F.map (ρ_ n).hom).app X = 𝟙 _ := begin have := congr_app (F.to_lax_monoidal_functor.right_unitality n) X, dsimp at this, simpa using this.symm, end @[simp] lemma ε_app_obj (n : M) (X : C) : F.ε.app ((F.obj n).obj X) = (F.map (ρ_ n).inv).app X ≫ (F.μ_iso n (𝟙_M)).inv.app X := begin refine eq.trans _ (category.id_comp _), rw [← category.assoc, ← is_iso.comp_inv_eq, ← is_iso.comp_inv_eq, category.assoc], convert right_unitality_app F n X, { simp }, { ext, simpa } end @[simp] lemma ε_inv_app_obj (n : M) (X : C) : F.ε_iso.inv.app ((F.obj n).obj X) = (F.μ n (𝟙_M)).app X ≫ (F.map (ρ_ n).hom).app X := begin rw [← cancel_mono (F.ε.app ((F.obj n).obj X)), ε_inv_hom_app], simpa end @[reassoc] lemma associativity_app (m₁ m₂ m₃: M) (X : C) : (F.obj m₃).map ((F.μ m₁ m₂).app X) ≫ (F.μ (m₁ ⊗ m₂) m₃).app X ≫ (F.map (α_ m₁ m₂ m₃).hom).app X = (F.μ m₂ m₃).app ((F.obj m₁).obj X) ≫ (F.μ m₁ (m₂ ⊗ m₃)).app X := begin have := congr_app (F.to_lax_monoidal_functor.associativity m₁ m₂ m₃) X, dsimp at this, simpa using this, end @[reassoc, simp] lemma obj_μ_app (m₁ m₂ m₃ : M) (X : C) : (F.obj m₃).map ((F.μ m₁ m₂).app X) = (F.μ m₂ m₃).app ((F.obj m₁).obj X) ≫ (F.μ m₁ (m₂ ⊗ m₃)).app X ≫ (F.map (α_ m₁ m₂ m₃).inv).app X ≫ (F.μ_iso (m₁ ⊗ m₂) m₃).inv.app X := begin rw [← associativity_app_assoc], dsimp, simp, dsimp, simp, end @[reassoc, simp] lemma obj_μ_inv_app (m₁ m₂ m₃ : M) (X : C) : (F.obj m₃).map ((F.μ_iso m₁ m₂).inv.app X) = (F.μ (m₁ ⊗ m₂) m₃).app X ≫ (F.map (α_ m₁ m₂ m₃).hom).app X ≫ (F.μ_iso m₁ (m₂ ⊗ m₃)).inv.app X ≫ (F.μ_iso m₂ m₃).inv.app ((F.obj m₁).obj X) := begin rw ← is_iso.inv_eq_inv, convert obj_μ_app F m₁ m₂ m₃ X using 1, { ext, rw ← functor.map_comp, simp }, { simp only [monoidal_functor.μ_iso_hom, category.assoc, nat_iso.inv_inv_app, is_iso.inv_comp], congr, { ext, simp }, { ext, simpa } } end @[simp, reassoc] lemma obj_zero_map_μ_app {m : M} {X Y : C} (f : X ⟶ (F.obj m).obj Y) : (F.obj (𝟙_M)).map f ≫ (F.μ m (𝟙_M)).app _ = F.ε_iso.inv.app _ ≫ f ≫ (F.map (ρ_ m).inv).app _ := begin rw [← is_iso.inv_comp_eq, ← is_iso.comp_inv_eq], simp, end @[simp] lemma obj_μ_zero_app (m₁ m₂ : M) (X : C) : (F.obj m₂).map ((F.μ m₁ (𝟙_M)).app X) = (F.μ (𝟙_M) m₂).app ((F.obj m₁).obj X) ≫ (F.map (λ_ m₂).hom).app ((F.obj m₁).obj X) ≫ (F.obj m₂).map ((F.map (ρ_ m₁).inv).app X) := begin rw [← obj_ε_inv_app_assoc, ← functor.map_comp], congr, simp, end /-- If `m ⊗ n ≅ 𝟙_M`, then `F.obj m` is a left inverse of `F.obj n`. -/ @[simps] noncomputable def unit_of_tensor_iso_unit (m n : M) (h : m ⊗ n ≅ 𝟙_M) : F.obj m ⋙ F.obj n ≅ 𝟭 C := F.μ_iso m n ≪≫ F.to_functor.map_iso h ≪≫ F.ε_iso.symm /-- If `m ⊗ n ≅ 𝟙_M` and `n ⊗ m ≅ 𝟙_M` (subject to some commuting constraints), then `F.obj m` and `F.obj n` forms a self-equivalence of `C`. -/ @[simps] noncomputable def equiv_of_tensor_iso_unit (m n : M) (h₁ : m ⊗ n ≅ 𝟙_M) (h₂ : n ⊗ m ≅ 𝟙_M) (H : (h₁.hom ⊗ 𝟙 m) ≫ (λ_ m).hom = (α_ m n m).hom ≫ (𝟙 m ⊗ h₂.hom) ≫ (ρ_ m).hom) : C ≌ C := { functor := F.obj m, inverse := F.obj n, unit_iso := (unit_of_tensor_iso_unit F m n h₁).symm, counit_iso := unit_of_tensor_iso_unit F n m h₂, functor_unit_iso_comp' := begin intro X, dsimp, simp only [μ_naturalityᵣ_assoc, μ_naturalityₗ_assoc, ε_inv_app_obj, category.assoc, obj_μ_inv_app, functor.map_comp, μ_inv_hom_app_assoc, obj_ε_app, unit_of_tensor_iso_unit_inv_app], simp [← nat_trans.comp_app, ← F.to_functor.map_comp, ← H, - functor.map_comp] end } end category_theory
54b35e4a5e4cc24fe76e05d48fc95dad9c662603
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/group_theory/group_action/default_auto.lean
972fbd6ab6d96f03c3ff9e3c10ad673a921cce57
[]
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
162
lean
import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.group_theory.group_action.basic import Mathlib.PostPort namespace Mathlib end Mathlib
b7b7a23a7e35e12e8593e025d282c5c1d81f73df
4727251e0cd73359b15b664c3170e5d754078599
/src/measure_theory/measure/sub.lean
cb8d1b097292a7626dd224d7aef43f7b2b8a4b3d
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
5,703
lean
/- Copyright (c) 2022 Martin Zinkevich. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Martin Zinkevich -/ import measure_theory.measure.measure_space /-! # Subtraction of measures In this file we define `μ - ν` to be the least measure `τ` such that `μ ≤ τ + ν`. It is the equivalent of `(μ - ν) ⊔ 0` if `μ` and `ν` were signed measures. Compare with `ennreal.has_sub`. Specifically, note that if you have `α = {1,2}`, and `μ {1} = 2`, `μ {2} = 0`, and `ν {2} = 2`, `ν {1} = 0`, then `(μ - ν) {1, 2} = 2`. However, if `μ ≤ ν`, and `ν univ ≠ ∞`, then `(μ - ν) + ν = μ`. -/ open set namespace measure_theory namespace measure /-- The measure `μ - ν` is defined to be the least measure `τ` such that `μ ≤ τ + ν`. It is the equivalent of `(μ - ν) ⊔ 0` if `μ` and `ν` were signed measures. Compare with `ennreal.has_sub`. Specifically, note that if you have `α = {1,2}`, and `μ {1} = 2`, `μ {2} = 0`, and `ν {2} = 2`, `ν {1} = 0`, then `(μ - ν) {1, 2} = 2`. However, if `μ ≤ ν`, and `ν univ ≠ ∞`, then `(μ - ν) + ν = μ`. -/ noncomputable instance has_sub {α : Type*} [measurable_space α] : has_sub (measure α) := ⟨λ μ ν, Inf {τ | μ ≤ τ + ν} ⟩ variables {α : Type*} {m : measurable_space α} {μ ν : measure α} {s : set α} lemma sub_def : μ - ν = Inf {d | μ ≤ d + ν} := rfl lemma sub_le_of_le_add {d} (h : μ ≤ d + ν) : μ - ν ≤ d := Inf_le h lemma sub_eq_zero_of_le (h : μ ≤ ν) : μ - ν = 0 := nonpos_iff_eq_zero'.1 $ sub_le_of_le_add $ by rwa zero_add lemma sub_le : μ - ν ≤ μ := sub_le_of_le_add $ measure.le_add_right le_rfl @[simp] lemma sub_top : μ - ⊤ = 0 := sub_eq_zero_of_le le_top @[simp] lemma zero_sub : 0 - μ = 0 := sub_eq_zero_of_le μ.zero_le @[simp] lemma sub_self : μ - μ = 0 := sub_eq_zero_of_le le_rfl /-- This application lemma only works in special circumstances. Given knowledge of when `μ ≤ ν` and `ν ≤ μ`, a more general application lemma can be written. -/ lemma sub_apply [is_finite_measure ν] (h₁ : measurable_set s) (h₂ : ν ≤ μ) : (μ - ν) s = μ s - ν s := begin -- We begin by defining `measure_sub`, which will be equal to `(μ - ν)`. let measure_sub : measure α := @measure_theory.measure.of_measurable α _ (λ (t : set α) (h_t_measurable_set : measurable_set t), (μ t - ν t)) begin simp end begin intros g h_meas h_disj, simp only, rw ennreal.tsum_sub, repeat { rw ← measure_theory.measure_Union h_disj h_meas }, exacts [measure_theory.measure_ne_top _ _, λ i, h₂ _ (h_meas _)] end, -- Now, we demonstrate `μ - ν = measure_sub`, and apply it. begin have h_measure_sub_add : (ν + measure_sub = μ), { ext t h_t_measurable_set, simp only [pi.add_apply, coe_add], rw [measure_theory.measure.of_measurable_apply _ h_t_measurable_set, add_comm, tsub_add_cancel_of_le (h₂ t h_t_measurable_set)] }, have h_measure_sub_eq : (μ - ν) = measure_sub, { rw measure_theory.measure.sub_def, apply le_antisymm, { apply @Inf_le (measure α) measure.complete_semilattice_Inf, simp [le_refl, add_comm, h_measure_sub_add] }, apply @le_Inf (measure α) measure.complete_semilattice_Inf, intros d h_d, rw [← h_measure_sub_add, mem_set_of_eq, add_comm d] at h_d, apply measure.le_of_add_le_add_left h_d }, rw h_measure_sub_eq, apply measure.of_measurable_apply _ h₁, end end lemma sub_add_cancel_of_le [is_finite_measure ν] (h₁ : ν ≤ μ) : μ - ν + ν = μ := begin ext s h_s_meas, rw [add_apply, sub_apply h_s_meas h₁, tsub_add_cancel_of_le (h₁ s h_s_meas)], end lemma restrict_sub_eq_restrict_sub_restrict (h_meas_s : measurable_set s) : (μ - ν).restrict s = (μ.restrict s) - (ν.restrict s) := begin repeat {rw sub_def}, have h_nonempty : {d | μ ≤ d + ν}.nonempty, from ⟨μ, measure.le_add_right le_rfl⟩, rw restrict_Inf_eq_Inf_restrict h_nonempty h_meas_s, apply le_antisymm, { refine Inf_le_Inf_of_forall_exists_le _, intros ν' h_ν'_in, rw mem_set_of_eq at h_ν'_in, refine ⟨ν'.restrict s, _, restrict_le_self⟩, refine ⟨ν' + (⊤ : measure α).restrict sᶜ, _, _⟩, { rw [mem_set_of_eq, add_right_comm, measure.le_iff], intros t h_meas_t, repeat { rw ← measure_inter_add_diff t h_meas_s }, refine add_le_add _ _, { rw [add_apply, add_apply], apply le_add_right _, rw [← restrict_eq_self μ (inter_subset_right _ _), ← restrict_eq_self ν (inter_subset_right _ _)], apply h_ν'_in _ (h_meas_t.inter h_meas_s) }, { rw [add_apply, restrict_apply (h_meas_t.diff h_meas_s), diff_eq, inter_assoc, inter_self, ← add_apply], have h_mu_le_add_top : μ ≤ ν' + ν + ⊤, by simp only [add_top, le_top], exact measure.le_iff'.1 h_mu_le_add_top _ } }, { ext1 t h_meas_t, simp [restrict_apply h_meas_t, restrict_apply (h_meas_t.inter h_meas_s), inter_assoc] } }, { refine Inf_le_Inf_of_forall_exists_le _, refine ball_image_iff.2 (λ t h_t_in, ⟨t.restrict s, _, le_rfl⟩), rw [set.mem_set_of_eq, ← restrict_add], exact restrict_mono subset.rfl h_t_in } end lemma sub_apply_eq_zero_of_restrict_le_restrict (h_le : μ.restrict s ≤ ν.restrict s) (h_meas_s : measurable_set s) : (μ - ν) s = 0 := by rw [← restrict_apply_self, restrict_sub_eq_restrict_sub_restrict, sub_eq_zero_of_le]; simp * instance is_finite_measure_sub [is_finite_measure μ] : is_finite_measure (μ - ν) := is_finite_measure_of_le μ sub_le end measure end measure_theory
30f65041572e8c8debce95956e818e6f82dd6406
274748215b6d042f0d9c9a505f9551fa8e0c5f38
/src/affine_algebraic_set/regular_function.lean
20fa9a92818f8fa37e588a96f6928286bf39ba21
[ "Apache-2.0" ]
permissive
ImperialCollegeLondon/M4P33
878ecb515c77d20cc799ff1ebd78f1bf4fd65c12
1a179372db71ad6802d11eacbc1f02f327d55f8f
refs/heads/master
1,607,519,867,193
1,583,344,297,000
1,583,344,297,000
233,316,107
59
4
Apache-2.0
1,579,285,778,000
1,578,788,367,000
Lean
UTF-8
Lean
false
false
8,253
lean
/- Copyright (c) 2020 Kevin Buzzard Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Buzzard, and whoever else wants to join in. -/ -- need the definition of an affine alg set import affine_algebraic_set.basic -- need so I can state that the kernel of the evaluation map is 𝕀 import affine_algebraic_set.I /-! # Regular functions Let k be a field (or even an integral domain), and let V be an affine algebraic subset of 𝔸ⁿ (where n can even be infinite). A _regular function_ on V is a function V → k which is induced by some polynomial F ∈ k[X₁, X₂, …, Xₙ]. Note that F itself is not part of the data, and a regular function can be induced by more than one polynomial in general. -/ -- I think it might all work with commutative semirings but -- let's let k be a commutative ring. variables {k : Type*} [comm_ring k] {n : Type*} local notation `𝔸ⁿ` := n → k local notation `k[n]` := mv_polynomial n k -- The idea: a mathematician shouldn't need to look at most of this file. -- The plan would be that they just read the API in the comments above. -- They just need to know the API. -- Throughout this file, let V ⊆ 𝔸ⁿ be an affine algebraic subset. variable {V : affine_algebraic_set k n} local notation `subset_of` := set open mv_polynomial def is_regular2 (f : (V : subset_of 𝔸ⁿ) → k) : Prop := ∃ F : k[n], ∀ (x : 𝔸ⁿ) (hx : x ∈ V), F.eval x = f ⟨x, hx⟩ def is_regular (f : (V : subset_of 𝔸ⁿ) → k) : Prop := ∃ F : k[n], ∀ (x : (V : subset_of 𝔸ⁿ)), F.eval x = f x /-- A "regular function" is a pair: a function V → k, and a proof that it comes from a polynomial -/ structure regular_fun (V : affine_algebraic_set k n) := (to_fun : {x : 𝔸ⁿ // x ∈ (V : subset_of 𝔸ⁿ)} → k) (is_regular' : is_regular to_fun) local notation `k[V]` := regular_fun V namespace regular_fun /-- A regular function on V can be regarded as a function from V to k -/ instance : has_coe_to_fun (k[V]) := ⟨_, regular_fun.to_fun⟩ variable {V} /-- A regular function is induced from a polynomial -/ lemma is_regular (f : k[V]): ∃ F : k[n], ∀ (x : (V : subset_of 𝔸ⁿ)), F.eval x = f x := f.is_regular' variable (V) def mk' (V : affine_algebraic_set k n) (F : k[n]) : regular_fun V := { to_fun := λ x, F.eval x, -- i.e. F(x) is_regular' := ⟨F, λ x, rfl⟩ } variable {V} def mk'.some_spec (F : k[n]) (x : (V : subset_of 𝔸ⁿ)) : (classical.some (mk' V F).is_regular).eval x = _ := classical.some_spec (mk' V F).is_regular x /- Two regular functions are equal if and only if their underlying functions V → k are equal -/ @[ext] lemma ext (f g : k[V]) : (f : (V : set 𝔸ⁿ) → k) = g → f = g := begin intro h, cases f, cases g, congr', end /-- The iff is also sometimes helpful-/ lemma ext_iff (f g : k[V]) : f = g ↔ (f : (V : set 𝔸ⁿ) → k) = g := ⟨λ h, h ▸ rfl, ext f g⟩ -- We prove the regular functions are naturally a ring. def zero : k[V] := { to_fun := λ x, 0, is_regular' := begin -- the function V → k sending everything to zero is a regular function use 0, intros, rw eval_zero, end } instance : has_zero (k[V]) := ⟨zero⟩ def one : k[V] := { to_fun := λ x, 1, is_regular' := begin -- the function V → k sending everything to zero is a regular function use 1, intros, rw eval_one, end } instance : has_one (k[V]) := ⟨one⟩ def add (f g : k[V]) : k[V] := { to_fun := λ x, f x + g x, is_regular' := begin -- sum of two regular functions is regular cases f.is_regular with F hF, cases g.is_regular with G hG, use F + G, intro x, rw eval_add, rw hF, rw hG, end } instance : has_add (k[V]) := ⟨add⟩ def neg (f : k[V]) : k[V] := { to_fun := λ x, -(f x), is_regular' := begin -- additive inverse of a regular function is regular cases f.is_regular with F hF, use -F, intro x, rw eval_neg, rw hF, end } instance : has_neg (k[V]) := ⟨neg⟩ def mul (f g : k[V]) : k[V] := { to_fun := λ x, (f x) * (g x), is_regular' := begin -- additive inverse of a regular function is regular cases f.is_regular with F hF, cases g.is_regular with G hG, use F * G, intro x, rw eval_mul, rw hF, rw hG, end } instance : has_mul (k[V]) := ⟨mul⟩ instance : comm_ring (k[V]) := { add := (+), add_assoc := begin intros f g h, ext, apply add_assoc, end, zero := 0, zero_add := begin intro f, ext, apply zero_add, end, add_zero := begin intro f, ext, apply add_zero, end, neg := has_neg.neg, add_left_neg := begin intro f, ext, apply add_left_neg, end, add_comm := begin intros f g, ext, apply add_comm, end, mul := (*), mul_assoc := begin intros f g h, ext, apply mul_assoc, end, one := 1, one_mul := begin intro f, ext, apply one_mul, end, mul_one := begin intro f, ext, apply mul_one, end, left_distrib := begin intros f g h, ext, apply left_distrib, end, right_distrib := begin intros f g h, ext, apply right_distrib, end, mul_comm := begin intros f g, ext, apply mul_comm, end } end regular_fun /-- The ring homomorphism from k[X₁, X₂, …, Xₙ] to k[V] -/ noncomputable def mv_polynomial.to_regular_fun : mv_polynomial n k →+* k[V] := { to_fun := λ F, { to_fun := λ x, F.eval x.1, is_regular' := ⟨F, λ x, rfl⟩ }, -- proof that it's a ring homomorphism map_one' := begin ext, apply eval_one, end, map_mul' := begin intros f g, ext, apply eval_mul, end, map_zero' := begin ext, unfold_coes, dsimp, apply eval_zero, end, map_add' := begin intros f g, ext, apply eval_add, end } namespace regular_fun instance : has_scalar k k[V] := { smul := λ t f, { to_fun := λ v, t * f v, is_regular' := begin cases f.is_regular with F hF, use (C t) * F, intro x, rw [eval_mul, eval_C, hF] end } } instance : is_ring_hom (λ t, mk' V (C t)) := { map_one := begin ext x, unfold_coes, unfold mk', dsimp, rw eval_one, refl, end, map_mul := begin intros s t, ext x, unfold_coes, unfold mk', dsimp, rw eval_C, simp [eval_C], refl, end, map_add := begin intros s t, ext x, unfold_coes, unfold mk', dsimp, simp [eval_C], refl end }. noncomputable instance : algebra k k[V] := { to_fun := (λ t, mk' V (C t)), hom := by apply_instance, commutes' := begin intros r x, apply mul_comm, end, smul_def' := begin intros r f, ext x, show _ = mk' V (C r) x * f x, unfold_coes, unfold mk', simp only [eval_C], refl, end } end regular_fun open mv_polynomial function lemma mv_polynomial.to_regular_fun.surjective : surjective ((to_regular_fun : mv_polynomial n k →+* k[V]) : mv_polynomial n k → k[V]) := begin intro f, cases f.is_regular with F hF, use F, ext x, rw ←hF x, refl, end open affine_algebraic_set lemma to_regular_fun.mem_kernel (F : mv_polynomial n k) : ((to_regular_fun : mv_polynomial n k →+* k[V]) : mv_polynomial n k → k[V]) F = 0 ↔ F ∈ 𝕀 V := begin rw mem_𝕀_iff, rw regular_fun.ext_iff, rw funext_iff, split, -- sigh { intros f x hx, exact f ⟨x, hx⟩}, { intros f x, exact f x.1 x.2} end -- let's prove it's a k-algebra hom noncomputable def mv_polynomial.to_regular_fun_algebra_map : k[n] →ₐ[k] k[V] := { to_fun := to_regular_fun.to_fun, map_one' := begin ext x, cases x with x hx, exact eval_one x end, map_mul' := begin intros f g, ext x, exact eval_mul, end, map_zero' := begin ext x, cases x with x hx, convert @eval_zero _ _ _ x, end, map_add' := begin intros f g, ext x, exact eval_add, end, commutes' := begin intro s, refl, end } /- TODO -- ask on Zulip why f is implicit and x explicit (note the trouble this caused me in map_zero') mv_polynomial.eval_one : ∀ {X : Type u_2} {R : Type u_1} [_inst_1 : comm_semiring R] (x : X → R), eval x 1 = 1 mv_polynomial.eval_zero : ∀ {α : Type ?} {n : Type ?} [_inst_1 : comm_semiring α] {f : n → α}, eval f 0 = 0 -/
2c5647e2e6e558416442571e9b58d09e5b1d7072
1fbca480c1574e809ae95a3eda58188ff42a5e41
/src/util/data/ordering.lean
5fd22c92d17aa3f237099f5e793439951aeb1cdb
[]
no_license
unitb/lean-lib
560eea0acf02b1fd4bcaac9986d3d7f1a4290e7e
439b80e606b4ebe4909a08b1d77f4f5c0ee3dee9
refs/heads/master
1,610,706,025,400
1,570,144,245,000
1,570,144,245,000
99,579,229
5
0
null
null
null
null
UTF-8
Lean
false
false
223
lean
import algebra.order universe u variables {α : Type u} variables [decidable_linear_order α] lemma cmp_eq_eq (a b : α) : cmp a b = ordering.eq = (a = b) := by { simp [cmp,cmp_using_eq_eq], rw ← le_antisymm_iff, cc }
476ddb90af1e89dc8db9567a27a7378cef5db0b6
8e2026ac8a0660b5a490dfb895599fb445bb77a0
/tests/lean/field_access.lean
fb3344ef3566e1b3e8bc61b5781a95c44aa3c59e
[ "Apache-2.0" ]
permissive
pcmoritz/lean
6a8575115a724af933678d829b4f791a0cb55beb
35eba0107e4cc8a52778259bb5392300267bfc29
refs/heads/master
1,607,896,326,092
1,490,752,175,000
1,490,752,175,000
86,612,290
0
0
null
1,490,809,641,000
1,490,809,641,000
null
UTF-8
Lean
false
false
536
lean
#check list.map variable l : list nat #check l^.1 -- Error l is not a structure #check (1, 2)^.5 -- Error insufficient fields example (l : list nat) : list nat := l^.forr (λ x, x + 1) -- Error there is no list.forr example (A : Type) (a : A) : A := a^.symm -- Error type of 'a' is not a constant application example (l : list nat) : list nat := l^.for (λ x, x + 1) example (l : list nat) : list nat := l^.for (λ x, x + 1) example (a b : nat) (h : a = b) : b = a := h^.symm example (a b : nat) (h : a = b) : b = a := h^.symm
5482aa408c4b5be9c96efbf0cf53000ccaea5cb0
3f7026ea8bef0825ca0339a275c03b911baef64d
/src/field_theory/finite_card.lean
4400adfc6a31c9d63a1eaecc80a602938ad2a0ef
[ "Apache-2.0" ]
permissive
rspencer01/mathlib
b1e3afa5c121362ef0881012cc116513ab09f18c
c7d36292c6b9234dc40143c16288932ae38fdc12
refs/heads/master
1,595,010,346,708
1,567,511,503,000
1,567,511,503,000
206,071,681
0
0
Apache-2.0
1,567,513,643,000
1,567,513,643,000
null
UTF-8
Lean
false
false
1,119
lean
/- Copyright (c) 2019 Casper Putz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joey van Langen, Casper Putz -/ import algebra.char_p data.zmod.basic linear_algebra.basis universes u variables (α : Type u) [discrete_field α] [fintype α] namespace finite_field theorem card (p : ℕ) [char_p α p] : ∃ (n : ℕ+), nat.prime p ∧ fintype.card α = p^(n : ℕ) := have hp : nat.prime p, from char_p.char_is_prime α p, have V : vector_space (zmodp p hp) α, from {..zmod.to_module'}, let ⟨n, h⟩ := @vector_space.card_fintype _ _ _ _ _ _ V _ _ in have hn : n > 0, from or.resolve_left (nat.eq_zero_or_pos n) (assume h0 : n = 0, have fintype.card α = 1, by rw[←nat.pow_zero (fintype.card _), ←h0]; exact h, have (1 : α) = 0, from (fintype.card_le_one_iff.mp (le_of_eq this)) 1 0, absurd this one_ne_zero), ⟨⟨n, hn⟩, hp, fintype.card_fin p ▸ h⟩ theorem card' : ∃ (p : ℕ) (n : ℕ+), nat.prime p ∧ fintype.card α = p^(n : ℕ) := let ⟨p, hc⟩ := char_p.exists α in ⟨p, @finite_field.card α _ _ p hc⟩ end finite_field
6ff7dd66fe7f919a3d393f5845f6797c1a1ca0df
1a61aba1b67cddccce19532a9596efe44be4285f
/hott/algebra/binary.hlean
a58114540a849673e2d406225266e353ffab7c97
[ "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
4,789
hlean
/- Copyright (c) 2014-15 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad General properties of binary operations. -/ open eq.ops function equiv namespace binary section variable {A : Type} variables (op₁ : A → A → A) (inv : A → A) (one : A) local notation a * b := op₁ a b local notation a ⁻¹ := inv a local notation 1 := one definition commutative [reducible] := ∀a b, a * b = b * a definition associative [reducible] := ∀a b c, (a * b) * c = a * (b * c) definition left_identity [reducible] := ∀a, 1 * a = a definition right_identity [reducible] := ∀a, a * 1 = a definition left_inverse [reducible] := ∀a, a⁻¹ * a = 1 definition right_inverse [reducible] := ∀a, a * a⁻¹ = 1 definition left_cancelative [reducible] := ∀a b c, a * b = a * c → b = c definition right_cancelative [reducible] := ∀a b c, a * b = c * b → a = c definition inv_op_cancel_left [reducible] := ∀a b, a⁻¹ * (a * b) = b definition op_inv_cancel_left [reducible] := ∀a b, a * (a⁻¹ * b) = b definition inv_op_cancel_right [reducible] := ∀a b, a * b⁻¹ * b = a definition op_inv_cancel_right [reducible] := ∀a b, a * b * b⁻¹ = a variable (op₂ : A → A → A) local notation a + b := op₂ a b definition left_distributive [reducible] := ∀a b c, a * (b + c) = a * b + a * c definition right_distributive [reducible] := ∀a b c, (a + b) * c = a * c + b * c definition right_commutative [reducible] {B : Type} (f : B → A → B) := ∀ b a₁ a₂, f (f b a₁) a₂ = f (f b a₂) a₁ definition left_commutative [reducible] {B : Type} (f : A → B → B) := ∀ a₁ a₂ b, f a₁ (f a₂ b) = f a₂ (f a₁ b) end section variable {A : Type} variable {f : A → A → A} variable H_comm : commutative f variable H_assoc : associative f local infixl `*` := f theorem left_comm : left_commutative f := take a b c, calc a*(b*c) = (a*b)*c : H_assoc ... = (b*a)*c : H_comm ... = b*(a*c) : H_assoc theorem right_comm : right_commutative f := take a b c, calc (a*b)*c = a*(b*c) : H_assoc ... = a*(c*b) : H_comm ... = (a*c)*b : H_assoc theorem comm4 (a b c d : A) : a*b*(c*d) = a*c*(b*d) := calc a*b*(c*d) = a*b*c*d : H_assoc ... = a*c*b*d : right_comm H_comm H_assoc ... = a*c*(b*d) : H_assoc end section variable {A : Type} variable {f : A → A → A} variable H_assoc : associative f local infixl `*` := f theorem assoc4helper (a b c d) : (a*b)*(c*d) = a*((b*c)*d) := calc (a*b)*(c*d) = a*(b*(c*d)) : H_assoc ... = a*((b*c)*d) : H_assoc end definition right_commutative_compose_right [reducible] {A B : Type} (f : A → A → A) (g : B → A) (rcomm : right_commutative f) : right_commutative (compose_right f g) := λ a b₁ b₂, !rcomm definition left_commutative_compose_left [reducible] {A B : Type} (f : A → A → A) (g : B → A) (lcomm : left_commutative f) : left_commutative (compose_left f g) := λ a b₁ b₂, !lcomm end binary open eq namespace is_equiv definition inv_preserve_binary {A B : Type} (f : A → B) [H : is_equiv f] (mA : A → A → A) (mB : B → B → B) (H : Π(a a' : A), mB (f a) (f a') = f (mA a a')) (b b' : B) : f⁻¹ (mB b b') = mA (f⁻¹ b) (f⁻¹ b') := begin have H2 : f⁻¹ (mB (f (f⁻¹ b)) (f (f⁻¹ b'))) = f⁻¹ (f (mA (f⁻¹ b) (f⁻¹ b'))), from ap f⁻¹ !H, rewrite [+right_inv f at H2,left_inv f at H2,▸* at H2,H2] end definition preserve_binary_of_inv_preserve {A B : Type} (f : A → B) [H : is_equiv f] (mA : A → A → A) (mB : B → B → B) (H : Π(b b' : B), mA (f⁻¹ b) (f⁻¹ b') = f⁻¹ (mB b b')) (a a' : A) : f (mA a a') = mB (f a) (f a') := begin have H2 : f (mA (f⁻¹ (f a)) (f⁻¹ (f a'))) = f (f⁻¹ (mB (f a) (f a'))), from ap f !H, rewrite [right_inv f at H2,+left_inv f at H2,▸* at H2,H2] end end is_equiv namespace equiv open is_equiv equiv.ops definition inv_preserve_binary {A B : Type} (f : A ≃ B) (mA : A → A → A) (mB : B → B → B) (H : Π(a a' : A), mB (f a) (f a') = f (mA a a')) (b b' : B) : f⁻¹ (mB b b') = mA (f⁻¹ b) (f⁻¹ b') := inv_preserve_binary f mA mB H b b' definition preserve_binary_of_inv_preserve {A B : Type} (f : A ≃ B) (mA : A → A → A) (mB : B → B → B) (H : Π(b b' : B), mA (f⁻¹ b) (f⁻¹ b') = f⁻¹ (mB b b')) (a a' : A) : f (mA a a') = mB (f a) (f a') := preserve_binary_of_inv_preserve f mA mB H a a' end equiv
c190f6dc1670078463099ea7c48cdf6fd16da082
4727251e0cd73359b15b664c3170e5d754078599
/src/testing/slim_check/functions.lean
f7948278aae72c314e7691223b26063230893f61
[ "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
19,549
lean
/- Copyright (c) 2020 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import data.list.sigma import data.int.range import data.finsupp.basic import data.finsupp.to_dfinsupp import tactic.pretty_cases import testing.slim_check.sampleable import testing.slim_check.testable /-! ## `slim_check`: generators for functions This file defines `sampleable` instances for `α → β` functions and `ℤ → ℤ` injective functions. Functions are generated by creating a list of pairs and one more value using the list as a lookup table and resorting to the additional value when a value is not found in the table. Injective functions are generated by creating a list of numbers and a permutation of that list. The permutation insures that every input is mapped to a unique output. When an input is not found in the list the input itself is used as an output. Injective functions `f : α → α` could be generated easily instead of `ℤ → ℤ` by generating a `list α`, removing duplicates and creating a permutations. One has to be careful when generating the domain to make if vast enough that, when generating arguments to apply `f` to, they argument should be likely to lie in the domain of `f`. This is the reason that injective functions `f : ℤ → ℤ` are generated by fixing the domain to the range `[-2*size .. -2*size]`, with `size` the size parameter of the `gen` monad. Much of the machinery provided in this file is applicable to generate injective functions of type `α → α` and new instances should be easy to define. Other classes of functions such as monotone functions can generated using similar techniques. For monotone functions, generating two lists, sorting them and matching them should suffice, with appropriate default values. Some care must be taken for shrinking such functions to make sure their defining property is invariant through shrinking. Injective functions are an example of how complicated it can get. -/ universes u v w variables {α : Type u} {β : Type v} {γ : Sort w} namespace slim_check /-- Data structure specifying a total function using a list of pairs and a default value returned when the input is not in the domain of the partial function. `with_default f y` encodes `x ↦ f x` when `x ∈ f` and `x ↦ y` otherwise. We use `Σ` to encode mappings instead of `×` because we rely on the association list API defined in `data.list.sigma`. -/ inductive total_function (α : Type u) (β : Type v) : Type (max u v) | with_default : list (Σ _ : α, β) → β → total_function instance total_function.inhabited [inhabited β] : inhabited (total_function α β) := ⟨ total_function.with_default ∅ default ⟩ namespace total_function /-- Apply a total function to an argument. -/ def apply [decidable_eq α] : total_function α β → α → β | (total_function.with_default m y) x := (m.lookup x).get_or_else y /-- Implementation of `has_repr (total_function α β)`. Creates a string for a given `finmap` and output, `x₀ ↦ y₀, .. xₙ ↦ yₙ` for each of the entries. The brackets are provided by the calling function. -/ def repr_aux [has_repr α] [has_repr β] (m : list (Σ _ : α, β)) : string := string.join $ list.qsort (λ x y, x < y) (m.map $ λ x, sformat!"{repr $ sigma.fst x} ↦ {repr $ sigma.snd x}, ") /-- Produce a string for a given `total_function`. The output is of the form `[x₀ ↦ f x₀, .. xₙ ↦ f xₙ, _ ↦ y]`. -/ protected def repr [has_repr α] [has_repr β] : total_function α β → string | (total_function.with_default m y) := sformat!"[{repr_aux m}_ ↦ {has_repr.repr y}]" instance (α : Type u) (β : Type v) [has_repr α] [has_repr β] : has_repr (total_function α β) := ⟨ total_function.repr ⟩ /-- Create a `finmap` from a list of pairs. -/ def list.to_finmap' (xs : list (α × β)) : list (Σ _ : α, β) := xs.map prod.to_sigma section variables [sampleable α] [sampleable β] /-- Redefine `sizeof` to follow the structure of `sampleable` instances. -/ def total.sizeof : total_function α β → ℕ | ⟨m, x⟩ := 1 + @sizeof _ sampleable.wf m + sizeof x @[priority 2000] instance : has_sizeof (total_function α β) := ⟨ total.sizeof ⟩ variables [decidable_eq α] /-- Shrink a total function by shrinking the lists that represent it. -/ protected def shrink : shrink_fn (total_function α β) | ⟨m, x⟩ := (sampleable.shrink (m, x)).map $ λ ⟨⟨m', x'⟩, h⟩, ⟨⟨list.dedupkeys m', x'⟩, lt_of_le_of_lt (by unfold_wf; refine @list.sizeof_dedupkeys _ _ _ (@sampleable.wf _ _) _) h ⟩ variables [has_repr α] [has_repr β] instance pi.sampleable_ext : sampleable_ext (α → β) := { proxy_repr := total_function α β, interp := total_function.apply, sample := do { xs ← (sampleable.sample (list (α × β)) : gen ((list (α × β)))), ⟨x⟩ ← (uliftable.up $ sample β : gen (ulift.{max u v} β)), pure $ total_function.with_default (list.to_finmap' xs) x }, shrink := total_function.shrink } end section finsupp variables [has_zero β] /-- Map a total_function to one whose default value is zero so that it represents a finsupp. -/ @[simp] def zero_default : total_function α β → total_function α β | (with_default A y) := with_default A 0 variables [decidable_eq α] [decidable_eq β] /-- The support of a zero default `total_function`. -/ @[simp] def zero_default_supp : total_function α β → finset α | (with_default A y) := list.to_finset $ (A.dedupkeys.filter (λ ab, sigma.snd ab ≠ 0)).map sigma.fst /-- Create a finitely supported function from a total function by taking the default value to zero. -/ def apply_finsupp (tf : total_function α β) : α →₀ β := { support := zero_default_supp tf, to_fun := tf.zero_default.apply, mem_support_to_fun := begin intro a, rcases tf with ⟨A, y⟩, simp only [apply, zero_default_supp, list.mem_map, list.mem_filter, exists_and_distrib_right, list.mem_to_finset, exists_eq_right, sigma.exists, ne.def, zero_default], split, { rintro ⟨od, hval, hod⟩, have := list.mem_lookup (list.nodupkeys_dedupkeys A) hval, rw (_ : list.lookup a A = od), { simpa, }, { simpa [list.lookup_dedupkeys, with_top.some_eq_coe], }, }, { intro h, use (A.lookup a).get_or_else (0 : β), rw ← list.lookup_dedupkeys at h ⊢, simp only [h, ←list.mem_lookup_iff A.nodupkeys_dedupkeys, and_true, not_false_iff, option.mem_def], cases list.lookup a A.dedupkeys, { simpa using h, }, { simp, }, } end } variables [sampleable α] [sampleable β] instance finsupp.sampleable_ext [has_repr α] [has_repr β] : sampleable_ext (α →₀ β) := { proxy_repr := total_function α β, interp := total_function.apply_finsupp, sample := (do xs ← (sampleable.sample (list (α × β)) : gen (list (α × β))), ⟨x⟩ ← (uliftable.up $ sample β : gen (ulift.{max u v} β)), pure $ total_function.with_default (list.to_finmap' xs) x), shrink := total_function.shrink } -- TODO: support a non-constant codomain type instance dfinsupp.sampleable_ext [has_repr α] [has_repr β] : sampleable_ext (Π₀ a : α, β) := { proxy_repr := total_function α β, interp := finsupp.to_dfinsupp ∘ total_function.apply_finsupp, sample := (do xs ← (sampleable.sample (list (α × β)) : gen (list (α × β))), ⟨x⟩ ← (uliftable.up $ sample β : gen (ulift.{max u v} β)), pure $ total_function.with_default (list.to_finmap' xs) x), shrink := total_function.shrink } end finsupp section sampleable_ext open sampleable_ext @[priority 2000] instance pi_pred.sampleable_ext [sampleable_ext (α → bool)] : sampleable_ext.{u+1} (α → Prop) := { proxy_repr := proxy_repr (α → bool), interp := λ m x, interp (α → bool) m x, sample := sample (α → bool), shrink := shrink } @[priority 2000] instance pi_uncurry.sampleable_ext [sampleable_ext (α × β → γ)] : sampleable_ext.{(imax (u+1) (v+1) w)} (α → β → γ) := { proxy_repr := proxy_repr (α × β → γ), interp := λ m x y, interp (α × β → γ) m (x, y), sample := sample (α × β → γ), shrink := shrink } end sampleable_ext end total_function /-- Data structure specifying a total function using a list of pairs and a default value returned when the input is not in the domain of the partial function. `map_to_self f` encodes `x ↦ f x` when `x ∈ f` and `x ↦ x`, i.e. `x` to itself, otherwise. We use `Σ` to encode mappings instead of `×` because we rely on the association list API defined in `data.list.sigma`. -/ inductive injective_function (α : Type u) : Type u | map_to_self (xs : list (Σ _ : α, α)) : xs.map sigma.fst ~ xs.map sigma.snd → list.nodup (xs.map sigma.snd) → injective_function instance : inhabited (injective_function α) := ⟨ ⟨ [], list.perm.nil, list.nodup_nil ⟩ ⟩ namespace injective_function /-- Apply a total function to an argument. -/ def apply [decidable_eq α] : injective_function α → α → α | (injective_function.map_to_self m _ _) x := (m.lookup x).get_or_else x /-- Produce a string for a given `total_function`. The output is of the form `[x₀ ↦ f x₀, .. xₙ ↦ f xₙ, x ↦ x]`. Unlike for `total_function`, the default value is not a constant but the identity function. -/ protected def repr [has_repr α] : injective_function α → string | (injective_function.map_to_self m _ _) := sformat!"[{total_function.repr_aux m}x ↦ x]" instance (α : Type u) [has_repr α] : has_repr (injective_function α) := ⟨ injective_function.repr ⟩ /-- Interpret a list of pairs as a total function, defaulting to the identity function when no entries are found for a given function -/ def list.apply_id [decidable_eq α] (xs : list (α × α)) (x : α) : α := ((xs.map prod.to_sigma).lookup x).get_or_else x @[simp] lemma list.apply_id_cons [decidable_eq α] (xs : list (α × α)) (x y z : α) : list.apply_id ((y, z) :: xs) x = if y = x then z else list.apply_id xs x := by simp only [list.apply_id, list.lookup, eq_rec_constant, prod.to_sigma, list.map]; split_ifs; refl open function _root_.list _root_.prod (to_sigma) open _root_.nat lemma list.apply_id_zip_eq [decidable_eq α] {xs ys : list α} (h₀ : list.nodup xs) (h₁ : xs.length = ys.length) (x y : α) (i : ℕ) (h₂ : xs.nth i = some x) : list.apply_id.{u} (xs.zip ys) x = y ↔ ys.nth i = some y := begin induction xs generalizing ys i, case list.nil : ys i h₁ h₂ { cases h₂ }, case list.cons : x' xs xs_ih ys i h₁ h₂ { cases i, { injection h₂ with h₀ h₁, subst h₀, cases ys, { cases h₁ }, { simp only [list.apply_id, to_sigma, option.get_or_else_some, nth, lookup_cons_eq, zip_cons_cons, list.map], } }, { cases ys, { cases h₁ }, { cases h₀ with _ _ h₀ h₁, simp only [nth, zip_cons_cons, list.apply_id_cons] at h₂ ⊢, rw if_neg, { apply xs_ih; solve_by_elim [succ.inj] }, { apply h₀, apply nth_mem h₂ } } } } end lemma apply_id_mem_iff [decidable_eq α] {xs ys : list α} (h₀ : list.nodup xs) (h₁ : xs ~ ys) (x : α) : list.apply_id.{u} (xs.zip ys) x ∈ ys ↔ x ∈ xs := begin simp only [list.apply_id], cases h₃ : (lookup x (map prod.to_sigma (xs.zip ys))), { dsimp [option.get_or_else], rw h₁.mem_iff }, { have h₂ : ys.nodup := h₁.nodup_iff.1 h₀, replace h₁ : xs.length = ys.length := h₁.length_eq, dsimp, induction xs generalizing ys, case list.nil : ys h₃ h₂ h₁ { contradiction }, case list.cons : x' xs xs_ih ys h₃ h₂ h₁ { cases ys with y ys, { cases h₃ }, dsimp [lookup] at h₃, split_ifs at h₃, { subst x', subst val, simp only [mem_cons_iff, true_or, eq_self_iff_true], }, { cases h₀ with _ _ h₀ h₅, cases h₂ with _ _ h₂ h₄, have h₆ := nat.succ.inj h₁, specialize @xs_ih h₅ ys h₃ h₄ h₆, simp only [ne.symm h, xs_ih, mem_cons_iff, false_or], suffices : val ∈ ys, tauto!, erw [← option.mem_def, mem_lookup_iff] at h₃, simp only [to_sigma, mem_map, heq_iff_eq, prod.exists] at h₃, rcases h₃ with ⟨a, b, h₃, h₄, h₅⟩, subst a, subst b, apply (mem_zip h₃).2, simp only [nodupkeys, keys, comp, prod.fst_to_sigma, map_map], rwa map_fst_zip _ _ (le_of_eq h₆) } } } end lemma list.apply_id_eq_self [decidable_eq α] {xs ys : list α} (x : α) : x ∉ xs → list.apply_id.{u} (xs.zip ys) x = x := begin intro h, dsimp [list.apply_id], rw lookup_eq_none.2, refl, simp only [keys, not_exists, to_sigma, exists_and_distrib_right, exists_eq_right, mem_map, comp_app, map_map, prod.exists], intros y hy, exact h (mem_zip hy).1, end lemma apply_id_injective [decidable_eq α] {xs ys : list α} (h₀ : list.nodup xs) (h₁ : xs ~ ys) : injective.{u+1 u+1} (list.apply_id (xs.zip ys)) := begin intros x y h, by_cases hx : x ∈ xs; by_cases hy : y ∈ xs, { rw mem_iff_nth at hx hy, cases hx with i hx, cases hy with j hy, suffices : some x = some y, { injection this }, have h₂ := h₁.length_eq, rw [list.apply_id_zip_eq h₀ h₂ _ _ _ hx] at h, rw [← hx, ← hy], congr, apply nth_injective _ (h₁.nodup_iff.1 h₀), { symmetry, rw h, rw ← list.apply_id_zip_eq; assumption }, { rw ← h₁.length_eq, rw nth_eq_some at hx, cases hx with hx hx', exact hx } }, { rw ← apply_id_mem_iff h₀ h₁ at hx hy, rw h at hx, contradiction, }, { rw ← apply_id_mem_iff h₀ h₁ at hx hy, rw h at hx, contradiction, }, { rwa [list.apply_id_eq_self, list.apply_id_eq_self] at h; assumption }, end open total_function (list.to_finmap') open sampleable /-- Remove a slice of length `m` at index `n` in a list and a permutation, maintaining the property that it is a permutation. -/ def perm.slice [decidable_eq α] (n m : ℕ) : (Σ' xs ys : list α, xs ~ ys ∧ ys.nodup) → (Σ' xs ys : list α, xs ~ ys ∧ ys.nodup) | ⟨xs, ys, h, h'⟩ := let xs' := list.slice n m xs in have h₀ : xs' ~ ys.inter xs', from perm.slice_inter _ _ h h', ⟨xs', ys.inter xs', h₀, h'.inter _⟩ /-- A lazy list, in decreasing order, of sizes that should be sliced off a list of length `n` -/ def slice_sizes : ℕ → lazy_list ℕ+ | n := if h : 0 < n then have n / 2 < n, from div_lt_self h dec_trivial, lazy_list.cons ⟨_, h⟩ (slice_sizes $ n / 2) else lazy_list.nil /-- Shrink a permutation of a list, slicing a segment in the middle. The sizes of the slice being removed start at `n` (with `n` the length of the list) and then `n / 2`, then `n / 4`, etc down to 1. The slices will be taken at index `0`, `n / k`, `2n / k`, `3n / k`, etc. -/ protected def shrink_perm {α : Type} [decidable_eq α] [has_sizeof α] : shrink_fn (Σ' xs ys : list α, xs ~ ys ∧ ys.nodup) | xs := do let k := xs.1.length, n ← slice_sizes k, i ← lazy_list.of_list $ list.fin_range $ k / n, have ↑i * ↑n < xs.1.length, from nat.lt_of_div_lt_div (lt_of_le_of_lt (by simp only [nat.mul_div_cancel, gt_iff_lt, fin.val_eq_coe, pnat.pos]) i.2), pure ⟨perm.slice (i*n) n xs, by rcases xs with ⟨a,b,c,d⟩; dsimp [sizeof_lt]; unfold_wf; simp only [perm.slice]; unfold_wf; apply list.sizeof_slice_lt _ _ n.2 _ this⟩ instance [has_sizeof α] : has_sizeof (injective_function α) := ⟨ λ ⟨xs,_,_⟩, sizeof (xs.map sigma.fst) ⟩ /-- Shrink an injective function slicing a segment in the middle of the domain and removing the corresponding elements in the codomain, hence maintaining the property that one is a permutation of the other. -/ protected def shrink {α : Type} [has_sizeof α] [decidable_eq α] : shrink_fn (injective_function α) | ⟨xs, h₀, h₁⟩ := do ⟨⟨xs', ys', h₀, h₁⟩, h₂⟩ ← injective_function.shrink_perm ⟨_, _, h₀, h₁⟩, have h₃ : xs'.length ≤ ys'.length, from le_of_eq (perm.length_eq h₀), have h₄ : ys'.length ≤ xs'.length, from le_of_eq (perm.length_eq h₀.symm), pure ⟨⟨(list.zip xs' ys').map prod.to_sigma, by simp only [comp, map_fst_zip, map_snd_zip, *, prod.fst_to_sigma, prod.snd_to_sigma, map_map], by simp only [comp, map_snd_zip, *, prod.snd_to_sigma, map_map] ⟩, by revert h₂; dsimp [sizeof_lt]; unfold_wf; simp only [has_sizeof._match_1, map_map, comp, map_fst_zip, *, prod.fst_to_sigma]; unfold_wf; intro h₂; convert h₂ ⟩ /-- Create an injective function from one list and a permutation of that list. -/ protected def mk (xs ys : list α) (h : xs ~ ys) (h' : ys.nodup) : injective_function α := have h₀ : xs.length ≤ ys.length, from le_of_eq h.length_eq, have h₁ : ys.length ≤ xs.length, from le_of_eq h.length_eq.symm, injective_function.map_to_self (list.to_finmap' (xs.zip ys)) (by { simp only [list.to_finmap', comp, map_fst_zip, map_snd_zip, *, prod.fst_to_sigma, prod.snd_to_sigma, map_map] }) (by { simp only [list.to_finmap', comp, map_snd_zip, *, prod.snd_to_sigma, map_map] }) protected lemma injective [decidable_eq α] (f : injective_function α) : injective (apply f) := begin cases f with xs hperm hnodup, generalize h₀ : map sigma.fst xs = xs₀, generalize h₁ : xs.map (@id ((Σ _ : α, α) → α) $ @sigma.snd α (λ _ : α, α)) = xs₁, dsimp [id] at h₁, have hxs : xs = total_function.list.to_finmap' (xs₀.zip xs₁), { rw [← h₀, ← h₁, list.to_finmap'], clear h₀ h₁ xs₀ xs₁ hperm hnodup, induction xs, case list.nil { simp only [zip_nil_right, map_nil] }, case list.cons : xs_hd xs_tl xs_ih { simp only [true_and, to_sigma, eq_self_iff_true, sigma.eta, zip_cons_cons, list.map], exact xs_ih }, }, revert hperm hnodup, rw hxs, intros, apply apply_id_injective, { rwa [← h₀, hxs, hperm.nodup_iff], }, { rwa [← hxs, h₀, h₁] at hperm, }, end instance pi_injective.sampleable_ext : sampleable_ext { f : ℤ → ℤ // function.injective f } := { proxy_repr := injective_function ℤ, interp := λ f, ⟨ apply f, f.injective ⟩, sample := gen.sized $ λ sz, do { let xs' := int.range (-(2*sz+2)) (2*sz + 2), ys ← gen.permutation_of xs', have Hinj : injective (λ (r : ℕ), -(2*sz + 2 : ℤ) + ↑r), from λ x y h, int.coe_nat_inj (add_right_injective _ h), let r : injective_function ℤ := injective_function.mk.{0} xs' ys.1 ys.2 (ys.2.nodup_iff.1 $ (nodup_range _).map Hinj) in pure r }, shrink := @injective_function.shrink ℤ _ _ } end injective_function open function instance injective.testable (f : α → β) [I : testable (named_binder "x" $ ∀ x : α, named_binder "y" $ ∀ y : α, named_binder "H" $ f x = f y → x = y)] : testable (injective f) := I instance monotone.testable [preorder α] [preorder β] (f : α → β) [I : testable (named_binder "x" $ ∀ x : α, named_binder "y" $ ∀ y : α, named_binder "H" $ x ≤ y → f x ≤ f y)] : testable (monotone f) := I instance antitone.testable [preorder α] [preorder β] (f : α → β) [I : testable (named_binder "x" $ ∀ x : α, named_binder "y" $ ∀ y : α, named_binder "H" $ x ≤ y → f y ≤ f x)] : testable (antitone f) := I end slim_check
5d971f64e61b22920ba8fedd1a6dec907764a29d
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/matchArrayLit.lean
c25fb8f02451af64428c64ba9f2c1895bfb52586
[ "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
3,097
lean
new_frontend universes u v theorem eqLitOfSize0 {α : Type u} (a : Array α) (hsz : a.size = 0) : a = #[] := a.toArrayLitEq 0 hsz theorem eqLitOfSize1 {α : Type u} (a : Array α) (hsz : a.size = 1) : a = #[a.getLit 0 hsz (ofDecideEqTrue rfl)] := a.toArrayLitEq 1 hsz theorem eqLitOfSize2 {α : Type u} (a : Array α) (hsz : a.size = 2) : a = #[a.getLit 0 hsz (ofDecideEqTrue rfl), a.getLit 1 hsz (ofDecideEqTrue rfl)] := a.toArrayLitEq 2 hsz theorem eqLitOfSize3 {α : Type u} (a : Array α) (hsz : a.size = 3) : a = #[a.getLit 0 hsz (ofDecideEqTrue rfl), a.getLit 1 hsz (ofDecideEqTrue rfl), a.getLit 2 hsz (ofDecideEqTrue rfl)] := a.toArrayLitEq 3 hsz /- Matcher for the following patterns ``` | #[] => _ | #[a₁] => _ | #[a₁, a₂, a₃] => _ | a => _ ``` -/ def matchArrayLit {α : Type u} (C : Array α → Sort v) (a : Array α) (h₁ : Unit → C #[]) (h₂ : ∀ a₁, C #[a₁]) (h₃ : ∀ a₁ a₂ a₃, C #[a₁, a₂, a₃]) (h₄ : ∀ a, C a) : C a := if h : a.size = 0 then @Eq.rec _ _ (fun x _ => C x) (h₁ ()) _ (a.toArrayLitEq 0 h).symm else if h : a.size = 1 then @Eq.rec _ _ (fun x _ => C x) (h₂ (a.getLit 0 h (ofDecideEqTrue rfl))) _ (a.toArrayLitEq 1 h).symm else if h : a.size = 3 then @Eq.rec _ _ (fun x _ => C x) (h₃ (a.getLit 0 h (ofDecideEqTrue rfl)) (a.getLit 1 h (ofDecideEqTrue rfl)) (a.getLit 2 h (ofDecideEqTrue rfl))) _ (a.toArrayLitEq 3 h).symm else h₄ a /- Equational lemmas that should be generated automatically. -/ theorem matchArrayLit.eq1 {α : Type u} (C : Array α → Sort v) (h₁ : Unit → C #[]) (h₂ : ∀ a₁, C #[a₁]) (h₃ : ∀ a₁ a₂ a₃, C #[a₁, a₂, a₃]) (h₄ : ∀ a, C a) : matchArrayLit C #[] h₁ h₂ h₃ h₄ = h₁ () := rfl theorem matchArrayLit.eq2 {α : Type u} (C : Array α → Sort v) (h₁ : Unit → C #[]) (h₂ : ∀ a₁, C #[a₁]) (h₃ : ∀ a₁ a₂ a₃, C #[a₁, a₂, a₃]) (h₄ : ∀ a, C a) (a₁ : α) : matchArrayLit C #[a₁] h₁ h₂ h₃ h₄ = h₂ a₁ := rfl theorem matchArrayLit.eq3 {α : Type u} (C : Array α → Sort v) (h₁ : Unit → C #[]) (h₂ : ∀ a₁, C #[a₁]) (h₃ : ∀ a₁ a₂ a₃, C #[a₁, a₂, a₃]) (h₄ : ∀ a, C a) (a₁ a₂ a₃ : α) : matchArrayLit C #[a₁, a₂, a₃] h₁ h₂ h₃ h₄ = h₃ a₁ a₂ a₃ := rfl theorem matchArrayLit.eq4 {α : Type u} (C : Array α → Sort v) (h₁ : Unit → C #[]) (h₂ : ∀ a₁, C #[a₁]) (h₃ : ∀ a₁ a₂ a₃, C #[a₁, a₂, a₃]) (h₄ : ∀ a, C a) (a : Array α) (n₁ : a.size ≠ 0) (n₂ : a.size ≠ 1) (n₃ : a.size ≠ 3) : matchArrayLit C a h₁ h₂ h₃ h₄ = h₄ a := match a, n₁, n₂, n₃ with | ⟨0, _⟩, n₁, _, _ => absurd rfl n₁ | ⟨1, _⟩, _, n₂, _ => absurd rfl n₂ | ⟨2, _⟩, _, _, _ => rfl | ⟨3, _⟩, _, _, n₃ => absurd rfl n₃ | ⟨n+4, _⟩, _, _, _ => rfl
ffca572d3f48dc34a49584081a7b66af4cd6b0f0
f20db13587f4dd28a4b1fbd31953afd491691fa0
/library/init/data/prod.lean
9f3088e46bada5546391f8fab18ca55cc61e4f9f
[ "Apache-2.0" ]
permissive
AHartNtkn/lean
9a971edfc6857c63edcbf96bea6841b9a84cf916
0d83a74b26541421fc1aa33044c35b03759710ed
refs/heads/master
1,620,592,591,236
1,516,749,881,000
1,516,749,881,000
118,697,288
1
0
null
1,516,759,470,000
1,516,759,470,000
null
UTF-8
Lean
false
false
1,566
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura, Jeremy Avigad -/ prelude import init.logic universes u v section variables {α : Type u} {β : Type v} instance [inhabited α] [inhabited β] : inhabited (prod α β) := ⟨(default α, default β)⟩ instance [h₁ : decidable_eq α] [h₂ : decidable_eq β] : decidable_eq (α × β) | (a, b) (a', b') := match (h₁ a a') with | (is_true e₁) := match (h₂ b b') with | (is_true e₂) := is_true (eq.rec_on e₁ (eq.rec_on e₂ rfl)) | (is_false n₂) := is_false (assume h, prod.no_confusion h (λ e₁' e₂', absurd e₂' n₂)) end | (is_false n₁) := is_false (assume h, prod.no_confusion h (λ e₁' e₂', absurd e₁' n₁)) end instance [has_lt α] [has_lt β] : has_lt (α × β) := ⟨λ s t, s.1 < t.1 ∨ (s.1 = t.1 ∧ s.2 < t.2)⟩ instance prod_has_decidable_lt [has_lt α] [has_lt β] [decidable_eq α] [decidable_eq β] [decidable_rel ((<) : α → α → Prop)] [decidable_rel ((<) : β → β → Prop)] : Π s t : α × β, decidable (s < t) := λ t s, or.decidable lemma prod.lt_def [has_lt α] [has_lt β] (s t : α × β) : (s < t) = (s.1 < t.1 ∨ (s.1 = t.1 ∧ s.2 < t.2)) := rfl end def {u₁ u₂ v₁ v₂} prod.map {α₁ : Type u₁} {α₂ : Type u₂} {β₁ : Type v₁} {β₂ : Type v₂} (f : α₁ → α₂) (g : β₁ → β₂) : α₁ × β₁ → α₂ × β₂ | (a, b) := (f a, g b)
afa1332b160a8d8f870a5877ac1a4d57bb0b9769
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/logic/function/basic.lean
68d3d51cd9c82558ee78e845765c55b70d4781e1
[ "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
26,984
lean
/- Copyright (c) 2016 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import logic.basic import data.option.defs /-! # Miscellaneous function constructions and lemmas -/ universes u v w namespace function section variables {α β γ : Sort*} {f : α → β} /-- Evaluate a function at an argument. Useful if you want to talk about the partially applied `function.eval x : (Π x, β x) → β x`. -/ @[reducible] def eval {β : α → Sort*} (x : α) (f : Π x, β x) : β x := f x @[simp] lemma eval_apply {β : α → Sort*} (x : α) (f : Π x, β x) : eval x f = f x := rfl lemma comp_apply {α : Sort u} {β : Sort v} {φ : Sort w} (f : β → φ) (g : α → β) (a : α) : (f ∘ g) a = f (g a) := rfl lemma const_def {y : β} : (λ x : α, y) = const α y := rfl @[simp] lemma const_apply {y : β} {x : α} : const α y x = y := rfl @[simp] lemma const_comp {f : α → β} {c : γ} : const β c ∘ f = const α c := rfl @[simp] lemma comp_const {f : β → γ} {b : β} : f ∘ const α b = const α (f b) := rfl lemma id_def : @id α = λ x, x := rfl lemma hfunext {α α': Sort u} {β : α → Sort v} {β' : α' → Sort v} {f : Πa, β a} {f' : Πa, β' a} (hα : α = α') (h : ∀a a', a == a' → f a == f' a') : f == f' := begin subst hα, have : ∀a, f a == f' a, { intro a, exact h a a (heq.refl a) }, have : β = β', { funext a, exact type_eq_of_heq (this a) }, subst this, apply heq_of_eq, funext a, exact eq_of_heq (this a) end lemma funext_iff {β : α → Sort*} {f₁ f₂ : Π (x : α), β x} : f₁ = f₂ ↔ (∀a, f₁ a = f₂ a) := iff.intro (assume h a, h ▸ rfl) funext protected lemma bijective.injective {f : α → β} (hf : bijective f) : injective f := hf.1 protected lemma bijective.surjective {f : α → β} (hf : bijective f) : surjective f := hf.2 theorem injective.eq_iff (I : injective f) {a b : α} : f a = f b ↔ a = b := ⟨@I _ _, congr_arg f⟩ theorem injective.eq_iff' (I : injective f) {a b : α} {c : β} (h : f b = c) : f a = c ↔ a = b := h ▸ I.eq_iff lemma injective.ne (hf : injective f) {a₁ a₂ : α} : a₁ ≠ a₂ → f a₁ ≠ f a₂ := mt (assume h, hf h) lemma injective.ne_iff (hf : injective f) {x y : α} : f x ≠ f y ↔ x ≠ y := ⟨mt $ congr_arg f, hf.ne⟩ lemma injective.ne_iff' (hf : injective f) {x y : α} {z : β} (h : f y = z) : f x ≠ z ↔ x ≠ y := h ▸ hf.ne_iff /-- If the co-domain `β` of an injective function `f : α → β` has decidable equality, then the domain `α` also has decidable equality. -/ def injective.decidable_eq [decidable_eq β] (I : injective f) : decidable_eq α := λ a b, decidable_of_iff _ I.eq_iff lemma injective.of_comp {g : γ → α} (I : injective (f ∘ g)) : injective g := λ x y h, I $ show f (g x) = f (g y), from congr_arg f h lemma injective.of_comp_iff {f : α → β} (hf : injective f) (g : γ → α) : injective (f ∘ g) ↔ injective g := ⟨injective.of_comp, hf.comp⟩ lemma injective.of_comp_iff' (f : α → β) {g : γ → α} (hg : bijective g) : injective (f ∘ g) ↔ injective f := ⟨ λ h x y, let ⟨x', hx⟩ := hg.surjective x, ⟨y', hy⟩ := hg.surjective y in hx ▸ hy ▸ λ hf, h hf ▸ rfl, λ h, h.comp hg.injective⟩ lemma injective.dite (p : α → Prop) [decidable_pred p] {f : {a : α // p a} → β} {f' : {a : α // ¬ p a} → β} (hf : injective f) (hf' : injective f') (im_disj : ∀ {x x' : α} {hx : p x} {hx' : ¬ p x'}, f ⟨x, hx⟩ ≠ f' ⟨x', hx'⟩) : function.injective (λ x, if h : p x then f ⟨x, h⟩ else f' ⟨x, h⟩) := λ x₁ x₂ h, begin dsimp only at h, by_cases h₁ : p x₁; by_cases h₂ : p x₂, { rw [dif_pos h₁, dif_pos h₂] at h, injection (hf h), }, { rw [dif_pos h₁, dif_neg h₂] at h, exact (im_disj h).elim, }, { rw [dif_neg h₁, dif_pos h₂] at h, exact (im_disj h.symm).elim, }, { rw [dif_neg h₁, dif_neg h₂] at h, injection (hf' h), }, end lemma surjective.of_comp {g : γ → α} (S : surjective (f ∘ g)) : surjective f := λ y, let ⟨x, h⟩ := S y in ⟨g x, h⟩ lemma surjective.of_comp_iff (f : α → β) {g : γ → α} (hg : surjective g) : surjective (f ∘ g) ↔ surjective f := ⟨surjective.of_comp, λ h, h.comp hg⟩ lemma surjective.of_comp_iff' {f : α → β} (hf : bijective f) (g : γ → α) : surjective (f ∘ g) ↔ surjective g := ⟨λ h x, let ⟨x', hx'⟩ := h (f x) in ⟨x', hf.injective hx'⟩, hf.surjective.comp⟩ instance decidable_eq_pfun (p : Prop) [decidable p] (α : p → Type*) [Π hp, decidable_eq (α hp)] : decidable_eq (Π hp, α hp) | f g := decidable_of_iff (∀ hp, f hp = g hp) funext_iff.symm theorem surjective.forall {f : α → β} (hf : surjective f) {p : β → Prop} : (∀ y, p y) ↔ ∀ x, p (f x) := ⟨λ h x, h (f x), λ h y, let ⟨x, hx⟩ := hf y in hx ▸ h x⟩ theorem surjective.forall₂ {f : α → β} (hf : surjective f) {p : β → β → Prop} : (∀ y₁ y₂, p y₁ y₂) ↔ ∀ x₁ x₂, p (f x₁) (f x₂) := hf.forall.trans $ forall_congr $ λ x, hf.forall theorem surjective.forall₃ {f : α → β} (hf : surjective f) {p : β → β → β → Prop} : (∀ y₁ y₂ y₃, p y₁ y₂ y₃) ↔ ∀ x₁ x₂ x₃, p (f x₁) (f x₂) (f x₃) := hf.forall.trans $ forall_congr $ λ x, hf.forall₂ theorem surjective.exists {f : α → β} (hf : surjective f) {p : β → Prop} : (∃ y, p y) ↔ ∃ x, p (f x) := ⟨λ ⟨y, hy⟩, let ⟨x, hx⟩ := hf y in ⟨x, hx.symm ▸ hy⟩, λ ⟨x, hx⟩, ⟨f x, hx⟩⟩ theorem surjective.exists₂ {f : α → β} (hf : surjective f) {p : β → β → Prop} : (∃ y₁ y₂, p y₁ y₂) ↔ ∃ x₁ x₂, p (f x₁) (f x₂) := hf.exists.trans $ exists_congr $ λ x, hf.exists theorem surjective.exists₃ {f : α → β} (hf : surjective f) {p : β → β → β → Prop} : (∃ y₁ y₂ y₃, p y₁ y₂ y₃) ↔ ∃ x₁ x₂ x₃, p (f x₁) (f x₂) (f x₃) := hf.exists.trans $ exists_congr $ λ x, hf.exists₂ lemma bijective_iff_exists_unique (f : α → β) : bijective f ↔ ∀ b : β, ∃! (a : α), f a = b := ⟨ λ hf b, let ⟨a, ha⟩ := hf.surjective b in ⟨a, ha, λ a' ha', hf.injective (ha'.trans ha.symm)⟩, λ he, ⟨ λ a a' h, unique_of_exists_unique (he (f a')) h rfl, λ b, exists_of_exists_unique (he b) ⟩⟩ /-- Shorthand for using projection notation with `function.bijective_iff_exists_unique`. -/ lemma bijective.exists_unique {f : α → β} (hf : bijective f) (b : β) : ∃! (a : α), f a = b := (bijective_iff_exists_unique f).mp hf b lemma bijective.of_comp_iff (f : α → β) {g : γ → α} (hg : bijective g) : bijective (f ∘ g) ↔ bijective f := and_congr (injective.of_comp_iff' _ hg) (surjective.of_comp_iff _ hg.surjective) lemma bijective.of_comp_iff' {f : α → β} (hf : bijective f) (g : γ → α) : function.bijective (f ∘ g) ↔ function.bijective g := and_congr (injective.of_comp_iff hf.injective _) (surjective.of_comp_iff' hf _) /-- Cantor's diagonal argument implies that there are no surjective functions from `α` to `set α`. -/ theorem cantor_surjective {α} (f : α → set α) : ¬ function.surjective f | h := let ⟨D, e⟩ := h (λ a, ¬ f a a) in (iff_not_self (f D D)).1 $ iff_of_eq (congr_fun e D) /-- Cantor's diagonal argument implies that there are no injective functions from `set α` to `α`. -/ theorem cantor_injective {α : Type*} (f : (set α) → α) : ¬ function.injective f | i := cantor_surjective (λ a b, ∀ U, a = f U → U b) $ right_inverse.surjective (λ U, funext $ λ a, propext ⟨λ h, h U rfl, λ h' U' e, i e ▸ h'⟩) /-- `g` is a partial inverse to `f` (an injective but not necessarily surjective function) if `g y = some x` implies `f x = y`, and `g y = none` implies that `y` is not in the range of `f`. -/ def is_partial_inv {α β} (f : α → β) (g : β → option α) : Prop := ∀ x y, g y = some x ↔ f x = y theorem is_partial_inv_left {α β} {f : α → β} {g} (H : is_partial_inv f g) (x) : g (f x) = some x := (H _ _).2 rfl theorem injective_of_partial_inv {α β} {f : α → β} {g} (H : is_partial_inv f g) : injective f := λ a b h, option.some.inj $ ((H _ _).2 h).symm.trans ((H _ _).2 rfl) theorem injective_of_partial_inv_right {α β} {f : α → β} {g} (H : is_partial_inv f g) (x y b) (h₁ : b ∈ g x) (h₂ : b ∈ g y) : x = y := ((H _ _).1 h₁).symm.trans ((H _ _).1 h₂) theorem left_inverse.comp_eq_id {f : α → β} {g : β → α} (h : left_inverse f g) : f ∘ g = id := funext h theorem left_inverse_iff_comp {f : α → β} {g : β → α} : left_inverse f g ↔ f ∘ g = id := ⟨left_inverse.comp_eq_id, congr_fun⟩ theorem right_inverse.comp_eq_id {f : α → β} {g : β → α} (h : right_inverse f g) : g ∘ f = id := funext h theorem right_inverse_iff_comp {f : α → β} {g : β → α} : right_inverse f g ↔ g ∘ f = id := ⟨right_inverse.comp_eq_id, congr_fun⟩ theorem left_inverse.comp {f : α → β} {g : β → α} {h : β → γ} {i : γ → β} (hf : left_inverse f g) (hh : left_inverse h i) : left_inverse (h ∘ f) (g ∘ i) := assume a, show h (f (g (i a))) = a, by rw [hf (i a), hh a] theorem right_inverse.comp {f : α → β} {g : β → α} {h : β → γ} {i : γ → β} (hf : right_inverse f g) (hh : right_inverse h i) : right_inverse (h ∘ f) (g ∘ i) := left_inverse.comp hh hf theorem left_inverse.right_inverse {f : α → β} {g : β → α} (h : left_inverse g f) : right_inverse f g := h theorem right_inverse.left_inverse {f : α → β} {g : β → α} (h : right_inverse g f) : left_inverse f g := h theorem left_inverse.surjective {f : α → β} {g : β → α} (h : left_inverse f g) : surjective f := h.right_inverse.surjective theorem right_inverse.injective {f : α → β} {g : β → α} (h : right_inverse f g) : injective f := h.left_inverse.injective theorem left_inverse.eq_right_inverse {f : α → β} {g₁ g₂ : β → α} (h₁ : left_inverse g₁ f) (h₂ : right_inverse g₂ f) : g₁ = g₂ := calc g₁ = g₁ ∘ f ∘ g₂ : by rw [h₂.comp_eq_id, comp.right_id] ... = g₂ : by rw [← comp.assoc, h₁.comp_eq_id, comp.left_id] local attribute [instance, priority 10] classical.prop_decidable /-- We can use choice to construct explicitly a partial inverse for a given injective function `f`. -/ noncomputable def partial_inv {α β} (f : α → β) (b : β) : option α := if h : ∃ a, f a = b then some (classical.some h) else none theorem partial_inv_of_injective {α β} {f : α → β} (I : injective f) : is_partial_inv f (partial_inv f) | a b := ⟨λ h, if h' : ∃ a, f a = b then begin rw [partial_inv, dif_pos h'] at h, injection h with h, subst h, apply classical.some_spec h' end else by rw [partial_inv, dif_neg h'] at h; contradiction, λ e, e ▸ have h : ∃ a', f a' = f a, from ⟨_, rfl⟩, (dif_pos h).trans (congr_arg _ (I $ classical.some_spec h))⟩ theorem partial_inv_left {α β} {f : α → β} (I : injective f) : ∀ x, partial_inv f (f x) = some x := is_partial_inv_left (partial_inv_of_injective I) end section inv_fun variables {α : Type u} [n : nonempty α] {β : Sort v} {f : α → β} {s : set α} {a : α} {b : β} include n local attribute [instance, priority 10] classical.prop_decidable /-- Construct the inverse for a function `f` on domain `s`. This function is a right inverse of `f` on `f '' s`. For a computable version, see `function.injective.inv_of_mem_range`. -/ noncomputable def inv_fun_on (f : α → β) (s : set α) (b : β) : α := if h : ∃a, a ∈ s ∧ f a = b then classical.some h else classical.choice n theorem inv_fun_on_pos (h : ∃a∈s, f a = b) : inv_fun_on f s b ∈ s ∧ f (inv_fun_on f s b) = b := by rw [bex_def] at h; rw [inv_fun_on, dif_pos h]; exact classical.some_spec h theorem inv_fun_on_mem (h : ∃a∈s, f a = b) : inv_fun_on f s b ∈ s := (inv_fun_on_pos h).left theorem inv_fun_on_eq (h : ∃a∈s, f a = b) : f (inv_fun_on f s b) = b := (inv_fun_on_pos h).right theorem inv_fun_on_eq' (h : ∀ (x ∈ s) (y ∈ s), f x = f y → x = y) (ha : a ∈ s) : inv_fun_on f s (f a) = a := have ∃a'∈s, f a' = f a, from ⟨a, ha, rfl⟩, h _ (inv_fun_on_mem this) _ ha (inv_fun_on_eq this) theorem inv_fun_on_neg (h : ¬ ∃a∈s, f a = b) : inv_fun_on f s b = classical.choice n := by rw [bex_def] at h; rw [inv_fun_on, dif_neg h] /-- The inverse of a function (which is a left inverse if `f` is injective and a right inverse if `f` is surjective). -/ noncomputable def inv_fun (f : α → β) : β → α := inv_fun_on f set.univ theorem inv_fun_eq (h : ∃a, f a = b) : f (inv_fun f b) = b := inv_fun_on_eq $ let ⟨a, ha⟩ := h in ⟨a, trivial, ha⟩ lemma inv_fun_neg (h : ¬ ∃ a, f a = b) : inv_fun f b = classical.choice n := by refine inv_fun_on_neg (mt _ h); exact assume ⟨a, _, ha⟩, ⟨a, ha⟩ theorem inv_fun_eq_of_injective_of_right_inverse {g : β → α} (hf : injective f) (hg : right_inverse g f) : inv_fun f = g := funext $ assume b, hf begin rw [hg b], exact inv_fun_eq ⟨g b, hg b⟩ end lemma right_inverse_inv_fun (hf : surjective f) : right_inverse (inv_fun f) f := assume b, inv_fun_eq $ hf b lemma left_inverse_inv_fun (hf : injective f) : left_inverse (inv_fun f) f := assume b, have f (inv_fun f (f b)) = f b, from inv_fun_eq ⟨b, rfl⟩, hf this lemma inv_fun_surjective (hf : injective f) : surjective (inv_fun f) := (left_inverse_inv_fun hf).surjective lemma inv_fun_comp (hf : injective f) : inv_fun f ∘ f = id := funext $ left_inverse_inv_fun hf end inv_fun section inv_fun variables {α : Type u} [i : nonempty α] {β : Sort v} {f : α → β} include i lemma injective.has_left_inverse (hf : injective f) : has_left_inverse f := ⟨inv_fun f, left_inverse_inv_fun hf⟩ lemma injective_iff_has_left_inverse : injective f ↔ has_left_inverse f := ⟨injective.has_left_inverse, has_left_inverse.injective⟩ end inv_fun section surj_inv variables {α : Sort u} {β : Sort v} {f : α → β} /-- The inverse of a surjective function. (Unlike `inv_fun`, this does not require `α` to be inhabited.) -/ noncomputable def surj_inv {f : α → β} (h : surjective f) (b : β) : α := classical.some (h b) lemma surj_inv_eq (h : surjective f) (b) : f (surj_inv h b) = b := classical.some_spec (h b) lemma right_inverse_surj_inv (hf : surjective f) : right_inverse (surj_inv hf) f := surj_inv_eq hf lemma left_inverse_surj_inv (hf : bijective f) : left_inverse (surj_inv hf.2) f := right_inverse_of_injective_of_left_inverse hf.1 (right_inverse_surj_inv hf.2) lemma surjective.has_right_inverse (hf : surjective f) : has_right_inverse f := ⟨_, right_inverse_surj_inv hf⟩ lemma surjective_iff_has_right_inverse : surjective f ↔ has_right_inverse f := ⟨surjective.has_right_inverse, has_right_inverse.surjective⟩ lemma bijective_iff_has_inverse : bijective f ↔ ∃ g, left_inverse g f ∧ right_inverse g f := ⟨λ hf, ⟨_, left_inverse_surj_inv hf, right_inverse_surj_inv hf.2⟩, λ ⟨g, gl, gr⟩, ⟨gl.injective, gr.surjective⟩⟩ lemma injective_surj_inv (h : surjective f) : injective (surj_inv h) := (right_inverse_surj_inv h).injective end surj_inv section update variables {α : Sort u} {β : α → Sort v} {α' : Sort w} [decidable_eq α] [decidable_eq α'] /-- Replacing the value of a function at a given point by a given value. -/ def update (f : Πa, β a) (a' : α) (v : β a') (a : α) : β a := if h : a = a' then eq.rec v h.symm else f a /-- On non-dependent functions, `function.update` can be expressed as an `ite` -/ lemma update_apply {β : Sort*} (f : α → β) (a' : α) (b : β) (a : α) : update f a' b a = if a = a' then b else f a := begin dunfold update, congr, funext, rw eq_rec_constant, end @[simp] lemma update_same (a : α) (v : β a) (f : Πa, β a) : update f a v a = v := dif_pos rfl lemma update_injective (f : Πa, β a) (a' : α) : injective (update f a') := λ v v' h, have _ := congr_fun h a', by rwa [update_same, update_same] at this @[simp] lemma update_noteq {a a' : α} (h : a ≠ a') (v : β a') (f : Πa, β a) : update f a' v a = f a := dif_neg h lemma forall_update_iff (f : Π a, β a) {a : α} {b : β a} (p : Π a, β a → Prop) : (∀ x, p x (update f a b x)) ↔ p a b ∧ ∀ x ≠ a, p x (f x) := calc (∀ x, p x (update f a b x)) ↔ ∀ x, (x = a ∨ x ≠ a) → p x (update f a b x) : by simp only [ne.def, classical.em, forall_prop_of_true] ... ↔ p a b ∧ ∀ x ≠ a, p x (f x) : by simp [or_imp_distrib, forall_and_distrib] { contextual := tt } lemma update_eq_iff {a : α} {b : β a} {f g : Π a, β a} : update f a b = g ↔ b = g a ∧ ∀ x ≠ a, f x = g x := funext_iff.trans $ forall_update_iff _ (λ x y, y = g x) lemma eq_update_iff {a : α} {b : β a} {f g : Π a, β a} : g = update f a b ↔ g a = b ∧ ∀ x ≠ a, g x = f x := funext_iff.trans $ forall_update_iff _ (λ x y, g x = y) @[simp] lemma update_eq_self (a : α) (f : Πa, β a) : update f a (f a) = f := update_eq_iff.2 ⟨rfl, λ _ _, rfl⟩ lemma update_comp_eq_of_forall_ne' {α'} (g : Π a, β a) {f : α' → α} {i : α} (a : β i) (h : ∀ x, f x ≠ i) : (λ j, (update g i a) (f j)) = (λ j, g (f j)) := funext $ λ x, update_noteq (h _) _ _ /-- Non-dependent version of `function.update_comp_eq_of_forall_ne'` -/ lemma update_comp_eq_of_forall_ne {α β : Sort*} (g : α' → β) {f : α → α'} {i : α'} (a : β) (h : ∀ x, f x ≠ i) : (update g i a) ∘ f = g ∘ f := update_comp_eq_of_forall_ne' g a h lemma update_comp_eq_of_injective' (g : Π a, β a) {f : α' → α} (hf : function.injective f) (i : α') (a : β (f i)) : (λ j, update g (f i) a (f j)) = update (λ i, g (f i)) i a := eq_update_iff.2 ⟨update_same _ _ _, λ j hj, update_noteq (hf.ne hj) _ _⟩ /-- Non-dependent version of `function.update_comp_eq_of_injective'` -/ lemma update_comp_eq_of_injective {β : Sort*} (g : α' → β) {f : α → α'} (hf : function.injective f) (i : α) (a : β) : (function.update g (f i) a) ∘ f = function.update (g ∘ f) i a := update_comp_eq_of_injective' g hf i a lemma apply_update {ι : Sort*} [decidable_eq ι] {α β : ι → Sort*} (f : Π i, α i → β i) (g : Π i, α i) (i : ι) (v : α i) (j : ι) : f j (update g i v j) = update (λ k, f k (g k)) i (f i v) j := begin by_cases h : j = i, { subst j, simp }, { simp [h] } end lemma comp_update {α' : Sort*} {β : Sort*} (f : α' → β) (g : α → α') (i : α) (v : α') : f ∘ (update g i v) = update (f ∘ g) i (f v) := funext $ apply_update _ _ _ _ theorem update_comm {α} [decidable_eq α] {β : α → Sort*} {a b : α} (h : a ≠ b) (v : β a) (w : β b) (f : Πa, β a) : update (update f a v) b w = update (update f b w) a v := begin funext c, simp only [update], by_cases h₁ : c = b; by_cases h₂ : c = a; try {simp [h₁, h₂]}, cases h (h₂.symm.trans h₁), end @[simp] theorem update_idem {α} [decidable_eq α] {β : α → Sort*} {a : α} (v w : β a) (f : Πa, β a) : update (update f a v) a w = update f a w := by {funext b, by_cases b = a; simp [update, h]} end update section extend noncomputable theory local attribute [instance, priority 10] classical.prop_decidable variables {α β γ : Type*} {f : α → β} /-- `extend f g e'` extends a function `g : α → γ` along a function `f : α → β` to a function `β → γ`, by using the values of `g` on the range of `f` and the values of an auxiliary function `e' : β → γ` elsewhere. Mostly useful when `f` is injective. -/ def extend (f : α → β) (g : α → γ) (e' : β → γ) : β → γ := λ b, if h : ∃ a, f a = b then g (classical.some h) else e' b lemma extend_def (f : α → β) (g : α → γ) (e' : β → γ) (b : β) : extend f g e' b = if h : ∃ a, f a = b then g (classical.some h) else e' b := rfl @[simp] lemma extend_apply (hf : injective f) (g : α → γ) (e' : β → γ) (a : α) : extend f g e' (f a) = g a := begin simp only [extend_def, dif_pos, exists_apply_eq_apply], exact congr_arg g (hf $ classical.some_spec (exists_apply_eq_apply f a)) end @[simp] lemma extend_comp (hf : injective f) (g : α → γ) (e' : β → γ) : extend f g e' ∘ f = g := funext $ λ a, extend_apply hf g e' a end extend lemma uncurry_def {α β γ} (f : α → β → γ) : uncurry f = (λp, f p.1 p.2) := rfl @[simp] lemma uncurry_apply_pair {α β γ} (f : α → β → γ) (x : α) (y : β) : uncurry f (x, y) = f x y := rfl @[simp] lemma curry_apply {α β γ} (f : α × β → γ) (x : α) (y : β) : curry f x y = f (x, y) := rfl section bicomp variables {α β γ δ ε : Type*} /-- Compose a binary function `f` with a pair of unary functions `g` and `h`. If both arguments of `f` have the same type and `g = h`, then `bicompl f g g = f on g`. -/ def bicompl (f : γ → δ → ε) (g : α → γ) (h : β → δ) (a b) := f (g a) (h b) /-- Compose an unary function `f` with a binary function `g`. -/ def bicompr (f : γ → δ) (g : α → β → γ) (a b) := f (g a b) -- Suggested local notation: local notation f `∘₂` g := bicompr f g lemma uncurry_bicompr (f : α → β → γ) (g : γ → δ) : uncurry (g ∘₂ f) = (g ∘ uncurry f) := rfl lemma uncurry_bicompl (f : γ → δ → ε) (g : α → γ) (h : β → δ) : uncurry (bicompl f g h) = (uncurry f) ∘ (prod.map g h) := rfl end bicomp section uncurry variables {α β γ δ : Type*} /-- Records a way to turn an element of `α` into a function from `β` to `γ`. The most generic use is to recursively uncurry. For instance `f : α → β → γ → δ` will be turned into `↿f : α × β × γ → δ`. One can also add instances for bundled maps. -/ class has_uncurry (α : Type*) (β : out_param Type*) (γ : out_param Type*) := (uncurry : α → (β → γ)) /-- Uncurrying operator. The most generic use is to recursively uncurry. For instance `f : α → β → γ → δ` will be turned into `↿f : α × β × γ → δ`. One can also add instances for bundled maps.-/ add_decl_doc has_uncurry.uncurry notation `↿`:max x:max := has_uncurry.uncurry x instance has_uncurry_base : has_uncurry (α → β) α β := ⟨id⟩ instance has_uncurry_induction [has_uncurry β γ δ] : has_uncurry (α → β) (α × γ) δ := ⟨λ f p, ↿(f p.1) p.2⟩ end uncurry /-- A function is involutive, if `f ∘ f = id`. -/ def involutive {α} (f : α → α) : Prop := ∀ x, f (f x) = x lemma involutive_iff_iter_2_eq_id {α} {f : α → α} : involutive f ↔ (f^[2] = id) := funext_iff.symm namespace involutive variables {α : Sort u} {f : α → α} (h : involutive f) include h @[simp] lemma comp_self : f ∘ f = id := funext h protected lemma left_inverse : left_inverse f f := h protected lemma right_inverse : right_inverse f f := h protected lemma injective : injective f := h.left_inverse.injective protected lemma surjective : surjective f := λ x, ⟨f x, h x⟩ protected lemma bijective : bijective f := ⟨h.injective, h.surjective⟩ /-- Involuting an `ite` of an involuted value `x : α` negates the `Prop` condition in the `ite`. -/ protected lemma ite_not (P : Prop) [decidable P] (x : α) : f (ite P x (f x)) = ite (¬ P) x (f x) := by rw [apply_ite f, h, ite_not] end involutive /-- The property of a binary function `f : α → β → γ` being injective. Mathematically this should be thought of as the corresponding function `α × β → γ` being injective. -/ @[reducible] def injective2 {α β γ} (f : α → β → γ) : Prop := ∀ ⦃a₁ a₂ b₁ b₂⦄, f a₁ b₁ = f a₂ b₂ → a₁ = a₂ ∧ b₁ = b₂ namespace injective2 variables {α β γ : Type*} (f : α → β → γ) protected lemma left (hf : injective2 f) ⦃a₁ a₂ b₁ b₂⦄ (h : f a₁ b₁ = f a₂ b₂) : a₁ = a₂ := (hf h).1 protected lemma right (hf : injective2 f) ⦃a₁ a₂ b₁ b₂⦄ (h : f a₁ b₁ = f a₂ b₂) : b₁ = b₂ := (hf h).2 lemma eq_iff (hf : injective2 f) ⦃a₁ a₂ b₁ b₂⦄ : f a₁ b₁ = f a₂ b₂ ↔ a₁ = a₂ ∧ b₁ = b₂ := ⟨λ h, hf h, λ⟨h1, h2⟩, congr_arg2 f h1 h2⟩ end injective2 section sometimes local attribute [instance, priority 10] classical.prop_decidable /-- `sometimes f` evaluates to some value of `f`, if it exists. This function is especially interesting in the case where `α` is a proposition, in which case `f` is necessarily a constant function, so that `sometimes f = f a` for all `a`. -/ noncomputable def sometimes {α β} [nonempty β] (f : α → β) : β := if h : nonempty α then f (classical.choice h) else classical.choice ‹_› theorem sometimes_eq {p : Prop} {α} [nonempty α] (f : p → α) (a : p) : sometimes f = f a := dif_pos ⟨a⟩ theorem sometimes_spec {p : Prop} {α} [nonempty α] (P : α → Prop) (f : p → α) (a : p) (h : P (f a)) : P (sometimes f) := by rwa sometimes_eq end sometimes end function /-- `s.piecewise f g` is the function equal to `f` on the set `s`, and to `g` on its complement. -/ def set.piecewise {α : Type u} {β : α → Sort v} (s : set α) (f g : Πi, β i) [∀j, decidable (j ∈ s)] : Πi, β i := λi, if i ∈ s then f i else g i /-! ### Bijectivity of `eq.rec`, `eq.mp`, `eq.mpr`, and `cast` -/ lemma eq_rec_on_bijective {α : Sort*} {C : α → Sort*} : ∀ {a a' : α} (h : a = a'), function.bijective (@eq.rec_on _ _ C _ h) | _ _ rfl := ⟨λ x y, id, λ x, ⟨x, rfl⟩⟩ lemma eq_mp_bijective {α β : Sort*} (h : α = β) : function.bijective (eq.mp h) := eq_rec_on_bijective h lemma eq_mpr_bijective {α β : Sort*} (h : α = β) : function.bijective (eq.mpr h) := eq_rec_on_bijective h.symm lemma cast_bijective {α β : Sort*} (h : α = β) : function.bijective (cast h) := eq_rec_on_bijective h /-! Note these lemmas apply to `Type*` not `Sort*`, as the latter interferes with `simp`, and is trivial anyway.-/ @[simp] lemma eq_rec_inj {α : Sort*} {a a' : α} (h : a = a') {C : α → Type*} (x y : C a) : (eq.rec x h : C a') = eq.rec y h ↔ x = y := (eq_rec_on_bijective h).injective.eq_iff @[simp] lemma cast_inj {α β : Type*} (h : α = β) {x y : α} : cast h x = cast h y ↔ x = y := (cast_bijective h).injective.eq_iff /-- A set of functions "separates points" if for each pair of distinct points there is a function taking different values on them. -/ def separates_points {α β : Type*} (A : set (α → β)) : Prop := ∀ ⦃x y : α⦄, x ≠ y → ∃ f ∈ A, (f x : β) ≠ f y /-- A set of functions "separates points strongly" if for each pair of distinct points there is a function with specified values on them. -/ def separates_points_strongly {α β : Type*} (A : set (α → β)) : Prop := ∀ (x y : α), x ≠ y → ∀ (a b : β), ∃ f ∈ A, (f x : β) = a ∧ f y = b
67f38f1f5c8b0053b5ed572509baa6679d06c6e0
9cba98daa30c0804090f963f9024147a50292fa0
/geom/geom1d.lean
dd46c080a7a02ecefa78c22394721c98130d4a1f
[]
no_license
kevinsullivan/phys
dcb192f7b3033797541b980f0b4a7e75d84cea1a
ebc2df3779d3605ff7a9b47eeda25c2a551e011f
refs/heads/master
1,637,490,575,500
1,629,899,064,000
1,629,899,064,000
168,012,884
0
3
null
1,629,644,436,000
1,548,699,832,000
Lean
UTF-8
Lean
false
false
17,620
lean
import ..phys_dimensions import linear_algebra.affine_space.basic import ...math.euclidnK.euclidnK_definitions import ..scalar open_locale affine section foo universes u abbreviation geom1d_frame := fm real_scalar 1 LENGTH abbreviation geom1d_space (f : geom1d_frame) := spc real_scalar f noncomputable def geom1d_std_frame : geom1d_frame := fm.base 1 LENGTH noncomputable def geom1d_std_space : geom1d_space geom1d_std_frame := mk_space (geom1d_std_frame) structure position1d {f : geom1d_frame} (s : spc _ f ) extends point s @[ext] lemma position1d.ext : ∀ {f : geom1d_frame} {s : geom1d_space f } (x y : position1d s), x.to_point = y.to_point → x = y := begin intros f s x y e, cases x, cases y, simp *, have h₁ : ({to_point := x} : position1d s).to_point = x := rfl, simp [h₁] at e, exact e end noncomputable def position1d.coords {f : geom1d_frame} {s : geom1d_space f } (t :position1d s) := t.to_point.coords @[simp] def mk_position1d' {f : geom1d_frame} (s : geom1d_space f ) (p : point s) : position1d s := position1d.mk p @[simp] noncomputable def mk_position1d {f : geom1d_frame} (s : geom1d_space f ) (k : real_scalar) : position1d s := position1d.mk (mk_point s ⟨[k],rfl⟩) structure displacement1d {f : geom1d_frame} (s : geom1d_space f ) extends vectr s @[ext] lemma displacement1d.ext : ∀ {f : geom1d_frame} {s : geom1d_space f } (x y : displacement1d s), x.to_vectr = y.to_vectr → x = y := begin intros f s x y e, cases x, cases y, simp *, have h₁ : ({to_vectr := x} : displacement1d s).to_vectr = x := rfl, simp [h₁] at e, exact e end noncomputable def displacement1d.coords {f : geom1d_frame} {s : geom1d_space f } (d :displacement1d s) := d.to_vectr.coords @[simp] def mk_displacement1d' {f : geom1d_frame} (s : geom1d_space f ) (v : vectr s) : displacement1d s := displacement1d.mk v @[simp] noncomputable def mk_displacement1d {f : geom1d_frame} (s : geom1d_space f ) (k : real_scalar) : displacement1d s := displacement1d.mk (mk_vectr s ⟨[k],rfl⟩) @[simp] noncomputable def mk_geom1d_frame {parent : geom1d_frame} {s : spc real_scalar parent} (p : position1d s) (v : displacement1d s) : geom1d_frame := ((mk_frame p.to_point (vectr_basis.mk (λi, v.to_vectr) sorry sorry)) : geom1d_frame) --fm.deriv LENGTH (p.to_point.to_pt, v.to_vectr.to_vec) parent -- TODO: make sure v ≠ 0 end foo section bar /- ************************************* Instantiate module real_scalar (vector real_scalar) ************************************* -/ namespace geom1d variables {f : geom1d_frame} {s : geom1d_space f } @[simp] noncomputable def add_displacement1d_displacement1d (v1 v2 : displacement1d s) : displacement1d s := mk_displacement1d' s (v1.to_vectr + v2.to_vectr) @[simp] noncomputable def smul_displacement1d (k : real_scalar) (v : displacement1d s) : displacement1d s := mk_displacement1d' s (k • v.to_vectr) @[simp] noncomputable def neg_displacement1d (v : displacement1d s) : displacement1d s := mk_displacement1d' s ((-1 : real_scalar) • v.to_vectr) @[simp] noncomputable def sub_displacement1d_displacement1d (v1 v2 : displacement1d s) : displacement1d s := -- v1-v2 add_displacement1d_displacement1d v1 (neg_displacement1d v2) -- See unframed file for template for proving module noncomputable instance has_add_displacement1d : has_add (displacement1d s) := ⟨ add_displacement1d_displacement1d ⟩ lemma add_assoc_displacement1d : ∀ a b c : displacement1d s, a + b + c = a + (b + c) := begin intros, ext, dsimp only [has_add.add], dsimp only [add_displacement1d_displacement1d, has_add.add], dsimp only [add_vectr_vectr, has_add.add], dsimp only [add_vec_vec, mk_displacement1d', mk_vectr'], simp only [add_assoc], end noncomputable instance add_semigroup_displacement1d : add_semigroup (displacement1d s) := ⟨ add_displacement1d_displacement1d, add_assoc_displacement1d⟩ @[simp] noncomputable def displacement1d_zero := mk_displacement1d s 0 noncomputable instance has_zero_displacement1d : has_zero (displacement1d s) := ⟨displacement1d_zero⟩ /- Andrew 5/14 - broke this, fix someposition1d soon -/ lemma zero_add_displacement1d : ∀ a : displacement1d s, 0 + a = a := begin intros, ext, dsimp only [has_zero.zero, has_add.add], dsimp only [add_displacement1d_displacement1d, displacement1d_zero, mk_displacement1d', mk_displacement1d, has_add.add], dsimp only [add_vectr_vectr, mk_vectr', mk_vectr, mk_vec_n, has_add.add], dsimp only [add_vec_vec, mk_vec, vector.nth], simp only [list.nth_le_singleton, zero_add], end lemma add_zero_displacement1d : ∀ a : displacement1d s, a + 0 = a := begin intros, ext, dsimp only [has_zero.zero, has_add.add], dsimp only [add_displacement1d_displacement1d, displacement1d_zero, mk_displacement1d', mk_displacement1d, has_add.add], dsimp only [add_vectr_vectr, mk_vectr', mk_vectr, mk_vec_n, has_add.add], dsimp only [add_vec_vec, mk_vec, vector.nth], simp only [list.nth_le_singleton, add_zero], end @[simp] noncomputable def nsmul_displacement1d : ℕ → (displacement1d s) → (displacement1d s) | nat.zero v := displacement1d_zero --| 1 v := v | (nat.succ n) v := (add_displacement1d_displacement1d) v (nsmul_displacement1d n v) noncomputable instance add_monoid_displacement1d : add_monoid (displacement1d s) := ⟨ -- add_semigroup add_displacement1d_displacement1d, add_assoc_displacement1d, -- has_zero displacement1d_zero, -- new structure @zero_add_displacement1d f s, add_zero_displacement1d, nsmul_displacement1d, begin admit end, begin admit end ⟩ noncomputable instance has_neg_displacement1d : has_neg (displacement1d s) := ⟨neg_displacement1d⟩ noncomputable instance has_sub_displacement1d : has_sub (displacement1d s) := ⟨ sub_displacement1d_displacement1d⟩ lemma sub_eq_add_neg_displacement1d : ∀ a b : displacement1d s, a - b = a + -b := begin intros,ext, refl, end noncomputable instance sub_neg_monoid_displacement1d : sub_neg_monoid (displacement1d s) := { neg := neg_displacement1d , ..(show add_monoid (displacement1d s), by apply_instance) } lemma add_left_neg_displacement1d : ∀ a : displacement1d s, -a + a = 0 := begin intros, ext, dsimp only [has_zero.zero, has_add.add, has_neg.neg], dsimp only [neg_displacement1d, has_scalar.smul], dsimp only [add_displacement1d_displacement1d, smul_vectr, has_add.add, has_scalar.smul], dsimp only [add_vectr_vectr, smul_vec, mk_displacement1d', mk_vectr', has_add.add], dsimp only [add_vec_vec], simp only [neg_mul_eq_neg_mul_symm, one_mul, mk_vectr, displacement1d_zero, mk_displacement1d, add_left_neg], dsimp only [mk_vec_n, mk_vec, vector.nth], simp only [list.nth_le_singleton], end noncomputable instance : add_group (displacement1d s) := { add_left_neg := begin exact add_left_neg_displacement1d, end, ..(show sub_neg_monoid (displacement1d s), by apply_instance), } lemma add_comm_displacement1d : ∀ a b : displacement1d s, a + b = b + a := begin intros, ext, dsimp only [has_add.add], dsimp only [add_displacement1d_displacement1d, has_add.add], dsimp only [add_vectr_vectr, has_add.add], dsimp only [add_vec_vec, mk_displacement1d', mk_vectr'], simp only [add_comm], end noncomputable instance add_comm_semigroup_displacement1d : add_comm_semigroup (displacement1d s) := ⟨ -- add_semigroup add_displacement1d_displacement1d, add_assoc_displacement1d, add_comm_displacement1d, ⟩ noncomputable instance add_comm_monoid_displacement1d : add_comm_monoid (displacement1d s) := { add_comm := begin exact add_comm_displacement1d end, ..(show add_monoid (displacement1d s), by apply_instance) } noncomputable instance has_scalar_displacement1d : has_scalar real_scalar (displacement1d s) := ⟨ smul_displacement1d, ⟩ lemma one_smul_displacement1d : ∀ b : displacement1d s, (1 : real_scalar) • b = b := begin intros, ext, dsimp only [has_scalar.smul], dsimp only [smul_displacement1d, has_scalar.smul, mk_displacement1d'], simp only [smul_vectr, smul_vec, one_smul, mk_vectr'], end lemma mul_smul_displacement1d : ∀ (x y : real_scalar) (b : displacement1d s), (x * y) • b = x • y • b := begin intros, cases b, ext, dsimp only [has_scalar.smul], dsimp only [smul_displacement1d, has_scalar.smul], dsimp only [smul_vectr, has_scalar.smul], dsimp only [smul_vec, mk_displacement1d', mk_vectr'], simp only [mul_assoc], end noncomputable instance mul_action_displacement1d : mul_action real_scalar (displacement1d s) := ⟨ one_smul_displacement1d, mul_smul_displacement1d, ⟩ lemma smul_add_displacement1d : ∀(r : real_scalar) (x y : displacement1d s), r • (x + y) = r • x + r • y := begin intros, ext, dsimp only [has_scalar.smul, has_add.add], dsimp only [smul_displacement1d, add_displacement1d_displacement1d, has_scalar.smul, has_add.add], dsimp only [smul_vectr, add_vectr_vectr, has_scalar.smul, has_add.add], dsimp only [smul_vec, add_vec_vec, mk_displacement1d', mk_vectr'], apply distrib.left_distrib, end lemma smul_zero_displacement1d : ∀(r : real_scalar), r • (0 : displacement1d s) = 0 := begin intros, ext, dsimp only [has_scalar.smul, has_zero.zero], dsimp only [smul_displacement1d, displacement1d_zero, has_scalar.smul], dsimp only [smul_vectr, has_scalar.smul], dsimp only [smul_vec, mk_displacement1d', mk_vectr', mk_displacement1d, mk_vectr, mk_vec_n, mk_vec, vector.nth], simp only [list.nth_le_singleton, mul_zero], end noncomputable instance distrib_mul_action_K_displacement1d : distrib_mul_action real_scalar (displacement1d s) := ⟨ smul_add_displacement1d, smul_zero_displacement1d, ⟩ -- renaming vs template due to clash with name "s" for prevailing variable lemma add_smul_displacement1d : ∀ (a b : real_scalar) (x : displacement1d s), (a + b) • x = a • x + b • x := begin intros, ext, dsimp only [has_scalar.smul], dsimp only [smul_displacement1d, has_scalar.smul], dsimp only [smul_vectr, has_scalar.smul], dsimp only [smul_vec, mk_displacement1d', mk_vectr'], sorry, end lemma zero_smul_displacement1d : ∀ (x : displacement1d s), (0 : real_scalar) • x = 0 := begin intros, ext, dsimp only [has_scalar.smul], dsimp only [smul_displacement1d, has_scalar.smul], dsimp only [smul_vectr, has_scalar.smul], dsimp only [smul_vec, mk_displacement1d', mk_vectr'], dsimp only [has_zero.zero], dsimp only [displacement1d_zero, mk_displacement1d, mk_vectr, mk_vec_n, mk_vec, vector.nth], simp only [list.nth_le_singleton, mul_eq_zero], apply or.inl, refl, end noncomputable instance module_K_displacement1d : module real_scalar (displacement1d s) := ⟨ add_smul_displacement1d, zero_smul_displacement1d ⟩ noncomputable instance add_comm_group_displacement1d : add_comm_group (displacement1d s) := { add_comm := begin exact add_comm_displacement1d /-intros, ext, let h0 : (a + b).to_vec = a.to_vec + b.to_vec := rfl, let h1 : (b + a).to_vec = b.to_vec + a.to_vec := rfl, rw [h0,h1], exact add_comm _ _, exact add_comm _ _,-/ end, --to_add_group := (show add_group (vec K), by apply_instance), --to_add_comm_monoid := (show add_comm_monoid (vec K), by apply_instance), ..(show add_group (displacement1d s), by apply_instance) } noncomputable instance : module real_scalar (displacement1d s) := @geom1d.module_K_displacement1d f s /- ******************** *** Affine space *** ******************** -/ /- Affine operations -/ noncomputable instance : has_add (displacement1d s) := ⟨add_displacement1d_displacement1d⟩ noncomputable instance : has_zero (displacement1d s) := ⟨displacement1d_zero⟩ noncomputable instance : has_neg (displacement1d s) := ⟨neg_displacement1d⟩ @[simp] noncomputable def sub_position1d_position1d {f : geom1d_frame} {s : geom1d_space f } (p1 p2 : position1d s) : displacement1d s := mk_displacement1d' s (p1.to_point -ᵥ p2.to_point) @[simp] noncomputable def add_position1d_displacement1d {f : geom1d_frame} {s : geom1d_space f } (p : position1d s) (v : displacement1d s) : position1d s := mk_position1d' s (v.to_vectr +ᵥ p.to_point) -- reorder assumes order is irrelevant @[simp] noncomputable def add_displacement1d_position1d {f : geom1d_frame} {s : geom1d_space f } (v : displacement1d s) (p : position1d s) : position1d s := mk_position1d' s (v.to_vectr +ᵥ p.to_point) noncomputable instance : has_vadd (displacement1d s) (position1d s) := ⟨add_displacement1d_position1d⟩ lemma zero_displacement1d_vadd'_a1 : ∀ p : position1d s, (0 : displacement1d s) +ᵥ p = p := begin intros, ext, dsimp only [has_vadd.vadd, has_zero.zero], dsimp only [add_displacement1d_position1d, displacement1d_zero, has_vadd.vadd], dsimp only [add_vectr_point, has_vadd.vadd], dsimp only [aff_vec_group_action, add_vec_pt, mk_position1d', mk_point', mk_displacement1d, mk_vectr, mk_vec_n, mk_vec, vector.nth], simp only [list.nth_le_singleton, add_zero], end lemma displacement1d_add_assoc'_a1 : ∀ (g1 g2 : displacement1d s) (p : position1d s), g1 +ᵥ (g2 +ᵥ p) = (g1 + g2) +ᵥ p := begin intros, ext, dsimp only [has_add.add, has_vadd.vadd], dsimp only [add_displacement1d_position1d, add_displacement1d_displacement1d, has_add.add, has_vadd.vadd], dsimp only [add_vectr_point, add_vectr_vectr, has_add.add, has_vadd.vadd], dsimp only [aff_vec_group_action, add_vec_vec, add_vec_pt, mk_position1d', mk_point', mk_displacement1d', mk_vectr'], simp only [add_assoc, add_right_inj], simp only [add_comm], end noncomputable instance displacement1d_add_action: add_action (displacement1d s) (position1d s) := ⟨ zero_displacement1d_vadd'_a1, begin let h0 := displacement1d_add_assoc'_a1, intros, exact (h0 g₁ g₂ p).symm end⟩ noncomputable instance position1d_has_vsub : has_vsub (displacement1d s) (position1d s) := ⟨ sub_position1d_position1d⟩ instance : nonempty (position1d s) := ⟨mk_position1d s 0⟩ lemma position1d_vsub_vadd_a1 : ∀ (p1 p2 : (position1d s)), (p1 -ᵥ p2) +ᵥ p2 = p1 := begin intros, ext, dsimp only [has_vsub.vsub, has_vadd.vadd], dsimp only [add_displacement1d_position1d, sub_position1d_position1d, has_vsub.vsub, has_vadd.vadd], dsimp only [add_vectr_point, aff_point_group_sub, sub_point_point, has_vsub.vsub, has_vadd.vadd], dsimp only [aff_vec_group_action, aff_point_group_sub, add_vec_pt, aff_pt_group_sub, sub_pt_pt, mk_position1d', mk_point', mk_displacement1d', mk_vectr'], simp only [add_sub_cancel'_right], end lemma position1d_vadd_vsub_a1 : ∀ (g : displacement1d s) (p : position1d s), g +ᵥ p -ᵥ p = g := begin intros, ext, repeat { have h0 : ((g +ᵥ p -ᵥ p) : displacement1d s).to_vectr = (g.to_vectr +ᵥ p.to_point -ᵥ p.to_point) := rfl, rw h0, simp *, } end noncomputable instance aff_geom1d_torsor : add_torsor (displacement1d s) (position1d s) := ⟨ begin exact position1d_vsub_vadd_a1, end, begin exact position1d_vadd_vsub_a1, end, ⟩ open_locale affine noncomputable instance : affine_space (displacement1d s) (position1d s) := @geom1d.aff_geom1d_torsor f s end geom1d -- ha ha end bar /- Newer version Tradeoff - Does not directly extend from affine equiv. Base class is an equiv on points and vectrs Extension methods are provided to directly transform Times and Duration between frames -/ @[ext] structure geom1d_transform {f1 : geom1d_frame} {f2 : geom1d_frame} (sp1 : geom1d_space f1) (sp2 : geom1d_space f2) extends fm_tr sp1 sp2 noncomputable def geom1d_space.mk_geom1d_transform_to {f1 : geom1d_frame} (s1 : geom1d_space f1) : Π {f2 : geom1d_frame} (s2 : geom1d_space f2), geom1d_transform s1 s2 := --(position1d s2) ≃ᵃ[real_scalar] (position1d s1) := λ f2 s2, ⟨s1.fm_tr s2⟩ noncomputable def geom1d_transform.symm {f1 : geom1d_frame} {f2 : geom1d_frame} {sp1 : geom1d_space f1} {sp2 : geom1d_space f2} (ttr : geom1d_transform sp1 sp2) : geom1d_transform sp2 sp1 := ⟨(ttr.1).symm⟩ noncomputable def geom1d_transform.trans {f1 : geom1d_frame} {f2 : geom1d_frame} {f3 : geom1d_frame} {sp1 : geom1d_space f1} {sp2 : geom1d_space f2} {sp3 : geom1d_space f3} (ttr : geom1d_transform sp1 sp2) : geom1d_transform sp2 sp3 → geom1d_transform sp1 sp3 := λttr_, ⟨(ttr.1).trans ttr_.1⟩ noncomputable def geom1d_transform.transform_position1d {f1 : geom1d_frame} {s1 : spc _ f1} {f2 : geom1d_frame} {s2 : spc _ f2} (tr: geom1d_transform s1 s2 ) : position1d s1 → position1d s2 := λt : position1d s1, ⟨tr.to_fm_tr.to_equiv t.to_point⟩ noncomputable def geom1d_transform.transform_displacement1d {f1 : geom1d_frame} {s1 : spc _ f1} {f2 : geom1d_frame} {s2 : spc _ f2} (tr: geom1d_transform s1 s2 ) : displacement1d s1 → displacement1d s2 := λd, let as_pt : point s1 := ⟨λi, mk_pt real_scalar (d.coords i).coord⟩ in let tr_pt := (tr.to_equiv as_pt) in ⟨⟨λi, mk_vec real_scalar (tr_pt.coords i).coord⟩⟩
56ab1ac13983cac1027b811f1c4341c5cb44e591
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/finset/option.lean
88793ad39b3f3a698b0f379ceec887411a8d35a8
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
4,456
lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Mario Carneiro, Sean Leather -/ import data.finset.card /-! # Finite sets in `option α` > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we define * `option.to_finset`: construct an empty or singleton `finset α` from an `option α`; * `finset.insert_none`: given `s : finset α`, lift it to a finset on `option α` using `option.some` and then insert `option.none`; * `finset.erase_none`: given `s : finset (option α)`, returns `t : finset α` such that `x ∈ t ↔ some x ∈ s`. Then we prove some basic lemmas about these definitions. ## Tags finset, option -/ variables {α β : Type*} open function namespace option /-- Construct an empty or singleton finset from an `option` -/ def to_finset (o : option α) : finset α := o.elim ∅ singleton @[simp] theorem to_finset_none : none.to_finset = (∅ : finset α) := rfl @[simp] theorem to_finset_some {a : α} : (some a).to_finset = {a} := rfl @[simp] theorem mem_to_finset {a : α} {o : option α} : a ∈ o.to_finset ↔ a ∈ o := by cases o; simp [eq_comm] theorem card_to_finset (o : option α) : o.to_finset.card = o.elim 0 1 := by cases o; refl end option namespace finset /-- Given a finset on `α`, lift it to being a finset on `option α` using `option.some` and then insert `option.none`. -/ def insert_none : finset α ↪o finset (option α) := order_embedding.of_map_le_iff (λ s, cons none (s.map embedding.some) $ by simp) $ λ s t, cons_subset_cons.trans map_subset_map /-⟨none ::ₘ s.1.map some, multiset.nodup_cons.2 ⟨by simp, s.nodup.map $ λ a b, option.some.inj⟩⟩-/ @[simp] theorem 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 some_mem_insert_none {s : finset α} {a : α} : some a ∈ s.insert_none ↔ a ∈ s := by simp @[simp] theorem card_insert_none (s : finset α) : s.insert_none.card = s.card + 1 := by simp [insert_none] /-- Given `s : finset (option α)`, `s.erase_none : finset α` is the set of `x : α` such that `some x ∈ s`. -/ def erase_none : finset (option α) →o finset α := (finset.map_embedding (equiv.option_is_some_equiv α).to_embedding).to_order_hom.comp ⟨finset.subtype _, subtype_mono⟩ @[simp] lemma mem_erase_none {s : finset (option α)} {x : α} : x ∈ s.erase_none ↔ some x ∈ s := by simp [erase_none] lemma erase_none_eq_bUnion [decidable_eq α] (s : finset (option α)) : s.erase_none = s.bUnion option.to_finset := by { ext, simp } @[simp] lemma erase_none_map_some (s : finset α) : (s.map embedding.some).erase_none = s := by { ext, simp } @[simp] lemma erase_none_image_some [decidable_eq (option α)] (s : finset α) : (s.image some).erase_none = s := by simpa only [map_eq_image] using erase_none_map_some s @[simp] lemma coe_erase_none (s : finset (option α)) : (s.erase_none : set α) = some ⁻¹' s := set.ext $ λ x, mem_erase_none @[simp] lemma erase_none_union [decidable_eq (option α)] [decidable_eq α] (s t : finset (option α)) : (s ∪ t).erase_none = s.erase_none ∪ t.erase_none := by { ext, simp } @[simp] lemma erase_none_inter [decidable_eq (option α)] [decidable_eq α] (s t : finset (option α)) : (s ∩ t).erase_none = s.erase_none ∩ t.erase_none := by { ext, simp } @[simp] lemma erase_none_empty : (∅ : finset (option α)).erase_none = ∅ := by { ext, simp } @[simp] lemma erase_none_none : ({none} : finset (option α)).erase_none = ∅ := by { ext, simp } @[simp] lemma image_some_erase_none [decidable_eq (option α)] (s : finset (option α)) : s.erase_none.image some = s.erase none := by ext (_|x); simp @[simp] lemma map_some_erase_none [decidable_eq (option α)] (s : finset (option α)) : s.erase_none.map embedding.some = s.erase none := by rw [map_eq_image, embedding.some_apply, image_some_erase_none] @[simp] lemma insert_none_erase_none [decidable_eq (option α)] (s : finset (option α)) : insert_none (erase_none s) = insert none s := by ext (_|x); simp @[simp] lemma erase_none_insert_none (s : finset α) : erase_none (insert_none s) = s := by { ext, simp } end finset
721cf143ff9c851fa794f2112f24a0f91e579029
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/topology/separation.lean
0dd1fcd92f90d9dfa7fbc4b6ddd611b9143fe2c7
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
21,995
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 Separation properties of topological spaces. -/ import topology.subset_properties open set filter open_locale topological_space filter local attribute [instance] classical.prop_decidable -- TODO: use "open_locale classical" universes u v variables {α : Type u} {β : Type v} [topological_space α] section separation /-- A T₀ space, also known as a Kolmogorov space, is a topological space where for every pair `x ≠ y`, there is an open set containing one but not the other. -/ class t0_space (α : Type u) [topological_space α] : Prop := (t0 : ∀ x y, x ≠ y → ∃ U:set α, is_open U ∧ (xor (x ∈ U) (y ∈ U))) theorem exists_open_singleton_of_open_finset [t0_space α] (s : finset α) (sne : s.nonempty) (hso : is_open (↑s : set α)) : ∃ x ∈ s, is_open ({x} : set α):= begin induction s using finset.strong_induction_on with s ihs, by_cases hs : set.subsingleton (↑s : set α), { rcases sne with ⟨x, hx⟩, refine ⟨x, hx, _⟩, have : (↑s : set α) = {x}, from hs.eq_singleton_of_mem hx, rwa this at hso }, { dunfold set.subsingleton at hs, push_neg at hs, rcases hs with ⟨x, hx, y, hy, hxy⟩, rcases t0_space.t0 x y hxy with ⟨U, hU, hxyU⟩, wlog H : x ∈ U ∧ y ∉ U := hxyU using [x y, y x], obtain ⟨z, hzs, hz⟩ : ∃ z ∈ s.filter (λ z, z ∈ U), is_open ({z} : set α), { refine ihs _ (finset.filter_ssubset.2 ⟨y, hy, H.2⟩) ⟨x, finset.mem_filter.2 ⟨hx, H.1⟩⟩ _, rw [finset.coe_filter], exact is_open_inter hso hU }, exact ⟨z, (finset.mem_filter.1 hzs).1, hz⟩ } end theorem exists_open_singleton_of_fintype [t0_space α] [f : fintype α] [ha : nonempty α] : ∃ x:α, is_open ({x}:set α) := begin refine ha.elim (λ x, _), have : is_open (↑(finset.univ : finset α) : set α), { simp }, rcases exists_open_singleton_of_open_finset _ ⟨x, finset.mem_univ x⟩ this with ⟨x, _, hx⟩, exact ⟨x, hx⟩ end instance subtype.t0_space [t0_space α] {p : α → Prop} : t0_space (subtype p) := ⟨λ x y hxy, let ⟨U, hU, hxyU⟩ := t0_space.t0 (x:α) y ((not_congr subtype.ext_iff_val).1 hxy) in ⟨(coe : subtype p → α) ⁻¹' U, is_open_induced hU, hxyU⟩⟩ /-- A T₁ space, also known as a Fréchet space, is a topological space where every singleton set is closed. Equivalently, for every pair `x ≠ y`, there is an open set containing `x` and not `y`. -/ class t1_space (α : Type u) [topological_space α] : Prop := (t1 : ∀x, is_closed ({x} : set α)) lemma is_closed_singleton [t1_space α] {x : α} : is_closed ({x} : set α) := t1_space.t1 x lemma is_open_ne [t1_space α] {x : α} : is_open {y | y ≠ x} := compl_singleton_eq x ▸ is_open_compl_iff.2 (t1_space.t1 x) instance subtype.t1_space {α : Type u} [topological_space α] [t1_space α] {p : α → Prop} : t1_space (subtype p) := ⟨λ ⟨x, hx⟩, is_closed_induced_iff.2 $ ⟨{x}, is_closed_singleton, set.ext $ λ y, by simp [subtype.ext_iff_val]⟩⟩ @[priority 100] -- see Note [lower instance priority] instance t1_space.t0_space [t1_space α] : t0_space α := ⟨λ x y h, ⟨{z | z ≠ y}, is_open_ne, or.inl ⟨h, not_not_intro rfl⟩⟩⟩ lemma compl_singleton_mem_nhds [t1_space α] {x y : α} (h : y ≠ x) : {x}ᶜ ∈ 𝓝 y := mem_nhds_sets is_closed_singleton $ by rwa [mem_compl_eq, mem_singleton_iff] @[simp] lemma closure_singleton [t1_space α] {a : α} : closure ({a} : set α) = {a} := is_closed_singleton.closure_eq /-- A T₂ space, also known as a Hausdorff space, is one in which for every `x ≠ y` there exists disjoint open sets around `x` and `y`. This is the most widely used of the separation axioms. -/ class t2_space (α : Type u) [topological_space α] : Prop := (t2 : ∀x y, x ≠ y → ∃u v : set α, is_open u ∧ is_open v ∧ x ∈ u ∧ y ∈ v ∧ u ∩ v = ∅) lemma t2_separation [t2_space α] {x y : α} (h : x ≠ y) : ∃u v : set α, is_open u ∧ is_open v ∧ x ∈ u ∧ y ∈ v ∧ u ∩ v = ∅ := t2_space.t2 x y h @[priority 100] -- see Note [lower instance priority] instance t2_space.t1_space [t2_space α] : t1_space α := ⟨λ x, is_open_iff_forall_mem_open.2 $ λ y hxy, let ⟨u, v, hu, hv, hyu, hxv, huv⟩ := t2_separation (mt mem_singleton_of_eq hxy) in ⟨u, λ z hz1 hz2, (ext_iff.1 huv x).1 ⟨mem_singleton_iff.1 hz2 ▸ hz1, hxv⟩, hu, hyu⟩⟩ lemma eq_of_nhds_ne_bot [ht : t2_space α] {x y : α} (h : ne_bot (𝓝 x ⊓ 𝓝 y)) : x = y := classical.by_contradiction $ assume : x ≠ y, let ⟨u, v, hu, hv, hx, hy, huv⟩ := t2_space.t2 x y this in absurd huv $ (inf_ne_bot_iff.1 h (mem_nhds_sets hu hx) (mem_nhds_sets hv hy)).ne_empty lemma t2_iff_nhds : t2_space α ↔ ∀ {x y : α}, ne_bot (𝓝 x ⊓ 𝓝 y) → x = y := ⟨assume h, by exactI λ x y, eq_of_nhds_ne_bot, assume h, ⟨assume x y xy, have 𝓝 x ⊓ 𝓝 y = ⊥ := classical.by_contradiction (mt h xy), let ⟨u', hu', v', hv', u'v'⟩ := empty_in_sets_eq_bot.mpr this, ⟨u, uu', uo, hu⟩ := mem_nhds_sets_iff.mp hu', ⟨v, vv', vo, hv⟩ := mem_nhds_sets_iff.mp hv' in ⟨u, v, uo, vo, hu, hv, disjoint.eq_bot $ disjoint.mono uu' vv' u'v'⟩⟩⟩ lemma t2_iff_ultrafilter : t2_space α ↔ ∀ f {x y : α}, is_ultrafilter f → f ≤ 𝓝 x → f ≤ 𝓝 y → x = y := t2_iff_nhds.trans ⟨assume h f x y u fx fy, h $ u.1.mono (le_inf fx fy), assume h x y xy, let ⟨f, hf, uf⟩ := @@exists_ultrafilter _ xy in h f uf (le_trans hf inf_le_left) (le_trans hf inf_le_right)⟩ lemma is_closed_diagonal [t2_space α] : is_closed (diagonal α) := is_closed_iff_cluster_pt.mpr $ assume ⟨a₁, a₂⟩ h, eq_of_nhds_ne_bot $ assume : 𝓝 a₁ ⊓ 𝓝 a₂ = ⊥, h $ let ⟨t₁, ht₁, t₂, ht₂, (h' : t₁ ∩ t₂ ⊆ ∅)⟩ := by rw [←empty_in_sets_eq_bot, mem_inf_sets] at this; exact this in begin change t₁ ∈ 𝓝 a₁ at ht₁, change t₂ ∈ 𝓝 a₂ at ht₂, rw [nhds_prod_eq, ←empty_in_sets_eq_bot], apply filter.sets_of_superset, apply inter_mem_inf_sets (prod_mem_prod ht₁ ht₂) (mem_principal_sets.mpr (subset.refl _)), exact assume ⟨x₁, x₂⟩ ⟨⟨hx₁, hx₂⟩, (heq : x₁ = x₂)⟩, show false, from @h' x₁ ⟨hx₁, heq.symm ▸ hx₂⟩ end lemma t2_iff_is_closed_diagonal : t2_space α ↔ is_closed (diagonal α) := begin split, { introI h, exact is_closed_diagonal }, { intro h, constructor, intros x y hxy, have : (x, y) ∈ (diagonal α)ᶜ, by rwa [mem_compl_iff], obtain ⟨t, t_sub, t_op, xyt⟩ : ∃ t ⊆ (diagonal α)ᶜ, is_open t ∧ (x, y) ∈ t := is_open_iff_forall_mem_open.mp h _ this, rcases is_open_prod_iff.mp t_op x y xyt with ⟨U, V, U_op, V_op, xU, yV, H⟩, use [U, V, U_op, V_op, xU, yV], have := subset.trans H t_sub, rw eq_empty_iff_forall_not_mem, rintros z ⟨zU, zV⟩, have : ¬ (z, z) ∈ diagonal α := this (mk_mem_prod zU zV), exact this rfl }, end @[simp] lemma nhds_eq_nhds_iff {a b : α} [t2_space α] : 𝓝 a = 𝓝 b ↔ a = b := ⟨assume h, eq_of_nhds_ne_bot $ by rw [h, inf_idem]; exact nhds_ne_bot, assume h, h ▸ rfl⟩ @[simp] lemma nhds_le_nhds_iff {a b : α} [t2_space α] : 𝓝 a ≤ 𝓝 b ↔ a = b := ⟨assume h, eq_of_nhds_ne_bot $ by rw [inf_of_le_left h]; exact nhds_ne_bot, assume h, h ▸ le_refl _⟩ lemma tendsto_nhds_unique [t2_space α] {f : β → α} {l : filter β} {a b : α} [ne_bot l] (ha : tendsto f l (𝓝 a)) (hb : tendsto f l (𝓝 b)) : a = b := eq_of_nhds_ne_bot $ ne_bot_of_le $ le_inf ha hb lemma tendsto_nhds_unique' [t2_space α] {f : β → α} {l : filter β} {a b : α} (hl : ne_bot l) (ha : tendsto f l (𝓝 a)) (hb : tendsto f l (𝓝 b)) : a = b := eq_of_nhds_ne_bot $ ne_bot_of_le $ le_inf ha hb section lim variables [t2_space α] {f : filter α} /-! ### Properties of `Lim` and `lim` In this section we use explicit `nonempty α` instances for `Lim` and `lim`. This way the lemmas are useful without a `nonempty α` instance. -/ lemma Lim_eq {a : α} [ne_bot f] (h : f ≤ 𝓝 a) : @Lim _ _ ⟨a⟩ f = a := tendsto_nhds_unique (Lim_spec ⟨a, h⟩) h lemma filter.tendsto.lim_eq {a : α} {f : filter β} {g : β → α} (h : tendsto g f (𝓝 a)) [ne_bot f] : @lim _ _ _ ⟨a⟩ f g = a := Lim_eq h lemma continuous.lim_eq [topological_space β] {f : β → α} (h : continuous f) (a : β) : @lim _ _ _ ⟨f a⟩ (𝓝 a) f = f a := (h.tendsto a).lim_eq @[simp] lemma Lim_nhds (a : α) : @Lim _ _ ⟨a⟩ (𝓝 a) = a := Lim_eq (le_refl _) @[simp] lemma lim_nhds_id (a : α) : @lim _ _ _ ⟨a⟩ (𝓝 a) id = a := Lim_nhds a @[simp] lemma Lim_nhds_within {a : α} {s : set α} (h : a ∈ closure s) : @Lim _ _ ⟨a⟩ (𝓝[s] a) = a := by haveI : ne_bot (𝓝[s] a) := mem_closure_iff_cluster_pt.1 h; exact Lim_eq inf_le_left @[simp] lemma lim_nhds_within_id {a : α} {s : set α} (h : a ∈ closure s) : @lim _ _ _ ⟨a⟩ (𝓝[s] a) id = a := Lim_nhds_within h end lim @[priority 100] -- see Note [lower instance priority] instance t2_space_discrete {α : Type*} [topological_space α] [discrete_topology α] : t2_space α := { t2 := assume x y hxy, ⟨{x}, {y}, is_open_discrete _, is_open_discrete _, rfl, rfl, eq_empty_iff_forall_not_mem.2 $ by intros z hz; cases eq_of_mem_singleton hz.1; cases eq_of_mem_singleton hz.2; cc⟩ } private lemma separated_by_f {α : Type*} {β : Type*} [tα : topological_space α] [tβ : topological_space β] [t2_space β] (f : α → β) (hf : tα ≤ tβ.induced f) {x y : α} (h : f x ≠ f y) : ∃u v : set α, is_open u ∧ is_open v ∧ x ∈ u ∧ y ∈ v ∧ u ∩ v = ∅ := let ⟨u, v, uo, vo, xu, yv, uv⟩ := t2_separation h in ⟨f ⁻¹' u, f ⁻¹' v, hf _ ⟨u, uo, rfl⟩, hf _ ⟨v, vo, rfl⟩, xu, yv, by rw [←preimage_inter, uv, preimage_empty]⟩ instance {α : Type*} {p : α → Prop} [t : topological_space α] [t2_space α] : t2_space (subtype p) := ⟨assume x y h, separated_by_f subtype.val (le_refl _) (mt subtype.eq h)⟩ instance {α : Type*} {β : Type*} [t₁ : topological_space α] [t2_space α] [t₂ : topological_space β] [t2_space β] : t2_space (α × β) := ⟨assume ⟨x₁,x₂⟩ ⟨y₁,y₂⟩ h, or.elim (not_and_distrib.mp (mt prod.ext_iff.mpr h)) (λ h₁, separated_by_f prod.fst inf_le_left h₁) (λ h₂, separated_by_f prod.snd inf_le_right h₂)⟩ instance Pi.t2_space {α : Type*} {β : α → Type v} [t₂ : Πa, topological_space (β a)] [Πa, t2_space (β a)] : t2_space (Πa, β a) := ⟨assume x y h, let ⟨i, hi⟩ := not_forall.mp (mt funext h) in separated_by_f (λz, z i) (infi_le _ i) hi⟩ variables [topological_space β] lemma is_closed_eq [t2_space α] {f g : β → α} (hf : continuous f) (hg : continuous g) : is_closed {x:β | f x = g x} := continuous_iff_is_closed.mp (hf.prod_mk hg) _ is_closed_diagonal lemma diagonal_eq_range_diagonal_map {α : Type*} : {p:α×α | p.1 = p.2} = range (λx, (x,x)) := ext $ assume p, iff.intro (assume h, ⟨p.1, prod.ext_iff.2 ⟨rfl, h⟩⟩) (assume ⟨x, hx⟩, show p.1 = p.2, by rw ←hx) lemma prod_subset_compl_diagonal_iff_disjoint {α : Type*} {s t : set α} : set.prod s t ⊆ {p:α×α | p.1 = p.2}ᶜ ↔ s ∩ t = ∅ := by rw [eq_empty_iff_forall_not_mem, subset_compl_comm, diagonal_eq_range_diagonal_map, range_subset_iff]; simp lemma compact_compact_separated [t2_space α] {s t : set α} (hs : is_compact s) (ht : is_compact t) (hst : s ∩ t = ∅) : ∃u v : set α, is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ u ∩ v = ∅ := by simp only [prod_subset_compl_diagonal_iff_disjoint.symm] at ⊢ hst; exact generalized_tube_lemma hs ht is_closed_diagonal hst lemma is_compact.is_closed [t2_space α] {s : set α} (hs : is_compact s) : is_closed s := is_open_compl_iff.mpr $ is_open_iff_forall_mem_open.mpr $ assume x hx, let ⟨u, v, uo, vo, su, xv, uv⟩ := compact_compact_separated hs (compact_singleton : is_compact {x}) (by rwa [inter_comm, ←subset_compl_iff_disjoint, singleton_subset_iff]) in have v ⊆ sᶜ, from subset_compl_comm.mp (subset.trans su (subset_compl_iff_disjoint.mpr uv)), ⟨v, this, vo, by simpa using xv⟩ lemma is_compact.inter [t2_space α] {s t : set α} (hs : is_compact s) (ht : is_compact t) : is_compact (s ∩ t) := hs.inter_right $ ht.is_closed /-- If a compact set is covered by two open sets, then we can cover it by two compact subsets. -/ lemma is_compact.binary_compact_cover [t2_space α] {K U V : set α} (hK : is_compact K) (hU : is_open U) (hV : is_open V) (h2K : K ⊆ U ∪ V) : ∃ K₁ K₂ : set α, is_compact K₁ ∧ is_compact K₂ ∧ K₁ ⊆ U ∧ K₂ ⊆ V ∧ K = K₁ ∪ K₂ := begin rcases compact_compact_separated (compact_diff hK hU) (compact_diff hK hV) (by rwa [diff_inter_diff, diff_eq_empty]) with ⟨O₁, O₂, h1O₁, h1O₂, h2O₁, h2O₂, hO⟩, refine ⟨_, _, compact_diff hK h1O₁, compact_diff hK h1O₂, by rwa [diff_subset_comm], by rwa [diff_subset_comm], by rw [← diff_inter, hO, diff_empty]⟩ end section open finset function /-- For every finite open cover `Uᵢ` of a compact set, there exists a compact cover `Kᵢ ⊆ Uᵢ`. -/ lemma is_compact.finite_compact_cover [t2_space α] {s : set α} (hs : is_compact s) {ι} (t : finset ι) (U : ι → set α) (hU : ∀ i ∈ t, is_open (U i)) (hsC : s ⊆ ⋃ i ∈ t, U i) : ∃ K : ι → set α, (∀ i, is_compact (K i)) ∧ (∀i, K i ⊆ U i) ∧ s = ⋃ i ∈ t, K i := begin classical, induction t using finset.induction with x t hx ih generalizing U hU s hs hsC, { refine ⟨λ _, ∅, λ i, compact_empty, λ i, empty_subset _, _⟩, simpa only [subset_empty_iff, finset.not_mem_empty, Union_neg, Union_empty, not_false_iff] using hsC }, simp only [finset.bUnion_insert] at hsC, simp only [finset.mem_insert] at hU, have hU' : ∀ i ∈ t, is_open (U i) := λ i hi, hU i (or.inr hi), rcases hs.binary_compact_cover (hU x (or.inl rfl)) (is_open_bUnion hU') hsC with ⟨K₁, K₂, h1K₁, h1K₂, h2K₁, h2K₂, hK⟩, rcases ih U hU' h1K₂ h2K₂ with ⟨K, h1K, h2K, h3K⟩, refine ⟨update K x K₁, _, _, _⟩, { intros i, by_cases hi : i = x, { simp only [update_same, hi, h1K₁] }, { rw [← ne.def] at hi, simp only [update_noteq hi, h1K] }}, { intros i, by_cases hi : i = x, { simp only [update_same, hi, h2K₁] }, { rw [← ne.def] at hi, simp only [update_noteq hi, h2K] }}, { simp only [bUnion_insert_update _ hx, hK, h3K] } end end lemma locally_compact_of_compact_nhds [t2_space α] (h : ∀ x : α, ∃ s, s ∈ 𝓝 x ∧ is_compact s) : locally_compact_space α := ⟨assume x n hn, let ⟨u, un, uo, xu⟩ := mem_nhds_sets_iff.mp hn in let ⟨k, kx, kc⟩ := h x in -- K is compact but not necessarily contained in N. -- K \ U is again compact and doesn't contain x, so -- we may find open sets V, W separating x from K \ U. -- Then K \ W is a compact neighborhood of x contained in U. let ⟨v, w, vo, wo, xv, kuw, vw⟩ := compact_compact_separated compact_singleton (compact_diff kc uo) (by rw [singleton_inter_eq_empty]; exact λ h, h.2 xu) in have wn : wᶜ ∈ 𝓝 x, from mem_nhds_sets_iff.mpr ⟨v, subset_compl_iff_disjoint.mpr vw, vo, singleton_subset_iff.mp xv⟩, ⟨k \ w, filter.inter_mem_sets kx wn, subset.trans (diff_subset_comm.mp kuw) un, compact_diff kc wo⟩⟩ @[priority 100] -- see Note [lower instance priority] instance locally_compact_of_compact [t2_space α] [compact_space α] : locally_compact_space α := locally_compact_of_compact_nhds (assume x, ⟨univ, mem_nhds_sets is_open_univ trivial, compact_univ⟩) /-- In a locally compact T₂ space, every point has an open neighborhood with compact closure -/ lemma exists_open_with_compact_closure [locally_compact_space α] [t2_space α] (x : α) : ∃ (U : set α), is_open U ∧ x ∈ U ∧ is_compact (closure U) := begin rcases locally_compact_space.local_compact_nhds x set.univ filter.univ_mem_sets with ⟨K, h1K, _, h2K⟩, rw [mem_nhds_sets_iff] at h1K, rcases h1K with ⟨t, h1t, h2t, h3t⟩, exact ⟨t, h2t, h3t, compact_of_is_closed_subset h2K is_closed_closure $ closure_minimal h1t $ h2K.is_closed⟩ end /-- In a locally compact T₂ space, every compact set is contained in the interior of a compact set. -/ lemma exists_compact_superset [locally_compact_space α] [t2_space α] {K : set α} (hK : is_compact K) : ∃ (K' : set α), is_compact K' ∧ K ⊆ interior K' := begin choose U hU using λ x : K, exists_open_with_compact_closure (x : α), rcases hK.elim_finite_subcover U (λ x, (hU x).1) (λ x hx, ⟨_, ⟨⟨x, hx⟩, rfl⟩, (hU ⟨x, hx⟩).2.1⟩) with ⟨s, hs⟩, refine ⟨⋃ (i : K) (H : i ∈ s), closure (U i), _, _⟩, exact (finite_mem_finset s).compact_bUnion (λ x hx, (hU x).2.2), refine subset.trans hs _, rw subset_interior_iff_subset_of_open, exact bUnion_subset_bUnion_right (λ x hx, subset_closure), exact is_open_bUnion (λ x hx, (hU x).1) end end separation section regularity section prio set_option default_priority 100 -- see Note [default priority] /-- A T₃ space, also known as a regular space (although this condition sometimes omits T₂), is one in which for every closed `C` and `x ∉ C`, there exist disjoint open sets containing `x` and `C` respectively. -/ class regular_space (α : Type u) [topological_space α] extends t1_space α : Prop := (regular : ∀{s:set α} {a}, is_closed s → a ∉ s → ∃t, is_open t ∧ s ⊆ t ∧ 𝓝[t] a = ⊥) end prio lemma nhds_is_closed [regular_space α] {a : α} {s : set α} (h : s ∈ 𝓝 a) : ∃t∈(𝓝 a), t ⊆ s ∧ is_closed t := let ⟨s', h₁, h₂, h₃⟩ := mem_nhds_sets_iff.mp h in have ∃t, is_open t ∧ s'ᶜ ⊆ t ∧ 𝓝[t] a = ⊥, from regular_space.regular (is_closed_compl_iff.mpr h₂) (not_not_intro h₃), let ⟨t, ht₁, ht₂, ht₃⟩ := this in ⟨tᶜ, mem_sets_of_eq_bot $ by rwa [compl_compl], subset.trans (compl_subset_comm.1 ht₂) h₁, is_closed_compl_iff.mpr ht₁⟩ lemma closed_nhds_basis [regular_space α] (a : α) : (𝓝 a).has_basis (λ s : set α, s ∈ 𝓝 a ∧ is_closed s) id := ⟨λ t, ⟨λ t_in, let ⟨s, s_in, h_st, h⟩ := nhds_is_closed t_in in ⟨s, ⟨s_in, h⟩, h_st⟩, λ ⟨s, ⟨s_in, hs⟩, hst⟩, mem_sets_of_superset s_in hst⟩⟩ instance subtype.regular_space [regular_space α] {p : α → Prop} : regular_space (subtype p) := ⟨begin intros s a hs ha, rcases is_closed_induced_iff.1 hs with ⟨s, hs', rfl⟩, rcases regular_space.regular hs' ha with ⟨t, ht, hst, hat⟩, refine ⟨coe ⁻¹' t, is_open_induced ht, preimage_mono hst, _⟩, rw [nhds_within, nhds_induced, ← comap_principal, ← comap_inf, ← nhds_within, hat, comap_bot] end⟩ variable (α) @[priority 100] -- see Note [lower instance priority] instance regular_space.t2_space [regular_space α] : t2_space α := ⟨λ x y hxy, let ⟨s, hs, hys, hxs⟩ := regular_space.regular is_closed_singleton (mt mem_singleton_iff.1 hxy), ⟨t, hxt, u, hsu, htu⟩ := empty_in_sets_eq_bot.2 hxs, ⟨v, hvt, hv, hxv⟩ := mem_nhds_sets_iff.1 hxt in ⟨v, s, hv, hs, hxv, singleton_subset_iff.1 hys, eq_empty_of_subset_empty $ λ z ⟨hzv, hzs⟩, htu ⟨hvt hzv, hsu hzs⟩⟩⟩ variable {α} lemma disjoint_nested_nhds [regular_space α] {x y : α} (h : x ≠ y) : ∃ (U₁ V₁ ∈ 𝓝 x) (U₂ V₂ ∈ 𝓝 y), is_closed V₁ ∧ is_closed V₂ ∧ is_open U₁ ∧ is_open U₂ ∧ V₁ ⊆ U₁ ∧ V₂ ⊆ U₂ ∧ U₁ ∩ U₂ = ∅ := begin rcases t2_separation h with ⟨U₁, U₂, U₁_op, U₂_op, x_in, y_in, H⟩, rcases nhds_is_closed (mem_nhds_sets U₁_op x_in) with ⟨V₁, V₁_in, h₁, V₁_closed⟩, rcases nhds_is_closed (mem_nhds_sets U₂_op y_in) with ⟨V₂, V₂_in, h₂, V₂_closed⟩, use [U₁, V₁, mem_sets_of_superset V₁_in h₁, V₁_in, U₂, V₂, mem_sets_of_superset V₂_in h₂, V₂_in], tauto end end regularity section normality section prio set_option default_priority 100 -- see Note [default priority] /-- A T₄ space, also known as a normal space (although this condition sometimes omits T₂), is one in which for every pair of disjoint closed sets `C` and `D`, there exist disjoint open sets containing `C` and `D` respectively. -/ class normal_space (α : Type u) [topological_space α] extends t1_space α : Prop := (normal : ∀ s t : set α, is_closed s → is_closed t → disjoint s t → ∃ u v, is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ disjoint u v) end prio theorem normal_separation [normal_space α] (s t : set α) (H1 : is_closed s) (H2 : is_closed t) (H3 : disjoint s t) : ∃ u v, is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ disjoint u v := normal_space.normal s t H1 H2 H3 @[priority 100] -- see Note [lower instance priority] instance normal_space.regular_space [normal_space α] : regular_space α := { regular := λ s x hs hxs, let ⟨u, v, hu, hv, hsu, hxv, huv⟩ := normal_separation s {x} hs is_closed_singleton (λ _ ⟨hx, hy⟩, hxs $ set.mem_of_eq_of_mem (set.eq_of_mem_singleton hy).symm hx) in ⟨u, hu, hsu, filter.empty_in_sets_eq_bot.1 $ filter.mem_inf_sets.2 ⟨v, mem_nhds_sets hv (set.singleton_subset_iff.1 hxv), u, filter.mem_principal_self u, set.inter_comm u v ▸ huv⟩⟩ } -- We can't make this an instance because it could cause an instance loop. lemma normal_of_compact_t2 [compact_space α] [t2_space α] : normal_space α := begin refine ⟨assume s t hs ht st, _⟩, simp only [disjoint_iff], exact compact_compact_separated hs.compact ht.compact st.eq_bot end end normality
f58a95c1e02a9eeab4190eb9b236d4d6677b8aa8
4d7079ae603c07560e99d1ce35876f769cbb3e24
/src/pareto.lean
410eb87dc03218f903974146afb4e906a5ca4dce
[]
no_license
chasenorman/Formalized-Voting
72493a08aa09d98d0fb589731b842e2d08c991d0
de04e630b83525b042db166670ba97f9952b5691
refs/heads/main
1,687,282,160,284
1,627,155,031,000
1,627,155,031,000
370,472,025
13
0
null
null
null
null
UTF-8
Lean
false
false
4,453
lean
import main import split_cycle import algebra.linear_ordered_comm_group_with_zero open_locale classical variables {V X : Type} def pareto (F : VSCC) (P : Prof V X ) : Prop := ∀ x y, ((∀ v, P v x y) → y ∉ F V X P) lemma unanimous_margin [fintype V] [inhabited V] (P : Prof V X) [profile_asymmetric P] (x y : X) : (∀ v, P v x y) → margin_pos P x y := begin intro r, unfold margin_pos, unfold margin, classical, simp, have empty : (finset.filter (λ (x_1 : V), P x_1 y x) finset.univ).card = 0, apply finset.card_eq_zero.mpr, apply finset.eq_empty_iff_forall_not_mem.mpr, simp, intro v, have b := (_inst_3.asymmetric), specialize b v, specialize r v, specialize b x, specialize b y, exact b r, rw empty, apply zero_lt_iff.mpr, by_contradiction a, push_neg at a, have a := finset.card_eq_zero.mp a, have a := finset.eq_empty_iff_forall_not_mem.mp a, simp at a, specialize a (_inst_2.default), specialize r (_inst_2.default), exact a r, end lemma ineq6 (a b : ℤ) : a > b → a - b > 0 := by omega lemma ineq7 (n : ℕ) : n > 0 → n - 1 < n := by omega lemma ineq8 (n : ℕ) : n > 1 → (n - 1) ≠ 0 := by omega lemma preference_acyclic_ineq (i n : ℕ) : i.succ < n → 0 < n - 1 := by omega lemma preference_acyclic_ineq2 (i n : ℕ) : ¬ (i = 0) → (i.succ < n → i < n - 1) := by omega def preference_acyclic (P : Prof V X) [profile_irreflexive P] [profile_transitive P] (v : V) : ¬ ∃ (l : list X), cycle (P v) l := begin by_contradiction a, cases a with l a, cases a with e a, rw list.chain_iff_pairwise at a, obviously, specialize a_left (l.last e), specialize a_left (list.last_mem e), have irrefl := _inst_1.irreflexive, specialize irrefl v, specialize irrefl (l.last e), exact irrefl a_left, have transitive := _inst_2.transitive, specialize transitive v, exact transitive ᾰ ᾰ_1, end lemma ineq9 (a b c : ℕ) : (a:ℤ) ≤ ↑b - ↑c → a ≤ b := begin intro a, have : (0:ℤ) ≤ c := int.coe_zero_le c, linarith, end def split_cycle_pareto [inhabited V] [fintype V] (P : Prof V X) [profile_asymmetric P] [profile_transitive P] : pareto split_cycle P := begin intros x y r, unfold split_cycle, unfold max_el_VSCC, simp, use x, unfold split_cycle_VCCR, unfold split_cycle_CCR, simp, introI _inst_10, split, exact unanimous_margin P x y r, intro c, intro x_mem, intro y_mem, by_contradiction a, -- suppose there is a "defending" cycle where x dominates y. cases a with e b, have pc : cycle (P _inst_1.default) c, -- show that the guaranteed voter has a cyclic preference unfold cycle, use e, -- since c is a cycle in dominate, it is of length ≠ 0. have everyone_prefers : margin P x y = fintype.card V, -- we will show that the margin is |V| unfold margin, haveI _inst_4 : fintype ↥{v : V | P v y x} := subtype.fintype (λ v, P v y x), have nobody : ((finset.filter (λ (v : V), P v y x) finset.univ).card) = 0, -- first, nobody prefers y to x. rw finset.card_eq_zero, apply finset.eq_empty_iff_forall_not_mem.mpr, simp, intro x_1, have asymm := _inst_3.asymmetric, specialize asymm x_1 x y (r x_1), -- we know this because of profile asymmetry exact asymm, -- thus nobody prefers y to x simp at nobody, rw nobody, simp, -- now we need to show that the cardinality of those that prefer x to y is the cardinality of V rw (eq.symm finset.card_univ), have everyone : (finset.filter (λ (x_1 : V), P x_1 x y) finset.univ) = finset.univ, refine finset.ext_iff.mpr _, simp, exact r, rw everyone, refine list.chain.imp _ b, intros z w, rw everyone_prefers, intro b', unfold margin at b', have b' := ineq9 (fintype.card V) ((finset.filter (λ (x : V), P x z w) finset.univ).card) ((finset.filter (λ (x : V), P x w z) finset.univ).card) b', contrapose b', simp, rw finset.card_lt_iff_ne_univ, contrapose b', push_neg at b', push_neg, rw finset.eq_univ_iff_forall at b', simp at b', exact b' (default V), haveI irrefl := irreflexive_of_asymmetric P, have acyclic := preference_acyclic P (default V), have exists_cycle : ∃ (l : list X), cycle (P (default V)) l, use c, exact pc, exact acyclic exists_cycle, end
0f0d2843d1845bbdd12604f95f36972060783dee
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/inlineLCNFIssue.lean
3b2fd7df59e28468d5e16e9437608c61faffac0a
[ "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
1,024
lean
import Lean def explode (x : Nat) : IO Unit := do if x == 0 then IO.println "arg is 0" if x == 1 then IO.println "arg is 1" if x == 2 then IO.println "arg is 2" if x == 3 then IO.println "arg is 3" if x == 4 then IO.println "arg is 4" if x == 5 then IO.println "arg is 5" if x == 6 then IO.println "arg is 6" if x == 7 then IO.println "arg is 7" if x == 8 then IO.println "arg is 8" if x == 9 then IO.println "arg is 9" if x == 10 then IO.println "arg is 10" if x == 11 then IO.println "arg is 11" if x == 12 then IO.println "arg is 12" if x == 13 then IO.println "arg is 13" if x == 14 then IO.println "arg is 14" if x == 15 then IO.println "arg is 15" if x == 16 then IO.println "arg is 16" if x == 17 then IO.println "arg is 17" if x == 18 then IO.println "arg is 18" if x == 19 then IO.println "arg is 19" if x == 20 then IO.println "arg is 20" #eval Lean.Compiler.compile #[``explode] set_option trace.Compiler.result true #eval Lean.Compiler.compile #[``explode]
8e8ee406e4c79f94f3f2460ec864fd04541354ea
48f4f349e1bb919d14ab7e5921d0cfe825f4c423
/fabstract/Bauer_A_InjBaireNat/categories.lean
3b4a6bd6f58d7363ea9d1833816b48a0f1308eb0
[]
no_license
thalesant/formalabstracts-2017
fdf4ff90d30ab1dcb6d4cf16a068a997ea5ecc80
c47181342c9e41954aa8d41f5049965b5f332bca
refs/heads/master
1,584,610,453,925
1,528,277,508,000
1,528,277,508,000
136,299,625
0
0
null
null
null
null
UTF-8
Lean
false
false
1,261
lean
-- a random and incomplete selection of notions in category theory import meta_data structure category := (obj : Type) (hom : obj → obj → Type) (id : Π (A : obj), hom A A) (compose : Π {A B C : obj}, hom B C → hom A B → hom A C) (id_left : Π {A B : obj} {f : hom A B}, compose (id B) f = f) (id_right : Π {A B : obj} {f : hom A B}, compose f (id A) = f) (compose_assoc : Π {A B C D : obj} (f : hom A B) (g : hom B C) (h : hom C D), compose (compose h g) f = compose h (compose g f)) -- this is probably wrong local infix ∘ := category.compose _ definition monomorphism {C : category} {X Y : C.obj} (f : C.hom X Y) := ∀ (Z : C.obj) (g h : C.hom Z X), f ∘ g = f ∘ h → g = h structure terminal_object {C : category} := (terminal_object : C.obj) (terminal_morphism : Π (A : C.obj), C.hom A terminal_object) (hom_to_terminal : ∀ (A : C.obj) (f g : C.hom A terminal_object), f = g) -- exponentials in a category unfinished missing_exponential_structure : Type := { description := "remaining properties of exponentials" } structure exponential {C : category} (A B : C.obj) := (underlying_object : C.obj) (exponential_structure : missing_exponential_structure)
0380937647a8c521a0ffb397ed8ab376c500ce72
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/measure_theory/measurable_space.lean
930dce376bc20df2e001d59b68f3eb78faa6aae1
[ "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
48,700
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import measure_theory.measurable_space_def import measure_theory.tactic import data.tprod import data.equiv.fin /-! # Measurable spaces and measurable functions This file provides properties of measurable spaces and the functions and isomorphisms between them. The definition of a measurable space is in `measure_theory.measurable_space_def`. A measurable space is a set equipped with a σ-algebra, a collection of subsets closed under complementation and countable union. A function between measurable spaces is measurable if the preimage of each measurable subset is measurable. σ-algebras on a fixed set `α` form a complete lattice. Here we order σ-algebras by writing `m₁ ≤ m₂` if every set which is `m₁`-measurable is also `m₂`-measurable (that is, `m₁` is a subset of `m₂`). In particular, any collection of subsets of `α` generates a smallest σ-algebra which contains all of them. A function `f : α → β` induces a Galois connection between the lattices of σ-algebras on `α` and `β`. A measurable equivalence between measurable spaces is an equivalence which respects the σ-algebras, that is, for which both directions of the equivalence are measurable functions. We say that a filter `f` is measurably generated if every set `s ∈ f` includes a measurable set `t ∈ f`. This property is useful, e.g., to extract a measurable witness of `filter.eventually`. ## Notation * We write `α ≃ᵐ β` for measurable equivalences between the measurable spaces `α` and `β`. This should not be confused with `≃ₘ` which is used for diffeomorphisms between manifolds. ## Implementation notes Measurability of a function `f : α → β` between measurable spaces is defined in terms of the Galois connection induced by f. ## References * <https://en.wikipedia.org/wiki/Measurable_space> * <https://en.wikipedia.org/wiki/Sigma-algebra> * <https://en.wikipedia.org/wiki/Dynkin_system> ## Tags measurable space, σ-algebra, measurable function, measurable equivalence, dynkin system, π-λ theorem, π-system -/ open set encodable function equiv open_locale classical filter variables {α β γ δ δ' : Type*} {ι : Sort*} {s t u : set α} namespace measurable_space section functors variables {m m₁ m₂ : measurable_space α} {m' : measurable_space β} {f : α → β} {g : β → α} /-- The forward image of a measurable space under a function. `map f m` contains the sets `s : set β` whose preimage under `f` is measurable. -/ protected def map (f : α → β) (m : measurable_space α) : measurable_space β := { measurable_set' := λ s, m.measurable_set' $ f ⁻¹' s, measurable_set_empty := m.measurable_set_empty, measurable_set_compl := assume s hs, m.measurable_set_compl _ hs, measurable_set_Union := assume f hf, by { rw preimage_Union, exact m.measurable_set_Union _ hf }} @[simp] lemma map_id : m.map id = m := measurable_space.ext $ assume s, iff.rfl @[simp] lemma map_comp {f : α → β} {g : β → γ} : (m.map f).map g = m.map (g ∘ f) := measurable_space.ext $ assume s, iff.rfl /-- The reverse image of a measurable space under a function. `comap f m` contains the sets `s : set α` such that `s` is the `f`-preimage of a measurable set in `β`. -/ protected def comap (f : α → β) (m : measurable_space β) : measurable_space α := { measurable_set' := λ s, ∃s', m.measurable_set' s' ∧ f ⁻¹' s' = s, measurable_set_empty := ⟨∅, m.measurable_set_empty, rfl⟩, measurable_set_compl := assume s ⟨s', h₁, h₂⟩, ⟨s'ᶜ, m.measurable_set_compl _ h₁, h₂ ▸ rfl⟩, measurable_set_Union := assume s hs, let ⟨s', hs'⟩ := classical.axiom_of_choice hs in ⟨⋃ i, s' i, m.measurable_set_Union _ (λ i, (hs' i).left), by simp [hs'] ⟩ } @[simp] lemma comap_id : m.comap id = m := measurable_space.ext $ assume s, ⟨assume ⟨s', hs', h⟩, h ▸ hs', assume h, ⟨s, h, rfl⟩⟩ @[simp] lemma comap_comp {f : β → α} {g : γ → β} : (m.comap f).comap g = m.comap (f ∘ g) := measurable_space.ext $ assume s, ⟨assume ⟨t, ⟨u, h, hu⟩, ht⟩, ⟨u, h, ht ▸ hu ▸ rfl⟩, assume ⟨t, h, ht⟩, ⟨f ⁻¹' t, ⟨_, h, rfl⟩, ht⟩⟩ lemma comap_le_iff_le_map {f : α → β} : m'.comap f ≤ m ↔ m' ≤ m.map f := ⟨assume h s hs, h _ ⟨_, hs, rfl⟩, assume h s ⟨t, ht, heq⟩, heq ▸ h _ ht⟩ lemma gc_comap_map (f : α → β) : galois_connection (measurable_space.comap f) (measurable_space.map f) := assume f g, comap_le_iff_le_map lemma map_mono (h : m₁ ≤ m₂) : m₁.map f ≤ m₂.map f := (gc_comap_map f).monotone_u h lemma monotone_map : monotone (measurable_space.map f) := assume a b h, map_mono h lemma comap_mono (h : m₁ ≤ m₂) : m₁.comap g ≤ m₂.comap g := (gc_comap_map g).monotone_l h lemma monotone_comap : monotone (measurable_space.comap g) := assume a b h, comap_mono h @[simp] lemma comap_bot : (⊥ : measurable_space α).comap g = ⊥ := (gc_comap_map g).l_bot @[simp] lemma comap_sup : (m₁ ⊔ m₂).comap g = m₁.comap g ⊔ m₂.comap g := (gc_comap_map g).l_sup @[simp] lemma comap_supr {m : ι → measurable_space α} : (⨆i, m i).comap g = (⨆i, (m i).comap g) := (gc_comap_map g).l_supr @[simp] lemma map_top : (⊤ : measurable_space α).map f = ⊤ := (gc_comap_map f).u_top @[simp] lemma map_inf : (m₁ ⊓ m₂).map f = m₁.map f ⊓ m₂.map f := (gc_comap_map f).u_inf @[simp] lemma map_infi {m : ι → measurable_space α} : (⨅i, m i).map f = (⨅i, (m i).map f) := (gc_comap_map f).u_infi lemma comap_map_le : (m.map f).comap f ≤ m := (gc_comap_map f).l_u_le _ lemma le_map_comap : m ≤ (m.comap g).map g := (gc_comap_map g).le_u_l _ end functors lemma generate_from_le_generate_from {s t : set (set α)} (h : s ⊆ t) : generate_from s ≤ generate_from t := gi_generate_from.gc.monotone_l h lemma generate_from_sup_generate_from {s t : set (set α)} : generate_from s ⊔ generate_from t = generate_from (s ∪ t) := (@gi_generate_from α).gc.l_sup.symm lemma comap_generate_from {f : α → β} {s : set (set β)} : (generate_from s).comap f = generate_from (preimage f '' s) := le_antisymm (comap_le_iff_le_map.2 $ generate_from_le $ assume t hts, generate_measurable.basic _ $ mem_image_of_mem _ $ hts) (generate_from_le $ assume t ⟨u, hu, eq⟩, eq ▸ ⟨u, generate_measurable.basic _ hu, rfl⟩) end measurable_space section measurable_functions open measurable_space lemma measurable_iff_le_map {m₁ : measurable_space α} {m₂ : measurable_space β} {f : α → β} : measurable f ↔ m₂ ≤ m₁.map f := iff.rfl alias measurable_iff_le_map ↔ measurable.le_map measurable.of_le_map lemma measurable_iff_comap_le {m₁ : measurable_space α} {m₂ : measurable_space β} {f : α → β} : measurable f ↔ m₂.comap f ≤ m₁ := comap_le_iff_le_map.symm alias measurable_iff_comap_le ↔ measurable.comap_le measurable.of_comap_le lemma measurable.mono {ma ma' : measurable_space α} {mb mb' : measurable_space β} {f : α → β} (hf : @measurable α β ma mb f) (ha : ma ≤ ma') (hb : mb' ≤ mb) : @measurable α β ma' mb' f := λ t ht, ha _ $ hf $ hb _ ht @[measurability] lemma measurable_from_top [measurable_space β] {f : α → β} : @measurable _ _ ⊤ _ f := λ s hs, trivial lemma measurable_generate_from [measurable_space α] {s : set (set β)} {f : α → β} (h : ∀ t ∈ s, measurable_set (f ⁻¹' t)) : @measurable _ _ _ (generate_from s) f := measurable.of_le_map $ generate_from_le h variables [measurable_space α] [measurable_space β] [measurable_space γ] @[measurability] lemma measurable_set_preimage {f : α → β} {t : set β} (hf : measurable f) (ht : measurable_set t) : measurable_set (f ⁻¹' t) := hf ht @[measurability] lemma measurable.iterate {f : α → α} (hf : measurable f) : ∀ n, measurable (f^[n]) | 0 := measurable_id | (n+1) := (measurable.iterate n).comp hf @[nontriviality, measurability] lemma subsingleton.measurable [subsingleton α] {f : α → β} : measurable f := λ s hs, @subsingleton.measurable_set α _ _ _ @[measurability] lemma measurable.piecewise {s : set α} {_ : decidable_pred (∈ s)} {f g : α → β} (hs : measurable_set s) (hf : measurable f) (hg : measurable g) : measurable (piecewise s f g) := begin intros t ht, rw piecewise_preimage, exact hs.ite (hf ht) (hg ht) end /-- this is slightly different from `measurable.piecewise`. It can be used to show `measurable (ite (x=0) 0 1)` by `exact measurable.ite (measurable_set_singleton 0) measurable_const measurable_const`, but replacing `measurable.ite` by `measurable.piecewise` in that example proof does not work. -/ lemma measurable.ite {p : α → Prop} {_ : decidable_pred p} {f g : α → β} (hp : measurable_set {a : α | p a}) (hf : measurable f) (hg : measurable g) : measurable (λ x, ite (p x) (f x) (g x)) := measurable.piecewise hp hf hg @[measurability] lemma measurable.indicator [has_zero β] {s : set α} {f : α → β} (hf : measurable f) (hs : measurable_set s) : measurable (s.indicator f) := hf.piecewise hs measurable_const @[to_additive] lemma measurable_one [has_one α] : measurable (1 : β → α) := @measurable_const _ _ _ _ 1 lemma measurable_of_empty [is_empty α] (f : α → β) : measurable f := begin assume s hs, convert measurable_set.empty, exact eq_empty_of_is_empty _, end lemma measurable_of_empty_codomain [is_empty β] (f : α → β) : measurable f := by { haveI := function.is_empty f, exact measurable_of_empty f } /-- A version of `measurable_const` that assumes `f x = f y` for all `x, y`. This version works for functions between empty types. -/ lemma measurable_const' {f : β → α} (hf : ∀ x y, f x = f y) : measurable f := begin casesI is_empty_or_nonempty β, { exact measurable_of_empty f }, { convert measurable_const, exact funext (λ x, hf x h.some) } end @[to_additive, measurability] lemma measurable_set_mul_support [has_one β] [measurable_singleton_class β] {f : α → β} (hf : measurable f) : measurable_set (mul_support f) := hf (measurable_set_singleton 1).compl attribute [measurability] measurable_set_support /-- If a function coincides with a measurable function outside of a countable set, it is measurable. -/ lemma measurable.measurable_of_countable_ne [measurable_singleton_class α] {f g : α → β} (hf : measurable f) (h : countable {x | f x ≠ g x}) : measurable g := begin assume t ht, have : g ⁻¹' t = (g ⁻¹' t ∩ {x | f x = g x}ᶜ) ∪ (g ⁻¹' t ∩ {x | f x = g x}), by simp [← inter_union_distrib_left], rw this, apply measurable_set.union (h.mono (inter_subset_right _ _)).measurable_set, have : g ⁻¹' t ∩ {x : α | f x = g x} = f ⁻¹' t ∩ {x : α | f x = g x}, by { ext x, simp {contextual := tt} }, rw this, exact (hf ht).inter h.measurable_set.of_compl, end lemma measurable_of_fintype [fintype α] [measurable_singleton_class α] (f : α → β) : measurable f := λ s hs, (finite.of_fintype (f ⁻¹' s)).measurable_set end measurable_functions section constructions variables [measurable_space α] [measurable_space β] [measurable_space γ] instance : measurable_space empty := ⊤ instance : measurable_space punit := ⊤ -- this also works for `unit` instance : measurable_space bool := ⊤ instance : measurable_space ℕ := ⊤ instance : measurable_space ℤ := ⊤ instance : measurable_space ℚ := ⊤ lemma measurable_to_encodable [encodable α] {f : β → α} (h : ∀ y, measurable_set (f ⁻¹' {f y})) : measurable f := begin assume s hs, rw [← bUnion_preimage_singleton], refine measurable_set.Union (λ y, measurable_set.Union_Prop $ λ hy, _), by_cases hyf : y ∈ range f, { rcases hyf with ⟨y, rfl⟩, apply h }, { simp only [preimage_singleton_eq_empty.2 hyf, measurable_set.empty] } end @[measurability] lemma measurable_unit (f : unit → α) : measurable f := measurable_from_top section nat @[measurability] lemma measurable_from_nat {f : ℕ → α} : measurable f := measurable_from_top lemma measurable_to_nat {f : α → ℕ} : (∀ y, measurable_set (f ⁻¹' {f y})) → measurable f := measurable_to_encodable lemma measurable_find_greatest' {p : α → ℕ → Prop} {N} (hN : ∀ k ≤ N, measurable_set {x | nat.find_greatest (p x) N = k}) : measurable (λ x, nat.find_greatest (p x) N) := measurable_to_nat $ λ x, hN _ nat.find_greatest_le lemma measurable_find_greatest {p : α → ℕ → Prop} {N} (hN : ∀ k ≤ N, measurable_set {x | p x k}) : measurable (λ x, nat.find_greatest (p x) N) := begin refine measurable_find_greatest' (λ k hk, _), simp only [nat.find_greatest_eq_iff, set_of_and, set_of_forall, ← compl_set_of], repeat { apply_rules [measurable_set.inter, measurable_set.const, measurable_set.Inter, measurable_set.Inter_Prop, measurable_set.compl, hN]; try { intros } } end lemma measurable_find {p : α → ℕ → Prop} (hp : ∀ x, ∃ N, p x N) (hm : ∀ k, measurable_set {x | p x k}) : measurable (λ x, nat.find (hp x)) := begin refine measurable_to_nat (λ x, _), rw [preimage_find_eq_disjointed], exact measurable_set.disjointed hm _ end end nat section subtype instance {α} {p : α → Prop} [m : measurable_space α] : measurable_space (subtype p) := m.comap (coe : _ → α) @[measurability] lemma measurable_subtype_coe {p : α → Prop} : measurable (coe : subtype p → α) := measurable_space.le_map_comap @[measurability] lemma measurable.subtype_coe {p : β → Prop} {f : α → subtype p} (hf : measurable f) : measurable (λ a : α, (f a : β)) := measurable_subtype_coe.comp hf @[measurability] lemma measurable.subtype_mk {p : β → Prop} {f : α → β} (hf : measurable f) {h : ∀ x, p (f x)} : measurable (λ x, (⟨f x, h x⟩ : subtype p)) := λ t ⟨s, hs⟩, hs.2 ▸ by simp only [← preimage_comp, (∘), subtype.coe_mk, hf hs.1] lemma measurable_set.subtype_image {s : set α} {t : set s} (hs : measurable_set s) : measurable_set t → measurable_set ((coe : s → α) '' t) | ⟨u, (hu : measurable_set u), (eq : coe ⁻¹' u = t)⟩ := begin rw [← eq, subtype.image_preimage_coe], exact hu.inter hs end lemma measurable_of_measurable_union_cover {f : α → β} (s t : set α) (hs : measurable_set s) (ht : measurable_set t) (h : univ ⊆ s ∪ t) (hc : measurable (λ a : s, f a)) (hd : measurable (λ a : t, f a)) : measurable f := begin intros u hu, convert (hs.subtype_image (hc hu)).union (ht.subtype_image (hd hu)), change f ⁻¹' u = coe '' (coe ⁻¹' (f ⁻¹' u) : set s) ∪ coe '' (coe ⁻¹' (f ⁻¹' u) : set t), rw [image_preimage_eq_inter_range, image_preimage_eq_inter_range, subtype.range_coe, subtype.range_coe, ← inter_distrib_left, univ_subset_iff.1 h, inter_univ], end instance {α} {p : α → Prop} [measurable_space α] [measurable_singleton_class α] : measurable_singleton_class (subtype p) := { measurable_set_singleton := λ x, begin have : measurable_set {(x : α)} := measurable_set_singleton _, convert @measurable_subtype_coe α _ p _ this, ext y, simp [subtype.ext_iff], end } lemma measurable_of_measurable_on_compl_finite [measurable_singleton_class α] {f : α → β} (s : set α) (hs : finite s) (hf : measurable (set.restrict f sᶜ)) : measurable f := begin letI : fintype s := finite.fintype hs, exact measurable_of_measurable_union_cover s sᶜ hs.measurable_set hs.measurable_set.compl (by simp only [union_compl_self]) (measurable_of_fintype _) hf end lemma measurable_of_measurable_on_compl_singleton [measurable_singleton_class α] {f : α → β} (a : α) (hf : measurable (set.restrict f {x | x ≠ a})) : measurable f := measurable_of_measurable_on_compl_finite {a} (finite_singleton a) hf end subtype section prod instance {α β} [m₁ : measurable_space α] [m₂ : measurable_space β] : measurable_space (α × β) := m₁.comap prod.fst ⊔ m₂.comap prod.snd @[measurability] lemma measurable_fst : measurable (prod.fst : α × β → α) := measurable.of_comap_le le_sup_left lemma measurable.fst {f : α → β × γ} (hf : measurable f) : measurable (λ a : α, (f a).1) := measurable_fst.comp hf @[measurability] lemma measurable_snd : measurable (prod.snd : α × β → β) := measurable.of_comap_le le_sup_right lemma measurable.snd {f : α → β × γ} (hf : measurable f) : measurable (λ a : α, (f a).2) := measurable_snd.comp hf @[measurability] lemma measurable.prod {f : α → β × γ} (hf₁ : measurable (λ a, (f a).1)) (hf₂ : measurable (λ a, (f a).2)) : measurable f := measurable.of_le_map $ sup_le (by { rw [measurable_space.comap_le_iff_le_map, measurable_space.map_comp], exact hf₁ }) (by { rw [measurable_space.comap_le_iff_le_map, measurable_space.map_comp], exact hf₂ }) lemma measurable_prod {f : α → β × γ} : measurable f ↔ measurable (λ a, (f a).1) ∧ measurable (λ a, (f a).2) := ⟨λ hf, ⟨measurable_fst.comp hf, measurable_snd.comp hf⟩, λ h, measurable.prod h.1 h.2⟩ lemma measurable.prod_mk {f : α → β} {g : α → γ} (hf : measurable f) (hg : measurable g) : measurable (λ a : α, (f a, g a)) := measurable.prod hf hg lemma measurable_prod_mk_left {x : α} : measurable (@prod.mk _ β x) := measurable_const.prod_mk measurable_id lemma measurable_prod_mk_right {y : β} : measurable (λ x : α, (x, y)) := measurable_id.prod_mk measurable_const lemma measurable.prod_map [measurable_space δ] {f : α → β} {g : γ → δ} (hf : measurable f) (hg : measurable g) : measurable (prod.map f g) := (hf.comp measurable_fst).prod_mk (hg.comp measurable_snd) lemma measurable.of_uncurry_left {f : α → β → γ} (hf : measurable (uncurry f)) {x : α} : measurable (f x) := hf.comp measurable_prod_mk_left lemma measurable.of_uncurry_right {f : α → β → γ} (hf : measurable (uncurry f)) {y : β} : measurable (λ x, f x y) := hf.comp measurable_prod_mk_right @[measurability] lemma measurable_swap : measurable (prod.swap : α × β → β × α) := measurable.prod measurable_snd measurable_fst lemma measurable_swap_iff {f : α × β → γ} : measurable (f ∘ prod.swap) ↔ measurable f := ⟨λ hf, by { convert hf.comp measurable_swap, ext ⟨x, y⟩, refl }, λ hf, hf.comp measurable_swap⟩ @[measurability] lemma measurable_set.prod {s : set α} {t : set β} (hs : measurable_set s) (ht : measurable_set t) : measurable_set (s.prod t) := measurable_set.inter (measurable_fst hs) (measurable_snd ht) lemma measurable_set_prod_of_nonempty {s : set α} {t : set β} (h : (s.prod t).nonempty) : measurable_set (s.prod t) ↔ measurable_set s ∧ measurable_set t := begin rcases h with ⟨⟨x, y⟩, hx, hy⟩, refine ⟨λ hst, _, λ h, h.1.prod h.2⟩, have : measurable_set ((λ x, (x, y)) ⁻¹' s.prod t) := measurable_id.prod_mk measurable_const hst, have : measurable_set (prod.mk x ⁻¹' s.prod t) := measurable_const.prod_mk measurable_id hst, simp * at * end lemma measurable_set_prod {s : set α} {t : set β} : measurable_set (s.prod t) ↔ (measurable_set s ∧ measurable_set t) ∨ s = ∅ ∨ t = ∅ := begin cases (s.prod t).eq_empty_or_nonempty with h h, { simp [h, prod_eq_empty_iff.mp h] }, { simp [←not_nonempty_iff_eq_empty, prod_nonempty_iff.mp h, measurable_set_prod_of_nonempty h] } end lemma measurable_set_swap_iff {s : set (α × β)} : measurable_set (prod.swap ⁻¹' s) ↔ measurable_set s := ⟨λ hs, by { convert measurable_swap hs, ext ⟨x, y⟩, refl }, λ hs, measurable_swap hs⟩ lemma measurable_from_prod_encodable [encodable β] [measurable_singleton_class β] {f : α × β → γ} (hf : ∀ y, measurable (λ x, f (x, y))) : measurable f := begin intros s hs, have : f ⁻¹' s = ⋃ y, ((λ x, f (x, y)) ⁻¹' s).prod {y}, { ext1 ⟨x, y⟩, simp [and_assoc, and.left_comm] }, rw this, exact measurable_set.Union (λ y, (hf y hs).prod (measurable_set_singleton y)) end end prod section pi variables {π : δ → Type*} instance measurable_space.pi [m : Π a, measurable_space (π a)] : measurable_space (Π a, π a) := ⨆ a, (m a).comap (λ b, b a) variables [Π a, measurable_space (π a)] [measurable_space γ] lemma measurable_pi_iff {g : α → Π a, π a} : measurable g ↔ ∀ a, measurable (λ x, g x a) := by simp_rw [measurable_iff_comap_le, measurable_space.pi, measurable_space.comap_supr, measurable_space.comap_comp, function.comp, supr_le_iff] @[measurability] lemma measurable_pi_apply (a : δ) : measurable (λ f : Π a, π a, f a) := measurable.of_comap_le $ le_supr _ a @[measurability] lemma measurable.eval {a : δ} {g : α → Π a, π a} (hg : measurable g) : measurable (λ x, g x a) := (measurable_pi_apply a).comp hg @[measurability] lemma measurable_pi_lambda (f : α → Π a, π a) (hf : ∀ a, measurable (λ c, f c a)) : measurable f := measurable_pi_iff.mpr hf /-- The function `update f a : π a → Π a, π a` is always measurable. This doesn't require `f` to be measurable. This should not be confused with the statement that `update f a x` is measurable. -/ @[measurability] lemma measurable_update (f : Π (a : δ), π a) {a : δ} : measurable (update f a) := begin apply measurable_pi_lambda, intro x, by_cases hx : x = a, { cases hx, convert measurable_id, ext, simp }, simp_rw [update_noteq hx], apply measurable_const, end /- Even though we cannot use projection notation, we still keep a dot to be consistent with similar lemmas, like `measurable_set.prod`. -/ @[measurability] lemma measurable_set.pi {s : set δ} {t : Π i : δ, set (π i)} (hs : countable s) (ht : ∀ i ∈ s, measurable_set (t i)) : measurable_set (s.pi t) := by { rw [pi_def], exact measurable_set.bInter hs (λ i hi, measurable_pi_apply _ (ht i hi)) } lemma measurable_set.univ_pi [encodable δ] {t : Π i : δ, set (π i)} (ht : ∀ i, measurable_set (t i)) : measurable_set (pi univ t) := measurable_set.pi (countable_encodable _) (λ i _, ht i) lemma measurable_set_pi_of_nonempty {s : set δ} {t : Π i, set (π i)} (hs : countable s) (h : (pi s t).nonempty) : measurable_set (pi s t) ↔ ∀ i ∈ s, measurable_set (t i) := begin rcases h with ⟨f, hf⟩, refine ⟨λ hst i hi, _, measurable_set.pi hs⟩, convert measurable_update f hst, rw [update_preimage_pi hi], exact λ j hj _, hf j hj end lemma measurable_set_pi {s : set δ} {t : Π i, set (π i)} (hs : countable s) : measurable_set (pi s t) ↔ (∀ i ∈ s, measurable_set (t i)) ∨ pi s t = ∅ := begin cases (pi s t).eq_empty_or_nonempty with h h, { simp [h] }, { simp [measurable_set_pi_of_nonempty hs, h, ← not_nonempty_iff_eq_empty] } end section variable (π) @[measurability] lemma measurable_pi_equiv_pi_subtype_prod_symm (p : δ → Prop) [decidable_pred p] : measurable (equiv.pi_equiv_pi_subtype_prod p π).symm := begin apply measurable_pi_iff.2 (λ j, _), by_cases hj : p j, { simp only [hj, dif_pos, equiv.pi_equiv_pi_subtype_prod_symm_apply], have : measurable (λ (f : (Π (i : {x // p x}), π ↑i)), f ⟨j, hj⟩) := measurable_pi_apply ⟨j, hj⟩, exact measurable.comp this measurable_fst }, { simp only [hj, equiv.pi_equiv_pi_subtype_prod_symm_apply, dif_neg, not_false_iff], have : measurable (λ (f : (Π (i : {x // ¬ p x}), π ↑i)), f ⟨j, hj⟩) := measurable_pi_apply ⟨j, hj⟩, exact measurable.comp this measurable_snd } end @[measurability] lemma measurable_pi_equiv_pi_subtype_prod (p : δ → Prop) [decidable_pred p] : measurable (equiv.pi_equiv_pi_subtype_prod p π) := begin refine measurable_prod.2 _, split; { apply measurable_pi_iff.2 (λ j, _), simp only [pi_equiv_pi_subtype_prod_apply, measurable_pi_apply] } end end section fintype local attribute [instance] fintype.encodable lemma measurable_set.pi_fintype [fintype δ] {s : set δ} {t : Π i, set (π i)} (ht : ∀ i ∈ s, measurable_set (t i)) : measurable_set (pi s t) := measurable_set.pi (countable_encodable _) ht lemma measurable_set.univ_pi_fintype [fintype δ] {t : Π i, set (π i)} (ht : ∀ i, measurable_set (t i)) : measurable_set (pi univ t) := measurable_set.pi_fintype (λ i _, ht i) end fintype end pi instance tprod.measurable_space (π : δ → Type*) [∀ x, measurable_space (π x)] : ∀ (l : list δ), measurable_space (list.tprod π l) | [] := punit.measurable_space | (i :: is) := @prod.measurable_space _ _ _ (tprod.measurable_space is) section tprod open list variables {π : δ → Type*} [∀ x, measurable_space (π x)] lemma measurable_tprod_mk (l : list δ) : measurable (@tprod.mk δ π l) := begin induction l with i l ih, { exact measurable_const }, { exact (measurable_pi_apply i).prod_mk ih } end lemma measurable_tprod_elim : ∀ {l : list δ} {i : δ} (hi : i ∈ l), measurable (λ (v : tprod π l), v.elim hi) | (i :: is) j hj := begin by_cases hji : j = i, { subst hji, simp [measurable_fst] }, { rw [funext $ tprod.elim_of_ne _ hji], exact (measurable_tprod_elim (hj.resolve_left hji)).comp measurable_snd } end lemma measurable_tprod_elim' {l : list δ} (h : ∀ i, i ∈ l) : measurable (tprod.elim' h : tprod π l → Π i, π i) := measurable_pi_lambda _ (λ i, measurable_tprod_elim (h i)) lemma measurable_set.tprod (l : list δ) {s : ∀ i, set (π i)} (hs : ∀ i, measurable_set (s i)) : measurable_set (set.tprod l s) := by { induction l with i l ih, exact measurable_set.univ, exact (hs i).prod ih } end tprod instance {α β} [m₁ : measurable_space α] [m₂ : measurable_space β] : measurable_space (α ⊕ β) := m₁.map sum.inl ⊓ m₂.map sum.inr section sum @[measurability] lemma measurable_inl : measurable (@sum.inl α β) := measurable.of_le_map inf_le_left @[measurability] lemma measurable_inr : measurable (@sum.inr α β) := measurable.of_le_map inf_le_right lemma measurable_sum {f : α ⊕ β → γ} (hl : measurable (f ∘ sum.inl)) (hr : measurable (f ∘ sum.inr)) : measurable f := measurable.of_comap_le $ le_inf (measurable_space.comap_le_iff_le_map.2 $ hl) (measurable_space.comap_le_iff_le_map.2 $ hr) @[measurability] lemma measurable.sum_elim {f : α → γ} {g : β → γ} (hf : measurable f) (hg : measurable g) : measurable (sum.elim f g) := measurable_sum hf hg lemma measurable_set.inl_image {s : set α} (hs : measurable_set s) : measurable_set (sum.inl '' s : set (α ⊕ β)) := ⟨show measurable_set (sum.inl ⁻¹' _), by { rwa [preimage_image_eq], exact (λ a b, sum.inl.inj) }, have sum.inr ⁻¹' (sum.inl '' s : set (α ⊕ β)) = ∅ := eq_empty_of_subset_empty $ assume x ⟨y, hy, eq⟩, by contradiction, show measurable_set (sum.inr ⁻¹' _), by { rw [this], exact measurable_set.empty }⟩ lemma measurable_set_range_inl : measurable_set (range sum.inl : set (α ⊕ β)) := by { rw [← image_univ], exact measurable_set.univ.inl_image } lemma measurable_set_inr_image {s : set β} (hs : measurable_set s) : measurable_set (sum.inr '' s : set (α ⊕ β)) := ⟨ have sum.inl ⁻¹' (sum.inr '' s : set (α ⊕ β)) = ∅ := eq_empty_of_subset_empty $ assume x ⟨y, hy, eq⟩, by contradiction, show measurable_set (sum.inl ⁻¹' _), by { rw [this], exact measurable_set.empty }, show measurable_set (sum.inr ⁻¹' _), by { rwa [preimage_image_eq], exact λ a b, sum.inr.inj }⟩ lemma measurable_set_range_inr : measurable_set (range sum.inr : set (α ⊕ β)) := by { rw [← image_univ], exact measurable_set_inr_image measurable_set.univ } end sum instance {α} {β : α → Type*} [m : Πa, measurable_space (β a)] : measurable_space (sigma β) := ⨅a, (m a).map (sigma.mk a) end constructions /-- Equivalences between measurable spaces. Main application is the simplification of measurability statements along measurable equivalences. -/ structure measurable_equiv (α β : Type*) [measurable_space α] [measurable_space β] extends α ≃ β := (measurable_to_fun : measurable to_equiv) (measurable_inv_fun : measurable to_equiv.symm) infix ` ≃ᵐ `:25 := measurable_equiv namespace measurable_equiv variables (α β) [measurable_space α] [measurable_space β] [measurable_space γ] [measurable_space δ] instance : has_coe_to_fun (α ≃ᵐ β) (λ _, α → β) := ⟨λ e, e.to_fun⟩ variables {α β} @[simp] lemma coe_to_equiv (e : α ≃ᵐ β) : (e.to_equiv : α → β) = e := rfl @[measurability] protected lemma measurable (e : α ≃ᵐ β) : measurable (e : α → β) := e.measurable_to_fun @[simp] lemma coe_mk (e : α ≃ β) (h1 : measurable e) (h2 : measurable e.symm) : ((⟨e, h1, h2⟩ : α ≃ᵐ β) : α → β) = e := rfl /-- Any measurable space is equivalent to itself. -/ def refl (α : Type*) [measurable_space α] : α ≃ᵐ α := { to_equiv := equiv.refl α, measurable_to_fun := measurable_id, measurable_inv_fun := measurable_id } instance : inhabited (α ≃ᵐ α) := ⟨refl α⟩ /-- The composition of equivalences between measurable spaces. -/ def trans (ab : α ≃ᵐ β) (bc : β ≃ᵐ γ) : α ≃ᵐ γ := { to_equiv := ab.to_equiv.trans bc.to_equiv, measurable_to_fun := bc.measurable_to_fun.comp ab.measurable_to_fun, measurable_inv_fun := ab.measurable_inv_fun.comp bc.measurable_inv_fun } /-- The inverse of an equivalence between measurable spaces. -/ def symm (ab : α ≃ᵐ β) : β ≃ᵐ α := { to_equiv := ab.to_equiv.symm, measurable_to_fun := ab.measurable_inv_fun, measurable_inv_fun := ab.measurable_to_fun } @[simp] lemma coe_to_equiv_symm (e : α ≃ᵐ β) : (e.to_equiv.symm : β → α) = e.symm := rfl /-- See Note [custom simps projection]. We need to specify this projection explicitly in this case, because it is a composition of multiple projections. -/ def simps.apply (h : α ≃ᵐ β) : α → β := h /-- See Note [custom simps projection] -/ def simps.symm_apply (h : α ≃ᵐ β) : β → α := h.symm initialize_simps_projections measurable_equiv (to_equiv_to_fun → apply, to_equiv_inv_fun → symm_apply) lemma to_equiv_injective : injective (to_equiv : (α ≃ᵐ β) → (α ≃ β)) := by { rintro ⟨e₁, _, _⟩ ⟨e₂, _, _⟩ (rfl : e₁ = e₂), refl } @[ext] lemma ext {e₁ e₂ : α ≃ᵐ β} (h : (e₁ : α → β) = e₂) : e₁ = e₂ := to_equiv_injective $ equiv.coe_fn_injective h @[simp] lemma symm_mk (e : α ≃ β) (h1 : measurable e) (h2 : measurable e.symm) : (⟨e, h1, h2⟩ : α ≃ᵐ β).symm = ⟨e.symm, h2, h1⟩ := rfl attribute [simps apply to_equiv] trans refl @[simp] lemma symm_refl (α : Type*) [measurable_space α] : (refl α).symm = refl α := rfl @[simp] theorem symm_comp_self (e : α ≃ᵐ β) : e.symm ∘ e = id := funext e.left_inv @[simp] theorem self_comp_symm (e : α ≃ᵐ β) : e ∘ e.symm = id := funext e.right_inv @[simp] theorem apply_symm_apply (e : α ≃ᵐ β) (y : β) : e (e.symm y) = y := e.right_inv y @[simp] theorem symm_apply_apply (e : α ≃ᵐ β) (x : α) : e.symm (e x) = x := e.left_inv x @[simp] theorem symm_trans_self (e : α ≃ᵐ β) : e.symm.trans e = refl β := ext e.self_comp_symm @[simp] theorem self_trans_symm (e : α ≃ᵐ β) : e.trans e.symm = refl α := ext e.symm_comp_self protected theorem surjective (e : α ≃ᵐ β) : surjective e := e.to_equiv.surjective protected theorem bijective (e : α ≃ᵐ β) : bijective e := e.to_equiv.bijective protected theorem injective (e : α ≃ᵐ β) : injective e := e.to_equiv.injective /-- Equal measurable spaces are equivalent. -/ protected def cast {α β} [i₁ : measurable_space α] [i₂ : measurable_space β] (h : α = β) (hi : i₁ == i₂) : α ≃ᵐ β := { to_equiv := equiv.cast h, measurable_to_fun := by { substI h, substI hi, exact measurable_id }, measurable_inv_fun := by { substI h, substI hi, exact measurable_id }} protected lemma measurable_coe_iff {f : β → γ} (e : α ≃ᵐ β) : measurable (f ∘ e) ↔ measurable f := iff.intro (assume hfe, have measurable (f ∘ (e.symm.trans e).to_equiv) := hfe.comp e.symm.measurable, by rwa [coe_to_equiv, symm_trans_self] at this) (λ h, h.comp e.measurable) /-- Products of equivalent measurable spaces are equivalent. -/ def prod_congr (ab : α ≃ᵐ β) (cd : γ ≃ᵐ δ) : α × γ ≃ᵐ β × δ := { to_equiv := prod_congr ab.to_equiv cd.to_equiv, measurable_to_fun := (ab.measurable_to_fun.comp measurable_id.fst).prod_mk (cd.measurable_to_fun.comp measurable_id.snd), measurable_inv_fun := (ab.measurable_inv_fun.comp measurable_id.fst).prod_mk (cd.measurable_inv_fun.comp measurable_id.snd) } /-- Products of measurable spaces are symmetric. -/ def prod_comm : α × β ≃ᵐ β × α := { to_equiv := prod_comm α β, measurable_to_fun := measurable_id.snd.prod_mk measurable_id.fst, measurable_inv_fun := measurable_id.snd.prod_mk measurable_id.fst } /-- Products of measurable spaces are associative. -/ def prod_assoc : (α × β) × γ ≃ᵐ α × (β × γ) := { to_equiv := prod_assoc α β γ, measurable_to_fun := measurable_fst.fst.prod_mk $ measurable_fst.snd.prod_mk measurable_snd, measurable_inv_fun := (measurable_fst.prod_mk measurable_snd.fst).prod_mk measurable_snd.snd } /-- Sums of measurable spaces are symmetric. -/ def sum_congr (ab : α ≃ᵐ β) (cd : γ ≃ᵐ δ) : α ⊕ γ ≃ᵐ β ⊕ δ := { to_equiv := sum_congr ab.to_equiv cd.to_equiv, measurable_to_fun := begin cases ab with ab' abm, cases ab', cases cd with cd' cdm, cases cd', refine measurable_sum (measurable_inl.comp abm) (measurable_inr.comp cdm) end, measurable_inv_fun := begin cases ab with ab' _ abm, cases ab', cases cd with cd' _ cdm, cases cd', refine measurable_sum (measurable_inl.comp abm) (measurable_inr.comp cdm) end } /-- `set.prod s t ≃ (s × t)` as measurable spaces. -/ def set.prod (s : set α) (t : set β) : s.prod t ≃ᵐ s × t := { to_equiv := equiv.set.prod s t, measurable_to_fun := measurable_id.subtype_coe.fst.subtype_mk.prod_mk measurable_id.subtype_coe.snd.subtype_mk, measurable_inv_fun := measurable.subtype_mk $ measurable_id.fst.subtype_coe.prod_mk measurable_id.snd.subtype_coe } /-- `univ α ≃ α` as measurable spaces. -/ def set.univ (α : Type*) [measurable_space α] : (univ : set α) ≃ᵐ α := { to_equiv := equiv.set.univ α, measurable_to_fun := measurable_id.subtype_coe, measurable_inv_fun := measurable_id.subtype_mk } /-- `{a} ≃ unit` as measurable spaces. -/ def set.singleton (a : α) : ({a} : set α) ≃ᵐ unit := { to_equiv := equiv.set.singleton a, measurable_to_fun := measurable_const, measurable_inv_fun := measurable_const } /-- A set is equivalent to its image under a function `f` as measurable spaces, if `f` is an injective measurable function that sends measurable sets to measurable sets. -/ noncomputable def set.image (f : α → β) (s : set α) (hf : injective f) (hfm : measurable f) (hfi : ∀ s, measurable_set s → measurable_set (f '' s)) : s ≃ᵐ (f '' s) := { to_equiv := equiv.set.image f s hf, measurable_to_fun := (hfm.comp measurable_id.subtype_coe).subtype_mk, measurable_inv_fun := begin rintro t ⟨u, hu, rfl⟩, simp [preimage_preimage, set.image_symm_preimage hf], exact measurable_subtype_coe (hfi u hu) end } /-- The domain of `f` is equivalent to its range as measurable spaces, if `f` is an injective measurable function that sends measurable sets to measurable sets. -/ noncomputable def set.range (f : α → β) (hf : injective f) (hfm : measurable f) (hfi : ∀ s, measurable_set s → measurable_set (f '' s)) : α ≃ᵐ (range f) := (measurable_equiv.set.univ _).symm.trans $ (measurable_equiv.set.image f univ hf hfm hfi).trans $ measurable_equiv.cast (by rw image_univ) (by rw image_univ) /-- `α` is equivalent to its image in `α ⊕ β` as measurable spaces. -/ def set.range_inl : (range sum.inl : set (α ⊕ β)) ≃ᵐ α := { to_fun := λ ab, match ab with | ⟨sum.inl a, _⟩ := a | ⟨sum.inr b, p⟩ := have false, by { cases p, contradiction }, this.elim end, inv_fun := λ a, ⟨sum.inl a, a, rfl⟩, left_inv := by { rintro ⟨ab, a, rfl⟩, refl }, right_inv := assume a, rfl, measurable_to_fun := assume s (hs : measurable_set s), begin refine ⟨_, hs.inl_image, set.ext _⟩, rintros ⟨ab, a, rfl⟩, simp [set.range_inl._match_1] end, measurable_inv_fun := measurable.subtype_mk measurable_inl } /-- `β` is equivalent to its image in `α ⊕ β` as measurable spaces. -/ def set.range_inr : (range sum.inr : set (α ⊕ β)) ≃ᵐ β := { to_fun := λ ab, match ab with | ⟨sum.inr b, _⟩ := b | ⟨sum.inl a, p⟩ := have false, by { cases p, contradiction }, this.elim end, inv_fun := λ b, ⟨sum.inr b, b, rfl⟩, left_inv := by { rintro ⟨ab, b, rfl⟩, refl }, right_inv := assume b, rfl, measurable_to_fun := assume s (hs : measurable_set s), begin refine ⟨_, measurable_set_inr_image hs, set.ext _⟩, rintros ⟨ab, b, rfl⟩, simp [set.range_inr._match_1] end, measurable_inv_fun := measurable.subtype_mk measurable_inr } /-- Products distribute over sums (on the right) as measurable spaces. -/ def sum_prod_distrib (α β γ) [measurable_space α] [measurable_space β] [measurable_space γ] : (α ⊕ β) × γ ≃ᵐ (α × γ) ⊕ (β × γ) := { to_equiv := sum_prod_distrib α β γ, measurable_to_fun := begin refine measurable_of_measurable_union_cover ((range sum.inl).prod univ) ((range sum.inr).prod univ) (measurable_set_range_inl.prod measurable_set.univ) (measurable_set_range_inr.prod measurable_set.univ) (by { rintro ⟨a|b, c⟩; simp [set.prod_eq] }) _ _, { refine (set.prod (range sum.inl) univ).symm.measurable_coe_iff.1 _, refine (prod_congr set.range_inl (set.univ _)).symm.measurable_coe_iff.1 _, dsimp [(∘)], convert measurable_inl, ext ⟨a, c⟩, refl }, { refine (set.prod (range sum.inr) univ).symm.measurable_coe_iff.1 _, refine (prod_congr set.range_inr (set.univ _)).symm.measurable_coe_iff.1 _, dsimp [(∘)], convert measurable_inr, ext ⟨b, c⟩, refl } end, measurable_inv_fun := measurable_sum ((measurable_inl.comp measurable_fst).prod_mk measurable_snd) ((measurable_inr.comp measurable_fst).prod_mk measurable_snd) } /-- Products distribute over sums (on the left) as measurable spaces. -/ def prod_sum_distrib (α β γ) [measurable_space α] [measurable_space β] [measurable_space γ] : α × (β ⊕ γ) ≃ᵐ (α × β) ⊕ (α × γ) := prod_comm.trans $ (sum_prod_distrib _ _ _).trans $ sum_congr prod_comm prod_comm /-- Products distribute over sums as measurable spaces. -/ def sum_prod_sum (α β γ δ) [measurable_space α] [measurable_space β] [measurable_space γ] [measurable_space δ] : (α ⊕ β) × (γ ⊕ δ) ≃ᵐ ((α × γ) ⊕ (α × δ)) ⊕ ((β × γ) ⊕ (β × δ)) := (sum_prod_distrib _ _ _).trans $ sum_congr (prod_sum_distrib _ _ _) (prod_sum_distrib _ _ _) variables {π π' : δ' → Type*} [∀ x, measurable_space (π x)] [∀ x, measurable_space (π' x)] /-- A family of measurable equivalences `Π a, β₁ a ≃ᵐ β₂ a` generates a measurable equivalence between `Π a, β₁ a` and `Π a, β₂ a`. -/ def Pi_congr_right (e : Π a, π a ≃ᵐ π' a) : (Π a, π a) ≃ᵐ (Π a, π' a) := { to_equiv := Pi_congr_right (λ a, (e a).to_equiv), measurable_to_fun := measurable_pi_lambda _ (λ i, (e i).measurable_to_fun.comp (measurable_pi_apply i)), measurable_inv_fun := measurable_pi_lambda _ (λ i, (e i).measurable_inv_fun.comp (measurable_pi_apply i)) } /-- Pi-types are measurably equivalent to iterated products. -/ @[simps {fully_applied := ff}] noncomputable def pi_measurable_equiv_tprod {l : list δ'} (hnd : l.nodup) (h : ∀ i, i ∈ l) : (Π i, π i) ≃ᵐ list.tprod π l := { to_equiv := list.tprod.pi_equiv_tprod hnd h, measurable_to_fun := measurable_tprod_mk l, measurable_inv_fun := measurable_tprod_elim' h } /-- If `α` has a unique term, then the type of function `α → β` is measurably equivalent to `β`. -/ @[simps {fully_applied := ff}] def fun_unique (α β : Type*) [unique α] [measurable_space β] : (α → β) ≃ᵐ β := { to_equiv := equiv.fun_unique α β, measurable_to_fun := measurable_pi_apply _, measurable_inv_fun := measurable_pi_iff.2 $ λ b, measurable_id } /-- The space `Π i : fin 2, α i` is measurably equivalent to `α 0 × α 1`. -/ @[simps {fully_applied := ff}] def pi_fin_two (α : fin 2 → Type*) [∀ i, measurable_space (α i)] : (Π i, α i) ≃ᵐ α 0 × α 1 := { to_equiv := pi_fin_two_equiv α, measurable_to_fun := measurable.prod (measurable_pi_apply _) (measurable_pi_apply _), measurable_inv_fun := measurable_pi_iff.2 $ fin.forall_fin_two.2 ⟨measurable_fst, measurable_snd⟩ } /-- The space `fin 2 → α` is measurably equivalent to `α × α`. -/ @[simps {fully_applied := ff}] def fin_two_arrow : (fin 2 → α) ≃ᵐ α × α := pi_fin_two (λ _, α) end measurable_equiv namespace filter variables [measurable_space α] /-- A filter `f` is measurably generates if each `s ∈ f` includes a measurable `t ∈ f`. -/ class is_measurably_generated (f : filter α) : Prop := (exists_measurable_subset : ∀ ⦃s⦄, s ∈ f → ∃ t ∈ f, measurable_set t ∧ t ⊆ s) instance is_measurably_generated_bot : is_measurably_generated (⊥ : filter α) := ⟨λ _ _, ⟨∅, mem_bot, measurable_set.empty, empty_subset _⟩⟩ instance is_measurably_generated_top : is_measurably_generated (⊤ : filter α) := ⟨λ s hs, ⟨univ, univ_mem, measurable_set.univ, λ x _, hs x⟩⟩ lemma eventually.exists_measurable_mem {f : filter α} [is_measurably_generated f] {p : α → Prop} (h : ∀ᶠ x in f, p x) : ∃ s ∈ f, measurable_set s ∧ ∀ x ∈ s, p x := is_measurably_generated.exists_measurable_subset h lemma eventually.exists_measurable_mem_of_lift' {f : filter α} [is_measurably_generated f] {p : set α → Prop} (h : ∀ᶠ s in f.lift' powerset, p s) : ∃ s ∈ f, measurable_set s ∧ p s := let ⟨s, hsf, hs⟩ := eventually_lift'_powerset.1 h, ⟨t, htf, htm, hts⟩ := is_measurably_generated.exists_measurable_subset hsf in ⟨t, htf, htm, hs t hts⟩ instance inf_is_measurably_generated (f g : filter α) [is_measurably_generated f] [is_measurably_generated g] : is_measurably_generated (f ⊓ g) := begin refine ⟨_⟩, rintros t ⟨sf, hsf, sg, hsg, rfl⟩, rcases is_measurably_generated.exists_measurable_subset hsf with ⟨s'f, hs'f, hmf, hs'sf⟩, rcases is_measurably_generated.exists_measurable_subset hsg with ⟨s'g, hs'g, hmg, hs'sg⟩, refine ⟨s'f ∩ s'g, inter_mem_inf hs'f hs'g, hmf.inter hmg, _⟩, exact inter_subset_inter hs'sf hs'sg end lemma principal_is_measurably_generated_iff {s : set α} : is_measurably_generated (𝓟 s) ↔ measurable_set s := begin refine ⟨_, λ hs, ⟨λ t ht, ⟨s, mem_principal_self s, hs, ht⟩⟩⟩, rintros ⟨hs⟩, rcases hs (mem_principal_self s) with ⟨t, ht, htm, hts⟩, have : t = s := subset.antisymm hts ht, rwa ← this end alias principal_is_measurably_generated_iff ↔ _ measurable_set.principal_is_measurably_generated instance infi_is_measurably_generated {f : ι → filter α} [∀ i, is_measurably_generated (f i)] : is_measurably_generated (⨅ i, f i) := begin refine ⟨λ s hs, _⟩, rw [← equiv.plift.surjective.infi_comp, mem_infi] at hs, rcases hs with ⟨t, ht, ⟨V, hVf, rfl⟩⟩, choose U hUf hU using λ i, is_measurably_generated.exists_measurable_subset (hVf i), refine ⟨⋂ i : t, U i, _, _, _⟩, { rw [← equiv.plift.surjective.infi_comp, mem_infi], refine ⟨t, ht, U, hUf, rfl⟩ }, { haveI := ht.countable.to_encodable, refine measurable_set.Inter (λ i, (hU i).1) }, { exact Inter_subset_Inter (λ i, (hU i).2) } end end filter /-- We say that a collection of sets is countably spanning if a countable subset spans the whole type. This is a useful condition in various parts of measure theory. For example, it is a needed condition to show that the product of two collections generate the product sigma algebra, see `generate_from_prod_eq`. -/ def is_countably_spanning (C : set (set α)) : Prop := ∃ (s : ℕ → set α), (∀ n, s n ∈ C) ∧ (⋃ n, s n) = univ lemma is_countably_spanning_measurable_set [measurable_space α] : is_countably_spanning {s : set α | measurable_set s} := ⟨λ _, univ, λ _, measurable_set.univ, Union_const _⟩ namespace measurable_set /-! ### Typeclasses on `subtype measurable_set` -/ variables [measurable_space α] instance : has_mem α (subtype (measurable_set : set α → Prop)) := ⟨λ a s, a ∈ (s : set α)⟩ @[simp] lemma mem_coe (a : α) (s : subtype (measurable_set : set α → Prop)) : a ∈ (s : set α) ↔ a ∈ s := iff.rfl instance : has_emptyc (subtype (measurable_set : set α → Prop)) := ⟨⟨∅, measurable_set.empty⟩⟩ @[simp] lemma coe_empty : ↑(∅ : subtype (measurable_set : set α → Prop)) = (∅ : set α) := rfl instance [measurable_singleton_class α] : has_insert α (subtype (measurable_set : set α → Prop)) := ⟨λ a s, ⟨has_insert.insert a s, s.prop.insert a⟩⟩ @[simp] lemma coe_insert [measurable_singleton_class α] (a : α) (s : subtype (measurable_set : set α → Prop)) : ↑(has_insert.insert a s) = (has_insert.insert a s : set α) := rfl instance : has_compl (subtype (measurable_set : set α → Prop)) := ⟨λ x, ⟨xᶜ, x.prop.compl⟩⟩ @[simp] lemma coe_compl (s : subtype (measurable_set : set α → Prop)) : ↑(sᶜ) = (sᶜ : set α) := rfl instance : has_union (subtype (measurable_set : set α → Prop)) := ⟨λ x y, ⟨x ∪ y, x.prop.union y.prop⟩⟩ @[simp] lemma coe_union (s t : subtype (measurable_set : set α → Prop)) : ↑(s ∪ t) = (s ∪ t : set α) := rfl instance : has_inter (subtype (measurable_set : set α → Prop)) := ⟨λ x y, ⟨x ∩ y, x.prop.inter y.prop⟩⟩ @[simp] lemma coe_inter (s t : subtype (measurable_set : set α → Prop)) : ↑(s ∩ t) = (s ∩ t : set α) := rfl instance : has_sdiff (subtype (measurable_set : set α → Prop)) := ⟨λ x y, ⟨x \ y, x.prop.diff y.prop⟩⟩ @[simp] lemma coe_sdiff (s t : subtype (measurable_set : set α → Prop)) : ↑(s \ t) = (s \ t : set α) := rfl instance : has_bot (subtype (measurable_set : set α → Prop)) := ⟨⟨⊥, measurable_set.empty⟩⟩ @[simp] lemma coe_bot : ↑(⊥ : subtype (measurable_set : set α → Prop)) = (⊥ : set α) := rfl instance : has_top (subtype (measurable_set : set α → Prop)) := ⟨⟨⊤, measurable_set.univ⟩⟩ @[simp] lemma coe_top : ↑(⊤ : subtype (measurable_set : set α → Prop)) = (⊤ : set α) := rfl instance : partial_order (subtype (measurable_set : set α → Prop)) := partial_order.lift _ subtype.coe_injective instance : bounded_distrib_lattice (subtype (measurable_set : set α → Prop)) := { sup := (∪), le_sup_left := λ a b, show (a : set α) ≤ a ⊔ b, from le_sup_left, le_sup_right := λ a b, show (b : set α) ≤ a ⊔ b, from le_sup_right, sup_le := λ a b c ha hb, show (a ⊔ b : set α) ≤ c, from sup_le ha hb, inf := (∩), inf_le_left := λ a b, show (a ⊓ b : set α) ≤ a, from inf_le_left, inf_le_right := λ a b, show (a ⊓ b : set α) ≤ b, from inf_le_right, le_inf := λ a b c ha hb, show (a : set α) ≤ b ⊓ c, from le_inf ha hb, top := ⊤, le_top := λ a, show (a : set α) ≤ ⊤, from le_top, bot := ⊥, bot_le := λ a, show (⊥ : set α) ≤ a, from bot_le, le_sup_inf := λ x y z, show ((x ⊔ y) ⊓ (x ⊔ z) : set α) ≤ x ⊔ y ⊓ z, from le_sup_inf, .. measurable_set.subtype.partial_order } instance : boolean_algebra (subtype (measurable_set : set α → Prop)) := { sdiff := (\), sup_inf_sdiff := λ a b, subtype.eq $ sup_inf_sdiff a b, inf_inf_sdiff := λ a b, subtype.eq $ inf_inf_sdiff a b, compl := has_compl.compl, inf_compl_le_bot := λ a, boolean_algebra.inf_compl_le_bot (a : set α), top_le_sup_compl := λ a, boolean_algebra.top_le_sup_compl (a : set α), sdiff_eq := λ a b, subtype.eq $ sdiff_eq, .. measurable_set.subtype.bounded_distrib_lattice } end measurable_set
0fd2485c58ccc255c6bf5093c16873e9474b5e70
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/category_theory/abelian/non_preadditive.lean
457ca077e65ebf941bb4257665528338bd427b5f
[ "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
17,258
lean
/- Copyright (c) 2020 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel -/ import category_theory.limits.shapes.finite_products import category_theory.limits.shapes.kernels import category_theory.limits.shapes.normal_mono.equalizers import category_theory.abelian.images import category_theory.preadditive.basic /-! # Every non_preadditive_abelian category is preadditive In mathlib, we define an abelian category as a preadditive category with a zero object, kernels and cokernels, products and coproducts and in which every monomorphism and epimorphis is normal. While virtually every interesting abelian category has a natural preadditive structure (which is why it is included in the definition), preadditivity is not actually needed: Every category that has all of the other properties appearing in the definition of an abelian category admits a preadditive structure. This is the construction we carry out in this file. The proof proceeds in roughly five steps: 1. Prove some results (for example that all equalizers exist) that would be trivial if we already had the preadditive structure but are a bit of work without it. 2. Develop images and coimages to show that every monomorphism is the kernel of its cokernel. The results of the first two steps are also useful for the "normal" development of abelian categories, and will be used there. 3. For every object `A`, define a "subtraction" morphism `σ : A ⨯ A ⟶ A` and use it to define subtraction on morphisms as `f - g := prod.lift f g ≫ σ`. 4. Prove a small number of identities about this subtraction from the definition of `σ`. 5. From these identities, prove a large number of other identities that imply that defining `f + g := f - (0 - g)` indeed gives an abelian group structure on morphisms such that composition is bilinear. The construction is non-trivial and it is quite remarkable that this abelian group structure can be constructed purely from the existence of a few limits and colimits. Even more remarkably, since abelian categories admit exactly one preadditive structure (see `subsingleton_preadditive_of_has_binary_biproducts`), the construction manages to exactly reconstruct any natural preadditive structure the category may have. ## References * [F. Borceux, *Handbook of Categorical Algebra 2*][borceux-vol2] -/ noncomputable theory open category_theory open category_theory.limits namespace category_theory section universes v u variables (C : Type u) [category.{v} C] /-- We call a category `non_preadditive_abelian` if it has a zero object, kernels, cokernels, binary products and coproducts, and every monomorphism and every epimorphism is normal. -/ class non_preadditive_abelian extends has_zero_morphisms C, normal_mono_category C, normal_epi_category C := [has_zero_object : has_zero_object C] [has_kernels : has_kernels C] [has_cokernels : has_cokernels C] [has_finite_products : has_finite_products C] [has_finite_coproducts : has_finite_coproducts C] set_option default_priority 100 attribute [instance] non_preadditive_abelian.has_zero_object attribute [instance] non_preadditive_abelian.has_kernels attribute [instance] non_preadditive_abelian.has_cokernels attribute [instance] non_preadditive_abelian.has_finite_products attribute [instance] non_preadditive_abelian.has_finite_coproducts end end category_theory open category_theory universes v u variables {C : Type u} [category.{v} C] [non_preadditive_abelian C] namespace category_theory.non_preadditive_abelian section factor variables {P Q : C} (f : P ⟶ Q) /-- The map `p : P ⟶ image f` is an epimorphism -/ instance : epi (abelian.factor_thru_image f) := let I := abelian.image f, p := abelian.factor_thru_image f, i := kernel.ι (cokernel.π f) in -- It will suffice to consider some g : I ⟶ R such that p ≫ g = 0 and show that g = 0. normal_mono_category.epi_of_zero_cancel _ $ λ R (g : I ⟶ R) (hpg : p ≫ g = 0), begin -- Since C is abelian, u := ker g ≫ i is the kernel of some morphism h. let u := kernel.ι g ≫ i, haveI : mono u := mono_comp _ _, haveI hu := normal_mono_of_mono u, let h := hu.g, -- By hypothesis, p factors through the kernel of g via some t. obtain ⟨t, ht⟩ := kernel.lift' g p hpg, have fh : f ≫ h = 0, calc f ≫ h = (p ≫ i) ≫ h : (abelian.image.fac f).symm ▸ rfl ... = ((t ≫ kernel.ι g) ≫ i) ≫ h : ht ▸ rfl ... = t ≫ u ≫ h : by simp only [category.assoc]; conv_lhs { congr, skip, rw ←category.assoc } ... = t ≫ 0 : hu.w ▸ rfl ... = 0 : has_zero_morphisms.comp_zero _ _, -- h factors through the cokernel of f via some l. obtain ⟨l, hl⟩ := cokernel.desc' f h fh, have hih : i ≫ h = 0, calc i ≫ h = i ≫ cokernel.π f ≫ l : hl ▸ rfl ... = 0 ≫ l : by rw [←category.assoc, kernel.condition] ... = 0 : zero_comp, -- i factors through u = ker h via some s. obtain ⟨s, hs⟩ := normal_mono.lift' u i hih, have hs' : (s ≫ kernel.ι g) ≫ i = 𝟙 I ≫ i, by rw [category.assoc, hs, category.id_comp], haveI : epi (kernel.ι g) := epi_of_epi_fac ((cancel_mono _).1 hs'), -- ker g is an epimorphism, but ker g ≫ g = 0 = ker g ≫ 0, so g = 0 as required. exact zero_of_epi_comp _ (kernel.condition g) end instance is_iso_factor_thru_image [mono f] : is_iso (abelian.factor_thru_image f) := is_iso_of_mono_of_epi _ /-- The canonical morphism `i : coimage f ⟶ Q` is a monomorphism -/ instance : mono (abelian.factor_thru_coimage f) := let I := abelian.coimage f, i := abelian.factor_thru_coimage f, p := cokernel.π (kernel.ι f) in normal_epi_category.mono_of_cancel_zero _ $ λ R (g : R ⟶ I) (hgi : g ≫ i = 0), begin -- Since C is abelian, u := p ≫ coker g is the cokernel of some morphism h. let u := p ≫ cokernel.π g, haveI : epi u := epi_comp _ _, haveI hu := normal_epi_of_epi u, let h := hu.g, -- By hypothesis, i factors through the cokernel of g via some t. obtain ⟨t, ht⟩ := cokernel.desc' g i hgi, have hf : h ≫ f = 0, calc h ≫ f = h ≫ (p ≫ i) : (abelian.coimage.fac f).symm ▸ rfl ... = h ≫ (p ≫ (cokernel.π g ≫ t)) : ht ▸ rfl ... = h ≫ u ≫ t : by simp only [category.assoc]; conv_lhs { congr, skip, rw ←category.assoc } ... = 0 ≫ t : by rw [←category.assoc, hu.w] ... = 0 : zero_comp, -- h factors through the kernel of f via some l. obtain ⟨l, hl⟩ := kernel.lift' f h hf, have hhp : h ≫ p = 0, calc h ≫ p = (l ≫ kernel.ι f) ≫ p : hl ▸ rfl ... = l ≫ 0 : by rw [category.assoc, cokernel.condition] ... = 0 : comp_zero, -- p factors through u = coker h via some s. obtain ⟨s, hs⟩ := normal_epi.desc' u p hhp, have hs' : p ≫ cokernel.π g ≫ s = p ≫ 𝟙 I, by rw [←category.assoc, hs, category.comp_id], haveI : mono (cokernel.π g) := mono_of_mono_fac ((cancel_epi _).1 hs'), -- coker g is a monomorphism, but g ≫ coker g = 0 = 0 ≫ coker g, so g = 0 as required. exact zero_of_comp_mono _ (cokernel.condition g) end instance is_iso_factor_thru_coimage [epi f] : is_iso (abelian.factor_thru_coimage f) := is_iso_of_mono_of_epi _ end factor section cokernel_of_kernel variables {X Y : C} {f : X ⟶ Y} /-- In a `non_preadditive_abelian` category, an epi is the cokernel of its kernel. More precisely: If `f` is an epimorphism and `s` is some limit kernel cone on `f`, then `f` is a cokernel of `fork.ι s`. -/ def epi_is_cokernel_of_kernel [epi f] (s : fork f 0) (h : is_limit s) : is_colimit (cokernel_cofork.of_π f (kernel_fork.condition s)) := is_cokernel.cokernel_iso _ _ (cokernel.of_iso_comp _ _ (limits.is_limit.cone_point_unique_up_to_iso (limit.is_limit _) h) (cone_morphism.w (limits.is_limit.unique_up_to_iso (limit.is_limit _) h).hom _)) (as_iso $ abelian.factor_thru_coimage f) (abelian.coimage.fac f) /-- In a `non_preadditive_abelian` category, a mono is the kernel of its cokernel. More precisely: If `f` is a monomorphism and `s` is some colimit cokernel cocone on `f`, then `f` is a kernel of `cofork.π s`. -/ def mono_is_kernel_of_cokernel [mono f] (s : cofork f 0) (h : is_colimit s) : is_limit (kernel_fork.of_ι f (cokernel_cofork.condition s)) := is_kernel.iso_kernel _ _ (kernel.of_comp_iso _ _ (limits.is_colimit.cocone_point_unique_up_to_iso h (colimit.is_colimit _)) (cocone_morphism.w (limits.is_colimit.unique_up_to_iso h $ colimit.is_colimit _).hom _)) (as_iso $ abelian.factor_thru_image f) (abelian.image.fac f) end cokernel_of_kernel section /-- The composite `A ⟶ A ⨯ A ⟶ cokernel (Δ A)`, where the first map is `(𝟙 A, 0)` and the second map is the canonical projection into the cokernel. -/ abbreviation r (A : C) : A ⟶ cokernel (diag A) := prod.lift (𝟙 A) 0 ≫ cokernel.π (diag A) instance mono_Δ {A : C} : mono (diag A) := mono_of_mono_fac $ prod.lift_fst _ _ instance mono_r {A : C} : mono (r A) := begin let hl : is_limit (kernel_fork.of_ι (diag A) (cokernel.condition (diag A))), { exact mono_is_kernel_of_cokernel _ (colimit.is_colimit _) }, apply normal_epi_category.mono_of_cancel_zero, intros Z x hx, have hxx : (x ≫ prod.lift (𝟙 A) (0 : A ⟶ A)) ≫ cokernel.π (diag A) = 0, { rw [category.assoc, hx] }, obtain ⟨y, hy⟩ := kernel_fork.is_limit.lift' hl _ hxx, rw kernel_fork.ι_of_ι at hy, have hyy : y = 0, { erw [←category.comp_id y, ←limits.prod.lift_snd (𝟙 A) (𝟙 A), ←category.assoc, hy, category.assoc, prod.lift_snd, has_zero_morphisms.comp_zero] }, haveI : mono (prod.lift (𝟙 A) (0 : A ⟶ A)) := mono_of_mono_fac (prod.lift_fst _ _), apply (cancel_mono (prod.lift (𝟙 A) (0 : A ⟶ A))).1, rw [←hy, hyy, zero_comp, zero_comp] end instance epi_r {A : C} : epi (r A) := begin have hlp : prod.lift (𝟙 A) (0 : A ⟶ A) ≫ limits.prod.snd = 0 := prod.lift_snd _ _, let hp1 : is_limit (kernel_fork.of_ι (prod.lift (𝟙 A) (0 : A ⟶ A)) hlp), { refine fork.is_limit.mk _ (λ s, fork.ι s ≫ limits.prod.fst) _ _, { intro s, ext; simp, erw category.comp_id }, { intros s m h, haveI : mono (prod.lift (𝟙 A) (0 : A ⟶ A)) := mono_of_mono_fac (prod.lift_fst _ _), apply (cancel_mono (prod.lift (𝟙 A) (0 : A ⟶ A))).1, convert h, ext; simp } }, let hp2 : is_colimit (cokernel_cofork.of_π (limits.prod.snd : A ⨯ A ⟶ A) hlp), { exact epi_is_cokernel_of_kernel _ hp1 }, apply normal_mono_category.epi_of_zero_cancel, intros Z z hz, have h : prod.lift (𝟙 A) (0 : A ⟶ A) ≫ cokernel.π (diag A) ≫ z = 0, { rw [←category.assoc, hz] }, obtain ⟨t, ht⟩ := cokernel_cofork.is_colimit.desc' hp2 _ h, rw cokernel_cofork.π_of_π at ht, have htt : t = 0, { rw [←category.id_comp t], change 𝟙 A ≫ t = 0, rw [←limits.prod.lift_snd (𝟙 A) (𝟙 A), category.assoc, ht, ←category.assoc, cokernel.condition, zero_comp] }, apply (cancel_epi (cokernel.π (diag A))).1, rw [←ht, htt, comp_zero, comp_zero] end instance is_iso_r {A : C} : is_iso (r A) := is_iso_of_mono_of_epi _ /-- The composite `A ⨯ A ⟶ cokernel (diag A) ⟶ A` given by the natural projection into the cokernel followed by the inverse of `r`. In the category of modules, using the normal kernels and cokernels, this map is equal to the map `(a, b) ↦ a - b`, hence the name `σ` for "subtraction". -/ abbreviation σ {A : C} : A ⨯ A ⟶ A := cokernel.π (diag A) ≫ inv (r A) end @[simp, reassoc] lemma diag_σ {X : C} : diag X ≫ σ = 0 := by rw [cokernel.condition_assoc, zero_comp] @[simp, reassoc] lemma lift_σ {X : C} : prod.lift (𝟙 X) 0 ≫ σ = 𝟙 X := by rw [←category.assoc, is_iso.hom_inv_id] @[reassoc] lemma lift_map {X Y : C} (f : X ⟶ Y) : prod.lift (𝟙 X) 0 ≫ limits.prod.map f f = f ≫ prod.lift (𝟙 Y) 0 := by simp /-- σ is a cokernel of Δ X. -/ def is_colimit_σ {X : C} : is_colimit (cokernel_cofork.of_π σ diag_σ) := cokernel.cokernel_iso _ σ (as_iso (r X)).symm (by rw [iso.symm_hom, as_iso_inv]) /-- This is the key identity satisfied by `σ`. -/ lemma σ_comp {X Y : C} (f : X ⟶ Y) : σ ≫ f = limits.prod.map f f ≫ σ := begin obtain ⟨g, hg⟩ := cokernel_cofork.is_colimit.desc' is_colimit_σ (limits.prod.map f f ≫ σ) (by simp), suffices hfg : f = g, { rw [←hg, cofork.π_of_π, hfg] }, calc f = f ≫ prod.lift (𝟙 Y) 0 ≫ σ : by rw [lift_σ, category.comp_id] ... = prod.lift (𝟙 X) 0 ≫ limits.prod.map f f ≫ σ : by rw lift_map_assoc ... = prod.lift (𝟙 X) 0 ≫ σ ≫ g : by rw [←hg, cokernel_cofork.π_of_π] ... = g : by rw [←category.assoc, lift_σ, category.id_comp] end section /- We write `f - g` for `prod.lift f g ≫ σ`. -/ /-- Subtraction of morphisms in a `non_preadditive_abelian` category. -/ def has_sub {X Y : C} : has_sub (X ⟶ Y) := ⟨λ f g, prod.lift f g ≫ σ⟩ local attribute [instance] has_sub /- We write `-f` for `0 - f`. -/ /-- Negation of morphisms in a `non_preadditive_abelian` category. -/ def has_neg {X Y : C} : has_neg (X ⟶ Y) := ⟨λ f, 0 - f⟩ local attribute [instance] has_neg /- We write `f + g` for `f - (-g)`. -/ /-- Addition of morphisms in a `non_preadditive_abelian` category. -/ def has_add {X Y : C} : has_add (X ⟶ Y) := ⟨λ f g, f - (-g)⟩ local attribute [instance] has_add lemma sub_def {X Y : C} (a b : X ⟶ Y) : a - b = prod.lift a b ≫ σ := rfl lemma add_def {X Y : C} (a b : X ⟶ Y) : a + b = a - (-b) := rfl lemma neg_def {X Y : C} (a : X ⟶ Y) : -a = 0 - a := rfl lemma sub_zero {X Y : C} (a : X ⟶ Y) : a - 0 = a := begin rw sub_def, conv_lhs { congr, congr, rw ←category.comp_id a, skip, rw (show 0 = a ≫ (0 : Y ⟶ Y), by simp)}, rw [← prod.comp_lift, category.assoc, lift_σ, category.comp_id] end lemma sub_self {X Y : C} (a : X ⟶ Y) : a - a = 0 := by rw [sub_def, ←category.comp_id a, ← prod.comp_lift, category.assoc, diag_σ, comp_zero] lemma lift_sub_lift {X Y : C} (a b c d : X ⟶ Y) : prod.lift a b - prod.lift c d = prod.lift (a - c) (b - d) := begin simp only [sub_def], ext, { rw [category.assoc, σ_comp, prod.lift_map_assoc, prod.lift_fst, prod.lift_fst, prod.lift_fst] }, { rw [category.assoc, σ_comp, prod.lift_map_assoc, prod.lift_snd, prod.lift_snd, prod.lift_snd] } end lemma sub_sub_sub {X Y : C} (a b c d : X ⟶ Y) : (a - c) - (b - d) = (a - b) - (c - d) := begin rw [sub_def, ←lift_sub_lift, sub_def, category.assoc, σ_comp, prod.lift_map_assoc], refl end lemma neg_sub {X Y : C} (a b : X ⟶ Y) : (-a) - b = (-b) - a := by conv_lhs { rw [neg_def, ←sub_zero b, sub_sub_sub, sub_zero, ←neg_def] } lemma neg_neg {X Y : C} (a : X ⟶ Y) : -(-a) = a := begin rw [neg_def, neg_def], conv_lhs { congr, rw ←sub_self a }, rw [sub_sub_sub, sub_zero, sub_self, sub_zero] end lemma add_comm {X Y : C} (a b : X ⟶ Y) : a + b = b + a := begin rw [add_def], conv_lhs { rw ←neg_neg a }, rw [neg_def, neg_def, neg_def, sub_sub_sub], conv_lhs {congr, skip, rw [←neg_def, neg_sub] }, rw [sub_sub_sub, add_def, ←neg_def, neg_neg b, neg_def] end lemma add_neg {X Y : C} (a b : X ⟶ Y) : a + (-b) = a - b := by rw [add_def, neg_neg] lemma add_neg_self {X Y : C} (a : X ⟶ Y) : a + (-a) = 0 := by rw [add_neg, sub_self] lemma neg_add_self {X Y : C} (a : X ⟶ Y) : (-a) + a = 0 := by rw [add_comm, add_neg_self] lemma neg_sub' {X Y : C} (a b : X ⟶ Y) : -(a - b) = (-a) + b := begin rw [neg_def, neg_def], conv_lhs { rw ←sub_self (0 : X ⟶ Y) }, rw [sub_sub_sub, add_def, neg_def] end lemma neg_add {X Y : C} (a b : X ⟶ Y) : -(a + b) = (-a) - b := by rw [add_def, neg_sub', add_neg] lemma sub_add {X Y : C} (a b c : X ⟶ Y) : (a - b) + c = a - (b - c) := by rw [add_def, neg_def, sub_sub_sub, sub_zero] lemma add_assoc {X Y : C} (a b c : X ⟶ Y) : (a + b) + c = a + (b + c) := begin conv_lhs { congr, rw add_def }, rw [sub_add, ←add_neg, neg_sub', neg_neg] end lemma add_zero {X Y : C} (a : X ⟶ Y) : a + 0 = a := by rw [add_def, neg_def, sub_self, sub_zero] lemma comp_sub {X Y Z : C} (f : X ⟶ Y) (g h : Y ⟶ Z) : f ≫ (g - h) = f ≫ g - f ≫ h := by rw [sub_def, ←category.assoc, prod.comp_lift, sub_def] lemma sub_comp {X Y Z : C} (f g : X ⟶ Y) (h : Y ⟶ Z) : (f - g) ≫ h = f ≫ h - g ≫ h := by rw [sub_def, category.assoc, σ_comp, ←category.assoc, prod.lift_map, sub_def] lemma comp_add (X Y Z : C) (f : X ⟶ Y) (g h : Y ⟶ Z) : f ≫ (g + h) = f ≫ g + f ≫ h := by rw [add_def, comp_sub, neg_def, comp_sub, comp_zero, add_def, neg_def] lemma add_comp (X Y Z : C) (f g : X ⟶ Y) (h : Y ⟶ Z) : (f + g) ≫ h = f ≫ h + g ≫ h := by rw [add_def, sub_comp, neg_def, sub_comp, zero_comp, add_def, neg_def] /-- Every `non_preadditive_abelian` category is preadditive. -/ def preadditive : preadditive C := { hom_group := λ X Y, { add := (+), add_assoc := add_assoc, zero := 0, zero_add := neg_neg, add_zero := add_zero, neg := λ f, -f, add_left_neg := neg_add_self, add_comm := add_comm }, add_comp' := add_comp, comp_add' := comp_add } end end category_theory.non_preadditive_abelian
410b2aec5d5dc100ce7d3adfe2ba935f1e93637e
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/algebra/homology/homology.lean
eaedab2049c5d7aee1b0f890c1a7a57ffd6a230e
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
4,978
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Markus Himmel -/ import algebra.homology.chain_complex import category_theory.limits.shapes.images import category_theory.limits.shapes.kernels /-! # Non-functorial cohomology groups for cochain complexes We setup that part of the theory of cohomology groups which works in any category with kernels and images. We define the cohomology groups themselves, and while we can show that chain maps induce maps on the kernels, at this level of generality chain maps do not induce maps on the images, and so not on the cohomology groups. We'll do this with stronger assumptions, later. -/ universes v u namespace cochain_complex open category_theory open category_theory.limits variables {V : Type u} [𝒱 : category.{v} V] [has_zero_morphisms.{v} V] include 𝒱 variable [has_kernels.{v} V] /-- The map induced by a chain map between the kernels of the differentials. -/ def kernel_map {C C' : cochain_complex V} (f : C ⟶ C') (i : ℤ) : kernel (C.d i) ⟶ kernel (C'.d i) := kernel.lift _ (kernel.ι _ ≫ f.f i) begin rw [category.assoc, ←comm_at f, ←category.assoc, kernel.condition, has_zero_morphisms.zero_comp], end @[simp] lemma kernel_map_condition {C C' : cochain_complex V} (f : C ⟶ C') (i : ℤ) : kernel_map f i ≫ kernel.ι (C'.d i) = kernel.ι (C.d i) ≫ f.f i := by erw [limit.lift_π, fork.of_ι_app_zero] @[simp] lemma kernel_map_id (C : cochain_complex.{v} V) (i : ℤ) : kernel_map (𝟙 C) i = 𝟙 _ := (cancel_mono (kernel.ι (C.d i))).1 $ by simp @[simp] lemma kernel_map_comp {C C' C'' : cochain_complex.{v} V} (f : C ⟶ C') (g : C' ⟶ C'') (i : ℤ) : kernel_map (f ≫ g) i = kernel_map f i ≫ kernel_map g i := (cancel_mono (kernel.ι (C''.d i))).1 $ by rw [kernel_map_condition, category.assoc, kernel_map_condition, ←category.assoc, kernel_map_condition, category.assoc, differential_object.comp_f, graded_object.comp_apply] -- TODO: Actually, this is a functor `cochain_complex V ⥤ cochain_complex V`, but to state this -- properly we will need `has_shift` on `differential_object` first. /-- The kernels of the differentials of a cochain complex form a ℤ-graded object. -/ def kernel_functor : cochain_complex.{v} V ⥤ graded_object ℤ V := { obj := λ C i, kernel (C.d i), map := λ X Y f i, kernel_map f i } /-! At this point we assume that we have all images, and all equalizers. We need to assume all equalizers, not just kernels, so that `factor_thru_image` is an epimorphism. -/ variables [has_images.{v} V] [has_equalizers.{v} V] /-- The connecting morphism from the image of `d i` to the kernel of `d (i+1)`. -/ def image_to_kernel_map (C : cochain_complex V) (i : ℤ) : image (C.d i) ⟶ kernel (C.d (i+1)) := kernel.lift _ (image.ι (C.d i)) begin rw ←cancel_epi (factor_thru_image (C.d i)), rw [has_zero_morphisms.comp_zero, image.fac_assoc, d_squared], end -- TODO (a good project!): -- At this level of generality, it's just not true that a chain map -- induces maps on boundaries -- -- Let's add these later, with appropriate (but hopefully fairly minimal) -- assumptions: perhaps that the category is regular? -- I think in that case we can compute `image` as the regular coimage, -- i.e. the coequalizer of the kernel pair, -- and that image has the appropriate mapping property. -- def image_map {C C' : cochain_complex.{v} V} (f : C ⟶ C') (i : ℤ) : -- image (C.d i) ⟶ image (C'.d i) := -- sorry -- -- I'm not certain what the minimal assumptions required to prove the following -- -- lemma are: -- lemma induced_maps_commute {C C' : cochain_complex.{v} V} (f : C ⟶ C') (i : ℤ) : -- image_to_kernel_map C i ≫ kernel_map f (i+1) = -- image_map f i ≫ image_to_kernel_map C' i := -- sorry variables [has_cokernels.{v} V] /-- The `i`-th cohomology group of the cochain complex `C`. -/ def cohomology (C : cochain_complex V) (i : ℤ) : V := cokernel (image_to_kernel_map C (i-1)) -- TODO: -- As noted above, as we don't get induced maps on boundaries with this generality, -- we can't assemble the cohomology groups into a functor. Hopefully, however, -- the commented out code below will work -- (with whatever added assumptions are needed above.) -- def cohomology_map {C C' : cochain_complex.{v} V} (f : C ⟶ C') (i : ℤ) : -- C.cohomology i ⟶ C'.cohomology i := -- cokernel.desc _ (kernel_map f (i-1) ≫ cokernel.π _) -- begin -- rw [←category.assoc, induced_maps_commute, category.assoc, cokernel.condition], -- erw [has_zero_morphisms.comp_zero], -- end -- /-- The cohomology functor from chain complexes to `ℤ` graded objects in `V`. -/ -- def cohomology_functor : cochain_complex.{v} V ⥤ graded_object ℤ V := -- { obj := λ C i, cohomology C i, -- map := λ C C' f i, cohomology_map f i, -- map_id' := sorry, -- map_comp' := sorry, } end cochain_complex
0aeebc506b188f02d0f09a893641f68451c895de
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/src/Lean/Meta/Tactic/Simp.lean
1a80748a75749f65f7d50c0fea5e236844e5e6c3
[ "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
533
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.Tactic.Simp.SimpLemmas import Lean.Meta.Tactic.Simp.CongrLemmas import Lean.Meta.Tactic.Simp.Types import Lean.Meta.Tactic.Simp.Main import Lean.Meta.Tactic.Simp.Rewrite import Lean.Meta.Tactic.Simp.SimpAll namespace Lean builtin_initialize registerTraceClass `Meta.Tactic.simp builtin_initialize registerTraceClass `Debug.Meta.Tactic.simp end Lean
ac38b8c3c5be6d30f652181483ba4f4830d1a331
82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7
/src/Lean/Meta/ForEachExpr.lean
b972155116a6abbe062d4a6d758182b704b8d0f5
[ "Apache-2.0" ]
permissive
banksonian/lean4
3a2e6b0f1eb63aa56ff95b8d07b2f851072d54dc
78da6b3aa2840693eea354a41e89fc5b212a5011
refs/heads/master
1,673,703,624,165
1,605,123,551,000
1,605,123,551,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,244
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.Expr import Lean.Util.MonadCache import Lean.Meta.Basic namespace Lean.Meta namespace ForEachExpr abbrev M := MonadCacheT Expr Unit MetaM mutual private partial def visitBinder (fn : Expr → MetaM Bool) : Array Expr → Nat → Expr → M Unit | fvars, j, Expr.lam n d b c => do let d := d.instantiateRevRange j fvars.size fvars; visit fn d; withLocalDecl n c.binderInfo d fun x => visitBinder fn (fvars.push x) j b | fvars, j, Expr.forallE n d b c => do let d := d.instantiateRevRange j fvars.size fvars; visit fn d; withLocalDecl n c.binderInfo d fun x => visitBinder fn (fvars.push x) j b | fvars, j, Expr.letE n t v b _ => do let t := t.instantiateRevRange j fvars.size fvars; visit fn t; let v := v.instantiateRevRange j fvars.size fvars; visit fn v; withLetDecl n t v fun x => visitBinder fn (fvars.push x) j b | fvars, j, e => visit fn $ e.instantiateRevRange j fvars.size fvars partial def visit (fn : Expr → MetaM Bool) (e : Expr) : M Unit := checkCache e fun e => do if (← liftM (fn e)) then match e with | Expr.forallE _ _ _ _ => visitBinder fn #[] 0 e | Expr.lam _ _ _ _ => visitBinder fn #[] 0 e | Expr.letE _ _ _ _ _ => visitBinder fn #[] 0 e | Expr.app f a _ => do visit fn f; visit fn a | Expr.mdata _ b _ => visit fn b | Expr.proj _ _ b _ => visit fn b | _ => pure () end end ForEachExpr def forEachExprImp' (e : Expr) (f : Expr → MetaM Bool) : MetaM Unit := ForEachExpr.visit f e $.run /-- Similar to `Expr.forEach'`, but creates free variables whenever going inside of a binder. -/ def forEachExpr' {m} [MonadLiftT MetaM m] (e : Expr) (f : Expr → MetaM Bool) : m Unit := liftM $ forEachExprImp' e f /-- Similar to `Expr.forEach`, but creates free variables whenever going inside of a binder. -/ def forEachExpr {m} [MonadLiftT MetaM m] (e : Expr) (f : Expr → MetaM Unit) : m Unit := forEachExpr' e fun e => do f e pure true end Lean.Meta
86669238197d0c7cde65deaa17181abc4fcfcdc2
6e9cd8d58e550c481a3b45806bd34a3514c6b3e0
/src/data/complex/exponential.lean
7a24dafefb0ecbb71e4ff3fc88eeb67b7a891d7a
[ "Apache-2.0" ]
permissive
sflicht/mathlib
220fd16e463928110e7b0a50bbed7b731979407f
1b2048d7195314a7e34e06770948ee00f0ac3545
refs/heads/master
1,665,934,056,043
1,591,373,803,000
1,591,373,803,000
269,815,267
0
0
Apache-2.0
1,591,402,068,000
1,591,402,067,000
null
UTF-8
Lean
false
false
47,766
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir -/ import algebra.geom_sum import data.nat.choose import data.complex.basic local notation `abs'` := _root_.abs open is_absolute_value open_locale classical section open real is_absolute_value finset lemma forall_ge_le_of_forall_le_succ {α : Type*} [preorder α] (f : ℕ → α) {m : ℕ} (h : ∀ n ≥ m, f n.succ ≤ f n) : ∀ {l}, ∀ k ≥ m, k ≤ l → f l ≤ f k := begin assume l k hkm hkl, generalize hp : l - k = p, have : l = k + p := add_comm p k ▸ (nat.sub_eq_iff_eq_add hkl).1 hp, subst this, clear hkl hp, induction p with p ih, { simp }, { exact le_trans (h _ (le_trans hkm (nat.le_add_right _ _))) ih } end variables {α : Type*} {β : Type*} [ring β] [discrete_linear_ordered_field α] [archimedean α] {abv : β → α} [is_absolute_value abv] lemma is_cau_of_decreasing_bounded (f : ℕ → α) {a : α} {m : ℕ} (ham : ∀ n ≥ m, abs (f n) ≤ a) (hnm : ∀ n ≥ m, f n.succ ≤ f n) : is_cau_seq abs f := λ ε ε0, let ⟨k, hk⟩ := archimedean.arch a ε0 in have h : ∃ l, ∀ n ≥ m, a - l •ℕ ε < f n := ⟨k + k + 1, λ n hnm, lt_of_lt_of_le (show a - (k + (k + 1)) •ℕ ε < -abs (f n), from lt_neg.1 $ lt_of_le_of_lt (ham n hnm) (begin rw [neg_sub, lt_sub_iff_add_lt, add_nsmul], exact add_lt_add_of_le_of_lt hk (lt_of_le_of_lt hk (lt_add_of_pos_left _ ε0)), end)) (neg_le.2 $ (abs_neg (f n)) ▸ le_abs_self _)⟩, let l := nat.find h in have hl : ∀ (n : ℕ), n ≥ m → f n > a - l •ℕ ε := nat.find_spec h, have hl0 : l ≠ 0 := λ hl0, not_lt_of_ge (ham m (le_refl _)) (lt_of_lt_of_le (by have := hl m (le_refl m); simpa [hl0] using this) (le_abs_self (f m))), begin cases classical.not_forall.1 (nat.find_min h (nat.pred_lt hl0)) with i hi, rw [not_imp, not_lt] at hi, existsi i, assume j hj, have hfij : f j ≤ f i := forall_ge_le_of_forall_le_succ f hnm _ hi.1 hj, rw [abs_of_nonpos (sub_nonpos.2 hfij), neg_sub, sub_lt_iff_lt_add'], exact calc f i ≤ a - (nat.pred l) •ℕ ε : hi.2 ... = a - l •ℕ ε + ε : by conv {to_rhs, rw [← nat.succ_pred_eq_of_pos (nat.pos_of_ne_zero hl0), succ_nsmul', sub_add, add_sub_cancel] } ... < f j + ε : add_lt_add_right (hl j (le_trans hi.1 hj)) _ end lemma is_cau_of_mono_bounded (f : ℕ → α) {a : α} {m : ℕ} (ham : ∀ n ≥ m, abs (f n) ≤ a) (hnm : ∀ n ≥ m, f n ≤ f n.succ) : is_cau_seq abs f := begin refine @eq.rec_on (ℕ → α) _ (is_cau_seq abs) _ _ (-⟨_, @is_cau_of_decreasing_bounded _ _ _ (λ n, -f n) a m (by simpa) (by simpa)⟩ : cau_seq α abs).2, ext, exact neg_neg _ end lemma is_cau_series_of_abv_le_cau {f : ℕ → β} {g : ℕ → α} (n : ℕ) : (∀ m, n ≤ m → abv (f m) ≤ g m) → is_cau_seq abs (λ n, (range n).sum g) → is_cau_seq abv (λ n, (range n).sum f) := begin assume hm hg ε ε0, cases hg (ε / 2) (div_pos ε0 (by norm_num)) with i hi, existsi max n i, assume j ji, have hi₁ := hi j (le_trans (le_max_right n i) ji), have hi₂ := hi (max n i) (le_max_right n i), have sub_le := abs_sub_le ((range j).sum g) ((range i).sum g) ((range (max n i)).sum g), have := add_lt_add hi₁ hi₂, rw [abs_sub ((range (max n i)).sum g), add_halves ε] at this, refine lt_of_le_of_lt (le_trans (le_trans _ (le_abs_self _)) sub_le) this, generalize hk : j - max n i = k, clear this hi₂ hi₁ hi ε0 ε hg sub_le, rw nat.sub_eq_iff_eq_add ji at hk, rw hk, clear hk ji j, induction k with k' hi, { simp [abv_zero abv] }, { dsimp at *, simp only [nat.succ_add, sum_range_succ, sub_eq_add_neg, add_assoc], refine le_trans (abv_add _ _ _) _, exact add_le_add (hm _ (le_add_of_nonneg_of_le (nat.zero_le _) (le_max_left _ _))) hi }, end lemma is_cau_series_of_abv_cau {f : ℕ → β} : is_cau_seq abs (λ m, (range m).sum (λ n, abv (f n))) → is_cau_seq abv (λ m, (range m).sum f) := is_cau_series_of_abv_le_cau 0 (λ n h, le_refl _) lemma is_cau_geo_series {β : Type*} [field β] {abv : β → α} [is_absolute_value abv] (x : β) (hx1 : abv x < 1) : is_cau_seq abv (λ n, (range n).sum (λ m, x ^ m)) := have hx1' : abv x ≠ 1 := λ h, by simpa [h, lt_irrefl] using hx1, is_cau_series_of_abv_cau begin simp only [abv_pow abv] {eta := ff}, have : (λ (m : ℕ), (range m).sum (λ n, (abv x) ^ n)) = λ m, geom_series (abv x) m := rfl, simp only [this, geom_sum hx1'] {eta := ff}, conv in (_ / _) { rw [← neg_div_neg_eq, neg_sub, neg_sub] }, refine @is_cau_of_mono_bounded _ _ _ _ ((1 : α) / (1 - abv x)) 0 _ _, { assume n hn, rw abs_of_nonneg, refine div_le_div_of_le_of_pos (sub_le_self _ (abv_pow abv x n ▸ abv_nonneg _ _)) (sub_pos.2 hx1), refine div_nonneg (sub_nonneg.2 _) (sub_pos.2 hx1), clear hn, induction n with n ih, { simp }, { rw [_root_.pow_succ, ← one_mul (1 : α)], refine mul_le_mul (le_of_lt hx1) ih (abv_pow abv x n ▸ abv_nonneg _ _) (by norm_num) } }, { assume n hn, refine div_le_div_of_le_of_pos (sub_le_sub_left _ _) (sub_pos.2 hx1), rw [← one_mul (_ ^ n), _root_.pow_succ], exact mul_le_mul_of_nonneg_right (le_of_lt hx1) (pow_nonneg (abv_nonneg _ _) _) } end lemma is_cau_geo_series_const (a : α) {x : α} (hx1 : abs x < 1) : is_cau_seq abs (λ m, (range m).sum (λ n, a * x ^ n)) := have is_cau_seq abs (λ m, a * (range m).sum (λ n, x ^ n)) := (cau_seq.const abs a * ⟨_, is_cau_geo_series x hx1⟩).2, by simpa only [mul_sum] lemma series_ratio_test {f : ℕ → β} (n : ℕ) (r : α) (hr0 : 0 ≤ r) (hr1 : r < 1) (h : ∀ m, n ≤ m → abv (f m.succ) ≤ r * abv (f m)) : is_cau_seq abv (λ m, (range m).sum f) := have har1 : abs r < 1, by rwa abs_of_nonneg hr0, begin refine is_cau_series_of_abv_le_cau n.succ _ (is_cau_geo_series_const (abv (f n.succ) * r⁻¹ ^ n.succ) har1), assume m hmn, cases classical.em (r = 0) with r_zero r_ne_zero, { have m_pos := lt_of_lt_of_le (nat.succ_pos n) hmn, have := h m.pred (nat.le_of_succ_le_succ (by rwa [nat.succ_pred_eq_of_pos m_pos])), simpa [r_zero, nat.succ_pred_eq_of_pos m_pos, pow_succ] }, generalize hk : m - n.succ = k, have r_pos : 0 < r := lt_of_le_of_ne hr0 (ne.symm r_ne_zero), replace hk : m = k + n.succ := (nat.sub_eq_iff_eq_add hmn).1 hk, induction k with k ih generalizing m n, { rw [hk, zero_add, mul_right_comm, inv_pow' _ _, ← div_eq_mul_inv, mul_div_cancel], exact (ne_of_lt (pow_pos r_pos _)).symm }, { have kn : k + n.succ ≥ n.succ, by rw ← zero_add n.succ; exact add_le_add (zero_le _) (by simp), rw [hk, nat.succ_add, pow_succ' r, ← mul_assoc], exact le_trans (by rw mul_comm; exact h _ (nat.le_of_succ_le kn)) (mul_le_mul_of_nonneg_right (ih (k + n.succ) n h kn rfl) hr0) } end lemma sum_range_diag_flip {α : Type*} [add_comm_monoid α] (n : ℕ) (f : ℕ → ℕ → α) : (range n).sum (λ m, (range (m + 1)).sum (λ k, f k (m - k))) = (range n).sum (λ m, (range (n - m)).sum (f m)) := have h₁ : ((range n).sigma (range ∘ nat.succ)).sum (λ (a : Σ m, ℕ), f (a.2) (a.1 - a.2)) = (range n).sum (λ m, (range (m + 1)).sum (λ k, f k (m - k))) := sum_sigma, have h₂ : ((range n).sigma (λ m, range (n - m))).sum (λ a : Σ (m : ℕ), ℕ, f (a.1) (a.2)) = (range n).sum (λ m, (range (n - m)).sum (f m)) := sum_sigma, h₁ ▸ h₂ ▸ sum_bij (λ a _, ⟨a.2, a.1 - a.2⟩) (λ a ha, have h₁ : a.1 < n := mem_range.1 (mem_sigma.1 ha).1, have h₂ : a.2 < nat.succ a.1 := mem_range.1 (mem_sigma.1 ha).2, mem_sigma.2 ⟨mem_range.2 (lt_of_lt_of_le h₂ h₁), mem_range.2 ((nat.sub_lt_sub_right_iff (nat.le_of_lt_succ h₂)).2 h₁)⟩) (λ _ _, rfl) (λ ⟨a₁, a₂⟩ ⟨b₁, b₂⟩ ha hb h, have ha : a₁ < n ∧ a₂ ≤ a₁ := ⟨mem_range.1 (mem_sigma.1 ha).1, nat.le_of_lt_succ (mem_range.1 (mem_sigma.1 ha).2)⟩, have hb : b₁ < n ∧ b₂ ≤ b₁ := ⟨mem_range.1 (mem_sigma.1 hb).1, nat.le_of_lt_succ (mem_range.1 (mem_sigma.1 hb).2)⟩, have h : a₂ = b₂ ∧ _ := sigma.mk.inj h, have h' : a₁ = b₁ - b₂ + a₂ := (nat.sub_eq_iff_eq_add ha.2).1 (eq_of_heq h.2), sigma.mk.inj_iff.2 ⟨nat.sub_add_cancel hb.2 ▸ h'.symm ▸ h.1 ▸ rfl, (heq_of_eq h.1)⟩) (λ ⟨a₁, a₂⟩ ha, have ha : a₁ < n ∧ a₂ < n - a₁ := ⟨mem_range.1 (mem_sigma.1 ha).1, (mem_range.1 (mem_sigma.1 ha).2)⟩, ⟨⟨a₂ + a₁, a₁⟩, ⟨mem_sigma.2 ⟨mem_range.2 (nat.lt_sub_right_iff_add_lt.1 ha.2), mem_range.2 (nat.lt_succ_of_le (nat.le_add_left _ _))⟩, sigma.mk.inj_iff.2 ⟨rfl, heq_of_eq (nat.add_sub_cancel _ _).symm⟩⟩⟩) lemma abv_sum_le_sum_abv {γ : Type*} (f : γ → β) (s : finset γ) : abv (s.sum f) ≤ s.sum (abv ∘ f) := by haveI := classical.dec_eq γ; exact finset.induction_on s (by simp [abv_zero abv]) (λ a s has ih, by rw [sum_insert has, sum_insert has]; exact le_trans (abv_add abv _ _) (add_le_add_left ih _)) lemma sum_range_sub_sum_range {α : Type*} [add_comm_group α] {f : ℕ → α} {n m : ℕ} (hnm : n ≤ m) : (range m).sum f - (range n).sum f = ((range m).filter (λ k, n ≤ k)).sum f := begin rw [← sum_sdiff (@filter_subset _ (λ k, n ≤ k) _ (range m)), sub_eq_iff_eq_add, ← eq_sub_iff_add_eq, add_sub_cancel'], refine finset.sum_congr (finset.ext.2 $ λ a, ⟨λ h, by simp at *; finish, λ h, have ham : a < m := lt_of_lt_of_le (mem_range.1 h) hnm, by simp * at *⟩) (λ _ _, rfl), end @[nolint ge_or_gt] -- see Note [nolint_ge] lemma cauchy_product {a b : ℕ → β} (ha : is_cau_seq abs (λ m, (range m).sum (λ n, abv (a n)))) (hb : is_cau_seq abv (λ m, (range m).sum b)) (ε : α) (ε0 : 0 < ε) : ∃ i : ℕ, ∀ j ≥ i, abv ((range j).sum a * (range j).sum b - (range j).sum (λ n, (range (n + 1)).sum (λ m, a m * b (n - m)))) < ε := let ⟨Q, hQ⟩ := cau_seq.bounded ⟨_, hb⟩ in let ⟨P, hP⟩ := cau_seq.bounded ⟨_, ha⟩ in have hP0 : 0 < P, from lt_of_le_of_lt (abs_nonneg _) (hP 0), have hPε0 : 0 < ε / (2 * P), from div_pos ε0 (mul_pos (show (2 : α) > 0, from by norm_num) hP0), let ⟨N, hN⟩ := cau_seq.cauchy₂ ⟨_, hb⟩ hPε0 in have hQε0 : 0 < ε / (4 * Q), from div_pos ε0 (mul_pos (show (0 : α) < 4, by norm_num) (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0))), let ⟨M, hM⟩ := cau_seq.cauchy₂ ⟨_, ha⟩ hQε0 in ⟨2 * (max N M + 1), λ K hK, have h₁ : (range K).sum (λ m, (range (m + 1)).sum (λ k, a k * b (m - k))) = (range K).sum (λ m, (range (K - m)).sum (λ n, a m * b n)), by simpa using sum_range_diag_flip K (λ m n, a m * b n), have h₂ : (λ i, (range (K - i)).sum (λ k, a i * b k)) = (λ i, a i * (range (K - i)).sum b), by simp [finset.mul_sum], have h₃ : (range K).sum (λ i, a i * (range (K - i)).sum b) = (range K).sum (λ i, a i * ((range (K - i)).sum b - (range K).sum b)) + (range K).sum (λ i, a i * (range K).sum b), by rw ← sum_add_distrib; simp [(mul_add _ _ _).symm], have two_mul_two : (4 : α) = 2 * 2, by norm_num, have hQ0 : Q ≠ 0, from λ h, by simpa [h, lt_irrefl] using hQε0, have h2Q0 : 2 * Q ≠ 0, from mul_ne_zero two_ne_zero hQ0, have hε : ε / (2 * P) * P + ε / (4 * Q) * (2 * Q) = ε, by rw [← div_div_eq_div_mul, div_mul_cancel _ (ne.symm (ne_of_lt hP0)), two_mul_two, mul_assoc, ← div_div_eq_div_mul, div_mul_cancel _ h2Q0, add_halves], have hNMK : max N M + 1 < K, from lt_of_lt_of_le (by rw two_mul; exact lt_add_of_pos_left _ (nat.succ_pos _)) hK, have hKN : N < K, from calc N ≤ max N M : le_max_left _ _ ... < max N M + 1 : nat.lt_succ_self _ ... < K : hNMK, have hsumlesum : (range (max N M + 1)).sum (λ i, abv (a i) * abv ((range (K - i)).sum b - (range K).sum b)) ≤ (range (max N M + 1)).sum (λ i, abv (a i) * (ε / (2 * P))), from sum_le_sum (λ m hmJ, mul_le_mul_of_nonneg_left (le_of_lt (hN (K - m) K (nat.le_sub_left_of_add_le (le_trans (by rw two_mul; exact add_le_add (le_of_lt (mem_range.1 hmJ)) (le_trans (le_max_left _ _) (le_of_lt (lt_add_one _)))) hK)) (le_of_lt hKN))) (abv_nonneg abv _)), have hsumltP : (range (max N M + 1)).sum (λ n, abv (a n)) < P := calc (range (max N M + 1)).sum (λ n, abv (a n)) = abs ((range (max N M + 1)).sum (λ n, abv (a n))) : eq.symm (abs_of_nonneg (sum_nonneg (λ x h, abv_nonneg abv (a x)))) ... < P : hP (max N M + 1), begin rw [h₁, h₂, h₃, sum_mul, ← sub_sub, sub_right_comm, sub_self, zero_sub, abv_neg abv], refine lt_of_le_of_lt (abv_sum_le_sum_abv _ _) _, suffices : (range (max N M + 1)).sum (λ (i : ℕ), abv (a i) * abv ((range (K - i)).sum b - (range K).sum b)) + ((range K).sum (λ (i : ℕ), abv (a i) * abv ((range (K - i)).sum b - (range K).sum b)) -(range (max N M + 1)).sum (λ (i : ℕ), abv (a i) * abv ((range (K - i)).sum b - (range K).sum b))) < ε / (2 * P) * P + ε / (4 * Q) * (2 * Q), { rw hε at this, simpa [abv_mul abv] }, refine add_lt_add (lt_of_le_of_lt hsumlesum (by rw [← sum_mul, mul_comm]; exact (mul_lt_mul_left hPε0).mpr hsumltP)) _, rw sum_range_sub_sum_range (le_of_lt hNMK), exact calc ((range K).filter (λ k, max N M + 1 ≤ k)).sum (λ i, abv (a i) * abv ((range (K - i)).sum b - (range K).sum b)) ≤ ((range K).filter (λ k, max N M + 1 ≤ k)).sum (λ i, abv (a i) * (2 * Q)) : sum_le_sum (λ n hn, begin refine mul_le_mul_of_nonneg_left _ (abv_nonneg _ _), rw sub_eq_add_neg, refine le_trans (abv_add _ _ _) _, rw [two_mul, abv_neg abv], exact add_le_add (le_of_lt (hQ _)) (le_of_lt (hQ _)), end) ... < ε / (4 * Q) * (2 * Q) : by rw [← sum_mul, ← sum_range_sub_sum_range (le_of_lt hNMK)]; refine (mul_lt_mul_right $ by rw two_mul; exact add_pos (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0)) (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0))).2 (lt_of_le_of_lt (le_abs_self _) (hM _ _ (le_trans (nat.le_succ_of_le (le_max_right _ _)) (le_of_lt hNMK)) (nat.le_succ_of_le (le_max_right _ _)))) end⟩ end open finset open cau_seq namespace complex lemma is_cau_abs_exp (z : ℂ) : is_cau_seq _root_.abs (λ n, (range n).sum (λ m, abs (z ^ m / nat.fact m))) := let ⟨n, hn⟩ := exists_nat_gt (abs z) in have hn0 : (0 : ℝ) < n, from lt_of_le_of_lt (abs_nonneg _) hn, series_ratio_test n (complex.abs z / n) (div_nonneg_of_nonneg_of_pos (complex.abs_nonneg _) hn0) (by rwa [div_lt_iff hn0, one_mul]) (λ m hm, by rw [abs_abs, abs_abs, nat.fact_succ, pow_succ, mul_comm m.succ, nat.cast_mul, ← div_div_eq_div_mul, mul_div_assoc, mul_div_right_comm, abs_mul, abs_div, abs_cast_nat]; exact mul_le_mul_of_nonneg_right (div_le_div_of_le_left (abs_nonneg _) hn0 (nat.cast_le.2 (le_trans hm (nat.le_succ _)))) (abs_nonneg _)) noncomputable theory lemma is_cau_exp (z : ℂ) : is_cau_seq abs (λ n, (range n).sum (λ m, z ^ m / nat.fact m)) := is_cau_series_of_abv_cau (is_cau_abs_exp z) def exp' (z : ℂ) : cau_seq ℂ complex.abs := ⟨λ n, (range n).sum (λ m, z ^ m / nat.fact m), is_cau_exp z⟩ def exp (z : ℂ) : ℂ := lim (exp' z) def sin (z : ℂ) : ℂ := ((exp (-z * I) - exp (z * I)) * I) / 2 def cos (z : ℂ) : ℂ := (exp (z * I) + exp (-z * I)) / 2 def tan (z : ℂ) : ℂ := sin z / cos z def sinh (z : ℂ) : ℂ := (exp z - exp (-z)) / 2 def cosh (z : ℂ) : ℂ := (exp z + exp (-z)) / 2 def tanh (z : ℂ) : ℂ := sinh z / cosh z end complex namespace real open complex def exp (x : ℝ) : ℝ := (exp x).re def sin (x : ℝ) : ℝ := (sin x).re def cos (x : ℝ) : ℝ := (cos x).re def tan (x : ℝ) : ℝ := (tan x).re def sinh (x : ℝ) : ℝ := (sinh x).re def cosh (x : ℝ) : ℝ := (cosh x).re def tanh (x : ℝ) : ℝ := (tanh x).re end real namespace complex variables (x y : ℂ) @[simp] lemma exp_zero : exp 0 = 1 := lim_eq_of_equiv_const $ λ ε ε0, ⟨1, λ j hj, begin convert ε0, cases j, { exact absurd hj (not_le_of_gt zero_lt_one) }, { dsimp [exp'], induction j with j ih, { dsimp [exp']; simp }, { rw ← ih dec_trivial, simp only [sum_range_succ, pow_succ], simp } } end⟩ lemma exp_add : exp (x + y) = exp x * exp y := show lim (⟨_, is_cau_exp (x + y)⟩ : cau_seq ℂ abs) = lim (show cau_seq ℂ abs, from ⟨_, is_cau_exp x⟩) * lim (show cau_seq ℂ abs, from ⟨_, is_cau_exp y⟩), from have hj : ∀ j : ℕ, (range j).sum (λ m, (x + y) ^ m / m.fact) = (range j).sum (λ i, (range (i + 1)).sum (λ k, x ^ k / k.fact * (y ^ (i - k) / (i - k).fact))), from assume j, finset.sum_congr rfl (λ m hm, begin rw [add_pow, div_eq_mul_inv, sum_mul], refine finset.sum_congr rfl (λ i hi, _), have h₁ : (nat.choose m i : ℂ) ≠ 0 := nat.cast_ne_zero.2 (nat.pos_iff_ne_zero.1 (nat.choose_pos (nat.le_of_lt_succ (mem_range.1 hi)))), have h₂ := nat.choose_mul_fact_mul_fact (nat.le_of_lt_succ $ finset.mem_range.1 hi), rw [← h₂, nat.cast_mul, nat.cast_mul, mul_inv', mul_inv'], simp only [mul_left_comm (nat.choose m i : ℂ), mul_assoc, mul_left_comm (nat.choose m i : ℂ)⁻¹, mul_comm (nat.choose m i : ℂ)], rw inv_mul_cancel h₁, simp [div_eq_mul_inv, mul_comm, mul_assoc, mul_left_comm] end), by rw lim_mul_lim; exact eq.symm (lim_eq_lim_of_equiv (by dsimp; simp only [hj]; exact cauchy_product (is_cau_abs_exp x) (is_cau_exp y))) attribute [irreducible] complex.exp lemma exp_list_sum (l : list ℂ) : exp l.sum = (l.map exp).prod := @monoid_hom.map_list_prod (multiplicative ℂ) ℂ _ _ ⟨exp, exp_zero, exp_add⟩ l lemma exp_multiset_sum (s : multiset ℂ) : exp s.sum = (s.map exp).prod := @monoid_hom.map_multiset_prod (multiplicative ℂ) ℂ _ _ ⟨exp, exp_zero, exp_add⟩ s lemma exp_sum {α : Type*} (s : finset α) (f : α → ℂ) : exp (s.sum f) = s.prod (exp ∘ f) := @monoid_hom.map_prod α (multiplicative ℂ) ℂ _ _ ⟨exp, exp_zero, exp_add⟩ f s lemma exp_nat_mul (x : ℂ) : ∀ n : ℕ, exp(n*x) = (exp x)^n | 0 := by rw [nat.cast_zero, zero_mul, exp_zero, pow_zero] | (nat.succ n) := by rw [pow_succ', nat.cast_add_one, add_mul, exp_add, ←exp_nat_mul, one_mul] lemma exp_ne_zero : exp x ≠ 0 := λ h, zero_ne_one $ by rw [← exp_zero, ← add_neg_self x, exp_add, h]; simp lemma exp_neg : exp (-x) = (exp x)⁻¹ := by rw [← domain.mul_right_inj (exp_ne_zero x), ← exp_add]; simp [mul_inv_cancel (exp_ne_zero x)] lemma exp_sub : exp (x - y) = exp x / exp y := by simp [sub_eq_add_neg, exp_add, exp_neg, div_eq_mul_inv] @[simp] lemma exp_conj : exp (conj x) = conj (exp x) := begin dsimp [exp], rw [← lim_conj], refine congr_arg lim (cau_seq.ext (λ _, _)), dsimp [exp', function.comp, cau_seq_conj], rw ← sum_hom _ conj, refine sum_congr rfl (λ n hn, _), rw [conj_div, conj_pow, ← of_real_nat_cast, conj_of_real] end @[simp] lemma of_real_exp_of_real_re (x : ℝ) : ((exp x).re : ℂ) = exp x := eq_conj_iff_re.1 $ by rw [← exp_conj, conj_of_real] @[simp] lemma of_real_exp (x : ℝ) : (real.exp x : ℂ) = exp x := of_real_exp_of_real_re _ @[simp] lemma exp_of_real_im (x : ℝ) : (exp x).im = 0 := by rw [← of_real_exp_of_real_re, of_real_im] lemma exp_of_real_re (x : ℝ) : (exp x).re = real.exp x := rfl lemma two_sinh : 2 * sinh x = exp x - exp (-x) := mul_div_cancel' _ two_ne_zero' lemma two_cosh : 2 * cosh x = exp x + exp (-x) := mul_div_cancel' _ two_ne_zero' @[simp] lemma sinh_zero : sinh 0 = 0 := by simp [sinh] @[simp] lemma sinh_neg : sinh (-x) = -sinh x := by simp [sinh, exp_neg, (neg_div _ _).symm, add_mul] private lemma sinh_add_aux {a b c d : ℂ} : (a - b) * (c + d) + (a + b) * (c - d) = 2 * (a * c - b * d) := by ring lemma sinh_add : sinh (x + y) = sinh x * cosh y + cosh x * sinh y := begin rw [← domain.mul_right_inj (@two_ne_zero' ℂ _ _ _), two_sinh, exp_add, neg_add, exp_add, eq_comm, mul_add, ← mul_assoc, two_sinh, mul_left_comm, two_sinh, ← domain.mul_right_inj (@two_ne_zero' ℂ _ _ _), mul_add, mul_left_comm, two_cosh, ← mul_assoc, two_cosh], exact sinh_add_aux end @[simp] lemma cosh_zero : cosh 0 = 1 := by simp [cosh] @[simp] lemma cosh_neg : cosh (-x) = cosh x := by simp [add_comm, cosh, exp_neg] private lemma cosh_add_aux {a b c d : ℂ} : (a + b) * (c + d) + (a - b) * (c - d) = 2 * (a * c + b * d) := by ring lemma cosh_add : cosh (x + y) = cosh x * cosh y + sinh x * sinh y := begin rw [← domain.mul_right_inj (@two_ne_zero' ℂ _ _ _), two_cosh, exp_add, neg_add, exp_add, eq_comm, mul_add, ← mul_assoc, two_cosh, ← mul_assoc, two_sinh, ← domain.mul_right_inj (@two_ne_zero' ℂ _ _ _), mul_add, mul_left_comm, two_cosh, mul_left_comm, two_sinh], exact cosh_add_aux end lemma sinh_sub : sinh (x - y) = sinh x * cosh y - cosh x * sinh y := by simp [sub_eq_add_neg, sinh_add, sinh_neg, cosh_neg] lemma cosh_sub : cosh (x - y) = cosh x * cosh y - sinh x * sinh y := by simp [sub_eq_add_neg, cosh_add, sinh_neg, cosh_neg] lemma sinh_conj : sinh (conj x) = conj (sinh x) := by rw [sinh, ← conj_neg, exp_conj, exp_conj, ← conj_sub, sinh, conj_div, conj_two] @[simp] lemma of_real_sinh_of_real_re (x : ℝ) : ((sinh x).re : ℂ) = sinh x := eq_conj_iff_re.1 $ by rw [← sinh_conj, conj_of_real] @[simp] lemma of_real_sinh (x : ℝ) : (real.sinh x : ℂ) = sinh x := of_real_sinh_of_real_re _ @[simp] lemma sinh_of_real_im (x : ℝ) : (sinh x).im = 0 := by rw [← of_real_sinh_of_real_re, of_real_im] lemma sinh_of_real_re (x : ℝ) : (sinh x).re = real.sinh x := rfl lemma cosh_conj : cosh (conj x) = conj (cosh x) := by rw [cosh, ← conj_neg, exp_conj, exp_conj, ← conj_add, cosh, conj_div, conj_two] @[simp] lemma of_real_cosh_of_real_re (x : ℝ) : ((cosh x).re : ℂ) = cosh x := eq_conj_iff_re.1 $ by rw [← cosh_conj, conj_of_real] @[simp] lemma of_real_cosh (x : ℝ) : (real.cosh x : ℂ) = cosh x := of_real_cosh_of_real_re _ @[simp] lemma cosh_of_real_im (x : ℝ) : (cosh x).im = 0 := by rw [← of_real_cosh_of_real_re, of_real_im] lemma cosh_of_real_re (x : ℝ) : (cosh x).re = real.cosh x := rfl lemma tanh_eq_sinh_div_cosh : tanh x = sinh x / cosh x := rfl @[simp] lemma tanh_zero : tanh 0 = 0 := by simp [tanh] @[simp] lemma tanh_neg : tanh (-x) = -tanh x := by simp [tanh, neg_div] lemma tanh_conj : tanh (conj x) = conj (tanh x) := by rw [tanh, sinh_conj, cosh_conj, ← conj_div, tanh] @[simp] lemma of_real_tanh_of_real_re (x : ℝ) : ((tanh x).re : ℂ) = tanh x := eq_conj_iff_re.1 $ by rw [← tanh_conj, conj_of_real] @[simp] lemma of_real_tanh (x : ℝ) : (real.tanh x : ℂ) = tanh x := of_real_tanh_of_real_re _ @[simp] lemma tanh_of_real_im (x : ℝ) : (tanh x).im = 0 := by rw [← of_real_tanh_of_real_re, of_real_im] lemma tanh_of_real_re (x : ℝ) : (tanh x).re = real.tanh x := rfl lemma cosh_add_sinh : cosh x + sinh x = exp x := by rw [← domain.mul_right_inj (@two_ne_zero' ℂ _ _ _), mul_add, two_cosh, two_sinh, add_add_sub_cancel, two_mul] lemma sinh_add_cosh : sinh x + cosh x = exp x := by rw [add_comm, cosh_add_sinh] lemma cosh_sub_sinh : cosh x - sinh x = exp (-x) := by rw [← domain.mul_right_inj (@two_ne_zero' ℂ _ _ _), mul_sub, two_cosh, two_sinh, add_sub_sub_cancel, two_mul] lemma cosh_sq_sub_sinh_sq : cosh x ^ 2 - sinh x ^ 2 = 1 := by rw [sq_sub_sq, cosh_add_sinh, cosh_sub_sinh, ← exp_add, add_neg_self, exp_zero] @[simp] lemma sin_zero : sin 0 = 0 := by simp [sin] @[simp] lemma sin_neg : sin (-x) = -sin x := by simp [sin, sub_eq_add_neg, exp_neg, (neg_div _ _).symm, add_mul] lemma two_sin : 2 * sin x = (exp (-x * I) - exp (x * I)) * I := mul_div_cancel' _ two_ne_zero' lemma two_cos : 2 * cos x = exp (x * I) + exp (-x * I) := mul_div_cancel' _ two_ne_zero' lemma sinh_mul_I : sinh (x * I) = sin x * I := by rw [← domain.mul_right_inj (@two_ne_zero' ℂ _ _ _), two_sinh, ← mul_assoc, two_sin, mul_assoc, I_mul_I, mul_neg_one, neg_sub, neg_mul_eq_neg_mul] lemma cosh_mul_I : cosh (x * I) = cos x := by rw [← domain.mul_right_inj (@two_ne_zero' ℂ _ _ _), two_cosh, two_cos, neg_mul_eq_neg_mul] lemma sin_add : sin (x + y) = sin x * cos y + cos x * sin y := by rw [← domain.mul_left_inj I_ne_zero, ← sinh_mul_I, add_mul, add_mul, mul_right_comm, ← sinh_mul_I, mul_assoc, ← sinh_mul_I, ← cosh_mul_I, ← cosh_mul_I, sinh_add] @[simp] lemma cos_zero : cos 0 = 1 := by simp [cos] @[simp] lemma cos_neg : cos (-x) = cos x := by simp [cos, sub_eq_add_neg, exp_neg, add_comm] private lemma cos_add_aux {a b c d : ℂ} : (a + b) * (c + d) - (b - a) * (d - c) * (-1) = 2 * (a * c + b * d) := by ring lemma cos_add : cos (x + y) = cos x * cos y - sin x * sin y := by rw [← cosh_mul_I, add_mul, cosh_add, cosh_mul_I, cosh_mul_I, sinh_mul_I, sinh_mul_I, mul_mul_mul_comm, I_mul_I, mul_neg_one, sub_eq_add_neg] lemma sin_sub : sin (x - y) = sin x * cos y - cos x * sin y := by simp [sub_eq_add_neg, sin_add, sin_neg, cos_neg] lemma cos_sub : cos (x - y) = cos x * cos y + sin x * sin y := by simp [sub_eq_add_neg, cos_add, sin_neg, cos_neg] lemma sin_conj : sin (conj x) = conj (sin x) := by rw [← domain.mul_left_inj I_ne_zero, ← sinh_mul_I, ← conj_neg_I, ← conj_mul, ← conj_mul, sinh_conj, mul_neg_eq_neg_mul_symm, sinh_neg, sinh_mul_I, mul_neg_eq_neg_mul_symm] @[simp] lemma of_real_sin_of_real_re (x : ℝ) : ((sin x).re : ℂ) = sin x := eq_conj_iff_re.1 $ by rw [← sin_conj, conj_of_real] @[simp] lemma of_real_sin (x : ℝ) : (real.sin x : ℂ) = sin x := of_real_sin_of_real_re _ @[simp] lemma sin_of_real_im (x : ℝ) : (sin x).im = 0 := by rw [← of_real_sin_of_real_re, of_real_im] lemma sin_of_real_re (x : ℝ) : (sin x).re = real.sin x := rfl lemma cos_conj : cos (conj x) = conj (cos x) := by rw [← cosh_mul_I, ← conj_neg_I, ← conj_mul, ← cosh_mul_I, cosh_conj, mul_neg_eq_neg_mul_symm, cosh_neg] @[simp] lemma of_real_cos_of_real_re (x : ℝ) : ((cos x).re : ℂ) = cos x := eq_conj_iff_re.1 $ by rw [← cos_conj, conj_of_real] @[simp] lemma of_real_cos (x : ℝ) : (real.cos x : ℂ) = cos x := of_real_cos_of_real_re _ @[simp] lemma cos_of_real_im (x : ℝ) : (cos x).im = 0 := by rw [← of_real_cos_of_real_re, of_real_im] lemma cos_of_real_re (x : ℝ) : (cos x).re = real.cos x := rfl @[simp] lemma tan_zero : tan 0 = 0 := by simp [tan] lemma tan_eq_sin_div_cos : tan x = sin x / cos x := rfl @[simp] lemma tan_neg : tan (-x) = -tan x := by simp [tan, neg_div] lemma tan_conj : tan (conj x) = conj (tan x) := by rw [tan, sin_conj, cos_conj, ← conj_div, tan] @[simp] lemma of_real_tan_of_real_re (x : ℝ) : ((tan x).re : ℂ) = tan x := eq_conj_iff_re.1 $ by rw [← tan_conj, conj_of_real] @[simp] lemma of_real_tan (x : ℝ) : (real.tan x : ℂ) = tan x := of_real_tan_of_real_re _ @[simp] lemma tan_of_real_im (x : ℝ) : (tan x).im = 0 := by rw [← of_real_tan_of_real_re, of_real_im] lemma tan_of_real_re (x : ℝ) : (tan x).re = real.tan x := rfl lemma cos_add_sin_I : cos x + sin x * I = exp (x * I) := by rw [← cosh_add_sinh, sinh_mul_I, cosh_mul_I] lemma cos_sub_sin_I : cos x - sin x * I = exp (-x * I) := by rw [← neg_mul_eq_neg_mul, ← cosh_sub_sinh, sinh_mul_I, cosh_mul_I] lemma sin_sq_add_cos_sq : sin x ^ 2 + cos x ^ 2 = 1 := eq.trans (by rw [cosh_mul_I, sinh_mul_I, mul_pow, I_sq, mul_neg_one, sub_neg_eq_add, add_comm]) (cosh_sq_sub_sinh_sq (x * I)) lemma cos_two_mul' : cos (2 * x) = cos x ^ 2 - sin x ^ 2 := by rw [two_mul, cos_add, ← pow_two, ← pow_two] lemma cos_two_mul : cos (2 * x) = 2 * cos x ^ 2 - 1 := by rw [cos_two_mul', eq_sub_iff_add_eq.2 (sin_sq_add_cos_sq x), ← sub_add, sub_add_eq_add_sub, two_mul] lemma sin_two_mul : sin (2 * x) = 2 * sin x * cos x := by rw [two_mul, sin_add, two_mul, add_mul, mul_comm] lemma cos_square : cos x ^ 2 = 1 / 2 + cos (2 * x) / 2 := by simp [cos_two_mul, div_add_div_same, mul_div_cancel_left, two_ne_zero', -one_div_eq_inv] lemma sin_square : sin x ^ 2 = 1 - cos x ^ 2 := by { rw [←sin_sq_add_cos_sq x], simp } lemma exp_mul_I : exp (x * I) = cos x + sin x * I := (cos_add_sin_I _).symm lemma exp_add_mul_I : exp (x + y * I) = exp x * (cos y + sin y * I) := by rw [exp_add, exp_mul_I] lemma exp_eq_exp_re_mul_sin_add_cos : exp x = exp x.re * (cos x.im + sin x.im * I) := by rw [← exp_add_mul_I, re_add_im] theorem cos_add_sin_mul_I_pow (n : ℕ) (z : ℂ) : (cos z + sin z * I) ^ n = cos (↑n * z) + sin (↑n * z) * I := begin rw [← exp_mul_I, ← exp_mul_I], induction n with n ih, { rw [pow_zero, nat.cast_zero, zero_mul, zero_mul, exp_zero] }, { rw [pow_succ', ih, nat.cast_succ, add_mul, add_mul, one_mul, exp_add] } end end complex namespace real open complex variables (x y : ℝ) @[simp] lemma exp_zero : exp 0 = 1 := by simp [real.exp] lemma exp_add : exp (x + y) = exp x * exp y := by simp [exp_add, exp] lemma exp_list_sum (l : list ℝ) : exp l.sum = (l.map exp).prod := @monoid_hom.map_list_prod (multiplicative ℝ) ℝ _ _ ⟨exp, exp_zero, exp_add⟩ l lemma exp_multiset_sum (s : multiset ℝ) : exp s.sum = (s.map exp).prod := @monoid_hom.map_multiset_prod (multiplicative ℝ) ℝ _ _ ⟨exp, exp_zero, exp_add⟩ s lemma exp_sum {α : Type*} (s : finset α) (f : α → ℝ) : exp (s.sum f) = s.prod (exp ∘ f) := @monoid_hom.map_prod α (multiplicative ℝ) ℝ _ _ ⟨exp, exp_zero, exp_add⟩ f s lemma exp_nat_mul (x : ℝ) : ∀ n : ℕ, exp(n*x) = (exp x)^n | 0 := by rw [nat.cast_zero, zero_mul, exp_zero, pow_zero] | (nat.succ n) := by rw [pow_succ', nat.cast_add_one, add_mul, exp_add, ←exp_nat_mul, one_mul] lemma exp_ne_zero : exp x ≠ 0 := λ h, exp_ne_zero x $ by rw [exp, ← of_real_inj] at h; simp * at * lemma exp_neg : exp (-x) = (exp x)⁻¹ := by rw [← of_real_inj, exp, of_real_exp_of_real_re, of_real_neg, exp_neg, of_real_inv, of_real_exp] lemma exp_sub : exp (x - y) = exp x / exp y := by simp [sub_eq_add_neg, exp_add, exp_neg, div_eq_mul_inv] @[simp] lemma sin_zero : sin 0 = 0 := by simp [sin] @[simp] lemma sin_neg : sin (-x) = -sin x := by simp [sin, exp_neg, (neg_div _ _).symm, add_mul] lemma sin_add : sin (x + y) = sin x * cos y + cos x * sin y := by rw [← of_real_inj]; simp [sin, sin_add] @[simp] lemma cos_zero : cos 0 = 1 := by simp [cos] @[simp] lemma cos_neg : cos (-x) = cos x := by simp [cos, exp_neg] lemma cos_add : cos (x + y) = cos x * cos y - sin x * sin y := by rw ← of_real_inj; simp [cos, cos_add] lemma sin_sub : sin (x - y) = sin x * cos y - cos x * sin y := by simp [sub_eq_add_neg, sin_add, sin_neg, cos_neg] lemma cos_sub : cos (x - y) = cos x * cos y + sin x * sin y := by simp [sub_eq_add_neg, cos_add, sin_neg, cos_neg] lemma tan_eq_sin_div_cos : tan x = sin x / cos x := if h : complex.cos x = 0 then by simp [sin, cos, tan, *, complex.tan, div_eq_mul_inv] at * else by rw [sin, cos, tan, complex.tan, ← of_real_inj, div_eq_mul_inv, mul_re]; simp [norm_sq, (div_div_eq_div_mul _ _ _).symm, div_self h]; refl @[simp] lemma tan_zero : tan 0 = 0 := by simp [tan] @[simp] lemma tan_neg : tan (-x) = -tan x := by simp [tan, neg_div] lemma sin_sq_add_cos_sq : sin x ^ 2 + cos x ^ 2 = 1 := of_real_inj.1 $ by simpa using sin_sq_add_cos_sq x lemma sin_sq_le_one : sin x ^ 2 ≤ 1 := by rw ← sin_sq_add_cos_sq x; exact le_add_of_nonneg_right' (pow_two_nonneg _) lemma cos_sq_le_one : cos x ^ 2 ≤ 1 := by rw ← sin_sq_add_cos_sq x; exact le_add_of_nonneg_left' (pow_two_nonneg _) lemma abs_sin_le_one : abs' (sin x) ≤ 1 := (mul_self_le_mul_self_iff (_root_.abs_nonneg (sin x)) (by exact zero_le_one)).2 $ by rw [← _root_.abs_mul, abs_mul_self, mul_one, ← pow_two]; apply sin_sq_le_one lemma abs_cos_le_one : abs' (cos x) ≤ 1 := (mul_self_le_mul_self_iff (_root_.abs_nonneg (cos x)) (by exact zero_le_one)).2 $ by rw [← _root_.abs_mul, abs_mul_self, mul_one, ← pow_two]; apply cos_sq_le_one lemma sin_le_one : sin x ≤ 1 := (abs_le.1 (abs_sin_le_one _)).2 lemma cos_le_one : cos x ≤ 1 := (abs_le.1 (abs_cos_le_one _)).2 lemma neg_one_le_sin : -1 ≤ sin x := (abs_le.1 (abs_sin_le_one _)).1 lemma neg_one_le_cos : -1 ≤ cos x := (abs_le.1 (abs_cos_le_one _)).1 lemma cos_two_mul : cos (2 * x) = 2 * cos x ^ 2 - 1 := by rw ← of_real_inj; simp [cos_two_mul] lemma sin_two_mul : sin (2 * x) = 2 * sin x * cos x := by rw ← of_real_inj; simp [sin_two_mul] lemma cos_square : cos x ^ 2 = 1 / 2 + cos (2 * x) / 2 := of_real_inj.1 $ by simpa using cos_square x lemma sin_square : sin x ^ 2 = 1 - cos x ^ 2 := eq_sub_iff_add_eq.2 $ sin_sq_add_cos_sq _ @[simp] lemma sinh_zero : sinh 0 = 0 := by simp [sinh] @[simp] lemma sinh_neg : sinh (-x) = -sinh x := by simp [sinh, exp_neg, (neg_div _ _).symm, add_mul] lemma sinh_add : sinh (x + y) = sinh x * cosh y + cosh x * sinh y := by rw ← of_real_inj; simp [sinh_add] @[simp] lemma cosh_zero : cosh 0 = 1 := by simp [cosh] @[simp] lemma cosh_neg : cosh (-x) = cosh x := by simp [cosh, exp_neg] lemma cosh_add : cosh (x + y) = cosh x * cosh y + sinh x * sinh y := by rw ← of_real_inj; simp [cosh, cosh_add] lemma sinh_sub : sinh (x - y) = sinh x * cosh y - cosh x * sinh y := by simp [sub_eq_add_neg, sinh_add, sinh_neg, cosh_neg] lemma cosh_sub : cosh (x - y) = cosh x * cosh y - sinh x * sinh y := by simp [sub_eq_add_neg, cosh_add, sinh_neg, cosh_neg] lemma tanh_eq_sinh_div_cosh : tanh x = sinh x / cosh x := of_real_inj.1 $ by simp [tanh_eq_sinh_div_cosh] @[simp] lemma tanh_zero : tanh 0 = 0 := by simp [tanh] @[simp] lemma tanh_neg : tanh (-x) = -tanh x := by simp [tanh, neg_div] open is_absolute_value /- TODO make this private and prove ∀ x -/ lemma add_one_le_exp_of_nonneg {x : ℝ} (hx : 0 ≤ x) : x + 1 ≤ exp x := calc x + 1 ≤ lim (⟨(λ n : ℕ, ((exp' x) n).re), is_cau_seq_re (exp' x)⟩ : cau_seq ℝ abs') : le_lim (cau_seq.le_of_exists ⟨2, λ j hj, show x + (1 : ℝ) ≤ ((range j).sum (λ m, (x ^ m / m.fact : ℂ))).re, from have h₁ : (((λ m : ℕ, (x ^ m / m.fact : ℂ)) ∘ nat.succ) 0).re = x, by simp, have h₂ : ((x : ℂ) ^ 0 / nat.fact 0).re = 1, by simp, begin rw [← nat.sub_add_cancel hj, sum_range_succ', sum_range_succ', add_re, add_re, h₁, h₂, add_assoc, ← @sum_hom _ _ _ _ _ _ _ complex.re (is_add_group_hom.to_is_add_monoid_hom _)], refine le_add_of_nonneg_of_le (sum_nonneg (λ m hm, _)) (le_refl _), rw [← of_real_pow, ← of_real_nat_cast, ← of_real_div, of_real_re], exact div_nonneg (pow_nonneg hx _) (nat.cast_pos.2 (nat.fact_pos _)), end⟩) ... = exp x : by rw [exp, complex.exp, ← cau_seq_re, lim_re] lemma one_le_exp {x : ℝ} (hx : 0 ≤ x) : 1 ≤ exp x := by linarith [add_one_le_exp_of_nonneg hx] lemma exp_pos (x : ℝ) : 0 < exp x := (le_total 0 x).elim (lt_of_lt_of_le zero_lt_one ∘ one_le_exp) (λ h, by rw [← neg_neg x, real.exp_neg]; exact inv_pos.2 (lt_of_lt_of_le zero_lt_one (one_le_exp (neg_nonneg.2 h)))) @[simp] lemma abs_exp (x : ℝ) : abs' (exp x) = exp x := abs_of_pos (exp_pos _) lemma exp_strict_mono : strict_mono exp := λ x y h, by rw [← sub_add_cancel y x, real.exp_add]; exact (lt_mul_iff_one_lt_left (exp_pos _)).2 (lt_of_lt_of_le (by linarith) (add_one_le_exp_of_nonneg (by linarith))) lemma exp_lt_exp {x y : ℝ} : exp x < exp y ↔ x < y := exp_strict_mono.lt_iff_lt lemma exp_le_exp {x y : ℝ} : exp x ≤ exp y ↔ x ≤ y := exp_strict_mono.le_iff_le lemma exp_injective : function.injective exp := exp_strict_mono.injective @[simp] lemma exp_eq_one_iff : exp x = 1 ↔ x = 0 := by rw [← exp_zero, exp_injective.eq_iff] lemma one_lt_exp_iff {x : ℝ} : 1 < exp x ↔ 0 < x := by rw [← exp_zero, exp_lt_exp] lemma exp_lt_one_iff {x : ℝ} : exp x < 1 ↔ x < 0 := by rw [← exp_zero, exp_lt_exp] end real namespace complex lemma sum_div_fact_le {α : Type*} [discrete_linear_ordered_field α] (n j : ℕ) (hn : 0 < n) : (filter (λ k, n ≤ k) (range j)).sum (λ m : ℕ, (1 / m.fact : α)) ≤ n.succ * (n.fact * n)⁻¹ := calc (filter (λ k, n ≤ k) (range j)).sum (λ m : ℕ, (1 / m.fact : α)) = (range (j - n)).sum (λ m, 1 / (m + n).fact) : sum_bij (λ m _, m - n) (λ m hm, mem_range.2 $ (nat.sub_lt_sub_right_iff (by simp at hm; tauto)).2 (by simp at hm; tauto)) (λ m hm, by rw nat.sub_add_cancel; simp at *; tauto) (λ a₁ a₂ ha₁ ha₂ h, by rwa [nat.sub_eq_iff_eq_add, ← nat.sub_add_comm, eq_comm, nat.sub_eq_iff_eq_add, add_left_inj, eq_comm] at h; simp at *; tauto) (λ b hb, ⟨b + n, mem_filter.2 ⟨mem_range.2 $ nat.add_lt_of_lt_sub_right (mem_range.1 hb), nat.le_add_left _ _⟩, by rw nat.add_sub_cancel⟩) ... ≤ (range (j - n)).sum (λ m, (nat.fact n * n.succ ^ m)⁻¹) : begin refine sum_le_sum (assume m n, _), rw [one_div_eq_inv, inv_le_inv], { rw [← nat.cast_pow, ← nat.cast_mul, nat.cast_le, add_comm], exact nat.fact_mul_pow_le_fact }, { exact nat.cast_pos.2 (nat.fact_pos _) }, { exact mul_pos (nat.cast_pos.2 (nat.fact_pos _)) (pow_pos (nat.cast_pos.2 (nat.succ_pos _)) _) }, end ... = (nat.fact n)⁻¹ * (range (j - n)).sum (λ m, n.succ⁻¹ ^ m) : by simp [mul_inv', mul_sum.symm, sum_mul.symm, -nat.fact_succ, mul_comm, inv_pow'] ... = (n.succ - n.succ * n.succ⁻¹ ^ (j - n)) / (n.fact * n) : have h₁ : (n.succ : α) ≠ 1, from @nat.cast_one α _ _ ▸ mt nat.cast_inj.1 (mt nat.succ_inj (nat.pos_iff_ne_zero.1 hn)), have h₂ : (n.succ : α) ≠ 0, from nat.cast_ne_zero.2 (nat.succ_ne_zero _), have h₃ : (n.fact * n : α) ≠ 0, from mul_ne_zero (nat.cast_ne_zero.2 (nat.pos_iff_ne_zero.1 (nat.fact_pos _))) (nat.cast_ne_zero.2 (nat.pos_iff_ne_zero.1 hn)), have h₄ : (n.succ - 1 : α) = n, by simp, by rw [← geom_series_def, geom_sum_inv h₁ h₂, eq_div_iff_mul_eq _ _ h₃, mul_comm _ (n.fact * n : α), ← mul_assoc (n.fact⁻¹ : α), ← mul_inv', h₄, ← mul_assoc (n.fact * n : α), mul_comm (n : α) n.fact, mul_inv_cancel h₃]; simp [mul_add, add_mul, mul_assoc, mul_comm] ... ≤ n.succ / (n.fact * n) : begin refine iff.mpr (div_le_div_right (mul_pos _ _)) _, exact nat.cast_pos.2 (nat.fact_pos _), exact nat.cast_pos.2 hn, exact sub_le_self _ (mul_nonneg (nat.cast_nonneg _) (pow_nonneg (inv_nonneg.2 (nat.cast_nonneg _)) _)) end lemma exp_bound {x : ℂ} (hx : abs x ≤ 1) {n : ℕ} (hn : 0 < n) : abs (exp x - (range n).sum (λ m, x ^ m / m.fact)) ≤ abs x ^ n * (n.succ * (n.fact * n)⁻¹) := begin rw [← lim_const ((range n).sum _), exp, sub_eq_add_neg, ← lim_neg, lim_add, ← lim_abs], refine lim_le (cau_seq.le_of_exists ⟨n, λ j hj, _⟩), show abs ((range j).sum (λ m, x ^ m / m.fact) - (range n).sum (λ m, x ^ m / m.fact)) ≤ abs x ^ n * (n.succ * (n.fact * n)⁻¹), rw sum_range_sub_sum_range hj, exact calc abs (((range j).filter (λ k, n ≤ k)).sum (λ m : ℕ, (x ^ m / m.fact : ℂ))) = abs (((range j).filter (λ k, n ≤ k)).sum (λ m : ℕ, (x ^ n * (x ^ (m - n) / m.fact) : ℂ))) : congr_arg abs (sum_congr rfl (λ m hm, by rw [← mul_div_assoc, ← pow_add, nat.add_sub_cancel']; simp at hm; tauto)) ... ≤ (filter (λ k, n ≤ k) (range j)).sum (λ m, abs (x ^ n * (_ / m.fact))) : abv_sum_le_sum_abv _ _ ... ≤ (filter (λ k, n ≤ k) (range j)).sum (λ m, abs x ^ n * (1 / m.fact)) : begin refine sum_le_sum (λ m hm, _), rw [abs_mul, abv_pow abs, abs_div, abs_cast_nat], refine mul_le_mul_of_nonneg_left ((div_le_div_right _).2 _) _, exact nat.cast_pos.2 (nat.fact_pos _), rw abv_pow abs, exact (pow_le_one _ (abs_nonneg _) hx), exact pow_nonneg (abs_nonneg _) _ end ... = abs x ^ n * (((range j).filter (λ k, n ≤ k)).sum (λ m : ℕ, (1 / m.fact : ℝ))) : by simp [abs_mul, abv_pow abs, abs_div, mul_sum.symm] ... ≤ abs x ^ n * (n.succ * (n.fact * n)⁻¹) : mul_le_mul_of_nonneg_left (sum_div_fact_le _ _ hn) (pow_nonneg (abs_nonneg _) _) end lemma abs_exp_sub_one_le {x : ℂ} (hx : abs x ≤ 1) : abs (exp x - 1) ≤ 2 * abs x := calc abs (exp x - 1) = abs (exp x - (range 1).sum (λ m, x ^ m / m.fact)) : by simp [sum_range_succ] ... ≤ abs x ^ 1 * ((nat.succ 1) * (nat.fact 1 * (1 : ℕ))⁻¹) : exp_bound hx dec_trivial ... = 2 * abs x : by simp [two_mul, mul_two, mul_add, mul_comm] lemma abs_exp_sub_one_sub_id_le {x : ℂ} (hx : abs x ≤ 1) : abs (exp x - 1 - x) ≤ (abs x)^2 := calc abs (exp x - 1 - x) = abs (exp x - (range 2).sum (λ m, x ^ m / m.fact)) : by simp [sub_eq_add_neg, sum_range_succ, add_assoc] ... ≤ (abs x)^2 * (nat.succ 2 * (nat.fact 2 * (2 : ℕ))⁻¹) : exp_bound hx dec_trivial ... ≤ (abs x)^2 * 1 : mul_le_mul_of_nonneg_left (by norm_num) (pow_two_nonneg (abs x)) ... = (abs x)^2 : by rw [mul_one] end complex namespace real open complex finset lemma cos_bound {x : ℝ} (hx : abs' x ≤ 1) : abs' (cos x - (1 - x ^ 2 / 2)) ≤ abs' x ^ 4 * (5 / 96) := calc abs' (cos x - (1 - x ^ 2 / 2)) = abs (complex.cos x - (1 - x ^ 2 / 2)) : by rw ← abs_of_real; simp [of_real_bit0, of_real_one, of_real_inv] ... = abs ((complex.exp (x * I) + complex.exp (-x * I) - (2 - x ^ 2)) / 2) : by simp [complex.cos, sub_div, add_div, neg_div, div_self (@two_ne_zero' ℂ _ _ _)] ... = abs (((complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact)) + ((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact)))) / 2) : congr_arg abs (congr_arg (λ x : ℂ, x / 2) begin simp only [sum_range_succ], simp [pow_succ], apply complex.ext; simp [div_eq_mul_inv, norm_sq]; ring end) ... ≤ abs ((complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact)) / 2) + abs ((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact)) / 2) : by rw add_div; exact abs_add _ _ ... = (abs ((complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact))) / 2 + abs ((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact))) / 2) : by simp [complex.abs_div] ... ≤ ((complex.abs (x * I) ^ 4 * (nat.succ 4 * (nat.fact 4 * (4 : ℕ))⁻¹)) / 2 + (complex.abs (-x * I) ^ 4 * (nat.succ 4 * (nat.fact 4 * (4 : ℕ))⁻¹)) / 2) : add_le_add ((div_le_div_right (by norm_num)).2 (exp_bound (by simpa) dec_trivial)) ((div_le_div_right (by norm_num)).2 (exp_bound (by simpa) dec_trivial)) ... ≤ abs' x ^ 4 * (5 / 96) : by norm_num; simp [mul_assoc, mul_comm, mul_left_comm, mul_div_assoc] lemma sin_bound {x : ℝ} (hx : abs' x ≤ 1) : abs' (sin x - (x - x ^ 3 / 6)) ≤ abs' x ^ 4 * (5 / 96) := calc abs' (sin x - (x - x ^ 3 / 6)) = abs (complex.sin x - (x - x ^ 3 / 6)) : by rw ← abs_of_real; simp [of_real_bit0, of_real_one, of_real_inv] ... = abs (((complex.exp (-x * I) - complex.exp (x * I)) * I - (2 * x - x ^ 3 / 3)) / 2) : by simp [complex.sin, sub_div, add_div, neg_div, mul_div_cancel_left _ (@two_ne_zero' ℂ _ _ _), div_div_eq_div_mul, show (3 : ℂ) * 2 = 6, by norm_num] ... = abs ((((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact)) - (complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact))) * I) / 2) : congr_arg abs (congr_arg (λ x : ℂ, x / 2) begin simp only [sum_range_succ], simp [pow_succ], apply complex.ext; simp [div_eq_mul_inv, norm_sq]; ring end) ... ≤ abs ((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact)) * I / 2) + abs (-((complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact)) * I) / 2) : by rw [sub_mul, sub_eq_add_neg, add_div]; exact abs_add _ _ ... = (abs ((complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact))) / 2 + abs ((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact))) / 2) : by simp [add_comm, complex.abs_div, complex.abs_mul] ... ≤ ((complex.abs (x * I) ^ 4 * (nat.succ 4 * (nat.fact 4 * (4 : ℕ))⁻¹)) / 2 + (complex.abs (-x * I) ^ 4 * (nat.succ 4 * (nat.fact 4 * (4 : ℕ))⁻¹)) / 2) : add_le_add ((div_le_div_right (by norm_num)).2 (exp_bound (by simpa) dec_trivial)) ((div_le_div_right (by norm_num)).2 (exp_bound (by simpa) dec_trivial)) ... ≤ abs' x ^ 4 * (5 / 96) : by norm_num; simp [mul_assoc, mul_comm, mul_left_comm, mul_div_assoc] lemma cos_pos_of_le_one {x : ℝ} (hx : abs' x ≤ 1) : 0 < cos x := calc 0 < (1 - x ^ 2 / 2) - abs' x ^ 4 * (5 / 96) : sub_pos.2 $ lt_sub_iff_add_lt.2 (calc abs' x ^ 4 * (5 / 96) + x ^ 2 / 2 ≤ 1 * (5 / 96) + 1 / 2 : add_le_add (mul_le_mul_of_nonneg_right (pow_le_one _ (abs_nonneg _) hx) (by norm_num)) ((div_le_div_right (by norm_num)).2 (by rw [pow_two, ← abs_mul_self, _root_.abs_mul]; exact mul_le_one hx (abs_nonneg _) hx)) ... < 1 : by norm_num) ... ≤ cos x : sub_le.1 (abs_sub_le_iff.1 (cos_bound hx)).2 lemma sin_pos_of_pos_of_le_one {x : ℝ} (hx0 : 0 < x) (hx : x ≤ 1) : 0 < sin x := calc 0 < x - x ^ 3 / 6 - abs' x ^ 4 * (5 / 96) : sub_pos.2 $ lt_sub_iff_add_lt.2 (calc abs' x ^ 4 * (5 / 96) + x ^ 3 / 6 ≤ x * (5 / 96) + x / 6 : add_le_add (mul_le_mul_of_nonneg_right (calc abs' x ^ 4 ≤ abs' x ^ 1 : pow_le_pow_of_le_one (abs_nonneg _) (by rwa _root_.abs_of_nonneg (le_of_lt hx0)) dec_trivial ... = x : by simp [_root_.abs_of_nonneg (le_of_lt (hx0))]) (by norm_num)) ((div_le_div_right (by norm_num)).2 (calc x ^ 3 ≤ x ^ 1 : pow_le_pow_of_le_one (le_of_lt hx0) hx dec_trivial ... = x : pow_one _)) ... < x : by linarith) ... ≤ sin x : sub_le.1 (abs_sub_le_iff.1 (sin_bound (by rwa [_root_.abs_of_nonneg (le_of_lt hx0)]))).2 lemma sin_pos_of_pos_of_le_two {x : ℝ} (hx0 : 0 < x) (hx : x ≤ 2) : 0 < sin x := have x / 2 ≤ 1, from div_le_of_le_mul (by norm_num) (by simpa), calc 0 < 2 * sin (x / 2) * cos (x / 2) : mul_pos (mul_pos (by norm_num) (sin_pos_of_pos_of_le_one (half_pos hx0) this)) (cos_pos_of_le_one (by rwa [_root_.abs_of_nonneg (le_of_lt (half_pos hx0))])) ... = sin x : by rw [← sin_two_mul, two_mul, add_halves] lemma cos_one_le : cos 1 ≤ 2 / 3 := calc cos 1 ≤ abs' (1 : ℝ) ^ 4 * (5 / 96) + (1 - 1 ^ 2 / 2) : sub_le_iff_le_add.1 (abs_sub_le_iff.1 (cos_bound (by simp))).1 ... ≤ 2 / 3 : by norm_num lemma cos_one_pos : 0 < cos 1 := cos_pos_of_le_one (by simp) lemma cos_two_neg : cos 2 < 0 := calc cos 2 = cos (2 * 1) : congr_arg cos (mul_one _).symm ... = _ : real.cos_two_mul 1 ... ≤ 2 * (2 / 3) ^ 2 - 1 : sub_le_sub_right (mul_le_mul_of_nonneg_left (by rw [pow_two, pow_two]; exact mul_self_le_mul_self (le_of_lt cos_one_pos) cos_one_le) (by norm_num)) _ ... < 0 : by norm_num end real namespace complex lemma abs_cos_add_sin_mul_I (x : ℝ) : abs (cos x + sin x * I) = 1 := have _ := real.sin_sq_add_cos_sq x, by simp [add_comm, abs, norm_sq, pow_two, *, sin_of_real_re, cos_of_real_re, mul_re] at * lemma abs_exp_eq_iff_re_eq {x y : ℂ} : abs (exp x) = abs (exp y) ↔ x.re = y.re := by rw [exp_eq_exp_re_mul_sin_add_cos, exp_eq_exp_re_mul_sin_add_cos y, abs_mul, abs_mul, abs_cos_add_sin_mul_I, abs_cos_add_sin_mul_I, ← of_real_exp, ← of_real_exp, abs_of_nonneg (le_of_lt (real.exp_pos _)), abs_of_nonneg (le_of_lt (real.exp_pos _)), mul_one, mul_one]; exact ⟨λ h, real.exp_injective h, congr_arg _⟩ @[simp] lemma abs_exp_of_real (x : ℝ) : abs (exp x) = real.exp x := by rw [← of_real_exp]; exact abs_of_nonneg (le_of_lt (real.exp_pos _)) end complex
119ffc5dca43cda67d1a05a14b0665e643fc71cb
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/fun_like/fintype.lean
64f0b9e8732f8c63c5cae164418593c36a68ed89
[ "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
2,549
lean
/- Copyright (c) 2022 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import data.finite.basic import data.fintype.basic import data.fun_like.basic /-! # Finiteness of `fun_like` types We show a type `F` with a `fun_like F α β` is finite if both `α` and `β` are finite. This corresponds to the following two pairs of declarations: * `fun_like.fintype` is a definition stating all `fun_like`s are finite if their domain and codomain are. * `fun_like.finite` is a lemma stating all `fun_like`s are finite if their domain and codomain are. * `fun_like.fintype'` is a non-dependent version of `fun_like.fintype` and * `fun_like.finite` is a non-dependent version of `fun_like.finite`, because dependent instances are harder to infer. You can use these to produce instances for specific `fun_like` types. (Although there might be options for `fintype` instances with better definitional behaviour.) They can't be instances themselves since they can cause loops. -/ section type variables (F G : Type*) {α γ : Type*} {β : α → Type*} [fun_like F α β] [fun_like G α (λ _, γ)] /-- All `fun_like`s are finite if their domain and codomain are. This is not an instance because specific `fun_like` types might have a better-suited definition. See also `fun_like.finite`. -/ noncomputable def fun_like.fintype [decidable_eq α] [fintype α] [Π i, fintype (β i)] : fintype F := fintype.of_injective _ fun_like.coe_injective /-- All `fun_like`s are finite if their domain and codomain are. Non-dependent version of `fun_like.fintype` that might be easier to infer. This is not an instance because specific `fun_like` types might have a better-suited definition. -/ noncomputable def fun_like.fintype' [decidable_eq α] [fintype α] [fintype γ] : fintype G := fun_like.fintype G end type section sort variables (F G : Sort*) {α γ : Sort*} {β : α → Sort*} [fun_like F α β] [fun_like G α (λ _, γ)] /-- All `fun_like`s are finite if their domain and codomain are. Can't be an instance because it can cause infinite loops. -/ lemma fun_like.finite [finite α] [∀ i, finite (β i)] : finite F := finite.of_injective _ fun_like.coe_injective /-- All `fun_like`s are finite if their domain and codomain are. Non-dependent version of `fun_like.finite` that might be easier to infer. Can't be an instance because it can cause infinite loops. -/ lemma fun_like.finite' [finite α] [finite γ] : finite G := fun_like.finite G end sort
f5f54e2e94252a0debadddaefe3fdc92aa3f9baf
df7bb3acd9623e489e95e85d0bc55590ab0bc393
/lean/love08_operational_semantics_exercise_solution.lean
d20f9beccb8997f188d1d442ba803966c51d5763
[]
no_license
MaschavanderMarel/logical_verification_2020
a41c210b9237c56cb35f6cd399e3ac2fe42e775d
7d562ef174cc6578ca6013f74db336480470b708
refs/heads/master
1,692,144,223,196
1,634,661,675,000
1,634,661,675,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,418
lean
import .love08_operational_semantics_demo /- # LoVe Exercise 8: Operational Semantics -/ set_option pp.beta true set_option pp.generalized_field_notation false namespace LoVe /- ## Question 1: Program Equivalence For this question, we introduce the notion of program equivalence: `S₁ ≈ S₂`. -/ def big_step_equiv (S₁ S₂ : stmt) : Prop := ∀s t, (S₁, s) ⟹ t ↔ (S₂, s) ⟹ t infix ` ≈ ` := big_step_equiv /- Program equivalence is a equivalence relation, i.e., it is reflexive, symmetric, and transitive. -/ @[refl] lemma big_step_equiv.refl {S : stmt} : S ≈ S := fix s t, show (S, s) ⟹ t ↔ (S, s) ⟹ t, from by refl @[symm] lemma big_step_equiv.symm {S₁ S₂ : stmt}: S₁ ≈ S₂ → S₂ ≈ S₁ := assume h : S₁ ≈ S₂, fix s t, show (S₂, s) ⟹ t ↔ (S₁, s) ⟹ t, from iff.symm (h s t) @[trans] lemma big_step_equiv.trans {S₁ S₂ S₃ : stmt} (h₁₂ : S₁ ≈ S₂) (h₂₃ : S₂ ≈ S₃) : S₁ ≈ S₃ := fix s t, show (S₁, s) ⟹ t ↔ (S₃, s) ⟹ t, from iff.trans (h₁₂ s t) (h₂₃ s t) /- 1.1. Prove the following program equivalences. -/ lemma big_step_equiv.skip_assign_id {x} : stmt.assign x (λs, s x) ≈ stmt.skip := begin intros s t, apply iff.intro, { intro hasn, cases' hasn, simp * at * }, { intro hskip, cases' hskip, simp * at * } end lemma big_step_equiv.seq_skip_left {S : stmt} : stmt.skip ;; S ≈ S := begin intros s t, apply iff.intro, { intro h, cases' h, cases' h_1, assumption }, { intro h, exact big_step.seq big_step.skip h } end lemma big_step_equiv.seq_skip_right {S : stmt} : S ;; stmt.skip ≈ S := begin intros s t, apply iff.intro, { intro h, cases' h, cases' h_1, assumption }, { intro h, exact big_step.seq h big_step.skip } end lemma big_step_equiv.ite_seq_while {b} {S : stmt} : stmt.ite b (S ;; stmt.while b S) stmt.skip ≈ stmt.while b S := begin intros s t, apply iff.intro, { intro hite, cases' hite, { cases' hite, apply big_step.while_true; assumption }, { cases' hite, apply big_step.while_false, assumption } }, { intro while, cases' while, { apply big_step.ite_true hcond, apply big_step.seq; assumption }, { apply big_step.ite_false hcond, exact big_step.skip } } end /- 1.2. Program equivalence can be used to replace subprograms by other subprograms with the same semantics. Prove the following so-called congruence rules: -/ lemma big_step_equiv.seq_congr {S₁ S₂ T₁ T₂ : stmt} (hS : S₁ ≈ S₂) (hT : T₁ ≈ T₂) : S₁ ;; T₁ ≈ S₂ ;; T₂ := begin intros s t, apply iff.intro, { intro hseq, cases' hseq with _ _ _ _ _ _ _ u _ hS₁ hT₁, apply big_step.seq, { apply iff.elim_left (hS _ _) hS₁ }, { apply iff.elim_left (hT _ _) hT₁ } }, { intros hseq, cases' hseq with _ _ _ _ _ _ _ u _ hS₂ hT₂, apply big_step.seq, { exact iff.elim_right (hS _ _) hS₂ }, { exact iff.elim_right (hT _ _) hT₂ } } end lemma big_step_equiv.ite_congr {b} {S₁ S₂ T₁ T₂ : stmt} (hS : S₁ ≈ S₂) (hT : T₁ ≈ T₂) : stmt.ite b S₁ T₁ ≈ stmt.ite b S₂ T₂ := begin intros s t, apply iff.intro, { intro hite, cases' hite, case ite_true : _ _ _ _ _ hcond hbody { apply big_step.ite_true hcond, exact iff.elim_left (hS _ _) hbody }, case ite_false : _ _ _ _ _ hcond hbody { apply big_step.ite_false hcond, exact iff.elim_left (hT _ _) hbody } }, { intro hite, cases' hite, case ite_true : _ _ _ _ _ hcond hbody { apply big_step.ite_true hcond, exact iff.elim_right (hS _ _) hbody }, case ite_false : _ _ _ _ _ hcond hbody { apply big_step.ite_false hcond, exact iff.elim_right (hT _ _) hbody } } end /- 1.3 (**optional**): Prove one more congruence rule. This one is more difficult. -/ lemma denote_equiv.while_congr {b} {S₁ S₂ : stmt} (hS : S₁ ≈ S₂) : stmt.while b S₁ ≈ stmt.while b S₂ := begin intros s t, apply iff.intro, { intro hwhile, induction' hwhile, case while_true : b S t u v hcond hbody hrest ih_body ih_rest { apply big_step.while_true hcond, exact iff.elim_left (hS _ _) hbody, exact ih_rest hS }, case while_false { exact big_step.while_false hcond } }, { intro hwhile, induction' hwhile, case while_true : b S t u v hcond hbody hrest ih_body ih_rest { apply big_step.while_true hcond, exact iff.elim_right (hS _ _) hbody, exact ih_rest hS }, case while_false { exact big_step.while_false hcond } } end /- ## Question 2: Guarded Command Language (GCL) In 1976, E. W. Dijkstra introduced the guarded command language, a minimalistic imperative language with built-in nondeterminism. A grammar for one of its variants is given below: S ::= x := e -- assignment | assert b -- assertion | S ; S -- sequential composition | S | ⋯ | S -- nondeterministic choice | loop S -- nondeterministic iteration Assignment and sequential composition are as in the WHILE language. The other statements have the following semantics: * `assert b` aborts if `b` evaluates to false; otherwise, the command is a no-op. * `S | ⋯ | S` chooses any of the branches and executes it, ignoring the other branches. * `loop S` executes `S` any number of times. In Lean, GCL is captured by the following inductive type: -/ namespace gcl inductive stmt (σ : Type) : Type | assign : string → (σ → ℕ) → stmt | assert : (σ → Prop) → stmt | seq : stmt → stmt → stmt | choice : list stmt → stmt | loop : stmt → stmt infixr ` ;; ` : 90 := stmt.seq /- The parameter `σ` abstracts over the state type. It is necessary as a work around for a Lean bug. The big-step semantics is defined as follows: -/ inductive big_step : (stmt state × state) → state → Prop | assign {x a s} : big_step (stmt.assign x a, s) (s{x ↦ a s}) | assert {b : state → Prop} {s} (hcond : b s) : big_step (stmt.assert b, s) s | seq {S T s t u} (h₁ : big_step (S, s) t) (h₂ : big_step (T, t) u) : big_step (S ;; T, s) u | choice {Ss s t} (i) (hless : i < list.length Ss) (hbody : big_step (list.nth_le Ss i hless, s) t) : big_step (stmt.choice Ss, s) t | loop_base {S s} : big_step (stmt.loop S, s) s | loop_step {S s u} (t) (hbody : big_step (S, s) t) (hrest : big_step (stmt.loop S, t) u) : big_step (stmt.loop S, s) u infix ` ⟹ ` : 110 := big_step /- 2.1. Prove the following inversion rules, as we did in the lecture for the WHILE language. -/ @[simp] lemma big_step_assign_iff {x a s t} : (stmt.assign x a, s) ⟹ t ↔ t = s{x ↦ a s} := begin apply iff.intro, { intro hasn, cases' hasn, refl }, { intro heq, rw heq, exact big_step.assign } end @[simp] lemma big_step_assert {b s t} : (stmt.assert b, s) ⟹ t ↔ t = s ∧ b s := begin apply iff.intro, { intro hast, cases' hast, simp * at * }, { intros hand, cases' hand, rw left, apply big_step.assert right } end @[simp] lemma big_step_seq_iff {S₁ S₂ s t} : (stmt.seq S₁ S₂, s) ⟹ t ↔ (∃u, (S₁, s) ⟹ u ∧ (S₂, u) ⟹ t) := begin apply iff.intro, { intro hseq, cases' hseq, apply exists.intro, apply and.intro; assumption }, { intro hseq, cases' hseq, cases' h, apply big_step.seq; assumption } end lemma big_step_loop {S s u} : (stmt.loop S, s) ⟹ u ↔ (s = u ∨ (∃t, (S, s) ⟹ t ∧ (stmt.loop S, t) ⟹ u)) := begin apply iff.intro, { intro hloop, cases' hloop, { apply or.intro_left, refl }, { apply or.intro_right, apply exists.intro t, cc } }, { intro hor, cases' hor, { rw h, apply big_step.loop_base }, { cases' h, cases' h, apply big_step.loop_step; assumption } } end @[simp] lemma big_step_choice {Ss s t} : (stmt.choice Ss, s) ⟹ t ↔ (∃(i : ℕ) (hless : i < list.length Ss), (list.nth_le Ss i hless, s) ⟹ t) := begin apply iff.intro, { intro hchoice, cases' hchoice, apply exists.intro i, apply exists.intro hless, assumption }, { intro hex, cases' hex with i hex, cases' hex with hless hstep, apply big_step.choice; assumption } end end gcl /- 2.2. Complete the translation below of a deterministic program to a GCL program, by filling in the `sorry` placeholders below. -/ def gcl_of : stmt → gcl.stmt state | stmt.skip := gcl.stmt.assert (λ_, true) | (stmt.assign x a) := gcl.stmt.assign x a | (S ;; T) := gcl_of S ;; gcl_of T | (stmt.ite b S T) := gcl.stmt.choice [ gcl.stmt.assert b ;; gcl_of S, gcl.stmt.assert (λs, ¬ b s) ;; gcl_of T ] | (stmt.while b S) := gcl.stmt.loop (gcl.stmt.assert b ;; gcl_of S) ;; gcl.stmt.assert (λs, ¬ b s) /- 2.3. In the definition of `gcl_of` above, `skip` is translated to `assert (λ_, true)`. Looking at the big-step semantics of both constructs, we can convince ourselves that it makes sense. Can you think of other correct ways to define the `skip` case? -/ /- Here are two other possibilities: * `loop (assert (λ_, false))` * `assign "x" (λs, s "x")` There are infinitely many variants, e.g., `seq` of the above two solutions. -/ end LoVe
d645fdded5ee692abd7f7982b8cabec0ff0381ea
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/analysis/normed_space/add_torsor_auto.lean
e5aa51acab416309140494623de32843c73a1158
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
20,380
lean
/- Copyright (c) 2020 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers, Yury Kudryashov. -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.linear_algebra.affine_space.midpoint import Mathlib.topology.metric_space.isometry import Mathlib.topology.instances.real_vector_space import Mathlib.PostPort universes u_1 u_2 l u_3 u_4 u_5 u_6 namespace Mathlib /-! # Torsors of additive normed group actions. This file defines torsors of additive normed group actions, with a metric space structure. The motivating case is Euclidean affine spaces. -/ /-- A `normed_add_torsor V P` is a torsor of an additive normed group action by a `normed_group V` on points `P`. We bundle the metric space structure and require the distance to be the same as results from the norm (which in fact implies the distance yields a metric space, but bundling just the distance and using an instance for the metric space results in type class problems). -/ class normed_add_torsor (V : outParam (Type u_1)) (P : Type u_2) [outParam (normed_group V)] [metric_space P] extends add_torsor V P where dist_eq_norm' : ∀ (x y : P), dist x y = norm (x -ᵥ y) /-- The distance equals the norm of subtracting two points. In this lemma, it is necessary to have `V` as an explicit argument; otherwise `rw dist_eq_norm_vsub` sometimes doesn't work. -/ theorem dist_eq_norm_vsub (V : Type u_2) {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (x : P) (y : P) : dist x y = norm (x -ᵥ y) := normed_add_torsor.dist_eq_norm' x y /-- A `normed_group` is a `normed_add_torsor` over itself. -/ protected instance normed_group.normed_add_torsor (V : Type u_2) [normed_group V] : normed_add_torsor V V := normed_add_torsor.mk dist_eq_norm @[simp] theorem dist_vadd_cancel_left {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (v : V) (x : P) (y : P) : dist (v +ᵥ x) (v +ᵥ y) = dist x y := sorry @[simp] theorem dist_vadd_cancel_right {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (v₁ : V) (v₂ : V) (x : P) : dist (v₁ +ᵥ x) (v₂ +ᵥ x) = dist v₁ v₂ := sorry @[simp] theorem dist_vadd_left {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (v : V) (x : P) : dist (v +ᵥ x) x = norm v := sorry @[simp] theorem dist_vadd_right {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (v : V) (x : P) : dist x (v +ᵥ x) = norm v := eq.mpr (id (Eq._oldrec (Eq.refl (dist x (v +ᵥ x) = norm v)) (dist_comm x (v +ᵥ x)))) (eq.mpr (id (Eq._oldrec (Eq.refl (dist (v +ᵥ x) x = norm v)) (dist_vadd_left v x))) (Eq.refl (norm v))) @[simp] theorem dist_vsub_cancel_left {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (x : P) (y : P) (z : P) : dist (x -ᵥ y) (x -ᵥ z) = dist y z := sorry @[simp] theorem dist_vsub_cancel_right {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (x : P) (y : P) (z : P) : dist (x -ᵥ z) (y -ᵥ z) = dist x y := eq.mpr (id (Eq._oldrec (Eq.refl (dist (x -ᵥ z) (y -ᵥ z) = dist x y)) (dist_eq_norm (x -ᵥ z) (y -ᵥ z)))) (eq.mpr (id (Eq._oldrec (Eq.refl (norm (x -ᵥ z - (y -ᵥ z)) = dist x y)) (vsub_sub_vsub_cancel_right x y z))) (eq.mpr (id (Eq._oldrec (Eq.refl (norm (x -ᵥ y) = dist x y)) (dist_eq_norm_vsub V x y))) (Eq.refl (norm (x -ᵥ y))))) theorem dist_vadd_vadd_le {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (v : V) (v' : V) (p : P) (p' : P) : dist (v +ᵥ p) (v' +ᵥ p') ≤ dist v v' + dist p p' := sorry theorem dist_vsub_vsub_le {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (p₁ : P) (p₂ : P) (p₃ : P) (p₄ : P) : dist (p₁ -ᵥ p₂) (p₃ -ᵥ p₄) ≤ dist p₁ p₃ + dist p₂ p₄ := sorry theorem nndist_vadd_vadd_le {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (v : V) (v' : V) (p : P) (p' : P) : nndist (v +ᵥ p) (v' +ᵥ p') ≤ nndist v v' + nndist p p' := sorry theorem nndist_vsub_vsub_le {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (p₁ : P) (p₂ : P) (p₃ : P) (p₄ : P) : nndist (p₁ -ᵥ p₂) (p₃ -ᵥ p₄) ≤ nndist p₁ p₃ + nndist p₂ p₄ := sorry theorem edist_vadd_vadd_le {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (v : V) (v' : V) (p : P) (p' : P) : edist (v +ᵥ p) (v' +ᵥ p') ≤ edist v v' + edist p p' := sorry theorem edist_vsub_vsub_le {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (p₁ : P) (p₂ : P) (p₃ : P) (p₄ : P) : edist (p₁ -ᵥ p₂) (p₃ -ᵥ p₄) ≤ edist p₁ p₃ + edist p₂ p₄ := sorry /-- The distance defines a metric space structure on the torsor. This is not an instance because it depends on `V` to define a `metric_space P`. -/ def metric_space_of_normed_group_of_add_torsor (V : Type u_1) (P : Type u_2) [normed_group V] [add_torsor V P] : metric_space P := metric_space.mk sorry sorry sorry sorry (fun (x y : P) => ennreal.of_real ((fun (x y : P) => norm (x -ᵥ y)) x y)) (uniform_space_of_dist (fun (x y : P) => norm (x -ᵥ y)) sorry sorry sorry) namespace isometric /-- The map `v ↦ v +ᵥ p` as an isometric equivalence between `V` and `P`. -/ def vadd_const {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (p : P) : V ≃ᵢ P := mk (equiv.vadd_const p) sorry @[simp] theorem coe_vadd_const {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (p : P) : ⇑(vadd_const p) = fun (v : V) => v +ᵥ p := rfl @[simp] theorem coe_vadd_const_symm {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (p : P) : ⇑(isometric.symm (vadd_const p)) = fun (p' : P) => p' -ᵥ p := rfl @[simp] theorem vadd_const_to_equiv {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (p : P) : to_equiv (vadd_const p) = equiv.vadd_const p := rfl /-- `p' ↦ p -ᵥ p'` as an equivalence. -/ def const_vsub {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (p : P) : P ≃ᵢ V := mk (equiv.const_vsub p) sorry @[simp] theorem coe_const_vsub {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (p : P) : ⇑(const_vsub p) = has_vsub.vsub p := rfl @[simp] theorem coe_const_vsub_symm {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (p : P) : ⇑(isometric.symm (const_vsub p)) = fun (v : V) => -v +ᵥ p := rfl /-- The map `p ↦ v +ᵥ p` as an isometric automorphism of `P`. -/ def const_vadd {V : Type u_2} (P : Type u_3) [normed_group V] [metric_space P] [normed_add_torsor V P] (v : V) : P ≃ᵢ P := mk (equiv.const_vadd P v) sorry @[simp] theorem coe_const_vadd {V : Type u_2} (P : Type u_3) [normed_group V] [metric_space P] [normed_add_torsor V P] (v : V) : ⇑(const_vadd P v) = has_vadd.vadd v := rfl @[simp] theorem const_vadd_zero (V : Type u_2) (P : Type u_3) [normed_group V] [metric_space P] [normed_add_torsor V P] : const_vadd P 0 = isometric.refl P := to_equiv_inj (equiv.const_vadd_zero V P) /-- Point reflection in `x` as an `isometric` homeomorphism. -/ def point_reflection {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (x : P) : P ≃ᵢ P := isometric.trans (const_vsub x) (vadd_const x) theorem point_reflection_apply {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (x : P) (y : P) : coe_fn (point_reflection x) y = x -ᵥ y +ᵥ x := rfl @[simp] theorem point_reflection_to_equiv {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (x : P) : to_equiv (point_reflection x) = equiv.point_reflection x := rfl @[simp] theorem point_reflection_self {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (x : P) : coe_fn (point_reflection x) x = x := equiv.point_reflection_self x theorem point_reflection_involutive {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (x : P) : function.involutive ⇑(point_reflection x) := equiv.point_reflection_involutive x @[simp] theorem point_reflection_symm {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (x : P) : isometric.symm (point_reflection x) = point_reflection x := to_equiv_inj (equiv.point_reflection_symm x) @[simp] theorem dist_point_reflection_fixed {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (x : P) (y : P) : dist (coe_fn (point_reflection x) y) x = dist y x := sorry theorem dist_point_reflection_self' {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (x : P) (y : P) : dist (coe_fn (point_reflection x) y) y = norm (bit0 (x -ᵥ y)) := sorry theorem dist_point_reflection_self {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (𝕜 : Type u_1) [normed_field 𝕜] [normed_space 𝕜 V] (x : P) (y : P) : dist (coe_fn (point_reflection x) y) y = norm (bit0 1) * dist x y := sorry theorem point_reflection_fixed_iff {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] (𝕜 : Type u_1) [normed_field 𝕜] [normed_space 𝕜 V] [invertible (bit0 1)] {x : P} {y : P} : coe_fn (point_reflection x) y = y ↔ y = x := affine_equiv.point_reflection_fixed_iff_of_module 𝕜 theorem dist_point_reflection_self_real {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] [normed_space ℝ V] (x : P) (y : P) : dist (coe_fn (point_reflection x) y) y = bit0 1 * dist x y := sorry @[simp] theorem point_reflection_midpoint_left {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] [normed_space ℝ V] (x : P) (y : P) : coe_fn (point_reflection (midpoint ℝ x y)) x = y := affine_equiv.point_reflection_midpoint_left x y @[simp] theorem point_reflection_midpoint_right {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] [normed_space ℝ V] (x : P) (y : P) : coe_fn (point_reflection (midpoint ℝ x y)) y = x := affine_equiv.point_reflection_midpoint_right x y end isometric theorem lipschitz_with.vadd {α : Type u_1} {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] [emetric_space α] {f : α → V} {g : α → P} {Kf : nnreal} {Kg : nnreal} (hf : lipschitz_with Kf f) (hg : lipschitz_with Kg g) : lipschitz_with (Kf + Kg) (f +ᵥ g) := fun (x y : α) => trans_rel_left LessEq (le_trans (edist_vadd_vadd_le (f x) (f y) (g x) (g y)) (add_le_add (hf x y) (hg x y))) (Eq.symm (add_mul (↑Kf) (↑Kg) (edist x y))) theorem lipschitz_with.vsub {α : Type u_1} {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] [emetric_space α] {f : α → P} {g : α → P} {Kf : nnreal} {Kg : nnreal} (hf : lipschitz_with Kf f) (hg : lipschitz_with Kg g) : lipschitz_with (Kf + Kg) (f -ᵥ g) := fun (x y : α) => trans_rel_left LessEq (le_trans (edist_vsub_vsub_le (f x) (g x) (f y) (g y)) (add_le_add (hf x y) (hg x y))) (Eq.symm (add_mul (↑Kf) (↑Kg) (edist x y))) theorem uniform_continuous_vadd {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] : uniform_continuous fun (x : V × P) => prod.fst x +ᵥ prod.snd x := lipschitz_with.uniform_continuous (lipschitz_with.vadd lipschitz_with.prod_fst lipschitz_with.prod_snd) theorem uniform_continuous_vsub {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] : uniform_continuous fun (x : P × P) => prod.fst x -ᵥ prod.snd x := lipschitz_with.uniform_continuous (lipschitz_with.vsub lipschitz_with.prod_fst lipschitz_with.prod_snd) theorem continuous_vadd {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] : continuous fun (x : V × P) => prod.fst x +ᵥ prod.snd x := uniform_continuous.continuous uniform_continuous_vadd theorem continuous_vsub {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] : continuous fun (x : P × P) => prod.fst x -ᵥ prod.snd x := uniform_continuous.continuous uniform_continuous_vsub theorem filter.tendsto.vadd {α : Type u_1} {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] {l : filter α} {f : α → V} {g : α → P} {v : V} {p : P} (hf : filter.tendsto f l (nhds v)) (hg : filter.tendsto g l (nhds p)) : filter.tendsto (f +ᵥ g) l (nhds (v +ᵥ p)) := filter.tendsto.comp (continuous.tendsto continuous_vadd (v, p)) (filter.tendsto.prod_mk_nhds hf hg) theorem filter.tendsto.vsub {α : Type u_1} {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] {l : filter α} {f : α → P} {g : α → P} {x : P} {y : P} (hf : filter.tendsto f l (nhds x)) (hg : filter.tendsto g l (nhds y)) : filter.tendsto (f -ᵥ g) l (nhds (x -ᵥ y)) := filter.tendsto.comp (continuous.tendsto continuous_vsub (x, y)) (filter.tendsto.prod_mk_nhds hf hg) theorem continuous.vadd {α : Type u_1} {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] [topological_space α] {f : α → V} {g : α → P} (hf : continuous f) (hg : continuous g) : continuous (f +ᵥ g) := continuous.comp continuous_vadd (continuous.prod_mk hf hg) theorem continuous.vsub {α : Type u_1} {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] [topological_space α] {f : α → P} {g : α → P} (hf : continuous f) (hg : continuous g) : continuous (f -ᵥ g) := continuous.comp continuous_vsub (continuous.prod_mk hf hg) theorem continuous_at.vadd {α : Type u_1} {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] [topological_space α] {f : α → V} {g : α → P} {x : α} (hf : continuous_at f x) (hg : continuous_at g x) : continuous_at (f +ᵥ g) x := filter.tendsto.vadd hf hg theorem continuous_at.vsub {α : Type u_1} {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] [topological_space α] {f : α → P} {g : α → P} {x : α} (hf : continuous_at f x) (hg : continuous_at g x) : continuous_at (f -ᵥ g) x := filter.tendsto.vsub hf hg theorem continuous_within_at.vadd {α : Type u_1} {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] [topological_space α] {f : α → V} {g : α → P} {x : α} {s : set α} (hf : continuous_within_at f s x) (hg : continuous_within_at g s x) : continuous_within_at (f +ᵥ g) s x := filter.tendsto.vadd hf hg theorem continuous_within_at.vsub {α : Type u_1} {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] [topological_space α] {f : α → P} {g : α → P} {x : α} {s : set α} (hf : continuous_within_at f s x) (hg : continuous_within_at g s x) : continuous_within_at (f -ᵥ g) s x := filter.tendsto.vsub hf hg /-- The map `g` from `V1` to `V2` corresponding to a map `f` from `P1` to `P2`, at a base point `p`, is an isometry if `f` is one. -/ theorem isometry.vadd_vsub {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] {V' : Type u_4} {P' : Type u_5} [normed_group V'] [metric_space P'] [normed_add_torsor V' P'] {f : P → P'} (hf : isometry f) {p : P} {g : V → V'} (hg : ∀ (v : V), g v = f (v +ᵥ p) -ᵥ f p) : isometry g := sorry /-- If `f` is an affine map, then its linear part is continuous iff `f` is continuous. -/ theorem affine_map.continuous_linear_iff {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] {V' : Type u_4} {P' : Type u_5} [normed_group V'] [metric_space P'] [normed_add_torsor V' P'] {𝕜 : Type u_6} [normed_field 𝕜] [normed_space 𝕜 V] [normed_space 𝕜 V'] {f : affine_map 𝕜 P P'} : continuous ⇑(affine_map.linear f) ↔ continuous ⇑f := sorry @[simp] theorem dist_center_homothety {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] {𝕜 : Type u_6} [normed_field 𝕜] [normed_space 𝕜 V] (p₁ : P) (p₂ : P) (c : 𝕜) : dist p₁ (coe_fn (affine_map.homothety p₁ c) p₂) = norm c * dist p₁ p₂ := sorry @[simp] theorem dist_homothety_center {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] {𝕜 : Type u_6} [normed_field 𝕜] [normed_space 𝕜 V] (p₁ : P) (p₂ : P) (c : 𝕜) : dist (coe_fn (affine_map.homothety p₁ c) p₂) p₁ = norm c * dist p₁ p₂ := sorry @[simp] theorem dist_homothety_self {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] {𝕜 : Type u_6} [normed_field 𝕜] [normed_space 𝕜 V] (p₁ : P) (p₂ : P) (c : 𝕜) : dist (coe_fn (affine_map.homothety p₁ c) p₂) p₂ = norm (1 - c) * dist p₁ p₂ := sorry @[simp] theorem dist_self_homothety {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] {𝕜 : Type u_6} [normed_field 𝕜] [normed_space 𝕜 V] (p₁ : P) (p₂ : P) (c : 𝕜) : dist p₂ (coe_fn (affine_map.homothety p₁ c) p₂) = norm (1 - c) * dist p₁ p₂ := sorry @[simp] theorem dist_left_midpoint {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] {𝕜 : Type u_6} [normed_field 𝕜] [normed_space 𝕜 V] [invertible (bit0 1)] (p₁ : P) (p₂ : P) : dist p₁ (midpoint 𝕜 p₁ p₂) = norm (bit0 1)⁻¹ * dist p₁ p₂ := sorry @[simp] theorem dist_midpoint_left {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] {𝕜 : Type u_6} [normed_field 𝕜] [normed_space 𝕜 V] [invertible (bit0 1)] (p₁ : P) (p₂ : P) : dist (midpoint 𝕜 p₁ p₂) p₁ = norm (bit0 1)⁻¹ * dist p₁ p₂ := sorry @[simp] theorem dist_midpoint_right {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] {𝕜 : Type u_6} [normed_field 𝕜] [normed_space 𝕜 V] [invertible (bit0 1)] (p₁ : P) (p₂ : P) : dist (midpoint 𝕜 p₁ p₂) p₂ = norm (bit0 1)⁻¹ * dist p₁ p₂ := sorry @[simp] theorem dist_right_midpoint {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] {𝕜 : Type u_6} [normed_field 𝕜] [normed_space 𝕜 V] [invertible (bit0 1)] (p₁ : P) (p₂ : P) : dist p₂ (midpoint 𝕜 p₁ p₂) = norm (bit0 1)⁻¹ * dist p₁ p₂ := sorry /-- A continuous map between two normed affine spaces is an affine map provided that it sends midpoints to midpoints. -/ def affine_map.of_map_midpoint {V : Type u_2} {P : Type u_3} [normed_group V] [metric_space P] [normed_add_torsor V P] {V' : Type u_4} {P' : Type u_5} [normed_group V'] [metric_space P'] [normed_add_torsor V' P'] [normed_space ℝ V] [normed_space ℝ V'] (f : P → P') (h : ∀ (x y : P), f (midpoint ℝ x y) = midpoint ℝ (f x) (f y)) (hfc : continuous f) : affine_map ℝ P P' := affine_map.mk' f (↑(add_monoid_hom.to_real_linear_map (add_monoid_hom.of_map_midpoint ℝ ℝ (⇑(affine_equiv.symm (affine_equiv.vadd_const ℝ (f (classical.arbitrary P)))) ∘ f ∘ ⇑(affine_equiv.vadd_const ℝ (classical.arbitrary P))) sorry sorry) sorry)) (classical.arbitrary P) sorry end Mathlib
9ddcfb44efd3a770fc42ddc9b0796202d072bea3
c03e95bc725cd3f471d8655b0b5c3557f82c289b
/writing a tactic/refine.lean
90976ace2019e6d0214741d527a02c9578140ef6
[]
no_license
unitb/lean-tutorials
7ee8983438c64e5efc08682f6f7ca7957ee5b534
95ea03ebbd7b7162285e6971ef9728377afdca9c
refs/heads/master
1,631,165,521,602
1,520,991,342,000
1,520,991,342,000
125,005,853
0
0
null
null
null
null
UTF-8
Lean
false
false
14,345
lean
import data.dlist /- We want to write a set of tactics to automate the construction of instances. One approach might be to automate the specific instances that we are interested in. Instead, we will try to build reusable components and aim for wide applicability. Our first example of application is the construction of a `functor` instance for `(_ ⊕)`. We would like the instance to look like: ``` instance {α : Type*} : functor (sum α) := begin refineS { map := sum.map_right }, -- `refineS` focuses on structure fields field map_id { ... }, field map_comp { ... }, end ``` This structure is similar to the `cases / case id` syntax and we will use its underlying machinery to make our tactic work. We can identify two parts of the Lean API that we need to understand in order to make this work 1. structure syntax and accessing field information 2. using goal tagging Often, writing a new tactic involves using parts of the API that we're not fully familiar with so we're better get used to experimenting, making small prototypes to check if our understanding is correct. In the above example, we'd like to get the list of fields that are specified in the expression given to `refineS` and we'd like to know what are the fields that are missing in some sense. Let's start with the former. We set off to explore the core library and "grep" our way to useful functions: ``` library $ grep -R . -e structure ``` The above is a miss: most of what we're getting is the list of structure declaration. What if we included underscores? ``` library $ grep -R . -e _structure ``` It's a hit! We're getting https://github.com/leanprover/lean/blob/master/library/init/meta/pexpr.lean#L22-L31 which looks promising. We reproduce the context below: ``` /-- Information about unelaborated structure instance expressions. -/ meta structure structure_instance_info := (struct : option name := none) (field_names : list name) (field_values : list pexpr) (sources : list pexpr := []) /-- Create a structure instance expression. -/ meta constant pexpr.mk_structure_instance : structure_instance_info → pexpr meta constant pexpr.get_structure_instance_info : pexpr → option structure_instance_info ``` Let's still try one more regex: ``` library $ grep -R . -e structure_ ``` We get more interesting stuff: https://github.com/leanprover/lean/blob/master/library/init/meta/environment.lean#L86-L87 ``` /-- Return the fields of the structure with the given name, or `none` if it is not a structure -/ meta constant structure_fields : environment → name → option (list name) ``` Let us see what `pexpr.get_structure_instance_info` tells us about `{ map := sum.map_right }`. We start by writing our tactic's type: -/ open tactic universes u v example {α : Type u} : functor (sum α) := begin (do let e : pexpr := ``({ map := sum.map_right }), str ← e.get_structure_instance_info, trace str.field_names, trace str.field_values), -- prints: -- [map] -- [field_notation sum] admit end /- It's a success! It's important to see that `refineS₀` does no type checking on the expression we given it. This is why we get no error messages for `sum.map_right` not existing yet. `refineS₀`'s `e` argument is of type `parse texpr` which is definitionally equal to `pexpr`, the result of parsing an expression but doing no elaboration or type checking. In `parse texpr`, the type of `texpr` is `texpr : lean.parser pexpr` where `lean.parser` is the parsing monad. Without going into details into `monads` as a programming construct or a mathematical object, objects of type `lean.parser α` are programs with access to the Lean parsing infrastructure including the current position of the parser and various parsing routines and the result of the program is of type `α`. Lean takes `parse texpr` as a hint that the tactic will be used interactively (as opposed to called by other tactics) and `texpr` is the parser to use to construct argument `e`. Other useful parsers can be found at https://github.com/leanprover/lean/blob/master/library/init/meta/interactive_base.lean#L69-L80. Our next experiment is to list the expected fields for the type that we are attempting to build. `environment.structure_fields` will help us in this: ``` meta constant structure_fields : environment → name → option (list name) ``` What argument do we give it? Searching in `init/meta/tactic.lean` shows us that `get_env` will provide the first argument. The second argument is the type of the object that we're trying to build. More specifically, the name of that type. If we call `target`, that type will be provided to us as an expression. In our case `functor (sum α)`. This expression contains multiple names. Which one do we want? Let us look at it closely by using `to_raw_fmt` to avoid any pretty printing and just get the syntax tree: -/ example {α : Type u} : functor.{v} (sum α) := -- (we use `.{v}` above so that Lean won't have to guess the universes which would -- create visual noise begin (do let e : pexpr := ``({ map := sum.map_right }), tgt ← target, trace tgt.to_raw_fmt), -- prints: -- (app (const functor [v, max u v]) -- (app (const sum [u, v]) -- (local_const _ α (const 1 [])))) admit end /- The printout shows us that our current type of interest is a function application whose function is `functor` and whose argument is another application. We don't actually care about any of the structure of the argument. The function is not always exactly what we care about either. Consider for example `vector_space α β`. The function in the top-most application is `vector_space α` so we have to look at its function in order to land on `vector_space` itself. Does that mean that we have to look at the function of the function? Let us consider a type class with the three arguments: `my_class α β γ`. The function of its function is `my_class α`. In short, we should keep retrieving functions until we run out of function applications. The result should be a constant and the name of that constant is what we need. If it is anything else than a constant, the user made a mistake and we will say no more of it here but various approaches can be taken for informative error messages. In `init/meta/expr.lean`, in the core library a function is provided to retrieve a function in a multi-argument function application: `expr.get_app_fn`. It gives us another expression which we expect should be a constant. `expr.const_name` will reveal it's name. -/ open nat meta def mk_mvar_list : ℕ → tactic (list expr) | 0 := pure [] | (succ n) := (::) <$> mk_mvar <*> mk_mvar_list n open interactive interactive.types lean.parser tactic.interactive (itactic) -- meta def tactic.interactive.refineS (e : parse texpr) : tactic unit := -- do str ← e.get_structure_instance_info, -- trace str.struct, -- trace str.sources, -- trace str.field_names, -- tgt ← target, -- let struct_n : name := tgt.get_app_fn.const_name, -- env ← get_env, -- exp_fields ← env.structure_fields struct_n, -- let missing_f := (exp_fields.diff str.field_names).diff [`to_has_map], -- vs ← mk_mvar_list missing_f.length, -- let e' : pexpr := pexpr.mk_structure_instance -- { -- struct := some struct_n -- -- , -- field_names := str.field_names ++ missing_f -- , field_values := str.field_values ++ vs.map to_pexpr }, -- refine ``(%%e' : %%tgt) meta def var_names : expr → list name | (expr.pi n _ _ b) := n :: var_names b | _ := [] meta def drop_binders : expr → tactic expr | (expr.pi n bi t b) := b.instantiate_var <$> mk_local' n bi t >>= drop_binders | e := pure e meta def subobject_names (struct_n : name) : tactic (list name × list name) := do env ← get_env, [c] ← pure $ env.constructors_of struct_n | fail "too many constructors", vs ← var_names <$> (mk_const c >>= infer_type), fields ← env.structure_fields struct_n, return $ fields.partition (λ fn, ↑("_" ++ fn.to_string) ∈ vs) def dlist.join {α} : list (dlist α) → dlist α | [] := dlist.empty | (x :: xs) := x ++ dlist.join xs meta def expanded_field_list' : name → tactic (dlist $ name × name) | struct_n := do (so,fs) ← subobject_names struct_n, ts ← so.mmap (λ n, do e ← mk_const (n.update_prefix struct_n) >>= infer_type >>= drop_binders, expanded_field_list' $ e.get_app_fn.const_name), -- trace so, trace fs, return $ dlist.join ts ++ dlist.of_list (fs.map $ prod.mk struct_n) meta def expanded_field_list (struct_n : name) : tactic (list $ name × name) := dlist.to_list <$> expanded_field_list' struct_n def mmap₂ {α β γ} {m : Type u → Type v} [applicative m] (f : α → β → m γ) : list α → list β → m (list γ) | [] _ := pure [] | _ [] := pure [] | (x :: xs) (y :: ys) := (::) <$> f x y <*> mmap₂ xs ys def mmap₂' {α β γ} {m : Type u → Type v} [applicative m] (f : α → β → m γ) : list α → list β → m punit | [] _ := pure punit.star | _ [] := pure punit.star | (x :: xs) (y :: ys) := f x y *> mmap₂' xs ys def sum.map_right {α β β'} (f : β → β') : α ⊕ β → α ⊕ β' | (sum.inr x) := sum.inr $ f x | (sum.inl x) := sum.inl x meta def tactic.interactive.refineS (e : parse texpr) (ph : parse $ optional $ tk "with" *> ident) : tactic unit := do str ← e.get_structure_instance_info, tgt ← target, let struct_n : name := tgt.get_app_fn.const_name, exp_fields ← expanded_field_list struct_n, let exp_fields' := exp_fields.map prod.snd, let missing_f := exp_fields.filter (λ f, (f.2 : name) ∉ str.field_names), let provided := exp_fields.filter (λ f, (f.2 : name) ∈ str.field_names), vs ← mk_mvar_list missing_f.length, let e' : pexpr := pexpr.mk_structure_instance { struct := some struct_n , field_names := str.field_names ++ missing_f.map prod.snd , field_values := str.field_values ++ vs.map to_pexpr }, refine e', gs ← with_enable_tags ( mmap₂ (λ (n : name × name) v, do set_goals [v], try (interactive.unfold (provided.map $ λ ⟨s,f⟩, f.update_prefix s) (loc.ns [none])), apply_auto_param <|> apply_opt_param <|> (do match ph with | (some ph) := () <$ (mk_const (n.2.update_prefix n.1) >>= pose ph none) | none := return () end, set_main_tag [`_field,n.2]), get_goals) missing_f vs), set_goals gs.join meta def collect_tagged_goals (pre : name) : tactic (list expr) := do gs ← get_goals, gs.mfoldr (λ g r, do pre' :: t ← get_tag g, if t = [pre] ∧ pre' = pre'.get_prefix <.> "_field" then return (g::r) else return r) [] -- meta def match_field_tag meta def tactic.interactive.field (tag : parse ident) (tac : itactic) : tactic unit := do ts ← collect_tagged_goals tag, match ts with | [] := fail format!"no field goal with tag {tag}" | [g] := do gs ← get_goals, set_goals $ g :: gs.filter (≠ g), solve1 tac | _ := fail format!"multiple goals have tag {tag}" end universe w instance {α : Type*} : functor (sum α) := -- (we use `.{v}` above so that Lean won't have to guess the universes which would -- create visual noise begin refineS { map := @sum.map_right _, .. } with law, all_goals { intros, casesm _ ⊕ _ ; refl }, end structure except_t (f : Type u → Type v) (α : Type u) := (run : f (string ⊕ α)) open has_map functor def except_t.map {F} [functor F] {α β} (f : α → β) : except_t F α → except_t F β | ⟨ x ⟩ := ⟨ @has_map.map _ sum.functor.to_has_map _ _ f <$> x ⟩ lemma map_comp' {f : Type u → Type v} [functor f] {α β γ : Type u} (g : β → γ) (h : α → β) : map g ∘ map h = (map (g ∘ h) : f α → f γ) := by funext x ; rw [map_comp] lemma map_id' {f : Type u → Type v} [functor f] {α} : (map id : f α → f α) = id := by funext x ; rw [id_map,id] example {α : Type u} {f : Type u → Type w} [functor f] : functor (except_t f) := begin refineS { map := @except_t.map _ _ } with law ; intros ; casesm except_t f _ ; simp [except_t.map,map_id'], rw [← law,map_comp'], end example {α : Type u} : functor.{v} (sum α) := -- (we use `.{v}` above so that Lean won't have to guess the universes which would -- create visual noise begin (do let e : pexpr := ``({ map := sum.map_right }), tgt ← target, let struct_n : name := tgt.get_app_fn.const_name, trace struct_n, -- what are the fields of `functor`? env ← get_env, trace $ env.structure_fields struct_n), -- prints: -- functor -- (some [to_has_map, map_const_eq, id_map, map_comp]) -- refine { map := sorry, .. }, -- refineS { map := sorry }, -- (do fs ← expanded_field_list `applicative, -- trace fs ), refineS { map := @sum.map_right _, .. } with field, -- field map_const_eq { dunfold has_map.map, tactic.apply_auto_param }, field map_comp { intros, casesm _ ⊕ _ ; refl }, field id_map { intros, casesm _ ⊕ _ ; refl }, -- trace_result -- (do let e : pexpr := ``({ map := @sum.map_right _ }), -- str ← e.get_structure_instance_info, -- tgt ← target, -- let struct_n : name := tgt.get_app_fn.const_name, -- exp_fields ← expanded_field_list struct_n, -- let missing_f := exp_fields.diff str.field_names, -- vs ← mk_mvar_list missing_f.length, -- trace str.field_values, -- trace str.field_names, -- trace missing_f, -- let e' : pexpr := pexpr.mk_structure_instance -- { struct := some struct_n -- , field_names := str.field_names ++ missing_f -- , field_values := str.field_values ++ vs.map to_pexpr }, -- trace e', -- refine e'), -- admit, end
9fa7d7f7c02bb21c80d99d12eddb367a3bb6102e
a523fc1740c8cb84cd0fa0f4b52a760da4e32a5c
/src/missing_mathlib/data/polynomial.lean
4c1906e3f44b6bd981e348f317db0242223317e2
[]
no_license
abentkamp/spectral
a1aff51e85d30b296a81d256ced1d382345d3396
751645679ef1cb6266316349de9e492eff85484c
refs/heads/master
1,669,994,798,064
1,597,591,646,000
1,597,591,646,000
287,544,072
0
0
null
null
null
null
UTF-8
Lean
false
false
2,433
lean
import data.polynomial import missing_mathlib.ring_theory.algebra universe variables u v w namespace polynomial variables {α : Type u} {β : Type v} open polynomial lemma leading_coeff_X_add_C {α : Type v} [integral_domain α] [decidable_eq α] (a b : α) (ha : a ≠ 0): leading_coeff (C a * X + C b) = a := begin rw [add_comm, leading_coeff_add_of_degree_lt], { simp }, { simp [degree_C ha], apply lt_of_le_of_lt degree_C_le (with_bot.coe_lt_coe.2 zero_lt_one)} end end polynomial section eval₂ variables {α : Type u} {β : Type v} [comm_ring α] [decidable_eq α] [semiring β] variables (f : α →+* β) (x : β) (p q : polynomial α) open is_semiring_hom open polynomial finsupp finset lemma eval₂_mul_noncomm (hf : ∀ b a, a * f b = f b * a) : (p * q).eval₂ f x = p.eval₂ f x * q.eval₂ f x := begin dunfold eval₂, rw [add_monoid_algebra.mul_def, finsupp.sum_mul _ p], simp only [finsupp.mul_sum _ q], rw [sum_sum_index], { apply sum_congr rfl, assume i hi, dsimp only, rw [sum_sum_index], { apply sum_congr rfl, assume j hj, dsimp only, rw [sum_single_index, is_semiring_hom.map_mul f, pow_add], { rw [mul_assoc, ←mul_assoc _ (x ^ i), ← hf _ (x ^ i)], simp only [mul_assoc] }, { rw [is_semiring_hom.map_zero f, zero_mul] } }, { intro, rw [is_semiring_hom.map_zero f, zero_mul] }, { intros, rw [is_semiring_hom.map_add f, add_mul] } }, { intro, rw [is_semiring_hom.map_zero f, zero_mul] }, { intros, rw [is_semiring_hom.map_add f, add_mul] } end end eval₂ lemma finsupp_sum_eq_eval₂ (α : Type v) (β : Type w) [decidable_eq α] [comm_ring α] [decidable_eq β] [add_comm_group β] [module α β] (f : β →ₗ[α] β) (v : β) (p : polynomial α) : (finsupp.sum p (λ n b, b • (f ^ n) v)) = polynomial.eval₂ (algebra_map α (β →ₗ[α] β)) f p v := begin dunfold polynomial.eval₂ finsupp.sum, convert @finset.sum_hom _ _ _ _ _ p.support _ (λ h : β →ₗ[α] β, h v) _, simp [module.endomorphism_algebra_map_apply] end lemma eval₂_prod_noncomm {α β : Type*} [comm_ring α] [decidable_eq α] [semiring β] (f : α →+* β) (hf : ∀ b a, a * f b = f b * a) (x : β) (ps : list (polynomial α)) : polynomial.eval₂ f x ps.prod = (ps.map (λ p, (polynomial.eval₂ f x p))).prod := begin induction ps, simp, simp [eval₂_mul_noncomm f _ _ _ hf, ps_ih] {contextual := tt} end
3b75d6ab419dda385068ae44a32864410a2810ae
b6f0d4562078d09b2d51c6aa5216cf0e07e8090f
/LeanRanges/NatRange.lean
feadb9c266114bacbbc923aff79c0d3bbc14af06
[ "Apache-2.0" ]
permissive
pnwamk/lean4-ranges
206a46e0ded663f546927f598549efacc36492f2
6c6a7e21edc1c2ad319749b75a222d77b1340f7d
refs/heads/master
1,680,233,414,507
1,617,384,186,000
1,617,384,186,000
349,486,531
1
0
null
null
null
null
UTF-8
Lean
false
false
2,055
lean
import LeanRanges.ToRange namespace Nat instance : HasMin Nat where minOf _ := 0 end Nat structure NatRange where start : Nat stop : Nat step : Nat ascending : Bool /-- An exclusive range `[start,stop)` of `Nat` values separated by `step`. -/ def Nat.range (start stop : Nat) (step : Nat := 1) (ascending : Bool := true) : NatRange := ⟨start, stop, step, ascending⟩ instance : ToRange Nat NatRange where toRange start stop step := Nat.range start stop step.natAbs (step >= 0) toRangeEq start stop step := Nat.range start (stop+1) step.natAbs (step >= 0) namespace NatRange universes u v @[inline] def steps (r : NatRange) : Nat := if r.step = 0 then 0 else let size := r.stop - r.start size / r.step + (if size % r.step = 0 then 0 else 1) def next (r : NatRange) (i : Nat) : Nat := if r.ascending then i + r.step else i - r.step def first (r : NatRange) : Nat := if r.ascending then r.start else r.start + (r.step * (r.steps - 1)) def reverse (r : NatRange) : NatRange := {r with ascending := !r.ascending} @[inline] def forIn {β : Type u} {m : Type u → Type v} [Monad m] (r : NatRange) (init : β) (f : Nat → β → m (ForInStep β)) : m β := let rec @[specialize] loop (fuel i : Nat) (b : β) : m β := do match fuel with | 0 => pure b | fuel+1 => match ← f i b with | ForInStep.done b => pure b | ForInStep.yield b => loop fuel (r.next i) b loop r.steps r.first init instance : ForIn m NatRange Nat where forIn := forIn @[inline] protected def forM {m : Type u → Type v} [Monad m] (r : NatRange) (f : Nat → m PUnit) : m PUnit := let rec @[specialize] loop (fuel i : Nat) : m PUnit := do match fuel with | 0 => pure ⟨⟩ | fuel+1 => f i; loop fuel (r.next i) loop r.steps r.first def toArray (r : NatRange) : Array Nat := do let mut arr := #[] for i in r do arr := arr.push i arr def toList (r : NatRange) : List Nat := do let mut l := [] for i in r.reverse do l := i::l l end NatRange
9af7dd0bc0f2a06882b32b1ca2ea9805b2e7057c
43390109ab88557e6090f3245c47479c123ee500
/src/vector_space.lean
0f7fe99a47249e5da4ad8cc00db2723e165551db
[ "Apache-2.0" ]
permissive
Ja1941/xena-UROP-2018
41f0956519f94d56b8bf6834a8d39473f4923200
b111fb87f343cf79eca3b886f99ee15c1dd9884b
refs/heads/master
1,662,355,955,139
1,590,577,325,000
1,590,577,325,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,476
lean
structure vector (R : Type) := vec :: (x : R) (y : R) (z : R) namespace vector variables {R : Type} [comm_ring R] def zero : vector R := vec 0 0 0 def add (a b : vector R) := vec (a.x + b.x) (a.y + b.y) (a.z + b.z) def neg (a : vector R) := vec (-a.x) (-a.y) (-a.z) def cross_prod (a b : vector R) := vec (a.y * b.z - a.z * b.y) (a.x * b.z - a.z * b.x) (a.x * b.y - a.y * b.x) def inner_prod (a b : vector R) := a.x * b.x + a.y * b.y +a.z * b.z def scalar (n : R) (a : vector R) := vec (n * a.x) (n * a.y) (n * a.z) theorem vector.ext (α : Type) (a b c d e f : α) : a = d → b = e → c = f → vec a b c = vec d e f := begin intro Had, intro Hbe, intro Hcf, rw [Had,Hbe,Hcf] end theorem vector.add_comm : ∀ (a b : vector R) , add a b = add b a := begin intro a , intro b , unfold add, simp end theorem vector.add_asso : ∀ (a b c : vector R) , add a (add b c) = add (add a b) c := begin intro a , intro b , intro c , unfold add, simp end theorem vector.inner_prod_comm : ∀ (a b : vector R) , inner_prod a b = inner_prod b a := begin intro a , intro b , unfold inner_prod, rw [mul_comm, add_assoc, add_comm], rw [mul_comm, add_assoc, add_comm], rw mul_comm, simp end theorem vector.zero_zero : ∀ (a : vector R) , add zero a = add a zero := begin intro a, unfold add, unfold zero, simp, end theorem vector.zero_add : ∀ (a : vector R) , add zero a = a := begin intro a, unfold add, unfold zero, cases a with a.x a.y a.z, dsimp, congr, rw zero_add, rw zero_add, rw zero_add end theorem vector.add_zero : ∀ (a : vector R) , add a zero = a := begin intro a, unfold add, unfold zero, cases a with a.x a.y a.z, dsimp, simp end theorem vector.vec_assoc : ∀ (a b : vector R) (n : R) , scalar n (add a b) = add (scalar n a) (scalar n b) := begin intro a, intro b, intro n, unfold add, unfold scalar, dsimp, congr, rw mul_add, rw mul_add, rw mul_add end theorem vector.num_assoc : ∀ (a : vector R) (m n : R) , scalar (m + n) a = add (scalar m a) (scalar n a) := begin intro a, intro m, intro n, unfold add, unfold scalar, dsimp, congr, rw add_mul, rw add_mul, rw add_mul end theorem vector.cross_pord_comm : ∀ (a b : vector R) , neg (cross_prod b a) = cross_prod a b := begin intro a, intro b, unfold neg, unfold cross_prod, dsimp, congr, simp, rw [mul_comm, add_comm, mul_comm], simp, rw [mul_comm, add_comm, mul_comm], simp, rw [mul_comm, add_comm, mul_comm], simp, end theorem mul_comm1 : ∀ (a b c : R) , a * b * c = c * a * b := begin intro a, intro b, intro c, rw [mul_comm, ← mul_assoc] end theorem mul_comm2 : ∀ (a b c : R) , a * b * c = b * c *a := begin intro a, intro b, intro c, rw [mul_assoc, mul_comm] end theorem Jacobian_Identity : ∀ (a b c : vector R) , add (cross_prod a (cross_prod b c)) (add (cross_prod b (cross_prod c a)) (cross_prod c (cross_prod a b))) = zero := begin intro a, intro b, intro c, unfold add, unfold cross_prod, unfold zero, dsimp, congr, rw [mul_add, mul_add, mul_add, mul_add, mul_add, mul_add], rw mul_comm, simp, rw mul_comm, simp, rw mul_comm, simp, rw mul_comm, simp, rw mul_comm, simp, rw mul_comm, simp, rw [mul_comm1, add_comm, add_assoc], rw [mul_comm1, add_comm, add_assoc, add_assoc], rw [mul_comm1, add_comm, add_assoc, add_assoc], rw [mul_comm2, add_comm, add_assoc, add_assoc], rw [mul_comm2, add_comm, add_assoc, add_assoc], rw [mul_comm2, add_comm, add_assoc, add_assoc], rw [mul_comm, add_comm, add_assoc, add_assoc], rw [mul_comm, add_comm, add_assoc, add_assoc], rw [mul_comm, add_comm, add_assoc, add_assoc], rw [mul_comm, add_comm, add_assoc, add_assoc], rw [mul_comm, add_comm, add_assoc], rw [mul_comm, add_comm], simp, rw [mul_add, mul_add, mul_add, mul_add, mul_add, mul_add], rw mul_comm, simp, rw mul_comm, simp, rw mul_comm, simp, rw mul_comm, simp, rw mul_comm, simp, rw mul_comm, simp, rw [mul_comm2, add_comm, add_assoc], rw [mul_comm2, add_comm, add_assoc, add_assoc], rw [mul_comm2, add_comm, add_assoc, add_assoc], rw [mul_comm2, add_comm, add_assoc, add_assoc], rw [mul_comm2, add_comm, add_assoc, add_assoc], rw [mul_comm2, add_comm, add_assoc, add_assoc], rw [mul_comm, add_comm, add_assoc, add_assoc], rw [mul_comm, add_comm, add_assoc, add_assoc], rw [mul_comm, add_comm, add_assoc, add_assoc], rw [mul_comm, add_comm, add_assoc, add_assoc], rw [mul_comm, add_comm, add_assoc], rw [mul_comm, add_comm], simp, rw [mul_add, mul_add, mul_add, mul_add, mul_add, mul_add], rw mul_comm, simp, rw mul_comm, simp, rw mul_comm, simp, rw mul_comm, simp, rw mul_comm, simp, rw mul_comm, simp, rw [mul_comm2, add_comm, add_assoc], rw [mul_comm2, add_comm, add_assoc, add_assoc], rw [mul_comm2, add_comm, add_assoc, add_assoc], rw [mul_comm1, add_comm, add_assoc, add_assoc], rw [mul_comm1, add_comm, add_assoc, add_assoc], rw [mul_comm1, add_comm, add_assoc, add_assoc], rw [mul_comm, add_comm, add_assoc, add_assoc], rw [mul_comm, add_comm, add_assoc, add_assoc], rw [mul_comm, add_comm, add_assoc, add_assoc], rw [mul_comm, add_comm, add_assoc, add_assoc], rw [mul_comm, add_comm, add_assoc], rw [mul_comm, add_comm], simp, end end vector
bda6ee19f3d0ab93d50459e2b8324eac041f648f
e21db629d2e37a833531fdcb0b37ce4d71825408
/src/mcl/compute_list.lean
0776a1dec553b10bd9800a05b522b29dde2375fc
[]
no_license
fischerman/GPU-transformation-verifier
614a28cb4606a05a0eb27e8d4eab999f4f5ea60c
75a5016f05382738ff93ce5859c4cfa47ccb63c1
refs/heads/master
1,586,985,789,300
1,579,290,514,000
1,579,290,514,000
165,031,073
1
0
null
null
null
null
UTF-8
Lean
false
false
3,144
lean
import parlang.defs import mcl.defs open parlang open parlang.state open parlang.thread_state open mcl def compute_list {σ ι : Type} {τ : ι → Type} [decidable_eq ι] : list (σ → σ) → (thread_state σ τ → thread_state σ τ) | (f :: tl) := compute_list tl ∘ @compute σ ι τ _ f | [] := id lemma compute_to_compute_list {σ ι : Type} {τ : ι → Type} [decidable_eq ι] (f : σ → σ) : @compute σ ι τ _ f = compute_list [f] := by refl lemma compute_list_merge {σ ι : Type} {τ : ι → Type} [decidable_eq ι] (f g : list (σ → σ)) : (@compute_list σ ι τ _ g) ∘ compute_list f = compute_list (f ++ g) := begin induction f, case list.nil { simp [compute_list], }, { simp [compute_list], rw ← f_ih, } end lemma compute_list_accesses {sig : signature} {n} (s : state n (memory $ parlang_mcl_tlocal sig) (parlang_mcl_shared sig)) (i) (computes) (tid) : i ∉ (compute_list computes (vector.nth (s.threads) tid)).accesses ↔ i ∉ (vector.nth (s.threads) tid).accesses := sorry -- todo: rewrite to s = { tlocal := ... s.tlocal, ..s } lemma compute_list_tlocal {sig : signature} {computes : list (memory (parlang_mcl_tlocal sig) → memory (parlang_mcl_tlocal sig))} {tlocal loads stores} {shared : memory $ parlang_mcl_shared sig} : compute_list computes {tlocal := tlocal, shared := shared, loads := loads, stores := stores} = {tlocal := computes.foldl (λ tl com, com tl) tlocal, shared := shared, loads := loads, stores := stores} := begin induction computes generalizing tlocal, { refl, }, { simp [compute_list, compute, computes_ih], } end @[simp] lemma compute_list_stores_core {sig : signature} {computes} {ts : thread_state (memory $ parlang_mcl_tlocal sig) (parlang_mcl_shared sig)} : (compute_list computes ts).stores = ts.stores := begin induction computes generalizing ts, { simp [compute_list], }, { cases ts, simp [compute_list, compute], rw computes_ih, }, end @[simp] lemma compute_list_loads_core {sig : signature} {computes} {ts : thread_state (memory $ parlang_mcl_tlocal sig) (parlang_mcl_shared sig)} : (compute_list computes ts).loads = ts.loads := begin induction computes generalizing ts, { simp [compute_list], }, { cases ts, simp [compute_list, compute], rw computes_ih, }, end @[simp] lemma compute_list_shared {sig : signature} {computes} {ts : thread_state (memory $ parlang_mcl_tlocal sig) (parlang_mcl_shared sig)} : (compute_list computes ts).shared = ts.shared := begin induction computes generalizing ts, { simp [compute_list], }, { cases ts, simp [compute_list, compute], rw computes_ih, }, end lemma compute_list_stores {sig : signature} {computes} {ts : thread_state (memory $ parlang_mcl_tlocal sig) (parlang_mcl_shared sig)} {i : mcl_address sig} : i ∉ ts.stores ↔ i ∉ (compute_list computes ts).stores := by simp lemma compute_list_loads {sig : signature} {computes} {ts : thread_state (memory $ parlang_mcl_tlocal sig) (parlang_mcl_shared sig)} {i : mcl_address sig} : i ∉ ts.loads ↔ i ∉ (compute_list computes ts).loads := by simp
425c1f9a84876e5122efc77a2ec6831839802e01
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/euclidean_domain.lean
804a774569455d0437b0e22fea176feb5d9a0fa5
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
13,045
lean
/- Copyright (c) 2018 Louis Carlin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Louis Carlin, Mario Carneiro Euclidean domains and Euclidean algorithm (extended to come) A lot is based on pre-existing code in mathlib for natural number gcds -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.int.basic import Mathlib.algebra.field import Mathlib.PostPort universes u l u_1 namespace Mathlib /-- A `euclidean_domain` is an `integral_domain` with a division and a remainder, satisfying `b * (a / b) + a % b = a`. The definition of a euclidean domain usually includes a valuation function `R → ℕ`. This definition is slightly generalised to include a well founded relation `r` with the property that `r (a % b) b`, instead of a valuation. -/ class euclidean_domain (R : Type u) extends comm_ring R, nontrivial R where quotient : R → R → R quotient_zero : ∀ (a : R), quotient a 0 = 0 remainder : R → R → R quotient_mul_add_remainder_eq : ∀ (a b : R), b * quotient a b + remainder a b = a r : R → R → Prop r_well_founded : well_founded r remainder_lt : ∀ (a : R) {b : R}, b ≠ 0 → r (remainder a b) b mul_left_not_lt : ∀ (a : R) {b : R}, b ≠ 0 → ¬r (a * b) a namespace euclidean_domain protected instance has_div {R : Type u} [euclidean_domain R] : Div R := { div := euclidean_domain.quotient } protected instance has_mod {R : Type u} [euclidean_domain R] : Mod R := { mod := euclidean_domain.remainder } theorem div_add_mod {R : Type u} [euclidean_domain R] (a : R) (b : R) : b * (a / b) + a % b = a := euclidean_domain.quotient_mul_add_remainder_eq a b theorem mod_add_div {R : Type u} [euclidean_domain R] (a : R) (b : R) : a % b + b * (a / b) = a := Eq.trans (add_comm (a % b) (b * (a / b))) (div_add_mod a b) theorem mod_eq_sub_mul_div {R : Type u_1} [euclidean_domain R] (a : R) (b : R) : a % b = a - b * (a / b) := Eq.trans (Eq.symm (add_sub_cancel' (b * (a / b)) (a % b))) (eq.mpr (id (Eq._oldrec (Eq.refl (b * (a / b) + a % b - b * (a / b) = a - b * (a / b))) (div_add_mod a b))) (Eq.refl (a - b * (a / b)))) theorem mod_lt {R : Type u} [euclidean_domain R] (a : R) {b : R} : b ≠ 0 → euclidean_domain.r (a % b) b := euclidean_domain.remainder_lt theorem mul_right_not_lt {R : Type u} [euclidean_domain R] {a : R} (b : R) (h : a ≠ 0) : ¬euclidean_domain.r (a * b) b := eq.mpr (id (Eq._oldrec (Eq.refl (¬euclidean_domain.r (a * b) b)) (mul_comm a b))) (mul_left_not_lt b h) theorem mul_div_cancel_left {R : Type u} [euclidean_domain R] {a : R} (b : R) (a0 : a ≠ 0) : a * b / a = b := sorry theorem mul_div_cancel {R : Type u} [euclidean_domain R] (a : R) {b : R} (b0 : b ≠ 0) : a * b / b = a := eq.mpr (id (Eq._oldrec (Eq.refl (a * b / b = a)) (mul_comm a b))) (mul_div_cancel_left a b0) @[simp] theorem mod_zero {R : Type u} [euclidean_domain R] (a : R) : a % 0 = a := sorry @[simp] theorem mod_eq_zero {R : Type u} [euclidean_domain R] {a : R} {b : R} : a % b = 0 ↔ b ∣ a := sorry @[simp] theorem mod_self {R : Type u} [euclidean_domain R] (a : R) : a % a = 0 := iff.mpr mod_eq_zero (dvd_refl a) theorem dvd_mod_iff {R : Type u} [euclidean_domain R] {a : R} {b : R} {c : R} (h : c ∣ b) : c ∣ a % b ↔ c ∣ a := eq.mpr (id (Eq._oldrec (Eq.refl (c ∣ a % b ↔ c ∣ a)) (propext (dvd_add_iff_right (dvd_mul_of_dvd_left h (a / b)))))) (eq.mpr (id (Eq._oldrec (Eq.refl (c ∣ b * (a / b) + a % b ↔ c ∣ a)) (div_add_mod a b))) (iff.refl (c ∣ a))) theorem lt_one {R : Type u} [euclidean_domain R] (a : R) : euclidean_domain.r a 1 → a = 0 := sorry theorem val_dvd_le {R : Type u} [euclidean_domain R] (a : R) (b : R) : b ∣ a → a ≠ 0 → ¬euclidean_domain.r a b := sorry @[simp] theorem mod_one {R : Type u} [euclidean_domain R] (a : R) : a % 1 = 0 := iff.mpr mod_eq_zero (one_dvd a) @[simp] theorem zero_mod {R : Type u} [euclidean_domain R] (b : R) : 0 % b = 0 := iff.mpr mod_eq_zero (dvd_zero b) @[simp] theorem div_zero {R : Type u} [euclidean_domain R] (a : R) : a / 0 = 0 := euclidean_domain.quotient_zero a @[simp] theorem zero_div {R : Type u} [euclidean_domain R] {a : R} : 0 / a = 0 := sorry @[simp] theorem div_self {R : Type u} [euclidean_domain R] {a : R} (a0 : a ≠ 0) : a / a = 1 := sorry theorem eq_div_of_mul_eq_left {R : Type u} [euclidean_domain R] {a : R} {b : R} {c : R} (hb : b ≠ 0) (h : a * b = c) : a = c / b := eq.mpr (id (Eq._oldrec (Eq.refl (a = c / b)) (Eq.symm h))) (eq.mpr (id (Eq._oldrec (Eq.refl (a = a * b / b)) (mul_div_cancel a hb))) (Eq.refl a)) theorem eq_div_of_mul_eq_right {R : Type u} [euclidean_domain R] {a : R} {b : R} {c : R} (ha : a ≠ 0) (h : a * b = c) : b = c / a := eq.mpr (id (Eq._oldrec (Eq.refl (b = c / a)) (Eq.symm h))) (eq.mpr (id (Eq._oldrec (Eq.refl (b = a * b / a)) (mul_div_cancel_left b ha))) (Eq.refl b)) theorem mul_div_assoc {R : Type u} [euclidean_domain R] (x : R) {y : R} {z : R} (h : z ∣ y) : x * y / z = x * (y / z) := sorry theorem gcd.induction {R : Type u} [euclidean_domain R] {P : R → R → Prop} (a : R) (b : R) : (∀ (x : R), P 0 x) → (∀ (a b : R), a ≠ 0 → P (b % a) a → P a b) → P a b := sorry /-- `gcd a b` is a (non-unique) element such that `gcd a b ∣ a` `gcd a b ∣ b`, and for any element `c` such that `c ∣ a` and `c ∣ b`, then `c ∣ gcd a b` -/ def gcd {R : Type u} [euclidean_domain R] [DecidableEq R] : R → R → R := sorry @[simp] theorem gcd_zero_left {R : Type u} [euclidean_domain R] [DecidableEq R] (a : R) : gcd 0 a = a := eq.mpr (id (Eq._oldrec (Eq.refl (gcd 0 a = a)) (gcd.equations._eqn_1 0))) (if_pos rfl) @[simp] theorem gcd_zero_right {R : Type u} [euclidean_domain R] [DecidableEq R] (a : R) : gcd a 0 = a := sorry theorem gcd_val {R : Type u} [euclidean_domain R] [DecidableEq R] (a : R) (b : R) : gcd a b = gcd (b % a) a := sorry theorem gcd_dvd {R : Type u} [euclidean_domain R] [DecidableEq R] (a : R) (b : R) : gcd a b ∣ a ∧ gcd a b ∣ b := sorry theorem gcd_dvd_left {R : Type u} [euclidean_domain R] [DecidableEq R] (a : R) (b : R) : gcd a b ∣ a := and.left (gcd_dvd a b) theorem gcd_dvd_right {R : Type u} [euclidean_domain R] [DecidableEq R] (a : R) (b : R) : gcd a b ∣ b := and.right (gcd_dvd a b) protected theorem gcd_eq_zero_iff {R : Type u} [euclidean_domain R] [DecidableEq R] {a : R} {b : R} : gcd a b = 0 ↔ a = 0 ∧ b = 0 := sorry theorem dvd_gcd {R : Type u} [euclidean_domain R] [DecidableEq R] {a : R} {b : R} {c : R} : c ∣ a → c ∣ b → c ∣ gcd a b := sorry theorem gcd_eq_left {R : Type u} [euclidean_domain R] [DecidableEq R] {a : R} {b : R} : gcd a b = a ↔ a ∣ b := sorry @[simp] theorem gcd_one_left {R : Type u} [euclidean_domain R] [DecidableEq R] (a : R) : gcd 1 a = 1 := iff.mpr gcd_eq_left (one_dvd a) @[simp] theorem gcd_self {R : Type u} [euclidean_domain R] [DecidableEq R] (a : R) : gcd a a = a := iff.mpr gcd_eq_left (dvd_refl a) /-- An implementation of the extended GCD algorithm. At each step we are computing a triple `(r, s, t)`, where `r` is the next value of the GCD algorithm, to compute the greatest common divisor of the input (say `x` and `y`), and `s` and `t` are the coefficients in front of `x` and `y` to obtain `r` (i.e. `r = s * x + t * y`). The function `xgcd_aux` takes in two triples, and from these recursively computes the next triple: ``` xgcd_aux (r, s, t) (r', s', t') = xgcd_aux (r' % r, s' - (r' / r) * s, t' - (r' / r) * t) (r, s, t) ``` -/ def xgcd_aux {R : Type u} [euclidean_domain R] [DecidableEq R] : R → R → R → R → R → R → R × R × R := sorry @[simp] theorem xgcd_zero_left {R : Type u} [euclidean_domain R] [DecidableEq R] {s : R} {t : R} {r' : R} {s' : R} {t' : R} : xgcd_aux 0 s t r' s' t' = (r', s', t') := sorry theorem xgcd_aux_rec {R : Type u} [euclidean_domain R] [DecidableEq R] {r : R} {s : R} {t : R} {r' : R} {s' : R} {t' : R} (h : r ≠ 0) : xgcd_aux r s t r' s' t' = xgcd_aux (r' % r) (s' - r' / r * s) (t' - r' / r * t) r s t := sorry /-- Use the extended GCD algorithm to generate the `a` and `b` values satisfying `gcd x y = x * a + y * b`. -/ def xgcd {R : Type u} [euclidean_domain R] [DecidableEq R] (x : R) (y : R) : R × R := prod.snd (xgcd_aux x 1 0 y 0 1) /-- The extended GCD `a` value in the equation `gcd x y = x * a + y * b`. -/ def gcd_a {R : Type u} [euclidean_domain R] [DecidableEq R] (x : R) (y : R) : R := prod.fst (xgcd x y) /-- The extended GCD `b` value in the equation `gcd x y = x * a + y * b`. -/ def gcd_b {R : Type u} [euclidean_domain R] [DecidableEq R] (x : R) (y : R) : R := prod.snd (xgcd x y) @[simp] theorem gcd_a_zero_left {R : Type u} [euclidean_domain R] [DecidableEq R] {s : R} : gcd_a 0 s = 0 := sorry @[simp] theorem gcd_b_zero_left {R : Type u} [euclidean_domain R] [DecidableEq R] {s : R} : gcd_b 0 s = 1 := sorry @[simp] theorem xgcd_aux_fst {R : Type u} [euclidean_domain R] [DecidableEq R] (x : R) (y : R) (s : R) (t : R) (s' : R) (t' : R) : prod.fst (xgcd_aux x s t y s' t') = gcd x y := sorry theorem xgcd_aux_val {R : Type u} [euclidean_domain R] [DecidableEq R] (x : R) (y : R) : xgcd_aux x 1 0 y 0 1 = (gcd x y, xgcd x y) := sorry theorem xgcd_val {R : Type u} [euclidean_domain R] [DecidableEq R] (x : R) (y : R) : xgcd x y = (gcd_a x y, gcd_b x y) := Eq.symm prod.mk.eta theorem xgcd_aux_P {R : Type u} [euclidean_domain R] [DecidableEq R] (a : R) (b : R) {r : R} {r' : R} {s : R} {t : R} {s' : R} {t' : R} : P a b (r, s, t) → P a b (r', s', t') → P a b (xgcd_aux r s t r' s' t') := sorry theorem gcd_eq_gcd_ab {R : Type u} [euclidean_domain R] [DecidableEq R] (a : R) (b : R) : gcd a b = a * gcd_a a b + b * gcd_b a b := sorry protected instance integral_domain (R : Type u_1) [e : euclidean_domain R] : integral_domain R := integral_domain.mk Add.add euclidean_domain.add_assoc 0 euclidean_domain.zero_add euclidean_domain.add_zero euclidean_domain.neg euclidean_domain.sub euclidean_domain.add_left_neg euclidean_domain.add_comm Mul.mul euclidean_domain.mul_assoc euclidean_domain.one euclidean_domain.one_mul euclidean_domain.mul_one euclidean_domain.left_distrib euclidean_domain.right_distrib euclidean_domain.mul_comm euclidean_domain.exists_pair_ne sorry /-- `lcm a b` is a (non-unique) element such that `a ∣ lcm a b` `b ∣ lcm a b`, and for any element `c` such that `a ∣ c` and `b ∣ c`, then `lcm a b ∣ c` -/ def lcm {R : Type u} [euclidean_domain R] [DecidableEq R] (x : R) (y : R) : R := x * y / gcd x y theorem dvd_lcm_left {R : Type u} [euclidean_domain R] [DecidableEq R] (x : R) (y : R) : x ∣ lcm x y := sorry theorem dvd_lcm_right {R : Type u} [euclidean_domain R] [DecidableEq R] (x : R) (y : R) : y ∣ lcm x y := sorry theorem lcm_dvd {R : Type u} [euclidean_domain R] [DecidableEq R] {x : R} {y : R} {z : R} (hxz : x ∣ z) (hyz : y ∣ z) : lcm x y ∣ z := sorry @[simp] theorem lcm_dvd_iff {R : Type u} [euclidean_domain R] [DecidableEq R] {x : R} {y : R} {z : R} : lcm x y ∣ z ↔ x ∣ z ∧ y ∣ z := sorry @[simp] theorem lcm_zero_left {R : Type u} [euclidean_domain R] [DecidableEq R] (x : R) : lcm 0 x = 0 := eq.mpr (id (Eq._oldrec (Eq.refl (lcm 0 x = 0)) (lcm.equations._eqn_1 0 x))) (eq.mpr (id (Eq._oldrec (Eq.refl (0 * x / gcd 0 x = 0)) (zero_mul x))) (eq.mpr (id (Eq._oldrec (Eq.refl (0 / gcd 0 x = 0)) zero_div)) (Eq.refl 0))) @[simp] theorem lcm_zero_right {R : Type u} [euclidean_domain R] [DecidableEq R] (x : R) : lcm x 0 = 0 := eq.mpr (id (Eq._oldrec (Eq.refl (lcm x 0 = 0)) (lcm.equations._eqn_1 x 0))) (eq.mpr (id (Eq._oldrec (Eq.refl (x * 0 / gcd x 0 = 0)) (mul_zero x))) (eq.mpr (id (Eq._oldrec (Eq.refl (0 / gcd x 0 = 0)) zero_div)) (Eq.refl 0))) @[simp] theorem lcm_eq_zero_iff {R : Type u} [euclidean_domain R] [DecidableEq R] {x : R} {y : R} : lcm x y = 0 ↔ x = 0 ∨ y = 0 := sorry @[simp] theorem gcd_mul_lcm {R : Type u} [euclidean_domain R] [DecidableEq R] (x : R) (y : R) : gcd x y * lcm x y = x * y := sorry end euclidean_domain protected instance int.euclidean_domain : euclidean_domain ℤ := euclidean_domain.mk Add.add comm_ring.add_assoc 0 comm_ring.zero_add comm_ring.add_zero Neg.neg comm_ring.sub comm_ring.add_left_neg comm_ring.add_comm Mul.mul comm_ring.mul_assoc 1 comm_ring.one_mul comm_ring.mul_one comm_ring.left_distrib comm_ring.right_distrib comm_ring.mul_comm nontrivial.exists_pair_ne Div.div int.div_zero Mod.mod sorry (fun (a b : ℤ) => int.nat_abs a < int.nat_abs b) sorry sorry sorry protected instance field.to_euclidean_domain {K : Type u} [field K] : euclidean_domain K := euclidean_domain.mk Add.add field.add_assoc 0 field.zero_add field.add_zero Neg.neg field.sub field.add_left_neg field.add_comm Mul.mul field.mul_assoc 1 field.one_mul field.mul_one field.left_distrib field.right_distrib field.mul_comm field.exists_pair_ne Div.div sorry (fun (a b : K) => a - a * b / b) sorry (fun (a b : K) => a = 0 ∧ b ≠ 0) sorry sorry sorry
f74996f797c8e6fb2862f5738353cce9a4f4e5da
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/ring_theory/hahn_series.lean
05899a5c64965624d08cf58725db845df02cdedd
[ "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
61,486
lean
/- Copyright (c) 2021 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson -/ import order.well_founded_set import algebra.big_operators.finprod import ring_theory.valuation.basic import ring_theory.power_series.basic import data.finsupp.pwo import data.finset.mul_antidiagonal import algebra.order.group.with_top /-! # Hahn Series If `Γ` is ordered and `R` has zero, then `hahn_series Γ R` consists of formal series over `Γ` with coefficients in `R`, whose supports are partially well-ordered. With further structure on `R` and `Γ`, we can add further structure on `hahn_series Γ R`, with the most studied case being when `Γ` is a linearly ordered abelian group and `R` is a field, in which case `hahn_series Γ R` is a valued field, with value group `Γ`. These generalize Laurent series (with value group `ℤ`), and Laurent series are implemented that way in the file `ring_theory/laurent_series`. ## Main Definitions * If `Γ` is ordered and `R` has zero, then `hahn_series Γ R` consists of formal series over `Γ` with coefficients in `R`, whose supports are partially well-ordered. * If `R` is a (commutative) additive monoid or group, then so is `hahn_series Γ R`. * If `R` is a (comm_)(semi)ring, then so is `hahn_series Γ R`. * `hahn_series.add_val Γ R` defines an `add_valuation` on `hahn_series Γ R` when `Γ` is linearly ordered. * A `hahn_series.summable_family` is a family of Hahn series such that the union of their supports is well-founded and only finitely many are nonzero at any given coefficient. They have a formal sum, `hahn_series.summable_family.hsum`, which can be bundled as a `linear_map` as `hahn_series.summable_family.lsum`. Note that this is different from `summable` in the valuation topology, because there are topologically summable families that do not satisfy the axioms of `hahn_series.summable_family`, and formally summable families whose sums do not converge topologically. * Laurent series over `R` are implemented as `hahn_series ℤ R` in the file `ring_theory/laurent_series`. ## TODO * Build an API for the variable `X` (defined to be `single 1 1 : hahn_series Γ R`) in analogy to `X : R[X]` and `X : power_series R` ## References - [J. van der Hoeven, *Operators on Generalized Power Series*][van_der_hoeven] -/ open finset function open_locale big_operators classical pointwise polynomial noncomputable theory /-- If `Γ` is linearly ordered and `R` has zero, then `hahn_series Γ R` consists of formal series over `Γ` with coefficients in `R`, whose supports are well-founded. -/ @[ext] structure hahn_series (Γ : Type*) (R : Type*) [partial_order Γ] [has_zero R] := (coeff : Γ → R) (is_pwo_support' : (support coeff).is_pwo) variables {Γ : Type*} {R : Type*} namespace hahn_series section zero variables [partial_order Γ] [has_zero R] lemma coeff_injective : injective (coeff : hahn_series Γ R → (Γ → R)) := ext @[simp] lemma coeff_inj {x y : hahn_series Γ R} : x.coeff = y.coeff ↔ x = y := coeff_injective.eq_iff /-- The support of a Hahn series is just the set of indices whose coefficients are nonzero. Notably, it is well-founded. -/ def support (x : hahn_series Γ R) : set Γ := support x.coeff @[simp] lemma is_pwo_support (x : hahn_series Γ R) : x.support.is_pwo := x.is_pwo_support' @[simp] lemma is_wf_support (x : hahn_series Γ R) : x.support.is_wf := x.is_pwo_support.is_wf @[simp] lemma mem_support (x : hahn_series Γ R) (a : Γ) : a ∈ x.support ↔ x.coeff a ≠ 0 := iff.refl _ instance : has_zero (hahn_series Γ R) := ⟨{ coeff := 0, is_pwo_support' := by simp }⟩ instance : inhabited (hahn_series Γ R) := ⟨0⟩ instance [subsingleton R] : subsingleton (hahn_series Γ R) := ⟨λ a b, a.ext b (subsingleton.elim _ _)⟩ @[simp] lemma zero_coeff {a : Γ} : (0 : hahn_series Γ R).coeff a = 0 := rfl @[simp] lemma coeff_fun_eq_zero_iff {x : hahn_series Γ R} : x.coeff = 0 ↔ x = 0 := coeff_injective.eq_iff' rfl lemma ne_zero_of_coeff_ne_zero {x : hahn_series Γ R} {g : Γ} (h : x.coeff g ≠ 0) : x ≠ 0 := mt (λ x0, (x0.symm ▸ zero_coeff : x.coeff g = 0)) h @[simp] lemma support_zero : support (0 : hahn_series Γ R) = ∅ := function.support_zero @[simp] lemma support_nonempty_iff {x : hahn_series Γ R} : x.support.nonempty ↔ x ≠ 0 := by rw [support, support_nonempty_iff, ne.def, coeff_fun_eq_zero_iff] @[simp] lemma support_eq_empty_iff {x : hahn_series Γ R} : x.support = ∅ ↔ x = 0 := support_eq_empty_iff.trans coeff_fun_eq_zero_iff /-- `single a r` is the Hahn series which has coefficient `r` at `a` and zero otherwise. -/ def single (a : Γ) : zero_hom R (hahn_series Γ R) := { to_fun := λ r, { coeff := pi.single a r, is_pwo_support' := (set.is_pwo_singleton a).mono pi.support_single_subset }, map_zero' := ext _ _ (pi.single_zero _) } variables {a b : Γ} {r : R} @[simp] theorem single_coeff_same (a : Γ) (r : R) : (single a r).coeff a = r := pi.single_eq_same a r @[simp] theorem single_coeff_of_ne (h : b ≠ a) : (single a r).coeff b = 0 := pi.single_eq_of_ne h r theorem single_coeff : (single a r).coeff b = if (b = a) then r else 0 := by { split_ifs with h; simp [h] } @[simp] lemma support_single_of_ne (h : r ≠ 0) : support (single a r) = {a} := pi.support_single_of_ne h lemma support_single_subset : support (single a r) ⊆ {a} := pi.support_single_subset lemma eq_of_mem_support_single {b : Γ} (h : b ∈ support (single a r)) : b = a := support_single_subset h @[simp] lemma single_eq_zero : (single a (0 : R)) = 0 := (single a).map_zero lemma single_injective (a : Γ) : function.injective (single a : R → hahn_series Γ R) := λ r s rs, by rw [← single_coeff_same a r, ← single_coeff_same a s, rs] lemma single_ne_zero (h : r ≠ 0) : single a r ≠ 0 := λ con, h (single_injective a (con.trans single_eq_zero.symm)) @[simp] lemma single_eq_zero_iff {a : Γ} {r : R} : single a r = 0 ↔ r = 0 := begin split, { contrapose!, exact single_ne_zero }, { simp {contextual := tt} } end instance [nonempty Γ] [nontrivial R] : nontrivial (hahn_series Γ R) := ⟨begin obtain ⟨r, s, rs⟩ := exists_pair_ne R, inhabit Γ, refine ⟨single (arbitrary Γ) r, single (arbitrary Γ) s, λ con, rs _⟩, rw [← single_coeff_same (arbitrary Γ) r, con, single_coeff_same], end⟩ section order variable [has_zero Γ] /-- The order of a nonzero Hahn series `x` is a minimal element of `Γ` where `x` has a nonzero coefficient, the order of 0 is 0. -/ def order (x : hahn_series Γ R) : Γ := if h : x = 0 then 0 else x.is_wf_support.min (support_nonempty_iff.2 h) @[simp] lemma order_zero : order (0 : hahn_series Γ R) = 0 := dif_pos rfl lemma order_of_ne {x : hahn_series Γ R} (hx : x ≠ 0) : order x = x.is_wf_support.min (support_nonempty_iff.2 hx) := dif_neg hx lemma coeff_order_ne_zero {x : hahn_series Γ R} (hx : x ≠ 0) : x.coeff x.order ≠ 0 := begin rw order_of_ne hx, exact x.is_wf_support.min_mem (support_nonempty_iff.2 hx) end lemma order_le_of_coeff_ne_zero {Γ} [linear_ordered_cancel_add_comm_monoid Γ] {x : hahn_series Γ R} {g : Γ} (h : x.coeff g ≠ 0) : x.order ≤ g := le_trans (le_of_eq (order_of_ne (ne_zero_of_coeff_ne_zero h))) (set.is_wf.min_le _ _ ((mem_support _ _).2 h)) @[simp] lemma order_single (h : r ≠ 0) : (single a r).order = a := (order_of_ne (single_ne_zero h)).trans (support_single_subset ((single a r).is_wf_support.min_mem (support_nonempty_iff.2 (single_ne_zero h)))) lemma coeff_eq_zero_of_lt_order {x : hahn_series Γ R} {i : Γ} (hi : i < x.order) : x.coeff i = 0 := begin rcases eq_or_ne x 0 with rfl|hx, { simp }, contrapose! hi, rw [←ne.def, ←mem_support] at hi, rw [order_of_ne hx], exact set.is_wf.not_lt_min _ _ hi end end order section domain variables {Γ' : Type*} [partial_order Γ'] /-- Extends the domain of a `hahn_series` by an `order_embedding`. -/ def emb_domain (f : Γ ↪o Γ') : hahn_series Γ R → hahn_series Γ' R := λ x, { coeff := λ (b : Γ'), if h : b ∈ f '' x.support then x.coeff (classical.some h) else 0, is_pwo_support' := (x.is_pwo_support.image_of_monotone f.monotone).mono (λ b hb, begin contrapose! hb, rw [function.mem_support, dif_neg hb, not_not], end) } @[simp] lemma emb_domain_coeff {f : Γ ↪o Γ'} {x : hahn_series Γ R} {a : Γ} : (emb_domain f x).coeff (f a) = x.coeff a := begin rw emb_domain, dsimp only, by_cases ha : a ∈ x.support, { rw dif_pos (set.mem_image_of_mem f ha), exact congr rfl (f.injective (classical.some_spec (set.mem_image_of_mem f ha)).2) }, { rw [dif_neg, not_not.1 (λ c, ha ((mem_support _ _).2 c))], contrapose! ha, obtain ⟨b, hb1, hb2⟩ := (set.mem_image _ _ _).1 ha, rwa f.injective hb2 at hb1 } end @[simp] lemma emb_domain_mk_coeff {f : Γ → Γ'} (hfi : function.injective f) (hf : ∀ g g' : Γ, f g ≤ f g' ↔ g ≤ g') {x : hahn_series Γ R} {a : Γ} : (emb_domain ⟨⟨f, hfi⟩, hf⟩ x).coeff (f a) = x.coeff a := emb_domain_coeff lemma emb_domain_notin_image_support {f : Γ ↪o Γ'} {x : hahn_series Γ R} {b : Γ'} (hb : b ∉ f '' x.support) : (emb_domain f x).coeff b = 0 := dif_neg hb lemma support_emb_domain_subset {f : Γ ↪o Γ'} {x : hahn_series Γ R} : support (emb_domain f x) ⊆ f '' x.support := begin intros g hg, contrapose! hg, rw [mem_support, emb_domain_notin_image_support hg, not_not], end lemma emb_domain_notin_range {f : Γ ↪o Γ'} {x : hahn_series Γ R} {b : Γ'} (hb : b ∉ set.range f) : (emb_domain f x).coeff b = 0 := emb_domain_notin_image_support (λ con, hb (set.image_subset_range _ _ con)) @[simp] lemma emb_domain_zero {f : Γ ↪o Γ'} : emb_domain f (0 : hahn_series Γ R) = 0 := by { ext, simp [emb_domain_notin_image_support] } @[simp] lemma emb_domain_single {f : Γ ↪o Γ'} {g : Γ} {r : R} : emb_domain f (single g r) = single (f g) r := begin ext g', by_cases h : g' = f g, { simp [h] }, rw [emb_domain_notin_image_support, single_coeff_of_ne h], by_cases hr : r = 0, { simp [hr] }, rwa [support_single_of_ne hr, set.image_singleton, set.mem_singleton_iff], end lemma emb_domain_injective {f : Γ ↪o Γ'} : function.injective (emb_domain f : hahn_series Γ R → hahn_series Γ' R) := λ x y xy, begin ext g, rw [ext_iff, function.funext_iff] at xy, have xyg := xy (f g), rwa [emb_domain_coeff, emb_domain_coeff] at xyg, end end domain end zero section addition variable [partial_order Γ] section add_monoid variable [add_monoid R] instance : has_add (hahn_series Γ R) := { add := λ x y, { coeff := x.coeff + y.coeff, is_pwo_support' := (x.is_pwo_support.union y.is_pwo_support).mono (function.support_add _ _) } } instance : add_monoid (hahn_series Γ R) := { zero := 0, add := (+), add_assoc := λ x y z, by { ext, apply add_assoc }, zero_add := λ x, by { ext, apply zero_add }, add_zero := λ x, by { ext, apply add_zero } } @[simp] lemma add_coeff' {x y : hahn_series Γ R} : (x + y).coeff = x.coeff + y.coeff := rfl lemma add_coeff {x y : hahn_series Γ R} {a : Γ} : (x + y).coeff a = x.coeff a + y.coeff a := rfl lemma support_add_subset {x y : hahn_series Γ R} : support (x + y) ⊆ support x ∪ support y := λ a ha, begin rw [mem_support, add_coeff] at ha, rw [set.mem_union, mem_support, mem_support], contrapose! ha, rw [ha.1, ha.2, add_zero], end lemma min_order_le_order_add {Γ} [linear_ordered_cancel_add_comm_monoid Γ] {x y : hahn_series Γ R} (hxy : x + y ≠ 0) : min x.order y.order ≤ (x + y).order := begin by_cases hx : x = 0, { simp [hx], }, by_cases hy : y = 0, { simp [hy], }, rw [order_of_ne hx, order_of_ne hy, order_of_ne hxy], refine le_trans _ (set.is_wf.min_le_min_of_subset support_add_subset), { exact x.is_wf_support.union y.is_wf_support }, { exact set.nonempty.mono (set.subset_union_left _ _) (support_nonempty_iff.2 hx) }, rw set.is_wf.min_union, end /-- `single` as an additive monoid/group homomorphism -/ @[simps] def single.add_monoid_hom (a : Γ) : R →+ (hahn_series Γ R) := { map_add' := λ x y, by { ext b, by_cases h : b = a; simp [h] }, ..single a } /-- `coeff g` as an additive monoid/group homomorphism -/ @[simps] def coeff.add_monoid_hom (g : Γ) : (hahn_series Γ R) →+ R := { to_fun := λ f, f.coeff g, map_zero' := zero_coeff, map_add' := λ x y, add_coeff } section domain variables {Γ' : Type*} [partial_order Γ'] lemma emb_domain_add (f : Γ ↪o Γ') (x y : hahn_series Γ R) : emb_domain f (x + y) = emb_domain f x + emb_domain f y := begin ext g, by_cases hg : g ∈ set.range f, { obtain ⟨a, rfl⟩ := hg, simp }, { simp [emb_domain_notin_range, hg] } end end domain end add_monoid instance [add_comm_monoid R] : add_comm_monoid (hahn_series Γ R) := { add_comm := λ x y, by { ext, apply add_comm } .. hahn_series.add_monoid } section add_group variable [add_group R] instance : add_group (hahn_series Γ R) := { neg := λ x, { coeff := λ a, - x.coeff a, is_pwo_support' := by { rw function.support_neg, exact x.is_pwo_support }, }, add_left_neg := λ x, by { ext, apply add_left_neg }, .. hahn_series.add_monoid } @[simp] lemma neg_coeff' {x : hahn_series Γ R} : (- x).coeff = - x.coeff := rfl lemma neg_coeff {x : hahn_series Γ R} {a : Γ} : (- x).coeff a = - x.coeff a := rfl @[simp] lemma support_neg {x : hahn_series Γ R} : (- x).support = x.support := by { ext, simp } @[simp] lemma sub_coeff' {x y : hahn_series Γ R} : (x - y).coeff = x.coeff - y.coeff := by { ext, simp [sub_eq_add_neg] } lemma sub_coeff {x y : hahn_series Γ R} {a : Γ} : (x - y).coeff a = x.coeff a - y.coeff a := by simp end add_group instance [add_comm_group R] : add_comm_group (hahn_series Γ R) := { .. hahn_series.add_comm_monoid, .. hahn_series.add_group } end addition section distrib_mul_action variables [partial_order Γ] {V : Type*} [monoid R] [add_monoid V] [distrib_mul_action R V] instance : has_smul R (hahn_series Γ V) := ⟨λ r x, { coeff := r • x.coeff, is_pwo_support' := x.is_pwo_support.mono (function.support_smul_subset_right r x.coeff) }⟩ @[simp] lemma smul_coeff {r : R} {x : hahn_series Γ V} {a : Γ} : (r • x).coeff a = r • (x.coeff a) := rfl instance : distrib_mul_action R (hahn_series Γ V) := { smul := (•), one_smul := λ _, by { ext, simp }, smul_zero := λ _, by { ext, simp }, smul_add := λ _ _ _, by { ext, simp [smul_add] }, mul_smul := λ _ _ _, by { ext, simp [mul_smul] } } variables {S : Type*} [monoid S] [distrib_mul_action S V] instance [has_smul R S] [is_scalar_tower R S V] : is_scalar_tower R S (hahn_series Γ V) := ⟨λ r s a, by { ext, simp }⟩ instance [smul_comm_class R S V] : smul_comm_class R S (hahn_series Γ V) := ⟨λ r s a, by { ext, simp [smul_comm] }⟩ end distrib_mul_action section module variables [partial_order Γ] [semiring R] {V : Type*} [add_comm_monoid V] [module R V] instance : module R (hahn_series Γ V) := { zero_smul := λ _, by { ext, simp }, add_smul := λ _ _ _, by { ext, simp [add_smul] }, .. hahn_series.distrib_mul_action } /-- `single` as a linear map -/ @[simps] def single.linear_map (a : Γ) : R →ₗ[R] (hahn_series Γ R) := { map_smul' := λ r s, by { ext b, by_cases h : b = a; simp [h] }, ..single.add_monoid_hom a } /-- `coeff g` as a linear map -/ @[simps] def coeff.linear_map (g : Γ) : (hahn_series Γ R) →ₗ[R] R := { map_smul' := λ r s, rfl, ..coeff.add_monoid_hom g } section domain variables {Γ' : Type*} [partial_order Γ'] lemma emb_domain_smul (f : Γ ↪o Γ') (r : R) (x : hahn_series Γ R) : emb_domain f (r • x) = r • emb_domain f x := begin ext g, by_cases hg : g ∈ set.range f, { obtain ⟨a, rfl⟩ := hg, simp }, { simp [emb_domain_notin_range, hg] } end /-- Extending the domain of Hahn series is a linear map. -/ @[simps] def emb_domain_linear_map (f : Γ ↪o Γ') : hahn_series Γ R →ₗ[R] hahn_series Γ' R := { to_fun := emb_domain f, map_add' := emb_domain_add f, map_smul' := emb_domain_smul f } end domain end module section multiplication variable [ordered_cancel_add_comm_monoid Γ] instance [has_zero R] [has_one R] : has_one (hahn_series Γ R) := ⟨single 0 1⟩ @[simp] lemma one_coeff [has_zero R] [has_one R] {a : Γ} : (1 : hahn_series Γ R).coeff a = if a = 0 then 1 else 0 := single_coeff @[simp] lemma single_zero_one [has_zero R] [has_one R] : (single 0 (1 : R)) = 1 := rfl @[simp] lemma support_one [mul_zero_one_class R] [nontrivial R] : support (1 : hahn_series Γ R) = {0} := support_single_of_ne one_ne_zero @[simp] lemma order_one [mul_zero_one_class R] : order (1 : hahn_series Γ R) = 0 := begin cases subsingleton_or_nontrivial R with h h; haveI := h, { rw [subsingleton.elim (1 : hahn_series Γ R) 0, order_zero] }, { exact order_single one_ne_zero } end instance [non_unital_non_assoc_semiring R] : has_mul (hahn_series Γ R) := { mul := λ x y, { coeff := λ a, ∑ ij in (add_antidiagonal x.is_pwo_support y.is_pwo_support a), x.coeff ij.fst * y.coeff ij.snd, is_pwo_support' := begin have h : {a : Γ | ∑ (ij : Γ × Γ) in add_antidiagonal x.is_pwo_support y.is_pwo_support a, x.coeff ij.fst * y.coeff ij.snd ≠ 0} ⊆ {a : Γ | (add_antidiagonal x.is_pwo_support y.is_pwo_support a).nonempty}, { intros a ha, contrapose! ha, simp [not_nonempty_iff_eq_empty.1 ha] }, exact is_pwo_support_add_antidiagonal.mono h, end, }, } @[simp] lemma mul_coeff [non_unital_non_assoc_semiring R] {x y : hahn_series Γ R} {a : Γ} : (x * y).coeff a = ∑ ij in (add_antidiagonal x.is_pwo_support y.is_pwo_support a), x.coeff ij.fst * y.coeff ij.snd := rfl lemma mul_coeff_right' [non_unital_non_assoc_semiring R] {x y : hahn_series Γ R} {a : Γ} {s : set Γ} (hs : s.is_pwo) (hys : y.support ⊆ s) : (x * y).coeff a = ∑ ij in (add_antidiagonal x.is_pwo_support hs a), x.coeff ij.fst * y.coeff ij.snd := begin rw mul_coeff, apply sum_subset_zero_on_sdiff (add_antidiagonal_mono_right hys) _ (λ _ _, rfl), intros b hb, simp only [not_and, mem_sdiff, mem_add_antidiagonal, mem_support, not_imp_not] at hb, rw [hb.2 hb.1.1 hb.1.2.2, mul_zero], end lemma mul_coeff_left' [non_unital_non_assoc_semiring R] {x y : hahn_series Γ R} {a : Γ} {s : set Γ} (hs : s.is_pwo) (hxs : x.support ⊆ s) : (x * y).coeff a = ∑ ij in (add_antidiagonal hs y.is_pwo_support a), x.coeff ij.fst * y.coeff ij.snd := begin rw mul_coeff, apply sum_subset_zero_on_sdiff (add_antidiagonal_mono_left hxs) _ (λ _ _, rfl), intros b hb, simp only [not_and', mem_sdiff, mem_add_antidiagonal, mem_support, not_ne_iff] at hb, rw [hb.2 ⟨hb.1.2.1, hb.1.2.2⟩, zero_mul], end instance [non_unital_non_assoc_semiring R] : distrib (hahn_series Γ R) := { left_distrib := λ x y z, begin ext a, have hwf := (y.is_pwo_support.union z.is_pwo_support), rw [mul_coeff_right' hwf, add_coeff, mul_coeff_right' hwf (set.subset_union_right _ _), mul_coeff_right' hwf (set.subset_union_left _ _)], { simp only [add_coeff, mul_add, sum_add_distrib] }, { intro b, simp only [add_coeff, ne.def, set.mem_union, set.mem_set_of_eq, mem_support], contrapose!, intro h, rw [h.1, h.2, add_zero], } end, right_distrib := λ x y z, begin ext a, have hwf := (x.is_pwo_support.union y.is_pwo_support), rw [mul_coeff_left' hwf, add_coeff, mul_coeff_left' hwf (set.subset_union_right _ _), mul_coeff_left' hwf (set.subset_union_left _ _)], { simp only [add_coeff, add_mul, sum_add_distrib] }, { intro b, simp only [add_coeff, ne.def, set.mem_union, set.mem_set_of_eq, mem_support], contrapose!, intro h, rw [h.1, h.2, add_zero], }, end, .. hahn_series.has_mul, .. hahn_series.has_add } lemma single_mul_coeff_add [non_unital_non_assoc_semiring R] {r : R} {x : hahn_series Γ R} {a : Γ} {b : Γ} : ((single b r) * x).coeff (a + b) = r * x.coeff a := begin by_cases hr : r = 0, { simp [hr] }, simp only [hr, smul_coeff, mul_coeff, support_single_of_ne, ne.def, not_false_iff, smul_eq_mul], by_cases hx : x.coeff a = 0, { simp only [hx, mul_zero], rw [sum_congr _ (λ _ _, rfl), sum_empty], ext ⟨a1, a2⟩, simp only [not_mem_empty, not_and, set.mem_singleton_iff, not_not, mem_add_antidiagonal, set.mem_set_of_eq, iff_false], rintro rfl h2 h1, rw add_comm at h1, rw ← add_right_cancel h1 at hx, exact h2 hx, }, transitivity ∑ (ij : Γ × Γ) in {(b, a)}, (single b r).coeff ij.fst * x.coeff ij.snd, { apply sum_congr _ (λ _ _, rfl), ext ⟨a1, a2⟩, simp only [set.mem_singleton_iff, prod.mk.inj_iff, mem_add_antidiagonal, mem_singleton, set.mem_set_of_eq], split, { rintro ⟨rfl, h2, h1⟩, rw add_comm at h1, refine ⟨rfl, add_right_cancel h1⟩ }, { rintro ⟨rfl, rfl⟩, exact ⟨rfl, by simp [hx], add_comm _ _⟩ } }, { simp } end lemma mul_single_coeff_add [non_unital_non_assoc_semiring R] {r : R} {x : hahn_series Γ R} {a : Γ} {b : Γ} : (x * (single b r)).coeff (a + b) = x.coeff a * r := begin by_cases hr : r = 0, { simp [hr] }, simp only [hr, smul_coeff, mul_coeff, support_single_of_ne, ne.def, not_false_iff, smul_eq_mul], by_cases hx : x.coeff a = 0, { simp only [hx, zero_mul], rw [sum_congr _ (λ _ _, rfl), sum_empty], ext ⟨a1, a2⟩, simp only [not_mem_empty, not_and, set.mem_singleton_iff, not_not, mem_add_antidiagonal, set.mem_set_of_eq, iff_false], rintro h2 rfl h1, rw ← add_right_cancel h1 at hx, exact h2 hx, }, transitivity ∑ (ij : Γ × Γ) in {(a,b)}, x.coeff ij.fst * (single b r).coeff ij.snd, { apply sum_congr _ (λ _ _, rfl), ext ⟨a1, a2⟩, simp only [set.mem_singleton_iff, prod.mk.inj_iff, mem_add_antidiagonal, mem_singleton, set.mem_set_of_eq], split, { rintro ⟨h2, rfl, h1⟩, refine ⟨add_right_cancel h1, rfl⟩ }, { rintro ⟨rfl, rfl⟩, simp [hx] } }, { simp } end @[simp] lemma mul_single_zero_coeff [non_unital_non_assoc_semiring R] {r : R} {x : hahn_series Γ R} {a : Γ} : (x * (single 0 r)).coeff a = x.coeff a * r := by rw [← add_zero a, mul_single_coeff_add, add_zero] lemma single_zero_mul_coeff [non_unital_non_assoc_semiring R] {r : R} {x : hahn_series Γ R} {a : Γ} : ((single 0 r) * x).coeff a = r * x.coeff a := by rw [← add_zero a, single_mul_coeff_add, add_zero] @[simp] lemma single_zero_mul_eq_smul [semiring R] {r : R} {x : hahn_series Γ R} : (single 0 r) * x = r • x := by { ext, exact single_zero_mul_coeff } theorem support_mul_subset_add_support [non_unital_non_assoc_semiring R] {x y : hahn_series Γ R} : support (x * y) ⊆ support x + support y := begin apply set.subset.trans (λ x hx, _) support_add_antidiagonal_subset_add, { exact x.is_pwo_support }, { exact y.is_pwo_support }, contrapose! hx, simp only [not_nonempty_iff_eq_empty, ne.def, set.mem_set_of_eq] at hx, simp [hx], end lemma mul_coeff_order_add_order {Γ} [linear_ordered_cancel_add_comm_monoid Γ] [non_unital_non_assoc_semiring R] (x y : hahn_series Γ R) : (x * y).coeff (x.order + y.order) = x.coeff x.order * y.coeff y.order := begin by_cases hx : x = 0, { simp [hx], }, by_cases hy : y = 0, { simp [hy], }, rw [order_of_ne hx, order_of_ne hy, mul_coeff, finset.add_antidiagonal_min_add_min, finset.sum_singleton], end private lemma mul_assoc' [non_unital_semiring R] (x y z : hahn_series Γ R) : x * y * z = x * (y * z) := begin ext b, rw [mul_coeff_left' (x.is_pwo_support.add y.is_pwo_support) support_mul_subset_add_support, mul_coeff_right' (y.is_pwo_support.add z.is_pwo_support) support_mul_subset_add_support], simp only [mul_coeff, add_coeff, sum_mul, mul_sum, sum_sigma'], refine sum_bij_ne_zero (λ a has ha0, ⟨⟨a.2.1, a.2.2 + a.1.2⟩, ⟨a.2.2, a.1.2⟩⟩) _ _ _ _, { rintros ⟨⟨i,j⟩, ⟨k,l⟩⟩ H1 H2, simp only [and_true, set.image2_add, eq_self_iff_true, mem_add_antidiagonal, ne.def, set.image_prod, mem_sigma, set.mem_set_of_eq] at H1 H2 ⊢, obtain ⟨⟨H3, nz, rfl⟩, nx, ny, rfl⟩ := H1, exact ⟨⟨nx, set.add_mem_add ny nz, (add_assoc _ _ _).symm⟩, ny, nz⟩ }, { rintros ⟨⟨i1,j1⟩, k1,l1⟩ ⟨⟨i2,j2⟩, k2,l2⟩ H1 H2 H3 H4 H5, simp only [set.image2_add, prod.mk.inj_iff, mem_add_antidiagonal, ne.def, set.image_prod, mem_sigma, set.mem_set_of_eq, heq_iff_eq] at H1 H3 H5, obtain ⟨⟨rfl, H⟩, rfl, rfl⟩ := H5, simp only [and_true, prod.mk.inj_iff, eq_self_iff_true, heq_iff_eq, ←H1.2.2.2, ←H3.2.2.2] }, { rintros ⟨⟨i,j⟩, ⟨k,l⟩⟩ H1 H2, simp only [exists_prop, set.image2_add, prod.mk.inj_iff, mem_add_antidiagonal, sigma.exists, ne.def, set.image_prod, mem_sigma, set.mem_set_of_eq, heq_iff_eq, prod.exists] at H1 H2 ⊢, obtain ⟨⟨nx, H, rfl⟩, ny, nz, rfl⟩ := H1, exact ⟨i + k, l, i, k, ⟨⟨set.add_mem_add nx ny, nz, add_assoc _ _ _⟩, nx, ny, rfl⟩, λ con, H2 ((mul_assoc _ _ _).symm.trans con), ⟨rfl, rfl⟩, rfl, rfl⟩ }, { rintros ⟨⟨i,j⟩, ⟨k,l⟩⟩ H1 H2, simp [mul_assoc], } end instance [non_unital_non_assoc_semiring R] : non_unital_non_assoc_semiring (hahn_series Γ R) := { zero := 0, add := (+), mul := (*), zero_mul := λ _, by { ext, simp }, mul_zero := λ _, by { ext, simp }, .. hahn_series.add_comm_monoid, .. hahn_series.distrib } instance [non_unital_semiring R] : non_unital_semiring (hahn_series Γ R) := { zero := 0, add := (+), mul := (*), mul_assoc := mul_assoc', .. hahn_series.non_unital_non_assoc_semiring } instance [non_assoc_semiring R] : non_assoc_semiring (hahn_series Γ R) := { zero := 0, one := 1, add := (+), mul := (*), one_mul := λ x, by { ext, exact single_zero_mul_coeff.trans (one_mul _) }, mul_one := λ x, by { ext, exact mul_single_zero_coeff.trans (mul_one _) }, .. add_monoid_with_one.unary, .. hahn_series.non_unital_non_assoc_semiring } instance [semiring R] : semiring (hahn_series Γ R) := { zero := 0, one := 1, add := (+), mul := (*), .. hahn_series.non_assoc_semiring, .. hahn_series.non_unital_semiring } instance [non_unital_comm_semiring R] : non_unital_comm_semiring (hahn_series Γ R) := { mul_comm := λ x y, begin ext, simp_rw [mul_coeff, mul_comm], refine sum_bij (λ a ha, a.swap) (λ a ha, _) (λ a ha, rfl) (λ _ _ _ _, prod.swap_inj.1) (λ a ha, ⟨a.swap, _, a.swap_swap.symm⟩); rwa swap_mem_add_antidiagonal, end, .. hahn_series.non_unital_semiring } instance [comm_semiring R] : comm_semiring (hahn_series Γ R) := { .. hahn_series.non_unital_comm_semiring, .. hahn_series.semiring } instance [non_unital_non_assoc_ring R] : non_unital_non_assoc_ring (hahn_series Γ R) := { .. hahn_series.non_unital_non_assoc_semiring, .. hahn_series.add_group } instance [non_unital_ring R] : non_unital_ring (hahn_series Γ R) := { .. hahn_series.non_unital_non_assoc_ring, .. hahn_series.non_unital_semiring } instance [non_assoc_ring R] : non_assoc_ring (hahn_series Γ R) := { .. hahn_series.non_unital_non_assoc_ring, .. hahn_series.non_assoc_semiring } instance [ring R] : ring (hahn_series Γ R) := { .. hahn_series.semiring, .. hahn_series.add_comm_group } instance [non_unital_comm_ring R] : non_unital_comm_ring (hahn_series Γ R) := { .. hahn_series.non_unital_comm_semiring, .. hahn_series.non_unital_ring } instance [comm_ring R] : comm_ring (hahn_series Γ R) := { .. hahn_series.comm_semiring, .. hahn_series.ring } instance {Γ} [linear_ordered_cancel_add_comm_monoid Γ] [non_unital_non_assoc_semiring R] [no_zero_divisors R] : no_zero_divisors (hahn_series Γ R) := { eq_zero_or_eq_zero_of_mul_eq_zero := λ x y xy, begin by_cases hx : x = 0, { left, exact hx }, right, contrapose! xy, rw [hahn_series.ext_iff, function.funext_iff, not_forall], refine ⟨x.order + y.order, _⟩, rw [mul_coeff_order_add_order x y, zero_coeff, mul_eq_zero], simp [coeff_order_ne_zero, hx, xy], end } instance {Γ} [linear_ordered_cancel_add_comm_monoid Γ] [ring R] [is_domain R] : is_domain (hahn_series Γ R) := { .. hahn_series.no_zero_divisors, .. hahn_series.nontrivial, .. hahn_series.ring } @[simp] lemma order_mul {Γ} [linear_ordered_cancel_add_comm_monoid Γ] [non_unital_non_assoc_semiring R] [no_zero_divisors R] {x y : hahn_series Γ R} (hx : x ≠ 0) (hy : y ≠ 0) : (x * y).order = x.order + y.order := begin apply le_antisymm, { apply order_le_of_coeff_ne_zero, rw [mul_coeff_order_add_order x y], exact mul_ne_zero (coeff_order_ne_zero hx) (coeff_order_ne_zero hy) }, { rw [order_of_ne hx, order_of_ne hy, order_of_ne (mul_ne_zero hx hy), ← set.is_wf.min_add], exact set.is_wf.min_le_min_of_subset (support_mul_subset_add_support) }, end @[simp] lemma order_pow {Γ} [linear_ordered_cancel_add_comm_monoid Γ] [semiring R] [no_zero_divisors R] (x : hahn_series Γ R) (n : ℕ) : (x ^ n).order = n • x.order := begin induction n with h IH, { simp }, rcases eq_or_ne x 0 with rfl|hx, { simp }, rw [pow_succ', order_mul (pow_ne_zero _ hx) hx, succ_nsmul', IH] end section non_unital_non_assoc_semiring variables [non_unital_non_assoc_semiring R] @[simp] lemma single_mul_single {a b : Γ} {r s : R} : single a r * single b s = single (a + b) (r * s) := begin ext x, by_cases h : x = a + b, { rw [h, mul_single_coeff_add], simp }, { rw [single_coeff_of_ne h, mul_coeff, sum_eq_zero], simp_rw mem_add_antidiagonal, rintro ⟨y, z⟩ ⟨hy, hz, rfl⟩, rw [eq_of_mem_support_single hy, eq_of_mem_support_single hz] at h, exact (h rfl).elim } end end non_unital_non_assoc_semiring section non_assoc_semiring variables [non_assoc_semiring R] /-- `C a` is the constant Hahn Series `a`. `C` is provided as a ring homomorphism. -/ @[simps] def C : R →+* (hahn_series Γ R) := { to_fun := single 0, map_zero' := single_eq_zero, map_one' := rfl, map_add' := λ x y, by { ext a, by_cases h : a = 0; simp [h] }, map_mul' := λ x y, by rw [single_mul_single, zero_add] } @[simp] lemma C_zero : C (0 : R) = (0 : hahn_series Γ R) := C.map_zero @[simp] lemma C_one : C (1 : R) = (1 : hahn_series Γ R) := C.map_one lemma C_injective : function.injective (C : R → hahn_series Γ R) := begin intros r s rs, rw [ext_iff, function.funext_iff] at rs, have h := rs 0, rwa [C_apply, single_coeff_same, C_apply, single_coeff_same] at h, end lemma C_ne_zero {r : R} (h : r ≠ 0) : (C r : hahn_series Γ R) ≠ 0 := begin contrapose! h, rw ← C_zero at h, exact C_injective h, end lemma order_C {r : R} : order (C r : hahn_series Γ R) = 0 := begin by_cases h : r = 0, { rw [h, C_zero, order_zero] }, { exact order_single h } end end non_assoc_semiring section semiring variables [semiring R] lemma C_mul_eq_smul {r : R} {x : hahn_series Γ R} : C r * x = r • x := single_zero_mul_eq_smul end semiring section domain variables {Γ' : Type*} [ordered_cancel_add_comm_monoid Γ'] lemma emb_domain_mul [non_unital_non_assoc_semiring R] (f : Γ ↪o Γ') (hf : ∀ x y, f (x + y) = f x + f y) (x y : hahn_series Γ R) : emb_domain f (x * y) = emb_domain f x * emb_domain f y := begin ext g, by_cases hg : g ∈ set.range f, { obtain ⟨g, rfl⟩ := hg, simp only [mul_coeff, emb_domain_coeff], transitivity ∑ ij in (add_antidiagonal x.is_pwo_support y.is_pwo_support g).map (function.embedding.prod_map f.to_embedding f.to_embedding), (emb_domain f x).coeff (ij.1) * (emb_domain f y).coeff (ij.2), { simp }, apply sum_subset, { rintro ⟨i, j⟩ hij, simp only [exists_prop, mem_map, prod.mk.inj_iff, mem_add_antidiagonal, function.embedding.coe_prod_map, mem_support, prod.exists] at hij, obtain ⟨i, j, ⟨hx, hy, rfl⟩, rfl, rfl⟩ := hij, simp [hx, hy, hf], }, { rintro ⟨_, _⟩ h1 h2, contrapose! h2, obtain ⟨i, hi, rfl⟩ := support_emb_domain_subset (ne_zero_and_ne_zero_of_mul h2).1, obtain ⟨j, hj, rfl⟩ := support_emb_domain_subset (ne_zero_and_ne_zero_of_mul h2).2, simp only [exists_prop, mem_map, prod.mk.inj_iff, mem_add_antidiagonal, function.embedding.coe_prod_map, mem_support, prod.exists], simp only [mem_add_antidiagonal, emb_domain_coeff, mem_support, ←hf, order_embedding.eq_iff_eq] at h1, exact ⟨i, j, h1, rfl⟩ } }, { rw [emb_domain_notin_range hg, eq_comm], contrapose! hg, obtain ⟨_, _, hi, hj, rfl⟩ := support_mul_subset_add_support ((mem_support _ _).2 hg), obtain ⟨i, hi, rfl⟩ := support_emb_domain_subset hi, obtain ⟨j, hj, rfl⟩ := support_emb_domain_subset hj, refine ⟨i + j, hf i j⟩, } end lemma emb_domain_one [non_assoc_semiring R] (f : Γ ↪o Γ') (hf : f 0 = 0): emb_domain f (1 : hahn_series Γ R) = (1 : hahn_series Γ' R) := emb_domain_single.trans $ hf.symm ▸ rfl /-- Extending the domain of Hahn series is a ring homomorphism. -/ @[simps] def emb_domain_ring_hom [non_assoc_semiring R] (f : Γ →+ Γ') (hfi : function.injective f) (hf : ∀ g g' : Γ, f g ≤ f g' ↔ g ≤ g') : hahn_series Γ R →+* hahn_series Γ' R := { to_fun := emb_domain ⟨⟨f, hfi⟩, hf⟩, map_one' := emb_domain_one _ f.map_zero, map_mul' := emb_domain_mul _ f.map_add, map_zero' := emb_domain_zero, map_add' := emb_domain_add _} lemma emb_domain_ring_hom_C [non_assoc_semiring R] {f : Γ →+ Γ'} {hfi : function.injective f} {hf : ∀ g g' : Γ, f g ≤ f g' ↔ g ≤ g'} {r : R} : emb_domain_ring_hom f hfi hf (C r) = C r := emb_domain_single.trans (by simp) end domain section algebra variables [comm_semiring R] {A : Type*} [semiring A] [algebra R A] instance : algebra R (hahn_series Γ A) := { to_ring_hom := C.comp (algebra_map R A), smul_def' := λ r x, by { ext, simp }, commutes' := λ r x, by { ext, simp only [smul_coeff, single_zero_mul_eq_smul, ring_hom.coe_comp, ring_hom.to_fun_eq_coe, C_apply, function.comp_app, algebra_map_smul, mul_single_zero_coeff], rw [← algebra.commutes, algebra.smul_def], }, } theorem C_eq_algebra_map : C = (algebra_map R (hahn_series Γ R)) := rfl theorem algebra_map_apply {r : R} : algebra_map R (hahn_series Γ A) r = C (algebra_map R A r) := rfl instance [nontrivial Γ] [nontrivial R] : nontrivial (subalgebra R (hahn_series Γ R)) := ⟨⟨⊥, ⊤, begin rw [ne.def, set_like.ext_iff, not_forall], obtain ⟨a, ha⟩ := exists_ne (0 : Γ), refine ⟨single a 1, _⟩, simp only [algebra.mem_bot, not_exists, set.mem_range, iff_true, algebra.mem_top], intros x, rw [ext_iff, function.funext_iff, not_forall], refine ⟨a, _⟩, rw [single_coeff_same, algebra_map_apply, C_apply, single_coeff_of_ne ha], exact zero_ne_one end⟩⟩ section domain variables {Γ' : Type*} [ordered_cancel_add_comm_monoid Γ'] /-- Extending the domain of Hahn series is an algebra homomorphism. -/ @[simps] def emb_domain_alg_hom (f : Γ →+ Γ') (hfi : function.injective f) (hf : ∀ g g' : Γ, f g ≤ f g' ↔ g ≤ g') : hahn_series Γ A →ₐ[R] hahn_series Γ' A := { commutes' := λ r, emb_domain_ring_hom_C, .. emb_domain_ring_hom f hfi hf } end domain end algebra end multiplication section semiring variables [semiring R] /-- The ring `hahn_series ℕ R` is isomorphic to `power_series R`. -/ @[simps] def to_power_series : hahn_series ℕ R ≃+* power_series R := { to_fun := λ f, power_series.mk f.coeff, inv_fun := λ f, ⟨λ n, power_series.coeff R n f, (nat.lt_wf.is_wf _).is_pwo⟩, left_inv := λ f, by { ext, simp }, right_inv := λ f, by { ext, simp }, map_add' := λ f g, by { ext, simp }, map_mul' := λ f g, begin ext n, simp only [power_series.coeff_mul, power_series.coeff_mk, mul_coeff, is_pwo_support], classical, refine sum_filter_ne_zero.symm.trans ((sum_congr _ $ λ _ _, rfl).trans sum_filter_ne_zero), ext m, simp only [nat.mem_antidiagonal, mem_add_antidiagonal, and.congr_left_iff, mem_filter, mem_support], rintro h, rw [and_iff_right (left_ne_zero_of_mul h), and_iff_right (right_ne_zero_of_mul h)], end } lemma coeff_to_power_series {f : hahn_series ℕ R} {n : ℕ} : power_series.coeff R n f.to_power_series = f.coeff n := power_series.coeff_mk _ _ lemma coeff_to_power_series_symm {f : power_series R} {n : ℕ} : (hahn_series.to_power_series.symm f).coeff n = power_series.coeff R n f := rfl variables (Γ R) [strict_ordered_semiring Γ] /-- Casts a power series as a Hahn series with coefficients from an `strict_ordered_semiring`. -/ def of_power_series : (power_series R) →+* hahn_series Γ R := (hahn_series.emb_domain_ring_hom (nat.cast_add_monoid_hom Γ) nat.strict_mono_cast.injective (λ _ _, nat.cast_le)).comp (ring_equiv.to_ring_hom to_power_series.symm) variables {Γ} {R} lemma of_power_series_injective : function.injective (of_power_series Γ R) := emb_domain_injective.comp to_power_series.symm.injective @[simp] lemma of_power_series_apply (x : power_series R) : of_power_series Γ R x = hahn_series.emb_domain ⟨⟨(coe : ℕ → Γ), nat.strict_mono_cast.injective⟩, λ a b, begin simp only [function.embedding.coe_fn_mk], exact nat.cast_le, end⟩ (to_power_series.symm x) := rfl lemma of_power_series_apply_coeff (x : power_series R) (n : ℕ) : (of_power_series Γ R x).coeff n = power_series.coeff R n x := by simp @[simp] lemma of_power_series_C (r : R) : of_power_series Γ R (power_series.C R r) = hahn_series.C r := begin ext n, simp only [C, single_coeff, of_power_series_apply, ring_hom.coe_mk], split_ifs with hn hn, { subst hn, convert @emb_domain_coeff _ _ _ _ _ _ _ _ 0; simp }, { rw emb_domain_notin_image_support, simp only [not_exists, set.mem_image, to_power_series_symm_apply_coeff, mem_support, power_series.coeff_C], intro, simp [ne.symm hn] {contextual := tt} } end @[simp] lemma of_power_series_X : of_power_series Γ R power_series.X = single 1 1 := begin ext n, simp only [single_coeff, of_power_series_apply, ring_hom.coe_mk], split_ifs with hn hn, { rw hn, convert @emb_domain_coeff _ _ _ _ _ _ _ _ 1; simp }, { rw emb_domain_notin_image_support, simp only [not_exists, set.mem_image, to_power_series_symm_apply_coeff, mem_support, power_series.coeff_X], intro, simp [ne.symm hn] {contextual := tt} } end @[simp] lemma of_power_series_X_pow {R} [comm_semiring R] (n : ℕ) : of_power_series Γ R (power_series.X ^ n) = single (n : Γ) 1 := begin rw ring_hom.map_pow, induction n with n ih, { simp, refl }, rw [pow_succ, ih, of_power_series_X, mul_comm, single_mul_single, one_mul, nat.cast_succ] end -- Lemmas about converting hahn_series over fintype to and from mv_power_series /-- The ring `hahn_series (σ →₀ ℕ) R` is isomorphic to `mv_power_series σ R` for a `fintype` `σ`. We take the index set of the hahn series to be `finsupp` rather than `pi`, even though we assume `fintype σ` as this is more natural for alignment with `mv_power_series`. After importing `algebra.order.pi` the ring `hahn_series (σ → ℕ) R` could be constructed instead. -/ @[simps] def to_mv_power_series {σ : Type*} [fintype σ] : hahn_series (σ →₀ ℕ) R ≃+* mv_power_series σ R := { to_fun := λ f, f.coeff, inv_fun := λ f, ⟨(f : (σ →₀ ℕ) → R), finsupp.is_pwo _⟩, left_inv := λ f, by { ext, simp }, right_inv := λ f, by { ext, simp }, map_add' := λ f g, by { ext, simp }, map_mul' := λ f g, begin ext n, simp only [mv_power_series.coeff_mul], classical, change (f * g).coeff n = _, simp_rw [mul_coeff], refine sum_filter_ne_zero.symm.trans ((sum_congr _ (λ _ _, rfl)).trans sum_filter_ne_zero), ext m, simp only [and.congr_left_iff, mem_add_antidiagonal, mem_filter, mem_support, finsupp.mem_antidiagonal], rintro h, rw [and_iff_right (left_ne_zero_of_mul h), and_iff_right (right_ne_zero_of_mul h)], end } variables {σ : Type*} [fintype σ] lemma coeff_to_mv_power_series {f : hahn_series (σ →₀ ℕ) R} {n : σ →₀ ℕ} : mv_power_series.coeff R n f.to_mv_power_series = f.coeff n := rfl lemma coeff_to_mv_power_series_symm {f : mv_power_series σ R} {n : σ →₀ ℕ} : (hahn_series.to_mv_power_series.symm f).coeff n = mv_power_series.coeff R n f := rfl end semiring section algebra variables (R) [comm_semiring R] {A : Type*} [semiring A] [algebra R A] /-- The `R`-algebra `hahn_series ℕ A` is isomorphic to `power_series A`. -/ @[simps] def to_power_series_alg : (hahn_series ℕ A) ≃ₐ[R] power_series A := { commutes' := λ r, begin ext n, simp only [algebra_map_apply, power_series.algebra_map_apply, ring_equiv.to_fun_eq_coe, C_apply, coeff_to_power_series], cases n, { simp only [power_series.coeff_zero_eq_constant_coeff, single_coeff_same], refl }, { simp only [n.succ_ne_zero, ne.def, not_false_iff, single_coeff_of_ne], rw [power_series.coeff_C, if_neg n.succ_ne_zero] } end, .. to_power_series } variables (Γ R) [strict_ordered_semiring Γ] /-- Casting a power series as a Hahn series with coefficients from an `strict_ordered_semiring` is an algebra homomorphism. -/ @[simps] def of_power_series_alg : (power_series A) →ₐ[R] hahn_series Γ A := (hahn_series.emb_domain_alg_hom (nat.cast_add_monoid_hom Γ) nat.strict_mono_cast.injective (λ _ _, nat.cast_le)).comp (alg_equiv.to_alg_hom (to_power_series_alg R).symm) instance power_series_algebra {S : Type*} [comm_semiring S] [algebra S (power_series R)] : algebra S (hahn_series Γ R) := ring_hom.to_algebra $ (of_power_series Γ R).comp (algebra_map S (power_series R)) variables {R} {S : Type*} [comm_semiring S] [algebra S (power_series R)] lemma algebra_map_apply' (x : S) : algebra_map S (hahn_series Γ R) x = of_power_series Γ R (algebra_map S (power_series R) x) := rfl @[simp] lemma _root_.polynomial.algebra_map_hahn_series_apply (f : R[X]) : algebra_map R[X] (hahn_series Γ R) f = of_power_series Γ R f := rfl lemma _root_.polynomial.algebra_map_hahn_series_injective : function.injective (algebra_map R[X] (hahn_series Γ R)) := of_power_series_injective.comp (polynomial.coe_injective R) end algebra section valuation variables (Γ R) [linear_ordered_cancel_add_comm_monoid Γ] [ring R] [is_domain R] /-- The additive valuation on `hahn_series Γ R`, returning the smallest index at which a Hahn Series has a nonzero coefficient, or `⊤` for the 0 series. -/ def add_val : add_valuation (hahn_series Γ R) (with_top Γ) := add_valuation.of (λ x, if x = (0 : hahn_series Γ R) then (⊤ : with_top Γ) else x.order) (if_pos rfl) ((if_neg one_ne_zero).trans (by simp [order_of_ne])) (λ x y, begin by_cases hx : x = 0, { by_cases hy : y = 0; { simp [hx, hy] } }, { by_cases hy : y = 0, { simp [hx, hy] }, { simp only [hx, hy, support_nonempty_iff, if_neg, not_false_iff, is_wf_support], by_cases hxy : x + y = 0, { simp [hxy] }, rw [if_neg hxy, ← with_top.coe_min, with_top.coe_le_coe], exact min_order_le_order_add hxy } }, end) (λ x y, begin by_cases hx : x = 0, { simp [hx] }, by_cases hy : y = 0, { simp [hy] }, rw [if_neg hx, if_neg hy, if_neg (mul_ne_zero hx hy), ← with_top.coe_add, with_top.coe_eq_coe, order_mul hx hy], end) variables {Γ} {R} lemma add_val_apply {x : hahn_series Γ R} : add_val Γ R x = if x = (0 : hahn_series Γ R) then (⊤ : with_top Γ) else x.order := add_valuation.of_apply _ @[simp] lemma add_val_apply_of_ne {x : hahn_series Γ R} (hx : x ≠ 0) : add_val Γ R x = x.order := if_neg hx lemma add_val_le_of_coeff_ne_zero {x : hahn_series Γ R} {g : Γ} (h : x.coeff g ≠ 0) : add_val Γ R x ≤ g := begin rw [add_val_apply_of_ne (ne_zero_of_coeff_ne_zero h), with_top.coe_le_coe], exact order_le_of_coeff_ne_zero h end end valuation lemma is_pwo_Union_support_powers [linear_ordered_cancel_add_comm_monoid Γ] [ring R] [is_domain R] {x : hahn_series Γ R} (hx : 0 < add_val Γ R x) : (⋃ n : ℕ, (x ^ n).support).is_pwo := begin apply (x.is_wf_support.is_pwo.add_submonoid_closure (λ g hg, _)).mono _, { exact with_top.coe_le_coe.1 (le_trans (le_of_lt hx) (add_val_le_of_coeff_ne_zero hg)) }, refine set.Union_subset (λ n, _), induction n with n ih; intros g hn, { simp only [exists_prop, and_true, set.mem_singleton_iff, set.set_of_eq_eq_singleton, mem_support, ite_eq_right_iff, ne.def, not_false_iff, one_ne_zero, pow_zero, not_forall, one_coeff] at hn, rw [hn, set_like.mem_coe], exact add_submonoid.zero_mem _ }, { obtain ⟨i, j, hi, hj, rfl⟩ := support_mul_subset_add_support hn, exact set_like.mem_coe.2 (add_submonoid.add_mem _ (add_submonoid.subset_closure hi) (ih hj)) } end section variables (Γ) (R) [partial_order Γ] [add_comm_monoid R] /-- An infinite family of Hahn series which has a formal coefficient-wise sum. The requirements for this are that the union of the supports of the series is well-founded, and that only finitely many series are nonzero at any given coefficient. -/ structure summable_family (α : Type*) := (to_fun : α → hahn_series Γ R) (is_pwo_Union_support' : set.is_pwo (⋃ (a : α), (to_fun a).support)) (finite_co_support' : ∀ (g : Γ), ({a | (to_fun a).coeff g ≠ 0}).finite) end namespace summable_family section add_comm_monoid variables [partial_order Γ] [add_comm_monoid R] {α : Type*} instance : has_coe_to_fun (summable_family Γ R α) (λ _, α → hahn_series Γ R):= ⟨to_fun⟩ lemma is_pwo_Union_support (s : summable_family Γ R α) : set.is_pwo (⋃ (a : α), (s a).support) := s.is_pwo_Union_support' lemma finite_co_support (s : summable_family Γ R α) (g : Γ) : (function.support (λ a, (s a).coeff g)).finite := s.finite_co_support' g lemma coe_injective : @function.injective (summable_family Γ R α) (α → hahn_series Γ R) coe_fn | ⟨f1, hU1, hf1⟩ ⟨f2, hU2, hf2⟩ h := begin change f1 = f2 at h, subst h, end @[ext] lemma ext {s t : summable_family Γ R α} (h : ∀ (a : α), s a = t a) : s = t := coe_injective $ funext h instance : has_add (summable_family Γ R α) := ⟨λ x y, { to_fun := x + y, is_pwo_Union_support' := (x.is_pwo_Union_support.union y.is_pwo_Union_support).mono (begin rw ← set.Union_union_distrib, exact set.Union_mono (λ a, support_add_subset) end), finite_co_support' := λ g, ((x.finite_co_support g).union (y.finite_co_support g)).subset begin intros a ha, change (x a).coeff g + (y a).coeff g ≠ 0 at ha, rw [set.mem_union, function.mem_support, function.mem_support], contrapose! ha, rw [ha.1, ha.2, add_zero] end }⟩ instance : has_zero (summable_family Γ R α) := ⟨⟨0, by simp, by simp⟩⟩ instance : inhabited (summable_family Γ R α) := ⟨0⟩ @[simp] lemma coe_add {s t : summable_family Γ R α} : ⇑(s + t) = s + t := rfl lemma add_apply {s t : summable_family Γ R α} {a : α} : (s + t) a = s a + t a := rfl @[simp] lemma coe_zero : ((0 : summable_family Γ R α) : α → hahn_series Γ R) = 0 := rfl lemma zero_apply {a : α} : (0 : summable_family Γ R α) a = 0 := rfl instance : add_comm_monoid (summable_family Γ R α) := { add := (+), zero := 0, zero_add := λ s, by { ext, apply zero_add }, add_zero := λ s, by { ext, apply add_zero }, add_comm := λ s t, by { ext, apply add_comm }, add_assoc := λ r s t, by { ext, apply add_assoc } } /-- The infinite sum of a `summable_family` of Hahn series. -/ def hsum (s : summable_family Γ R α) : hahn_series Γ R := { coeff := λ g, ∑ᶠ i, (s i).coeff g, is_pwo_support' := s.is_pwo_Union_support.mono (λ g, begin contrapose, rw [set.mem_Union, not_exists, function.mem_support, not_not], simp_rw [mem_support, not_not], intro h, rw [finsum_congr h, finsum_zero], end) } @[simp] lemma hsum_coeff {s : summable_family Γ R α} {g : Γ} : s.hsum.coeff g = ∑ᶠ i, (s i).coeff g := rfl lemma support_hsum_subset {s : summable_family Γ R α} : s.hsum.support ⊆ ⋃ (a : α), (s a).support := λ g hg, begin rw [mem_support, hsum_coeff, finsum_eq_sum _ (s.finite_co_support _)] at hg, obtain ⟨a, h1, h2⟩ := exists_ne_zero_of_sum_ne_zero hg, rw [set.mem_Union], exact ⟨a, h2⟩, end @[simp] lemma hsum_add {s t : summable_family Γ R α} : (s + t).hsum = s.hsum + t.hsum := begin ext g, simp only [hsum_coeff, add_coeff, add_apply], exact finsum_add_distrib (s.finite_co_support _) (t.finite_co_support _) end end add_comm_monoid section add_comm_group variables [partial_order Γ] [add_comm_group R] {α : Type*} {s t : summable_family Γ R α} {a : α} instance : add_comm_group (summable_family Γ R α) := { neg := λ s, { to_fun := λ a, - s a, is_pwo_Union_support' := by { simp_rw [support_neg], exact s.is_pwo_Union_support' }, finite_co_support' := λ g, by { simp only [neg_coeff', pi.neg_apply, ne.def, neg_eq_zero], exact s.finite_co_support g } }, add_left_neg := λ a, by { ext, apply add_left_neg }, .. summable_family.add_comm_monoid } @[simp] lemma coe_neg : ⇑(-s) = - s := rfl lemma neg_apply : (-s) a = - (s a) := rfl @[simp] lemma coe_sub : ⇑(s - t) = s - t := rfl lemma sub_apply : (s - t) a = s a - t a := rfl end add_comm_group section semiring variables [ordered_cancel_add_comm_monoid Γ] [semiring R] {α : Type*} instance : has_smul (hahn_series Γ R) (summable_family Γ R α) := { smul := λ x s, { to_fun := λ a, x * (s a), is_pwo_Union_support' := begin apply (x.is_pwo_support.add s.is_pwo_Union_support).mono, refine set.subset.trans (set.Union_mono (λ a, support_mul_subset_add_support)) _, intro g, simp only [set.mem_Union, exists_imp_distrib], exact λ a ha, (set.add_subset_add (set.subset.refl _) (set.subset_Union _ a)) ha, end, finite_co_support' := λ g, begin refine ((add_antidiagonal x.is_pwo_support s.is_pwo_Union_support g).finite_to_set.bUnion' (λ ij hij, _)).subset (λ a ha, _), { exact λ ij hij, function.support (λ a, (s a).coeff ij.2) }, { apply s.finite_co_support }, { obtain ⟨i, j, hi, hj, rfl⟩ := support_mul_subset_add_support ha, simp only [exists_prop, set.mem_Union, mem_add_antidiagonal, mul_coeff, mem_support, is_pwo_support, prod.exists], exact ⟨i, j, mem_coe.2 (mem_add_antidiagonal.2 ⟨hi, set.mem_Union.2 ⟨a, hj⟩, rfl⟩), hj⟩ } end } } @[simp] lemma smul_apply {x : hahn_series Γ R} {s : summable_family Γ R α} {a : α} : (x • s) a = x * (s a) := rfl instance : module (hahn_series Γ R) (summable_family Γ R α) := { smul := (•), smul_zero := λ x, ext (λ a, mul_zero _), zero_smul := λ x, ext (λ a, zero_mul _), one_smul := λ x, ext (λ a, one_mul _), add_smul := λ x y s, ext (λ a, add_mul _ _ _), smul_add := λ x s t, ext (λ a, mul_add _ _ _), mul_smul := λ x y s, ext (λ a, mul_assoc _ _ _) } @[simp] lemma hsum_smul {x : hahn_series Γ R} {s : summable_family Γ R α} : (x • s).hsum = x * s.hsum := begin ext g, simp only [mul_coeff, hsum_coeff, smul_apply], have h : ∀ i, (s i).support ⊆ ⋃ j, (s j).support := set.subset_Union _, refine (eq.trans (finsum_congr (λ a, _)) (finsum_sum_comm (add_antidiagonal x.is_pwo_support s.is_pwo_Union_support g) (λ i ij, x.coeff (prod.fst ij) * (s i).coeff ij.snd) _)).trans _, { refine sum_subset (add_antidiagonal_mono_right (set.subset_Union _ a)) _, rintro ⟨i, j⟩ hU ha, rw mem_add_antidiagonal at *, rw [not_not.1 (λ con, ha ⟨hU.1, con, hU.2.2⟩), mul_zero] }, { rintro ⟨i, j⟩ hij, refine (s.finite_co_support j).subset _, simp_rw [function.support_subset_iff', function.mem_support, not_not], intros a ha, rw [ha, mul_zero] }, { refine (sum_congr rfl _).trans (sum_subset (add_antidiagonal_mono_right _) _).symm, { rintro ⟨i, j⟩ hij, rw mul_finsum, apply s.finite_co_support, }, { intros x hx, simp only [set.mem_Union, ne.def, mem_support], contrapose! hx, simp [hx] }, { rintro ⟨i, j⟩ hU ha, rw mem_add_antidiagonal at *, rw [← hsum_coeff, not_not.1 (λ con, ha ⟨hU.1, con, hU.2.2⟩), mul_zero] } } end /-- The summation of a `summable_family` as a `linear_map`. -/ @[simps] def lsum : (summable_family Γ R α) →ₗ[hahn_series Γ R] (hahn_series Γ R) := { to_fun := hsum, map_add' := λ _ _, hsum_add, map_smul' := λ _ _, hsum_smul } @[simp] lemma hsum_sub {R : Type*} [ring R] {s t : summable_family Γ R α} : (s - t).hsum = s.hsum - t.hsum := by rw [← lsum_apply, linear_map.map_sub, lsum_apply, lsum_apply] end semiring section of_finsupp variables [partial_order Γ] [add_comm_monoid R] {α : Type*} /-- A family with only finitely many nonzero elements is summable. -/ def of_finsupp (f : α →₀ (hahn_series Γ R)) : summable_family Γ R α := { to_fun := f, is_pwo_Union_support' := begin apply (f.support.is_pwo_bUnion.2 $ λ a ha, (f a).is_pwo_support).mono, refine set.Union_subset_iff.2 (λ a g hg, _), have haf : a ∈ f.support, { rw [finsupp.mem_support_iff, ← support_nonempty_iff], exact ⟨g, hg⟩ }, exact set.mem_bUnion haf hg end, finite_co_support' := λ g, begin refine f.support.finite_to_set.subset (λ a ha, _), simp only [coeff.add_monoid_hom_apply, mem_coe, finsupp.mem_support_iff, ne.def, function.mem_support], contrapose! ha, simp [ha] end } @[simp] lemma coe_of_finsupp {f : α →₀ (hahn_series Γ R)} : ⇑(summable_family.of_finsupp f) = f := rfl @[simp] lemma hsum_of_finsupp {f : α →₀ (hahn_series Γ R)} : (of_finsupp f).hsum = f.sum (λ a, id) := begin ext g, simp only [hsum_coeff, coe_of_finsupp, finsupp.sum, ne.def], simp_rw [← coeff.add_monoid_hom_apply, id.def], rw [add_monoid_hom.map_sum, finsum_eq_sum_of_support_subset], intros x h, simp only [coeff.add_monoid_hom_apply, mem_coe, finsupp.mem_support_iff, ne.def], contrapose! h, simp [h] end end of_finsupp section emb_domain variables [partial_order Γ] [add_comm_monoid R] {α β : Type*} /-- A summable family can be reindexed by an embedding without changing its sum. -/ def emb_domain (s : summable_family Γ R α) (f : α ↪ β) : summable_family Γ R β := { to_fun := λ b, if h : b ∈ set.range f then s (classical.some h) else 0, is_pwo_Union_support' := begin refine s.is_pwo_Union_support.mono (set.Union_subset (λ b g h, _)), by_cases hb : b ∈ set.range f, { rw dif_pos hb at h, exact set.mem_Union.2 ⟨classical.some hb, h⟩ }, { contrapose! h, simp [hb] } end, finite_co_support' := λ g, ((s.finite_co_support g).image f).subset begin intros b h, by_cases hb : b ∈ set.range f, { simp only [ne.def, set.mem_set_of_eq, dif_pos hb] at h, exact ⟨classical.some hb, h, classical.some_spec hb⟩ }, { contrapose! h, simp only [ne.def, set.mem_set_of_eq, dif_neg hb, not_not, zero_coeff] } end } variables (s : summable_family Γ R α) (f : α ↪ β) {a : α} {b : β} lemma emb_domain_apply : s.emb_domain f b = if h : b ∈ set.range f then s (classical.some h) else 0 := rfl @[simp] lemma emb_domain_image : s.emb_domain f (f a) = s a := begin rw [emb_domain_apply, dif_pos (set.mem_range_self a)], exact congr rfl (f.injective (classical.some_spec (set.mem_range_self a))) end @[simp] lemma emb_domain_notin_range (h : b ∉ set.range f) : s.emb_domain f b = 0 := by rw [emb_domain_apply, dif_neg h] @[simp] lemma hsum_emb_domain : (s.emb_domain f).hsum = s.hsum := begin ext g, simp only [hsum_coeff, emb_domain_apply, apply_dite hahn_series.coeff, dite_apply, zero_coeff], exact finsum_emb_domain f (λ a, (s a).coeff g) end end emb_domain section powers variables [linear_ordered_cancel_add_comm_monoid Γ] [comm_ring R] [is_domain R] /-- The powers of an element of positive valuation form a summable family. -/ def powers (x : hahn_series Γ R) (hx : 0 < add_val Γ R x) : summable_family Γ R ℕ := { to_fun := λ n, x ^ n, is_pwo_Union_support' := is_pwo_Union_support_powers hx, finite_co_support' := λ g, begin have hpwo := (is_pwo_Union_support_powers hx), by_cases hg : g ∈ ⋃ n : ℕ, {g | (x ^ n).coeff g ≠ 0 }, swap, { exact set.finite_empty.subset (λ n hn, hg (set.mem_Union.2 ⟨n, hn⟩)) }, apply hpwo.is_wf.induction hg, intros y ys hy, refine ((((add_antidiagonal x.is_pwo_support hpwo y).finite_to_set.bUnion (λ ij hij, hy ij.snd _ _)).image nat.succ).union (set.finite_singleton 0)).subset _, { exact (mem_add_antidiagonal.1 (mem_coe.1 hij)).2.1 }, { obtain ⟨hi, hj, rfl⟩ := mem_add_antidiagonal.1 (mem_coe.1 hij), rw [← zero_add ij.snd, ← add_assoc, add_zero], exact add_lt_add_right (with_top.coe_lt_coe.1 (lt_of_lt_of_le hx (add_val_le_of_coeff_ne_zero hi))) _, }, { rintro (_ | n) hn, { exact set.mem_union_right _ (set.mem_singleton 0) }, { obtain ⟨i, j, hi, hj, rfl⟩ := support_mul_subset_add_support hn, refine set.mem_union_left _ ⟨n, set.mem_Union.2 ⟨⟨i, j⟩, set.mem_Union.2 ⟨_, hj⟩⟩, rfl⟩, simp only [and_true, set.mem_Union, mem_add_antidiagonal, mem_coe, eq_self_iff_true, ne.def, mem_support, set.mem_set_of_eq], exact ⟨hi, n, hj⟩ } } end } variables {x : hahn_series Γ R} (hx : 0 < add_val Γ R x) @[simp] lemma coe_powers : ⇑(powers x hx) = pow x := rfl lemma emb_domain_succ_smul_powers : (x • powers x hx).emb_domain ⟨nat.succ, nat.succ_injective⟩ = powers x hx - of_finsupp (finsupp.single 0 1) := begin apply summable_family.ext (λ n, _), cases n, { rw [emb_domain_notin_range, sub_apply, coe_powers, pow_zero, coe_of_finsupp, finsupp.single_eq_same, sub_self], rw [set.mem_range, not_exists], exact nat.succ_ne_zero }, { refine eq.trans (emb_domain_image _ ⟨nat.succ, nat.succ_injective⟩) _, simp only [pow_succ, coe_powers, coe_sub, smul_apply, coe_of_finsupp, pi.sub_apply], rw [finsupp.single_eq_of_ne (n.succ_ne_zero).symm, sub_zero] } end lemma one_sub_self_mul_hsum_powers : (1 - x) * (powers x hx).hsum = 1 := begin rw [← hsum_smul, sub_smul, one_smul, hsum_sub, ← hsum_emb_domain (x • powers x hx) ⟨nat.succ, nat.succ_injective⟩, emb_domain_succ_smul_powers], simp, end end powers end summable_family section inversion variables [linear_ordered_add_comm_group Γ] section is_domain variables [comm_ring R] [is_domain R] lemma unit_aux (x : hahn_series Γ R) {r : R} (hr : r * x.coeff x.order = 1) : 0 < add_val Γ R (1 - C r * (single (- x.order) 1) * x) := begin have h10 : (1 : R) ≠ 0 := one_ne_zero, have x0 : x ≠ 0 := ne_zero_of_coeff_ne_zero (right_ne_zero_of_mul_eq_one hr), refine lt_of_le_of_ne ((add_val Γ R).map_le_sub (ge_of_eq (add_val Γ R).map_one) _) _, { simp only [add_valuation.map_mul], rw [add_val_apply_of_ne x0, add_val_apply_of_ne (single_ne_zero h10), add_val_apply_of_ne _, order_C, order_single h10, with_top.coe_zero, zero_add, ← with_top.coe_add, neg_add_self, with_top.coe_zero], { exact le_refl 0 }, { exact C_ne_zero (left_ne_zero_of_mul_eq_one hr) } }, { rw [add_val_apply, ← with_top.coe_zero], split_ifs, { apply with_top.coe_ne_top }, rw [ne.def, with_top.coe_eq_coe], intro con, apply coeff_order_ne_zero h, rw [← con, mul_assoc, sub_coeff, one_coeff, if_pos rfl, C_mul_eq_smul, smul_coeff, smul_eq_mul, ← add_neg_self x.order, single_mul_coeff_add, one_mul, hr, sub_self] } end lemma is_unit_iff {x : hahn_series Γ R} : is_unit x ↔ is_unit (x.coeff x.order) := begin split, { rintro ⟨⟨u, i, ui, iu⟩, rfl⟩, refine is_unit_of_mul_eq_one (u.coeff u.order) (i.coeff i.order) ((mul_coeff_order_add_order u i).symm.trans _), rw [ui, one_coeff, if_pos], rw [← order_mul (left_ne_zero_of_mul_eq_one ui) (right_ne_zero_of_mul_eq_one ui), ui, order_one] }, { rintro ⟨⟨u, i, ui, iu⟩, h⟩, rw [units.coe_mk] at h, rw h at iu, have h := summable_family.one_sub_self_mul_hsum_powers (unit_aux x iu), rw [sub_sub_cancel] at h, exact is_unit_of_mul_is_unit_right (is_unit_of_mul_eq_one _ _ h) }, end end is_domain instance [field R] : field (hahn_series Γ R) := { inv := λ x, if x0 : x = 0 then 0 else (C (x.coeff x.order)⁻¹ * (single (-x.order)) 1 * (summable_family.powers _ (unit_aux x (inv_mul_cancel (coeff_order_ne_zero x0)))).hsum), inv_zero := dif_pos rfl, mul_inv_cancel := λ x x0, begin refine (congr rfl (dif_neg x0)).trans _, have h := summable_family.one_sub_self_mul_hsum_powers (unit_aux x (inv_mul_cancel (coeff_order_ne_zero x0))), rw [sub_sub_cancel] at h, rw [← mul_assoc, mul_comm x, h], end, .. hahn_series.is_domain, .. hahn_series.comm_ring } end inversion end hahn_series
7121e7bcc413a05ef76921ccd736c3487f84ce25
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/quote1.lean
97139db658f4ae6a3014fe3a5ccfee85bf993a12
[ "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
315
lean
open tactic list meta definition foo (a : pexpr) : pexpr := ```(%%a + %%a + %%a + b) example (a b : nat) : a = a := by do a ← get_local `a, t1 ← mk_app ``has_add.add [a, a], t2 ← to_expr (foo (to_pexpr t1)), trace t2, r ← mk_app (`eq.refl) [a], exact r private def f := unit #check ``f
2ea05b1464d8dbf16e5da266b5e051145f092679
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/ring_theory/polynomial/basic.lean
dff704c08562ab38c7d9b7a7c5aa861706920fdd
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
50,677
lean
/- Copyright (c) 2019 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import algebra.char_p.basic import data.mv_polynomial.comm_ring import data.mv_polynomial.equiv import ring_theory.polynomial.content import ring_theory.unique_factorization_domain /-! # Ring-theoretic supplement of data.polynomial. ## Main results * `mv_polynomial.is_domain`: If a ring is an integral domain, then so is its polynomial ring over finitely many variables. * `polynomial.is_noetherian_ring`: Hilbert basis theorem, that if a ring is noetherian then so is its polynomial ring. * `polynomial.wf_dvd_monoid`: If an integral domain is a `wf_dvd_monoid`, then so is its polynomial ring. * `polynomial.unique_factorization_monoid`, `mv_polynomial.unique_factorization_monoid`: If an integral domain is a `unique_factorization_monoid`, then so is its polynomial ring (of any number of variables). -/ noncomputable theory open_locale classical big_operators polynomial open finset universes u v w variables {R : Type u} {S : Type*} namespace polynomial section semiring variables [semiring R] instance (p : ℕ) [h : char_p R p] : char_p R[X] p := let ⟨h⟩ := h in ⟨λ n, by rw [← map_nat_cast C, ← C_0, C_inj, h]⟩ variables (R) /-- The `R`-submodule of `R[X]` consisting of polynomials of degree ≤ `n`. -/ def degree_le (n : with_bot ℕ) : submodule R R[X] := ⨅ k : ℕ, ⨅ h : ↑k > n, (lcoeff R k).ker /-- The `R`-submodule of `R[X]` consisting of polynomials of degree < `n`. -/ def degree_lt (n : ℕ) : submodule R R[X] := ⨅ k : ℕ, ⨅ h : k ≥ n, (lcoeff R k).ker variable {R} theorem mem_degree_le {n : with_bot ℕ} {f : R[X]} : f ∈ degree_le R n ↔ degree f ≤ n := by simp only [degree_le, submodule.mem_infi, degree_le_iff_coeff_zero, linear_map.mem_ker]; refl @[mono] theorem degree_le_mono {m n : with_bot ℕ} (H : m ≤ n) : degree_le R m ≤ degree_le R n := λ f hf, mem_degree_le.2 (le_trans (mem_degree_le.1 hf) H) theorem degree_le_eq_span_X_pow {n : ℕ} : degree_le R n = submodule.span R ↑((finset.range (n+1)).image (λ n, (X : R[X])^n)) := begin apply le_antisymm, { intros p hp, replace hp := mem_degree_le.1 hp, rw [← polynomial.sum_monomial_eq p, polynomial.sum], refine submodule.sum_mem _ (λ k hk, _), show monomial _ _ ∈ _, have := with_bot.coe_le_coe.1 (finset.sup_le_iff.1 hp k hk), rw [monomial_eq_C_mul_X, C_mul'], refine submodule.smul_mem _ _ (submodule.subset_span $ finset.mem_coe.2 $ finset.mem_image.2 ⟨_, finset.mem_range.2 (nat.lt_succ_of_le this), rfl⟩) }, rw [submodule.span_le, finset.coe_image, set.image_subset_iff], intros k hk, apply mem_degree_le.2, exact (degree_X_pow_le _).trans (with_bot.coe_le_coe.2 $ nat.le_of_lt_succ $ finset.mem_range.1 hk) end theorem mem_degree_lt {n : ℕ} {f : R[X]} : f ∈ degree_lt R n ↔ degree f < n := by { simp_rw [degree_lt, submodule.mem_infi, linear_map.mem_ker, degree, finset.max_eq_sup_coe, finset.sup_lt_iff (with_bot.bot_lt_coe n), mem_support_iff, with_bot.coe_lt_coe, lt_iff_not_le, ne, not_imp_not], refl } @[mono] theorem degree_lt_mono {m n : ℕ} (H : m ≤ n) : degree_lt R m ≤ degree_lt R n := λ f hf, mem_degree_lt.2 (lt_of_lt_of_le (mem_degree_lt.1 hf) $ with_bot.coe_le_coe.2 H) theorem degree_lt_eq_span_X_pow {n : ℕ} : degree_lt R n = submodule.span R ↑((finset.range n).image (λ n, X^n) : finset R[X]) := begin apply le_antisymm, { intros p hp, replace hp := mem_degree_lt.1 hp, rw [← polynomial.sum_monomial_eq p, polynomial.sum], refine submodule.sum_mem _ (λ k hk, _), show monomial _ _ ∈ _, have := with_bot.coe_lt_coe.1 ((finset.sup_lt_iff $ with_bot.bot_lt_coe n).1 hp k hk), rw [monomial_eq_C_mul_X, C_mul'], refine submodule.smul_mem _ _ (submodule.subset_span $ finset.mem_coe.2 $ finset.mem_image.2 ⟨_, finset.mem_range.2 this, rfl⟩) }, rw [submodule.span_le, finset.coe_image, set.image_subset_iff], intros k hk, apply mem_degree_lt.2, exact lt_of_le_of_lt (degree_X_pow_le _) (with_bot.coe_lt_coe.2 $ finset.mem_range.1 hk) end /-- The first `n` coefficients on `degree_lt n` form a linear equivalence with `fin n → R`. -/ def degree_lt_equiv (R) [semiring R] (n : ℕ) : degree_lt R n ≃ₗ[R] (fin n → R) := { to_fun := λ p n, (↑p : R[X]).coeff n, inv_fun := λ f, ⟨∑ i : fin n, monomial i (f i), (degree_lt R n).sum_mem (λ i _, mem_degree_lt.mpr (lt_of_le_of_lt (degree_monomial_le i (f i)) (with_bot.coe_lt_coe.mpr i.is_lt)))⟩, map_add' := λ p q, by { ext, rw [submodule.coe_add, coeff_add], refl }, map_smul' := λ x p, by { ext, rw [submodule.coe_smul, coeff_smul], refl }, left_inv := begin rintro ⟨p, hp⟩, ext1, simp only [submodule.coe_mk], by_cases hp0 : p = 0, { subst hp0, simp only [coeff_zero, linear_map.map_zero, finset.sum_const_zero] }, rw [mem_degree_lt, degree_eq_nat_degree hp0, with_bot.coe_lt_coe] at hp, conv_rhs { rw [p.as_sum_range' n hp, ← fin.sum_univ_eq_sum_range] }, end, right_inv := begin intro f, ext i, simp only [finset_sum_coeff, submodule.coe_mk], rw [finset.sum_eq_single i, coeff_monomial, if_pos rfl], { rintro j - hji, rw [coeff_monomial, if_neg], rwa [← subtype.ext_iff] }, { intro h, exact (h (finset.mem_univ _)).elim } end } @[simp] theorem degree_lt_equiv_eq_zero_iff_eq_zero {n : ℕ} {p : R[X]} (hp : p ∈ degree_lt R n) : degree_lt_equiv _ _ ⟨p, hp⟩ = 0 ↔ p = 0 := by rw [linear_equiv.map_eq_zero_iff, submodule.mk_eq_zero] theorem eval_eq_sum_degree_lt_equiv {n : ℕ} {p : R[X]} (hp : p ∈ degree_lt R n) (x : R) : p.eval x = ∑ i, degree_lt_equiv _ _ ⟨p, hp⟩ i * (x ^ (i : ℕ)) := begin simp_rw [eval_eq_sum], exact (sum_fin _ (by simp_rw [zero_mul, forall_const]) (mem_degree_lt.mp hp)).symm end /-- The finset of nonzero coefficients of a polynomial. -/ def frange (p : R[X]) : finset R := finset.image (λ n, p.coeff n) p.support lemma frange_zero : frange (0 : R[X]) = ∅ := rfl lemma mem_frange_iff {p : R[X]} {c : R} : c ∈ p.frange ↔ ∃ n ∈ p.support, c = p.coeff n := by simp [frange, eq_comm] lemma frange_one : frange (1 : R[X]) ⊆ {1} := begin simp [frange, finset.image_subset_iff], simp only [← C_1, coeff_C], assume n hn, simp only [exists_prop, ite_eq_right_iff, not_forall] at hn, simp [hn], end lemma coeff_mem_frange (p : R[X]) (n : ℕ) (h : p.coeff n ≠ 0) : p.coeff n ∈ p.frange := begin simp only [frange, exists_prop, mem_support_iff, finset.mem_image, ne.def], exact ⟨n, h, rfl⟩, end lemma geom_sum_X_comp_X_add_one_eq_sum (n : ℕ) : (∑ i in range n, (X : R[X]) ^ i).comp (X + 1) = (finset.range n).sum (λ (i : ℕ), (n.choose (i + 1) : R[X]) * X ^ i) := begin ext i, transitivity (n.choose (i + 1) : R), swap, { simp only [finset_sum_coeff, ← C_eq_nat_cast, coeff_C_mul_X_pow], rw [finset.sum_eq_single i, if_pos rfl], { simp only [@eq_comm _ i, if_false, eq_self_iff_true, implies_true_iff] {contextual := tt}, }, { simp only [nat.lt_add_one_iff, nat.choose_eq_zero_of_lt, nat.cast_zero, finset.mem_range, not_lt, eq_self_iff_true, if_true, implies_true_iff] {contextual := tt}, } }, induction n with n ih generalizing i, { simp only [geom_sum_zero, zero_comp, coeff_zero, nat.choose_zero_succ, nat.cast_zero], }, simp only [geom_sum_succ', ih, add_comp, X_pow_comp, coeff_add, nat.choose_succ_succ, nat.cast_add, coeff_X_add_one_pow], end lemma monic.geom_sum {P : R[X]} (hP : P.monic) (hdeg : 0 < P.nat_degree) {n : ℕ} (hn : n ≠ 0) : (∑ i in range n, P ^ i).monic := begin nontriviality R, cases n, { exact (hn rfl).elim }, rw [geom_sum_succ'], refine (hP.pow _).add_of_left _, refine lt_of_le_of_lt (degree_sum_le _ _) _, rw [finset.sup_lt_iff], { simp only [finset.mem_range, degree_eq_nat_degree (hP.pow _).ne_zero, with_bot.coe_lt_coe, hP.nat_degree_pow], intro k, exact nsmul_lt_nsmul hdeg }, { rw [bot_lt_iff_ne_bot, ne.def, degree_eq_bot], exact (hP.pow _).ne_zero } end lemma monic.geom_sum' {P : R[X]} (hP : P.monic) (hdeg : 0 < P.degree) {n : ℕ} (hn : n ≠ 0) : (∑ i in range n, P ^ i).monic := hP.geom_sum (nat_degree_pos_iff_degree_pos.2 hdeg) hn lemma monic_geom_sum_X {n : ℕ} (hn : n ≠ 0) : (∑ i in range n, (X : R[X]) ^ i).monic := begin nontriviality R, apply monic_X.geom_sum _ hn, simpa only [nat_degree_X] using zero_lt_one end end semiring section ring variables [ring R] /-- Given a polynomial, return the polynomial whose coefficients are in the ring closure of the original coefficients. -/ def restriction (p : R[X]) : polynomial (subring.closure (↑p.frange : set R)) := ∑ i in p.support, monomial i (⟨p.coeff i, if H : p.coeff i = 0 then H.symm ▸ (subring.closure _).zero_mem else subring.subset_closure (p.coeff_mem_frange _ H)⟩ : (subring.closure (↑p.frange : set R))) @[simp] theorem coeff_restriction {p : R[X]} {n : ℕ} : ↑(coeff (restriction p) n) = coeff p n := begin simp only [restriction, coeff_monomial, finset_sum_coeff, mem_support_iff, finset.sum_ite_eq', ne.def, ite_not], split_ifs, { rw h, refl }, { refl } end @[simp] theorem coeff_restriction' {p : R[X]} {n : ℕ} : (coeff (restriction p) n).1 = coeff p n := coeff_restriction @[simp] lemma support_restriction (p : R[X]) : support (restriction p) = support p := begin ext i, simp only [mem_support_iff, not_iff_not, ne.def], conv_rhs { rw [← coeff_restriction] }, exact ⟨λ H, by { rw H, refl }, λ H, subtype.coe_injective H⟩ end @[simp] theorem map_restriction {R : Type u} [comm_ring R] (p : R[X]) : p.restriction.map (algebra_map _ _) = p := ext $ λ n, by rw [coeff_map, algebra.algebra_map_of_subring_apply, coeff_restriction] @[simp] theorem degree_restriction {p : R[X]} : (restriction p).degree = p.degree := by simp [degree] @[simp] theorem nat_degree_restriction {p : R[X]} : (restriction p).nat_degree = p.nat_degree := by simp [nat_degree] @[simp] theorem monic_restriction {p : R[X]} : monic (restriction p) ↔ monic p := begin simp only [monic, leading_coeff, nat_degree_restriction], rw [←@coeff_restriction _ _ p], exact ⟨λ H, by { rw H, refl }, λ H, subtype.coe_injective H⟩ end @[simp] theorem restriction_zero : restriction (0 : R[X]) = 0 := by simp only [restriction, finset.sum_empty, support_zero] @[simp] theorem restriction_one : restriction (1 : R[X]) = 1 := ext $ λ i, subtype.eq $ by rw [coeff_restriction', coeff_one, coeff_one]; split_ifs; refl variables [semiring S] {f : R →+* S} {x : S} theorem eval₂_restriction {p : R[X]} : eval₂ f x p = eval₂ (f.comp (subring.subtype (subring.closure (p.frange : set R)))) x p.restriction := begin simp only [eval₂_eq_sum, sum, support_restriction, ←@coeff_restriction _ _ p], refl, end section to_subring variables (p : R[X]) (T : subring R) /-- Given a polynomial `p` and a subring `T` that contains the coefficients of `p`, return the corresponding polynomial whose coefficients are in `T`. -/ def to_subring (hp : (↑p.frange : set R) ⊆ T) : T[X] := ∑ i in p.support, monomial i (⟨p.coeff i, if H : p.coeff i = 0 then H.symm ▸ T.zero_mem else hp (p.coeff_mem_frange _ H)⟩ : T) variables (hp : (↑p.frange : set R) ⊆ T) include hp @[simp] theorem coeff_to_subring {n : ℕ} : ↑(coeff (to_subring p T hp) n) = coeff p n := begin simp only [to_subring, coeff_monomial, finset_sum_coeff, mem_support_iff, finset.sum_ite_eq', ne.def, ite_not], split_ifs, { rw h, refl }, { refl } end @[simp] theorem coeff_to_subring' {n : ℕ} : (coeff (to_subring p T hp) n).1 = coeff p n := coeff_to_subring _ _ hp @[simp] lemma support_to_subring : support (to_subring p T hp) = support p := begin ext i, simp only [mem_support_iff, not_iff_not, ne.def], conv_rhs { rw [← coeff_to_subring p T hp] }, exact ⟨λ H, by { rw H, refl }, λ H, subtype.coe_injective H⟩ end @[simp] theorem degree_to_subring : (to_subring p T hp).degree = p.degree := by simp [degree] @[simp] theorem nat_degree_to_subring : (to_subring p T hp).nat_degree = p.nat_degree := by simp [nat_degree] @[simp] theorem monic_to_subring : monic (to_subring p T hp) ↔ monic p := begin simp_rw [monic, leading_coeff, nat_degree_to_subring, ← coeff_to_subring p T hp], exact ⟨λ H, by { rw H, refl }, λ H, subtype.coe_injective H⟩ end omit hp @[simp] theorem to_subring_zero : to_subring (0 : R[X]) T (by simp [frange_zero]) = 0 := by { ext i, simp } @[simp] theorem to_subring_one : to_subring (1 : R[X]) T (set.subset.trans frange_one $finset.singleton_subset_set_iff.2 T.one_mem) = 1 := ext $ λ i, subtype.eq $ by rw [coeff_to_subring', coeff_one, coeff_one]; split_ifs; refl @[simp] theorem map_to_subring : (p.to_subring T hp).map (subring.subtype T) = p := by { ext n, simp [coeff_map] } end to_subring variables (T : subring R) /-- Given a polynomial whose coefficients are in some subring, return the corresponding polynomial whose coefficients are in the ambient ring. -/ def of_subring (p : T[X]) : R[X] := ∑ i in p.support, monomial i (p.coeff i : R) lemma coeff_of_subring (p : T[X]) (n : ℕ) : coeff (of_subring T p) n = (coeff p n : T) := begin simp only [of_subring, coeff_monomial, finset_sum_coeff, mem_support_iff, finset.sum_ite_eq', ite_eq_right_iff, ne.def, ite_not, not_not, ite_eq_left_iff], assume h, rw h, refl end @[simp] theorem frange_of_subring {p : T[X]} : (↑(p.of_subring T).frange : set R) ⊆ T := begin assume i hi, simp only [frange, set.mem_image, mem_support_iff, ne.def, finset.mem_coe, finset.coe_image] at hi, rcases hi with ⟨n, hn, h'n⟩, rw [← h'n, coeff_of_subring], exact subtype.mem (coeff p n : T) end end ring section comm_ring variables [comm_ring R] section mod_by_monic variables {q : R[X]} lemma mem_ker_mod_by_monic (hq : q.monic) {p : R[X]} : p ∈ (mod_by_monic_hom q).ker ↔ q ∣ p := linear_map.mem_ker.trans (dvd_iff_mod_by_monic_eq_zero hq) @[simp] lemma ker_mod_by_monic_hom (hq : q.monic) : (polynomial.mod_by_monic_hom q).ker = (ideal.span {q}).restrict_scalars R := submodule.ext (λ f, (mem_ker_mod_by_monic hq).trans ideal.mem_span_singleton.symm) end mod_by_monic end comm_ring end polynomial namespace ideal open polynomial section semiring variables [semiring R] /-- Transport an ideal of `R[X]` to an `R`-submodule of `R[X]`. -/ def of_polynomial (I : ideal R[X]) : submodule R R[X] := { carrier := I.carrier, zero_mem' := I.zero_mem, add_mem' := λ _ _, I.add_mem, smul_mem' := λ c x H, by { rw [← C_mul'], exact I.mul_mem_left _ H } } variables {I : ideal R[X]} theorem mem_of_polynomial (x) : x ∈ I.of_polynomial ↔ x ∈ I := iff.rfl variables (I) /-- Given an ideal `I` of `R[X]`, make the `R`-submodule of `I` consisting of polynomials of degree ≤ `n`. -/ def degree_le (n : with_bot ℕ) : submodule R R[X] := degree_le R n ⊓ I.of_polynomial /-- Given an ideal `I` of `R[X]`, make the ideal in `R` of leading coefficients of polynomials in `I` with degree ≤ `n`. -/ def leading_coeff_nth (n : ℕ) : ideal R := (I.degree_le n).map $ lcoeff R n /-- Given an ideal `I` in `R[X]`, make the ideal in `R` of the leading coefficients in `I`. -/ def leading_coeff : ideal R := ⨆ n : ℕ, I.leading_coeff_nth n end semiring section comm_semiring variables [comm_semiring R] [semiring S] /-- If every coefficient of a polynomial is in an ideal `I`, then so is the polynomial itself -/ lemma polynomial_mem_ideal_of_coeff_mem_ideal (I : ideal R[X]) (p : R[X]) (hp : ∀ (n : ℕ), (p.coeff n) ∈ I.comap (C : R →+* R[X])) : p ∈ I := sum_C_mul_X_eq p ▸ submodule.sum_mem I (λ n hn, I.mul_mem_right _ (hp n)) /-- The push-forward of an ideal `I` of `R` to `polynomial R` via inclusion is exactly the set of polynomials whose coefficients are in `I` -/ theorem mem_map_C_iff {I : ideal R} {f : R[X]} : f ∈ (ideal.map (C : R →+* R[X]) I : ideal R[X]) ↔ ∀ n : ℕ, f.coeff n ∈ I := begin split, { intros hf, apply submodule.span_induction hf, { intros f hf n, cases (set.mem_image _ _ _).mp hf with x hx, rw [← hx.right, coeff_C], by_cases (n = 0), { simpa [h] using hx.left }, { simp [h] } }, { simp }, { exact λ f g hf hg n, by simp [I.add_mem (hf n) (hg n)] }, { refine λ f g hg n, _, rw [smul_eq_mul, coeff_mul], exact I.sum_mem (λ c hc, I.mul_mem_left (f.coeff c.fst) (hg c.snd)) } }, { intros hf, rw ← sum_monomial_eq f, refine (I.map C : ideal R[X]).sum_mem (λ n hn, _), simp [monomial_eq_C_mul_X], rw mul_comm, exact (I.map C : ideal R[X]).mul_mem_left _ (mem_map_of_mem _ (hf n)) } end lemma _root_.polynomial.ker_map_ring_hom (f : R →+* S) : (polynomial.map_ring_hom f).ker = f.ker.map (C : R →+* R[X]) := begin ext, rw [mem_map_C_iff, ring_hom.mem_ker, polynomial.ext_iff], simp_rw [coe_map_ring_hom, coeff_map, coeff_zero, ring_hom.mem_ker], end variable (I : ideal R[X]) theorem mem_leading_coeff_nth (n : ℕ) (x) : x ∈ I.leading_coeff_nth n ↔ ∃ p ∈ I, degree p ≤ n ∧ p.leading_coeff = x := begin simp only [leading_coeff_nth, degree_le, submodule.mem_map, lcoeff_apply, submodule.mem_inf, mem_degree_le], split, { rintro ⟨p, ⟨hpdeg, hpI⟩, rfl⟩, cases lt_or_eq_of_le hpdeg with hpdeg hpdeg, { refine ⟨0, I.zero_mem, bot_le, _⟩, rw [leading_coeff_zero, eq_comm], exact coeff_eq_zero_of_degree_lt hpdeg }, { refine ⟨p, hpI, le_of_eq hpdeg, _⟩, rw [polynomial.leading_coeff, nat_degree, hpdeg], refl } }, { rintro ⟨p, hpI, hpdeg, rfl⟩, have : nat_degree p + (n - nat_degree p) = n, { exact add_tsub_cancel_of_le (nat_degree_le_of_degree_le hpdeg) }, refine ⟨p * X ^ (n - nat_degree p), ⟨_, I.mul_mem_right _ hpI⟩, _⟩, { apply le_trans (degree_mul_le _ _) _, apply le_trans (add_le_add (degree_le_nat_degree) (degree_X_pow_le _)) _, rw [← with_bot.coe_add, this], exact le_rfl }, { rw [polynomial.leading_coeff, ← coeff_mul_X_pow p (n - nat_degree p), this] } } end theorem mem_leading_coeff_nth_zero (x) : x ∈ I.leading_coeff_nth 0 ↔ C x ∈ I := (mem_leading_coeff_nth _ _ _).trans ⟨λ ⟨p, hpI, hpdeg, hpx⟩, by rwa [← hpx, polynomial.leading_coeff, nat.eq_zero_of_le_zero (nat_degree_le_of_degree_le hpdeg), ← eq_C_of_degree_le_zero hpdeg], λ hx, ⟨C x, hx, degree_C_le, leading_coeff_C x⟩⟩ theorem leading_coeff_nth_mono {m n : ℕ} (H : m ≤ n) : I.leading_coeff_nth m ≤ I.leading_coeff_nth n := begin intros r hr, simp only [set_like.mem_coe, mem_leading_coeff_nth] at hr ⊢, rcases hr with ⟨p, hpI, hpdeg, rfl⟩, refine ⟨p * X ^ (n - m), I.mul_mem_right _ hpI, _, leading_coeff_mul_X_pow⟩, refine le_trans (degree_mul_le _ _) _, refine le_trans (add_le_add hpdeg (degree_X_pow_le _)) _, rw [← with_bot.coe_add, add_tsub_cancel_of_le H], exact le_rfl end theorem mem_leading_coeff (x) : x ∈ I.leading_coeff ↔ ∃ p ∈ I, polynomial.leading_coeff p = x := begin rw [leading_coeff, submodule.mem_supr_of_directed], simp only [mem_leading_coeff_nth], { split, { rintro ⟨i, p, hpI, hpdeg, rfl⟩, exact ⟨p, hpI, rfl⟩ }, rintro ⟨p, hpI, rfl⟩, exact ⟨nat_degree p, p, hpI, degree_le_nat_degree, rfl⟩ }, intros i j, exact ⟨i + j, I.leading_coeff_nth_mono (nat.le_add_right _ _), I.leading_coeff_nth_mono (nat.le_add_left _ _)⟩ end /-- If `I` is an ideal, and `pᵢ` is a finite family of polynomials each satisfying `∀ k, (pᵢ)ₖ ∈ Iⁿⁱ⁻ᵏ` for some `nᵢ`, then `p = ∏ pᵢ` also satisfies `∀ k, pₖ ∈ Iⁿ⁻ᵏ` with `n = ∑ nᵢ`. -/ lemma _root_.polynomial.coeff_prod_mem_ideal_pow_tsub {ι : Type*} (s : finset ι) (f : ι → R[X]) (I : ideal R) (n : ι → ℕ) (h : ∀ (i ∈ s) k, (f i).coeff k ∈ I ^ (n i - k)) (k : ℕ) : (s.prod f).coeff k ∈ I ^ (s.sum n - k) := begin classical, induction s using finset.induction with a s ha hs generalizing k, { rw [sum_empty, prod_empty, coeff_one, zero_tsub, pow_zero, ideal.one_eq_top], exact submodule.mem_top }, { rw [sum_insert ha, prod_insert ha, coeff_mul], apply sum_mem, rintro ⟨i, j⟩ e, obtain rfl : i + j = k := nat.mem_antidiagonal.mp e, apply ideal.pow_le_pow add_tsub_add_le_tsub_add_tsub, rw pow_add, exact ideal.mul_mem_mul (h _ (finset.mem_insert.mpr $ or.inl rfl) _) (hs (λ i hi k, h _ (finset.mem_insert.mpr $ or.inr hi) _) j) } end end comm_semiring section ring variables [ring R] /-- `polynomial R` is never a field for any ring `R`. -/ lemma polynomial_not_is_field : ¬ is_field R[X] := begin nontriviality R, intro hR, obtain ⟨p, hp⟩ := hR.mul_inv_cancel X_ne_zero, have hp0 : p ≠ 0, { rintro rfl, rw [mul_zero] at hp, exact zero_ne_one hp }, have := degree_lt_degree_mul_X hp0, rw [←X_mul, congr_arg degree hp, degree_one, nat.with_bot.lt_zero_iff, degree_eq_bot] at this, exact hp0 this, end /-- The only constant in a maximal ideal over a field is `0`. -/ lemma eq_zero_of_constant_mem_of_maximal (hR : is_field R) (I : ideal R[X]) [hI : I.is_maximal] (x : R) (hx : C x ∈ I) : x = 0 := begin refine classical.by_contradiction (λ hx0, hI.ne_top ((eq_top_iff_one I).2 _)), obtain ⟨y, hy⟩ := hR.mul_inv_cancel hx0, convert I.mul_mem_left (C y) hx, rw [← C.map_mul, hR.mul_comm y x, hy, ring_hom.map_one], end end ring section comm_ring variables [comm_ring R] lemma quotient_map_C_eq_zero {I : ideal R} : ∀ a ∈ I, ((quotient.mk (map (C : R →+* R[X]) I : ideal R[X])).comp C) a = 0 := begin intros a ha, rw [ring_hom.comp_apply, quotient.eq_zero_iff_mem], exact mem_map_of_mem _ ha, end lemma eval₂_C_mk_eq_zero {I : ideal R} : ∀ f ∈ (map (C : R →+* R[X]) I : ideal R[X]), eval₂_ring_hom (C.comp (quotient.mk I)) X f = 0 := begin intros a ha, rw ← sum_monomial_eq a, dsimp, rw eval₂_sum, refine finset.sum_eq_zero (λ n hn, _), dsimp, rw eval₂_monomial (C.comp (quotient.mk I)) X, refine mul_eq_zero_of_left (polynomial.ext (λ m, _)) (X ^ n), erw coeff_C, by_cases h : m = 0, { simpa [h] using quotient.eq_zero_iff_mem.2 ((mem_map_C_iff.1 ha) n) }, { simp [h] } end /-- If `I` is an ideal of `R`, then the ring polynomials over the quotient ring `I.quotient` is isomorphic to the quotient of `polynomial R` by the ideal `map C I`, where `map C I` contains exactly the polynomials whose coefficients all lie in `I` -/ def polynomial_quotient_equiv_quotient_polynomial (I : ideal R) : polynomial (R ⧸ I) ≃+* R[X] ⧸ (map C I : ideal R[X]) := { to_fun := eval₂_ring_hom (quotient.lift I ((quotient.mk (map C I : ideal R[X])).comp C) quotient_map_C_eq_zero) ((quotient.mk (map C I : ideal R[X]) X)), inv_fun := quotient.lift (map C I : ideal R[X]) (eval₂_ring_hom (C.comp (quotient.mk I)) X) eval₂_C_mk_eq_zero, map_mul' := λ f g, by simp only [coe_eval₂_ring_hom, eval₂_mul], map_add' := λ f g, by simp only [eval₂_add, coe_eval₂_ring_hom], left_inv := begin intro f, apply polynomial.induction_on' f, { intros p q hp hq, simp only [coe_eval₂_ring_hom] at hp, simp only [coe_eval₂_ring_hom] at hq, simp only [coe_eval₂_ring_hom, hp, hq, ring_hom.map_add] }, { rintros n ⟨x⟩, simp only [monomial_eq_smul_X, C_mul', quotient.lift_mk, submodule.quotient.quot_mk_eq_mk, quotient.mk_eq_mk, eval₂_X_pow, eval₂_smul, coe_eval₂_ring_hom, ring_hom.map_pow, eval₂_C, ring_hom.coe_comp, ring_hom.map_mul, eval₂_X] } end, right_inv := begin rintro ⟨f⟩, apply polynomial.induction_on' f, { simp_intros p q hp hq, rw [hp, hq] }, { intros n a, simp only [monomial_eq_smul_X, ← C_mul' a (X ^ n), quotient.lift_mk, submodule.quotient.quot_mk_eq_mk, quotient.mk_eq_mk, eval₂_X_pow, eval₂_smul, coe_eval₂_ring_hom, ring_hom.map_pow, eval₂_C, ring_hom.coe_comp, ring_hom.map_mul, eval₂_X] }, end, } @[simp] lemma polynomial_quotient_equiv_quotient_polynomial_symm_mk (I : ideal R) (f : R[X]) : I.polynomial_quotient_equiv_quotient_polynomial.symm (quotient.mk _ f) = f.map (quotient.mk I) := by rw [polynomial_quotient_equiv_quotient_polynomial, ring_equiv.symm_mk, ring_equiv.coe_mk, ideal.quotient.lift_mk, coe_eval₂_ring_hom, eval₂_eq_eval_map, ←polynomial.map_map, ←eval₂_eq_eval_map, polynomial.eval₂_C_X] @[simp] lemma polynomial_quotient_equiv_quotient_polynomial_map_mk (I : ideal R) (f : R[X]) : I.polynomial_quotient_equiv_quotient_polynomial (f.map I^.quotient.mk) = quotient.mk _ f := begin apply (polynomial_quotient_equiv_quotient_polynomial I).symm.injective, rw [ring_equiv.symm_apply_apply, polynomial_quotient_equiv_quotient_polynomial_symm_mk], end /-- If `P` is a prime ideal of `R`, then `R[x]/(P)` is an integral domain. -/ lemma is_domain_map_C_quotient {P : ideal R} (H : is_prime P) : is_domain (R[X] ⧸ (map (C : R →+* R[X]) P : ideal R[X])) := ring_equiv.is_domain (polynomial (R ⧸ P)) (polynomial_quotient_equiv_quotient_polynomial P).symm /-- If `P` is a prime ideal of `R`, then `P.R[x]` is a prime ideal of `R[x]`. -/ lemma is_prime_map_C_of_is_prime {P : ideal R} (H : is_prime P) : is_prime (map (C : R →+* R[X]) P : ideal R[X]) := (quotient.is_domain_iff_prime (map C P : ideal R[X])).mp (is_domain_map_C_quotient H) /-- Given any ring `R` and an ideal `I` of `polynomial R`, we get a map `R → R[x] → R[x]/I`. If we let `R` be the image of `R` in `R[x]/I` then we also have a map `R[x] → R'[x]`. In particular we can map `I` across this map, to get `I'` and a new map `R' → R'[x] → R'[x]/I`. This theorem shows `I'` will not contain any non-zero constant polynomials -/ lemma eq_zero_of_polynomial_mem_map_range (I : ideal R[X]) (x : ((quotient.mk I).comp C).range) (hx : C x ∈ (I.map (polynomial.map_ring_hom ((quotient.mk I).comp C).range_restrict))) : x = 0 := begin let i := ((quotient.mk I).comp C).range_restrict, have hi' : (polynomial.map_ring_hom i).ker ≤ I, { refine λ f hf, polynomial_mem_ideal_of_coeff_mem_ideal I f (λ n, _), rw [mem_comap, ← quotient.eq_zero_iff_mem, ← ring_hom.comp_apply], rw [ring_hom.mem_ker, coe_map_ring_hom] at hf, replace hf := congr_arg (λ (f : polynomial _), f.coeff n) hf, simp only [coeff_map, coeff_zero] at hf, rwa [subtype.ext_iff, ring_hom.coe_range_restrict] at hf }, obtain ⟨x, hx'⟩ := x, obtain ⟨y, rfl⟩ := (ring_hom.mem_range).1 hx', refine subtype.eq _, simp only [ring_hom.comp_apply, quotient.eq_zero_iff_mem, add_submonoid_class.coe_zero, subtype.val_eq_coe], suffices : C (i y) ∈ (I.map (polynomial.map_ring_hom i)), { obtain ⟨f, hf⟩ := mem_image_of_mem_map_of_surjective (polynomial.map_ring_hom i) (polynomial.map_surjective _ (((quotient.mk I).comp C).range_restrict_surjective)) this, refine sub_add_cancel (C y) f ▸ I.add_mem (hi' _ : (C y - f) ∈ I) hf.1, rw [ring_hom.mem_ker, ring_hom.map_sub, hf.2, sub_eq_zero, coe_map_ring_hom, map_C] }, exact hx, end theorem is_fg_degree_le [is_noetherian_ring R] (I : ideal R[X]) (n : ℕ) : submodule.fg (I.degree_le n) := is_noetherian_submodule_left.1 (is_noetherian_of_fg_of_noetherian _ ⟨_, degree_le_eq_span_X_pow.symm⟩) _ end comm_ring end ideal variables {σ : Type v} {M : Type w} variables [comm_ring R] [comm_ring S] [add_comm_group M] [module R M] section prime variables (σ) {r : R} namespace polynomial lemma prime_C_iff : prime (C r) ↔ prime r := ⟨ comap_prime C (eval_ring_hom (0 : R)) (λ r, eval_C), λ hr, by { have := hr.1, rw ← ideal.span_singleton_prime at hr ⊢, { convert ideal.is_prime_map_C_of_is_prime hr using 1, rw [ideal.map_span, set.image_singleton] }, exacts [λ h, this (C_eq_zero.1 h), this] } ⟩ end polynomial namespace mv_polynomial private lemma prime_C_iff_of_fintype [fintype σ] : prime (C r : mv_polynomial σ R) ↔ prime r := begin rw (rename_equiv R (fintype.equiv_fin σ)).to_mul_equiv.prime_iff, convert_to prime (C r) ↔ _, { congr, apply rename_C }, { symmetry, induction fintype.card σ with d hd, { exact (is_empty_alg_equiv R (fin 0)).to_mul_equiv.symm.prime_iff }, { rw [hd, ← polynomial.prime_C_iff], convert (fin_succ_equiv R d).to_mul_equiv.symm.prime_iff, rw ← fin_succ_equiv_comp_C_eq_C, refl } }, end lemma prime_C_iff : prime (C r : mv_polynomial σ R) ↔ prime r := ⟨ comap_prime C constant_coeff constant_coeff_C, λ hr, ⟨ λ h, hr.1 $ by { rw [← C_inj, h], simp }, λ h, hr.2.1 $ by { rw ← constant_coeff_C r, exact h.map _ }, λ a b hd, begin obtain ⟨s,a',b',rfl,rfl⟩ := exists_finset_rename₂ a b, rw ← algebra_map_eq at hd, have : algebra_map R _ r ∣ a' * b', { convert (kill_compl subtype.coe_injective).to_ring_hom.map_dvd hd, simpa, simp }, rw ← rename_C (coe : s → σ), let f := (rename (coe : s → σ)).to_ring_hom, exact (((prime_C_iff_of_fintype s).2 hr).2.2 a' b' this).imp f.map_dvd f.map_dvd, end ⟩ ⟩ variable {σ} lemma prime_rename_iff (s : set σ) {p : mv_polynomial s R} : prime (rename (coe : s → σ) p) ↔ prime p := begin classical, symmetry, let eqv := (sum_alg_equiv R _ _).symm.trans (rename_equiv R $ (equiv.sum_comm ↥sᶜ s).trans $ equiv.set.sum_compl s), rw [← prime_C_iff ↥sᶜ, eqv.to_mul_equiv.prime_iff], convert iff.rfl, suffices : (rename coe).to_ring_hom = eqv.to_alg_hom.to_ring_hom.comp C, { apply ring_hom.congr_fun this }, { apply ring_hom_ext, { intro, dsimp [eqv], erw [iter_to_sum_C_C, rename_C, rename_C] }, { intro, dsimp [eqv], erw [iter_to_sum_C_X, rename_X, rename_X], refl } }, end end mv_polynomial end prime namespace polynomial @[priority 100] instance {R : Type*} [comm_ring R] [is_domain R] [wf_dvd_monoid R] : wf_dvd_monoid R[X] := { well_founded_dvd_not_unit := begin classical, refine rel_hom_class.well_founded (⟨λ (p : R[X]), ((if p = 0 then ⊤ else ↑p.degree : with_top (with_bot ℕ)), p.leading_coeff), _⟩ : dvd_not_unit →r prod.lex (<) dvd_not_unit) (prod.lex_wf (with_top.well_founded_lt $ with_bot.well_founded_lt nat.lt_wf) ‹wf_dvd_monoid R›.well_founded_dvd_not_unit), rintros a b ⟨ane0, ⟨c, ⟨not_unit_c, rfl⟩⟩⟩, rw [polynomial.degree_mul, if_neg ane0], split_ifs with hac, { rw [hac, polynomial.leading_coeff_zero], apply prod.lex.left, exact lt_of_le_of_ne le_top with_top.coe_ne_top }, have cne0 : c ≠ 0 := right_ne_zero_of_mul hac, simp only [cne0, ane0, polynomial.leading_coeff_mul], by_cases hdeg : c.degree = 0, { simp only [hdeg, add_zero], refine prod.lex.right _ ⟨_, ⟨c.leading_coeff, (λ unit_c, not_unit_c _), rfl⟩⟩, { rwa [ne, polynomial.leading_coeff_eq_zero] }, rw [polynomial.is_unit_iff, polynomial.eq_C_of_degree_eq_zero hdeg], use [c.leading_coeff, unit_c], rw [polynomial.leading_coeff, polynomial.nat_degree_eq_of_degree_eq_some hdeg] }, { apply prod.lex.left, rw polynomial.degree_eq_nat_degree cne0 at *, rw [with_top.coe_lt_coe, polynomial.degree_eq_nat_degree ane0, ← with_bot.coe_add, with_bot.coe_lt_coe], exact lt_add_of_pos_right _ (nat.pos_of_ne_zero (λ h, hdeg (h.symm ▸ with_bot.coe_zero))) }, end } end polynomial /-- Hilbert basis theorem: a polynomial ring over a noetherian ring is a noetherian ring. -/ protected theorem polynomial.is_noetherian_ring [is_noetherian_ring R] : is_noetherian_ring R[X] := is_noetherian_ring_iff.2 ⟨assume I : ideal R[X], let M := well_founded.min (is_noetherian_iff_well_founded.1 (by apply_instance)) (set.range I.leading_coeff_nth) ⟨_, ⟨0, rfl⟩⟩ in have hm : M ∈ set.range I.leading_coeff_nth := well_founded.min_mem _ _ _, let ⟨N, HN⟩ := hm, ⟨s, hs⟩ := I.is_fg_degree_le N in have hm2 : ∀ k, I.leading_coeff_nth k ≤ M := λ k, or.cases_on (le_or_lt k N) (λ h, HN ▸ I.leading_coeff_nth_mono h) (λ h x hx, classical.by_contradiction $ λ hxm, have ¬M < I.leading_coeff_nth k, by refine well_founded.not_lt_min (well_founded_submodule_gt _ _) _ _ _; exact ⟨k, rfl⟩, this ⟨HN ▸ I.leading_coeff_nth_mono (le_of_lt h), λ H, hxm (H hx)⟩), have hs2 : ∀ {x}, x ∈ I.degree_le N → x ∈ ideal.span (↑s : set R[X]), from hs ▸ λ x hx, submodule.span_induction hx (λ _ hx, ideal.subset_span hx) (ideal.zero_mem _) (λ _ _, ideal.add_mem _) (λ c f hf, f.C_mul' c ▸ ideal.mul_mem_left _ _ hf), ⟨s, le_antisymm (ideal.span_le.2 $ λ x hx, have x ∈ I.degree_le N, from hs ▸ submodule.subset_span hx, this.2) $ begin have : submodule.span R[X] ↑s = ideal.span ↑s, by refl, rw this, intros p hp, generalize hn : p.nat_degree = k, induction k using nat.strong_induction_on with k ih generalizing p, cases le_or_lt k N, { subst k, refine hs2 ⟨polynomial.mem_degree_le.2 (le_trans polynomial.degree_le_nat_degree $ with_bot.coe_le_coe.2 h), hp⟩ }, { have hp0 : p ≠ 0, { rintro rfl, cases hn, exact nat.not_lt_zero _ h }, have : (0 : R) ≠ 1, { intro h, apply hp0, ext i, refine (mul_one _).symm.trans _, rw [← h, mul_zero], refl }, haveI : nontrivial R := ⟨⟨0, 1, this⟩⟩, have : p.leading_coeff ∈ I.leading_coeff_nth N, { rw HN, exact hm2 k ((I.mem_leading_coeff_nth _ _).2 ⟨_, hp, hn ▸ polynomial.degree_le_nat_degree, rfl⟩) }, rw I.mem_leading_coeff_nth at this, rcases this with ⟨q, hq, hdq, hlqp⟩, have hq0 : q ≠ 0, { intro H, rw [← polynomial.leading_coeff_eq_zero] at H, rw [hlqp, polynomial.leading_coeff_eq_zero] at H, exact hp0 H }, have h1 : p.degree = (q * polynomial.X ^ (k - q.nat_degree)).degree, { rw [polynomial.degree_mul', polynomial.degree_X_pow], rw [polynomial.degree_eq_nat_degree hp0, polynomial.degree_eq_nat_degree hq0], rw [← with_bot.coe_add, add_tsub_cancel_of_le, hn], { refine le_trans (polynomial.nat_degree_le_of_degree_le hdq) (le_of_lt h) }, rw [polynomial.leading_coeff_X_pow, mul_one], exact mt polynomial.leading_coeff_eq_zero.1 hq0 }, have h2 : p.leading_coeff = (q * polynomial.X ^ (k - q.nat_degree)).leading_coeff, { rw [← hlqp, polynomial.leading_coeff_mul_X_pow] }, have := polynomial.degree_sub_lt h1 hp0 h2, rw [polynomial.degree_eq_nat_degree hp0] at this, rw ← sub_add_cancel p (q * polynomial.X ^ (k - q.nat_degree)), refine (ideal.span ↑s).add_mem _ ((ideal.span ↑s).mul_mem_right _ _), { by_cases hpq : p - q * polynomial.X ^ (k - q.nat_degree) = 0, { rw hpq, exact ideal.zero_mem _ }, refine ih _ _ (I.sub_mem hp (I.mul_mem_right _ hq)) rfl, rwa [polynomial.degree_eq_nat_degree hpq, with_bot.coe_lt_coe, hn] at this }, exact hs2 ⟨polynomial.mem_degree_le.2 hdq, hq⟩ } end⟩⟩ attribute [instance] polynomial.is_noetherian_ring namespace polynomial theorem exists_irreducible_of_degree_pos {R : Type u} [comm_ring R] [is_domain R] [wf_dvd_monoid R] {f : R[X]} (hf : 0 < f.degree) : ∃ g, irreducible g ∧ g ∣ f := wf_dvd_monoid.exists_irreducible_factor (λ huf, ne_of_gt hf $ degree_eq_zero_of_is_unit huf) (λ hf0, not_lt_of_lt hf $ hf0.symm ▸ (@degree_zero R _).symm ▸ with_bot.bot_lt_coe _) theorem exists_irreducible_of_nat_degree_pos {R : Type u} [comm_ring R] [is_domain R] [wf_dvd_monoid R] {f : R[X]} (hf : 0 < f.nat_degree) : ∃ g, irreducible g ∧ g ∣ f := exists_irreducible_of_degree_pos $ by { contrapose! hf, exact nat_degree_le_of_degree_le hf } theorem exists_irreducible_of_nat_degree_ne_zero {R : Type u} [comm_ring R] [is_domain R] [wf_dvd_monoid R] {f : R[X]} (hf : f.nat_degree ≠ 0) : ∃ g, irreducible g ∧ g ∣ f := exists_irreducible_of_nat_degree_pos $ nat.pos_of_ne_zero hf lemma linear_independent_powers_iff_aeval (f : M →ₗ[R] M) (v : M) : linear_independent R (λ n : ℕ, (f ^ n) v) ↔ ∀ (p : R[X]), aeval f p v = 0 → p = 0 := begin rw linear_independent_iff, simp only [finsupp.total_apply, aeval_endomorphism, forall_iff_forall_finsupp, sum, support, coeff, of_finsupp_eq_zero], exact iff.rfl, end lemma disjoint_ker_aeval_of_coprime (f : M →ₗ[R] M) {p q : R[X]} (hpq : is_coprime p q) : disjoint (aeval f p).ker (aeval f q).ker := begin intros v hv, rcases hpq with ⟨p', q', hpq'⟩, simpa [linear_map.mem_ker.1 (submodule.mem_inf.1 hv).1, linear_map.mem_ker.1 (submodule.mem_inf.1 hv).2] using congr_arg (λ p : R[X], aeval f p v) hpq'.symm, end lemma sup_aeval_range_eq_top_of_coprime (f : M →ₗ[R] M) {p q : R[X]} (hpq : is_coprime p q) : (aeval f p).range ⊔ (aeval f q).range = ⊤ := begin rw eq_top_iff, intros v hv, rw submodule.mem_sup, rcases hpq with ⟨p', q', hpq'⟩, use aeval f (p * p') v, use linear_map.mem_range.2 ⟨aeval f p' v, by simp only [linear_map.mul_apply, aeval_mul]⟩, use aeval f (q * q') v, use linear_map.mem_range.2 ⟨aeval f q' v, by simp only [linear_map.mul_apply, aeval_mul]⟩, simpa only [mul_comm p p', mul_comm q q', aeval_one, aeval_add] using congr_arg (λ p : R[X], aeval f p v) hpq' end lemma sup_ker_aeval_le_ker_aeval_mul {f : M →ₗ[R] M} {p q : R[X]} : (aeval f p).ker ⊔ (aeval f q).ker ≤ (aeval f (p * q)).ker := begin intros v hv, rcases submodule.mem_sup.1 hv with ⟨x, hx, y, hy, hxy⟩, have h_eval_x : aeval f (p * q) x = 0, { rw [mul_comm, aeval_mul, linear_map.mul_apply, linear_map.mem_ker.1 hx, linear_map.map_zero] }, have h_eval_y : aeval f (p * q) y = 0, { rw [aeval_mul, linear_map.mul_apply, linear_map.mem_ker.1 hy, linear_map.map_zero] }, rw [linear_map.mem_ker, ←hxy, linear_map.map_add, h_eval_x, h_eval_y, add_zero], end lemma sup_ker_aeval_eq_ker_aeval_mul_of_coprime (f : M →ₗ[R] M) {p q : R[X]} (hpq : is_coprime p q) : (aeval f p).ker ⊔ (aeval f q).ker = (aeval f (p * q)).ker := begin apply le_antisymm sup_ker_aeval_le_ker_aeval_mul, intros v hv, rw submodule.mem_sup, rcases hpq with ⟨p', q', hpq'⟩, have h_eval₂_qpp' := calc aeval f (q * (p * p')) v = aeval f (p' * (p * q)) v : by rw [mul_comm, mul_assoc, mul_comm, mul_assoc, mul_comm q p] ... = 0 : by rw [aeval_mul, linear_map.mul_apply, linear_map.mem_ker.1 hv, linear_map.map_zero], have h_eval₂_pqq' := calc aeval f (p * (q * q')) v = aeval f (q' * (p * q)) v : by rw [←mul_assoc, mul_comm] ... = 0 : by rw [aeval_mul, linear_map.mul_apply, linear_map.mem_ker.1 hv, linear_map.map_zero], rw aeval_mul at h_eval₂_qpp' h_eval₂_pqq', refine ⟨aeval f (q * q') v, linear_map.mem_ker.1 h_eval₂_pqq', aeval f (p * p') v, linear_map.mem_ker.1 h_eval₂_qpp', _⟩, rw [add_comm, mul_comm p p', mul_comm q q'], simpa using congr_arg (λ p : R[X], aeval f p v) hpq' end end polynomial namespace mv_polynomial lemma is_noetherian_ring_fin_0 [is_noetherian_ring R] : is_noetherian_ring (mv_polynomial (fin 0) R) := is_noetherian_ring_of_ring_equiv R ((mv_polynomial.is_empty_ring_equiv R pempty).symm.trans (rename_equiv R fin_zero_equiv'.symm).to_ring_equiv) theorem is_noetherian_ring_fin [is_noetherian_ring R] : ∀ {n : ℕ}, is_noetherian_ring (mv_polynomial (fin n) R) | 0 := is_noetherian_ring_fin_0 | (n+1) := @is_noetherian_ring_of_ring_equiv (polynomial (mv_polynomial (fin n) R)) _ _ _ (mv_polynomial.fin_succ_equiv _ n).to_ring_equiv.symm (@polynomial.is_noetherian_ring (mv_polynomial (fin n) R) _ (is_noetherian_ring_fin)) /-- The multivariate polynomial ring in finitely many variables over a noetherian ring is itself a noetherian ring. -/ instance is_noetherian_ring [fintype σ] [is_noetherian_ring R] : is_noetherian_ring (mv_polynomial σ R) := @is_noetherian_ring_of_ring_equiv (mv_polynomial (fin (fintype.card σ)) R) _ _ _ (rename_equiv R (fintype.equiv_fin σ).symm).to_ring_equiv is_noetherian_ring_fin lemma is_domain_fin_zero (R : Type u) [comm_ring R] [is_domain R] : is_domain (mv_polynomial (fin 0) R) := ring_equiv.is_domain R ((rename_equiv R fin_zero_equiv').to_ring_equiv.trans (mv_polynomial.is_empty_ring_equiv R pempty)) /-- Auxiliary lemma: Multivariate polynomials over an integral domain with variables indexed by `fin n` form an integral domain. This fact is proven inductively, and then used to prove the general case without any finiteness hypotheses. See `mv_polynomial.is_domain` for the general case. -/ lemma is_domain_fin (R : Type u) [comm_ring R] [is_domain R] : ∀ (n : ℕ), is_domain (mv_polynomial (fin n) R) | 0 := is_domain_fin_zero R | (n+1) := begin haveI := is_domain_fin n, exact ring_equiv.is_domain (polynomial (mv_polynomial (fin n) R)) (mv_polynomial.fin_succ_equiv _ n).to_ring_equiv end /-- Auxiliary definition: Multivariate polynomials in finitely many variables over an integral domain form an integral domain. This fact is proven by transport of structure from the `mv_polynomial.is_domain_fin`, and then used to prove the general case without finiteness hypotheses. See `mv_polynomial.is_domain` for the general case. -/ lemma is_domain_fintype (R : Type u) (σ : Type v) [comm_ring R] [fintype σ] [is_domain R] : is_domain (mv_polynomial σ R) := @ring_equiv.is_domain _ (mv_polynomial (fin $ fintype.card σ) R) _ _ (mv_polynomial.is_domain_fin _ _) (rename_equiv R (fintype.equiv_fin σ)).to_ring_equiv protected theorem eq_zero_or_eq_zero_of_mul_eq_zero {R : Type u} [comm_ring R] [is_domain R] {σ : Type v} (p q : mv_polynomial σ R) (h : p * q = 0) : p = 0 ∨ q = 0 := begin obtain ⟨s, p, rfl⟩ := exists_finset_rename p, obtain ⟨t, q, rfl⟩ := exists_finset_rename q, have : rename (subtype.map id (finset.subset_union_left s t) : {x // x ∈ s} → {x // x ∈ s ∪ t}) p * rename (subtype.map id (finset.subset_union_right s t) : {x // x ∈ t} → {x // x ∈ s ∪ t}) q = 0, { apply rename_injective _ subtype.val_injective, simpa using h }, letI := mv_polynomial.is_domain_fintype R {x // x ∈ (s ∪ t)}, rw mul_eq_zero at this, cases this; [left, right], all_goals { simpa using congr_arg (rename subtype.val) this } end /-- The multivariate polynomial ring over an integral domain is an integral domain. -/ instance {R : Type u} {σ : Type v} [comm_ring R] [is_domain R] : is_domain (mv_polynomial σ R) := { eq_zero_or_eq_zero_of_mul_eq_zero := mv_polynomial.eq_zero_or_eq_zero_of_mul_eq_zero, exists_pair_ne := ⟨0, 1, λ H, begin have : eval₂ (ring_hom.id _) (λ s, (0:R)) (0 : mv_polynomial σ R) = eval₂ (ring_hom.id _) (λ s, (0:R)) (1 : mv_polynomial σ R), { congr, exact H }, simpa, end⟩, .. (by apply_instance : comm_ring (mv_polynomial σ R)) } lemma map_mv_polynomial_eq_eval₂ {S : Type*} [comm_ring S] [fintype σ] (ϕ : mv_polynomial σ R →+* S) (p : mv_polynomial σ R) : ϕ p = mv_polynomial.eval₂ (ϕ.comp mv_polynomial.C) (λ s, ϕ (mv_polynomial.X s)) p := begin refine trans (congr_arg ϕ (mv_polynomial.as_sum p)) _, rw [mv_polynomial.eval₂_eq', ϕ.map_sum], congr, ext, simp only [monomial_eq, ϕ.map_pow, ϕ.map_prod, ϕ.comp_apply, ϕ.map_mul, finsupp.prod_pow], end lemma quotient_map_C_eq_zero {I : ideal R} {i : R} (hi : i ∈ I) : (ideal.quotient.mk (ideal.map (C : R →+* mv_polynomial σ R) I : ideal (mv_polynomial σ R))).comp C i = 0 := begin simp only [function.comp_app, ring_hom.coe_comp, ideal.quotient.eq_zero_iff_mem], exact ideal.mem_map_of_mem _ hi end /-- If every coefficient of a polynomial is in an ideal `I`, then so is the polynomial itself, multivariate version. -/ lemma mem_ideal_of_coeff_mem_ideal (I : ideal (mv_polynomial σ R)) (p : mv_polynomial σ R) (hcoe : ∀ (m : σ →₀ ℕ), p.coeff m ∈ I.comap (C : R →+* mv_polynomial σ R)) : p ∈ I := begin rw as_sum p, suffices : ∀ m ∈ p.support, monomial m (mv_polynomial.coeff m p) ∈ I, { exact submodule.sum_mem I this }, intros m hm, rw [← mul_one (coeff m p), ← C_mul_monomial], suffices : C (coeff m p) ∈ I, { exact I.mul_mem_right (monomial m 1) this }, simpa [ideal.mem_comap] using hcoe m end /-- The push-forward of an ideal `I` of `R` to `mv_polynomial σ R` via inclusion is exactly the set of polynomials whose coefficients are in `I` -/ theorem mem_map_C_iff {I : ideal R} {f : mv_polynomial σ R} : f ∈ (ideal.map (C : R →+* mv_polynomial σ R) I : ideal (mv_polynomial σ R)) ↔ ∀ (m : σ →₀ ℕ), f.coeff m ∈ I := begin split, { intros hf, apply submodule.span_induction hf, { intros f hf n, cases (set.mem_image _ _ _).mp hf with x hx, rw [← hx.right, coeff_C], by_cases (n = 0), { simpa [h] using hx.left }, { simp [ne.symm h] } }, { simp }, { exact λ f g hf hg n, by simp [I.add_mem (hf n) (hg n)] }, { refine λ f g hg n, _, rw [smul_eq_mul, coeff_mul], exact I.sum_mem (λ c hc, I.mul_mem_left (f.coeff c.fst) (hg c.snd)) } }, { intros hf, rw as_sum f, suffices : ∀ m ∈ f.support, monomial m (coeff m f) ∈ (ideal.map C I : ideal (mv_polynomial σ R)), { exact submodule.sum_mem _ this }, intros m hm, rw [← mul_one (coeff m f), ← C_mul_monomial], suffices : C (coeff m f) ∈ (ideal.map C I : ideal (mv_polynomial σ R)), { exact ideal.mul_mem_right _ _ this }, apply ideal.mem_map_of_mem _, exact hf m } end lemma ker_map (f : R →+* S) : (map f : mv_polynomial σ R →+* mv_polynomial σ S).ker = f.ker.map (C : R →+* mv_polynomial σ R) := begin ext, rw [mv_polynomial.mem_map_C_iff, ring_hom.mem_ker, mv_polynomial.ext_iff], simp_rw [coeff_map, coeff_zero, ring_hom.mem_ker], end lemma eval₂_C_mk_eq_zero {I : ideal R} {a : mv_polynomial σ R} (ha : a ∈ (ideal.map (C : R →+* mv_polynomial σ R) I : ideal (mv_polynomial σ R))) : eval₂_hom (C.comp (ideal.quotient.mk I)) X a = 0 := begin rw as_sum a, rw [coe_eval₂_hom, eval₂_sum], refine finset.sum_eq_zero (λ n hn, _), simp only [eval₂_monomial, function.comp_app, ring_hom.coe_comp], refine mul_eq_zero_of_left _ _, suffices : coeff n a ∈ I, { rw [← @ideal.mk_ker R _ I, ring_hom.mem_ker] at this, simp only [this, C_0] }, exact mem_map_C_iff.1 ha n end /-- If `I` is an ideal of `R`, then the ring `mv_polynomial σ I.quotient` is isomorphic as an `R`-algebra to the quotient of `mv_polynomial σ R` by the ideal generated by `I`. -/ def quotient_equiv_quotient_mv_polynomial (I : ideal R) : mv_polynomial σ (R ⧸ I) ≃ₐ[R] mv_polynomial σ R ⧸ (ideal.map C I : ideal (mv_polynomial σ R)) := { to_fun := eval₂_hom (ideal.quotient.lift I ((ideal.quotient.mk (ideal.map C I : ideal (mv_polynomial σ R))).comp C) (λ i hi, quotient_map_C_eq_zero hi)) (λ i, ideal.quotient.mk (ideal.map C I : ideal (mv_polynomial σ R)) (X i)), inv_fun := ideal.quotient.lift (ideal.map C I : ideal (mv_polynomial σ R)) (eval₂_hom (C.comp (ideal.quotient.mk I)) X) (λ a ha, eval₂_C_mk_eq_zero ha), map_mul' := ring_hom.map_mul _, map_add' := ring_hom.map_add _, left_inv := begin intro f, apply induction_on f, { rintro ⟨r⟩, rw [coe_eval₂_hom, eval₂_C], simp only [eval₂_hom_eq_bind₂, submodule.quotient.quot_mk_eq_mk, ideal.quotient.lift_mk, ideal.quotient.mk_eq_mk, bind₂_C_right, ring_hom.coe_comp] }, { simp_intros p q hp hq only [ring_hom.map_add, mv_polynomial.coe_eval₂_hom, coe_eval₂_hom, mv_polynomial.eval₂_add, mv_polynomial.eval₂_hom_eq_bind₂, eval₂_hom_eq_bind₂], rw [hp, hq] }, { simp_intros p i hp only [eval₂_hom_eq_bind₂, coe_eval₂_hom], simp only [hp, eval₂_hom_eq_bind₂, coe_eval₂_hom, ideal.quotient.lift_mk, bind₂_X_right, eval₂_mul, ring_hom.map_mul, eval₂_X] } end, right_inv := begin rintro ⟨f⟩, apply induction_on f, { intros r, simp only [submodule.quotient.quot_mk_eq_mk, ideal.quotient.lift_mk, ideal.quotient.mk_eq_mk, ring_hom.coe_comp, eval₂_hom_C] }, { simp_intros p q hp hq only [eval₂_hom_eq_bind₂, submodule.quotient.quot_mk_eq_mk, eval₂_add, ring_hom.map_add, coe_eval₂_hom, ideal.quotient.lift_mk, ideal.quotient.mk_eq_mk], rw [hp, hq] }, { simp_intros p i hp only [eval₂_hom_eq_bind₂, submodule.quotient.quot_mk_eq_mk, coe_eval₂_hom, ideal.quotient.lift_mk, ideal.quotient.mk_eq_mk, bind₂_X_right, eval₂_mul, ring_hom.map_mul, eval₂_X], simp only [hp] } end, commutes' := λ r, eval₂_hom_C _ _ (ideal.quotient.mk I r) } end mv_polynomial section unique_factorization_domain variables {D : Type u} [comm_ring D] [is_domain D] [unique_factorization_monoid D] (σ) open unique_factorization_monoid namespace polynomial @[priority 100] instance unique_factorization_monoid : unique_factorization_monoid (polynomial D) := begin haveI := arbitrary (normalization_monoid D), haveI := to_normalized_gcd_monoid D, exact ufm_of_gcd_of_wf_dvd_monoid end end polynomial namespace mv_polynomial private lemma unique_factorization_monoid_of_fintype [fintype σ] : unique_factorization_monoid (mv_polynomial σ D) := (rename_equiv D (fintype.equiv_fin σ)).to_mul_equiv.symm.unique_factorization_monoid $ begin induction fintype.card σ with d hd, { apply (is_empty_alg_equiv D (fin 0)).to_mul_equiv.symm.unique_factorization_monoid, apply_instance }, { apply (fin_succ_equiv D d).to_mul_equiv.symm.unique_factorization_monoid, exactI polynomial.unique_factorization_monoid }, end @[priority 100] instance : unique_factorization_monoid (mv_polynomial σ D) := begin rw iff_exists_prime_factors, intros a ha, obtain ⟨s,a',rfl⟩ := exists_finset_rename a, obtain ⟨w,h,u,hw⟩ := iff_exists_prime_factors.1 (unique_factorization_monoid_of_fintype s) a' (λ h, ha $ by simp [h]), exact ⟨ w.map (rename coe), λ b hb, let ⟨b',hb',he⟩ := multiset.mem_map.1 hb in he ▸ (prime_rename_iff ↑s).2 (h b' hb'), units.map (@rename s σ D _ coe).to_ring_hom.to_monoid_hom u, by erw [multiset.prod_hom, ← map_mul, hw] ⟩, end end mv_polynomial end unique_factorization_domain
fe1c598359986ec78d3abcb0920800e534e3008d
2a70b774d16dbdf5a533432ee0ebab6838df0948
/_target/deps/mathlib/src/ring_theory/polynomial/cyclotomic.lean
8378d4d011563789eeae601414cfe3e374fe62d4
[ "Apache-2.0" ]
permissive
hjvromen/lewis
40b035973df7c77ebf927afab7878c76d05ff758
105b675f73630f028ad5d890897a51b3c1146fb0
refs/heads/master
1,677,944,636,343
1,676,555,301,000
1,676,555,301,000
327,553,599
0
0
null
null
null
null
UTF-8
Lean
false
false
30,971
lean
/- Copyright (c) 2020 Riccardo Brasca. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Riccardo Brasca -/ import field_theory.splitting_field import ring_theory.roots_of_unity import algebra.polynomial.big_operators import number_theory.arithmetic_function import data.polynomial.lifts import analysis.complex.roots_of_unity import field_theory.separable /-! # Cyclotomic polynomials. For `n : ℕ` and an integral domain `R`, we define a modified version of the `n`-th cyclotomic polynomial with coefficients in `R`, denoted `cyclotomic' n R`, as `∏ (X - μ)`, where `μ` varies over the primitive `n`th roots of unity. If there is a primitive `n`th root of unity in `R` then this the standard definition. We then define the standard cyclotomic polynomial `cyclotomic n R` with coefficients in any ring `R`. ## Main definition * `cyclotomic n R` : the `n`-th cyclotomic polynomial with coefficients in `R`. ## Main results * `int_coeff_of_cycl` : If there is a primitive `n`-th root of unity in `K`, then `cyclotomic' n K` comes from a polynomial with integer coefficients. * `deg_of_cyclotomic` : The degree of `cyclotomic n` is `totient n`. * `prod_cyclotomic_eq_X_pow_sub_one` : `X ^ n - 1 = ∏ (cyclotomic i)`, where `i` divides `n`. * `cyclotomic_eq_prod_X_pow_sub_one_pow_moebius` : The Möbius inversion formula for `cyclotomic n R` over an abstract fraction field for `polynomial R`. * `cyclotomic.irreducible` : `cyclotomic n ℤ` is irreducible. ## Implementation details Our definition of `cyclotomic' n R` makes sense in any integral domain `R`, but the interesting results hold if there is a primitive `n`-th root of unity in `R`. In particular, our definition is not the standard one unless there is a primitive `n`th root of unity in `R`. For example, `cyclotomic' 3 ℤ = 1`, since there are no primitive cube roots of unity in `ℤ`. The main example is `R = ℂ`, we decided to work in general since the difficulties are essentially the same. To get the standard cyclotomic polynomials, we use `int_coeff_of_cycl`, with `R = ℂ`, to get a polynomial with integer coefficients and then we map it to `polynomial R`, for any ring `R`. To prove `cyclotomic.irreducible`, the irreducibility of `cyclotomic n ℤ`, we show in `minimal_polynomial_primitive_root_eq_cyclotomic` that `cyclotomic n ℤ` is the minimal polynomial of any `n`-th primitive root of unity `μ : K`, where `K` is a field of characteristic `0`. -/ open_locale classical big_operators noncomputable theory universe u namespace polynomial section cyclotomic' section integral_domain variables {R : Type*} [integral_domain R] /-- The modified `n`-th cyclotomic polynomial with coefficients in `R`, it is the usual cyclotomic polynomial if there is a primitive `n`-th root of unity in `R`. -/ def cyclotomic' (n : ℕ) (R : Type*) [integral_domain R] : polynomial R := ∏ μ in primitive_roots n R, (X - C μ) /-- The zeroth modified cyclotomic polyomial is `1`. -/ @[simp] lemma cyclotomic'_zero (R : Type*) [integral_domain R] : cyclotomic' 0 R = 1 := by simp only [cyclotomic', finset.prod_empty, is_primitive_root.primitive_roots_zero] /-- The first modified cyclotomic polyomial is `X - 1`. -/ @[simp] lemma cyclotomic'_one (R : Type*) [integral_domain R] : cyclotomic' 1 R = X - 1 := begin simp only [cyclotomic', finset.prod_singleton, ring_hom.map_one, is_primitive_root.primitive_roots_one] end /-- The second modified cyclotomic polyomial is `X + 1` if the characteristic of `R` is not `2`. -/ @[simp] lemma cyclotomic'_two (R : Type*) [integral_domain R] (p : ℕ) [char_p R p] (hp : p ≠ 2) : cyclotomic' 2 R = X + 1 := begin rw [cyclotomic'], have prim_root_two : primitive_roots 2 R = {(-1 : R)}, { apply finset.eq_singleton_iff_unique_mem.2, split, { simp only [is_primitive_root.neg_one p hp, nat.succ_pos', mem_primitive_roots] }, { intros x hx, rw [mem_primitive_roots zero_lt_two] at hx, exact is_primitive_root.eq_neg_one_of_two_right hx } }, simp only [prim_root_two, finset.prod_singleton, ring_hom.map_neg, ring_hom.map_one, sub_neg_eq_add] end /-- `cyclotomic' n R` is monic. -/ lemma cyclotomic'.monic (n : ℕ) (R : Type*) [integral_domain R] : (cyclotomic' n R).monic := monic_prod_of_monic _ _ $ λ z hz, monic_X_sub_C _ /-- `cyclotomic' n R` is different from `0`. -/ lemma cyclotomic'_ne_zero (n : ℕ) (R : Type*) [integral_domain R] : cyclotomic' n R ≠ 0 := (cyclotomic'.monic n R).ne_zero /-- The natural degree of `cyclotomic' n R` is `totient n` if there is a primitive root of unity in `R`. -/ lemma nat_degree_cyclotomic' {ζ : R} {n : ℕ} (h : is_primitive_root ζ n) : (cyclotomic' n R).nat_degree = nat.totient n := begin cases nat.eq_zero_or_pos n with hzero hpos, { simp only [hzero, cyclotomic'_zero, nat.totient_zero, nat_degree_one] }, rw [cyclotomic'], rw nat_degree_prod (primitive_roots n R) (λ (z : R), (X - C z)), simp only [is_primitive_root.card_primitive_roots h hpos, mul_one, nat_degree_X_sub_C, nat.cast_id, finset.sum_const, nsmul_eq_mul], intros z hz, exact X_sub_C_ne_zero z end /-- The degree of `cyclotomic' n R` is `totient n` if there is a primitive root of unity in `R`. -/ lemma degree_cyclotomic' {ζ : R} {n : ℕ} (h : is_primitive_root ζ n) : (cyclotomic' n R).degree = nat.totient n := by simp only [degree_eq_nat_degree (cyclotomic'_ne_zero n R), nat_degree_cyclotomic' h] /-- The roots of `cyclotomic' n R` are the primitive `n`-th roots of unity. -/ lemma roots_of_cyclotomic (n : ℕ) (R : Type*) [integral_domain R] : (cyclotomic' n R).roots = (primitive_roots n R).val := by { rw cyclotomic', exact roots_prod_X_sub_C (primitive_roots n R) } end integral_domain section field variables {K : Type*} [field K] /-- If there is a primitive `n`th root of unity in `K`, then `X ^ n - 1 = ∏ (X - μ)`, where `μ` varies over the `n`-th roots of unity. -/ lemma X_pow_sub_one_eq_prod {ζ : K} {n : ℕ} (hpos : 0 < n) (h : is_primitive_root ζ n) : X ^ n - 1 = ∏ ζ in nth_roots_finset n K, (X - C ζ) := begin rw [nth_roots_finset, ← multiset.to_finset_eq (is_primitive_root.nth_roots_nodup h)], simp only [finset.prod_mk, ring_hom.map_one], rw [nth_roots], have hmonic : (X ^ n - C (1 : K)).monic := monic_X_pow_sub_C (1 : K) (ne_of_lt hpos).symm, symmetry, apply prod_multiset_X_sub_C_of_monic_of_roots_card_eq hmonic, rw [@nat_degree_X_pow_sub_C K _ _ n hpos 1, ← nth_roots], exact is_primitive_root.card_nth_roots h end /-- `cyclotomic' n K` splits. -/ lemma cyclotomic'_splits (n : ℕ) : splits (ring_hom.id K) (cyclotomic' n K) := begin apply splits_prod (ring_hom.id K), intros z hz, simp only [splits_X_sub_C (ring_hom.id K)] end /-- If there is a primitive `n`-th root of unity in `K`, then `X ^ n - 1`splits. -/ lemma X_pow_sub_one_splits {ζ : K} {n : ℕ} (h : is_primitive_root ζ n) : splits (ring_hom.id K) (X ^ n - C (1 : K)) := begin by_cases hzero : n = 0, { simp only [hzero, ring_hom.map_one, splits_zero, pow_zero, sub_self] }, rw [splits_iff_card_roots, ← nth_roots, is_primitive_root.card_nth_roots h, nat_degree_X_pow_sub_C], exact nat.pos_of_ne_zero hzero end /-- If there is a primitive `n`-th root of unity in `K`, then `∏ i in nat.divisors n, cyclotomic' i K = X ^ n - 1`. -/ lemma prod_cyclotomic'_eq_X_pow_sub_one {ζ : K} {n : ℕ} (hpos : 0 < n) (h : is_primitive_root ζ n) : ∏ i in nat.divisors n, cyclotomic' i K = X ^ n - 1 := begin rw [X_pow_sub_one_eq_prod hpos h], have rwcyc : ∀ i ∈ nat.divisors n, cyclotomic' i K = ∏ μ in primitive_roots i K, (X - C μ), { intros i hi, simp only [cyclotomic'] }, conv_lhs { apply_congr, skip, simp [rwcyc, H] }, rw ← finset.prod_bind, { simp only [is_primitive_root.nth_roots_one_eq_bind_primitive_roots hpos h] }, intros x hx y hy hdiff, simp only [nat.mem_divisors, and_true, ne.def, pnat.ne_zero, not_false_iff] at hx hy, refine is_primitive_root.disjoint _ _ hdiff, { exact @nat.pos_of_mem_divisors n x (nat.mem_divisors.2 hx) }, { exact @nat.pos_of_mem_divisors n y (nat.mem_divisors.2 hy) } end /-- If there is a primitive `n`-th root of unity in `K`, then `cyclotomic' n K = (X ^ k - 1) /ₘ (∏ i in nat.proper_divisors k, cyclotomic' i K)`. -/ lemma cyclotomic'_eq_X_pow_sub_one_div {ζ : K} {n : ℕ} (hpos: 0 < n) (h : is_primitive_root ζ n) : cyclotomic' n K = (X ^ n - 1) /ₘ (∏ i in nat.proper_divisors n, cyclotomic' i K) := begin rw [←prod_cyclotomic'_eq_X_pow_sub_one hpos h, nat.divisors_eq_proper_divisors_insert_self_of_pos hpos, finset.prod_insert nat.proper_divisors.not_self_mem], have prod_monic : (∏ i in nat.proper_divisors n, cyclotomic' i K).monic, { apply monic_prod_of_monic, intros i hi, exact cyclotomic'.monic i K }, rw (div_mod_by_monic_unique (cyclotomic' n K) 0 prod_monic _).1, simp only [degree_zero, zero_add], split, { rw mul_comm }, rw [bot_lt_iff_ne_bot], intro h, exact monic.ne_zero prod_monic (degree_eq_bot.1 h) end /-- If there is a primitive `n`-th root of unity in `K`, then `cyclotomic' n K` comes from a polynomial with integer coefficients. -/ lemma int_coeff_of_cyclotomic {ζ : K} {n : ℕ} (h : is_primitive_root ζ n) : (∃ (P : polynomial ℤ), map (int.cast_ring_hom K) P = cyclotomic' n K ∧ P.degree = (cyclotomic' n K).degree ∧ P.monic) := begin refine lifts_and_degree_eq_and_monic _ (cyclotomic'.monic n K), revert h ζ, apply nat.strong_induction_on n, intros k hk z hzeta, cases nat.eq_zero_or_pos k with hzero hpos, { use 1, simp only [hzero, cyclotomic'_zero, set.mem_univ, subsemiring.coe_top, eq_self_iff_true, map_one, ring_hom.coe_of, and_self], }, by_cases hone : k = 1, { use X - 1, simp only [hone, cyclotomic'_one K, set.mem_univ, pnat.one_coe, subsemiring.coe_top, eq_self_iff_true, map_X, map_one, ring_hom.coe_of, and_self, map_sub] }, let B : polynomial K := ∏ i in nat.proper_divisors k, cyclotomic' i K, have Bmo : B.monic, { apply monic_prod_of_monic, intros i hi, exact (cyclotomic'.monic i K) }, have Bint : B ∈ lifts (int.cast_ring_hom K), { refine subsemiring.prod_mem (lifts (int.cast_ring_hom K)) _, intros x hx, have xsmall := (nat.mem_proper_divisors.1 hx).2, obtain ⟨d, hd⟩ := (nat.mem_proper_divisors.1 hx).1, rw [mul_comm] at hd, exact hk x xsmall (is_primitive_root.pow hpos hzeta hd) }, replace Bint := lifts_and_degree_eq_and_monic Bint Bmo, obtain ⟨B₁, hB₁, hB₁deg, hB₁mo⟩ := Bint, let Q₁ : polynomial ℤ := (X ^ k - 1) /ₘ B₁, have huniq : 0 + B * cyclotomic' k K = X ^ k - 1 ∧ (0 : polynomial K).degree < B.degree, { split, { rw [zero_add, mul_comm, ←(prod_cyclotomic'_eq_X_pow_sub_one hpos hzeta), nat.divisors_eq_proper_divisors_insert_self_of_pos hpos], simp only [true_and, finset.prod_insert, not_lt, nat.mem_proper_divisors, dvd_refl] }, rw [degree_zero, bot_lt_iff_ne_bot], intro habs, exact (monic.ne_zero Bmo) (degree_eq_bot.1 habs) }, replace huniq := div_mod_by_monic_unique (cyclotomic' k K) (0 : polynomial K) Bmo huniq, simp only [lifts, ring_hom.coe_of, ring_hom.mem_srange], use Q₁, rw [(map_div_by_monic (int.cast_ring_hom K) hB₁mo), hB₁, ← huniq.1], simp end /-- If `K` is of characteristic `0` and there is a primitive `n`-th root of unity in `K`, then `cyclotomic n K` comes from a unique polynomial with integer coefficients. -/ lemma unique_int_coeff_of_cycl [char_zero K] {ζ : K} {n : ℕ+} (h : is_primitive_root ζ n) : (∃! (P : polynomial ℤ), map (int.cast_ring_hom K) P = cyclotomic' n K) := begin obtain ⟨P, hP⟩ := int_coeff_of_cyclotomic h, rw exists_unique, use [P, hP.1], intros Q hQ, have mapinj : function.injective (map (int.cast_ring_hom K)), { apply map_injective, simp only [int.cast_injective, int.coe_cast_ring_hom] }, rw [function.injective] at mapinj, apply mapinj, rw [hP.1, hQ] end end field end cyclotomic' section cyclotomic /-- The `n`-th cyclotomic polynomial with coefficients in `R`. -/ def cyclotomic (n : ℕ) (R : Type*) [ring R] : polynomial R := if h : n = 0 then 1 else map (int.cast_ring_hom R) (classical.some (int_coeff_of_cyclotomic (complex.is_primitive_root_exp n h))) lemma int_cyclotomic_rw {n : ℕ} (h : n ≠ 0) : cyclotomic n ℤ = (classical.some (int_coeff_of_cyclotomic (complex.is_primitive_root_exp n h))) := begin simp only [cyclotomic, h, dif_neg, not_false_iff], ext i, simp only [coeff_map, int.cast_id, ring_hom.eq_int_cast] end /-- `cyclotomic n R` comes from `cyclotomic n ℤ`. -/ lemma map_cyclotomic_int (n : ℕ) (R : Type*) [ring R] : map (int.cast_ring_hom R) (cyclotomic n ℤ) = cyclotomic n R := begin by_cases hzero : n = 0, { simp only [hzero, cyclotomic, dif_pos, map_one] }, simp only [cyclotomic, int_cyclotomic_rw, hzero, ne.def, dif_neg, not_false_iff] end lemma int_cyclotomic_spec (n : ℕ) : map (int.cast_ring_hom ℂ) (cyclotomic n ℤ) = cyclotomic' n ℂ ∧ (cyclotomic n ℤ).degree = (cyclotomic' n ℂ).degree ∧ (cyclotomic n ℤ).monic := begin by_cases hzero : n = 0, { simp only [hzero, cyclotomic, degree_one, monic_one, cyclotomic'_zero, dif_pos, eq_self_iff_true, map_one, and_self] }, rw int_cyclotomic_rw hzero, exact classical.some_spec (int_coeff_of_cyclotomic (complex.is_primitive_root_exp n hzero)) end lemma int_cyclotomic_unique {n : ℕ} {P : polynomial ℤ} (h : map (int.cast_ring_hom ℂ) P = cyclotomic' n ℂ) : P = cyclotomic n ℤ := begin have mapinj : function.injective (map (int.cast_ring_hom ℂ)), { apply map_injective, simp only [int.cast_injective, int.coe_cast_ring_hom] }, apply mapinj, rw [h, (int_cyclotomic_spec n).1] end /-- The definition of `cyclotomic n R` commutes with any ring homomorphism. -/ @[simp] lemma map_cyclotomic (n : ℕ) {R S : Type*} [ring R] [ring S] (f : R →+* S) : map f (cyclotomic n R) = cyclotomic n S := begin rw [←map_cyclotomic_int n R, ←map_cyclotomic_int n S], ext i, simp only [coeff_map, ring_hom.eq_int_cast, ring_hom.map_int_cast] end /-- The zeroth cyclotomic polyomial is `1`. -/ @[simp] lemma cyclotomic_zero (R : Type*) [ring R] : cyclotomic 0 R = 1 := by simp only [cyclotomic, dif_pos] /-- The first cyclotomic polyomial is `X - 1`. -/ @[simp] lemma cyclotomic_one (R : Type*) [ring R] : cyclotomic 1 R = X - 1 := begin have hspec : map (int.cast_ring_hom ℂ) (X - 1) = cyclotomic' 1 ℂ, { simp only [cyclotomic'_one, pnat.one_coe, map_X, map_one, map_sub] }, symmetry, rw [←map_cyclotomic_int, ←(int_cyclotomic_unique hspec)], simp only [map_X, map_one, map_sub] end /-- The second cyclotomic polyomial is `X + 1`. -/ @[simp] lemma cyclotomic_two (R : Type*) [ring R] : cyclotomic 2 R = X + 1 := begin have hspec : map (int.cast_ring_hom ℂ) (X + 1) = cyclotomic' 2 ℂ, { simp only [cyclotomic'_two ℂ 0 two_ne_zero.symm, map_add, map_X, map_one] }, symmetry, rw [←map_cyclotomic_int, ←(int_cyclotomic_unique hspec)], simp only [map_add, map_X, map_one] end /-- `cyclotomic n` is monic. -/ lemma cyclotomic.monic (n : ℕ) (R : Type*) [ring R] : (cyclotomic n R).monic := begin rw ←map_cyclotomic_int, apply monic_map, exact (int_cyclotomic_spec n).2.2 end /-- `cyclotomic n R` is different from `0`. -/ lemma cyclotomic_ne_zero (n : ℕ) (R : Type*) [ring R] [nontrivial R] : cyclotomic n R ≠ 0 := monic.ne_zero (cyclotomic.monic n R) /-- The degree of `cyclotomic n` is `totient n`. -/ lemma degree_cyclotomic (n : ℕ) (R : Type*) [ring R] [nontrivial R] : (cyclotomic n R).degree = nat.totient n := begin rw ←map_cyclotomic_int, rw degree_map_eq_of_leading_coeff_ne_zero (int.cast_ring_hom R) _, { cases n with k, { simp only [cyclotomic, degree_one, dif_pos, nat.totient_zero, with_top.coe_zero]}, rw [←degree_cyclotomic' (complex.is_primitive_root_exp k.succ (nat.succ_ne_zero k))], exact (int_cyclotomic_spec k.succ).2.1 }, simp only [(int_cyclotomic_spec n).right.right, ring_hom.eq_int_cast, monic.leading_coeff, int.cast_one, ne.def, not_false_iff, one_ne_zero] end /-- The natural degree of `cyclotomic n` is `totient n`. -/ lemma nat_degree_cyclotomic (n : ℕ) (R : Type*) [ring R] [nontrivial R] : (cyclotomic n R).nat_degree = nat.totient n := begin have hdeg := degree_cyclotomic n R, rw degree_eq_nat_degree (cyclotomic_ne_zero n R) at hdeg, norm_cast at hdeg, exact hdeg end /-- The degree of `cyclotomic n R` is positive. -/ lemma degree_cyclotomic_pos (n : ℕ) (R : Type*) (hpos : 0 < n) [ring R] [nontrivial R] : 0 < (cyclotomic n R).degree := by { rw degree_cyclotomic n R; exact_mod_cast (nat.totient_pos hpos) } /-- `∏ i in nat.divisors n, cyclotomic i R = X ^ n - 1`. -/ lemma prod_cyclotomic_eq_X_pow_sub_one {n : ℕ} (hpos : 0 < n) (R : Type*) [comm_ring R] : ∏ i in nat.divisors n, cyclotomic i R = X ^ n - 1 := begin have integer : ∏ i in nat.divisors n, cyclotomic i ℤ = X ^ n - 1, { have mapinj : function.injective (map (int.cast_ring_hom ℂ)), { apply map_injective, simp only [int.cast_injective, int.coe_cast_ring_hom] }, apply mapinj, rw map_prod (int.cast_ring_hom ℂ) (λ i, cyclotomic i ℤ), simp only [int_cyclotomic_spec, map_pow, nat.cast_id, map_X, map_one, ring_hom.coe_of, map_sub], exact prod_cyclotomic'_eq_X_pow_sub_one hpos (complex.is_primitive_root_exp n (ne_of_lt hpos).symm) }, have coerc : X ^ n - 1 = map (int.cast_ring_hom R) (X ^ n - 1), { simp only [map_pow, map_X, map_one, map_sub] }, have h : ∀ i ∈ n.divisors, cyclotomic i R = map (int.cast_ring_hom R) (cyclotomic i ℤ), { intros i hi, exact (map_cyclotomic_int i R).symm }, rw [finset.prod_congr (refl n.divisors) h, coerc, ←map_prod (int.cast_ring_hom R) (λ i, cyclotomic i ℤ), integer] end section arithmetic_function open nat.arithmetic_function open_locale arithmetic_function /-- `cyclotomic n R` can be expressed as a product in a fraction field of `polynomial R` using Möbius inversion. -/ lemma cyclotomic_eq_prod_X_pow_sub_one_pow_moebius {n : ℕ} (hpos : 0 < n) (R : Type*) [comm_ring R] [nontrivial R] {K : Type*} [field K] (f : fraction_map (polynomial R) K) : f.to_map (cyclotomic n R) = ∏ i in n.divisors_antidiagonal, (f.to_map (X ^ i.snd - 1)) ^ μ i.fst := begin have h : ∀ (n : ℕ), 0 < n → ∏ i in nat.divisors n, f.to_map (cyclotomic i R) = f.to_map (X ^ n - 1), { intros n hn, rw [← prod_cyclotomic_eq_X_pow_sub_one hn R, ring_hom.map_prod] }, rw (prod_eq_iff_prod_pow_moebius_eq_of_nonzero (λ n hn, _) (λ n hn, _)).1 h n hpos; rw [ne.def, fraction_map.to_map_eq_zero_iff], { apply cyclotomic_ne_zero }, { apply monic.ne_zero, apply monic_X_pow_sub_C _ (ne_of_gt hn) } end end arithmetic_function /-- We have `cyclotomic n R = (X ^ k - 1) /ₘ (∏ i in nat.proper_divisors k, cyclotomic i K)`. -/ lemma cyclotomic_eq_X_pow_sub_one_div {R : Type*} [comm_ring R] [nontrivial R] {n : ℕ} (hpos: 0 < n) : cyclotomic n R = (X ^ n - 1) /ₘ (∏ i in nat.proper_divisors n, cyclotomic i R) := begin rw [←prod_cyclotomic_eq_X_pow_sub_one hpos, nat.divisors_eq_proper_divisors_insert_self_of_pos hpos, finset.prod_insert nat.proper_divisors.not_self_mem], have prod_monic : (∏ i in nat.proper_divisors n, cyclotomic i R).monic, { apply monic_prod_of_monic, intros i hi, exact cyclotomic.monic i R }, rw (div_mod_by_monic_unique (cyclotomic n R) 0 prod_monic _).1, simp only [degree_zero, zero_add], split, { rw mul_comm }, rw [bot_lt_iff_ne_bot], intro h, exact monic.ne_zero prod_monic (degree_eq_bot.1 h) end /-- If `m` is a proper divisor of `n`, then `X ^ m - 1` divides `∏ i in nat.proper_divisors n, cyclotomic i R`. -/ lemma X_pow_sub_one_dvd_prod_cyclotomic (R : Type*) [comm_ring R] {n m : ℕ} (hpos : 0 < n) (hm : m ∣ n) (hdiff : m ≠ n) : X ^ m - 1 ∣ ∏ i in nat.proper_divisors n, cyclotomic i R := begin replace hm := nat.mem_proper_divisors.2 ⟨hm, lt_of_le_of_ne (nat.divisor_le (nat.mem_divisors.2 ⟨hm, (ne_of_lt hpos).symm⟩)) hdiff⟩, rw [← finset.sdiff_union_of_subset (nat.divisors_subset_proper_divisors (ne_of_lt hpos).symm (nat.mem_proper_divisors.1 hm).1 (ne_of_lt (nat.mem_proper_divisors.1 hm).2)), finset.prod_union finset.sdiff_disjoint, prod_cyclotomic_eq_X_pow_sub_one (nat.pos_of_mem_proper_divisors hm)], exact ⟨(∏ (x : ℕ) in n.proper_divisors \ m.divisors, cyclotomic x R), by rw mul_comm⟩ end /-- If there is a primitive `n`-th root of unity in `K`, then `cyclotomic n K = ∏ μ in primitive_roots n R, (X - C μ)`. In particular, `cyclotomic n K = cyclotomic' n K` -/ lemma cyclotomic_eq_prod_X_sub_primitive_roots {K : Type*} [field K] {ζ : K} {n : ℕ} (h : is_primitive_root ζ n) : cyclotomic n K = ∏ μ in primitive_roots n K, (X - C μ) := begin rw ←cyclotomic', revert h ζ, apply nat.strong_induction_on n, intros k hk z hz, cases nat.eq_zero_or_pos k with hzero hpos, { simp only [hzero, cyclotomic'_zero, cyclotomic_zero] }, have h : ∀ i ∈ k.proper_divisors, cyclotomic i K = cyclotomic' i K, { intros i hi, obtain ⟨d, hd⟩ := (nat.mem_proper_divisors.1 hi).1, rw mul_comm at hd, exact hk i (nat.mem_proper_divisors.1 hi).2 (is_primitive_root.pow hpos hz hd) }, rw [@cyclotomic_eq_X_pow_sub_one_div _ _ (field.to_nontrivial K) _ hpos, cyclotomic'_eq_X_pow_sub_one_div hpos hz, finset.prod_congr (refl k.proper_divisors) h] end /-- Any `n`-th primitive root of unity is a root of `cyclotomic n ℤ`.-/ lemma is_root_cyclotomic {n : ℕ} {K : Type*} [field K] (hpos : 0 < n) {μ : K} (h : is_primitive_root μ n) : is_root (cyclotomic n K) μ := begin rw [← mem_roots (cyclotomic_ne_zero n K), cyclotomic_eq_prod_X_sub_primitive_roots h, roots_prod_X_sub_C, ← finset.mem_def], rwa [← mem_primitive_roots hpos] at h, end lemma eq_cyclotomic_iff {R : Type*} [comm_ring R] [nontrivial R] {n : ℕ} (hpos: 0 < n) (P : polynomial R) : P = cyclotomic n R ↔ P * (∏ i in nat.proper_divisors n, polynomial.cyclotomic i R) = X ^ n - 1 := begin split, { intro hcycl, rw [hcycl, ← finset.prod_insert (@nat.proper_divisors.not_self_mem n), ← nat.divisors_eq_proper_divisors_insert_self_of_pos hpos], exact prod_cyclotomic_eq_X_pow_sub_one hpos R }, { intro hP, have prod_monic : (∏ i in nat.proper_divisors n, cyclotomic i R).monic, { apply monic_prod_of_monic, intros i hi, exact cyclotomic.monic i R }, rw [@cyclotomic_eq_X_pow_sub_one_div R _ _ _ hpos, (div_mod_by_monic_unique P 0 prod_monic _).1], split, { rwa [zero_add, mul_comm] }, rw [degree_zero, bot_lt_iff_ne_bot], intro h, exact monic.ne_zero prod_monic (degree_eq_bot.1 h) }, end /-- If `p` is prime, then `cyclotomic p R = geom_series X p`. -/ lemma cyclotomic_eq_geom_series {R : Type*} [comm_ring R] [nontrivial R] {p : ℕ} (hp : nat.prime p) : cyclotomic p R = geom_series X p := begin symmetry, refine (eq_cyclotomic_iff hp.pos _).mpr _, simp only [nat.prime.proper_divisors hp, geom_sum_mul, finset.prod_singleton, cyclotomic_one], end /-- The constant term of `cyclotomic n R` is `1` if `2 ≤ n`. -/ lemma cyclotomic_coeff_zero (R : Type*) [comm_ring R] {n : ℕ} (hn : 2 ≤ n) : (cyclotomic n R).coeff 0 = 1 := begin induction n using nat.strong_induction_on with n hi, have hprod : (∏ i in nat.proper_divisors n, (polynomial.cyclotomic i R).coeff 0) = -1, { rw [←finset.insert_erase (nat.one_mem_proper_divisors_iff_one_lt.2 (lt_of_lt_of_le one_lt_two hn)), finset.prod_insert (finset.not_mem_erase 1 _), cyclotomic_one R], have hleq : ∀ j ∈ n.proper_divisors.erase 1, 2 ≤ j, { intros j hj, apply nat.succ_le_of_lt, exact (ne.le_iff_lt ((finset.mem_erase.1 hj).1).symm).mp (nat.succ_le_of_lt (nat.pos_of_mem_proper_divisors (finset.mem_erase.1 hj).2)) }, have hcongr : ∀ j ∈ n.proper_divisors.erase 1, (cyclotomic j R).coeff 0 = 1, { intros j hj, exact hi j (nat.mem_proper_divisors.1 (finset.mem_erase.1 hj).2).2 (hleq j hj) }, have hrw : ∏ (x : ℕ) in n.proper_divisors.erase 1, (cyclotomic x R).coeff 0 = 1, { rw finset.prod_congr (refl (n.proper_divisors.erase 1)) hcongr, simp only [finset.prod_const_one] }, simp only [hrw, mul_one, zero_sub, coeff_one_zero, coeff_X_zero, coeff_sub] }, have heq : (X ^ n - 1).coeff 0 = -(cyclotomic n R).coeff 0, { rw [←prod_cyclotomic_eq_X_pow_sub_one (lt_of_lt_of_le zero_lt_two hn), nat.divisors_eq_proper_divisors_insert_self_of_pos (lt_of_lt_of_le zero_lt_two hn), finset.prod_insert nat.proper_divisors.not_self_mem, mul_coeff_zero, coeff_zero_prod, hprod, mul_neg_eq_neg_mul_symm, mul_one] }, have hzero : (X ^ n - 1).coeff 0 = (-1 : R), { rw coeff_zero_eq_eval_zero _, simp only [zero_pow (lt_of_lt_of_le zero_lt_two hn), eval_X, eval_one, zero_sub, eval_pow, eval_sub] }, rw hzero at heq, exact neg_inj.mp (eq.symm heq) end /-- If `(a : ℕ)` is a root of `cyclotomic n (zmod p)`, where `p` is a prime, then `a` and `p` are coprime. -/ lemma coprime_of_root_cyclotomic {n : ℕ} (hpos : 0 < n) {p : ℕ} [hprime : fact p.prime] {a : ℕ} (hroot : is_root (cyclotomic n (zmod p)) (nat.cast_ring_hom (zmod p) a)) : a.coprime p := begin apply nat.coprime.symm, rw [nat.prime.coprime_iff_not_dvd hprime], by_contra h, replace h := (zmod.nat_coe_zmod_eq_zero_iff_dvd a p).2 h, rw [is_root.def, ring_hom.eq_nat_cast, h, ← coeff_zero_eq_eval_zero] at hroot, by_cases hone : n = 1, { simp only [hone, cyclotomic_one, zero_sub, coeff_one_zero, coeff_X_zero, neg_eq_zero, one_ne_zero, coeff_sub] at hroot, exact hroot }, rw [cyclotomic_coeff_zero (zmod p) (nat.succ_le_of_lt (lt_of_le_of_ne (nat.succ_le_of_lt hpos) (ne.symm hone)))] at hroot, exact one_ne_zero hroot end end cyclotomic section order /-- If `(a : ℕ)` is a root of `cyclotomic n (zmod p)`, then the multiplicative order of `a` modulo `p` divides `n`. -/ lemma order_of_root_cyclotomic_dvd {n : ℕ} (hpos : 0 < n) {p : ℕ} [hprime : fact p.prime] {a : ℕ} (hroot : is_root (cyclotomic n (zmod p)) (nat.cast_ring_hom (zmod p) a)) : order_of (zmod.unit_of_coprime a (coprime_of_root_cyclotomic hpos hroot)) ∣ n := begin apply order_of_dvd_of_pow_eq_one, suffices hpow : eval (nat.cast_ring_hom (zmod p) a) (X ^ n - 1 : polynomial (zmod p)) = 0, { simp only [eval_X, eval_one, eval_pow, eval_sub, ring_hom.eq_nat_cast] at hpow, apply units.coe_eq_one.1, simp only [sub_eq_zero.mp hpow, zmod.cast_unit_of_coprime, units.coe_pow] }, rw [is_root.def] at hroot, rw [← prod_cyclotomic_eq_X_pow_sub_one hpos (zmod p), nat.divisors_eq_proper_divisors_insert_self_of_pos hpos, finset.prod_insert nat.proper_divisors.not_self_mem, eval_mul, hroot, zero_mul] end /-- If `(a : ℕ)` is a root of `cyclotomic n (zmod p)`, where `p` is a prime that does not divide `n`, then the multiplicative order of `a` modulo `p` is exactly `n`. -/ lemma order_of_root_cyclotomic {n : ℕ} (hpos : 0 < n) {p : ℕ} [hprime : fact p.prime] {a : ℕ} (hn : ¬ p ∣ n) (hroot : is_root (cyclotomic n (zmod p)) (nat.cast_ring_hom (zmod p) a)) : order_of (zmod.unit_of_coprime a (coprime_of_root_cyclotomic hpos hroot)) = n := begin set m := order_of (zmod.unit_of_coprime a (coprime_of_root_cyclotomic hpos hroot)), have ha := coprime_of_root_cyclotomic hpos hroot, have hdivcycl : map (int.cast_ring_hom (zmod p)) (X - a) ∣ (cyclotomic n (zmod p)), { replace hrootdiv := dvd_iff_is_root.2 hroot, simp only [C_eq_nat_cast, ring_hom.eq_nat_cast] at hrootdiv, simp only [hrootdiv, map_nat_cast, map_X, map_sub] }, by_contra hdiff, have hdiv : map (int.cast_ring_hom (zmod p)) (X - a) ∣ ∏ i in nat.proper_divisors n, cyclotomic i (zmod p), { suffices hdivm : map (int.cast_ring_hom (zmod p)) (X - a) ∣ X ^ m - 1, { exact dvd_trans hdivm (X_pow_sub_one_dvd_prod_cyclotomic (zmod p) hpos (order_of_root_cyclotomic_dvd hpos hroot) hdiff) }, rw [map_sub, map_X, map_nat_cast, ← C_eq_nat_cast, dvd_iff_is_root, is_root.def, eval_sub, eval_pow, eval_one, eval_X, sub_eq_zero, ← zmod.cast_unit_of_coprime a ha, ← units.coe_pow, units.coe_eq_one], exact pow_order_of_eq_one (zmod.unit_of_coprime a ha) }, have habs : (map (int.cast_ring_hom (zmod p)) (X - a)) ^ 2 ∣ X ^ n - 1, { obtain ⟨P, hP⟩ := hdivcycl, obtain ⟨Q, hQ⟩ := hdiv, rw [← prod_cyclotomic_eq_X_pow_sub_one hpos, nat.divisors_eq_proper_divisors_insert_self_of_pos hpos, finset.prod_insert nat.proper_divisors.not_self_mem, hP, hQ], exact ⟨P * Q, by ring⟩ }, have hnzero : ↑n ≠ (0 : (zmod p)), { intro ha, exact hn (int.coe_nat_dvd.1 ((zmod.int_coe_zmod_eq_zero_iff_dvd n p).1 ha)) }, rw [pow_two] at habs, replace habs := squarefree_X_pow_sub_C (1 : (zmod p)) hnzero one_ne_zero (map (int.cast_ring_hom (zmod p)) (X - a)) habs, simp only [map_nat_cast, map_X, map_sub] at habs, replace habs := degree_eq_zero_of_is_unit habs, rw [← C_eq_nat_cast, degree_X_sub_C] at habs, norm_cast at habs end end order end polynomial section minimial_polynomial open is_primitive_root polynomial complex /-- The minimal polynomial of a primitive `n`-th root of unity `μ` divides `cyclotomic n ℤ`. -/ lemma minimal_polynomial_primitive_root_dvd_cyclotomic {n : ℕ} {K : Type*} [field K] {μ : K} (h : is_primitive_root μ n) (hpos : 0 < n) [char_zero K] : minimal_polynomial (is_integral h hpos) ∣ cyclotomic n ℤ := begin apply minimal_polynomial.integer_dvd (is_integral h hpos) (cyclotomic.monic n ℤ).is_primitive, simpa [aeval_def, eval₂_eq_eval_map, is_root.def] using is_root_cyclotomic hpos h end /-- `cyclotomic n ℤ` is the minimal polynomial of a primitive `n`-th root of unity `μ`. -/ lemma minimal_polynomial_primitive_root_eq_cyclotomic {n : ℕ} {K : Type*} [field K] {μ : K} (h : is_primitive_root μ n) (hpos : 0 < n) [char_zero K] : cyclotomic n ℤ = minimal_polynomial (is_integral h hpos) := begin refine eq_of_monic_of_dvd_of_nat_degree_le (minimal_polynomial.monic (is_integral h hpos)) (cyclotomic.monic n ℤ) (minimal_polynomial_primitive_root_dvd_cyclotomic h hpos) _, simpa [nat_degree_cyclotomic n ℤ] using totient_le_degree_minimal_polynomial h hpos end /-- `cyclotomic n ℤ` is irreducible. -/ lemma cyclotomic.irreducible {n : ℕ} (hpos : 0 < n) : irreducible (cyclotomic n ℤ) := begin have h0 := (ne_of_lt hpos).symm, rw [minimal_polynomial_primitive_root_eq_cyclotomic (is_primitive_root_exp n h0) hpos], exact minimal_polynomial.irreducible (is_integral (is_primitive_root_exp n h0) hpos) end end minimial_polynomial
e02e7b0d5378f8b19f20db4f42bed86c16d92813
ce89339993655da64b6ccb555c837ce6c10f9ef4
/bluejam/chap4_exercise2.lean
beb9d36adc7877892a97aebac779c2ce86c9f21b
[]
no_license
zeptometer/LearnLean
ef32dc36a22119f18d843f548d0bb42f907bff5d
bb84d5dbe521127ba134d4dbf9559b294a80b9f7
refs/heads/master
1,625,710,824,322
1,601,382,570,000
1,601,382,570,000
195,228,870
2
0
null
null
null
null
UTF-8
Lean
false
false
1,117
lean
variables (α : Type) (p q : α → Prop) variable r : Prop example : α → ((∀ x : α, r) ↔ r) := assume y: α, iff.intro ( assume h: (∀ x: α, r), show r, from h y ) ( assume h: r, assume x: α, h ) -- example : (∀ x, p x ∨ r) ↔ (∀ x, p x) ∨ r := sorry -- left to right requires classical logic example : (∀ x, p x) ∨ r → (∀ x, p x ∨ r) := assume h: (∀ x, p x) ∨ r, or.elim h ( assume h₁: ∀ x, p x, assume y: α, or.intro_left r (h₁ y) ) ( assume h₂: r, assume y: α, or.intro_right (p y) h₂ ) example : (∀ x, r → p x) ↔ (r → ∀ x, p x) := iff.intro ( assume h: ∀ x, r → p x, assume hr: r, assume y: α, show p y, from (h y) hr ) ( assume h: r → ∀ x, p x, assume y: α, assume hr: r, show p y, from (h hr) y )
57ce0c84081ffed88015698538e170f747a046ee
bb31430994044506fa42fd667e2d556327e18dfe
/src/algebra/order/monoid/with_top.lean
c40041e3a9e031550594e9a45a8d385ac60dee86
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
20,877
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes Hölzl -/ import algebra.hom.group import algebra.order.monoid.order_dual import algebra.order.monoid.with_zero.basic import data.nat.cast.defs /-! # Adjoining top/bottom elements to ordered monoids. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4.-/ universes u v variables {α : Type u} {β : Type v} open function namespace with_top section has_one variables [has_one α] @[to_additive] instance : has_one (with_top α) := ⟨(1 : α)⟩ @[simp, norm_cast, to_additive] lemma coe_one : ((1 : α) : with_top α) = 1 := rfl @[simp, norm_cast, to_additive] lemma coe_eq_one {a : α} : (a : with_top α) = 1 ↔ a = 1 := coe_eq_coe @[simp, norm_cast, to_additive coe_pos] lemma one_lt_coe [has_lt α] {a : α} : 1 < (a : with_top α) ↔ 1 < a := coe_lt_coe @[simp, norm_cast, to_additive coe_lt_zero] lemma coe_lt_one [has_lt α] {a : α} : (a : with_top α) < 1 ↔ a < 1 := coe_lt_coe @[simp, to_additive] protected lemma map_one {β} (f : α → β) : (1 : with_top α).map f = (f 1 : with_top β) := rfl @[simp, norm_cast, to_additive] theorem one_eq_coe {a : α} : 1 = (a : with_top α) ↔ a = 1 := trans eq_comm coe_eq_one @[simp, to_additive] theorem top_ne_one : ⊤ ≠ (1 : with_top α) . @[simp, to_additive] theorem one_ne_top : (1 : with_top α) ≠ ⊤ . instance [has_zero α] [has_le α] [zero_le_one_class α] : zero_le_one_class (with_top α) := ⟨some_le_some.2 zero_le_one⟩ end has_one section has_add variables [has_add α] {a b c d : with_top α} {x y : α} instance : has_add (with_top α) := ⟨λ o₁ o₂, o₁.bind $ λ a, o₂.map $ (+) a⟩ @[norm_cast] lemma coe_add : ((x + y : α) : with_top α) = x + y := rfl @[norm_cast] lemma coe_bit0 : ((bit0 x : α) : with_top α) = bit0 x := rfl @[norm_cast] lemma coe_bit1 [has_one α] {a : α} : ((bit1 a : α) : with_top α) = bit1 a := rfl @[simp] lemma top_add (a : with_top α) : ⊤ + a = ⊤ := rfl @[simp] lemma add_top (a : with_top α) : a + ⊤ = ⊤ := by cases a; refl @[simp] lemma add_eq_top : a + b = ⊤ ↔ a = ⊤ ∨ b = ⊤ := by cases a; cases b; simp [none_eq_top, some_eq_coe, ←with_top.coe_add] lemma add_ne_top : a + b ≠ ⊤ ↔ a ≠ ⊤ ∧ b ≠ ⊤ := add_eq_top.not.trans not_or_distrib lemma add_lt_top [partial_order α] {a b : with_top α} : a + b < ⊤ ↔ a < ⊤ ∧ b < ⊤ := by simp_rw [lt_top_iff_ne_top, add_ne_top] lemma add_eq_coe : ∀ {a b : with_top α} {c : α}, a + b = c ↔ ∃ (a' b' : α), ↑a' = a ∧ ↑b' = b ∧ a' + b' = c | none b c := by simp [none_eq_top] | (some a) none c := by simp [none_eq_top] | (some a) (some b) c := by simp only [some_eq_coe, ← coe_add, coe_eq_coe, exists_and_distrib_left, exists_eq_left] @[simp] lemma add_coe_eq_top_iff {x : with_top α} {y : α} : x + y = ⊤ ↔ x = ⊤ := by { induction x using with_top.rec_top_coe; simp [← coe_add] } @[simp] lemma coe_add_eq_top_iff {y : with_top α} : ↑x + y = ⊤ ↔ y = ⊤ := by { induction y using with_top.rec_top_coe; simp [← coe_add] } instance covariant_class_add_le [has_le α] [covariant_class α α (+) (≤)] : covariant_class (with_top α) (with_top α) (+) (≤) := ⟨λ a b c h, begin cases a; cases c; try { exact le_top }, rcases le_coe_iff.1 h with ⟨b, rfl, h'⟩, exact coe_le_coe.2 (add_le_add_left (coe_le_coe.1 h) _) end⟩ instance covariant_class_swap_add_le [has_le α] [covariant_class α α (swap (+)) (≤)] : covariant_class (with_top α) (with_top α) (swap (+)) (≤) := ⟨λ a b c h, begin cases a; cases c; try { exact le_top }, rcases le_coe_iff.1 h with ⟨b, rfl, h'⟩, exact coe_le_coe.2 (add_le_add_right (coe_le_coe.1 h) _) end⟩ instance contravariant_class_add_lt [has_lt α] [contravariant_class α α (+) (<)] : contravariant_class (with_top α) (with_top α) (+) (<) := ⟨λ a b c h, begin induction a using with_top.rec_top_coe, { exact (not_none_lt _ h).elim }, induction b using with_top.rec_top_coe, { exact (not_none_lt _ h).elim }, induction c using with_top.rec_top_coe, { exact coe_lt_top _ }, { exact coe_lt_coe.2 (lt_of_add_lt_add_left $ coe_lt_coe.1 h) } end⟩ instance contravariant_class_swap_add_lt [has_lt α] [contravariant_class α α (swap (+)) (<)] : contravariant_class (with_top α) (with_top α) (swap (+)) (<) := ⟨λ a b c h, begin cases a; cases b; try { exact (not_none_lt _ h).elim }, cases c, { exact coe_lt_top _ }, { exact coe_lt_coe.2 (lt_of_add_lt_add_right $ coe_lt_coe.1 h) } end⟩ protected lemma le_of_add_le_add_left [has_le α] [contravariant_class α α (+) (≤)] (ha : a ≠ ⊤) (h : a + b ≤ a + c) : b ≤ c := begin lift a to α using ha, induction c using with_top.rec_top_coe, { exact le_top }, induction b using with_top.rec_top_coe, { exact (not_top_le_coe _ h).elim }, simp only [← coe_add, coe_le_coe] at h ⊢, exact le_of_add_le_add_left h end protected lemma le_of_add_le_add_right [has_le α] [contravariant_class α α (swap (+)) (≤)] (ha : a ≠ ⊤) (h : b + a ≤ c + a) : b ≤ c := begin lift a to α using ha, cases c, { exact le_top }, cases b, { exact (not_top_le_coe _ h).elim }, { exact coe_le_coe.2 (le_of_add_le_add_right $ coe_le_coe.1 h) } end protected lemma add_lt_add_left [has_lt α] [covariant_class α α (+) (<)] (ha : a ≠ ⊤) (h : b < c) : a + b < a + c := begin lift a to α using ha, rcases lt_iff_exists_coe.1 h with ⟨b, rfl, h'⟩, cases c, { exact coe_lt_top _ }, { exact coe_lt_coe.2 (add_lt_add_left (coe_lt_coe.1 h) _) } end protected lemma add_lt_add_right [has_lt α] [covariant_class α α (swap (+)) (<)] (ha : a ≠ ⊤) (h : b < c) : b + a < c + a := begin lift a to α using ha, rcases lt_iff_exists_coe.1 h with ⟨b, rfl, h'⟩, cases c, { exact coe_lt_top _ }, { exact coe_lt_coe.2 (add_lt_add_right (coe_lt_coe.1 h) _) } end protected lemma add_le_add_iff_left [has_le α] [covariant_class α α (+) (≤)] [contravariant_class α α (+) (≤)] (ha : a ≠ ⊤) : a + b ≤ a + c ↔ b ≤ c := ⟨with_top.le_of_add_le_add_left ha, λ h, add_le_add_left h a⟩ protected lemma add_le_add_iff_right [has_le α] [covariant_class α α (swap (+)) (≤)] [contravariant_class α α (swap (+)) (≤)] (ha : a ≠ ⊤) : b + a ≤ c + a ↔ b ≤ c := ⟨with_top.le_of_add_le_add_right ha, λ h, add_le_add_right h a⟩ protected lemma add_lt_add_iff_left [has_lt α] [covariant_class α α (+) (<)] [contravariant_class α α (+) (<)] (ha : a ≠ ⊤) : a + b < a + c ↔ b < c := ⟨lt_of_add_lt_add_left, with_top.add_lt_add_left ha⟩ protected lemma add_lt_add_iff_right [has_lt α] [covariant_class α α (swap (+)) (<)] [contravariant_class α α (swap (+)) (<)] (ha : a ≠ ⊤) : b + a < c + a ↔ b < c := ⟨lt_of_add_lt_add_right, with_top.add_lt_add_right ha⟩ protected lemma add_lt_add_of_le_of_lt [preorder α] [covariant_class α α (+) (<)] [covariant_class α α (swap (+)) (≤)] (ha : a ≠ ⊤) (hab : a ≤ b) (hcd : c < d) : a + c < b + d := (with_top.add_lt_add_left ha hcd).trans_le $ add_le_add_right hab _ protected lemma add_lt_add_of_lt_of_le [preorder α] [covariant_class α α (+) (≤)] [covariant_class α α (swap (+)) (<)] (hc : c ≠ ⊤) (hab : a < b) (hcd : c ≤ d) : a + c < b + d := (with_top.add_lt_add_right hc hab).trans_le $ add_le_add_left hcd _ /- There is no `with_top.map_mul_of_mul_hom`, since `with_top` does not have a multiplication. -/ @[simp] protected lemma map_add {F} [has_add β] [add_hom_class F α β] (f : F) (a b : with_top α) : (a + b).map f = a.map f + b.map f := begin induction a using with_top.rec_top_coe, { exact (top_add _).symm }, { induction b using with_top.rec_top_coe, { exact (add_top _).symm }, { rw [map_coe, map_coe, ← coe_add, ← coe_add, ← map_add], refl } }, end end has_add instance [add_semigroup α] : add_semigroup (with_top α) := { add_assoc := begin repeat { refine with_top.rec_top_coe _ _; try { intro }}; simp [←with_top.coe_add, add_assoc] end, ..with_top.has_add } instance [add_comm_semigroup α] : add_comm_semigroup (with_top α) := { add_comm := begin repeat { refine with_top.rec_top_coe _ _; try { intro }}; simp [←with_top.coe_add, add_comm] end, ..with_top.add_semigroup } instance [add_zero_class α] : add_zero_class (with_top α) := { zero_add := begin refine with_top.rec_top_coe _ _, { simp }, { intro, rw [←with_top.coe_zero, ←with_top.coe_add, zero_add] } end, add_zero := begin refine with_top.rec_top_coe _ _, { simp }, { intro, rw [←with_top.coe_zero, ←with_top.coe_add, add_zero] } end, ..with_top.has_zero, ..with_top.has_add } instance [add_monoid α] : add_monoid (with_top α) := { ..with_top.add_zero_class, ..with_top.has_zero, ..with_top.add_semigroup } instance [add_comm_monoid α] : add_comm_monoid (with_top α) := { ..with_top.add_monoid, ..with_top.add_comm_semigroup } instance [add_monoid_with_one α] : add_monoid_with_one (with_top α) := { nat_cast := λ n, ↑(n : α), nat_cast_zero := by rw [nat.cast_zero, with_top.coe_zero], nat_cast_succ := λ n, by rw [nat.cast_add_one, with_top.coe_add, with_top.coe_one], .. with_top.has_one, .. with_top.add_monoid } instance [add_comm_monoid_with_one α] : add_comm_monoid_with_one (with_top α) := { .. with_top.add_monoid_with_one, .. with_top.add_comm_monoid } instance [ordered_add_comm_monoid α] : ordered_add_comm_monoid (with_top α) := { add_le_add_left := begin rintros a b h (_|c), { simp [none_eq_top] }, rcases b with (_|b), { simp [none_eq_top] }, rcases le_coe_iff.1 h with ⟨a, rfl, h⟩, simp only [some_eq_coe, ← coe_add, coe_le_coe] at h ⊢, exact add_le_add_left h c end, ..with_top.partial_order, ..with_top.add_comm_monoid } instance [linear_ordered_add_comm_monoid α] : linear_ordered_add_comm_monoid_with_top (with_top α) := { top_add' := with_top.top_add, ..with_top.order_top, ..with_top.linear_order, ..with_top.ordered_add_comm_monoid, ..option.nontrivial } instance [has_le α] [has_add α] [has_exists_add_of_le α] : has_exists_add_of_le (with_top α) := ⟨λ a b, match a, b with | ⊤, ⊤ := by simp | (a : α), ⊤ := λ _, ⟨⊤, rfl⟩ | (a : α), (b : α) := λ h, begin obtain ⟨c, rfl⟩ := exists_add_of_le (with_top.coe_le_coe.1 h), exact ⟨c, rfl⟩ end | ⊤, (b : α) := λ h, (not_top_le_coe _ h).elim end⟩ instance [canonically_ordered_add_monoid α] : canonically_ordered_add_monoid (with_top α) := { le_self_add := λ a b, match a, b with | ⊤, ⊤ := le_rfl | (a : α), ⊤ := le_top | (a : α), (b : α) := with_top.coe_le_coe.2 le_self_add | ⊤, (b : α) := le_rfl end, ..with_top.order_bot, ..with_top.ordered_add_comm_monoid, ..with_top.has_exists_add_of_le } instance [canonically_linear_ordered_add_monoid α] : canonically_linear_ordered_add_monoid (with_top α) := { ..with_top.canonically_ordered_add_monoid, ..with_top.linear_order } @[simp, norm_cast] lemma coe_nat [add_monoid_with_one α] (n : ℕ) : ((n : α) : with_top α) = n := rfl @[simp] lemma nat_ne_top [add_monoid_with_one α] (n : ℕ) : (n : with_top α) ≠ ⊤ := coe_ne_top @[simp] lemma top_ne_nat [add_monoid_with_one α] (n : ℕ) : (⊤ : with_top α) ≠ n := top_ne_coe /-- Coercion from `α` to `with_top α` as an `add_monoid_hom`. -/ def coe_add_hom [add_monoid α] : α →+ with_top α := ⟨coe, rfl, λ _ _, rfl⟩ @[simp] lemma coe_coe_add_hom [add_monoid α] : ⇑(coe_add_hom : α →+ with_top α) = coe := rfl @[simp] lemma zero_lt_top [ordered_add_comm_monoid α] : (0 : with_top α) < ⊤ := coe_lt_top 0 @[simp, norm_cast] lemma zero_lt_coe [ordered_add_comm_monoid α] (a : α) : (0 : with_top α) < a ↔ 0 < a := coe_lt_coe /-- A version of `with_top.map` for `one_hom`s. -/ @[to_additive "A version of `with_top.map` for `zero_hom`s", simps { fully_applied := ff }] protected def _root_.one_hom.with_top_map {M N : Type*} [has_one M] [has_one N] (f : one_hom M N) : one_hom (with_top M) (with_top N) := { to_fun := with_top.map f, map_one' := by rw [with_top.map_one, map_one, coe_one] } /-- A version of `with_top.map` for `add_hom`s. -/ @[simps { fully_applied := ff }] protected def _root_.add_hom.with_top_map {M N : Type*} [has_add M] [has_add N] (f : add_hom M N) : add_hom (with_top M) (with_top N) := { to_fun := with_top.map f, map_add' := with_top.map_add f } /-- A version of `with_top.map` for `add_monoid_hom`s. -/ @[simps { fully_applied := ff }] protected def _root_.add_monoid_hom.with_top_map {M N : Type*} [add_zero_class M] [add_zero_class N] (f : M →+ N) : with_top M →+ with_top N := { to_fun := with_top.map f, .. f.to_zero_hom.with_top_map, .. f.to_add_hom.with_top_map } end with_top namespace with_bot @[to_additive] instance [has_one α] : has_one (with_bot α) := with_top.has_one instance [has_add α] : has_add (with_bot α) := with_top.has_add instance [add_semigroup α] : add_semigroup (with_bot α) := with_top.add_semigroup instance [add_comm_semigroup α] : add_comm_semigroup (with_bot α) := with_top.add_comm_semigroup instance [add_zero_class α] : add_zero_class (with_bot α) := with_top.add_zero_class instance [add_monoid α] : add_monoid (with_bot α) := with_top.add_monoid instance [add_comm_monoid α] : add_comm_monoid (with_bot α) := with_top.add_comm_monoid instance [add_monoid_with_one α] : add_monoid_with_one (with_bot α) := with_top.add_monoid_with_one instance [add_comm_monoid_with_one α] : add_comm_monoid_with_one (with_bot α) := with_top.add_comm_monoid_with_one instance [has_zero α] [has_one α] [has_le α] [zero_le_one_class α] : zero_le_one_class (with_bot α) := ⟨some_le_some.2 zero_le_one⟩ -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` @[to_additive] lemma coe_one [has_one α] : ((1 : α) : with_bot α) = 1 := rfl -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` @[to_additive] lemma coe_eq_one [has_one α] {a : α} : (a : with_bot α) = 1 ↔ a = 1 := with_top.coe_eq_one @[norm_cast, to_additive coe_pos] lemma one_lt_coe [has_one α] [has_lt α] {a : α} : 1 < (a : with_bot α) ↔ 1 < a := coe_lt_coe @[norm_cast, to_additive coe_lt_zero] lemma coe_lt_one [has_one α] [has_lt α] {a : α} : (a : with_bot α) < 1 ↔ a < 1 := coe_lt_coe @[to_additive] protected lemma map_one {β} [has_one α] (f : α → β) : (1 : with_bot α).map f = (f 1 : with_bot β) := rfl @[norm_cast] lemma coe_nat [add_monoid_with_one α] (n : ℕ) : ((n : α) : with_bot α) = n := rfl @[simp] lemma nat_ne_bot [add_monoid_with_one α] (n : ℕ) : (n : with_bot α) ≠ ⊥ := coe_ne_bot @[simp] lemma bot_ne_nat [add_monoid_with_one α] (n : ℕ) : (⊥ : with_bot α) ≠ n := bot_ne_coe section has_add variables [has_add α] {a b c d : with_bot α} {x y : α} -- `norm_cast` proves those lemmas, because `with_top`/`with_bot` are reducible lemma coe_add (a b : α) : ((a + b : α) : with_bot α) = a + b := rfl lemma coe_bit0 : ((bit0 x : α) : with_bot α) = bit0 x := rfl lemma coe_bit1 [has_one α] {a : α} : ((bit1 a : α) : with_bot α) = bit1 a := rfl @[simp] lemma bot_add (a : with_bot α) : ⊥ + a = ⊥ := rfl @[simp] lemma add_bot (a : with_bot α) : a + ⊥ = ⊥ := by cases a; refl @[simp] lemma add_eq_bot : a + b = ⊥ ↔ a = ⊥ ∨ b = ⊥ := with_top.add_eq_top lemma add_ne_bot : a + b ≠ ⊥ ↔ a ≠ ⊥ ∧ b ≠ ⊥ := with_top.add_ne_top lemma bot_lt_add [partial_order α] {a b : with_bot α} : ⊥ < a + b ↔ ⊥ < a ∧ ⊥ < b := @with_top.add_lt_top αᵒᵈ _ _ _ _ lemma add_eq_coe : a + b = x ↔ ∃ (a' b' : α), ↑a' = a ∧ ↑b' = b ∧ a' + b' = x := with_top.add_eq_coe @[simp] lemma add_coe_eq_bot_iff : a + y = ⊥ ↔ a = ⊥ := with_top.add_coe_eq_top_iff @[simp] lemma coe_add_eq_bot_iff : ↑x + b = ⊥ ↔ b = ⊥ := with_top.coe_add_eq_top_iff /- There is no `with_bot.map_mul_of_mul_hom`, since `with_bot` does not have a multiplication. -/ @[simp] protected lemma map_add {F} [has_add β] [add_hom_class F α β] (f : F) (a b : with_bot α) : (a + b).map f = a.map f + b.map f := with_top.map_add f a b /-- A version of `with_bot.map` for `one_hom`s. -/ @[to_additive "A version of `with_bot.map` for `zero_hom`s", simps { fully_applied := ff }] protected def _root_.one_hom.with_bot_map {M N : Type*} [has_one M] [has_one N] (f : one_hom M N) : one_hom (with_bot M) (with_bot N) := { to_fun := with_bot.map f, map_one' := by rw [with_bot.map_one, map_one, coe_one] } /-- A version of `with_bot.map` for `add_hom`s. -/ @[simps { fully_applied := ff }] protected def _root_.add_hom.with_bot_map {M N : Type*} [has_add M] [has_add N] (f : add_hom M N) : add_hom (with_bot M) (with_bot N) := { to_fun := with_bot.map f, map_add' := with_bot.map_add f } /-- A version of `with_bot.map` for `add_monoid_hom`s. -/ @[simps { fully_applied := ff }] protected def _root_.add_monoid_hom.with_bot_map {M N : Type*} [add_zero_class M] [add_zero_class N] (f : M →+ N) : with_bot M →+ with_bot N := { to_fun := with_bot.map f, .. f.to_zero_hom.with_bot_map, .. f.to_add_hom.with_bot_map } variables [preorder α] instance covariant_class_add_le [covariant_class α α (+) (≤)] : covariant_class (with_bot α) (with_bot α) (+) (≤) := @order_dual.covariant_class_add_le (with_top αᵒᵈ) _ _ _ instance covariant_class_swap_add_le [covariant_class α α (swap (+)) (≤)] : covariant_class (with_bot α) (with_bot α) (swap (+)) (≤) := @order_dual.covariant_class_swap_add_le (with_top αᵒᵈ) _ _ _ instance contravariant_class_add_lt [contravariant_class α α (+) (<)] : contravariant_class (with_bot α) (with_bot α) (+) (<) := @order_dual.contravariant_class_add_lt (with_top αᵒᵈ) _ _ _ instance contravariant_class_swap_add_lt [contravariant_class α α (swap (+)) (<)] : contravariant_class (with_bot α) (with_bot α) (swap (+)) (<) := @order_dual.contravariant_class_swap_add_lt (with_top αᵒᵈ) _ _ _ protected lemma le_of_add_le_add_left [contravariant_class α α (+) (≤)] (ha : a ≠ ⊥) (h : a + b ≤ a + c) : b ≤ c := @with_top.le_of_add_le_add_left αᵒᵈ _ _ _ _ _ _ ha h protected lemma le_of_add_le_add_right [contravariant_class α α (swap (+)) (≤)] (ha : a ≠ ⊥) (h : b + a ≤ c + a) : b ≤ c := @with_top.le_of_add_le_add_right αᵒᵈ _ _ _ _ _ _ ha h protected lemma add_lt_add_left [covariant_class α α (+) (<)] (ha : a ≠ ⊥) (h : b < c) : a + b < a + c := @with_top.add_lt_add_left αᵒᵈ _ _ _ _ _ _ ha h protected lemma add_lt_add_right [covariant_class α α (swap (+)) (<)] (ha : a ≠ ⊥) (h : b < c) : b + a < c + a := @with_top.add_lt_add_right αᵒᵈ _ _ _ _ _ _ ha h protected lemma add_le_add_iff_left [covariant_class α α (+) (≤)] [contravariant_class α α (+) (≤)] (ha : a ≠ ⊥) : a + b ≤ a + c ↔ b ≤ c := ⟨with_bot.le_of_add_le_add_left ha, λ h, add_le_add_left h a⟩ protected lemma add_le_add_iff_right [covariant_class α α (swap (+)) (≤)] [contravariant_class α α (swap (+)) (≤)] (ha : a ≠ ⊥) : b + a ≤ c + a ↔ b ≤ c := ⟨with_bot.le_of_add_le_add_right ha, λ h, add_le_add_right h a⟩ protected lemma add_lt_add_iff_left [covariant_class α α (+) (<)] [contravariant_class α α (+) (<)] (ha : a ≠ ⊥) : a + b < a + c ↔ b < c := ⟨lt_of_add_lt_add_left, with_bot.add_lt_add_left ha⟩ protected lemma add_lt_add_iff_right [covariant_class α α (swap (+)) (<)] [contravariant_class α α (swap (+)) (<)] (ha : a ≠ ⊥) : b + a < c + a ↔ b < c := ⟨lt_of_add_lt_add_right, with_bot.add_lt_add_right ha⟩ protected lemma add_lt_add_of_le_of_lt [covariant_class α α (+) (<)] [covariant_class α α (swap (+)) (≤)] (hb : b ≠ ⊥) (hab : a ≤ b) (hcd : c < d) : a + c < b + d := @with_top.add_lt_add_of_le_of_lt αᵒᵈ _ _ _ _ _ _ _ _ hb hab hcd protected lemma add_lt_add_of_lt_of_le [covariant_class α α (+) (≤)] [covariant_class α α (swap (+)) (<)] (hd : d ≠ ⊥) (hab : a < b) (hcd : c ≤ d) : a + c < b + d := @with_top.add_lt_add_of_lt_of_le αᵒᵈ _ _ _ _ _ _ _ _ hd hab hcd end has_add instance [ordered_add_comm_monoid α] : ordered_add_comm_monoid (with_bot α) := { add_le_add_left := λ a b h c, add_le_add_left h c, ..with_bot.partial_order, ..with_bot.add_comm_monoid } instance [linear_ordered_add_comm_monoid α] : linear_ordered_add_comm_monoid (with_bot α) := { ..with_bot.linear_order, ..with_bot.ordered_add_comm_monoid } end with_bot
7c36b450cc4ee44e33a9689997ecce267ef25ced
9028d228ac200bbefe3a711342514dd4e4458bff
/src/order/lattice.lean
8ac6ff0f7c318c3042caaf044dba3afa84e58d9f
[ "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
17,633
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 the inf/sup (semi)-lattice with optionally top/bot type class hierarchy. -/ import order.rel_classes set_option old_structure_cmd true universes u v w variables {α : Type u} {β : Type v} -- TODO: move this eventually, if we decide to use them attribute [ematch] le_trans lt_of_le_of_lt lt_of_lt_of_le lt_trans section -- TODO: this seems crazy, but it also seems to work reasonably well @[ematch] theorem le_antisymm' [partial_order α] : ∀ {a b : α}, (: a ≤ b :) → b ≤ a → a = b := @le_antisymm _ _ end /- TODO: automatic construction of dual definitions / theorems -/ reserve infixl ` ⊓ `:70 reserve infixl ` ⊔ `:65 /-- Typeclass for the `⊔` (`\lub`) notation -/ class has_sup (α : Type u) := (sup : α → α → α) /-- Typeclass for the `⊓` (`\glb`) notation -/ class has_inf (α : Type u) := (inf : α → α → α) infix ⊔ := has_sup.sup infix ⊓ := has_inf.inf /-- A `semilattice_sup` is a join-semilattice, that is, a partial order with a join (a.k.a. lub / least upper bound, sup / supremum) operation `⊔` which is the least element larger than both factors. -/ class semilattice_sup (α : Type u) extends has_sup α, partial_order α := (le_sup_left : ∀ a b : α, a ≤ a ⊔ b) (le_sup_right : ∀ a b : α, b ≤ a ⊔ b) (sup_le : ∀ a b c : α, a ≤ c → b ≤ c → a ⊔ b ≤ c) instance (α : Type*) [has_inf α] : has_sup (order_dual α) := ⟨((⊓) : α → α → α)⟩ instance (α : Type*) [has_sup α] : has_inf (order_dual α) := ⟨((⊔) : α → α → α)⟩ section semilattice_sup variables [semilattice_sup α] {a b c d : α} @[simp] theorem le_sup_left : a ≤ a ⊔ b := semilattice_sup.le_sup_left a b @[ematch] theorem le_sup_left' : a ≤ (: a ⊔ b :) := le_sup_left @[simp] theorem le_sup_right : b ≤ a ⊔ b := semilattice_sup.le_sup_right a b @[ematch] theorem le_sup_right' : b ≤ (: a ⊔ b :) := le_sup_right theorem le_sup_left_of_le (h : c ≤ a) : c ≤ a ⊔ b := le_trans h le_sup_left theorem le_sup_right_of_le (h : c ≤ b) : c ≤ a ⊔ b := le_trans h le_sup_right theorem sup_le : a ≤ c → b ≤ c → a ⊔ b ≤ c := semilattice_sup.sup_le a b c @[simp] theorem sup_le_iff : a ⊔ b ≤ c ↔ a ≤ c ∧ b ≤ c := ⟨assume h : a ⊔ b ≤ c, ⟨le_trans le_sup_left h, le_trans le_sup_right h⟩, assume ⟨h₁, h₂⟩, sup_le h₁ h₂⟩ @[simp] theorem sup_eq_left : a ⊔ b = a ↔ b ≤ a := le_antisymm_iff.trans $ by simp [le_refl] theorem sup_of_le_left (h : b ≤ a) : a ⊔ b = a := sup_eq_left.2 h @[simp] theorem left_eq_sup : a = a ⊔ b ↔ b ≤ a := eq_comm.trans sup_eq_left @[simp] theorem sup_eq_right : a ⊔ b = b ↔ a ≤ b := le_antisymm_iff.trans $ by simp [le_refl] theorem sup_of_le_right (h : a ≤ b) : a ⊔ b = b := sup_eq_right.2 h @[simp] theorem right_eq_sup : b = a ⊔ b ↔ a ≤ b := eq_comm.trans sup_eq_right theorem sup_le_sup (h₁ : a ≤ b) (h₂ : c ≤ d) : a ⊔ c ≤ b ⊔ d := sup_le (le_sup_left_of_le h₁) (le_sup_right_of_le h₂) theorem sup_le_sup_left (h₁ : a ≤ b) (c) : c ⊔ a ≤ c ⊔ b := sup_le_sup (le_refl _) h₁ theorem sup_le_sup_right (h₁ : a ≤ b) (c) : a ⊔ c ≤ b ⊔ c := sup_le_sup h₁ (le_refl _) theorem le_of_sup_eq (h : a ⊔ b = b) : a ≤ b := by { rw ← h, simp } lemma sup_ind [is_total α (≤)] (a b : α) {p : α → Prop} (ha : p a) (hb : p b) : p (a ⊔ b) := (is_total.total a b).elim (λ h : a ≤ b, by rwa sup_eq_right.2 h) (λ h, by rwa sup_eq_left.2 h) @[simp] lemma sup_lt_iff [is_total α (≤)] {a b c : α} : b ⊔ c < a ↔ b < a ∧ c < a := ⟨λ h, ⟨le_sup_left.trans_lt h, le_sup_right.trans_lt h⟩, λ h, sup_ind b c h.1 h.2⟩ @[simp] lemma le_sup_iff [is_total α (≤)] {a b c : α} : a ≤ b ⊔ c ↔ a ≤ b ∨ a ≤ c := by rw [← not_iff_not]; simp only [not_or_distrib, @sup_lt_iff α, @not_le (as_linear_order α)] @[simp] lemma lt_sup_iff [is_total α (≤)] {a b c : α} : a < b ⊔ c ↔ a < b ∨ a < c := by { rw ← not_iff_not, simp only [not_or_distrib, @not_lt (as_linear_order α), sup_le_iff] } @[simp] theorem sup_idem : a ⊔ a = a := by apply le_antisymm; simp instance sup_is_idempotent : is_idempotent α (⊔) := ⟨@sup_idem _ _⟩ theorem sup_comm : a ⊔ b = b ⊔ a := by apply le_antisymm; simp instance sup_is_commutative : is_commutative α (⊔) := ⟨@sup_comm _ _⟩ theorem sup_assoc : a ⊔ b ⊔ c = a ⊔ (b ⊔ c) := le_antisymm (sup_le (sup_le le_sup_left (le_sup_right_of_le le_sup_left)) (le_sup_right_of_le le_sup_right)) (sup_le (le_sup_left_of_le le_sup_left) (sup_le (le_sup_left_of_le le_sup_right) le_sup_right)) instance sup_is_associative : is_associative α (⊔) := ⟨@sup_assoc _ _⟩ @[simp] lemma sup_left_idem : a ⊔ (a ⊔ b) = a ⊔ b := by rw [← sup_assoc, sup_idem] @[simp] lemma sup_right_idem : (a ⊔ b) ⊔ b = a ⊔ b := by rw [sup_assoc, sup_idem] lemma sup_left_comm (a b c : α) : a ⊔ (b ⊔ c) = b ⊔ (a ⊔ c) := by rw [← sup_assoc, ← sup_assoc, @sup_comm α _ a] lemma forall_le_or_exists_lt_sup (a : α) : (∀b, b ≤ a) ∨ (∃b, a < b) := suffices (∃b, ¬b ≤ a) → (∃b, a < b), by rwa [or_iff_not_imp_left, not_forall], assume ⟨b, hb⟩, ⟨a ⊔ b, lt_of_le_of_ne le_sup_left $ mt left_eq_sup.1 hb⟩ theorem semilattice_sup.ext_sup {α} {A B : semilattice_sup α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) (x y : α) : (by haveI := A; exact (x ⊔ y)) = x ⊔ y := eq_of_forall_ge_iff $ λ c, by simp only [sup_le_iff]; rw [← H, @sup_le_iff α A, H, H] theorem semilattice_sup.ext {α} {A B : semilattice_sup α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : A = B := begin have := partial_order.ext H, have ss := funext (λ x, funext $ semilattice_sup.ext_sup H x), casesI A, casesI B, injection this; congr' end end semilattice_sup /-- A `semilattice_inf` is a meet-semilattice, that is, a partial order with a meet (a.k.a. glb / greatest lower bound, inf / infimum) operation `⊓` which is the greatest element smaller than both factors. -/ class semilattice_inf (α : Type u) extends has_inf α, partial_order α := (inf_le_left : ∀ a b : α, a ⊓ b ≤ a) (inf_le_right : ∀ a b : α, a ⊓ b ≤ b) (le_inf : ∀ a b c : α, a ≤ b → a ≤ c → a ≤ b ⊓ c) instance (α) [semilattice_inf α] : semilattice_sup (order_dual α) := { le_sup_left := semilattice_inf.inf_le_left, le_sup_right := semilattice_inf.inf_le_right, sup_le := assume a b c hca hcb, @semilattice_inf.le_inf α _ _ _ _ hca hcb, .. order_dual.partial_order α, .. order_dual.has_sup α } instance (α) [semilattice_sup α] : semilattice_inf (order_dual α) := { inf_le_left := @le_sup_left α _, inf_le_right := @le_sup_right α _, le_inf := assume a b c hca hcb, @sup_le α _ _ _ _ hca hcb, .. order_dual.partial_order α, .. order_dual.has_inf α } section semilattice_inf variables [semilattice_inf α] {a b c d : α} @[simp] theorem inf_le_left : a ⊓ b ≤ a := semilattice_inf.inf_le_left a b @[ematch] theorem inf_le_left' : (: a ⊓ b :) ≤ a := semilattice_inf.inf_le_left a b @[simp] theorem inf_le_right : a ⊓ b ≤ b := semilattice_inf.inf_le_right a b @[ematch] theorem inf_le_right' : (: a ⊓ b :) ≤ b := semilattice_inf.inf_le_right a b theorem le_inf : a ≤ b → a ≤ c → a ≤ b ⊓ c := semilattice_inf.le_inf a b c theorem inf_le_left_of_le (h : a ≤ c) : a ⊓ b ≤ c := le_trans inf_le_left h theorem inf_le_right_of_le (h : b ≤ c) : a ⊓ b ≤ c := le_trans inf_le_right h @[simp] theorem le_inf_iff : a ≤ b ⊓ c ↔ a ≤ b ∧ a ≤ c := @sup_le_iff (order_dual α) _ _ _ _ @[simp] theorem inf_eq_left : a ⊓ b = a ↔ a ≤ b := le_antisymm_iff.trans $ by simp [le_refl] theorem inf_of_le_left (h : a ≤ b) : a ⊓ b = a := inf_eq_left.2 h @[simp] theorem left_eq_inf : a = a ⊓ b ↔ a ≤ b := eq_comm.trans inf_eq_left @[simp] theorem inf_eq_right : a ⊓ b = b ↔ b ≤ a := le_antisymm_iff.trans $ by simp [le_refl] theorem inf_of_le_right (h : b ≤ a) : a ⊓ b = b := inf_eq_right.2 h @[simp] theorem right_eq_inf : b = a ⊓ b ↔ b ≤ a := eq_comm.trans inf_eq_right theorem inf_le_inf (h₁ : a ≤ b) (h₂ : c ≤ d) : a ⊓ c ≤ b ⊓ d := le_inf (inf_le_left_of_le h₁) (inf_le_right_of_le h₂) lemma inf_le_inf_right (a : α) {b c : α} (h : b ≤ c) : b ⊓ a ≤ c ⊓ a := inf_le_inf h (le_refl _) lemma inf_le_inf_left (a : α) {b c : α} (h : b ≤ c) : a ⊓ b ≤ a ⊓ c := inf_le_inf (le_refl _) h theorem le_of_inf_eq (h : a ⊓ b = a) : a ≤ b := by { rw ← h, simp } lemma inf_ind [is_total α (≤)] (a b : α) {p : α → Prop} (ha : p a) (hb : p b) : p (a ⊓ b) := @sup_ind (order_dual α) _ _ _ _ _ ha hb @[simp] lemma lt_inf_iff [is_total α (≤)] {a b c : α} : a < b ⊓ c ↔ a < b ∧ a < c := @sup_lt_iff (order_dual α) _ _ _ _ _ @[simp] lemma inf_le_iff [is_total α (≤)] {a b c : α} : b ⊓ c ≤ a ↔ b ≤ a ∨ c ≤ a := @le_sup_iff (order_dual α) _ _ _ _ _ @[simp] theorem inf_idem : a ⊓ a = a := @sup_idem (order_dual α) _ _ instance inf_is_idempotent : is_idempotent α (⊓) := ⟨@inf_idem _ _⟩ theorem inf_comm : a ⊓ b = b ⊓ a := @sup_comm (order_dual α) _ _ _ instance inf_is_commutative : is_commutative α (⊓) := ⟨@inf_comm _ _⟩ theorem inf_assoc : a ⊓ b ⊓ c = a ⊓ (b ⊓ c) := @sup_assoc (order_dual α) _ a b c instance inf_is_associative : is_associative α (⊓) := ⟨@inf_assoc _ _⟩ @[simp] lemma inf_left_idem : a ⊓ (a ⊓ b) = a ⊓ b := @sup_left_idem (order_dual α) _ a b @[simp] lemma inf_right_idem : (a ⊓ b) ⊓ b = a ⊓ b := @sup_right_idem (order_dual α) _ a b lemma inf_left_comm (a b c : α) : a ⊓ (b ⊓ c) = b ⊓ (a ⊓ c) := @sup_left_comm (order_dual α) _ a b c lemma forall_le_or_exists_lt_inf (a : α) : (∀b, a ≤ b) ∨ (∃b, b < a) := @forall_le_or_exists_lt_sup (order_dual α) _ a theorem semilattice_inf.ext_inf {α} {A B : semilattice_inf α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) (x y : α) : (by haveI := A; exact (x ⊓ y)) = x ⊓ y := eq_of_forall_le_iff $ λ c, by simp only [le_inf_iff]; rw [← H, @le_inf_iff α A, H, H] theorem semilattice_inf.ext {α} {A B : semilattice_inf α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : A = B := begin have := partial_order.ext H, have ss := funext (λ x, funext $ semilattice_inf.ext_inf H x), casesI A, casesI B, injection this; congr' end end semilattice_inf /-! ### Lattices -/ /-- A lattice is a join-semilattice which is also a meet-semilattice. -/ class lattice (α : Type u) extends semilattice_sup α, semilattice_inf α instance (α) [lattice α] : lattice (order_dual α) := { .. order_dual.semilattice_sup α, .. order_dual.semilattice_inf α } section lattice variables [lattice α] {a b c d : α} /- Distributivity laws -/ /- TODO: better names? -/ theorem sup_inf_le : a ⊔ (b ⊓ c) ≤ (a ⊔ b) ⊓ (a ⊔ c) := le_inf (sup_le_sup_left inf_le_left _) (sup_le_sup_left inf_le_right _) theorem le_inf_sup : (a ⊓ b) ⊔ (a ⊓ c) ≤ a ⊓ (b ⊔ c) := sup_le (inf_le_inf_left _ le_sup_left) (inf_le_inf_left _ le_sup_right) theorem inf_sup_self : a ⊓ (a ⊔ b) = a := by simp theorem sup_inf_self : a ⊔ (a ⊓ b) = a := by simp theorem lattice.ext {α} {A B : lattice α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : A = B := begin have SS : @lattice.to_semilattice_sup α A = @lattice.to_semilattice_sup α B := semilattice_sup.ext H, have II := semilattice_inf.ext H, casesI A, casesI B, injection SS; injection II; congr' end end lattice /-- A distributive lattice is a lattice that satisfies any of four equivalent distribution properties (of sup over inf or inf over sup, on the left or right). A classic example of a distributive lattice is the lattice of subsets of a set, and in fact this example is generic in the sense that every distributive lattice is realizable as a sublattice of a powerset lattice. -/ class distrib_lattice α extends lattice α := (le_sup_inf : ∀x y z : α, (x ⊔ y) ⊓ (x ⊔ z) ≤ x ⊔ (y ⊓ z)) section distrib_lattice variables [distrib_lattice α] {x y z : α} theorem le_sup_inf : ∀{x y z : α}, (x ⊔ y) ⊓ (x ⊔ z) ≤ x ⊔ (y ⊓ z) := distrib_lattice.le_sup_inf theorem sup_inf_left : x ⊔ (y ⊓ z) = (x ⊔ y) ⊓ (x ⊔ z) := le_antisymm sup_inf_le le_sup_inf theorem sup_inf_right : (y ⊓ z) ⊔ x = (y ⊔ x) ⊓ (z ⊔ x) := by simp only [sup_inf_left, λy:α, @sup_comm α _ y x, eq_self_iff_true] theorem inf_sup_left : x ⊓ (y ⊔ z) = (x ⊓ y) ⊔ (x ⊓ z) := calc x ⊓ (y ⊔ z) = (x ⊓ (x ⊔ z)) ⊓ (y ⊔ z) : by rw [inf_sup_self] ... = x ⊓ ((x ⊓ y) ⊔ z) : by simp only [inf_assoc, sup_inf_right, eq_self_iff_true] ... = (x ⊔ (x ⊓ y)) ⊓ ((x ⊓ y) ⊔ z) : by rw [sup_inf_self] ... = ((x ⊓ y) ⊔ x) ⊓ ((x ⊓ y) ⊔ z) : by rw [sup_comm] ... = (x ⊓ y) ⊔ (x ⊓ z) : by rw [sup_inf_left] instance (α : Type*) [distrib_lattice α] : distrib_lattice (order_dual α) := { le_sup_inf := assume x y z, le_of_eq inf_sup_left.symm, .. order_dual.lattice α } theorem inf_sup_right : (y ⊔ z) ⊓ x = (y ⊓ x) ⊔ (z ⊓ x) := by simp only [inf_sup_left, λy:α, @inf_comm α _ y x, eq_self_iff_true] lemma le_of_inf_le_sup_le (h₁ : x ⊓ z ≤ y ⊓ z) (h₂ : x ⊔ z ≤ y ⊔ z) : x ≤ y := calc x ≤ (y ⊓ z) ⊔ x : le_sup_right ... = (y ⊔ x) ⊓ (x ⊔ z) : by rw [sup_inf_right, @sup_comm _ _ x] ... ≤ (y ⊔ x) ⊓ (y ⊔ z) : inf_le_inf_left _ h₂ ... = y ⊔ (x ⊓ z) : sup_inf_left.symm ... ≤ y ⊔ (y ⊓ z) : sup_le_sup_left h₁ _ ... ≤ _ : sup_le (le_refl y) inf_le_left lemma eq_of_inf_eq_sup_eq {α : Type u} [distrib_lattice α] {a b c : α} (h₁ : b ⊓ a = c ⊓ a) (h₂ : b ⊔ a = c ⊔ a) : b = c := le_antisymm (le_of_inf_le_sup_le (le_of_eq h₁) (le_of_eq h₂)) (le_of_inf_le_sup_le (le_of_eq h₁.symm) (le_of_eq h₂.symm)) end distrib_lattice /-! ### Lattices derived from linear orders -/ @[priority 100] -- see Note [lower instance priority] instance lattice_of_decidable_linear_order {α : Type u} [o : decidable_linear_order α] : lattice α := { sup := max, le_sup_left := le_max_left, le_sup_right := le_max_right, sup_le := assume a b c, max_le, inf := min, inf_le_left := min_le_left, inf_le_right := min_le_right, le_inf := assume a b c, le_min, ..o } theorem sup_eq_max [decidable_linear_order α] {x y : α} : x ⊔ y = max x y := rfl theorem inf_eq_min [decidable_linear_order α] {x y : α} : x ⊓ y = min x y := rfl @[priority 100] -- see Note [lower instance priority] instance distrib_lattice_of_decidable_linear_order {α : Type u} [o : decidable_linear_order α] : distrib_lattice α := { le_sup_inf := assume a b c, match le_total b c with | or.inl h := inf_le_left_of_le $ sup_le_sup_left (le_inf (le_refl b) h) _ | or.inr h := inf_le_right_of_le $ sup_le_sup_left (le_inf h (le_refl c)) _ end, ..lattice_of_decidable_linear_order } instance nat.distrib_lattice : distrib_lattice ℕ := by apply_instance namespace monotone lemma le_map_sup [semilattice_sup α] [semilattice_sup β] {f : α → β} (h : monotone f) (x y : α) : f x ⊔ f y ≤ f (x ⊔ y) := sup_le (h le_sup_left) (h le_sup_right) lemma map_sup [semilattice_sup α] [is_total α (≤)] [semilattice_sup β] {f : α → β} (hf : monotone f) (x y : α) : f (x ⊔ y) = f x ⊔ f y := (is_total.total x y).elim (λ h : x ≤ y, by simp only [h, hf h, sup_of_le_right]) (λ h, by simp only [h, hf h, sup_of_le_left]) lemma map_inf_le [semilattice_inf α] [semilattice_inf β] {f : α → β} (h : monotone f) (x y : α) : f (x ⊓ y) ≤ f x ⊓ f y := le_inf (h inf_le_left) (h inf_le_right) lemma map_inf [semilattice_inf α] [is_total α (≤)] [semilattice_inf β] {f : α → β} (hf : monotone f) (x y : α) : f (x ⊓ y) = f x ⊓ f y := @monotone.map_sup (order_dual α) _ _ _ _ _ hf.order_dual x y end monotone namespace prod variables (α β) instance [has_sup α] [has_sup β] : has_sup (α × β) := ⟨λp q, ⟨p.1 ⊔ q.1, p.2 ⊔ q.2⟩⟩ instance [has_inf α] [has_inf β] : has_inf (α × β) := ⟨λp q, ⟨p.1 ⊓ q.1, p.2 ⊓ q.2⟩⟩ instance [semilattice_sup α] [semilattice_sup β] : semilattice_sup (α × β) := { sup_le := assume a b c h₁ h₂, ⟨sup_le h₁.1 h₂.1, sup_le h₁.2 h₂.2⟩, le_sup_left := assume a b, ⟨le_sup_left, le_sup_left⟩, le_sup_right := assume a b, ⟨le_sup_right, le_sup_right⟩, .. prod.partial_order α β, .. prod.has_sup α β } instance [semilattice_inf α] [semilattice_inf β] : semilattice_inf (α × β) := { le_inf := assume a b c h₁ h₂, ⟨le_inf h₁.1 h₂.1, le_inf h₁.2 h₂.2⟩, inf_le_left := assume a b, ⟨inf_le_left, inf_le_left⟩, inf_le_right := assume a b, ⟨inf_le_right, inf_le_right⟩, .. prod.partial_order α β, .. prod.has_inf α β } instance [lattice α] [lattice β] : lattice (α × β) := { .. prod.semilattice_inf α β, .. prod.semilattice_sup α β } instance [distrib_lattice α] [distrib_lattice β] : distrib_lattice (α × β) := { le_sup_inf := assume a b c, ⟨le_sup_inf, le_sup_inf⟩, .. prod.lattice α β } end prod
432d2eaa7e485bda0901fd53e7da607a19cf6457
01ae0d022f2e2fefdaaa898938c1ac1fbce3b3ab
/categories/products/bifunctors.lean
c3f43a44f057f08f160aa4bda6a7a71dcdef4e86
[]
no_license
PatrickMassot/lean-category-theory
0f56a83464396a253c28a42dece16c93baf8ad74
ef239978e91f2e1c3b8e88b6e9c64c155dc56c99
refs/heads/master
1,629,739,187,316
1,512,422,659,000
1,512,422,659,000
113,098,786
0
0
null
1,512,424,022,000
1,512,424,022,000
null
UTF-8
Lean
false
false
3,283
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Stephen Morgan, Scott Morrison import ..products open categories open categories.functor open categories.natural_transformation namespace categories.products @[simp] lemma {u1 v1 u2 v2 u3 v3} Bifunctor_identities { C : Category.{u1 v1} } { D : Category.{u2 v2} } { E : Category.{u3 v3} } ( F : Functor (C × D) E ) ( X : C.Obj ) ( Y : D.Obj ) : @Functor.onMorphisms _ _ F (X, Y) (X, Y) (C.identity X, D.identity Y) = E.identity (F.onObjects (X, Y)) := F.identities (X, Y) @[simp] lemma {u1 v1 u2 v2 u3 v3} Bifunctor_left_identity { C : Category.{u1 v1} } { D : Category.{u2 v2} } { E : Category.{u3 v3} } ( F : Functor (C × D) E ) ( W : C.Obj ) { X Y Z : D.Obj } ( f : D.Hom X Y ) ( g : D.Hom Y Z ) : @Functor.onMorphisms _ _ F (W, X) (W, Z) (C.identity W, D.compose f g) = E.compose (@Functor.onMorphisms _ _ F (W, X) (W, Y) (C.identity W, f)) (@Functor.onMorphisms _ _ F (W, Y) (W, Z) (C.identity W, g)) := begin have p := @Functor.functoriality _ _ F (W, X) (W, Y) (W, Z) (C.identity W, f) (C.identity W, g), tidy, end @[simp] lemma {u1 v1 u2 v2 u3 v3} Bifunctor_right_identity { C : Category.{u1 v1} } { D : Category.{u2 v2} } { E : Category.{u3 v3} } ( F : Functor (C × D) E ) ( X Y Z : C.Obj ) { W : D.Obj } ( f : C.Hom X Y ) ( g : C.Hom Y Z ) : @Functor.onMorphisms _ _ F (X, W) (Z, W) (C.compose f g, D.identity W) = E.compose (@Functor.onMorphisms _ _ F (X, W) (Y, W) (f, D.identity W)) (@Functor.onMorphisms _ _ F (Y, W) (Z, W) (g, D.identity W)) := begin have p := @Functor.functoriality _ _ F (X, W) (Y, W) (Z, W) (f, D.identity W) (g, D.identity W), tidy end open tactic -- set_option pp.all true @[simp] lemma {u1 v1 u2 v2 u3 v3} Bifunctor_diagonal_identities_1 { C : Category.{u1 v1} } { D : Category.{u2 v2} } { E : Category.{u3 v3} } ( F : Functor (C × D) E ) ( X X' : C.Obj ) ( f : C.Hom X X' ) ( Y Y' : D.Obj ) ( g : D.Hom Y Y' ) : E.compose (@Functor.onMorphisms _ _ F (X, Y) (X, Y') (C.identity X, g)) (@Functor.onMorphisms _ _ F (X, Y') (X', Y') (f, D.identity Y')) = @Functor.onMorphisms _ _ F (X, Y) (X', Y') (f, g) := begin -- PROJECT automation have p := eq.symm (@Functor.functoriality _ _ F (X, Y) (X, Y') (X', Y') (C.identity X, g) (f, D.identity Y')), dsimp [eq.mpr] {unfold_reducible := tt}, unfold_projs at * {md:=semireducible}, dsimp at p, simp at p, exact p, end @[simp] lemma {u1 v1 u2 v2 u3 v3} Bifunctor_diagonal_identities_2 { C : Category.{u1 v1} } { D : Category.{u2 v2} } { E : Category.{u3 v3} } ( F : Functor (C × D) E ) ( X X' : C.Obj ) ( f : C.Hom X X' ) ( Y Y' : D.Obj ) ( g : D.Hom Y Y' ) : E.compose (@Functor.onMorphisms _ _ F (X, Y) (X', Y) (f, D.identity Y)) (@Functor.onMorphisms _ _ F (X', Y) (X', Y') (C.identity X', g)) = @Functor.onMorphisms _ _ F (X, Y) (X', Y') (f, g) := begin have p := eq.symm (@Functor.functoriality _ _ F (X, Y) (X', Y) (X', Y') (f, D.identity Y) (C.identity X', g)), dsimp [eq.mpr] {unfold_reducible := tt}, unfold_projs at * {md:=semireducible}, dsimp at p, simp at p, exact p, end end categories.products
8302b92186ec03f508a31f5eaf70ac74c87bb0bb
f57749ca63d6416f807b770f67559503fdb21001
/hott/hit/susp.hlean
c06c9abcdad6972c90a8900b1be5e050b3a63dd6
[ "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
7,574
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 Declaration of suspension -/ import .pushout types.pointed types.cubical.square open pushout unit eq equiv equiv.ops definition susp (A : Type) : Type := pushout (λ(a : A), star) (λ(a : A), star) namespace susp variable {A : Type} definition north {A : Type} : susp A := inl _ _ star definition south {A : Type} : susp A := inr _ _ star definition merid (a : A) : @north A = @south A := glue _ _ a protected definition rec {P : susp A → Type} (PN : P north) (PS : P south) (Pm : Π(a : A), PN =[merid a] PS) (x : susp A) : P x := begin induction x with u u, { cases u, exact PN}, { cases u, exact PS}, { apply Pm}, end protected definition rec_on [reducible] {P : susp A → Type} (y : susp A) (PN : P north) (PS : P south) (Pm : Π(a : A), PN =[merid a] PS) : P y := susp.rec PN PS Pm y theorem rec_merid {P : susp A → Type} (PN : P north) (PS : P south) (Pm : Π(a : A), PN =[merid a] PS) (a : A) : apdo (susp.rec PN PS Pm) (merid a) = Pm a := !rec_glue protected definition elim {P : Type} (PN : P) (PS : P) (Pm : A → PN = PS) (x : susp A) : P := susp.rec PN PS (λa, pathover_of_eq (Pm a)) x protected definition elim_on [reducible] {P : Type} (x : susp A) (PN : P) (PS : P) (Pm : A → PN = PS) : P := susp.elim PN PS Pm x theorem elim_merid {P : Type} {PN PS : P} (Pm : A → PN = PS) (a : A) : ap (susp.elim PN PS Pm) (merid a) = Pm a := begin apply eq_of_fn_eq_fn_inv !(pathover_constant (merid a)), rewrite [▸*,-apdo_eq_pathover_of_eq_ap,↑susp.elim,rec_merid], end protected definition elim_type (PN : Type) (PS : Type) (Pm : A → PN ≃ PS) (x : susp A) : Type := susp.elim PN PS (λa, ua (Pm a)) x protected definition elim_type_on [reducible] (x : susp A) (PN : Type) (PS : Type) (Pm : A → PN ≃ PS) : Type := susp.elim_type PN PS Pm x theorem elim_type_merid (PN : Type) (PS : Type) (Pm : A → PN ≃ PS) (a : A) : transport (susp.elim_type PN PS Pm) (merid a) = Pm a := by rewrite [tr_eq_cast_ap_fn,↑susp.elim_type,elim_merid];apply cast_ua_fn end susp attribute susp.north susp.south [constructor] attribute susp.rec susp.elim [unfold 6] [recursor 6] attribute susp.elim_type [unfold 5] attribute susp.rec_on susp.elim_on [unfold 3] attribute susp.elim_type_on [unfold 2] namespace susp open pointed variables {X Y Z : Pointed} definition pointed_susp [instance] [constructor] (X : Type) : pointed (susp X) := pointed.mk north definition Susp [constructor] (X : Type) : Pointed := pointed.mk' (susp X) definition Susp_functor (f : X →* Y) : Susp X →* Susp Y := begin fconstructor, { intro x, induction x, apply north, apply south, exact merid (f a)}, { reflexivity} end definition Susp_functor_compose (g : Y →* Z) (f : X →* Y) : Susp_functor (g ∘* f) ~* Susp_functor g ∘* Susp_functor f := begin fconstructor, { intro a, induction a, { reflexivity}, { reflexivity}, { apply eq_pathover, apply hdeg_square, rewrite [▸*,ap_compose' _ (Susp_functor f),↑Susp_functor,+elim_merid]}}, { reflexivity} end -- adjunction from Coq-HoTT definition loop_susp_unit [constructor] (X : Pointed) : X →* Ω(Susp X) := begin fconstructor, { intro x, exact merid x ⬝ (merid pt)⁻¹}, { apply con.right_inv}, end definition loop_susp_unit_natural (f : X →* Y) : loop_susp_unit Y ∘* f ~* ap1 (Susp_functor f) ∘* loop_susp_unit X := begin induction X with X x, induction Y with Y y, induction f with f pf, esimp at *, induction pf, fconstructor, { intro x', esimp [Susp_functor], symmetry, exact !idp_con ⬝ (!ap_con ⬝ whisker_left _ !ap_inv) ⬝ (!elim_merid ◾ (inverse2 !elim_merid)) }, { rewrite [▸*,idp_con (con.right_inv _)], apply inv_con_eq_of_eq_con, refine _ ⬝ !con.assoc', rewrite inverse2_right_inv, refine _ ⬝ !con.assoc', rewrite [ap_con_right_inv], unfold Susp_functor, xrewrite [idp_con_idp,-ap_compose], }, end definition loop_susp_counit [constructor] (X : Pointed) : Susp (Ω X) →* X := begin fconstructor, { intro x, induction x, exact pt, exact pt, exact a}, { reflexivity}, end definition loop_susp_counit_natural (f : X →* Y) : f ∘* loop_susp_counit X ~* loop_susp_counit Y ∘* (Susp_functor (ap1 f)) := begin induction X with X x, induction Y with Y y, induction f with f pf, esimp at *, induction pf, fconstructor, { intro x', induction x' with p, { reflexivity}, { reflexivity}, { esimp, apply eq_pathover, apply hdeg_square, xrewrite [ap_compose f,ap_compose (susp.elim (f x) (f x) (λ (a : f x = f x), a)),▸*], xrewrite [+elim_merid,▸*,idp_con]}}, { reflexivity} end definition loop_susp_counit_unit (X : Pointed) : ap1 (loop_susp_counit X) ∘* loop_susp_unit (Ω X) ~* pid (Ω X) := begin induction X with X x, fconstructor, { intro p, esimp, refine !idp_con ⬝ (!ap_con ⬝ whisker_left _ !ap_inv) ⬝ (!elim_merid ◾ inverse2 !elim_merid)}, { rewrite [▸*,inverse2_right_inv (elim_merid function.id idp)], refine !con.assoc ⬝ _, xrewrite [ap_con_right_inv (susp.elim x x (λa, a)) (merid idp),idp_con_idp,-ap_compose]} end definition loop_susp_unit_counit (X : Pointed) : loop_susp_counit (Susp X) ∘* Susp_functor (loop_susp_unit X) ~* pid (Susp X) := begin induction X with X x, fconstructor, { intro x', induction x', { reflexivity}, { exact merid pt}, { apply eq_pathover, xrewrite [▸*, ap_id, ap_compose (susp.elim north north (λa, a)), +elim_merid,▸*], apply square_of_eq, exact !idp_con ⬝ !inv_con_cancel_right⁻¹}}, { reflexivity} end definition susp_adjoint_loop (X Y : Pointed) : map₊ (pointed.mk' (susp X)) Y ≃ map₊ X (Ω Y) := begin fapply equiv.MK, { intro f, exact ap1 f ∘* loop_susp_unit X}, { intro g, exact loop_susp_counit Y ∘* Susp_functor g}, { intro g, apply eq_of_phomotopy, esimp, refine !pwhisker_right !ap1_compose ⬝* _, refine !passoc ⬝* _, refine !pwhisker_left !loop_susp_unit_natural⁻¹* ⬝* _, refine !passoc⁻¹* ⬝* _, refine !pwhisker_right !loop_susp_counit_unit ⬝* _, apply pid_comp}, { intro f, apply eq_of_phomotopy, esimp, refine !pwhisker_left !Susp_functor_compose ⬝* _, refine !passoc⁻¹* ⬝* _, refine !pwhisker_right !loop_susp_counit_natural⁻¹* ⬝* _, refine !passoc ⬝* _, refine !pwhisker_left !loop_susp_unit_counit ⬝* _, apply comp_pid}, end definition susp_adjoint_loop_nat_right (f : Susp X →* Y) (g : Y →* Z) : susp_adjoint_loop X Z (g ∘* f) ~* ap1 g ∘* susp_adjoint_loop X Y f := begin esimp [susp_adjoint_loop], refine _ ⬝* !passoc, apply pwhisker_right, apply ap1_compose end definition susp_adjoint_loop_nat_left (f : Y →* Ω Z) (g : X →* Y) : (susp_adjoint_loop X Z)⁻¹ (f ∘* g) ~* (susp_adjoint_loop Y Z)⁻¹ f ∘* Susp_functor g := begin esimp [susp_adjoint_loop], refine _ ⬝* !passoc⁻¹*, apply pwhisker_left, apply Susp_functor_compose end end susp
d6c10d3f6fa3cf8d8ec1ea73736ca2af7e3ab57e
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/interactive/catHover.lean
7c3f523fa652f47c03fbc9628cebb8f8a66dc965
[ "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
497
lean
/-- Index syntax categoy -/ declare_syntax_cat index --v textDocument/hover syntax "foo " term prio index : term --^ textDocument/hover --^ textDocument/hover namespace Foo /-- Value syntax category -/ declare_syntax_cat value syntax "bla " value : term --^ textDocument/hover end Foo macro "boo " : term => `(0) --^ textDocument/hover syntax term " ++ " value : term --^ textDocument/hover
817dbf961486201c1273170033ba1ee67be817e2
c777c32c8e484e195053731103c5e52af26a25d1
/src/linear_algebra/bilinear_form/tensor_product.lean
97fdc9fcd996fe1d7696e5be11abd8dc2b19df46
[ "Apache-2.0" ]
permissive
kbuzzard/mathlib
2ff9e85dfe2a46f4b291927f983afec17e946eb8
58537299e922f9c77df76cb613910914a479c1f7
refs/heads/master
1,685,313,702,744
1,683,974,212,000
1,683,974,212,000
128,185,277
1
0
null
1,522,920,600,000
1,522,920,600,000
null
UTF-8
Lean
false
false
3,066
lean
/- Copyright (c) 2023 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import linear_algebra.bilinear_form import linear_algebra.tensor_product /-! # The bilinear form on a tensor product ## Main definitions * `bilin_form.tensor_distrib (B₁ ⊗ₜ B₂)`: the bilinear form on `M₁ ⊗ M₂` constructed by applying `B₁` on `M₁` and `B₂` on `M₂`. * `bilin_form.tensor_distrib_equiv`: `bilin_form.tensor_distrib` as an equivalence on finite free modules. -/ universes u v w variables {ι : Type*} {R : Type*} {M₁ M₂ : Type*} open_locale tensor_product namespace bilin_form section comm_semiring variables [comm_semiring R] variables [add_comm_monoid M₁] [add_comm_monoid M₂] variables [module R M₁] [module R M₂] /-- The tensor product of two bilinear forms injects into bilinear forms on tensor products. -/ def tensor_distrib : bilin_form R M₁ ⊗[R] bilin_form R M₂ →ₗ[R] bilin_form R (M₁ ⊗[R] M₂) := ((tensor_product.tensor_tensor_tensor_comm R _ _ _ _).dual_map ≪≫ₗ (tensor_product.lift.equiv R _ _ _).symm ≪≫ₗ linear_map.to_bilin).to_linear_map ∘ₗ tensor_product.dual_distrib R _ _ ∘ₗ (tensor_product.congr (bilin_form.to_lin ≪≫ₗ tensor_product.lift.equiv R _ _ _) (bilin_form.to_lin ≪≫ₗ tensor_product.lift.equiv R _ _ _)).to_linear_map @[simp] lemma tensor_distrib_tmul (B₁ : bilin_form R M₁) (B₂ : bilin_form R M₂) (m₁ : M₁) (m₂ : M₂) (m₁' : M₁) (m₂' : M₂) : tensor_distrib (B₁ ⊗ₜ B₂) (m₁ ⊗ₜ m₂) (m₁' ⊗ₜ m₂') = B₁ m₁ m₁' * B₂ m₂ m₂' := rfl /-- The tensor product of two bilinear forms, a shorthand for dot notation. -/ @[reducible] protected def tmul (B₁ : bilin_form R M₁) (B₂ : bilin_form R M₂) : bilin_form R (M₁ ⊗[R] M₂) := tensor_distrib (B₁ ⊗ₜ[R] B₂) end comm_semiring section comm_ring variables [comm_ring R] variables [add_comm_group M₁] [add_comm_group M₂] variables [module R M₁] [module R M₂] variables [module.free R M₁] [module.finite R M₁] variables [module.free R M₂] [module.finite R M₂] variables [nontrivial R] /-- `tensor_distrib` as an equivalence. -/ noncomputable def tensor_distrib_equiv : bilin_form R M₁ ⊗[R] bilin_form R M₂ ≃ₗ[R] bilin_form R (M₁ ⊗[R] M₂) := -- the same `linear_equiv`s as from `tensor_distrib`, but with the inner linear map also as an -- equiv tensor_product.congr (bilin_form.to_lin ≪≫ₗ tensor_product.lift.equiv R _ _ _) (bilin_form.to_lin ≪≫ₗ tensor_product.lift.equiv R _ _ _) ≪≫ₗ tensor_product.dual_distrib_equiv R (M₁ ⊗ M₁) (M₂ ⊗ M₂) ≪≫ₗ (tensor_product.tensor_tensor_tensor_comm R _ _ _ _).dual_map ≪≫ₗ (tensor_product.lift.equiv R _ _ _).symm ≪≫ₗ linear_map.to_bilin @[simp] lemma tensor_distrib_equiv_apply (B : bilin_form R M₁ ⊗ bilin_form R M₂) : tensor_distrib_equiv B = tensor_distrib B := rfl end comm_ring end bilin_form
8b71037b6be1cad2ca6f89beb9ba7177a0897653
367134ba5a65885e863bdc4507601606690974c1
/src/category_theory/monad/adjunction.lean
66c33af29f3aa9a1225ae38e86bcd333c3777234
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
6,330
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Bhavik Mehta -/ import category_theory.monad.algebra import category_theory.adjunction namespace category_theory open category universes v₁ v₂ u₁ u₂ -- declare the `v`'s first; see `category_theory.category` for an explanation variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D] variables (L : C ⥤ D) (R : D ⥤ C) namespace adjunction /-- For a pair of functors `L : C ⥤ D`, `R : D ⥤ C`, an adjunction `h : L ⊣ R` induces a monad on the category `C`. -/ @[simps] def to_monad {L : C ⥤ D} {R : D ⥤ C} (h : L ⊣ R) : monad C := { to_functor := L ⋙ R, η' := h.unit, μ' := whisker_right (whisker_left L h.counit) R, assoc' := λ X, by { dsimp, rw [←R.map_comp], simp }, right_unit' := λ X, by { dsimp, rw [←R.map_comp], simp } } /-- For a pair of functors `L : C ⥤ D`, `R : D ⥤ C`, an adjunction `h : L ⊣ R` induces a comonad on the category `D`. -/ @[simps] def to_comonad {L : C ⥤ D} {R : D ⥤ C} (h : L ⊣ R) : comonad D := { to_functor := R ⋙ L, ε' := h.counit, δ' := whisker_right (whisker_left R h.unit) L, coassoc' := λ X, by { dsimp, rw ← L.map_comp, simp }, right_counit' := λ X, by { dsimp, rw ← L.map_comp, simp } } end adjunction /-- Gven any adjunction `L ⊣ R`, there is a comparison functor `category_theory.monad.comparison R` sending objects `Y : D` to Eilenberg-Moore algebras for `L ⋙ R` with underlying object `R.obj X`. We later show that this is full when `R` is full, faithful when `R` is faithful, and essentially surjective when `R` is reflective. -/ @[simps] def monad.comparison {L : C ⥤ D} {R : D ⥤ C} (h : L ⊣ R) : D ⥤ h.to_monad.algebra := { obj := λ X, { A := R.obj X, a := R.map (h.counit.app X), assoc' := by { dsimp, rw [← R.map_comp, ← adjunction.counit_naturality, R.map_comp], refl } }, map := λ X Y f, { f := R.map f, h' := by { dsimp, rw [← R.map_comp, adjunction.counit_naturality, R.map_comp] } } }. /-- The underlying object of `(monad.comparison R).obj X` is just `R.obj X`. -/ @[simps] def monad.comparison_forget {L : C ⥤ D} {R : D ⥤ C} (h : L ⊣ R) : monad.comparison h ⋙ h.to_monad.forget ≅ R := { hom := { app := λ X, 𝟙 _, }, inv := { app := λ X, 𝟙 _, } } /-- Gven any adjunction `L ⊣ R`, there is a comparison functor `category_theory.comonad.comparison L` sending objects `X : C` to Eilenberg-Moore coalgebras for `L ⋙ R` with underlying object `L.obj X`. -/ @[simps] def comonad.comparison {L : C ⥤ D} {R : D ⥤ C} (h : L ⊣ R) : C ⥤ h.to_comonad.coalgebra := { obj := λ X, { A := L.obj X, a := L.map (h.unit.app X), coassoc' := by { dsimp, rw [← L.map_comp, ← adjunction.unit_naturality, L.map_comp], refl } }, map := λ X Y f, { f := L.map f, h' := by { dsimp, rw ← L.map_comp, simp } } } /-- The underlying object of `(comonad.comparison L).obj X` is just `L.obj X`. -/ @[simps] def comparison_forget {L : C ⥤ D} {R : D ⥤ C} (h : L ⊣ R) : comonad.comparison h ⋙ h.to_comonad.forget ≅ L := { hom := { app := λ X, 𝟙 _, }, inv := { app := λ X, 𝟙 _, } } /-- A right adjoint functor `R : D ⥤ C` is *monadic* if the comparison functor `monad.comparison R` from `D` to the category of Eilenberg-Moore algebras for the adjunction is an equivalence. -/ class monadic_right_adjoint (R : D ⥤ C) extends is_right_adjoint R := (eqv : is_equivalence (monad.comparison (adjunction.of_right_adjoint R))) /-- A left adjoint functor `L : C ⥤ D` is *comonadic* if the comparison functor `comonad.comparison L` from `C` to the category of Eilenberg-Moore algebras for the adjunction is an equivalence. -/ class comonadic_left_adjoint (L : C ⥤ D) extends is_left_adjoint L := (eqv : is_equivalence (comonad.comparison (adjunction.of_left_adjoint L))) -- TODO: This holds more generally for idempotent adjunctions, not just reflective adjunctions. instance μ_iso_of_reflective [reflective R] : is_iso (adjunction.of_right_adjoint R).to_monad.μ := by { dsimp, apply_instance } attribute [instance] monadic_right_adjoint.eqv attribute [instance] comonadic_left_adjoint.eqv namespace reflective instance [reflective R] (X : (adjunction.of_right_adjoint R).to_monad.algebra) : is_iso ((adjunction.of_right_adjoint R).unit.app X.A) := { inv := X.a, hom_inv_id' := X.unit, inv_hom_id' := begin dsimp only [functor.id_obj], rw ← (adjunction.of_right_adjoint R).unit_naturality, dsimp only [functor.comp_obj, adjunction.to_monad_coe], rw [unit_obj_eq_map_unit, ←functor.map_comp, ←functor.map_comp], erw X.unit, simp, end } instance comparison_ess_surj [reflective R] : ess_surj (monad.comparison (adjunction.of_right_adjoint R)) := begin refine ⟨λ X, ⟨(left_adjoint R).obj X.A, ⟨_⟩⟩⟩, symmetry, refine monad.algebra.iso_mk _ _, { exact as_iso ((adjunction.of_right_adjoint R).unit.app X.A) }, dsimp only [functor.comp_map, monad.comparison_obj_a, as_iso_hom, functor.comp_obj, monad.comparison_obj_A, monad_to_functor_eq_coe, adjunction.to_monad_coe], rw [←cancel_epi ((adjunction.of_right_adjoint R).unit.app X.A), adjunction.unit_naturality_assoc, adjunction.right_triangle_components, comp_id], apply (X.unit_assoc _).symm, end instance comparison_full [full R] [is_right_adjoint R] : full (monad.comparison (adjunction.of_right_adjoint R)) := { preimage := λ X Y f, R.preimage f.f } instance comparison_faithful [faithful R] [is_right_adjoint R] : faithful (monad.comparison (adjunction.of_right_adjoint R)) := { map_injective' := λ X Y f g w, by { have w' := congr_arg monad.algebra.hom.f w, exact R.map_injective w' } } end reflective -- It is possible to do this computably since the construction gives the data of the inverse, not -- just the existence of an inverse on each object. /-- Any reflective inclusion has a monadic right adjoint. cf Prop 5.3.3 of [Riehl][riehl2017] -/ @[priority 100] -- see Note [lower instance priority] noncomputable instance monadic_of_reflective [reflective R] : monadic_right_adjoint R := { eqv := equivalence.equivalence_of_fully_faithfully_ess_surj _ } end category_theory
87d7ac935cec612ad6d2632255f3276e9fcb35dc
19cc34575500ee2e3d4586c15544632aa07a8e66
/src/algebra/group_power/basic.lean
66ef70e5c96af2856b44c642f343f87ac97c54ad
[ "Apache-2.0" ]
permissive
LibertasSpZ/mathlib
b9fcd46625eb940611adb5e719a4b554138dade6
33f7870a49d7cc06d2f3036e22543e6ec5046e68
refs/heads/master
1,672,066,539,347
1,602,429,158,000
1,602,429,158,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
21,774
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Robert Y. Lewis -/ import algebra.order_functions import deprecated.group /-! # Power operations on monoids and groups The power operation on monoids and groups. We separate this from group, because it depends on `ℕ`, which in turn depends on other parts of algebra. This module contains the definitions of `monoid.pow` and `group.pow` and their additive counterparts `nsmul` and `gsmul`, along with a few lemmas. Further lemmas can be found in `algebra.group_power.lemmas`. ## Notation The class `has_pow α β` provides the notation `a^b` for powers. We define instances of `has_pow M ℕ`, for monoids `M`, and `has_pow G ℤ` for groups `G`. We also define infix operators `•ℕ` and `•ℤ` for scalar multiplication by a natural and an integer numbers, respectively. ## Implementation details We adopt the convention that `0^0 = 1`. This module provides the instance `has_pow ℕ ℕ` (via `monoid.has_pow`) and is imported by `data.nat.basic`, so it has to live low in the import hierarchy. Not all of its imports are needed yet; the intent is to move more lemmas here from `.lemmas` so that they are available in `data.nat.basic`, and the imports will be required then. -/ universes u v w x y z u₁ u₂ variables {M : Type u} {N : Type v} {G : Type w} {H : Type x} {A : Type y} {B : Type z} {R : Type u₁} {S : Type u₂} /-- The power operation in a monoid. `a^n = a*a*...*a` n times. -/ def monoid.pow [has_mul M] [has_one M] (a : M) : ℕ → M | 0 := 1 | (n+1) := a * monoid.pow n /-- The scalar multiplication in an additive monoid. `n •ℕ a = a+a+...+a` n times. -/ def nsmul [has_add A] [has_zero A] (n : ℕ) (a : A) : A := @monoid.pow (multiplicative A) _ _ a n infix ` •ℕ `:70 := nsmul instance monoid.has_pow [monoid M] : has_pow M ℕ := ⟨monoid.pow⟩ @[simp] lemma monoid.pow_eq_has_pow [monoid M] (a : M) (n : ℕ) : monoid.pow a n = a^n := rfl /-! ### Commutativity First we prove some facts about `semiconj_by` and `commute`. They do not require any theory about `pow` and/or `nsmul` and will be useful later in this file. -/ namespace semiconj_by variables [monoid M] @[simp] lemma pow_right {a x y : M} (h : semiconj_by a x y) (n : ℕ) : semiconj_by a (x^n) (y^n) := nat.rec_on n (one_right a) $ λ n ihn, h.mul_right ihn end semiconj_by namespace commute variables [monoid M] {a b : M} @[simp] theorem pow_right (h : commute a b) (n : ℕ) : commute a (b ^ n) := h.pow_right n @[simp] theorem pow_left (h : commute a b) (n : ℕ) : commute (a ^ n) b := (h.symm.pow_right n).symm @[simp] theorem pow_pow (h : commute a b) (m n : ℕ) : commute (a ^ m) (b ^ n) := (h.pow_left m).pow_right n @[simp] theorem self_pow (a : M) (n : ℕ) : commute a (a ^ n) := (commute.refl a).pow_right n @[simp] theorem pow_self (a : M) (n : ℕ) : commute (a ^ n) a := (commute.refl a).pow_left n @[simp] theorem pow_pow_self (a : M) (m n : ℕ) : commute (a ^ m) (a ^ n) := (commute.refl a).pow_pow m n end commute section monoid variables [monoid M] [monoid N] [add_monoid A] [add_monoid B] @[simp] theorem pow_zero (a : M) : a^0 = 1 := rfl @[simp] theorem zero_nsmul (a : A) : 0 •ℕ a = 0 := rfl theorem pow_succ (a : M) (n : ℕ) : a^(n+1) = a * a^n := rfl theorem succ_nsmul (a : A) (n : ℕ) : (n+1) •ℕ a = a + n •ℕ a := rfl theorem pow_two (a : M) : a^2 = a * a := show a*(a*1)=a*a, by rw mul_one theorem two_nsmul (a : A) : 2 •ℕ a = a + a := @pow_two (multiplicative A) _ a theorem pow_mul_comm' (a : M) (n : ℕ) : a^n * a = a * a^n := commute.pow_self a n theorem nsmul_add_comm' : ∀ (a : A) (n : ℕ), n •ℕ a + a = a + n •ℕ a := @pow_mul_comm' (multiplicative A) _ theorem pow_succ' (a : M) (n : ℕ) : a^(n+1) = a^n * a := by rw [pow_succ, pow_mul_comm'] theorem succ_nsmul' (a : A) (n : ℕ) : (n+1) •ℕ a = n •ℕ a + a := @pow_succ' (multiplicative A) _ _ _ theorem pow_add (a : M) (m n : ℕ) : a^(m + n) = a^m * a^n := by induction n with n ih; [rw [nat.add_zero, pow_zero, mul_one], rw [pow_succ', ← mul_assoc, ← ih, ← pow_succ', nat.add_assoc]] theorem add_nsmul : ∀ (a : A) (m n : ℕ), (m + n) •ℕ a = m •ℕ a + n •ℕ a := @pow_add (multiplicative A) _ @[simp] theorem pow_one (a : M) : a^1 = a := mul_one _ @[simp] theorem one_nsmul (a : A) : 1 •ℕ a = a := add_zero _ @[simp] lemma pow_ite (P : Prop) [decidable P] (a : M) (b c : ℕ) : a ^ (if P then b else c) = if P then a ^ b else a ^ c := by split_ifs; refl @[simp] lemma ite_pow (P : Prop) [decidable P] (a b : M) (c : ℕ) : (if P then a else b) ^ c = if P then a ^ c else b ^ c := by split_ifs; refl @[simp] lemma pow_boole (P : Prop) [decidable P] (a : M) : a ^ (if P then 1 else 0) = if P then a else 1 := by simp @[simp] theorem one_pow (n : ℕ) : (1 : M)^n = 1 := by induction n with n ih; [refl, rw [pow_succ, ih, one_mul]] @[simp] theorem nsmul_zero (n : ℕ) : n •ℕ (0 : A) = 0 := by induction n with n ih; [refl, rw [succ_nsmul, ih, zero_add]] theorem pow_mul (a : M) (m n : ℕ) : a^(m * n) = (a^m)^n := by induction n with n ih; [rw nat.mul_zero, rw [nat.mul_succ, pow_add, pow_succ', ih]]; refl theorem mul_nsmul' : ∀ (a : A) (m n : ℕ), m * n •ℕ a = n •ℕ (m •ℕ a) := @pow_mul (multiplicative A) _ theorem pow_mul' (a : M) (m n : ℕ) : a^(m * n) = (a^n)^m := by rw [nat.mul_comm, pow_mul] theorem mul_nsmul (a : A) (m n : ℕ) : m * n •ℕ a = m •ℕ (n •ℕ a) := @pow_mul' (multiplicative A) _ a m n theorem pow_mul_pow_sub (a : M) {m n : ℕ} (h : m ≤ n) : a ^ m * a ^ (n - m) = a ^ n := by rw [←pow_add, nat.add_comm, nat.sub_add_cancel h] theorem nsmul_add_sub_nsmul (a : A) {m n : ℕ} (h : m ≤ n) : (m •ℕ a) + ((n - m) •ℕ a) = n •ℕ a := @pow_mul_pow_sub (multiplicative A) _ _ _ _ h theorem pow_sub_mul_pow (a : M) {m n : ℕ} (h : m ≤ n) : a ^ (n - m) * a ^ m = a ^ n := by rw [←pow_add, nat.sub_add_cancel h] theorem sub_nsmul_nsmul_add (a : A) {m n : ℕ} (h : m ≤ n) : ((n - m) •ℕ a) + (m •ℕ a) = n •ℕ a := @pow_sub_mul_pow (multiplicative A) _ _ _ _ h theorem pow_bit0 (a : M) (n : ℕ) : a ^ bit0 n = a^n * a^n := pow_add _ _ _ theorem bit0_nsmul (a : A) (n : ℕ) : bit0 n •ℕ a = n •ℕ a + n •ℕ a := add_nsmul _ _ _ theorem pow_bit1 (a : M) (n : ℕ) : a ^ bit1 n = a^n * a^n * a := by rw [bit1, pow_succ', pow_bit0] theorem bit1_nsmul : ∀ (a : A) (n : ℕ), bit1 n •ℕ a = n •ℕ a + n •ℕ a + a := @pow_bit1 (multiplicative A) _ theorem pow_mul_comm (a : M) (m n : ℕ) : a^m * a^n = a^n * a^m := commute.pow_pow_self a m n theorem nsmul_add_comm : ∀ (a : A) (m n : ℕ), m •ℕ a + n •ℕ a = n •ℕ a + m •ℕ a := @pow_mul_comm (multiplicative A) _ theorem monoid_hom.map_pow (f : M →* N) (a : M) : ∀(n : ℕ), f (a ^ n) = (f a) ^ n | 0 := f.map_one | (n+1) := by rw [pow_succ, pow_succ, f.map_mul, monoid_hom.map_pow] theorem add_monoid_hom.map_nsmul (f : A →+ B) (a : A) (n : ℕ) : f (n •ℕ a) = n •ℕ f a := f.to_multiplicative.map_pow a n theorem is_monoid_hom.map_pow (f : M → N) [is_monoid_hom f] (a : M) : ∀(n : ℕ), f (a ^ n) = (f a) ^ n := (monoid_hom.of f).map_pow a theorem is_add_monoid_hom.map_nsmul (f : A → B) [is_add_monoid_hom f] (a : A) (n : ℕ) : f (n •ℕ a) = n •ℕ f a := (add_monoid_hom.of f).map_nsmul a n lemma commute.mul_pow {a b : M} (h : commute a b) (n : ℕ) : (a * b) ^ n = a ^ n * b ^ n := nat.rec_on n (by simp) $ λ n ihn, by simp only [pow_succ, ihn, ← mul_assoc, (h.pow_left n).right_comm] theorem neg_pow [ring R] (a : R) (n : ℕ) : (- a) ^ n = (-1) ^ n * a ^ n := (neg_one_mul a) ▸ (commute.neg_one_left a).mul_pow n end monoid /-! ### Commutative (additive) monoid -/ section comm_monoid variables [comm_monoid M] [add_comm_monoid A] theorem mul_pow (a b : M) (n : ℕ) : (a * b)^n = a^n * b^n := (commute.all a b).mul_pow n theorem nsmul_add : ∀ (a b : A) (n : ℕ), n •ℕ (a + b) = n •ℕ a + n •ℕ b := @mul_pow (multiplicative A) _ instance pow.is_monoid_hom (n : ℕ) : is_monoid_hom ((^ n) : M → M) := { map_mul := λ _ _, mul_pow _ _ _, map_one := one_pow _ } instance nsmul.is_add_monoid_hom (n : ℕ) : is_add_monoid_hom (nsmul n : A → A) := { map_add := λ _ _, nsmul_add _ _ _, map_zero := nsmul_zero _ } lemma dvd_pow {x y : M} : ∀ {n : ℕ} (hxy : x ∣ y) (hn : n ≠ 0), x ∣ y^n | 0 hxy hn := (hn rfl).elim | (n+1) hxy hn := by { rw [pow_succ], exact dvd_mul_of_dvd_left hxy _ } end comm_monoid section group variables [group G] [group H] [add_group A] [add_group B] open int /-- The power operation in a group. This extends `monoid.pow` to negative integers with the definition `a^(-n) = (a^n)⁻¹`. -/ def gpow (a : G) : ℤ → G | (of_nat n) := a^n | -[1+n] := (a^(nat.succ n))⁻¹ /-- The scalar multiplication by integers on an additive group. This extends `nsmul` to negative integers with the definition `(-n) •ℤ a = -(n •ℕ a)`. -/ def gsmul (n : ℤ) (a : A) : A := @gpow (multiplicative A) _ a n instance group.has_pow : has_pow G ℤ := ⟨gpow⟩ infix ` •ℤ `:70 := gsmul @[simp] lemma group.gpow_eq_has_pow (a : G) (n : ℤ) : gpow a n = a ^ n := rfl section nat @[simp] theorem inv_pow (a : G) (n : ℕ) : (a⁻¹)^n = (a^n)⁻¹ := by induction n with n ih; [exact one_inv.symm, rw [pow_succ', pow_succ, ih, mul_inv_rev]] @[simp] theorem neg_nsmul : ∀ (a : A) (n : ℕ), n •ℕ (-a) = -(n •ℕ a) := @inv_pow (multiplicative A) _ theorem pow_sub (a : G) {m n : ℕ} (h : n ≤ m) : a^(m - n) = a^m * (a^n)⁻¹ := have h1 : m - n + n = m, from nat.sub_add_cancel h, have h2 : a^(m - n) * a^n = a^m, by rw [←pow_add, h1], eq_mul_inv_of_mul_eq h2 theorem nsmul_sub : ∀ (a : A) {m n : ℕ}, n ≤ m → (m - n) •ℕ a = m •ℕ a - n •ℕ a := @pow_sub (multiplicative A) _ theorem pow_inv_comm (a : G) (m n : ℕ) : (a⁻¹)^m * a^n = a^n * (a⁻¹)^m := (commute.refl a).inv_left.pow_pow m n theorem nsmul_neg_comm : ∀ (a : A) (m n : ℕ), m •ℕ (-a) + n •ℕ a = n •ℕ a + m •ℕ (-a) := @pow_inv_comm (multiplicative A) _ end nat @[simp] theorem gpow_coe_nat (a : G) (n : ℕ) : a ^ (n:ℤ) = a ^ n := rfl @[simp] theorem gsmul_coe_nat (a : A) (n : ℕ) : n •ℤ a = n •ℕ a := rfl theorem gpow_of_nat (a : G) (n : ℕ) : a ^ of_nat n = a ^ n := rfl theorem gsmul_of_nat (a : A) (n : ℕ) : of_nat n •ℤ a = n •ℕ a := rfl @[simp] theorem gpow_neg_succ_of_nat (a : G) (n : ℕ) : a ^ -[1+n] = (a ^ n.succ)⁻¹ := rfl @[simp] theorem gsmul_neg_succ_of_nat (a : A) (n : ℕ) : -[1+n] •ℤ a = - (n.succ •ℕ a) := rfl @[simp] theorem gpow_zero (a : G) : a ^ (0:ℤ) = 1 := rfl @[simp] theorem zero_gsmul (a : A) : (0:ℤ) •ℤ a = 0 := rfl @[simp] theorem gpow_one (a : G) : a ^ (1:ℤ) = a := pow_one a @[simp] theorem one_gsmul (a : A) : (1:ℤ) •ℤ a = a := add_zero _ @[simp] theorem one_gpow : ∀ (n : ℤ), (1 : G) ^ n = 1 | (n : ℕ) := one_pow _ | -[1+ n] := show _⁻¹=(1:G), by rw [one_pow, one_inv] @[simp] theorem gsmul_zero : ∀ (n : ℤ), n •ℤ (0 : A) = 0 := @one_gpow (multiplicative A) _ @[simp] theorem gpow_neg (a : G) : ∀ (n : ℤ), a ^ -n = (a ^ n)⁻¹ | (n+1:ℕ) := rfl | 0 := one_inv.symm | -[1+ n] := (inv_inv _).symm lemma mul_gpow_neg_one (a b : G) : (a*b)^(-(1:ℤ)) = b^(-(1:ℤ))*a^(-(1:ℤ)) := by simp only [mul_inv_rev, gpow_one, gpow_neg] @[simp] theorem neg_gsmul : ∀ (a : A) (n : ℤ), -n •ℤ a = -(n •ℤ a) := @gpow_neg (multiplicative A) _ theorem gpow_neg_one (x : G) : x ^ (-1:ℤ) = x⁻¹ := congr_arg has_inv.inv $ pow_one x theorem neg_one_gsmul (x : A) : (-1:ℤ) •ℤ x = -x := congr_arg has_neg.neg $ one_nsmul x theorem inv_gpow (a : G) : ∀n:ℤ, a⁻¹ ^ n = (a ^ n)⁻¹ | (n : ℕ) := inv_pow a n | -[1+ n] := congr_arg has_inv.inv $ inv_pow a (n+1) theorem gsmul_neg (a : A) (n : ℤ) : gsmul n (- a) = - gsmul n a := @inv_gpow (multiplicative A) _ a n theorem commute.mul_gpow {a b : G} (h : commute a b) : ∀ n : ℤ, (a * b) ^ n = a ^ n * b ^ n | (n : ℕ) := h.mul_pow n | -[1+n] := by simp [h.mul_pow, (h.pow_pow n.succ n.succ).inv_inv.symm.eq] end group section comm_group variables [comm_group G] [add_comm_group A] theorem mul_gpow (a b : G) (n : ℤ) : (a * b)^n = a^n * b^n := (commute.all a b).mul_gpow n theorem gsmul_add : ∀ (a b : A) (n : ℤ), n •ℤ (a + b) = n •ℤ a + n •ℤ b := @mul_gpow (multiplicative A) _ theorem gsmul_sub (a b : A) (n : ℤ) : gsmul n (a - b) = gsmul n a - gsmul n b := by simp only [gsmul_add, gsmul_neg, sub_eq_add_neg] instance gpow.is_group_hom (n : ℤ) : is_group_hom ((^ n) : G → G) := { map_mul := λ _ _, mul_gpow _ _ n } instance gsmul.is_add_group_hom (n : ℤ) : is_add_group_hom (gsmul n : A → A) := { map_add := λ _ _, gsmul_add _ _ n } end comm_group lemma zero_pow [monoid_with_zero R] : ∀ {n : ℕ}, 0 < n → (0 : R) ^ n = 0 | (n+1) _ := zero_mul _ namespace ring_hom variables [semiring R] [semiring S] @[simp] lemma map_pow (f : R →+* S) (a) : ∀ n : ℕ, f (a ^ n) = (f a) ^ n := f.to_monoid_hom.map_pow a end ring_hom theorem neg_one_pow_eq_or [ring R] : ∀ n : ℕ, (-1 : R)^n = 1 ∨ (-1 : R)^n = -1 | 0 := or.inl rfl | (n+1) := (neg_one_pow_eq_or n).swap.imp (λ h, by rw [pow_succ, h, neg_one_mul, neg_neg]) (λ h, by rw [pow_succ, h, mul_one]) lemma pow_dvd_pow [monoid R] (a : R) {m n : ℕ} (h : m ≤ n) : a ^ m ∣ a ^ n := ⟨a ^ (n - m), by rw [← pow_add, nat.add_comm, nat.sub_add_cancel h]⟩ theorem pow_dvd_pow_of_dvd [comm_monoid R] {a b : R} (h : a ∣ b) : ∀ n : ℕ, a ^ n ∣ b ^ n | 0 := dvd_refl _ | (n+1) := mul_dvd_mul h (pow_dvd_pow_of_dvd n) lemma pow_two_sub_pow_two {R : Type*} [comm_ring R] (a b : R) : a ^ 2 - b ^ 2 = (a + b) * (a - b) := by simp only [pow_two, mul_sub, add_mul, sub_sub, add_sub, mul_comm, sub_add_cancel] lemma eq_or_eq_neg_of_pow_two_eq_pow_two [integral_domain R] (a b : R) (h : a ^ 2 = b ^ 2) : a = b ∨ a = -b := by rwa [← add_eq_zero_iff_eq_neg, ← sub_eq_zero, or_comm, ← mul_eq_zero, ← pow_two_sub_pow_two a b, sub_eq_zero] theorem sq_sub_sq [comm_ring R] (a b : R) : a ^ 2 - b ^ 2 = (a + b) * (a - b) := by rw [pow_two, pow_two, mul_self_sub_mul_self] theorem pow_eq_zero [monoid_with_zero R] [no_zero_divisors R] {x : R} {n : ℕ} (H : x^n = 0) : x = 0 := begin induction n with n ih, { rw pow_zero at H, rw [← mul_one x, H, mul_zero] }, exact or.cases_on (mul_eq_zero.1 H) id ih end @[field_simps] theorem pow_ne_zero [monoid_with_zero R] [no_zero_divisors R] {a : R} (n : ℕ) (h : a ≠ 0) : a ^ n ≠ 0 := mt pow_eq_zero h lemma pow_abs [decidable_linear_ordered_comm_ring R] (a : R) (n : ℕ) : (abs a)^n = abs (a^n) := by induction n with n ih; [exact (abs_one).symm, rw [pow_succ, pow_succ, ih, abs_mul]] lemma abs_neg_one_pow [decidable_linear_ordered_comm_ring R] (n : ℕ) : abs ((-1 : R)^n) = 1 := by rw [←pow_abs, abs_neg, abs_one, one_pow] section add_monoid variable [ordered_add_comm_monoid A] theorem nsmul_nonneg {a : A} (H : 0 ≤ a) : ∀ n : ℕ, 0 ≤ n •ℕ a | 0 := le_refl _ | (n+1) := add_nonneg H (nsmul_nonneg n) lemma nsmul_pos {a : A} (ha : 0 < a) {k : ℕ} (hk : 0 < k) : 0 < k •ℕ a := begin rcases nat.exists_eq_succ_of_ne_zero (ne_of_gt hk) with ⟨l, rfl⟩, clear hk, induction l with l IH, { simpa using ha }, { exact add_pos ha IH } end theorem nsmul_le_nsmul {a : A} {n m : ℕ} (ha : 0 ≤ a) (h : n ≤ m) : n •ℕ a ≤ m •ℕ a := let ⟨k, hk⟩ := nat.le.dest h in calc n •ℕ a = n •ℕ a + 0 : (add_zero _).symm ... ≤ n •ℕ a + k •ℕ a : add_le_add_left (nsmul_nonneg ha _) _ ... = m •ℕ a : by rw [← hk, add_nsmul] lemma nsmul_le_nsmul_of_le_right {a b : A} (hab : a ≤ b) : ∀ i : ℕ, i •ℕ a ≤ i •ℕ b | 0 := by simp | (k+1) := add_le_add hab (nsmul_le_nsmul_of_le_right _) end add_monoid section add_group variable [ordered_add_comm_group A] theorem gsmul_nonneg {a : A} (H : 0 ≤ a) {n : ℤ} (hn : 0 ≤ n) : 0 ≤ n •ℤ a := begin lift n to ℕ using hn, apply nsmul_nonneg H end end add_group section cancel_add_monoid variable [ordered_cancel_add_comm_monoid A] theorem nsmul_lt_nsmul {a : A} {n m : ℕ} (ha : 0 < a) (h : n < m) : n •ℕ a < m •ℕ a := let ⟨k, hk⟩ := nat.le.dest h in begin have succ_swap : n.succ + k = n + k.succ := nat.succ_add n k, calc n •ℕ a = (n •ℕ a : A) + (0 : A) : (add_zero _).symm ... < n •ℕ a + (k.succ •ℕ a : A) : add_lt_add_left (nsmul_pos ha (nat.succ_pos k)) _ ... = m •ℕ a : by rw [← hk, succ_swap, add_nsmul] end end cancel_add_monoid section comm_semiring variables [comm_semiring R] lemma min_pow_dvd_add {n m : ℕ} {a b c : R} (ha : c ^ n ∣ a) (hb : c ^ m ∣ b) : c ^ (min n m) ∣ a + b := begin replace ha := dvd.trans (pow_dvd_pow c (min_le_left n m)) ha, replace hb := dvd.trans (pow_dvd_pow c (min_le_right n m)) hb, exact dvd_add ha hb end end comm_semiring namespace canonically_ordered_semiring variable [canonically_ordered_comm_semiring R] theorem pow_pos {a : R} (H : 0 < a) : ∀ n : ℕ, 0 < a ^ n | 0 := canonically_ordered_semiring.zero_lt_one | (n+1) := canonically_ordered_semiring.mul_pos.2 ⟨H, pow_pos n⟩ lemma pow_le_pow_of_le_left {a b : R} (hab : a ≤ b) : ∀ i : ℕ, a^i ≤ b^i | 0 := by simp | (k+1) := canonically_ordered_semiring.mul_le_mul hab (pow_le_pow_of_le_left k) theorem one_le_pow_of_one_le {a : R} (H : 1 ≤ a) (n : ℕ) : 1 ≤ a ^ n := by simpa only [one_pow] using pow_le_pow_of_le_left H n theorem pow_le_one {a : R} (H : a ≤ 1) (n : ℕ) : a ^ n ≤ 1:= by simpa only [one_pow] using pow_le_pow_of_le_left H n end canonically_ordered_semiring section linear_ordered_semiring variable [linear_ordered_semiring R] @[simp] theorem pow_pos {a : R} (H : 0 < a) : ∀ (n : ℕ), 0 < a ^ n | 0 := zero_lt_one | (n+1) := mul_pos H (pow_pos _) @[simp] theorem pow_nonneg {a : R} (H : 0 ≤ a) : ∀ (n : ℕ), 0 ≤ a ^ n | 0 := zero_le_one | (n+1) := mul_nonneg H (pow_nonneg _) theorem pow_lt_pow_of_lt_left {x y : R} {n : ℕ} (Hxy : x < y) (Hxpos : 0 ≤ x) (Hnpos : 0 < n) : x ^ n < y ^ n := begin cases lt_or_eq_of_le Hxpos, { rw ←nat.sub_add_cancel Hnpos, induction (n - 1), { simpa only [pow_one] }, rw [pow_add, pow_add, nat.succ_eq_add_one, pow_one, pow_one], apply mul_lt_mul ih (le_of_lt Hxy) h (le_of_lt (pow_pos (lt_trans h Hxy) _)) }, { rw [←h, zero_pow Hnpos], apply pow_pos (by rwa ←h at Hxy : 0 < y),} end theorem pow_left_inj {x y : R} {n : ℕ} (Hxpos : 0 ≤ x) (Hypos : 0 ≤ y) (Hnpos : 0 < n) (Hxyn : x ^ n = y ^ n) : x = y := begin rcases lt_trichotomy x y with hxy | rfl | hyx, { exact absurd Hxyn (ne_of_lt (pow_lt_pow_of_lt_left hxy Hxpos Hnpos)) }, { refl }, { exact absurd Hxyn (ne_of_gt (pow_lt_pow_of_lt_left hyx Hypos Hnpos)) }, end theorem one_le_pow_of_one_le {a : R} (H : 1 ≤ a) : ∀ (n : ℕ), 1 ≤ a ^ n | 0 := le_refl _ | (n+1) := by simpa only [mul_one] using mul_le_mul H (one_le_pow_of_one_le n) zero_le_one (le_trans zero_le_one H) theorem pow_le_pow {a : R} {n m : ℕ} (ha : 1 ≤ a) (h : n ≤ m) : a ^ n ≤ a ^ m := let ⟨k, hk⟩ := nat.le.dest h in calc a ^ n = a ^ n * 1 : (mul_one _).symm ... ≤ a ^ n * a ^ k : mul_le_mul_of_nonneg_left (one_le_pow_of_one_le ha _) (pow_nonneg (le_trans zero_le_one ha) _) ... = a ^ m : by rw [←hk, pow_add] lemma pow_lt_pow {a : R} {n m : ℕ} (h : 1 < a) (h2 : n < m) : a ^ n < a ^ m := begin have h' : 1 ≤ a := le_of_lt h, have h'' : 0 < a := lt_trans zero_lt_one h, cases m, cases h2, rw [pow_succ, ←one_mul (a ^ n)], exact mul_lt_mul h (pow_le_pow h' (nat.le_of_lt_succ h2)) (pow_pos h'' _) (le_of_lt h'') end lemma pow_le_pow_of_le_left {a b : R} (ha : 0 ≤ a) (hab : a ≤ b) : ∀ i : ℕ, a^i ≤ b^i | 0 := by simp | (k+1) := mul_le_mul hab (pow_le_pow_of_le_left _) (pow_nonneg ha _) (le_trans ha hab) lemma lt_of_pow_lt_pow {a b : R} (n : ℕ) (hb : 0 ≤ b) (h : a ^ n < b ^ n) : a < b := lt_of_not_ge $ λ hn, not_lt_of_ge (pow_le_pow_of_le_left hb hn _) h end linear_ordered_semiring theorem pow_two_nonneg [linear_ordered_ring R] (a : R) : 0 ≤ a ^ 2 := by { rw pow_two, exact mul_self_nonneg _ } theorem pow_two_pos_of_ne_zero [linear_ordered_ring R] (a : R) (h : a ≠ 0) : 0 < a ^ 2 := lt_of_le_of_ne (pow_two_nonneg a) (pow_ne_zero 2 h).symm @[simp] lemma neg_square {α} [ring α] (z : α) : (-z)^2 = z^2 := by simp [pow, monoid.pow] lemma of_add_nsmul [add_monoid A] (x : A) (n : ℕ) : multiplicative.of_add (n •ℕ x) = (multiplicative.of_add x)^n := rfl lemma of_add_gsmul [add_group A] (x : A) (n : ℤ) : multiplicative.of_add (n •ℤ x) = (multiplicative.of_add x)^n := rfl @[simp] lemma semiconj_by.gpow_right [group G] {a x y : G} (h : semiconj_by a x y) : ∀ m : ℤ, semiconj_by a (x^m) (y^m) | (n : ℕ) := h.pow_right n | -[1+n] := (h.pow_right n.succ).inv_right namespace commute variables [group G] {a b : G} @[simp] lemma gpow_right (h : commute a b) (m : ℤ) : commute a (b^m) := h.gpow_right m @[simp] lemma gpow_left (h : commute a b) (m : ℤ) : commute (a^m) b := (h.symm.gpow_right m).symm lemma gpow_gpow (h : commute a b) (m n : ℤ) : commute (a^m) (b^n) := (h.gpow_left m).gpow_right n variables (a) (m n : ℕ) @[simp] theorem self_gpow : commute a (a ^ n) := (commute.refl a).gpow_right n @[simp] theorem gpow_self : commute (a ^ n) a := (commute.refl a).gpow_left n @[simp] theorem gpow_gpow_self : commute (a ^ m) (a ^ n) := (commute.refl a).gpow_gpow m n end commute
74ff0edf9f88020637f189eaa3d27382503dbe83
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/pack_unpack1.lean
01e3191c00590553438a07a00dab64b6319152ef
[ "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
2,295
lean
inductive {u} tree_core (A : Type u) : bool → (Type u) | leaf' : A → tree_core ff | node' : tree_core tt → tree_core ff | nil' {} : tree_core tt | cons' : tree_core ff → tree_core tt → tree_core tt attribute [reducible] definition tree (A : Sort*) := tree_core A ff attribute [reducible] definition tree_list (A : Sort*) := tree_core A tt open tree_core definition pack {A : Sort*} : list (tree A) → tree_core A tt | [] := nil' | (a::l) := cons' a (pack l) definition unpack {A : Sort*} : ∀ {b}, tree_core A b → list (tree A) | .(tt) nil' := [] | .(tt) (cons' a t) := a :: unpack t | .(ff) (leaf' a) := [] | .(ff) (node' l) := [] attribute [inverse] lemma unpack_pack {A : Sort*} : ∀ (l : list (tree A)), unpack (pack l) = l | [] := rfl | (a::l) := show a :: unpack (pack l) = a :: l, from congr_arg (λ x, a :: x) (unpack_pack l) attribute [inverse] lemma pack_unpack {A : Sort*} : ∀ t : tree_core A tt, pack (unpack t) = t := λ t, @tree_core.rec_on A (λ b, bool.cases_on b (λ t, true) (λ t, pack (unpack t) = t)) tt t (λ a, trivial) (λ t ih, trivial) rfl (λ h t ih1 ih2, show cons' h (pack (unpack t)) = cons' h t, from congr_arg (λ x, cons' h x) ih2) attribute [pattern] definition tree.node {A : Sort*} (l : list (tree A)) : tree A := tree_core.node' (pack l) attribute [pattern] definition tree.leaf {A : Sort*} : A → tree A := tree_core.leaf' set_option trace.eqn_compiler true definition sz {A : Sort*} : tree A → nat | (tree.leaf a) := 1 | (tree.node l) := list.length l + 1 constant P {A : Sort*} : tree A → Type 1 constant mk1 {A : Sort*} (a : A) : P (tree.leaf a) constant mk2 {A : Sort*} (l : list (tree A)) : P (tree.node l) noncomputable definition bla {A : Sort*} : ∀ n : tree A, P n | (tree.leaf a) := mk1 a | (tree.node l) := mk2 l #check bla._main.equations._eqn_1 #check bla._main.equations._eqn_2 definition foo {A : Sort*} : nat → tree A → nat | 0 _ := 0 | (n+1) (tree.leaf a) := 0 | (n+1) (tree.node []) := foo n (tree.node []) | (n+1) (tree.node (x::xs)) := foo n x #check @foo._main.equations._eqn_1 #check @foo._main.equations._eqn_2 #check @foo._main.equations._eqn_3 #check @foo._main.equations._eqn_4
eeea646226dc6bc3d943b9dde1f2d67c6640eac2
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/data/complex/exponential.lean
7ff1c9de3b19a33f210e6e6aaa1ae50d392121a0
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
47,448
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir -/ import algebra.archimedean import data.nat.choose data.complex.basic import tactic.linarith local attribute [instance, priority 0] classical.prop_decidable local notation `abs'` := _root_.abs open is_absolute_value section open real is_absolute_value finset lemma forall_ge_le_of_forall_le_succ {α : Type*} [preorder α] (f : ℕ → α) {m : ℕ} (h : ∀ n ≥ m, f n.succ ≤ f n) : ∀ {l}, ∀ k ≥ m, k ≤ l → f l ≤ f k := begin assume l k hkm hkl, generalize hp : l - k = p, have : l = k + p := add_comm p k ▸ (nat.sub_eq_iff_eq_add hkl).1 hp, subst this, clear hkl hp, induction p with p ih, { simp }, { exact le_trans (h _ (le_trans hkm (nat.le_add_right _ _))) ih } end variables {α : Type*} {β : Type*} [ring β] [discrete_linear_ordered_field α] [archimedean α] {abv : β → α} [is_absolute_value abv] lemma is_cau_of_decreasing_bounded (f : ℕ → α) {a : α} {m : ℕ} (ham : ∀ n ≥ m, abs (f n) ≤ a) (hnm : ∀ n ≥ m, f n.succ ≤ f n) : is_cau_seq abs f := λ ε ε0, let ⟨k, hk⟩ := archimedean.arch a ε0 in have h : ∃ l, ∀ n ≥ m, a - add_monoid.smul l ε < f n := ⟨k + k + 1, λ n hnm, lt_of_lt_of_le (show a - add_monoid.smul (k + (k + 1)) ε < -abs (f n), from lt_neg.1 $ lt_of_le_of_lt (ham n hnm) (begin rw [neg_sub, lt_sub_iff_add_lt, add_monoid.add_smul], exact add_lt_add_of_le_of_lt hk (lt_of_le_of_lt hk (lt_add_of_pos_left _ ε0)), end)) (neg_le.2 $ (abs_neg (f n)) ▸ le_abs_self _)⟩, let l := nat.find h in have hl : ∀ (n : ℕ), n ≥ m → f n > a - add_monoid.smul l ε := nat.find_spec h, have hl0 : l ≠ 0 := λ hl0, not_lt_of_ge (ham m (le_refl _)) (lt_of_lt_of_le (by have := hl m (le_refl m); simpa [hl0] using this) (le_abs_self (f m))), begin cases classical.not_forall.1 (nat.find_min h (nat.pred_lt hl0)) with i hi, rw [not_imp, not_lt] at hi, existsi i, assume j hj, have hfij : f j ≤ f i := forall_ge_le_of_forall_le_succ f hnm _ hi.1 hj, rw [abs_of_nonpos (sub_nonpos.2 hfij), neg_sub, sub_lt_iff_lt_add'], exact calc f i ≤ a - add_monoid.smul (nat.pred l) ε : hi.2 ... = a - add_monoid.smul l ε + ε : by conv {to_rhs, rw [← nat.succ_pred_eq_of_pos (nat.pos_of_ne_zero hl0), succ_smul', sub_add, add_sub_cancel] } ... < f j + ε : add_lt_add_right (hl j (le_trans hi.1 hj)) _ end lemma is_cau_of_mono_bounded (f : ℕ → α) {a : α} {m : ℕ} (ham : ∀ n ≥ m, abs (f n) ≤ a) (hnm : ∀ n ≥ m, f n ≤ f n.succ) : is_cau_seq abs f := begin refine @eq.rec_on (ℕ → α) _ (is_cau_seq abs) _ _ (-⟨_, @is_cau_of_decreasing_bounded _ _ _ (λ n, -f n) a m (by simpa) (by simpa)⟩ : cau_seq α abs).2, ext, exact neg_neg _ end lemma is_cau_series_of_abv_le_cau {f : ℕ → β} {g : ℕ → α} (n : ℕ) : (∀ m, n ≤ m → abv (f m) ≤ g m) → is_cau_seq abs (λ n, (range n).sum g) → is_cau_seq abv (λ n, (range n).sum f) := begin assume hm hg ε ε0, cases hg (ε / 2) (div_pos ε0 (by norm_num)) with i hi, existsi max n i, assume j ji, have hi₁ := hi j (le_trans (le_max_right n i) ji), have hi₂ := hi (max n i) (le_max_right n i), have sub_le := abs_sub_le ((range j).sum g) ((range i).sum g) ((range (max n i)).sum g), have := add_lt_add hi₁ hi₂, rw [abs_sub ((range (max n i)).sum g), add_halves ε] at this, refine lt_of_le_of_lt (le_trans (le_trans _ (le_abs_self _)) sub_le) this, generalize hk : j - max n i = k, clear this hi₂ hi₁ hi ε0 ε hg sub_le, rw nat.sub_eq_iff_eq_add ji at hk, rw hk, clear hk ji j, induction k with k' hi, { simp [abv_zero abv] }, { dsimp at *, rw [nat.succ_add, sum_range_succ, sum_range_succ, add_assoc, add_assoc], refine le_trans (abv_add _ _ _) _, exact add_le_add (hm _ (le_add_of_nonneg_of_le (nat.zero_le _) (le_max_left _ _))) hi }, end lemma is_cau_series_of_abv_cau {f : ℕ → β} : is_cau_seq abs (λ m, (range m).sum (λ n, abv (f n))) → is_cau_seq abv (λ m, (range m).sum f) := is_cau_series_of_abv_le_cau 0 (λ n h, le_refl _) lemma is_cau_geo_series {β : Type*} [field β] {abv : β → α} [is_absolute_value abv] (x : β) (hx1 : abv x < 1) : is_cau_seq abv (λ n, (range n).sum (λ m, x ^ m)) := have hx1' : abv x ≠ 1 := λ h, by simpa [h, lt_irrefl] using hx1, is_cau_series_of_abv_cau begin simp only [abv_pow abv, geom_sum hx1'] {eta := ff}, conv in (_ / _) { rw [← neg_div_neg_eq, neg_sub, neg_sub] }, refine @is_cau_of_mono_bounded _ _ _ _ ((1 : α) / (1 - abv x)) 0 _ _, { assume n hn, rw abs_of_nonneg, refine div_le_div_of_le_of_pos (sub_le_self _ (abv_pow abv x n ▸ abv_nonneg _ _)) (sub_pos.2 hx1), refine div_nonneg (sub_nonneg.2 _) (sub_pos.2 hx1), clear hn, induction n with n ih, { simp }, { rw [_root_.pow_succ, ← one_mul (1 : α)], refine mul_le_mul (le_of_lt hx1) ih (abv_pow abv x n ▸ abv_nonneg _ _) (by norm_num) } }, { assume n hn, refine div_le_div_of_le_of_pos (sub_le_sub_left _ _) (sub_pos.2 hx1), rw [← one_mul (_ ^ n), _root_.pow_succ], exact mul_le_mul_of_nonneg_right (le_of_lt hx1) (pow_nonneg (abv_nonneg _ _) _) } end lemma is_cau_geo_series_const (a : α) {x : α} (hx1 : abs x < 1) : is_cau_seq abs (λ m, (range m).sum (λ n, a * x ^ n)) := have is_cau_seq abs (λ m, a * (range m).sum (λ n, x ^ n)) := (cau_seq.const abs a * ⟨_, is_cau_geo_series x hx1⟩).2, by simpa only [mul_sum] lemma series_ratio_test {f : ℕ → β} (n : ℕ) (r : α) (hr0 : 0 ≤ r) (hr1 : r < 1) (h : ∀ m, n ≤ m → abv (f m.succ) ≤ r * abv (f m)) : is_cau_seq abv (λ m, (range m).sum f) := have har1 : abs r < 1, by rwa abs_of_nonneg hr0, begin refine is_cau_series_of_abv_le_cau n.succ _ (is_cau_geo_series_const (abv (f n.succ) * r⁻¹ ^ n.succ) har1), assume m hmn, cases classical.em (r = 0) with r_zero r_ne_zero, { have m_pos := lt_of_lt_of_le (nat.succ_pos n) hmn, have := h m.pred (nat.le_of_succ_le_succ (by rwa [nat.succ_pred_eq_of_pos m_pos])), simpa [r_zero, nat.succ_pred_eq_of_pos m_pos, pow_succ] }, generalize hk : m - n.succ = k, have r_pos : 0 < r := lt_of_le_of_ne hr0 (ne.symm r_ne_zero), replace hk : m = k + n.succ := (nat.sub_eq_iff_eq_add hmn).1 hk, induction k with k ih generalizing m n, { rw [hk, zero_add, mul_right_comm, ← pow_inv _ _ r_ne_zero, ← div_eq_mul_inv, mul_div_cancel], exact (ne_of_lt (pow_pos r_pos _)).symm }, { have kn : k + n.succ ≥ n.succ, by rw ← zero_add n.succ; exact add_le_add (zero_le _) (by simp), rw [hk, nat.succ_add, pow_succ' r, ← mul_assoc], exact le_trans (by rw mul_comm; exact h _ (nat.le_of_succ_le kn)) (mul_le_mul_of_nonneg_right (ih (k + n.succ) n h kn rfl) hr0) } end lemma sum_range_diag_flip {α : Type*} [add_comm_monoid α] (n : ℕ) (f : ℕ → ℕ → α) : (range n).sum (λ m, (range (m + 1)).sum (λ k, f k (m - k))) = (range n).sum (λ m, (range (n - m)).sum (f m)) := have h₁ : ((range n).sigma (range ∘ nat.succ)).sum (λ (a : Σ m, ℕ), f (a.2) (a.1 - a.2)) = (range n).sum (λ m, (range (m + 1)).sum (λ k, f k (m - k))) := sum_sigma, have h₂ : ((range n).sigma (λ m, range (n - m))).sum (λ a : Σ (m : ℕ), ℕ, f (a.1) (a.2)) = (range n).sum (λ m, sum (range (n - m)) (f m)) := sum_sigma, h₁ ▸ h₂ ▸ sum_bij (λ a _, ⟨a.2, a.1 - a.2⟩) (λ a ha, have h₁ : a.1 < n := mem_range.1 (mem_sigma.1 ha).1, have h₂ : a.2 < nat.succ a.1 := mem_range.1 (mem_sigma.1 ha).2, mem_sigma.2 ⟨mem_range.2 (lt_of_lt_of_le h₂ h₁), mem_range.2 ((nat.sub_lt_sub_right_iff (nat.le_of_lt_succ h₂)).2 h₁)⟩) (λ _ _, rfl) (λ ⟨a₁, a₂⟩ ⟨b₁, b₂⟩ ha hb h, have ha : a₁ < n ∧ a₂ ≤ a₁ := ⟨mem_range.1 (mem_sigma.1 ha).1, nat.le_of_lt_succ (mem_range.1 (mem_sigma.1 ha).2)⟩, have hb : b₁ < n ∧ b₂ ≤ b₁ := ⟨mem_range.1 (mem_sigma.1 hb).1, nat.le_of_lt_succ (mem_range.1 (mem_sigma.1 hb).2)⟩, have h : a₂ = b₂ ∧ _ := sigma.mk.inj h, have h' : a₁ = b₁ - b₂ + a₂ := (nat.sub_eq_iff_eq_add ha.2).1 (eq_of_heq h.2), sigma.mk.inj_iff.2 ⟨nat.sub_add_cancel hb.2 ▸ h'.symm ▸ h.1 ▸ rfl, (heq_of_eq h.1)⟩) (λ ⟨a₁, a₂⟩ ha, have ha : a₁ < n ∧ a₂ < n - a₁ := ⟨mem_range.1 (mem_sigma.1 ha).1, (mem_range.1 (mem_sigma.1 ha).2)⟩, ⟨⟨a₂ + a₁, a₁⟩, ⟨mem_sigma.2 ⟨mem_range.2 (nat.lt_sub_right_iff_add_lt.1 ha.2), mem_range.2 (nat.lt_succ_of_le (nat.le_add_left _ _))⟩, sigma.mk.inj_iff.2 ⟨rfl, heq_of_eq (nat.add_sub_cancel _ _).symm⟩⟩⟩) lemma abv_sum_le_sum_abv {γ : Type*} (f : γ → β) (s : finset γ) : abv (s.sum f) ≤ s.sum (abv ∘ f) := by haveI := classical.dec_eq γ; exact finset.induction_on s (by simp [abv_zero abv]) (λ a s has ih, by rw [sum_insert has, sum_insert has]; exact le_trans (abv_add abv _ _) (add_le_add_left ih _)) lemma sum_range_sub_sum_range {α : Type*} [add_comm_group α] {f : ℕ → α} {n m : ℕ} (hnm : n ≤ m) : (range m).sum f - (range n).sum f = ((range m).filter (λ k, n ≤ k)).sum f := begin rw [← sum_sdiff (@filter_subset _ (λ k, n ≤ k) _ (range m)), sub_eq_iff_eq_add, ← eq_sub_iff_add_eq, add_sub_cancel'], refine finset.sum_congr (finset.ext.2 $ λ a, ⟨λ h, by simp at *; finish, λ h, have ham : a < m := lt_of_lt_of_le (mem_range.1 h) hnm, by simp * at *⟩) (λ _ _, rfl), end lemma cauchy_product {a b : ℕ → β} (ha : is_cau_seq abs (λ m, (range m).sum (λ n, abv (a n)))) (hb : is_cau_seq abv (λ m, (range m).sum b)) (ε : α) (ε0 : 0 < ε) : ∃ i : ℕ, ∀ j ≥ i, abv ((range j).sum a * (range j).sum b - (range j).sum (λ n, (range (n + 1)).sum (λ m, a m * b (n - m)))) < ε := let ⟨Q, hQ⟩ := cau_seq.bounded ⟨_, hb⟩ in let ⟨P, hP⟩ := cau_seq.bounded ⟨_, ha⟩ in have hP0 : 0 < P, from lt_of_le_of_lt (abs_nonneg _) (hP 0), have hPε0 : 0 < ε / (2 * P), from div_pos ε0 (mul_pos (show (2 : α) > 0, from by norm_num) hP0), let ⟨N, hN⟩ := cau_seq.cauchy₂ ⟨_, hb⟩ hPε0 in have hQε0 : 0 < ε / (4 * Q), from div_pos ε0 (mul_pos (show (0 : α) < 4, by norm_num) (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0))), let ⟨M, hM⟩ := cau_seq.cauchy₂ ⟨_, ha⟩ hQε0 in ⟨2 * (max N M + 1), λ K hK, have h₁ : sum (range K) (λ m, (range (m + 1)).sum (λ k, a k * b (m - k))) = sum (range K) (λ m, sum (range (K - m)) (λ n, a m * b n)), by simpa using sum_range_diag_flip K (λ m n, a m * b n), have h₂ : (λ i, (range (K - i)).sum (λ k, a i * b k)) = (λ i, a i * (range (K - i)).sum b), by simp [finset.mul_sum], have h₃ : (range K).sum (λ i, a i * (range (K - i)).sum b) = (range K).sum (λ i, a i * ((range (K - i)).sum b - (range K).sum b)) + (range K).sum (λ i, a i * (range K).sum b), by rw ← sum_add_distrib; simp [(mul_add _ _ _).symm], have two_mul_two : (4 : α) = 2 * 2, by norm_num, have hQ0 : Q ≠ 0, from λ h, by simpa [h, lt_irrefl] using hQε0, have h2Q0 : 2 * Q ≠ 0, from mul_ne_zero two_ne_zero hQ0, have hε : ε / (2 * P) * P + ε / (4 * Q) * (2 * Q) = ε, by rw [← div_div_eq_div_mul, div_mul_cancel _ (ne.symm (ne_of_lt hP0)), two_mul_two, mul_assoc, ← div_div_eq_div_mul, div_mul_cancel _ h2Q0, add_halves], have hNMK : max N M + 1 < K, from lt_of_lt_of_le (by rw two_mul; exact lt_add_of_pos_left _ (nat.succ_pos _)) hK, have hKN : N < K, from calc N ≤ max N M : le_max_left _ _ ... < max N M + 1 : nat.lt_succ_self _ ... < K : hNMK, have hsumlesum : (range (max N M + 1)).sum (λ i, abv (a i) * abv ((range (K - i)).sum b - (range K).sum b)) ≤ (range (max N M + 1)).sum (λ i, abv (a i) * (ε / (2 * P))), from sum_le_sum (λ m hmJ, mul_le_mul_of_nonneg_left (le_of_lt (hN (K - m) K (nat.le_sub_left_of_add_le (le_trans (by rw two_mul; exact add_le_add (le_of_lt (mem_range.1 hmJ)) (le_trans (le_max_left _ _) (le_of_lt (lt_add_one _)))) hK)) (le_of_lt hKN))) (abv_nonneg abv _)), have hsumltP : sum (range (max N M + 1)) (λ n, abv (a n)) < P := calc sum (range (max N M + 1)) (λ n, abv (a n)) = abs (sum (range (max N M + 1)) (λ n, abv (a n))) : eq.symm (abs_of_nonneg (zero_le_sum (λ x h, abv_nonneg abv (a x)))) ... < P : hP (max N M + 1), begin rw [h₁, h₂, h₃, sum_mul, ← sub_sub, sub_right_comm, sub_self, zero_sub, abv_neg abv], refine lt_of_le_of_lt (abv_sum_le_sum_abv _ _) _, suffices : (range (max N M + 1)).sum (λ (i : ℕ), abv (a i) * abv ((range (K - i)).sum b - (range K).sum b)) + ((range K).sum (λ (i : ℕ), abv (a i) * abv ((range (K - i)).sum b - (range K).sum b)) -(range (max N M + 1)).sum (λ (i : ℕ), abv (a i) * abv ((range (K - i)).sum b - (range K).sum b))) < ε / (2 * P) * P + ε / (4 * Q) * (2 * Q), { rw hε at this, simpa [abv_mul abv] }, refine add_lt_add (lt_of_le_of_lt hsumlesum (by rw [← sum_mul, mul_comm]; exact (mul_lt_mul_left hPε0).mpr hsumltP)) _, rw sum_range_sub_sum_range (le_of_lt hNMK), exact calc sum ((range K).filter (λ k, max N M + 1 ≤ k)) (λ i, abv (a i) * abv (sum (range (K - i)) b - sum (range K) b)) ≤ sum ((range K).filter (λ k, max N M + 1 ≤ k)) (λ i, abv (a i) * (2 * Q)) : sum_le_sum (λ n hn, begin refine mul_le_mul_of_nonneg_left _ (abv_nonneg _ _), rw sub_eq_add_neg, refine le_trans (abv_add _ _ _) _, rw [two_mul, abv_neg abv], exact add_le_add (le_of_lt (hQ _)) (le_of_lt (hQ _)), end) ... < ε / (4 * Q) * (2 * Q) : by rw [← sum_mul, ← sum_range_sub_sum_range (le_of_lt hNMK)]; refine (mul_lt_mul_right $ by rw two_mul; exact add_pos (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0)) (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0))).2 (lt_of_le_of_lt (le_abs_self _) (hM _ _ (le_trans (nat.le_succ_of_le (le_max_right _ _)) (le_of_lt hNMK)) (nat.le_succ_of_le (le_max_right _ _)))) end⟩ end open finset open cau_seq namespace complex lemma is_cau_abs_exp (z : ℂ) : is_cau_seq _root_.abs (λ n, (range n).sum (λ m, abs (z ^ m / nat.fact m))) := let ⟨n, hn⟩ := exists_nat_gt (abs z) in have hn0 : (0 : ℝ) < n, from lt_of_le_of_lt (abs_nonneg _) hn, series_ratio_test n (complex.abs z / n) (div_nonneg_of_nonneg_of_pos (complex.abs_nonneg _) hn0) (by rwa [div_lt_iff hn0, one_mul]) (λ m hm, by rw [abs_abs, abs_abs, nat.fact_succ, pow_succ, mul_comm m.succ, nat.cast_mul, ← div_div_eq_div_mul, mul_div_assoc, mul_div_right_comm, abs_mul, abs_div, abs_cast_nat]; exact mul_le_mul_of_nonneg_right (div_le_div_of_le_left (abs_nonneg _) (nat.cast_pos.2 (nat.succ_pos _)) hn0 (nat.cast_le.2 (le_trans hm (nat.le_succ _)))) (abs_nonneg _)) noncomputable theory lemma is_cau_exp (z : ℂ) : is_cau_seq abs (λ n, (range n).sum (λ m, z ^ m / nat.fact m)) := is_cau_series_of_abv_cau (is_cau_abs_exp z) def exp' (z : ℂ) : cau_seq ℂ complex.abs := ⟨λ n, (range n).sum (λ m, z ^ m / nat.fact m), is_cau_exp z⟩ def exp (z : ℂ) : ℂ := lim (exp' z) def sin (z : ℂ) : ℂ := ((exp (-z * I) - exp (z * I)) * I) / 2 def cos (z : ℂ) : ℂ := (exp (z * I) + exp (-z * I)) / 2 def tan (z : ℂ) : ℂ := sin z / cos z def sinh (z : ℂ) : ℂ := (exp z - exp (-z)) / 2 def cosh (z : ℂ) : ℂ := (exp z + exp (-z)) / 2 def tanh (z : ℂ) : ℂ := sinh z / cosh z end complex namespace real open complex def exp (x : ℝ) : ℝ := (exp x).re def sin (x : ℝ) : ℝ := (sin x).re def cos (x : ℝ) : ℝ := (cos x).re def tan (x : ℝ) : ℝ := (tan x).re def sinh (x : ℝ) : ℝ := (sinh x).re def cosh (x : ℝ) : ℝ := (cosh x).re def tanh (x : ℝ) : ℝ := (tanh x).re end real namespace complex variables (x y : ℂ) @[simp] lemma exp_zero : exp 0 = 1 := lim_eq_of_equiv_const $ λ ε ε0, ⟨1, λ j hj, begin convert ε0, cases j, { exact absurd hj (not_le_of_gt zero_lt_one) }, { dsimp [exp'], induction j with j ih, { dsimp [exp']; simp }, { rw ← ih dec_trivial, simp only [sum_range_succ, pow_succ], simp } } end⟩ lemma exp_add : exp (x + y) = exp x * exp y := show lim (⟨_, is_cau_exp (x + y)⟩ : cau_seq ℂ abs) = lim (show cau_seq ℂ abs, from ⟨_, is_cau_exp x⟩) * lim (show cau_seq ℂ abs, from ⟨_, is_cau_exp y⟩), from have hj : ∀ j : ℕ, (range j).sum (λ m, (x + y) ^ m / m.fact) = (range j).sum (λ i, (range (i + 1)).sum (λ k, x ^ k / k.fact * (y ^ (i - k) / (i - k).fact))), from assume j, finset.sum_congr rfl (λ m hm, begin rw [add_pow, div_eq_mul_inv, sum_mul], refine finset.sum_congr rfl (λ i hi, _), have h₁ : (nat.choose m i : ℂ) ≠ 0 := nat.cast_ne_zero.2 (nat.pos_iff_ne_zero.1 (nat.choose_pos (nat.le_of_lt_succ (mem_range.1 hi)))), have h₂ := nat.choose_mul_fact_mul_fact (nat.le_of_lt_succ $ finset.mem_range.1 hi), rw [← h₂, nat.cast_mul, nat.cast_mul, mul_inv', mul_inv'], simp only [mul_left_comm (nat.choose m i : ℂ), mul_assoc, mul_left_comm (nat.choose m i : ℂ)⁻¹, mul_comm (nat.choose m i : ℂ)], rw inv_mul_cancel h₁, simp [div_eq_mul_inv, mul_comm, mul_assoc, mul_left_comm] end), by rw lim_mul_lim; exact eq.symm (lim_eq_lim_of_equiv (by dsimp; simp only [hj]; exact cauchy_product (is_cau_abs_exp x) (is_cau_exp y))) lemma exp_nat_mul (x : ℂ) : ∀ n : ℕ, exp(n*x) = (exp x)^n | 0 := by rw [nat.cast_zero, zero_mul, exp_zero, pow_zero] | (nat.succ n) := by rw [pow_succ', nat.cast_add_one, add_mul, exp_add, ←exp_nat_mul, one_mul] lemma exp_ne_zero : exp x ≠ 0 := λ h, @zero_ne_one ℂ _ $ by rw [← exp_zero, ← add_neg_self x, exp_add, h]; simp lemma exp_neg : exp (-x) = (exp x)⁻¹ := by rw [← domain.mul_left_inj (exp_ne_zero x), ← exp_add]; simp [mul_inv_cancel (exp_ne_zero x)] lemma exp_sub : exp (x - y) = exp x / exp y := by simp [exp_add, exp_neg, div_eq_mul_inv] @[simp] lemma exp_conj : exp (conj x) = conj (exp x) := begin dsimp [exp], rw [← lim_conj], refine congr_arg lim (cau_seq.ext (λ _, _)), dsimp [exp', function.comp, cau_seq_conj], rw ← sum_hom conj, refine sum_congr rfl (λ n hn, _), rw [conj_div, conj_pow, ← of_real_nat_cast, conj_of_real] end @[simp] lemma exp_of_real_im (x : ℝ) : (exp x).im = 0 := let ⟨r, hr⟩ := (eq_conj_iff_real (exp x)).1 (by rw [← exp_conj, conj_of_real]) in by rw [hr, of_real_im] lemma exp_of_real_re (x : ℝ) : (exp x).re = real.exp x := rfl @[simp] lemma of_real_exp_of_real_re (x : ℝ) : ((exp x).re : ℂ) = exp x := complex.ext (by simp) (by simp) @[simp] lemma of_real_exp (x : ℝ) : (real.exp x : ℂ) = exp x := complex.ext (by simp [real.exp]) (by simp [real.exp]) @[simp] lemma sin_zero : sin 0 = 0 := by simp [sin] @[simp] lemma sin_neg : sin (-x) = -sin x := by simp [sin, exp_neg, (neg_div _ _).symm, add_mul] lemma sin_add : sin (x + y) = sin x * cos y + cos x * sin y := begin rw [← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _), ← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _)], simp only [mul_add, add_mul, exp_add, div_mul_div, div_add_div_same, mul_assoc, (div_div_eq_div_mul _ _ _).symm, mul_div_cancel' _ (@two_ne_zero' ℂ _ _ _), sin, cos], simp [mul_add, add_mul, exp_add], ring end @[simp] lemma cos_zero : cos 0 = 1 := by simp [cos] @[simp] lemma cos_neg : cos (-x) = cos x := by simp [cos, exp_neg] lemma cos_add : cos (x + y) = cos x * cos y - sin x * sin y := begin rw [← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _), ← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _)], simp only [mul_add, add_mul, mul_sub, sub_mul, exp_add, div_mul_div, div_add_div_same, mul_assoc, (div_div_eq_div_mul _ _ _).symm, mul_div_cancel' _ (@two_ne_zero' ℂ _ _ _), sin, cos], apply complex.ext; simp [mul_add, add_mul, exp_add]; ring end lemma sin_sub : sin (x - y) = sin x * cos y - cos x * sin y := by simp [sin_add, sin_neg, cos_neg] lemma cos_sub : cos (x - y) = cos x * cos y + sin x * sin y := by simp [cos_add, sin_neg, cos_neg] lemma sin_conj : sin (conj x) = conj (sin x) := begin rw [sin, ← conj_neg_I, ← conj_mul, ← conj_neg, ← conj_mul, exp_conj, exp_conj, ← conj_sub, sin, conj_div, conj_two, ← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _), mul_div_cancel' _ (@two_ne_zero' ℂ _ _ _), mul_div_cancel' _ (@two_ne_zero' ℂ _ _ _)], apply complex.ext; simp [sin, neg_add], end @[simp] lemma sin_of_real_im (x : ℝ) : (sin x).im = 0 := let ⟨r, hr⟩ := (eq_conj_iff_real (sin x)).1 (by rw [← sin_conj, conj_of_real]) in by rw [hr, of_real_im] lemma sin_of_real_re (x : ℝ) : (sin x).re = real.sin x := rfl @[simp] lemma of_real_sin_of_real_re (x : ℝ) : ((sin x).re : ℂ) = sin x := by apply complex.ext; simp @[simp] lemma of_real_sin (x : ℝ) : (real.sin x : ℂ) = sin x := by simp [real.sin] lemma cos_conj : cos (conj x) = conj (cos x) := begin rw [cos, ← conj_neg_I, ← conj_mul, ← conj_neg, ← conj_mul, exp_conj, exp_conj, cos, conj_div, conj_two, ← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _), mul_div_cancel' _ (@two_ne_zero' ℂ _ _ _), mul_div_cancel' _ (@two_ne_zero' ℂ _ _ _)], apply complex.ext; simp end @[simp] lemma cos_of_real_im (x : ℝ) : (cos x).im = 0 := let ⟨r, hr⟩ := (eq_conj_iff_real (cos x)).1 (by rw [← cos_conj, conj_of_real]) in by rw [hr, of_real_im] lemma cos_of_real_re (x : ℝ) : (cos x).re = real.cos x := rfl @[simp] lemma of_real_cos_of_real_re (x : ℝ) : ((cos x).re : ℂ) = cos x := by apply complex.ext; simp @[simp] lemma of_real_cos (x : ℝ) : (real.cos x : ℂ) = cos x := by simp [real.cos, -cos_of_real_re] @[simp] lemma tan_zero : tan 0 = 0 := by simp [tan] lemma tan_eq_sin_div_cos : tan x = sin x / cos x := rfl @[simp] lemma tan_neg : tan (-x) = -tan x := by simp [tan, neg_div] lemma tan_conj : tan (conj x) = conj (tan x) := by rw [tan, sin_conj, cos_conj, ← conj_div, tan] @[simp] lemma tan_of_real_im (x : ℝ) : (tan x).im = 0 := let ⟨r, hr⟩ := (eq_conj_iff_real (tan x)).1 (by rw [← tan_conj, conj_of_real]) in by rw [hr, of_real_im] lemma tan_of_real_re (x : ℝ) : (tan x).re = real.tan x := rfl @[simp] lemma of_real_tan_of_real_re (x : ℝ) : ((tan x).re : ℂ) = tan x := by apply complex.ext; simp @[simp] lemma of_real_tan (x : ℝ) : (real.tan x : ℂ) = tan x := by simp [real.tan, -tan_of_real_re] lemma sin_pow_two_add_cos_pow_two : sin x ^ 2 + cos x ^ 2 = 1 := begin simp only [pow_two, mul_sub, sub_mul, mul_add, add_mul, div_eq_mul_inv, neg_mul_eq_neg_mul_symm, exp_neg, mul_comm (exp _), mul_left_comm (exp _), mul_assoc, mul_left_comm (exp _)⁻¹, inv_mul_cancel (exp_ne_zero _), mul_inv', mul_one, one_mul, sin, cos], apply complex.ext; simp [norm_sq]; ring end lemma cos_two_mul : cos (2 * x) = 2 * cos x ^ 2 - 1 := by rw [two_mul, cos_add, ← pow_two, ← pow_two, eq_sub_iff_add_eq.2 (sin_pow_two_add_cos_pow_two x)]; simp [two_mul] lemma sin_two_mul : sin (2 * x) = 2 * sin x * cos x := by rw [two_mul, sin_add, two_mul, add_mul, mul_comm] lemma cos_square : cos x ^ 2 = 1 / 2 + cos (2 * x) / 2 := by simp [cos_two_mul, div_add_div_same, mul_div_cancel_left, two_ne_zero', -one_div_eq_inv] lemma sin_square : sin x ^ 2 = 1 - cos x ^ 2 := by { rw [←sin_pow_two_add_cos_pow_two x], simp } lemma exp_mul_I : exp (x * I) = cos x + sin x * I := by rw [cos, sin, mul_comm (_ / 2) I, ← mul_div_assoc, mul_left_comm I, I_mul_I, ← add_div]; simp lemma exp_add_mul_I : exp (x + y * I) = exp x * (cos y + sin y * I) := by rw [exp_add, exp_mul_I] lemma exp_eq_exp_re_mul_sin_add_cos : exp x = exp x.re * (cos x.im + sin x.im * I) := by rw [← exp_add_mul_I, re_add_im] theorem cos_add_sin_mul_I_pow (n : ℕ) (z : ℂ) : (cos z + sin z * I) ^ n = cos (↑n * z) + sin (↑n * z) * I := begin rw [← exp_mul_I, ← exp_mul_I], induction n with n ih, { rw [pow_zero, nat.cast_zero, zero_mul, zero_mul, exp_zero] }, { rw [pow_succ', ih, nat.cast_succ, add_mul, add_mul, one_mul, exp_add] } end @[simp] lemma sinh_zero : sinh 0 = 0 := by simp [sinh] @[simp] lemma sinh_neg : sinh (-x) = -sinh x := by simp [sinh, exp_neg, (neg_div _ _).symm, add_mul] lemma sinh_add : sinh (x + y) = sinh x * cosh y + cosh x * sinh y := begin rw [← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _), ← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _)], simp only [mul_add, add_mul, exp_add, div_mul_div, div_add_div_same, mul_assoc, (div_div_eq_div_mul _ _ _).symm, mul_div_cancel' _ (@two_ne_zero' ℂ _ _ _), sinh, cosh], simp [mul_add, add_mul, exp_add], ring end @[simp] lemma cosh_zero : cosh 0 = 1 := by simp [cosh] @[simp] lemma cosh_neg : cosh (-x) = cosh x := by simp [cosh, exp_neg] lemma cosh_add : cosh (x + y) = cosh x * cosh y + sinh x * sinh y := begin rw [← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _), ← domain.mul_left_inj (@two_ne_zero' ℂ _ _ _)], simp only [mul_add, add_mul, mul_sub, sub_mul, exp_add, div_mul_div, div_add_div_same, mul_assoc, (div_div_eq_div_mul _ _ _).symm, mul_div_cancel' _ (@two_ne_zero' ℂ _ _ _), sinh, cosh], apply complex.ext; simp [mul_add, add_mul, exp_add]; ring end lemma sinh_sub : sinh (x - y) = sinh x * cosh y - cosh x * sinh y := by simp [sinh_add, sinh_neg, cosh_neg] lemma cosh_sub : cosh (x - y) = cosh x * cosh y - sinh x * sinh y := by simp [cosh_add, sinh_neg, cosh_neg] lemma sinh_conj : sinh (conj x) = conj (sinh x) := by rw [sinh, ← conj_neg, exp_conj, exp_conj, ← conj_sub, sinh, conj_div, conj_two] @[simp] lemma sinh_of_real_im (x : ℝ) : (sinh x).im = 0 := let ⟨r, hr⟩ := (eq_conj_iff_real (sinh x)).1 (by rw [← sinh_conj, conj_of_real]) in by rw [hr, of_real_im] lemma sinh_of_real_re (x : ℝ) : (sinh x).re = real.sinh x := rfl @[simp] lemma of_real_sinh_of_real_re (x : ℝ) : ((sinh x).re : ℂ) = sinh x := by apply complex.ext; simp @[simp] lemma of_real_sinh (x : ℝ) : (real.sinh x : ℂ) = sinh x := by simp [real.sinh] lemma cosh_conj : cosh (conj x) = conj (cosh x) := by rw [cosh, ← conj_neg, exp_conj, exp_conj, ← conj_add, cosh, conj_div, conj_two] @[simp] lemma cosh_of_real_im (x : ℝ) : (cosh x).im = 0 := let ⟨r, hr⟩ := (eq_conj_iff_real (cosh x)).1 (by rw [← cosh_conj, conj_of_real]) in by rw [hr, of_real_im] lemma cosh_of_real_re (x : ℝ) : (cosh x).re = real.cosh x := rfl @[simp] lemma of_real_cosh_of_real_re (x : ℝ) : ((cosh x).re : ℂ) = cosh x := by apply complex.ext; simp @[simp] lemma of_real_cosh (x : ℝ) : (real.cosh x : ℂ) = cosh x := by simp [real.cosh] lemma tanh_eq_sinh_div_cosh : tanh x = sinh x / cosh x := rfl @[simp] lemma tanh_zero : tanh 0 = 0 := by simp [tanh] @[simp] lemma tanh_neg : tanh (-x) = -tanh x := by simp [tanh, neg_div] lemma tanh_conj : tanh (conj x) = conj (tanh x) := by rw [tanh, sinh_conj, cosh_conj, ← conj_div, tanh] @[simp] lemma tanh_of_real_im (x : ℝ) : (tanh x).im = 0 := let ⟨r, hr⟩ := (eq_conj_iff_real (tanh x)).1 (by rw [← tanh_conj, conj_of_real]) in by rw [hr, of_real_im] lemma tanh_of_real_re (x : ℝ) : (tanh x).re = real.tanh x := rfl @[simp] lemma of_real_tanh_of_real_re (x : ℝ) : ((tanh x).re : ℂ) = tanh x := by apply complex.ext; simp @[simp] lemma of_real_tanh (x : ℝ) : (real.tanh x : ℂ) = tanh x := by simp [real.tanh] end complex namespace real open complex variables (x y : ℝ) @[simp] lemma exp_zero : exp 0 = 1 := by simp [real.exp] lemma exp_add : exp (x + y) = exp x * exp y := by simp [exp_add, exp] lemma exp_nat_mul (x : ℝ) : ∀ n : ℕ, exp(n*x) = (exp x)^n | 0 := by rw [nat.cast_zero, zero_mul, exp_zero, pow_zero] | (nat.succ n) := by rw [pow_succ', nat.cast_add_one, add_mul, exp_add, ←exp_nat_mul, one_mul] lemma exp_ne_zero : exp x ≠ 0 := λ h, exp_ne_zero x $ by rw [exp, ← of_real_inj] at h; simp * at * lemma exp_neg : exp (-x) = (exp x)⁻¹ := by rw [← of_real_inj, exp, of_real_exp_of_real_re, of_real_neg, exp_neg, of_real_inv, of_real_exp] lemma exp_sub : exp (x - y) = exp x / exp y := by simp [exp_add, exp_neg, div_eq_mul_inv] @[simp] lemma sin_zero : sin 0 = 0 := by simp [sin] @[simp] lemma sin_neg : sin (-x) = -sin x := by simp [sin, exp_neg, (neg_div _ _).symm, add_mul] lemma sin_add : sin (x + y) = sin x * cos y + cos x * sin y := by rw [← of_real_inj]; simp [sin, sin_add] @[simp] lemma cos_zero : cos 0 = 1 := by simp [cos] @[simp] lemma cos_neg : cos (-x) = cos x := by simp [cos, exp_neg] lemma cos_add : cos (x + y) = cos x * cos y - sin x * sin y := by rw ← of_real_inj; simp [cos, cos_add] lemma sin_sub : sin (x - y) = sin x * cos y - cos x * sin y := by simp [sin_add, sin_neg, cos_neg] lemma cos_sub : cos (x - y) = cos x * cos y + sin x * sin y := by simp [cos_add, sin_neg, cos_neg] lemma tan_eq_sin_div_cos : tan x = sin x / cos x := if h : complex.cos x = 0 then by simp [sin, cos, tan, *, complex.tan, div_eq_mul_inv] at * else by rw [sin, cos, tan, complex.tan, ← of_real_inj, div_eq_mul_inv, mul_re]; simp [norm_sq, (div_div_eq_div_mul _ _ _).symm, div_self h]; refl @[simp] lemma tan_zero : tan 0 = 0 := by simp [tan] @[simp] lemma tan_neg : tan (-x) = -tan x := by simp [tan, neg_div] lemma sin_pow_two_add_cos_pow_two : sin x ^ 2 + cos x ^ 2 = 1 := by rw ← of_real_inj; simpa [sin, of_real_pow] using sin_pow_two_add_cos_pow_two x lemma abs_sin_le_one : abs' (sin x) ≤ 1 := not_lt.1 $ λ h, lt_irrefl (1 * 1 : ℝ) (calc 1 * 1 < abs' (sin x) * abs' (sin x) : mul_lt_mul h (le_of_lt h) zero_lt_one (le_trans zero_le_one (le_of_lt h)) ... = sin x ^ 2 : by rw [← _root_.abs_mul, abs_mul_self, pow_two] ... ≤ sin x ^ 2 + cos x ^ 2 : le_add_of_nonneg_right (pow_two_nonneg _) ... = 1 * 1 : by rw [sin_pow_two_add_cos_pow_two, one_mul]) lemma abs_cos_le_one : abs' (cos x) ≤ 1 := not_lt.1 $ λ h, lt_irrefl (1 * 1 : ℝ) (calc 1 * 1 < abs' (cos x) * abs' (cos x) : mul_lt_mul h (le_of_lt h) zero_lt_one (le_trans zero_le_one (le_of_lt h)) ... = cos x ^ 2 : by rw [← _root_.abs_mul, abs_mul_self, pow_two] ... ≤ sin x ^ 2 + cos x ^ 2 : le_add_of_nonneg_left (pow_two_nonneg _) ... = 1 * 1 : by rw [sin_pow_two_add_cos_pow_two, one_mul]) lemma sin_le_one : sin x ≤ 1 := (abs_le.1 (abs_sin_le_one _)).2 lemma cos_le_one : cos x ≤ 1 := (abs_le.1 (abs_cos_le_one _)).2 lemma neg_one_le_sin : -1 ≤ sin x := (abs_le.1 (abs_sin_le_one _)).1 lemma neg_one_le_cos : -1 ≤ cos x := (abs_le.1 (abs_cos_le_one _)).1 lemma sin_pow_two_le_one : sin x ^ 2 ≤ 1 := by rw [pow_two, ← abs_mul_self, _root_.abs_mul]; exact mul_le_one (abs_sin_le_one _) (abs_nonneg _) (abs_sin_le_one _) lemma cos_pow_two_le_one : cos x ^ 2 ≤ 1 := by rw [pow_two, ← abs_mul_self, _root_.abs_mul]; exact mul_le_one (abs_cos_le_one _) (abs_nonneg _) (abs_cos_le_one _) lemma cos_two_mul : cos (2 * x) = 2 * cos x ^ 2 - 1 := by rw ← of_real_inj; simp [cos_two_mul, cos, pow_two] lemma sin_two_mul : sin (2 * x) = 2 * sin x * cos x := by rw ← of_real_inj; simp [sin_two_mul, sin, pow_two] lemma cos_square : cos x ^ 2 = 1 / 2 + cos (2 * x) / 2 := by simp [cos_two_mul, div_add_div_same, mul_div_cancel_left, two_ne_zero, -one_div_eq_inv] lemma sin_square : sin x ^ 2 = 1 - cos x ^ 2 := by { rw [←sin_pow_two_add_cos_pow_two x], simp } @[simp] lemma sinh_zero : sinh 0 = 0 := by simp [sinh] @[simp] lemma sinh_neg : sinh (-x) = -sinh x := by simp [sinh, exp_neg, (neg_div _ _).symm, add_mul] lemma sinh_add : sinh (x + y) = sinh x * cosh y + cosh x * sinh y := by rw ← of_real_inj; simp [sinh, sinh_add] @[simp] lemma cosh_zero : cosh 0 = 1 := by simp [cosh] @[simp] lemma cosh_neg : cosh (-x) = cosh x := by simp [cosh, exp_neg] lemma cosh_add : cosh (x + y) = cosh x * cosh y + sinh x * sinh y := by rw ← of_real_inj; simp [cosh, cosh_add] lemma sinh_sub : sinh (x - y) = sinh x * cosh y - cosh x * sinh y := by simp [sinh_add, sinh_neg, cosh_neg] lemma cosh_sub : cosh (x - y) = cosh x * cosh y - sinh x * sinh y := by simp [cosh_add, sinh_neg, cosh_neg] lemma tanh_eq_sinh_div_cosh : tanh x = sinh x / cosh x := if h : complex.cosh x = 0 then by simp [sinh, cosh, tanh, *, complex.tanh, div_eq_mul_inv] at * else by rw [sinh, cosh, tanh, complex.tanh, ← of_real_inj, div_eq_mul_inv, mul_re]; simp [norm_sq, (div_div_eq_div_mul _ _ _).symm, div_self h]; refl @[simp] lemma tanh_zero : tanh 0 = 0 := by simp [tanh] @[simp] lemma tanh_neg : tanh (-x) = -tanh x := by simp [tanh, neg_div] open is_absolute_value /- TODO make this private and prove ∀ x -/ lemma add_one_le_exp_of_nonneg {x : ℝ} (hx : 0 ≤ x) : x + 1 ≤ exp x := calc x + 1 ≤ lim (⟨(λ n : ℕ, ((exp' x) n).re), is_cau_seq_re (exp' x)⟩ : cau_seq ℝ abs') : le_lim (cau_seq.le_of_exists ⟨2, λ j hj, show x + (1 : ℝ) ≤ ((range j).sum (λ m, (x ^ m / m.fact : ℂ))).re, from have h₁ : (((λ m : ℕ, (x ^ m / m.fact : ℂ)) ∘ nat.succ) 0).re = x, by simp, have h₂ : ((x : ℂ) ^ 0 / nat.fact 0).re = 1, by simp, begin rw [← nat.sub_add_cancel hj, sum_range_succ', sum_range_succ', add_re, add_re, h₁, h₂, add_assoc, ← @sum_hom _ _ _ _ _ _ _ complex.re (is_add_group_hom.to_is_add_monoid_hom _)], refine le_add_of_nonneg_of_le (zero_le_sum (λ m hm, _)) (le_refl _), dsimp [-nat.fact_succ], rw [← of_real_pow, ← of_real_nat_cast, ← of_real_div, of_real_re], exact div_nonneg (pow_nonneg hx _) (nat.cast_pos.2 (nat.fact_pos _)), end⟩) ... = exp x : by rw [exp, complex.exp, ← cau_seq_re, lim_re] lemma one_le_exp {x : ℝ} (hx : 0 ≤ x) : 1 ≤ exp x := by linarith using [add_one_le_exp_of_nonneg hx] lemma exp_pos (x : ℝ) : 0 < exp x := (le_total 0 x).elim (lt_of_lt_of_le zero_lt_one ∘ one_le_exp) (λ h, by rw [← neg_neg x, real.exp_neg]; exact inv_pos (lt_of_lt_of_le zero_lt_one (one_le_exp (neg_nonneg.2 h)))) @[simp] lemma abs_exp (x : ℝ) : abs' (exp x) = exp x := abs_of_nonneg (le_of_lt (exp_pos _)) lemma exp_lt_exp {x y : ℝ} (h : x < y) : exp x < exp y := by rw [← sub_add_cancel y x, real.exp_add]; exact (lt_mul_iff_one_lt_left (exp_pos _)).2 (lt_of_lt_of_le (by linarith) (add_one_le_exp_of_nonneg (by linarith))) lemma exp_le_exp {x y : ℝ} : real.exp x ≤ real.exp y ↔ x ≤ y := ⟨λ h, le_of_not_gt $ mt exp_lt_exp $ by simpa, λ h, by rw [←sub_add_cancel y x, real.exp_add]; exact (le_mul_iff_one_le_left (exp_pos _)).2 (one_le_exp (sub_nonneg.2 h))⟩ lemma exp_injective : function.injective exp := λ x y h, begin rcases lt_trichotomy x y with h₁ | h₁ | h₁, { exact absurd h (ne_of_lt (exp_lt_exp h₁)) }, { exact h₁ }, { exact absurd h (ne.symm (ne_of_lt (exp_lt_exp h₁))) } end @[simp] lemma exp_eq_one_iff : exp x = 1 ↔ x = 0 := ⟨by rw ← exp_zero; exact λ h, exp_injective h, λ h, by rw [h, exp_zero]⟩ end real namespace complex lemma sum_div_fact_le {α : Type*} [discrete_linear_ordered_field α] (n j : ℕ) (hn : 0 < n) : (sum (filter (λ k, n ≤ k) (range j)) (λ m : ℕ, (1 / m.fact : α))) ≤ n.succ * (n.fact * n)⁻¹ := calc (filter (λ k, n ≤ k) (range j)).sum (λ m : ℕ, (1 / m.fact : α)) = (range (j - n)).sum (λ m, 1 / (m + n).fact) : sum_bij (λ m _, m - n) (λ m hm, mem_range.2 $ (nat.sub_lt_sub_right_iff (by simp at hm; tauto)).2 (by simp at hm; tauto)) (λ m hm, by rw nat.sub_add_cancel; simp at *; tauto) (λ a₁ a₂ ha₁ ha₂ h, by rwa [nat.sub_eq_iff_eq_add, ← nat.sub_add_comm, eq_comm, nat.sub_eq_iff_eq_add, add_right_inj, eq_comm] at h; simp at *; tauto) (λ b hb, ⟨b + n, mem_filter.2 ⟨mem_range.2 $ nat.add_lt_of_lt_sub_right (mem_range.1 hb), nat.le_add_left _ _⟩, by rw nat.add_sub_cancel⟩) ... ≤ (range (j - n)).sum (λ m, (nat.fact n * n.succ ^ m)⁻¹) : begin refine sum_le_sum (assume m n, _), rw [one_div_eq_inv, inv_le_inv], { rw [← nat.cast_pow, ← nat.cast_mul, nat.cast_le, add_comm], exact nat.fact_mul_pow_le_fact }, { exact nat.cast_pos.2 (nat.fact_pos _) }, { exact mul_pos (nat.cast_pos.2 (nat.fact_pos _)) (pow_pos (nat.cast_pos.2 (nat.succ_pos _)) _) }, end ... = (nat.fact n)⁻¹ * (range (j - n)).sum (λ m, n.succ⁻¹ ^ m) : by simp [mul_inv', mul_sum.symm, sum_mul.symm, -nat.fact_succ, mul_comm, inv_pow'] ... = (n.succ - n.succ * n.succ⁻¹ ^ (j - n)) / (n.fact * n) : have h₁ : (n.succ : α) ≠ 1, from @nat.cast_one α _ _ ▸ mt nat.cast_inj.1 (mt nat.succ_inj (nat.pos_iff_ne_zero.1 hn)), have h₂ : (n.succ : α) ≠ 0, from nat.cast_ne_zero.2 (nat.succ_ne_zero _), have h₃ : (n.fact * n : α) ≠ 0, from mul_ne_zero (nat.cast_ne_zero.2 (nat.pos_iff_ne_zero.1 (nat.fact_pos _))) (nat.cast_ne_zero.2 (nat.pos_iff_ne_zero.1 hn)), have h₄ : (n.succ - 1 : α) = n, by simp, by rw [geom_sum_inv h₁ h₂, eq_div_iff_mul_eq _ _ h₃, mul_comm _ (n.fact * n : α), ← mul_assoc (n.fact⁻¹ : α), ← mul_inv', h₄, ← mul_assoc (n.fact * n : α), mul_comm (n : α) n.fact, mul_inv_cancel h₃]; simp [mul_add, add_mul, mul_assoc, mul_comm] ... ≤ n.succ / (n.fact * n) : begin refine (div_le_div_right (mul_pos _ _)).2 _, exact nat.cast_pos.2 (nat.fact_pos _), exact nat.cast_pos.2 hn, exact sub_le_self _ (mul_nonneg (nat.cast_nonneg _) (pow_nonneg (inv_nonneg.2 (nat.cast_nonneg _)) _)) end lemma exp_bound {x : ℂ} (hx : abs x ≤ 1) {n : ℕ} (hn : 0 < n) : abs (exp x - (range n).sum (λ m, x ^ m / m.fact)) ≤ abs x ^ n * (n.succ * (n.fact * n)⁻¹) := begin rw [← lim_const ((range n).sum _), exp, sub_eq_add_neg, ← lim_neg, lim_add, ← lim_abs], refine lim_le (cau_seq.le_of_exists ⟨n, λ j hj, _⟩), show abs ((range j).sum (λ m, x ^ m / m.fact) - (range n).sum (λ m, x ^ m / m.fact)) ≤ abs x ^ n * (n.succ * (n.fact * n)⁻¹), rw sum_range_sub_sum_range hj, exact calc abs (((range j).filter (λ k, n ≤ k)).sum (λ m : ℕ, (x ^ m / m.fact : ℂ))) = abs (((range j).filter (λ k, n ≤ k)).sum (λ m : ℕ, (x ^ n * (x ^ (m - n) / m.fact) : ℂ))) : congr_arg abs (sum_congr rfl (λ m hm, by rw [← mul_div_assoc, ← pow_add, nat.add_sub_cancel']; simp at hm; tauto)) ... ≤ sum (filter (λ k, n ≤ k) (range j)) (λ m, abs (x ^ n * (_ / m.fact))) : abv_sum_le_sum_abv _ _ ... ≤ sum (filter (λ k, n ≤ k) (range j)) (λ m, abs x ^ n * (1 / m.fact)) : begin refine sum_le_sum (λ m hm, _), rw [abs_mul, abv_pow abs, abs_div, abs_cast_nat], refine mul_le_mul_of_nonneg_left ((div_le_div_right _).2 _) _, exact nat.cast_pos.2 (nat.fact_pos _), rw abv_pow abs, exact (pow_le_one _ (abs_nonneg _) hx), exact pow_nonneg (abs_nonneg _) _ end ... = abs x ^ n * (((range j).filter (λ k, n ≤ k)).sum (λ m : ℕ, (1 / m.fact : ℝ))) : by simp [abs_mul, abv_pow abs, abs_div, mul_sum.symm] ... ≤ abs x ^ n * (n.succ * (n.fact * n)⁻¹) : mul_le_mul_of_nonneg_left (sum_div_fact_le _ _ hn) (pow_nonneg (abs_nonneg _) _) end lemma abs_exp_sub_one_le {x : ℂ} (hx : abs x ≤ 1) : abs (exp x - 1) ≤ 2 * abs x := calc abs (exp x - 1) = abs (exp x - (range 1).sum (λ m, x ^ m / m.fact)) : by simp [sum_range_succ] ... ≤ abs x ^ 1 * ((nat.succ 1) * (nat.fact 1 * (1 : ℕ))⁻¹) : exp_bound hx dec_trivial ... = 2 * abs x : by simp [two_mul, mul_two, mul_add, mul_comm] end complex namespace real open complex finset lemma cos_bound {x : ℝ} (hx : abs' x ≤ 1) : abs' (cos x - (1 - x ^ 2 / 2)) ≤ abs' x ^ 4 * (5 / 96) := calc abs' (cos x - (1 - x ^ 2 / 2)) = abs (complex.cos x - (1 - x ^ 2 / 2)) : by rw ← abs_of_real; simp [of_real_bit0, of_real_one, of_real_inv] ... = abs ((complex.exp (x * I) + complex.exp (-x * I) - (2 - x ^ 2)) / 2) : by simp [complex.cos, sub_div, add_div, neg_div, div_self (@two_ne_zero' ℂ _ _ _)] ... = abs (((complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact)) + ((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact)))) / 2) : congr_arg abs (congr_arg (λ x : ℂ, x / 2) begin simp only [sum_range_succ], simp [pow_succ], apply complex.ext; simp [div_eq_mul_inv, norm_sq]; ring end) ... ≤ abs ((complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact)) / 2) + abs ((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact)) / 2) : by rw add_div; exact abs_add _ _ ... = (abs ((complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact))) / 2 + abs ((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact))) / 2) : by simp [complex.abs_div] ... ≤ ((complex.abs (x * I) ^ 4 * (nat.succ 4 * (nat.fact 4 * (4 : ℕ))⁻¹)) / 2 + (complex.abs (-x * I) ^ 4 * (nat.succ 4 * (nat.fact 4 * (4 : ℕ))⁻¹)) / 2) : add_le_add ((div_le_div_right (by norm_num)).2 (exp_bound (by simpa) dec_trivial)) ((div_le_div_right (by norm_num)).2 (exp_bound (by simpa) dec_trivial)) ... ≤ abs' x ^ 4 * (5 / 96) : by norm_num; simp [mul_assoc, mul_comm, mul_left_comm, mul_div_assoc] lemma sin_bound {x : ℝ} (hx : abs' x ≤ 1) : abs' (sin x - (x - x ^ 3 / 6)) ≤ abs' x ^ 4 * (5 / 96) := calc abs' (sin x - (x - x ^ 3 / 6)) = abs (complex.sin x - (x - x ^ 3 / 6)) : by rw ← abs_of_real; simp [of_real_bit0, of_real_one, of_real_inv] ... = abs (((complex.exp (-x * I) - complex.exp (x * I)) * I - (2 * x - x ^ 3 / 3)) / 2) : by simp [complex.sin, sub_div, add_div, neg_div, mul_div_cancel_left _ (@two_ne_zero' ℂ _ _ _), div_div_eq_div_mul, show (3 : ℂ) * 2 = 6, by norm_num] ... = abs ((((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact)) - (complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact))) * I) / 2) : congr_arg abs (congr_arg (λ x : ℂ, x / 2) begin simp only [sum_range_succ], simp [pow_succ], apply complex.ext; simp [div_eq_mul_inv, norm_sq]; ring end) ... ≤ abs ((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact)) * I / 2) + abs (-((complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact)) * I) / 2) : by rw [sub_mul, sub_eq_add_neg, add_div]; exact abs_add _ _ ... = (abs ((complex.exp (x * I) - (range 4).sum (λ m, (x * I) ^ m / m.fact))) / 2 + abs ((complex.exp (-x * I) - (range 4).sum (λ m, (-x * I) ^ m / m.fact))) / 2) : by simp [complex.abs_div, complex.abs_mul] ... ≤ ((complex.abs (x * I) ^ 4 * (nat.succ 4 * (nat.fact 4 * (4 : ℕ))⁻¹)) / 2 + (complex.abs (-x * I) ^ 4 * (nat.succ 4 * (nat.fact 4 * (4 : ℕ))⁻¹)) / 2) : add_le_add ((div_le_div_right (by norm_num)).2 (exp_bound (by simpa) dec_trivial)) ((div_le_div_right (by norm_num)).2 (exp_bound (by simpa) dec_trivial)) ... ≤ abs' x ^ 4 * (5 / 96) : by norm_num; simp [mul_assoc, mul_comm, mul_left_comm, mul_div_assoc] lemma cos_pos_of_le_one {x : ℝ} (hx : abs' x ≤ 1) : 0 < cos x := calc 0 < (1 - x ^ 2 / 2) - abs' x ^ 4 * (5 / 96) : sub_pos.2 $ lt_sub_iff_add_lt.2 (calc abs' x ^ 4 * (5 / 96) + x ^ 2 / 2 ≤ 1 * (5 / 96) + 1 / 2 : add_le_add (mul_le_mul_of_nonneg_right (pow_le_one _ (abs_nonneg _) hx) (by norm_num)) ((div_le_div_right (by norm_num)).2 (by rw [pow_two, ← abs_mul_self, _root_.abs_mul]; exact mul_le_one hx (abs_nonneg _) hx)) ... < 1 : by norm_num) ... ≤ cos x : sub_le.1 (abs_sub_le_iff.1 (cos_bound hx)).2 lemma sin_pos_of_pos_of_le_one {x : ℝ} (hx0 : 0 < x) (hx : x ≤ 1) : 0 < sin x := calc 0 < x - x ^ 3 / 6 - abs' x ^ 4 * (5 / 96) : sub_pos.2 $ lt_sub_iff_add_lt.2 (calc abs' x ^ 4 * (5 / 96) + x ^ 3 / 6 ≤ x * (5 / 96) + x / 6 : add_le_add (mul_le_mul_of_nonneg_right (calc abs' x ^ 4 ≤ abs' x ^ 1 : pow_le_pow_of_le_one (abs_nonneg _) (by rwa _root_.abs_of_nonneg (le_of_lt hx0)) dec_trivial ... = x : by simp [_root_.abs_of_nonneg (le_of_lt (hx0))]) (by norm_num)) ((div_le_div_right (by norm_num)).2 (calc x ^ 3 ≤ x ^ 1 : pow_le_pow_of_le_one (le_of_lt hx0) hx dec_trivial ... = x : pow_one _)) ... < x : by linarith) ... ≤ sin x : sub_le.1 (abs_sub_le_iff.1 (sin_bound (by rwa [_root_.abs_of_nonneg (le_of_lt hx0)]))).2 lemma sin_pos_of_pos_of_le_two {x : ℝ} (hx0 : 0 < x) (hx : x ≤ 2) : 0 < sin x := have x / 2 ≤ 1, from div_le_of_le_mul (by norm_num) (by simpa), calc 0 < 2 * sin (x / 2) * cos (x / 2) : mul_pos (mul_pos (by norm_num) (sin_pos_of_pos_of_le_one (half_pos hx0) this)) (cos_pos_of_le_one (by rwa [_root_.abs_of_nonneg (le_of_lt (half_pos hx0))])) ... = sin x : by rw [← sin_two_mul, two_mul, add_halves] lemma cos_one_le : cos 1 ≤ 2 / 3 := calc cos 1 ≤ abs' (1 : ℝ) ^ 4 * (5 / 96) + (1 - 1 ^ 2 / 2) : sub_le_iff_le_add.1 (abs_sub_le_iff.1 (cos_bound (by simp))).1 ... ≤ 2 / 3 : by norm_num lemma cos_one_pos : 0 < cos 1 := cos_pos_of_le_one (by simp) lemma cos_two_neg : cos 2 < 0 := calc cos 2 = cos (2 * 1) : congr_arg cos (mul_one _).symm ... = _ : real.cos_two_mul 1 ... ≤ 2 * (2 / 3) ^ 2 - 1 : sub_le_sub_right (mul_le_mul_of_nonneg_left (by rw [pow_two, pow_two]; exact mul_self_le_mul_self (le_of_lt cos_one_pos) cos_one_le) (by norm_num)) _ ... < 0 : by norm_num end real namespace complex lemma abs_cos_add_sin_mul_I (x : ℝ) : abs (cos x + sin x * I) = 1 := have _ := real.sin_pow_two_add_cos_pow_two x, by simp [abs, norm_sq, pow_two, *, sin_of_real_re, cos_of_real_re, mul_re] at * lemma abs_exp_eq_iff_re_eq {x y : ℂ} : abs (exp x) = abs (exp y) ↔ x.re = y.re := by rw [exp_eq_exp_re_mul_sin_add_cos, exp_eq_exp_re_mul_sin_add_cos y, abs_mul, abs_mul, abs_cos_add_sin_mul_I, abs_cos_add_sin_mul_I, ← of_real_exp, ← of_real_exp, abs_of_nonneg (le_of_lt (real.exp_pos _)), abs_of_nonneg (le_of_lt (real.exp_pos _)), mul_one, mul_one]; exact ⟨λ h, real.exp_injective h, congr_arg _⟩ @[simp] lemma abs_exp_of_real (x : ℝ) : abs (exp x) = real.exp x := by rw [← of_real_exp]; exact abs_of_nonneg (le_of_lt (real.exp_pos _)) end complex
10ae63fef252b6cb959ceccad573a33b7e4e7063
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/ring_theory/polynomial/scale_roots.lean
81f1f9a01c64484f54dd2bc6f37307d9e76ba593
[ "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,693
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen, Devon Tuma -/ import ring_theory.non_zero_divisors import data.polynomial.algebra_map /-! # Scaling the roots of a polynomial > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines `scale_roots p s` for a polynomial `p` in one variable and a ring element `s` to be the polynomial with root `r * s` for each root `r` of `p` and proves some basic results about it. -/ variables {A K R S : Type*} [comm_ring A] [is_domain A] [field K] [comm_ring R] [comm_ring S] variables {M : submonoid A} namespace polynomial open_locale big_operators polynomial /-- `scale_roots p s` is a polynomial with root `r * s` for each root `r` of `p`. -/ noncomputable def scale_roots (p : R[X]) (s : R) : R[X] := ∑ i in p.support, monomial i (p.coeff i * s ^ (p.nat_degree - i)) @[simp] lemma coeff_scale_roots (p : R[X]) (s : R) (i : ℕ) : (scale_roots p s).coeff i = coeff p i * s ^ (p.nat_degree - i) := by simp [scale_roots, coeff_monomial] {contextual := tt} lemma coeff_scale_roots_nat_degree (p : R[X]) (s : R) : (scale_roots p s).coeff p.nat_degree = p.leading_coeff := by rw [leading_coeff, coeff_scale_roots, tsub_self, pow_zero, mul_one] @[simp] lemma zero_scale_roots (s : R) : scale_roots 0 s = 0 := by { ext, simp } lemma scale_roots_ne_zero {p : R[X]} (hp : p ≠ 0) (s : R) : scale_roots p s ≠ 0 := begin intro h, have : p.coeff p.nat_degree ≠ 0 := mt leading_coeff_eq_zero.mp hp, have : (scale_roots p s).coeff p.nat_degree = 0 := congr_fun (congr_arg (coeff : R[X] → ℕ → R) h) p.nat_degree, rw [coeff_scale_roots_nat_degree] at this, contradiction end lemma support_scale_roots_le (p : R[X]) (s : R) : (scale_roots p s).support ≤ p.support := by { intro, simpa using left_ne_zero_of_mul } lemma support_scale_roots_eq (p : R[X]) {s : R} (hs : s ∈ non_zero_divisors R) : (scale_roots p s).support = p.support := le_antisymm (support_scale_roots_le p s) begin intro i, simp only [coeff_scale_roots, polynomial.mem_support_iff], intros p_ne_zero ps_zero, have := pow_mem hs (p.nat_degree - i) _ ps_zero, contradiction end @[simp] lemma degree_scale_roots (p : R[X]) {s : R} : degree (scale_roots p s) = degree p := begin haveI := classical.prop_decidable, by_cases hp : p = 0, { rw [hp, zero_scale_roots] }, have := scale_roots_ne_zero hp s, refine le_antisymm (finset.sup_mono (support_scale_roots_le p s)) (degree_le_degree _), rw coeff_scale_roots_nat_degree, intro h, have := leading_coeff_eq_zero.mp h, contradiction, end @[simp] lemma nat_degree_scale_roots (p : R[X]) (s : R) : nat_degree (scale_roots p s) = nat_degree p := by simp only [nat_degree, degree_scale_roots] lemma monic_scale_roots_iff {p : R[X]} (s : R) : monic (scale_roots p s) ↔ monic p := by simp only [monic, leading_coeff, nat_degree_scale_roots, coeff_scale_roots_nat_degree] lemma scale_roots_eval₂_mul {p : S[X]} (f : S →+* R) (r : R) (s : S) : eval₂ f (f s * r) (scale_roots p s) = f s ^ p.nat_degree * eval₂ f r p := calc eval₂ f (f s * r) (scale_roots p s) = (scale_roots p s).support.sum (λ i, f (coeff p i * s ^ (p.nat_degree - i)) * (f s * r) ^ i) : by simp [eval₂_eq_sum, sum_def] ... = p.support.sum (λ i, f (coeff p i * s ^ (p.nat_degree - i)) * (f s * r) ^ i) : finset.sum_subset (support_scale_roots_le p s) (λ i hi hi', let this : coeff p i * s ^ (p.nat_degree - i) = 0 := by simpa using hi' in by simp [this]) ... = p.support.sum (λ (i : ℕ), f (p.coeff i) * f s ^ (p.nat_degree - i + i) * r ^ i) : finset.sum_congr rfl (λ i hi, by simp_rw [f.map_mul, f.map_pow, pow_add, mul_pow, mul_assoc]) ... = p.support.sum (λ (i : ℕ), f s ^ p.nat_degree * (f (p.coeff i) * r ^ i)) : finset.sum_congr rfl (λ i hi, by { rw [mul_assoc, mul_left_comm, tsub_add_cancel_of_le], exact le_nat_degree_of_ne_zero (polynomial.mem_support_iff.mp hi) }) ... = f s ^ p.nat_degree * p.support.sum (λ (i : ℕ), (f (p.coeff i) * r ^ i)) : finset.mul_sum.symm ... = f s ^ p.nat_degree * eval₂ f r p : by { simp [eval₂_eq_sum, sum_def] } lemma scale_roots_eval₂_eq_zero {p : S[X]} (f : S →+* R) {r : R} {s : S} (hr : eval₂ f r p = 0) : eval₂ f (f s * r) (scale_roots p s) = 0 := by rw [scale_roots_eval₂_mul, hr, _root_.mul_zero] lemma scale_roots_aeval_eq_zero [algebra S R] {p : S[X]} {r : R} {s : S} (hr : aeval r p = 0) : aeval (algebra_map S R s * r) (scale_roots p s) = 0 := scale_roots_eval₂_eq_zero (algebra_map S R) hr lemma scale_roots_eval₂_eq_zero_of_eval₂_div_eq_zero {p : A[X]} {f : A →+* K} (hf : function.injective f) {r s : A} (hr : eval₂ f (f r / f s) p = 0) (hs : s ∈ non_zero_divisors A) : eval₂ f (f r) (scale_roots p s) = 0 := begin convert scale_roots_eval₂_eq_zero f hr, rw [←mul_div_assoc, mul_comm, mul_div_cancel], exact map_ne_zero_of_mem_non_zero_divisors _ hf hs end lemma scale_roots_aeval_eq_zero_of_aeval_div_eq_zero [algebra A K] (inj : function.injective (algebra_map A K)) {p : A[X]} {r s : A} (hr : aeval (algebra_map A K r / algebra_map A K s) p = 0) (hs : s ∈ non_zero_divisors A) : aeval (algebra_map A K r) (scale_roots p s) = 0 := scale_roots_eval₂_eq_zero_of_eval₂_div_eq_zero inj hr hs lemma map_scale_roots (p : R[X]) (x : R) (f : R →+* S) (h : f p.leading_coeff ≠ 0) : (p.scale_roots x).map f = (p.map f).scale_roots (f x) := begin ext, simp [polynomial.nat_degree_map_of_leading_coeff_ne_zero _ h], end end polynomial
eb546ed5ac2e74eec4a4d308a68b0d52105a3d12
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/category/default.lean
2b74e64f59d233482c21f43af63b142404d0b2f4
[]
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
10,849
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Stephen Morgan, Scott Morrison, Johannes Hölzl, Reid Barton -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.tactic.basic import Mathlib.PostPort universes v u l u' namespace Mathlib /-! # Categories Defines a category, as a type class parametrised by the type of objects. ## Notations Introduces notations * `X ⟶ Y` for the morphism spaces, * `f ≫ g` for composition in the 'arrows' convention. Users may like to add `f ⊚ g` for composition in the standard convention, using ```lean local notation f ` ⊚ `:80 g:80 := category.comp g f -- type as \oo ``` -/ -- The order in this declaration matters: v often needs to be explicitly specified while u often -- can be omitted namespace category_theory /-- A 'notation typeclass' on the way to defining a category. -/ class has_hom (obj : Type u) where hom : obj → obj → Type v infixr:10 " ⟶ " => Mathlib.category_theory.has_hom.hom /-- A preliminary structure on the way to defining a category, containing the data, but none of the axioms. -/ class category_struct (obj : Type u) extends has_hom obj where id : (X : obj) → X ⟶ X comp : {X Y Z : obj} → (X ⟶ Y) → (Y ⟶ Z) → (X ⟶ Z) notation:1024 "𝟙" => Mathlib.category_theory.category_struct.id infixr:80 " ≫ " => Mathlib.category_theory.category_struct.comp /-- The typeclass `category C` describes morphisms associated to objects of type `C`. The universe levels of the objects and morphisms are unconstrained, and will often need to be specified explicitly, as `category.{v} C`. (See also `large_category` and `small_category`.) See https://stacks.math.columbia.edu/tag/0014. -/ class category (obj : Type u) extends category_struct obj where id_comp' : autoParam (∀ {X Y : obj} (f : X ⟶ Y), 𝟙 ≫ f = f) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.obviously") (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "obviously") []) comp_id' : autoParam (∀ {X Y : obj} (f : X ⟶ Y), f ≫ 𝟙 = f) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.obviously") (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "obviously") []) assoc' : autoParam (∀ {W X Y Z : obj} (f : W ⟶ X) (g : X ⟶ Y) (h : Y ⟶ Z), (f ≫ g) ≫ h = f ≫ g ≫ h) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.obviously") (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "obviously") []) -- `restate_axiom` is a command that creates a lemma from a structure field, -- discarding any auto_param wrappers from the type. -- (It removes a backtick from the name, if it finds one, and otherwise adds "_lemma".) @[simp] theorem category.id_comp {obj : Type u} [c : category obj] {X : obj} {Y : obj} (f : X ⟶ Y) : 𝟙 ≫ f = f := sorry @[simp] theorem category.comp_id {obj : Type u} [c : category obj] {X : obj} {Y : obj} (f : X ⟶ Y) : f ≫ 𝟙 = f := sorry @[simp] theorem category.assoc {obj : Type u} [c : category obj] {W : obj} {X : obj} {Y : obj} {Z : obj} (f : W ⟶ X) (g : X ⟶ Y) (h : Y ⟶ Z) : (f ≫ g) ≫ h = f ≫ g ≫ h := sorry /-- A `large_category` has objects in one universe level higher than the universe level of the morphisms. It is useful for examples such as the category of types, or the category of groups, etc. -/ /-- def large_category (C : Type (u + 1)) := category C A `small_category` has objects and morphisms in the same universe level. -/ def small_category (C : Type u) := category C /-- postcompose an equation between morphisms by another morphism -/ theorem eq_whisker {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : X ⟶ Y} (w : f = g) (h : Y ⟶ Z) : f ≫ h = g ≫ h := eq.mpr (id (Eq._oldrec (Eq.refl (f ≫ h = g ≫ h)) w)) (Eq.refl (g ≫ h)) /-- precompose an equation between morphisms by another morphism -/ theorem whisker_eq {C : Type u} [category C] {X : C} {Y : C} {Z : C} (f : X ⟶ Y) {g : Y ⟶ Z} {h : Y ⟶ Z} (w : g = h) : f ≫ g = f ≫ h := eq.mpr (id (Eq._oldrec (Eq.refl (f ≫ g = f ≫ h)) w)) (Eq.refl (f ≫ h)) infixr:80 " =≫ " => Mathlib.category_theory.eq_whisker infixr:80 " ≫= " => Mathlib.category_theory.whisker_eq theorem eq_of_comp_left_eq {C : Type u} [category C] {X : C} {Y : C} {f : X ⟶ Y} {g : X ⟶ Y} (w : ∀ {Z : C} (h : Y ⟶ Z), f ≫ h = g ≫ h) : f = g := sorry theorem eq_of_comp_right_eq {C : Type u} [category C] {Y : C} {Z : C} {f : Y ⟶ Z} {g : Y ⟶ Z} (w : ∀ {X : C} (h : X ⟶ Y), h ≫ f = h ≫ g) : f = g := sorry theorem eq_of_comp_left_eq' {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) (g : X ⟶ Y) (w : (fun {Z : C} (h : Y ⟶ Z) => f ≫ h) = fun {Z : C} (h : Y ⟶ Z) => g ≫ h) : f = g := sorry theorem eq_of_comp_right_eq' {C : Type u} [category C] {Y : C} {Z : C} (f : Y ⟶ Z) (g : Y ⟶ Z) (w : (fun {X : C} (h : X ⟶ Y) => h ≫ f) = fun {X : C} (h : X ⟶ Y) => h ≫ g) : f = g := sorry theorem id_of_comp_left_id {C : Type u} [category C] {X : C} (f : X ⟶ X) (w : ∀ {Y : C} (g : X ⟶ Y), f ≫ g = g) : f = 𝟙 := sorry theorem id_of_comp_right_id {C : Type u} [category C] {X : C} (f : X ⟶ X) (w : ∀ {Y : C} (g : Y ⟶ X), g ≫ f = g) : f = 𝟙 := sorry theorem comp_dite {C : Type u} [category C] {P : Prop} [Decidable P] {X : C} {Y : C} {Z : C} (f : X ⟶ Y) (g : P → (Y ⟶ Z)) (g' : ¬P → (Y ⟶ Z)) : (f ≫ dite P (fun (h : P) => g h) fun (h : ¬P) => g' h) = dite P (fun (h : P) => f ≫ g h) fun (h : ¬P) => f ≫ g' h := sorry theorem dite_comp {C : Type u} [category C] {P : Prop} [Decidable P] {X : C} {Y : C} {Z : C} (f : P → (X ⟶ Y)) (f' : ¬P → (X ⟶ Y)) (g : Y ⟶ Z) : (dite P (fun (h : P) => f h) fun (h : ¬P) => f' h) ≫ g = dite P (fun (h : P) => f h ≫ g) fun (h : ¬P) => f' h ≫ g := sorry /-- A morphism `f` is an epimorphism if it can be "cancelled" when precomposed: `f ≫ g = f ≫ h` implies `g = h`. See https://stacks.math.columbia.edu/tag/003B. -/ class epi {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) where left_cancellation : ∀ {Z : C} (g h : Y ⟶ Z), f ≫ g = f ≫ h → g = h /-- A morphism `f` is a monomorphism if it can be "cancelled" when postcomposed: `g ≫ f = h ≫ f` implies `g = h`. See https://stacks.math.columbia.edu/tag/003B. -/ class mono {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) where right_cancellation : ∀ {Z : C} (g h : Z ⟶ X), g ≫ f = h ≫ f → g = h protected instance category_struct.id.epi {C : Type u} [category C] (X : C) : epi 𝟙 := epi.mk fun (Z : C) (g h : X ⟶ Z) (w : 𝟙 ≫ g = 𝟙 ≫ h) => eq.mpr (id (Eq.refl (g = h))) (eq.mp ((fun (a a_1 : X ⟶ Z) (e_1 : a = a_1) (ᾰ ᾰ_1 : X ⟶ Z) (e_2 : ᾰ = ᾰ_1) => congr (congr_arg Eq e_1) e_2) (𝟙 ≫ g) g (category.id_comp g) (𝟙 ≫ h) h (category.id_comp h)) w) protected instance category_struct.id.mono {C : Type u} [category C] (X : C) : mono 𝟙 := mono.mk fun (Z : C) (g h : Z ⟶ X) (w : g ≫ 𝟙 = h ≫ 𝟙) => eq.mpr (id (Eq.refl (g = h))) (eq.mp ((fun (a a_1 : Z ⟶ X) (e_1 : a = a_1) (ᾰ ᾰ_1 : Z ⟶ X) (e_2 : ᾰ = ᾰ_1) => congr (congr_arg Eq e_1) e_2) (g ≫ 𝟙) g (category.comp_id g) (h ≫ 𝟙) h (category.comp_id h)) w) theorem cancel_epi {C : Type u} [category C] {X : C} {Y : C} {Z : C} (f : X ⟶ Y) [epi f] {g : Y ⟶ Z} {h : Y ⟶ Z} : f ≫ g = f ≫ h ↔ g = h := { mp := fun (p : f ≫ g = f ≫ h) => epi.left_cancellation g h p, mpr := fun (a : g = h) => Eq._oldrec (Eq.refl (f ≫ g)) a } theorem cancel_mono {C : Type u} [category C] {X : C} {Y : C} {Z : C} (f : X ⟶ Y) [mono f] {g : Z ⟶ X} {h : Z ⟶ X} : g ≫ f = h ≫ f ↔ g = h := { mp := fun (p : g ≫ f = h ≫ f) => mono.right_cancellation g h p, mpr := fun (a : g = h) => Eq._oldrec (Eq.refl (g ≫ f)) a } theorem cancel_epi_id {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) [epi f] {h : Y ⟶ Y} : f ≫ h = f ↔ h = 𝟙 := sorry theorem cancel_mono_id {C : Type u} [category C] {X : C} {Y : C} (f : X ⟶ Y) [mono f] {g : X ⟶ X} : g ≫ f = f ↔ g = 𝟙 := sorry theorem epi_comp {C : Type u} [category C] {X : C} {Y : C} {Z : C} (f : X ⟶ Y) [epi f] (g : Y ⟶ Z) [epi g] : epi (f ≫ g) := sorry theorem mono_comp {C : Type u} [category C] {X : C} {Y : C} {Z : C} (f : X ⟶ Y) [mono f] (g : Y ⟶ Z) [mono g] : mono (f ≫ g) := sorry theorem mono_of_mono {C : Type u} [category C] {X : C} {Y : C} {Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) [mono (f ≫ g)] : mono f := sorry theorem mono_of_mono_fac {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : Y ⟶ Z} {h : X ⟶ Z} [mono h] (w : f ≫ g = h) : mono f := Eq._oldrec (mono_of_mono f g) w _inst_2 theorem epi_of_epi {C : Type u} [category C] {X : C} {Y : C} {Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) [epi (f ≫ g)] : epi g := sorry theorem epi_of_epi_fac {C : Type u} [category C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : Y ⟶ Z} {h : X ⟶ Z} [epi h] (w : f ≫ g = h) : epi g := Eq._oldrec (epi_of_epi f g) w _inst_2 protected instance ulift_category (C : Type u) [category C] : category (ulift C) := category.mk -- We verify that this previous instance can lift small categories to large categories. end category_theory /-! We now put a category instance on any preorder. Because we do not allow the morphisms of a category to live in `Prop`, unfortunately we need to use `plift` and `ulift` when defining the morphisms. As convenience functions, we provide `hom_of_le` and `le_of_hom` to wrap and unwrap inequalities. -/ namespace preorder /-- The category structure coming from a preorder. There is a morphism `X ⟶ Y` if and only if `X ≤ Y`. Because we don't allow morphisms to live in `Prop`, we have to define `X ⟶ Y` as `ulift (plift (X ≤ Y))`. See `category_theory.hom_of_le` and `category_theory.le_of_hom`. See https://stacks.math.columbia.edu/tag/00D3. -/ protected instance small_category (α : Type u) [preorder α] : category_theory.small_category α := category_theory.category.mk end preorder namespace category_theory /-- Express an inequality as a morphism in the corresponding preorder category. -/ def hom_of_le {α : Type u} [preorder α] {U : α} {V : α} (h : U ≤ V) : U ⟶ V := ulift.up (plift.up h) /-- Extract the underlying inequality from a morphism in a preorder category. -/ theorem le_of_hom {α : Type u} [preorder α] {U : α} {V : α} (h : U ⟶ V) : U ≤ V := plift.down (ulift.down h) end category_theory /-- Many proofs in the category theory library use the `dsimp, simp` pattern,
9ab1435cf7eca75b7c0c3b2d888d93cc3eb14c84
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/measure_theory/decomposition/jordan.lean
dcaae29fdc4ee89d6a829c5caf9a6185ba7e192f
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
16,591
lean
/- Copyright (c) 2021 Kexing Ying. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kexing Ying -/ import measure_theory.decomposition.signed_hahn /-! # Jordan decomposition This file proves the existence and uniqueness of the Jordan decomposition for signed measures. The Jordan decomposition theorem states that, given a signed measure `s`, there exists a unique pair of mutually singular measures `μ` and `ν`, such that `s = μ - ν`. The Jordan decomposition theorem for measures is a corollary of the Hahn decomposition theorem and is useful for the Lebesgue decomposition theorem. ## Main definitions * `measure_theory.jordan_decomposition`: a Jordan decomposition of a measurable space is a pair of mutually singular finite measures. We say `j` is a Jordan decomposition of a signed meausre `s` if `s = j.pos_part - j.neg_part`. * `measure_theory.signed_measure.to_jordan_decomposition`: the Jordan decomposition of a signed measure. * `measure_theory.signed_measure.to_jordan_decomposition_equiv`: is the `equiv` between `measure_theory.signed_measure` and `measure_theory.jordan_decomposition` formed by `measure_theory.signed_measure.to_jordan_decomposition`. ## Main results * `measure_theory.signed_measure.to_signed_measure_to_jordan_decomposition` : the Jordan decomposition theorem. * `measure_theory.jordan_decomposition.to_signed_measure_injective` : the Jordan decomposition of a signed measure is unique. ## Tags Jordan decomposition theorem -/ noncomputable theory open_locale classical measure_theory ennreal variables {α β : Type*} [measurable_space α] namespace measure_theory /-- A Jordan decomposition of a measurable space is a pair of mutually singular, finite measures. -/ @[ext] structure jordan_decomposition (α : Type*) [measurable_space α] := (pos_part neg_part : measure α) [pos_part_finite : finite_measure pos_part] [neg_part_finite : finite_measure neg_part] (mutually_singular : pos_part ⊥ₘ neg_part) attribute [instance] jordan_decomposition.pos_part_finite attribute [instance] jordan_decomposition.neg_part_finite namespace jordan_decomposition open measure vector_measure variable (j : jordan_decomposition α) instance : has_zero (jordan_decomposition α) := { zero := ⟨0, 0, mutually_singular.zero⟩ } instance : inhabited (jordan_decomposition α) := { default := 0 } instance : has_neg (jordan_decomposition α) := { neg := λ j, ⟨j.neg_part, j.pos_part, j.mutually_singular.symm⟩ } @[simp] lemma zero_pos_part : (0 : jordan_decomposition α).pos_part = 0 := rfl @[simp] lemma zero_neg_part : (0 : jordan_decomposition α).neg_part = 0 := rfl @[simp] lemma neg_pos_part : (-j).pos_part = j.neg_part := rfl @[simp] lemma neg_neg_part : (-j).neg_part = j.pos_part := rfl /-- The signed measure associated with a Jordan decomposition. -/ def to_signed_measure : signed_measure α := j.pos_part.to_signed_measure - j.neg_part.to_signed_measure lemma to_signed_measure_zero : (0 : jordan_decomposition α).to_signed_measure = 0 := begin ext1 i hi, erw [to_signed_measure, to_signed_measure_sub_apply hi, sub_self, zero_apply], end lemma to_signed_measure_neg : (-j).to_signed_measure = -j.to_signed_measure := begin ext1 i hi, rw [neg_apply, to_signed_measure, to_signed_measure, to_signed_measure_sub_apply hi, to_signed_measure_sub_apply hi, neg_sub], refl, end /-- A Jordan decomposition provides a Hahn decomposition. -/ lemma exists_compl_positive_negative : ∃ S : set α, measurable_set S ∧ j.to_signed_measure ≤[S] 0 ∧ 0 ≤[Sᶜ] j.to_signed_measure ∧ j.pos_part S = 0 ∧ j.neg_part Sᶜ = 0 := begin obtain ⟨S, hS₁, hS₂, hS₃⟩ := j.mutually_singular, refine ⟨S, hS₁, _, _, hS₂, hS₃⟩, { refine restrict_le_restrict_of_subset_le _ _ (λ A hA hA₁, _), rw [to_signed_measure, to_signed_measure_sub_apply hA, show j.pos_part A = 0, by exact nonpos_iff_eq_zero.1 (hS₂ ▸ measure_mono hA₁), ennreal.zero_to_real, zero_sub, neg_le, zero_apply, neg_zero], exact ennreal.to_real_nonneg }, { refine restrict_le_restrict_of_subset_le _ _ (λ A hA hA₁, _), rw [to_signed_measure, to_signed_measure_sub_apply hA, show j.neg_part A = 0, by exact nonpos_iff_eq_zero.1 (hS₃ ▸ measure_mono hA₁), ennreal.zero_to_real, sub_zero], exact ennreal.to_real_nonneg }, end end jordan_decomposition namespace signed_measure open measure vector_measure jordan_decomposition classical variables {s : signed_measure α} {μ ν : measure α} [finite_measure μ] [finite_measure ν] /-- Given a signed measure `s`, `s.to_jordan_decomposition` is the Jordan decomposition `j`, such that `s = j.to_signed_measure`. This property is known as the Jordan decomposition theorem, and is shown by `measure_theory.signed_measure.to_signed_measure_to_jordan_decomposition`. -/ def to_jordan_decomposition (s : signed_measure α) : jordan_decomposition α := let i := some s.exists_compl_positive_negative in let hi := some_spec s.exists_compl_positive_negative in { pos_part := s.to_measure_of_zero_le i hi.1 hi.2.1, neg_part := s.to_measure_of_le_zero iᶜ hi.1.compl hi.2.2, pos_part_finite := infer_instance, neg_part_finite := infer_instance, mutually_singular := begin refine ⟨iᶜ, hi.1.compl, _, _⟩, { rw [to_measure_of_zero_le_apply _ _ hi.1 hi.1.compl], simpa }, { rw [to_measure_of_le_zero_apply _ _ hi.1.compl hi.1.compl.compl], simpa } end } lemma to_jordan_decomposition_spec (s : signed_measure α) : ∃ (i : set α) (hi₁ : measurable_set i) (hi₂ : 0 ≤[i] s) (hi₃ : s ≤[iᶜ] 0), s.to_jordan_decomposition.pos_part = s.to_measure_of_zero_le i hi₁ hi₂ ∧ s.to_jordan_decomposition.neg_part = s.to_measure_of_le_zero iᶜ hi₁.compl hi₃ := begin set i := some s.exists_compl_positive_negative, obtain ⟨hi₁, hi₂, hi₃⟩ := some_spec s.exists_compl_positive_negative, exact ⟨i, hi₁, hi₂, hi₃, rfl, rfl⟩, end /-- **The Jordan decomposition theorem**: Given a signed measure `s`, there exists a pair of mutually singular measures `μ` and `ν` such that `s = μ - ν`. In this case, the measures `μ` and `ν` are given by `s.to_jordan_decomposition.pos_part` and `s.to_jordan_decomposition.neg_part` respectively. Note that we use `measure_theory.jordan_decomposition.to_signed_measure` to represent the signed measure corresponding to `s.to_jordan_decomposition.pos_part - s.to_jordan_decomposition.neg_part`. -/ @[simp] lemma to_signed_measure_to_jordan_decomposition (s : signed_measure α) : s.to_jordan_decomposition.to_signed_measure = s := begin obtain ⟨i, hi₁, hi₂, hi₃, hμ, hν⟩ := s.to_jordan_decomposition_spec, simp only [jordan_decomposition.to_signed_measure, hμ, hν], ext k hk, rw [to_signed_measure_sub_apply hk, to_measure_of_zero_le_apply _ hi₂ hi₁ hk, to_measure_of_le_zero_apply _ hi₃ hi₁.compl hk], simp only [ennreal.coe_to_real, subtype.coe_mk, ennreal.some_eq_coe, sub_neg_eq_add], rw [← of_union _ (measurable_set.inter hi₁ hk) (measurable_set.inter hi₁.compl hk), set.inter_comm i, set.inter_comm iᶜ, set.inter_union_compl _ _], { apply_instance }, { rintro x ⟨⟨hx₁, _⟩, hx₂, _⟩, exact false.elim (hx₂ hx₁) } end section variables {u v w : set α} /-- A subset `v` of a null-set `w` has zero measure if `w` is a subset of a positive set `u`. -/ lemma subset_positive_null_set (hu : measurable_set u) (hv : measurable_set v) (hw : measurable_set w) (hsu : 0 ≤[u] s) (hw₁ : s w = 0) (hw₂ : w ⊆ u) (hwt : v ⊆ w) : s v = 0 := begin have : s v + s (w \ v) = 0, { rw [← hw₁, ← of_union set.disjoint_diff hv (hw.diff hv), set.union_diff_self, set.union_eq_self_of_subset_left hwt], apply_instance }, have h₁ := nonneg_of_zero_le_restrict _ (restrict_le_restrict_subset _ _ hu hsu (hwt.trans hw₂)), have h₂ := nonneg_of_zero_le_restrict _ (restrict_le_restrict_subset _ _ hu hsu ((w.diff_subset v).trans hw₂)), linarith, end /-- A subset `v` of a null-set `w` has zero measure if `w` is a subset of a negative set `u`. -/ lemma subset_negative_null_set (hu : measurable_set u) (hv : measurable_set v) (hw : measurable_set w) (hsu : s ≤[u] 0) (hw₁ : s w = 0) (hw₂ : w ⊆ u) (hwt : v ⊆ w) : s v = 0 := begin rw [← s.neg_le_neg_iff _ hu, neg_zero] at hsu, have := subset_positive_null_set hu hv hw hsu, simp only [pi.neg_apply, neg_eq_zero, coe_neg] at this, exact this hw₁ hw₂ hwt, end /-- If the symmetric difference of two positive sets is a null-set, then so are the differences between the two sets. -/ lemma of_diff_eq_zero_of_symm_diff_eq_zero_positive (hu : measurable_set u) (hv : measurable_set v) (hsu : 0 ≤[u] s) (hsv : 0 ≤[v] s) (hs : s (u Δ v) = 0) : s (u \ v) = 0 ∧ s (v \ u) = 0 := begin rw restrict_le_restrict_iff at hsu hsv, have a := hsu (hu.diff hv) (u.diff_subset v), have b := hsv (hv.diff hu) (v.diff_subset u), erw [of_union (set.disjoint_of_subset_left (u.diff_subset v) set.disjoint_diff) (hu.diff hv) (hv.diff hu)] at hs, rw zero_apply at a b, split, all_goals { linarith <|> apply_instance <|> assumption }, end /-- If the symmetric difference of two negative sets is a null-set, then so are the differences between the two sets. -/ lemma of_diff_eq_zero_of_symm_diff_eq_zero_negative (hu : measurable_set u) (hv : measurable_set v) (hsu : s ≤[u] 0) (hsv : s ≤[v] 0) (hs : s (u Δ v) = 0) : s (u \ v) = 0 ∧ s (v \ u) = 0 := begin rw [← s.neg_le_neg_iff _ hu, neg_zero] at hsu, rw [← s.neg_le_neg_iff _ hv, neg_zero] at hsv, have := of_diff_eq_zero_of_symm_diff_eq_zero_positive hu hv hsu hsv, simp only [pi.neg_apply, neg_eq_zero, coe_neg] at this, exact this hs, end lemma of_inter_eq_of_symm_diff_eq_zero_positive (hu : measurable_set u) (hv : measurable_set v) (hw : measurable_set w) (hsu : 0 ≤[u] s) (hsv : 0 ≤[v] s) (hs : s (u Δ v) = 0) : s (w ∩ u) = s (w ∩ v) := begin have hwuv : s ((w ∩ u) Δ (w ∩ v)) = 0, { refine subset_positive_null_set (hu.union hv) ((hw.inter hu).symm_diff (hw.inter hv)) (hu.symm_diff hv) (restrict_le_restrict_union _ _ hu hsu hv hsv) hs _ _, { exact symm_diff_le_sup u v }, { rintro x (⟨⟨hxw, hxu⟩, hx⟩ | ⟨⟨hxw, hxv⟩, hx⟩); rw [set.mem_inter_eq, not_and] at hx, { exact or.inl ⟨hxu, hx hxw⟩ }, { exact or.inr ⟨hxv, hx hxw⟩ } } }, obtain ⟨huv, hvu⟩ := of_diff_eq_zero_of_symm_diff_eq_zero_positive (hw.inter hu) (hw.inter hv) (restrict_le_restrict_subset _ _ hu hsu (w.inter_subset_right u)) (restrict_le_restrict_subset _ _ hv hsv (w.inter_subset_right v)) hwuv, rw [← of_diff_of_diff_eq_zero (hw.inter hu) (hw.inter hv) hvu, huv, zero_add] end lemma of_inter_eq_of_symm_diff_eq_zero_negative (hu : measurable_set u) (hv : measurable_set v) (hw : measurable_set w) (hsu : s ≤[u] 0) (hsv : s ≤[v] 0) (hs : s (u Δ v) = 0) : s (w ∩ u) = s (w ∩ v) := begin rw [← s.neg_le_neg_iff _ hu, neg_zero] at hsu, rw [← s.neg_le_neg_iff _ hv, neg_zero] at hsv, have := of_inter_eq_of_symm_diff_eq_zero_positive hu hv hw hsu hsv, simp only [pi.neg_apply, neg_inj, neg_eq_zero, coe_neg] at this, exact this hs, end end end signed_measure namespace jordan_decomposition open measure vector_measure signed_measure function private lemma eq_of_pos_part_eq_pos_part {j₁ j₂ : jordan_decomposition α} (hj : j₁.pos_part = j₂.pos_part) (hj' : j₁.to_signed_measure = j₂.to_signed_measure) : j₁ = j₂ := begin ext1, { exact hj }, { rw ← to_signed_measure_eq_to_signed_measure_iff, suffices : j₁.pos_part.to_signed_measure - j₁.neg_part.to_signed_measure = j₁.pos_part.to_signed_measure - j₂.neg_part.to_signed_measure, { exact sub_right_inj.mp this }, convert hj' } end /-- The Jordan decomposition of a signed measure is unique. -/ theorem to_signed_measure_injective : injective $ @jordan_decomposition.to_signed_measure α _ := begin /- The main idea is that two Jordan decompositions of a signed measure provide two Hahn decompositions for that measure. Then, from `of_symm_diff_compl_positive_negative`, the symmetric difference of the two Hahn decompositions has measure zero, thus, allowing us to show the equality of the underlying measures of the Jordan decompositions. -/ intros j₁ j₂ hj, -- obtain the two Hahn decompositions from the Jordan decompositions obtain ⟨S, hS₁, hS₂, hS₃, hS₄, hS₅⟩ := j₁.exists_compl_positive_negative, obtain ⟨T, hT₁, hT₂, hT₃, hT₄, hT₅⟩ := j₂.exists_compl_positive_negative, rw ← hj at hT₂ hT₃, -- the symmetric differences of the two Hahn decompositions have measure zero obtain ⟨hST₁, -⟩ := of_symm_diff_compl_positive_negative hS₁.compl hT₁.compl ⟨hS₃, (compl_compl S).symm ▸ hS₂⟩ ⟨hT₃, (compl_compl T).symm ▸ hT₂⟩, -- it suffices to show the Jordan decompositions have the same positive parts refine eq_of_pos_part_eq_pos_part _ hj, ext1 i hi, -- we see that the positive parts of the two Jordan decompositions are equal to their -- associated signed measures restricted on their associated Hahn decompositions have hμ₁ : (j₁.pos_part i).to_real = j₁.to_signed_measure (i ∩ Sᶜ), { rw [to_signed_measure, to_signed_measure_sub_apply (hi.inter hS₁.compl), show j₁.neg_part (i ∩ Sᶜ) = 0, by exact nonpos_iff_eq_zero.1 (hS₅ ▸ measure_mono (set.inter_subset_right _ _)), ennreal.zero_to_real, sub_zero], conv_lhs { rw ← set.inter_union_compl i S }, rw [measure_union, show j₁.pos_part (i ∩ S) = 0, by exact nonpos_iff_eq_zero.1 (hS₄ ▸ measure_mono (set.inter_subset_right _ _)), zero_add], { refine set.disjoint_of_subset_left (set.inter_subset_right _ _) (set.disjoint_of_subset_right (set.inter_subset_right _ _) disjoint_compl_right) }, { exact hi.inter hS₁ }, { exact hi.inter hS₁.compl } }, have hμ₂ : (j₂.pos_part i).to_real = j₂.to_signed_measure (i ∩ Tᶜ), { rw [to_signed_measure, to_signed_measure_sub_apply (hi.inter hT₁.compl), show j₂.neg_part (i ∩ Tᶜ) = 0, by exact nonpos_iff_eq_zero.1 (hT₅ ▸ measure_mono (set.inter_subset_right _ _)), ennreal.zero_to_real, sub_zero], conv_lhs { rw ← set.inter_union_compl i T }, rw [measure_union, show j₂.pos_part (i ∩ T) = 0, by exact nonpos_iff_eq_zero.1 (hT₄ ▸ measure_mono (set.inter_subset_right _ _)), zero_add], { exact set.disjoint_of_subset_left (set.inter_subset_right _ _) (set.disjoint_of_subset_right (set.inter_subset_right _ _) disjoint_compl_right) }, { exact hi.inter hT₁ }, { exact hi.inter hT₁.compl } }, -- since the two signed measures associated with the Jordan decompositions are the same, -- and the symmetric difference of the Hahn decompositions have measure zero, the result follows rw [← ennreal.to_real_eq_to_real (measure_lt_top _ _) (measure_lt_top _ _), hμ₁, hμ₂, ← hj], exact of_inter_eq_of_symm_diff_eq_zero_positive hS₁.compl hT₁.compl hi hS₃ hT₃ hST₁, all_goals { apply_instance }, end @[simp] lemma to_jordan_decomposition_to_signed_measure (j : jordan_decomposition α) : (j.to_signed_measure).to_jordan_decomposition = j := (@to_signed_measure_injective _ _ j (j.to_signed_measure).to_jordan_decomposition (by simp)).symm end jordan_decomposition namespace signed_measure open jordan_decomposition /-- `measure_theory.signed_measure.to_jordan_decomposition` and `measure_theory.jordan_decomposition.to_signed_measure` form a `equiv`. -/ @[simps apply symm_apply] def to_jordan_decomposition_equiv (α : Type*) [measurable_space α] : signed_measure α ≃ jordan_decomposition α := { to_fun := to_jordan_decomposition, inv_fun := to_signed_measure, left_inv := to_signed_measure_to_jordan_decomposition, right_inv := to_jordan_decomposition_to_signed_measure } lemma to_jordan_decomposition_zero : (0 : signed_measure α).to_jordan_decomposition = 0 := begin apply to_signed_measure_injective, simp [to_signed_measure_zero], end lemma to_jordan_decomposition_neg (s : signed_measure α) : (-s).to_jordan_decomposition = -s.to_jordan_decomposition := begin apply to_signed_measure_injective, simp [to_signed_measure_neg], end end signed_measure end measure_theory
58759286e01147dcf9dde0e67a9ec837f5b8a2db
d406927ab5617694ec9ea7001f101b7c9e3d9702
/archive/imo/imo1987_q1.lean
b4809852dd33bd83006d1d75368574708980818a
[ "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,179
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 data.fintype.big_operators import data.fintype.perm import data.fintype.prod import dynamics.fixed_points.basic /-! # Formalization of IMO 1987, Q1 Let $p_{n, k}$ be the number of permutations of a set of cardinality `n ≥ 1` that fix exactly `k` elements. Prove that $∑_{k=0}^n k p_{n,k}=n!$. To prove this identity, we show that both sides are equal to the cardinality of the set `{(x : α, σ : perm α) | σ x = x}`, regrouping by `card (fixed_points σ)` for the left hand side and by `x` for the right hand side. The original problem assumes `n ≥ 1`. It turns out that a version with `n * (n - 1)!` in the RHS holds true for `n = 0` as well, so we first prove it, then deduce the original version in the case `n ≥ 1`. -/ variables (α : Type*) [fintype α] [decidable_eq α] open_locale big_operators nat open equiv fintype function finset (range sum_const) set (Iic) namespace imo_1987_q1 /-- The set of pairs `(x : α, σ : perm α)` such that `σ x = x` is equivalent to the set of pairs `(x : α, σ : perm {x}ᶜ)`. -/ def fixed_points_equiv : {σx : α × perm α // σx.2 σx.1 = σx.1} ≃ Σ x : α, perm ({x}ᶜ : set α) := calc {σx : α × perm α // σx.2 σx.1 = σx.1} ≃ Σ x : α, {σ : perm α // σ x = x} : set_prod_equiv_sigma _ ... ≃ Σ x : α, {σ : perm α // ∀ y : ({x} : set α), σ y = equiv.refl ↥({x} : set α) y} : sigma_congr_right (λ x, equiv.set.of_eq $ by { simp only [set_coe.forall], dsimp, simp }) ... ≃ Σ x : α, perm ({x}ᶜ : set α) : sigma_congr_right (λ x, by apply equiv.set.compl) theorem card_fixed_points : card {σx : α × perm α // σx.2 σx.1 = σx.1} = card α * (card α - 1)! := by simp [card_congr (fixed_points_equiv α), card_perm, finset.filter_not, finset.card_sdiff, finset.filter_eq', finset.card_univ] /-- Given `α : Type*` and `k : ℕ`, `fiber α k` is the set of permutations of `α` with exactly `k` fixed points. -/ @[derive fintype] def fiber (k : ℕ) : set (perm α) := {σ : perm α | card (fixed_points σ) = k} @[simp] lemma mem_fiber {σ : perm α} {k : ℕ} : σ ∈ fiber α k ↔ card (fixed_points σ) = k := iff.rfl /-- `p α k` is the number of permutations of `α` with exactly `k` fixed points. -/ def p (k : ℕ) := card (fiber α k) /-- The set of triples `(k ≤ card α, σ ∈ fiber α k, x ∈ fixed_points σ)` is equivalent to the set of pairs `(x : α, σ : perm α)` such that `σ x = x`. The equivalence sends `(k, σ, x)` to `(x, σ)` and `(x, σ)` to `(card (fixed_points σ), σ, x)`. It is easy to see that the cardinality of the LHS is given by `∑ k : fin (card α + 1), k * p α k`. -/ def fixed_points_equiv' : (Σ (k : fin (card α + 1)) (σ : fiber α k), fixed_points σ.1) ≃ {σx : α × perm α // σx.2 σx.1 = σx.1} := { to_fun := λ p, ⟨⟨p.2.2, p.2.1⟩, p.2.2.2⟩, inv_fun := λ p, ⟨⟨card (fixed_points p.1.2), (card_subtype_le _).trans_lt (nat.lt_succ_self _)⟩, ⟨p.1.2, rfl⟩, ⟨p.1.1, p.2⟩⟩, left_inv := λ ⟨⟨k, hk⟩, ⟨σ, hσ⟩, ⟨x, hx⟩⟩, by { simp only [mem_fiber, fin.coe_mk] at hσ, subst k, refl }, right_inv := λ ⟨⟨x, σ⟩, h⟩, rfl } /-- Main statement for any `(α : Type*) [fintype α]`. -/ theorem main_fintype : ∑ k in range (card α + 1), k * p α k = card α * (card α - 1)! := have A : ∀ k (σ : fiber α k), card (fixed_points ⇑(↑σ : perm α)) = k := λ k σ, σ.2, by simpa [A, ← fin.sum_univ_eq_sum_range, -card_of_finset, finset.card_univ, card_fixed_points, mul_comm] using card_congr (fixed_points_equiv' α) /-- Main statement for permutations of `fin n`, a version that works for `n = 0`. -/ theorem main₀ (n : ℕ) : ∑ k in range (n + 1), k * p (fin n) k = n * (n - 1)! := by simpa using main_fintype (fin n) /-- Main statement for permutations of `fin n`. -/ theorem main {n : ℕ} (hn : 1 ≤ n) : ∑ k in range (n + 1), k * p (fin n) k = n! := by rw [main₀, nat.mul_factorial_pred (zero_lt_one.trans_le hn)] end imo_1987_q1
edd08d67bd8075a419710894f58d35869cbd5dd6
4727251e0cd73359b15b664c3170e5d754078599
/src/order/filter/cofinite.lean
38f8908f2dec682cfd833ab1bbd035385dfdfd14
[ "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
7,269
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, Jeremy Avigad, Yury Kudryashov -/ import order.filter.at_top_bot import order.filter.pi /-! # The cofinite filter In this file we define `cofinite`: the filter of sets with finite complement and prove its basic properties. In particular, we prove that for `ℕ` it is equal to `at_top`. ## TODO Define filters for other cardinalities of the complement. -/ open set function open_locale classical variables {ι α β : Type*} namespace filter /-- The cofinite filter is the filter of subsets whose complements are finite. -/ def cofinite : filter α := { sets := {s | finite sᶜ}, univ_sets := by simp only [compl_univ, finite_empty, mem_set_of_eq], sets_of_superset := assume s t (hs : finite sᶜ) (st: s ⊆ t), hs.subset $ compl_subset_compl.2 st, inter_sets := assume s t (hs : finite sᶜ) (ht : finite (tᶜ)), by simp only [compl_inter, finite.union, ht, hs, mem_set_of_eq] } @[simp] lemma mem_cofinite {s : set α} : s ∈ (@cofinite α) ↔ finite sᶜ := iff.rfl @[simp] lemma eventually_cofinite {p : α → Prop} : (∀ᶠ x in cofinite, p x) ↔ finite {x | ¬p x} := iff.rfl lemma has_basis_cofinite : has_basis cofinite (λ s : set α, s.finite) compl := ⟨λ s, ⟨λ h, ⟨sᶜ, h, (compl_compl s).subset⟩, λ ⟨t, htf, hts⟩, htf.subset $ compl_subset_comm.2 hts⟩⟩ instance cofinite_ne_bot [infinite α] : ne_bot (@cofinite α) := has_basis_cofinite.ne_bot_iff.2 $ λ s hs, hs.infinite_compl.nonempty lemma frequently_cofinite_iff_infinite {p : α → Prop} : (∃ᶠ x in cofinite, p x) ↔ set.infinite {x | p x} := by simp only [filter.frequently, filter.eventually, mem_cofinite, compl_set_of, not_not, set.infinite] lemma _root_.set.finite.compl_mem_cofinite {s : set α} (hs : s.finite) : sᶜ ∈ (@cofinite α) := mem_cofinite.2 $ (compl_compl s).symm ▸ hs lemma _root_.set.finite.eventually_cofinite_nmem {s : set α} (hs : s.finite) : ∀ᶠ x in cofinite, x ∉ s := hs.compl_mem_cofinite lemma _root_.finset.eventually_cofinite_nmem (s : finset α) : ∀ᶠ x in cofinite, x ∉ s := s.finite_to_set.eventually_cofinite_nmem lemma _root_.set.infinite_iff_frequently_cofinite {s : set α} : set.infinite s ↔ (∃ᶠ x in cofinite, x ∈ s) := frequently_cofinite_iff_infinite.symm lemma eventually_cofinite_ne (x : α) : ∀ᶠ a in cofinite, a ≠ x := (set.finite_singleton x).eventually_cofinite_nmem lemma le_cofinite_iff_compl_singleton_mem {l : filter α} : l ≤ cofinite ↔ ∀ x, {x}ᶜ ∈ l := begin refine ⟨λ h x, h (finite_singleton x).compl_mem_cofinite, λ h s (hs : sᶜ.finite), _⟩, rw [← compl_compl s, ← bUnion_of_singleton sᶜ, compl_Union₂,filter.bInter_mem hs], exact λ x _, h x end lemma le_cofinite_iff_eventually_ne {l : filter α} : l ≤ cofinite ↔ ∀ x, ∀ᶠ y in l, y ≠ x := le_cofinite_iff_compl_singleton_mem /-- If `α` is a preorder with no maximal element, then `at_top ≤ cofinite`. -/ lemma at_top_le_cofinite [preorder α] [no_max_order α] : (at_top : filter α) ≤ cofinite := le_cofinite_iff_eventually_ne.mpr eventually_ne_at_top lemma comap_cofinite_le (f : α → β) : comap f cofinite ≤ cofinite := le_cofinite_iff_eventually_ne.mpr $ λ x, mem_comap.2 ⟨{f x}ᶜ, (finite_singleton _).compl_mem_cofinite, λ y, ne_of_apply_ne f⟩ /-- The coproduct of the cofinite filters on two types is the cofinite filter on their product. -/ lemma coprod_cofinite : (cofinite : filter α).coprod (cofinite : filter β) = cofinite := filter.coext $ λ s, by simp only [compl_mem_coprod, mem_cofinite, compl_compl, finite_image_fst_and_snd_iff] /-- Finite product of finite sets is finite -/ lemma Coprod_cofinite {α : ι → Type*} [fintype ι] : filter.Coprod (λ i, (cofinite : filter (α i))) = cofinite := filter.coext $ λ s, by simp only [compl_mem_Coprod, mem_cofinite, compl_compl, forall_finite_image_eval_iff] end filter open filter /-- For natural numbers the filters `cofinite` and `at_top` coincide. -/ lemma nat.cofinite_eq_at_top : @cofinite ℕ = at_top := begin refine le_antisymm _ at_top_le_cofinite, refine at_top_basis.ge_iff.2 (λ N hN, _), simpa only [mem_cofinite, compl_Ici] using finite_lt_nat N end lemma nat.frequently_at_top_iff_infinite {p : ℕ → Prop} : (∃ᶠ n in at_top, p n) ↔ set.infinite {n | p n} := by rw [← nat.cofinite_eq_at_top, frequently_cofinite_iff_infinite] lemma filter.tendsto.exists_within_forall_le {α β : Type*} [linear_order β] {s : set α} (hs : s.nonempty) {f : α → β} (hf : filter.tendsto f filter.cofinite filter.at_top) : ∃ a₀ ∈ s, ∀ a ∈ s, f a₀ ≤ f a := begin rcases em (∃ y ∈ s, ∃ x, f y < x) with ⟨y, hys, x, hx⟩|not_all_top, { -- the set of points `{y | f y < x}` is nonempty and finite, so we take `min` over this set have : finite {y | ¬x ≤ f y} := (filter.eventually_cofinite.mp (tendsto_at_top.1 hf x)), simp only [not_le] at this, obtain ⟨a₀, ⟨ha₀ : f a₀ < x, ha₀s⟩, others_bigger⟩ := exists_min_image _ f (this.inter_of_left s) ⟨y, hx, hys⟩, refine ⟨a₀, ha₀s, λ a has, (lt_or_le (f a) x).elim _ (le_trans ha₀.le)⟩, exact λ h, others_bigger a ⟨h, has⟩ }, { -- in this case, f is constant because all values are at top push_neg at not_all_top, obtain ⟨a₀, ha₀s⟩ := hs, exact ⟨a₀, ha₀s, λ a ha, not_all_top a ha (f a₀)⟩ } end lemma filter.tendsto.exists_forall_le [nonempty α] [linear_order β] {f : α → β} (hf : tendsto f cofinite at_top) : ∃ a₀, ∀ a, f a₀ ≤ f a := let ⟨a₀, _, ha₀⟩ := hf.exists_within_forall_le univ_nonempty in ⟨a₀, λ a, ha₀ a (mem_univ _)⟩ lemma filter.tendsto.exists_within_forall_ge [linear_order β] {s : set α} (hs : s.nonempty) {f : α → β} (hf : filter.tendsto f filter.cofinite filter.at_bot) : ∃ a₀ ∈ s, ∀ a ∈ s, f a ≤ f a₀ := @filter.tendsto.exists_within_forall_le _ βᵒᵈ _ _ hs _ hf lemma filter.tendsto.exists_forall_ge [nonempty α] [linear_order β] {f : α → β} (hf : tendsto f cofinite at_bot) : ∃ a₀, ∀ a, f a ≤ f a₀ := @filter.tendsto.exists_forall_le _ βᵒᵈ _ _ _ hf /-- For an injective function `f`, inverse images of finite sets are finite. See also `filter.comap_cofinite_le` and `function.injective.comap_cofinite_eq`. -/ lemma function.injective.tendsto_cofinite {f : α → β} (hf : injective f) : tendsto f cofinite cofinite := λ s h, h.preimage (hf.inj_on _) /-- The pullback of the `filter.cofinite` under an injective function is equal to `filter.cofinite`. See also `filter.comap_cofinite_le` and `function.injective.tendsto_cofinite`. -/ lemma function.injective.comap_cofinite_eq {f : α → β} (hf : injective f) : comap f cofinite = cofinite := (comap_cofinite_le f).antisymm hf.tendsto_cofinite.le_comap /-- An injective sequence `f : ℕ → ℕ` tends to infinity at infinity. -/ lemma function.injective.nat_tendsto_at_top {f : ℕ → ℕ} (hf : injective f) : tendsto f at_top at_top := nat.cofinite_eq_at_top ▸ hf.tendsto_cofinite
c798411cd58f7def223a033cf3b990c64b82236a
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/tests/lean/run/optParam.lean
818d3c26232f31ee4e5dea884bb02356f4096c0e
[ "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
325
lean
def p (x : Nat := 0) : Nat × Nat := (x, x) theorem ex1 : p.1 = 0 := rfl theorem ex2 : p (x := 1) |>.2 = 1 := rfl def c {α : Type} [Inhabited α] : α × α := (arbitrary, arbitrary) theorem ex3 {α} [Inhabited α] : c.1 = arbitrary (α := α) := rfl theorem ex4 {α} [Inhabited α] : c.2 = arbitrary (α := α) := rfl
3e744053858c490dafae3d229b460a207a5319aa
8e381650eb2c1c5361be64ff97e47d956bf2ab9f
/src/Kenny/sheaf_of_rings_on_opens.lean
f069c9f5e019e21bf76f3b10e7a34041cd95d1b0
[]
no_license
alreadydone/lean-scheme
04c51ab08eca7ccf6c21344d45d202780fa667af
52d7624f57415eea27ed4dfa916cd94189221a1c
refs/heads/master
1,599,418,221,423
1,562,248,559,000
1,562,248,559,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
22,643
lean
import sheaves.sheaf_of_rings Kenny.sheaf_on_opens ring_theory.subring universes v w u₁ v₁ u open topological_space lattice def sheaf_of_rings_on_opens (X : Type u) [topological_space X] (U : opens X) : Type (max u (v+1)) := sheaf_of_rings.{u v} X namespace sheaf_of_rings_on_opens variables {X : Type u} [topological_space X] {U : opens X} def to_sheaf_on_opens (F : sheaf_of_rings_on_opens X U) : sheaf_on_opens X U := { F := F.1.1, locality := F.2, gluing := F.3 } -- def eval (F : sheaf_of_rings_on_opens X U) : Π (V : opens X), V ≤ U → Type v := -- F.to_sheaf_on_opens.eval instance comm_ring_eval (F : sheaf_of_rings_on_opens X U) (V HVU) : comm_ring (F.to_sheaf_on_opens.eval V HVU) := F.1.2 V -- def res (F : sheaf_of_rings_on_opens X U) : Π (V : opens X) (HVU : V ≤ U) (W : opens X) (HWU : W ≤ U) (HWV : W ≤ V), F.to_sheaf_on_opens.eval V HVU → F.to_sheaf_on_opens.eval W HWU := -- F.to_sheaf_on_opens.res instance is_ring_hom_res (F : sheaf_of_rings_on_opens X U) (V HVU W HWU HWV) : is_ring_hom (F.to_sheaf_on_opens.res V HVU W HWU HWV) := F.1.3 V W HWV section variables (F : sheaf_of_rings_on_opens X U) (V : opens X) (HVU : V ≤ U) (W : opens X) (HWU : W ≤ U) (HWV : W ≤ V) variables (x y : F.to_sheaf_on_opens.eval V HVU) (n : ℕ) @[simp] lemma res_add : F.to_sheaf_on_opens.res V HVU W HWU HWV (x + y) = F.to_sheaf_on_opens.res V HVU W HWU HWV x + F.to_sheaf_on_opens.res V HVU W HWU HWV y := is_ring_hom.map_add _ @[simp] lemma res_zero : F.to_sheaf_on_opens.res V HVU W HWU HWV 0 = 0 := is_ring_hom.map_zero _ @[simp] lemma res_neg : F.to_sheaf_on_opens.res V HVU W HWU HWV (-x) = -F.to_sheaf_on_opens.res V HVU W HWU HWV x := is_ring_hom.map_neg _ @[simp] lemma res_sub : F.to_sheaf_on_opens.res V HVU W HWU HWV (x - y) = F.to_sheaf_on_opens.res V HVU W HWU HWV x - F.to_sheaf_on_opens.res V HVU W HWU HWV y := is_ring_hom.map_sub _ @[simp] lemma res_mul : F.to_sheaf_on_opens.res V HVU W HWU HWV (x * y) = F.to_sheaf_on_opens.res V HVU W HWU HWV x * F.to_sheaf_on_opens.res V HVU W HWU HWV y := is_ring_hom.map_mul _ @[simp] lemma res_one : F.to_sheaf_on_opens.res V HVU W HWU HWV 1 = 1 := is_ring_hom.map_one _ @[simp] lemma res_pow : F.to_sheaf_on_opens.res V HVU W HWU HWV (x^n) = (F.to_sheaf_on_opens.res V HVU W HWU HWV x)^n := is_semiring_hom.map_pow _ x n end theorem res_self (F : sheaf_of_rings_on_opens X U) (V HVU HV x) : F.to_sheaf_on_opens.res V HVU V HVU HV x = x := F.to_sheaf_on_opens.res_self V HVU HV x theorem res_res (F : sheaf_of_rings_on_opens X U) (V HVU W HWU HWV S HSU HSW x) : F.to_sheaf_on_opens.res W HWU S HSU HSW (F.to_sheaf_on_opens.res V HVU W HWU HWV x) = F.to_sheaf_on_opens.res V HVU S HSU (le_trans HSW HWV) x := F.to_sheaf_on_opens.res_res V HVU W HWU HWV S HSU HSW x theorem locality (F : sheaf_of_rings_on_opens X U) (V HVU s t) (OC : covering V) (H : ∀ i : OC.γ, F.to_sheaf_on_opens.res V HVU (OC.Uis i) (le_trans (subset_covering i) HVU) (subset_covering i) s = F.to_sheaf_on_opens.res V HVU (OC.Uis i) (le_trans (subset_covering i) HVU) (subset_covering i) t) : s = t := F.locality OC s t H -- noncomputable def glue (F : sheaf_of_rings_on_opens X U) (V HVU) (OC : covering V) -- (s : Π i : OC.γ, F.to_sheaf_on_opens.eval (OC.Uis i) (le_trans (subset_covering i) HVU)) -- (H : ∀ i j : OC.γ, F.to_sheaf_on_opens.res _ _ (OC.Uis i ⊓ OC.Uis j) (le_trans inf_le_left (le_trans (subset_covering i) HVU)) inf_le_left (s i) = -- F.to_sheaf_on_opens.res _ _ (OC.Uis i ⊓ OC.Uis j) (le_trans inf_le_left (le_trans (subset_covering i) HVU)) inf_le_right (s j)) : -- F.to_sheaf_on_opens.eval V HVU := -- classical.some $ F.gluing OC s H -- theorem res_glue (F : sheaf_of_rings_on_opens X U) (V HVU) (OC : covering V) (s H i) : -- F.to_sheaf_on_opens.res V HVU (OC.Uis i) (le_trans (subset_covering i) HVU) (subset_covering i) (F.glue V HVU OC s H) = s i := -- classical.some_spec (F.gluing OC s H) i -- theorem eq_glue (F : sheaf_of_rings_on_opens X U) (V HVU) (OC : covering V) -- (s : Π i : OC.γ, F.to_sheaf_on_opens.eval (OC.Uis i) (le_trans (subset_covering i) HVU)) (H t) -- (ht : ∀ i, F.to_sheaf_on_opens.res V HVU (OC.Uis i) (le_trans (subset_covering i) HVU) (subset_covering i) t = s i) : -- F.glue V HVU OC s H = t := -- F.locality V HVU _ _ OC $ λ i, by rw [res_glue, ht] def res_subset (F : sheaf_of_rings_on_opens X U) (V : opens X) (HVU : V ≤ U) : sheaf_of_rings_on_opens X V := F theorem res_res_subset (F : sheaf_of_rings_on_opens X U) (V HVU S HSV T HTV HTS x) : (F.to_sheaf_on_opens.res_subset V HVU).res S HSV T HTV HTS x = F.to_sheaf_on_opens.res S (le_trans HSV HVU) T (le_trans HTV HVU) HTS x := rfl -- def stalk (F : sheaf_of_rings_on_opens.{v} X U) (x : X) (hx : x ∈ U) : Type (max u v) := -- stalk_of_rings F.1 x instance comm_ring_stalk (F : sheaf_of_rings_on_opens.{v} X U) (x : X) (hx : x ∈ U) : comm_ring (F.to_sheaf_on_opens.stalk x hx) := stalk_of_rings_is_comm_ring F.1 x -- def to_stalk (F : sheaf_of_rings_on_opens.{v} X U) (x : X) (hx : x ∈ U) (V : opens X) (hxV : x ∈ V) (HVU : V ≤ U) (s : F.to_sheaf_on_opens.eval V HVU) : F.to_sheaf_on_opens.stalk x hx := -- F.to_sheaf_on_opens.to_stalk x hx V hxV HVU s instance is_ring_hom_to_stalk (F : sheaf_of_rings_on_opens X U) (x hx V hxV HVU) : is_ring_hom (F.to_sheaf_on_opens.to_stalk x hx V hxV HVU) := to_stalk.is_ring_hom _ _ _ _ section variables (F : sheaf_of_rings_on_opens.{v} X U) (x : X) (hx : x ∈ U) (V : opens X) (hxV : x ∈ V) (HVU : V ≤ U) variables (s t : F.to_sheaf_on_opens.eval V HVU) (n : ℕ) @[simp] lemma to_stalk_add : F.to_sheaf_on_opens.to_stalk x hx V hxV HVU (s + t) = F.to_sheaf_on_opens.to_stalk x hx V hxV HVU s + F.to_sheaf_on_opens.to_stalk x hx V hxV HVU t := is_ring_hom.map_add _ @[simp] lemma to_stalk_zero : F.to_sheaf_on_opens.to_stalk x hx V hxV HVU 0 = 0 := is_ring_hom.map_zero _ @[simp] lemma to_stalk_neg : F.to_sheaf_on_opens.to_stalk x hx V hxV HVU (-s) = -F.to_sheaf_on_opens.to_stalk x hx V hxV HVU s := is_ring_hom.map_neg _ @[simp] lemma to_stalk_sub : F.to_sheaf_on_opens.to_stalk x hx V hxV HVU (s - t) = F.to_sheaf_on_opens.to_stalk x hx V hxV HVU s - F.to_sheaf_on_opens.to_stalk x hx V hxV HVU t := is_ring_hom.map_sub _ @[simp] lemma to_stalk_mul : F.to_sheaf_on_opens.to_stalk x hx V hxV HVU (s * t) = F.to_sheaf_on_opens.to_stalk x hx V hxV HVU s * F.to_sheaf_on_opens.to_stalk x hx V hxV HVU t := is_ring_hom.map_mul _ @[simp] lemma to_stalk_one : F.to_sheaf_on_opens.to_stalk x hx V hxV HVU 1 = 1 := is_ring_hom.map_one _ @[simp] lemma to_stalk_pow : F.to_sheaf_on_opens.to_stalk x hx V hxV HVU (s^n) = (F.to_sheaf_on_opens.to_stalk x hx V hxV HVU s)^n := is_semiring_hom.map_pow _ s n end @[simp] lemma to_stalk_res (F : sheaf_of_rings_on_opens.{v} X U) (x : X) (hx : x ∈ U) (V : opens X) (hxV : x ∈ V) (HVU : V ≤ U) (W : opens X) (hxW : x ∈ W) (HWV : W ≤ V) (s : F.to_sheaf_on_opens.eval V HVU) : F.to_sheaf_on_opens.to_stalk x hx W hxW (le_trans HWV HVU) (F.to_sheaf_on_opens.res _ _ _ _ HWV s) = F.to_sheaf_on_opens.to_stalk x hx V hxV HVU s := to_stalk_res _ _ _ _ _ _ _ _ @[elab_as_eliminator] theorem stalk.induction_on {F : sheaf_of_rings_on_opens X U} {x : X} {hx : x ∈ U} {C : F.to_sheaf_on_opens.stalk x hx → Prop} (g : F.to_sheaf_on_opens.stalk x hx) (H : ∀ V : opens X, ∀ hxV : x ∈ V, ∀ HVU : V ≤ U, ∀ s : F.to_sheaf_on_opens.eval V HVU, C (F.to_sheaf_on_opens.to_stalk x hx V hxV HVU s)) : C g := quotient.induction_on g $ λ e, have (⟦e⟧ : F.to_sheaf_on_opens.stalk x hx) = ⟦⟨e.1 ⊓ U, ⟨e.2, hx⟩, F.F.res _ _ (set.inter_subset_left _ _) e.3⟩⟧, from quotient.sound ⟨e.1 ⊓ U, ⟨e.2, hx⟩, set.inter_subset_left _ _, set.subset.refl _, by dsimp only [to_sheaf_on_opens]; rw ← presheaf.Hcomp'; refl⟩, this.symm ▸ H (e.1 ⊓ U) ⟨e.2, hx⟩ inf_le_right _ @[elab_as_eliminator] theorem stalk.induction_on₂ {F : sheaf_of_rings_on_opens X U} {x : X} {hx : x ∈ U} {C : F.to_sheaf_on_opens.stalk x hx → F.to_sheaf_on_opens.stalk x hx → Prop} (g1 g2 : F.to_sheaf_on_opens.stalk x hx) (H : ∀ V : opens X, ∀ hxV : x ∈ V, ∀ HVU : V ≤ U, ∀ s t : F.to_sheaf_on_opens.eval V HVU, C (F.to_sheaf_on_opens.to_stalk x hx V hxV HVU s) (F.to_sheaf_on_opens.to_stalk x hx V hxV HVU t)) : C g1 g2 := quotient.induction_on₂ g1 g2 $ λ e1 e2, have h1 : (⟦e1⟧ : F.to_sheaf_on_opens.stalk x hx) = _root_.to_stalk F.F x (e1.1 ⊓ e2.1 ⊓ U) ⟨⟨e1.2, e2.2⟩, hx⟩ (F.F.res _ _ (λ p hp, hp.1.1) e1.3), by erw [_root_.to_stalk_res]; cases e1; refl, have h2 : (⟦e2⟧ : F.to_sheaf_on_opens.stalk x hx) = _root_.to_stalk F.F x (e1.1 ⊓ e2.1 ⊓ U) ⟨⟨e1.2, e2.2⟩, hx⟩ (F.F.res _ _ (λ p hp, hp.1.2) e2.3), by erw [_root_.to_stalk_res]; cases e2; refl, h1.symm ▸ h2.symm ▸ H _ _ inf_le_right _ _ structure morphism (F : sheaf_of_rings_on_opens.{v} X U) (G : sheaf_of_rings_on_opens.{w} X U) : Type (max u v w) := (η : F.to_sheaf_on_opens.morphism G.to_sheaf_on_opens) [hom : ∀ V HV, is_ring_hom (η.map V HV)] attribute [instance] morphism.hom namespace morphism section variables {F : sheaf_of_rings_on_opens.{v} X U} {G : sheaf_of_rings_on_opens.{w} X U} variables (η : F.morphism G) (V : opens X) (HVU : V ≤ U) (x y : F.to_sheaf_on_opens.eval V HVU) (n : ℕ) @[simp] lemma map_add : η.1.map V HVU (x + y) = η.1.map V HVU x + η.1.map V HVU y := is_ring_hom.map_add _ @[simp] lemma map_zero : η.1.map V HVU 0 = 0 := is_ring_hom.map_zero _ @[simp] lemma map_neg : η.1.map V HVU (-x) = -η.1.map V HVU x := is_ring_hom.map_neg _ @[simp] lemma map_sub : η.1.map V HVU (x - y) = η.1.map V HVU x - η.1.map V HVU y := is_ring_hom.map_sub _ @[simp] lemma map_mul : η.1.map V HVU (x * y) = η.1.map V HVU x * η.1.map V HVU y := is_ring_hom.map_mul _ @[simp] lemma map_one : η.1.map V HVU 1 = 1 := is_ring_hom.map_one _ @[simp] lemma map_pow : η.1.map V HVU (x^n) = (η.1.map V HVU x)^n := is_semiring_hom.map_pow _ x n end protected def id (F : sheaf_of_rings_on_opens.{v} X U) : F.morphism F := { η := sheaf_on_opens.morphism.id _, hom := λ _ _, is_ring_hom.id } def comp {F : sheaf_of_rings_on_opens.{v} X U} {G : sheaf_of_rings_on_opens.{w} X U} {H : sheaf_of_rings_on_opens.{u₁} X U} (η : G.morphism H) (ξ : F.morphism G) : F.morphism H := { η := η.1.comp ξ.1, hom := λ _ _, is_ring_hom.comp _ _ } @[simp] lemma comp_apply {F : sheaf_of_rings_on_opens.{v} X U} {G : sheaf_of_rings_on_opens.{w} X U} {H : sheaf_of_rings_on_opens.{u₁} X U} (η : G.morphism H) (ξ : F.morphism G) (V HV s) : (η.comp ξ).1.1 V HV s = η.1.1 V HV (ξ.1.1 V HV s) := rfl @[extensionality] lemma ext {F : sheaf_of_rings_on_opens.{v} X U} {G : sheaf_of_rings_on_opens.{w} X U} {η ξ : F.morphism G} (H : ∀ V HV x, η.1.map V HV x = ξ.1.map V HV x) : η = ξ := by cases η; cases ξ; congr; ext; apply H @[simp] lemma id_comp {F : sheaf_of_rings_on_opens.{v} X U} {G : sheaf_of_rings_on_opens.{w} X U} (η : F.morphism G) : (morphism.id G).comp η = η := ext $ λ V HV x, rfl @[simp] lemma comp_id {F : sheaf_of_rings_on_opens.{v} X U} {G : sheaf_of_rings_on_opens.{w} X U} (η : F.morphism G) : η.comp (morphism.id F) = η := ext $ λ V HV x, rfl @[simp] lemma comp_assoc {F : sheaf_of_rings_on_opens.{v} X U} {G : sheaf_of_rings_on_opens.{w} X U} {H : sheaf_of_rings_on_opens.{u₁} X U} {I : sheaf_of_rings_on_opens.{v₁} X U} (η : H.morphism I) (ξ : G.morphism H) (χ : F.morphism G) : (η.comp ξ).comp χ = η.comp (ξ.comp χ) := rfl def res_subset {F : sheaf_of_rings_on_opens.{v} X U} {G : sheaf_of_rings_on_opens.{w} X U} (η : F.morphism G) (V : opens X) (HVU : V ≤ U) : (F.res_subset V HVU).morphism (G.res_subset V HVU) := { η := η.1.res_subset V HVU, hom := λ _ _, η.2 _ _ } @[simp] lemma res_subset_apply {F : sheaf_of_rings_on_opens.{v} X U} {G : sheaf_of_rings_on_opens.{w} X U} (η : F.morphism G) (V : opens X) (HVU : V ≤ U) (W HWV s) : (η.res_subset V HVU).1.1 W HWV s = η.1.1 W (le_trans HWV HVU) s := rfl @[simp] lemma comp_res_subset {F : sheaf_of_rings_on_opens.{v} X U} {G : sheaf_of_rings_on_opens.{w} X U} {H : sheaf_of_rings_on_opens.{u₁} X U} (η : G.morphism H) (ξ : F.morphism G) (V : opens X) (HVU : V ≤ U) : (η.res_subset V HVU).comp (ξ.res_subset V HVU) = (η.comp ξ).res_subset V HVU := rfl @[simp] lemma id_res_subset {F : sheaf_of_rings_on_opens.{v} X U} (V : opens X) (HVU : V ≤ U) : (morphism.id F).res_subset V HVU = morphism.id (F.res_subset V HVU) := rfl -- def stalk {F : sheaf_of_rings_on_opens.{v} X U} {G : sheaf_of_rings_on_opens.{w} X U} (η : F.morphism G) (x : X) (hx : x ∈ U) -- (s : F.to_sheaf_on_opens.stalk x hx) : G.to_sheaf_on_opens.stalk x hx := -- quotient.lift_on s (λ g, ⟦(⟨g.1 ⊓ U, (⟨g.2, hx⟩ : x ∈ g.1 ⊓ U), -- η.1.map _ inf_le_right (presheaf.res F.1.1 _ _ (set.inter_subset_left _ _) g.3)⟩ : stalk.elem _ _)⟧) $ -- λ g₁ g₂ ⟨V, hxV, HV1, HV2, hg⟩, quotient.sound ⟨V ⊓ U, ⟨hxV, hx⟩, set.inter_subset_inter_left _ HV1, set.inter_subset_inter_left _ HV2, -- calc G.to_sheaf_on_opens.res _ _ (V ⊓ U) inf_le_right (inf_le_inf HV1 (le_refl _)) (η.1.map (g₁.U ⊓ U) inf_le_right ((F.F).res (g₁.U) (g₁.U ⊓ U) (set.inter_subset_left _ _) (g₁.s))) -- = η.1.map (V ⊓ U) inf_le_right ((F.F).res V (V ⊓ U) (set.inter_subset_left _ _) ((F.F).res (g₁.U) V HV1 (g₁.s))) : -- by rw ← η.3; dsimp only [sheaf_on_opens.res, sheaf_of_rings_on_opens.to_sheaf_on_opens]; rw [← presheaf.Hcomp', ← presheaf.Hcomp'] -- ... = G.to_sheaf_on_opens.res _ _ (V ⊓ U) _ _ (η.1.map (g₂.U ⊓ U) inf_le_right ((F.F).res (g₂.U) (g₂.U ⊓ U) _ (g₂.s))) : -- by erw [hg, ← η.3]; dsimp only [sheaf_on_opens.res, sheaf_of_rings_on_opens.to_sheaf_on_opens]; rw [← presheaf.Hcomp', ← presheaf.Hcomp']⟩ -- @[simp] lemma stalk_to_stalk {F : sheaf_of_rings_on_opens.{v} X U} {G : sheaf_of_rings_on_opens.{w} X U} (η : F.morphism G) (x : X) (hx : x ∈ U) -- (V : opens X) (HVU : V ≤ U) (hxV : x ∈ V) (s : F.to_sheaf_on_opens.eval V HVU) : -- η.stalk x hx (F.to_sheaf_on_opens.to_stalk x hx V hxV HVU s) = -- G.to_sheaf_on_opens.to_stalk x hx V hxV HVU (η.1.map V HVU s) := -- quotient.sound ⟨V, hxV, set.subset_inter (set.subset.refl _) HVU, set.subset.refl _, -- calc G.to_sheaf_on_opens.res (V ⊓ U) inf_le_right V HVU (le_inf (le_refl V) HVU) (η.1.map (V ⊓ U) inf_le_right (F.to_sheaf_on_opens.res V HVU (V ⊓ U) inf_le_right inf_le_left s)) -- = G.to_sheaf_on_opens.res V HVU V HVU (le_refl V) (η.1.map V HVU s) : by rw [η.3, res_res]⟩ instance is_ring_hom_stalk {F : sheaf_of_rings_on_opens.{v} X U} {G : sheaf_of_rings_on_opens.{w} X U} (η : F.morphism G) (x : X) (hx : x ∈ U) : is_ring_hom (η.1.stalk x hx) := { map_one := quotient.sound ⟨U, hx, set.subset_inter (set.subset_univ U.1) (set.subset.refl U.1), set.subset_univ U.1, by dsimp only; erw [_root_.res_one, η.map_one, _root_.res_one, _root_.res_one]⟩, map_mul := λ y z, stalk.induction_on₂ y z $ λ V hxV HVU s t, by rw [sheaf_on_opens.morphism.stalk_to_stalk, sheaf_on_opens.morphism.stalk_to_stalk, ← to_stalk_mul, sheaf_on_opens.morphism.stalk_to_stalk, η.map_mul, to_stalk_mul], map_add := λ y z, stalk.induction_on₂ y z $ λ V hxV HVU s t, by rw [sheaf_on_opens.morphism.stalk_to_stalk, sheaf_on_opens.morphism.stalk_to_stalk, ← to_stalk_add, sheaf_on_opens.morphism.stalk_to_stalk, η.map_add, to_stalk_add] } section variables {F : sheaf_of_rings_on_opens.{v} X U} {G : sheaf_of_rings_on_opens.{w} X U} (η : F.morphism G) (x : X) (hx : x ∈ U) variables (s t : F.to_sheaf_on_opens.stalk x hx) (n : ℕ) @[simp] lemma stalk_add : η.1.stalk x hx (s + t) = η.1.stalk x hx s + η.1.stalk x hx t := is_ring_hom.map_add _ @[simp] lemma stalk_zero : η.1.stalk x hx 0 = 0 := is_ring_hom.map_zero _ @[simp] lemma stalk_neg : η.1.stalk x hx (-s) = -η.1.stalk x hx s := is_ring_hom.map_neg _ @[simp] lemma stalk_sub : η.1.stalk x hx (s - t) = η.1.stalk x hx s - η.1.stalk x hx t := is_ring_hom.map_sub _ @[simp] lemma stalk_mul : η.1.stalk x hx (s * t) = η.1.stalk x hx s * η.1.stalk x hx t := is_ring_hom.map_mul _ @[simp] lemma stalk_one : η.1.stalk x hx 1 = 1 := is_ring_hom.map_one _ @[simp] lemma stalk_pow : η.1.stalk x hx (s^n) = (η.1.stalk x hx s)^n := is_semiring_hom.map_pow _ s n end end morphism structure equiv (F : sheaf_of_rings_on_opens.{v} X U) (G : sheaf_of_rings_on_opens.{w} X U) : Type (max u v w) := (to_fun : F.morphism G) (inv_fun : G.to_sheaf_on_opens.morphism F.to_sheaf_on_opens) (left_inv : ∀ V HVU s, inv_fun.1 V HVU (to_fun.1.1 V HVU s) = s) (right_inv : ∀ V HVU s, to_fun.1.1 V HVU (inv_fun.1 V HVU s) = s) namespace equiv def to_sheaf_on_opens {F : sheaf_of_rings_on_opens.{v} X U} {G : sheaf_of_rings_on_opens.{w} X U} (e : F.equiv G) : F.to_sheaf_on_opens.equiv G.to_sheaf_on_opens := { to_fun := e.1.1, .. e } def refl (F : sheaf_of_rings_on_opens.{v} X U) : equiv F F := ⟨morphism.id F, sheaf_on_opens.morphism.id F.to_sheaf_on_opens, λ _ _ _, rfl, λ _ _ _, rfl⟩ @[simp] lemma refl_apply (F : sheaf_of_rings_on_opens.{v} X U) (V HV s) : (refl F).1.1.1 V HV s = s := rfl def symm {F : sheaf_of_rings_on_opens.{v} X U} {G : sheaf_of_rings_on_opens.{v} X U} (e : equiv F G) : equiv G F := ⟨{ η := e.2, hom := λ V HVU, (ring_equiv.symm { to_fun := e.1.1.1 V HVU, inv_fun := e.2.1 V HVU, left_inv := e.3 V HVU, right_inv := e.4 V HVU, hom := e.1.2 V HVU }).hom }, e.1.1, e.4, e.3⟩ def trans {F : sheaf_of_rings_on_opens.{v} X U} {G : sheaf_of_rings_on_opens.{v} X U} {H : sheaf_of_rings_on_opens.{u₁} X U} (e₁ : equiv F G) (e₂ : equiv G H) : equiv F H := ⟨e₂.1.comp e₁.1, e₁.2.comp e₂.2, λ _ _ _, by rw [morphism.comp_apply, sheaf_on_opens.morphism.comp_apply, e₂.3, e₁.3], λ _ _ _, by rw [morphism.comp_apply, sheaf_on_opens.morphism.comp_apply, e₁.4, e₂.4]⟩ @[simp] lemma trans_apply {F : sheaf_of_rings_on_opens.{v} X U} {G : sheaf_of_rings_on_opens.{v} X U} {H : sheaf_of_rings_on_opens.{u₁} X U} (e₁ : equiv F G) (e₂ : equiv G H) (V HV s) : (e₁.trans e₂).1.1.1 V HV s = e₂.1.1.1 V HV (e₁.1.1.1 V HV s) := rfl def res_subset {F : sheaf_of_rings_on_opens.{v} X U} {G : sheaf_of_rings_on_opens.{w} X U} (e : equiv F G) (V : opens X) (HVU : V ≤ U) : equiv (F.res_subset V HVU) (G.res_subset V HVU) := ⟨e.1.res_subset V HVU, e.2.res_subset V HVU, λ _ _ _, by rw [morphism.res_subset_apply, sheaf_on_opens.morphism.res_subset_apply, e.3], λ _ _ _, by rw [morphism.res_subset_apply, sheaf_on_opens.morphism.res_subset_apply, e.4]⟩ @[simp] lemma res_subset_apply {F : sheaf_of_rings_on_opens.{v} X U} {G : sheaf_of_rings_on_opens.{w} X U} (e : equiv F G) (V : opens X) (HVU : V ≤ U) (W HW s) : (e.res_subset V HVU).1.1.1 W HW s = e.1.1.1 W (le_trans HW HVU) s := rfl -- def stalk {F : sheaf_of_rings_on_opens.{v} X U} {G : sheaf_of_rings_on_opens.{w} X U} (e : equiv F G) (x : X) (hx : x ∈ U) : -- F.to_sheaf_on_opens.stalk x hx ≃ G.to_sheaf_on_opens.stalk x hx := -- { to_fun := e.1.1.stalk x hx, -- inv_fun := e.2.stalk x hx, -- left_inv := λ g, stalk.induction_on g $ λ V hxV HVU s, -- by rw [sheaf_on_opens.morphism.stalk_to_stalk, sheaf_on_opens.morphism.stalk_to_stalk, e.3]; refl, -- right_inv := λ g, stalk.induction_on g $ λ V hxV HVU s, -- by rw [sheaf_on_opens.morphism.stalk_to_stalk, sheaf_on_opens.morphism.stalk_to_stalk, e.4]; refl } end equiv def sheaf_glue {I : Type u} (S : I → opens X) (F : Π (i : I), sheaf_of_rings_on_opens.{v} X (S i)) (φ : Π i j, equiv ((F i).res_subset ((S i) ⊓ (S j)) inf_le_left) ((F j).res_subset ((S i) ⊓ (S j)) inf_le_right)) : sheaf_of_rings_on_opens.{max u v} X (⋃S) := { F := { Fring := λ U, @subtype.comm_ring (Π (i : I), (F i).to_sheaf_on_opens.eval (S i ⊓ U) inf_le_left) _ { f | ∀ (i j : I), (φ i j).1.1.1 (S i ⊓ S j ⊓ U) inf_le_left ((F i).to_sheaf_on_opens.res (S i ⊓ U) inf_le_left (S i ⊓ S j ⊓ U) (le_trans inf_le_left inf_le_left) (le_inf (le_trans inf_le_left inf_le_left) inf_le_right) (f i)) = (F j).to_sheaf_on_opens.res (S j ⊓ U) inf_le_left (S i ⊓ S j ⊓ U) (le_trans inf_le_left inf_le_right) (by rw inf_assoc; exact inf_le_right) (f j) } { add_mem := λ f g hf hg i j, by erw [res_add, morphism.map_add, res_add, hf i j, hg i j], zero_mem := λ i j, by erw [res_zero, morphism.map_zero, res_zero]; refl, neg_mem := λ f hf i j, by erw [res_neg, morphism.map_neg, res_neg, hf i j], one_mem := λ i j, by erw [res_one, morphism.map_one, res_one]; refl, mul_mem := λ f g hf hg i j, by erw [res_mul, morphism.map_mul, res_mul, hf i j, hg i j] }, res_is_ring_hom := λ U V HVU, { map_one := subtype.eq $ funext $ λ i, res_one _ _ _ _ _ _, map_mul := λ f g, subtype.eq $ funext $ λ i, res_mul _ _ _ _ _ _ _ _, map_add := λ f g, subtype.eq $ funext $ λ i, res_add _ _ _ _ _ _ _ _ }, .. (sheaf_on_opens.sheaf_glue S (λ i, (F i).to_sheaf_on_opens) (λ i j, (φ i j).to_sheaf_on_opens)).F } .. sheaf_on_opens.sheaf_glue S (λ i, (F i).to_sheaf_on_opens) (λ i j, (φ i j).to_sheaf_on_opens) } @[simp] lemma sheaf_glue_res_val {I : Type u} (S : I → opens X) (F : Π (i : I), sheaf_of_rings_on_opens.{v} X (S i)) (φ : Π i j, equiv ((F i).res_subset ((S i) ⊓ (S j)) inf_le_left) ((F j).res_subset ((S i) ⊓ (S j)) inf_le_right)) (U HU V HV HVU s i) : ((sheaf_glue S F φ).to_sheaf_on_opens.res U HU V HV HVU s).1 i = (F i).to_sheaf_on_opens.res _ _ _ _ (inf_le_inf (le_refl _) HVU) (s.1 i) := rfl def universal_property (I : Type u) (S : I → opens X) (F : Π (i : I), sheaf_of_rings_on_opens.{v} X (S i)) (φ : Π i j, equiv ((F i).res_subset ((S i) ⊓ (S j)) inf_le_left) ((F j).res_subset ((S i) ⊓ (S j)) inf_le_right)) (Hφ1 : ∀ i V HV s, (φ i i).1.1.1 V HV s = s) (Hφ2 : ∀ i j k V HV1 HV2 HV3 s, (φ j k).1.1.1 V HV1 ((φ i j).1.1.1 V HV2 s) = (φ i k).1.1.1 V HV3 s) (i : I) : equiv (res_subset (sheaf_glue S F φ) (S i) (le_supr S i)) (F i) := { to_fun := { η := (sheaf_on_opens.universal_property I S (λ i, (F i).to_sheaf_on_opens) (λ i j, (φ i j).to_sheaf_on_opens) Hφ1 Hφ2 i).1, hom := λ U HU, { map_one := res_one _ _ _ _ _ _, map_mul := λ x y, res_mul _ _ _ _ _ _ _ _, map_add := λ x y, res_add _ _ _ _ _ _ _ _ } }, .. sheaf_on_opens.universal_property I S (λ i, (F i).to_sheaf_on_opens) (λ i j, (φ i j).to_sheaf_on_opens) Hφ1 Hφ2 i } end sheaf_of_rings_on_opens
c407c2be8e71c18b01b0fa59a7b1c88db17f2890
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/section4.lean
5a4f427deff9f1372f8a664cfc8327a02ae863b7
[ "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
648
lean
import logic set_option pp.universes true set_option pp.implicit true section universe k parameter A : Type section universe variable l universe variable u parameter B : Type definition foo (a : A) (b : B) := b inductive mypair := mk : A → B → mypair definition pr1' (p : mypair) : A := mypair.rec (λ a b, a) p definition pr2' (p : mypair) : B := mypair.rec (λ a b, b) p check mypair.rec end check mypair.rec variable a : A check foo num a 0 definition pr1 (p : mypair num) : A := mypair.rec (λ a b, a) p definition pr2 (p : mypair num) : num := mypair.rec (λ a b, b) p end
adcb8bc3a7696ac57e7ee467547e370f15006a8b
359199d7253811b032ab92108191da7336eba86e
/src/instructor/lectures/lecture_5.lean
1368bc7298b70c9e7bc8991861617ecbc6718d89
[]
no_license
arte-et-marte/my_cs2120f21
0bc6215cb5018a3b7c90d9d399a173233f587064
91609c3609ad81fda895bee8b97cc76813241e17
refs/heads/main
1,693,298,928,348
1,634,931,202,000
1,634,931,202,000
399,946,705
0
0
null
null
null
null
UTF-8
Lean
false
false
6,005
lean
/- INTRODUCTION and ELIMINATION RULES -/ /- For ∀ x, P x (every x has property P) - introduction rule: assume arbitrary x, then show P x - elimination rule: *apply* a proof of ∀ x, P x, as a kind of function to a specific value of x, say k, to produce a proof of P k. -/ /- Now we need a short detour, so as to understand the examples of the preceding point that we're about to present. On this detour, we meet the proposition, true. As an AXIOM of our logic, we now accept that the proposition true is always logically true. That means that there has to be a proof of true. By means we don't explain yet, the definition of our logic in Lean gives us the axiom (true.intro : true). I.e., true.intro is a proof of true. It's always defined, and so we can use it at anytime to prove the propositions, true. That in fact is the introduction rule for true: that true has a proof (and is thus invariably logically true). In the end, a proof of true is worthless because it carries no information at all, not even one binary digit (bit). There is thus no use for an elimination rule for true, and there isn't one. Summary. If we're really being rigorous we'd say there are actually two axioms for true: first it is a proposition; second, there is a proof of it (so it is logically true without any conditions). There is no elimination rule for true. -/ /- Please now merge back onto the yellow brick road. -/ /- What that detour on the proposition true and its truth, and proof, values, we can now give a very simple example of a "forall proposition", and both the construction and the use of a proof of such a proposition. -/ /- Let's first conjecture that no matter what natural number we might be given, our only obligation is to return a proof of true. It's an almost Alice-in-Wonderland idea. And of course it's true. You just ignore the value that you're given and invariable return the value, true.intro. Here then is the formal statement for which we'll first construct and the use a proof of this proposition: ∀ (n : ℕ), true. -/ theorem silly : ∀ (n : ℕ), true := begin assume (n : ℕ), -- ∀ introduction exact true.intro, -- true.introduction end /- My Notes: "true" proposition is just always true. goal translated: if you give me a value of type nat, I'll give you a proof that true is true. trying to prove an implication. How do we prove an implication? Assume...return proof. -/ /- The proposition true is unconditionally true, as proven by an always available proof called (in Lean) true.intro. -/ #check silly 7 /-applying to the number 7, elim rule: apply to args, return some result.-/ /- The check command will tell you the type of any expression (aka term) in Lean. Here we can see that silly is like a function, and that when we apply it to the specific argument, 7, we get back a proof of the resulting proposition (which is just, "true"). We'll soon be equipped to deal with more interesting "return types". -/ /- For P → Q (if P is true then Q must also be true) - introduction rule: assume arbitrary P, then show Q - elimination rule: *apply* a proof of P → Q, as a kind of function, to any proof of P to derive a proof of Q! -/ lemma foo : ∀ (x : ℕ), x = 0 → x + 1 = 1 := begin assume x h, rw h, end /- My Notes: for all, apply intro rule for for all, assume given value of nat, assume given proof (that x = 0), in that context show that x+1=1. what do you have in context that - have x=0, how to use? use it to rw x as 0 in our goal, get 0 + 1 = 1, which eq.refl will take care of. use rw when there is something in the context to change for the goal.-/ /- Wow! ∀ and → sure do seem similar. Indeed they're the same! They define function types. We construct a proof of ∀ or → by assuming the premise and showing that in that context we can derive a result of the conclusion type. We can then use a proof of a ∀ or → by treating it as a function that can be applied to a specific value to derive a proof *for that specific value. Indeed, in Lean, → is really just another notation for forall! -/ /- Intro rule for ∧ Give a proof of P and a proof of Q get back a proof of (P ∧ Q) -/ axioms (P Q : Prop) #check P #check (P ∧ Q) axioms (p : P) /-assume proof of P-/ (q : Q) example : P ∧ Q := and.intro p q /-trying to prove that P ∧ Q, apply and's intro rule, which needs a proof of P and a proof of Q.-/ /-Prove that if arbitrary props. P and Q are true (which is to say that we have a proof of each of them), that the prop of P ∧ Q is also true. Proof: The conjecture that P ∧ Q is true is proved by application of the introduction rule for and. (which returns a pair of proofs for the larger proof?)-/ example : 0 = 0 ∧ 1 = 1 := begin apply and.intro _ _, /-1st slot: proof of 0=0 (P), 2nd slot: proof of 1=1 (Q)-/ apply eq.refl 0, /-gives a proof of 0 = 0-/ apply eq.refl 1 /-gives a proof to 1 = 1, to accomplish goal-/ end /-we assume only if we have premises to our proposition-/ example : 0 = 0 ∧ 1 = 1 := begin apply and.intro (eq.refl 0) (eq.refl 1) end theorem bar : 0 = 0 ∧ 1 = 1 := begin apply and.intro (eq.refl 0) (eq.refl 1), end #check bar #check and.elim_left bar #check and.elim_right bar /- Now we have a major theorem. -/ theorem and_commutative : ∀ (P Q : Prop), P ∧ Q → Q ∧ P := begin assume P Q h, apply and.intro _ _, apply and.elim_right h, apply and.elim_left h, end /- assume P Q h, apply and.intro _ _, /-proof of p and proof q individually-/ apply and.elim_right h, /-gives back q from p ∧ q-/ apply and.elim_left h, /-gives us p from h-/ end ======= assume P Q h, apply and.intro _ _, apply and.elim_right h, apply and.elim_left h, end >>>>>>> 138d43446c443c1d15cd2f17fb607c4f0dff702f -/ example : ∀ (P Q : Prop), P ∧ Q → Q ∧ P := begin assume P Q h, have p : P := and.elim_left h, have q : Q := and.elim_right h, exact and.intro q p, end
c4f3855d933f621e81ea2555cb5625d1f96fb0e9
0f54cce53cdd7d43dc5b0017dfb0a3eaf09ba18a
/src/simp_loop/exists_one_point_elim.lean
45b87bdbb21b0ae85bf74e73c6ad70edd11a9dd4
[]
no_license
johoelzl/lean-simp-loop
dabc7629b21e319fa0b9c45639c82f9bea57a18a
c1ad8c34be7c6fd323fc5eff5ce337fd23a72e04
refs/heads/master
1,618,774,503,904
1,525,883,646,000
1,525,883,646,000
126,789,660
1
0
null
null
null
null
UTF-8
Lean
false
false
11,586
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 1-point elimination for ∃. This implements a conversion transforming `∃x, t[x, x = a] ~> t[a, rfl]` where `t` is an expression consiting of existentials and conjunctions, and the equality is either the type of one of the existentials or in one of the conjunctions. Concrete examples: `∃a, a = t ∧ p a ~> p t` `∃a, p a ∧ ∃b, ∃h : a = f b, q a h ~> ∃b, p (f b) ∧ q (f b) rfl` This is implemented by * `find`: find all possible equalities, and keep track of dependencies on `x` * `reorder_non_dependent`: move all existentials which are not dependent on `x` before the first one * `elim_equality`: walk to the equality and bring it in the normal form eq : E ex : ∃h:E, q cn : E ∧ q end finally eliminated it. To bring it into normal form we need to keep care of the following cases: l : p ∧ X r : X ∧ p e : ∃a, X applying these rules: l_eq : p ∧ E ~> E ∧ p l_ex : p ∧ (∃h:E, q h) ~> ∃h:E, p ∧ q h l_cn : p ∧ (E ∧ q) ~> E ∧ (p ∧ q) r_eq : E ∧ p ~> E ∧ p r_ex : (∃h:E, q h) ∧ p ~> ∃h:E, q h ∧ p r_cn : (E ∧ q) ∧ p ~> E ∧ (q ∧ p) e_eq_Prop : ∃a, E ~> E ∧ a e_eq : ∃a, E ~> E ∧ nonempty a e_ex : ∃a, ∃h:E, q h a ~> ∃h:E, ∃a, q h a e_cn : ∃a, E ∧ p a ~> E ∧ (∃a, p a) And finally for elimination: elim_eq : ∃x, x = t ~> true elim_ex : ∃x, ∃h:x = t, p x h ~> p t rfl elim_cn : ∃x, (x = t) ∧ p x ~> p t This tactic is inspired by the one-point elimination in Isabelle's simplifier. -/ import simp_loop.conv namespace simp_loop open conv_t tactic expr namespace exists_eq_elim section section lemma l_eq {p E : Prop} : (p ∧ E) ↔ (E ∧ p) := and_comm _ _ lemma l_ex {p E : Prop} {r : E → Prop} : (p ∧ (∃h:E, r h)) ↔ (∃h:E, p ∧ r h) := ⟨λ⟨h, e, r⟩, ⟨e, h, r⟩, λ⟨e, h, r⟩, ⟨h, e, r⟩⟩ lemma l_cn {p q E : Prop} : (p ∧ (E ∧ q)) ↔ (E ∧ (p ∧ q)) := ⟨λ⟨h, e, r⟩, ⟨e, h, r⟩, λ⟨e, h, r⟩, ⟨h, e, r⟩⟩ lemma r_eq {p E : Prop} : (E ∧ p) ↔ (E ∧ p) := iff.refl _ lemma r_ex {p E : Prop} {r : E → Prop} : ((∃h:E, r h) ∧ p) ↔ (∃h:E, r h ∧ p) := ⟨λ⟨⟨e, r⟩, h⟩, ⟨e, r, h⟩, λ⟨e, r, h⟩, ⟨⟨e, r⟩, h⟩⟩ lemma r_cn {p q E : Prop} : ((E ∧ q) ∧ p) ↔ (E ∧ (q ∧ p)) := ⟨λ⟨⟨e, r⟩, h⟩, ⟨e, r, h⟩, λ⟨e, r, h⟩, ⟨⟨e, r⟩, h⟩⟩ lemma e_eq_Prop {p E : Prop} : (∃a:p, E) ↔ (E ∧ p) := ⟨λ⟨a, h⟩, ⟨h, a⟩, λ⟨h, a⟩, ⟨a, h⟩⟩ lemma e_eq {α : Sort*} {E : Prop} : (∃a:α, E) ↔ (E ∧ nonempty α) := ⟨λ⟨a, h⟩, ⟨h, ⟨a⟩⟩, λ⟨h, ⟨a⟩⟩, ⟨a, h⟩⟩ lemma e_ex {α : Sort*} {E : Prop} {s : E → α → Prop} : (∃a:α, ∃h:E, s h a) ↔ (∃h:E, ∃a:α, s h a) := ⟨λ⟨a, h, t⟩, ⟨h, a, t⟩, λ⟨a, h, t⟩, ⟨h, a, t⟩⟩ lemma e_cn {α : Sort*} {E : Prop} {t : α → Prop} : (∃a:α, E ∧ t a) ↔ (E ∧ (∃a:α, t a)) := ⟨λ⟨a, e, t⟩, ⟨e, ⟨a, t⟩⟩, λ⟨e, ⟨a, t⟩⟩, ⟨a, e, t⟩⟩ lemma elim_eq_left {α : Sort*} {a : α} : (∃x, x = a) ↔ true := ⟨λ_, ⟨⟩, λ_, ⟨a, rfl⟩⟩ lemma elim_eq_right {α : Sort*} {a : α} : (∃x, a = x) ↔ true := ⟨λ_, ⟨⟩, λ_, ⟨a, rfl⟩⟩ lemma elim_ex_left {α : Sort*} {a : α} {q : Πx, x = a → Prop} : (∃x, ∃h:x = a, q x h) ↔ q a rfl := ⟨λ⟨x, rfl, q⟩, q, λh, ⟨a, rfl, h⟩⟩ lemma elim_ex_right {α : Sort*} {a : α} {q : Πx, a = x → Prop} : (∃x, ∃h:a = x, q x h) ↔ q a rfl := ⟨λ⟨x, rfl, q⟩, q, λh, ⟨a, rfl, h⟩⟩ lemma elim_cn_left {α : Sort*} {a : α} {p : α → Prop} : (∃x, (x = a) ∧ p x) ↔ p a := ⟨λ⟨x, rfl, p⟩, p, λh, ⟨a, rfl, h⟩⟩ lemma elim_cn_right {α : Sort*} {a : α} {p : α → Prop} : (∃x, (a = x) ∧ p x) ↔ p a := ⟨λ⟨x, rfl, p⟩, p, λh, ⟨a, rfl, h⟩⟩ lemma l_congr {p q r : Prop} (h : q ↔ r) : p ∧ q ↔ p ∧ r := and_congr (iff.refl p) h lemma r_congr {p q r : Prop} (h : q ↔ r) : q ∧ p ↔ r ∧ p := and_congr h (iff.refl p) lemma ex_congr {α : Sort*} {p q : α → Prop} (h : ∀a, p a ↔ q a) : (∃a, p a) ↔ (∃a, q a) := exists_congr h lemma comm_l {α : Sort*} {p : Prop} {q : α → Prop} : p ∧ (∃a, q a) ↔ ∃a, p ∧ q a := ⟨λ⟨a, hq, hp⟩, ⟨hq, ⟨a, hp⟩⟩, λ ⟨hq, ⟨a, hp⟩⟩, ⟨a, hq, hp⟩⟩ lemma comm_r {α : Sort*} {p : Prop} {q : α → Prop} : (∃a, q a) ∧ p ↔ ∃a, q a ∧ p := ⟨λ ⟨⟨a, hp⟩, hq⟩, ⟨a, hp, hq⟩, λ⟨a, hp, hq⟩, ⟨⟨a, hp⟩, hq⟩⟩ lemma comm_ex {α : Sort*} {β : Sort*} {p : α → β → Prop} : (∃a b, p a b) ↔ (∃b a, p a b) := ⟨λ⟨a, ⟨b, h⟩⟩, ⟨b, ⟨a, h⟩⟩, λ⟨a, ⟨b, h⟩⟩, ⟨b, ⟨a, h⟩⟩⟩ end end meta inductive info | binder (v : expr) (dependent : bool) | operator (side : bool) namespace info open format meta instance : has_to_format info := ⟨λi, match i with | info.binder v d := to_fmt "binder " ++ to_fmt v ++ " " ++ to_fmt d | info.operator s := to_fmt "operator " ++ to_fmt s end⟩ end info meta inductive norm_form | eq | ex | cn namespace norm_form open format meta instance : has_to_format norm_form := ⟨λi, match i with | norm_form.eq := to_fmt "eq" | norm_form.ex := to_fmt "ex" | norm_form.cn := to_fmt "cn" end⟩ end norm_form meta def check_eq (x : expr) (deps : list expr) : expr → bool | `(%%l = %%r) := (l = x ∧ deps.all (λx, ¬ x.occurs r)) ∨ (r = x ∧ deps.all (λx, ¬ x.occurs l)) | _ := ff meta def congr_ex {α} (c : conv α) : conv α := congr_binder ``ex_congr (λ_, c) meta def congr {α} (c : conv α) : info → conv α | (info.binder _ _) := congr_ex c | (info.operator tt) := congr_simple ``r_congr c | (info.operator ff) := congr_simple ``l_congr c meta def apply_norm (n_eq n_ex n_cn : list name) : norm_form → conv norm_form | norm_form.eq := do n_eq.mfirst apply_const, return norm_form.cn | norm_form.ex := do n_ex.mfirst apply_const, return norm_form.ex | norm_form.cn := do n_cn.mfirst apply_const, return norm_form.cn meta def analyse (v : expr) (deps : list expr) : expr → tactic (option $ list $ info × expr) | `(@Exists %%α %%p) := if check_eq v deps α then return none else do (lam pp_n bi domain body) ← return p | return (some []), x ← mk_local' pp_n bi domain, return [(info.binder x (deps.any $ λv, v.occurs α), body.instantiate_var x)] | `(%%p ∧ %%q) := return [(info.operator tt, p), (info.operator ff, q)] | t := return $ if check_eq v deps t then none else some [] meta def find (v : expr) : list expr → expr → tactic (list $ list info) | deps e := do some is ← analyse v deps e | return [[]], iss ← is.mmap (λ⟨i, t⟩, do deps ← return $ match i with (info.binder v tt) := v :: deps | _ := deps end, iss ← find deps t, return $ iss.map $ λis, i :: is), return iss.join meta def reorder_equality : list info → conv norm_form | [] := (do -- trace "reorder_equality []", trace_lhs, `(_ = _) ← lhs, return norm_form.eq) <|> return norm_form.ex | (i@(info.binder _ _)::xs) := congr (reorder_equality xs) i >>= apply_norm [``e_eq_Prop, ``e_eq] [``e_ex] [``e_cn] | (i@(info.operator tt)::xs) := congr (reorder_equality xs) i >>= apply_norm [``r_eq] [``r_ex] [``r_cn] | (i@(info.operator ff)::xs) := congr (reorder_equality xs) i >>= apply_norm [``l_eq] [``l_ex] [``l_cn] meta def elim_equality (l : list info) : conv unit := do -- trace (to_fmt "elim_equality " ++ to_fmt l), n ← congr_ex (reorder_equality l), -- trace (to_fmt "elim_equality [reordered] " ++ to_fmt n), trace_lhs, apply_norm [``elim_eq_left, ``elim_eq_right] [``elim_ex_left, ``elim_ex_right] [``elim_cn_left, ``elim_cn_right] n, skip meta def reorder_non_dependent : list info → conv (option (list info)) | [] := return none | (i::is) := (do info.binder _ ff ← return i, return is) <|> (do some is' ← congr (reorder_non_dependent is) i | return none, r ← return $ match i with | info.binder _ _ := ``comm_ex | info.operator tt := ``comm_r | info.operator ff := ``comm_l end, apply_const r, return (i :: is')) meta def reorder_and_elim : list info → conv unit | l := do -- trace (to_fmt "reorder_and_elim " ++ to_fmt l), some l' ← congr_ex (reorder_non_dependent l) | elim_equality l, apply_const ``comm_ex, congr_ex (reorder_and_elim l') meta def run : conv unit := do pss ← congr_binder ``ex_congr (λv, do t ← lhs, find v [v] t), pss.mfirst reorder_and_elim section test variables {α : Sort*} {β : Sort*} {γ : β → Sort*} {δ : α → Sort*} example {f : α → β} {a : α} {p : β → Prop} : (∃b, ∃c:γ b, ∃h : f a = b, p b) ↔ (∃c:γ (f a), p (f a)) := by conversion exists_eq_elim.run example {f : α → β} {p : β → Prop} : (∃b, ∃c:γ b, ∃a, ∃h : f a = b, p b) ↔ (∃a, ∃c:γ (f a), p (f a)) := by conversion exists_eq_elim.run example {f : Πa, δ a → β} {p : β → Prop} : (∃b, ∃c:γ b, ∃a, ∃d:δ a, ∃h : f a d = b, p b) ↔ (∃a, ∃d:δ a, ∃c:γ (f a d), p (f a d)) := by conversion exists_eq_elim.run example {f : Πa, δ a → β} {p : β → Prop} : (∃b, ∃a, ∃c:γ b, ∃d:δ a, ∃h : f a d = b, p b) ↔ (∃a, ∃d:δ a, ∃c:γ (f a d), p (f a d)) := by conversion exists_eq_elim.run example {f : Πa, δ a → β} {p : β → Prop} : (∃b, ∃a, ∃d:δ a, ∃c:γ b, ∃h : f a d = b, p b) ↔ (∃a, ∃d:δ a, ∃c:γ (f a d), p (f a d)) := by conversion exists_eq_elim.run example {f : Πa, δ a → β} {p : β → Prop} : (∃b, ∃a, ∃d:δ a, ∃h : f a d = b, ∃c:γ b, p b) ↔ (∃a, ∃d:δ a, ∃c:γ (f a d), p (f a d)) := by conversion exists_eq_elim.run example {f : Πa, δ a → β} {p : β → Prop} : (∃b, ∃a, ∃d:δ a, ∃h' : a = a ∧ b = b, ∃h : f a d = b, ∃c:γ b, p b) ↔ (∃a (d : δ a) (h' : a = a ∧ f a d = f a d) (c : γ (f a d)), p (f a d)) := by conversion exists_eq_elim.run example {f : α → β} {q : β → α → Prop} {p : β → Prop} : (∃b a, ∃h:q b a, ∃h' : b = b, ∃h : f a = b, ∃c:γ b, p b) ↔ (∃a (h : q (f a) a) (h : f a = f a) (c : γ (f a)), p (f a)) := by conversion exists_eq_elim.run example {f : α → β} {p : β → Prop} : (∃a, (∃c, ∃h: a = a , ∃b : β, a = f c) ∧ (∃c:α, p a)) ↔ (∃a (b:β), f a = f a ∧ ∃ (c : α), p (f a)) := by conversion exists_eq_elim.run example {f : α → β} {p : β → Prop} : (∃a, (∃c, ∃h: a = a , ∃b : β, a = f c) ∧ (∃c:α, p a)) ↔ (∃a (b:β), f a = f a ∧ ∃ (c : α), p (f a)) := by conversion exists_eq_elim.run example {f g : α → β} {p : α → Prop} : (∃b, ∃a:(λb:β, α) b, ∃a₂, ∃h:f a = b, b = g a₂ ∧ p a₂) ↔ (∃a₂ (a : (λ (b : β), α) (g a₂)) (h : f a = g a₂), p a₂) := by conversion exists_eq_elim.run example {p q r : α → Prop} {a : α}: (∃a', (p a' ∧ r a') ∧ (a = a' ∧ q a)) ↔ (p a ∧ r a) ∧ q a := by conversion exists_eq_elim.run example {p q r : α → Prop} {a : α} : (∃a', (p a' ∧ r a') ∧ (a' = a ∧ q a)) ↔ (p a ∧ r a) ∧ q a := by conversion exists_eq_elim.run example {p : α → Prop} {q : Prop} : (∃a, p a ∧ ∃a', a = a' ∧ q) ↔ (∃a, p a ∧ q) := by conversion exists_eq_elim.run end test end exists_eq_elim end simp_loop
fcc3c6747eb0fcc41ccbda092b924e7e128fe65b
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/number_theory/pell.lean
ffdd65bd0783a75cd1786202cd01541b00ab3cba
[ "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
37,733
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.nat.modeq import number_theory.zsqrtd.basic /-! # Pell's equation and Matiyasevic's theorem This file solves Pell's equation, i.e. integer solutions to `x ^ 2 - d * y ^ 2 = 1` in the special case that `d = a ^ 2 - 1`. This is then applied to prove Matiyasevic's theorem that the power function is Diophantine, which is the last key ingredient in the solution to Hilbert's tenth problem. For the definition of Diophantine function, see `dioph.lean`. ## Main definition * `pell` is a function assigning to a natural number `n` the `n`-th solution to Pell's equation constructed recursively from the initial solution `(0, 1)`. ## Main statements * `eq_pell` shows that every solution to Pell's equation is recursively obtained using `pell` * `matiyasevic` shows that a certain system of Diophantine equations has a solution if and only if the first variable is the `x`-component in a solution to Pell's equation - the key step towards Hilbert's tenth problem in Davis' version of Matiyasevic's theorem. * `eq_pow_of_pell` shows that the power function is Diophantine. ## Implementation notes The proof of Matiyasevic's theorem doesn't follow Matiyasevic's original account of using Fibonacci numbers but instead Davis' variant of using solutions to Pell's equation. ## References * [M. Carneiro, _A Lean formalization of Matiyasevič's theorem_][carneiro2018matiyasevic] * [M. Davis, _Hilbert's tenth problem is unsolvable_][MR317916] ## Tags Pell's equation, Matiyasevic's theorem, Hilbert's tenth problem ## TODO * Provide solutions to Pell's equation for the case of arbitrary `d` (not just `d = a ^ 2 - 1` like in the current version) and furthermore also for `x ^ 2 - d * y ^ 2 = -1`. * Connect solutions to the continued fraction expansion of `√d`. -/ namespace pell open nat section parameters {a : ℕ} (a1 : 1 < a) include a1 private def d := a*a - 1 @[simp] theorem d_pos : 0 < d := tsub_pos_of_lt (mul_lt_mul a1 (le_of_lt a1) dec_trivial dec_trivial : 1*1<a*a) /-- The Pell sequences, i.e. the sequence of integer solutions to `x ^ 2 - d * y ^ 2 = 1`, where `d = a ^ 2 - 1`, defined together in mutual recursion. -/ -- TODO(lint): Fix double namespace issue @[nolint dup_namespace] def pell : ℕ → ℕ × ℕ := λn, nat.rec_on n (1, 0) (λn xy, (xy.1*a + d*xy.2, xy.1 + xy.2*a)) /-- The Pell `x` sequence. -/ def xn (n : ℕ) : ℕ := (pell n).1 /-- The Pell `y` sequence. -/ def yn (n : ℕ) : ℕ := (pell n).2 @[simp] theorem pell_val (n : ℕ) : pell n = (xn n, yn n) := show pell n = ((pell n).1, (pell n).2), from match pell n with (a, b) := rfl end @[simp] theorem xn_zero : xn 0 = 1 := rfl @[simp] theorem yn_zero : yn 0 = 0 := rfl @[simp] theorem xn_succ (n : ℕ) : xn (n+1) = xn n * a + d * yn n := rfl @[simp] theorem yn_succ (n : ℕ) : yn (n+1) = xn n + yn n * a := rfl @[simp] theorem xn_one : xn 1 = a := by simp @[simp] theorem yn_one : yn 1 = 1 := by simp /-- The Pell `x` sequence, considered as an integer sequence.-/ def xz (n : ℕ) : ℤ := xn n /-- The Pell `y` sequence, considered as an integer sequence.-/ def yz (n : ℕ) : ℤ := yn n section omit a1 /-- The element `a` such that `d = a ^ 2 - 1`, considered as an integer.-/ def az : ℤ := a end theorem asq_pos : 0 < a*a := le_trans (le_of_lt a1) (by have := @nat.mul_le_mul_left 1 a a (le_of_lt a1); rwa mul_one at this) theorem dz_val : ↑d = az*az - 1 := have 1 ≤ a*a, from asq_pos, show ↑(a*a - 1) = _, by rw int.coe_nat_sub this; refl @[simp] theorem xz_succ (n : ℕ) : xz (n+1) = xz n * az + ↑d * yz n := rfl @[simp] theorem yz_succ (n : ℕ) : yz (n+1) = xz n + yz n * az := rfl /-- The Pell sequence can also be viewed as an element of `ℤ√d` -/ def pell_zd (n : ℕ) : ℤ√d := ⟨xn n, yn n⟩ @[simp] theorem pell_zd_re (n : ℕ) : (pell_zd n).re = xn n := rfl @[simp] theorem pell_zd_im (n : ℕ) : (pell_zd n).im = yn n := rfl /-- The property of being a solution to the Pell equation, expressed as a property of elements of `ℤ√d`. -/ def is_pell : ℤ√d → Prop | ⟨x, y⟩ := x*x - d*y*y = 1 theorem is_pell_nat {x y : ℕ} : is_pell ⟨x, y⟩ ↔ x*x - d*y*y = 1 := ⟨λh, int.coe_nat_inj (by rw int.coe_nat_sub (int.le_of_coe_nat_le_coe_nat $ int.le.intro_sub h); exact h), λh, show ((x*x : ℕ) - (d*y*y:ℕ) : ℤ) = 1, by rw [← int.coe_nat_sub $ le_of_lt $ nat.lt_of_sub_eq_succ h, h]; refl⟩ theorem is_pell_norm : Π {b : ℤ√d}, is_pell b ↔ b * b.conj = 1 | ⟨x, y⟩ := by simp [zsqrtd.ext, is_pell, mul_comm]; ring_nf theorem is_pell_mul {b c : ℤ√d} (hb : is_pell b) (hc : is_pell c) : is_pell (b * c) := is_pell_norm.2 (by simp [mul_comm, mul_left_comm, zsqrtd.conj_mul, pell.is_pell_norm.1 hb, pell.is_pell_norm.1 hc]) theorem is_pell_conj : ∀ {b : ℤ√d}, is_pell b ↔ is_pell b.conj | ⟨x, y⟩ := by simp [is_pell, zsqrtd.conj] @[simp] theorem pell_zd_succ (n : ℕ) : pell_zd (n+1) = pell_zd n * ⟨a, 1⟩ := by simp [zsqrtd.ext] theorem is_pell_one : is_pell ⟨a, 1⟩ := show az*az-d*1*1=1, by simp [dz_val]; ring theorem is_pell_pell_zd : ∀ (n : ℕ), is_pell (pell_zd n) | 0 := rfl | (n+1) := let o := is_pell_one in by simp; exact pell.is_pell_mul (is_pell_pell_zd n) o @[simp] theorem pell_eqz (n : ℕ) : xz n * xz n - d * yz n * yz n = 1 := is_pell_pell_zd n @[simp] theorem pell_eq (n : ℕ) : xn n * xn n - d * yn n * yn n = 1 := let pn := pell_eqz n in have h : (↑(xn n * xn n) : ℤ) - ↑(d * yn n * yn n) = 1, by repeat {rw int.coe_nat_mul}; exact pn, have hl : d * yn n * yn n ≤ xn n * xn n, from int.le_of_coe_nat_le_coe_nat $ int.le.intro $ add_eq_of_eq_sub' $ eq.symm h, int.coe_nat_inj (by rw int.coe_nat_sub hl; exact h) instance dnsq : zsqrtd.nonsquare d := ⟨λn h, have n*n + 1 = a*a, by rw ← h; exact nat.succ_pred_eq_of_pos (asq_pos a1), have na : n < a, from nat.mul_self_lt_mul_self_iff.2 (by rw ← this; exact nat.lt_succ_self _), have (n+1)*(n+1) ≤ n*n + 1, by rw this; exact nat.mul_self_le_mul_self na, have n+n ≤ 0, from @nat.le_of_add_le_add_right (n*n + 1) _ _ (by ring_nf at this ⊢; assumption), ne_of_gt d_pos $ by rwa nat.eq_zero_of_le_zero ((nat.le_add_left _ _).trans this) at h⟩ theorem xn_ge_a_pow : ∀ (n : ℕ), a^n ≤ xn n | 0 := le_refl 1 | (n+1) := by simp [pow_succ']; exact le_trans (nat.mul_le_mul_right _ (xn_ge_a_pow n)) (nat.le_add_right _ _) theorem n_lt_a_pow : ∀ (n : ℕ), n < a^n | 0 := nat.le_refl 1 | (n+1) := begin have IH := n_lt_a_pow n, have : a^n + a^n ≤ a^n * a, { rw ← mul_two, exact nat.mul_le_mul_left _ a1 }, simp [pow_succ'], refine lt_of_lt_of_le _ this, exact add_lt_add_of_lt_of_le IH (lt_of_le_of_lt (nat.zero_le _) IH) end theorem n_lt_xn (n) : n < xn n := lt_of_lt_of_le (n_lt_a_pow n) (xn_ge_a_pow n) theorem x_pos (n) : 0 < xn n := lt_of_le_of_lt (nat.zero_le n) (n_lt_xn n) lemma eq_pell_lem : ∀n (b:ℤ√d), 1 ≤ b → is_pell b → b ≤ pell_zd n → ∃n, b = pell_zd n | 0 b := λh1 hp hl, ⟨0, @zsqrtd.le_antisymm _ dnsq _ _ hl h1⟩ | (n+1) b := λh1 hp h, have a1p : (0:ℤ√d) ≤ ⟨a, 1⟩, from trivial, have am1p : (0:ℤ√d) ≤ ⟨a, -1⟩, from show (_:nat) ≤ _, by simp; exact nat.pred_le _, have a1m : (⟨a, 1⟩ * ⟨a, -1⟩ : ℤ√d) = 1, from is_pell_norm.1 is_pell_one, if ha : (⟨↑a, 1⟩ : ℤ√d) ≤ b then let ⟨m, e⟩ := eq_pell_lem n (b * ⟨a, -1⟩) (by rw ← a1m; exact mul_le_mul_of_nonneg_right ha am1p) (is_pell_mul hp (is_pell_conj.1 is_pell_one)) (by have t := mul_le_mul_of_nonneg_right h am1p; rwa [pell_zd_succ, mul_assoc, a1m, mul_one] at t) in ⟨m+1, by rw [show b = b * ⟨a, -1⟩ * ⟨a, 1⟩, by rw [mul_assoc, eq.trans (mul_comm _ _) a1m]; simp, pell_zd_succ, e]⟩ else suffices ¬1 < b, from ⟨0, show b = 1, from (or.resolve_left (lt_or_eq_of_le h1) this).symm⟩, λ h1l, by cases b with x y; exact have bm : (_*⟨_,_⟩ :ℤ√(d a1)) = 1, from pell.is_pell_norm.1 hp, have y0l : (0:ℤ√(d a1)) < ⟨x - x, y - -y⟩, from sub_lt_sub h1l $ λ(hn : (1:ℤ√(d a1)) ≤ ⟨x, -y⟩), by have t := mul_le_mul_of_nonneg_left hn (le_trans zero_le_one h1); rw [bm, mul_one] at t; exact h1l t, have yl2 : (⟨_, _⟩ : ℤ√_) < ⟨_, _⟩, from show (⟨x, y⟩ - ⟨x, -y⟩ : ℤ√(d a1)) < ⟨a, 1⟩ - ⟨a, -1⟩, from sub_lt_sub (by exact ha) $ λ(hn : (⟨x, -y⟩ : ℤ√(d a1)) ≤ ⟨a, -1⟩), by have t := mul_le_mul_of_nonneg_right (mul_le_mul_of_nonneg_left hn (le_trans zero_le_one h1)) a1p; rw [bm, one_mul, mul_assoc, eq.trans (mul_comm _ _) a1m, mul_one] at t; exact ha t, by simp at y0l; simp at yl2; exact match y, y0l, (yl2 : (⟨_, _⟩ : ℤ√_) < ⟨_, _⟩) with | 0, y0l, yl2 := y0l (le_refl 0) | (y+1 : ℕ), y0l, yl2 := yl2 (zsqrtd.le_of_le_le (le_refl 0) (let t := int.coe_nat_le_coe_nat_of_le (nat.succ_pos y) in add_le_add t t)) | -[1+y], y0l, yl2 := y0l trivial end theorem eq_pell_zd (b : ℤ√d) (b1 : 1 ≤ b) (hp : is_pell b) : ∃n, b = pell_zd n := let ⟨n, h⟩ := @zsqrtd.le_arch d b in eq_pell_lem n b b1 hp $ h.trans $ by rw zsqrtd.coe_nat_val; exact zsqrtd.le_of_le_le (int.coe_nat_le_coe_nat_of_le $ le_of_lt $ n_lt_xn _ _) (int.coe_zero_le _) /-- Every solution to **Pell's equation** is recursively obtained from the initial solution `(1,0)` using the recursion `pell`. -/ theorem eq_pell {x y : ℕ} (hp : x*x - d*y*y = 1) : ∃n, x = xn n ∧ y = yn n := have (1:ℤ√d) ≤ ⟨x, y⟩, from match x, hp with | 0, (hp : 0 - _ = 1) := by rw zero_tsub at hp; contradiction | (x+1), hp := zsqrtd.le_of_le_le (int.coe_nat_le_coe_nat_of_le $ nat.succ_pos x) (int.coe_zero_le _) end, let ⟨m, e⟩ := eq_pell_zd ⟨x, y⟩ this (is_pell_nat.2 hp) in ⟨m, match x, y, e with ._, ._, rfl := ⟨rfl, rfl⟩ end⟩ theorem pell_zd_add (m) : ∀ n, pell_zd (m + n) = pell_zd m * pell_zd n | 0 := (mul_one _).symm | (n+1) := by rw[← add_assoc, pell_zd_succ, pell_zd_succ, pell_zd_add n, ← mul_assoc] theorem xn_add (m n) : xn (m + n) = xn m * xn n + d * yn m * yn n := by injection (pell_zd_add _ m n) with h _; repeat {rw ← int.coe_nat_add at h <|> rw ← int.coe_nat_mul at h}; exact int.coe_nat_inj h theorem yn_add (m n) : yn (m + n) = xn m * yn n + yn m * xn n := by injection (pell_zd_add _ m n) with _ h; repeat {rw ← int.coe_nat_add at h <|> rw ← int.coe_nat_mul at h}; exact int.coe_nat_inj h theorem pell_zd_sub {m n} (h : n ≤ m) : pell_zd (m - n) = pell_zd m * (pell_zd n).conj := let t := pell_zd_add n (m - n) in by rw [add_tsub_cancel_of_le h] at t; rw [t, mul_comm (pell_zd _ n) _, mul_assoc, (is_pell_norm _).1 (is_pell_pell_zd _ _), mul_one] theorem xz_sub {m n} (h : n ≤ m) : xz (m - n) = xz m * xz n - d * yz m * yz n := by { rw [sub_eq_add_neg, ←mul_neg], exact congr_arg zsqrtd.re (pell_zd_sub a1 h) } theorem yz_sub {m n} (h : n ≤ m) : yz (m - n) = xz n * yz m - xz m * yz n := by { rw [sub_eq_add_neg, ←mul_neg, mul_comm, add_comm], exact congr_arg zsqrtd.im (pell_zd_sub a1 h) } theorem xy_coprime (n) : (xn n).coprime (yn n) := nat.coprime_of_dvd' $ λk kp kx ky, let p := pell_eq n in by rw ← p; exact nat.dvd_sub (le_of_lt $ nat.lt_of_sub_eq_succ p) (kx.mul_left _) (ky.mul_left _) theorem strict_mono_y : strict_mono yn | m 0 h := absurd h $ nat.not_lt_zero _ | m (n+1) h := have yn m ≤ yn n, from or.elim (lt_or_eq_of_le $ nat.le_of_succ_le_succ h) (λhl, le_of_lt $ strict_mono_y hl) (λe, by rw e), by simp; refine lt_of_le_of_lt _ (nat.lt_add_of_pos_left $ x_pos a1 n); rw ← mul_one (yn a1 m); exact mul_le_mul this (le_of_lt a1) (nat.zero_le _) (nat.zero_le _) theorem strict_mono_x : strict_mono xn | m 0 h := absurd h $ nat.not_lt_zero _ | m (n+1) h := have xn m ≤ xn n, from or.elim (lt_or_eq_of_le $ nat.le_of_succ_le_succ h) (λhl, le_of_lt $ strict_mono_x hl) (λe, by rw e), by simp; refine lt_of_lt_of_le (lt_of_le_of_lt this _) (nat.le_add_right _ _); have t := nat.mul_lt_mul_of_pos_left a1 (x_pos a1 n); rwa mul_one at t theorem yn_ge_n : Π n, n ≤ yn n | 0 := nat.zero_le _ | (n+1) := show n < yn (n+1), from lt_of_le_of_lt (yn_ge_n n) (strict_mono_y $ nat.lt_succ_self n) theorem y_mul_dvd (n) : ∀k, yn n ∣ yn (n * k) | 0 := dvd_zero _ | (k+1) := by rw [nat.mul_succ, yn_add]; exact dvd_add (dvd_mul_left _ _) ((y_mul_dvd k).mul_right _) theorem y_dvd_iff (m n) : yn m ∣ yn n ↔ m ∣ n := ⟨λh, nat.dvd_of_mod_eq_zero $ (nat.eq_zero_or_pos _).resolve_right $ λhp, have co : nat.coprime (yn m) (xn (m * (n / m))), from nat.coprime.symm $ (xy_coprime _).coprime_dvd_right (y_mul_dvd m (n / m)), have m0 : 0 < m, from m.eq_zero_or_pos.resolve_left $ λe, by rw [e, nat.mod_zero] at hp; rw [e] at h; exact ne_of_lt (strict_mono_y a1 hp) (eq_zero_of_zero_dvd h).symm, by rw [← nat.mod_add_div n m, yn_add] at h; exact not_le_of_gt (strict_mono_y _ $ nat.mod_lt n m0) (nat.le_of_dvd (strict_mono_y _ hp) $ co.dvd_of_dvd_mul_right $ (nat.dvd_add_iff_right $ (y_mul_dvd _ _ _).mul_left _).2 h), λ⟨k, e⟩, by rw e; apply y_mul_dvd⟩ theorem xy_modeq_yn (n) : ∀ k, xn (n * k) ≡ (xn n)^k [MOD (yn n)^2] ∧ yn (n * k) ≡ k * (xn n)^(k-1) * yn n [MOD (yn n)^3] | 0 := by constructor; simp | (k+1) := let ⟨hx, hy⟩ := xy_modeq_yn k in have L : xn (n * k) * xn n + d * yn (n * k) * yn n ≡ xn n^k * xn n + 0 [MOD yn n^2], from (hx.mul_right _ ).add $ modeq_zero_iff_dvd.2 $ by rw pow_succ'; exact mul_dvd_mul_right (dvd_mul_of_dvd_right (modeq_zero_iff_dvd.1 $ (hy.modeq_of_dvd $ by simp [pow_succ']).trans $ modeq_zero_iff_dvd.2 $ by simp [-mul_comm, -mul_assoc]) _) _, have R : xn (n * k) * yn n + yn (n * k) * xn n ≡ xn n^k * yn n + k * xn n^k * yn n [MOD yn n^3], from modeq.add (by { rw pow_succ', exact hx.mul_right' _ }) $ have k * xn n^(k - 1) * yn n * xn n = k * xn n^k * yn n, by clear _let_match; cases k with k; simp [pow_succ', mul_comm, mul_left_comm], by { rw ← this, exact hy.mul_right _ }, by { rw [add_tsub_cancel_right, nat.mul_succ, xn_add, yn_add, pow_succ' (xn _ n), nat.succ_mul, add_comm (k * xn _ n^k) (xn _ n^k), right_distrib], exact ⟨L, R⟩ } theorem ysq_dvd_yy (n) : yn n * yn n ∣ yn (n * yn n) := modeq_zero_iff_dvd.1 $ ((xy_modeq_yn n (yn n)).right.modeq_of_dvd $ by simp [pow_succ]).trans (modeq_zero_iff_dvd.2 $ by simp [mul_dvd_mul_left, mul_assoc]) theorem dvd_of_ysq_dvd {n t} (h : yn n * yn n ∣ yn t) : yn n ∣ t := have nt : n ∣ t, from (y_dvd_iff n t).1 $ dvd_of_mul_left_dvd h, n.eq_zero_or_pos.elim (λ n0, by rwa n0 at ⊢ nt) $ λ (n0l : 0 < n), let ⟨k, ke⟩ := nt in have yn n ∣ k * (xn n)^(k-1), from nat.dvd_of_mul_dvd_mul_right (strict_mono_y n0l) $ modeq_zero_iff_dvd.1 $ by have xm := (xy_modeq_yn a1 n k).right; rw ← ke at xm; exact (xm.modeq_of_dvd $ by simp [pow_succ]).symm.trans h.modeq_zero_nat, by rw ke; exact dvd_mul_of_dvd_right (((xy_coprime _ _).pow_left _).symm.dvd_of_dvd_mul_right this) _ theorem pell_zd_succ_succ (n) : pell_zd (n + 2) + pell_zd n = (2 * a : ℕ) * pell_zd (n + 1) := have (1:ℤ√d) + ⟨a, 1⟩ * ⟨a, 1⟩ = ⟨a, 1⟩ * (2 * a), by { rw zsqrtd.coe_nat_val, change (⟨_,_⟩:ℤ√(d a1))=⟨_,_⟩, rw dz_val, dsimp [az], rw zsqrtd.ext, dsimp, split; ring }, by simpa [mul_add, mul_comm, mul_left_comm, add_comm] using congr_arg (* pell_zd a1 n) this theorem xy_succ_succ (n) : xn (n + 2) + xn n = (2 * a) * xn (n + 1) ∧ yn (n + 2) + yn n = (2 * a) * yn (n + 1) := begin have := pell_zd_succ_succ a1 n, unfold pell_zd at this, erw [zsqrtd.smul_val (2 * a : ℕ)] at this, injection this with h₁ h₂, split; apply int.coe_nat_inj; [simpa using h₁, simpa using h₂] end theorem xn_succ_succ (n) : xn (n + 2) + xn n = (2 * a) * xn (n + 1) := (xy_succ_succ n).1 theorem yn_succ_succ (n) : yn (n + 2) + yn n = (2 * a) * yn (n + 1) := (xy_succ_succ n).2 theorem xz_succ_succ (n) : xz (n + 2) = (2 * a : ℕ) * xz (n + 1) - xz n := eq_sub_of_add_eq $ by delta xz; rw [← int.coe_nat_add, ← int.coe_nat_mul, xn_succ_succ] theorem yz_succ_succ (n) : yz (n + 2) = (2 * a : ℕ) * yz (n + 1) - yz n := eq_sub_of_add_eq $ by delta yz; rw [← int.coe_nat_add, ← int.coe_nat_mul, yn_succ_succ] theorem yn_modeq_a_sub_one : ∀ n, yn n ≡ n [MOD a-1] | 0 := by simp | 1 := by simp | (n+2) := (yn_modeq_a_sub_one n).add_right_cancel $ begin rw [yn_succ_succ, (by ring : n + 2 + n = 2 * (n + 1))], exact ((modeq_sub a1.le).mul_left 2).mul (yn_modeq_a_sub_one (n+1)), end theorem yn_modeq_two : ∀ n, yn n ≡ n [MOD 2] | 0 := by simp | 1 := by simp | (n+2) := (yn_modeq_two n).add_right_cancel $ begin rw [yn_succ_succ, mul_assoc, (by ring : n + 2 + n = 2 * (n + 1))], exact (dvd_mul_right 2 _).modeq_zero_nat.trans (dvd_mul_right 2 _).zero_modeq_nat, end section omit a1 lemma x_sub_y_dvd_pow_lem (y2 y1 y0 yn1 yn0 xn1 xn0 ay a2 : ℤ) : (a2 * yn1 - yn0) * ay + y2 - (a2 * xn1 - xn0) = y2 - a2 * y1 + y0 + a2 * (yn1 * ay + y1 - xn1) - (yn0 * ay + y0 - xn0) := by ring end theorem x_sub_y_dvd_pow (y : ℕ) : ∀ n, (2*a*y - y*y - 1 : ℤ) ∣ yz n * (a - y) + ↑(y^n) - xz n | 0 := by simp [xz, yz, int.coe_nat_zero, int.coe_nat_one] | 1 := by simp [xz, yz, int.coe_nat_zero, int.coe_nat_one] | (n+2) := have (2*a*y - y*y - 1 : ℤ) ∣ ↑(y^(n + 2)) - ↑(2 * a) * ↑(y^(n + 1)) + ↑(y^n), from ⟨-↑(y^n), by { simp [pow_succ, mul_add, int.coe_nat_mul, show ((2:ℕ):ℤ) = 2, from rfl, mul_comm, mul_left_comm], ring }⟩, by { rw [xz_succ_succ, yz_succ_succ, x_sub_y_dvd_pow_lem ↑(y^(n+2)) ↑(y^(n+1)) ↑(y^n)], exact dvd_sub (dvd_add this $ (x_sub_y_dvd_pow (n+1)).mul_left _) (x_sub_y_dvd_pow n) } theorem xn_modeq_x2n_add_lem (n j) : xn n ∣ d * yn n * (yn n * xn j) + xn j := have h1 : d * yn n * (yn n * xn j) + xn j = (d * yn n * yn n + 1) * xn j, by simp [add_mul, mul_assoc], have h2 : d * yn n * yn n + 1 = xn n * xn n, by apply int.coe_nat_inj; repeat {rw int.coe_nat_add <|> rw int.coe_nat_mul}; exact add_eq_of_eq_sub' (eq.symm $ pell_eqz _ _), by rw h2 at h1; rw [h1, mul_assoc]; exact dvd_mul_right _ _ theorem xn_modeq_x2n_add (n j) : xn (2 * n + j) + xn j ≡ 0 [MOD xn n] := begin rw [two_mul, add_assoc, xn_add, add_assoc, ←zero_add 0], refine (dvd_mul_right (xn a1 n) (xn a1 (n + j))).modeq_zero_nat.add _, rw [yn_add, left_distrib, add_assoc, ←zero_add 0], exact ((dvd_mul_right _ _).mul_left _).modeq_zero_nat.add (xn_modeq_x2n_add_lem _ _ _).modeq_zero_nat, end lemma xn_modeq_x2n_sub_lem {n j} (h : j ≤ n) : xn (2 * n - j) + xn j ≡ 0 [MOD xn n] := have h1 : xz n ∣ ↑d * yz n * yz (n - j) + xz j, by rw [yz_sub _ h, mul_sub_left_distrib, sub_add_eq_add_sub]; exact dvd_sub (by delta xz; delta yz; repeat {rw ← int.coe_nat_add <|> rw ← int.coe_nat_mul}; rw mul_comm (xn a1 j) (yn a1 n); exact int.coe_nat_dvd.2 (xn_modeq_x2n_add_lem _ _ _)) ((dvd_mul_right _ _).mul_left _), begin rw [two_mul, add_tsub_assoc_of_le h, xn_add, add_assoc, ←zero_add 0], exact (dvd_mul_right _ _).modeq_zero_nat.add (int.coe_nat_dvd.1 $ by simpa [xz, yz] using h1).modeq_zero_nat, end theorem xn_modeq_x2n_sub {n j} (h : j ≤ 2 * n) : xn (2 * n - j) + xn j ≡ 0 [MOD xn n] := (le_total j n).elim xn_modeq_x2n_sub_lem (λjn, have 2 * n - j + j ≤ n + j, by rw [tsub_add_cancel_of_le h, two_mul]; exact nat.add_le_add_left jn _, let t := xn_modeq_x2n_sub_lem (nat.le_of_add_le_add_right this) in by rwa [tsub_tsub_cancel_of_le h, add_comm] at t) theorem xn_modeq_x4n_add (n j) : xn (4 * n + j) ≡ xn j [MOD xn n] := modeq.add_right_cancel' (xn (2 * n + j)) $ by refine @modeq.trans _ _ 0 _ _ (by rw add_comm; exact (xn_modeq_x2n_add _ _ _).symm); rw [show 4*n = 2*n + 2*n, from right_distrib 2 2 n, add_assoc]; apply xn_modeq_x2n_add theorem xn_modeq_x4n_sub {n j} (h : j ≤ 2 * n) : xn (4 * n - j) ≡ xn j [MOD xn n] := have h' : j ≤ 2*n, from le_trans h (by rw nat.succ_mul; apply nat.le_add_left), modeq.add_right_cancel' (xn (2 * n - j)) $ by refine @modeq.trans _ _ 0 _ _ (by rw add_comm; exact (xn_modeq_x2n_sub _ h).symm); rw [show 4*n = 2*n + 2*n, from right_distrib 2 2 n, add_tsub_assoc_of_le h']; apply xn_modeq_x2n_add theorem eq_of_xn_modeq_lem1 {i n} : Π {j}, i < j → j < n → xn i % xn n < xn j % xn n | 0 ij _ := absurd ij (nat.not_lt_zero _) | (j+1) ij jn := suffices xn j % xn n < xn (j + 1) % xn n, from (lt_or_eq_of_le (nat.le_of_succ_le_succ ij)).elim (λh, lt_trans (eq_of_xn_modeq_lem1 h (le_of_lt jn)) this) (λh, by rw h; exact this), by rw [nat.mod_eq_of_lt (strict_mono_x _ (nat.lt_of_succ_lt jn)), nat.mod_eq_of_lt (strict_mono_x _ jn)]; exact strict_mono_x _ (nat.lt_succ_self _) theorem eq_of_xn_modeq_lem2 {n} (h : 2 * xn n = xn (n + 1)) : a = 2 ∧ n = 0 := by rw [xn_succ, mul_comm] at h; exact have n = 0, from n.eq_zero_or_pos.resolve_right $ λnp, ne_of_lt (lt_of_le_of_lt (nat.mul_le_mul_left _ a1) (nat.lt_add_of_pos_right $ mul_pos (d_pos a1) (strict_mono_y a1 np))) h, by cases this; simp at h; exact ⟨h.symm, rfl⟩ theorem eq_of_xn_modeq_lem3 {i n} (npos : 0 < n) : Π {j}, i < j → j ≤ 2 * n → j ≠ n → ¬(a = 2 ∧ n = 1 ∧ i = 0 ∧ j = 2) → xn i % xn n < xn j % xn n | 0 ij _ _ _ := absurd ij (nat.not_lt_zero _) | (j+1) ij j2n jnn ntriv := have lem2 : ∀k > n, k ≤ 2*n → (↑(xn k % xn n) : ℤ) = xn n - xn (2 * n - k), from λk kn k2n, let k2nl := lt_of_add_lt_add_right $ show 2*n-k+k < n+k, by {rw tsub_add_cancel_of_le, rw two_mul; exact (add_lt_add_left kn n), exact k2n } in have xle : xn (2 * n - k) ≤ xn n, from le_of_lt $ strict_mono_x k2nl, suffices xn k % xn n = xn n - xn (2 * n - k), by rw [this, int.coe_nat_sub xle], by { rw ← nat.mod_eq_of_lt (nat.sub_lt (x_pos a1 n) (x_pos a1 (2 * n - k))), apply modeq.add_right_cancel' (xn a1 (2 * n - k)), rw [tsub_add_cancel_of_le xle], have t := xn_modeq_x2n_sub_lem a1 k2nl.le, rw tsub_tsub_cancel_of_le k2n at t, exact t.trans dvd_rfl.zero_modeq_nat }, (lt_trichotomy j n).elim (λ (jn : j < n), eq_of_xn_modeq_lem1 ij (lt_of_le_of_ne jn jnn)) $ λ o, o.elim (λ (jn : j = n), by { cases jn, apply int.lt_of_coe_nat_lt_coe_nat, rw [lem2 (n+1) (nat.lt_succ_self _) j2n, show 2 * n - (n + 1) = n - 1, by rw[two_mul, tsub_add_eq_tsub_tsub, add_tsub_cancel_right]], refine lt_sub_left_of_add_lt (int.coe_nat_lt_coe_nat_of_lt _), cases (lt_or_eq_of_le $ nat.le_of_succ_le_succ ij) with lin ein, { rw nat.mod_eq_of_lt (strict_mono_x _ lin), have ll : xn a1 (n-1) + xn a1 (n-1) ≤ xn a1 n, { rw [← two_mul, mul_comm, show xn a1 n = xn a1 (n-1+1), by rw [tsub_add_cancel_of_le (succ_le_of_lt npos)], xn_succ], exact le_trans (nat.mul_le_mul_left _ a1) (nat.le_add_right _ _) }, have npm : (n-1).succ = n := nat.succ_pred_eq_of_pos npos, have il : i ≤ n - 1, { apply nat.le_of_succ_le_succ, rw npm, exact lin }, cases lt_or_eq_of_le il with ill ile, { exact lt_of_lt_of_le (nat.add_lt_add_left (strict_mono_x a1 ill) _) ll }, { rw ile, apply lt_of_le_of_ne ll, rw ← two_mul, exact λe, ntriv $ let ⟨a2, s1⟩ := @eq_of_xn_modeq_lem2 _ a1 (n-1) (by rwa [tsub_add_cancel_of_le (succ_le_of_lt npos)]) in have n1 : n = 1, from le_antisymm (tsub_eq_zero_iff_le.mp s1) npos, by rw [ile, a2, n1]; exact ⟨rfl, rfl, rfl, rfl⟩ } }, { rw [ein, nat.mod_self, add_zero], exact strict_mono_x _ (nat.pred_lt npos.ne') } }) (λ (jn : j > n), have lem1 : j ≠ n → xn j % xn n < xn (j + 1) % xn n → xn i % xn n < xn (j + 1) % xn n, from λjn s, (lt_or_eq_of_le (nat.le_of_succ_le_succ ij)).elim (λh, lt_trans (eq_of_xn_modeq_lem3 h (le_of_lt j2n) jn $ λ⟨a1, n1, i0, j2⟩, by rw [n1, j2] at j2n; exact absurd j2n dec_trivial) s) (λh, by rw h; exact s), lem1 (ne_of_gt jn) $ int.lt_of_coe_nat_lt_coe_nat $ by { rw [lem2 j jn (le_of_lt j2n), lem2 (j+1) (nat.le_succ_of_le jn) j2n], refine sub_lt_sub_left (int.coe_nat_lt_coe_nat_of_lt $ strict_mono_x _ _) _, rw [nat.sub_succ], exact nat.pred_lt (ne_of_gt $ tsub_pos_of_lt j2n) }) theorem eq_of_xn_modeq_le {i j n} (ij : i ≤ j) (j2n : j ≤ 2 * n) (h : xn i ≡ xn j [MOD xn n]) (ntriv : ¬(a = 2 ∧ n = 1 ∧ i = 0 ∧ j = 2)) : i = j := if npos : n = 0 then by simp [*] at * else (lt_or_eq_of_le ij).resolve_left $ λij', if jn : j = n then by { refine ne_of_gt _ h, rw [jn, nat.mod_self], have x0 : 0 < xn a1 0 % xn a1 n := by rw [nat.mod_eq_of_lt (strict_mono_x a1 (nat.pos_of_ne_zero npos))]; exact dec_trivial, cases i with i, exact x0, rw jn at ij', exact x0.trans (eq_of_xn_modeq_lem3 _ (nat.pos_of_ne_zero npos) (nat.succ_pos _) (le_trans ij j2n) (ne_of_lt ij') $ λ⟨a1, n1, _, i2⟩, by rw [n1, i2] at ij'; exact absurd ij' dec_trivial) } else ne_of_lt (eq_of_xn_modeq_lem3 (nat.pos_of_ne_zero npos) ij' j2n jn ntriv) h theorem eq_of_xn_modeq {i j n} (i2n : i ≤ 2 * n) (j2n : j ≤ 2 * n) (h : xn i ≡ xn j [MOD xn n]) (ntriv : a = 2 → n = 1 → (i = 0 → j ≠ 2) ∧ (i = 2 → j ≠ 0)) : i = j := (le_total i j).elim (λij, eq_of_xn_modeq_le ij j2n h $ λ⟨a2, n1, i0, j2⟩, (ntriv a2 n1).left i0 j2) (λij, (eq_of_xn_modeq_le ij i2n h.symm $ λ⟨a2, n1, j0, i2⟩, (ntriv a2 n1).right i2 j0).symm) theorem eq_of_xn_modeq' {i j n} (ipos : 0 < i) (hin : i ≤ n) (j4n : j ≤ 4 * n) (h : xn j ≡ xn i [MOD xn n]) : j = i ∨ j + i = 4 * n := have i2n : i ≤ 2*n, by apply le_trans hin; rw two_mul; apply nat.le_add_left, (le_or_gt j (2 * n)).imp (λj2n : j ≤ 2 * n, eq_of_xn_modeq j2n i2n h $ λa2 n1, ⟨λj0 i2, by rw [n1, i2] at hin; exact absurd hin dec_trivial, λj2 i0, ne_of_gt ipos i0⟩) (λj2n : 2 * n < j, suffices i = 4*n - j, by rw [this, add_tsub_cancel_of_le j4n], have j42n : 4*n - j ≤ 2*n, from @nat.le_of_add_le_add_right j _ _ $ by rw [tsub_add_cancel_of_le j4n, show 4*n = 2*n + 2*n, from right_distrib 2 2 n]; exact nat.add_le_add_left (le_of_lt j2n) _, eq_of_xn_modeq i2n j42n (h.symm.trans $ let t := xn_modeq_x4n_sub j42n in by rwa [tsub_tsub_cancel_of_le j4n] at t) (λa2 n1, ⟨λi0, absurd i0 (ne_of_gt ipos), λi2, by { rw [n1, i2] at hin, exact absurd hin dec_trivial }⟩)) theorem modeq_of_xn_modeq {i j n} (ipos : 0 < i) (hin : i ≤ n) (h : xn j ≡ xn i [MOD xn n]) : j ≡ i [MOD 4 * n] ∨ j + i ≡ 0 [MOD 4 * n] := let j' := j % (4 * n) in have n4 : 0 < 4 * n, from mul_pos dec_trivial (ipos.trans_le hin), have jl : j' < 4 * n, from nat.mod_lt _ n4, have jj : j ≡ j' [MOD 4 * n], by delta modeq; rw nat.mod_eq_of_lt jl, have ∀j q, xn (j + 4 * n * q) ≡ xn j [MOD xn n], begin intros j q, induction q with q IH, { simp }, rw [nat.mul_succ, ← add_assoc, add_comm], exact (xn_modeq_x4n_add _ _ _).trans IH end, or.imp (λ(ji : j' = i), by rwa ← ji) (λ(ji : j' + i = 4 * n), (jj.add_right _).trans $ by { rw ji, exact dvd_rfl.modeq_zero_nat }) (eq_of_xn_modeq' ipos hin jl.le $ (h.symm.trans $ by { rw ← nat.mod_add_div j (4*n), exact this j' _ }).symm) end theorem xy_modeq_of_modeq {a b c} (a1 : 1 < a) (b1 : 1 < b) (h : a ≡ b [MOD c]) : ∀ n, xn a1 n ≡ xn b1 n [MOD c] ∧ yn a1 n ≡ yn b1 n [MOD c] | 0 := by constructor; refl | 1 := by simp; exact ⟨h, modeq.refl 1⟩ | (n+2) := ⟨ (xy_modeq_of_modeq n).left.add_right_cancel $ by { rw [xn_succ_succ a1, xn_succ_succ b1], exact (h.mul_left _ ).mul (xy_modeq_of_modeq (n+1)).left }, (xy_modeq_of_modeq n).right.add_right_cancel $ by { rw [yn_succ_succ a1, yn_succ_succ b1], exact (h.mul_left _ ).mul (xy_modeq_of_modeq (n+1)).right }⟩ theorem matiyasevic {a k x y} : (∃ a1 : 1 < a, xn a1 k = x ∧ yn a1 k = y) ↔ 1 < a ∧ k ≤ y ∧ (x = 1 ∧ y = 0 ∨ ∃ (u v s t b : ℕ), x * x - (a * a - 1) * y * y = 1 ∧ u * u - (a * a - 1) * v * v = 1 ∧ s * s - (b * b - 1) * t * t = 1 ∧ 1 < b ∧ b ≡ 1 [MOD 4 * y] ∧ b ≡ a [MOD u] ∧ 0 < v ∧ y * y ∣ v ∧ s ≡ x [MOD u] ∧ t ≡ k [MOD 4 * y]) := ⟨λ⟨a1, hx, hy⟩, by rw [← hx, ← hy]; refine ⟨a1, (nat.eq_zero_or_pos k).elim (λk0, by rw k0; exact ⟨le_rfl, or.inl ⟨rfl, rfl⟩⟩) (λkpos, _)⟩; exact let x := xn a1 k, y := yn a1 k, m := 2 * (k * y), u := xn a1 m, v := yn a1 m in have ky : k ≤ y, from yn_ge_n a1 k, have yv : y * y ∣ v, from (ysq_dvd_yy a1 k).trans $ (y_dvd_iff _ _ _).2 $ dvd_mul_left _ _, have uco : nat.coprime u (4 * y), from have 2 ∣ v, from modeq_zero_iff_dvd.1 $ (yn_modeq_two _ _).trans (dvd_mul_right _ _).modeq_zero_nat, have nat.coprime u 2, from (xy_coprime a1 m).coprime_dvd_right this, (this.mul_right this).mul_right $ (xy_coprime _ _).coprime_dvd_right (dvd_of_mul_left_dvd yv), let ⟨b, ba, bm1⟩ := chinese_remainder uco a 1 in have m1 : 1 < m, from have 0 < k * y, from mul_pos kpos (strict_mono_y a1 kpos), nat.mul_le_mul_left 2 this, have vp : 0 < v, from strict_mono_y a1 (lt_trans zero_lt_one m1), have b1 : 1 < b, from have xn a1 1 < u, from strict_mono_x a1 m1, have a < u, by simp at this; exact this, lt_of_lt_of_le a1 $ by delta modeq at ba; rw nat.mod_eq_of_lt this at ba; rw ← ba; apply nat.mod_le, let s := xn b1 k, t := yn b1 k in have sx : s ≡ x [MOD u], from (xy_modeq_of_modeq b1 a1 ba k).left, have tk : t ≡ k [MOD 4 * y], from have 4 * y ∣ b - 1, from int.coe_nat_dvd.1 $ by rw int.coe_nat_sub (le_of_lt b1); exact bm1.symm.dvd, (yn_modeq_a_sub_one _ _).modeq_of_dvd this, ⟨ky, or.inr ⟨u, v, s, t, b, pell_eq _ _, pell_eq _ _, pell_eq _ _, b1, bm1, ba, vp, yv, sx, tk⟩⟩, λ⟨a1, ky, o⟩, ⟨a1, match o with | or.inl ⟨x1, y0⟩ := by rw y0 at ky; rw [nat.eq_zero_of_le_zero ky, x1, y0]; exact ⟨rfl, rfl⟩ | or.inr ⟨u, v, s, t, b, xy, uv, st, b1, rem⟩ := match x, y, eq_pell a1 xy, u, v, eq_pell a1 uv, s, t, eq_pell b1 st, rem, ky with | ._, ._, ⟨i, rfl, rfl⟩, ._, ._, ⟨n, rfl, rfl⟩, ._, ._, ⟨j, rfl, rfl⟩, ⟨(bm1 : b ≡ 1 [MOD 4 * yn a1 i]), (ba : b ≡ a [MOD xn a1 n]), (vp : 0 < yn a1 n), (yv : yn a1 i * yn a1 i ∣ yn a1 n), (sx : xn b1 j ≡ xn a1 i [MOD xn a1 n]), (tk : yn b1 j ≡ k [MOD 4 * yn a1 i])⟩, (ky : k ≤ yn a1 i) := (nat.eq_zero_or_pos i).elim (λi0, by simp [i0] at ky; rw [i0, ky]; exact ⟨rfl, rfl⟩) $ λipos, suffices i = k, by rw this; exact ⟨rfl, rfl⟩, by clear _x o rem xy uv st _match _match _fun_match; exact have iln : i ≤ n, from le_of_not_gt $ λhin, not_lt_of_ge (nat.le_of_dvd vp (dvd_of_mul_left_dvd yv)) (strict_mono_y a1 hin), have yd : 4 * yn a1 i ∣ 4 * n, from mul_dvd_mul_left _ $ dvd_of_ysq_dvd a1 yv, have jk : j ≡ k [MOD 4 * yn a1 i], from have 4 * yn a1 i ∣ b - 1, from int.coe_nat_dvd.1 $ by rw int.coe_nat_sub (le_of_lt b1); exact bm1.symm.dvd, ((yn_modeq_a_sub_one b1 _).modeq_of_dvd this).symm.trans tk, have ki : k + i < 4 * yn a1 i, from lt_of_le_of_lt (add_le_add ky (yn_ge_n a1 i)) $ by rw ← two_mul; exact nat.mul_lt_mul_of_pos_right dec_trivial (strict_mono_y a1 ipos), have ji : j ≡ i [MOD 4 * n], from have xn a1 j ≡ xn a1 i [MOD xn a1 n], from (xy_modeq_of_modeq b1 a1 ba j).left.symm.trans sx, (modeq_of_xn_modeq a1 ipos iln this).resolve_right $ λ (ji : j + i ≡ 0 [MOD 4 * n]), not_le_of_gt ki $ nat.le_of_dvd (lt_of_lt_of_le ipos $ nat.le_add_left _ _) $ modeq_zero_iff_dvd.1 $ (jk.symm.add_right i).trans $ ji.modeq_of_dvd yd, by have : i % (4 * yn a1 i) = k % (4 * yn a1 i) := (ji.modeq_of_dvd yd).symm.trans jk; rwa [nat.mod_eq_of_lt (lt_of_le_of_lt (nat.le_add_left _ _) ki), nat.mod_eq_of_lt (lt_of_le_of_lt (nat.le_add_right _ _) ki)] at this end end⟩⟩ lemma eq_pow_of_pell_lem {a y k} (a1 : 1 < a) (ypos : 0 < y) : 0 < k → y^k < a → (↑(y^k) : ℤ) < 2*a*y - y*y - 1 := have y < a → a + (y*y + 1) ≤ 2*a*y, begin intro ya, induction y with y IH, exact absurd ypos (lt_irrefl _), cases nat.eq_zero_or_pos y with y0 ypos, { rw y0, simpa [two_mul], }, { rw [nat.mul_succ, nat.mul_succ, nat.succ_mul y], have : y + nat.succ y ≤ 2 * a, { change y + y < 2 * a, rw ← two_mul, exact mul_lt_mul_of_pos_left (nat.lt_of_succ_lt ya) dec_trivial }, have := add_le_add (IH ypos (nat.lt_of_succ_lt ya)) this, convert this using 1, ring } end, λk0 yak, lt_of_lt_of_le (int.coe_nat_lt_coe_nat_of_lt yak) $ by rw sub_sub; apply le_sub_right_of_add_le; apply int.coe_nat_le_coe_nat_of_le; have y1 := nat.pow_le_pow_of_le_right ypos k0; simp at y1; exact this (lt_of_le_of_lt y1 yak) theorem eq_pow_of_pell {m n k} : (n^k = m ↔ k = 0 ∧ m = 1 ∨ 0 < k ∧ (n = 0 ∧ m = 0 ∨ 0 < n ∧ ∃ (w a t z : ℕ) (a1 : 1 < a), xn a1 k ≡ yn a1 k * (a - n) + m [MOD t] ∧ 2 * a * n = t + (n * n + 1) ∧ m < t ∧ n ≤ w ∧ k ≤ w ∧ a * a - ((w + 1) * (w + 1) - 1) * (w * z) * (w * z) = 1)) := ⟨λe, by rw ← e; refine (nat.eq_zero_or_pos k).elim (λk0, by rw k0; exact or.inl ⟨rfl, rfl⟩) (λkpos, or.inr ⟨kpos, _⟩); refine (nat.eq_zero_or_pos n).elim (λn0, by rw [n0, zero_pow kpos]; exact or.inl ⟨rfl, rfl⟩) (λnpos, or.inr ⟨npos, _⟩); exact let w := max n k in have nw : n ≤ w, from le_max_left _ _, have kw : k ≤ w, from le_max_right _ _, have wpos : 0 < w, from lt_of_lt_of_le npos nw, have w1 : 1 < w + 1, from nat.succ_lt_succ wpos, let a := xn w1 w in have a1 : 1 < a, from strict_mono_x w1 wpos, let x := xn a1 k, y := yn a1 k in let ⟨z, ze⟩ := show w ∣ yn w1 w, from modeq_zero_iff_dvd.1 $ (yn_modeq_a_sub_one w1 w).trans dvd_rfl.modeq_zero_nat in have nt : (↑(n^k) : ℤ) < 2 * a * n - n * n - 1, from eq_pow_of_pell_lem a1 npos kpos $ calc n^k ≤ n^w : nat.pow_le_pow_of_le_right npos kw ... < (w + 1)^w : nat.pow_lt_pow_of_lt_left (nat.lt_succ_of_le nw) wpos ... ≤ a : xn_ge_a_pow w1 w, let ⟨t, te⟩ := int.eq_coe_of_zero_le $ le_trans (int.coe_zero_le _) nt.le in have na : n ≤ a, from nw.trans $ le_of_lt $ n_lt_xn w1 w, have tm : x ≡ y * (a - n) + n^k [MOD t], begin apply modeq_of_dvd, rw [int.coe_nat_add, int.coe_nat_mul, int.coe_nat_sub na, ← te], exact x_sub_y_dvd_pow a1 n k end, have ta : 2 * a * n = t + (n * n + 1), from int.coe_nat_inj $ by rw [int.coe_nat_add, ← te, sub_sub]; repeat {rw int.coe_nat_add <|> rw int.coe_nat_mul}; rw [int.coe_nat_one, sub_add_cancel]; refl, have mt : n^k < t, from int.lt_of_coe_nat_lt_coe_nat $ by rw ← te; exact nt, have zp : a * a - ((w + 1) * (w + 1) - 1) * (w * z) * (w * z) = 1, by rw ← ze; exact pell_eq w1 w, ⟨w, a, t, z, a1, tm, ta, mt, nw, kw, zp⟩, λo, match o with | or.inl ⟨k0, m1⟩ := by rw [k0, m1]; refl | or.inr ⟨kpos, or.inl ⟨n0, m0⟩⟩ := by rw [n0, m0, zero_pow kpos] | or.inr ⟨kpos, or.inr ⟨npos, w, a, t, z, (a1 : 1 < a), (tm : xn a1 k ≡ yn a1 k * (a - n) + m [MOD t]), (ta : 2 * a * n = t + (n * n + 1)), (mt : m < t), (nw : n ≤ w), (kw : k ≤ w), (zp : a * a - ((w + 1) * (w + 1) - 1) * (w * z) * (w * z) = 1)⟩⟩ := have wpos : 0 < w, from lt_of_lt_of_le npos nw, have w1 : 1 < w + 1, from nat.succ_lt_succ wpos, let ⟨j, xj, yj⟩ := eq_pell w1 zp in by clear _match o _let_match; exact have jpos : 0 < j, from (nat.eq_zero_or_pos j).resolve_left $ λj0, have a1 : a = 1, by rw j0 at xj; exact xj, have 2 * n = t + (n * n + 1), by rw a1 at ta; exact ta, have n1 : n = 1, from have n * n < n * 2, by rw [mul_comm n 2, this]; apply nat.le_add_left, have n ≤ 1, from nat.le_of_lt_succ $ lt_of_mul_lt_mul_left this (nat.zero_le _), le_antisymm this npos, by rw n1 at this; rw ← @nat.add_right_cancel 0 2 t this at mt; exact nat.not_lt_zero _ mt, have wj : w ≤ j, from nat.le_of_dvd jpos $ modeq_zero_iff_dvd.1 $ (yn_modeq_a_sub_one w1 j).symm.trans $ modeq_zero_iff_dvd.2 ⟨z, yj.symm⟩, have nt : (↑(n^k) : ℤ) < 2 * a * n - n * n - 1, from eq_pow_of_pell_lem a1 npos kpos $ calc n^k ≤ n^j : nat.pow_le_pow_of_le_right npos (le_trans kw wj) ... < (w + 1)^j : nat.pow_lt_pow_of_lt_left (nat.lt_succ_of_le nw) jpos ... ≤ xn w1 j : xn_ge_a_pow w1 j ... = a : xj.symm, have na : n ≤ a, by rw xj; exact le_trans (le_trans nw wj) (le_of_lt $ n_lt_xn _ _), have te : (t : ℤ) = 2 * ↑a * ↑n - ↑n * ↑n - 1, by rw sub_sub; apply eq_sub_of_add_eq; apply (int.coe_nat_eq_coe_nat_iff _ _).2; exact ta.symm, have xn a1 k ≡ yn a1 k * (a - n) + n^k [MOD t], by have := x_sub_y_dvd_pow a1 n k; rw [← te, ← int.coe_nat_sub na] at this; exact modeq_of_dvd this, have n^k % t = m % t, from (this.symm.trans tm).add_left_cancel' _, by rw ← te at nt; rwa [nat.mod_eq_of_lt (int.lt_of_coe_nat_lt_coe_nat nt), nat.mod_eq_of_lt mt] at this end⟩ end pell
6b752cb9c45f65c91e0826bcbfb30b0dd2792a41
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/1852.lean
970ca3c2555cc4e1740c1360ce5880801b7ed587
[ "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
473
lean
class foo (F : Type) where foo : F class foobar (F : outParam Type) [foo F] where bar : F class C (α : Type) where val : α class D (α : Type) (β : outParam Type) [C β] where val1 : α val2 : β := C.val instance : C String where val := "hello" instance : C Nat where val := 42 instance : D Nat String where val1 := 37 def f (α : Type) {β : Type} {_ : C β} [D α β] : α × β := (D.val1, D.val2 α) example : f Nat = (37, "hello") := rfl
bd1faf5166cd38379d36f72e916b8bd1c26222be
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/blast_simp_sum.lean
289e2aa9422d77cd9f3b5237eeaee7b994eecc26
[ "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
1,102
lean
import data.nat open - [simp] nat definition Sum : nat → (nat → nat) → nat := sorry notation `Σ` binders ` < ` n `, ` r:(scoped f, Sum n f) := r lemma Sum_const [simp] (n : nat) (c : nat) : (Σ x < n, c) = n * c := sorry lemma Sum_add [simp] (f g : nat → nat) (n : nat) : (Σ x < n, f x + g x) = (Σ x < n, f x) + (Σ x < n, g x) := sorry attribute add.assoc add.comm add.left_comm mul_one add_zero zero_add one_mul mul.comm mul.assoc mul.left_comm [simp] example (f : nat → nat) (n : nat) : (Σ x < n, f x + 1) = (Σ x < n, f x) + n := by simp example (f g h : nat → nat) (n : nat) : (Σ x < n, f x + g x + h x) = (Σ x < n, h x) + (Σ x < n, f x) + (Σ x < n, g x) := by simp example (f g h : nat → nat) (n : nat) : (Σ x < n, f x + g x + h x) = Sum n h + (Σ x < n, f x) + (Σ x < n, g x) := by simp example (f g h : nat → nat) (n : nat) : (Σ x < n, f x + g x + h x + 0) = Sum n h + (Σ x < n, f x) + (Σ x < n, g x) := by simp example (f g h : nat → nat) (n : nat) : (Σ x < n, f x + g x + h x + 2) = 0 + Sum n h + (Σ x < n, f x) + (Σ x < n, g x) + 2 * n := by simp
0939261022bc37f1fb6d64b7d0ffb5dff00baa70
bb31430994044506fa42fd667e2d556327e18dfe
/src/topology/metric_space/pi_nat.lean
ba35f8bb48a1a0c474e2222bd8702f209bd81a9f
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
41,448
lean
/- Copyright (c) 2022 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import tactic.ring_exp import topology.metric_space.hausdorff_distance /-! # Topological study of spaces `Π (n : ℕ), E n` When `E n` are topological spaces, the space `Π (n : ℕ), E n` is naturally a topological space (with the product topology). When `E n` are uniform spaces, it also inherits a uniform structure. However, it does not inherit a canonical metric space structure of the `E n`. Nevertheless, one can put a noncanonical metric space structure (or rather, several of them). This is done in this file. ## Main definitions and results One can define a combinatorial distance on `Π (n : ℕ), E n`, as follows: * `pi_nat.cylinder x n` is the set of points `y` with `x i = y i` for `i < n`. * `pi_nat.first_diff x y` is the first index at which `x i ≠ y i`. * `pi_nat.dist x y` is equal to `(1/2) ^ (first_diff x y)`. It defines a distance on `Π (n : ℕ), E n`, compatible with the topology when the `E n` have the discrete topology. * `pi_nat.metric_space`: the metric space structure, given by this distance. Not registered as an instance. This space is a complete metric space. * `pi_nat.metric_space_of_discrete_uniformity`: the same metric space structure, but adjusting the uniformity defeqness when the `E n` already have the discrete uniformity. Not registered as an instance * `pi_nat.metric_space_nat_nat`: the particular case of `ℕ → ℕ`, not registered as an instance. These results are used to construct continuous functions on `Π n, E n`: * `pi_nat.exists_retraction_of_is_closed`: given a nonempty closed subset `s` of `Π (n : ℕ), E n`, there exists a retraction onto `s`, i.e., a continuous map from the whole space to `s` restricting to the identity on `s`. * `exists_nat_nat_continuous_surjective_of_complete_space`: given any nonempty complete metric space with second-countable topology, there exists a continuous surjection from `ℕ → ℕ` onto this space. One can also put distances on `Π (i : ι), E i` when the spaces `E i` are metric spaces (not discrete in general), and `ι` is countable. * `pi_countable.dist` is the distance on `Π i, E i` given by `dist x y = ∑' i, min (1/2)^(encode i) (dist (x i) (y i))`. * `pi_countable.metric_space` is the corresponding metric space structure, adjusted so that the uniformity is definitionally the product uniformity. Not registered as an instance. -/ noncomputable theory open_locale classical topological_space filter open topological_space set metric filter function local attribute [simp] pow_le_pow_iff one_lt_two inv_le_inv variable {E : ℕ → Type*} namespace pi_nat /-! ### The first_diff function -/ /-- In a product space `Π n, E n`, then `first_diff x y` is the first index at which `x` and `y` differ. If `x = y`, then by convention we set `first_diff x x = 0`. -/ @[irreducible, pp_nodot] def first_diff (x y : Π n, E n) : ℕ := if h : x ≠ y then nat.find (ne_iff.1 h) else 0 lemma apply_first_diff_ne {x y : Π n, E n} (h : x ≠ y) : x (first_diff x y) ≠ y (first_diff x y) := begin rw [first_diff, dif_pos h], exact nat.find_spec (ne_iff.1 h), end lemma apply_eq_of_lt_first_diff {x y : Π n, E n} {n : ℕ} (hn : n < first_diff x y) : x n = y n := begin rw first_diff at hn, split_ifs at hn, { convert nat.find_min (ne_iff.1 h) hn, simp }, { exact (not_lt_zero' hn).elim } end lemma first_diff_comm (x y : Π n, E n) : first_diff x y = first_diff y x := begin rcases eq_or_ne x y with rfl|hxy, { refl }, rcases lt_trichotomy (first_diff x y) (first_diff y x) with h|h|h, { exact (apply_first_diff_ne hxy (apply_eq_of_lt_first_diff h).symm).elim }, { exact h }, { exact (apply_first_diff_ne hxy.symm (apply_eq_of_lt_first_diff h).symm).elim } end lemma min_first_diff_le (x y z : Π n, E n) (h : x ≠ z) : min (first_diff x y) (first_diff y z) ≤ first_diff x z := begin by_contra' H, have : x (first_diff x z) = z (first_diff x z), from calc x (first_diff x z) = y (first_diff x z) : apply_eq_of_lt_first_diff (H.trans_le (min_le_left _ _)) ... = z ((first_diff x z)) : apply_eq_of_lt_first_diff (H.trans_le (min_le_right _ _)), exact (apply_first_diff_ne h this).elim, end /-! ### Cylinders -/ /-- In a product space `Π n, E n`, the cylinder set of length `n` around `x`, denoted `cylinder x n`, is the set of sequences `y` that coincide with `x` on the first `n` symbols, i.e., such that `y i = x i` for all `i < n`. -/ def cylinder (x : Π n, E n) (n : ℕ) : set (Π n, E n) := {y | ∀ i, i < n → y i = x i} lemma cylinder_eq_pi (x : Π n, E n) (n : ℕ) : cylinder x n = set.pi (finset.range n : set ℕ) (λ (i : ℕ), {x i}) := by { ext y, simp [cylinder] } @[simp] lemma cylinder_zero (x : Π n, E n) : cylinder x 0 = univ := by simp [cylinder_eq_pi] lemma cylinder_anti (x : Π n, E n) {m n : ℕ} (h : m ≤ n) : cylinder x n ⊆ cylinder x m := λ y hy i hi, hy i (hi.trans_le h) @[simp] lemma mem_cylinder_iff {x y : Π n, E n} {n : ℕ} : y ∈ cylinder x n ↔ ∀ i, i < n → y i = x i := iff.rfl lemma self_mem_cylinder (x : Π n, E n) (n : ℕ) : x ∈ cylinder x n := by simp lemma mem_cylinder_iff_eq {x y : Π n, E n} {n : ℕ} : y ∈ cylinder x n ↔ cylinder y n = cylinder x n := begin split, { assume hy, apply subset.antisymm, { assume z hz i hi, rw ← hy i hi, exact hz i hi }, { assume z hz i hi, rw hy i hi, exact hz i hi } }, { assume h, rw ← h, exact self_mem_cylinder _ _ } end lemma mem_cylinder_comm (x y : Π n, E n) (n : ℕ) : y ∈ cylinder x n ↔ x ∈ cylinder y n := by simp [mem_cylinder_iff_eq, eq_comm] lemma mem_cylinder_iff_le_first_diff {x y : Π n, E n} (hne : x ≠ y) (i : ℕ) : x ∈ cylinder y i ↔ i ≤ first_diff x y := begin split, { assume h, by_contra', exact apply_first_diff_ne hne (h _ this) }, { assume hi j hj, exact apply_eq_of_lt_first_diff (hj.trans_le hi) } end lemma mem_cylinder_first_diff (x y : Π n, E n) : x ∈ cylinder y (first_diff x y) := λ i hi, apply_eq_of_lt_first_diff hi lemma cylinder_eq_cylinder_of_le_first_diff (x y : Π n, E n) {n : ℕ} (hn : n ≤ first_diff x y) : cylinder x n = cylinder y n := begin rw ← mem_cylinder_iff_eq, assume i hi, exact apply_eq_of_lt_first_diff (hi.trans_le hn), end lemma Union_cylinder_update (x : Π n, E n) (n : ℕ) : (⋃ k, cylinder (update x n k) (n+1)) = cylinder x n := begin ext y, simp only [mem_cylinder_iff, mem_Union], split, { rintros ⟨k, hk⟩ i hi, simpa [hi.ne] using hk i (nat.lt_succ_of_lt hi) }, { assume H, refine ⟨y n, λ i hi, _⟩, rcases nat.lt_succ_iff_lt_or_eq.1 hi with h'i|rfl, { simp [H i h'i, h'i.ne] }, { simp } }, end lemma update_mem_cylinder (x : Π n, E n) (n : ℕ) (y : E n) : update x n y ∈ cylinder x n := mem_cylinder_iff.2 (λ i hi, by simp [hi.ne]) /-! ### A distance function on `Π n, E n` We define a distance function on `Π n, E n`, given by `dist x y = (1/2)^n` where `n` is the first index at which `x` and `y` differ. When each `E n` has the discrete topology, this distance will define the right topology on the product space. We do not record a global `has_dist` instance nor a `metric_space`instance, as other distances may be used on these spaces, but we register them as local instances in this section. -/ /-- The distance function on a product space `Π n, E n`, given by `dist x y = (1/2)^n` where `n` is the first index at which `x` and `y` differ. -/ protected def has_dist : has_dist (Π n, E n) := ⟨λ x y, if h : x ≠ y then (1/2 : ℝ) ^ (first_diff x y) else 0⟩ local attribute [instance] pi_nat.has_dist lemma dist_eq_of_ne {x y : Π n, E n} (h : x ≠ y) : dist x y = (1/2 : ℝ) ^ (first_diff x y) := by simp [dist, h] protected lemma dist_self (x : Π n, E n) : dist x x = 0 := by simp [dist] protected lemma dist_comm (x y : Π n, E n) : dist x y = dist y x := by simp [dist, @eq_comm _ x y, first_diff_comm] protected lemma dist_nonneg (x y : Π n, E n) : 0 ≤ dist x y := begin rcases eq_or_ne x y with rfl|h, { simp [dist] }, { simp [dist, h] } end lemma dist_triangle_nonarch (x y z : Π n, E n) : dist x z ≤ max (dist x y) (dist y z) := begin rcases eq_or_ne x z with rfl|hxz, { simp [pi_nat.dist_self x, pi_nat.dist_nonneg] }, rcases eq_or_ne x y with rfl|hxy, { simp }, rcases eq_or_ne y z with rfl|hyz, { simp }, simp only [dist_eq_of_ne, hxz, hxy, hyz, inv_le_inv, one_div, inv_pow, zero_lt_bit0, ne.def, not_false_iff, le_max_iff, zero_lt_one, pow_le_pow_iff, one_lt_two, pow_pos, min_le_iff.1 (min_first_diff_le x y z hxz)], end protected lemma dist_triangle (x y z : Π n, E n) : dist x z ≤ dist x y + dist y z := calc dist x z ≤ max (dist x y) (dist y z) : dist_triangle_nonarch x y z ... ≤ dist x y + dist y z : max_le_add_of_nonneg (pi_nat.dist_nonneg _ _) (pi_nat.dist_nonneg _ _) protected lemma eq_of_dist_eq_zero (x y : Π n, E n) (hxy : dist x y = 0) : x = y := begin rcases eq_or_ne x y with rfl|h, { refl }, simp [dist_eq_of_ne h] at hxy, exact (two_ne_zero (pow_eq_zero hxy)).elim end lemma mem_cylinder_iff_dist_le {x y : Π n, E n} {n : ℕ} : y ∈ cylinder x n ↔ dist y x ≤ (1/2)^n := begin rcases eq_or_ne y x with rfl|hne, { simp [pi_nat.dist_self] }, suffices : (∀ (i : ℕ), i < n → y i = x i) ↔ n ≤ first_diff y x, by simpa [dist_eq_of_ne hne], split, { assume hy, by_contra' H, exact apply_first_diff_ne hne (hy _ H) }, { assume h i hi, exact apply_eq_of_lt_first_diff (hi.trans_le h) } end lemma apply_eq_of_dist_lt {x y : Π n, E n} {n : ℕ} (h : dist x y < (1/2) ^ n) {i : ℕ} (hi : i ≤ n) : x i = y i := begin rcases eq_or_ne x y with rfl|hne, { refl }, have : n < first_diff x y, by simpa [dist_eq_of_ne hne, inv_lt_inv, pow_lt_pow_iff, one_lt_two] using h, exact apply_eq_of_lt_first_diff (hi.trans_lt this), end /-- A function to a pseudo-metric-space is `1`-Lipschitz if and only if points in the same cylinder of length `n` are sent to points within distance `(1/2)^n`. Not expressed using `lipschitz_with` as we don't have a metric space structure -/ lemma lipschitz_with_one_iff_forall_dist_image_le_of_mem_cylinder {α : Type*} [pseudo_metric_space α] {f : (Π n, E n) → α} : (∀ (x y : Π n, E n), dist (f x) (f y) ≤ dist x y) ↔ (∀ x y n, y ∈ cylinder x n → dist (f x) (f y) ≤ (1/2)^n) := begin split, { assume H x y n hxy, apply (H x y).trans, rw pi_nat.dist_comm, exact mem_cylinder_iff_dist_le.1 hxy }, { assume H x y, rcases eq_or_ne x y with rfl|hne, { simp [pi_nat.dist_nonneg] }, rw dist_eq_of_ne hne, apply H x y (first_diff x y), rw first_diff_comm, exact mem_cylinder_first_diff _ _ } end variables (E) [∀ n, topological_space (E n)] [∀ n, discrete_topology (E n)] lemma is_topological_basis_cylinders : is_topological_basis {s : set (Π n, E n) | ∃ (x : Π n, E n) (n : ℕ), s = cylinder x n} := begin apply is_topological_basis_of_open_of_nhds, { rintros u ⟨x, n, rfl⟩, rw cylinder_eq_pi, exact is_open_set_pi (finset.range n).finite_to_set (λ a ha, is_open_discrete _) }, { assume x u hx u_open, obtain ⟨v, ⟨U, F, hUF, rfl⟩, xU, Uu⟩ : ∃ (v : set (Π (i : ℕ), E i)) (H : v ∈ {S : set (Π (i : ℕ), E i) | ∃ (U : Π (i : ℕ), set (E i)) (F : finset ℕ), (∀ (i : ℕ), i ∈ F → U i ∈ {s : set (E i) | is_open s}) ∧ S = (F : set ℕ).pi U}), x ∈ v ∧ v ⊆ u := (is_topological_basis_pi (λ (n : ℕ), is_topological_basis_opens)).exists_subset_of_mem_open hx u_open, rcases finset.bdd_above F with ⟨n, hn⟩, refine ⟨cylinder x (n+1), ⟨x, n+1, rfl⟩, self_mem_cylinder _ _, subset.trans _ Uu⟩, assume y hy, suffices : ∀ (i : ℕ), i ∈ F → y i ∈ U i, by simpa, assume i hi, have : y i = x i := mem_cylinder_iff.1 hy i ((hn hi).trans_lt (lt_add_one n)), rw this, simp only [set.mem_pi, finset.mem_coe] at xU, exact xU i hi } end variable {E} lemma is_open_iff_dist (s : set (Π n, E n)) : is_open s ↔ ∀ x ∈ s, ∃ ε > 0, ∀ y, dist x y < ε → y ∈ s := begin split, { assume hs x hx, obtain ⟨v, ⟨y, n, rfl⟩, h'x, h's⟩ : ∃ (v : set (Π (n : ℕ), E n)) (H : v ∈ {s | ∃ (x : Π (n : ℕ), E n) (n : ℕ), s = cylinder x n}), x ∈ v ∧ v ⊆ s := (is_topological_basis_cylinders E).exists_subset_of_mem_open hx hs, rw ← mem_cylinder_iff_eq.1 h'x at h's, exact ⟨(1/2 : ℝ)^n, by simp, λ y hy, h's (λ i hi, (apply_eq_of_dist_lt hy hi.le).symm)⟩ }, { assume h, apply (is_topological_basis_cylinders E).is_open_iff.2 (λ x hx, _), rcases h x hx with ⟨ε, εpos, hε⟩, obtain ⟨n, hn⟩ : ∃ (n : ℕ), (1/2 : ℝ) ^ n < ε := exists_pow_lt_of_lt_one εpos one_half_lt_one, refine ⟨cylinder x n, ⟨x, n, rfl⟩, self_mem_cylinder x n, λ y hy, hε y _⟩, rw pi_nat.dist_comm, exact (mem_cylinder_iff_dist_le.1 hy).trans_lt hn } end /-- Metric space structure on `Π (n : ℕ), E n` when the spaces `E n` have the discrete topology, where the distance is given by `dist x y = (1/2)^n`, where `n` is the smallest index where `x` and `y` differ. Not registered as a global instance by default. Warning: this definition makes sure that the topology is defeq to the original product topology, but it does not take care of a possible uniformity. If the `E n` have a uniform structure, then there will be two non-defeq uniform structures on `Π n, E n`, the product one and the one coming from the metric structure. In this case, use `metric_space_of_discrete_uniformity` instead. -/ protected def metric_space : metric_space (Π n, E n) := metric_space.of_metrizable dist pi_nat.dist_self pi_nat.dist_comm pi_nat.dist_triangle is_open_iff_dist pi_nat.eq_of_dist_eq_zero /-- Metric space structure on `Π (n : ℕ), E n` when the spaces `E n` have the discrete uniformity, where the distance is given by `dist x y = (1/2)^n`, where `n` is the smallest index where `x` and `y` differ. Not registered as a global instance by default. -/ protected def metric_space_of_discrete_uniformity {E : ℕ → Type*} [∀ n, uniform_space (E n)] (h : ∀ n, uniformity (E n) = 𝓟 id_rel) : metric_space (Π n, E n) := begin haveI : ∀ n, discrete_topology (E n) := λ n, discrete_topology_of_discrete_uniformity (h n), exact { dist_triangle := pi_nat.dist_triangle, dist_comm := pi_nat.dist_comm, dist_self := pi_nat.dist_self, eq_of_dist_eq_zero := pi_nat.eq_of_dist_eq_zero, to_uniform_space := Pi.uniform_space _, uniformity_dist := begin simp [Pi.uniformity, comap_infi, gt_iff_lt, preimage_set_of_eq, comap_principal, pseudo_metric_space.uniformity_dist, h, id_rel], apply le_antisymm, { simp only [le_infi_iff, le_principal_iff], assume ε εpos, obtain ⟨n, hn⟩ : ∃ n, (1/2 : ℝ)^n < ε := exists_pow_lt_of_lt_one εpos (by norm_num), apply @mem_infi_of_Inter _ _ _ _ _ (finset.range n).finite_to_set (λ i, {p : (Π (n : ℕ), E n) × Π (n : ℕ), E n | p.fst i = p.snd i}), { simp only [mem_principal, set_of_subset_set_of, imp_self, implies_true_iff] }, { rintros ⟨x, y⟩ hxy, simp only [finset.mem_coe, finset.mem_range, Inter_coe_set, mem_Inter, mem_set_of_eq] at hxy, apply lt_of_le_of_lt _ hn, rw [← mem_cylinder_iff_dist_le, mem_cylinder_iff], exact hxy } }, { simp only [le_infi_iff, le_principal_iff], assume n, refine mem_infi_of_mem ((1/2)^n) _, refine mem_infi_of_mem (by positivity) _, simp only [mem_principal, set_of_subset_set_of, prod.forall], assume x y hxy, exact apply_eq_of_dist_lt hxy le_rfl } end } end /-- Metric space structure on `ℕ → ℕ` where the distance is given by `dist x y = (1/2)^n`, where `n` is the smallest index where `x` and `y` differ. Not registered as a global instance by default. -/ def metric_space_nat_nat : metric_space (ℕ → ℕ) := pi_nat.metric_space_of_discrete_uniformity (λ n, rfl) local attribute [instance] pi_nat.metric_space protected lemma complete_space : complete_space (Π n, E n) := begin refine metric.complete_of_convergent_controlled_sequences (λ n, (1/2)^n) (by simp) _, assume u hu, refine ⟨λ n, u n n, tendsto_pi_nhds.2 (λ i, _)⟩, refine tendsto_const_nhds.congr' _, filter_upwards [filter.Ici_mem_at_top i] with n hn, exact apply_eq_of_dist_lt (hu i i n le_rfl hn) le_rfl, end /-! ### Retractions inside product spaces We show that, in a space `Π (n : ℕ), E n` where each `E n` is discrete, there is a retraction on any closed nonempty subset `s`, i.e., a continuous map `f` from the whole space to `s` restricting to the identity on `s`. The map `f` is defined as follows. For `x ∈ s`, let `f x = x`. Otherwise, consider the longest prefix `w` that `x` shares with an element of `s`, and let `f x = z_w` where `z_w` is an element of `s` starting with `w`. -/ lemma exists_disjoint_cylinder {s : set (Π n, E n)} (hs : is_closed s) {x : Π n, E n} (hx : x ∉ s) : ∃ n, disjoint s (cylinder x n) := begin unfreezingI { rcases eq_empty_or_nonempty s with rfl|hne }, { exact ⟨0, by simp⟩ }, have A : 0 < inf_dist x s := (hs.not_mem_iff_inf_dist_pos hne).1 hx, obtain ⟨n, hn⟩ : ∃ n, (1/2 : ℝ)^n < inf_dist x s := exists_pow_lt_of_lt_one A (one_half_lt_one), refine ⟨n, _⟩, apply disjoint_left.2 (λ y ys hy, _), apply lt_irrefl (inf_dist x s), calc inf_dist x s ≤ dist x y : inf_dist_le_dist_of_mem ys ... ≤ (1/2)^n : by { rw mem_cylinder_comm at hy, exact mem_cylinder_iff_dist_le.1 hy } ... < inf_dist x s : hn end /-- Given a point `x` in a product space `Π (n : ℕ), E n`, and `s` a subset of this space, then `shortest_prefix_diff x s` if the smallest `n` for which there is no element of `s` having the same prefix of length `n` as `x`. If there is no such `n`, then use `0` by convention. -/ def shortest_prefix_diff {E : ℕ → Type*} (x : (Π n, E n)) (s : set (Π n, E n)) : ℕ := if h : ∃ n, disjoint s (cylinder x n) then nat.find h else 0 lemma first_diff_lt_shortest_prefix_diff {s : set (Π n, E n)} (hs : is_closed s) {x y : (Π n, E n)} (hx : x ∉ s) (hy : y ∈ s) : first_diff x y < shortest_prefix_diff x s := begin have A := exists_disjoint_cylinder hs hx, rw [shortest_prefix_diff, dif_pos A], have B := nat.find_spec A, contrapose! B, rw not_disjoint_iff_nonempty_inter, refine ⟨y, hy, _⟩, rw mem_cylinder_comm, exact cylinder_anti y B (mem_cylinder_first_diff x y) end lemma shortest_prefix_diff_pos {s : set (Π n, E n)} (hs : is_closed s) (hne : s.nonempty) {x : (Π n, E n)} (hx : x ∉ s) : 0 < shortest_prefix_diff x s := begin rcases hne with ⟨y, hy⟩, exact (zero_le _).trans_lt (first_diff_lt_shortest_prefix_diff hs hx hy) end /-- Given a point `x` in a product space `Π (n : ℕ), E n`, and `s` a subset of this space, then `longest_prefix x s` if the largest `n` for which there is an element of `s` having the same prefix of length `n` as `x`. If there is no such `n`, use `0` by convention. -/ def longest_prefix {E : ℕ → Type*} (x : (Π n, E n)) (s : set (Π n, E n)) : ℕ := shortest_prefix_diff x s - 1 lemma first_diff_le_longest_prefix {s : set (Π n, E n)} (hs : is_closed s) {x y : (Π n, E n)} (hx : x ∉ s) (hy : y ∈ s) : first_diff x y ≤ longest_prefix x s := begin rw [longest_prefix, le_tsub_iff_right], { exact first_diff_lt_shortest_prefix_diff hs hx hy }, { exact shortest_prefix_diff_pos hs ⟨y, hy⟩ hx } end lemma inter_cylinder_longest_prefix_nonempty {s : set (Π n, E n)} (hs : is_closed s) (hne : s.nonempty) (x : (Π n, E n)) : (s ∩ cylinder x (longest_prefix x s)).nonempty := begin by_cases hx : x ∈ s, { exact ⟨x, hx, self_mem_cylinder _ _⟩ }, have A := exists_disjoint_cylinder hs hx, have B : longest_prefix x s < shortest_prefix_diff x s := nat.pred_lt (shortest_prefix_diff_pos hs hne hx).ne', rw [longest_prefix, shortest_prefix_diff, dif_pos A] at B ⊢, obtain ⟨y, ys, hy⟩ : ∃ (y : Π (n : ℕ), E n), y ∈ s ∧ x ∈ cylinder y (nat.find A - 1), { have := nat.find_min A B, push_neg at this, simp_rw [not_disjoint_iff, mem_cylinder_comm] at this, exact this }, refine ⟨y, ys, _⟩, rw mem_cylinder_iff_eq at hy ⊢, rw hy end lemma disjoint_cylinder_of_longest_prefix_lt {s : set (Π n, E n)} (hs : is_closed s) {x : (Π n, E n)} (hx : x ∉ s) {n : ℕ} (hn : longest_prefix x s < n) : disjoint s (cylinder x n) := begin rcases eq_empty_or_nonempty s with h's|hne, { simp [h's] }, contrapose! hn, rcases not_disjoint_iff_nonempty_inter.1 hn with ⟨y, ys, hy⟩, apply le_trans _ (first_diff_le_longest_prefix hs hx ys), apply (mem_cylinder_iff_le_first_diff (ne_of_mem_of_not_mem ys hx).symm _).1, rwa mem_cylinder_comm, end /-- If two points `x, y` coincide up to length `n`, and the longest common prefix of `x` with `s` is strictly shorter than `n`, then the longest common prefix of `y` with `s` is the same, and both cylinders of this length based at `x` and `y` coincide. -/ lemma cylinder_longest_prefix_eq_of_longest_prefix_lt_first_diff {x y : Π n, E n} {s : set (Π n, E n)} (hs : is_closed s) (hne : s.nonempty) (H : longest_prefix x s < first_diff x y) (xs : x ∉ s) (ys : y ∉ s) : cylinder x (longest_prefix x s) = cylinder y (longest_prefix y s) := begin have l_eq : longest_prefix y s = longest_prefix x s, { rcases lt_trichotomy (longest_prefix y s) (longest_prefix x s) with L|L|L, { have Ax : (s ∩ cylinder x (longest_prefix x s)).nonempty := inter_cylinder_longest_prefix_nonempty hs hne x, have Z := disjoint_cylinder_of_longest_prefix_lt hs ys L, rw first_diff_comm at H, rw [cylinder_eq_cylinder_of_le_first_diff _ _ H.le] at Z, exact (Ax.not_disjoint Z).elim }, { exact L }, { have Ay : (s ∩ cylinder y (longest_prefix y s)).nonempty := inter_cylinder_longest_prefix_nonempty hs hne y, have A'y : (s ∩ cylinder y (longest_prefix x s).succ).nonempty := Ay.mono (inter_subset_inter_right s (cylinder_anti _ L)), have Z := disjoint_cylinder_of_longest_prefix_lt hs xs (nat.lt_succ_self _), rw cylinder_eq_cylinder_of_le_first_diff _ _ H at Z, exact (A'y.not_disjoint Z).elim } }, rw [l_eq, ← mem_cylinder_iff_eq], exact cylinder_anti y H.le (mem_cylinder_first_diff x y) end /-- Given a closed nonempty subset `s` of `Π (n : ℕ), E n`, there exists a Lipschitz retraction onto this set, i.e., a Lipschitz map with range equal to `s`, equal to the identity on `s`. -/ theorem exists_lipschitz_retraction_of_is_closed {s : set (Π n, E n)} (hs : is_closed s) (hne : s.nonempty) : ∃ f : (Π n, E n) → (Π n, E n), (∀ x ∈ s, f x = x) ∧ (range f = s) ∧ lipschitz_with 1 f := begin /- The map `f` is defined as follows. For `x ∈ s`, let `f x = x`. Otherwise, consider the longest prefix `w` that `x` shares with an element of `s`, and let `f x = z_w` where `z_w` is an element of `s` starting with `w`. All the desired properties are clear, except the fact that `f` is `1`-Lipschitz: if two points `x, y` belong to a common cylinder of length `n`, one should show that their images also belong to a common cylinder of length `n`. This is a case analysis: * if both `x, y ∈ s`, then this is clear. * if `x ∈ s` but `y ∉ s`, then the longest prefix `w` of `y` shared by an element of `s` is of length at least `n` (because of `x`), and then `f y` starts with `w` and therefore stays in the same length `n` cylinder. * if `x ∉ s`, `y ∉ s`, let `w` be the longest prefix of `x` shared by an element of `s`. If its length is `< n`, then it is also the longest prefix of `y`, and we get `f x = f y = z_w`. Otherwise, `f x` remains in the same `n`-cylinder as `x`. Similarly for `y`. Finally, `f x` and `f y` are again in the same `n`-cylinder, as desired. -/ set f := λ x, if x ∈ s then x else (inter_cylinder_longest_prefix_nonempty hs hne x).some with hf, have fs : ∀ x ∈ s, f x = x := λ x xs, by simp [xs], refine ⟨f, fs, _, _⟩, -- check that the range of `f` is `s`. { apply subset.antisymm, { rintros x ⟨y, rfl⟩, by_cases hy : y ∈ s, { rwa fs y hy }, simpa [hf, if_neg hy] using (inter_cylinder_longest_prefix_nonempty hs hne y).some_spec.1 }, { assume x hx, rw ← fs x hx, exact mem_range_self _ } }, -- check that `f` is `1`-Lipschitz, by a case analysis. { apply lipschitz_with.mk_one (λ x y, _), -- exclude the trivial cases where `x = y`, or `f x = f y`. rcases eq_or_ne x y with rfl|hxy, { simp }, rcases eq_or_ne (f x) (f y) with h'|hfxfy, { simp [h', dist_nonneg] }, have I2 : cylinder x (first_diff x y) = cylinder y (first_diff x y), { rw ← mem_cylinder_iff_eq, apply mem_cylinder_first_diff }, suffices : first_diff x y ≤ first_diff (f x) (f y), by simpa [dist_eq_of_ne hxy, dist_eq_of_ne hfxfy], -- case where `x ∈ s` by_cases xs : x ∈ s, { rw [fs x xs] at ⊢ hfxfy, -- case where `y ∈ s`, trivial by_cases ys : y ∈ s, { rw [fs y ys] }, -- case where `y ∉ s` have A : (s ∩ cylinder y (longest_prefix y s)).nonempty := inter_cylinder_longest_prefix_nonempty hs hne y, have fy : f y = A.some, by simp_rw [hf, if_neg ys], have I : cylinder A.some (first_diff x y) = cylinder y (first_diff x y), { rw [← mem_cylinder_iff_eq, first_diff_comm], apply cylinder_anti y _ A.some_spec.2, exact first_diff_le_longest_prefix hs ys xs }, rwa [← fy, ← I2, ← mem_cylinder_iff_eq, mem_cylinder_iff_le_first_diff hfxfy.symm, first_diff_comm _ x] at I }, -- case where `x ∉ s` { by_cases ys : y ∈ s, -- case where `y ∈ s` (similar to the above) { have A : (s ∩ cylinder x (longest_prefix x s)).nonempty := inter_cylinder_longest_prefix_nonempty hs hne x, have fx : f x = A.some, by simp_rw [hf, if_neg xs], have I : cylinder A.some (first_diff x y) = cylinder x (first_diff x y), { rw ← mem_cylinder_iff_eq, apply cylinder_anti x _ A.some_spec.2, apply first_diff_le_longest_prefix hs xs ys }, rw fs y ys at ⊢ hfxfy, rwa [← fx, I2, ← mem_cylinder_iff_eq, mem_cylinder_iff_le_first_diff hfxfy] at I }, -- case where `y ∉ s` { have Ax : (s ∩ cylinder x (longest_prefix x s)).nonempty := inter_cylinder_longest_prefix_nonempty hs hne x, have fx : f x = Ax.some, by simp_rw [hf, if_neg xs], have Ay : (s ∩ cylinder y (longest_prefix y s)).nonempty := inter_cylinder_longest_prefix_nonempty hs hne y, have fy : f y = Ay.some, by simp_rw [hf, if_neg ys], -- case where the common prefix to `x` and `s`, or `y` and `s`, is shorter than the -- common part to `x` and `y` -- then `f x = f y`. by_cases H : longest_prefix x s < first_diff x y ∨ longest_prefix y s < first_diff x y, { have : cylinder x (longest_prefix x s) = cylinder y (longest_prefix y s), { cases H, { exact cylinder_longest_prefix_eq_of_longest_prefix_lt_first_diff hs hne H xs ys }, { symmetry, rw first_diff_comm at H, exact cylinder_longest_prefix_eq_of_longest_prefix_lt_first_diff hs hne H ys xs } }, rw [fx, fy] at hfxfy, apply (hfxfy _).elim, congr' }, -- case where the common prefix to `x` and `s` is long, as well as the common prefix to -- `y` and `s`. Then all points remain in the same cylinders. { push_neg at H, have I1 : cylinder Ax.some (first_diff x y) = cylinder x (first_diff x y), { rw ← mem_cylinder_iff_eq, exact cylinder_anti x H.1 Ax.some_spec.2 }, have I3 : cylinder y (first_diff x y) = cylinder Ay.some (first_diff x y), { rw [eq_comm, ← mem_cylinder_iff_eq], exact cylinder_anti y H.2 Ay.some_spec.2 }, have : cylinder Ax.some (first_diff x y) = cylinder Ay.some (first_diff x y), by rw [I1, I2, I3], rw [← fx, ← fy, ← mem_cylinder_iff_eq, mem_cylinder_iff_le_first_diff hfxfy] at this, exact this } } } } end /-- Given a closed nonempty subset `s` of `Π (n : ℕ), E n`, there exists a retraction onto this set, i.e., a continuous map with range equal to `s`, equal to the identity on `s`. -/ theorem exists_retraction_of_is_closed {s : set (Π n, E n)} (hs : is_closed s) (hne : s.nonempty) : ∃ f : (Π n, E n) → (Π n, E n), (∀ x ∈ s, f x = x) ∧ (range f = s) ∧ continuous f := begin rcases exists_lipschitz_retraction_of_is_closed hs hne with ⟨f, fs, frange, hf⟩, exact ⟨f, fs, frange, hf.continuous⟩ end theorem exists_retraction_subtype_of_is_closed {s : set (Π n, E n)} (hs : is_closed s) (hne : s.nonempty) : ∃ f : (Π n, E n) → s, (∀ x : s, f x = x) ∧ surjective f ∧ continuous f := begin obtain ⟨f, fs, f_range, f_cont⟩ : ∃ f : (Π n, E n) → (Π n, E n), (∀ x ∈ s, f x = x) ∧ (range f = s) ∧ continuous f := exists_retraction_of_is_closed hs hne, have A : ∀ x, f x ∈ s, by simp [← f_range], have B : ∀ (x : s), cod_restrict f s A x = x, { assume x, apply subtype.coe_injective.eq_iff.1, simpa only using fs x.val x.property }, exact ⟨cod_restrict f s A, B, λ x, ⟨x, B x⟩, f_cont.subtype_mk _⟩, end end pi_nat open pi_nat /-- Any nonempty complete second countable metric space is the continuous image of the fundamental space `ℕ → ℕ`. For a version of this theorem in the context of Polish spaces, see `exists_nat_nat_continuous_surjective_of_polish_space`. -/ lemma exists_nat_nat_continuous_surjective_of_complete_space (α : Type*) [metric_space α] [complete_space α] [second_countable_topology α] [nonempty α] : ∃ (f : (ℕ → ℕ) → α), continuous f ∧ surjective f := begin /- First, we define a surjective map from a closed subset `s` of `ℕ → ℕ`. Then, we compose this map with a retraction of `ℕ → ℕ` onto `s` to obtain the desired map. Let us consider a dense sequence `u` in `α`. Then `s` is the set of sequences `xₙ` such that the balls `closed_ball (u xₙ) (1/2^n)` have a nonempty intersection. This set is closed, and we define `f x` there to be the unique point in the intersection. This function is continuous and surjective by design. -/ letI : metric_space (ℕ → ℕ) := pi_nat.metric_space_nat_nat, have I0 : (0 : ℝ) < 1/2, by norm_num, have I1 : (1/2 : ℝ) < 1, by norm_num, rcases exists_dense_seq α with ⟨u, hu⟩, let s : set (ℕ → ℕ) := {x | (⋂ (n : ℕ), closed_ball (u (x n)) ((1/2)^n)).nonempty}, let g : s → α := λ x, x.2.some, have A : ∀ (x : s) (n : ℕ), dist (g x) (u ((x : ℕ → ℕ) n)) ≤ (1/2)^n := λ x n, (mem_Inter.1 x.2.some_mem n : _), have g_cont : continuous g, { apply continuous_iff_continuous_at.2 (λ y, _), apply continuous_at_of_locally_lipschitz zero_lt_one 4 (λ x hxy, _), rcases eq_or_ne x y with rfl|hne, { simp }, have hne' : x.1 ≠ y.1 := subtype.coe_injective.ne hne, have dist' : dist x y = dist x.1 y.1 := rfl, let n := first_diff x.1 y.1 - 1, have diff_pos : 0 < first_diff x.1 y.1, { by_contra' h, apply apply_first_diff_ne hne', rw [le_zero_iff.1 h], apply apply_eq_of_dist_lt _ le_rfl, rw pow_zero, exact hxy }, have hn : first_diff x.1 y.1 = n + 1 := (nat.succ_pred_eq_of_pos diff_pos).symm, rw [dist', dist_eq_of_ne hne', hn], have B : x.1 n = y.1 n := mem_cylinder_first_diff x.1 y.1 n (nat.pred_lt diff_pos.ne'), calc dist (g x) (g y) ≤ dist (g x) (u (x.1 n)) + dist (g y) (u (x.1 n)) : dist_triangle_right _ _ _ ... = dist (g x) (u (x.1 n)) + dist (g y) (u (y.1 n)) : by rw ← B ... ≤ (1/2)^n + (1/2)^n : add_le_add (A x n) (A y n) ... = 4 * (1 / 2) ^ (n + 1) : by ring_exp }, have g_surj : surjective g, { assume y, have : ∀ (n : ℕ), ∃ j, y ∈ closed_ball (u j) ((1/2)^n), { assume n, rcases hu.exists_dist_lt y (by simp : (0 : ℝ) < (1/2)^n) with ⟨j, hj⟩, exact ⟨j, hj.le⟩ }, choose x hx using this, have I : (⋂ (n : ℕ), closed_ball (u (x n)) ((1 / 2) ^ n)).nonempty := ⟨y, mem_Inter.2 hx⟩, refine ⟨⟨x, I⟩, _⟩, refine dist_le_zero.1 _, have J : ∀ (n : ℕ), dist (g ⟨x, I⟩) y ≤ (1/2)^n + (1/2)^n := λ n, calc dist (g ⟨x, I⟩) y ≤ dist (g ⟨x, I⟩) (u (x n)) + dist y (u (x n)) : dist_triangle_right _ _ _ ... ≤ (1/2)^n + (1/2)^n : add_le_add (A ⟨x, I⟩ n) (hx n), have L : tendsto (λ (n : ℕ), (1/2 : ℝ)^n + (1/2)^n) at_top (𝓝 (0 + 0)) := (tendsto_pow_at_top_nhds_0_of_lt_1 I0.le I1).add (tendsto_pow_at_top_nhds_0_of_lt_1 I0.le I1), rw add_zero at L, exact ge_of_tendsto' L J }, have s_closed : is_closed s, { refine is_closed_iff_cluster_pt.mpr _, assume x hx, have L : tendsto (λ (n : ℕ), diam (closed_ball (u (x n)) ((1 / 2) ^ n))) at_top (𝓝 0), { have : tendsto (λ (n : ℕ), (2 : ℝ) * (1/2)^n) at_top (𝓝 (2 * 0)) := (tendsto_pow_at_top_nhds_0_of_lt_1 I0.le I1).const_mul _, rw mul_zero at this, exact squeeze_zero (λ n, diam_nonneg) (λ n, diam_closed_ball (pow_nonneg I0.le _)) this }, refine nonempty_Inter_of_nonempty_bInter (λ n, is_closed_ball) (λ n, bounded_closed_ball) _ L, assume N, obtain ⟨y, hxy, ys⟩ : ∃ y, y ∈ ball x ((1 / 2) ^ N) ∩ s := cluster_pt_principal_iff.1 hx _ (ball_mem_nhds x (pow_pos I0 N)), have E : (⋂ (n : ℕ) (H : n ≤ N), closed_ball (u (x n)) ((1 / 2) ^ n)) = ⋂ (n : ℕ) (H : n ≤ N), closed_ball (u (y n)) ((1 / 2) ^ n), { congr, ext1 n, congr, ext1 hn, have : x n = y n := apply_eq_of_dist_lt (mem_ball'.1 hxy) hn, rw this }, rw E, apply nonempty.mono _ ys, apply Inter_subset_Inter₂ }, obtain ⟨f, -, f_surj, f_cont⟩ : ∃ f : (ℕ → ℕ) → s, (∀ x : s, f x = x) ∧ surjective f ∧ continuous f, { apply exists_retraction_subtype_of_is_closed s_closed, simpa only [nonempty_coe_sort] using g_surj.nonempty }, exact ⟨g ∘ f, g_cont.comp f_cont, g_surj.comp f_surj⟩, end namespace pi_countable /-! ### Products of (possibly non-discrete) metric spaces -/ variables {ι : Type*} [encodable ι] {F : ι → Type*} [∀ i, metric_space (F i)] open encodable /-- Given a countable family of metric spaces, one may put a distance on their product `Π i, E i`. It is highly non-canonical, though, and therefore not registered as a global instance. The distance we use here is `dist x y = ∑' i, min (1/2)^(encode i) (dist (x i) (y i))`. -/ protected def has_dist : has_dist (Π i, F i) := ⟨λ x y, ∑' (i : ι), min ((1/2)^(encode i)) (dist (x i) (y i))⟩ local attribute [instance] pi_countable.has_dist lemma dist_eq_tsum (x y : Π i, F i) : dist x y = ∑' (i : ι), min ((1/2)^(encode i)) (dist (x i) (y i)) := rfl lemma dist_summable (x y : Π i, F i) : summable (λ (i : ι), min ((1/2)^(encode i)) (dist (x i) (y i))) := begin refine summable_of_nonneg_of_le (λ i, _) (λ i, min_le_left _ _) summable_geometric_two_encode, exact le_min (pow_nonneg (by norm_num) _) (dist_nonneg) end lemma min_dist_le_dist_pi (x y : Π i, F i) (i : ι) : min ((1/2)^(encode i)) (dist (x i) (y i)) ≤ dist x y := le_tsum (dist_summable x y) i (λ j hj, le_min (by simp) (dist_nonneg)) lemma dist_le_dist_pi_of_dist_lt {x y : Π i, F i} {i : ι} (h : dist x y < (1/2)^(encode i)) : dist (x i) (y i) ≤ dist x y := by simpa only [not_le.2 h, false_or] using min_le_iff.1 (min_dist_le_dist_pi x y i) open_locale big_operators topological_space open filter open_locale nnreal variable (E) /-- Given a countable family of metric spaces, one may put a distance on their product `Π i, E i`, defining the right topology and uniform structure. It is highly non-canonical, though, and therefore not registered as a global instance. The distance we use here is `dist x y = ∑' n, min (1/2)^(encode i) (dist (x n) (y n))`. -/ protected def metric_space : metric_space (Π i, F i) := { dist_self := λ x, by simp [dist_eq_tsum], dist_comm := λ x y, by simp [dist_eq_tsum, dist_comm], dist_triangle := λ x y z, begin have I : ∀ i, min ((1/2)^(encode i)) (dist (x i) (z i)) ≤ min ((1/2)^(encode i)) (dist (x i) (y i)) + min ((1/2)^(encode i)) (dist (y i) (z i)) := λ i, calc min ((1/2)^(encode i)) (dist (x i) (z i)) ≤ min ((1/2)^(encode i)) (dist (x i) (y i) + dist (y i) (z i)) : min_le_min le_rfl (dist_triangle _ _ _) ... = min ((1/2)^(encode i)) (min ((1/2)^(encode i)) (dist (x i) (y i)) + min ((1/2)^(encode i)) (dist (y i) (z i))) : begin convert congr_arg (coe : ℝ≥0 → ℝ) (min_add_distrib ((1/2 : ℝ≥0)^(encode i)) (nndist (x i) (y i)) (nndist (y i) (z i))); simp end ... ≤ min ((1/2)^(encode i)) (dist (x i) (y i)) + min ((1/2)^(encode i)) (dist (y i) (z i)) : min_le_right _ _, calc dist x z ≤ ∑' i, (min ((1/2)^(encode i)) (dist (x i) (y i)) + min ((1/2)^(encode i)) (dist (y i) (z i))) : tsum_le_tsum I (dist_summable x z) ((dist_summable x y).add (dist_summable y z)) ... = dist x y + dist y z : tsum_add (dist_summable x y) (dist_summable y z) end, eq_of_dist_eq_zero := begin assume x y hxy, ext1 n, rw [← dist_le_zero, ← hxy], apply dist_le_dist_pi_of_dist_lt, rw hxy, simp end, to_uniform_space := Pi.uniform_space _, uniformity_dist := begin have I0 : (0 : ℝ) ≤ 1/2, by norm_num, have I1 : (1/2 : ℝ) < 1, by norm_num, simp only [Pi.uniformity, comap_infi, gt_iff_lt, preimage_set_of_eq, comap_principal, pseudo_metric_space.uniformity_dist], apply le_antisymm, { simp only [le_infi_iff, le_principal_iff], assume ε εpos, obtain ⟨K, hK⟩ : ∃ (K : finset ι), ∑' (i : {j // j ∉ K}), (1/2 : ℝ)^(encode (i : ι)) < ε/2 := ((tendsto_order.1 (tendsto_tsum_compl_at_top_zero (λ (i : ι), (1/2 : ℝ)^(encode i)))).2 _ (half_pos εpos)).exists, obtain ⟨δ, δpos, hδ⟩ : ∃ (δ : ℝ) (δpos : 0 < δ), (K.card : ℝ) * δ ≤ ε/2, { rcases nat.eq_zero_or_pos K.card with hK|hK, { exact ⟨1, zero_lt_one, by simpa only [hK, nat.cast_zero, zero_mul] using (half_pos εpos).le⟩ }, { have Kpos : 0 < (K.card : ℝ) := nat.cast_pos.2 hK, refine ⟨(ε / 2) / (K.card : ℝ), (div_pos (half_pos εpos) Kpos), le_of_eq _⟩, field_simp [Kpos.ne'], ring } }, apply @mem_infi_of_Inter _ _ _ _ _ K.finite_to_set (λ i, {p : (Π (i : ι), F i) × Π (i : ι), F i | dist (p.fst i) (p.snd i) < δ}), { rintros ⟨i, hi⟩, refine mem_infi_of_mem δ (mem_infi_of_mem δpos _), simp only [prod.forall, imp_self, mem_principal] }, { rintros ⟨x, y⟩ hxy, simp only [mem_Inter, mem_set_of_eq, set_coe.forall, finset.mem_range, finset.mem_coe] at hxy, calc dist x y = ∑' (i : ι), min ((1/2)^(encode i)) (dist (x i) (y i)) : rfl ... = ∑ i in K, min ((1/2)^(encode i)) (dist (x i) (y i)) + ∑' (i : (↑K : set ι)ᶜ), min ((1/2)^(encode (i : ι))) (dist (x i) (y i)) : (sum_add_tsum_compl (dist_summable _ _)).symm ... ≤ ∑ i in K, (dist (x i) (y i)) + ∑' (i : (↑K : set ι)ᶜ), (1/2)^(encode (i : ι)) : begin refine add_le_add (finset.sum_le_sum (λ i hi, min_le_right _ _)) _, refine tsum_le_tsum (λ i, min_le_left _ _) _ _, { apply summable.subtype (dist_summable x y) (↑K : set ι)ᶜ }, { apply summable.subtype summable_geometric_two_encode (↑K : set ι)ᶜ } end ... < (∑ i in K, δ) + ε / 2 : begin apply add_lt_add_of_le_of_lt _ hK, apply finset.sum_le_sum (λ i hi, _), apply (hxy i _).le, simpa using hi end ... ≤ ε / 2 + ε / 2 : add_le_add_right (by simpa only [finset.sum_const, nsmul_eq_mul] using hδ) _ ... = ε : add_halves _ } }, { simp only [le_infi_iff, le_principal_iff], assume i ε εpos, refine mem_infi_of_mem (min ((1/2)^(encode i)) ε) _, have : 0 < min ((1/2)^(encode i)) ε := lt_min (by simp) εpos, refine mem_infi_of_mem this _, simp only [and_imp, prod.forall, set_of_subset_set_of, lt_min_iff, mem_principal], assume x y hn hε, calc dist (x i) (y i) ≤ dist x y : dist_le_dist_pi_of_dist_lt hn ... < ε : hε } end } end pi_countable
5bb90486d0edb62569e7d445d895fd356efa7b97
94e33a31faa76775069b071adea97e86e218a8ee
/src/measure_theory/function/l2_space.lean
c9e52cd5a9fc9780b2c1d497805e5adffc127a86
[ "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
10,950
lean
/- Copyright (c) 2021 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémy Degenne -/ import analysis.inner_product_space.basic import measure_theory.integral.set_integral /-! # `L^2` space If `E` is an inner product space over `𝕜` (`ℝ` or `ℂ`), then `Lp E 2 μ` (defined in `lp_space.lean`) is also an inner product space, with inner product defined as `inner f g = ∫ a, ⟪f a, g a⟫ ∂μ`. ### Main results * `mem_L1_inner` : for `f` and `g` in `Lp E 2 μ`, the pointwise inner product `λ x, ⟪f x, g x⟫` belongs to `Lp 𝕜 1 μ`. * `integrable_inner` : for `f` and `g` in `Lp E 2 μ`, the pointwise inner product `λ x, ⟪f x, g x⟫` is integrable. * `L2.inner_product_space` : `Lp E 2 μ` is an inner product space. -/ noncomputable theory open topological_space measure_theory measure_theory.Lp open_locale nnreal ennreal measure_theory namespace measure_theory section variables {α F : Type*} {m : measurable_space α} {μ : measure α} [normed_group F] lemma mem_ℒp.integrable_sq {f : α → ℝ} (h : mem_ℒp f 2 μ) : integrable (λ x, (f x)^2) μ := by simpa [← mem_ℒp_one_iff_integrable] using h.norm_rpow ennreal.two_ne_zero ennreal.two_ne_top lemma mem_ℒp_two_iff_integrable_sq_norm {f : α → F} (hf : ae_strongly_measurable f μ) : mem_ℒp f 2 μ ↔ integrable (λ x, ∥f x∥^2) μ := begin rw ← mem_ℒp_one_iff_integrable, convert (mem_ℒp_norm_rpow_iff hf ennreal.two_ne_zero ennreal.two_ne_top).symm, { simp }, { rw [div_eq_mul_inv, ennreal.mul_inv_cancel ennreal.two_ne_zero ennreal.two_ne_top] } end lemma mem_ℒp_two_iff_integrable_sq {f : α → ℝ} (hf : ae_strongly_measurable f μ) : mem_ℒp f 2 μ ↔ integrable (λ x, (f x)^2) μ := begin convert mem_ℒp_two_iff_integrable_sq_norm hf, ext x, simp, end end namespace L2 variables {α E F 𝕜 : Type*} [is_R_or_C 𝕜] [measurable_space α] {μ : measure α} [inner_product_space 𝕜 E] [normed_group F] local notation `⟪`x`, `y`⟫` := @inner 𝕜 _ _ x y lemma snorm_rpow_two_norm_lt_top (f : Lp F 2 μ) : snorm (λ x, ∥f x∥ ^ (2 : ℝ)) 1 μ < ∞ := begin have h_two : ennreal.of_real (2 : ℝ) = 2, by simp [zero_le_one], rw [snorm_norm_rpow f zero_lt_two, one_mul, h_two], exact ennreal.rpow_lt_top_of_nonneg zero_le_two (Lp.snorm_ne_top f), end lemma snorm_inner_lt_top (f g : α →₂[μ] E) : snorm (λ (x : α), ⟪f x, g x⟫) 1 μ < ∞ := begin have h : ∀ x, is_R_or_C.abs ⟪f x, g x⟫ ≤ ∥f x∥ * ∥g x∥, from λ x, abs_inner_le_norm _ _, have h' : ∀ x, is_R_or_C.abs ⟪f x, g x⟫ ≤ is_R_or_C.abs (∥f x∥^2 + ∥g x∥^2), { refine λ x, le_trans (h x) _, rw [is_R_or_C.abs_to_real, abs_eq_self.mpr], swap, { exact add_nonneg (by simp) (by simp), }, refine le_trans _ (half_le_self (add_nonneg (sq_nonneg _) (sq_nonneg _))), refine (le_div_iff (@zero_lt_two ℝ _ _)).mpr ((le_of_eq _).trans (two_mul_le_add_sq _ _)), ring, }, simp_rw [← is_R_or_C.norm_eq_abs, ← real.rpow_nat_cast] at h', refine (snorm_mono_ae (ae_of_all _ h')).trans_lt ((snorm_add_le _ _ le_rfl).trans_lt _), { exact ((Lp.ae_strongly_measurable f).norm.ae_measurable.pow_const _).ae_strongly_measurable }, { exact ((Lp.ae_strongly_measurable g).norm.ae_measurable.pow_const _).ae_strongly_measurable }, simp only [nat.cast_bit0, ennreal.add_lt_top, nat.cast_one], exact ⟨snorm_rpow_two_norm_lt_top f, snorm_rpow_two_norm_lt_top g⟩, end section inner_product_space open_locale complex_conjugate include 𝕜 instance : has_inner 𝕜 (α →₂[μ] E) := ⟨λ f g, ∫ a, ⟪f a, g a⟫ ∂μ⟩ lemma inner_def (f g : α →₂[μ] E) : ⟪f, g⟫ = ∫ a : α, ⟪f a, g a⟫ ∂μ := rfl lemma integral_inner_eq_sq_snorm (f : α →₂[μ] E) : ∫ a, ⟪f a, f a⟫ ∂μ = ennreal.to_real ∫⁻ a, (∥f a∥₊ : ℝ≥0∞) ^ (2:ℝ) ∂μ := begin simp_rw inner_self_eq_norm_sq_to_K, norm_cast, rw integral_eq_lintegral_of_nonneg_ae, rotate, { exact filter.eventually_of_forall (λ x, sq_nonneg _), }, { exact ((Lp.ae_strongly_measurable f).norm.ae_measurable.pow_const _).ae_strongly_measurable }, congr, ext1 x, have h_two : (2 : ℝ) = ((2 : ℕ) : ℝ), by simp, rw [← real.rpow_nat_cast _ 2, ← h_two, ← ennreal.of_real_rpow_of_nonneg (norm_nonneg _) zero_le_two, of_real_norm_eq_coe_nnnorm], norm_cast, end private lemma norm_sq_eq_inner' (f : α →₂[μ] E) : ∥f∥ ^ 2 = is_R_or_C.re ⟪f, f⟫ := begin have h_two : (2 : ℝ≥0∞).to_real = 2 := by simp, rw [inner_def, integral_inner_eq_sq_snorm, norm_def, ← ennreal.to_real_pow, is_R_or_C.of_real_re, ennreal.to_real_eq_to_real (ennreal.pow_ne_top (Lp.snorm_ne_top f)) _], { rw [←ennreal.rpow_nat_cast, snorm_eq_snorm' ennreal.two_ne_zero ennreal.two_ne_top, snorm', ← ennreal.rpow_mul, one_div, h_two], simp, }, { refine (lintegral_rpow_nnnorm_lt_top_of_snorm'_lt_top zero_lt_two _).ne, rw [← h_two, ← snorm_eq_snorm' ennreal.two_ne_zero ennreal.two_ne_top], exact Lp.snorm_lt_top f, }, end lemma mem_L1_inner (f g : α →₂[μ] E) : ae_eq_fun.mk (λ x, ⟪f x, g x⟫) ((Lp.ae_strongly_measurable f).inner (Lp.ae_strongly_measurable g)) ∈ Lp 𝕜 1 μ := by { simp_rw [mem_Lp_iff_snorm_lt_top, snorm_ae_eq_fun], exact snorm_inner_lt_top f g, } lemma integrable_inner (f g : α →₂[μ] E) : integrable (λ x : α, ⟪f x, g x⟫) μ := (integrable_congr (ae_eq_fun.coe_fn_mk (λ x, ⟪f x, g x⟫) ((Lp.ae_strongly_measurable f).inner (Lp.ae_strongly_measurable g)))).mp (ae_eq_fun.integrable_iff_mem_L1.mpr (mem_L1_inner f g)) private lemma add_left' (f f' g : α →₂[μ] E) : ⟪f + f', g⟫ = inner f g + inner f' g := begin simp_rw [inner_def, ← integral_add (integrable_inner f g) (integrable_inner f' g), ←inner_add_left], refine integral_congr_ae ((coe_fn_add f f').mono (λ x hx, _)), congr, rwa pi.add_apply at hx, end private lemma smul_left' (f g : α →₂[μ] E) (r : 𝕜) : ⟪r • f, g⟫ = conj r * inner f g := begin rw [inner_def, inner_def, ← smul_eq_mul, ← integral_smul], refine integral_congr_ae ((coe_fn_smul r f).mono (λ x hx, _)), rw [smul_eq_mul, ← inner_smul_left], congr, rwa pi.smul_apply at hx, end instance inner_product_space : inner_product_space 𝕜 (α →₂[μ] E) := { norm_sq_eq_inner := norm_sq_eq_inner', conj_sym := λ _ _, by simp_rw [inner_def, ← integral_conj, inner_conj_sym], add_left := add_left', smul_left := smul_left', } end inner_product_space section indicator_const_Lp variables (𝕜) {s : set α} /-- The inner product in `L2` of the indicator of a set `indicator_const_Lp 2 hs hμs c` and `f` is equal to the integral of the inner product over `s`: `∫ x in s, ⟪c, f x⟫ ∂μ`. -/ lemma inner_indicator_const_Lp_eq_set_integral_inner (f : Lp E 2 μ) (hs : measurable_set s) (c : E) (hμs : μ s ≠ ∞) : (⟪indicator_const_Lp 2 hs hμs c, f⟫ : 𝕜) = ∫ x in s, ⟪c, f x⟫ ∂μ := begin rw [inner_def, ← integral_add_compl hs (L2.integrable_inner _ f)], have h_left : ∫ x in s, ⟪(indicator_const_Lp 2 hs hμs c) x, f x⟫ ∂μ = ∫ x in s, ⟪c, f x⟫ ∂μ, { suffices h_ae_eq : ∀ᵐ x ∂μ, x ∈ s → ⟪indicator_const_Lp 2 hs hμs c x, f x⟫ = ⟪c, f x⟫, from set_integral_congr_ae hs h_ae_eq, have h_indicator : ∀ᵐ (x : α) ∂μ, x ∈ s → (indicator_const_Lp 2 hs hμs c x) = c, from indicator_const_Lp_coe_fn_mem, refine h_indicator.mono (λ x hx hxs, _), congr, exact hx hxs, }, have h_right : ∫ x in sᶜ, ⟪(indicator_const_Lp 2 hs hμs c) x, f x⟫ ∂μ = 0, { suffices h_ae_eq : ∀ᵐ x ∂μ, x ∉ s → ⟪indicator_const_Lp 2 hs hμs c x, f x⟫ = 0, { simp_rw ← set.mem_compl_iff at h_ae_eq, suffices h_int_zero : ∫ x in sᶜ, inner (indicator_const_Lp 2 hs hμs c x) (f x) ∂μ = ∫ x in sᶜ, (0 : 𝕜) ∂μ, { rw h_int_zero, simp, }, exact set_integral_congr_ae hs.compl h_ae_eq, }, have h_indicator : ∀ᵐ (x : α) ∂μ, x ∉ s → (indicator_const_Lp 2 hs hμs c x) = 0, from indicator_const_Lp_coe_fn_nmem, refine h_indicator.mono (λ x hx hxs, _), rw hx hxs, exact inner_zero_left, }, rw [h_left, h_right, add_zero], end /-- The inner product in `L2` of the indicator of a set `indicator_const_Lp 2 hs hμs c` and `f` is equal to the inner product of the constant `c` and the integral of `f` over `s`. -/ lemma inner_indicator_const_Lp_eq_inner_set_integral [complete_space E] [normed_space ℝ E] (hs : measurable_set s) (hμs : μ s ≠ ∞) (c : E) (f : Lp E 2 μ) : (⟪indicator_const_Lp 2 hs hμs c, f⟫ : 𝕜) = ⟪c, ∫ x in s, f x ∂μ⟫ := by rw [← integral_inner (integrable_on_Lp_of_measure_ne_top f fact_one_le_two_ennreal.elim hμs), L2.inner_indicator_const_Lp_eq_set_integral_inner] variables {𝕜} /-- The inner product in `L2` of the indicator of a set `indicator_const_Lp 2 hs hμs (1 : 𝕜)` and a real or complex function `f` is equal to the integral of `f` over `s`. -/ lemma inner_indicator_const_Lp_one (hs : measurable_set s) (hμs : μ s ≠ ∞) (f : Lp 𝕜 2 μ) : ⟪indicator_const_Lp 2 hs hμs (1 : 𝕜), f⟫ = ∫ x in s, f x ∂μ := by { rw L2.inner_indicator_const_Lp_eq_inner_set_integral 𝕜 hs hμs (1 : 𝕜) f, simp, } end indicator_const_Lp end L2 section inner_continuous variables {α : Type*} [topological_space α] [measure_space α] [borel_space α] {𝕜 : Type*} [is_R_or_C 𝕜] variables (μ : measure α) [is_finite_measure μ] open_locale bounded_continuous_function complex_conjugate local notation `⟪`x`, `y`⟫` := @inner 𝕜 (α →₂[μ] 𝕜) _ x y /-- For bounded continuous functions `f`, `g` on a finite-measure topological space `α`, the L^2 inner product is the integral of their pointwise inner product. -/ lemma bounded_continuous_function.inner_to_Lp (f g : α →ᵇ 𝕜) : ⟪bounded_continuous_function.to_Lp 2 μ 𝕜 f, bounded_continuous_function.to_Lp 2 μ 𝕜 g⟫ = ∫ x, conj (f x) * g x ∂μ := begin apply integral_congr_ae, have hf_ae := f.coe_fn_to_Lp μ, have hg_ae := g.coe_fn_to_Lp μ, filter_upwards [hf_ae, hg_ae] with _ hf hg, rw [hf, hg], simp end variables [compact_space α] /-- For continuous functions `f`, `g` on a compact, finite-measure topological space `α`, the L^2 inner product is the integral of their pointwise inner product. -/ lemma continuous_map.inner_to_Lp (f g : C(α, 𝕜)) : ⟪continuous_map.to_Lp 2 μ 𝕜 f, continuous_map.to_Lp 2 μ 𝕜 g⟫ = ∫ x, conj (f x) * g x ∂μ := begin apply integral_congr_ae, have hf_ae := f.coe_fn_to_Lp μ, have hg_ae := g.coe_fn_to_Lp μ, filter_upwards [hf_ae, hg_ae] with _ hf hg, rw [hf, hg], simp end end inner_continuous end measure_theory
cf5761a4997861db39e7a35b3547d21a30129b42
2469dcaa81734b4823dbd29f4534010be87e60ea
/library/system/io.lean
75138b2facfccb651161971680126536807e37a8
[ "Apache-2.0" ]
permissive
elefthei/lean
d43213fee34f1d971121d872ad9bfcae684c354c
6e02ce9b345bc9263272668ead709d3d24c2a1fa
refs/heads/master
1,630,434,764,528
1,513,935,338,000
1,513,940,069,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,179
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Luke Nelson, Jared Roesch and Leonardo de Moura -/ import data.buffer inductive io.error | other : string → io.error | sys : nat → io.error structure io.terminal (m : Type → Type → Type) := (put_str : string → m io.error unit) (get_line : m io.error string) (cmdline_args : list string) inductive io.mode | read | write | read_write | append structure io.file_system (handle : Type) (m : Type → Type → Type) := /- Remark: in Haskell, they also provide (Maybe TextEncoding) and NewlineMode -/ (mk_file_handle : string → io.mode → bool → m io.error handle) (is_eof : handle → m io.error bool) (flush : handle → m io.error unit) (close : handle → m io.error unit) (read : handle → nat → m io.error char_buffer) (write : handle → char_buffer → m io.error unit) (get_line : handle → m io.error char_buffer) (stdin : m io.error handle) (stdout : m io.error handle) (stderr : m io.error handle) structure io.environment (m : Type → Type → Type) := (get_env : string → m io.error (option string)) -- we don't provide set_env as it is (thread-)unsafe (at least with glibc) inductive io.process.stdio | piped | inherit | null structure io.process.spawn_args := /- Command name. -/ (cmd : string) /- Arguments for the process -/ (args : list string := []) /- Configuration for the process' stdin handle. -/ (stdin := stdio.inherit) /- Configuration for the process' stdout handle. -/ (stdout := stdio.inherit) /- Configuration for the process' stderr handle. -/ (stderr := stdio.inherit) /- Working directory for the process. -/ (cwd : option string := none) /- Environment variables for the process. -/ (env : list (string × option string) := []) structure io.process (handle : Type) (m : Type → Type → Type) := (child : Type) (stdin : child → handle) (stdout : child → handle) (stderr : child → handle) (spawn : io.process.spawn_args → m io.error child) (wait : child → m io.error nat) class io.interface := (m : Type → Type → Type) (monad : Π e, monad (m e)) (catch : Π e₁ e₂ α, m e₁ α → (e₁ → m e₂ α) → m e₂ α) (fail : Π e α, e → m e α) (iterate : Π e α, α → (α → m e (option α)) → m e α) -- Primitive Types (handle : Type) -- Interface Extensions (term : io.terminal m) (fs : io.file_system handle m) (process : io.process handle m) (env : io.environment m) variable [ioi : io.interface] include ioi def io_core (e : Type) (α : Type) := io.interface.m e α @[reducible] def io (α : Type) := io_core io.error α instance io_core_is_monad (e : Type) : monad (io_core e) := io.interface.monad e protected def io.fail {α : Type} (s : string) : io α := io.interface.fail io.error α (io.error.other s) instance : monad_fail io := { fail := @io.fail _, ..io_core_is_monad io.error } namespace io def iterate {e α} (a : α) (f : α → io_core e (option α)) : io_core e α := interface.iterate e α a f def forever {e} (a : io_core e unit) : io_core e unit := iterate () $ λ _, a >> return (some ()) def catch {e₁ e₂ α} (a : io_core e₁ α) (b : e₁ → io_core e₂ α) : io_core e₂ α := interface.catch e₁ e₂ α a b def finally {α e} (a : io_core e α) (cleanup : io_core e unit) : io_core e α := do res ← catch (sum.inr <$> a) (return ∘ sum.inl), cleanup, match res with | sum.inr res := return res | sum.inl error := io.interface.fail _ _ error end instance : alternative io := { orelse := λ _ a b, catch a (λ _, b), failure := λ _, io.fail "failure", ..interface.monad _ } def put_str : string → io unit := interface.term.put_str def put_str_ln (s : string) : io unit := put_str s >> put_str "\n" def get_line : io string := interface.term.get_line def cmdline_args : io (list string) := return interface.term.cmdline_args def print {α} [has_to_string α] (s : α) : io unit := put_str ∘ to_string $ s def print_ln {α} [has_to_string α] (s : α) : io unit := print s >> put_str "\n" def handle : Type := interface.handle def mk_file_handle (s : string) (m : mode) (bin : bool := ff) : io handle := interface.fs.mk_file_handle s m bin def stdin : io handle := interface.fs.stdin def stderr : io handle := interface.fs.stderr def stdout : io handle := interface.fs.stdout namespace env def get (env_var : string) : io (option string) := interface.env.get_env env_var end env namespace fs def is_eof : handle → io bool := interface.fs.is_eof def flush : handle → io unit := interface.fs.flush def close : handle → io unit := interface.fs.close def read : handle → nat → io char_buffer := interface.fs.read def write : handle → char_buffer → io unit := interface.fs.write def get_char (h : handle) : io char := do b ← read h 1, if h : b.size = 1 then return $ b.read ⟨0, h.symm ▸ zero_lt_one⟩ else io.fail "get_char failed" def get_line : handle → io char_buffer := interface.fs.get_line def put_char (h : handle) (c : char) : io unit := write h (mk_buffer.push_back c) def put_str (h : handle) (s : string) : io unit := write h (mk_buffer.append_string s) def put_str_ln (h : handle) (s : string) : io unit := put_str h s >> put_str h "\n" def read_to_end (h : handle) : io char_buffer := iterate mk_buffer $ λ r, do done ← is_eof h, if done then return none else do c ← read h 1024, return $ some (r ++ c) def read_file (s : string) (bin := ff) : io char_buffer := do h ← mk_file_handle s io.mode.read bin, read_to_end h end fs namespace proc def child : Type := interface.process.child def child.stdin : child → handle := interface.process.stdin def child.stdout : child → handle := interface.process.stdout def child.stderr : child → handle := interface.process.stderr def spawn (p : io.process.spawn_args) : io child := interface.process.spawn p def wait (c : child) : io nat := interface.process.wait c end proc end io meta constant format.print_using : format → options → io unit meta definition format.print (fmt : format) : io unit := format.print_using fmt options.mk meta definition pp_using {α : Type} [has_to_format α] (a : α) (o : options) : io unit := format.print_using (to_fmt a) o meta definition pp {α : Type} [has_to_format α] (a : α) : io unit := format.print (to_fmt a) /-- Run the external process specified by `args`. The process will run to completion with its output captured by a pipe, and read into `string` which is then returned. -/ def io.cmd (args : io.process.spawn_args) : io string := do child ← io.proc.spawn { stdout := io.process.stdio.piped, ..args }, buf ← io.fs.read_to_end child.stdout, exitv ← io.proc.wait child, when (exitv ≠ 0) $ io.fail $ "process exited with status " ++ repr exitv, return buf.to_string omit ioi /-- Lift a monadic `io` action into the `tactic` monad. -/ meta constant tactic.run_io {α : Type} : (Π ioi : io.interface, @io ioi α) → tactic α
7815811fed734db745175b47c85f25be95dca07c
d642a6b1261b2cbe691e53561ac777b924751b63
/src/data/fin.lean
b6de34d5a70c7f1a523c732e456b8fa3e39bae7e
[ "Apache-2.0" ]
permissive
cipher1024/mathlib
fee56b9954e969721715e45fea8bcb95f9dc03fe
d077887141000fefa5a264e30fa57520e9f03522
refs/heads/master
1,651,806,490,504
1,573,508,694,000
1,573,508,694,000
107,216,176
0
0
Apache-2.0
1,647,363,136,000
1,508,213,014,000
Lean
UTF-8
Lean
false
false
14,249
lean
/- Copyright (c) 2017 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis, Keeley Hoek More about finite numbers. -/ import data.nat.basic open fin nat function /-- `fin 0` is empty -/ def fin_zero_elim {C : Sort*} : fin 0 → C := λ x, false.elim $ nat.not_lt_zero x.1 x.2 def {u} fin_zero_elim' {α : fin 0 → Sort u} : ∀(x : fin 0), α x | ⟨n, hn⟩ := false.elim (nat.not_lt_zero n hn) namespace fin variables {n m : ℕ} {a b : fin n} @[simp] protected lemma eta (a : fin n) (h : a.1 < n) : (⟨a.1, h⟩ : fin n) = a := by cases a; refl protected lemma ext_iff (a b : fin n) : a = b ↔ a.val = b.val := iff.intro (congr_arg _) fin.eq_of_veq lemma injective_val {n : ℕ} : injective (val : fin n → ℕ) := λ _ _, fin.eq_of_veq lemma eq_iff_veq (a b : fin n) : a = b ↔ a.1 = b.1 := ⟨veq_of_eq, eq_of_veq⟩ @[simp] protected lemma mk.inj_iff {n a b : ℕ} {ha : a < n} {hb : b < n} : fin.mk a ha = fin.mk b hb ↔ a = b := ⟨fin.mk.inj, λ h, by subst h⟩ instance fin_to_nat (n : ℕ) : has_coe (fin n) nat := ⟨fin.val⟩ @[simp] lemma mk_val {m n : ℕ} (h : m < n) : (⟨m, h⟩ : fin n).val = m := rfl @[simp] lemma coe_mk {m n : ℕ} (h : m < n) : ((⟨m, h⟩ : fin n) : ℕ) = m := rfl lemma coe_eq_val (a : fin n) : (a : ℕ) = a.val := rfl @[simp] lemma val_one {n : ℕ} : (1 : fin (n+2)).val = 1 := rfl @[simp] lemma val_two {n : ℕ} : (2 : fin (n+3)).val = 2 := rfl @[simp] lemma coe_zero {n : ℕ} : ((0 : fin (n+1)) : ℕ) = 0 := rfl @[simp] lemma coe_one {n : ℕ} : ((1 : fin (n+2)) : ℕ) = 1 := rfl @[simp] lemma coe_two {n : ℕ} : ((2 : fin (n+3)) : ℕ) = 2 := rfl instance {n : ℕ} : decidable_linear_order (fin n) := decidable_linear_order.lift fin.val (@fin.eq_of_veq _) (by apply_instance) lemma exists_iff {p : fin n → Prop} : (∃ i, p i) ↔ ∃ i h, p ⟨i, h⟩ := ⟨λ h, exists.elim h (λ ⟨i, hi⟩ hpi, ⟨i, hi, hpi⟩), λ h, exists.elim h (λ i hi, ⟨⟨i, hi.fst⟩, hi.snd⟩)⟩ lemma forall_iff {p : fin n → Prop} : (∀ i, p i) ↔ ∀ i h, p ⟨i, h⟩ := ⟨λ h i hi, h ⟨i, hi⟩, λ h ⟨i, hi⟩, h i hi⟩ lemma zero_le (a : fin (n + 1)) : 0 ≤ a := zero_le a.1 lemma lt_iff_val_lt_val : a < b ↔ a.val < b.val := iff.refl _ lemma le_iff_val_le_val : a ≤ b ↔ a.val ≤ b.val := iff.refl _ @[simp] lemma succ_val (j : fin n) : j.succ.val = j.val.succ := by cases j; simp [fin.succ] protected theorem succ.inj (p : fin.succ a = fin.succ b) : a = b := by cases a; cases b; exact eq_of_veq (nat.succ.inj (veq_of_eq p)) lemma succ_ne_zero {n} : ∀ k : fin n, fin.succ k ≠ 0 | ⟨k, hk⟩ heq := nat.succ_ne_zero k $ (fin.ext_iff _ _).1 heq @[simp] lemma pred_val (j : fin (n+1)) (h : j ≠ 0) : (j.pred h).val = j.val.pred := by cases j; simp [fin.pred] @[simp] lemma succ_pred : ∀(i : fin (n+1)) (h : i ≠ 0), (i.pred h).succ = i | ⟨0, h⟩ hi := by contradiction | ⟨n + 1, h⟩ hi := rfl @[simp] lemma pred_succ (i : fin n) {h : i.succ ≠ 0} : i.succ.pred h = i := by cases i; refl @[simp] lemma pred_inj : ∀ {a b : fin (n + 1)} {ha : a ≠ 0} {hb : b ≠ 0}, a.pred ha = b.pred hb ↔ a = b | ⟨0, _⟩ b ha hb := by contradiction | ⟨i+1, _⟩ ⟨0, _⟩ ha hb := by contradiction | ⟨i+1, hi⟩ ⟨j+1, hj⟩ ha hb := by simp [fin.eq_iff_veq] /-- The greatest value of `fin (n+1)` -/ def last (n : ℕ) : fin (n+1) := ⟨_, n.lt_succ_self⟩ /-- `cast_lt i h` embeds `i` into a `fin` where `h` proves it belongs into. -/ def cast_lt (i : fin m) (h : i.1 < n) : fin n := ⟨i.1, h⟩ /-- `cast_le h i` embeds `i` into a larger `fin` type. -/ def cast_le (h : n ≤ m) (a : fin n) : fin m := cast_lt a (lt_of_lt_of_le a.2 h) /-- `cast eq i` embeds `i` into a equal `fin` type. -/ def cast (eq : n = m) : fin n → fin m := cast_le $ le_of_eq eq /-- `cast_add m i` embedds `i` in `fin (n+m)`. -/ def cast_add (m) : fin n → fin (n + m) := cast_le $ le_add_right n m /-- `cast_succ i` embedds `i` in `fin (n+1)`. -/ def cast_succ : fin n → fin (n + 1) := cast_add 1 /-- `succ_above p i` embeds into `fin (n + 1)` with a hole around `p`. -/ def succ_above (p : fin (n+1)) (i : fin n) : fin (n+1) := if i.1 < p.1 then i.cast_succ else i.succ /-- `pred_above p i h` embeds `i` into `fin n` by ignoring `p`. -/ def pred_above (p : fin (n+1)) (i : fin (n+1)) (hi : i ≠ p) : fin n := if h : i < p then i.cast_lt (lt_of_lt_of_le h $ nat.le_of_lt_succ p.2) else i.pred $ have p < i, from lt_of_le_of_ne (le_of_not_gt h) hi.symm, ne_of_gt (lt_of_le_of_lt (zero_le p) this) /-- `sub_nat i h` subtracts `m` from `i`, generalizes `fin.pred`. -/ def sub_nat (m) (i : fin (n + m)) (h : m ≤ i.val) : fin n := ⟨i.val - m, by simp [nat.sub_lt_right_iff_lt_add h, i.is_lt]⟩ /-- `add_nat i h` adds `m` on `i`, generalizes `fin.succ`. -/ def add_nat (m) (i : fin n) : fin (n + m) := ⟨i.1 + m, add_lt_add_right i.2 _⟩ /-- `nat_add i h` adds `n` on `i` -/ def nat_add (n) {m} (i : fin m) : fin (n + m) := ⟨n + i.1, add_lt_add_left i.2 _⟩ theorem le_last (i : fin (n+1)) : i ≤ last n := le_of_lt_succ i.is_lt @[simp] lemma cast_val (k : fin n) (h : n = m) : (fin.cast h k).val = k.val := rfl @[simp] lemma cast_succ_val (k : fin n) : k.cast_succ.val = k.val := rfl @[simp] lemma cast_lt_val (k : fin m) (h : k.1 < n) : (k.cast_lt h).val = k.val := rfl @[simp] lemma cast_le_val (k : fin m) (h : m ≤ n) : (k.cast_le h).val = k.val := rfl @[simp] lemma cast_add_val (k : fin m) : (k.cast_add n).val = k.val := rfl @[simp] lemma last_val (n : ℕ) : (last n).val = n := rfl @[simp] lemma cast_succ_cast_lt (i : fin (n + 1)) (h : i.val < n) : cast_succ (cast_lt i h) = i := fin.eq_of_veq rfl @[simp] lemma cast_lt_cast_succ {n : ℕ} (a : fin n) (h : a.1 < n) : cast_lt (cast_succ a) h = a := by cases a; refl @[simp] lemma sub_nat_val (i : fin (n + m)) (h : m ≤ i.val) : (i.sub_nat m h).val = i.val - m := rfl @[simp] lemma add_nat_val (i : fin (n + m)) (h : m ≤ i.val) : (i.add_nat m).val = i.val + m := rfl @[simp] lemma cast_succ_inj {a b : fin n} : a.cast_succ = b.cast_succ ↔ a = b := by simp [eq_iff_veq] def clamp (n m : ℕ) : fin (m + 1) := fin.of_nat $ min n m @[simp] lemma clamp_val (n m : ℕ) : (clamp n m).val = min n m := nat.mod_eq_of_lt $ nat.lt_succ_iff.mpr $ min_le_right _ _ lemma injective_cast_le {n₁ n₂ : ℕ} (h : n₁ ≤ n₂) : injective (fin.cast_le h) | ⟨i₁, h₁⟩ ⟨i₂, h₂⟩ eq := fin.eq_of_veq $ show i₁ = i₂, from fin.veq_of_eq eq theorem succ_above_ne (p : fin (n+1)) (i : fin n) : p.succ_above i ≠ p := begin assume eq, unfold fin.succ_above at eq, split_ifs at eq with h; simpa [lt_irrefl, nat.lt_succ_self, eq.symm] using h end @[simp] lemma succ_above_descend : ∀(p i : fin (n+1)) (h : i ≠ p), p.succ_above (p.pred_above i h) = i | ⟨p, hp⟩ ⟨0, hi⟩ h := fin.eq_of_veq $ by simp [succ_above, pred_above]; split_ifs; simp * at * | ⟨p, hp⟩ ⟨i+1, hi⟩ h := fin.eq_of_veq begin have : i + 1 ≠ p, by rwa [(≠), fin.ext_iff] at h, unfold succ_above pred_above, split_ifs with h1 h2; simp only [fin.cast_succ_cast_lt, add_right_inj, pred_val, ne.def, cast_succ_val, nat.pred_succ, fin.succ_pred, add_right_inj] at *, exact (this (le_antisymm h2 (le_of_not_gt h1))).elim end @[simp] lemma pred_above_succ_above (p : fin (n+1)) (i : fin n) (h : p.succ_above i ≠ p) : p.pred_above (p.succ_above i) h = i := begin unfold fin.succ_above, apply eq_of_veq, split_ifs with h₀, { simp [pred_above, h₀, lt_iff_val_lt_val], }, { unfold pred_above, split_ifs with h₁, { exfalso, rw [lt_iff_val_lt_val, succ_val] at h₁, exact h₀ (lt_trans (nat.lt_succ_self _) h₁) }, { rw [pred_succ] } } end section rec @[elab_as_eliminator] def succ_rec {C : ∀ n, fin n → Sort*} (H0 : ∀ n, C (succ n) 0) (Hs : ∀ n i, C n i → C (succ n) i.succ) : ∀ {n : ℕ} (i : fin n), C n i | 0 i := i.elim0 | (succ n) ⟨0, _⟩ := H0 _ | (succ n) ⟨succ i, h⟩ := Hs _ _ (succ_rec ⟨i, lt_of_succ_lt_succ h⟩) @[elab_as_eliminator] def succ_rec_on {n : ℕ} (i : fin n) {C : ∀ n, fin n → Sort*} (H0 : ∀ n, C (succ n) 0) (Hs : ∀ n i, C n i → C (succ n) i.succ) : C n i := i.succ_rec H0 Hs @[simp] theorem succ_rec_on_zero {C : ∀ n, fin n → Sort*} {H0 Hs} (n) : @fin.succ_rec_on (succ n) 0 C H0 Hs = H0 n := rfl @[simp] theorem succ_rec_on_succ {C : ∀ n, fin n → Sort*} {H0 Hs} {n} (i : fin n) : @fin.succ_rec_on (succ n) i.succ C H0 Hs = Hs n i (fin.succ_rec_on i H0 Hs) := by cases i; refl @[elab_as_eliminator] def cases {C : fin (succ n) → Sort*} (H0 : C 0) (Hs : ∀ i : fin n, C (i.succ)) : ∀ (i : fin (succ n)), C i | ⟨0, h⟩ := H0 | ⟨succ i, h⟩ := Hs ⟨i, lt_of_succ_lt_succ h⟩ @[simp] theorem cases_zero {n} {C : fin (succ n) → Sort*} {H0 Hs} : @fin.cases n C H0 Hs 0 = H0 := rfl @[simp] theorem cases_succ {n} {C : fin (succ n) → Sort*} {H0 Hs} (i : fin n) : @fin.cases n C H0 Hs i.succ = Hs i := by cases i; refl lemma forall_fin_succ {P : fin (n+1) → Prop} : (∀ i, P i) ↔ P 0 ∧ (∀ i:fin n, P i.succ) := ⟨λ H, ⟨H 0, λ i, H _⟩, λ ⟨H0, H1⟩ i, fin.cases H0 H1 i⟩ lemma exists_fin_succ {P : fin (n+1) → Prop} : (∃ i, P i) ↔ P 0 ∨ (∃i:fin n, P i.succ) := ⟨λ ⟨i, h⟩, fin.cases or.inl (λ i hi, or.inr ⟨i, hi⟩) i h, λ h, or.elim h (λ h, ⟨0, h⟩) $ λ⟨i, hi⟩, ⟨i.succ, hi⟩⟩ end rec section tuple /- We can think of the type `fin n → α` as `n`-tuples in `α`. Here are some relevant operations. -/ def tail {α} (p : fin (n+1) → α) : fin n → α := λ i, p i.succ def cons {α} (x : α) (v : fin n → α) : fin (n+1) → α := λ j, fin.cases x v j @[simp] lemma tail_cons {α} (x : α) (p : fin n → α) : tail (cons x p) = p := by simp [tail, cons] @[simp] lemma cons_succ {α} (x : α) (p : fin n → α) (i : fin n) : cons x p i.succ = p i := by simp [cons] @[simp] lemma cons_zero {α} (x : α) (p : fin n → α) : cons x p 0 = x := by simp [cons] end tuple section find def find : Π {n : ℕ} (p : fin n → Prop) [decidable_pred p], option (fin n) | 0 p _ := none | (n+1) p _ := by resetI; exact option.cases_on (@find n (λ i, p (i.cast_lt (nat.lt_succ_of_lt i.2))) _) (if h : p (fin.last n) then some (fin.last n) else none) (λ i, some (i.cast_lt (nat.lt_succ_of_lt i.2))) lemma find_spec : Π {n : ℕ} (p : fin n → Prop) [decidable_pred p] {i : fin n} (hi : i ∈ by exactI fin.find p), p i | 0 p I i hi := option.no_confusion hi | (n+1) p I i hi := begin dsimp [find] at hi, resetI, cases h : find (λ i : fin n, (p (i.cast_lt (nat.lt_succ_of_lt i.2)))) with j, { rw h at hi, dsimp at hi, split_ifs at hi with hl hl, { exact option.some_inj.1 hi ▸ hl }, { exact option.no_confusion hi } }, { rw h at hi, rw [← option.some_inj.1 hi], exact find_spec _ h } end lemma is_some_find_iff : Π {n : ℕ} {p : fin n → Prop} [decidable_pred p], by exactI (find p).is_some ↔ ∃ i, p i | 0 p _ := iff_of_false (λ h, bool.no_confusion h) (λ ⟨i, _⟩, fin.elim0 i) | (n+1) p _ := ⟨λ h, begin resetI, rw [option.is_some_iff_exists] at h, cases h with i hi, exact ⟨i, find_spec _ hi⟩ end, λ ⟨⟨i, hin⟩, hi⟩, begin resetI, dsimp [find], cases h : find (λ i : fin n, (p (i.cast_lt (nat.lt_succ_of_lt i.2)))) with j, { split_ifs with hl hl, { exact option.is_some_some }, { have := (@is_some_find_iff n (λ x, p (x.cast_lt (nat.lt_succ_of_lt x.2))) _).2 ⟨⟨i, lt_of_le_of_ne (nat.le_of_lt_succ hin) (λ h, by clear_aux_decl; subst h; exact hl hi)⟩, hi⟩, rw h at this, exact this } }, { simp } end⟩ lemma find_eq_none_iff {n : ℕ} {p : fin n → Prop} [decidable_pred p] : find p = none ↔ ∀ i, ¬ p i := by rw [← not_exists, ← is_some_find_iff]; cases (find p); simp lemma find_min : Π {n : ℕ} {p : fin n → Prop} [decidable_pred p] {i : fin n} (hi : i ∈ by exactI fin.find p) {j : fin n} (hj : j < i), ¬ p j | 0 p _ i hi j hj hpj := option.no_confusion hi | (n+1) p _ i hi ⟨j, hjn⟩ hj hpj := begin resetI, dsimp [find] at hi, cases h : find (λ i : fin n, (p (i.cast_lt (nat.lt_succ_of_lt i.2)))) with k, { rw [h] at hi, split_ifs at hi with hl hl, { have := option.some_inj.1 hi, subst this, rw [find_eq_none_iff] at h, exact h ⟨j, hj⟩ hpj }, { exact option.no_confusion hi } }, { rw h at hi, dsimp at hi, have := option.some_inj.1 hi, subst this, exact find_min h (show (⟨j, lt_trans hj k.2⟩ : fin n) < k, from hj) hpj } end lemma find_min' {p : fin n → Prop} [decidable_pred p] {i : fin n} (h : i ∈ fin.find p) {j : fin n} (hj : p j) : i ≤ j := le_of_not_gt (λ hij, find_min h hij hj) lemma nat_find_mem_find {p : fin n → Prop} [decidable_pred p] (h : ∃ i, ∃ hin : i < n, p ⟨i, hin⟩) : (⟨nat.find h, (nat.find_spec h).fst⟩ : fin n) ∈ find p := let ⟨i, hin, hi⟩ := h in begin cases hf : find p with f, { rw [find_eq_none_iff] at hf, exact (hf ⟨i, hin⟩ hi).elim }, { refine option.some_inj.2 (le_antisymm _ _), { exact find_min' hf (nat.find_spec h).snd }, { exact nat.find_min' _ ⟨f.2, by convert find_spec p hf; exact fin.eta _ _⟩ } } end lemma mem_find_iff {p : fin n → Prop} [decidable_pred p] {i : fin n} : i ∈ fin.find p ↔ p i ∧ ∀ j, p j → i ≤ j := ⟨λ hi, ⟨find_spec _ hi, λ _, find_min' hi⟩, begin rintros ⟨hpi, hj⟩, cases hfp : fin.find p, { rw [find_eq_none_iff] at hfp, exact (hfp _ hpi).elim }, { exact option.some_inj.2 (le_antisymm (find_min' hfp hpi) (hj _ (find_spec _ hfp))) } end⟩ lemma find_eq_some_iff {p : fin n → Prop} [decidable_pred p] {i : fin n} : fin.find p = some i ↔ p i ∧ ∀ j, p j → i ≤ j := mem_find_iff lemma mem_find_of_unique {p : fin n → Prop} [decidable_pred p] (h : ∀ i j, p i → p j → i = j) {i : fin n} (hi : p i) : i ∈ fin.find p := mem_find_iff.2 ⟨hi, λ j hj, le_of_eq $ h i j hi hj⟩ end find end fin
de4fabf0ab6a09aa8a23dba95414fa1cd7c32169
bb31430994044506fa42fd667e2d556327e18dfe
/src/data/finset/pointwise.lean
b73271640aefe84fb397993eb3d5b7ace6fbd59b
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
49,504
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, Yaël Dillies -/ import data.finset.n_ary import data.finset.preimage import data.set.pointwise.smul import data.set.pointwise.list_of_fn /-! # Pointwise operations of finsets This file defines pointwise algebraic operations on finsets. ## Main declarations For finsets `s` and `t`: * `0` (`finset.has_zero`): The singleton `{0}`. * `1` (`finset.has_one`): The singleton `{1}`. * `-s` (`finset.has_neg`): Negation, finset of all `-x` where `x ∈ s`. * `s⁻¹` (`finset.has_inv`): Inversion, finset of all `x⁻¹` where `x ∈ s`. * `s + t` (`finset.has_add`): Addition, finset of all `x + y` where `x ∈ s` and `y ∈ t`. * `s * t` (`finset.has_mul`): Multiplication, finset of all `x * y` where `x ∈ s` and `y ∈ t`. * `s - t` (`finset.has_sub`): Subtraction, finset of all `x - y` where `x ∈ s` and `y ∈ t`. * `s / t` (`finset.has_div`): Division, finset of all `x / y` where `x ∈ s` and `y ∈ t`. * `s +ᵥ t` (`finset.has_vadd`): Scalar addition, finset of all `x +ᵥ y` where `x ∈ s` and `y ∈ t`. * `s • t` (`finset.has_smul`): Scalar multiplication, finset of all `x • y` where `x ∈ s` and `y ∈ t`. * `s -ᵥ t` (`finset.has_vsub`): Scalar subtraction, finset of all `x -ᵥ y` where `x ∈ s` and `y ∈ t`. * `a • s` (`finset.has_smul_finset`): Scaling, finset of all `a • x` where `x ∈ s`. * `a +ᵥ s` (`finset.has_vadd_finset`): Translation, finset of all `a +ᵥ x` where `x ∈ s`. For `α` a semigroup/monoid, `finset α` is a semigroup/monoid. As an unfortunate side effect, this means that `n • s`, where `n : ℕ`, is ambiguous between pointwise scaling and repeated pointwise addition; the former has `(2 : ℕ) • {1, 2} = {2, 4}`, while the latter has `(2 : ℕ) • {1, 2} = {2, 3, 4}`. See note [pointwise nat action]. ## Implementation notes We put all instances in the locale `pointwise`, so that these instances are not available by default. Note that we do not mark them as reducible (as argued by note [reducible non-instances]) since we expect the locale to be open whenever the instances are actually used (and making the instances reducible changes the behavior of `simp`. ## Tags finset multiplication, finset addition, pointwise addition, pointwise multiplication, pointwise subtraction -/ open function open_locale big_operators pointwise variables {F α β γ : Type*} namespace finset /-! ### `0`/`1` as finsets -/ section has_one variables [has_one α] {s : finset α} {a : α} /-- The finset `1 : finset α` is defined as `{1}` in locale `pointwise`. -/ @[to_additive "The finset `0 : finset α` is defined as `{0}` in locale `pointwise`."] protected def has_one : has_one (finset α) := ⟨{1}⟩ localized "attribute [instance] finset.has_one finset.has_zero" in pointwise @[simp, to_additive] lemma mem_one : a ∈ (1 : finset α) ↔ a = 1 := mem_singleton @[simp, norm_cast, to_additive] lemma coe_one : ↑(1 : finset α) = (1 : set α) := coe_singleton 1 @[simp, to_additive] lemma one_subset : (1 : finset α) ⊆ s ↔ (1 : α) ∈ s := singleton_subset_iff @[to_additive] lemma singleton_one : ({1} : finset α) = 1 := rfl @[to_additive] lemma one_mem_one : (1 : α) ∈ (1 : finset α) := mem_singleton_self _ @[to_additive] lemma one_nonempty : (1 : finset α).nonempty := ⟨1, one_mem_one⟩ @[simp, to_additive] protected lemma map_one {f : α ↪ β} : map f 1 = {f 1} := map_singleton f 1 @[simp, to_additive] lemma image_one [decidable_eq β] {f : α → β} : image f 1 = {f 1} := image_singleton _ _ @[to_additive] lemma subset_one_iff_eq : s ⊆ 1 ↔ s = ∅ ∨ s = 1 := subset_singleton_iff @[to_additive] lemma nonempty.subset_one_iff (h : s.nonempty) : s ⊆ 1 ↔ s = 1 := h.subset_singleton_iff /-- The singleton operation as a `one_hom`. -/ @[to_additive "The singleton operation as a `zero_hom`."] def singleton_one_hom : one_hom α (finset α) := ⟨singleton, singleton_one⟩ @[simp, to_additive] lemma coe_singleton_one_hom : (singleton_one_hom : α → finset α) = singleton := rfl @[simp, to_additive] lemma singleton_one_hom_apply (a : α) : singleton_one_hom a = {a} := rfl /-- Lift a `one_hom` to `finset` via `image`. -/ @[to_additive "Lift a `zero_hom` to `finset` via `image`", simps] def image_one_hom [decidable_eq β] [has_one β] [one_hom_class F α β] (f : F) : one_hom (finset α) (finset β) := { to_fun := finset.image f, map_one' := by rw [image_one, map_one, singleton_one] } end has_one /-! ### Finset negation/inversion -/ section has_inv variables [decidable_eq α] [has_inv α] {s s₁ s₂ t t₁ t₂ u : finset α} {a b : α} /-- The pointwise inversion of finset `s⁻¹` is defined as `{x⁻¹ | x ∈ s}` in locale `pointwise`. -/ @[to_additive "The pointwise negation of finset `-s` is defined as `{-x | x ∈ s}` in locale `pointwise`."] protected def has_inv : has_inv (finset α) := ⟨image has_inv.inv⟩ localized "attribute [instance] finset.has_inv finset.has_neg" in pointwise @[to_additive] lemma inv_def : s⁻¹ = s.image (λ x, x⁻¹) := rfl @[to_additive] lemma image_inv : s.image (λ x, x⁻¹) = s⁻¹ := rfl @[to_additive] lemma mem_inv {x : α} : x ∈ s⁻¹ ↔ ∃ y ∈ s, y⁻¹ = x := mem_image @[to_additive] lemma inv_mem_inv (ha : a ∈ s) : a⁻¹ ∈ s⁻¹ := mem_image_of_mem _ ha @[to_additive] lemma card_inv_le : s⁻¹.card ≤ s.card := card_image_le @[simp, to_additive] lemma inv_empty : (∅ : finset α)⁻¹ = ∅ := image_empty _ @[simp, to_additive] lemma inv_nonempty_iff : s⁻¹.nonempty ↔ s.nonempty := nonempty.image_iff _ alias inv_nonempty_iff ↔ nonempty.inv nonempty.of_inv @[to_additive, mono] lemma inv_subset_inv (h : s ⊆ t) : s⁻¹ ⊆ t⁻¹ := image_subset_image h attribute [mono] neg_subset_neg @[simp, to_additive] lemma inv_singleton (a : α) : ({a} : finset α)⁻¹ = {a⁻¹} := image_singleton _ _ @[simp, to_additive] lemma inv_insert (a : α) (s : finset α) : (insert a s)⁻¹ = insert a⁻¹ s⁻¹ := image_insert _ _ _ end has_inv open_locale pointwise section has_involutive_inv variables [decidable_eq α] [has_involutive_inv α] (s : finset α) @[simp, norm_cast, to_additive] lemma coe_inv : ↑(s⁻¹) = (s : set α)⁻¹ := coe_image.trans set.image_inv @[simp, to_additive] lemma card_inv : s⁻¹.card = s.card := card_image_of_injective _ inv_injective @[simp, to_additive] lemma preimage_inv : s.preimage has_inv.inv (inv_injective.inj_on _) = s⁻¹ := coe_injective $ by rw [coe_preimage, set.inv_preimage, coe_inv] end has_involutive_inv /-! ### Finset addition/multiplication -/ section has_mul variables [decidable_eq α] [decidable_eq β] [has_mul α] [has_mul β] [mul_hom_class F α β] (f : F) {s s₁ s₂ t t₁ t₂ u : finset α} {a b : α} /-- The pointwise multiplication of finsets `s * t` and `t` is defined as `{x * y | x ∈ s, y ∈ t}` in locale `pointwise`. -/ @[to_additive "The pointwise addition of finsets `s + t` is defined as `{x + y | x ∈ s, y ∈ t}` in locale `pointwise`."] protected def has_mul : has_mul (finset α) := ⟨image₂ (*)⟩ localized "attribute [instance] finset.has_mul finset.has_add" in pointwise @[to_additive] lemma mul_def : s * t = (s ×ˢ t).image (λ p : α × α, p.1 * p.2) := rfl @[to_additive] lemma image_mul_product : (s ×ˢ t).image (λ x : α × α, x.fst * x.snd) = s * t := rfl @[to_additive] lemma mem_mul {x : α} : x ∈ s * t ↔ ∃ y z, y ∈ s ∧ z ∈ t ∧ y * z = x := mem_image₂ @[simp, norm_cast, to_additive] lemma coe_mul (s t : finset α) : (↑(s * t) : set α) = ↑s * ↑t := coe_image₂ _ _ _ @[to_additive] lemma mul_mem_mul : a ∈ s → b ∈ t → a * b ∈ s * t := mem_image₂_of_mem @[to_additive] lemma card_mul_le : (s * t).card ≤ s.card * t.card := card_image₂_le _ _ _ @[to_additive] lemma card_mul_iff : (s * t).card = s.card * t.card ↔ (s ×ˢ t : set (α × α)).inj_on (λ p, p.1 * p.2) := card_image₂_iff @[simp, to_additive] lemma empty_mul (s : finset α) : ∅ * s = ∅ := image₂_empty_left @[simp, to_additive] lemma mul_empty (s : finset α) : s * ∅ = ∅ := image₂_empty_right @[simp, to_additive] lemma mul_eq_empty : s * t = ∅ ↔ s = ∅ ∨ t = ∅ := image₂_eq_empty_iff @[simp, to_additive] lemma mul_nonempty : (s * t).nonempty ↔ s.nonempty ∧ t.nonempty := image₂_nonempty_iff @[to_additive] lemma nonempty.mul : s.nonempty → t.nonempty → (s * t).nonempty := nonempty.image₂ @[to_additive] lemma nonempty.of_mul_left : (s * t).nonempty → s.nonempty := nonempty.of_image₂_left @[to_additive] lemma nonempty.of_mul_right : (s * t).nonempty → t.nonempty := nonempty.of_image₂_right @[to_additive] lemma mul_singleton (a : α) : s * {a} = s.image (* a) := image₂_singleton_right @[to_additive] lemma singleton_mul (a : α) : {a} * s = s.image ((*) a) := image₂_singleton_left @[simp, to_additive] lemma singleton_mul_singleton (a b : α) : ({a} : finset α) * {b} = {a * b} := image₂_singleton @[to_additive, mono] lemma mul_subset_mul : s₁ ⊆ s₂ → t₁ ⊆ t₂ → s₁ * t₁ ⊆ s₂ * t₂ := image₂_subset @[to_additive] lemma mul_subset_mul_left : t₁ ⊆ t₂ → s * t₁ ⊆ s * t₂ := image₂_subset_left @[to_additive] lemma mul_subset_mul_right : s₁ ⊆ s₂ → s₁ * t ⊆ s₂ * t := image₂_subset_right @[to_additive] lemma mul_subset_iff : s * t ⊆ u ↔ ∀ (x ∈ s) (y ∈ t), x * y ∈ u := image₂_subset_iff attribute [mono] add_subset_add @[to_additive] lemma union_mul : (s₁ ∪ s₂) * t = s₁ * t ∪ s₂ * t := image₂_union_left @[to_additive] lemma mul_union : s * (t₁ ∪ t₂) = s * t₁ ∪ s * t₂ := image₂_union_right @[to_additive] lemma inter_mul_subset : (s₁ ∩ s₂) * t ⊆ s₁ * t ∩ (s₂ * t) := image₂_inter_subset_left @[to_additive] lemma mul_inter_subset : s * (t₁ ∩ t₂) ⊆ s * t₁ ∩ (s * t₂) := image₂_inter_subset_right /-- If a finset `u` is contained in the product of two sets `s * t`, we can find two finsets `s'`, `t'` such that `s' ⊆ s`, `t' ⊆ t` and `u ⊆ s' * t'`. -/ @[to_additive "If a finset `u` is contained in the sum of two sets `s + t`, we can find two finsets `s'`, `t'` such that `s' ⊆ s`, `t' ⊆ t` and `u ⊆ s' + t'`."] lemma subset_mul {s t : set α} : ↑u ⊆ s * t → ∃ s' t' : finset α, ↑s' ⊆ s ∧ ↑t' ⊆ t ∧ u ⊆ s' * t' := subset_image₂ @[to_additive] lemma image_mul : (s * t).image (f : α → β) = s.image f * t.image f := image_image₂_distrib $ map_mul f /-- The singleton operation as a `mul_hom`. -/ @[to_additive "The singleton operation as an `add_hom`."] def singleton_mul_hom : α →ₙ* finset α := ⟨singleton, λ a b, (singleton_mul_singleton _ _).symm⟩ @[simp, to_additive] lemma coe_singleton_mul_hom : (singleton_mul_hom : α → finset α) = singleton := rfl @[simp, to_additive] lemma singleton_mul_hom_apply (a : α) : singleton_mul_hom a = {a} := rfl /-- Lift a `mul_hom` to `finset` via `image`. -/ @[to_additive "Lift an `add_hom` to `finset` via `image`", simps] def image_mul_hom : finset α →ₙ* finset β := { to_fun := finset.image f, map_mul' := λ s t, image_mul _ } end has_mul /-! ### Finset subtraction/division -/ section has_div variables [decidable_eq α] [has_div α] {s s₁ s₂ t t₁ t₂ u : finset α} {a b : α} /-- The pointwise division of sfinets `s / t` is defined as `{x / y | x ∈ s, y ∈ t}` in locale `pointwise`. -/ @[to_additive "The pointwise subtraction of finsets `s - t` is defined as `{x - y | x ∈ s, y ∈ t}` in locale `pointwise`."] protected def has_div : has_div (finset α) := ⟨image₂ (/)⟩ localized "attribute [instance] finset.has_div finset.has_sub" in pointwise @[to_additive] lemma div_def : s / t = (s ×ˢ t).image (λ p : α × α, p.1 / p.2) := rfl @[to_additive add_image_prod] lemma image_div_prod : (s ×ˢ t).image (λ x : α × α, x.fst / x.snd) = s / t := rfl @[to_additive] lemma mem_div : a ∈ s / t ↔ ∃ b c, b ∈ s ∧ c ∈ t ∧ b / c = a := mem_image₂ @[simp, norm_cast, to_additive] lemma coe_div (s t : finset α) : (↑(s / t) : set α) = ↑s / ↑t := coe_image₂ _ _ _ @[to_additive] lemma div_mem_div : a ∈ s → b ∈ t → a / b ∈ s / t := mem_image₂_of_mem @[to_additive] lemma div_card_le : (s / t).card ≤ s.card * t.card := card_image₂_le _ _ _ @[simp, to_additive] lemma empty_div (s : finset α) : ∅ / s = ∅ := image₂_empty_left @[simp, to_additive] lemma div_empty (s : finset α) : s / ∅ = ∅ := image₂_empty_right @[simp, to_additive] lemma div_eq_empty : s / t = ∅ ↔ s = ∅ ∨ t = ∅ := image₂_eq_empty_iff @[simp, to_additive] lemma div_nonempty : (s / t).nonempty ↔ s.nonempty ∧ t.nonempty := image₂_nonempty_iff @[to_additive] lemma nonempty.div : s.nonempty → t.nonempty → (s / t).nonempty := nonempty.image₂ @[to_additive] lemma nonempty.of_div_left : (s / t).nonempty → s.nonempty := nonempty.of_image₂_left @[to_additive] lemma nonempty.of_div_right : (s / t).nonempty → t.nonempty := nonempty.of_image₂_right @[simp, to_additive] lemma div_singleton (a : α) : s / {a} = s.image (/ a) := image₂_singleton_right @[simp, to_additive] lemma singleton_div (a : α) : {a} / s = s.image ((/) a) := image₂_singleton_left @[simp, to_additive] lemma singleton_div_singleton (a b : α) : ({a} : finset α) / {b} = {a / b} := image₂_singleton @[to_additive, mono] lemma div_subset_div : s₁ ⊆ s₂ → t₁ ⊆ t₂ → s₁ / t₁ ⊆ s₂ / t₂ := image₂_subset @[to_additive] lemma div_subset_div_left : t₁ ⊆ t₂ → s / t₁ ⊆ s / t₂ := image₂_subset_left @[to_additive] lemma div_subset_div_right : s₁ ⊆ s₂ → s₁ / t ⊆ s₂ / t := image₂_subset_right @[to_additive] lemma div_subset_iff : s / t ⊆ u ↔ ∀ (x ∈ s) (y ∈ t), x / y ∈ u := image₂_subset_iff attribute [mono] sub_subset_sub @[to_additive] lemma union_div : (s₁ ∪ s₂) / t = s₁ / t ∪ s₂ / t := image₂_union_left @[to_additive] lemma div_union : s / (t₁ ∪ t₂) = s / t₁ ∪ s / t₂ := image₂_union_right @[to_additive] lemma inter_div_subset : (s₁ ∩ s₂) / t ⊆ s₁ / t ∩ (s₂ / t) := image₂_inter_subset_left @[to_additive] lemma div_inter_subset : s / (t₁ ∩ t₂) ⊆ s / t₁ ∩ (s / t₂) := image₂_inter_subset_right /-- If a finset `u` is contained in the product of two sets `s / t`, we can find two finsets `s'`, `t'` such that `s' ⊆ s`, `t' ⊆ t` and `u ⊆ s' / t'`. -/ @[to_additive "If a finset `u` is contained in the sum of two sets `s - t`, we can find two finsets `s'`, `t'` such that `s' ⊆ s`, `t' ⊆ t` and `u ⊆ s' - t'`."] lemma subset_div {s t : set α} : ↑u ⊆ s / t → ∃ s' t' : finset α, ↑s' ⊆ s ∧ ↑t' ⊆ t ∧ u ⊆ s' / t' := subset_image₂ end has_div /-! ### Instances -/ open_locale pointwise section instances variables [decidable_eq α] [decidable_eq β] /-- Repeated pointwise addition (not the same as pointwise repeated addition!) of a `finset`. See note [pointwise nat action]. -/ protected def has_nsmul [has_zero α] [has_add α] : has_smul ℕ (finset α) := ⟨nsmul_rec⟩ /-- Repeated pointwise multiplication (not the same as pointwise repeated multiplication!) of a `finset`. See note [pointwise nat action]. -/ @[to_additive] protected def has_npow [has_one α] [has_mul α] : has_pow (finset α) ℕ := ⟨λ s n, npow_rec n s⟩ /-- Repeated pointwise addition/subtraction (not the same as pointwise repeated addition/subtraction!) of a `finset`. See note [pointwise nat action]. -/ protected def has_zsmul [has_zero α] [has_add α] [has_neg α] : has_smul ℤ (finset α) := ⟨zsmul_rec⟩ /-- Repeated pointwise multiplication/division (not the same as pointwise repeated multiplication/division!) of a `finset`. See note [pointwise nat action]. -/ @[to_additive] protected def has_zpow [has_one α] [has_mul α] [has_inv α] : has_pow (finset α) ℤ := ⟨λ s n, zpow_rec n s⟩ localized "attribute [instance] finset.has_nsmul finset.has_npow finset.has_zsmul finset.has_zpow" in pointwise /-- `finset α` is a `semigroup` under pointwise operations if `α` is. -/ @[to_additive "`finset α` is an `add_semigroup` under pointwise operations if `α` is. "] protected def semigroup [semigroup α] : semigroup (finset α) := coe_injective.semigroup _ coe_mul /-- `finset α` is a `comm_semigroup` under pointwise operations if `α` is. -/ @[to_additive "`finset α` is an `add_comm_semigroup` under pointwise operations if `α` is. "] protected def comm_semigroup [comm_semigroup α] : comm_semigroup (finset α) := coe_injective.comm_semigroup _ coe_mul section mul_one_class variables [mul_one_class α] /-- `finset α` is a `mul_one_class` under pointwise operations if `α` is. -/ @[to_additive "`finset α` is an `add_zero_class` under pointwise operations if `α` is."] protected def mul_one_class : mul_one_class (finset α) := coe_injective.mul_one_class _ (coe_singleton 1) coe_mul localized "attribute [instance] finset.semigroup finset.add_semigroup finset.comm_semigroup finset.add_comm_semigroup finset.mul_one_class finset.add_zero_class" in pointwise @[to_additive] lemma subset_mul_left (s : finset α) {t : finset α} (ht : (1 : α) ∈ t) : s ⊆ s * t := λ a ha, mem_mul.2 ⟨a, 1, ha, ht, mul_one _⟩ @[to_additive] lemma subset_mul_right {s : finset α} (t : finset α) (hs : (1 : α) ∈ s) : t ⊆ s * t := λ a ha, mem_mul.2 ⟨1, a, hs, ha, one_mul _⟩ /-- The singleton operation as a `monoid_hom`. -/ @[to_additive "The singleton operation as an `add_monoid_hom`."] def singleton_monoid_hom : α →* finset α := { ..singleton_mul_hom, ..singleton_one_hom } @[simp, to_additive] lemma coe_singleton_monoid_hom : (singleton_monoid_hom : α → finset α) = singleton := rfl @[simp, to_additive] lemma singleton_monoid_hom_apply (a : α) : singleton_monoid_hom a = {a} := rfl /-- The coercion from `finset` to `set` as a `monoid_hom`. -/ @[to_additive "The coercion from `finset` to `set` as an `add_monoid_hom`."] def coe_monoid_hom : finset α →* set α := { to_fun := coe, map_one' := coe_one, map_mul' := coe_mul } @[simp, to_additive] lemma coe_coe_monoid_hom : (coe_monoid_hom : finset α → set α) = coe := rfl @[simp, to_additive] lemma coe_monoid_hom_apply (s : finset α) : coe_monoid_hom s = s := rfl /-- Lift a `monoid_hom` to `finset` via `image`. -/ @[to_additive "Lift an `add_monoid_hom` to `finset` via `image`", simps] def image_monoid_hom [mul_one_class β] [monoid_hom_class F α β] (f : F) : finset α →* finset β := { ..image_mul_hom f, ..image_one_hom f } end mul_one_class section monoid variables [monoid α] {s t : finset α} {a : α} {m n : ℕ} @[simp, norm_cast, to_additive] lemma coe_pow (s : finset α) (n : ℕ) : ↑(s ^ n) = (s ^ n : set α) := begin change ↑(npow_rec n s) = _, induction n with n ih, { rw [npow_rec, pow_zero, coe_one] }, { rw [npow_rec, pow_succ, coe_mul, ih] } end /-- `finset α` is a `monoid` under pointwise operations if `α` is. -/ @[to_additive "`finset α` is an `add_monoid` under pointwise operations if `α` is. "] protected def monoid : monoid (finset α) := coe_injective.monoid _ coe_one coe_mul coe_pow localized "attribute [instance] finset.monoid finset.add_monoid" in pointwise @[to_additive] lemma pow_mem_pow (ha : a ∈ s) : ∀ n : ℕ, a ^ n ∈ s ^ n | 0 := by { rw pow_zero, exact one_mem_one } | (n + 1) := by { rw pow_succ, exact mul_mem_mul ha (pow_mem_pow _) } @[to_additive] lemma pow_subset_pow (hst : s ⊆ t) : ∀ n : ℕ, s ^ n ⊆ t ^ n | 0 := by { rw pow_zero, exact subset.rfl } | (n + 1) := by { rw pow_succ, exact mul_subset_mul hst (pow_subset_pow _) } @[to_additive] lemma pow_subset_pow_of_one_mem (hs : (1 : α) ∈ s) : m ≤ n → s ^ m ⊆ s ^ n := begin refine nat.le_induction _ (λ n h ih, _) _, { exact subset.rfl }, { rw pow_succ, exact ih.trans (subset_mul_right _ hs) } end @[simp, norm_cast, to_additive] lemma coe_list_prod (s : list (finset α)) : (↑s.prod : set α) = (s.map coe).prod := map_list_prod (coe_monoid_hom : finset α →* set α) _ @[to_additive] lemma mem_prod_list_of_fn {a : α} {s : fin n → finset α} : a ∈ (list.of_fn s).prod ↔ ∃ f : (Π i : fin n, s i), (list.of_fn (λ i, (f i : α))).prod = a := by { rw [←mem_coe, coe_list_prod, list.map_of_fn, set.mem_prod_list_of_fn], refl } @[to_additive] lemma mem_pow {a : α} {n : ℕ} : a ∈ s ^ n ↔ ∃ f : fin n → s, (list.of_fn (λ i, ↑(f i))).prod = a := by { simp_rw [←mem_coe, coe_pow, set.mem_pow], refl } @[simp, to_additive] lemma empty_pow (hn : n ≠ 0) : (∅ : finset α) ^ n = ∅ := by rw [←tsub_add_cancel_of_le (nat.succ_le_of_lt $ nat.pos_of_ne_zero hn), pow_succ, empty_mul] @[to_additive] lemma mul_univ_of_one_mem [fintype α] (hs : (1 : α) ∈ s) : s * univ = univ := eq_univ_iff_forall.2 $ λ a, mem_mul.2 ⟨_, _, hs, mem_univ _, one_mul _⟩ @[to_additive] lemma univ_mul_of_one_mem [fintype α] (ht : (1 : α) ∈ t) : univ * t = univ := eq_univ_iff_forall.2 $ λ a, mem_mul.2 ⟨_, _, mem_univ _, ht, mul_one _⟩ @[simp, to_additive] lemma univ_mul_univ [fintype α] : (univ : finset α) * univ = univ := mul_univ_of_one_mem $ mem_univ _ @[simp, to_additive nsmul_univ] lemma univ_pow [fintype α] (hn : n ≠ 0) : (univ : finset α) ^ n = univ := coe_injective $ by rw [coe_pow, coe_univ, set.univ_pow hn] @[to_additive] protected lemma _root_.is_unit.finset : is_unit a → is_unit ({a} : finset α) := is_unit.map (singleton_monoid_hom : α →* finset α) end monoid section comm_monoid variables [comm_monoid α] /-- `finset α` is a `comm_monoid` under pointwise operations if `α` is. -/ @[to_additive "`finset α` is an `add_comm_monoid` under pointwise operations if `α` is. "] protected def comm_monoid : comm_monoid (finset α) := coe_injective.comm_monoid _ coe_one coe_mul coe_pow localized "attribute [instance] finset.comm_monoid finset.add_comm_monoid" in pointwise @[simp, norm_cast, to_additive] lemma coe_prod {ι : Type*} (s : finset ι) (f : ι → finset α) : (↑(∏ i in s, f i) : set α) = ∏ i in s, f i := map_prod (coe_monoid_hom : finset α →* set α) _ _ end comm_monoid open_locale pointwise section division_monoid variables [division_monoid α] {s t : finset α} @[simp, to_additive] lemma coe_zpow (s : finset α) : ∀ n : ℤ, ↑(s ^ n) = (s ^ n : set α) | (int.of_nat n) := coe_pow _ _ | (int.neg_succ_of_nat n) := by { refine (coe_inv _).trans _, convert congr_arg has_inv.inv (coe_pow _ _) } @[to_additive] protected lemma mul_eq_one_iff : s * t = 1 ↔ ∃ a b, s = {a} ∧ t = {b} ∧ a * b = 1 := by simp_rw [←coe_inj, coe_mul, coe_one, set.mul_eq_one_iff, coe_singleton] /-- `finset α` is a division monoid under pointwise operations if `α` is. -/ @[to_additive "`finset α` is a subtraction monoid under pointwise operations if `α` is."] protected def division_monoid : division_monoid (finset α) := coe_injective.division_monoid _ coe_one coe_mul coe_inv coe_div coe_pow coe_zpow @[simp, to_additive] lemma is_unit_iff : is_unit s ↔ ∃ a, s = {a} ∧ is_unit a := begin split, { rintro ⟨u, rfl⟩, obtain ⟨a, b, ha, hb, h⟩ := finset.mul_eq_one_iff.1 u.mul_inv, refine ⟨a, ha, ⟨a, b, h, singleton_injective _⟩, rfl⟩, rw [←singleton_mul_singleton, ←ha, ←hb], exact u.inv_mul }, { rintro ⟨a, rfl, ha⟩, exact ha.finset } end @[simp, to_additive] lemma is_unit_coe : is_unit (s : set α) ↔ is_unit s := by simp_rw [is_unit_iff, set.is_unit_iff, coe_eq_singleton] end division_monoid /-- `finset α` is a commutative division monoid under pointwise operations if `α` is. -/ @[to_additive subtraction_comm_monoid "`finset α` is a commutative subtraction monoid under pointwise operations if `α` is."] protected def division_comm_monoid [division_comm_monoid α] : division_comm_monoid (finset α) := coe_injective.division_comm_monoid _ coe_one coe_mul coe_inv coe_div coe_pow coe_zpow /-- `finset α` has distributive negation if `α` has. -/ protected def has_distrib_neg [has_mul α] [has_distrib_neg α] : has_distrib_neg (finset α) := coe_injective.has_distrib_neg _ coe_neg coe_mul localized "attribute [instance] finset.division_monoid finset.subtraction_monoid finset.division_comm_monoid finset.subtraction_comm_monoid finset.has_distrib_neg" in pointwise section distrib variables [distrib α] (s t u : finset α) /-! Note that `finset α` is not a `distrib` because `s * t + s * u` has cross terms that `s * (t + u)` lacks. ```lean -- {10, 16, 18, 20, 8, 9} #eval {1, 2} * ({3, 4} + {5, 6} : finset ℕ) -- {10, 11, 12, 13, 14, 15, 16, 18, 20, 8, 9} #eval ({1, 2} : finset ℕ) * {3, 4} + {1, 2} * {5, 6} ``` -/ lemma mul_add_subset : s * (t + u) ⊆ s * t + s * u := image₂_distrib_subset_left mul_add lemma add_mul_subset : (s + t) * u ⊆ s * u + t * u := image₂_distrib_subset_right add_mul end distrib section mul_zero_class variables [mul_zero_class α] {s t : finset α} /-! Note that `finset` is not a `mul_zero_class` because `0 * ∅ ≠ 0`. -/ lemma mul_zero_subset (s : finset α) : s * 0 ⊆ 0 := by simp [subset_iff, mem_mul] lemma zero_mul_subset (s : finset α) : 0 * s ⊆ 0 := by simp [subset_iff, mem_mul] lemma nonempty.mul_zero (hs : s.nonempty) : s * 0 = 0 := s.mul_zero_subset.antisymm $ by simpa [mem_mul] using hs lemma nonempty.zero_mul (hs : s.nonempty) : 0 * s = 0 := s.zero_mul_subset.antisymm $ by simpa [mem_mul] using hs end mul_zero_class section group variables [group α] [division_monoid β] [monoid_hom_class F α β] (f : F) {s t : finset α} {a b : α} /-! Note that `finset` is not a `group` because `s / s ≠ 1` in general. -/ @[simp, to_additive] lemma one_mem_div_iff : (1 : α) ∈ s / t ↔ ¬ disjoint s t := by rw [←mem_coe, ←disjoint_coe, coe_div, set.one_mem_div_iff] @[to_additive] lemma not_one_mem_div_iff : (1 : α) ∉ s / t ↔ disjoint s t := one_mem_div_iff.not_left @[to_additive] lemma nonempty.one_mem_div (h : s.nonempty) : (1 : α) ∈ s / s := let ⟨a, ha⟩ := h in mem_div.2 ⟨a, a, ha, ha, div_self' _⟩ @[to_additive] lemma is_unit_singleton (a : α) : is_unit ({a} : finset α) := (group.is_unit a).finset @[simp] lemma is_unit_iff_singleton : is_unit s ↔ ∃ a, s = {a} := by simp only [is_unit_iff, group.is_unit, and_true] @[simp, to_additive] lemma image_mul_left : image (λ b, a * b) t = preimage t (λ b, a⁻¹ * b) ((mul_right_injective _).inj_on _) := coe_injective $ by simp @[simp, to_additive] lemma image_mul_right : image (* b) t = preimage t (* b⁻¹) ((mul_left_injective _).inj_on _) := coe_injective $ by simp @[to_additive] lemma image_mul_left' : image (λ b, a⁻¹ * b) t = preimage t (λ b, a * b) ((mul_right_injective _).inj_on _) := by simp @[to_additive] lemma image_mul_right' : image (* b⁻¹) t = preimage t (* b) ((mul_left_injective _).inj_on _) := by simp lemma image_div : (s / t).image (f : α → β) = s.image f / t.image f := image_image₂_distrib $ map_div f end group section group_with_zero variables [group_with_zero α] {s t : finset α} lemma div_zero_subset (s : finset α) : s / 0 ⊆ 0 := by simp [subset_iff, mem_div] lemma zero_div_subset (s : finset α) : 0 / s ⊆ 0 := by simp [subset_iff, mem_div] lemma nonempty.div_zero (hs : s.nonempty) : s / 0 = 0 := s.div_zero_subset.antisymm $ by simpa [mem_div] using hs lemma nonempty.zero_div (hs : s.nonempty) : 0 / s = 0 := s.zero_div_subset.antisymm $ by simpa [mem_div] using hs end group_with_zero end instances section group variables [group α] {s t : finset α} {a b : α} @[simp, to_additive] lemma preimage_mul_left_singleton : preimage {b} ((*) a) ((mul_right_injective _).inj_on _) = {a⁻¹ * b} := by { classical, rw [← image_mul_left', image_singleton] } @[simp, to_additive] lemma preimage_mul_right_singleton : preimage {b} (* a) ((mul_left_injective _).inj_on _) = {b * a⁻¹} := by { classical, rw [← image_mul_right', image_singleton] } @[simp, to_additive] lemma preimage_mul_left_one : preimage 1 ((*) a) ((mul_right_injective _).inj_on _) = {a⁻¹} := by { classical, rw [← image_mul_left', image_one, mul_one] } @[simp, to_additive] lemma preimage_mul_right_one : preimage 1 (* b) ((mul_left_injective _).inj_on _) = {b⁻¹} := by { classical, rw [← image_mul_right', image_one, one_mul] } @[to_additive] lemma preimage_mul_left_one' : preimage 1 ((*) a⁻¹) ((mul_right_injective _).inj_on _) = {a} := by rw [preimage_mul_left_one, inv_inv] @[to_additive] lemma preimage_mul_right_one' : preimage 1 (* b⁻¹) ((mul_left_injective _).inj_on _) = {b} := by rw [preimage_mul_right_one, inv_inv] end group /-! ### Scalar addition/multiplication of finsets -/ section has_smul variables [decidable_eq β] [has_smul α β] {s s₁ s₂ : finset α} {t t₁ t₂ u : finset β} {a : α} {b : β} /-- The pointwise product of two finsets `s` and `t`: `s • t = {x • y | x ∈ s, y ∈ t}`. -/ @[to_additive "The pointwise sum of two finsets `s` and `t`: `s +ᵥ t = {x +ᵥ y | x ∈ s, y ∈ t}`."] protected def has_smul : has_smul (finset α) (finset β) := ⟨image₂ (•)⟩ localized "attribute [instance] finset.has_smul finset.has_vadd" in pointwise @[to_additive] lemma smul_def : s • t = (s ×ˢ t).image (λ p : α × β, p.1 • p.2) := rfl @[to_additive] lemma image_smul_product : (s ×ˢ t).image (λ x : α × β, x.fst • x.snd) = s • t := rfl @[to_additive] lemma mem_smul {x : β} : x ∈ s • t ↔ ∃ y z, y ∈ s ∧ z ∈ t ∧ y • z = x := mem_image₂ @[simp, norm_cast, to_additive] lemma coe_smul (s : finset α) (t : finset β) : (↑(s • t) : set β) = (s : set α) • t := coe_image₂ _ _ _ @[to_additive] lemma smul_mem_smul : a ∈ s → b ∈ t → a • b ∈ s • t := mem_image₂_of_mem @[to_additive] lemma smul_card_le : (s • t).card ≤ s.card • t.card := card_image₂_le _ _ _ @[simp, to_additive] lemma empty_smul (t : finset β) : (∅ : finset α) • t = ∅ := image₂_empty_left @[simp, to_additive] lemma smul_empty (s : finset α) : s • (∅ : finset β) = ∅ := image₂_empty_right @[simp, to_additive] lemma smul_eq_empty : s • t = ∅ ↔ s = ∅ ∨ t = ∅ := image₂_eq_empty_iff @[simp, to_additive] lemma smul_nonempty_iff : (s • t).nonempty ↔ s.nonempty ∧ t.nonempty := image₂_nonempty_iff @[to_additive] lemma nonempty.smul : s.nonempty → t.nonempty → (s • t).nonempty := nonempty.image₂ @[to_additive] lemma nonempty.of_smul_left : (s • t).nonempty → s.nonempty := nonempty.of_image₂_left @[to_additive] lemma nonempty.of_smul_right : (s • t).nonempty → t.nonempty := nonempty.of_image₂_right @[to_additive] lemma smul_singleton (b : β) : s • ({b} : finset β) = s.image (• b) := image₂_singleton_right @[to_additive] lemma singleton_smul_singleton (a : α) (b : β) : ({a} : finset α) • ({b} : finset β) = {a • b} := image₂_singleton @[to_additive, mono] lemma smul_subset_smul : s₁ ⊆ s₂ → t₁ ⊆ t₂ → s₁ • t₁ ⊆ s₂ • t₂ := image₂_subset @[to_additive] lemma smul_subset_smul_left : t₁ ⊆ t₂ → s • t₁ ⊆ s • t₂ := image₂_subset_left @[to_additive] lemma smul_subset_smul_right : s₁ ⊆ s₂ → s₁ • t ⊆ s₂ • t := image₂_subset_right @[to_additive] lemma smul_subset_iff : s • t ⊆ u ↔ ∀ (a ∈ s) (b ∈ t), a • b ∈ u := image₂_subset_iff attribute [mono] vadd_subset_vadd @[to_additive] lemma union_smul [decidable_eq α] : (s₁ ∪ s₂) • t = s₁ • t ∪ s₂ • t := image₂_union_left @[to_additive] lemma smul_union : s • (t₁ ∪ t₂) = s • t₁ ∪ s • t₂ := image₂_union_right @[to_additive] lemma inter_smul_subset [decidable_eq α] : (s₁ ∩ s₂) • t ⊆ s₁ • t ∩ s₂ • t := image₂_inter_subset_left @[to_additive] lemma smul_inter_subset : s • (t₁ ∩ t₂) ⊆ s • t₁ ∩ s • t₂ := image₂_inter_subset_right /-- If a finset `u` is contained in the scalar product of two sets `s • t`, we can find two finsets `s'`, `t'` such that `s' ⊆ s`, `t' ⊆ t` and `u ⊆ s' • t'`. -/ @[to_additive "If a finset `u` is contained in the scalar sum of two sets `s +ᵥ t`, we can find two finsets `s'`, `t'` such that `s' ⊆ s`, `t' ⊆ t` and `u ⊆ s' +ᵥ t'`."] lemma subset_smul {s : set α} {t : set β} : ↑u ⊆ s • t → ∃ (s' : finset α) (t' : finset β), ↑s' ⊆ s ∧ ↑t' ⊆ t ∧ u ⊆ s' • t' := subset_image₂ end has_smul /-! ### Scalar subtraction of finsets -/ section has_vsub variables [decidable_eq α] [has_vsub α β] {s s₁ s₂ t t₁ t₂ : finset β} {u : finset α} {a : α} {b c : β} include α /-- The pointwise product of two finsets `s` and `t`: `s -ᵥ t = {x -ᵥ y | x ∈ s, y ∈ t}`. -/ protected def has_vsub : has_vsub (finset α) (finset β) := ⟨image₂ (-ᵥ)⟩ localized "attribute [instance] finset.has_vsub" in pointwise lemma vsub_def : s -ᵥ t = image₂ (-ᵥ) s t := rfl @[simp] lemma image_vsub_product : image₂ (-ᵥ) s t = s -ᵥ t := rfl lemma mem_vsub : a ∈ s -ᵥ t ↔ ∃ b c, b ∈ s ∧ c ∈ t ∧ b -ᵥ c = a := mem_image₂ @[simp, norm_cast] lemma coe_vsub (s t : finset β) : (↑(s -ᵥ t) : set α) = (s : set β) -ᵥ t := coe_image₂ _ _ _ lemma vsub_mem_vsub : b ∈ s → c ∈ t → b -ᵥ c ∈ s -ᵥ t := mem_image₂_of_mem lemma vsub_card_le : (s -ᵥ t : finset α).card ≤ s.card * t.card := card_image₂_le _ _ _ @[simp] lemma empty_vsub (t : finset β) : (∅ : finset β) -ᵥ t = ∅ := image₂_empty_left @[simp] lemma vsub_empty (s : finset β) : s -ᵥ (∅ : finset β) = ∅ := image₂_empty_right @[simp] lemma vsub_eq_empty : s -ᵥ t = ∅ ↔ s = ∅ ∨ t = ∅ := image₂_eq_empty_iff @[simp] lemma vsub_nonempty : (s -ᵥ t : finset α).nonempty ↔ s.nonempty ∧ t.nonempty := image₂_nonempty_iff lemma nonempty.vsub : s.nonempty → t.nonempty → (s -ᵥ t : finset α).nonempty := nonempty.image₂ lemma nonempty.of_vsub_left : (s -ᵥ t : finset α).nonempty → s.nonempty := nonempty.of_image₂_left lemma nonempty.of_vsub_right : (s -ᵥ t : finset α).nonempty → t.nonempty := nonempty.of_image₂_right @[simp] lemma vsub_singleton (b : β) : s -ᵥ ({b} : finset β) = s.image (-ᵥ b) := image₂_singleton_right lemma singleton_vsub (a : β) : ({a} : finset β) -ᵥ t = t.image ((-ᵥ) a) := image₂_singleton_left @[simp] lemma singleton_vsub_singleton (a b : β) : ({a} : finset β) -ᵥ {b} = {a -ᵥ b} := image₂_singleton @[mono] lemma vsub_subset_vsub : s₁ ⊆ s₂ → t₁ ⊆ t₂ → s₁ -ᵥ t₁ ⊆ s₂ -ᵥ t₂ := image₂_subset lemma vsub_subset_vsub_left : t₁ ⊆ t₂ → s -ᵥ t₁ ⊆ s -ᵥ t₂ := image₂_subset_left lemma vsub_subset_vsub_right : s₁ ⊆ s₂ → s₁ -ᵥ t ⊆ s₂ -ᵥ t := image₂_subset_right lemma vsub_subset_iff : s -ᵥ t ⊆ u ↔ ∀ (x ∈ s) (y ∈ t), x -ᵥ y ∈ u := image₂_subset_iff section variables [decidable_eq β] lemma union_vsub : (s₁ ∪ s₂) -ᵥ t = (s₁ -ᵥ t) ∪ (s₂ -ᵥ t) := image₂_union_left lemma vsub_union : s -ᵥ (t₁ ∪ t₂) = (s -ᵥ t₁) ∪ (s -ᵥ t₂) := image₂_union_right lemma inter_vsub_subset : (s₁ ∩ s₂) -ᵥ t ⊆ (s₁ -ᵥ t) ∩ (s₂ -ᵥ t) := image₂_inter_subset_left lemma vsub_inter_subset : s -ᵥ (t₁ ∩ t₂) ⊆ (s -ᵥ t₁) ∩ (s -ᵥ t₂) := image₂_inter_subset_right end /-- If a finset `u` is contained in the pointwise subtraction of two sets `s -ᵥ t`, we can find two finsets `s'`, `t'` such that `s' ⊆ s`, `t' ⊆ t` and `u ⊆ s' -ᵥ t'`. -/ lemma subset_vsub {s t : set β} : ↑u ⊆ s -ᵥ t → ∃ s' t' : finset β, ↑s' ⊆ s ∧ ↑t' ⊆ t ∧ u ⊆ s' -ᵥ t' := subset_image₂ end has_vsub open_locale pointwise /-! ### Translation/scaling of finsets -/ section has_smul variables [decidable_eq β] [has_smul α β] {s s₁ s₂ t u : finset β} {a : α} {b : β} /-- The scaling of a finset `s` by a scalar `a`: `a • s = {a • x | x ∈ s}`. -/ @[to_additive "The translation of a finset `s` by a vector `a`: `a +ᵥ s = {a +ᵥ x | x ∈ s}`."] protected def has_smul_finset : has_smul α (finset β) := ⟨λ a, image $ (•) a⟩ localized "attribute [instance] finset.has_smul_finset finset.has_vadd_finset" in pointwise @[to_additive] lemma smul_finset_def : a • s = s.image ((•) a) := rfl @[to_additive] lemma image_smul : s.image (λ x, a • x) = a • s := rfl @[to_additive] lemma mem_smul_finset {x : β} : x ∈ a • s ↔ ∃ y, y ∈ s ∧ a • y = x := by simp only [finset.smul_finset_def, and.assoc, mem_image, exists_prop, prod.exists, mem_product] @[simp, norm_cast, to_additive] lemma coe_smul_finset (a : α) (s : finset β) : (↑(a • s) : set β) = a • s := coe_image @[to_additive] lemma smul_finset_mem_smul_finset : b ∈ s → a • b ∈ a • s := mem_image_of_mem _ @[to_additive] lemma smul_finset_card_le : (a • s).card ≤ s.card := card_image_le @[simp, to_additive] lemma smul_finset_empty (a : α) : a • (∅ : finset β) = ∅ := image_empty _ @[simp, to_additive] lemma smul_finset_eq_empty : a • s = ∅ ↔ s = ∅ := image_eq_empty @[simp, to_additive] lemma smul_finset_nonempty : (a • s).nonempty ↔ s.nonempty := nonempty.image_iff _ @[to_additive] lemma nonempty.smul_finset (hs : s.nonempty) : (a • s).nonempty := hs.image _ @[simp, to_additive] lemma singleton_smul (a : α) : ({a} : finset α) • t = a • t := image₂_singleton_left @[to_additive, mono] lemma smul_finset_subset_smul_finset : s ⊆ t → a • s ⊆ a • t := image_subset_image attribute [mono] vadd_finset_subset_vadd_finset @[simp, to_additive] lemma smul_finset_singleton (b : β) : a • ({b} : finset β) = {a • b} := image_singleton _ _ @[to_additive] lemma smul_finset_union : a • (s₁ ∪ s₂) = a • s₁ ∪ a • s₂ := image_union _ _ @[to_additive] lemma smul_finset_inter_subset : a • (s₁ ∩ s₂) ⊆ a • s₁ ∩ (a • s₂) := image_inter_subset _ _ _ @[simp] lemma bUnion_smul_finset (s : finset α) (t : finset β) : s.bUnion (• t) = s • t := bUnion_image_left end has_smul open_locale pointwise section instances variables [decidable_eq γ] @[to_additive] instance smul_comm_class_finset [has_smul α γ] [has_smul β γ] [smul_comm_class α β γ] : smul_comm_class α β (finset γ) := ⟨λ _ _, commute.finset_image $ smul_comm _ _⟩ @[to_additive] instance smul_comm_class_finset' [has_smul α γ] [has_smul β γ] [smul_comm_class α β γ] : smul_comm_class α (finset β) (finset γ) := ⟨λ a s t, coe_injective $ by simp only [coe_smul_finset, coe_smul, smul_comm]⟩ @[to_additive] instance smul_comm_class_finset'' [has_smul α γ] [has_smul β γ] [smul_comm_class α β γ] : smul_comm_class (finset α) β (finset γ) := by haveI := smul_comm_class.symm α β γ; exact smul_comm_class.symm _ _ _ @[to_additive] instance smul_comm_class [has_smul α γ] [has_smul β γ] [smul_comm_class α β γ] : smul_comm_class (finset α) (finset β) (finset γ) := ⟨λ s t u, coe_injective $ by simp_rw [coe_smul, smul_comm]⟩ @[to_additive] instance is_scalar_tower [has_smul α β] [has_smul α γ] [has_smul β γ] [is_scalar_tower α β γ] : is_scalar_tower α β (finset γ) := ⟨λ a b s, by simp only [←image_smul, image_image, smul_assoc]⟩ variables [decidable_eq β] @[to_additive] instance is_scalar_tower' [has_smul α β] [has_smul α γ] [has_smul β γ] [is_scalar_tower α β γ] : is_scalar_tower α (finset β) (finset γ) := ⟨λ a s t, coe_injective $ by simp only [coe_smul_finset, coe_smul, smul_assoc]⟩ @[to_additive] instance is_scalar_tower'' [has_smul α β] [has_smul α γ] [has_smul β γ] [is_scalar_tower α β γ] : is_scalar_tower (finset α) (finset β) (finset γ) := ⟨λ a s t, coe_injective $ by simp only [coe_smul_finset, coe_smul, smul_assoc]⟩ instance is_central_scalar [has_smul α β] [has_smul αᵐᵒᵖ β] [is_central_scalar α β] : is_central_scalar α (finset β) := ⟨λ a s, coe_injective $ by simp only [coe_smul_finset, coe_smul, op_smul_eq_smul]⟩ /-- A multiplicative action of a monoid `α` on a type `β` gives a multiplicative action of `finset α` on `finset β`. -/ @[to_additive "An additive action of an additive monoid `α` on a type `β` gives an additive action of `finset α` on `finset β`"] protected def mul_action [decidable_eq α] [monoid α] [mul_action α β] : mul_action (finset α) (finset β) := { mul_smul := λ _ _ _, image₂_assoc mul_smul, one_smul := λ s, image₂_singleton_left.trans $ by simp_rw [one_smul, image_id'] } /-- A multiplicative action of a monoid on a type `β` gives a multiplicative action on `finset β`. -/ @[to_additive "An additive action of an additive monoid on a type `β` gives an additive action on `finset β`."] protected def mul_action_finset [monoid α] [mul_action α β] : mul_action α (finset β) := coe_injective.mul_action _ coe_smul_finset localized "attribute [instance] finset.mul_action_finset finset.add_action_finset finset.mul_action finset.add_action" in pointwise /-- A distributive multiplicative action of a monoid on an additive monoid `β` gives a distributive multiplicative action on `finset β`. -/ protected def distrib_mul_action_finset [monoid α] [add_monoid β] [distrib_mul_action α β] : distrib_mul_action α (finset β) := function.injective.distrib_mul_action ⟨coe, coe_zero, coe_add⟩ coe_injective coe_smul_finset /-- A multiplicative action of a monoid on a monoid `β` gives a multiplicative action on `set β`. -/ protected def mul_distrib_mul_action_finset [monoid α] [monoid β] [mul_distrib_mul_action α β] : mul_distrib_mul_action α (finset β) := function.injective.mul_distrib_mul_action ⟨coe, coe_one, coe_mul⟩ coe_injective coe_smul_finset localized "attribute [instance] finset.distrib_mul_action_finset finset.mul_distrib_mul_action_finset" in pointwise instance [decidable_eq α] [has_zero α] [has_mul α] [no_zero_divisors α] : no_zero_divisors (finset α) := coe_injective.no_zero_divisors _ coe_zero coe_mul instance [has_zero α] [has_zero β] [has_smul α β] [no_zero_smul_divisors α β] : no_zero_smul_divisors (finset α) (finset β) := ⟨λ s t h, begin by_contra' H, have hst : (s • t).nonempty := h.symm.subst zero_nonempty, simp_rw [←hst.of_smul_left.subset_zero_iff, ←hst.of_smul_right.subset_zero_iff, not_subset, mem_zero] at H, obtain ⟨⟨a, hs, ha⟩, b, ht, hb⟩ := H, have := subset_of_eq h, exact (eq_zero_or_eq_zero_of_smul_eq_zero $ mem_zero.1 $ this $ smul_mem_smul hs ht).elim ha hb, end⟩ instance no_zero_smul_divisors_finset [has_zero α] [has_zero β] [has_smul α β] [no_zero_smul_divisors α β] : no_zero_smul_divisors α (finset β) := coe_injective.no_zero_smul_divisors _ coe_zero coe_smul_finset end instances section left_cancel_semigroup variables [left_cancel_semigroup α] [decidable_eq α] (s t : finset α) (a : α) @[to_additive] lemma pairwise_disjoint_smul_iff {s : set α} {t : finset α} : s.pairwise_disjoint (• t) ↔ (s ×ˢ t : set (α × α)).inj_on (λ p, p.1 * p.2) := by simp_rw [←pairwise_disjoint_coe, coe_smul_finset, set.pairwise_disjoint_smul_iff] @[simp, to_additive] lemma card_singleton_mul : ({a} * t).card = t.card := card_image₂_singleton_left _ $ mul_right_injective _ @[to_additive] lemma singleton_mul_inter : {a} * (s ∩ t) = ({a} * s) ∩ ({a} * t) := image₂_singleton_inter _ _ $ mul_right_injective _ @[to_additive] lemma card_le_card_mul_left {s : finset α} (hs : s.nonempty) : t.card ≤ (s * t).card := card_le_card_image₂_left _ hs mul_right_injective end left_cancel_semigroup section variables [right_cancel_semigroup α] [decidable_eq α] (s t : finset α) (a : α) @[simp, to_additive] lemma card_mul_singleton : (s * {a}).card = s.card := card_image₂_singleton_right _ $ mul_left_injective _ @[to_additive] lemma inter_mul_singleton : (s ∩ t) * {a} = (s * {a}) ∩ (t * {a}) := image₂_inter_singleton _ _ $ mul_left_injective _ @[to_additive] lemma card_le_card_mul_right {t : finset α} (ht : t.nonempty) : s.card ≤ (s * t).card := card_le_card_image₂_right _ ht mul_left_injective end open_locale pointwise section group variables [decidable_eq β] [group α] [mul_action α β] {s t : finset β} {a : α} {b : β} @[simp, to_additive] lemma smul_mem_smul_finset_iff (a : α) : a • b ∈ a • s ↔ b ∈ s := (mul_action.injective _).mem_finset_image @[to_additive] lemma inv_smul_mem_iff : a⁻¹ • b ∈ s ↔ b ∈ a • s := by rw [←smul_mem_smul_finset_iff a, smul_inv_smul] @[to_additive] lemma mem_inv_smul_finset_iff : b ∈ a⁻¹ • s ↔ a • b ∈ s := by rw [←smul_mem_smul_finset_iff a, smul_inv_smul] @[simp, to_additive] lemma smul_finset_subset_smul_finset_iff : a • s ⊆ a • t ↔ s ⊆ t := image_subset_image_iff $ mul_action.injective _ @[to_additive] lemma smul_finset_subset_iff : a • s ⊆ t ↔ s ⊆ a⁻¹ • t := by { simp_rw ←coe_subset, push_cast, exact set.set_smul_subset_iff } @[to_additive] lemma subset_smul_finset_iff : s ⊆ a • t ↔ a⁻¹ • s ⊆ t := by { simp_rw ←coe_subset, push_cast, exact set.subset_set_smul_iff } end group section group_with_zero variables [decidable_eq β] [group_with_zero α] [mul_action α β] {s t : finset β} {a : α} {b : β} @[simp] lemma smul_mem_smul_finset_iff₀ (ha : a ≠ 0) : a • b ∈ a • s ↔ b ∈ s := smul_mem_smul_finset_iff (units.mk0 a ha) lemma inv_smul_mem_iff₀ (ha : a ≠ 0) : a⁻¹ • b ∈ s ↔ b ∈ a • s := show _ ↔ _ ∈ units.mk0 a ha • _, from inv_smul_mem_iff lemma mem_inv_smul_finset_iff₀ (ha : a ≠ 0) : b ∈ a⁻¹ • s ↔ a • b ∈ s := show _ ∈ (units.mk0 a ha)⁻¹ • _ ↔ _, from mem_inv_smul_finset_iff @[simp] lemma smul_finset_subset_smul_finset_iff₀ (ha : a ≠ 0) : a • s ⊆ a • t ↔ s ⊆ t := show units.mk0 a ha • _ ⊆ _ ↔ _, from smul_finset_subset_smul_finset_iff lemma smul_finset_subset_iff₀ (ha : a ≠ 0) : a • s ⊆ t ↔ s ⊆ a⁻¹ • t := show units.mk0 a ha • _ ⊆ _ ↔ _, from smul_finset_subset_iff lemma subset_smul_finset_iff₀ (ha : a ≠ 0) : s ⊆ a • t ↔ a⁻¹ • s ⊆ t := show _ ⊆ units.mk0 a ha • _ ↔ _, from subset_smul_finset_iff lemma smul_univ₀ [fintype β] {s : finset α} (hs : ¬ s ⊆ 0) : s • (univ : finset β) = univ := coe_injective $ by { rw ←coe_subset at hs, push_cast at ⊢ hs, exact set.smul_univ₀ hs } lemma smul_finset_univ₀ [fintype β] (ha : a ≠ 0) : a • (univ : finset β) = univ := coe_injective $ by { push_cast, exact set.smul_set_univ₀ ha } end group_with_zero section smul_with_zero variables [has_zero α] [has_zero β] [smul_with_zero α β] [decidable_eq β] {s : finset α} {t : finset β} /-! Note that we have neither `smul_with_zero α (finset β)` nor `smul_with_zero (finset α) (finset β)` because `0 * ∅ ≠ 0`. -/ lemma smul_zero_subset (s : finset α) : s • (0 : finset β) ⊆ 0 := by simp [subset_iff, mem_smul] lemma zero_smul_subset (t : finset β) : (0 : finset α) • t ⊆ 0 := by simp [subset_iff, mem_smul] lemma nonempty.smul_zero (hs : s.nonempty) : s • (0 : finset β) = 0 := s.smul_zero_subset.antisymm $ by simpa [mem_smul] using hs lemma nonempty.zero_smul (ht : t.nonempty) : (0 : finset α) • t = 0 := t.zero_smul_subset.antisymm $ by simpa [mem_smul] using ht /-- A nonempty set is scaled by zero to the singleton set containing 0. -/ lemma zero_smul_finset {s : finset β} (h : s.nonempty) : (0 : α) • s = (0 : finset β) := coe_injective $ by simpa using @set.zero_smul_set α _ _ _ _ _ h lemma zero_smul_finset_subset (s : finset β) : (0 : α) • s ⊆ 0 := image_subset_iff.2 $ λ x _, mem_zero.2 $ zero_smul α x lemma zero_mem_smul_finset {t : finset β} {a : α} (h : (0 : β) ∈ t) : (0 : β) ∈ a • t := mem_smul_finset.2 ⟨0, h, smul_zero _⟩ variables [no_zero_smul_divisors α β] {a : α} lemma zero_mem_smul_iff : (0 : β) ∈ s • t ↔ (0 : α) ∈ s ∧ t.nonempty ∨ (0 : β) ∈ t ∧ s.nonempty := by { rw [←mem_coe, coe_smul, set.zero_mem_smul_iff], refl } lemma zero_mem_smul_finset_iff (ha : a ≠ 0) : (0 : β) ∈ a • t ↔ (0 : β) ∈ t := by { rw [←mem_coe, coe_smul_finset, set.zero_mem_smul_set_iff ha, mem_coe], apply_instance } end smul_with_zero section monoid variables [monoid α] [add_group β] [distrib_mul_action α β] [decidable_eq β] (a : α) (s : finset α) (t : finset β) @[simp] lemma smul_finset_neg : a • -t = -(a • t) := by simp only [←image_smul, ←image_neg, function.comp, image_image, smul_neg] @[simp] protected lemma smul_neg : s • -t = -(s • t) := by { simp_rw ←image_neg, exact image_image₂_right_comm smul_neg } end monoid section ring variables [ring α] [add_comm_group β] [module α β] [decidable_eq β] {s : finset α} {t : finset β} {a : α} @[simp] lemma neg_smul_finset : -a • t = -(a • t) := by simp only [←image_smul, ←image_neg, image_image, neg_smul] @[simp] protected lemma neg_smul [decidable_eq α] : -s • t = -(s • t) := by { simp_rw ←image_neg, exact image₂_image_left_comm neg_smul } end ring end finset
424f10697382aa469407d5ee90c7e574e42cd60e
d29d82a0af640c937e499f6be79fc552eae0aa13
/src/linear_algebra/matrix/to_lin.lean
2164c0bc14645dd62d49790b7da1b9cfc45ccde3
[ "Apache-2.0" ]
permissive
AbdulMajeedkhurasani/mathlib
835f8a5c5cf3075b250b3737172043ab4fa1edf6
79bc7323b164aebd000524ebafd198eb0e17f956
refs/heads/master
1,688,003,895,660
1,627,788,521,000
1,627,788,521,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
27,608
lean
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Patrick Massot, Casper Putz, Anne Baanen -/ import data.matrix.block import linear_algebra.matrix.finite_dimensional import linear_algebra.std_basis import ring_theory.algebra_tower import ring_theory.matrix_algebra /-! # Linear maps and matrices This file defines the maps to send matrices to a linear map, and to send linear maps between modules with a finite bases to matrices. This defines a linear equivalence between linear maps between finite-dimensional vector spaces and matrices indexed by the respective bases. ## Main definitions In the list below, and in all this file, `R` is a commutative ring (semiring is sometimes enough), `M` and its variations are `R`-modules, `ι`, `κ`, `n` and `m` are finite types used for indexing. * `linear_map.to_matrix`: given bases `v₁ : ι → M₁` and `v₂ : κ → M₂`, the `R`-linear equivalence from `M₁ →ₗ[R] M₂` to `matrix κ ι R` * `matrix.to_lin`: the inverse of `linear_map.to_matrix` * `linear_map.to_matrix'`: the `R`-linear equivalence from `(n → R) →ₗ[R] (m → R)` to `matrix n m R` (with the standard basis on `n → R` and `m → R`) * `matrix.to_lin'`: the inverse of `linear_map.to_matrix'` * `alg_equiv_matrix`: given a basis indexed by `n`, the `R`-algebra equivalence between `R`-endomorphisms of `M` and `matrix n n R` ## Tags linear_map, matrix, linear_equiv, diagonal, det, trace -/ noncomputable theory open linear_map matrix set submodule open_locale big_operators open_locale matrix universes u v w section to_matrix' variables {R : Type*} [comm_ring R] variables {l m n : Type*} [fintype l] [fintype m] [fintype n] instance [decidable_eq m] [decidable_eq n] (R) [fintype R] : fintype (matrix m n R) := by unfold matrix; apply_instance /-- `matrix.mul_vec M` is a linear map. -/ def matrix.mul_vec_lin (M : matrix m n R) : (n → R) →ₗ[R] (m → R) := { to_fun := M.mul_vec, map_add' := λ v w, funext (λ i, dot_product_add _ _ _), map_smul' := λ c v, funext (λ i, dot_product_smul _ _ _) } @[simp] lemma matrix.mul_vec_lin_apply (M : matrix m n R) (v : n → R) : matrix.mul_vec_lin M v = M.mul_vec v := rfl variables [decidable_eq n] @[simp] lemma matrix.mul_vec_std_basis (M : matrix m n R) (i j) : M.mul_vec (std_basis R (λ _, R) j 1) i = M i j := begin have : (∑ j', M i j' * if j = j' then 1 else 0) = M i j, { simp_rw [mul_boole, finset.sum_ite_eq, finset.mem_univ, if_true] }, convert this, ext, split_ifs with h; simp only [std_basis_apply], { rw [h, function.update_same] }, { rw [function.update_noteq (ne.symm h), pi.zero_apply] } end /-- Linear maps `(n → R) →ₗ[R] (m → R)` are linearly equivalent to `matrix m n R`. -/ def linear_map.to_matrix' : ((n → R) →ₗ[R] (m → R)) ≃ₗ[R] matrix m n R := { to_fun := λ f i j, f (std_basis R (λ _, R) j 1) i, inv_fun := matrix.mul_vec_lin, right_inv := λ M, by { ext i j, simp only [matrix.mul_vec_std_basis, matrix.mul_vec_lin_apply] }, left_inv := λ f, begin apply (pi.basis_fun R n).ext, intro j, ext i, simp only [pi.basis_fun_apply, matrix.mul_vec_std_basis, matrix.mul_vec_lin_apply] end, map_add' := λ f g, by { ext i j, simp only [pi.add_apply, linear_map.add_apply] }, map_smul' := λ c f, by { ext i j, simp only [pi.smul_apply, linear_map.smul_apply] } } /-- A `matrix m n R` is linearly equivalent to a linear map `(n → R) →ₗ[R] (m → R)`. -/ def matrix.to_lin' : matrix m n R ≃ₗ[R] ((n → R) →ₗ[R] (m → R)) := linear_map.to_matrix'.symm @[simp] lemma linear_map.to_matrix'_symm : (linear_map.to_matrix'.symm : matrix m n R ≃ₗ[R] _) = matrix.to_lin' := rfl @[simp] lemma matrix.to_lin'_symm : (matrix.to_lin'.symm : ((n → R) →ₗ[R] (m → R)) ≃ₗ[R] _) = linear_map.to_matrix' := rfl @[simp] lemma linear_map.to_matrix'_to_lin' (M : matrix m n R) : linear_map.to_matrix' (matrix.to_lin' M) = M := linear_map.to_matrix'.apply_symm_apply M @[simp] lemma matrix.to_lin'_to_matrix' (f : (n → R) →ₗ[R] (m → R)) : matrix.to_lin' (linear_map.to_matrix' f) = f := matrix.to_lin'.apply_symm_apply f @[simp] lemma linear_map.to_matrix'_apply (f : (n → R) →ₗ[R] (m → R)) (i j) : linear_map.to_matrix' f i j = f (λ j', if j' = j then 1 else 0) i := begin simp only [linear_map.to_matrix', linear_equiv.coe_mk], congr, ext j', split_ifs with h, { rw [h, std_basis_same] }, apply std_basis_ne _ _ _ _ h end @[simp] lemma matrix.to_lin'_apply (M : matrix m n R) (v : n → R) : matrix.to_lin' M v = M.mul_vec v := rfl @[simp] lemma matrix.to_lin'_one : matrix.to_lin' (1 : matrix n n R) = id := by { ext, simp [linear_map.one_apply, std_basis_apply] } @[simp] lemma linear_map.to_matrix'_id : (linear_map.to_matrix' (linear_map.id : (n → R) →ₗ[R] (n → R))) = 1 := by { ext, rw [matrix.one_apply, linear_map.to_matrix'_apply, id_apply] } @[simp] lemma matrix.to_lin'_mul [decidable_eq m] (M : matrix l m R) (N : matrix m n R) : matrix.to_lin' (M ⬝ N) = (matrix.to_lin' M).comp (matrix.to_lin' N) := by { ext, simp } /-- Shortcut lemma for `matrix.to_lin'_mul` and `linear_map.comp_apply` -/ lemma matrix.to_lin'_mul_apply [decidable_eq m] (M : matrix l m R) (N : matrix m n R) (x) : matrix.to_lin' (M ⬝ N) x = (matrix.to_lin' M (matrix.to_lin' N x)) := by rw [matrix.to_lin'_mul, linear_map.comp_apply] lemma linear_map.to_matrix'_comp [decidable_eq l] (f : (n → R) →ₗ[R] (m → R)) (g : (l → R) →ₗ[R] (n → R)) : (f.comp g).to_matrix' = f.to_matrix' ⬝ g.to_matrix' := suffices (f.comp g) = (f.to_matrix' ⬝ g.to_matrix').to_lin', by rw [this, linear_map.to_matrix'_to_lin'], by rw [matrix.to_lin'_mul, matrix.to_lin'_to_matrix', matrix.to_lin'_to_matrix'] lemma linear_map.to_matrix'_mul [decidable_eq m] (f g : (m → R) →ₗ[R] (m → R)) : (f * g).to_matrix' = f.to_matrix' ⬝ g.to_matrix' := linear_map.to_matrix'_comp f g /-- If `M` and `M'` are each other's inverse matrices, they provide an equivalence between `m → A` and `n → A` corresponding to `M.mul_vec` and `M'.mul_vec`. -/ @[simps] def matrix.to_lin'_of_inv [decidable_eq m] {M : matrix m n R} {M' : matrix n m R} (hMM' : M ⬝ M' = 1) (hM'M : M' ⬝ M = 1) : (m → R) ≃ₗ[R] (n → R) := { to_fun := matrix.to_lin' M', inv_fun := M.to_lin', left_inv := λ x, by rw [← matrix.to_lin'_mul_apply, hMM', matrix.to_lin'_one, id_apply], right_inv := λ x, by rw [← matrix.to_lin'_mul_apply, hM'M, matrix.to_lin'_one, id_apply], .. matrix.to_lin' M' } /-- Linear maps `(n → R) →ₗ[R] (n → R)` are algebra equivalent to `matrix n n R`. -/ def linear_map.to_matrix_alg_equiv' : ((n → R) →ₗ[R] (n → R)) ≃ₐ[R] matrix n n R := alg_equiv.of_linear_equiv linear_map.to_matrix' linear_map.to_matrix'_mul (by simp [module.algebra_map_End_eq_smul_id]) /-- A `matrix n n R` is algebra equivalent to a linear map `(n → R) →ₗ[R] (n → R)`. -/ def matrix.to_lin_alg_equiv' : matrix n n R ≃ₐ[R] ((n → R) →ₗ[R] (n → R)) := linear_map.to_matrix_alg_equiv'.symm @[simp] lemma linear_map.to_matrix_alg_equiv'_symm : (linear_map.to_matrix_alg_equiv'.symm : matrix n n R ≃ₐ[R] _) = matrix.to_lin_alg_equiv' := rfl @[simp] lemma matrix.to_lin_alg_equiv'_symm : (matrix.to_lin_alg_equiv'.symm : ((n → R) →ₗ[R] (n → R)) ≃ₐ[R] _) = linear_map.to_matrix_alg_equiv' := rfl @[simp] lemma linear_map.to_matrix_alg_equiv'_to_lin_alg_equiv' (M : matrix n n R) : linear_map.to_matrix_alg_equiv' (matrix.to_lin_alg_equiv' M) = M := linear_map.to_matrix_alg_equiv'.apply_symm_apply M @[simp] lemma matrix.to_lin_alg_equiv'_to_matrix_alg_equiv' (f : (n → R) →ₗ[R] (n → R)) : matrix.to_lin_alg_equiv' (linear_map.to_matrix_alg_equiv' f) = f := matrix.to_lin_alg_equiv'.apply_symm_apply f @[simp] lemma linear_map.to_matrix_alg_equiv'_apply (f : (n → R) →ₗ[R] (n → R)) (i j) : linear_map.to_matrix_alg_equiv' f i j = f (λ j', if j' = j then 1 else 0) i := by simp [linear_map.to_matrix_alg_equiv'] @[simp] lemma matrix.to_lin_alg_equiv'_apply (M : matrix n n R) (v : n → R) : matrix.to_lin_alg_equiv' M v = M.mul_vec v := rfl @[simp] lemma matrix.to_lin_alg_equiv'_one : matrix.to_lin_alg_equiv' (1 : matrix n n R) = id := by { ext, simp [matrix.one_apply, std_basis_apply] } @[simp] lemma linear_map.to_matrix_alg_equiv'_id : (linear_map.to_matrix_alg_equiv' (linear_map.id : (n → R) →ₗ[R] (n → R))) = 1 := by { ext, rw [matrix.one_apply, linear_map.to_matrix_alg_equiv'_apply, id_apply] } @[simp] lemma matrix.to_lin_alg_equiv'_mul (M N : matrix n n R) : matrix.to_lin_alg_equiv' (M ⬝ N) = (matrix.to_lin_alg_equiv' M).comp (matrix.to_lin_alg_equiv' N) := by { ext, simp } lemma linear_map.to_matrix_alg_equiv'_comp (f g : (n → R) →ₗ[R] (n → R)) : (f.comp g).to_matrix_alg_equiv' = f.to_matrix_alg_equiv' ⬝ g.to_matrix_alg_equiv' := suffices (f.comp g) = (f.to_matrix_alg_equiv' ⬝ g.to_matrix_alg_equiv').to_lin_alg_equiv', by rw [this, linear_map.to_matrix_alg_equiv'_to_lin_alg_equiv'], by rw [matrix.to_lin_alg_equiv'_mul, matrix.to_lin_alg_equiv'_to_matrix_alg_equiv', matrix.to_lin_alg_equiv'_to_matrix_alg_equiv'] lemma linear_map.to_matrix_alg_equiv'_mul (f g : (n → R) →ₗ[R] (n → R)) : (f * g).to_matrix_alg_equiv' = f.to_matrix_alg_equiv' ⬝ g.to_matrix_alg_equiv' := linear_map.to_matrix_alg_equiv'_comp f g lemma matrix.rank_vec_mul_vec {K m n : Type u} [field K] [fintype m] [fintype n] [decidable_eq n] (w : m → K) (v : n → K) : rank (vec_mul_vec w v).to_lin' ≤ 1 := begin rw [vec_mul_vec_eq, matrix.to_lin'_mul], refine le_trans (rank_comp_le1 _ _) _, refine le_trans (rank_le_domain _) _, rw [dim_fun', ← cardinal.lift_eq_nat_iff.mpr (cardinal.fintype_card unit), cardinal.mk_unit], exact le_of_eq (cardinal.lift_one) end end to_matrix' section to_matrix variables {R : Type*} [comm_ring R] variables {l m n : Type*} [fintype l] [fintype m] [fintype n] [decidable_eq n] variables {M₁ M₂ : Type*} [add_comm_group M₁] [add_comm_group M₂] [module R M₁] [module R M₂] variables (v₁ : basis n R M₁) (v₂ : basis m R M₂) /-- Given bases of two modules `M₁` and `M₂` over a commutative ring `R`, we get a linear equivalence between linear maps `M₁ →ₗ M₂` and matrices over `R` indexed by the bases. -/ def linear_map.to_matrix : (M₁ →ₗ[R] M₂) ≃ₗ[R] matrix m n R := linear_equiv.trans (linear_equiv.arrow_congr v₁.equiv_fun v₂.equiv_fun) linear_map.to_matrix' /-- Given bases of two modules `M₁` and `M₂` over a commutative ring `R`, we get a linear equivalence between matrices over `R` indexed by the bases and linear maps `M₁ →ₗ M₂`. -/ def matrix.to_lin : matrix m n R ≃ₗ[R] (M₁ →ₗ[R] M₂) := (linear_map.to_matrix v₁ v₂).symm @[simp] lemma linear_map.to_matrix_symm : (linear_map.to_matrix v₁ v₂).symm = matrix.to_lin v₁ v₂ := rfl @[simp] lemma matrix.to_lin_symm : (matrix.to_lin v₁ v₂).symm = linear_map.to_matrix v₁ v₂ := rfl @[simp] lemma matrix.to_lin_to_matrix (f : M₁ →ₗ[R] M₂) : matrix.to_lin v₁ v₂ (linear_map.to_matrix v₁ v₂ f) = f := by rw [← matrix.to_lin_symm, linear_equiv.apply_symm_apply] @[simp] lemma linear_map.to_matrix_to_lin (M : matrix m n R) : linear_map.to_matrix v₁ v₂ (matrix.to_lin v₁ v₂ M) = M := by rw [← matrix.to_lin_symm, linear_equiv.symm_apply_apply] lemma linear_map.to_matrix_apply (f : M₁ →ₗ[R] M₂) (i : m) (j : n) : linear_map.to_matrix v₁ v₂ f i j = v₂.repr (f (v₁ j)) i := begin rw [linear_map.to_matrix, linear_equiv.trans_apply, linear_map.to_matrix'_apply, linear_equiv.arrow_congr_apply, basis.equiv_fun_symm_apply, finset.sum_eq_single j, if_pos rfl, one_smul, basis.equiv_fun_apply], { intros j' _ hj', rw [if_neg hj', zero_smul] }, { intro hj, have := finset.mem_univ j, contradiction } end lemma linear_map.to_matrix_transpose_apply (f : M₁ →ₗ[R] M₂) (j : n) : (linear_map.to_matrix v₁ v₂ f)ᵀ j = v₂.repr (f (v₁ j)) := funext $ λ i, f.to_matrix_apply _ _ i j lemma linear_map.to_matrix_apply' (f : M₁ →ₗ[R] M₂) (i : m) (j : n) : linear_map.to_matrix v₁ v₂ f i j = v₂.repr (f (v₁ j)) i := linear_map.to_matrix_apply v₁ v₂ f i j lemma linear_map.to_matrix_transpose_apply' (f : M₁ →ₗ[R] M₂) (j : n) : (linear_map.to_matrix v₁ v₂ f)ᵀ j = v₂.repr (f (v₁ j)) := linear_map.to_matrix_transpose_apply v₁ v₂ f j lemma matrix.to_lin_apply (M : matrix m n R) (v : M₁) : matrix.to_lin v₁ v₂ M v = ∑ j, M.mul_vec (v₁.repr v) j • v₂ j := show v₂.equiv_fun.symm (matrix.to_lin' M (v₁.repr v)) = _, by rw [matrix.to_lin'_apply, v₂.equiv_fun_symm_apply] @[simp] lemma matrix.to_lin_self (M : matrix m n R) (i : n) : matrix.to_lin v₁ v₂ M (v₁ i) = ∑ j, M j i • v₂ j := begin rw [matrix.to_lin_apply, finset.sum_congr rfl (λ j hj, _)], rw [basis.repr_self, matrix.mul_vec, dot_product, finset.sum_eq_single i, finsupp.single_eq_same, mul_one], { intros i' _ i'_ne, rw [finsupp.single_eq_of_ne i'_ne.symm, mul_zero] }, { intros, have := finset.mem_univ i, contradiction }, end /-- This will be a special case of `linear_map.to_matrix_id_eq_basis_to_matrix`. -/ lemma linear_map.to_matrix_id : linear_map.to_matrix v₁ v₁ id = 1 := begin ext i j, simp [linear_map.to_matrix_apply, matrix.one_apply, finsupp.single, eq_comm] end lemma linear_map.to_matrix_one : linear_map.to_matrix v₁ v₁ 1 = 1 := linear_map.to_matrix_id v₁ @[simp] lemma matrix.to_lin_one : matrix.to_lin v₁ v₁ 1 = id := by rw [← linear_map.to_matrix_id v₁, matrix.to_lin_to_matrix] theorem linear_map.to_matrix_reindex_range [decidable_eq M₁] [decidable_eq M₂] (f : M₁ →ₗ[R] M₂) (k : m) (i : n) : linear_map.to_matrix v₁.reindex_range v₂.reindex_range f ⟨v₂ k, mem_range_self k⟩ ⟨v₁ i, mem_range_self i⟩ = linear_map.to_matrix v₁ v₂ f k i := by simp_rw [linear_map.to_matrix_apply, basis.reindex_range_self, basis.reindex_range_repr] variables {M₃ : Type*} [add_comm_group M₃] [module R M₃] (v₃ : basis l R M₃) lemma linear_map.to_matrix_comp [decidable_eq m] (f : M₂ →ₗ[R] M₃) (g : M₁ →ₗ[R] M₂) : linear_map.to_matrix v₁ v₃ (f.comp g) = linear_map.to_matrix v₂ v₃ f ⬝ linear_map.to_matrix v₁ v₂ g := by simp_rw [linear_map.to_matrix, linear_equiv.trans_apply, linear_equiv.arrow_congr_comp _ v₂.equiv_fun, linear_map.to_matrix'_comp] lemma linear_map.to_matrix_mul (f g : M₁ →ₗ[R] M₁) : linear_map.to_matrix v₁ v₁ (f * g) = linear_map.to_matrix v₁ v₁ f ⬝ linear_map.to_matrix v₁ v₁ g := by { rw [show (@has_mul.mul (M₁ →ₗ[R] M₁) _) = linear_map.comp, from rfl, linear_map.to_matrix_comp v₁ v₁ v₁ f g] } lemma linear_map.to_matrix_mul_vec_repr (f : M₁ →ₗ[R] M₂) (x : M₁) : (linear_map.to_matrix v₁ v₂ f).mul_vec (v₁.repr x) = v₂.repr (f x) := by { ext i, rw [← matrix.to_lin'_apply, linear_map.to_matrix, linear_equiv.trans_apply, matrix.to_lin'_to_matrix', linear_equiv.arrow_congr_apply, v₂.equiv_fun_apply], congr, exact v₁.equiv_fun.symm_apply_apply x } lemma matrix.to_lin_mul [decidable_eq m] (A : matrix l m R) (B : matrix m n R) : matrix.to_lin v₁ v₃ (A ⬝ B) = (matrix.to_lin v₂ v₃ A).comp (matrix.to_lin v₁ v₂ B) := begin apply (linear_map.to_matrix v₁ v₃).injective, haveI : decidable_eq l := λ _ _, classical.prop_decidable _, rw linear_map.to_matrix_comp v₁ v₂ v₃, repeat { rw linear_map.to_matrix_to_lin }, end /-- Shortcut lemma for `matrix.to_lin_mul` and `linear_map.comp_apply`. -/ lemma matrix.to_lin_mul_apply [decidable_eq m] (A : matrix l m R) (B : matrix m n R) (x) : matrix.to_lin v₁ v₃ (A ⬝ B) x = (matrix.to_lin v₂ v₃ A) (matrix.to_lin v₁ v₂ B x) := by rw [matrix.to_lin_mul v₁ v₂, linear_map.comp_apply] /-- If `M` and `M` are each other's inverse matrices, `matrix.to_lin M` and `matrix.to_lin M'` form a linear equivalence. -/ @[simps] def matrix.to_lin_of_inv [decidable_eq m] {M : matrix m n R} {M' : matrix n m R} (hMM' : M ⬝ M' = 1) (hM'M : M' ⬝ M = 1) : M₁ ≃ₗ[R] M₂ := { to_fun := matrix.to_lin v₁ v₂ M, inv_fun := matrix.to_lin v₂ v₁ M', left_inv := λ x, by rw [← matrix.to_lin_mul_apply, hM'M, matrix.to_lin_one, id_apply], right_inv := λ x, by rw [← matrix.to_lin_mul_apply, hMM', matrix.to_lin_one, id_apply], .. matrix.to_lin v₁ v₂ M } /-- Given a basis of a module `M₁` over a commutative ring `R`, we get an algebra equivalence between linear maps `M₁ →ₗ M₁` and square matrices over `R` indexed by the basis. -/ def linear_map.to_matrix_alg_equiv : (M₁ →ₗ[R] M₁) ≃ₐ[R] matrix n n R := alg_equiv.of_linear_equiv (linear_map.to_matrix v₁ v₁) (linear_map.to_matrix_mul v₁) (by simp [module.algebra_map_End_eq_smul_id, linear_map.to_matrix_id]) /-- Given a basis of a module `M₁` over a commutative ring `R`, we get an algebra equivalence between square matrices over `R` indexed by the basis and linear maps `M₁ →ₗ M₁`. -/ def matrix.to_lin_alg_equiv : matrix n n R ≃ₐ[R] (M₁ →ₗ[R] M₁) := (linear_map.to_matrix_alg_equiv v₁).symm @[simp] lemma linear_map.to_matrix_alg_equiv_symm : (linear_map.to_matrix_alg_equiv v₁).symm = matrix.to_lin_alg_equiv v₁ := rfl @[simp] lemma matrix.to_lin_alg_equiv_symm : (matrix.to_lin_alg_equiv v₁).symm = linear_map.to_matrix_alg_equiv v₁ := rfl @[simp] lemma matrix.to_lin_alg_equiv_to_matrix_alg_equiv (f : M₁ →ₗ[R] M₁) : matrix.to_lin_alg_equiv v₁ (linear_map.to_matrix_alg_equiv v₁ f) = f := by rw [← matrix.to_lin_alg_equiv_symm, alg_equiv.apply_symm_apply] @[simp] lemma linear_map.to_matrix_alg_equiv_to_lin_alg_equiv (M : matrix n n R) : linear_map.to_matrix_alg_equiv v₁ (matrix.to_lin_alg_equiv v₁ M) = M := by rw [← matrix.to_lin_alg_equiv_symm, alg_equiv.symm_apply_apply] lemma linear_map.to_matrix_alg_equiv_apply (f : M₁ →ₗ[R] M₁) (i j : n) : linear_map.to_matrix_alg_equiv v₁ f i j = v₁.repr (f (v₁ j)) i := by simp [linear_map.to_matrix_alg_equiv, linear_map.to_matrix_apply] lemma linear_map.to_matrix_alg_equiv_transpose_apply (f : M₁ →ₗ[R] M₁) (j : n) : (linear_map.to_matrix_alg_equiv v₁ f)ᵀ j = v₁.repr (f (v₁ j)) := funext $ λ i, f.to_matrix_apply _ _ i j lemma linear_map.to_matrix_alg_equiv_apply' (f : M₁ →ₗ[R] M₁) (i j : n) : linear_map.to_matrix_alg_equiv v₁ f i j = v₁.repr (f (v₁ j)) i := linear_map.to_matrix_alg_equiv_apply v₁ f i j lemma linear_map.to_matrix_alg_equiv_transpose_apply' (f : M₁ →ₗ[R] M₁) (j : n) : (linear_map.to_matrix_alg_equiv v₁ f)ᵀ j = v₁.repr (f (v₁ j)) := linear_map.to_matrix_alg_equiv_transpose_apply v₁ f j lemma matrix.to_lin_alg_equiv_apply (M : matrix n n R) (v : M₁) : matrix.to_lin_alg_equiv v₁ M v = ∑ j, M.mul_vec (v₁.repr v) j • v₁ j := show v₁.equiv_fun.symm (matrix.to_lin_alg_equiv' M (v₁.repr v)) = _, by rw [matrix.to_lin_alg_equiv'_apply, v₁.equiv_fun_symm_apply] @[simp] lemma matrix.to_lin_alg_equiv_self (M : matrix n n R) (i : n) : matrix.to_lin_alg_equiv v₁ M (v₁ i) = ∑ j, M j i • v₁ j := matrix.to_lin_self _ _ _ _ lemma linear_map.to_matrix_alg_equiv_id : linear_map.to_matrix_alg_equiv v₁ id = 1 := by simp_rw [linear_map.to_matrix_alg_equiv, alg_equiv.of_linear_equiv_apply, linear_map.to_matrix_id] @[simp] lemma matrix.to_lin_alg_equiv_one : matrix.to_lin_alg_equiv v₁ 1 = id := by rw [← linear_map.to_matrix_alg_equiv_id v₁, matrix.to_lin_alg_equiv_to_matrix_alg_equiv] theorem linear_map.to_matrix_alg_equiv_reindex_range [decidable_eq M₁] (f : M₁ →ₗ[R] M₁) (k i : n) : linear_map.to_matrix_alg_equiv v₁.reindex_range f ⟨v₁ k, mem_range_self k⟩ ⟨v₁ i, mem_range_self i⟩ = linear_map.to_matrix_alg_equiv v₁ f k i := by simp_rw [linear_map.to_matrix_alg_equiv_apply, basis.reindex_range_self, basis.reindex_range_repr] lemma linear_map.to_matrix_alg_equiv_comp (f g : M₁ →ₗ[R] M₁) : linear_map.to_matrix_alg_equiv v₁ (f.comp g) = linear_map.to_matrix_alg_equiv v₁ f ⬝ linear_map.to_matrix_alg_equiv v₁ g := by simp [linear_map.to_matrix_alg_equiv, linear_map.to_matrix_comp v₁ v₁ v₁ f g] lemma linear_map.to_matrix_alg_equiv_mul (f g : M₁ →ₗ[R] M₁) : linear_map.to_matrix_alg_equiv v₁ (f * g) = linear_map.to_matrix_alg_equiv v₁ f ⬝ linear_map.to_matrix_alg_equiv v₁ g := by { rw [show (@has_mul.mul (M₁ →ₗ[R] M₁) _) = linear_map.comp, from rfl, linear_map.to_matrix_alg_equiv_comp v₁ f g] } lemma matrix.to_lin_alg_equiv_mul (A B : matrix n n R) : matrix.to_lin_alg_equiv v₁ (A ⬝ B) = (matrix.to_lin_alg_equiv v₁ A).comp (matrix.to_lin_alg_equiv v₁ B) := by convert matrix.to_lin_mul v₁ v₁ v₁ A B end to_matrix namespace algebra section lmul variables {R S T : Type*} [comm_ring R] [comm_ring S] [comm_ring T] variables [algebra R S] [algebra S T] [algebra R T] [is_scalar_tower R S T] variables {m n : Type*} [fintype m] [decidable_eq m] [fintype n] [decidable_eq n] variables (b : basis m R S) (c : basis n S T) open algebra lemma to_matrix_lmul' (x : S) (i j) : linear_map.to_matrix b b (lmul R S x) i j = b.repr (x * b j) i := by rw [linear_map.to_matrix_apply', lmul_apply] @[simp] lemma to_matrix_lsmul (x : R) (i j) : linear_map.to_matrix b b (algebra.lsmul R S x) i j = if i = j then x else 0 := by { rw [linear_map.to_matrix_apply', algebra.lsmul_coe, linear_equiv.map_smul, finsupp.smul_apply, b.repr_self_apply, smul_eq_mul, mul_boole], congr' 1; simp only [eq_comm] } /-- `left_mul_matrix b x` is the matrix corresponding to the linear map `λ y, x * y`. `left_mul_matrix_eq_repr_mul` gives a formula for the entries of `left_mul_matrix`. This definition is useful for doing (more) explicit computations with `algebra.lmul`, such as the trace form or norm map for algebras. -/ noncomputable def left_mul_matrix : S →ₐ[R] matrix m m R := { to_fun := λ x, linear_map.to_matrix b b (algebra.lmul R S x), map_zero' := by rw [alg_hom.map_zero, linear_equiv.map_zero], map_one' := by rw [alg_hom.map_one, linear_map.to_matrix_one], map_add' := λ x y, by rw [alg_hom.map_add, linear_equiv.map_add], map_mul' := λ x y, by rw [alg_hom.map_mul, linear_map.to_matrix_mul, matrix.mul_eq_mul], commutes' := λ r, by { ext, rw [lmul_algebra_map, to_matrix_lsmul, algebra_map_matrix_apply, id.map_eq_self] } } lemma left_mul_matrix_apply (x : S) : left_mul_matrix b x = linear_map.to_matrix b b (lmul R S x) := rfl lemma left_mul_matrix_eq_repr_mul (x : S) (i j) : left_mul_matrix b x i j = b.repr (x * b j) i := -- This is defeq to just `to_matrix_lmul' b x i j`, -- but the unfolding goes a lot faster with this explicit `rw`. by rw [left_mul_matrix_apply, to_matrix_lmul' b x i j] lemma left_mul_matrix_mul_vec_repr (x y : S) : (left_mul_matrix b x).mul_vec (b.repr y) = b.repr (x * y) := linear_map.to_matrix_mul_vec_repr b b (algebra.lmul R S x) y @[simp] lemma to_matrix_lmul_eq (x : S) : linear_map.to_matrix b b (lmul R S x) = left_mul_matrix b x := rfl lemma left_mul_matrix_injective : function.injective (left_mul_matrix b) := λ x x' h, calc x = algebra.lmul R S x 1 : (mul_one x).symm ... = algebra.lmul R S x' 1 : by rw (linear_map.to_matrix b b).injective h ... = x' : mul_one x' lemma smul_left_mul_matrix (x) (ik jk) : left_mul_matrix (b.smul c) x ik jk = left_mul_matrix b (left_mul_matrix c x ik.2 jk.2) ik.1 jk.1 := by simp only [left_mul_matrix_apply, linear_map.to_matrix_apply, mul_comm, basis.smul_apply, basis.smul_repr, finsupp.smul_apply, algebra.lmul_apply, id.smul_eq_mul, linear_equiv.map_smul, mul_smul_comm] lemma smul_left_mul_matrix_algebra_map (x : S) : left_mul_matrix (b.smul c) (algebra_map _ _ x) = block_diagonal (λ k, left_mul_matrix b x) := begin ext ⟨i, k⟩ ⟨j, k'⟩, rw [smul_left_mul_matrix, alg_hom.commutes, block_diagonal_apply, algebra_map_matrix_apply], split_ifs with h; simp [h], end lemma smul_left_mul_matrix_algebra_map_eq (x : S) (i j k) : left_mul_matrix (b.smul c) (algebra_map _ _ x) (i, k) (j, k) = left_mul_matrix b x i j := by rw [smul_left_mul_matrix_algebra_map, block_diagonal_apply_eq] lemma smul_left_mul_matrix_algebra_map_ne (x : S) (i j) {k k'} (h : k ≠ k') : left_mul_matrix (b.smul c) (algebra_map _ _ x) (i, k) (j, k') = 0 := by rw [smul_left_mul_matrix_algebra_map, block_diagonal_apply_ne _ _ _ h] end lmul end algebra namespace linear_map section finite_dimensional open_locale classical variables {K : Type*} [field K] variables {V : Type*} [add_comm_group V] [module K V] [finite_dimensional K V] variables {W : Type*} [add_comm_group W] [module K W] [finite_dimensional K W] instance : finite_dimensional K (V →ₗ[K] W) := linear_equiv.finite_dimensional (linear_map.to_matrix (basis.of_vector_space K V) (basis.of_vector_space K W)).symm /-- The dimension of the space of linear transformations is the product of the dimensions of the domain and codomain. -/ @[simp] lemma finrank_linear_map : finite_dimensional.finrank K (V →ₗ[K] W) = (finite_dimensional.finrank K V) * (finite_dimensional.finrank K W) := begin let hbV := basis.of_vector_space K V, let hbW := basis.of_vector_space K W, rw [linear_equiv.finrank_eq (linear_map.to_matrix hbV hbW), matrix.finrank_matrix, finite_dimensional.finrank_eq_card_basis hbV, finite_dimensional.finrank_eq_card_basis hbW, mul_comm], end end finite_dimensional end linear_map /-- The natural equivalence between linear endomorphisms of finite free modules and square matrices is compatible with the algebra structures. -/ def alg_equiv_matrix' {R : Type v} [comm_ring R] {n : Type*} [fintype n] [decidable_eq n] : module.End R (n → R) ≃ₐ[R] matrix n n R := { map_mul' := linear_map.to_matrix'_comp, map_add' := linear_map.to_matrix'.map_add, commutes' := λ r, by { change (r • (linear_map.id : module.End R _)).to_matrix' = r • 1, rw ←linear_map.to_matrix'_id, refl, }, ..linear_map.to_matrix' } /-- A linear equivalence of two modules induces an equivalence of algebras of their endomorphisms. -/ def linear_equiv.alg_conj {R : Type v} [comm_ring R] {M₁ M₂ : Type*} [add_comm_group M₁] [module R M₁] [add_comm_group M₂] [module R M₂] (e : M₁ ≃ₗ[R] M₂) : module.End R M₁ ≃ₐ[R] module.End R M₂ := { map_mul' := λ f g, by apply e.arrow_congr_comp, map_add' := e.conj.map_add, commutes' := λ r, by { change e.conj (r • linear_map.id) = r • linear_map.id, rw [linear_equiv.map_smul, linear_equiv.conj_id], }, ..e.conj } /-- A basis of a module induces an equivalence of algebras from the endomorphisms of the module to square matrices. -/ def alg_equiv_matrix {R : Type v} {M : Type w} {n : Type*} [fintype n] [comm_ring R] [add_comm_group M] [module R M] [decidable_eq n] (h : basis n R M) : module.End R M ≃ₐ[R] matrix n n R := h.equiv_fun.alg_conj.trans alg_equiv_matrix'
c4dc7a67c54da6da167aac4aacbf58c448e56f81
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/fin_category.lean
e297f2ce5e26d2965a065d88f0abe9a8b8e872b8
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
2,384
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.fintype.basic import Mathlib.category_theory.discrete_category import Mathlib.PostPort universes u_1 v l namespace Mathlib /-! # Finite categories A category is finite in this sense if it has finitely many objects, and finitely many morphisms. ## Implementation We also ask for decidable equality of objects and morphisms, but it may be reasonable to just go classical in future. -/ namespace category_theory protected instance discrete_fintype {α : Type u_1} [fintype α] : fintype (discrete α) := id _inst_1 protected instance discrete_hom_fintype {α : Type u_1} [DecidableEq α] (X : discrete α) (Y : discrete α) : fintype (X ⟶ Y) := ulift.fintype (plift (X = Y)) /-- A category with a `fintype` of objects, and a `fintype` for each morphism space. -/ class fin_category (J : Type v) [small_category J] where decidable_eq_obj : autoParam (DecidableEq J) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.tactic.apply_instance") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "tactic") "apply_instance") []) fintype_obj : autoParam (fintype J) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.tactic.apply_instance") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "tactic") "apply_instance") []) decidable_eq_hom : autoParam ((j j' : J) → DecidableEq (j ⟶ j')) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.tactic.apply_instance") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "tactic") "apply_instance") []) fintype_hom : autoParam ((j j' : J) → fintype (j ⟶ j')) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.tactic.apply_instance") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "tactic") "apply_instance") []) -- We need a `decidable_eq` instance here to construct `fintype` on the morphism spaces. protected instance fin_category_discrete_of_decidable_fintype (J : Type v) [DecidableEq J] [fintype J] : fin_category (discrete J) := fin_category.mk
20c19552ca203f869c38127a1d641593c0b79fb7
159fed64bfae88f3b6a6166836d6278f953bcbf9
/Structure/Generic/Axioms/AbstractProducts.lean
663fbbf4f7720af9f953fe19f8be09f0c1cd31a7
[ "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
1,369
lean
import Structure.Generic.Axioms.Universes import Structure.Generic.Axioms.AbstractFunctors import mathlib4_experiments.Data.Equiv.Basic set_option autoBoundImplicitLocal false --set_option pp.universes true def ExternalProduct {U V : Universe} (α : U) (β : V) := PProd ⌈α⌉ ⌈β⌉ namespace ExternalProduct infixr:35 " ⊓' " => ExternalProduct end ExternalProduct class HasInternalProducts (U : Universe) [h : HasExternalFunctors U U] where (Prod : U → U → U) (prodEquiv (α β : U) : ⌈Prod α β⌉ ≃ (α ⊓' β)) (prodIntroIsFun {α β ω : U} (F : ω ⟶' α) (G : ω ⟶' β) : h.IsFun (λ a : ω => (prodEquiv α β).invFun ⟨F a, G a⟩)) (prodElimFstIsFun (α β : U) : h.IsFun (λ P : Prod α β => ((prodEquiv α β).toFun P).fst)) (prodElimSndIsFun (α β : U) : h.IsFun (λ P : Prod α β => ((prodEquiv α β).toFun P).snd)) namespace HasInternalProducts infixr:35 " ⊓ " => HasInternalProducts.Prod end HasInternalProducts -- TODO: Add a type class that says that the combination of intro and elim are equivalences (when the other side is fixed). -- Prove that this is an equivalence between U and U ⨯ U. -- TODO: Derive some logic and especially the arrow/product laws.
ca4a7da1ee8bd669a7c05e7587dcb190f2b89a46
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/blast_simp_subsingleton.lean
f6eb70d6b1abbf7700fc5bbdb4a5912dddf247a5
[ "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
1,036
lean
import data.unit open nat unit constant f {A : Type} (a : A) {B : Type} (b : B) : nat constant g : unit → nat example (a b : unit) : g a = g b := by simp example (a c : unit) (b d : nat) : b = d → f a b = f c d := by simp constant h {A B : Type} : A → B → nat example (a b c d : unit) : h a b = h c d := by simp definition C [reducible] : nat → Type₁ | nat.zero := unit | (nat.succ a) := nat constant g₂ : Π {n : nat}, C n → nat → nat example (a b : C zero) (c d : nat) : c = d → g₂ a c = g₂ b d := by simp example (n : nat) (h : zero = n) (a b : C n) (c d : nat) : c = d → g₂ a c = g₂ b d := by simp -- The following one cannot be solved as is -- example (a c : nat) (b d : unit) : a = c → b = d → f a b = f c d := -- by simp -- But, we can use the following trick definition f_aux {A B : Type} (a : A) (b : B) := f a b lemma to_f_aux [simp] {A B : Type} (a : A) (b : B) : f a b = f_aux a b := rfl example (a c : nat) (b d : unit) : a = c → b = d → f a b = f c d := by simp
60de56488eb5003d054bc9728f11898583516d60
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/match1.lean
4e973b2a1596f372e43c0fd8793bff49420cbf07
[ "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
3,855
lean
-- #print "---- h1" def h1 (b : Bool) : Nat := match b with | true => 0 | false => 10 #eval h1 false #print "---- h2" def h2 (x : List Nat) : Nat := match x with | [x1, x2] => x1 + x2 | x::xs => x | _ => 0 #eval h2 [1, 2] #eval h2 [10, 4, 5] #eval h2 [] #print "---- h3" def h3 (x : Array Nat) : Nat := match x with | #[x] => x | #[x, y] => x + y | xs => xs.size #eval h3 #[10] #eval h3 #[10, 20] #eval h3 #[10, 20, 30, 40] #print "---- inv" inductive Image {α β : Type} (f : α → β) : β → Type | mk (a : α) : Image f (f a) def mkImage {α β : Type} (f : α → β) (a : α) : Image f (f a) := Image.mk a def inv {α β : Type} {f : α → β} {b : β} (t : Image f b) : α := match b, t with | _, Image.mk a => a #eval inv (mkImage Nat.succ 10) theorem foo {p q} (h : p ∨ q) : q ∨ p := match h with | Or.inl h => Or.inr h | Or.inr h => Or.inl h def f (x : Nat × Nat) : Bool × Bool × Bool → Nat := match x with | (a, b) => fun _ => a structure S := (x y z : Nat := 0) def f1 : S → S := fun { x := x, ..} => { y := x } theorem ex2 : f1 { x := 10 } = { y := 10 } := rfl universe u inductive Vec (α : Type u) : Nat → Type u | nil : Vec α 0 | cons {n} (head : α) (tail : Vec α n) : Vec α (n+1) inductive VecPred {α : Type u} (P : α → Prop) : {n : Nat} → Vec α n → Prop | nil : VecPred P Vec.nil | cons {n : Nat} {head : α} {tail : Vec α n} : P head → VecPred P tail → VecPred P (Vec.cons head tail) theorem ex3 {α : Type u} (P : α → Prop) : {n : Nat} → (v : Vec α (n+1)) → VecPred P v → Exists P | _, Vec.cons head _, VecPred.cons h _ => ⟨head, h⟩ theorem ex4 {α : Type u} (P : α → Prop) : {n : Nat} → (v : Vec α (n+1)) → VecPred P v → Exists P | _, Vec.cons head _, VecPred.cons h (w : VecPred P Vec.nil) => ⟨head, h⟩ -- ERROR axiom someNat : Nat noncomputable def f2 (x : Nat) := -- must mark as noncomputable since it uses axiom `someNat` x + someNat inductive Parity : Nat -> Type | even (n) : Parity (n + n) | odd (n) : Parity (Nat.succ (n + n)) axiom nDiv2 (n : Nat) : n % 2 = 0 → n = n/2 + n/2 axiom nDiv2Succ (n : Nat) : n % 2 ≠ 0 → n = Nat.succ (n/2 + n/2) def parity (n : Nat) : Parity n := if h : n % 2 = 0 then Eq.ndrec (Parity.even (n/2)) (nDiv2 n h).symm else Eq.ndrec (Parity.odd (n/2)) (nDiv2Succ n h).symm partial def natToBin : (n : Nat) → List Bool | 0 => [] | n => match n, parity n with | _, Parity.even j => false :: natToBin j | _, Parity.odd j => true :: natToBin j #eval natToBin 6 partial def natToBin' : (n : Nat) → List Bool | 0 => [] | n => match parity n with | Parity.even j => false :: natToBin j | Parity.odd j => true :: natToBin j partial def natToBinBad (n : Nat) : List Bool := match n, parity n with | 0, _ => [] | _, Parity.even j => false :: natToBin j | _, Parity.odd j => true :: natToBin j partial def natToBin2 (n : Nat) : List Bool := match n, parity n with | _, Parity.even 0 => [] | _, Parity.even j => false :: natToBin j | _, Parity.odd j => true :: natToBin j #eval natToBin2 6 partial def natToBin2' (n : Nat) : List Bool := match parity n with | Parity.even 0 => [] | Parity.even j => false :: natToBin j | Parity.odd j => true :: natToBin j #check fun (a, b) => a -- Error type of pattern variable contains metavariables #check fun (a, b) => (a:Nat) + b #check fun (a, b) => a && b #check fun ((a : Nat), (b : Nat)) => a + b #check fun | some a, some b => some (a + b : Nat) | _, _ => none -- overapplied matcher #check fun x => (match x with | 0 => id | x+1 => id) x #check fun | #[1, 2] => 2 | #[] => 0 | #[3, 4, 5] => 3 | _ => 4 -- underapplied matcher def g {α} : List α → Nat | [a] => 1 | _ => 0 #check g.match_1 #check fun (e : Empty) => (nomatch e : False)
8f593a75df15defaf4dcd56be47ca06c9ec40205
fcf3ffa92a3847189ca669cb18b34ef6b2ec2859
/src/world1/level1.lean
7a77edcb50edf89af325508bc2b2a81e4788567b
[ "Apache-2.0" ]
permissive
nomoid/lean-proofs
4a80a97888699dee42b092b7b959b22d9aa0c066
b9f03a24623d1a1d111d6c2bbf53c617e2596d6a
refs/heads/master
1,674,955,317,080
1,607,475,706,000
1,607,475,706,000
314,104,281
0
0
null
null
null
null
UTF-8
Lean
false
false
178
lean
import mynat.definition import mynat.add import mynat.mul namespace mynat lemma example1 (x y z : mynat) : x * y + z = x * y + z := begin [nat_num_game] refl end end mynat
efc923b88c144b9e29ee371ca85cc7fdac41ff4f
e151e9053bfd6d71740066474fc500a087837323
/src/hott/types/pointed2.lean
7cf22e35915caaf430888b2c5b047e5b7209a00a
[ "Apache-2.0" ]
permissive
daniel-carranza/hott3
15bac2d90589dbb952ef15e74b2837722491963d
913811e8a1371d3a5751d7d32ff9dec8aa6815d9
refs/heads/master
1,610,091,349,670
1,596,222,336,000
1,596,222,336,000
241,957,822
0
0
Apache-2.0
1,582,222,839,000
1,582,222,838,000
null
UTF-8
Lean
false
false
43,752
lean
/- Copyright (c) 2017 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn More results about pointed types. Contains - squares of pointed maps, - equalities between pointed homotopies and - squares between pointed homotopies - pointed maps into and out of (ppmap A B), the pointed type of pointed maps from A to B -/ import ..eq2 .pointed .unit .bool .equiv ..algebra.bundled --algebra.homotopy_group universes u v w hott_theory namespace hott open hott.trunc /-hott.nat-/ is_trunc hott.equiv hott.is_equiv hott.bool hott.sigma --open hott.unit trunc nat group sigma function bool namespace pointed variables {A : Type*} {B : Type*} {C : Type*} {P : A → Type _} {p₀ : P pt} {k k' l m n : ppi P p₀} @[hott] def punit_pmap_phomotopy {A : Type*} (f : unit* →* A) : f ~* pconst unit* A := phomotopy_of_is_contr_dom _ _ @[hott] def punit_ppi (P : unit* → Type) (p₀ : P ⋆) : ppi P p₀ := begin fapply ppi.mk, intro u, induction u, exact p₀, refl end @[hott] def punit_ppi_phomotopy {P : unit* → Type} {p₀ : P ⋆} (f : ppi P p₀) : f ~* punit_ppi P p₀ := phomotopy_of_is_contr_dom _ _ @[hott] def is_contr_punit_ppi (P : unit* → Type) (p₀ : P ⋆) : is_contr (ppi P p₀) := is_contr.mk (punit_ppi P p₀) (λf, eq_of_phomotopy (punit_ppi_phomotopy f)⁻¹*) @[hott] def is_contr_punit_pmap (A : Type*) : is_contr (unit* →* A) := is_contr_punit_ppi _ _ -- @[hott] def phomotopy_eq_equiv (h₁ h₂ : k ~* l) : -- (h₁ = h₂) ≃ Σ(p : to_homotopy h₁ ~ to_homotopy h₂), -- whisker_right (respect_pt l) (p pt) ⬝ to_homotopy_pt h₂ = to_homotopy_pt h₁ := -- begin -- refine ppi_eq_equiv _ _ ⬝e phomotopy.sigma_char _ _ ⬝e sigma_equiv_sigma_right _, -- intro p, -- end /- Short term TODO: generalize to dependent maps (use ppi_eq_equiv?) Long term TODO: use homotopies between pointed homotopies, not equalities -/ @[hott] def phomotopy_eq_equiv {A B : Type*} {f g : A →* B} (h k : f ~* g) : (h = k) ≃ Σ(p : to_homotopy h ~ to_homotopy k), whisker_right (respect_pt g) (p pt) ⬝ to_homotopy_pt k = to_homotopy_pt h := sorry -- calc -- h = k ≃ phomotopy.sigma_char f g h = phomotopy.sigma_char f g k -- : eq_equiv_fn_eq (phomotopy.sigma_char f g) h k -- ... ≃ Σ(p : to_homotopy h = to_homotopy k), -- to_homotopy_pt h =[p; λ(q : to_homotopy h = to_homotopy k), q pt ⬝ respect_pt g = respect_pt f] to_homotopy_pt k -- : sigma_eq_equiv _ _ -- ... ≃ Σ(p : to_homotopy h = to_homotopy k), -- to_homotopy_pt h = ap (λq, q pt ⬝ respect_pt g) p ⬝ to_homotopy_pt k -- : sigma_equiv_sigma_right (λp, eq_pathover_equiv_Fl p (to_homotopy_pt h) (to_homotopy_pt k)) -- ... ≃ Σ(p : to_homotopy h = to_homotopy k), -- ap (λq, q pt ⬝ respect_pt g) p ⬝ to_homotopy_pt k = to_homotopy_pt h -- : sigma_equiv_sigma_right (λp, eq_equiv_eq_symm _ _) -- ... ≃ Σ(p : to_homotopy h = to_homotopy k), -- whisker_right (respect_pt g) (apd10 p pt) ⬝ to_homotopy_pt k = to_homotopy_pt h -- : sigma_equiv_sigma_right (λp, equiv_eq_closed_left _ (whisker_right _ )(whisker_right_ap _ _)⁻¹)) -- ... ≃ Σ(p : to_homotopy h ~ to_homotopy k), -- whisker_right (respect_pt g) (p pt) ⬝ to_homotopy_pt k = to_homotopy_pt h -- : sigma_equiv_sigma_left' eq_equiv_homotopy @[hott] def phomotopy_eq {A B : Type*} {f g : A →* B} {h k : f ~* g} (p : to_homotopy h ~ to_homotopy k) (q : whisker_right (respect_pt g) (p pt) ⬝ to_homotopy_pt k = to_homotopy_pt h) : h = k := to_inv (phomotopy_eq_equiv h k) ⟨p, q⟩ @[hott] def phomotopy_eq' {A B : Type*} {f g : A →* B} {h k : f ~* g} (p : to_homotopy h ~ to_homotopy k) (q : square (to_homotopy_pt h) (to_homotopy_pt k) (whisker_right (respect_pt g) (p pt)) idp) : h = k := phomotopy_eq p (eq_of_square q)⁻¹ @[hott, hsimp] def trans_refl (p : k ~* l) : p ⬝* phomotopy.rfl = p := begin induction A with A a₀, induction k with k k₀, induction l with l l₀, induction p with p p₀', dsimp at *, induction l₀, dsimp at p₀', induction p₀', refl end @[hott] def eq_of_phomotopy_trans {X Y : Type*} {f g h : X →* Y} (p : f ~* g) (q : g ~* h) : eq_of_phomotopy (p ⬝* q) = eq_of_phomotopy p ⬝ eq_of_phomotopy q := begin hinduction p using phomotopy_rec_idp, hinduction q using phomotopy_rec_idp, exact ap eq_of_phomotopy (trans_refl _) ⬝ whisker_left _ (eq_of_phomotopy_refl _)⁻¹ᵖ end @[hott] def refl_trans (p : k ~* l) : phomotopy.rfl ⬝* p = p := begin hinduction p using phomotopy_rec_idp, apply trans_refl end @[hott] def trans_assoc (p : k ~* l) (q : l ~* m) (r : m ~* n) : p ⬝* q ⬝* r = p ⬝* (q ⬝* r) := begin hinduction r using phomotopy_rec_idp, hinduction q using phomotopy_rec_idp, hinduction p using phomotopy_rec_idp, induction k with k k₀, induction k₀, refl end @[hott] def refl_symm : phomotopy.rfl⁻¹* = phomotopy.refl k := begin induction k with k k₀, induction k₀, refl end @[hott] def symm_symm (p : k ~* l) : p⁻¹*⁻¹* = p := begin hinduction p using phomotopy_rec_idp, induction k with k k₀, induction k₀, refl end @[hott] def trans_right_inv (p : k ~* l) : p ⬝* p⁻¹* = phomotopy.rfl := begin hinduction p using phomotopy_rec_idp, exact refl_trans _ ⬝ refl_symm end @[hott] def trans_left_inv (p : k ~* l) : p⁻¹* ⬝* p = phomotopy.rfl := begin hinduction p using phomotopy_rec_idp, exact trans_refl _ ⬝ refl_symm end @[hott] def trans2 {p p' : k ~* l} {q q' : l ~* m} (r : p = p') (s : q = q') : p ⬝* q = p' ⬝* q' := ap011 phomotopy.trans r s @[hott] def pcompose3 {A B C : Type*} {g g' : B →* C} {f f' : A →* B} {p p' : g ~* g'} {q q' : f ~* f'} (r : p = p') (s : q = q') : p ◾* q = p' ◾* q' := ap011 pcompose2 r s @[hott] def symm2 {p p' : k ~* l} (r : p = p') : p⁻¹* = p'⁻¹* := ap phomotopy.symm r infixl ` ◾** `:80 := pointed.trans2 infixl ` ◽* `:81 := pointed.pcompose3 postfix `⁻²**`:(max+1) := pointed.symm2 @[hott] def trans_symm (p : k ~* l) (q : l ~* m) : (p ⬝* q)⁻¹* = q⁻¹* ⬝* p⁻¹* := begin hinduction p using phomotopy_rec_idp, hinduction q using phomotopy_rec_idp, exact (trans_refl _)⁻²** ⬝ (trans_refl _)⁻¹ ⬝ idp ◾** refl_symm⁻¹ end @[hott] def phwhisker_left (p : k ~* l) {q q' : l ~* m} (s : q = q') : p ⬝* q = p ⬝* q' := idp ◾** s @[hott] def phwhisker_right {p p' : k ~* l} (q : l ~* m) (r : p = p') : p ⬝* q = p' ⬝* q := r ◾** idp @[hott, hsimp] def pwhisker_left_refl {A B C : Type*} (g : B →* C) (f : A →* B) : pwhisker_left g (phomotopy.refl f) = phomotopy.refl (g ∘* f) := begin induction A with A a₀, induction B with B b₀, induction C with C c₀, induction f with f f₀, induction g with g g₀, dsimp at *, induction g₀, induction f₀, refl end @[hott, hsimp] def pwhisker_right_refl {A B C : Type*} (f : A →* B) (g : B →* C) : pwhisker_right f (phomotopy.refl g) = phomotopy.refl (g ∘* f) := begin induction A with A a₀, induction B with B b₀, induction C with C c₀, induction f with f f₀, induction g with g g₀, dsimp at *, induction g₀, induction f₀, refl end @[hott] def pcompose2_refl {A B C : Type*} (g : B →* C) (f : A →* B) : phomotopy.refl g ◾* phomotopy.refl f = phomotopy.rfl := pwhisker_right_refl _ _ ◾** pwhisker_left_refl _ _ ⬝ refl_trans _ @[hott] def pcompose2_refl_left {A B C : Type*} (g : B →* C) {f f' : A →* B} (p : f ~* f') : phomotopy.rfl ◾* p = pwhisker_left g p := pwhisker_right_refl _ _ ◾** idp ⬝ refl_trans _ @[hott] def pcompose2_refl_right {A B C : Type*} {g g' : B →* C} (f : A →* B) (p : g ~* g') : p ◾* phomotopy.rfl = pwhisker_right f p := idp ◾** pwhisker_left_refl _ _ ⬝ trans_refl _ @[hott] def pwhisker_left_trans {A B C : Type*} (g : B →* C) {f₁ f₂ f₃ : A →* B} (p : f₁ ~* f₂) (q : f₂ ~* f₃) : pwhisker_left g (p ⬝* q) = pwhisker_left g p ⬝* pwhisker_left g q := begin hinduction p using phomotopy_rec_idp, hinduction q using phomotopy_rec_idp, refine _ ⬝ (pwhisker_left_refl _ _)⁻¹ ◾** (pwhisker_left_refl _ _)⁻¹, refine ap (pwhisker_left g) (trans_refl _) ⬝ pwhisker_left_refl _ _ ⬝ (trans_refl _)⁻¹ end @[hott] def pwhisker_right_trans {A B C : Type*} (f : A →* B) {g₁ g₂ g₃ : B →* C} (p : g₁ ~* g₂) (q : g₂ ~* g₃) : pwhisker_right f (p ⬝* q) = pwhisker_right f p ⬝* pwhisker_right f q := begin hinduction p using phomotopy_rec_idp, hinduction q using phomotopy_rec_idp, refine _ ⬝ (pwhisker_right_refl _ _)⁻¹ ◾** (pwhisker_right_refl _ _)⁻¹, refine ap (pwhisker_right f) (trans_refl _) ⬝ pwhisker_right_refl _ _ ⬝ (trans_refl _)⁻¹ end @[hott] def pwhisker_left_symm {A B C : Type*} (g : B →* C) {f₁ f₂ : A →* B} (p : f₁ ~* f₂) : pwhisker_left g p⁻¹* = (pwhisker_left g p)⁻¹* := begin hinduction p using phomotopy_rec_idp, refine _ ⬝ ap phomotopy.symm (pwhisker_left_refl _ _)⁻¹ᵖ, refine ap (pwhisker_left g) refl_symm ⬝ pwhisker_left_refl _ _ ⬝ refl_symm⁻¹ end @[hott] def pwhisker_right_symm {A B C : Type*} (f : A →* B) {g₁ g₂ : B →* C} (p : g₁ ~* g₂) : pwhisker_right f p⁻¹* = (pwhisker_right f p)⁻¹* := begin hinduction p using phomotopy_rec_idp, refine _ ⬝ ap phomotopy.symm (pwhisker_right_refl _ _)⁻¹ᵖ, refine ap (pwhisker_right f) refl_symm ⬝ pwhisker_right_refl _ _ ⬝ refl_symm⁻¹ end @[hott] def trans_eq_of_eq_symm_trans {p : k ~* l} {q : l ~* m} {r : k ~* m} (s : q = p⁻¹* ⬝* r) : p ⬝* q = r := idp ◾** s ⬝ (trans_assoc _ _ _)⁻¹ ⬝ trans_right_inv p ◾** idp ⬝ refl_trans _ @[hott] def eq_symm_trans_of_trans_eq {p : k ~* l} {q : l ~* m} {r : k ~* m} (s : p ⬝* q = r) : q = p⁻¹* ⬝* r := (refl_trans _)⁻¹ ⬝ (trans_left_inv _)⁻¹ ◾** idp ⬝ trans_assoc _ _ _ ⬝ idp ◾** s @[hott] def trans_eq_of_eq_trans_symm {p : k ~* l} {q : l ~* m} {r : k ~* m} (s : p = r ⬝* q⁻¹*) : p ⬝* q = r := s ◾** idp ⬝ trans_assoc _ _ _ ⬝ idp ◾** trans_left_inv q ⬝ trans_refl _ @[hott] def eq_trans_symm_of_trans_eq {p : k ~* l} {q : l ~* m} {r : k ~* m} (s : p ⬝* q = r) : p = r ⬝* q⁻¹* := (trans_refl _)⁻¹ ⬝ idp ◾** (trans_right_inv _)⁻¹ ⬝ (trans_assoc _ _ _)⁻¹ ⬝ s ◾** idp @[hott] def eq_trans_of_symm_trans_eq {p : k ~* l} {q : l ~* m} {r : k ~* m} (s : p⁻¹* ⬝* r = q) : r = p ⬝* q := (refl_trans _)⁻¹ ⬝ (trans_right_inv _)⁻¹ ◾** idp ⬝ trans_assoc _ _ _ ⬝ idp ◾** s @[hott] def symm_trans_eq_of_eq_trans {p : k ~* l} {q : l ~* m} {r : k ~* m} (s : r = p ⬝* q) : p⁻¹* ⬝* r = q := idp ◾** s ⬝ (trans_assoc _ _ _)⁻¹ ⬝ trans_left_inv p ◾** idp ⬝ refl_trans _ @[hott] def eq_trans_of_trans_symm_eq {p : k ~* l} {q : l ~* m} {r : k ~* m} (s : r ⬝* q⁻¹* = p) : r = p ⬝* q := (trans_refl _)⁻¹ ⬝ idp ◾** (trans_left_inv _)⁻¹ ⬝ (trans_assoc _ _ _)⁻¹ ⬝ s ◾** idp @[hott] def trans_symm_eq_of_eq_trans {p : k ~* l} {q : l ~* m} {r : k ~* m} (s : r = p ⬝* q) : r ⬝* q⁻¹* = p := s ◾** idp ⬝ trans_assoc _ _ _ ⬝ idp ◾** trans_right_inv q ⬝ trans_refl _ section phsquare /- Squares of pointed homotopies -/ variables {f f' f₀₀ f₂₀ f₄₀ f₀₂ f₂₂ f₄₂ f₀₄ f₂₄ f₄₄ : ppi P p₀} {p₁₀ : f₀₀ ~* f₂₀} {p₃₀ : f₂₀ ~* f₄₀} {p₀₁ : f₀₀ ~* f₀₂} {p₂₁ : f₂₀ ~* f₂₂} {p₄₁ : f₄₀ ~* f₄₂} {p₁₂ : f₀₂ ~* f₂₂} {p₃₂ : f₂₂ ~* f₄₂} {p₀₃ : f₀₂ ~* f₀₄} {p₂₃ : f₂₂ ~* f₂₄} {p₄₃ : f₄₂ ~* f₄₄} {p₁₄ : f₀₄ ~* f₂₄} {p₃₄ : f₂₄ ~* f₄₄} @[hott, reducible] def phsquare (p₁₀ : f₀₀ ~* f₂₀) (p₁₂ : f₀₂ ~* f₂₂) (p₀₁ : f₀₀ ~* f₀₂) (p₂₁ : f₂₀ ~* f₂₂) : Type _ := p₁₀ ⬝* p₂₁ = p₀₁ ⬝* p₁₂ @[hott] def phsquare_of_eq (p : p₁₀ ⬝* p₂₁ = p₀₁ ⬝* p₁₂) : phsquare p₁₀ p₁₂ p₀₁ p₂₁ := p @[hott] def eq_of_phsquare (p : phsquare p₁₀ p₁₂ p₀₁ p₂₁) : p₁₀ ⬝* p₂₁ = p₀₁ ⬝* p₁₂ := p -- @[hott] def phsquare.mk (p : Πx, square (p₁₀ x) (p₁₂ x) (p₀₁ x) (p₂₁ x)) -- (q : cube (square_of_eq (to_homotopy_pt p₁₀)) (square_of_eq (to_homotopy_pt p₁₂)) -- (square_of_eq (to_homotopy_pt p₀₁)) (square_of_eq (to_homotopy_pt p₂₁)) -- (p pt) ids) : phsquare p₁₀ p₁₂ p₀₁ p₂₁ := -- begin -- fapply phomotopy_eq, -- { intro x, apply eq_of_square (p x) }, -- { generalize p pt, intro r, exact sorry } -- end @[hott] def phhconcat (p : phsquare p₁₀ p₁₂ p₀₁ p₂₁) (q : phsquare p₃₀ p₃₂ p₂₁ p₄₁) : phsquare (p₁₀ ⬝* p₃₀) (p₁₂ ⬝* p₃₂) p₀₁ p₄₁ := trans_assoc _ _ _ ⬝ idp ◾** q ⬝ (trans_assoc _ _ _)⁻¹ ⬝ p ◾** idp ⬝ trans_assoc _ _ _ @[hott] def phvconcat (p : phsquare p₁₀ p₁₂ p₀₁ p₂₁) (q : phsquare p₁₂ p₁₄ p₀₃ p₂₃) : phsquare p₁₀ p₁₄ (p₀₁ ⬝* p₀₃) (p₂₁ ⬝* p₂₃) := (phhconcat p⁻¹ q⁻¹)⁻¹ @[hott] def phhdeg_square {p₁ p₂ : f ~* f'} (q : p₁ = p₂) : phsquare phomotopy.rfl phomotopy.rfl p₁ p₂ := refl_trans _ ⬝ q⁻¹ ⬝ (trans_refl _)⁻¹ @[hott] def phvdeg_square {p₁ p₂ : f ~* f'} (q : p₁ = p₂) : phsquare p₁ p₂ phomotopy.rfl phomotopy.rfl := trans_refl _ ⬝ q ⬝ (refl_trans _)⁻¹ variables (p₀₁ p₁₀) @[hott] def phhrefl : phsquare phomotopy.rfl phomotopy.rfl p₀₁ p₀₁ := phhdeg_square idp @[hott] def phvrefl : phsquare p₁₀ p₁₀ phomotopy.rfl phomotopy.rfl := phvdeg_square idp variables {p₀₁ p₁₀} @[hott] def phhrfl : phsquare phomotopy.rfl phomotopy.rfl p₀₁ p₀₁ := phhrefl p₀₁ @[hott] def phvrfl : phsquare p₁₀ p₁₀ phomotopy.rfl phomotopy.rfl := phvrefl p₁₀ /- The names are very baroque. The following stands for "pointed homotopy path-horizontal composition" (i.e. composition on the left with a path) The names are obtained by using the ones for squares, and putting "ph" in front of it. In practice, use the notation ⬝ph** defined below, which might be easier to remember -/ @[hott] def phphconcat {p₀₁'} (p : p₀₁' = p₀₁) (q : phsquare p₁₀ p₁₂ p₀₁ p₂₁) : phsquare p₁₀ p₁₂ p₀₁' p₂₁ := by induction p; exact q @[hott] def phhpconcat {p₂₁'} (q : phsquare p₁₀ p₁₂ p₀₁ p₂₁) (p : p₂₁ = p₂₁') : phsquare p₁₀ p₁₂ p₀₁ p₂₁' := by induction p; exact q @[hott] def phpvconcat {p₁₀'} (p : p₁₀' = p₁₀) (q : phsquare p₁₀ p₁₂ p₀₁ p₂₁) : phsquare p₁₀' p₁₂ p₀₁ p₂₁ := by induction p; exact q @[hott] def phvpconcat {p₁₂'} (q : phsquare p₁₀ p₁₂ p₀₁ p₂₁) (p : p₁₂ = p₁₂') : phsquare p₁₀ p₁₂' p₀₁ p₂₁ := by induction p; exact q @[hott] def phhinverse (p : phsquare p₁₀ p₁₂ p₀₁ p₂₁) : phsquare p₁₀⁻¹* p₁₂⁻¹* p₂₁ p₀₁ := begin refine (eq_symm_trans_of_trans_eq _)⁻¹, refine (trans_assoc _ _ _)⁻¹ ⬝ _, refine (eq_trans_symm_of_trans_eq _)⁻¹, exact (eq_of_phsquare p)⁻¹ end @[hott] def phvinverse (p : phsquare p₁₀ p₁₂ p₀₁ p₂₁) : phsquare p₁₂ p₁₀ p₀₁⁻¹* p₂₁⁻¹* := (phhinverse p⁻¹)⁻¹ infix ` ⬝h** `:78 := phhconcat infix ` ⬝v** `:78 := phvconcat infixr ` ⬝ph** `:77 := phphconcat infixl ` ⬝hp** `:77 := phhpconcat infixr ` ⬝pv** `:77 := phpvconcat infixl ` ⬝vp** `:77 := phvpconcat postfix `⁻¹ʰ**`:(max+1) := phhinverse postfix `⁻¹ᵛ**`:(max+1) := phvinverse @[hott] def phwhisker_rt (p : f ~* f₂₀) (q : phsquare p₁₀ p₁₂ p₀₁ p₂₁) : phsquare (p₁₀ ⬝* p⁻¹*) p₁₂ p₀₁ (p ⬝* p₂₁) := trans_assoc _ _ _ ⬝ idp ◾** ((trans_assoc _ _ _)⁻¹ ⬝ trans_left_inv _ ◾** idp ⬝ refl_trans _) ⬝ q @[hott] def phwhisker_br (p : f₂₂ ~* f) (q : phsquare p₁₀ p₁₂ p₀₁ p₂₁) : phsquare p₁₀ (p₁₂ ⬝* p) p₀₁ (p₂₁ ⬝* p) := (trans_assoc _ _ _)⁻¹ ⬝ q ◾** idp ⬝ trans_assoc _ _ _ @[hott] def phmove_top_of_left' {p₀₁ : f ~* f₀₂} (p : f₀₀ ~* f) (q : phsquare p₁₀ p₁₂ (p ⬝* p₀₁) p₂₁) : phsquare (p⁻¹* ⬝* p₁₀) p₁₂ p₀₁ p₂₁ := trans_assoc _ _ _ ⬝ (eq_symm_trans_of_trans_eq (q ⬝ (trans_assoc _ _ _))⁻¹)⁻¹ @[hott] def phmove_bot_of_left {p₀₁ : f₀₀ ~* f} (p : f ~* f₀₂) (q : phsquare p₁₀ p₁₂ (p₀₁ ⬝* p) p₂₁) : phsquare p₁₀ (p ⬝* p₁₂) p₀₁ p₂₁ := q ⬝ trans_assoc _ _ _ @[hott] def passoc_phomotopy_right {A B C D : Type*} (h : C →* D) (g : B →* C) {f f' : A →* B} (p : f ~* f') : phsquare (passoc h g f) (passoc h g f') (pwhisker_left (h ∘* g) p) (pwhisker_left h (pwhisker_left g p)) := begin hinduction p using phomotopy_rec_idp, refine idp ◾** (ap (pwhisker_left h) (pwhisker_left_refl _ _) ⬝ pwhisker_left_refl _ _) ⬝ _ ⬝ (pwhisker_left_refl _ _)⁻¹ ◾** idp, exact trans_refl _ ⬝ (refl_trans _)⁻¹ end @[hott] theorem passoc_phomotopy_middle {A B C D : Type*} (h : C →* D) {g g' : B →* C} (f : A →* B) (p : g ~* g') : phsquare (passoc h g f) (passoc h g' f) (pwhisker_right f (pwhisker_left h p)) (pwhisker_left h (pwhisker_right f p)) := begin hinduction p using phomotopy_rec_idp, rwr [pwhisker_right_refl, pwhisker_left_refl], rwr [pwhisker_right_refl, pwhisker_left_refl], exact phvrfl end @[hott] def pwhisker_right_pwhisker_left {A B C : Type*} {g g' : B →* C} {f f' : A →* B} (p : g ~* g') (q : f ~* f') : phsquare (pwhisker_right f p) (pwhisker_right f' p) (pwhisker_left g q) (pwhisker_left g' q) := begin hinduction p using phomotopy_rec_idp, hinduction q using phomotopy_rec_idp, exact pwhisker_right_refl _ _ ◾** pwhisker_left_refl _ _ ⬝ (pwhisker_left_refl _ _)⁻¹ ◾** (pwhisker_right_refl _ _)⁻¹ end end phsquare section nondep_phsquare variables {f f' f₀₀ f₂₀ f₀₂ f₂₂ : A →* B} {p₁₀ : f₀₀ ~* f₂₀} {p₀₁ : f₀₀ ~* f₀₂} {p₂₁ : f₂₀ ~* f₂₂} {p₁₂ : f₀₂ ~* f₂₂} @[hott] def pwhisker_left_phsquare (f : B →* C) (p : phsquare p₁₀ p₁₂ p₀₁ p₂₁) : phsquare (pwhisker_left f p₁₀) (pwhisker_left f p₁₂) (pwhisker_left f p₀₁) (pwhisker_left f p₂₁) := (pwhisker_left_trans _ _ _)⁻¹ ⬝ ap (pwhisker_left f) p ⬝ pwhisker_left_trans _ _ _ @[hott] def pwhisker_right_phsquare (f : C →* A) (p : phsquare p₁₀ p₁₂ p₀₁ p₂₁) : phsquare (pwhisker_right f p₁₀) (pwhisker_right f p₁₂) (pwhisker_right f p₀₁) (pwhisker_right f p₂₁) := (pwhisker_right_trans _ _ _)⁻¹ ⬝ ap (pwhisker_right f) p ⬝ pwhisker_right_trans _ _ _ end nondep_phsquare @[hott] def phomotopy_of_eq_con (p : k = l) (q : l = m) : phomotopy_of_eq (p ⬝ q) = phomotopy_of_eq p ⬝* phomotopy_of_eq q := begin induction q, induction p, symmetry, apply trans_refl end @[hott] def pcompose_left_eq_of_phomotopy {A B C : Type*} (g : B →* C) {f f' : A →* B} (H : f ~* f') : ap (λf, g ∘* f) (eq_of_phomotopy H) = eq_of_phomotopy (pwhisker_left g H) := begin hinduction H using phomotopy_rec_idp, refine ap02 _ (eq_of_phomotopy_refl _) ⬝ (eq_of_phomotopy_refl _)⁻¹ ⬝ ap eq_of_phomotopy _, exact (pwhisker_left_refl _ _)⁻¹ end @[hott] def pcompose_right_eq_of_phomotopy {A B C : Type*} {g g' : B →* C} (f : A →* B) (H : g ~* g') : ap (λg, g ∘* f) (eq_of_phomotopy H) = eq_of_phomotopy (pwhisker_right f H) := begin hinduction H using phomotopy_rec_idp, refine ap02 _ (eq_of_phomotopy_refl _) ⬝ (eq_of_phomotopy_refl _)⁻¹ ⬝ ap eq_of_phomotopy _, exact (pwhisker_right_refl _ _)⁻¹ end @[hott] def phomotopy_of_eq_pcompose_left {A B C : Type*} (g : B →* C) {f f' : A →* B} (p : f = f') : phomotopy_of_eq (ap (λf, g ∘* f) p) = pwhisker_left g (phomotopy_of_eq p) := begin induction p, exact (pwhisker_left_refl _ _)⁻¹ end @[hott] def phomotopy_of_eq_pcompose_right {A B C : Type*} {g g' : B →* C} (f : A →* B) (p : g = g') : phomotopy_of_eq (ap (λg, g ∘* f) p) = pwhisker_right f (phomotopy_of_eq p) := begin induction p, exact (pwhisker_right_refl _ _)⁻¹ end @[hott] def phomotopy_mk_ppmap {A B C : Type*} {f g : A →* ppmap B C} (p : Πa, f a ~* g a) (q : p pt ⬝* phomotopy_of_eq (respect_pt g) = phomotopy_of_eq (respect_pt f)) : f ~* g := begin apply phomotopy.mk (λa, eq_of_phomotopy (p a)), apply eq_of_fn_eq_fn (pmap_eq_equiv _ _), refine phomotopy_of_eq_con _ _ ⬝ _, refine phomotopy_of_eq_of_phomotopy _ ◾** idp ⬝ q, end /- properties of ppmap, the pointed type of pointed maps -/ @[hott] def pcompose_pconst (f : B →* C) : f ∘* pconst A B ~* pconst A C := phomotopy.mk (λa, respect_pt f) (idp_con _)⁻¹ @[hott] def pconst_pcompose (f : A →* B) : pconst B C ∘* f ~* pconst A C := phomotopy.mk (λa, rfl) (ap_constant _ _)⁻¹ @[hott] def ppcompose_left (g : B →* C) : ppmap A B →* ppmap A C := pmap.mk (pcompose g) (eq_of_phomotopy (pcompose_pconst g)) @[hott] def ppcompose_right (f : A →* B) : ppmap B C →* ppmap A C := pmap.mk (λg, g ∘* f) (eq_of_phomotopy (pconst_pcompose f)) /- TODO: give construction using pequiv.MK, which computes better (see comment for a start of the proof), rename to ppmap_pequiv_ppmap_right -/ @[hott] def pequiv_ppcompose_left (g : B ≃* C) : ppmap A B ≃* ppmap A C := pequiv.MK' (ppcompose_left g.to_pmap) (ppcompose_left g⁻¹ᵉ*.to_pmap).to_fun begin intro f, apply eq_of_phomotopy, apply pinv_pcompose_cancel_left end begin intro f, apply eq_of_phomotopy, apply pcompose_pinv_cancel_left end -- pequiv.MK (ppcompose_left g) (ppcompose_left g⁻¹ᵉ*) -- abstract begin -- apply phomotopy_mk_ppmap (pinv_pcompose_cancel_left g), esimp, -- refine trans_refl _ ⬝ _, -- refine _ ⬝ (phomotopy_of_eq_con _ _ ⬝ (phomotopy_of_eq_pcompose_left _ _ ⬝ -- ap (pwhisker_left _) (phomotopy_of_eq_of_phomotopy _)) ◾** (phomotopy_of_eq_of_phomotopy _))⁻¹, -- end end -- abstract begin -- exact sorry -- end end @[hott] def pequiv_ppcompose_right (f : A ≃* B) : ppmap B C ≃* ppmap A C := begin fapply pequiv.MK', { exact ppcompose_right f.to_pmap }, { exact (ppcompose_right f⁻¹ᵉ*.to_pmap).to_fun }, { intro g, apply eq_of_phomotopy, apply pcompose_pinv_cancel_right }, { intro g, apply eq_of_phomotopy, apply pinv_pcompose_cancel_right }, end @[hott] def loop_ppmap_commute (A B : Type*) : Ω(ppmap A B) ≃* (ppmap A (Ω B)) := pequiv_of_equiv (calc Ω(ppmap A B) ≃ (pconst A B ~* pconst A B) : pmap_eq_equiv _ _ ... ≃ Σ(p : pconst A B ~ pconst A B), p pt ⬝ rfl = rfl : phomotopy.sigma_char _ _ ... ≃ (A →* Ω B) : pmap.sigma_char⁻¹ᵉ) (by refl) @[hott] def papply {A : Type*} (B : Type*) (a : A) : ppmap A B →* B := pmap.mk (λ(f : A →* B), f a) idp @[hott] def papply_pcompose {A : Type*} (B : Type*) (a : A) : ppmap A B →* B := pmap.mk (λ(f : A →* B), f a) idp @[hott] def ppmap_pbool_pequiv (B : Type*) : ppmap bool* B ≃* B := begin fapply pequiv.MK', { exact papply B tt }, { exact pbool_pmap }, { intro f, fapply eq_of_phomotopy, fapply phomotopy.mk, { intro b, cases b, exact (respect_pt _)⁻¹ᵖ, refl }, { exact con.left_inv _ }}, { intro b, refl }, end @[hott] def papn_pt (n : ℕ) (A B : Type*) : ppmap A B →* ppmap (Ω[n] A) (Ω[n] B) := pmap.mk (λf, apn n f) (eq_of_phomotopy (apn_pconst _ _ _)) @[hott] def papn_fun {n : ℕ} {A : Type*} (B : Type*) (p : Ω[n] A) : ppmap A B →* Ω[n] B := papply _ p ∘* papn_pt n A B @[hott] def pconst_pcompose_pconst (A B C : Type*) : pconst_pcompose (pconst A B) = pcompose_pconst (pconst B C) := idp @[hott] def pconst_pcompose_phomotopy_pconst {A B C : Type*} {f : A →* B} (p : f ~* pconst A B) : pconst_pcompose f = pwhisker_left (pconst B C) p ⬝* pcompose_pconst (pconst B C) := begin have H : Π(p : pconst A B ~* f), pconst_pcompose f = pwhisker_left (pconst B C) p⁻¹* ⬝* pcompose_pconst (pconst B C), { intro p, hinduction p using phomotopy_rec_idp, refl }, refine H p⁻¹* ⬝ ap (pwhisker_left _) (symm_symm _) ◾** idp, end @[hott] def passoc_pconst_right {A B C D : Type*} (h : C →* D) (g : B →* C) : passoc h g (pconst A B) ⬝* (pwhisker_left h (pcompose_pconst g) ⬝* pcompose_pconst h) = pcompose_pconst (h ∘* g) := begin fapply phomotopy_eq, { intro a, apply idp_con }, { induction h with h h₀, induction g with g g₀, induction D with D d₀, induction C with C c₀, dsimp at *, induction g₀, induction h₀, refl } end @[hott] def passoc_pconst_middle {A A' B B' : Type*} (g : B →* B') (f : A' →* A) : passoc g (pconst A B) f ⬝* (pwhisker_left g (pconst_pcompose f) ⬝* pcompose_pconst g) = pwhisker_right f (pcompose_pconst g) ⬝* pconst_pcompose f := begin fapply phomotopy_eq, { intro a, exact idp_con _ ⬝ idp_con _ }, { induction g with g g₀, induction f with f f₀, induction B' with D d₀, induction A with C c₀, dsimp at *, induction g₀, induction f₀, refl } end @[hott] def passoc_pconst_left {A B C D : Type*} (g : B →* C) (f : A →* B) : phsquare (passoc (pconst C D) g f) (pconst_pcompose f) (pwhisker_right f (pconst_pcompose g)) (pconst_pcompose (g ∘* f)) := begin fapply phomotopy_eq, { intro a, dsimp [passoc, phomotopy.trans, phomotopy.mk], exact idp_con _ }, { induction g with g g₀, induction f with f f₀, induction C with C c₀, induction B with B b₀, dsimp at *, induction g₀, induction f₀, refl } end @[hott] def ppcompose_left_pcompose {A B C D : Type*} (h : C →* D) (g : B →* C) : @ppcompose_left A _ _ (h ∘* g) ~* ppcompose_left h ∘* ppcompose_left g := begin fapply phomotopy_mk_ppmap, { exact passoc h g }, { dsimp [ppcompose_left], refine idp ◾** (phomotopy_of_eq_con _ _ ⬝ (ap phomotopy_of_eq (pcompose_left_eq_of_phomotopy _ _) ⬝ phomotopy_of_eq_of_phomotopy _) ◾** phomotopy_of_eq_of_phomotopy _) ⬝ _, refine _ ⬝ (phomotopy_of_eq_of_phomotopy _)⁻¹ᵖ, exact passoc_pconst_right h g } end @[hott] def ppcompose_right_pcompose {A B C D : Type*} (g : B →* C) (f : A →* B) : @ppcompose_right _ _ D (g ∘* f) ~* ppcompose_right f ∘* ppcompose_right g := begin symmetry, fapply phomotopy_mk_ppmap, { intro h, exact passoc h g f }, { dsimp [ppcompose_right], refine idp ◾** phomotopy_of_eq_of_phomotopy _ ⬝ _ ⬝ (phomotopy_of_eq_con _ _ ⬝ (ap phomotopy_of_eq (pcompose_right_eq_of_phomotopy _ _) ⬝ (phomotopy_of_eq_of_phomotopy _)) ◾** (phomotopy_of_eq_of_phomotopy _))⁻¹, exact passoc_pconst_left g f } end @[hott] def ppcompose_left_ppcompose_right {A A' B B' : Type*} (g : B →* B') (f : A' →* A) : psquare (ppcompose_left g) (ppcompose_left g) (ppcompose_right f) (ppcompose_right f) := begin fapply phomotopy_mk_ppmap, { intro h, exact passoc g h f }, { dsimp [ppcompose_left, ppcompose_right], refine idp ◾** (phomotopy_of_eq_con _ _ ⬝ (ap phomotopy_of_eq (pcompose_left_eq_of_phomotopy _ _) ⬝ (phomotopy_of_eq_of_phomotopy _)) ◾** (phomotopy_of_eq_of_phomotopy _)) ⬝ _ ⬝ (phomotopy_of_eq_con _ _ ⬝ (ap phomotopy_of_eq (pcompose_right_eq_of_phomotopy _ _) ⬝ (phomotopy_of_eq_of_phomotopy _)) ◾** (phomotopy_of_eq_of_phomotopy _))⁻¹, apply passoc_pconst_middle } end @[hott] def pcompose_pconst_phomotopy {A B C : Type*} {f f' : B →* C} (p : f ~* f') : pwhisker_right (pconst A B) p ⬝* pcompose_pconst f' = pcompose_pconst f := begin fapply phomotopy_eq, { intro a, exact to_homotopy_pt p }, { hinduction p using phomotopy_rec_idp, induction C with C c₀, induction f with f f₀, dsimp at *, induction f₀, refl } end @[hott] def pid_pconst (A B : Type*) : pcompose_pconst (pid B) = pid_pcompose (pconst A B) := by refl @[hott] def pid_pconst_pcompose {A B C : Type*} (f : A →* B) : phsquare (pid_pcompose (pconst B C ∘* f)) (pcompose_pconst (pid C)) (pwhisker_left (pid C) (pconst_pcompose f)) (pconst_pcompose f) := begin fapply phomotopy_eq, { refl }, { induction f with f f₀, induction B with B b₀, dsimp at *, induction f₀, refl } end @[hott] def ppcompose_left_pconst (A B C : Type*) : @ppcompose_left A _ _ (pconst B C) ~* pconst (ppmap A B) (ppmap A C) := begin fapply phomotopy_mk_ppmap, { exact pconst_pcompose }, { dsimp [ppcompose_left], exact idp ◾** phomotopy_of_eq_idp _ ⬝ (phomotopy_of_eq_of_phomotopy _)⁻¹ } end @[hott] def ppcompose_left_phomotopy {A B C : Type*} {g g' : B →* C} (p : g ~* g') : @ppcompose_left A _ _ g ~* ppcompose_left g' := begin hinduction p using phomotopy_rec_idp, refl end @[hott] def ppcompose_left_phomotopy_refl {A B C : Type*} (g : B →* C) : ppcompose_left_phomotopy (phomotopy.refl g) = phomotopy.refl (@ppcompose_left A _ _ g) := by dsimp [ppcompose_left_phomotopy]; exact phomotopy_rec_idp_refl _ /- a more explicit proof of ppcompose_left_phomotopy, which might be useful if we need to prove properties about it -/ -- fapply phomotopy_mk_ppmap, -- { intro f, exact pwhisker_right f p }, -- { refine ap (λx, _ ⬝* x) (phomotopy_of_eq_of_phomotopy _) ⬝ _ ⬝ (phomotopy_of_eq_of_phomotopy _)⁻¹, -- exact pcompose_pconst_phomotopy p } @[hott] def ppcompose_right_phomotopy {A B C : Type*} {f f' : A →* B} (p : f ~* f') : @ppcompose_right _ _ C f ~* ppcompose_right f' := begin hinduction p using phomotopy_rec_idp, refl end @[hott] def pppcompose (A B C : Type*) : ppmap B C →* ppmap (ppmap A B) (ppmap A C) := pmap.mk ppcompose_left (eq_of_phomotopy (ppcompose_left_pconst _ _ _)) section psquare variables {A' : Type*} {A₀₀ : Type*} {A₂₀ : Type*} {A₄₀ : Type*} {A₀₂ : Type*} {A₂₂ : Type*} {A₄₂ : Type*} {A₀₄ : Type*} {A₂₄ : Type*} {A₄₄ : Type*} {f₁₀ f₁₀' : A₀₀ →* A₂₀} {f₃₀ : A₂₀ →* A₄₀} {f₀₁ f₀₁' : A₀₀ →* A₀₂} {f₂₁ f₂₁' : A₂₀ →* A₂₂} {f₄₁ : A₄₀ →* A₄₂} {f₁₂ f₁₂' : A₀₂ →* A₂₂} {f₃₂ : A₂₂ →* A₄₂} {f₀₃ : A₀₂ →* A₀₄} {f₂₃ : A₂₂ →* A₂₄} {f₄₃ : A₄₂ →* A₄₄} {f₁₄ : A₀₄ →* A₂₄} {f₃₄ : A₂₄ →* A₄₄} -- @[hott] def ptrunc_functor_psquare (n : ℕ₋₂) (p : psquare f₁₀ f₁₂ f₀₁ f₂₁) : -- psquare (ptrunc_functor n f₁₀) (ptrunc_functor n f₁₂) -- (ptrunc_functor n f₀₁) (ptrunc_functor n f₂₁) := -- (ptrunc_functor_pcompose _ _ _)⁻¹* ⬝* ptrunc_functor_phomotopy n p ⬝* -- ptrunc_functor_pcompose _ _ _ -- @[hott] def homotopy_group_functor_psquare (n : ℕ) (p : psquare f₁₀ f₁₂ f₀₁ f₂₁) : -- psquare (π→[n] f₁₀) (π→[n] f₁₂) (π→[n] f₀₁) (π→[n] f₂₁) := -- (homotopy_group_functor_compose _ _ _)⁻¹* ⬝* homotopy_group_functor_phomotopy n p ⬝* -- homotopy_group_functor_compose _ _ _ -- @[hott] def homotopy_group_homomorphism_psquare (n : ℕ) [H : is_succ n] -- (p : psquare f₁₀ f₁₂ f₀₁ f₂₁) : hsquare (π→g[n] f₁₀) (π→g[n] f₁₂) (π→g[n] f₀₁) (π→g[n] f₂₁) := -- begin -- induction H with n, exact to_homotopy (ptrunc_functor_psquare 0 (apn_psquare (succ n) p)) -- end @[hott] def ppcompose_left_psquare {A : Type*} (p : psquare f₁₀ f₁₂ f₀₁ f₂₁) : psquare (@ppcompose_left A _ _ f₁₀) (ppcompose_left f₁₂) (ppcompose_left f₀₁) (ppcompose_left f₂₁) := (ppcompose_left_pcompose _ _)⁻¹* ⬝* ppcompose_left_phomotopy p ⬝* ppcompose_left_pcompose _ _ @[hott] def ppcompose_right_psquare {A : Type*} (p : psquare f₁₀ f₁₂ f₀₁ f₂₁) : psquare (@ppcompose_right _ _ A f₁₂) (ppcompose_right f₁₀) (ppcompose_right f₂₁) (ppcompose_right f₀₁) := (ppcompose_right_pcompose _ _)⁻¹* ⬝* ppcompose_right_phomotopy p⁻¹* ⬝* ppcompose_right_pcompose _ _ @[hott] def trans_phomotopy_hconcat {f₀₁' f₀₁''} (q₂ : f₀₁'' ~* f₀₁') (q₁ : f₀₁' ~* f₀₁) (p : psquare f₁₀ f₁₂ f₀₁ f₂₁) : (q₂ ⬝* q₁) ⬝ph* p = q₂ ⬝ph* q₁ ⬝ph* p := idp ◾** (ap (pwhisker_left f₁₂) (trans_symm _ _) ⬝ pwhisker_left_trans _ _ _) ⬝ (trans_assoc _ _ _)⁻¹ @[hott] def symm_phomotopy_hconcat {f₀₁'} (q : f₀₁ ~* f₀₁') (p : psquare f₁₀ f₁₂ f₀₁ f₂₁) : q⁻¹* ⬝ph* p = p ⬝* pwhisker_left f₁₂ q := idp ◾** ap (pwhisker_left f₁₂) (symm_symm _) @[hott] def refl_phomotopy_hconcat (p : psquare f₁₀ f₁₂ f₀₁ f₂₁) : phomotopy.rfl ⬝ph* p = p := idp ◾** (ap (pwhisker_left _) refl_symm ⬝ pwhisker_left_refl _ _) ⬝ trans_refl _ local attribute [reducible] phomotopy.rfl @[hott] theorem pwhisker_left_phomotopy_hconcat {f₀₁'} (r : f₀₁' ~* f₀₁) (p : psquare f₁₀ f₁₂ f₀₁ f₂₁) (q : psquare f₁₂ f₁₄ f₀₃ f₂₃) : pwhisker_left f₀₃ r ⬝ph* (p ⬝v* q) = (r ⬝ph* p) ⬝v* q := begin hinduction r using phomotopy_rec_idp, rwr [pwhisker_left_refl, refl_phomotopy_hconcat, refl_phomotopy_hconcat] end @[hott] theorem pvcompose_pwhisker_left {f₀₁'} (r : f₀₁ ~* f₀₁') (p : psquare f₁₀ f₁₂ f₀₁ f₂₁) (q : psquare f₁₂ f₁₄ f₀₃ f₂₃) : (p ⬝v* q) ⬝* (pwhisker_left f₁₄ (pwhisker_left f₀₃ r)) = (p ⬝* pwhisker_left f₁₂ r) ⬝v* q := begin hinduction r using phomotopy_rec_idp, hsimp end -- by hinduction r using phomotopy_rec_idp; rwr [+pwhisker_left_refl, + trans_refl] @[hott] def phconcat2 {p p' : psquare f₁₀ f₁₂ f₀₁ f₂₁} {q q' : psquare f₃₀ f₃₂ f₂₁ f₄₁} (r : p = p') (s : q = q') : p ⬝h* q = p' ⬝h* q' := ap011 phconcat r s @[hott] def pvconcat2 {p p' : psquare f₁₀ f₁₂ f₀₁ f₂₁} {q q' : psquare f₁₂ f₁₄ f₀₃ f₂₃} (r : p = p') (s : q = q') : p ⬝v* q = p' ⬝v* q' := ap011 pvconcat r s @[hott] def phinverse2 {f₁₀ : A₀₀ ≃* A₂₀} {f₁₂ : A₀₂ ≃* A₂₂} {p p' : psquare f₁₀.to_pmap f₁₂.to_pmap f₀₁ f₂₁} (r : p = p') : p⁻¹ʰ* = p'⁻¹ʰ* := ap phinverse r @[hott] def pvinverse2 {f₀₁ : A₀₀ ≃* A₀₂} {f₂₁ : A₂₀ ≃* A₂₂} {p p' : psquare f₁₀ f₁₂ f₀₁.to_pmap f₂₁.to_pmap} (r : p = p') : p⁻¹ᵛ* = p'⁻¹ᵛ* := ap pvinverse r @[hott] def phomotopy_hconcat2 {q q' : f₀₁' ~* f₀₁} {p p' : psquare f₁₀ f₁₂ f₀₁ f₂₁} (r : q = q') (s : p = p') : q ⬝ph* p = q' ⬝ph* p' := ap011 phomotopy_hconcat r s @[hott] def hconcat_phomotopy2 {p p' : psquare f₁₀ f₁₂ f₀₁ f₂₁} {q q' : f₂₁' ~* f₂₁} (r : p = p') (s : q = q') : p ⬝hp* q = p' ⬝hp* q' := ap011 hconcat_phomotopy r s @[hott] def phomotopy_vconcat2 {q q' : f₁₀' ~* f₁₀} {p p' : psquare f₁₀ f₁₂ f₀₁ f₂₁} (r : q = q') (s : p = p') : q ⬝pv* p = q' ⬝pv* p' := ap011 phomotopy_vconcat r s @[hott] def vconcat_phomotopy2 {p p' : psquare f₁₀ f₁₂ f₀₁ f₂₁} {q q' : f₁₂' ~* f₁₂} (r : p = p') (s : q = q') : p ⬝vp* q = p' ⬝vp* q' := ap011 vconcat_phomotopy r s -- for consistency, should there be a second star here? infix ` ◾h* `:79 := phconcat2 infix ` ◾v* `:79 := pvconcat2 infixl ` ◾hp* `:79 := hconcat_phomotopy2 infixr ` ◾ph* `:79 := phomotopy_hconcat2 infixl ` ◾vp* `:79 := vconcat_phomotopy2 infixr ` ◾pv* `:79 := phomotopy_vconcat2 postfix `⁻²ʰ*`:(max+1) := phinverse2 postfix `⁻²ᵛ*`:(max+1) := pvinverse2 end psquare variables {X : Type*} {X' : Type*} {Y : Type*} {Y' : Type*} {Z : Type*} @[hott] def pap1 (X Y : Type*) : ppmap X Y →* ppmap (Ω X) (Ω Y) := pmap.mk ap1 (eq_of_phomotopy (ap1_pconst _ _)) @[hott] def ap1_gen_const {A B : Type _} {a₁ a₂ : A} (b : B) (p : a₁ = a₂) : ap1_gen (const A b) idp idp p = idp := ap1_gen_idp_left (const A b) p ⬝ ap_constant p b @[hott] def ap1_gen_compose_const_left {A B C : Type _} (c : C) (f : A → B) {a₁ a₂ : A} (p : a₁ = a₂) : ap1_gen_compose (const B c) f idp idp idp idp p ⬝ ap1_gen_const c (ap1_gen f idp idp p) = ap1_gen_const c p := begin induction p, refl end local attribute [reducible] ap1_gen @[hott] def ap1_gen_compose_const_right {A B C : Type _} (g : B → C) (b : B) {a₁ a₂ : A} (p : a₁ = a₂) : ap1_gen_compose g (const A b) idp idp idp idp p ⬝ begin change ap1_gen g idp idp (ap1_gen (const A b) idp idp p) = ap1_gen g idp idp idp, apply ap (ap1_gen g idp idp), exact (ap1_gen_const b p) end = ap1_gen_const (g b) p := begin induction p, refl end @[hott] def ap1_pcompose_pconst_left {A B C : Type*} (f : A →* B) : phsquare (ap1_pcompose (pconst B C) f) (ap1_pconst A C) (ap1_phomotopy (pconst_pcompose f)) (pwhisker_right (Ω→ f) (ap1_pconst B C) ⬝* pconst_pcompose (Ω→ f)) := begin induction A with A a₀, induction B with B b₀, induction C with C c₀, induction f with f f₀, dsimp at *, induction f₀, refine idp ◾** trans_refl _ ⬝ _ ⬝ (refl_trans _)⁻¹ᵖ ⬝ (ap1_phomotopy_refl _)⁻¹ ◾** idp, fapply phomotopy_eq, { exact ap1_gen_compose_const_left c₀ f }, { refl } end @[hott] def ap1_pcompose_pconst_right {A B C : Type*} (g : B →* C) : phsquare (ap1_pcompose g (pconst A B)) (ap1_pconst A C) (ap1_phomotopy (pcompose_pconst g)) (pwhisker_left (Ω→ g) (ap1_pconst A B) ⬝* pcompose_pconst (Ω→ g)) := begin induction A with A a₀, induction B with B b₀, induction C with C c₀, induction g with g g₀, dsimp at *, induction g₀, refine idp ◾** trans_refl _ ⬝ _ ⬝ (refl_trans _)⁻¹ᵖ ⬝ (ap1_phomotopy_refl _)⁻¹ ◾** idp, fapply phomotopy_eq, { exact ap1_gen_compose_const_right g b₀ }, { refl } end @[hott] def pap1_natural_left (f : X' →* X) : psquare (pap1 X Y) (pap1 X' Y) (ppcompose_right f) (ppcompose_right (Ω→ f)) := begin fapply phomotopy_mk_ppmap, { intro g, exact (ap1_pcompose _ _)⁻¹* }, { dsimp [ppcompose_right], refine idp ◾** (ap phomotopy_of_eq (ap1_eq_of_phomotopy _ ◾ idp ⬝ (eq_of_phomotopy_trans _ _)⁻¹ᵖ) ⬝ (phomotopy_of_eq_of_phomotopy _)) ⬝ _, refine _ ⬝ (ap phomotopy_of_eq ((pcompose_right_eq_of_phomotopy _ _) ◾ idp ⬝ (eq_of_phomotopy_trans _ _)⁻¹ᵖ) ⬝ (phomotopy_of_eq_of_phomotopy _))⁻¹ᵖ, apply symm_trans_eq_of_eq_trans, exact (ap1_pcompose_pconst_left f)⁻¹ᵖ } end @[hott] def pap1_natural_right (f : Y →* Y') : psquare (pap1 X Y) (pap1 X Y') (ppcompose_left f) (ppcompose_left (Ω→ f)) := begin fapply phomotopy_mk_ppmap, { intro g, exact (ap1_pcompose _ _)⁻¹* }, { dsimp [ppcompose_left, pap1], refine idp ◾** (ap phomotopy_of_eq (ap1_eq_of_phomotopy _ ◾ idp ⬝ (eq_of_phomotopy_trans _ _)⁻¹ᵖ) ⬝ (phomotopy_of_eq_of_phomotopy _)) ⬝ _, refine _ ⬝ (ap phomotopy_of_eq ((pcompose_left_eq_of_phomotopy _ _) ◾ idp ⬝ (eq_of_phomotopy_trans _ _)⁻¹ᵖ) ⬝ (phomotopy_of_eq_of_phomotopy _))⁻¹ᵖ, apply symm_trans_eq_of_eq_trans, exact (ap1_pcompose_pconst_right f)⁻¹ᵖ } end @[hott] def pequiv.sigma_char {A B : Type*} : (A ≃* B) ≃ Σ(f : A →* B), (Σ(g : B →* A), f ∘* g ~* pid B) × (Σ(h : B →* A), h ∘* f ~* pid A) := begin fapply equiv.MK, { intro f, exact ⟨f.to_pmap, (⟨pequiv.to_pinv1 f, pequiv.pright_inv f⟩, ⟨pequiv.to_pinv2 f, pequiv.pleft_inv f⟩)⟩, }, { intro f, exact pequiv.mk' f.1 f.2.1.1 f.2.2.1 f.2.1.2 f.2.2.2 }, { intro f, induction f with f v, induction v with hl hr, induction hl, induction hr, refl }, { intro f, induction f, refl } end @[hott] def is_contr_pright_inv (f : A ≃* B) : is_contr (Σ(g : B →* A), f.to_pmap ∘* g ~* pid B) := begin apply is_trunc_equiv_closed -2 (fiber.sigma_char _ _ ⬝e sigma_equiv_sigma_right (λg, pmap_eq_equiv _ _)), napply is_contr_fiber_of_is_equiv, exact pequiv.to_is_equiv (pequiv_ppcompose_left f) end @[hott] def is_contr_pleft_inv (f : A ≃* B) : is_contr (Σ(h : B →* A), h ∘* f.to_pmap ~* pid A) := begin apply is_trunc_equiv_closed, { exact fiber.sigma_char _ _ ⬝e sigma_equiv_sigma_right (λg, pmap_eq_equiv _ _) }, napply is_contr_fiber_of_is_equiv, exact pequiv.to_is_equiv (pequiv_ppcompose_right f) end @[hott] def pequiv_eq_equiv (f g : A ≃* B) : (f = g) ≃ f.to_pmap ~* g.to_pmap := have Π(f : A →* B), is_prop ((Σ(g : B →* A), f ∘* g ~* pid B) × (Σ(h : B →* A), h ∘* f ~* pid A)), begin intro f, apply is_prop_of_imp_is_contr, intro v, let f' := pequiv.sigma_char⁻¹ᵉ.to_fun ⟨f, v⟩, napply prod.is_trunc_prod, exact is_contr_pright_inv f', exact is_contr_pleft_inv f' end, calc (f = g) ≃ (pequiv.sigma_char.to_fun f = pequiv.sigma_char.to_fun g) : eq_equiv_fn_eq pequiv.sigma_char.to_fun f g ... ≃ (f.to_pmap = g.to_pmap) : @subtype_eq_equiv _ _ this _ _ ... ≃ (f.to_pmap ~* g.to_pmap) : pmap_eq_equiv f.to_pmap g.to_pmap @[hott] def pequiv_eq {f g : A ≃* B} (H : f.to_pmap ~* g.to_pmap) : f = g := (pequiv_eq_equiv f g)⁻¹ᵉ H open algebra -- @[hott] def pequiv_of_isomorphism_of_eq {G₁ G₂ : Group} (p : G₁ = G₂) : -- pequiv_of_isomorphism (isomorphism_of_eq p) = pequiv_of_eq (ap pType_of_Group p) := -- begin -- induction p, -- apply pequiv_eq, -- fapply phomotopy.mk, -- { intro g, refl }, -- { apply is_prop.elim } -- end end pointed end hott
343693d21510f8ee1cdeca4f092a3585c639f92d
36938939954e91f23dec66a02728db08a7acfcf9
/lean/deps/sexpr/src/galois/sexpr.lean
16d8a143e11304a9ccc5b40cb64fb2ddf9143688
[ "Apache-2.0" ]
permissive
pnwamk/reopt-vcg
f8b56dd0279392a5e1c6aee721be8138e6b558d3
c9f9f185fbefc25c36c4b506bbc85fd1a03c3b6d
refs/heads/master
1,631,145,017,772
1,593,549,019,000
1,593,549,143,000
254,191,418
0
0
null
1,586,377,077,000
1,586,377,077,000
null
UTF-8
Lean
false
false
5,887
lean
/- This declares an s-expression datatype for generating expressions and reasoning about their interpretation. -/ import system.io import galois.category.except import galois.data.array import galois.data.buffer import galois.data.list import galois.data.nat import .char_reader universes u v w namespace sexpr class is_atom (α : Type) := (to_char_buffer : α → char_buffer) (read {m:Type → Type} [char_reader string m] (n:ℕ) : m α) end sexpr /-- Data-structure for representing s-expressions. The internal types are strictly general than what one may ordinarily expect of s-expressions to allow arbitrary strings to be converted into s-expressions, and concatenating two s-expressions. We do add a decidable_eq instance for s-expressions, but it is structural and two s expressions may be distinct, but render to the same string. The intent is to allow one to efficiently generate s-expressions, do lightweight pattern matching on them, and write them to handles. -/ inductive sexpr (α:Type) | atom : α -> sexpr | parens : list sexpr -> sexpr namespace sexpr protected def mk_atom {α:Type} (c:α) : sexpr α := sexpr.atom c protected def nil {α:Type} : sexpr α := sexpr.parens [] /-- Render s-expression as a string -/ mutual def render, list_render {α:Type} [h : is_atom α] {β} [has_append β] (rend : char_buffer → β) with render : sexpr α -> β | (atom s) := rend (@is_atom.to_char_buffer _ h s) | (parens []) := rend "()".to_char_buffer | (parens (h::t)) := rend "(".to_char_buffer ++ render h ++ list_render t with list_render : list (sexpr α) -> β | [] := rend ")".to_char_buffer | (s :: ss) := rend " ".to_char_buffer ++ render s ++ list_render ss /-- Render s-expression as a string -/ def repr {α:Type} [h : is_atom α] : sexpr α -> string := render buffer.to_string instance {α} [is_atom α] : has_repr (sexpr α) := { repr := sexpr.repr } /-- Construct an s-expression from a list of s-expressions -/ protected def of_list {α} : list (sexpr α) → sexpr α := sexpr.parens /-- A frame represents the first part of an s-expression that started with a open-paren -/ structure frame (α:Type) := (exprs : list (sexpr α)) namespace frame /-- Create an empty frame -/ def nil {α} : frame α := ⟨[]⟩ /-- Close a frame by appending close paren -/ def close {α} (f:frame α) : sexpr α := sexpr.parens f.exprs.reverse /-- This adds the partial token as an sexpression if the token is non-empty -/ def append_sexpr {α} (f:frame α) (t:sexpr α) : frame α := ⟨ t :: f.exprs⟩ /-- This adds the partial token as an sexpression if the token is non-empty -/ def append_frame {α} (f:frame α) (t:frame α) : frame α := append_sexpr f t.close end frame section open char_reader /-- The character after an atom should be empty or a close paren or whitespace. -/ def check_atom_term {m} [char_reader string m] : m unit := do mc ← peek_char, match mc with | option.none := pure () | (option.some c) := do when (c ∉ ['(', ')'] ∧ ¬c.is_whitespace) (throw ("Unexpected character "++ c.repr)) end /-- Read the s-expression from the handle/ -/ def read_core {α:Type} [is_atom α] {m} [char_reader string m] : ℕ → list (frame α) → m (sexpr α) | nat.zero frames := do throw "out of gas" | (nat.succ n) frames := (do mc ← peek_char, match mc with | option.none := throw "Unexpected end of expression." | option.some '(' := do consume_char *> read_core n (frame.nil :: frames) | option.some ')' := do match frames with | [] := throw "unexpected close paren" | (top_frame::lower_frames) := do consume_char, match lower_frames with | [] := do pure top_frame.close | (return_frame::rest_frames) := read_core n (frame.append_frame return_frame top_frame :: rest_frames) end end | option.some c := if c.is_whitespace then do consume_char, read_core n frames else do a ← sexpr.mk_atom <$> is_atom.read α n, check_atom_term, match frames with | [] := pure a | (top_frame::lower_frames) := do read_core n (top_frame.append_sexpr a :: lower_frames) end end) /-- Read the s-expression from the handle/ -/ def read (α:Type) [i:is_atom α] {m} [char_reader string m] (gas : ℕ) : m (sexpr α) := @read_core α i _ _ gas [] end local attribute [instance] char_reader.handle_char_reader.string_is_char_reader /-- Read the s-expression from the handle/ -/ def read_from_handle {α} [i:is_atom α] (h:io.handle) : io (except string (sexpr α)) := do let gas : ℕ := 2^64-1 in char_reader.read_from_handle h (@sexpr.read α i _ _ gas) --local attribute [instance] string_char_reader.string_is_char_reader /-- Parse the string as an s-expression -/ def parse {α:Type} [i:is_atom α] (s:string) : except string (sexpr α) := let gas : ℕ := 2^64-1 in char_reader.read_from_string s (@sexpr.read α i _ _ gas) /-- Write the s-expression to the handle. -/ def write {α} [is_atom α] (h:io.handle) (s:sexpr α) : io unit := @sexpr.render α _ (io unit) {append := λx y, x >> y} (io.fs.write h) s local infixr `<+>`:50 := sexpr.append -- Create an operation applied to some argumen ts. def app {α} (op:sexpr α) (args : list (sexpr α)) : sexpr α := sexpr.parens (op :: args) /-- Apply an binary operation to two sexpressions. -/ def bin_app {α} (op:sexpr α) (x y : sexpr α) : sexpr α := sexpr.parens [op, x,y] instance (α) [decidable_eq α] : decidable_eq (sexpr α) := by tactic.mk_dec_eq_instance meta def exact_trivial_tac : tactic unit := `[exact trivial] /- Construct an atom from a string literal. -/ def of_string {α} [i:is_atom α] (nm:string) (p : except.is_ok (char_reader.read_from_string nm (@sexpr.read α i _ _ 1024)) . exact_trivial_tac) : sexpr α := p.value end sexpr
b903457f509af52250adc9576bda59cdd1f43075
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/topology/algebra/module.lean
d3fa6b4ebca3aee5f659d9a9e616328e6d922b41
[ "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
26,040
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jan-David Salchow, Sébastien Gouëzel, Jean Lo -/ import topology.algebra.ring linear_algebra.basic ring_theory.algebra /-! # Theory of topological modules and continuous linear maps. We define classes `topological_semimodule`, `topological_module` and `topological_vector_spaces`, as extensions of the corresponding algebraic classes where the algebraic operations are continuous. We also define continuous linear maps, as linear maps between topological modules which are continuous. The set of continuous linear maps between the topological `R`-modules `M` and `M₂` is denoted by `M →L[R] M₂`. Continuous linear equivalences are denoted by `M ≃L[R] M₂`. ## Implementation notes Topological vector spaces are defined as an `abbreviation` for topological modules, if the base ring is a field. This has as advantage that topological vector spaces are completely transparent for type class inference, which means that all instances for topological modules are immediately picked up for vector spaces as well. A cosmetic disadvantage is that one can not extend topological vector spaces. The solution is to extend `topological_module` instead. -/ open filter open_locale topological_space universes u v w u' section prio set_option default_priority 100 -- see Note [default priority] /-- A topological semimodule, over a semiring which is also a topological space, is a semimodule in which scalar multiplication is continuous. In applications, R will be a topological semiring and M a topological additive semigroup, but this is not needed for the definition -/ class topological_semimodule (R : Type u) (M : Type v) [semiring R] [topological_space R] [topological_space M] [add_comm_monoid M] [semimodule R M] : Prop := (continuous_smul : continuous (λp : R × M, p.1 • p.2)) end prio section variables {R : Type u} {M : Type v} [semiring R] [topological_space R] [topological_space M] [add_comm_monoid M] [semimodule R M] [topological_semimodule R M] lemma continuous_smul : continuous (λp:R×M, p.1 • p.2) := topological_semimodule.continuous_smul lemma continuous.smul {α : Type*} [topological_space α] {f : α → R} {g : α → M} (hf : continuous f) (hg : continuous g) : continuous (λp, f p • g p) := continuous_smul.comp (hf.prod_mk hg) lemma tendsto_smul {c : R} {x : M} : tendsto (λp:R×M, p.fst • p.snd) (𝓝 (c, x)) (𝓝 (c • x)) := continuous_smul.tendsto _ lemma filter.tendsto.smul {α : Type*} {l : filter α} {f : α → R} {g : α → M} {c : R} {x : M} (hf : tendsto f l (𝓝 c)) (hg : tendsto g l (𝓝 x)) : tendsto (λ a, f a • g a) l (𝓝 (c • x)) := tendsto_smul.comp (hf.prod_mk_nhds hg) end section prio set_option default_priority 100 -- see Note [default priority] /-- A topological module, over a ring which is also a topological space, is a module in which scalar multiplication is continuous. In applications, `R` will be a topological ring and `M` a topological additive group, but this is not needed for the definition -/ class topological_module (R : Type u) (M : Type v) [ring R] [topological_space R] [topological_space M] [add_comm_group M] [module R M] extends topological_semimodule R M : Prop /-- A topological vector space is a topological module over a field. -/ abbreviation topological_vector_space (R : Type u) (M : Type v) [field R] [topological_space R] [topological_space M] [add_comm_group M] [module R M] := topological_module R M end prio section variables {R : Type*} {M : Type*} [ring R] [topological_space R] [topological_space M] [add_comm_group M] [module R M] [topological_module R M] /-- Scalar multiplication by a unit is a homeomorphism from a topological module onto itself. -/ protected def homeomorph.smul_of_unit (a : units R) : M ≃ₜ M := { to_fun := λ x, (a : R) • x, inv_fun := λ x, ((a⁻¹ : units R) : R) • x, right_inv := λ x, calc (a : R) • ((a⁻¹ : units R) : R) • x = x : by rw [smul_smul, units.mul_inv, one_smul], left_inv := λ x, calc ((a⁻¹ : units R) : R) • (a : R) • x = x : by rw [smul_smul, units.inv_mul, one_smul], continuous_to_fun := continuous_const.smul continuous_id, continuous_inv_fun := continuous_const.smul continuous_id } lemma is_open_map_smul_of_unit (a : units R) : is_open_map (λ (x : M), (a : R) • x) := (homeomorph.smul_of_unit a).is_open_map lemma is_closed_map_smul_of_unit (a : units R) : is_closed_map (λ (x : M), (a : R) • x) := (homeomorph.smul_of_unit a).is_closed_map /-- If `M` is a topological module over `R` and `0` is a limit of invertible elements of `R`, then `⊤` is the only submodule of `M` with a nonempty interior. See also `submodule.eq_top_of_nonempty_interior` for a `normed_space` version. -/ lemma submodule.eq_top_of_nonempty_interior' [topological_add_monoid M] (h : nhds_within (0:R) {x | is_unit x} ≠ ⊥) (s : submodule R M) (hs : (interior (s:set M)).nonempty) : s = ⊤ := begin rcases hs with ⟨y, hy⟩, refine (submodule.eq_top_iff'.2 $ λ x, _), rw [mem_interior_iff_mem_nhds] at hy, have : tendsto (λ c:R, y + c • x) (nhds_within 0 {x | is_unit x}) (𝓝 (y + (0:R) • x)), from tendsto_const_nhds.add ((tendsto_nhds_within_of_tendsto_nhds tendsto_id).smul tendsto_const_nhds), rw [zero_smul, add_zero] at this, rcases nonempty_of_mem_sets h (inter_mem_sets (mem_map.1 (this hy)) self_mem_nhds_within) with ⟨_, hu, u, rfl⟩, have hy' : y ∈ ↑s := mem_of_nhds hy, exact (s.smul_mem_iff' _).1 ((s.add_mem_iff_right hy').1 hu) end end section variables {R : Type*} {M : Type*} {a : R} [field R] [topological_space R] [topological_space M] [add_comm_group M] [vector_space R M] [topological_vector_space R M] set_option class.instance_max_depth 36 /-- Scalar multiplication by a non-zero field element is a homeomorphism from a topological vector space onto itself. -/ protected def homeomorph.smul_of_ne_zero (ha : a ≠ 0) : M ≃ₜ M := {.. homeomorph.smul_of_unit (units.mk0 a ha)} lemma is_open_map_smul_of_ne_zero (ha : a ≠ 0) : is_open_map (λ (x : M), a • x) := (homeomorph.smul_of_ne_zero ha).is_open_map lemma is_closed_map_smul_of_ne_zero (ha : a ≠ 0) : is_closed_map (λ (x : M), a • x) := (homeomorph.smul_of_ne_zero ha).is_closed_map end /-- Continuous linear maps between modules. We only put the type classes that are necessary for the definition, although in applications `M` and `M₂` will be topological modules over the topological ring `R`. -/ structure continuous_linear_map (R : Type*) [ring R] (M : Type*) [topological_space M] [add_comm_group M] (M₂ : Type*) [topological_space M₂] [add_comm_group M₂] [module R M] [module R M₂] extends linear_map R M M₂ := (cont : continuous to_fun) notation M ` →L[`:25 R `] ` M₂ := continuous_linear_map R M M₂ /-- Continuous linear equivalences between modules. We only put the type classes that are necessary for the definition, although in applications `M` and `M₂` will be topological modules over the topological ring `R`. -/ structure continuous_linear_equiv (R : Type*) [ring R] (M : Type*) [topological_space M] [add_comm_group M] (M₂ : Type*) [topological_space M₂] [add_comm_group M₂] [module R M] [module R M₂] extends linear_equiv R M M₂ := (continuous_to_fun : continuous to_fun) (continuous_inv_fun : continuous inv_fun) notation M ` ≃L[`:50 R `] ` M₂ := continuous_linear_equiv R M M₂ namespace continuous_linear_map section general_ring /- Properties that hold for non-necessarily commutative rings. -/ variables {R : Type*} [ring R] {M : Type*} [topological_space M] [add_comm_group M] {M₂ : Type*} [topological_space M₂] [add_comm_group M₂] {M₃ : Type*} [topological_space M₃] [add_comm_group M₃] {M₄ : Type*} [topological_space M₄] [add_comm_group M₄] [module R M] [module R M₂] [module R M₃] [module R M₄] /-- Coerce continuous linear maps to linear maps. -/ instance : has_coe (M →L[R] M₂) (M →ₗ[R] M₂) := ⟨to_linear_map⟩ /-- Coerce continuous linear maps to functions. -/ -- see Note [function coercion] instance to_fun : has_coe_to_fun $ M →L[R] M₂ := ⟨λ _, M → M₂, λ f, f⟩ protected lemma continuous (f : M →L[R] M₂) : continuous f := f.2 @[ext] theorem ext {f g : M →L[R] M₂} (h : ∀ x, f x = g x) : f = g := by cases f; cases g; congr' 1; ext x; apply h theorem ext_iff {f g : M →L[R] M₂} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, by rw h, by ext⟩ variables (c : R) (f g : M →L[R] M₂) (h : M₂ →L[R] M₃) (x y z : M) -- make some straightforward lemmas available to `simp`. @[simp] lemma map_zero : f (0 : M) = 0 := (to_linear_map _).map_zero @[simp] lemma map_add : f (x + y) = f x + f y := (to_linear_map _).map_add _ _ @[simp] lemma map_sub : f (x - y) = f x - f y := (to_linear_map _).map_sub _ _ @[simp] lemma map_smul : f (c • x) = c • f x := (to_linear_map _).map_smul _ _ @[simp] lemma map_neg : f (-x) = - (f x) := (to_linear_map _).map_neg _ @[simp, squash_cast] lemma coe_coe : ((f : M →ₗ[R] M₂) : (M → M₂)) = (f : M → M₂) := rfl /-- The continuous map that is constantly zero. -/ instance: has_zero (M →L[R] M₂) := ⟨⟨0, continuous_const⟩⟩ instance : inhabited (M →L[R] M₂) := ⟨0⟩ @[simp] lemma zero_apply : (0 : M →L[R] M₂) x = 0 := rfl @[simp, elim_cast] lemma coe_zero : ((0 : M →L[R] M₂) : M →ₗ[R] M₂) = 0 := rfl /- no simp attribute on the next line as simp does not always simplify `0 x` to `0` when `0` is the zero function, while it does for the zero continuous linear map, and this is the most important property we care about. -/ @[elim_cast] lemma coe_zero' : ((0 : M →L[R] M₂) : M → M₂) = 0 := rfl /-- the identity map as a continuous linear map. -/ def id : M →L[R] M := ⟨linear_map.id, continuous_id⟩ instance : has_one (M →L[R] M) := ⟨id⟩ lemma id_apply : (id : M →L[R] M) x = x := rfl @[simp, elim_cast] lemma coe_id : ((id : M →L[R] M) : M →ₗ[R] M) = linear_map.id := rfl @[simp, elim_cast] lemma coe_id' : ((id : M →L[R] M) : M → M) = _root_.id := rfl @[simp] lemma one_apply : (1 : M →L[R] M) x = x := rfl section add variables [topological_add_group M₂] instance : has_add (M →L[R] M₂) := ⟨λ f g, ⟨f + g, f.2.add g.2⟩⟩ @[simp] lemma add_apply : (f + g) x = f x + g x := rfl @[simp, move_cast] lemma coe_add : (((f + g) : M →L[R] M₂) : M →ₗ[R] M₂) = (f : M →ₗ[R] M₂) + g := rfl @[move_cast] lemma coe_add' : (((f + g) : M →L[R] M₂) : M → M₂) = (f : M → M₂) + g := rfl instance : has_neg (M →L[R] M₂) := ⟨λ f, ⟨-f, f.2.neg⟩⟩ @[simp] lemma neg_apply : (-f) x = - (f x) := rfl @[simp, move_cast] lemma coe_neg : (((-f) : M →L[R] M₂) : M →ₗ[R] M₂) = -(f : M →ₗ[R] M₂) := rfl @[move_cast] lemma coe_neg' : (((-f) : M →L[R] M₂) : M → M₂) = -(f : M → M₂) := rfl instance : add_comm_group (M →L[R] M₂) := by { refine {zero := 0, add := (+), neg := has_neg.neg, ..}; intros; ext; apply_rules [zero_add, add_assoc, add_zero, add_left_neg, add_comm] } lemma sub_apply (x : M) : (f - g) x = f x - g x := rfl @[simp, move_cast] lemma coe_sub : (((f - g) : M →L[R] M₂) : M →ₗ[R] M₂) = (f : M →ₗ[R] M₂) - g := rfl @[simp, move_cast] lemma coe_sub' : (((f - g) : M →L[R] M₂) : M → M₂) = (f : M → M₂) - g := rfl end add @[simp] lemma sub_apply' (x : M) : ((f : M →ₗ[R] M₂) - g) x = f x - g x := rfl /-- Composition of bounded linear maps. -/ def comp (g : M₂ →L[R] M₃) (f : M →L[R] M₂) : M →L[R] M₃ := ⟨linear_map.comp g.to_linear_map f.to_linear_map, g.2.comp f.2⟩ @[simp, move_cast] lemma coe_comp : ((h.comp f) : (M →ₗ[R] M₃)) = (h : M₂ →ₗ[R] M₃).comp f := rfl @[simp, move_cast] lemma coe_comp' : ((h.comp f) : (M → M₃)) = (h : M₂ → M₃) ∘ f := rfl @[simp] theorem comp_id : f.comp id = f := ext $ λ x, rfl @[simp] theorem id_comp : id.comp f = f := ext $ λ x, rfl @[simp] theorem comp_zero : f.comp (0 : M₃ →L[R] M) = 0 := by { ext, simp } @[simp] theorem zero_comp : (0 : M₂ →L[R] M₃).comp f = 0 := by { ext, simp } @[simp] lemma comp_add [topological_add_group M₂] [topological_add_group M₃] (g : M₂ →L[R] M₃) (f₁ f₂ : M →L[R] M₂) : g.comp (f₁ + f₂) = g.comp f₁ + g.comp f₂ := by { ext, simp } @[simp] lemma add_comp [topological_add_group M₃] (g₁ g₂ : M₂ →L[R] M₃) (f : M →L[R] M₂) : (g₁ + g₂).comp f = g₁.comp f + g₂.comp f := by { ext, simp } theorem comp_assoc (h : M₃ →L[R] M₄) (g : M₂ →L[R] M₃) (f : M →L[R] M₂) : (h.comp g).comp f = h.comp (g.comp f) := rfl instance : has_mul (M →L[R] M) := ⟨comp⟩ instance [topological_add_group M] : ring (M →L[R] M) := { mul := (*), one := 1, mul_one := λ _, ext $ λ _, rfl, one_mul := λ _, ext $ λ _, rfl, mul_assoc := λ _ _ _, ext $ λ _, rfl, left_distrib := λ _ _ _, ext $ λ _, map_add _ _ _, right_distrib := λ _ _ _, ext $ λ _, linear_map.add_apply _ _ _, ..continuous_linear_map.add_comm_group } /-- The cartesian product of two bounded linear maps, as a bounded linear map. -/ protected def prod (f₁ : M →L[R] M₂) (f₂ : M →L[R] M₃) : M →L[R] (M₂ × M₃) := { cont := f₁.2.prod_mk f₂.2, ..f₁.to_linear_map.prod f₂.to_linear_map } @[simp, move_cast] lemma coe_prod (f₁ : M →L[R] M₂) (f₂ : M →L[R] M₃) : (f₁.prod f₂ : M →ₗ[R] M₂ × M₃) = linear_map.prod f₁ f₂ := rfl @[simp, move_cast] lemma prod_apply (f₁ : M →L[R] M₂) (f₂ : M →L[R] M₃) (x : M) : f₁.prod f₂ x = (f₁ x, f₂ x) := rfl variables (R M M₂) /-- `prod.fst` as a `continuous_linear_map`. -/ def fst : M × M₂ →L[R] M := { cont := continuous_fst, to_linear_map := linear_map.fst R M M₂ } /-- `prod.snd` as a `continuous_linear_map`. -/ def snd : M × M₂ →L[R] M₂ := { cont := continuous_snd, to_linear_map := linear_map.snd R M M₂ } variables {R M M₂} @[simp, move_cast] lemma coe_fst : (fst R M M₂ : M × M₂ →ₗ[R] M) = linear_map.fst R M M₂ := rfl @[simp, move_cast] lemma coe_fst' : (fst R M M₂ : M × M₂ → M) = prod.fst := rfl @[simp, move_cast] lemma coe_snd : (snd R M M₂ : M × M₂ →ₗ[R] M₂) = linear_map.snd R M M₂ := rfl @[simp, move_cast] lemma coe_snd' : (snd R M M₂ : M × M₂ → M₂) = prod.snd := rfl /-- `prod.map` of two continuous linear maps. -/ def prod_map (f₁ : M →L[R] M₂) (f₂ : M₃ →L[R] M₄) : (M × M₃) →L[R] (M₂ × M₄) := (f₁.comp (fst R M M₃)).prod (f₂.comp (snd R M M₃)) @[simp, move_cast] lemma coe_prod_map (f₁ : M →L[R] M₂) (f₂ : M₃ →L[R] M₄) : (f₁.prod_map f₂ : (M × M₃) →ₗ[R] (M₂ × M₄)) = ((f₁ : M →ₗ[R] M₂).prod_map (f₂ : M₃ →ₗ[R] M₄)) := rfl @[simp, move_cast] lemma prod_map_apply (f₁ : M →L[R] M₂) (f₂ : M₃ →L[R] M₄) (x) : f₁.prod_map f₂ x = (f₁ x.1, f₂ x.2) := rfl end general_ring section comm_ring variables {R : Type*} [comm_ring R] [topological_space R] {M : Type*} [topological_space M] [add_comm_group M] {M₂ : Type*} [topological_space M₂] [add_comm_group M₂] {M₃ : Type*} [topological_space M₃] [add_comm_group M₃] [module R M] [module R M₂] [module R M₃] [topological_module R M₃] instance : has_scalar R (M →L[R] M₃) := ⟨λ c f, ⟨c • f, continuous_const.smul f.2⟩⟩ variables (c : R) (h : M₂ →L[R] M₃) (f g : M →L[R] M₂) (x y z : M) @[simp] lemma smul_comp : (c • h).comp f = c • (h.comp f) := rfl variable [topological_module R M₂] @[simp] lemma smul_apply : (c • f) x = c • (f x) := rfl @[simp, move_cast] lemma coe_apply : (((c • f) : M →L[R] M₂) : M →ₗ[R] M₂) = c • (f : M →ₗ[R] M₂) := rfl @[move_cast] lemma coe_apply' : (((c • f) : M →L[R] M₂) : M → M₂) = c • (f : M → M₂) := rfl @[simp] lemma comp_smul : h.comp (c • f) = c • (h.comp f) := by { ext, simp } /-- The linear map `λ x, c x • f`. Associates to a scalar-valued linear map and an element of `M₂` the `M₂`-valued linear map obtained by multiplying the two (a.k.a. tensoring by `M₂`) -/ def smul_right (c : M →L[R] R) (f : M₂) : M →L[R] M₂ := { cont := c.2.smul continuous_const, ..c.to_linear_map.smul_right f } @[simp] lemma smul_right_apply {c : M →L[R] R} {f : M₂} {x : M} : (smul_right c f : M → M₂) x = (c : M → R) x • f := rfl @[simp] lemma smul_right_one_one (c : R →L[R] M₂) : smul_right 1 ((c : R → M₂) 1) = c := by ext; simp [-continuous_linear_map.map_smul, (continuous_linear_map.map_smul _ _ _).symm] @[simp] lemma smul_right_one_eq_iff {f f' : M₂} : smul_right (1 : R →L[R] R) f = smul_right 1 f' ↔ f = f' := ⟨λ h, have (smul_right (1 : R →L[R] R) f : R → M₂) 1 = (smul_right (1 : R →L[R] R) f' : R → M₂) 1, by rw h, by simp at this; assumption, by cc⟩ variable [topological_add_group M₂] instance : module R (M →L[R] M₂) := { smul_zero := λ _, ext $ λ _, smul_zero _, zero_smul := λ _, ext $ λ _, zero_smul _ _, one_smul := λ _, ext $ λ _, one_smul _ _, mul_smul := λ _ _ _, ext $ λ _, mul_smul _ _ _, add_smul := λ _ _ _, ext $ λ _, add_smul _ _ _, smul_add := λ _ _ _, ext $ λ _, smul_add _ _ _ } set_option class.instance_max_depth 55 instance : is_ring_hom (λ c : R, c • (1 : M₂ →L[R] M₂)) := { map_one := one_smul _ _, map_add := λ _ _, ext $ λ _, add_smul _ _ _, map_mul := λ _ _, ext $ λ _, mul_smul _ _ _ } instance : algebra R (M₂ →L[R] M₂) := (ring_hom.of $ λ c, c • (1 : M₂ →L[R] M₂)).to_algebra $ λ _ _, ext $ λ _, (map_smul _ _ _).symm end comm_ring end continuous_linear_map namespace continuous_linear_equiv variables {R : Type*} [ring R] {M : Type*} [topological_space M] [add_comm_group M] {M₂ : Type*} [topological_space M₂] [add_comm_group M₂] {M₃ : Type*} [topological_space M₃] [add_comm_group M₃] {M₄ : Type*} [topological_space M₄] [add_comm_group M₄] [module R M] [module R M₂] [module R M₃] [module R M₄] /-- A continuous linear equivalence induces a continuous linear map. -/ def to_continuous_linear_map (e : M ≃L[R] M₂) : M →L[R] M₂ := { cont := e.continuous_to_fun, ..e.to_linear_equiv.to_linear_map } /-- Coerce continuous linear equivs to continuous linear maps. -/ instance : has_coe (M ≃L[R] M₂) (M →L[R] M₂) := ⟨to_continuous_linear_map⟩ /-- Coerce continuous linear equivs to maps. -/ -- see Note [function coercion] instance : has_coe_to_fun (M ≃L[R] M₂) := ⟨λ _, M → M₂, λ f, f⟩ @[simp] theorem coe_def_rev (e : M ≃L[R] M₂) : e.to_continuous_linear_map = e := rfl @[simp] theorem coe_apply (e : M ≃L[R] M₂) (b : M) : (e : M →L[R] M₂) b = e b := rfl @[squash_cast] lemma coe_coe (e : M ≃L[R] M₂) : ((e : M →L[R] M₂) : M → M₂) = e := rfl @[ext] lemma ext {f g : M ≃L[R] M₂} (h : (f : M → M₂) = g) : f = g := begin cases f; cases g, simp only [], ext x, induction h, refl end /-- A continuous linear equivalence induces a homeomorphism. -/ def to_homeomorph (e : M ≃L[R] M₂) : M ≃ₜ M₂ := { ..e } -- Make some straightforward lemmas available to `simp`. @[simp] lemma map_zero (e : M ≃L[R] M₂) : e (0 : M) = 0 := (e : M →L[R] M₂).map_zero @[simp] lemma map_add (e : M ≃L[R] M₂) (x y : M) : e (x + y) = e x + e y := (e : M →L[R] M₂).map_add x y @[simp] lemma map_sub (e : M ≃L[R] M₂) (x y : M) : e (x - y) = e x - e y := (e : M →L[R] M₂).map_sub x y @[simp] lemma map_smul (e : M ≃L[R] M₂) (c : R) (x : M) : e (c • x) = c • (e x) := (e : M →L[R] M₂).map_smul c x @[simp] lemma map_neg (e : M ≃L[R] M₂) (x : M) : e (-x) = -e x := (e : M →L[R] M₂).map_neg x @[simp] lemma map_eq_zero_iff (e : M ≃L[R] M₂) {x : M} : e x = 0 ↔ x = 0 := e.to_linear_equiv.map_eq_zero_iff protected lemma continuous (e : M ≃L[R] M₂) : continuous (e : M → M₂) := e.continuous_to_fun protected lemma continuous_on (e : M ≃L[R] M₂) {s : set M} : continuous_on (e : M → M₂) s := e.continuous.continuous_on protected lemma continuous_at (e : M ≃L[R] M₂) {x : M} : continuous_at (e : M → M₂) x := e.continuous.continuous_at protected lemma continuous_within_at (e : M ≃L[R] M₂) {s : set M} {x : M} : continuous_within_at (e : M → M₂) s x := e.continuous.continuous_within_at lemma comp_continuous_on_iff {α : Type*} [topological_space α] (e : M ≃L[R] M₂) (f : α → M) (s : set α) : continuous_on (e ∘ f) s ↔ continuous_on f s := e.to_homeomorph.comp_continuous_on_iff _ _ lemma comp_continuous_iff {α : Type*} [topological_space α] (e : M ≃L[R] M₂) (f : α → M) : continuous (e ∘ f) ↔ continuous f := e.to_homeomorph.comp_continuous_iff _ section variables (R M) /-- The identity map as a continuous linear equivalence. -/ @[refl] protected def refl : M ≃L[R] M := { continuous_to_fun := continuous_id, continuous_inv_fun := continuous_id, .. linear_equiv.refl R M } end @[simp, elim_cast] lemma coe_refl : ((continuous_linear_equiv.refl R M) : M →L[R] M) = continuous_linear_map.id := rfl @[simp, elim_cast] lemma coe_refl' : ((continuous_linear_equiv.refl R M) : M → M) = id := rfl /-- The inverse of a continuous linear equivalence as a continuous linear equivalence-/ @[symm] protected def symm (e : M ≃L[R] M₂) : M₂ ≃L[R] M := { continuous_to_fun := e.continuous_inv_fun, continuous_inv_fun := e.continuous_to_fun, .. e.to_linear_equiv.symm } @[simp] lemma symm_to_linear_equiv (e : M ≃L[R] M₂) : e.symm.to_linear_equiv = e.to_linear_equiv.symm := by { ext, refl } /-- The composition of two continuous linear equivalences as a continuous linear equivalence. -/ @[trans] protected def trans (e₁ : M ≃L[R] M₂) (e₂ : M₂ ≃L[R] M₃) : M ≃L[R] M₃ := { continuous_to_fun := e₂.continuous_to_fun.comp e₁.continuous_to_fun, continuous_inv_fun := e₁.continuous_inv_fun.comp e₂.continuous_inv_fun, .. e₁.to_linear_equiv.trans e₂.to_linear_equiv } @[simp] lemma trans_to_linear_equiv (e₁ : M ≃L[R] M₂) (e₂ : M₂ ≃L[R] M₃) : (e₁.trans e₂).to_linear_equiv = e₁.to_linear_equiv.trans e₂.to_linear_equiv := by { ext, refl } /-- Product of two continuous linear equivalences. The map comes from `equiv.prod_congr`. -/ def prod (e : M ≃L[R] M₂) (e' : M₃ ≃L[R] M₄) : (M × M₃) ≃L[R] (M₂ × M₄) := { continuous_to_fun := e.continuous_to_fun.prod_map e'.continuous_to_fun, continuous_inv_fun := e.continuous_inv_fun.prod_map e'.continuous_inv_fun, .. e.to_linear_equiv.prod e'.to_linear_equiv } @[simp, move_cast] lemma prod_apply (e : M ≃L[R] M₂) (e' : M₃ ≃L[R] M₄) (x) : e.prod e' x = (e x.1, e' x.2) := rfl @[simp, move_cast] lemma coe_prod (e : M ≃L[R] M₂) (e' : M₃ ≃L[R] M₄) : (e.prod e' : (M × M₃) →L[R] (M₂ × M₄)) = (e : M →L[R] M₂).prod_map (e' : M₃ →L[R] M₄) := rfl variables [topological_add_group M₄] /-- Equivalence given by a block lower diagonal matrix. `e` and `e'` are diagonal square blocks, and `f` is a rectangular block below the diagonal. -/ def skew_prod (e : M ≃L[R] M₂) (e' : M₃ ≃L[R] M₄) (f : M →L[R] M₄) : (M × M₃) ≃L[R] M₂ × M₄ := { continuous_to_fun := (e.continuous_to_fun.comp continuous_fst).prod_mk ((e'.continuous_to_fun.comp continuous_snd).add $ f.continuous.comp continuous_fst), continuous_inv_fun := (e.continuous_inv_fun.comp continuous_fst).prod_mk (e'.continuous_inv_fun.comp $ continuous_snd.sub $ f.continuous.comp $ e.continuous_inv_fun.comp continuous_fst), .. e.to_linear_equiv.skew_prod e'.to_linear_equiv ↑f } @[simp] lemma skew_prod_apply (e : M ≃L[R] M₂) (e' : M₃ ≃L[R] M₄) (f : M →L[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 (e : M ≃L[R] M₂) (e' : M₃ ≃L[R] M₄) (f : M →L[R] M₄) (x) : (e.skew_prod e' f).symm x = (e.symm x.1, e'.symm (x.2 - f (e.symm x.1))) := rfl theorem bijective (e : M ≃L[R] M₂) : function.bijective e := e.to_linear_equiv.to_equiv.bijective theorem injective (e : M ≃L[R] M₂) : function.injective e := e.to_linear_equiv.to_equiv.injective theorem surjective (e : M ≃L[R] M₂) : function.surjective e := e.to_linear_equiv.to_equiv.surjective @[simp] theorem apply_symm_apply (e : M ≃L[R] M₂) (c : M₂) : e (e.symm c) = c := e.1.6 c @[simp] theorem symm_apply_apply (e : M ≃L[R] M₂) (b : M) : e.symm (e b) = b := e.1.5 b @[simp] theorem coe_comp_coe_symm (e : M ≃L[R] M₂) : (e : M →L[R] M₂).comp (e.symm : M₂ →L[R] M) = continuous_linear_map.id := continuous_linear_map.ext e.apply_symm_apply @[simp] theorem coe_symm_comp_coe (e : M ≃L[R] M₂) : (e.symm : M₂ →L[R] M).comp (e : M →L[R] M₂) = continuous_linear_map.id := continuous_linear_map.ext e.symm_apply_apply lemma symm_comp_self (e : M ≃L[R] M₂) : (e.symm : M₂ → M) ∘ (e : M → M₂) = id := by{ ext x, exact symm_apply_apply e x } lemma self_comp_symm (e : M ≃L[R] M₂) : (e : M → M₂) ∘ (e.symm : M₂ → M) = id := by{ ext x, exact apply_symm_apply e x } @[simp] lemma symm_comp_self' (e : M ≃L[R] M₂) : ((e.symm : M₂ →L[R] M) : M₂ → M) ∘ ((e : M →L[R] M₂) : M → M₂) = id := symm_comp_self e @[simp] lemma self_comp_symm' (e : M ≃L[R] M₂) : ((e : M →L[R] M₂) : M → M₂) ∘ ((e.symm : M₂ →L[R] M) : M₂ → M) = id := self_comp_symm e @[simp] theorem symm_symm (e : M ≃L[R] M₂) : e.symm.symm = e := by { ext x, refl } @[simp] theorem symm_symm_apply (e : M ≃L[R] M₂) (x : M) : e.symm.symm x = e x := rfl end continuous_linear_equiv
f43db0cae60d7e5104ff0753e691d6f6fcc3d460
8cb37a089cdb4af3af9d8bf1002b417e407a8e9e
/library/init/meta/declaration.lean
22361c54e11bdcafde5ca4ebb647616cab7df92f
[ "Apache-2.0" ]
permissive
kbuzzard/lean
ae3c3db4bb462d750dbf7419b28bafb3ec983ef7
ed1788fd674bb8991acffc8fca585ec746711928
refs/heads/master
1,620,983,366,617
1,618,937,600,000
1,618,937,600,000
359,886,396
1
0
Apache-2.0
1,618,936,987,000
1,618,936,987,000
null
UTF-8
Lean
false
false
5,495
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.meta.expr init.meta.name init.meta.task /-- Reducibility hints are used in the convertibility checker. When trying to solve a constraint such a (f ...) =?= (g ...) where f and g are definitions, the checker has to decide which one will be unfolded. If f (g) is opaque, then g (f) is unfolded if it is also not marked as opaque, Else if f (g) is abbrev, then f (g) is unfolded if g (f) is also not marked as abbrev, Else if f and g are regular, then we unfold the one with the biggest definitional height. Otherwise both are unfolded. The arguments of the `regular` constructor are: the definitional height and the flag `self_opt`. The definitional height is by default computed by the kernel. It only takes into account other regular definitions used in a definition. When creating declarations using meta-programming, we can specify the definitional depth manually. For definitions marked as regular, we also have a hint for constraints such as (f a) =?= (f b) if self_opt == true, then checker will first try to solve (a =?= b), only if it fails, it unfolds f. Remark: the hint only affects performance. None of the hints prevent the kernel from unfolding a declaration during type checking. Remark: the reducibility_hints are not related to the attributes: reducible/irrelevance/semireducible. These attributes are used by the elaborator. The reducibility_hints are used by the kernel (and elaborator). Moreover, the reducibility_hints cannot be changed after a declaration is added to the kernel. -/ inductive reducibility_hints | opaque : reducibility_hints | abbrev : reducibility_hints | regular : nat → bool → reducibility_hints /-- Reflect a C++ declaration object. The VM replaces it with the C++ implementation. -/ meta inductive declaration /- definition: name, list universe parameters, type, value, is_trusted -/ | defn : name → list name → expr → expr → reducibility_hints → bool → declaration /- theorem: name, list universe parameters, type, value (remark: theorems are always trusted) -/ | thm : name → list name → expr → task expr → declaration /- constant assumption: name, list universe parameters, type, is_trusted -/ | cnst : name → list name → expr → bool → declaration /- axiom : name → list universe parameters, type (remark: axioms are always trusted) -/ | ax : name → list name → expr → declaration open declaration meta def mk_definition (n : name) (ls : list name) (v : expr) (e : expr) : declaration := defn n ls v e (reducibility_hints.regular 1 tt) tt namespace declaration meta def to_name : declaration → name | (defn n _ _ _ _ _) := n | (thm n _ _ _) := n | (cnst n _ _ _) := n | (ax n _ _) := n meta def univ_params : declaration → list name | (defn _ ls _ _ _ _) := ls | (thm _ ls _ _) := ls | (cnst _ ls _ _) := ls | (ax _ ls _) := ls meta def type : declaration → expr | (defn _ _ t _ _ _) := t | (thm _ _ t _) := t | (cnst _ _ t _) := t | (ax _ _ t) := t meta def value : declaration → expr | (defn _ _ _ v _ _) := v | (thm _ _ _ v) := v.get | _ := default expr meta def value_task : declaration → task expr | (defn _ _ _ v _ _) := task.pure v | (thm _ _ _ v) := v | _ := task.pure (default expr) meta def is_trusted : declaration → bool | (defn _ _ _ _ _ t) := t | (cnst _ _ _ t) := t | _ := tt meta def update_type : declaration → expr → declaration | (defn n ls t v h tr) new_t := defn n ls new_t v h tr | (thm n ls t v) new_t := thm n ls new_t v | (cnst n ls t tr) new_t := cnst n ls new_t tr | (ax n ls t) new_t := ax n ls new_t meta def update_name : declaration → name → declaration | (defn n ls t v h tr) new_n := defn new_n ls t v h tr | (thm n ls t v) new_n := thm new_n ls t v | (cnst n ls t tr) new_n := cnst new_n ls t tr | (ax n ls t) new_n := ax new_n ls t meta def update_value : declaration → expr → declaration | (defn n ls t v h tr) new_v := defn n ls t new_v h tr | (thm n ls t v) new_v := thm n ls t (task.pure new_v) | d new_v := d meta def update_value_task : declaration → task expr → declaration | (defn n ls t v h tr) new_v := defn n ls t new_v.get h tr | (thm n ls t v) new_v := thm n ls t new_v | d new_v := d meta def map_value : declaration → (expr → expr) → declaration | (defn n ls t v h tr) f := defn n ls t (f v) h tr | (thm n ls t v) f := thm n ls t (task.map f v) | d f := d meta def to_definition : declaration → declaration | (cnst n ls t tr) := defn n ls t (default expr) reducibility_hints.abbrev tr | (ax n ls t) := thm n ls t (task.pure (default expr)) | d := d meta def is_definition : declaration → bool | (defn _ _ _ _ _ _) := tt | _ := ff /-- Instantiate a universe polymorphic declaration type with the given universes. -/ meta constant instantiate_type_univ_params : declaration → list level → option expr /-- Instantiate a universe polymorphic declaration value with the given universes. -/ meta constant instantiate_value_univ_params : declaration → list level → option expr end declaration
88426d9f7792509b76815892120e1020e265a770
38bf3fd2bb651ab70511408fcf70e2029e2ba310
/src/measure_theory/l1_space.lean
e19feab1a237fc3144af21e1b1bbcebf340ff7b5
[ "Apache-2.0" ]
permissive
JaredCorduan/mathlib
130392594844f15dad65a9308c242551bae6cd2e
d5de80376088954d592a59326c14404f538050a1
refs/heads/master
1,595,862,206,333
1,570,816,457,000
1,570,816,457,000
209,134,499
0
0
Apache-2.0
1,568,746,811,000
1,568,746,811,000
null
UTF-8
Lean
false
false
6,540
lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou Integrable functions and L¹ space -/ import measure_theory.ae_eq_fun noncomputable theory open_locale classical set_option class.instance_max_depth 100 namespace measure_theory open set lattice filter topological_space ennreal emetric universes u v variables {α : Type u} {β : Type v} [measure_space α] variables {γ : Type*} [normed_group γ] infixr ` →ₘ `:25 := ae_eq_fun def integrable (f : α → γ) : Prop := (∫⁻ a, nnnorm (f a)) < ⊤ @[simp] lemma lintegral_nnnorm_zero : (∫⁻ a : α, nnnorm (0 : γ)) = 0 := by simp lemma integrable_zero : integrable (0 : α → γ) := by { have := coe_lt_top, simpa [integrable] } lemma lintegral_nnnorm_add {f g : α → γ} (hf : measurable f) (hg : measurable g) : (∫⁻ a, nnnorm (f a) + nnnorm (g a)) = (∫⁻ a, nnnorm (f a)) + ∫⁻ a, nnnorm (g a) := lintegral_add (measurable_coe_nnnorm hf) (measurable_coe_nnnorm hg) lemma integrable_add {f g : α → γ} (hfm : measurable f) (hgm : measurable g) : integrable f → integrable g → integrable (f + g) := assume hfi hgi, calc (∫⁻ (a : α), ↑(nnnorm ((f + g) a))) ≤ ∫⁻ (a : α), ↑(nnnorm (f a)) + ↑(nnnorm (g a)) : lintegral_le_lintegral _ _ (assume a, by { simp only [coe_add.symm, coe_le_coe], exact nnnorm_triangle _ _ }) ... = _ : lintegral_nnnorm_add hfm hgm ... < ⊤ : add_lt_top.2 ⟨hfi, hgi⟩ -- We don't need `f` to be measurable here, but it's easier to have a uniform API @[sanity_skip] lemma lintegral_nnnorm_neg {f : α → γ} (hf : measurable f) : (∫⁻ (a : α), ↑(nnnorm ((-f) a))) = ∫⁻ (a : α), ↑(nnnorm ((f) a)) := lintegral_congr_ae $ by { filter_upwards [], simp } lemma integrable_neg {f : α → γ} (hfm : measurable f) : integrable f → integrable (-f) := assume hfi, calc _ = _ : lintegral_nnnorm_neg hfm ... < ⊤ : hfi section normed_space variables {K : Type*} [normed_field K] [normed_space K γ] lemma integrable_smul {c : K} {f : α → γ} (hfm : measurable f) : integrable f → integrable (c • f) := begin simp only [integrable], assume hfi, calc (∫⁻ (a : α), nnnorm ((c • f) a)) = (∫⁻ (a : α), (nnnorm c) * nnnorm (f a)) : by { apply lintegral_congr_ae, filter_upwards [], assume a, simp [nnnorm_smul] } ... < ⊤ : begin rw lintegral_const_mul, apply mul_lt_top, { simp }, { exact hfi }, { exact measurable_coe_nnnorm hfm } end end end normed_space variables [second_countable_topology γ] namespace ae_eq_fun def integrable (f : α →ₘ γ) : Prop := f ∈ ball (0 : α →ₘ γ) ⊤ lemma integrable_mk (f : α → γ) (hf : measurable f) : (integrable (mk f hf)) ↔ measure_theory.integrable f := by simp [integrable, zero_def, edist_mk_mk', measure_theory.integrable, nndist_eq_nnnorm] local attribute [simp] integrable_mk lemma integrable_zero : integrable (0 : α →ₘ γ) := mem_ball_self coe_lt_top lemma integrable_add : ∀ {f g : α →ₘ γ}, integrable f → integrable g → integrable (f + g) := begin rintros ⟨f, hf⟩ ⟨g, hg⟩, have := measure_theory.integrable_add hf hg, simpa [mem_ball, zero_def] end lemma integrable_neg : ∀ {f : α →ₘ γ}, integrable f → integrable (-f) := by { rintros ⟨f, hf⟩, have := measure_theory.integrable_neg hf, simpa } instance : is_add_subgroup (ball (0 : α →ₘ γ) ⊤) := { zero_mem := integrable_zero, add_mem := λ _ _, integrable_add, neg_mem := λ _, integrable_neg } section normed_space variables {K : Type*} [normed_field K] [second_countable_topology K] [normed_space K γ] lemma integrable_smul : ∀ {c : K} {f : α →ₘ γ}, integrable f → integrable (c • f) := by { assume c, rintros ⟨f, hf⟩, have := integrable_smul hf, simpa } end normed_space end ae_eq_fun section variables (α γ) def l1 : Type* := subtype (@ae_eq_fun.integrable α _ γ _ _) local notation `L¹` := l1 infixr ` →₁ `:25 := l1 end namespace l1 open ae_eq_fun section normed_group def mk (f : α → γ) : measurable f → integrable f → (α →₁ γ) := assume hfm hfi, ⟨mk f hfm, by { rw integrable_mk, assumption }⟩ protected lemma ext_iff {f g : α →₁ γ} : f = g ↔ f.1 = g.1 := ⟨congr rfl , subtype.eq⟩ instance : emetric_space (α →₁ γ) := subtype.emetric_space instance : metric_space (α →₁ γ) := metric_space_emetric_ball 0 ⊤ lemma dist_def (f g : α →₁ γ) : dist f g = ennreal.to_real (edist f.1 g.1) := rfl instance : add_comm_group (α →₁ γ) := subtype.add_comm_group lemma zero_def : (0 : α →₁ γ) = ⟨(0 : α →ₘ γ), ae_eq_fun.integrable_zero⟩ := rfl lemma add_def (f g : α →₁ γ) : f + g = ⟨f.1 + g.1, ae_eq_fun.integrable_add f.2 g.2⟩ := rfl instance : has_norm (α →₁ γ) := ⟨λ f, dist f 0⟩ lemma norm_def (f : α →₁ γ) : (norm f) = ennreal.to_real (edist f.1 0) := rfl instance : normed_group (α →₁ γ) := normed_group.of_add_dist (λ x, rfl) $ by { rintros ⟨f, _⟩ ⟨g, _⟩ ⟨h, _⟩, simp only [dist_def, add_def], rw [edist_eq_add_add] } end normed_group section normed_space variables {K : Type*} [normed_field K] [second_countable_topology K] [normed_space K γ] protected def smul : K → (α →₁ γ) → (α →₁ γ) := λ x f, ⟨x • f.1, ae_eq_fun.integrable_smul f.2⟩ instance : has_scalar K (α →₁ γ) := ⟨l1.smul⟩ lemma smul_def {x : K} {f : α →₁ γ} : x • f = ⟨x • f.1, ae_eq_fun.integrable_smul f.2⟩ := rfl local attribute [simp] smul_def norm_def add_def zero_def dist_def ext_iff instance : semimodule K (α →₁ γ) := { one_smul := by { rintros ⟨f, hf⟩, simp [ae_eq_fun.semimodule.one_smul] }, mul_smul := by { rintros x y ⟨f, hf⟩, simp [ae_eq_fun.semimodule.mul_smul] }, smul_add := by { rintros x ⟨f, hf⟩ ⟨g, hg⟩, simp [smul_add] }, smul_zero := by { assume x, simp [smul_zero x] }, add_smul := by { rintros x y ⟨f, hf⟩, simp [add_smul x y f] }, zero_smul := by { rintro ⟨f, hf⟩, simp [zero_smul K f] } } instance : vector_space K (α →₁ γ) := { .. l1.semimodule } instance : normed_space K (α →₁ γ) := ⟨ begin rintros x ⟨f, hf⟩, show ennreal.to_real (edist (x • f) 0) = ∥x∥ * ennreal.to_real (edist f 0), rw [edist_smul, to_real_of_real_mul], exact norm_nonneg _ end ⟩ end normed_space end l1 end measure_theory
ff1a015e157fb4ec36caec4cc0bea4bd2de01513
5e3548e65f2c037cb94cd5524c90c623fbd6d46a
/src_icannos_totilas/reduction/cpge_reduction_012_b.lean
23e72ae4c0a32f10a65a136e1293896cf940f0cb
[]
no_license
ahayat16/lean_exos
d4f08c30adb601a06511a71b5ffb4d22d12ef77f
682f2552d5b04a8c8eb9e4ab15f875a91b03845c
refs/heads/main
1,693,101,073,585
1,636,479,336,000
1,636,479,336,000
415,000,441
0
0
null
null
null
null
UTF-8
Lean
false
false
1,049
lean
import data.real.basic import data.polynomial.derivative import linear_algebra.basic import linear_algebra.finite_dimensional -- On se place dans le R -espace vectoriel E = R[X] . -- (a) Soit H un sous-espace vectoriel de dimension finie et f un endomorphisme de H. Montrer qu'il existe p ∈ N tel que -- ∀k ≥ p, Ker f k+1 = Ker f k. -- -- Soit F un sous-espace vectoriel de E stable par l'opérateur D de dérivation. -- (b) On suppose que F est de dimension finie non nulle. Montrer que l'endomorphisme induit par D sur R n [X] est nilpotent pour tout n ∈ N . -- Montrer qu'il existe m ∈ N tel que F = R m [X] . -- (c) Montrer que si F est de dimension innie alors F = R[X] . -- (d) Soit g ∈ L(E) tel que g^2 = kId + D avec k ∈ R . Quel est le signe de k ? variable F: submodule real (polynomial real) variable HF: forall p: polynomial real, p ∈ F.carrier -> polynomial.derivative p ∈ F.carrier theorem b (_: finite_dimensional real F): exists k >= 0, (linear_map.restrict polynomial.derivative HF)^ k = 0 := sorry
438bde0d8fbbab21e926e268a15b4ef6fb085b1b
367134ba5a65885e863bdc4507601606690974c1
/src/data/mv_polynomial/invertible.lean
e7ba6f0675eaf222003a2dbb809901a133a0f4c4
[ "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
1,034
lean
/- Copyright (c) 2020 Johan Commelin and Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin and Robert Y. Lewis -/ import data.mv_polynomial.basic import ring_theory.algebra_tower /-! # Invertible polynomials This file is a stub containing some basic facts about invertible elements in the ring of polynomials. -/ open mv_polynomial noncomputable instance mv_polynomial.invertible_C (σ : Type*) {R : Type*} [comm_semiring R] (r : R) [invertible r] : invertible (C r : mv_polynomial σ R) := invertible.map C.to_monoid_hom _ /-- A natural number that is invertible when coerced to a commutative semiring `R` is also invertible when coerced to any polynomial ring with rational coefficients. Short-cut for typeclass resolution. -/ noncomputable instance mv_polynomial.invertible_coe_nat (σ R : Type*) (p : ℕ) [comm_semiring R] [invertible (p : R)] : invertible (p : mv_polynomial σ R) := is_scalar_tower.invertible_algebra_coe_nat R _ _
64bb404e24ef6af52d1cb33e27ad976ddccc7ba4
367134ba5a65885e863bdc4507601606690974c1
/src/data/finsupp/lattice.lean
fe6beb9bea6fe47f0cb631846f528b6a74d2e28c
[ "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
3,163
lean
/- Copyright (c) 2020 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson -/ import data.finsupp.basic import algebra.ordered_group /-! # Lattice structure on finsupps This file provides instances of ordered structures on finsupps. -/ open_locale classical noncomputable theory variables {α : Type*} {β : Type*} [has_zero β] {μ : Type*} [canonically_ordered_add_monoid μ] variables {γ : Type*} [canonically_linear_ordered_add_monoid γ] namespace finsupp instance : order_bot (α →₀ μ) := { bot := 0, bot_le := by simp [finsupp.le_def, ← bot_eq_zero], .. finsupp.partial_order} instance [semilattice_inf β] : semilattice_inf (α →₀ β) := { inf := zip_with (⊓) inf_idem, inf_le_left := λ a b c, inf_le_left, inf_le_right := λ a b c, inf_le_right, le_inf := λ a b c h1 h2 s, le_inf (h1 s) (h2 s), ..finsupp.partial_order, } @[simp] lemma inf_apply [semilattice_inf β] {a : α} {f g : α →₀ β} : (f ⊓ g) a = f a ⊓ g a := rfl @[simp] lemma support_inf {f g : α →₀ γ} : (f ⊓ g).support = f.support ∩ g.support := begin ext, simp only [inf_apply, mem_support_iff, ne.def, finset.mem_union, finset.mem_filter, finset.mem_inter], rw [← decidable.not_or_iff_and_not, ← not_congr], rw inf_eq_min, unfold min, split_ifs; { try {apply or_iff_left_of_imp}, try {apply or_iff_right_of_imp}, intro con, rw con at h, revert h, simp, } end instance [semilattice_sup β] : semilattice_sup (α →₀ β) := { sup := zip_with (⊔) sup_idem, le_sup_left := λ a b c, le_sup_left, le_sup_right := λ a b c, le_sup_right, sup_le := λ a b c h1 h2 s, sup_le (h1 s) (h2 s), ..finsupp.partial_order, } @[simp] lemma sup_apply [semilattice_sup β] {a : α} {f g : α →₀ β} : (f ⊔ g) a = f a ⊔ g a := rfl @[simp] lemma support_sup {f g : α →₀ γ} : (f ⊔ g).support = f.support ∪ g.support := begin ext, simp only [finset.mem_union, mem_support_iff, sup_apply, ne.def, ← bot_eq_zero], rw sup_eq_bot_iff, tauto, end instance lattice [lattice β] : lattice (α →₀ β) := { .. finsupp.semilattice_inf, .. finsupp.semilattice_sup} instance semilattice_inf_bot : semilattice_inf_bot (α →₀ γ) := { ..finsupp.order_bot, ..finsupp.lattice} lemma bot_eq_zero : (⊥ : α →₀ γ) = 0 := rfl lemma disjoint_iff {x y : α →₀ γ} : disjoint x y ↔ disjoint x.support y.support := begin unfold disjoint, repeat {rw le_bot_iff}, rw [finsupp.bot_eq_zero, ← finsupp.support_eq_empty, finsupp.support_inf], refl, end variable [partial_order β] /-- The order on `finsupp`s over a partial order embeds into the order on functions -/ def order_embedding_to_fun : (α →₀ β) ↪o (α → β) := ⟨⟨λ (f : α →₀ β) (a : α), f a, λ f g h, finsupp.ext (λ a, by { dsimp at h, rw h,} )⟩, λ a b, (@le_def _ _ _ _ a b).symm⟩ @[simp] lemma order_embedding_to_fun_apply {f : α →₀ β} {a : α} : order_embedding_to_fun f a = f a := rfl lemma monotone_to_fun : monotone (finsupp.to_fun : (α →₀ β) → (α → β)) := λ f g h a, le_def.1 h a end finsupp
ffdc633d314aa954da32257f3fb0e9cef0ebb743
491068d2ad28831e7dade8d6dff871c3e49d9431
/library/data/nat/examples/fib.lean
53dd3a58d8dfd6d01908a08575ceca55e88771b6
[ "Apache-2.0" ]
permissive
davidmueller13/lean
65a3ed141b4088cd0a268e4de80eb6778b21a0e9
c626e2e3c6f3771e07c32e82ee5b9e030de5b050
refs/heads/master
1,611,278,313,401
1,444,021,177,000
1,444,021,177,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,292
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ import data.nat open nat definition fib : nat → nat | 0 := 1 | 1 := 1 | (n+2) := fib (n+1) + fib n private definition fib_fast_aux : nat → (nat × nat) | 0 := (0, 1) | 1 := (1, 1) | (n+2) := match fib_fast_aux (n+1) with | (fn, fn1) := (fn1, fn1 + fn) end open prod.ops -- Get .1 .2 notation for pairs definition fib_fast (n : nat) := (fib_fast_aux n).2 -- We now prove that fib_fast and fib are equal lemma fib_fast_aux_lemma : ∀ n, (fib_fast_aux (succ n)).1 = (fib_fast_aux n).2 | 0 := rfl | 1 := rfl | (succ (succ n)) := begin unfold fib_fast_aux at {1}, rewrite [-prod.eta (fib_fast_aux _)], end theorem fib_eq_fib_fast : ∀ n, fib_fast n = fib n | 0 := rfl | 1 := rfl | (n+2) := begin have feq : fib_fast n = fib n, from fib_eq_fib_fast n, have f1eq : fib_fast (succ n) = fib (succ n), from fib_eq_fib_fast (succ n), unfold [fib, fib_fast, fib_fast_aux], rewrite [-prod.eta (fib_fast_aux _)], fold fib_fast (succ n), rewrite f1eq, rewrite fib_fast_aux_lemma, fold fib_fast n, rewrite feq, end
d795e5152676a682c2c1f7b89d5bc2283a472883
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/list/tfae_auto.lean
16d92c0a4dc2922cef5a9259a4a4797229699ef1
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
2,321
lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Simon Hudon -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.list.basic import Mathlib.PostPort namespace Mathlib namespace list /-- tfae: The Following (propositions) Are Equivalent. The `tfae_have` and `tfae_finish` tactics can be useful in proofs with `tfae` goals. -/ def tfae (l : List Prop) := ∀ (x : Prop), x ∈ l → ∀ (y : Prop), y ∈ l → (x ↔ y) theorem tfae_nil : tfae [] := forall_mem_nil fun (x : Prop) => ∀ (y : Prop), y ∈ [] → (x ↔ y) theorem tfae_singleton (p : Prop) : tfae [p] := sorry theorem tfae_cons_of_mem {a : Prop} {b : Prop} {l : List Prop} (h : b ∈ l) : tfae (a :: l) ↔ (a ↔ b) ∧ tfae l := sorry theorem tfae_cons_cons {a : Prop} {b : Prop} {l : List Prop} : tfae (a :: b :: l) ↔ (a ↔ b) ∧ tfae (b :: l) := tfae_cons_of_mem (Or.inl rfl) theorem tfae_of_forall (b : Prop) (l : List Prop) (h : ∀ (a : Prop), a ∈ l → (a ↔ b)) : tfae l := fun (a₁ : Prop) (h₁ : a₁ ∈ l) (a₂ : Prop) (h₂ : a₂ ∈ l) => iff.trans (h a₁ h₁) (iff.symm (h a₂ h₂)) theorem tfae_of_cycle {a : Prop} {b : Prop} {l : List Prop} : chain (fun (_x _y : Prop) => _x → _y) a (b :: l) → (ilast' b l → a) → tfae (a :: b :: l) := sorry theorem tfae.out {l : List Prop} (h : tfae l) (n₁ : ℕ) (n₂ : ℕ) {a : Prop} {b : Prop} (h₁ : autoParam (nth l n₁ = some a) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.tactic.interactive.refl") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "tactic") "interactive") "refl") [])) (h₂ : autoParam (nth l n₂ = some b) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.tactic.interactive.refl") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "tactic") "interactive") "refl") [])) : a ↔ b := h a (nth_mem h₁) b (nth_mem h₂) end Mathlib
177d021041be0ffb807eb1b6fb831470c344267e
6fbf10071e62af7238f2de8f9aa83d55d8763907
/hw/hw8.lean
5442c5a2f571f518c0c3dceb907670931e29955c
[]
no_license
HasanMukati/uva-cs-dm-s19
ee5aad4568a3ca330c2738ed579c30e1308b03b0
3e7177682acdb56a2d16914e0344c10335583dcf
refs/heads/master
1,596,946,213,130
1,568,221,949,000
1,568,221,949,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,762
lean
/- 1a. [10 points] Implement a function, bool_implies, that takes two bool values, b1 and b2 (not propositions), and that returns the the value of b1 → b2. Here the → operator means implication in Boolean algebra. To know what your function should do, write out the truth table for implication. You may not use Lean's → operator (which does work for bool values, by the way) in your answer. Define the function by cases. -/ def bool_implies : bool → bool → bool | _ := _ /- 1b. Given your implementation of bool_implies, prove the following proposition. Use induction rather than the cases tactic to do case analysis on Boolean values. -/ example : ∀ b1 b2 : bool, (bool_implies b1 b2 = tt) → (b2 = tt ∨ b1 = ff) := begin _ end /- 2a. A tree of natural numbers is either empty or it is constructed from a natural number and two smaller trees of natural numbers. Give an inductive definition of the type of such trees. Call you datatype tree_nat. Your type will have two constructors. Call the first one "empty", and call the second "node". Hint: The second will take three arguments. -/ inductive tree_nat : Type | _ /- 2b. Define aTree to be a node containing the value 3 and two trees, the first one empty and the second one a node containing the value 2 and two empty trees. -/ def aTree := _ /- 3. Define a polymorphic type, "tree", just like tree_nat, but where the value stored in a node can be of any type, T. Hint: make the type of elements a parameter. You can look at the myprod type for an example. Then define aTree' to be the same as aTree except that it's now of type tree rather than of type tree_nat. Make the type argument implicit. Finally define a tree of strings, aTree'', just like aTree' except that 3 is replaced by "Hi!" and 2 is replaced by "Jo". -/ -- Your answer here /- 4. Write a recursive function, num_nodes, that takes a value of type tree T, as an argument, where T is some type, and that returns the number of nodes in the tree. The number of nodes in an empty tree is zero, while the number of nodes in a non-empty tree is one (for the "top" node) plus the number of nodes in each of the subtrees. The "at sign" before "tree" in the following function signature tells Lean that even though tree takes its type argument implicitly, in this case we want to give it explicitly. We need to specify T explicitly here because Lean has no way of knowing that we want the type of elements to be T. -/ def num_nodes : ∀ {T : Type}, @tree T → nat | T tree.empty := _ /- The following questions use our definition of mynat to practice proof by induction. Here is our mynat type and the implementations of addition and multiplication. -/ inductive mynat : Type | zero : mynat | succ : mynat → mynat def add_mynat: mynat → mynat → mynat | mynat.zero m := m | (mynat.succ n') m := mynat.succ (add_mynat n' m) def mult_mynat: mynat → mynat → mynat | mynat.zero m := mynat.zero | (mynat.succ n') m := add_mynat m (mult_mynat n' m) /- Here's a proof that zero is a right identity for addition. We explain details in comments. You will want to use some of the same ideas in your proofs. -/ theorem zero_right_id_add : ∀ (n : mynat), add_mynat n mynat.zero = n := begin -- forall introduction intro n, -- induction induction n with n' ih, -- base case exact rfl, -- recursive case -- first simplify based on definition of add_mynat simp [add_mynat], -- now apply induction hypothesis exact ih, end /- #5 Prove the following by induction on n in Lean. -/ -- 5b. Prove that succ (n + m) = n + (succ m). theorem add_succ : ∀ (n m : mynat), mynat.succ (add_mynat n m) = add_mynat n (mynat.succ m) := begin _ end -- 5a. Prove zero is a right identity for mult. theorem zero_right_absorb_mult : ∀ (n : mynat), mult_mynat n mynat.zero = mynat.zero := begin _ end -- 5b. Prove addition is associative. theorem addition_assoc : ∀ (n m p : mynat), add_mynat (add_mynat n m) p = add_mynat n (add_mynat m p) := begin intros n m p, induction n, exact rfl, simp [add_mynat], assumption, end -- 5c. Prove addition is commutative. theorem addition_comm : ∀ (n m : mynat), add_mynat n m = add_mynat m n := begin _ end /- 6a. Complete then test the following definition of a function that computes the n'th Fibonacci number when given n as an argument. -/ def fib : ℕ → ℕ | _ := _ /- 6b. Implement the factorial function. You will need to define the function for both its base and recursive cases. -/ def fac : ℕ → ℕ | _ := _ /- 7. Give an *informal proof by induction of the proposition that forall natural numbers, n, the sum of the natural numbers from 0 to n is n * (n + 1) / 2. -/
dd81338259838e1ff75355607e0f875d74c1ddfb
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/let3.lean
87315a552461c7632ed142e45ea6fd82677abd9d
[ "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
900
lean
definition p1 := (10, 20, 30) definition v1 : nat := let (a, b, c) := p1 in a + b + c definition v2 : nat := let ⟨a, b, c⟩ := p1 in a + b + c example : v2 = 60 := rfl /- let with patterns is just syntax sugar for the match convoy pattern. let (a, b, c) := p1 in a + b + c is encoded as match p1 with (a, b, c) := a + b + c end One limitation is that the expexted type of the let-expression must be known. TODO(Leo): improve visit_convoy in the elaborator, and remove this restriction. -/ #eval (let (a, b, c) := p1 in a + b : nat) -- We have to provide the type. #eval (let (a, b) := p1, (c, d) := b in c + d : nat) definition v3 : nat := let (a, b) := p1, (c, d) := b in c + d example : v3 = 50 := rfl #check (let (a, b, c) := p1 in a + b : nat) -- We have to provide the type. #reduce (let (a, b, c) := p1 in a + b : nat) -- We have to provide the type.
1c864572a74aaa2d3472b7e7e4edfda6aa06c943
649957717d58c43b5d8d200da34bf374293fe739
/src/category_theory/limits/functor_category.lean
eb8da8bf5a65b56b243336328346b4da7596e94f
[ "Apache-2.0" ]
permissive
Vtec234/mathlib
b50c7b21edea438df7497e5ed6a45f61527f0370
fb1848bbbfce46152f58e219dc0712f3289d2b20
refs/heads/master
1,592,463,095,113
1,562,737,749,000
1,562,737,749,000
196,202,858
0
0
Apache-2.0
1,562,762,338,000
1,562,762,337,000
null
UTF-8
Lean
false
false
4,863
lean
-- Copyright (c) 2018 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Scott Morrison import category_theory.products import category_theory.limits.preserves open category_theory category_theory.category namespace category_theory.limits universes v u -- declare the `v`'s first; see `category_theory.category` for an explanation variables {C : Type u} [𝒞 : category.{v+1} C] include 𝒞 variables {J K : Type v} [small_category J] [small_category K] @[simp] lemma cone.functor_w {F : J ⥤ (K ⥤ C)} (c : cone F) {j j' : J} (f : j ⟶ j') (k : K) : (c.π.app j).app k ≫ (F.map f).app k = (c.π.app j').app k := by convert ←nat_trans.congr_app (c.π.naturality f).symm k; apply id_comp @[simp] lemma cocone.functor_w {F : J ⥤ (K ⥤ C)} (c : cocone F) {j j' : J} (f : j ⟶ j') (k : K) : (F.map f).app k ≫ (c.ι.app j').app k = (c.ι.app j).app k := by convert ←nat_trans.congr_app (c.ι.naturality f) k; apply comp_id @[simp] def functor_category_limit_cone [has_limits_of_shape J C] (F : J ⥤ K ⥤ C) : cone F := { X := F.flip ⋙ lim, π := { app := λ j, { app := λ k, limit.π (F.flip.obj k) j }, naturality' := λ j j' f, by ext k; convert (limit.w (F.flip.obj k) _).symm using 1; apply id_comp } } @[simp] def functor_category_colimit_cocone [has_colimits_of_shape J C] (F : J ⥤ K ⥤ C) : cocone F := { X := F.flip ⋙ colim, ι := { app := λ j, { app := λ k, colimit.ι (F.flip.obj k) j }, naturality' := λ j j' f, by ext k; convert (colimit.w (F.flip.obj k) _) using 1; apply comp_id } } @[simp] def evaluate_functor_category_limit_cone [has_limits_of_shape J C] (F : J ⥤ K ⥤ C) (k : K) : ((evaluation K C).obj k).map_cone (functor_category_limit_cone F) ≅ limit.cone (F.flip.obj k) := cones.ext (iso.refl _) (by tidy) @[simp] def evaluate_functor_category_colimit_cocone [has_colimits_of_shape J C] (F : J ⥤ K ⥤ C) (k : K) : ((evaluation K C).obj k).map_cocone (functor_category_colimit_cocone F) ≅ colimit.cocone (F.flip.obj k) := cocones.ext (iso.refl _) (by tidy) def functor_category_is_limit_cone [has_limits_of_shape J C] (F : J ⥤ K ⥤ C) : is_limit (functor_category_limit_cone F) := { lift := λ s, { app := λ k, limit.lift (F.flip.obj k) (((evaluation K C).obj k).map_cone s) }, uniq' := λ s m w, begin ext1 k, exact is_limit.uniq _ (((evaluation K C).obj k).map_cone s) (m.app k) (λ j, nat_trans.congr_app (w j) k) end } def functor_category_is_colimit_cocone [has_colimits_of_shape.{v} J C] (F : J ⥤ K ⥤ C) : is_colimit (functor_category_colimit_cocone F) := { desc := λ s, { app := λ k, colimit.desc (F.flip.obj k) (((evaluation K C).obj k).map_cocone s) }, uniq' := λ s m w, begin ext1 k, exact is_colimit.uniq _ (((evaluation K C).obj k).map_cocone s) (m.app k) (λ j, nat_trans.congr_app (w j) k) end } instance functor_category_has_limits_of_shape [has_limits_of_shape J C] : has_limits_of_shape J (K ⥤ C) := { has_limit := λ F, { cone := functor_category_limit_cone F, is_limit := functor_category_is_limit_cone F } } instance functor_category_has_colimits_of_shape [has_colimits_of_shape J C] : has_colimits_of_shape J (K ⥤ C) := { has_colimit := λ F, { cocone := functor_category_colimit_cocone F, is_colimit := functor_category_is_colimit_cocone F } } instance functor_category_has_limits [has_limits.{v} C] : has_limits.{v} (K ⥤ C) := { has_limits_of_shape := λ J 𝒥, by resetI; apply_instance } instance functor_category_has_colimits [has_colimits.{v} C] : has_colimits.{v} (K ⥤ C) := { has_colimits_of_shape := λ J 𝒥, by resetI; apply_instance } instance evaluation_preserves_limits_of_shape [has_limits_of_shape J C] (k : K) : preserves_limits_of_shape J ((evaluation K C).obj k) := { preserves_limit := λ F, preserves_limit_of_preserves_limit_cone (limit.is_limit _) $ is_limit.of_iso_limit (limit.is_limit _) (evaluate_functor_category_limit_cone F k).symm } instance evaluation_preserves_colimits_of_shape [has_colimits_of_shape J C] (k : K) : preserves_colimits_of_shape J ((evaluation K C).obj k) := { preserves_colimit := λ F, preserves_colimit_of_preserves_colimit_cocone (colimit.is_colimit _) $ is_colimit.of_iso_colimit (colimit.is_colimit _) (evaluate_functor_category_colimit_cocone F k).symm } instance evaluation_preserves_limits [has_limits.{v} C] (k : K) : preserves_limits ((evaluation K C).obj k) := { preserves_limits_of_shape := λ J 𝒥, by resetI; apply_instance } instance evaluation_preserves_colimits [has_colimits.{v} C] (k : K) : preserves_colimits ((evaluation K C).obj k) := { preserves_colimits_of_shape := λ J 𝒥, by resetI; apply_instance } end category_theory.limits
8a694ea84e1a4e3806a287fe29f3d1a935f25d5a
3f7026ea8bef0825ca0339a275c03b911baef64d
/src/topology/uniform_space/basic.lean
fd233bd8ac647bc49db60538c5ff8a821a306846
[ "Apache-2.0" ]
permissive
rspencer01/mathlib
b1e3afa5c121362ef0881012cc116513ab09f18c
c7d36292c6b9234dc40143c16288932ae38fdc12
refs/heads/master
1,595,010,346,708
1,567,511,503,000
1,567,511,503,000
206,071,681
0
0
Apache-2.0
1,567,513,643,000
1,567,513,643,000
null
UTF-8
Lean
false
false
42,459
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 Theory of uniform spaces. Uniform spaces are a generalization of metric spaces and topological groups. Many concepts directly generalize to uniform spaces, e.g. * completeness * extension of uniform continuous functions to complete spaces * uniform contiunuity & embedding * totally bounded * totally bounded ∧ complete → compact The central concept of uniform spaces is its uniformity: a filter relating two elements of the space. This filter is reflexive, symmetric and transitive. So a set (i.e. a relation) in this filter represents a 'distance': it is reflexive, symmetric and the uniformity contains a set for which the `triangular` rule holds. The formalization is mostly based on the books: N. Bourbaki: General Topology I. M. James: Topologies and Uniformities A major difference is that this formalization is heavily based on the filter library. -/ import order.filter.basic order.filter.lift topology.constructions open set lattice filter classical local attribute [instance, priority 0] prop_decidable set_option eqn_compiler.zeta true universes u section variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} {ι : Sort*} /-- The identity relation, or the graph of the identity function -/ def id_rel {α : Type*} := {p : α × α | p.1 = p.2} @[simp] theorem mem_id_rel {a b : α} : (a, b) ∈ @id_rel α ↔ a = b := iff.rfl @[simp] theorem id_rel_subset {s : set (α × α)} : id_rel ⊆ s ↔ ∀ a, (a, a) ∈ s := by simp [subset_def]; exact forall_congr (λ a, by simp) /-- The composition of relations -/ def comp_rel {α : Type u} (r₁ r₂ : set (α×α)) := {p : α × α | ∃z:α, (p.1, z) ∈ r₁ ∧ (z, p.2) ∈ r₂} @[simp] theorem mem_comp_rel {r₁ r₂ : set (α×α)} {x y : α} : (x, y) ∈ comp_rel r₁ r₂ ↔ ∃ z, (x, z) ∈ r₁ ∧ (z, y) ∈ r₂ := iff.rfl @[simp] theorem swap_id_rel : prod.swap '' id_rel = @id_rel α := set.ext $ assume ⟨a, b⟩, by simp [image_swap_eq_preimage_swap]; exact eq_comm theorem monotone_comp_rel [preorder β] {f g : β → set (α×α)} (hf : monotone f) (hg : monotone g) : monotone (λx, comp_rel (f x) (g x)) := assume a b h p ⟨z, h₁, h₂⟩, ⟨z, hf h h₁, hg h h₂⟩ lemma prod_mk_mem_comp_rel {a b c : α} {s t : set (α×α)} (h₁ : (a, c) ∈ s) (h₂ : (c, b) ∈ t) : (a, b) ∈ comp_rel s t := ⟨c, h₁, h₂⟩ @[simp] lemma id_comp_rel {r : set (α×α)} : comp_rel id_rel r = r := set.ext $ assume ⟨a, b⟩, by simp lemma comp_rel_assoc {r s t : set (α×α)} : comp_rel (comp_rel r s) t = comp_rel r (comp_rel s t) := by ext p; cases p; simp only [mem_comp_rel]; tauto /-- This core description of a uniform space is outside of the type class hierarchy. It is useful for constructions of uniform spaces, when the topology is derived from the uniform space. -/ structure uniform_space.core (α : Type u) := (uniformity : filter (α × α)) (refl : principal id_rel ≤ uniformity) (symm : tendsto prod.swap uniformity uniformity) (comp : uniformity.lift' (λs, comp_rel s s) ≤ uniformity) def uniform_space.core.mk' {α : Type u} (U : filter (α × α)) (refl : ∀ (r ∈ U) x, (x, x) ∈ r) (symm : ∀ r ∈ U, {p | prod.swap p ∈ r} ∈ U) (comp : ∀ r ∈ U, ∃ t ∈ U, comp_rel t t ⊆ r) : uniform_space.core α := ⟨U, λ r ru, id_rel_subset.2 (refl _ ru), symm, begin intros r ru, rw [mem_lift'_sets], exact comp _ ru, apply monotone_comp_rel; exact monotone_id, end⟩ /-- A uniform space generates a topological space -/ def uniform_space.core.to_topological_space {α : Type u} (u : uniform_space.core α) : topological_space α := { is_open := λs, ∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ u.uniformity, is_open_univ := by simp; intro; exact univ_mem_sets, is_open_inter := assume s t hs ht x ⟨xs, xt⟩, by filter_upwards [hs x xs, ht x xt]; simp {contextual := tt}, is_open_sUnion := assume s hs x ⟨t, ts, xt⟩, by filter_upwards [hs t ts x xt] assume p ph h, ⟨t, ts, ph h⟩ } lemma uniform_space.core_eq : ∀{u₁ u₂ : uniform_space.core α}, u₁.uniformity = u₂.uniformity → u₁ = u₂ | ⟨u₁, _, _, _⟩ ⟨u₂, _, _, _⟩ h := have u₁ = u₂, from h, by simp [*] /-- A uniform space is a generalization of the "uniform" topological aspects of a metric space. It consists of a filter on `α × α` called the "uniformity", which satisfies properties analogous to the reflexivity, symmetry, and triangle properties of a metric. A metric space has a natural uniformity, and a uniform space has a natural topology. A topological group also has a natural uniformity, even when it is not metrizable. -/ class uniform_space (α : Type u) extends topological_space α, uniform_space.core α := (is_open_uniformity : ∀s, is_open s ↔ (∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ uniformity)) @[pattern] def uniform_space.mk' {α} (t : topological_space α) (c : uniform_space.core α) (is_open_uniformity : ∀s:set α, t.is_open s ↔ (∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ c.uniformity)) : uniform_space α := ⟨c, is_open_uniformity⟩ def uniform_space.of_core {α : Type u} (u : uniform_space.core α) : uniform_space α := { to_core := u, to_topological_space := u.to_topological_space, is_open_uniformity := assume a, iff.refl _ } def uniform_space.of_core_eq {α : Type u} (u : uniform_space.core α) (t : topological_space α) (h : t = u.to_topological_space) : uniform_space α := { to_core := u, to_topological_space := t, is_open_uniformity := assume a, h.symm ▸ iff.refl _ } lemma uniform_space.to_core_to_topological_space (u : uniform_space α) : u.to_core.to_topological_space = u.to_topological_space := topological_space_eq $ funext $ assume s, by rw [uniform_space.core.to_topological_space, uniform_space.is_open_uniformity] @[extensionality] lemma uniform_space_eq : ∀{u₁ u₂ : uniform_space α}, u₁.uniformity = u₂.uniformity → u₁ = u₂ | (uniform_space.mk' t₁ u₁ o₁) (uniform_space.mk' t₂ u₂ o₂) h := have u₁ = u₂, from uniform_space.core_eq h, have t₁ = t₂, from topological_space_eq $ funext $ assume s, by rw [o₁, o₂]; simp [this], by simp [*] lemma uniform_space.of_core_eq_to_core (u : uniform_space α) (t : topological_space α) (h : t = u.to_core.to_topological_space) : uniform_space.of_core_eq u.to_core t h = u := uniform_space_eq rfl section uniform_space variables [uniform_space α] /-- The uniformity is a filter on α × α (inferred from an ambient uniform space structure on α). -/ def uniformity (α : Type u) [uniform_space α] : filter (α × α) := (@uniform_space.to_core α _).uniformity local notation `𝓤` := uniformity lemma is_open_uniformity {s : set α} : is_open s ↔ (∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ 𝓤 α) := uniform_space.is_open_uniformity s lemma refl_le_uniformity : principal id_rel ≤ 𝓤 α := (@uniform_space.to_core α _).refl lemma refl_mem_uniformity {x : α} {s : set (α × α)} (h : s ∈ 𝓤 α) : (x, x) ∈ s := refl_le_uniformity h rfl lemma symm_le_uniformity : map (@prod.swap α α) (𝓤 _) ≤ (𝓤 _) := (@uniform_space.to_core α _).symm lemma comp_le_uniformity : (𝓤 α).lift' (λs:set (α×α), comp_rel s s) ≤ 𝓤 α := (@uniform_space.to_core α _).comp lemma tendsto_swap_uniformity : tendsto (@prod.swap α α) (𝓤 α) (𝓤 α) := symm_le_uniformity lemma tendsto_const_uniformity {a : α} {f : filter β} : tendsto (λ _, (a, a)) f (𝓤 α) := assume s hs, show {x | (a, a) ∈ s} ∈ f, from univ_mem_sets' $ assume b, refl_mem_uniformity hs lemma comp_mem_uniformity_sets {s : set (α × α)} (hs : s ∈ 𝓤 α) : ∃ t ∈ 𝓤 α, comp_rel t t ⊆ s := have s ∈ (𝓤 α).lift' (λt:set (α×α), comp_rel t t), from comp_le_uniformity hs, (mem_lift'_sets $ monotone_comp_rel monotone_id monotone_id).mp this lemma symm_of_uniformity {s : set (α × α)} (hs : s ∈ 𝓤 α) : ∃ t ∈ 𝓤 α, (∀a b, (a, b) ∈ t → (b, a) ∈ t) ∧ t ⊆ s := have preimage prod.swap s ∈ 𝓤 α, from symm_le_uniformity hs, ⟨s ∩ preimage prod.swap s, inter_mem_sets hs this, assume a b ⟨h₁, h₂⟩, ⟨h₂, h₁⟩, inter_subset_left _ _⟩ lemma comp_symm_of_uniformity {s : set (α × α)} (hs : s ∈ 𝓤 α) : ∃ t ∈ 𝓤 α, (∀{a b}, (a, b) ∈ t → (b, a) ∈ t) ∧ comp_rel t t ⊆ s := let ⟨t, ht₁, ht₂⟩ := comp_mem_uniformity_sets hs in let ⟨t', ht', ht'₁, ht'₂⟩ := symm_of_uniformity ht₁ in ⟨t', ht', ht'₁, subset.trans (monotone_comp_rel monotone_id monotone_id ht'₂) ht₂⟩ lemma uniformity_le_symm : 𝓤 α ≤ (@prod.swap α α) <$> 𝓤 α := by rw [map_swap_eq_comap_swap]; from map_le_iff_le_comap.1 tendsto_swap_uniformity lemma uniformity_eq_symm : 𝓤 α = (@prod.swap α α) <$> 𝓤 α := le_antisymm uniformity_le_symm symm_le_uniformity theorem uniformity_lift_le_swap {g : set (α×α) → filter β} {f : filter β} (hg : monotone g) (h : (𝓤 α).lift (λs, g (preimage prod.swap s)) ≤ f) : (𝓤 α).lift g ≤ f := calc (𝓤 α).lift g ≤ (filter.map (@prod.swap α α) $ 𝓤 α).lift g : lift_mono uniformity_le_symm (le_refl _) ... ≤ _ : by rw [map_lift_eq2 hg, image_swap_eq_preimage_swap]; exact h lemma uniformity_lift_le_comp {f : set (α×α) → filter β} (h : monotone f) : (𝓤 α).lift (λs, f (comp_rel s s)) ≤ (𝓤 α).lift f := calc (𝓤 α).lift (λs, f (comp_rel s s)) = ((𝓤 α).lift' (λs:set (α×α), comp_rel s s)).lift f : begin rw [lift_lift'_assoc], exact monotone_comp_rel monotone_id monotone_id, exact h end ... ≤ (𝓤 α).lift f : lift_mono comp_le_uniformity (le_refl _) lemma comp_le_uniformity3 : (𝓤 α).lift' (λs:set (α×α), comp_rel s (comp_rel s s)) ≤ (𝓤 α) := calc (𝓤 α).lift' (λd, comp_rel d (comp_rel d d)) = (𝓤 α).lift (λs, (𝓤 α).lift' (λt:set(α×α), comp_rel s (comp_rel t t))) : begin rw [lift_lift'_same_eq_lift'], exact (assume x, monotone_comp_rel monotone_const $ monotone_comp_rel monotone_id monotone_id), exact (assume x, monotone_comp_rel monotone_id monotone_const), end ... ≤ (𝓤 α).lift (λs, (𝓤 α).lift' (λt:set(α×α), comp_rel s t)) : lift_mono' $ assume s hs, @uniformity_lift_le_comp α _ _ (principal ∘ comp_rel s) $ monotone_comp (monotone_comp_rel monotone_const monotone_id) monotone_principal ... = (𝓤 α).lift' (λs:set(α×α), comp_rel s s) : lift_lift'_same_eq_lift' (assume s, monotone_comp_rel monotone_const monotone_id) (assume s, monotone_comp_rel monotone_id monotone_const) ... ≤ (𝓤 α) : comp_le_uniformity lemma mem_nhds_uniformity_iff {x : α} {s : set α} : s ∈ nhds x ↔ {p : α × α | p.1 = x → p.2 ∈ s} ∈ 𝓤 α := ⟨ begin simp only [mem_nhds_sets_iff, is_open_uniformity, and_imp, exists_imp_distrib], exact assume t ts ht xt, by filter_upwards [ht x xt] assume ⟨x', y⟩ h eq, ts $ h eq end, assume hs, mem_nhds_sets_iff.mpr ⟨{x | {p : α × α | p.1 = x → p.2 ∈ s} ∈ 𝓤 α}, assume x' hx', refl_mem_uniformity hx' rfl, is_open_uniformity.mpr $ assume x' hx', let ⟨t, ht, tr⟩ := comp_mem_uniformity_sets hx' in by filter_upwards [ht] assume ⟨a, b⟩ hp' (hax' : a = x'), by filter_upwards [ht] assume ⟨a, b'⟩ hp'' (hab : a = b), have hp : (x', b) ∈ t, from hax' ▸ hp', have (b, b') ∈ t, from hab ▸ hp'', have (x', b') ∈ comp_rel t t, from ⟨b, hp, this⟩, show b' ∈ s, from tr this rfl, hs⟩⟩ lemma nhds_eq_comap_uniformity {x : α} : nhds x = (𝓤 α).comap (prod.mk x) := by ext s; rw [mem_nhds_uniformity_iff, mem_comap_sets]; from iff.intro (assume hs, ⟨_, hs, assume x hx, hx rfl⟩) (assume ⟨t, h, ht⟩, (𝓤 α).sets_of_superset h $ assume ⟨p₁, p₂⟩ hp (h : p₁ = x), ht $ by simp [h.symm, hp]) lemma nhds_eq_uniformity {x : α} : nhds x = (𝓤 α).lift' (λs:set (α×α), {y | (x, y) ∈ s}) := begin ext s, rw [mem_lift'_sets], tactic.swap, apply monotone_preimage, simp [mem_nhds_uniformity_iff], exact ⟨assume h, ⟨_, h, assume y h, h rfl⟩, assume ⟨t, h₁, h₂⟩, (𝓤 α).sets_of_superset h₁ $ assume ⟨x', y⟩ hp (eq : x' = x), h₂ $ show (x, y) ∈ t, from eq ▸ hp⟩ end lemma mem_nhds_left (x : α) {s : set (α×α)} (h : s ∈ 𝓤 α) : {y : α | (x, y) ∈ s} ∈ nhds x := have nhds x ≤ principal {y : α | (x, y) ∈ s}, by rw [nhds_eq_uniformity]; exact infi_le_of_le s (infi_le _ h), by simp at this; assumption lemma mem_nhds_right (y : α) {s : set (α×α)} (h : s ∈ 𝓤 α) : {x : α | (x, y) ∈ s} ∈ nhds y := mem_nhds_left _ (symm_le_uniformity h) lemma tendsto_right_nhds_uniformity {a : α} : tendsto (λa', (a', a)) (nhds a) (𝓤 α) := assume s, mem_nhds_right a lemma tendsto_left_nhds_uniformity {a : α} : tendsto (λa', (a, a')) (nhds a) (𝓤 α) := assume s, mem_nhds_left a lemma lift_nhds_left {x : α} {g : set α → filter β} (hg : monotone g) : (nhds x).lift g = (𝓤 α).lift (λs:set (α×α), g {y | (x, y) ∈ s}) := eq.trans begin rw [nhds_eq_uniformity], exact (filter.lift_assoc $ monotone_comp monotone_preimage $ monotone_comp monotone_preimage monotone_principal) end (congr_arg _ $ funext $ assume s, filter.lift_principal hg) lemma lift_nhds_right {x : α} {g : set α → filter β} (hg : monotone g) : (nhds x).lift g = (𝓤 α).lift (λs:set (α×α), g {y | (y, x) ∈ s}) := calc (nhds x).lift g = (𝓤 α).lift (λs:set (α×α), g {y | (x, y) ∈ s}) : lift_nhds_left hg ... = ((@prod.swap α α) <$> (𝓤 α)).lift (λs:set (α×α), g {y | (x, y) ∈ s}) : by rw [←uniformity_eq_symm] ... = (𝓤 α).lift (λs:set (α×α), g {y | (x, y) ∈ image prod.swap s}) : map_lift_eq2 $ monotone_comp monotone_preimage hg ... = _ : by simp [image_swap_eq_preimage_swap] lemma nhds_nhds_eq_uniformity_uniformity_prod {a b : α} : filter.prod (nhds a) (nhds b) = (𝓤 α).lift (λs:set (α×α), (𝓤 α).lift' (λt:set (α×α), set.prod {y : α | (y, a) ∈ s} {y : α | (b, y) ∈ t})) := begin rw [prod_def], show (nhds a).lift (λs:set α, (nhds b).lift (λt:set α, principal (set.prod s t))) = _, rw [lift_nhds_right], apply congr_arg, funext s, rw [lift_nhds_left], refl, exact monotone_comp (monotone_prod monotone_const monotone_id) monotone_principal, exact (monotone_lift' monotone_const $ monotone_lam $ assume x, monotone_prod monotone_id monotone_const) end lemma nhds_eq_uniformity_prod {a b : α} : nhds (a, b) = (𝓤 α).lift' (λs:set (α×α), set.prod {y : α | (y, a) ∈ s} {y : α | (b, y) ∈ s}) := begin rw [nhds_prod_eq, nhds_nhds_eq_uniformity_uniformity_prod, lift_lift'_same_eq_lift'], { intro s, exact monotone_prod monotone_const monotone_preimage }, { intro t, exact monotone_prod monotone_preimage monotone_const } end lemma nhdset_of_mem_uniformity {d : set (α×α)} (s : set (α×α)) (hd : d ∈ 𝓤 α) : ∃(t : set (α×α)), is_open t ∧ s ⊆ t ∧ t ⊆ {p | ∃x y, (p.1, x) ∈ d ∧ (x, y) ∈ s ∧ (y, p.2) ∈ d} := let cl_d := {p:α×α | ∃x y, (p.1, x) ∈ d ∧ (x, y) ∈ s ∧ (y, p.2) ∈ d} in have ∀p ∈ s, ∃t ⊆ cl_d, is_open t ∧ p ∈ t, from assume ⟨x, y⟩ hp, mem_nhds_sets_iff.mp $ show cl_d ∈ nhds (x, y), begin rw [nhds_eq_uniformity_prod, mem_lift'_sets], exact ⟨d, hd, assume ⟨a, b⟩ ⟨ha, hb⟩, ⟨x, y, ha, hp, hb⟩⟩, exact monotone_prod monotone_preimage monotone_preimage end, have ∃t:(Π(p:α×α) (h:p ∈ s), set (α×α)), ∀p, ∀h:p ∈ s, t p h ⊆ cl_d ∧ is_open (t p h) ∧ p ∈ t p h, by simp [classical.skolem] at this; simp; assumption, match this with | ⟨t, ht⟩ := ⟨(⋃ p:α×α, ⋃ h : p ∈ s, t p h : set (α×α)), is_open_Union $ assume (p:α×α), is_open_Union $ assume hp, (ht p hp).right.left, assume ⟨a, b⟩ hp, begin simp; exact ⟨a, b, hp, (ht (a,b) hp).right.right⟩ end, Union_subset $ assume p, Union_subset $ assume hp, (ht p hp).left⟩ end lemma closure_eq_inter_uniformity {t : set (α×α)} : closure t = (⋂ d ∈ 𝓤 α, comp_rel d (comp_rel t d)) := set.ext $ assume ⟨a, b⟩, calc (a, b) ∈ closure t ↔ (nhds (a, b) ⊓ principal t ≠ ⊥) : by simp [closure_eq_nhds] ... ↔ (((@prod.swap α α) <$> 𝓤 α).lift' (λ (s : set (α × α)), set.prod {x : α | (x, a) ∈ s} {y : α | (b, y) ∈ s}) ⊓ principal t ≠ ⊥) : by rw [←uniformity_eq_symm, nhds_eq_uniformity_prod] ... ↔ ((map (@prod.swap α α) (𝓤 α)).lift' (λ (s : set (α × α)), set.prod {x : α | (x, a) ∈ s} {y : α | (b, y) ∈ s}) ⊓ principal t ≠ ⊥) : by refl ... ↔ ((𝓤 α).lift' (λ (s : set (α × α)), set.prod {y : α | (a, y) ∈ s} {x : α | (x, b) ∈ s}) ⊓ principal t ≠ ⊥) : begin rw [map_lift'_eq2], simp [image_swap_eq_preimage_swap, function.comp], exact monotone_prod monotone_preimage monotone_preimage end ... ↔ (∀s ∈ 𝓤 α, ∃x, x ∈ set.prod {y : α | (a, y) ∈ s} {x : α | (x, b) ∈ s} ∩ t) : begin rw [lift'_inf_principal_eq, lift'_neq_bot_iff], apply forall_congr, intro s, rw [ne_empty_iff_exists_mem], exact monotone_inter (monotone_prod monotone_preimage monotone_preimage) monotone_const end ... ↔ (∀ s ∈ 𝓤 α, (a, b) ∈ comp_rel s (comp_rel t s)) : forall_congr $ assume s, forall_congr $ assume hs, ⟨assume ⟨⟨x, y⟩, ⟨⟨hx, hy⟩, hxyt⟩⟩, ⟨x, hx, y, hxyt, hy⟩, assume ⟨x, hx, y, hxyt, hy⟩, ⟨⟨x, y⟩, ⟨⟨hx, hy⟩, hxyt⟩⟩⟩ ... ↔ _ : by simp lemma uniformity_eq_uniformity_closure : 𝓤 α = (𝓤 α).lift' closure := le_antisymm (le_infi $ assume s, le_infi $ assume hs, by simp; filter_upwards [hs] subset_closure) (calc (𝓤 α).lift' closure ≤ (𝓤 α).lift' (λd, comp_rel d (comp_rel d d)) : lift'_mono' (by intros s hs; rw [closure_eq_inter_uniformity]; exact bInter_subset_of_mem hs) ... ≤ (𝓤 α) : comp_le_uniformity3) lemma uniformity_eq_uniformity_interior : 𝓤 α = (𝓤 α).lift' interior := le_antisymm (le_infi $ assume d, le_infi $ assume hd, let ⟨s, hs, hs_comp⟩ := (mem_lift'_sets $ monotone_comp_rel monotone_id $ monotone_comp_rel monotone_id monotone_id).mp (comp_le_uniformity3 hd) in let ⟨t, ht, hst, ht_comp⟩ := nhdset_of_mem_uniformity s hs in have s ⊆ interior d, from calc s ⊆ t : hst ... ⊆ interior d : (subset_interior_iff_subset_of_open ht).mpr $ assume x, assume : x ∈ t, let ⟨x, y, h₁, h₂, h₃⟩ := ht_comp this in hs_comp ⟨x, h₁, y, h₂, h₃⟩, have interior d ∈ 𝓤 α, by filter_upwards [hs] this, by simp [this]) (assume s hs, ((𝓤 α).lift' interior).sets_of_superset (mem_lift' hs) interior_subset) lemma interior_mem_uniformity {s : set (α × α)} (hs : s ∈ 𝓤 α) : interior s ∈ 𝓤 α := by rw [uniformity_eq_uniformity_interior]; exact mem_lift' hs lemma mem_uniformity_is_closed {s : set (α×α)} (h : s ∈ 𝓤 α) : ∃t ∈ 𝓤 α, is_closed t ∧ t ⊆ s := have s ∈ (𝓤 α).lift' closure, by rwa [uniformity_eq_uniformity_closure] at h, have ∃ t ∈ 𝓤 α, closure t ⊆ s, by rwa [mem_lift'_sets] at this; apply closure_mono, let ⟨t, ht, hst⟩ := this in ⟨closure t, (𝓤 α).sets_of_superset ht subset_closure, is_closed_closure, hst⟩ /- uniform continuity -/ def uniform_continuous [uniform_space β] (f : α → β) := tendsto (λx:α×α, (f x.1, f x.2)) (𝓤 α) (𝓤 β) theorem uniform_continuous_def [uniform_space β] {f : α → β} : uniform_continuous f ↔ ∀ r ∈ 𝓤 β, {x : α × α | (f x.1, f x.2) ∈ r} ∈ 𝓤 α := iff.rfl lemma uniform_continuous_of_const [uniform_space β] {c : α → β} (h : ∀a b, c a = c b) : uniform_continuous c := have (λ (x : α × α), (c (x.fst), c (x.snd))) ⁻¹' id_rel = univ, from eq_univ_iff_forall.2 $ assume ⟨a, b⟩, h a b, le_trans (map_le_iff_le_comap.2 $ by simp [comap_principal, this, univ_mem_sets]) refl_le_uniformity lemma uniform_continuous_id : uniform_continuous (@id α) := by simp [uniform_continuous]; exact tendsto_id lemma uniform_continuous_const [uniform_space β] {b : β} : uniform_continuous (λa:α, b) := @tendsto_const_uniformity _ _ _ b (𝓤 α) lemma uniform_continuous.comp [uniform_space β] [uniform_space γ] {f : α → β} {g : β → γ} (hg : uniform_continuous g) (hf : uniform_continuous f) : uniform_continuous (g ∘ f) := hg.comp hf lemma uniform_continuous.continuous [uniform_space β] {f : α → β} (hf : uniform_continuous f) : continuous f := continuous_iff_continuous_at.mpr $ assume a, calc map f (nhds a) ≤ (map (λp:α×α, (f p.1, f p.2)) (𝓤 α)).lift' (λs:set (β×β), {y | (f a, y) ∈ s}) : begin rw [nhds_eq_uniformity, map_lift'_eq, map_lift'_eq2], exact (lift'_mono' $ assume s hs b ⟨a', (ha' : (_, a') ∈ s), a'_eq⟩, ⟨(a, a'), ha', show (f a, f a') = (f a, b), from a'_eq ▸ rfl⟩), exact monotone_preimage, exact monotone_preimage end ... ≤ nhds (f a) : by rw [nhds_eq_uniformity]; exact lift'_mono hf (le_refl _) end uniform_space end local notation `𝓤` := uniformity section constructions variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} {ι : Sort*} instance : partial_order (uniform_space α) := { le := λt s, t.uniformity ≤ s.uniformity, le_antisymm := assume t s h₁ h₂, uniform_space_eq $ le_antisymm h₁ h₂, le_refl := assume t, le_refl _, le_trans := assume a b c h₁ h₂, le_trans h₁ h₂ } instance : has_Inf (uniform_space α) := ⟨assume s, uniform_space.of_core { uniformity := (⨅u∈s, @uniformity α u), refl := le_infi $ assume u, le_infi $ assume hu, u.refl, symm := le_infi $ assume u, le_infi $ assume hu, le_trans (map_mono $ infi_le_of_le _ $ infi_le _ hu) u.symm, comp := le_infi $ assume u, le_infi $ assume hu, le_trans (lift'_mono (infi_le_of_le _ $ infi_le _ hu) $ le_refl _) u.comp }⟩ private lemma Inf_le {tt : set (uniform_space α)} {t : uniform_space α} (h : t ∈ tt) : Inf tt ≤ t := show (⨅u∈tt, @uniformity α u) ≤ t.uniformity, from infi_le_of_le t $ infi_le _ h private lemma le_Inf {tt : set (uniform_space α)} {t : uniform_space α} (h : ∀t'∈tt, t ≤ t') : t ≤ Inf tt := show t.uniformity ≤ (⨅u∈tt, @uniformity α u), from le_infi $ assume t', le_infi $ assume ht', h t' ht' instance : has_top (uniform_space α) := ⟨uniform_space.of_core { uniformity := ⊤, refl := le_top, symm := le_top, comp := le_top }⟩ instance : has_bot (uniform_space α) := ⟨{ to_topological_space := ⊥, uniformity := principal id_rel, refl := le_refl _, symm := by simp [tendsto]; apply subset.refl, comp := begin rw [lift'_principal], {simp}, exact monotone_comp_rel monotone_id monotone_id end, is_open_uniformity := assume s, by simp [is_open_fold, subset_def, id_rel] {contextual := tt } } ⟩ instance : complete_lattice (uniform_space α) := { sup := λa b, Inf {x | a ≤ x ∧ b ≤ x}, le_sup_left := λ a b, le_Inf (λ _ ⟨h, _⟩, h), le_sup_right := λ a b, le_Inf (λ _ ⟨_, h⟩, h), sup_le := λ a b c h₁ h₂, Inf_le ⟨h₁, h₂⟩, inf := λ a b, Inf {a, b}, le_inf := λ a b c h₁ h₂, le_Inf (λ u h, by { cases h, exact h.symm ▸ h₂, exact (mem_singleton_iff.1 h).symm ▸ h₁ }), inf_le_left := λ a b, Inf_le (by simp), inf_le_right := λ a b, Inf_le (by simp), top := ⊤, le_top := λ a, show a.uniformity ≤ ⊤, from le_top, bot := ⊥, bot_le := λ u, u.refl, Sup := λ tt, Inf {t | ∀ t' ∈ tt, t' ≤ t}, le_Sup := λ s u h, le_Inf (λ u' h', h' u h), Sup_le := λ s u h, Inf_le h, Inf := Inf, le_Inf := λ s a hs, le_Inf hs, Inf_le := λ s a ha, Inf_le ha, ..uniform_space.partial_order } lemma infi_uniformity {ι : Sort*} {u : ι → uniform_space α} : (infi u).uniformity = (⨅i, (u i).uniformity) := show (⨅a (h : ∃i:ι, u i = a), a.uniformity) = _, from le_antisymm (le_infi $ assume i, infi_le_of_le (u i) $ infi_le _ ⟨i, rfl⟩) (le_infi $ assume a, le_infi $ assume ⟨i, (ha : u i = a)⟩, ha ▸ infi_le _ _) lemma inf_uniformity {u v : uniform_space α} : (u ⊓ v).uniformity = u.uniformity ⊓ v.uniformity := have (u ⊓ v) = (⨅i (h : i = u ∨ i = v), i), by simp [infi_or, infi_inf_eq], calc (u ⊓ v).uniformity = ((⨅i (h : i = u ∨ i = v), i) : uniform_space α).uniformity : by rw [this] ... = _ : by simp [infi_uniformity, infi_or, infi_inf_eq] instance inhabited_uniform_space : inhabited (uniform_space α) := ⟨⊥⟩ /-- Given `f : α → β` and a uniformity `u` on `β`, the inverse image of `u` under `f` is the inverse image in the filter sense of the induced function `α × α → β × β`. -/ def uniform_space.comap (f : α → β) (u : uniform_space β) : uniform_space α := { uniformity := u.uniformity.comap (λp:α×α, (f p.1, f p.2)), to_topological_space := u.to_topological_space.induced f, refl := le_trans (by simp; exact assume ⟨a, b⟩ (h : a = b), h ▸ rfl) (comap_mono u.refl), symm := by simp [tendsto_comap_iff, prod.swap, (∘)]; exact tendsto_swap_uniformity.comp tendsto_comap, comp := le_trans begin rw [comap_lift'_eq, comap_lift'_eq2], exact (lift'_mono' $ assume s hs ⟨a₁, a₂⟩ ⟨x, h₁, h₂⟩, ⟨f x, h₁, h₂⟩), repeat { exact monotone_comp_rel monotone_id monotone_id } end (comap_mono u.comp), is_open_uniformity := λ s, begin change (@is_open α (u.to_topological_space.induced f) s ↔ _), simp [is_open_iff_nhds, nhds_induced, mem_nhds_uniformity_iff, filter.comap, and_comm], refine ball_congr (λ x hx, ⟨_, _⟩), { rintro ⟨t, hts, ht⟩, refine ⟨_, ht, _⟩, rintro ⟨x₁, x₂⟩ h rfl, exact hts (h rfl) }, { rintro ⟨t, ht, hts⟩, exact ⟨{y | (f x, y) ∈ t}, λ y hy, @hts (x, y) hy rfl, mem_nhds_uniformity_iff.1 $ mem_nhds_left _ ht⟩ } end } lemma uniform_space_comap_id {α : Type*} : uniform_space.comap (id : α → α) = id := by ext u ; dsimp [uniform_space.comap] ; rw [prod.id_prod, filter.comap_id] lemma uniform_space.comap_comap_comp {α β γ} [uγ : uniform_space γ] {f : α → β} {g : β → γ} : uniform_space.comap (g ∘ f) uγ = uniform_space.comap f (uniform_space.comap g uγ) := by ext ; dsimp [uniform_space.comap] ; rw filter.comap_comap_comp lemma uniform_continuous_iff {α β} [uα : uniform_space α] [uβ : uniform_space β] (f : α → β) : uniform_continuous f ↔ uα ≤ uβ.comap f := filter.map_le_iff_le_comap lemma uniform_continuous_comap {f : α → β} [u : uniform_space β] : @uniform_continuous α β (uniform_space.comap f u) u f := tendsto_comap theorem to_topological_space_comap {f : α → β} {u : uniform_space β} : @uniform_space.to_topological_space _ (uniform_space.comap f u) = topological_space.induced f (@uniform_space.to_topological_space β u) := rfl lemma uniform_continuous_comap' {f : γ → β} {g : α → γ} [v : uniform_space β] [u : uniform_space α] (h : uniform_continuous (f ∘ g)) : @uniform_continuous α γ u (uniform_space.comap f v) g := tendsto_comap_iff.2 h lemma to_topological_space_mono {u₁ u₂ : uniform_space α} (h : u₁ ≤ u₂) : @uniform_space.to_topological_space _ u₁ ≤ @uniform_space.to_topological_space _ u₂ := le_of_nhds_le_nhds $ assume a, by rw [@nhds_eq_uniformity α u₁ a, @nhds_eq_uniformity α u₂ a]; exact (lift'_mono h $ le_refl _) lemma to_topological_space_bot : @uniform_space.to_topological_space α ⊥ = ⊥ := rfl lemma to_topological_space_top : @uniform_space.to_topological_space α ⊤ = ⊤ := top_unique $ assume s hs, classical.by_cases (assume : s = ∅, this.symm ▸ @is_open_empty _ ⊤) (assume : s ≠ ∅, let ⟨x, hx⟩ := exists_mem_of_ne_empty this in have s = univ, from top_unique $ assume y hy, hs x hx (x, y) rfl, this.symm ▸ @is_open_univ _ ⊤) lemma to_topological_space_infi {ι : Sort*} {u : ι → uniform_space α} : (infi u).to_topological_space = ⨅i, (u i).to_topological_space := classical.by_cases (assume h : nonempty ι, eq_of_nhds_eq_nhds $ assume a, begin rw [nhds_infi, nhds_eq_uniformity], change (infi u).uniformity.lift' (preimage $ prod.mk a) = _, begin rw [infi_uniformity, lift'_infi], exact (congr_arg _ $ funext $ assume i, (@nhds_eq_uniformity α (u i) a).symm), exact h, exact assume a b, rfl end end) (assume : ¬ nonempty ι, le_antisymm (le_infi $ assume i, to_topological_space_mono $ infi_le _ _) (have infi u = ⊤, from top_unique $ le_infi $ assume i, (this ⟨i⟩).elim, have @uniform_space.to_topological_space _ (infi u) = ⊤, from this.symm ▸ to_topological_space_top, this.symm ▸ le_top)) lemma to_topological_space_Inf {s : set (uniform_space α)} : (Inf s).to_topological_space = (⨅i∈s, @uniform_space.to_topological_space α i) := begin rw [Inf_eq_infi, to_topological_space_infi], apply congr rfl, funext x, exact to_topological_space_infi end lemma to_topological_space_inf {u v : uniform_space α} : (u ⊓ v).to_topological_space = u.to_topological_space ⊓ v.to_topological_space := by rw [to_topological_space_Inf, infi_pair] instance : uniform_space empty := ⊥ instance : uniform_space unit := ⊥ instance : uniform_space bool := ⊥ instance : uniform_space ℕ := ⊥ instance : uniform_space ℤ := ⊥ instance {p : α → Prop} [t : uniform_space α] : uniform_space (subtype p) := uniform_space.comap subtype.val t lemma uniformity_subtype {p : α → Prop} [t : uniform_space α] : 𝓤 (subtype p) = comap (λq:subtype p × subtype p, (q.1.1, q.2.1)) (𝓤 α) := rfl lemma uniform_continuous_subtype_val {p : α → Prop} [uniform_space α] : uniform_continuous (subtype.val : {a : α // p a} → α) := uniform_continuous_comap lemma uniform_continuous_subtype_mk {p : α → Prop} [uniform_space α] [uniform_space β] {f : β → α} (hf : uniform_continuous f) (h : ∀x, p (f x)) : uniform_continuous (λx, ⟨f x, h x⟩ : β → subtype p) := uniform_continuous_comap' hf lemma tendsto_of_uniform_continuous_subtype [uniform_space α] [uniform_space β] {f : α → β} {s : set α} {a : α} (hf : uniform_continuous (λx:s, f x.val)) (ha : s ∈ nhds a) : tendsto f (nhds a) (nhds (f a)) := by rw [(@map_nhds_subtype_val_eq α _ s a (mem_of_nhds ha) ha).symm]; exact tendsto_map' (continuous_iff_continuous_at.mp hf.continuous _) section prod /- a similar product space is possible on the function space (uniformity of pointwise convergence), but we want to have the uniformity of uniform convergence on function spaces -/ instance [u₁ : uniform_space α] [u₂ : uniform_space β] : uniform_space (α × β) := uniform_space.of_core_eq (u₁.comap prod.fst ⊓ u₂.comap prod.snd).to_core prod.topological_space (calc prod.topological_space = (u₁.comap prod.fst ⊓ u₂.comap prod.snd).to_topological_space : by rw [to_topological_space_inf, to_topological_space_comap, to_topological_space_comap]; refl ... = _ : by rw [uniform_space.to_core_to_topological_space]) theorem uniformity_prod [uniform_space α] [uniform_space β] : 𝓤 (α × β) = (𝓤 α).comap (λp:(α × β) × α × β, (p.1.1, p.2.1)) ⊓ (𝓤 β).comap (λp:(α × β) × α × β, (p.1.2, p.2.2)) := inf_uniformity lemma uniformity_prod_eq_prod [uniform_space α] [uniform_space β] : 𝓤 (α×β) = map (λp:(α×α)×(β×β), ((p.1.1, p.2.1), (p.1.2, p.2.2))) (filter.prod (𝓤 α) (𝓤 β)) := have map (λp:(α×α)×(β×β), ((p.1.1, p.2.1), (p.1.2, p.2.2))) = comap (λp:(α×β)×(α×β), ((p.1.1, p.2.1), (p.1.2, p.2.2))), from funext $ assume f, map_eq_comap_of_inverse (funext $ assume ⟨⟨_, _⟩, ⟨_, _⟩⟩, rfl) (funext $ assume ⟨⟨_, _⟩, ⟨_, _⟩⟩, rfl), by rw [this, uniformity_prod, filter.prod, comap_inf, comap_comap_comp, comap_comap_comp] lemma mem_map_sets_iff' {α : Type*} {β : Type*} {f : filter α} {m : α → β} {t : set β} : t ∈ (map m f).sets ↔ (∃s∈f, m '' s ⊆ t) := mem_map_sets_iff lemma mem_uniformity_of_uniform_continuous_invarant [uniform_space α] {s:set (α×α)} {f : α → α → α} (hf : uniform_continuous (λp:α×α, f p.1 p.2)) (hs : s ∈ 𝓤 α) : ∃u∈𝓤 α, ∀a b c, (a, b) ∈ u → (f a c, f b c) ∈ s := begin rw [uniform_continuous, uniformity_prod_eq_prod, tendsto_map'_iff, (∘)] at hf, rcases mem_map_sets_iff'.1 (hf hs) with ⟨t, ht, hts⟩, clear hf, rcases mem_prod_iff.1 ht with ⟨u, hu, v, hv, huvt⟩, clear ht, refine ⟨u, hu, assume a b c hab, hts $ (mem_image _ _ _).2 ⟨⟨⟨a, b⟩, ⟨c, c⟩⟩, huvt ⟨_, _⟩, _⟩⟩, exact hab, exact refl_mem_uniformity hv, refl end lemma mem_uniform_prod [t₁ : uniform_space α] [t₂ : uniform_space β] {a : set (α × α)} {b : set (β × β)} (ha : a ∈ 𝓤 α) (hb : b ∈ 𝓤 β) : {p:(α×β)×(α×β) | (p.1.1, p.2.1) ∈ a ∧ (p.1.2, p.2.2) ∈ b } ∈ (@uniformity (α × β) _) := by rw [uniformity_prod]; exact inter_mem_inf_sets (preimage_mem_comap ha) (preimage_mem_comap hb) lemma tendsto_prod_uniformity_fst [uniform_space α] [uniform_space β] : tendsto (λp:(α×β)×(α×β), (p.1.1, p.2.1)) (𝓤 (α × β)) (𝓤 α) := le_trans (map_mono (@inf_le_left (uniform_space (α×β)) _ _ _)) map_comap_le lemma tendsto_prod_uniformity_snd [uniform_space α] [uniform_space β] : tendsto (λp:(α×β)×(α×β), (p.1.2, p.2.2)) (𝓤 (α × β)) (𝓤 β) := le_trans (map_mono (@inf_le_right (uniform_space (α×β)) _ _ _)) map_comap_le lemma uniform_continuous_fst [uniform_space α] [uniform_space β] : uniform_continuous (λp:α×β, p.1) := tendsto_prod_uniformity_fst lemma uniform_continuous_snd [uniform_space α] [uniform_space β] : uniform_continuous (λp:α×β, p.2) := tendsto_prod_uniformity_snd variables [uniform_space α] [uniform_space β] [uniform_space γ] lemma uniform_continuous.prod_mk {f₁ : α → β} {f₂ : α → γ} (h₁ : uniform_continuous f₁) (h₂ : uniform_continuous f₂) : uniform_continuous (λa, (f₁ a, f₂ a)) := by rw [uniform_continuous, uniformity_prod]; exact tendsto_inf.2 ⟨tendsto_comap_iff.2 h₁, tendsto_comap_iff.2 h₂⟩ lemma uniform_continuous.prod_mk_left {f : α × β → γ} (h : uniform_continuous f) (b) : uniform_continuous (λ a, f (a,b)) := h.comp (uniform_continuous_id.prod_mk uniform_continuous_const) lemma uniform_continuous.prod_mk_right {f : α × β → γ} (h : uniform_continuous f) (a) : uniform_continuous (λ b, f (a,b)) := h.comp (uniform_continuous_const.prod_mk uniform_continuous_id) lemma to_topological_space_prod {α} {β} [u : uniform_space α] [v : uniform_space β] : @uniform_space.to_topological_space (α × β) prod.uniform_space = @prod.topological_space α β u.to_topological_space v.to_topological_space := rfl end prod section open uniform_space function variables [uniform_space α] [uniform_space β] [uniform_space γ] [uniform_space δ] local notation f `∘₂` g := function.bicompr f g def uniform_continuous₂ (f : α → β → γ) := uniform_continuous (uncurry' f) lemma uniform_continuous₂_def (f : α → β → γ) : uniform_continuous₂ f ↔ uniform_continuous (uncurry' f) := iff.rfl lemma uniform_continuous₂_curry (f : α × β → γ) : uniform_continuous₂ (function.curry f) ↔ uniform_continuous f := by rw [←uncurry'_curry f] {occs := occurrences.pos [2]} ; refl lemma uniform_continuous₂.comp {f : α → β → γ} {g : γ → δ} (hg : uniform_continuous g) (hf : uniform_continuous₂ f) : uniform_continuous₂ (g ∘₂ f) := hg.comp hf end lemma to_topological_space_subtype [u : uniform_space α] {p : α → Prop} : @uniform_space.to_topological_space (subtype p) subtype.uniform_space = @subtype.topological_space α p u.to_topological_space := rfl section sum variables [uniform_space α] [uniform_space β] open sum /-- Uniformity on a disjoint union. Entourages of the diagonal in the union are obtained by taking independently an entourage of the diagonal in the first part, and an entourage of the diagonal in the second part. -/ def uniform_space.core.sum : uniform_space.core (α ⊕ β) := uniform_space.core.mk' (map (λ p : α × α, (inl p.1, inl p.2)) (𝓤 α) ⊔ map (λ p : β × β, (inr p.1, inr p.2)) (𝓤 β)) (λ r ⟨H₁, H₂⟩ x, by cases x; [apply refl_mem_uniformity H₁, apply refl_mem_uniformity H₂]) (λ r ⟨H₁, H₂⟩, ⟨symm_le_uniformity H₁, symm_le_uniformity H₂⟩) (λ r ⟨Hrα, Hrβ⟩, begin rcases comp_mem_uniformity_sets Hrα with ⟨tα, htα, Htα⟩, rcases comp_mem_uniformity_sets Hrβ with ⟨tβ, htβ, Htβ⟩, refine ⟨_, ⟨mem_map_sets_iff.2 ⟨tα, htα, subset_union_left _ _⟩, mem_map_sets_iff.2 ⟨tβ, htβ, subset_union_right _ _⟩⟩, _⟩, rintros ⟨_, _⟩ ⟨z, ⟨⟨a, b⟩, hab, ⟨⟩⟩ | ⟨⟨a, b⟩, hab, ⟨⟩⟩, ⟨⟨_, c⟩, hbc, ⟨⟩⟩ | ⟨⟨_, c⟩, hbc, ⟨⟩⟩⟩, { have A : (a, c) ∈ comp_rel tα tα := ⟨b, hab, hbc⟩, exact Htα A }, { have A : (a, c) ∈ comp_rel tβ tβ := ⟨b, hab, hbc⟩, exact Htβ A } end) /-- The union of an entourage of the diagonal in each set of a disjoint union is again an entourage of the diagonal. -/ lemma union_mem_uniformity_sum {a : set (α × α)} (ha : a ∈ 𝓤 α) {b : set (β × β)} (hb : b ∈ 𝓤 β) : ((λ p : (α × α), (inl p.1, inl p.2)) '' a ∪ (λ p : (β × β), (inr p.1, inr p.2)) '' b) ∈ (@uniform_space.core.sum α β _ _).uniformity := ⟨mem_map_sets_iff.2 ⟨_, ha, subset_union_left _ _⟩, mem_map_sets_iff.2 ⟨_, hb, subset_union_right _ _⟩⟩ /- To prove that the topology defined by the uniform structure on the disjoint union coincides with the disjoint union topology, we need two lemmas saying that open sets can be characterized by the uniform structure -/ lemma uniformity_sum_of_open_aux {s : set (α ⊕ β)} (hs : is_open s) {x : α ⊕ β} (xs : x ∈ s) : { p : ((α ⊕ β) × (α ⊕ β)) | p.1 = x → p.2 ∈ s } ∈ (@uniform_space.core.sum α β _ _).uniformity := begin cases x, { refine mem_sets_of_superset (union_mem_uniformity_sum (mem_nhds_uniformity_iff.1 (mem_nhds_sets hs.1 xs)) univ_mem_sets) (union_subset _ _); rintro _ ⟨⟨_, b⟩, h, ⟨⟩⟩ ⟨⟩, exact h rfl }, { refine mem_sets_of_superset (union_mem_uniformity_sum univ_mem_sets (mem_nhds_uniformity_iff.1 (mem_nhds_sets hs.2 xs))) (union_subset _ _); rintro _ ⟨⟨a, _⟩, h, ⟨⟩⟩ ⟨⟩, exact h rfl }, end lemma open_of_uniformity_sum_aux {s : set (α ⊕ β)} (hs : ∀x ∈ s, { p : ((α ⊕ β) × (α ⊕ β)) | p.1 = x → p.2 ∈ s } ∈ (@uniform_space.core.sum α β _ _).uniformity) : is_open s := begin split, { refine (@is_open_iff_mem_nhds α _ _).2 (λ a ha, mem_nhds_uniformity_iff.2 _), rcases mem_map_sets_iff.1 (hs _ ha).1 with ⟨t, ht, st⟩, refine mem_sets_of_superset ht _, rintro p pt rfl, exact st ⟨_, pt, rfl⟩ rfl }, { refine (@is_open_iff_mem_nhds β _ _).2 (λ b hb, mem_nhds_uniformity_iff.2 _), rcases mem_map_sets_iff.1 (hs _ hb).2 with ⟨t, ht, st⟩, refine mem_sets_of_superset ht _, rintro p pt rfl, exact st ⟨_, pt, rfl⟩ rfl } end /- We can now define the uniform structure on the disjoint union -/ instance sum.uniform_space : uniform_space (α ⊕ β) := { to_core := uniform_space.core.sum, is_open_uniformity := λ s, ⟨uniformity_sum_of_open_aux, open_of_uniformity_sum_aux⟩ } lemma sum.uniformity : 𝓤 (α ⊕ β) = map (λ p : α × α, (inl p.1, inl p.2)) (𝓤 α) ⊔ map (λ p : β × β, (inr p.1, inr p.2)) (𝓤 β) := rfl end sum end constructions lemma lebesgue_number_lemma {α : Type u} [uniform_space α] {s : set α} {ι} {c : ι → set α} (hs : compact s) (hc₁ : ∀ i, is_open (c i)) (hc₂ : s ⊆ ⋃ i, c i) : ∃ n ∈ 𝓤 α, ∀ x ∈ s, ∃ i, {y | (x, y) ∈ n} ⊆ c i := begin let u := λ n, {x | ∃ i (m ∈ 𝓤 α), {y | (x, y) ∈ comp_rel m n} ⊆ c i}, have hu₁ : ∀ n ∈ 𝓤 α, is_open (u n), { refine λ n hn, is_open_uniformity.2 _, rintro x ⟨i, m, hm, h⟩, rcases comp_mem_uniformity_sets hm with ⟨m', hm', mm'⟩, apply (𝓤 α).sets_of_superset hm', rintros ⟨x, y⟩ hp rfl, refine ⟨i, m', hm', λ z hz, h (monotone_comp_rel monotone_id monotone_const mm' _)⟩, dsimp at hz ⊢, rw comp_rel_assoc, exact ⟨y, hp, hz⟩ }, have hu₂ : s ⊆ ⋃ n ∈ 𝓤 α, u n, { intros x hx, rcases mem_Union.1 (hc₂ hx) with ⟨i, h⟩, rcases comp_mem_uniformity_sets (is_open_uniformity.1 (hc₁ i) x h) with ⟨m', hm', mm'⟩, exact mem_bUnion hm' ⟨i, _, hm', λ y hy, mm' hy rfl⟩ }, rcases compact_elim_finite_subcover_image hs hu₁ hu₂ with ⟨b, bu, b_fin, b_cover⟩, refine ⟨_, Inter_mem_sets b_fin bu, λ x hx, _⟩, rcases mem_bUnion_iff.1 (b_cover hx) with ⟨n, bn, i, m, hm, h⟩, refine ⟨i, λ y hy, h _⟩, exact prod_mk_mem_comp_rel (refl_mem_uniformity hm) (bInter_subset_of_mem bn hy) end lemma lebesgue_number_lemma_sUnion {α : Type u} [uniform_space α] {s : set α} {c : set (set α)} (hs : compact s) (hc₁ : ∀ t ∈ c, is_open t) (hc₂ : s ⊆ ⋃₀ c) : ∃ n ∈ 𝓤 α, ∀ x ∈ s, ∃ t ∈ c, ∀ y, (x, y) ∈ n → y ∈ t := by rw sUnion_eq_Union at hc₂; simpa using lebesgue_number_lemma hs (by simpa) hc₂
ddcae87ce1a88f47d79bdad4681c62fbceef515e
4e39e38e0c2134eafdc7ab6e0f8ca5c27795f7a2
/test.lean
1b5fc0e5882cdbf239b3231c12720a56d5e32391
[]
no_license
tcmch/cs-dm
a33c4169b8282b54653be72b9600e83ad329953c
4d9e45e398d7ec12538fe0335c007b98f0b32403
refs/heads/master
1,585,694,000,689
1,544,118,938,000
1,544,118,938,000
152,622,502
0
0
null
1,539,275,269,000
1,539,275,269,000
null
UTF-8
Lean
false
false
4,972
lean
/- This exam focuses on assessing your ability to write and prove propositions in predicate logic, with an emphasis on predicates, disjunctions, and existentially quantified propositions. There are three parts: A: Predicates [20 points in 4 parts] B: Disjuctions [40 points in 2 parts] C. Existentials [40 point in 2 parts] -/ /- Chuanhao Ma cm9mb -/ /- ******************************** -/ /- *** A. PREDICATES [20 points] ***-/ /- ******************************** -/ /- 1a. Define a function called isOdd that takes an argument, n : ℕ, and returns a proposition that asserts that n is odd. The function will thus be a predicate on values of type ℕ. Hint: a number is odd if it's one more than an even number. -/ def isOdd(n : ℕ) : Prop := ∃ m, m*2+1 = n /- 1b. To test your predicate, use "example" to write and prove isOdd(15). -/ example : isOdd(15) := begin unfold isOdd, apply exists.intro 7, apply eq.refl, end /- 1c. Define isSmall : ℕ → Prop, to be a predicate that is true exactly when the argument, n, is such that n = 0 ∨ n = 1 ∨ n = 2 ∨ n = 3 ∨ n = 4 ∨ n = 5. (Don't try to rewrite this proposition as an inequality; just use it as is.) -/ def isSmall(n : ℕ) : Prop := n = 0 ∨ n = 1 ∨ n = 2 ∨ n = 3 ∨ n = 4 ∨ n = 5 /- 1d. Define a predicate, isBoth(n:ℕ) that is true exacly when n satisfies both the isOdd and isSmall predicates. Use isOdd and isSmall in your answer. -/ def isBoth(n : ℕ) : Prop := (isOdd n) ∧ (isSmall n) /- ******************* -/ /- *** DISJUNCTIONS ***-/ /- ******************* -/ /- 2. [20 Points] Jane walks to school or she carries her lunch. In either case, she uses a bag. If she walks, she uses a bag for her books. If she carries her lunch, she uses a bag to carry her food. So if she walks, she uses a bag, and if she carries her lunch, she uses a bag. From the fact that she walks or carries her lunch, and from the added facts that in either case she uses a bag, we can conclude that she uses a bag. Using Walks, Carries, and Bag as names of propositions, write a Lean example that asserts the following proposition; then prove it. If Walks, Carries, and Bag are propositions, then if (Walks ∨ Carries) is true, and then if (Walks implies Bag) is true, and then if (Carries implies Bag) is true, then Bag is true. Here's a start. example : ∀ (Walks Carries Bag : Prop), ... -/ example : ∀ (Walks Carries Bag : Prop), (Walks ∨ Carries) → (Walks → Bag) → (Carries → Bag) → Bag := begin assume Walks Carries Bag, assume WalksOrCarries: (Walks ∨ Carries), assume WalksWithBag: (Walks → Bag), assume CarriesWithBag: (Carries → Bag), cases WalksOrCarries with walks carries, show Bag, from WalksWithBag walks, show Bag, from CarriesWithBag carries, end /- 3. [20 Points] Prove the following proposition. -/ example : ∀ (P Q R : Prop), (P ∧ Q) → (Q ∨ R) → (P ∨ R) := begin intros P Q R, intros PandQ QorR, apply or.inl PandQ.left, end /- *********************** -/ /- *** C. EXISTENTIALS ***-/ /- *********************** -/ /- 4. [20 points] Referring to the isBoth predicate you defined in question #1, state and prove the proposition that there *exists* a number, n, that satisfies isBoth. Remember that you can use the unfold tactic to expand the name of a predicate in a goal. Use "example" to state the proposition. -/ example : ∃ n, isBoth n := begin unfold isBoth, apply exists.intro 3, apply and.intro, unfold isOdd, apply exists.intro 1, apply eq.refl, unfold isSmall, apply or.inr, apply or.inr, apply or.inr, apply or.inl, apply eq.refl, end /- 5. [20 points] Suppose that Heavy and Round are predicates on values of any type, T. Prove the proposition that if every t : T is Heavy (satisfies the Heavy predicate) and if there exists some t : T that is Round (satisfies the Round predicate) then there exists some t : T is both Heavy and Round (satisfies the conjunction of the two properties). -/ example : ∀ T : Type, ∀ (Heavy Round : T → Prop), (∀ t, Heavy t) → (∃ t, Round t) → (∃ t, Heavy t ∧ Round t) := begin intro T, intros H R, assume allTH, assume someTR, apply exists.elim someTR, intro w, assume wR, have wH := allTH w, apply exists.intro w, exact ⟨ wH, wR ⟩, end /- 5. [20 points] New problem Since we worked out that problem in class, here's a replacement problem: Show that if there exists an object of some type, T, that has properties P and Q, then there exists an object of that type that has the property P or Q. Remember that a property is defined by a predicate. -/ example : ∀ T : Type, ∀ (P Q : T → Prop), (∃ t, P t ∧ Q t) → (∃ o, P o ∨ Q o) := begin intros T P Q, intro ex, apply exists.elim ex, intros w pwandqw, apply exists.intro w, exact or.inl pwandqw.left, end
9fe6dd4020acbb4e7fea438715f7a8110dacd31e
1ce2e14f78a7f514bae439fbbb1f7fa2932dd7dd
/src/common.lean
34e08b0e2f6f71e3385060f863714e5c8d056921
[ "MIT" ]
permissive
wudcscheme/lean-mathgirls
b88faf76d12e5ca07b83d6e5580663380b0bb45c
95b64a33ffb34f3c45e212c4adff3cc988dcbc60
refs/heads/master
1,678,994,115,961
1,585,127,877,000
1,585,127,877,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,219
lean
import data.list.basic data.list.perm init.data.int.order import tactic.omega tactic.basic tactic.tidy notation `ℙ` := nat.primes meta def by_eval (typ: Type*) [reflected typ]: tactic unit := do e <- tactic.target, match expr.is_eq e with | (some (e1, e2)) := tactic.eval_expr' typ e1 >> tactic.reflexivity | none := tactic.failed end -- 1-based def fib: nat -> nat | 0 := 0 -- sentinel | 1 := 1 | 2 := 1 | (n+2) := (fib (n)) + (fib (n+1)) @[simp] def nat.sum (n: ℕ) := ((n+1)*n)/2 -- seg a b = [a, ..., b) @[simp, reducible] def seg (a b: ℕ) := (list.range (b-a)).map(λ x, x+a) -- #eval seg 3 5 @[simp, reducible] def find {α: Type} (p: α -> Prop) [decidable_pred p] [inhabited α] : list α -> α | [] := arbitrary α | (h::t) := cond (p h) h (find t) @[simp] def nonneg_to_nat (i: ℤ) (h: i ≥ 0): ℕ := begin cases i, exact i, exact false.elim h, end theorem nonneg_to_nat_eq {i: ℤ} {h: i ≥ 0}: i = (nonneg_to_nat i h) := begin simp, induction i, reflexivity, exfalso, tidy, end theorem sum_nonneg {l: list ℤ} (h: ∀ i: ℤ, i ∈ l -> i ≥ 0): l.sum ≥ 0 := begin induction l, simp, simp, apply add_nonneg, tidy, end
a12783363e337f1e2e6dd761f85979d14fde902e
d7c0dac4019ec4c77a89fd6d3e672674a3cad8f8
/ANT.lean
ecbe3f0dfdd17b90cb47f297354e9d9769fa4e47
[]
no_license
TudorTitan/Lean_ElementaryNT
97fb78afaa07822c9ee0f563e263e16536b28638
e9fa9e1db315fa7aca88666aee9910d5d9d34a11
refs/heads/master
1,630,895,651,127
1,517,510,995,000
1,517,510,995,000
108,649,763
0
1
null
1,509,757,816,000
1,509,195,975,000
Lean
UTF-8
Lean
false
false
9,529
lean
-- Needed for sheaves temporarily import analysis.topology.topological_space -- Auxiliary lemmas lemma exists_and_commute {α : Type} {p q r: α → Prop} : (∃! a : α, r a ∧ p a ∧ q a) → ∃! a : α, r a ∧ q a ∧ p a := λ ⟨a, ⟨b, c ⟩⟩, begin existsi a, simp at *, exact ⟨b.symm.2, b.symm.1.1 ,b.symm.1.2 ,λ y g h, c y g h⟩ end -- Categories and functors structure Category (type : Type) := (obj : type → Prop) (mor : type → type → (type → type) → Prop) (associativity : ∀ a b c, ∀ f g : type → type, mor a b f → mor b c g → mor a c (g ∘ f)) (identity: ∀ a, obj a → mor a a id) structure Functor {a b : Type} (C1: Category a) (C2: Category b) : Type := (obj_map: a → b) (mor_map: (a → a) → (b → b)) (respect_obj: ∀ x, C1.obj x → C2.obj (obj_map x)) (respect_id : mor_map id = id) (respect_mor: ∀ x y z, C1.mor x y z → C2.mor (obj_map x) (obj_map y) (mor_map z)) (respect_composition: ∀ f g: a → a, mor_map (f ∘ g) = mor_map f ∘ mor_map g) structure Contravariant_Functor {a b : Type} (C1: Category a) (C2: Category b) : Type := (obj_map: a → b) (mor_map: (a → a) → (b → b)) (respect_obj: ∀ x, C1.obj x → C2.obj (obj_map x)) (respect_id : mor_map id = id) (swap_mor: ∀ x y z, C1.mor x y z → C2.mor (obj_map y) (obj_map x) (mor_map z)) (respect_composition: ∀ f g: a → a, mor_map (f ∘ g) = mor_map g ∘ mor_map f) -- Notions of isomorphims definition isomorphism {a: Type} (x : Category a) (f: a → a) (b c: a) := ∃ g : a → a, f ∘ g = id ∧ g ∘ f = id ∧ x.mor b c f ∧ x.mor c b g definition isomorphic {a: Type} (x : Category a) (b c : a) := ∃ f : a → a, isomorphism x f b c -- Category description of groups, groupoids and monoids structure Groupoid (a: Type) extends x : Category a := (isomorphisms: ∀ f: a → a, ∃ b c: a, x.obj b ∧ x.obj c ∧ isomorphism x f b c) structure Monoid (a: Type) extends x : Category a := (singleton : a) (monoid : ∀ c : a, obj c ↔ c = singleton) structure Group (a: Type) extends Monoid a := (isomorphisms: ∀ f: a → a, ∃ b c: a, x.obj b ∧ x.obj c ∧ isomorphism x f b c) -- Initial, terminal and zero objects definition initial_object {t: Type} (c : Category t) (a: t) := c.obj a ∧ ∀ b : t, c.obj b → ∃! f : t → t, c.mor a b f definition terminal_object {t: Type} (c : Category t) (a: t) := c.obj a ∧ ∀ b : t, c.obj b → ∃! f : t → t, c.mor b a f definition zero_object {t: Type} (c : Category t) (a: t) := c.obj a ∧ initial_object c a ∧ terminal_object c a -- Lemmas for uniqueness of zero, terminal and initial objects up to isomorphism lemma unique_initial {t: Type } : ∀ c : Category t, ∀ a b: t, initial_object c a ∧ initial_object c b → isomorphic c a b := begin intros, unfold isomorphic, unfold initial_object at a_1, cases a_1, intros, apply exists.elim ((right.2 a) left.1), intros, apply exists.elim ((left.2 b) right.1), intros, existsi (a_3), existsi (a_1), have H: c.mor b b (a_3∘a_1), from c.associativity b a b a_1 a_3 a_2.1 a_4.1, apply exists.elim ((right.2 b) right.1), intros, have H2: a_3∘a_1 = a_5, from a_6.2 (a_3∘a_1) H, have I1: id = a_5, from a_6.2 id ((c.identity b) right.1), subst I1, have HH: c.mor a a (a_1∘a_3), from c.associativity a b a a_3 a_1 a_4.1 a_2.1, apply exists.elim ((left.2 a) left.1), intros, have H3: a_1∘a_3 = a_5, from a_7.2 (a_1∘a_3) HH, have I2: id = a_5, from a_7.2 id ((c.identity a) left.1), subst I2, rw [H2, H3], simp, exact and.intro a_4.1 a_2.1 end lemma unique_terminal {t: Type } : ∀ c : Category t, ∀ a b: t, terminal_object c a ∧ terminal_object c b → isomorphic c a b := begin intros, unfold isomorphic, unfold terminal_object at a_1, cases a_1, intros, apply exists.elim ((right.2 a) left.1), intros, apply exists.elim ((left.2 b) right.1), intros, existsi (a_1), existsi (a_3), have H: c.mor a a (a_3∘a_1), from c.associativity a b a a_1 a_3 a_2.1 a_4.1, apply exists.elim ((left.2 a) left.1), intros, have H2: a_3∘a_1 = a_5, from a_6.2 (a_3∘a_1) H, have I1: id = a_5, from a_6.2 id ((c.identity a) left.1), subst I1, have HH: c.mor b b (a_1∘a_3), from c.associativity b a b a_3 a_1 a_4.1 a_2.1, apply exists.elim ((right.2 b) right.1), intros, have H3: a_1∘a_3 = a_5, from a_7.2 (a_1∘a_3) HH, have I2: id = a_5, from a_7.2 id ((c.identity b) right.1), subst I2, rw [H2, H3], simp, exact ⟨a_2.1, a_4.1⟩ end lemma unique_zero {t: Type } : ∀ c : Category t, ∀ a b: t, zero_object c a ∧ zero_object c b → isomorphic c a b := begin intros, unfold isomorphic, unfold zero_object at a_1, apply unique_initial, exact and.intro a_1.1.2.1 a_1.2.2.1 end -- Natural transformations structure Natural_Transformation {T S : Type} {A : Category T} {B: Category S} (U V: Functor A B) := (obj_assign : T → (S → S)) (commutes : ∀ a b f, A.mor a b f → (obj_assign a) ∘ (U.mor_map f) = (V.mor_map f) ∘ (obj_assign b) ) (existence : ∀ a, B.mor (U.obj_map a) (V.obj_map a) (obj_assign a)) -- Monomorphisms and epimorphisms definition monomorphism {t : Type} {A : Category t} (a b : t) (f : t → t) := A.mor a b f ∧ (∀ m g h, A.mor m a g ∧ A.mor m a h ∧ (f ∘ g) = (f ∘ h) → g = h) definition epimorphism {t : Type} {A : Category t} (a b : t) (f : t → t) := A.mor a b f ∧ (∀ m g h, A.mor b m g ∧ A.mor b m h ∧ (g ∘ f) = (h ∘ f) → g = h) -- Composition lemmas for epimorphisms and monomorphisms -- Products and Coproducts definition product {t : Type} {A : Category t} (a b : t) (p : t) (f g: t → t) := A.obj a ∧ A.obj b ∧ A.obj p ∧ A.mor p a f ∧ A.mor p b g ∧ (∀ k m n, A.obj k ∧ A.mor k a m ∧ A.mor k b n → ∃! u, A.mor k p u ∧ g ∘ u = n ∧ f ∘ u = m ) definition coproduct {t : Type} {A : Category t} (a b : t) (p : t) (f g: t → t) := A.obj a ∧ A.obj b ∧ A.obj p ∧ A.mor a p f ∧ A.mor b p g ∧ (∀ k m n, A.obj k ∧ A.mor a k m ∧ A.mor b k n → ∃! u, A.mor p k u ∧ u ∘ g = n ∧ u ∘ f = m) -- Proof of associativity, commutativity and uniqueness up to isomorphism of product and coproduct lemma product_commutes {t : Type} {A : Category t} (a b : t) (p : t) (f g: t → t) : @product t A a b p f g → @product t A b a p g f := begin intros, unfold product at a_1, unfold product, have H: (∀ k m n, A.obj k ∧ A.mor k b m ∧ A.mor k a n → ∃! u, A.mor k p u ∧ f ∘ u = n ∧ g ∘ u = m ), from ( begin intros, let X := a_1.2.2.2.2.2 k n m, let X2 := X (⟨a_2.1, a_2.2.2, a_2.2.1⟩), apply exists_and_commute, exact X2 end ), exact ⟨a_1.2.1, a_1.1, a_1.2.2.1, a_1.2.2.2.2.1, a_1.2.2.2.1, H⟩ end lemma coproduct_commutes {t : Type} {A : Category t} (a b : t) (p : t) (f g: t → t) : @coproduct t A a b p f g → @coproduct t A b a p g f := begin intros, unfold coproduct at a_1, unfold coproduct, have H: (∀ k m n, A.obj k ∧ A.mor b k m ∧ A.mor a k n → ∃! u, A.mor p k u ∧ u ∘ f = n ∧ u ∘ g = m ), from ( begin intros, let X := a_1.2.2.2.2.2 k n m, let X2 := X (⟨a_2.1, a_2.2.2, a_2.2.1⟩), apply exists_and_commute, exact X2 end ), exact ⟨a_1.2.1, a_1.1, a_1.2.2.1, a_1.2.2.2.2.1, a_1.2.2.2.1, H⟩ end -- Equalizers and co-equalizers definition equalizer {t : Type} {A : Category t} (f g h: t → t) (a b c: t) := A.obj a ∧ A.obj b ∧ A.obj c ∧ A.mor b c g ∧ A.mor b c h ∧ A.mor a b f ∧ g ∘ f = h ∘ f ∧ (∀ x m, A.mor x b m ∧ g ∘ m = h ∘ m → ∃! u, A.mor x a u ∧ f ∘ u = m) definition coequalizer {t : Type} {A : Category t} (f g h: t → t) (a b c: t) := A.obj a ∧ A.obj b ∧ A.obj c ∧ A.mor a b g ∧ A.mor a b h ∧ A.mor b c f ∧ f ∘ g = f ∘ h ∧ (∀ x m, A.mor b x m ∧ m ∘ g = m ∘ h → ∃! u, A.mor c x u ∧ u ∘ f = m) -- Kernels and cokernels -- Experimental sheaf description definition inc {A : Type} (a b: set A) (c: set A → set A) {x : Category (set A)} := x.mor a b c ↔ c = id ∧ a ⊂ b definition open_category {a: Type} (t : topological_space a) : Category (set a) := { obj := t.is_open, mor := λ A B: set a, λ C: set a → set a, C = id ∧ A ⊆ B, associativity := λ a b c, λ f g , begin intros h1 h2, split, rw [h1.1, h2.1], apply id.def, exact λ x hx, (h2.2 (h1.2 hx)) end, identity := λ a, begin split, simp, exact λ x hx, hx end } definition open_Cover {a : Type} (T: topological_space a) (t: set a) (s : set (set a)) := ⋃₀ s = t ∧ ∀ b ∈ s, T.is_open b structure Presheaf {a b: Type} (t : topological_space a) (c : Category b) extends Contravariant_Functor (open_category t) c structure Sheaf {a b: Type} (t : topological_space a) (c : Category b) extends Presheaf t c := (sheaf_axiom := ∀ g s, open_Cover t g s → ∀ x y, ∀ f h, c.mor (obj_map y) (obj_map (x ∩ y)) f → c.mor (obj_map x) (obj_map (x ∩ y)) h → f (obj_map y) = h (obj_map x) → ∃ n:b, n = (obj_map g) → ∀ l ∈ s, ∀ e, c.mor n (obj_map l) e → e n = obj_map l)
d0882374363a8436a3a3078db8f3ed4b7d380b06
82e44445c70db0f03e30d7be725775f122d72f3e
/src/topology/algebra/multilinear.lean
7ac860de6d656aaa7dc1779a734870b9d605104a
[ "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
17,198
lean
/- Copyright (c) 2020 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import topology.algebra.module import linear_algebra.multilinear /-! # Continuous multilinear maps We define continuous multilinear maps as maps from `Π(i : ι), M₁ i` to `M₂` which are multilinear and continuous, by extending the space of multilinear maps with a continuity assumption. Here, `M₁ i` and `M₂` are modules over a ring `R`, and `ι` is an arbitrary type, and all these spaces are also topological spaces. ## Main definitions * `continuous_multilinear_map R M₁ M₂` is the space of continuous multilinear maps from `Π(i : ι), M₁ i` to `M₂`. We show that it is an `R`-module. ## Implementation notes We mostly follow the API of multilinear maps. ## Notation We introduce the notation `M [×n]→L[R] M'` for the space of continuous `n`-multilinear maps from `M^n` to `M'`. This is a particular case of the general notion (where we allow varying dependent types as the arguments of our continuous multilinear maps), but arguably the most important one, especially when defining iterated derivatives. -/ open function fin set open_locale big_operators universes u v w w₁ w₁' w₂ w₃ w₄ variables {R : Type u} {ι : Type v} {n : ℕ} {M : fin n.succ → Type w} {M₁ : ι → Type w₁} {M₁' : ι → Type w₁'} {M₂ : Type w₂} {M₃ : Type w₃} {M₄ : Type w₄} [decidable_eq ι] /-- Continuous multilinear maps over the ring `R`, from `Πi, M₁ i` to `M₂` where `M₁ i` and `M₂` are modules over `R` with a topological structure. In applications, there will be compatibility conditions between the algebraic and the topological structures, but this is not needed for the definition. -/ structure continuous_multilinear_map (R : Type u) {ι : Type v} (M₁ : ι → Type w₁) (M₂ : Type w₂) [decidable_eq ι] [semiring R] [∀i, add_comm_monoid (M₁ i)] [add_comm_monoid M₂] [∀i, module R (M₁ i)] [module R M₂] [∀i, topological_space (M₁ i)] [topological_space M₂] extends multilinear_map R M₁ M₂ := (cont : continuous to_fun) notation M `[×`:25 n `]→L[`:25 R `] ` M' := continuous_multilinear_map R (λ (i : fin n), M) M' namespace continuous_multilinear_map section semiring variables [semiring R] [Πi, add_comm_monoid (M i)] [Πi, add_comm_monoid (M₁ i)] [Πi, add_comm_monoid (M₁' i)] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M₄] [Π i, module R (M i)] [Π i, module R (M₁ i)] [Π i, module R (M₁' i)] [module R M₂] [module R M₃] [module R M₄] [Π i, topological_space (M i)] [Π i, topological_space (M₁ i)] [Π i, topological_space (M₁' i)] [topological_space M₂] [topological_space M₃] [topological_space M₄] (f f' : continuous_multilinear_map R M₁ M₂) instance : has_coe_to_fun (continuous_multilinear_map R M₁ M₂) := ⟨_, λ f, f.to_multilinear_map.to_fun⟩ @[continuity] lemma coe_continuous : continuous (f : (Π i, M₁ i) → M₂) := f.cont @[simp] lemma coe_coe : (f.to_multilinear_map : (Π i, M₁ i) → M₂) = f := rfl theorem to_multilinear_map_inj : function.injective (continuous_multilinear_map.to_multilinear_map : continuous_multilinear_map R M₁ M₂ → multilinear_map R M₁ M₂) | ⟨f, hf⟩ ⟨g, hg⟩ rfl := rfl @[ext] theorem ext {f f' : continuous_multilinear_map R M₁ M₂} (H : ∀ x, f x = f' x) : f = f' := to_multilinear_map_inj $ multilinear_map.ext H @[simp] lemma map_add (m : Πi, M₁ i) (i : ι) (x y : M₁ i) : f (update m i (x + y)) = f (update m i x) + f (update m i y) := f.map_add' m i x y @[simp] lemma map_smul (m : Πi, M₁ i) (i : ι) (c : R) (x : M₁ i) : f (update m i (c • x)) = c • f (update m i x) := f.map_smul' m i c x lemma map_coord_zero {m : Πi, M₁ i} (i : ι) (h : m i = 0) : f m = 0 := f.to_multilinear_map.map_coord_zero i h @[simp] lemma map_zero [nonempty ι] : f 0 = 0 := f.to_multilinear_map.map_zero instance : has_zero (continuous_multilinear_map R M₁ M₂) := ⟨{ cont := continuous_const, ..(0 : multilinear_map R M₁ M₂) }⟩ instance : inhabited (continuous_multilinear_map R M₁ M₂) := ⟨0⟩ @[simp] lemma zero_apply (m : Πi, M₁ i) : (0 : continuous_multilinear_map R M₁ M₂) m = 0 := rfl section has_continuous_add variable [has_continuous_add M₂] instance : has_add (continuous_multilinear_map R M₁ M₂) := ⟨λ f f', ⟨f.to_multilinear_map + f'.to_multilinear_map, f.cont.add f'.cont⟩⟩ @[simp] lemma add_apply (m : Πi, M₁ i) : (f + f') m = f m + f' m := rfl @[simp] lemma to_multilinear_map_add (f g : continuous_multilinear_map R M₁ M₂) : (f + g).to_multilinear_map = f.to_multilinear_map + g.to_multilinear_map := rfl instance add_comm_monoid : add_comm_monoid (continuous_multilinear_map R M₁ M₂) := to_multilinear_map_inj.add_comm_monoid _ rfl (λ _ _, rfl) /-- Evaluation of a `continuous_multilinear_map` at a vector as an `add_monoid_hom`. -/ def apply_add_hom (m : Π i, M₁ i) : continuous_multilinear_map R M₁ M₂ →+ M₂ := ⟨λ f, f m, rfl, λ _ _, rfl⟩ @[simp] lemma sum_apply {α : Type*} (f : α → continuous_multilinear_map R M₁ M₂) (m : Πi, M₁ i) {s : finset α} : (∑ a in s, f a) m = ∑ a in s, f a m := (apply_add_hom m).map_sum f s end has_continuous_add /-- If `f` is a continuous multilinear map, then `f.to_continuous_linear_map m i` is the continuous linear map obtained by fixing all coordinates but `i` equal to those of `m`, and varying the `i`-th coordinate. -/ def to_continuous_linear_map (m : Πi, M₁ i) (i : ι) : M₁ i →L[R] M₂ := { cont := f.cont.comp (continuous_const.update i continuous_id), .. f.to_multilinear_map.to_linear_map m i } /-- The cartesian product of two continuous multilinear maps, as a continuous multilinear map. -/ def prod (f : continuous_multilinear_map R M₁ M₂) (g : continuous_multilinear_map R M₁ M₃) : continuous_multilinear_map R M₁ (M₂ × M₃) := { cont := f.cont.prod_mk g.cont, .. f.to_multilinear_map.prod g.to_multilinear_map } @[simp] lemma prod_apply (f : continuous_multilinear_map R M₁ M₂) (g : continuous_multilinear_map R M₁ M₃) (m : Πi, M₁ i) : (f.prod g) m = (f m, g m) := rfl /-- Combine a family of continuous multilinear maps with the same domain and codomains `M' i` into a continuous multilinear map taking values in the space of functions `Π i, M' i`. -/ def pi {ι' : Type*} {M' : ι' → Type*} [Π i, add_comm_monoid (M' i)] [Π i, topological_space (M' i)] [Π i, module R (M' i)] (f : Π i, continuous_multilinear_map R M₁ (M' i)) : continuous_multilinear_map R M₁ (Π i, M' i) := { cont := continuous_pi $ λ i, (f i).coe_continuous, to_multilinear_map := multilinear_map.pi (λ i, (f i).to_multilinear_map) } @[simp] lemma coe_pi {ι' : Type*} {M' : ι' → Type*} [Π i, add_comm_monoid (M' i)] [Π i, topological_space (M' i)] [Π i, module R (M' i)] (f : Π i, continuous_multilinear_map R M₁ (M' i)) : ⇑(pi f) = λ m j, f j m := rfl lemma pi_apply {ι' : Type*} {M' : ι' → Type*} [Π i, add_comm_monoid (M' i)] [Π i, topological_space (M' i)] [Π i, module R (M' i)] (f : Π i, continuous_multilinear_map R M₁ (M' i)) (m : Π i, M₁ i) (j : ι') : pi f m j = f j m := rfl /-- If `g` is continuous multilinear and `f` is a collection of continuous linear maps, then `g (f₁ m₁, ..., fₙ mₙ)` is again a continuous multilinear map, that we call `g.comp_continuous_linear_map f`. -/ def comp_continuous_linear_map (g : continuous_multilinear_map R M₁' M₄) (f : Π i : ι, M₁ i →L[R] M₁' i) : continuous_multilinear_map R M₁ M₄ := { cont := g.cont.comp $ continuous_pi $ λj, (f j).cont.comp $ continuous_apply _, .. g.to_multilinear_map.comp_linear_map (λ i, (f i).to_linear_map) } @[simp] lemma comp_continuous_linear_map_apply (g : continuous_multilinear_map R M₁' M₄) (f : Π i : ι, M₁ i →L[R] M₁' i) (m : Π i, M₁ i) : g.comp_continuous_linear_map f m = g (λ i, f i $ m i) := rfl /-- Composing a continuous multilinear map with a continuous linear map gives again a continuous multilinear map. -/ def _root_.continuous_linear_map.comp_continuous_multilinear_map (g : M₂ →L[R] M₃) (f : continuous_multilinear_map R M₁ M₂) : continuous_multilinear_map R M₁ M₃ := { cont := g.cont.comp f.cont, .. g.to_linear_map.comp_multilinear_map f.to_multilinear_map } @[simp] lemma _root_.continuous_linear_map.comp_continuous_multilinear_map_coe (g : M₂ →L[R] M₃) (f : continuous_multilinear_map R M₁ M₂) : ((g.comp_continuous_multilinear_map f) : (Πi, M₁ i) → M₃) = (g : M₂ → M₃) ∘ (f : (Πi, M₁ i) → M₂) := by { ext m, refl } /-- `continuous_multilinear_map.pi` as an `equiv`. -/ @[simps] def pi_equiv {ι' : Type*} {M' : ι' → Type*} [Π i, add_comm_monoid (M' i)] [Π i, topological_space (M' i)] [Π i, module R (M' i)] : (Π i, continuous_multilinear_map R M₁ (M' i)) ≃ continuous_multilinear_map R M₁ (Π i, M' i) := { to_fun := continuous_multilinear_map.pi, inv_fun := λ f i, (continuous_linear_map.proj i : _ →L[R] M' i).comp_continuous_multilinear_map f, left_inv := λ f, by { ext, refl }, right_inv := λ f, by { ext, refl } } /-- In the specific case of continuous multilinear maps on spaces indexed by `fin (n+1)`, where one can build an element of `Π(i : fin (n+1)), M i` using `cons`, one can express directly the additivity of a multilinear map along the first variable. -/ lemma cons_add (f : continuous_multilinear_map R M M₂) (m : Π(i : fin n), M i.succ) (x y : M 0) : f (cons (x+y) m) = f (cons x m) + f (cons y m) := f.to_multilinear_map.cons_add m x y /-- In the specific case of continuous multilinear maps on spaces indexed by `fin (n+1)`, where one can build an element of `Π(i : fin (n+1)), M i` using `cons`, one can express directly the multiplicativity of a multilinear map along the first variable. -/ lemma cons_smul (f : continuous_multilinear_map R M M₂) (m : Π(i : fin n), M i.succ) (c : R) (x : M 0) : f (cons (c • x) m) = c • f (cons x m) := f.to_multilinear_map.cons_smul m c x lemma map_piecewise_add (m m' : Πi, M₁ i) (t : finset ι) : f (t.piecewise (m + m') m') = ∑ s in t.powerset, f (s.piecewise m m') := f.to_multilinear_map.map_piecewise_add _ _ _ /-- Additivity of a continuous multilinear map along all coordinates at the same time, writing `f (m + m')` as the sum of `f (s.piecewise m m')` over all sets `s`. -/ lemma map_add_univ [fintype ι] (m m' : Πi, M₁ i) : f (m + m') = ∑ s : finset ι, f (s.piecewise m m') := f.to_multilinear_map.map_add_univ _ _ section apply_sum open fintype finset variables {α : ι → Type*} [fintype ι] (g : Π i, α i → M₁ i) (A : Π i, finset (α i)) /-- If `f` is continuous multilinear, then `f (Σ_{j₁ ∈ A₁} g₁ j₁, ..., Σ_{jₙ ∈ Aₙ} gₙ jₙ)` is the sum of `f (g₁ (r 1), ..., gₙ (r n))` where `r` ranges over all functions with `r 1 ∈ A₁`, ..., `r n ∈ Aₙ`. This follows from multilinearity by expanding successively with respect to each coordinate. -/ lemma map_sum_finset : f (λ i, ∑ j in A i, g i j) = ∑ r in pi_finset A, f (λ i, g i (r i)) := f.to_multilinear_map.map_sum_finset _ _ /-- If `f` is continuous multilinear, then `f (Σ_{j₁} g₁ j₁, ..., Σ_{jₙ} gₙ jₙ)` is the sum of `f (g₁ (r 1), ..., gₙ (r n))` where `r` ranges over all functions `r`. This follows from multilinearity by expanding successively with respect to each coordinate. -/ lemma map_sum [∀ i, fintype (α i)] : f (λ i, ∑ j, g i j) = ∑ r : Π i, α i, f (λ i, g i (r i)) := f.to_multilinear_map.map_sum _ end apply_sum section restrict_scalar variables (R) {A : Type*} [semiring A] [has_scalar R A] [Π (i : ι), module A (M₁ i)] [module A M₂] [∀ i, is_scalar_tower R A (M₁ i)] [is_scalar_tower R A M₂] /-- Reinterpret an `A`-multilinear map as an `R`-multilinear map, if `A` is an algebra over `R` and their actions on all involved modules agree with the action of `R` on `A`. -/ def restrict_scalars (f : continuous_multilinear_map A M₁ M₂) : continuous_multilinear_map R M₁ M₂ := { to_multilinear_map := f.to_multilinear_map.restrict_scalars R, cont := f.cont } @[simp] lemma coe_restrict_scalars (f : continuous_multilinear_map A M₁ M₂) : ⇑(f.restrict_scalars R) = f := rfl end restrict_scalar end semiring section ring variables [ring R] [∀i, add_comm_group (M₁ i)] [add_comm_group M₂] [∀i, module R (M₁ i)] [module R M₂] [∀i, topological_space (M₁ i)] [topological_space M₂] (f f' : continuous_multilinear_map R M₁ M₂) @[simp] lemma map_sub (m : Πi, M₁ i) (i : ι) (x y : M₁ i) : f (update m i (x - y)) = f (update m i x) - f (update m i y) := f.to_multilinear_map.map_sub _ _ _ _ section topological_add_group variable [topological_add_group M₂] instance : has_neg (continuous_multilinear_map R M₁ M₂) := ⟨λ f, {cont := f.cont.neg, ..(-f.to_multilinear_map)}⟩ @[simp] lemma neg_apply (m : Πi, M₁ i) : (-f) m = - (f m) := rfl instance : has_sub (continuous_multilinear_map R M₁ M₂) := ⟨λ f g, { cont := f.cont.sub g.cont, .. (f.to_multilinear_map - g.to_multilinear_map) }⟩ @[simp] lemma sub_apply (m : Πi, M₁ i) : (f - f') m = f m - f' m := rfl instance : add_comm_group (continuous_multilinear_map R M₁ M₂) := to_multilinear_map_inj.add_comm_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) end topological_add_group end ring section comm_semiring variables [comm_semiring R] [∀i, add_comm_monoid (M₁ i)] [add_comm_monoid M₂] [∀i, module R (M₁ i)] [module R M₂] [∀i, topological_space (M₁ i)] [topological_space M₂] (f : continuous_multilinear_map R M₁ M₂) lemma map_piecewise_smul (c : ι → R) (m : Πi, M₁ i) (s : finset ι) : f (s.piecewise (λ i, c i • m i) m) = (∏ i in s, c i) • f m := f.to_multilinear_map.map_piecewise_smul _ _ _ /-- Multiplicativity of a continuous multilinear map along all coordinates at the same time, writing `f (λ i, c i • m i)` as `(∏ i, c i) • f m`. -/ lemma map_smul_univ [fintype ι] (c : ι → R) (m : Πi, M₁ i) : f (λ i, c i • m i) = (∏ i, c i) • f m := f.to_multilinear_map.map_smul_univ _ _ variables {R' A : Type*} [comm_semiring R'] [semiring A] [algebra R' A] [Π i, module A (M₁ i)] [module R' M₂] [module A M₂] [is_scalar_tower R' A M₂] [topological_space R'] [has_continuous_smul R' M₂] instance : has_scalar R' (continuous_multilinear_map A M₁ M₂) := ⟨λ c f, { cont := continuous_const.smul f.cont, .. c • f.to_multilinear_map }⟩ @[simp] lemma smul_apply (f : continuous_multilinear_map A M₁ M₂) (c : R') (m : Πi, M₁ i) : (c • f) m = c • f m := rfl @[simp] lemma to_multilinear_map_smul (c : R') (f : continuous_multilinear_map A M₁ M₂) : (c • f).to_multilinear_map = c • f.to_multilinear_map := rfl instance {R''} [comm_semiring R''] [has_scalar R' R''] [algebra R'' A] [module R'' M₂] [is_scalar_tower R'' A M₂] [is_scalar_tower R' R'' M₂] [topological_space R''] [has_continuous_smul R'' M₂]: is_scalar_tower R' R'' (continuous_multilinear_map A M₁ M₂) := ⟨λ c₁ c₂ f, ext $ λ x, smul_assoc _ _ _⟩ variable [has_continuous_add M₂] /-- The space of continuous multilinear maps over an algebra over `R` is a module over `R`, for the pointwise addition and scalar multiplication. -/ instance : module R' (continuous_multilinear_map A M₁ M₂) := { one_smul := λ f, ext $ λ x, one_smul _ _, mul_smul := λ c₁ c₂ f, ext $ λ x, mul_smul _ _ _, smul_zero := λ r, ext $ λ x, smul_zero _, smul_add := λ r f₁ f₂, ext $ λ x, smul_add _ _ _, add_smul := λ r₁ r₂ f, ext $ λ x, add_smul _ _ _, zero_smul := λ f, ext $ λ x, zero_smul _ _ } /-- Linear map version of the map `to_multilinear_map` associating to a continuous multilinear map the corresponding multilinear map. -/ @[simps] def to_multilinear_map_linear : (continuous_multilinear_map A M₁ M₂) →ₗ[R'] (multilinear_map A M₁ M₂) := { to_fun := λ f, f.to_multilinear_map, map_add' := λ f g, rfl, map_smul' := λ c f, rfl } /-- `continuous_multilinear_map.pi` as a `linear_equiv`. -/ @[simps {simp_rhs := tt}] def pi_linear_equiv {ι' : Type*} {M' : ι' → Type*} [Π i, add_comm_monoid (M' i)] [Π i, topological_space (M' i)] [∀ i, has_continuous_add (M' i)] [Π i, module R' (M' i)] [Π i, module A (M' i)] [∀ i, is_scalar_tower R' A (M' i)] [Π i, has_continuous_smul R' (M' i)] : -- typeclass search doesn't find this instance, presumably due to struggles converting -- `Π i, module R (M' i)` to `Π i, has_scalar R (M' i)` in dependent arguments. let inst : has_continuous_smul R' (Π i, M' i) := pi.has_continuous_smul in (Π i, continuous_multilinear_map A M₁ (M' i)) ≃ₗ[R'] continuous_multilinear_map A M₁ (Π i, M' i) := { map_add' := λ x y, rfl, map_smul' := λ c x, rfl, .. pi_equiv } end comm_semiring end continuous_multilinear_map
04acac95a0c3a5f6b617ca2fad459f7d7994e269
e91b0bc0bcf14cf6e1bfc20ad1f00ad7cfa5fa76
/src/sheaves/stalk_on_basis.lean
b418b8a3515dc2c66ff735c64175bcbdba6fbfb0
[]
no_license
kckennylau/lean-scheme
b2de50025289a0339d97798466ef777e1899b0f8
8dc513ef9606d2988227490e915b7c7e173a2791
refs/heads/master
1,587,165,137,978
1,548,172,249,000
1,548,172,249,000
167,025,881
0
0
null
1,548,173,930,000
1,548,173,924,000
Lean
UTF-8
Lean
false
false
2,564
lean
-- stuff mentioned in section "Bases and sheaves" (6.30; tag 009H) -- but not in any definition etc import sheaves.presheaf_of_types_on_basis universe u -- definition after 6.30.1 and before 6.30.2 section presheaf_on_basis_stalk parameters {α : Type u} [TX : topological_space α] {B : set (set α )} {HB : topological_space.is_topological_basis B} (FPTB : presheaf_of_types_on_basis α HB) (x : α) structure presheaf_on_basis_stalk.aux := (U : set α) (BU : U ∈ B) (Hx : x ∈ U) (s : FPTB.F BU) instance presheaf_on_basis_stalk.setoid : setoid (presheaf_on_basis_stalk.aux) := sorry -- { r := λ Us Vt, ∃ (W : set X) (Hx : x ∈ W) (BW : W ∈ B) (HWU : W ⊆ Us.U) (HWV : W ⊆ Vt.U), -- FPTB.res Us.BU BW HWU Us.s = FPTB.res Vt.BU BW HWV Vt.s, -- iseqv := ⟨ -- -- reflexive -- λ Us,⟨Us.U,Us.Hx,Us.BU,set.subset.refl _,set.subset.refl _,rfl⟩, -- -- symmetric -- λ ⟨U,BU,Hx,s⟩ ⟨V,BV,HVx,t⟩ ⟨W,Hx,BW,HWU,HWV,H⟩,⟨W,Hx,BW,HWV,HWU,H.symm⟩, -- -- transitive -- λ ⟨U,BU,Hx,s⟩ ⟨V,BV,HVx,t⟩ ⟨W,BW,HWx,u⟩ ⟨W1,Hx1,BW1,HWU1,HWV1,H1⟩ ⟨W2,Hx2,BW2,HWU2,HWV2,H2⟩, -- let ⟨W3,BW3,Hx3,H3⟩ := HB.1 W1 BW1 W2 BW2 x ⟨Hx1, Hx2⟩ in -- have h1 : _ := FPTB.Hcomp U W1 W3 BU BW1 BW3 HWU1 (λ z hz, (H3 hz).1), -- have h2 : _ := FPTB.Hcomp V W1 W3 BV BW1 BW3 HWV1 (λ z hz, (H3 hz).1), -- have h3 : _ := FPTB.Hcomp V W2 W3 BV BW2 BW3 HWU2 (λ z hz, (H3 hz).2), -- have h4 : _ := FPTB.Hcomp W W2 W3 BW BW2 BW3 HWV2 (λ z hz, (H3 hz).2), -- ⟨W3,Hx3,BW3,λ z hz,HWU1 (H3 hz).1,λ z hz,HWV2 (H3 hz).2, calc -- FPTB.res BU BW3 _ s -- = FPTB.res BW1 BW3 _ (FPTB.res BU BW1 _ s) : congr_fun h1 s -- ... = FPTB.res BW1 BW3 _ (FPTB.res BV BW1 _ t) : congr_arg _ H1 -- ... = FPTB.res BV BW3 _ t : (congr_fun h2 t).symm -- ... = FPTB.res BW2 BW3 _ (FPTB.res BV BW2 _ t) : congr_fun h3 t -- ... = FPTB.res BW2 BW3 _ (FPTB.res BW BW2 _ u) : congr_arg _ H2 -- ... = FPTB.res BW BW3 _ u : (congr_fun h4 u).symm⟩⟩ -- } definition presheaf_on_basis_stalk := quotient (presheaf_on_basis_stalk.setoid) -- -- set Z is pairs (U,s) with U in B and x in U and s in FPTB.F(U) -- equiv reln on Z : (U,s) tilde (V,t) iff there exists W in B -- such that x in W, W in U, W in V, and FPT.res (U to W) s = FPT.res (V to W) t -- note basis axiom HB.1: -- (∀t₁∈s, ∀t₂∈s, ∀ x ∈ t₁ ∩ t₂, ∃ t₃∈s, x ∈ t₃ ∧ t₃ ⊆ t₁ ∩ t₂) -- Will need this for transitivity end presheaf_on_basis_stalk
2dbd56268f0effad5750bd0c4fc90fc566b70494
5c7fe6c4a9d4079b5457ffa5f061797d42a1cd65
/src/exercises/src_33_identity_and_inverse_functions.lean
896ddf9097088010abbdd245f43324258160dbd8
[]
no_license
gihanmarasingha/mth1001_tutorial
8e0817feeb96e7c1bb3bac49b63e3c9a3a329061
bb277eebd5013766e1418365b91416b406275130
refs/heads/master
1,675,008,746,310
1,607,993,443,000
1,607,993,443,000
321,511,270
3
0
null
null
null
null
UTF-8
Lean
false
false
10,222
lean
import data.set.function import tactic.linarith import tactic.norm_num open function namespace mth1001 section identity /- For a given type `α`, there is a special function, the identity function `id : α → α` defined so that `id a = a`, for every `a : α`. -/ def p₁ (x : ℕ) : ℤ := 3 * x /- For every function `f : α → β`, we have `id ∘ f = f` and `f ∘ id = f`. -/ #eval (p₁ ∘ id) 6 #eval (id ∘ p₁) 6 /- To be more precise, we should acknowledge that the identity function `id : α → α` depends on the type `α`. In Lean, we dentote this function by `@id α`. In mathematical writing, we may use a subscript `id_α` instead. So if `f : α → β`, then `f ∘ @id α = f` and `@id β ∘ f = f`. -/ #check p₁ ∘ @id ℕ #check @id ℤ ∘ p₁ end identity section inverse_examples /- Given a function `f : α → β`, a function `g : β → α` is a *right inverse* of `f` if `∀ b : β, f (g b) = b`. The function `h : β → α` is a *left inverse* of `f` if `∀ a : α, h (f a) = a`. The notions are captured by the Lean defintions `right_inverse` and `left_inverse`. An equivalent characterisation: Given a function `f : α → β`, a function `g : β → α` is a *right inverse* of `f` if `f ∘ g = @id β`. A function `h : β → α` is a *left inverse* of `f` if `h ∘ f = @id α`. Given `k₁ : left_inverse h f`, i.e. that `h` is a left inverse of `f` & given `k₂ : right_inverse g f`, i.e. that `g` is a right inverse of `f`, `left_inverse.comp_eq_id k₁` is a proof that `h ∘ f = id` and `right_inverse.comp_eq_id k₂` is a proof that `f ∘ g = id`. Note: if `g` is a right inverse of `f`, then `f` is a left inverse of `g`, and vice versa. -/ #print left_inverse #print right_inverse -- We'll construct a left inverse of the function `f₁ : ℕ → ℤ`, `f₁ x = x + 3`. def f₁ (x : ℕ) : ℤ := x + 3 /- The function `φ₁` below is defined piecewise. -/ def φ₁ : ℤ → ℕ | (n + 3 : ℕ) := n -- If `x ≥ -3`, then `φ₁ x = x - 3`. | _ := 0 -- Otherwise, `φ₁ x = 0`. #eval φ₁ 2 -- `φ₁ 2 = 0` #eval φ₁ 8 -- `φ₁ 8 = 8 - 3 = 5`. -- We show `φ₁` is a left inverse of `f₁` (equally, `f₁` is a right inverse of `φ₁`). lemma left_inv_phi1f1: left_inverse φ₁ f₁ := begin intro x, -- Assume `x : ℕ`. It suffices to prove `φ₁ (f₁ x) = x`. split, -- This holds for each of the cases that define `φ₁`. end /- Here's another piecewise function. -/ def φ₂ : ℤ → ℕ | (n + 3 : ℕ) := n -- If `x ≥ 3`, then `φ₂ x = x - 3`. | _ := 720 -- Otherwise, `φ₂ x = 720`. #eval φ₂ 2 -- `φ₂ 2 = 720` #eval φ₂ 8 -- `φ₂ 8 = 8 - 3 = 5`. -- We show `φ₂` is also a left inverse of `f₁` (equally, `f₁` is a right inverse of `φ₂`). lemma left_inv_phi2f1 : left_inverse φ₂ f₁ := begin intro x, -- Assume `x : ℕ`. It suffices to prove `φ₂ (f₁ x) = x`. split, -- This holds for each of the cases that define `φ₂`. end /- The upshot of these two examples is that even when a function has a left inverse, that left inverse need not be *unique*. Here, both `φ₁` and `φ₂` are left inverses of `f₁`. -/ /- As noted, `f₁` is a right inverse of `φ₁`. We'll now find *another* right inverse of `φ₁`. -/ def f₂ : ℕ → ℤ | 0 := 2 -- `f₂ 0 = 2`. For other values, `n`, of the input, | n := n + 3 -- `f₂ n = n + 3`. example : right_inverse f₂ φ₁ := begin intro x, -- Assume `x : ℕ`. It suffices to prove `φ₁ (f₂ x) = x`. by_cases h : (x = 0), -- We consider two cases 1. `h : x = 0` and 2. `h : x ≠ 0`. { rw h, -- Substituting `h : x = 0`, the goal is `φ₁ (f₂ 0) = 0`. refl, }, -- This is true, by reflexivity. { rw f₂, -- Use the definition of `f₂`. { split, }, -- Consider all { exact h, },}, -- possible cases. end end inverse_examples section uniqueness_theorems variable {α : Type*} variable {β : Type*} -- A function that has both a left and a right inverse is said to be *invertible*. def invertible (f : α → β) := has_left_inverse f ∧ has_right_inverse f -- For example, the identity function on any type is invertible. example : invertible (@id α) := begin split, -- It suffices to prove `has_left_inverse (@id α)` and `has_right_inverse (@id α)`. { use (@id α), -- It suffices to show `@id α` is a left inverse of `@id α`. intro x, -- Assume `x : α`. It suffices to prove `id (id x) = x`. unfold id, }, -- This follows by definition of `id`. { use (@id α), -- It suffces to show `@id α` is a right inverse of `@id α`. intro x, -- Assume `x : α`. It suffices to prove `id (id x) = x`. unfold id, }, -- This follos by definition of `id`. end /- Inverses are unique in the following sense. If a function `f : α → β` has a left inverse `h : β → α` and a right inverse `g : β → α`, then `h = g`. The proof below uses *functional extensionality*. This is the principle that, given functions `h : β → α` and `g : β → α`, the claim `h = g` is equivalent to the claim `∀ b : β, h b = g b`. -/ theorem left_inverse_eq_right_inverse (f : α → β) (g h : β → α) (k₁ : left_inverse h f) (k₂ : right_inverse g f) : h = g := begin ext b, -- Assume `b : β`. It suffices to prove `h b = g b`. calc h b = h (f (g b)) : by rw k₂ b -- Note `f (g b) = b`, as `g` is a right inverse of `f`. ... = g b : k₁ (g b) -- Further, `h (f (g b)) = g b`, as `h` is left inverse of `f`. end /- The result can also be proved using the composite-based definition of left and right inverses. The proof below, though longer, is illustrative of more general principles in algebra that you will see next term and in later years. -/ example (f : α → β) (g h : β → α) (k₁ : left_inverse h f) (k₂ : right_inverse g f) : h = g := begin calc h = h ∘ id : rfl -- This holds by reflexivity ... = h ∘ (f ∘ g) : by rw (right_inverse.comp_eq_id k₂) ... = (h ∘ f) ∘ g : rfl ... = id ∘ g : by rw (left_inverse.comp_eq_id k₁) ... = g : rfl end /- As a simple corrolary, every invertible function has exactly one left inverse and exactly one right inverse. -/ theorem unique_left_inverse_of_invertible (f : α → β) (fi : invertible f) (h₁ h₂ : β → α) (k₁ : left_inverse h₁ f) (k₂ : left_inverse h₂ f) : h₁ = h₂ := begin sorry end theorem unique_right_inverse_of_invertible (f : α → β) (fi : invertible f) (g₁ g₂ : β → α) (k₁ : right_inverse g₁ f) (k₂ : right_inverse g₂ f) : g₁ = g₂ := begin sorry end /- The function `f₁ : ℕ → ℤ` defined before by `f x := x + 3` has (at least) two different left inverses, `φ₁` and `φ₂`. We infer that `f₁` cannot be invertible! -/ end uniqueness_theorems section inverse_theorems variable {α : Type*} variable {β : Type*} /- We'll show that a function is surjective if it has a right inverse. -/ theorem surjective_of_right_inverse (f : α → β) (g : β → α) (h : right_inverse g f) : surjective f := begin sorry end /- Now we'll show a function is injective if it has a left inverse. We'll give two proofs. The second is nicer but it introduces some new Lean syntax. -/ example (f : α → β) (g : β → α) (h : left_inverse g f) : injective f := begin intros a₁ a₂ h₂, -- Assume `a₁ a₂ : α` and `h₂ : f a₁ = f a₂`. have h₃ : g (f a₁) = g (f a₂), { rw h₂, }, have h₄ : a₁ = g (f a₁), from (h a₁).symm, have h₅ : g (f a₂) = a₂, from h a₂, transitivity, { exact h₄, }, { transitivity, { exact h₃, }, { exact h₅, }, }, end /- The proof above used two applications of transitivity on three new hypotheses. Calculations like this can be expressed in `calc` mode, which simulates a mathematical series of calculations. -/ theorem injective_of_left_inverse (f : α → β) (g : β → α) (h : left_inverse g f) : injective f := begin intros a₁ a₂ h₂, -- Assume `a₁ a₂ : α` and `h₂ : f a₁ = f a₂`. calc a₁ = g (f a₁) : (h a₁).symm ... = g (f a₂) : by rw h₂ ... = a₂ : h a₂ end /- Combining the results above, we see that every invertible function is bijective. -/ theorem bijective_of_invertible (f : α → β) (k : invertible f) : bijective f := begin sorry end end inverse_theorems section application_of_inverse_theorems /- Earlier, we showed that the function `f₁ : ℕ → ℤ` given by `f₁ x = x + 3` has a left inverse (indeed, it has more than one left inverse). We infer that `f₁` is injective. -/ example : injective f₁ := injective_of_left_inverse f₁ φ₁ left_inv_phi1f1 /- It wasn't clear at the time whether `f₁` has a right inverse. We'll now show that `f₁` *doesn't* have a right inverse. We do this by using the contrapositive of the result that a function with a right inverse is surjective. -/ example : ¬(has_right_inverse f₁) := begin intro h, have h₂ : surjective f₁, from surjective_of_has_right_inverse h, have h₃ : ¬(∃ x : ℕ, f₁ x = -1), -- We have `h₃`: there is no `x : ℕ` for which `f₁ x = -1`. { push_neg, -- Don't be too concerned about the details intro x, -- of the proof of `h₃`. unfold f₁, have : ↑x + (3 : ℤ) ≥ 0, { norm_num, }, linarith, }, specialize h₂ (-1 : ℤ), -- By surjectivity of `f₁`, `∃ a : ℕ, f₁ a = -1`. exact h₃ h₂, -- But this contradicts `h₃`. end /- Likewise, we'll show that `φ₁` is surjective, but does not have a left inverse. N.B. `left_inv_phi1f1` below is equivalent to the assertion that `f₁` is a right inverse of `φ₁`. -/ example : surjective φ₁ := surjective_of_right_inverse φ₁ f₁ left_inv_phi1f1 example : ¬(has_left_inverse φ₁) := begin intro h, have h₂ : injective φ₁, from injective_of_has_left_inverse h, unfold injective at h₂, have h₃ : φ₁ 0 = φ₁ 1, { split, }, have h₄ : (0 : ℤ) = 1, from h₂ h₃, linarith, end end application_of_inverse_theorems end mth1001
1d40530df671787856804b940372cb3889f46c9a
969dbdfed67fda40a6f5a2b4f8c4a3c7dc01e0fb
/src/algebra/smul_with_zero.lean
435294d90aff414c0d7c394a6d453cc2444a0eea
[ "Apache-2.0" ]
permissive
SAAluthwela/mathlib
62044349d72dd63983a8500214736aa7779634d3
83a4b8b990907291421de54a78988c024dc8a552
refs/heads/master
1,679,433,873,417
1,615,998,031,000
1,615,998,031,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,423
lean
/- Copyright (c) 2021 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa -/ import group_theory.group_action.defs /-! # Introduce `smul_with_zero` In analogy with the usual monoid action on a Type `M`, we introduce an action of a `monoid_with_zero` on a Type with `0`. In particular, for Types `R` and `M`, both containing `0`, we define `smul_with_zero R M` to be the typeclass where the products `r • 0` and `0 • m` vanish for all `r : R` and all `m : M`. Moreover, in the case in which `R` is a `monoid_with_zero`, we introduce the typeclass `mul_action_with_zero R M`, mimicking group actions and having an absorbing `0` in `R`. Thus, the action is required to be compatible with * the unit of the monoid, acting as the identity; * the zero of the monoid_with_zero, acting as zero; * associativity of the monoid. We also add `instances`: * any `monoid_with_zero` has a `mul_action_with_zero R R` acting on itself; -/ variables (R M : Type*) section has_zero variable [has_zero M] /-- `smul_with_zero` is a class consisting of a Type `R` with `0 : R` and a scalar multiplication of `R` on a Type `M` with `0`, such that the equality `r • m = 0` holds if at least one among `r` or `m` equals `0`. -/ class smul_with_zero [has_zero R] extends has_scalar R M := (smul_zero : ∀ r : R, r • (0 : M) = 0) (zero_smul : ∀ m : M, (0 : R) • m = 0) variables {M} @[simp] lemma zero_smul [has_zero R] [smul_with_zero R M] (m : M) : (0 : R) • m = 0 := smul_with_zero.zero_smul m variables (M) section monoid_with_zero variable [monoid_with_zero R] /-- An action of a monoid with zero `R` on a Type `M`, also with `0`, compatible with `0` (both in `R` and in `M`), with `1 ∈ R`, and with associativity of multiplication on the monoid `M`. -/ class mul_action_with_zero extends mul_action R M := -- these fields are copied from `smul_with_zero`, as `extends` behaves poorly (smul_zero : ∀ r : R, r • (0 : M) = 0) (zero_smul : ∀ m : M, (0 : R) • m = 0) @[priority 100] -- see Note [lower instance priority] instance mul_action_with_zero.to_smul_with_zero [m : mul_action_with_zero R M] : smul_with_zero R M := {..m} instance monoid_with_zero.to_mul_action_with_zero : mul_action_with_zero R R := { smul_zero := mul_zero, zero_smul := zero_mul, ..monoid.to_mul_action R } end monoid_with_zero end has_zero
b50dbc46f90f080aa9961d96b65402e8ee2a54b6
cbcb0199842f03e7606d4e43666573fc15dd07a5
/src/measure_theory/borel_space.lean
b001835370bce20e1a07a8bc06e1de2566bf51bc
[ "Apache-2.0" ]
permissive
truonghoangle/mathlib
a6a7c14b3767ec71156239d8ea97f6921fe79627
673bae584febcd830c2c9256eb7e7a81e27ed303
refs/heads/master
1,590,347,998,944
1,559,728,860,000
1,559,728,860,000
187,431,971
0
0
null
1,558,238,525,000
1,558,238,525,000
null
UTF-8
Lean
false
false
18,619
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 Borel (measurable) space -- the smallest σ-algebra generated by open sets It would be nice to encode this in the topological space type class, i.e. each topological space carries a measurable space, the Borel space. This would be similar how each uniform space carries a topological space. The idea is to allow definitional equality for product instances. We would like to have definitional equality for borel t₁ × borel t₂ = borel (t₁ × t₂) Unfortunately, this only holds if t₁ and t₂ are second-countable topologies. -/ import measure_theory.measurable_space topology.instances.ennreal noncomputable theory open classical set lattice real local attribute [instance] prop_decidable universes u v w x y variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} {ι : Sort y} {s t u : set α} namespace measure_theory open measurable_space topological_space @[instance, priority 900] def borel (α : Type u) [topological_space α] : measurable_space α := generate_from {s : set α | is_open s} lemma borel_eq_generate_from_of_subbasis {s : set (set α)} [t : topological_space α] [second_countable_topology α] (hs : t = generate_from s) : borel α = generate_from s := le_antisymm (generate_from_le $ assume u (hu : t.is_open u), begin rw [hs] at hu, induction hu, case generate_open.basic : u hu { exact generate_measurable.basic u hu }, case generate_open.univ { exact @is_measurable.univ α (generate_from s) }, case generate_open.inter : s₁ s₂ _ _ hs₁ hs₂ { exact @is_measurable.inter α (generate_from s) _ _ hs₁ hs₂ }, case generate_open.sUnion : f hf ih { rcases is_open_sUnion_countable f (by rwa hs) with ⟨v, hv, vf, vu⟩, rw ← vu, exact @is_measurable.sUnion α (generate_from s) _ hv (λ x xv, ih _ (vf xv)) } end) (generate_from_le $ assume u hu, generate_measurable.basic _ $ show t.is_open u, by rw [hs]; exact generate_open.basic _ hu) lemma borel_eq_generate_Iio (α) [topological_space α] [second_countable_topology α] [linear_order α] [orderable_topology α] : borel α = generate_from (range Iio) := begin refine le_antisymm _ (generate_from_le _), { rw borel_eq_generate_from_of_subbasis (orderable_topology.topology_eq_generate_intervals α), have H : ∀ a:α, is_measurable (measurable_space.generate_from (range Iio)) (Iio a) := λ a, generate_measurable.basic _ ⟨_, rfl⟩, refine generate_from_le _, rintro _ ⟨a, rfl | rfl⟩; [skip, apply H], by_cases h : ∃ a', ∀ b, a < b ↔ a' ≤ b, { rcases h with ⟨a', ha'⟩, rw (_ : {b | a < b} = -Iio a'), {exact (H _).compl _}, simp [set.ext_iff, ha'] }, { rcases is_open_Union_countable (λ a' : {a' : α // a < a'}, {b | a'.1 < b}) (λ a', is_open_lt' _) with ⟨v, ⟨hv⟩, vu⟩, simp [set.ext_iff] at vu, have : {b | a < b} = ⋃ x : v, -Iio x.1.1, { simp [set.ext_iff], refine λ x, ⟨λ ax, _, λ ⟨a', ⟨h, av⟩, ax⟩, lt_of_lt_of_le h ax⟩, rcases (vu x).2 _ with ⟨a', h₁, h₂⟩, { exact ⟨a', h₁, le_of_lt h₂⟩ }, refine not_imp_comm.1 (λ h, _) h, exact ⟨x, λ b, ⟨λ ab, le_of_not_lt (λ h', h ⟨b, ab, h'⟩), lt_of_lt_of_le ax⟩⟩ }, rw this, resetI, apply is_measurable.Union, exact λ _, (H _).compl _ } }, { simp, rintro _ a rfl, exact generate_measurable.basic _ is_open_Iio } end lemma borel_eq_generate_Ioi (α) [topological_space α] [second_countable_topology α] [linear_order α] [orderable_topology α] : borel α = generate_from (range (λ a, {x | a < x})) := begin refine le_antisymm _ (generate_from_le _), { rw borel_eq_generate_from_of_subbasis (orderable_topology.topology_eq_generate_intervals α), have H : ∀ a:α, is_measurable (measurable_space.generate_from (range (λ a, {x | a < x}))) {x | a < x} := λ a, generate_measurable.basic _ ⟨_, rfl⟩, refine generate_from_le _, rintro _ ⟨a, rfl | rfl⟩, {apply H}, by_cases h : ∃ a', ∀ b, b < a ↔ b ≤ a', { rcases h with ⟨a', ha'⟩, rw (_ : {b | b < a} = -{x | a' < x}), {exact (H _).compl _}, simp [set.ext_iff, ha'] }, { rcases is_open_Union_countable (λ a' : {a' : α // a' < a}, {b | b < a'.1}) (λ a', is_open_gt' _) with ⟨v, ⟨hv⟩, vu⟩, simp [set.ext_iff] at vu, have : {b | b < a} = ⋃ x : v, -{b | x.1.1 < b}, { simp [set.ext_iff], refine λ x, ⟨λ ax, _, λ ⟨a', ⟨h, av⟩, ax⟩, lt_of_le_of_lt ax h⟩, rcases (vu x).2 _ with ⟨a', h₁, h₂⟩, { exact ⟨a', h₁, le_of_lt h₂⟩ }, refine not_imp_comm.1 (λ h, _) h, exact ⟨x, λ b, ⟨λ ab, le_of_not_lt (λ h', h ⟨b, ab, h'⟩), λ h, lt_of_le_of_lt h ax⟩⟩ }, rw this, resetI, apply is_measurable.Union, exact λ _, (H _).compl _ } }, { simp, rintro _ a rfl, exact generate_measurable.basic _ (is_open_lt' _) } end lemma borel_comap {f : α → β} {t : topological_space β} : @borel α (t.induced f) = (@borel β t).comap f := calc @borel α (t.induced f) = measurable_space.generate_from (preimage f '' {s | is_open s }) : congr_arg measurable_space.generate_from $ set.ext $ assume s : set α, show (t.induced f).is_open s ↔ s ∈ preimage f '' {s | is_open s}, by simp [topological_space.induced, set.image, eq_comm]; refl ... = (@borel β t).comap f : comap_generate_from.symm section variables [topological_space α] lemma is_measurable_of_is_open : is_open s → is_measurable s := generate_measurable.basic s lemma is_measurable_interior : is_measurable (interior s) := is_measurable_of_is_open is_open_interior lemma is_measurable_of_is_closed (h : is_closed s) : is_measurable s := is_measurable.compl_iff.1 $ is_measurable_of_is_open h lemma is_measurable_closure : is_measurable (closure s) := is_measurable_of_is_closed is_closed_closure lemma measurable_of_continuous [topological_space β] {f : α → β} (h : continuous f) : measurable f := measurable_generate_from $ assume t ht, is_measurable_of_is_open $ h t ht lemma borel_prod_le [topological_space β] : prod.measurable_space ≤ borel (α × β) := sup_le (comap_le_iff_le_map.mpr $ measurable_of_continuous continuous_fst) (comap_le_iff_le_map.mpr $ measurable_of_continuous continuous_snd) lemma borel_induced [t : topological_space β] (f : α → β) : @borel α (t.induced f) = (borel β).comap f := comap_generate_from.symm lemma borel_eq_subtype [topological_space α] (s : set α) : borel s = subtype.measurable_space := borel_induced coe lemma borel_prod [second_countable_topology α] [topological_space β] [second_countable_topology β] : prod.measurable_space = borel (α × β) := let ⟨a, ha₁, ha₂, ha₃, ha₄, ha₅⟩ := @is_open_generated_countable_inter α _ _ in let ⟨b, hb₁, hb₂, hb₃, hb₄, hb₅⟩ := @is_open_generated_countable_inter β _ _ in le_antisymm borel_prod_le begin have : prod.topological_space = generate_from {g | ∃u∈a, ∃v∈b, g = set.prod u v}, { rw [ha₅, hb₅], exact prod_generate_from_generate_from_eq ha₄ hb₄ }, rw [borel_eq_generate_from_of_subbasis this], exact generate_from_le (assume p ⟨u, hu, v, hv, eq⟩, have hu : is_open u, by rw [ha₅]; exact generate_open.basic _ hu, have hv : is_open v, by rw [hb₅]; exact generate_open.basic _ hv, eq.symm ▸ is_measurable_set_prod (is_measurable_of_is_open hu) (is_measurable_of_is_open hv)) end lemma measurable_of_continuous2 [topological_space α] [second_countable_topology α] [topological_space β] [second_countable_topology β] [topological_space γ] [measurable_space δ] {f : δ → α} {g : δ → β} {c : α → β → γ} (h : continuous (λp:α×β, c p.1 p.2)) (hf : measurable f) (hg : measurable g) : measurable (λa, c (f a) (g a)) := show measurable ((λp:α×β, c p.1 p.2) ∘ (λa, (f a, g a))), begin apply measurable.comp, { rw borel_prod, exact measurable_of_continuous h }, { exact measurable_prod_mk hf hg } end lemma measurable_add [add_monoid α] [topological_add_monoid α] [second_countable_topology α] [measurable_space β] {f : β → α} {g : β → α} : measurable f → measurable g → measurable (λa, f a + g a) := measurable_of_continuous2 continuous_add' lemma measurable_finset_sum {ι : Type*} [add_comm_monoid α] [topological_add_monoid α] [second_countable_topology α] [measurable_space β] {f : ι → β → α} (s : finset ι) (hf : ∀i, measurable (f i)) : measurable (λa, s.sum (λi, f i a)) := finset.induction_on s (by simpa using measurable_const) (assume i s his ih, by simpa [his] using measurable_add (hf i) ih) lemma measurable_neg [add_group α] [topological_add_group α] [measurable_space β] {f : β → α} (hf : measurable f) : measurable (λa, - f a) := (measurable_of_continuous continuous_neg').comp hf lemma measurable_sub [add_group α] [topological_add_group α] [second_countable_topology α] [measurable_space β] {f : β → α} {g : β → α} : measurable f → measurable g → measurable (λa, f a - g a) := measurable_of_continuous2 continuous_sub' lemma measurable_mul [monoid α] [topological_monoid α] [second_countable_topology α] [measurable_space β] {f : β → α} {g : β → α} : measurable f → measurable g → measurable (λa, f a * g a) := measurable_of_continuous2 continuous_mul' lemma measurable_le {α β} [topological_space α] [partial_order α] [ordered_topology α] [second_countable_topology α] [measurable_space β] {f : β → α} {g : β → α} (hf : measurable f) (hg : measurable g) : is_measurable {a | f a ≤ g a} := have is_measurable {p : α × α | p.1 ≤ p.2}, by rw borel_prod; exact is_measurable_of_is_closed (ordered_topology.is_closed_le' _), show is_measurable {a | (f a, g a).1 ≤ (f a, g a).2}, begin refine measurable.preimage _ this, exact measurable_prod_mk hf hg end -- generalize lemma measurable_coe_int_real : measurable (λa, a : ℤ → ℝ) := assume s (hs : is_measurable s), by trivial section ordered_topology variables [linear_order α] [topological_space α] [ordered_topology α] {a b c : α} lemma is_measurable_Ioo : is_measurable (Ioo a b) := is_measurable_of_is_open is_open_Ioo lemma is_measurable_Iio : is_measurable (Iio a) := is_measurable_of_is_open is_open_Iio lemma is_measurable_Ico : is_measurable (Ico a b) := (is_measurable_of_is_closed $ is_closed_le continuous_const continuous_id).inter is_measurable_Iio end ordered_topology lemma measurable.is_lub {α} [topological_space α] [linear_order α] [orderable_topology α] [second_countable_topology α] {β} [measurable_space β] {ι} [encodable ι] {f : ι → β → α} {g : β → α} (hf : ∀ i, measurable (f i)) (hg : ∀ b, is_lub {a | ∃ i, f i b = a} (g b)) : measurable g := begin rw borel_eq_generate_Ioi α, apply measurable_generate_from, rintro _ ⟨a, rfl⟩, have : {b | a < g b} = ⋃ i, {b | a < f i b}, { simp [set.ext_iff], intro b, rw [lt_is_lub_iff (hg b)], exact ⟨λ ⟨_, ⟨i, rfl⟩, h⟩, ⟨i, h⟩, λ ⟨i, h⟩, ⟨_, ⟨i, rfl⟩, h⟩⟩ }, show is_measurable {b | a < g b}, rw this, exact is_measurable.Union (λ i, hf i _ (is_measurable_of_is_open (is_open_lt' _))) end lemma measurable.is_glb {α} [topological_space α] [linear_order α] [orderable_topology α] [second_countable_topology α] {β} [measurable_space β] {ι} [encodable ι] {f : ι → β → α} {g : β → α} (hf : ∀ i, measurable (f i)) (hg : ∀ b, is_glb {a | ∃ i, f i b = a} (g b)) : measurable g := begin rw borel_eq_generate_Iio α, apply measurable_generate_from, rintro _ ⟨a, rfl⟩, have : {b | g b < a} = ⋃ i, {b | f i b < a}, { simp [set.ext_iff], intro b, rw [is_glb_lt_iff (hg b)], exact ⟨λ ⟨_, ⟨i, rfl⟩, h⟩, ⟨i, h⟩, λ ⟨i, h⟩, ⟨_, ⟨i, rfl⟩, h⟩⟩ }, show is_measurable {b | g b < a}, rw this, exact is_measurable.Union (λ i, hf i _ (is_measurable_of_is_open (is_open_gt' _))) end lemma measurable.supr {α} [topological_space α] [complete_linear_order α] [orderable_topology α] [second_countable_topology α] {β} [measurable_space β] {ι} [encodable ι] {f : ι → β → α} (hf : ∀ i, measurable (f i)) : measurable (λ b, ⨆ i, f i b) := measurable.is_lub hf $ λ b, is_lub_supr lemma measurable.infi {α} [topological_space α] [complete_linear_order α] [orderable_topology α] [second_countable_topology α] {β} [measurable_space β] {ι} [encodable ι] {f : ι → β → α} (hf : ∀ i, measurable (f i)) : measurable (λ b, ⨅ i, f i b) := measurable.is_glb hf $ λ b, is_glb_infi end end measure_theory def homemorph.to_measurable_equiv [topological_space α] [topological_space β] (h : α ≃ₜ β) : measurable_equiv α β := { to_equiv := h.to_equiv, measurable_to_fun := measure_theory.measurable_of_continuous h.continuous_to_fun, measurable_inv_fun := measure_theory.measurable_of_continuous h.continuous_inv_fun } namespace real open measure_theory measurable_space lemma borel_eq_generate_from_Ioo_rat : borel ℝ = generate_from (⋃(a b : ℚ) (h : a < b), {Ioo a b}) := borel_eq_generate_from_of_subbasis is_topological_basis_Ioo_rat.2.2 lemma borel_eq_generate_from_Iio_rat : borel ℝ = generate_from (⋃a:ℚ, {Iio a}) := begin let g, swap, apply le_antisymm (_ : _ ≤ g) (measurable_space.generate_from_le (λ t, _)), { rw borel_eq_generate_from_Ioo_rat, refine generate_from_le (λ t, _), simp only [mem_Union], rintro ⟨a, b, h, rfl|⟨⟨⟩⟩⟩, rw (set.ext (λ x, _) : Ioo (a:ℝ) b = (⋃c>a, - Iio c) ∩ Iio b), { have hg : ∀q:ℚ, g.is_measurable (Iio q) := λ q, generate_measurable.basic _ (by simp; exact ⟨_, rfl⟩), refine @is_measurable.inter _ g _ _ _ (hg _), refine @is_measurable.bUnion _ _ g _ _ (countable_encodable _) (λ c h, _), exact @is_measurable.compl _ _ g (hg _) }, { simp [Ioo, Iio], refine and_congr _ iff.rfl, exact ⟨λ h, let ⟨c, ac, cx⟩ := exists_rat_btwn h in ⟨c, rat.cast_lt.1 ac, le_of_lt cx⟩, λ ⟨c, ac, cx⟩, lt_of_lt_of_le (rat.cast_lt.2 ac) cx⟩ } }, { simp, rintro r rfl, exact is_measurable_of_is_open (is_open_gt' _) } end end real namespace ennreal open filter measure_theory lemma measurable_coe : measurable (coe : nnreal → ennreal) := measurable_of_continuous (continuous_coe.2 continuous_id) def ennreal_equiv_nnreal : measurable_equiv {r : ennreal | r < ⊤} nnreal := { to_fun := λr, ennreal.to_nnreal r.1, inv_fun := λr, ⟨r, coe_lt_top⟩, left_inv := assume ⟨r, hr⟩, by simp [coe_to_nnreal (ne_of_lt hr)], right_inv := assume r, to_nnreal_coe, measurable_to_fun := begin rw [← borel_eq_subtype], refine measurable_of_continuous (continuous_iff_continuous_at.2 _), rintros ⟨r, hr⟩, simp [continuous_at, nhds_subtype_eq_comap], refine tendsto.comp tendsto_comap (tendsto_to_nnreal (ne_of_lt hr)) end, measurable_inv_fun := measurable_subtype_mk measurable_coe } lemma measurable_of_measurable_nnreal [measurable_space α] {f : ennreal → α} (h : measurable (λp:nnreal, f p)) : measurable f := begin refine measurable_of_measurable_union_cover {⊤} {r : ennreal | r < ⊤} (is_measurable_of_is_closed $ is_closed_singleton) (is_measurable_of_is_open $ is_open_gt' _) (assume r _, by cases r; simp [ennreal.none_eq_top, ennreal.some_eq_coe]) _ _, exact (measurable_equiv.set.singleton ⊤).symm.measurable_coe_iff.1 (measurable_unit _), exact (ennreal_equiv_nnreal.symm.measurable_coe_iff.1 h) end def ennreal_equiv_sum : @measurable_equiv ennreal (nnreal ⊕ unit) _ sum.measurable_space := { to_fun := @option.rec nnreal (λ_, nnreal ⊕ unit) (sum.inr ()) (sum.inl : nnreal → nnreal ⊕ unit), inv_fun := @sum.rec nnreal unit (λ_, ennreal) (coe : nnreal → ennreal) (λ_, ⊤), left_inv := assume s, by cases s; refl, right_inv := assume s, by rcases s with r | ⟨⟨⟩⟩; refl, measurable_to_fun := measurable_of_measurable_nnreal measurable_inl, measurable_inv_fun := measurable_sum measurable_coe (@measurable_const ennreal unit _ _ ⊤) } lemma measurable_of_measurable_nnreal_nnreal [measurable_space α] [measurable_space β] (f : ennreal → ennreal → β) {g : α → ennreal} {h : α → ennreal} (h₁ : measurable (λp:nnreal × nnreal, f p.1 p.2)) (h₂ : measurable (λr:nnreal, f ⊤ r)) (h₃ : measurable (λr:nnreal, f r ⊤)) (hg : measurable g) (hh : measurable h) : measurable (λa, f (g a) (h a)) := let e : measurable_equiv (ennreal × ennreal) (((nnreal × nnreal) ⊕ (nnreal × unit)) ⊕ ((unit × nnreal) ⊕ (unit × unit))) := (measurable_equiv.prod_congr ennreal_equiv_sum ennreal_equiv_sum).trans (measurable_equiv.sum_prod_sum _ _ _ _) in have measurable (λp:ennreal×ennreal, f p.1 p.2), begin refine e.symm.measurable_coe_iff.1 (measurable_sum (measurable_sum _ _) (measurable_sum _ _)), { show measurable (λp:nnreal × nnreal, f p.1 p.2), exact h₁ }, { show measurable (λp:nnreal × unit, f p.1 ⊤), exact h₃.comp (measurable_fst measurable_id) }, { show measurable ((λp:nnreal, f ⊤ p) ∘ (λp:unit × nnreal, p.2)), exact h₂.comp (measurable_snd measurable_id) }, { show measurable (λp:unit × unit, f ⊤ ⊤), exact measurable_const } end, this.comp (measurable_prod_mk hg hh) lemma measurable_mul {α : Type*} [measurable_space α] {f g : α → ennreal} : measurable f → measurable g → measurable (λa, f a * g a) := begin refine measurable_of_measurable_nnreal_nnreal (*) _ _ _, { simp only [ennreal.coe_mul.symm], exact measurable_coe.comp (measurable_mul (measurable_fst measurable_id) (measurable_snd measurable_id)) }, { simp [top_mul], exact measurable.if (is_measurable_of_is_closed $ is_closed_eq continuous_id continuous_const) measurable_const measurable_const }, { simp [mul_top], exact measurable.if (is_measurable_of_is_closed $ is_closed_eq continuous_id continuous_const) measurable_const measurable_const } end end ennreal
c0fd5a5d310930cdd4a83a3713269149f8f71336
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/list/palindrome.lean
7e741c4ea071e9e9d46ef3b173f9bcf54458d912
[]
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,869
lean
/- Copyright (c) 2020 Google LLC. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Wong -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.list.basic import Mathlib.PostPort universes u_1 namespace Mathlib /-! # Palindromes This module defines *palindromes*, lists which are equal to their reverse. The main result is the `palindrome` inductive type, and its associated `palindrome.rec_on` induction principle. Also provided are conversions to and from other equivalent definitions. ## References * [Pierre Castéran, *On palindromes*][casteran] [casteran]: https://www.labri.fr/perso/casteran/CoqArt/inductive-prop-chap/palindrome.html ## Tags palindrome, reverse, induction -/ /-- `palindrome l` asserts that `l` is a palindrome. This is defined inductively: * The empty list is a palindrome; * A list with one element is a palindrome; * Adding the same element to both ends of a palindrome results in a bigger palindrome. -/ inductive palindrome {α : Type u_1} : List α → Prop where | nil : palindrome [] | singleton : ∀ (x : α), palindrome [x] | cons_concat : ∀ (x : α) {l : List α}, palindrome l → palindrome (x :: (l ++ [x])) namespace palindrome theorem reverse_eq {α : Type u_1} {l : List α} (p : palindrome l) : list.reverse l = l := sorry theorem of_reverse_eq {α : Type u_1} {l : List α} : list.reverse l = l → palindrome l := sorry theorem iff_reverse_eq {α : Type u_1} {l : List α} : palindrome l ↔ list.reverse l = l := { mp := reverse_eq, mpr := of_reverse_eq } theorem append_reverse {α : Type u_1} (l : List α) : palindrome (l ++ list.reverse l) := sorry protected instance decidable {α : Type u_1} [DecidableEq α] (l : List α) : Decidable (palindrome l) := decidable_of_iff' (list.reverse l = l) iff_reverse_eq
aa0061fb0f3ef90f07dc3c3772d2cc226e13292a
bb31430994044506fa42fd667e2d556327e18dfe
/src/order/hom/complete_lattice.lean
e37431e776b624337cfb6d5afe573f8d25503c26
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
26,672
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.complete_lattice import order.hom.lattice /-! # Complete lattice homomorphisms This file defines frame homorphisms and complete lattice homomorphisms. We use the `fun_like` design, so each type of morphisms has a companion typeclass which is meant to be satisfied by itself and all stricter types. ## Types of morphisms * `Sup_hom`: Maps which preserve `⨆`. * `Inf_hom`: Maps which preserve `⨅`. * `frame_hom`: Frame homomorphisms. Maps which preserve `⨆`, `⊓` and `⊤`. * `complete_lattice_hom`: Complete lattice homomorphisms. Maps which preserve `⨆` and `⨅`. ## Typeclasses * `Sup_hom_class` * `Inf_hom_class` * `frame_hom_class` * `complete_lattice_hom_class` ## Concrete homs * `complete_lattice.set_preimage`: `set.preimage` as a complete lattice homomorphism. ## TODO Frame homs are Heyting homs. -/ open function order_dual set variables {F α β γ δ : Type*} {ι : Sort*} {κ : ι → Sort*} /-- The type of `⨆`-preserving functions from `α` to `β`. -/ structure Sup_hom (α β : Type*) [has_Sup α] [has_Sup β] := (to_fun : α → β) (map_Sup' (s : set α) : to_fun (Sup s) = Sup (to_fun '' s)) /-- The type of `⨅`-preserving functions from `α` to `β`. -/ structure Inf_hom (α β : Type*) [has_Inf α] [has_Inf β] := (to_fun : α → β) (map_Inf' (s : set α) : to_fun (Inf s) = Inf (to_fun '' s)) /-- The type of frame homomorphisms from `α` to `β`. They preserve finite meets and arbitrary joins. -/ structure frame_hom (α β : Type*) [complete_lattice α] [complete_lattice β] extends inf_top_hom α β := (map_Sup' (s : set α) : to_fun (Sup s) = Sup (to_fun '' s)) /-- The type of complete lattice homomorphisms from `α` to `β`. -/ structure complete_lattice_hom (α β : Type*) [complete_lattice α] [complete_lattice β] extends Inf_hom α β := (map_Sup' (s : set α) : to_fun (Sup s) = Sup (to_fun '' s)) section set_option old_structure_cmd true /-- `Sup_hom_class F α β` states that `F` is a type of `⨆`-preserving morphisms. You should extend this class when you extend `Sup_hom`. -/ class Sup_hom_class (F : Type*) (α β : out_param $ Type*) [has_Sup α] [has_Sup β] extends fun_like F α (λ _, β) := (map_Sup (f : F) (s : set α) : f (Sup s) = Sup (f '' s)) /-- `Inf_hom_class F α β` states that `F` is a type of `⨅`-preserving morphisms. You should extend this class when you extend `Inf_hom`. -/ class Inf_hom_class (F : Type*) (α β : out_param $ Type*) [has_Inf α] [has_Inf β] extends fun_like F α (λ _, β) := (map_Inf (f : F) (s : set α) : f (Inf s) = Inf (f '' s)) /-- `frame_hom_class F α β` states that `F` is a type of frame morphisms. They preserve `⊓` and `⨆`. You should extend this class when you extend `frame_hom`. -/ class frame_hom_class (F : Type*) (α β : out_param $ Type*) [complete_lattice α] [complete_lattice β] extends inf_top_hom_class F α β := (map_Sup (f : F) (s : set α) : f (Sup s) = Sup (f '' s)) /-- `complete_lattice_hom_class F α β` states that `F` is a type of complete lattice morphisms. You should extend this class when you extend `complete_lattice_hom`. -/ class complete_lattice_hom_class (F : Type*) (α β : out_param $ Type*) [complete_lattice α] [complete_lattice β] extends Inf_hom_class F α β := (map_Sup (f : F) (s : set α) : f (Sup s) = Sup (f '' s)) end export Sup_hom_class (map_Sup) export Inf_hom_class (map_Inf) attribute [simp] map_Sup map_Inf lemma map_supr [has_Sup α] [has_Sup β] [Sup_hom_class F α β] (f : F) (g : ι → α) : f (⨆ i, g i) = ⨆ i, f (g i) := by rw [supr, supr, map_Sup, set.range_comp] lemma map_supr₂ [has_Sup α] [has_Sup β] [Sup_hom_class F α β] (f : F) (g : Π i, κ i → α) : f (⨆ i j, g i j) = ⨆ i j, f (g i j) := by simp_rw map_supr lemma map_infi [has_Inf α] [has_Inf β] [Inf_hom_class F α β] (f : F) (g : ι → α) : f (⨅ i, g i) = ⨅ i, f (g i) := by rw [infi, infi, map_Inf, set.range_comp] lemma map_infi₂ [has_Inf α] [has_Inf β] [Inf_hom_class F α β] (f : F) (g : Π i, κ i → α) : f (⨅ i j, g i j) = ⨅ i j, f (g i j) := by simp_rw map_infi @[priority 100] -- See note [lower instance priority] instance Sup_hom_class.to_sup_bot_hom_class [complete_lattice α] [complete_lattice β] [Sup_hom_class F α β] : sup_bot_hom_class F α β := { map_sup := λ f a b, by rw [←Sup_pair, map_Sup, set.image_pair, Sup_pair], map_bot := λ f, by rw [←Sup_empty, map_Sup, set.image_empty, Sup_empty], ..‹Sup_hom_class F α β› } @[priority 100] -- See note [lower instance priority] instance Inf_hom_class.to_inf_top_hom_class [complete_lattice α] [complete_lattice β] [Inf_hom_class F α β] : inf_top_hom_class F α β := { map_inf := λ f a b, by rw [←Inf_pair, map_Inf, set.image_pair, Inf_pair], map_top := λ f, by rw [←Inf_empty, map_Inf, set.image_empty, Inf_empty], ..‹Inf_hom_class F α β› } @[priority 100] -- See note [lower instance priority] instance frame_hom_class.to_Sup_hom_class [complete_lattice α] [complete_lattice β] [frame_hom_class F α β] : Sup_hom_class F α β := { .. ‹frame_hom_class F α β› } @[priority 100] -- See note [lower instance priority] instance frame_hom_class.to_bounded_lattice_hom_class [complete_lattice α] [complete_lattice β] [frame_hom_class F α β] : bounded_lattice_hom_class F α β := { .. ‹frame_hom_class F α β›, ..Sup_hom_class.to_sup_bot_hom_class } @[priority 100] -- See note [lower instance priority] instance complete_lattice_hom_class.to_frame_hom_class [complete_lattice α] [complete_lattice β] [complete_lattice_hom_class F α β] : frame_hom_class F α β := { .. ‹complete_lattice_hom_class F α β›, ..Inf_hom_class.to_inf_top_hom_class } @[priority 100] -- See note [lower instance priority] instance complete_lattice_hom_class.to_bounded_lattice_hom_class [complete_lattice α] [complete_lattice β] [complete_lattice_hom_class F α β] : bounded_lattice_hom_class F α β := { ..Sup_hom_class.to_sup_bot_hom_class, ..Inf_hom_class.to_inf_top_hom_class } @[priority 100] -- See note [lower instance priority] instance order_iso_class.to_Sup_hom_class [complete_lattice α] [complete_lattice β] [order_iso_class F α β] : Sup_hom_class F α β := { map_Sup := λ f s, eq_of_forall_ge_iff $ λ c, by simp only [←le_map_inv_iff, Sup_le_iff, set.ball_image_iff], .. show order_hom_class F α β, from infer_instance } @[priority 100] -- See note [lower instance priority] instance order_iso_class.to_Inf_hom_class [complete_lattice α] [complete_lattice β] [order_iso_class F α β] : Inf_hom_class F α β := { map_Inf := λ f s, eq_of_forall_le_iff $ λ c, by simp only [←map_inv_le_iff, le_Inf_iff, set.ball_image_iff], .. show order_hom_class F α β, from infer_instance } @[priority 100] -- See note [lower instance priority] instance order_iso_class.to_complete_lattice_hom_class [complete_lattice α] [complete_lattice β] [order_iso_class F α β] : complete_lattice_hom_class F α β := { ..order_iso_class.to_Sup_hom_class, ..order_iso_class.to_lattice_hom_class, .. show Inf_hom_class F α β, from infer_instance } instance [has_Sup α] [has_Sup β] [Sup_hom_class F α β] : has_coe_t F (Sup_hom α β) := ⟨λ f, ⟨f, map_Sup f⟩⟩ instance [has_Inf α] [has_Inf β] [Inf_hom_class F α β] : has_coe_t F (Inf_hom α β) := ⟨λ f, ⟨f, map_Inf f⟩⟩ instance [complete_lattice α] [complete_lattice β] [frame_hom_class F α β] : has_coe_t F (frame_hom α β) := ⟨λ f, ⟨f, map_Sup f⟩⟩ instance [complete_lattice α] [complete_lattice β] [complete_lattice_hom_class F α β] : has_coe_t F (complete_lattice_hom α β) := ⟨λ f, ⟨f, map_Sup f⟩⟩ /-! ### Supremum homomorphisms -/ namespace Sup_hom variables [has_Sup α] section has_Sup variables [has_Sup β] [has_Sup γ] [has_Sup δ] instance : Sup_hom_class (Sup_hom α β) α β := { coe := Sup_hom.to_fun, coe_injective' := λ f g h, by cases f; cases g; congr', map_Sup := Sup_hom.map_Sup' } /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ instance : has_coe_to_fun (Sup_hom α β) (λ _, α → β) := fun_like.has_coe_to_fun @[simp] lemma to_fun_eq_coe {f : Sup_hom α β} : f.to_fun = (f : α → β) := rfl @[ext] lemma ext {f g : Sup_hom α β} (h : ∀ a, f a = g a) : f = g := fun_like.ext f g h /-- Copy of a `Sup_hom` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (f : Sup_hom α β) (f' : α → β) (h : f' = f) : Sup_hom α β := { to_fun := f', map_Sup' := h.symm ▸ f.map_Sup' } @[simp] lemma coe_copy (f : Sup_hom α β) (f' : α → β) (h : f' = f) : ⇑(f.copy f' h) = f' := rfl lemma copy_eq (f : Sup_hom α β) (f' : α → β) (h : f' = f) : f.copy f' h = f := fun_like.ext' h variables (α) /-- `id` as a `Sup_hom`. -/ protected def id : Sup_hom α α := ⟨id, λ s, by rw [id, set.image_id]⟩ instance : inhabited (Sup_hom α α) := ⟨Sup_hom.id α⟩ @[simp] lemma coe_id : ⇑(Sup_hom.id α) = id := rfl variables {α} @[simp] lemma id_apply (a : α) : Sup_hom.id α a = a := rfl /-- Composition of `Sup_hom`s as a `Sup_hom`. -/ def comp (f : Sup_hom β γ) (g : Sup_hom α β) : Sup_hom α γ := { to_fun := f ∘ g, map_Sup' := λ s, by rw [comp_apply, map_Sup, map_Sup, set.image_image] } @[simp] lemma coe_comp (f : Sup_hom β γ) (g : Sup_hom α β) : ⇑(f.comp g) = f ∘ g := rfl @[simp] lemma comp_apply (f : Sup_hom β γ) (g : Sup_hom α β) (a : α) : (f.comp g) a = f (g a) := rfl @[simp] lemma comp_assoc (f : Sup_hom γ δ) (g : Sup_hom β γ) (h : Sup_hom α β) : (f.comp g).comp h = f.comp (g.comp h) := rfl @[simp] lemma comp_id (f : Sup_hom α β) : f.comp (Sup_hom.id α) = f := ext $ λ a, rfl @[simp] lemma id_comp (f : Sup_hom α β) : (Sup_hom.id β).comp f = f := ext $ λ a, rfl lemma cancel_right {g₁ g₂ : Sup_hom β γ} {f : Sup_hom α β} (hf : surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨λ h, ext $ hf.forall.2 $ fun_like.ext_iff.1 h, congr_arg _⟩ lemma cancel_left {g : Sup_hom β γ} {f₁ f₂ : Sup_hom α β} (hg : injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨λ h, ext $ λ a, hg $ by rw [←comp_apply, h, comp_apply], congr_arg _⟩ end has_Sup variables [complete_lattice β] instance : partial_order (Sup_hom α β) := partial_order.lift _ fun_like.coe_injective instance : has_bot (Sup_hom α β) := ⟨⟨λ _, ⊥, λ s, begin obtain rfl | hs := s.eq_empty_or_nonempty, { rw [set.image_empty, Sup_empty] }, { rw [hs.image_const, Sup_singleton] } end⟩⟩ instance : order_bot (Sup_hom α β) := ⟨⊥, λ f a, bot_le⟩ @[simp] lemma coe_bot : ⇑(⊥ : Sup_hom α β) = ⊥ := rfl @[simp] lemma bot_apply (a : α) : (⊥ : Sup_hom α β) a = ⊥ := rfl end Sup_hom /-! ### Infimum homomorphisms -/ namespace Inf_hom variables [has_Inf α] section has_Inf variables [has_Inf β] [has_Inf γ] [has_Inf δ] instance : Inf_hom_class (Inf_hom α β) α β := { coe := Inf_hom.to_fun, coe_injective' := λ f g h, by cases f; cases g; congr', map_Inf := Inf_hom.map_Inf' } /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ instance : has_coe_to_fun (Inf_hom α β) (λ _, α → β) := fun_like.has_coe_to_fun @[simp] lemma to_fun_eq_coe {f : Inf_hom α β} : f.to_fun = (f : α → β) := rfl @[ext] lemma ext {f g : Inf_hom α β} (h : ∀ a, f a = g a) : f = g := fun_like.ext f g h /-- Copy of a `Inf_hom` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (f : Inf_hom α β) (f' : α → β) (h : f' = f) : Inf_hom α β := { to_fun := f', map_Inf' := h.symm ▸ f.map_Inf' } @[simp] lemma coe_copy (f : Inf_hom α β) (f' : α → β) (h : f' = f) : ⇑(f.copy f' h) = f' := rfl lemma copy_eq (f : Inf_hom α β) (f' : α → β) (h : f' = f) : f.copy f' h = f := fun_like.ext' h variables (α) /-- `id` as an `Inf_hom`. -/ protected def id : Inf_hom α α := ⟨id, λ s, by rw [id, set.image_id]⟩ instance : inhabited (Inf_hom α α) := ⟨Inf_hom.id α⟩ @[simp] lemma coe_id : ⇑(Inf_hom.id α) = id := rfl variables {α} @[simp] lemma id_apply (a : α) : Inf_hom.id α a = a := rfl /-- Composition of `Inf_hom`s as a `Inf_hom`. -/ def comp (f : Inf_hom β γ) (g : Inf_hom α β) : Inf_hom α γ := { to_fun := f ∘ g, map_Inf' := λ s, by rw [comp_apply, map_Inf, map_Inf, set.image_image] } @[simp] lemma coe_comp (f : Inf_hom β γ) (g : Inf_hom α β) : ⇑(f.comp g) = f ∘ g := rfl @[simp] lemma comp_apply (f : Inf_hom β γ) (g : Inf_hom α β) (a : α) : (f.comp g) a = f (g a) := rfl @[simp] lemma comp_assoc (f : Inf_hom γ δ) (g : Inf_hom β γ) (h : Inf_hom α β) : (f.comp g).comp h = f.comp (g.comp h) := rfl @[simp] lemma comp_id (f : Inf_hom α β) : f.comp (Inf_hom.id α) = f := ext $ λ a, rfl @[simp] lemma id_comp (f : Inf_hom α β) : (Inf_hom.id β).comp f = f := ext $ λ a, rfl lemma cancel_right {g₁ g₂ : Inf_hom β γ} {f : Inf_hom α β} (hf : surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨λ h, ext $ hf.forall.2 $ fun_like.ext_iff.1 h, congr_arg _⟩ lemma cancel_left {g : Inf_hom β γ} {f₁ f₂ : Inf_hom α β} (hg : injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨λ h, ext $ λ a, hg $ by rw [←comp_apply, h, comp_apply], congr_arg _⟩ end has_Inf variables [complete_lattice β] instance : partial_order (Inf_hom α β) := partial_order.lift _ fun_like.coe_injective instance : has_top (Inf_hom α β) := ⟨⟨λ _, ⊤, λ s, begin obtain rfl | hs := s.eq_empty_or_nonempty, { rw [set.image_empty, Inf_empty] }, { rw [hs.image_const, Inf_singleton] } end⟩⟩ instance : order_top (Inf_hom α β) := ⟨⊤, λ f a, le_top⟩ @[simp] lemma coe_top : ⇑(⊤ : Inf_hom α β) = ⊤ := rfl @[simp] lemma top_apply (a : α) : (⊤ : Inf_hom α β) a = ⊤ := rfl end Inf_hom /-! ### Frame homomorphisms -/ namespace frame_hom variables [complete_lattice α] [complete_lattice β] [complete_lattice γ] [complete_lattice δ] instance : frame_hom_class (frame_hom α β) α β := { coe := λ f, f.to_fun, coe_injective' := λ f g h, by { obtain ⟨⟨⟨_, _⟩, _⟩, _⟩ := f, obtain ⟨⟨⟨_, _⟩, _⟩, _⟩ := g, congr' }, map_Sup := λ f, f.map_Sup', map_inf := λ f, f.map_inf', map_top := λ f, f.map_top' } /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ instance : has_coe_to_fun (frame_hom α β) (λ _, α → β) := fun_like.has_coe_to_fun /-- Reinterpret a `frame_hom` as a `lattice_hom`. -/ def to_lattice_hom (f : frame_hom α β) : lattice_hom α β := f @[simp] lemma to_fun_eq_coe {f : frame_hom α β} : f.to_fun = (f : α → β) := rfl @[ext] lemma ext {f g : frame_hom α β} (h : ∀ a, f a = g a) : f = g := fun_like.ext f g h /-- Copy of a `frame_hom` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (f : frame_hom α β) (f' : α → β) (h : f' = f) : frame_hom α β := { to_inf_top_hom := f.to_inf_top_hom.copy f' h, ..(f : Sup_hom α β).copy f' h } @[simp] lemma coe_copy (f : frame_hom α β) (f' : α → β) (h : f' = f) : ⇑(f.copy f' h) = f' := rfl lemma copy_eq (f : frame_hom α β) (f' : α → β) (h : f' = f) : f.copy f' h = f := fun_like.ext' h variables (α) /-- `id` as a `frame_hom`. -/ protected def id : frame_hom α α := { to_inf_top_hom := inf_top_hom.id α, ..Sup_hom.id α } instance : inhabited (frame_hom α α) := ⟨frame_hom.id α⟩ @[simp] lemma coe_id : ⇑(frame_hom.id α) = id := rfl variables {α} @[simp] lemma id_apply (a : α) : frame_hom.id α a = a := rfl /-- Composition of `frame_hom`s as a `frame_hom`. -/ def comp (f : frame_hom β γ) (g : frame_hom α β) : frame_hom α γ := { to_inf_top_hom := f.to_inf_top_hom.comp g.to_inf_top_hom, ..(f : Sup_hom β γ).comp (g : Sup_hom α β) } @[simp] lemma coe_comp (f : frame_hom β γ) (g : frame_hom α β) : ⇑(f.comp g) = f ∘ g := rfl @[simp] lemma comp_apply (f : frame_hom β γ) (g : frame_hom α β) (a : α) : (f.comp g) a = f (g a) := rfl @[simp] lemma comp_assoc (f : frame_hom γ δ) (g : frame_hom β γ) (h : frame_hom α β) : (f.comp g).comp h = f.comp (g.comp h) := rfl @[simp] lemma comp_id (f : frame_hom α β) : f.comp (frame_hom.id α) = f := ext $ λ a, rfl @[simp] lemma id_comp (f : frame_hom α β) : (frame_hom.id β).comp f = f := ext $ λ a, rfl lemma cancel_right {g₁ g₂ : frame_hom β γ} {f : frame_hom α β} (hf : surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨λ h, ext $ hf.forall.2 $ fun_like.ext_iff.1 h, congr_arg _⟩ lemma cancel_left {g : frame_hom β γ} {f₁ f₂ : frame_hom α β} (hg : injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨λ h, ext $ λ a, hg $ by rw [←comp_apply, h, comp_apply], congr_arg _⟩ instance : partial_order (frame_hom α β) := partial_order.lift _ fun_like.coe_injective end frame_hom /-! ### Complete lattice homomorphisms -/ namespace complete_lattice_hom variables [complete_lattice α] [complete_lattice β] [complete_lattice γ] [complete_lattice δ] instance : complete_lattice_hom_class (complete_lattice_hom α β) α β := { coe := λ f, f.to_fun, coe_injective' := λ f g h, by obtain ⟨⟨_, _⟩, _⟩ := f; obtain ⟨⟨_, _⟩, _⟩ := g; congr', map_Sup := λ f, f.map_Sup', map_Inf := λ f, f.map_Inf' } /-- Reinterpret a `complete_lattice_hom` as a `Sup_hom`. -/ def to_Sup_hom (f : complete_lattice_hom α β) : Sup_hom α β := f /-- Reinterpret a `complete_lattice_hom` as a `bounded_lattice_hom`. -/ def to_bounded_lattice_hom (f : complete_lattice_hom α β) : bounded_lattice_hom α β := f /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ instance : has_coe_to_fun (complete_lattice_hom α β) (λ _, α → β) := fun_like.has_coe_to_fun @[simp] lemma to_fun_eq_coe {f : complete_lattice_hom α β} : f.to_fun = (f : α → β) := rfl @[ext] lemma ext {f g : complete_lattice_hom α β} (h : ∀ a, f a = g a) : f = g := fun_like.ext f g h /-- Copy of a `complete_lattice_hom` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (f : complete_lattice_hom α β) (f' : α → β) (h : f' = f) : complete_lattice_hom α β := { to_Inf_hom := f.to_Inf_hom.copy f' h, .. f.to_Sup_hom.copy f' h } @[simp] lemma coe_copy (f : complete_lattice_hom α β) (f' : α → β) (h : f' = f) : ⇑(f.copy f' h) = f' := rfl lemma copy_eq (f : complete_lattice_hom α β) (f' : α → β) (h : f' = f) : f.copy f' h = f := fun_like.ext' h variables (α) /-- `id` as a `complete_lattice_hom`. -/ protected def id : complete_lattice_hom α α := { to_fun := id, ..Sup_hom.id α, ..Inf_hom.id α } instance : inhabited (complete_lattice_hom α α) := ⟨complete_lattice_hom.id α⟩ @[simp] lemma coe_id : ⇑(complete_lattice_hom.id α) = id := rfl variables {α} @[simp] lemma id_apply (a : α) : complete_lattice_hom.id α a = a := rfl /-- Composition of `complete_lattice_hom`s as a `complete_lattice_hom`. -/ def comp (f : complete_lattice_hom β γ) (g : complete_lattice_hom α β) : complete_lattice_hom α γ := { to_Inf_hom := f.to_Inf_hom.comp g.to_Inf_hom, ..f.to_Sup_hom.comp g.to_Sup_hom } @[simp] lemma coe_comp (f : complete_lattice_hom β γ) (g : complete_lattice_hom α β) : ⇑(f.comp g) = f ∘ g := rfl @[simp] lemma comp_apply (f : complete_lattice_hom β γ) (g : complete_lattice_hom α β) (a : α) : (f.comp g) a = f (g a) := rfl @[simp] lemma comp_assoc (f : complete_lattice_hom γ δ) (g : complete_lattice_hom β γ) (h : complete_lattice_hom α β) : (f.comp g).comp h = f.comp (g.comp h) := rfl @[simp] lemma comp_id (f : complete_lattice_hom α β) : f.comp (complete_lattice_hom.id α) = f := ext $ λ a, rfl @[simp] lemma id_comp (f : complete_lattice_hom α β) : (complete_lattice_hom.id β).comp f = f := ext $ λ a, rfl lemma cancel_right {g₁ g₂ : complete_lattice_hom β γ} {f : complete_lattice_hom α β} (hf : surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨λ h, ext $ hf.forall.2 $ fun_like.ext_iff.1 h, congr_arg _⟩ lemma cancel_left {g : complete_lattice_hom β γ} {f₁ f₂ : complete_lattice_hom α β} (hg : injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨λ h, ext $ λ a, hg $ by rw [←comp_apply, h, comp_apply], congr_arg _⟩ end complete_lattice_hom /-! ### Dual homs -/ namespace Sup_hom variables [has_Sup α] [has_Sup β] [has_Sup γ] /-- Reinterpret a `⨆`-homomorphism as an `⨅`-homomorphism between the dual orders. -/ @[simps] protected def dual : Sup_hom α β ≃ Inf_hom αᵒᵈ βᵒᵈ := { to_fun := λ f, ⟨to_dual ∘ f ∘ of_dual, f.map_Sup'⟩, inv_fun := λ f, ⟨of_dual ∘ f ∘ to_dual, f.map_Inf'⟩, left_inv := λ f, Sup_hom.ext $ λ a, rfl, right_inv := λ f, Inf_hom.ext $ λ a, rfl } @[simp] lemma dual_id : (Sup_hom.id α).dual = Inf_hom.id _ := rfl @[simp] lemma dual_comp (g : Sup_hom β γ) (f : Sup_hom α β) : (g.comp f).dual = g.dual.comp f.dual := rfl @[simp] lemma symm_dual_id : Sup_hom.dual.symm (Inf_hom.id _) = Sup_hom.id α := rfl @[simp] lemma symm_dual_comp (g : Inf_hom βᵒᵈ γᵒᵈ) (f : Inf_hom αᵒᵈ βᵒᵈ) : Sup_hom.dual.symm (g.comp f) = (Sup_hom.dual.symm g).comp (Sup_hom.dual.symm f) := rfl end Sup_hom namespace Inf_hom variables [has_Inf α] [has_Inf β] [has_Inf γ] /-- Reinterpret an `⨅`-homomorphism as a `⨆`-homomorphism between the dual orders. -/ @[simps] protected def dual : Inf_hom α β ≃ Sup_hom αᵒᵈ βᵒᵈ := { to_fun := λ f, { to_fun := to_dual ∘ f ∘ of_dual, map_Sup' := λ _, congr_arg to_dual (map_Inf f _) }, inv_fun := λ f, { to_fun := of_dual ∘ f ∘ to_dual, map_Inf' := λ _, congr_arg of_dual (map_Sup f _) }, left_inv := λ f, Inf_hom.ext $ λ a, rfl, right_inv := λ f, Sup_hom.ext $ λ a, rfl } @[simp] lemma dual_id : (Inf_hom.id α).dual = Sup_hom.id _ := rfl @[simp] lemma dual_comp (g : Inf_hom β γ) (f : Inf_hom α β) : (g.comp f).dual = g.dual.comp f.dual := rfl @[simp] lemma symm_dual_id : Inf_hom.dual.symm (Sup_hom.id _) = Inf_hom.id α := rfl @[simp] lemma symm_dual_comp (g : Sup_hom βᵒᵈ γᵒᵈ) (f : Sup_hom αᵒᵈ βᵒᵈ) : Inf_hom.dual.symm (g.comp f) = (Inf_hom.dual.symm g).comp (Inf_hom.dual.symm f) := rfl end Inf_hom namespace complete_lattice_hom variables [complete_lattice α] [complete_lattice β] [complete_lattice γ] /-- Reinterpret a complete lattice homomorphism as a complete lattice homomorphism between the dual lattices. -/ @[simps] protected def dual : complete_lattice_hom α β ≃ complete_lattice_hom αᵒᵈ βᵒᵈ := { to_fun := λ f, ⟨f.to_Sup_hom.dual, f.map_Inf'⟩, inv_fun := λ f, ⟨f.to_Sup_hom.dual, f.map_Inf'⟩, left_inv := λ f, ext $ λ a, rfl, right_inv := λ f, ext $ λ a, rfl } @[simp] lemma dual_id : (complete_lattice_hom.id α).dual = complete_lattice_hom.id _ := rfl @[simp] lemma dual_comp (g : complete_lattice_hom β γ) (f : complete_lattice_hom α β) : (g.comp f).dual = g.dual.comp f.dual := rfl @[simp] lemma symm_dual_id : complete_lattice_hom.dual.symm (complete_lattice_hom.id _) = complete_lattice_hom.id α := rfl @[simp] lemma symm_dual_comp (g : complete_lattice_hom βᵒᵈ γᵒᵈ) (f : complete_lattice_hom αᵒᵈ βᵒᵈ) : complete_lattice_hom.dual.symm (g.comp f) = (complete_lattice_hom.dual.symm g).comp (complete_lattice_hom.dual.symm f) := rfl end complete_lattice_hom /-! ### Concrete homs -/ namespace complete_lattice_hom /-- `set.preimage` as a complete lattice homomorphism. See also `Sup_hom.set_image`. -/ def set_preimage (f : α → β) : complete_lattice_hom (set β) (set α) := { to_fun := preimage f, map_Sup' := λ s, preimage_sUnion.trans $ by simp only [set.Sup_eq_sUnion, set.sUnion_image], map_Inf' := λ s, preimage_sInter.trans $ by simp only [set.Inf_eq_sInter, set.sInter_image] } @[simp] lemma coe_set_preimage (f : α → β) : ⇑(set_preimage f) = preimage f := rfl @[simp] lemma set_preimage_apply (f : α → β) (s : set β) : set_preimage f s = s.preimage f := rfl @[simp] lemma set_preimage_id : set_preimage (id : α → α) = complete_lattice_hom.id _ := rfl -- This lemma can't be `simp` because `g ∘ f` matches anything (`id ∘ f = f` synctatically) lemma set_preimage_comp (g : β → γ) (f : α → β) : set_preimage (g ∘ f) = (set_preimage f).comp (set_preimage g) := rfl end complete_lattice_hom lemma set.image_Sup {f : α → β} (s : set (set α)) : f '' Sup s = Sup (image f '' s) := begin ext b, simp only [Sup_eq_sUnion, mem_image, mem_sUnion, exists_prop, sUnion_image, mem_Union], split, { rintros ⟨a, ⟨t, ht₁, ht₂⟩, rfl⟩, exact ⟨t, ht₁, a, ht₂, rfl⟩, }, { rintros ⟨t, ht₁, a, ht₂, rfl⟩, exact ⟨a, ⟨t, ht₁, ht₂⟩, rfl⟩, }, end /-- Using `set.image`, a function between types yields a `Sup_hom` between their lattices of subsets. See also `complete_lattice_hom.set_preimage`. -/ @[simps] def Sup_hom.set_image (f : α → β) : Sup_hom (set α) (set β) := { to_fun := image f, map_Sup' := set.image_Sup } /-- An equivalence of types yields an order isomorphism between their lattices of subsets. -/ @[simps] def equiv.to_order_iso_set (e : α ≃ β) : set α ≃o set β := { to_fun := image e, inv_fun := image e.symm, left_inv := λ s, by simp only [← image_comp, equiv.symm_comp_self, id.def, image_id'], right_inv := λ s, by simp only [← image_comp, equiv.self_comp_symm, id.def, image_id'], map_rel_iff' := λ s t, ⟨λ h, by simpa using @monotone_image _ _ e.symm _ _ h, λ h, monotone_image h⟩ } section variables (α) [complete_lattice α] /-- The map `(a, b) ↦ a ⊓ b` as an `Inf_hom`. -/ @[simps] def inf_Inf_hom : Inf_hom (α × α) α := { to_fun := λ x, x.1 ⊓ x.2, map_Inf' := λ s, by simp_rw [prod.fst_Inf, prod.snd_Inf, Inf_image, infi_inf_eq], } /-- The map `(a, b) ↦ a ⊔ b` as a `Sup_hom`. -/ @[simps] def sup_Sup_hom : Sup_hom (α × α) α := { to_fun := λ x, x.1 ⊔ x.2, map_Sup' := (inf_Inf_hom αᵒᵈ).map_Inf' } end
6cd1eed1d7bd3a917bdb97b0c3883236abfb4ace
a4673261e60b025e2c8c825dfa4ab9108246c32e
/stage0/src/Init/Data/Array/Subarray.lean
f1af76e1d47c06a3463064a7aa510783c81afef1
[ "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
4,018
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.Data.Array.Basic universes u v w structure Subarray (α : Type u) := (as : Array α) (start : Nat) (stop : Nat) (h₁ : start ≤ stop) (h₂ : stop ≤ as.size) namespace Subarray @[inline] unsafe def forInUnsafe {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (s : Subarray α) (b : β) (f : α → β → m (ForInStep β)) : m β := let sz := USize.ofNat s.stop let rec @[specialize] loop (i : USize) (b : β) : m β := do if i < sz then let a := s.as.uget i lcProof match (← f a b) with | ForInStep.done b => pure b | ForInStep.yield b => loop (i+1) b else pure b loop (USize.ofNat s.start) b -- TODO: provide reference implementation @[implementedBy Subarray.forInUnsafe] constant forIn {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (s : Subarray α) (b : β) (f : α → β → m (ForInStep β)) : m β := pure b @[inline] def foldlM {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : β → α → m β) (init : β) (as : Subarray α) : m β := as.as.foldlM f (init := init) (start := as.start) (stop := as.stop) @[inline] def foldrM {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : α → β → m β) (init : β) (as : Subarray α) : m β := as.as.foldrM f (init := init) (start := as.stop) (stop := as.start) @[inline] def anyM {α : Type u} {m : Type → Type w} [Monad m] (p : α → m Bool) (as : Subarray α) : m Bool := as.as.anyM p (start := as.start) (stop := as.stop) @[inline] def allM {α : Type u} {m : Type → Type w} [Monad m] (p : α → m Bool) (as : Subarray α) : m Bool := as.as.allM p (start := as.start) (stop := as.stop) @[inline] def forM {α : Type u} {m : Type v → Type w} [Monad m] (f : α → m PUnit) (as : Subarray α) : m PUnit := as.as.forM f (start := as.start) (stop := as.stop) @[inline] def forRevM {α : Type u} {m : Type v → Type w} [Monad m] (f : α → m PUnit) (as : Subarray α) : m PUnit := as.as.forRevM f (start := as.stop) (stop := as.start) @[inline] def foldl {α : Type u} {β : Type v} (f : β → α → β) (init : β) (as : Subarray α) : β := Id.run <| as.foldlM f (init := init) @[inline] def foldr {α : Type u} {β : Type v} (f : α → β → β) (init : β) (as : Subarray α) : β := Id.run <| as.foldrM f (init := init) @[inline] def any {α : Type u} (p : α → Bool) (as : Subarray α) : Bool := Id.run <| as.anyM p @[inline] def all {α : Type u} (p : α → Bool) (as : Subarray α) : Bool := Id.run <| as.allM p end Subarray namespace Array variables {α : Type u} def toSubarray (as : Array α) (start stop : Nat) : Subarray α := if h₂ : stop ≤ as.size then if h₁ : start ≤ stop then { as := as, start := start, stop := stop, h₁ := h₁, h₂ := h₂ } else { as := as, start := stop, stop := stop, h₁ := Nat.leRefl _, h₂ := h₂ } else if h₁ : start ≤ as.size then { as := as, start := start, stop := as.size, h₁ := h₁, h₂ := Nat.leRefl _ } else { as := as, start := as.size, stop := as.size, h₁ := Nat.leRefl _, h₂ := Nat.leRefl _ } def ofSubarray (s : Subarray α) : Array α := do let mut as := mkEmpty (s.stop - s.start) for a in s do as := as.push a return as def extract (as : Array α) (start stop : Nat) : Array α := ofSubarray (as.toSubarray start stop) instance : Coe (Subarray α) (Array α) := ⟨ofSubarray⟩ syntax:max term noWs "[" term ":" term "]" : term syntax:max term noWs "[" term ":" "]" : term syntax:max term noWs "[" ":" term "]" : term macro_rules | `($a[$start : $stop]) => `(Array.toSubarray $a $start $stop) | `($a[ : $stop]) => `(Array.toSubarray $a 0 $stop) | `($a[$start : ]) => `(let a := $a; Array.toSubarray a $start a.size) end Array
e64d243017faa25eda1e7d6dc4488a5ac9e6558d
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/category_theory/sites/compatible_plus.lean
dc0f90947a65848ccb9fdcdd62dd7d04eb72cc38
[ "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
8,728
lean
/- Copyright (c) 2021 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Adam Topaz -/ import category_theory.sites.whiskering import category_theory.sites.plus /-! In this file, we prove that the plus functor is compatible with functors which preserve the correct limits and colimits. See `category_theory/sites/compatible_sheafification` for the compatibility of sheafification, which follows easily from the content in this file. -/ namespace category_theory.grothendieck_topology open category_theory open category_theory.limits open opposite universes w₁ w₂ v u variables {C : Type u} [category.{v} C] (J : grothendieck_topology C) variables {D : Type w₁} [category.{max v u} D] variables {E : Type w₂} [category.{max v u} E] variables (F : D ⥤ E) noncomputable theory variables [∀ (α β : Type (max v u)) (fst snd : β → α), has_limits_of_shape (walking_multicospan fst snd) D] variables [∀ (α β : Type (max v u)) (fst snd : β → α), has_limits_of_shape (walking_multicospan fst snd) E] variables [∀ (X : C) (W : J.cover X) (P : Cᵒᵖ ⥤ D), preserves_limit (W.index P).multicospan F] variables (P : Cᵒᵖ ⥤ D) /-- The diagram used to define `P⁺`, composed with `F`, is isomorphic to the diagram used to define `P ⋙ F`. -/ def diagram_comp_iso (X : C) : J.diagram P X ⋙ F ≅ J.diagram (P ⋙ F) X := nat_iso.of_components (λ W, begin refine _ ≪≫ has_limit.iso_of_nat_iso (W.unop.multicospan_comp _ _).symm, refine (is_limit_of_preserves F (limit.is_limit _)).cone_point_unique_up_to_iso (limit.is_limit _) end) begin intros A B f, ext, dsimp, simp only [functor.map_cone_π_app, multiequalizer.multifork_π_app_left, iso.symm_hom, multiequalizer.lift_ι, eq_to_hom_refl, category.comp_id, limit.cone_point_unique_up_to_iso_hom_comp, grothendieck_topology.cover.multicospan_comp_hom_inv_left, has_limit.iso_of_nat_iso_hom_π, category.assoc], simp only [← F.map_comp, multiequalizer.lift_ι], end @[simp, reassoc] lemma diagram_comp_iso_hom_ι (X : C) (W : (J.cover X)ᵒᵖ) (i : W.unop.arrow): (J.diagram_comp_iso F P X).hom.app W ≫ multiequalizer.ι _ i = F.map (multiequalizer.ι _ _) := begin delta diagram_comp_iso, dsimp, simp, end variables [∀ (X : C), has_colimits_of_shape (J.cover X)ᵒᵖ D] variables [∀ (X : C), has_colimits_of_shape (J.cover X)ᵒᵖ E] variables [∀ (X : C), preserves_colimits_of_shape (J.cover X)ᵒᵖ F] /-- The isomorphism between `P⁺ ⋙ F` and `(P ⋙ F)⁺`. -/ def plus_comp_iso : J.plus_obj P ⋙ F ≅ J.plus_obj (P ⋙ F) := nat_iso.of_components (λ X, begin refine _ ≪≫ has_colimit.iso_of_nat_iso (J.diagram_comp_iso F P X.unop), refine (is_colimit_of_preserves F (colimit.is_colimit (J.diagram P (unop X)))).cocone_point_unique_up_to_iso (colimit.is_colimit _) end) begin intros X Y f, apply (is_colimit_of_preserves F (colimit.is_colimit (J.diagram P X.unop))).hom_ext, intros W, dsimp [plus_obj, plus_map], simp only [functor.map_comp, category.assoc], slice_rhs 1 2 { erw (is_colimit_of_preserves F (colimit.is_colimit (J.diagram P X.unop))).fac }, slice_lhs 1 3 { simp only [← F.map_comp], dsimp [colim_map, is_colimit.map, colimit.pre], simp only [colimit.ι_desc_assoc, colimit.ι_desc], dsimp [cocones.precompose], rw [category.assoc, colimit.ι_desc], dsimp [cocone.whisker], rw F.map_comp }, simp only [category.assoc], slice_lhs 2 3 { erw (is_colimit_of_preserves F (colimit.is_colimit (J.diagram P Y.unop))).fac }, dsimp, simp only [has_colimit.iso_of_nat_iso_ι_hom_assoc, grothendieck_topology.diagram_pullback_app, colimit.ι_pre, has_colimit.iso_of_nat_iso_ι_hom, ι_colim_map_assoc], simp only [← category.assoc], congr' 1, ext, dsimp, simp only [category.assoc], erw [multiequalizer.lift_ι, diagram_comp_iso_hom_ι, diagram_comp_iso_hom_ι, ← F.map_comp, multiequalizer.lift_ι], end @[simp, reassoc] lemma ι_plus_comp_iso_hom (X) (W) : F.map (colimit.ι _ W) ≫ (J.plus_comp_iso F P).hom.app X = (J.diagram_comp_iso F P X.unop).hom.app W ≫ colimit.ι _ W := begin delta diagram_comp_iso plus_comp_iso, simp only [is_colimit.desc_cocone_morphism_hom, is_colimit.unique_up_to_iso_hom, cocones.forget_map, iso.trans_hom, nat_iso.of_components_hom_app, functor.map_iso_hom, ← category.assoc], erw (is_colimit_of_preserves F (colimit.is_colimit (J.diagram P (unop X)))).fac, simp only [category.assoc, has_limit.iso_of_nat_iso_hom_π, iso.symm_hom, cover.multicospan_comp_hom_inv_left, eq_to_hom_refl, category.comp_id, limit.cone_point_unique_up_to_iso_hom_comp, functor.map_cone_π_app, multiequalizer.multifork_π_app_left, multiequalizer.lift_ι, functor.map_comp, eq_self_iff_true, category.assoc, iso.trans_hom, iso.cancel_iso_hom_left, nat_iso.of_components_hom_app, colimit.cocone_ι, category.assoc, has_colimit.iso_of_nat_iso_ι_hom], end @[simp, reassoc] lemma plus_comp_iso_whisker_left {F G : D ⥤ E} (η : F ⟶ G) (P : Cᵒᵖ ⥤ D) [∀ (X : C), preserves_colimits_of_shape (J.cover X)ᵒᵖ F] [∀ (X : C) (W : J.cover X) (P : Cᵒᵖ ⥤ D), preserves_limit (W.index P).multicospan F] [∀ (X : C), preserves_colimits_of_shape (J.cover X)ᵒᵖ G] [∀ (X : C) (W : J.cover X) (P : Cᵒᵖ ⥤ D), preserves_limit (W.index P).multicospan G] : whisker_left _ η ≫ (J.plus_comp_iso G P).hom = (J.plus_comp_iso F P).hom ≫ J.plus_map (whisker_left _ η) := begin ext X, apply (is_colimit_of_preserves F (colimit.is_colimit (J.diagram P X.unop))).hom_ext, intros W, dsimp [plus_obj, plus_map], simp only [ι_plus_comp_iso_hom, ι_colim_map, whisker_left_app, ι_plus_comp_iso_hom_assoc, nat_trans.naturality_assoc, grothendieck_topology.diagram_nat_trans_app], simp only [← category.assoc], congr' 1, ext, dsimp, simpa, end /-- The isomorphism between `P⁺ ⋙ F` and `(P ⋙ F)⁺`, functorially in `F`. -/ @[simps hom_app inv_app] def plus_functor_whisker_left_iso (P : Cᵒᵖ ⥤ D) [∀ (F : D ⥤ E) (X : C), preserves_colimits_of_shape (J.cover X)ᵒᵖ F] [∀ (F : D ⥤ E) (X : C) (W : J.cover X) (P : Cᵒᵖ ⥤ D), preserves_limit (W.index P).multicospan F] : (whiskering_left _ _ E).obj (J.plus_obj P) ≅ (whiskering_left _ _ _).obj P ⋙ J.plus_functor E := nat_iso.of_components (λ X, plus_comp_iso _ _ _) $ λ F G η, plus_comp_iso_whisker_left _ _ _ @[simp, reassoc] lemma plus_comp_iso_whisker_right {P Q : Cᵒᵖ ⥤ D} (η : P ⟶ Q) : whisker_right (J.plus_map η) F ≫ (J.plus_comp_iso F Q).hom = (J.plus_comp_iso F P).hom ≫ J.plus_map (whisker_right η F) := begin ext X, apply (is_colimit_of_preserves F (colimit.is_colimit (J.diagram P X.unop))).hom_ext, intros W, dsimp [plus_obj, plus_map], simp only [ι_colim_map, whisker_right_app, ι_plus_comp_iso_hom_assoc, grothendieck_topology.diagram_nat_trans_app], simp only [← category.assoc, ← F.map_comp], dsimp [colim_map, is_colimit.map], simp only [colimit.ι_desc], dsimp [cocones.precompose], simp only [functor.map_comp, category.assoc, ι_plus_comp_iso_hom], simp only [← category.assoc], congr' 1, ext, dsimp, simp only [diagram_comp_iso_hom_ι_assoc, multiequalizer.lift_ι, diagram_comp_iso_hom_ι, category.assoc], simp only [← F.map_comp, multiequalizer.lift_ι], end /-- The isomorphism between `P⁺ ⋙ F` and `(P ⋙ F)⁺`, functorially in `P`. -/ @[simps hom_app inv_app] def plus_functor_whisker_right_iso : J.plus_functor D ⋙ (whiskering_right _ _ _).obj F ≅ (whiskering_right _ _ _).obj F ⋙ J.plus_functor E := nat_iso.of_components (λ P, J.plus_comp_iso _ _) $ λ P Q η, plus_comp_iso_whisker_right _ _ _ @[simp, reassoc] lemma whisker_right_to_plus_comp_plus_comp_iso_hom : whisker_right (J.to_plus _) _ ≫ (J.plus_comp_iso F P).hom = J.to_plus _ := begin ext, dsimp [to_plus], simp only [ι_plus_comp_iso_hom, functor.map_comp, category.assoc], simp only [← category.assoc], congr' 1, ext, delta cover.to_multiequalizer, simp only [diagram_comp_iso_hom_ι, category.assoc, ← F.map_comp], erw [multiequalizer.lift_ι, multiequalizer.lift_ι], refl, end @[simp] lemma to_plus_comp_plus_comp_iso_inv : J.to_plus _ ≫ (J.plus_comp_iso F P).inv = whisker_right (J.to_plus _) _ := by simp [iso.comp_inv_eq] lemma plus_comp_iso_inv_eq_plus_lift (hP : presheaf.is_sheaf J ((J.plus_obj P) ⋙ F)) : (J.plus_comp_iso F P).inv = J.plus_lift (whisker_right (J.to_plus _) _) hP := by { apply J.plus_lift_unique, simp [iso.comp_inv_eq] } end category_theory.grothendieck_topology
8d143854e8eb856e43cb55fcb3222f85936e4f37
36c7a18fd72e5b57229bd8ba36493daf536a19ce
/library/data/int/power.lean
f461849c0dc151e1f95141b8301c0f544da1bdeb
[ "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
787
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad The power function on the integers. -/ import data.int.basic data.int.order data.int.div algebra.group_power data.nat.power namespace int open algebra definition int_has_pow_nat [reducible] [instance] [priority int.prio] : has_pow_nat int := has_pow_nat.mk has_pow_nat.pow_nat /- definition nmul (n : ℕ) (a : ℤ) : ℤ := algebra.nmul n a infix [priority int.prio] ⬝ := nmul definition imul (i : ℤ) (a : ℤ) : ℤ := algebra.imul i a -/ open nat theorem of_nat_pow (a n : ℕ) : of_nat (a^n) = (of_nat a)^n := begin induction n with n ih, apply eq.refl, rewrite [pow_succ, pow_succ, of_nat_mul, ih] end end int