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
4d0412959bde51cfb0ca64025ca956a8e441f103
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/list/nodup_auto.lean
1e48bda78ce156f38acf69dad2a0c14d8c448a47
[]
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,717
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 -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.list.pairwise import Mathlib.data.list.forall2 import Mathlib.PostPort universes u v u_1 namespace Mathlib /-! # Lists with no duplicates `list.nodup` is defined in `data/list/defs`. In this file we prove various properties of this predicate. -/ namespace list @[simp] theorem forall_mem_ne {α : Type u} {a : α} {l : List α} : (∀ (a' : α), a' ∈ l → ¬a = a') ↔ ¬a ∈ l := { mp := fun (h : ∀ (a' : α), a' ∈ l → ¬a = a') (m : a ∈ l) => h a m rfl, mpr := fun (h : ¬a ∈ l) (a' : α) (m : a' ∈ l) (e : a = a') => h (Eq.symm e ▸ m) } @[simp] theorem nodup_nil {α : Type u} : nodup [] := pairwise.nil @[simp] theorem nodup_cons {α : Type u} {a : α} {l : List α} : nodup (a :: l) ↔ ¬a ∈ l ∧ nodup l := sorry protected theorem pairwise.nodup {α : Type u} {l : List α} {r : α → α → Prop} [is_irrefl α r] (h : pairwise r l) : nodup l := pairwise.imp (fun (a b : α) => ne_of_irrefl) h theorem rel_nodup {α : Type u} {β : Type v} {r : α → β → Prop} (hr : relator.bi_unique r) : relator.lift_fun (forall₂ r) Iff nodup nodup := sorry theorem nodup_cons_of_nodup {α : Type u} {a : α} {l : List α} (m : ¬a ∈ l) (n : nodup l) : nodup (a :: l) := iff.mpr nodup_cons { left := m, right := n } theorem nodup_singleton {α : Type u} (a : α) : nodup [a] := nodup_cons_of_nodup (not_mem_nil a) nodup_nil theorem nodup_of_nodup_cons {α : Type u} {a : α} {l : List α} (h : nodup (a :: l)) : nodup l := and.right (iff.mp nodup_cons h) theorem not_mem_of_nodup_cons {α : Type u} {a : α} {l : List α} (h : nodup (a :: l)) : ¬a ∈ l := and.left (iff.mp nodup_cons h) theorem not_nodup_cons_of_mem {α : Type u} {a : α} {l : List α} : a ∈ l → ¬nodup (a :: l) := iff.mp imp_not_comm not_mem_of_nodup_cons theorem nodup_of_sublist {α : Type u} {l₁ : List α} {l₂ : List α} : l₁ <+ l₂ → nodup l₂ → nodup l₁ := pairwise_of_sublist theorem not_nodup_pair {α : Type u} (a : α) : ¬nodup [a, a] := not_nodup_cons_of_mem (mem_singleton_self a) theorem nodup_iff_sublist {α : Type u} {l : List α} : nodup l ↔ ∀ (a : α), ¬[a, a] <+ l := sorry theorem nodup_iff_nth_le_inj {α : Type u} {l : List α} : nodup l ↔ ∀ (i j : ℕ) (h₁ : i < length l) (h₂ : j < length l), nth_le l i h₁ = nth_le l j h₂ → i = j := sorry @[simp] theorem nth_le_index_of {α : Type u} [DecidableEq α] {l : List α} (H : nodup l) (n : ℕ) (h : n < length l) : index_of (nth_le l n h) l = n := iff.mp nodup_iff_nth_le_inj H (index_of (nth_le l n h) l) n (iff.mpr index_of_lt_length (nth_le_mem l n h)) h (index_of_nth_le (iff.mpr index_of_lt_length (nth_le_mem l n h))) theorem nodup_iff_count_le_one {α : Type u} [DecidableEq α] {l : List α} : nodup l ↔ ∀ (a : α), count a l ≤ 1 := sorry theorem nodup_repeat {α : Type u} (a : α) {n : ℕ} : nodup (repeat a n) ↔ n ≤ 1 := sorry @[simp] theorem count_eq_one_of_mem {α : Type u} [DecidableEq α] {a : α} {l : List α} (d : nodup l) (h : a ∈ l) : count a l = 1 := le_antisymm (iff.mp nodup_iff_count_le_one d a) (iff.mpr count_pos h) theorem nodup_of_nodup_append_left {α : Type u} {l₁ : List α} {l₂ : List α} : nodup (l₁ ++ l₂) → nodup l₁ := nodup_of_sublist (sublist_append_left l₁ l₂) theorem nodup_of_nodup_append_right {α : Type u} {l₁ : List α} {l₂ : List α} : nodup (l₁ ++ l₂) → nodup l₂ := nodup_of_sublist (sublist_append_right l₁ l₂) theorem nodup_append {α : Type u} {l₁ : List α} {l₂ : List α} : nodup (l₁ ++ l₂) ↔ nodup l₁ ∧ nodup l₂ ∧ disjoint l₁ l₂ := sorry theorem disjoint_of_nodup_append {α : Type u} {l₁ : List α} {l₂ : List α} (d : nodup (l₁ ++ l₂)) : disjoint l₁ l₂ := and.right (and.right (iff.mp nodup_append d)) theorem nodup_append_of_nodup {α : Type u} {l₁ : List α} {l₂ : List α} (d₁ : nodup l₁) (d₂ : nodup l₂) (dj : disjoint l₁ l₂) : nodup (l₁ ++ l₂) := iff.mpr nodup_append { left := d₁, right := { left := d₂, right := dj } } theorem nodup_append_comm {α : Type u} {l₁ : List α} {l₂ : List α} : nodup (l₁ ++ l₂) ↔ nodup (l₂ ++ l₁) := sorry theorem nodup_middle {α : Type u} {a : α} {l₁ : List α} {l₂ : List α} : nodup (l₁ ++ a :: l₂) ↔ nodup (a :: (l₁ ++ l₂)) := sorry theorem nodup_of_nodup_map {α : Type u} {β : Type v} (f : α → β) {l : List α} : nodup (map f l) → nodup l := pairwise_of_pairwise_map f fun (a b : α) => mt (congr_arg f) theorem nodup_map_on {α : Type u} {β : Type v} {f : α → β} {l : List α} (H : ∀ (x : α), x ∈ l → ∀ (y : α), y ∈ l → f x = f y → x = y) (d : nodup l) : nodup (map f l) := sorry theorem nodup_map {α : Type u} {β : Type v} {f : α → β} {l : List α} (hf : function.injective f) : nodup l → nodup (map f l) := nodup_map_on fun (x : α) (_x : x ∈ l) (y : α) (_x : y ∈ l) (h : f x = f y) => hf h theorem nodup_map_iff {α : Type u} {β : Type v} {f : α → β} {l : List α} (hf : function.injective f) : nodup (map f l) ↔ nodup l := { mp := nodup_of_nodup_map f, mpr := nodup_map hf } @[simp] theorem nodup_attach {α : Type u} {l : List α} : nodup (attach l) ↔ nodup l := sorry theorem nodup_pmap {α : Type u} {β : Type v} {p : α → Prop} {f : (a : α) → p a → β} {l : List α} {H : ∀ (a : α), a ∈ l → p a} (hf : ∀ (a : α) (ha : p a) (b : α) (hb : p b), f a ha = f b hb → a = b) (h : nodup l) : nodup (pmap f l H) := sorry theorem nodup_filter {α : Type u} (p : α → Prop) [decidable_pred p] {l : List α} : nodup l → nodup (filter p l) := pairwise_filter_of_pairwise p @[simp] theorem nodup_reverse {α : Type u} {l : List α} : nodup (reverse l) ↔ nodup l := sorry theorem nodup_erase_eq_filter {α : Type u} [DecidableEq α] (a : α) {l : List α} (d : nodup l) : list.erase l a = filter (fun (_x : α) => _x ≠ a) l := sorry theorem nodup_erase_of_nodup {α : Type u} [DecidableEq α] (a : α) {l : List α} : nodup l → nodup (list.erase l a) := nodup_of_sublist (erase_sublist a l) theorem nodup_diff {α : Type u} [DecidableEq α] {l₁ : List α} {l₂ : List α} (h : nodup l₁) : nodup (list.diff l₁ l₂) := sorry theorem mem_erase_iff_of_nodup {α : Type u} [DecidableEq α] {a : α} {b : α} {l : List α} (d : nodup l) : a ∈ list.erase l b ↔ a ≠ b ∧ a ∈ l := sorry theorem mem_erase_of_nodup {α : Type u} [DecidableEq α] {a : α} {l : List α} (h : nodup l) : ¬a ∈ list.erase l a := fun (H : a ∈ list.erase l a) => and.left (iff.mp (mem_erase_iff_of_nodup h) H) rfl theorem nodup_join {α : Type u} {L : List (List α)} : nodup (join L) ↔ (∀ (l : List α), l ∈ L → nodup l) ∧ pairwise disjoint L := sorry theorem nodup_bind {α : Type u} {β : Type v} {l₁ : List α} {f : α → List β} : nodup (list.bind l₁ f) ↔ (∀ (x : α), x ∈ l₁ → nodup (f x)) ∧ pairwise (fun (a b : α) => disjoint (f a) (f b)) l₁ := sorry theorem nodup_product {α : Type u} {β : Type v} {l₁ : List α} {l₂ : List β} (d₁ : nodup l₁) (d₂ : nodup l₂) : nodup (product l₁ l₂) := sorry theorem nodup_sigma {α : Type u} {σ : α → Type u_1} {l₁ : List α} {l₂ : (a : α) → List (σ a)} (d₁ : nodup l₁) (d₂ : ∀ (a : α), nodup (l₂ a)) : nodup (list.sigma l₁ l₂) := sorry theorem nodup_filter_map {α : Type u} {β : Type v} {f : α → Option β} {l : List α} (H : ∀ (a a' : α) (b : β), b ∈ f a → b ∈ f a' → a = a') : nodup l → nodup (filter_map f l) := pairwise_filter_map_of_pairwise f fun (a a' : α) (n : a ≠ a') (b : β) (bm : b ∈ f a) (b' : β) (bm' : b' ∈ f a') (e : b = b') => n (H a a' b' (e ▸ bm) bm') theorem nodup_concat {α : Type u} {a : α} {l : List α} (h : ¬a ∈ l) (h' : nodup l) : nodup (concat l a) := eq.mpr (id (Eq._oldrec (Eq.refl (nodup (concat l a))) (concat_eq_append a l))) (nodup_append_of_nodup h' (nodup_singleton a) (iff.mpr disjoint_singleton h)) theorem nodup_insert {α : Type u} [DecidableEq α] {a : α} {l : List α} (h : nodup l) : nodup (insert a l) := sorry theorem nodup_union {α : Type u} [DecidableEq α] (l₁ : List α) {l₂ : List α} (h : nodup l₂) : nodup (l₁ ∪ l₂) := sorry theorem nodup_inter_of_nodup {α : Type u} [DecidableEq α] {l₁ : List α} (l₂ : List α) : nodup l₁ → nodup (l₁ ∩ l₂) := nodup_filter fun (_x : α) => _x ∈ l₂ @[simp] theorem nodup_sublists {α : Type u} {l : List α} : nodup (sublists l) ↔ nodup l := sorry @[simp] theorem nodup_sublists' {α : Type u} {l : List α} : nodup (sublists' l) ↔ nodup l := sorry theorem nodup_sublists_len {α : Type u_1} (n : ℕ) {l : List α} (nd : nodup l) : nodup (sublists_len n l) := nodup_of_sublist (sublists_len_sublist_sublists' n l) (iff.mpr nodup_sublists' nd) theorem diff_eq_filter_of_nodup {α : Type u} [DecidableEq α] {l₁ : List α} {l₂ : List α} (hl₁ : nodup l₁) : list.diff l₁ l₂ = filter (fun (_x : α) => ¬_x ∈ l₂) l₁ := sorry theorem mem_diff_iff_of_nodup {α : Type u} [DecidableEq α] {l₁ : List α} {l₂ : List α} (hl₁ : nodup l₁) {a : α} : a ∈ list.diff l₁ l₂ ↔ a ∈ l₁ ∧ ¬a ∈ l₂ := eq.mpr (id (Eq._oldrec (Eq.refl (a ∈ list.diff l₁ l₂ ↔ a ∈ l₁ ∧ ¬a ∈ l₂)) (diff_eq_filter_of_nodup hl₁))) (eq.mpr (id (Eq._oldrec (Eq.refl (a ∈ filter (fun (_x : α) => ¬_x ∈ l₂) l₁ ↔ a ∈ l₁ ∧ ¬a ∈ l₂)) (propext mem_filter))) (iff.refl (a ∈ l₁ ∧ ¬a ∈ l₂))) theorem nodup_update_nth {α : Type u} {l : List α} {n : ℕ} {a : α} (hl : nodup l) (ha : ¬a ∈ l) : nodup (update_nth l n a) := sorry theorem nodup.map_update {α : Type u} {β : Type v} [DecidableEq α] {l : List α} (hl : nodup l) (f : α → β) (x : α) (y : β) : map (function.update f x y) l = ite (x ∈ l) (update_nth (map f l) (index_of x l) y) (map f l) := sorry end list theorem option.to_list_nodup {α : Type u_1} (o : Option α) : list.nodup (option.to_list o) := option.cases_on o (idRhs (list.nodup []) list.nodup_nil) fun (o : α) => idRhs (list.nodup [o]) (list.nodup_singleton o) end Mathlib
f3065bd239a9cf333f48f810f93d3290cb69f9c9
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/category_theory/monoidal/internal/types.lean
40e3dde9e6f17cc39679aaac6a1b145c9f73d487
[ "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
2,448
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import algebra.category.Mon.basic import category_theory.monoidal.internal import category_theory.monoidal.types /-! # `Mon_ (Type u) ≌ Mon.{u}` The category of internal monoid objects in `Type` is equivalent to the category of "native" bundled monoids. Moreover, this equivalence is compatible with the forgetful functors to `Type`. -/ universes v u open category_theory open category_theory.monoidal namespace Mon_Type_equivalence_Mon /-- Converting a monoid object in `Type` to a bundled monoid. -/ def functor : Mon_ (Type u) ⥤ Mon.{u} := { obj := λ A, ⟨A.X, { one := A.one punit.star, mul := λ x y, A.mul (x, y), one_mul := λ x, by convert congr_fun A.one_mul (punit.star, x), mul_one := λ x, by convert congr_fun A.mul_one (x, punit.star), mul_assoc := λ x y z, by convert congr_fun A.mul_assoc ((x, y), z), }⟩, map := λ A B f, { to_fun := f.hom, map_one' := congr_fun f.one_hom punit.star, map_mul' := λ x y, congr_fun f.mul_hom (x, y), }, } /-- Converting bundled monoid to a monoid object in `Type`. -/ def inverse : Mon.{u} ⥤ Mon_ (Type u) := { obj := λ A, { X := A, one := λ _, 1, mul := λ p, p.1 * p.2, mul_assoc' := by { ext ⟨⟨x, y⟩, z⟩, simp [mul_assoc], }, }, map := λ A B f, { hom := f, }, } end Mon_Type_equivalence_Mon open Mon_Type_equivalence_Mon /-- The category of internal monoid objects in `Type` is equivalent to the category of "native" bundled monoids. -/ def Mon_Type_equivalence_Mon : Mon_ (Type u) ≌ Mon.{u} := { functor := functor, inverse := inverse, unit_iso := nat_iso.of_components (λ A, { hom := { hom := 𝟙 _, }, inv := { hom := 𝟙 _, }, }) (by tidy), counit_iso := nat_iso.of_components (λ A, { hom := { to_fun := id, map_one' := rfl, map_mul' := λ x y, rfl, }, inv := { to_fun := id, map_one' := rfl, map_mul' := λ x y, rfl, }, }) (by tidy), } /-- The equivalence `Mon_ (Type u) ≌ Mon.{u}` is naturally compatible with the forgetful functors to `Type u`. -/ def Mon_Type_equivalence_Mon_forget : Mon_Type_equivalence_Mon.functor ⋙ forget Mon ≅ Mon_.forget := nat_iso.of_components (λ A, iso.refl _) (by tidy) instance Mon_Type_inhabited : inhabited (Mon_ (Type u)) := ⟨Mon_Type_equivalence_Mon.inverse.obj (Mon.of punit)⟩
3145e0caa6f8b0eb9ecfe3359a44bd8dde818d05
cf39355caa609c0f33405126beee2739aa3cb77e
/tmp/micro_lenses.lean
76458e9361c7310eaff08812782d0134588d485f
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
3,417
lean
universes u v w structure lens (α : Type u) (β : Type v) := (get : α → β) (modify : α → (β → β) → α) (set : α → β → α := λ a b, modify a (λ _, b)) def lens.compose {α : Type u} {β : Type v} {σ : Type w} (t : lens β σ) (s : lens α β) : lens α σ := { get := t^.get ∘ s^.get, modify := λ a f, s^.modify a $ λ b, t^.modify b f, set := λ a v, s^.modify a $ λ b, t^.set b v } infix `∙`:1 := lens.compose def fst {α β} : lens (α × β) α := { get := prod.fst, modify := λ ⟨a, b⟩ f, (f a, b), set := λ ⟨a, b⟩ a', (a', b)} def snd {α β} : lens (α × β) β := { get := prod.snd, modify := λ ⟨a, b⟩ f, (a, f b), set := λ ⟨a, b⟩ b', (a, b') } def idx {α} {n} (i : fin n) : lens (array α n) α := { get := λ a, a^.read i, modify := λ a f, a^.write i $ f $ a^.read i, set := λ a b, a^.write i b } def modify_ith {α} : nat → list α → (α → α) → list α | _ [] f := [] | 0 (b::l) f := f b :: l | (n+1) (b::l) f := b :: modify_ith n l f def ith {α} [inhabited α] : nat → list α → α | 0 (a::l) := a | (n+1) (a::l) := ith n l | _ _ := default α def nth {α} [inhabited α] (i : nat) : lens (list α) α := { get := ith i, modify := modify_ith i } set_option trace.array.update true def f (a : array nat 10 × array bool 5) : array nat 10 × array bool 5 := (idx 2 ∙ snd)^.set ((idx 1 ∙ fst)^.set a 1) ff #eval f (mk_array 10 0, mk_array 5 tt) #eval (idx 2 ∙ snd)^.set ((idx 1 ∙ fst)^.set (mk_array 10 0, mk_array 5 tt) 1) ff #eval let p₀ := (mk_array 10 0, mk_array 5 tt), p₁ := (idx 1 ∙ fst)^.set p₀ 1, p₂ := (idx 2 ∙ snd)^.set p₁ ff in p₂ example : (fst ∙ nth 1)^.set [(1, 2), (3, 4), (0, 3)] 30 = [(1, 2), (30, 4), (0, 3)] := rfl example : (snd ∙ nth 1)^.get [(1, 2), (3, 4), (0, 3)] = 4 := rfl def micro_lens (f : Type u → Type w) [functor f] (α β : Type u) := (β → f β) → α → f α def micro_lens.compose {f : Type u → Type w} [functor f] {α β δ: Type u} (l₁ : micro_lens f β δ) (l₂ : micro_lens f α β) : micro_lens f α δ := λ g a, l₂ (l₁ g) a instance : applicative id := {pure := λ _ a, a, seq := λ _ _ f a, f a} def micro_lens.modify {α β : Type u} (l : micro_lens id α β) (a : α) (b : β → β) : α := l b a def micro_lens.set {α β : Type u} (l : micro_lens id α β) (a : α) (b : β) : α := l (λ _, b) a def fconst (α : Type v) (β : Type u) : Type v := α instance (α : Type v) : functor (fconst α) := {map := λ (β δ : Type u) f a, a} def micro_lens.get {α β : Type u} (l : micro_lens (fconst β) α β) (a : α) : β := l (λ b, b) a def pi₁ {f : Type u → Type w} [functor f] {α β : Type u} : micro_lens f (α × β) α := λ g ⟨a, b⟩, (λ x, (x, b)) <$> g a def pi₂ {f : Type u → Type w} [functor f] {α β : Type u} : micro_lens f (α × β) β := λ g ⟨a, b⟩, (λ x, (a, x)) <$> g b #eval micro_lens.get pi₁ (10, 20) #eval micro_lens.get pi₂ (10, 20) #eval micro_lens.set pi₂ (10, 20) 100 #eval micro_lens.set (pi₁ ∘ pi₁) ((10, 20), 30) 1 #eval micro_lens.set (pi₁ ∘ pi₂) ((10, 20), 30) 1 #eval micro_lens.set pi₂ (((10, 20), 30) : (nat × nat) × nat) 1 #eval micro_lens.get (pi₁ ∘ pi₂) ((10, 20), 30) #eval micro_lens.modify (pi₁ ∘ pi₂) ((10, 20), 30) (+1)
f091c9ea331a0e42ff89256e7e7f430904fc3a63
5df84495ec6c281df6d26411cc20aac5c941e745
/src/formal_ml/real_measurable_space.lean
582710f3ef16c51b81cd8083c5257f48d2f875cc
[ "Apache-2.0" ]
permissive
eric-wieser/formal-ml
e278df5a8df78aa3947bc8376650419e1b2b0a14
630011d19fdd9539c8d6493a69fe70af5d193590
refs/heads/master
1,681,491,589,256
1,612,642,743,000
1,612,642,743,000
360,114,136
0
0
Apache-2.0
1,618,998,189,000
1,618,998,188,000
null
UTF-8
Lean
false
false
27,653
lean
/- Copyright 2020 Google LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -/ import measure_theory.measurable_space import measure_theory.measure_space import measure_theory.outer_measure import measure_theory.lebesgue_measure import measure_theory.integration import measure_theory.borel_space import formal_ml.set import formal_ml.measurable_space import formal_ml.topological_space /- The big question is how to introduce a measurable function over the reals. The topology over the reals is induced by a metric space, using absolute difference as a measure of distance. This metric space induces a topological_space. The measurable space is the borel space on top of this topological space. A key result is borel_eq_generate_from_of_subbasis. which shows that if a basis generates a second countable topology, it generates the borel space as well. For example, consider an arbitrary open sets on the real line. For any point x in the open set, there exists a real number e such that the open interval (x-e,x+e) is in the set. Moreover, there is a rational number p in the interval (x-e,x) and another in (x,x+e), implying x is in (p,q). If we take union of all the intervals (p,q) for each x in U, then we get U. So, the set of all open intervals with rational endpoints is a countable basis, implying the reals are a second order topology. It is also immediately obvious that any open set can be formed with a countable union for the reals. However, let's look at an arbitrary second countable topology. The generalization of Lindelöf's lemma shows that for a second countable topology, there is a set of open sets such that any open set is the countable union. For Munkres, the definition of second countable topology is that there exists a countable basis. The definition of basis from Munkres is that for every point is contained in at least one basis set, and for any element x in the intersection of two basis sets A and B, there is a basis set C containing x that is a subset of B and C. set, and to generate the topology, one considers arbitrary union of elements in the bases. Thus, any union of a subset of a countable set must be representable as a countable union. So, there is a countable union. The more interesting part of borel_eq_generate_from_of_subbasis is that ANY set that generates a second countable topology is a basis for the borel space. I am not exactly sure how this works: regardless, the theorem gives great insights into second countable topologies (especially, reals, nnreals, ennreals, et cetera) and borel measures. To start with, this implies that the cartesian product of the borel spaces is the same as the borel space of the cartesian product of the topologies (i.e. it commutes). So, results about continuous functions on two real variables (e.g., add, multiply, power) translate to measurable functions on measurable spaces. -/ /- We begin with basic guarantees about intervals and measurable functions. -/ set_option pp.implicit true lemma nnreal_measurable_space_def:nnreal.measurable_space = borel nnreal := rfl lemma real_measurable_space_def:real.measurable_space = borel real := rfl lemma ennreal_measurable_space_def:ennreal.measurable_space = borel ennreal := rfl lemma borel_eq_generate_Iic (α:Type*) [topological_space α] [topological_space.second_countable_topology α] [linear_order α] [order_topology α] : borel α = measurable_space.generate_from (set.range set.Iic) := begin rw borel_eq_generate_Ioi α, apply le_antisymm, { rw measurable_space.generate_from_le_iff, rw set.subset_def, intros V A1, simp, simp at A1, cases A1 with y A1, subst V, rw ← set.compl_Iic, apply measurable_space.generate_measurable.compl, apply measurable_space.generate_measurable.basic, simp, }, { rw measurable_space.generate_from_le_iff, rw set.subset_def, intros V A1, simp, simp at A1, cases A1 with y A1, subst V, rw ← set.compl_Ioi, apply measurable_space.generate_measurable.compl, apply measurable_space.generate_measurable.basic, simp, }, end lemma measurable_set_intro_Iio (α β: Type*) [M:measurable_space α] [_inst_1 : topological_space β] [_inst_2 : @topological_space.second_countable_topology β _inst_1] [_inst_3 : linear_order β] [_inst_4 : @order_topology β _inst_1 (@partial_order.to_preorder β (@linear_order.to_partial_order β _inst_3))] (f:α → β):(∀ b:β, measurable_set (set.preimage f {y|y < b})) → (@measurable α β M (borel β) f) := begin intros A1, rw borel_eq_generate_Iio β, apply generate_from_measurable, refl, intros, cases H with z A2, subst B, unfold set.Iio, apply A1, end lemma is_nnreal_measurable_intro_Iio {α:Type*} [M:measurable_space α] (f:α → nnreal): (∀ x:nnreal, measurable_set (set.preimage f {y|y < x})) → (measurable f) := begin rw nnreal_measurable_space_def, apply measurable_set_intro_Iio, end lemma is_ennreal_measurable_intro_Iio {α:Type*} [M:measurable_space α] (f:α → ennreal): (∀ x:ennreal, measurable_set (set.preimage f {y|y < x})) → (measurable f) := begin rw ennreal_measurable_space_def, apply measurable_set_intro_Iio, end lemma is_real_measurable_intro_Iio {α:Type*} [M:measurable_space α] (f:α → real): (∀ x:real, measurable_set (set.preimage f {y|y < x})) → (measurable f) := begin rw real_measurable_space_def, apply measurable_set_intro_Iio, end lemma measurable_set_set_intro_Iio (β: Type*) [_inst_1 : topological_space β] [_inst_2 : @topological_space.second_countable_topology β _inst_1] [_inst_3 : linear_order β] [_inst_4 : @order_topology β _inst_1 (@partial_order.to_preorder β (@linear_order.to_partial_order β _inst_3))] (b:β):@measurable_set β (borel β) {y|y < b} := begin rw (@borel_eq_generate_Iio β _ _ _ _), apply measurable_space.measurable_set_generate_from, simp, unfold set.Iio, apply exists.intro b, refl, end lemma is_nnreal_measurable_set_intro_Iio (x:nnreal): (measurable_set {y|y < x}) := begin rw nnreal_measurable_space_def, apply measurable_set_set_intro_Iio, end lemma is_ennreal_measurable_set_intro_Iio (x:ennreal): (measurable_set {y|y < x}) := begin rw ennreal_measurable_space_def, apply measurable_set_set_intro_Iio, end lemma is_real_measurable_set_intro_Iio (x:real): (measurable_set {y|y < x}) := begin rw real_measurable_space_def, apply measurable_set_set_intro_Iio, end lemma measurable_set_set_intro_Ioi (β: Type*) [_inst_1 : topological_space β] [_inst_2 : @topological_space.second_countable_topology β _inst_1] [_inst_3 : linear_order β] [_inst_4 : @order_topology β _inst_1 (@partial_order.to_preorder β (@linear_order.to_partial_order β _inst_3))] (b:β):@measurable_set β (borel β) {y|b < y} := begin rw (@borel_eq_generate_Ioi β _ _ _ _), apply measurable_space.measurable_set_generate_from, simp, unfold set.Ioi, apply exists.intro b, refl, end lemma is_nnreal_measurable_set_intro_Ioi (x:nnreal): (measurable_set {y|x < y}) := begin rw nnreal_measurable_space_def, apply measurable_set_set_intro_Ioi, end lemma is_ennreal_measurable_set_intro_Ioi (x:ennreal): (measurable_set {y|x < y}) := begin rw ennreal_measurable_space_def, apply measurable_set_set_intro_Ioi, end lemma is_real_measurable_set_intro_Ioi (x:real): (measurable_set {y|x < y}) := begin rw real_measurable_space_def, apply measurable_set_set_intro_Ioi, end lemma measurable_set_intro_Ioi (α β: Type*) [M:measurable_space α] [_inst_1 : topological_space β] [_inst_2 : @topological_space.second_countable_topology β _inst_1] [_inst_3 : linear_order β] [_inst_4 : @order_topology β _inst_1 (@partial_order.to_preorder β (@linear_order.to_partial_order β _inst_3))] (f:α → β):(∀ b:β, measurable_set (set.preimage f {y|b< y})) → (@measurable α β M (borel β) f) := begin intros A1, rw borel_eq_generate_Ioi β, apply generate_from_measurable, refl, intros, cases H with z A2, subst B, unfold set.Ioi, apply A1, end lemma is_nnreal_measurable_intro_Ioi {α:Type*} [M:measurable_space α] (f:α → nnreal): (∀ x:nnreal, measurable_set (set.preimage f {y|x < y})) → (measurable f) := begin rw nnreal_measurable_space_def, apply measurable_set_intro_Ioi, end lemma is_ennreal_measurable_intro_Ioi {α:Type*} [M:measurable_space α] (f:α → ennreal): (∀ x:ennreal, measurable_set (set.preimage f {y|x < y})) → (measurable f) := begin rw ennreal_measurable_space_def, apply measurable_set_intro_Ioi, end lemma is_real_measurable_intro_Ioi {α:Type*} [M:measurable_space α] (f:α → real): (∀ x:real, measurable_set (set.preimage f {y|x < y})) → (measurable f) := begin rw real_measurable_space_def, apply measurable_set_intro_Ioi, end lemma Iic_Iio_compl (β: Type*) [L : linear_order β] (x:β):{y:β|y ≤ x}ᶜ={y:β|x < y} := begin intros, ext,split;intros A1, { simp, simp at A1, exact A1, }, { simp, simp at A1, exact A1, } end lemma Ioi_Iic_compl (β: Type*) [L : linear_order β] (x:β):{y:β|x < y}={y:β|y ≤ x}ᶜ := begin intros, ext,split;intros A1, { simp, simp at A1, exact A1, }, { simp, simp at A1, exact A1, } end lemma Ici_Iio_compl (β: Type*) [L : linear_order β] (x:β):{y:β|x ≤ y}={y:β|y < x}ᶜ := begin intros, ext,split;intros A1, { simp, simp at A1, exact A1, }, { simp, simp at A1, exact A1, } end lemma Iic_Ioi_compl (β: Type*) [L : linear_order β] (x:β):{y:β|y ≤ x}={y:β|x < y}ᶜ := begin ext, split; intros A1; simp; simp at A1; exact A1, end lemma measurable_set_intro_Iic (α β: Type*) [M:measurable_space α] [_inst_1 : topological_space β] [_inst_2 : @topological_space.second_countable_topology β _inst_1] [_inst_3 : linear_order β] [_inst_4 : @order_topology β _inst_1 (@partial_order.to_preorder β (@linear_order.to_partial_order β _inst_3))] (f:α → β):(∀ b:β, measurable_set (set.preimage f {y|y ≤ b})) → (@measurable α β M (borel β) f) := begin intros A1, apply measurable_set_intro_Ioi, intros x, rw ← Iic_Iio_compl, apply measurable_set.compl, apply A1, end lemma is_nnreal_measurable_intro_Iic {α:Type*} [M:measurable_space α] (f:α → nnreal): (∀ x:nnreal, measurable_set (set.preimage f {y|y ≤ x})) → (measurable f) := begin rw nnreal_measurable_space_def, apply measurable_set_intro_Iic, end lemma measurable_set_elim_Iio (α β: Type*) [M:measurable_space α] [_inst_1 : topological_space β] [_inst_2 : @topological_space.second_countable_topology β _inst_1] [_inst_3 : linear_order β] [_inst_4 : @order_topology β _inst_1 (@partial_order.to_preorder β (@linear_order.to_partial_order β _inst_3))] (f:α → β) (b:β):(@measurable α β M (borel β) f) → (measurable_set (set.preimage f {y|y < b})) := begin intros A1, apply @measurable_elim α β M (borel β), apply A1, rw borel_eq_generate_Iio β, apply measurable_space.measurable_set_generate_from, simp, apply exists.intro b, unfold set.Iio, end lemma is_nnreal_measurable_elim_Iio {α:Type*} [M:measurable_space α] (f:α → nnreal) (x:nnreal): (measurable f) → measurable_set (set.preimage f {y|y < x}) := begin rw nnreal_measurable_space_def, apply measurable_set_elim_Iio, end lemma measurable_set_set_intro_Ici (β: Type*) [_inst_1 : topological_space β] [_inst_2 : @topological_space.second_countable_topology β _inst_1] [_inst_3 : linear_order β] [_inst_4 : @order_topology β _inst_1 (@partial_order.to_preorder β (@linear_order.to_partial_order β _inst_3))] (b:β):@measurable_set β (borel β) {y|b ≤ y} := begin rw Ici_Iio_compl, apply measurable_set.compl, apply measurable_set_set_intro_Iio, end lemma is_nnreal_measurable_set_intro_Ici (b:nnreal):measurable_set {y|b ≤ y} := begin apply measurable_set_set_intro_Ici, end --#check set.Iic_eq_compl_Ioi lemma measurable_set_set_intro_Iic (β: Type*) [_inst_1 : topological_space β] [_inst_2 : @topological_space.second_countable_topology β _inst_1] [_inst_3 : linear_order β] [_inst_4 : @order_topology β _inst_1 (@partial_order.to_preorder β (@linear_order.to_partial_order β _inst_3))] (b:β):@measurable_set β (borel β) {y|y ≤ b} := begin rw Iic_Ioi_compl, apply measurable_set.compl, apply measurable_set_set_intro_Ioi, end lemma is_nnreal_measurable_set_intro_Iic (b:nnreal):measurable_set {y|b ≤ y} := begin apply measurable_set_set_intro_Ici, end lemma is_ennreal_measurable_set_intro_Iic (b:ennreal):measurable_set {y|b ≤ y} := begin apply measurable_set_set_intro_Ici, end lemma measurable_set_elim_Iic (α β: Type*) [M:measurable_space α] [_inst_1 : topological_space β] [_inst_2 : @topological_space.second_countable_topology β _inst_1] [_inst_3 : linear_order β] [_inst_4 : @order_topology β _inst_1 (@partial_order.to_preorder β (@linear_order.to_partial_order β _inst_3))] (f:α → β) (b:β):(@measurable α β M (borel β) f) → (measurable_set (set.preimage f {y|y ≤ b})) := begin intro A1, apply @measurable_elim α β M (borel β), apply A1, rw borel_eq_generate_Ioi β, have A2:{y|y ≤ b}={y|b<y}ᶜ, { ext,split; intro A2A, { simp, simp at A2A, exact A2A, }, { simp, simp at A2A, exact A2A, } }, rw A2, apply measurable_set.compl, apply measurable_space.measurable_set_generate_from, simp, apply exists.intro b, refl, end lemma is_nnreal_measurable_elim_Iic {α:Type*} [M:measurable_space α] (f:α → nnreal) (x:nnreal): (measurable f) → (measurable_set (set.preimage f {y|y ≤ x})) := begin rw nnreal_measurable_space_def, apply measurable_set_elim_Iic, end lemma is_real_measurable_elim_Iic {α:Type*} [M:measurable_space α] (f:α → real) (x:real): (measurable f) → (measurable_set (set.preimage f {y|y ≤ x})) := begin rw real_measurable_space_def, apply measurable_set_elim_Iic, end /- Many more elimination rules can be added here. -/ lemma borel_def {α:Type*} [topological_space α]:(borel α) = measurable_space.generate_from {s : set α | is_open s} := rfl lemma continuous_measurable {α β:Type*} [topological_space α] [topological_space β] (f:α → β): continuous f → @measurable α β (borel α) (borel β) f := begin intros A1, rw borel_def, rw borel_def, apply generate_from_measurable, { refl, }, { intros, apply measurable_space.measurable_set_generate_from, rw continuous_def at A1, apply A1, apply H, } end lemma borel_def_prod_second_countable {α β:Type*} [Tα:topological_space α] [Tβ:topological_space β] [SCα:topological_space.second_countable_topology α] [SCβ:topological_space.second_countable_topology β] :(@prod.measurable_space α β (borel α) (borel β)) = (@borel (α × β) (@prod.topological_space α β Tα Tβ)) := begin /- Note that the second countable property is required here. For instance the topology with a basis [x,y) is not second countable, and its product has a set that is open, but not coverable with a countable number of sets (e.g., the line y = -x). So, this would not work. The general idea is that since each topology has a countable basis the product topology has a countable basis as well. -/ have A1:∃b:set (set α), set.countable b ∧ Tα = topological_space.generate_from b, { apply SCα.is_open_generated_countable, }, cases A1 with basisα A1, cases A1 with A2 A3, rw (@borel_eq_generate_from_of_subbasis α basisα _ _ A3), have A4:∃b:set (set β), set.countable b ∧ Tβ = topological_space.generate_from b, { apply SCβ.is_open_generated_countable, }, cases A4 with basisβ A4, cases A4 with A5 A6, rw (@borel_eq_generate_from_of_subbasis β basisβ _ _ A6), rw prod_measurable_space_def, rw @borel_eq_generate_from_of_subbasis, rw A3, rw A6, rw prod_topological_space_def, end lemma continuous_measurable_binary {α β γ:Type*} [topological_space α] [topological_space β] [topological_space γ] [topological_space.second_countable_topology α] [topological_space.second_countable_topology β] (f:α × β → γ): continuous f → (@measurable (α × β) γ (@prod.measurable_space α β (borel α) (borel β)) (borel γ) f) := begin rw borel_def_prod_second_countable, apply continuous_measurable, end -- Note: there used to be theorems like this in mathlib, but they disappeared lemma measurable_set_of_is_open {α:Type*} [topological_space α] (S:set α):is_open S → measurable_space.measurable_set' (borel α) S := begin rw borel_def, apply measurable_space.measurable_set_generate_from, end lemma measurable_set_of_is_closed {α:Type*} [topological_space α] (S:set α):is_closed S → measurable_space.measurable_set' (borel α) S := begin intro A1, unfold is_closed at A1, have A2:S = ((Sᶜ)ᶜ), { rw compl_compl, }, rw A2, apply measurable_space.measurable_set_compl, apply measurable_set_of_is_open, apply A1, end lemma is_open_measurable_set_binary {α β:Type*} [topological_space α] [topological_space β] [topological_space.second_countable_topology α] [topological_space.second_countable_topology β] (S:set (α × β)):is_open S → measurable_space.measurable_set' (@prod.measurable_space α β (borel α) (borel β)) S := begin rw borel_def_prod_second_countable, apply measurable_set_of_is_open, end lemma is_closed_measurable_set_binary {α β:Type*} [topological_space α] [topological_space β] [topological_space.second_countable_topology α] [topological_space.second_countable_topology β] (S:set (α × β)):is_closed S → measurable_space.measurable_set' (@prod.measurable_space α β (borel α) (borel β)) S := begin rw borel_def_prod_second_countable, apply measurable_set_of_is_closed, end lemma SC_composition {Ω:Type*} [MΩ:measurable_space Ω] {β:Type*} [Tβ:topological_space β] [CT:topological_space.second_countable_topology β] (f:Ω → β) (g:Ω → β) (h:β → β → β): (@measurable Ω β MΩ (borel β) f) → (@measurable Ω β MΩ (borel β) g) → continuous2 h → @measurable Ω β MΩ (borel β) (λ ω:Ω, h (f ω) (g ω)) := begin intros A1 A2 A3, have A4:@measurable Ω (β × β) MΩ (@prod.measurable_space β β (borel β) (borel β)) (λ ω:Ω, prod.mk (f ω) (g ω)), { apply measurable_fun_product_measurable;assumption, }, have A5:@measurable (β × β) β (@prod.measurable_space β β (borel β) (borel β)) (borel β) (λ p: (β × β), h (p.fst) (p.snd)), { rw continuous2_def at A3, apply continuous_measurable_binary, apply A3, }, have A6:(λ ω:Ω, h (f ω) (g ω))=(λ p: (β × β), h (p.fst) (p.snd)) ∘ (λ ω:Ω, prod.mk (f ω) (g ω)), { simp, }, rw A6, apply @compose_measurable_fun_measurable Ω (β × β) β MΩ (@prod.measurable_space β β (borel β) (borel β)) (borel β) (λ p: (β × β), h (p.fst) (p.snd)) (λ ω:Ω, prod.mk (f ω) (g ω)) A5 A4, end lemma nnreal_composition {Ω:Type*} [MΩ:measurable_space Ω] (f:Ω → nnreal) (g:Ω → nnreal) (h:nnreal → nnreal → nnreal): measurable f → measurable g → continuous2 h → measurable (λ ω:Ω, h (f ω) (g ω)) := begin apply SC_composition, end lemma SC_sum_measurable {Ω:Type*} [MΩ:measurable_space Ω] {β:Type*} {T:topological_space β} {SC:topological_space.second_countable_topology β} {CSR:add_monoid β} {TA:has_continuous_add β} (X Y:Ω → β): (@measurable _ _ _ (borel β) X) → (@measurable _ _ _ (borel β) Y) → (@measurable _ _ _ (borel β) (λ ω:Ω, (X ω + Y ω))) := begin intros A1 A2, apply @SC_composition Ω MΩ β T SC X Y, apply A1, apply A2, apply TA.continuous_add, end lemma SC_neg_measurable {Ω:Type*} [MΩ:measurable_space Ω] {β:Type*} {T:topological_space β} {SC:topological_space.second_countable_topology β} {CSR:add_group β} {TA:topological_add_group β} (X:Ω → β): (@measurable _ _ _ (borel β) X) → (@measurable _ _ _ (borel β) (λ ω:Ω, (-X ω))) := begin intros A1, have A2:(λ (ω:Ω), -X ω)=has_neg.neg ∘ X := rfl, rw A2, apply @compose_measurable_fun_measurable Ω β β MΩ (borel β) (borel β), { apply continuous_measurable, apply topological_add_group.continuous_neg, }, { apply A1, }, end --const_measurable {Ω:Type*} [measurable_space Ω] {β:Type*} [measurable_space β] (c:β) /- ∀ {α : Type u_1} {β : Type u_2} [_inst_1 : measurable_space α] [_inst_2 : measurable_space β] {a : α}, @measurable β α _inst_2 _inst_1 (λ (b : β), a) -/ def SC_sum_measurable_is_add_submonoid {Ω:Type*} [MΩ:measurable_space Ω] {β:Type*} {T:topological_space β} {SC:topological_space.second_countable_topology β} {CSR:add_monoid β} {TA:has_continuous_add β}: is_add_submonoid (@measurable Ω β MΩ (borel β)) := { zero_mem := @measurable_const β Ω (borel β) MΩ (0:β), add_mem := @SC_sum_measurable Ω MΩ β T SC CSR TA, } def SC_sum_measurable_is_add_subgroup {Ω:Type*} [MΩ:measurable_space Ω] {β:Type*} {T:topological_space β} {SC:topological_space.second_countable_topology β} {CSR:add_group β} {TA:topological_add_group β}: is_add_subgroup (@measurable Ω β MΩ (borel β)) := { zero_mem := @measurable_const β Ω (borel β) MΩ (0:β), add_mem := @SC_sum_measurable Ω MΩ β T SC (add_group.to_add_monoid β) (topological_add_group.to_has_continuous_add), neg_mem := @SC_neg_measurable Ω MΩ β T SC CSR TA, } lemma SC_mul_measurable {Ω:Type*} [MΩ:measurable_space Ω] {β:Type*} {T:topological_space β} {SC:topological_space.second_countable_topology β} {CSR:monoid β} {TA:has_continuous_mul β} (X Y:Ω → β): (@measurable _ _ _ (borel β) X) → (@measurable _ _ _ (borel β) Y) → (@measurable _ _ _ (borel β) (λ ω:Ω, (X ω * Y ω))) := begin intros A1 A2, apply @SC_composition Ω MΩ β T SC X Y, apply A1, apply A2, apply TA.continuous_mul, end def SC_mul_measurable_is_submonoid {Ω:Type*} [MΩ:measurable_space Ω] {β:Type*} {T:topological_space β} {SC:topological_space.second_countable_topology β} {CSR:monoid β} {TA:has_continuous_mul β}: is_submonoid (@measurable Ω β MΩ (borel β)) := { one_mem := @measurable_const β Ω (borel β) MΩ (1:β), mul_mem := @SC_mul_measurable Ω MΩ β T SC CSR TA, } lemma nnreal_sum_measurable {Ω:Type*} [MΩ:measurable_space Ω] (X Y:Ω → nnreal): (measurable X) → (measurable Y) → measurable (λ ω:Ω, (X ω + Y ω)) := begin apply SC_sum_measurable, apply nnreal.topological_space.second_countable_topology, apply nnreal.topological_semiring.to_has_continuous_add, end lemma finset_sum_measurable {Ω β:Type*} [measurable_space Ω] [decidable_eq β] {γ:Type*} {T:topological_space γ} {SC:topological_space.second_countable_topology γ} {CSR:add_comm_monoid γ} {TA:has_continuous_add γ} (S:finset β) (X:β → Ω → γ): (∀ b:β, @measurable Ω γ _ (borel γ) (X b)) → @measurable Ω γ _ (borel γ) (λ ω:Ω, S.sum (λ b:β, ((X b) ω))) := begin apply finset.induction_on S, { intros A1, simp, }, { intros b T A2 A3 A4, -- A3, -- A4 B, have A5:(λ (ω : Ω), finset.sum (insert b T) (λ (b : β), X b ω)) = (λ (ω : Ω), (X b ω) + finset.sum T (λ (b : β), X b ω)), { ext, rw finset.sum_insert, exact A2, }, rw A5, apply SC_sum_measurable, { apply SC, }, { apply TA, }, { apply A4, }, { apply A3, exact A4, } } end lemma finset_sum_measurable_classical {Ω β:Type*} [MΩ:measurable_space Ω] {γ:Type*} {T:topological_space γ} {SC:topological_space.second_countable_topology γ} {CSR:add_comm_monoid γ} {TA:has_continuous_add γ} (S:finset β) (X:β → Ω → γ): (∀ b:β, @measurable Ω γ _ (borel γ) (X b)) → @measurable Ω γ _ (borel γ) (λ ω:Ω, S.sum (λ b:β, ((X b) ω))) := begin have D:decidable_eq β, { apply classical.decidable_eq β, }, apply @finset_sum_measurable Ω β MΩ D γ T SC CSR TA S X, end lemma nnreal_finset_sum_measurable {Ω β:Type*} [measurable_space Ω] [decidable_eq β] (S:finset β) (X:β → Ω → nnreal): (∀ b:β, measurable (X b)) → measurable (λ ω:Ω, S.sum (λ b:β, ((X b) ω))) := begin apply finset_sum_measurable, apply_instance, apply_instance, end lemma fintype_sum_measurable {Ω β:Type*} [F:fintype β] (X:β → Ω → nnreal) [M:measurable_space Ω]: (∀ b:β, measurable (X b)) → measurable (λ ω:Ω, F.elems.sum (λ b:β, ((X b) ω))) := begin have A1:decidable_eq β, { apply classical.decidable_eq, }, apply @nnreal_finset_sum_measurable Ω β M A1, end lemma nnreal_fintype_sum_measurable {Ω β:Type*} [F:fintype β] (X:β → Ω → nnreal) [M:measurable_space Ω]: (∀ b:β, measurable (X b)) → measurable (λ ω:Ω, F.elems.sum (λ b:β, ((X b) ω))) := begin have A1:decidable_eq β, { apply classical.decidable_eq, }, apply @nnreal_finset_sum_measurable Ω β M A1, end lemma nnreal_div_mul (x y:nnreal):(x/y = x * y⁻¹) := begin refl, end ----------------Measurable Sets For Inequalities --------------------------------------------------- -- These are basic results about order topologies. lemma is_closed_le_alt {α:Type*} [T:topological_space α] [P:linear_order α] [OT:order_topology α]:is_closed {p:α × α|p.fst ≤ p.snd} := begin have A1:order_closed_topology α, { apply @order_topology.to_order_closed_topology, }, apply A1.is_closed_le', end lemma measurable_set_of_le {α:Type*} [T:topological_space α] [SC:topological_space.second_countable_topology α] [P:linear_order α] [OT:order_topology α]:measurable_space.measurable_set' (@prod.measurable_space α α (borel α) (borel α)) {p:α × α|p.fst ≤ p.snd} := begin apply is_closed_measurable_set_binary, apply is_closed_le_alt, end lemma measurable_set_of_eq {α:Type*} [T:topological_space α] [SC:topological_space.second_countable_topology α] [T2:t2_space α]:measurable_space.measurable_set' (@prod.measurable_space α α (borel α) (borel α)) {p:α × α|p.fst = p.snd} := begin apply is_closed_measurable_set_binary, apply is_closed_diagonal, end lemma measurable_set_singleton_sc {α:Type*} [T:topological_space α] [SC:topological_space.second_countable_topology α] [T2:t2_space α]:∀ (a:α), measurable_space.measurable_set' (borel α) ({a}:set α) := begin intros a, apply measurable_set_of_is_closed, apply is_closed_singleton, end
86a54e9e91110f90dd151aef99ff542ed9f43d03
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/dynamics/fixed_points/basic_auto.lean
9efbf1b3df0be2de0228785ce696e4044d5bba12
[]
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
5,837
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.set.function import Mathlib.logic.function.iterate import Mathlib.PostPort universes u v namespace Mathlib /-! # Fixed points of a self-map In this file we define * the predicate `is_fixed_pt f x := f x = x`; * the set `fixed_points f` of fixed points of a self-map `f`. We also prove some simple lemmas about `is_fixed_pt` and `∘`, `iterate`, and `semiconj`. ## Tags fixed point -/ namespace function /-- A point `x` is a fixed point of `f : α → α` if `f x = x`. -/ def is_fixed_pt {α : Type u} (f : α → α) (x : α) := f x = x /-- Every point is a fixed point of `id`. -/ theorem is_fixed_pt_id {α : Type u} (x : α) : is_fixed_pt id x := rfl namespace is_fixed_pt protected instance decidable {α : Type u} [h : DecidableEq α] {f : α → α} {x : α} : Decidable (is_fixed_pt f x) := h (f x) x /-- If `x` is a fixed point of `f`, then `f x = x`. This is useful, e.g., for `rw` or `simp`.-/ protected theorem eq {α : Type u} {f : α → α} {x : α} (hf : is_fixed_pt f x) : f x = x := hf /-- If `x` is a fixed point of `f` and `g`, then it is a fixed point of `f ∘ g`. -/ protected theorem comp {α : Type u} {f : α → α} {g : α → α} {x : α} (hf : is_fixed_pt f x) (hg : is_fixed_pt g x) : is_fixed_pt (f ∘ g) x := Eq.trans (congr_arg f hg) hf /-- If `x` is a fixed point of `f`, then it is a fixed point of `f^[n]`. -/ protected theorem iterate {α : Type u} {f : α → α} {x : α} (hf : is_fixed_pt f x) (n : ℕ) : is_fixed_pt (nat.iterate f n) x := iterate_fixed hf n /-- If `x` is a fixed point of `f ∘ g` and `g`, then it is a fixed point of `f`. -/ theorem left_of_comp {α : Type u} {f : α → α} {g : α → α} {x : α} (hfg : is_fixed_pt (f ∘ g) x) (hg : is_fixed_pt g x) : is_fixed_pt f x := Eq.trans (congr_arg f (Eq.symm hg)) hfg /-- If `x` is a fixed point of `f` and `g` is a left inverse of `f`, then `x` is a fixed point of `g`. -/ theorem to_left_inverse {α : Type u} {f : α → α} {g : α → α} {x : α} (hf : is_fixed_pt f x) (h : left_inverse g f) : is_fixed_pt g x := Eq.trans (congr_arg g (Eq.symm hf)) (h x) /-- If `g` (semi)conjugates `fa` to `fb`, then it sends fixed points of `fa` to fixed points of `fb`. -/ protected theorem map {α : Type u} {β : Type v} {fa : α → α} {fb : β → β} {x : α} (hx : is_fixed_pt fa x) {g : α → β} (h : semiconj g fa fb) : is_fixed_pt fb (g x) := Eq.trans (Eq.symm (semiconj.eq h x)) (congr_arg g hx) end is_fixed_pt /-- The set of fixed points of a map `f : α → α`. -/ def fixed_points {α : Type u} (f : α → α) : set α := set_of fun (x : α) => is_fixed_pt f x protected instance fixed_points.decidable {α : Type u} [DecidableEq α] (f : α → α) (x : α) : Decidable (x ∈ fixed_points f) := is_fixed_pt.decidable @[simp] theorem mem_fixed_points {α : Type u} {f : α → α} {x : α} : x ∈ fixed_points f ↔ is_fixed_pt f x := iff.rfl /-- If `g` semiconjugates `fa` to `fb`, then it sends fixed points of `fa` to fixed points of `fb`. -/ theorem semiconj.maps_to_fixed_pts {α : Type u} {β : Type v} {fa : α → α} {fb : β → β} {g : α → β} (h : semiconj g fa fb) : set.maps_to g (fixed_points fa) (fixed_points fb) := fun (x : α) (hx : x ∈ fixed_points fa) => is_fixed_pt.map hx h /-- Any two maps `f : α → β` and `g : β → α` are inverse of each other on the sets of fixed points of `f ∘ g` and `g ∘ f`, respectively. -/ theorem inv_on_fixed_pts_comp {α : Type u} {β : Type v} (f : α → β) (g : β → α) : set.inv_on f g (fixed_points (f ∘ g)) (fixed_points (g ∘ f)) := { left := fun (x : β) => id, right := fun (x : α) => id } /-- Any map `f` sends fixed points of `g ∘ f` to fixed points of `f ∘ g`. -/ theorem maps_to_fixed_pts_comp {α : Type u} {β : Type v} (f : α → β) (g : β → α) : set.maps_to f (fixed_points (g ∘ f)) (fixed_points (f ∘ g)) := fun (x : α) (hx : x ∈ fixed_points (g ∘ f)) => is_fixed_pt.map hx fun (x : α) => rfl /-- Given two maps `f : α → β` and `g : β → α`, `g` is a bijective map between the fixed points of `f ∘ g` and the fixed points of `g ∘ f`. The inverse map is `f`, see `inv_on_fixed_pts_comp`. -/ theorem bij_on_fixed_pts_comp {α : Type u} {β : Type v} (f : α → β) (g : β → α) : set.bij_on g (fixed_points (f ∘ g)) (fixed_points (g ∘ f)) := set.inv_on.bij_on (inv_on_fixed_pts_comp f g) (maps_to_fixed_pts_comp g f) (maps_to_fixed_pts_comp f g) /-- If self-maps `f` and `g` commute, then they are inverse of each other on the set of fixed points of `f ∘ g`. This is a particular case of `function.inv_on_fixed_pts_comp`. -/ theorem commute.inv_on_fixed_pts_comp {α : Type u} {f : α → α} {g : α → α} (h : commute f g) : set.inv_on f g (fixed_points (f ∘ g)) (fixed_points (f ∘ g)) := sorry /-- If self-maps `f` and `g` commute, then `f` is bijective on the set of fixed points of `f ∘ g`. This is a particular case of `function.bij_on_fixed_pts_comp`. -/ theorem commute.left_bij_on_fixed_pts_comp {α : Type u} {f : α → α} {g : α → α} (h : commute f g) : set.bij_on f (fixed_points (f ∘ g)) (fixed_points (f ∘ g)) := sorry /-- If self-maps `f` and `g` commute, then `g` is bijective on the set of fixed points of `f ∘ g`. This is a particular case of `function.bij_on_fixed_pts_comp`. -/ theorem commute.right_bij_on_fixed_pts_comp {α : Type u} {f : α → α} {g : α → α} (h : commute f g) : set.bij_on g (fixed_points (f ∘ g)) (fixed_points (f ∘ g)) := sorry end Mathlib
858c12d1ba9b708c5c22852f6ba6214a10456251
9dc8cecdf3c4634764a18254e94d43da07142918
/src/data/zmod/basic.lean
fb7dd8c76d543feb4a4103c67a0fa4d332e1c967
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
33,948
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import algebra.char_p.basic import tactic.fin_cases /-! # Integers mod `n` Definition of the integers mod n, and the field structure on the integers mod p. ## Definitions * `zmod n`, which is for integers modulo a nat `n : ℕ` * `val a` is defined as a natural number: - for `a : zmod 0` it is the absolute value of `a` - for `a : zmod n` with `0 < n` it is the least natural number in the equivalence class * `val_min_abs` returns the integer closest to zero in the equivalence class. * A coercion `cast` is defined from `zmod n` into any ring. This is a ring hom if the ring has characteristic dividing `n` -/ namespace zmod instance : char_zero (zmod 0) := (by apply_instance : char_zero ℤ) /-- `val a` is a natural number defined as: - for `a : zmod 0` it is the absolute value of `a` - for `a : zmod n` with `0 < n` it is the least natural number in the equivalence class See `zmod.val_min_abs` for a variant that takes values in the integers. -/ def val : Π {n : ℕ}, zmod n → ℕ | 0 := int.nat_abs | (n+1) := (coe : fin (n + 1) → ℕ) lemma val_lt {n : ℕ} [ne_zero n] (a : zmod n) : a.val < n := begin casesI n, { cases ne_zero.ne 0 rfl }, exact fin.is_lt a end lemma val_le {n : ℕ} [ne_zero n] (a : zmod n) : a.val ≤ n := a.val_lt.le @[simp] lemma val_zero : ∀ {n}, (0 : zmod n).val = 0 | 0 := rfl | (n+1) := rfl @[simp] lemma val_one' : (1 : zmod 0).val = 1 := rfl @[simp] lemma val_neg' {n : zmod 0} : (-n).val = n.val := by simp [val] @[simp] lemma val_mul' {m n : zmod 0} : (m * n).val = m.val * n.val := by simp [val, int.nat_abs_mul] lemma val_nat_cast {n : ℕ} (a : ℕ) : (a : zmod n).val = a % n := begin casesI n, { rw [nat.mod_zero], exact int.nat_abs_of_nat a, }, rw ← fin.of_nat_eq_coe, refl end instance (n : ℕ) : char_p (zmod n) n := { cast_eq_zero_iff := begin intro k, cases n, { simp only [zero_dvd_iff, int.coe_nat_eq_zero], }, rw [fin.eq_iff_veq], show (k : zmod (n+1)).val = (0 : zmod (n+1)).val ↔ _, rw [val_nat_cast, val_zero, nat.dvd_iff_mod_eq_zero], end } @[simp] lemma add_order_of_one (n : ℕ) : add_order_of (1 : zmod n) = n := char_p.eq _ (char_p.add_order_of_one _) (zmod.char_p n) /-- This lemma works in the case in which `zmod n` is not infinite, i.e. `n ≠ 0`. The version where `a ≠ 0` is `add_order_of_coe'`. -/ @[simp] lemma add_order_of_coe (a : ℕ) {n : ℕ} (n0 : n ≠ 0) : add_order_of (a : zmod n) = n / n.gcd a := begin cases a, simp [nat.pos_of_ne_zero n0], rw [← nat.smul_one_eq_coe, add_order_of_nsmul' _ a.succ_ne_zero, zmod.add_order_of_one], end /-- This lemma works in the case in which `a ≠ 0`. The version where `zmod n` is not infinite, i.e. `n ≠ 0`, is `add_order_of_coe`. -/ @[simp] lemma add_order_of_coe' {a : ℕ} (n : ℕ) (a0 : a ≠ 0) : add_order_of (a : zmod n) = n / n.gcd a := by rw [← nat.smul_one_eq_coe, add_order_of_nsmul' _ a0, zmod.add_order_of_one] /-- We have that `ring_char (zmod n) = n`. -/ lemma ring_char_zmod_n (n : ℕ) : ring_char (zmod n) = n := by { rw ring_char.eq_iff, exact zmod.char_p n, } @[simp] lemma nat_cast_self (n : ℕ) : (n : zmod n) = 0 := char_p.cast_eq_zero (zmod n) n @[simp] lemma nat_cast_self' (n : ℕ) : (n + 1 : zmod (n + 1)) = 0 := by rw [← nat.cast_add_one, nat_cast_self (n + 1)] section universal_property variables {n : ℕ} {R : Type*} section variables [add_group_with_one R] /-- Cast an integer modulo `n` to another semiring. This function is a morphism if the characteristic of `R` divides `n`. See `zmod.cast_hom` for a bundled version. -/ def cast : Π {n : ℕ}, zmod n → R | 0 := int.cast | (n+1) := λ i, i.val -- see Note [coercion into rings] @[priority 900] instance (n : ℕ) : has_coe_t (zmod n) R := ⟨cast⟩ @[simp] lemma cast_zero : ((0 : zmod n) : R) = 0 := by cases n; simp lemma cast_eq_val [ne_zero n] (a : zmod n) : (a : R) = a.val := begin casesI n, { cases ne_zero.ne 0 rfl }, refl, end variables {S : Type*} [add_group_with_one S] @[simp] lemma _root_.prod.fst_zmod_cast (a : zmod n) : (a : R × S).fst = a := by cases n; simp @[simp] lemma _root_.prod.snd_zmod_cast (a : zmod n) : (a : R × S).snd = a := by cases n; simp end /-- So-named because the coercion is `nat.cast` into `zmod`. For `nat.cast` into an arbitrary ring, see `zmod.nat_cast_val`. -/ lemma nat_cast_zmod_val {n : ℕ} [ne_zero n] (a : zmod n) : (a.val : zmod n) = a := begin casesI n, { cases ne_zero.ne 0 rfl }, { apply fin.coe_coe_eq_self } end lemma nat_cast_right_inverse [ne_zero n] : function.right_inverse val (coe : ℕ → zmod n) := nat_cast_zmod_val lemma nat_cast_zmod_surjective [ne_zero n] : function.surjective (coe : ℕ → zmod n) := nat_cast_right_inverse.surjective /-- So-named because the outer coercion is `int.cast` into `zmod`. For `int.cast` into an arbitrary ring, see `zmod.int_cast_cast`. -/ @[norm_cast] lemma int_cast_zmod_cast (a : zmod n) : ((a : ℤ) : zmod n) = a := begin cases n, { rw [int.cast_id a, int.cast_id a], }, { rw [coe_coe, int.cast_coe_nat, fin.coe_coe_eq_self] } end lemma int_cast_right_inverse : function.right_inverse (coe : zmod n → ℤ) (coe : ℤ → zmod n) := int_cast_zmod_cast lemma int_cast_surjective : function.surjective (coe : ℤ → zmod n) := int_cast_right_inverse.surjective @[norm_cast] lemma cast_id : ∀ n (i : zmod n), ↑i = i | 0 i := int.cast_id i | (n+1) i := nat_cast_zmod_val i @[simp] lemma cast_id' : (coe : zmod n → zmod n) = id := funext (cast_id n) variables (R) [ring R] /-- The coercions are respectively `nat.cast` and `zmod.cast`. -/ @[simp] lemma nat_cast_comp_val [ne_zero n] : (coe : ℕ → R) ∘ (val : zmod n → ℕ) = coe := begin casesI n, { cases ne_zero.ne 0 rfl }, refl end /-- The coercions are respectively `int.cast`, `zmod.cast`, and `zmod.cast`. -/ @[simp] lemma int_cast_comp_cast : (coe : ℤ → R) ∘ (coe : zmod n → ℤ) = coe := begin cases n, { exact congr_arg ((∘) int.cast) zmod.cast_id', }, { ext, simp } end variables {R} @[simp] lemma nat_cast_val [ne_zero n] (i : zmod n) : (i.val : R) = i := congr_fun (nat_cast_comp_val R) i @[simp] lemma int_cast_cast (i : zmod n) : ((i : ℤ) : R) = i := congr_fun (int_cast_comp_cast R) i lemma coe_add_eq_ite {n : ℕ} (a b : zmod n) : (↑(a + b) : ℤ) = if (n : ℤ) ≤ a + b then a + b - n else a + b := begin cases n, { simp }, simp only [coe_coe, fin.coe_add_eq_ite, ← int.coe_nat_add, ← int.coe_nat_succ, int.coe_nat_le], split_ifs with h, { exact int.coe_nat_sub h }, { refl } end section char_dvd /-! If the characteristic of `R` divides `n`, then `cast` is a homomorphism. -/ variables {n} {m : ℕ} [char_p R m] @[simp] lemma cast_one (h : m ∣ n) : ((1 : zmod n) : R) = 1 := begin casesI n, { exact int.cast_one }, show ((1 % (n+1) : ℕ) : R) = 1, cases n, { rw [nat.dvd_one] at h, substI m, apply subsingleton.elim }, rw nat.mod_eq_of_lt, { exact nat.cast_one }, exact nat.lt_of_sub_eq_succ rfl end lemma cast_add (h : m ∣ n) (a b : zmod n) : ((a + b : zmod n) : R) = a + b := begin casesI n, { apply int.cast_add }, simp only [coe_coe], symmetry, erw [fin.coe_add, ← nat.cast_add, ← sub_eq_zero, ← nat.cast_sub (nat.mod_le _ _), @char_p.cast_eq_zero_iff R _ m], exact h.trans (nat.dvd_sub_mod _), end lemma cast_mul (h : m ∣ n) (a b : zmod n) : ((a * b : zmod n) : R) = a * b := begin casesI n, { apply int.cast_mul }, simp only [coe_coe], symmetry, erw [fin.coe_mul, ← nat.cast_mul, ← sub_eq_zero, ← nat.cast_sub (nat.mod_le _ _), @char_p.cast_eq_zero_iff R _ m], exact h.trans (nat.dvd_sub_mod _), end /-- The canonical ring homomorphism from `zmod n` to a ring of characteristic `n`. See also `zmod.lift` (in `data.zmod.quotient`) for a generalized version working in `add_group`s. -/ def cast_hom (h : m ∣ n) (R : Type*) [ring R] [char_p R m] : zmod n →+* R := { to_fun := coe, map_zero' := cast_zero, map_one' := cast_one h, map_add' := cast_add h, map_mul' := cast_mul h } @[simp] lemma cast_hom_apply {h : m ∣ n} (i : zmod n) : cast_hom h R i = i := rfl @[simp, norm_cast] lemma cast_sub (h : m ∣ n) (a b : zmod n) : ((a - b : zmod n) : R) = a - b := (cast_hom h R).map_sub a b @[simp, norm_cast] lemma cast_neg (h : m ∣ n) (a : zmod n) : ((-a : zmod n) : R) = -a := (cast_hom h R).map_neg a @[simp, norm_cast] lemma cast_pow (h : m ∣ n) (a : zmod n) (k : ℕ) : ((a ^ k : zmod n) : R) = a ^ k := (cast_hom h R).map_pow a k @[simp, norm_cast] lemma cast_nat_cast (h : m ∣ n) (k : ℕ) : ((k : zmod n) : R) = k := map_nat_cast (cast_hom h R) k @[simp, norm_cast] lemma cast_int_cast (h : m ∣ n) (k : ℤ) : ((k : zmod n) : R) = k := map_int_cast (cast_hom h R) k end char_dvd section char_eq /-! Some specialised simp lemmas which apply when `R` has characteristic `n`. -/ variable [char_p R n] @[simp] lemma cast_one' : ((1 : zmod n) : R) = 1 := cast_one dvd_rfl @[simp] lemma cast_add' (a b : zmod n) : ((a + b : zmod n) : R) = a + b := cast_add dvd_rfl a b @[simp] lemma cast_mul' (a b : zmod n) : ((a * b : zmod n) : R) = a * b := cast_mul dvd_rfl a b @[simp] lemma cast_sub' (a b : zmod n) : ((a - b : zmod n) : R) = a - b := cast_sub dvd_rfl a b @[simp] lemma cast_pow' (a : zmod n) (k : ℕ) : ((a ^ k : zmod n) : R) = a ^ k := cast_pow dvd_rfl a k @[simp, norm_cast] lemma cast_nat_cast' (k : ℕ) : ((k : zmod n) : R) = k := cast_nat_cast dvd_rfl k @[simp, norm_cast] lemma cast_int_cast' (k : ℤ) : ((k : zmod n) : R) = k := cast_int_cast dvd_rfl k variables (R) lemma cast_hom_injective : function.injective (zmod.cast_hom (dvd_refl n) R) := begin rw injective_iff_map_eq_zero, intro x, obtain ⟨k, rfl⟩ := zmod.int_cast_surjective x, rw [map_int_cast, char_p.int_cast_eq_zero_iff R n, char_p.int_cast_eq_zero_iff (zmod n) n], exact id end lemma cast_hom_bijective [fintype R] (h : fintype.card R = n) : function.bijective (zmod.cast_hom (dvd_refl n) R) := begin haveI : ne_zero n := ⟨begin intro hn, rw hn at h, exact (fintype.card_eq_zero_iff.mp h).elim' 0 end⟩, rw [fintype.bijective_iff_injective_and_card, zmod.card, h, eq_self_iff_true, and_true], apply zmod.cast_hom_injective end /-- The unique ring isomorphism between `zmod n` and a ring `R` of characteristic `n` and cardinality `n`. -/ noncomputable def ring_equiv [fintype R] (h : fintype.card R = n) : zmod n ≃+* R := ring_equiv.of_bijective _ (zmod.cast_hom_bijective R h) /-- The identity between `zmod m` and `zmod n` when `m = n`, as a ring isomorphism. -/ def ring_equiv_congr {m n : ℕ} (h : m = n) : zmod m ≃+* zmod n := begin cases m; cases n, { exact ring_equiv.refl _ }, { exfalso, exact n.succ_ne_zero h.symm }, { exfalso, exact m.succ_ne_zero h }, { exact { map_mul' := λ a b, begin rw [order_iso.to_fun_eq_coe], ext, rw [fin.coe_cast, fin.coe_mul, fin.coe_mul, fin.coe_cast, fin.coe_cast, ← h] end, map_add' := λ a b, begin rw [order_iso.to_fun_eq_coe], ext, rw [fin.coe_cast, fin.coe_add, fin.coe_add, fin.coe_cast, fin.coe_cast, ← h] end, ..fin.cast h } } end end char_eq end universal_property lemma int_coe_eq_int_coe_iff (a b : ℤ) (c : ℕ) : (a : zmod c) = (b : zmod c) ↔ a ≡ b [ZMOD c] := char_p.int_coe_eq_int_coe_iff (zmod c) c a b lemma int_coe_eq_int_coe_iff' (a b : ℤ) (c : ℕ) : (a : zmod c) = (b : zmod c) ↔ a % c = b % c := zmod.int_coe_eq_int_coe_iff a b c lemma nat_coe_eq_nat_coe_iff (a b c : ℕ) : (a : zmod c) = (b : zmod c) ↔ a ≡ b [MOD c] := by simpa [int.coe_nat_modeq_iff] using zmod.int_coe_eq_int_coe_iff a b c lemma nat_coe_eq_nat_coe_iff' (a b c : ℕ) : (a : zmod c) = (b : zmod c) ↔ a % c = b % c := zmod.nat_coe_eq_nat_coe_iff a b c lemma int_coe_zmod_eq_zero_iff_dvd (a : ℤ) (b : ℕ) : (a : zmod b) = 0 ↔ (b : ℤ) ∣ a := by rw [← int.cast_zero, zmod.int_coe_eq_int_coe_iff, int.modeq_zero_iff_dvd] lemma int_coe_eq_int_coe_iff_dvd_sub (a b : ℤ) (c : ℕ) : (a : zmod c) = ↑b ↔ ↑c ∣ b-a := begin rw [zmod.int_coe_eq_int_coe_iff, int.modeq_iff_dvd], end lemma nat_coe_zmod_eq_zero_iff_dvd (a b : ℕ) : (a : zmod b) = 0 ↔ b ∣ a := by rw [← nat.cast_zero, zmod.nat_coe_eq_nat_coe_iff, nat.modeq_zero_iff_dvd] lemma val_int_cast {n : ℕ} (a : ℤ) [ne_zero n] : ↑(a : zmod n).val = a % n := begin have hle : (0 : ℤ) ≤ ↑(a : zmod n).val := int.coe_nat_nonneg _, have hlt : ↑(a : zmod n).val < (n : ℤ) := int.coe_nat_lt.mpr (zmod.val_lt a), refine (int.mod_eq_of_lt hle hlt).symm.trans _, rw [←zmod.int_coe_eq_int_coe_iff', int.cast_coe_nat, zmod.nat_cast_val, zmod.cast_id], end lemma coe_int_cast {n : ℕ} (a : ℤ) : ↑(a : zmod n) = a % n := begin cases n, { rw [int.coe_nat_zero, int.mod_zero, int.cast_id, int.cast_id] }, { rw [←val_int_cast, val, coe_coe] }, end @[simp] lemma val_neg_one (n : ℕ) : (-1 : zmod n.succ).val = n := begin rw [val, fin.coe_neg], cases n, { rw [nat.mod_one] }, { rw [fin.coe_one, nat.succ_add_sub_one, nat.mod_eq_of_lt (nat.lt.base _)] }, end /-- `-1 : zmod n` lifts to `n - 1 : R`. This avoids the characteristic assumption in `cast_neg`. -/ lemma cast_neg_one {R : Type*} [ring R] (n : ℕ) : ↑(-1 : zmod n) = (n - 1 : R) := begin cases n, { rw [int.cast_neg, int.cast_one, nat.cast_zero, zero_sub] }, { rw [←nat_cast_val, val_neg_one, nat.cast_succ, add_sub_cancel] }, end lemma cast_sub_one {R : Type*} [ring R] {n : ℕ} (k : zmod n) : ((k - 1 : zmod n) : R) = (if k = 0 then n else k) - 1 := begin split_ifs with hk, { rw [hk, zero_sub, zmod.cast_neg_one] }, { cases n, { rw [int.cast_sub, int.cast_one] }, { rw [←zmod.nat_cast_val, zmod.val, fin.coe_sub_one, if_neg], { rw [nat.cast_sub, nat.cast_one, coe_coe], rwa [fin.ext_iff, fin.coe_zero, ←ne, ←nat.one_le_iff_ne_zero] at hk }, { exact hk } } }, end lemma nat_coe_zmod_eq_iff (p : ℕ) (n : ℕ) (z : zmod p) [ne_zero p] : ↑n = z ↔ ∃ k, n = z.val + p * k := begin split, { rintro rfl, refine ⟨n / p, _⟩, rw [val_nat_cast, nat.mod_add_div] }, { rintro ⟨k, rfl⟩, rw [nat.cast_add, nat_cast_zmod_val, nat.cast_mul, nat_cast_self, zero_mul, add_zero] } end lemma int_coe_zmod_eq_iff (p : ℕ) (n : ℤ) (z : zmod p) [ne_zero p] : ↑n = z ↔ ∃ k, n = z.val + p * k := begin split, { rintro rfl, refine ⟨n / p, _⟩, rw [val_int_cast, int.mod_add_div] }, { rintro ⟨k, rfl⟩, rw [int.cast_add, int.cast_mul, int.cast_coe_nat, int.cast_coe_nat, nat_cast_val, zmod.nat_cast_self, zero_mul, add_zero, cast_id] } end @[push_cast, simp] lemma int_cast_mod (a : ℤ) (b : ℕ) : ((a % b : ℤ) : zmod b) = (a : zmod b) := begin rw zmod.int_coe_eq_int_coe_iff, apply int.mod_modeq, end lemma ker_int_cast_add_hom (n : ℕ) : (int.cast_add_hom (zmod n)).ker = add_subgroup.zmultiples n := by { ext, rw [int.mem_zmultiples_iff, add_monoid_hom.mem_ker, int.coe_cast_add_hom, int_coe_zmod_eq_zero_iff_dvd] } lemma ker_int_cast_ring_hom (n : ℕ) : (int.cast_ring_hom (zmod n)).ker = ideal.span ({n} : set ℤ) := by { ext, rw [ideal.mem_span_singleton, ring_hom.mem_ker, int.coe_cast_ring_hom, int_coe_zmod_eq_zero_iff_dvd] } local attribute [semireducible] int.nonneg @[simp] lemma nat_cast_to_nat (p : ℕ) : ∀ {z : ℤ} (h : 0 ≤ z), (z.to_nat : zmod p) = z | (n : ℕ) h := by simp only [int.cast_coe_nat, int.to_nat_coe_nat] | -[1+n] h := false.elim h lemma val_injective (n : ℕ) [ne_zero n] : function.injective (zmod.val : zmod n → ℕ) := begin casesI n, { cases ne_zero.ne 0 rfl }, assume a b h, ext, exact h end lemma val_one_eq_one_mod (n : ℕ) : (1 : zmod n).val = 1 % n := by rw [← nat.cast_one, val_nat_cast] lemma val_one (n : ℕ) [fact (1 < n)] : (1 : zmod n).val = 1 := by { rw val_one_eq_one_mod, exact nat.mod_eq_of_lt (fact.out _) } lemma val_add {n : ℕ} [ne_zero n] (a b : zmod n) : (a + b).val = (a.val + b.val) % n := begin casesI n, { cases ne_zero.ne 0 rfl }, { apply fin.val_add } end lemma val_mul {n : ℕ} (a b : zmod n) : (a * b).val = (a.val * b.val) % n := begin cases n, { rw nat.mod_zero, apply int.nat_abs_mul }, { apply fin.val_mul } end instance nontrivial (n : ℕ) [fact (1 < n)] : nontrivial (zmod n) := ⟨⟨0, 1, assume h, zero_ne_one $ calc 0 = (0 : zmod n).val : by rw val_zero ... = (1 : zmod n).val : congr_arg zmod.val h ... = 1 : val_one n ⟩⟩ instance nontrivial' : nontrivial (zmod 0) := int.nontrivial /-- The inversion on `zmod n`. It is setup in such a way that `a * a⁻¹` is equal to `gcd a.val n`. In particular, if `a` is coprime to `n`, and hence a unit, `a * a⁻¹ = 1`. -/ def inv : Π (n : ℕ), zmod n → zmod n | 0 i := int.sign i | (n+1) i := nat.gcd_a i.val (n+1) instance (n : ℕ) : has_inv (zmod n) := ⟨inv n⟩ lemma inv_zero : ∀ (n : ℕ), (0 : zmod n)⁻¹ = 0 | 0 := int.sign_zero | (n+1) := show (nat.gcd_a _ (n+1) : zmod (n+1)) = 0, by { rw val_zero, unfold nat.gcd_a nat.xgcd nat.xgcd_aux, refl } lemma mul_inv_eq_gcd {n : ℕ} (a : zmod n) : a * a⁻¹ = nat.gcd a.val n := begin cases n, { calc a * a⁻¹ = a * int.sign a : rfl ... = a.nat_abs : by rw int.mul_sign ... = a.val.gcd 0 : by rw nat.gcd_zero_right; refl }, { set k := n.succ, calc a * a⁻¹ = a * a⁻¹ + k * nat.gcd_b (val a) k : by rw [nat_cast_self, zero_mul, add_zero] ... = ↑(↑a.val * nat.gcd_a (val a) k + k * nat.gcd_b (val a) k) : by { push_cast, rw nat_cast_zmod_val, refl } ... = nat.gcd a.val k : (congr_arg coe (nat.gcd_eq_gcd_ab a.val k)).symm, } end @[simp] lemma nat_cast_mod (a : ℕ) (n : ℕ) : ((a % n : ℕ) : zmod n) = a := by conv {to_rhs, rw ← nat.mod_add_div a n}; simp lemma eq_iff_modeq_nat (n : ℕ) {a b : ℕ} : (a : zmod n) = b ↔ a ≡ b [MOD n] := begin cases n, { simp only [nat.modeq, int.coe_nat_inj', nat.mod_zero], }, { rw [fin.ext_iff, nat.modeq, ← val_nat_cast, ← val_nat_cast], exact iff.rfl, } end lemma coe_mul_inv_eq_one {n : ℕ} (x : ℕ) (h : nat.coprime x n) : (x * x⁻¹ : zmod n) = 1 := begin rw [nat.coprime, nat.gcd_comm, nat.gcd_rec] at h, rw [mul_inv_eq_gcd, val_nat_cast, h, nat.cast_one], end /-- `unit_of_coprime` makes an element of `(zmod n)ˣ` given a natural number `x` and a proof that `x` is coprime to `n` -/ def unit_of_coprime {n : ℕ} (x : ℕ) (h : nat.coprime x n) : (zmod n)ˣ := ⟨x, x⁻¹, coe_mul_inv_eq_one x h, by rw [mul_comm, coe_mul_inv_eq_one x h]⟩ @[simp] lemma coe_unit_of_coprime {n : ℕ} (x : ℕ) (h : nat.coprime x n) : (unit_of_coprime x h : zmod n) = x := rfl lemma val_coe_unit_coprime {n : ℕ} (u : (zmod n)ˣ) : nat.coprime (u : zmod n).val n := begin cases n, { rcases int.units_eq_one_or u with rfl|rfl; simp }, apply nat.coprime_of_mul_modeq_one ((u⁻¹ : units (zmod (n+1))) : zmod (n+1)).val, have := units.ext_iff.1 (mul_right_inv u), rw [units.coe_one] at this, rw [← eq_iff_modeq_nat, nat.cast_one, ← this], clear this, rw [← nat_cast_zmod_val ((u * u⁻¹ : units (zmod (n+1))) : zmod (n+1))], rw [units.coe_mul, val_mul, nat_cast_mod], end @[simp] lemma inv_coe_unit {n : ℕ} (u : (zmod n)ˣ) : (u : zmod n)⁻¹ = (u⁻¹ : (zmod n)ˣ) := begin have := congr_arg (coe : ℕ → zmod n) (val_coe_unit_coprime u), rw [← mul_inv_eq_gcd, nat.cast_one] at this, let u' : (zmod n)ˣ := ⟨u, (u : zmod n)⁻¹, this, by rwa mul_comm⟩, have h : u = u', { apply units.ext, refl }, rw h, refl end lemma mul_inv_of_unit {n : ℕ} (a : zmod n) (h : is_unit a) : a * a⁻¹ = 1 := begin rcases h with ⟨u, rfl⟩, rw [inv_coe_unit, u.mul_inv], end lemma inv_mul_of_unit {n : ℕ} (a : zmod n) (h : is_unit a) : a⁻¹ * a = 1 := by rw [mul_comm, mul_inv_of_unit a h] -- TODO: this equivalence is true for `zmod 0 = ℤ`, but needs to use different functions. /-- Equivalence between the units of `zmod n` and the subtype of terms `x : zmod n` for which `x.val` is comprime to `n` -/ def units_equiv_coprime {n : ℕ} [ne_zero n] : (zmod n)ˣ ≃ {x : zmod n // nat.coprime x.val n} := { to_fun := λ x, ⟨x, val_coe_unit_coprime x⟩, inv_fun := λ x, unit_of_coprime x.1.val x.2, left_inv := λ ⟨_, _, _, _⟩, units.ext (nat_cast_zmod_val _), right_inv := λ ⟨_, _⟩, by simp } /-- The **Chinese remainder theorem**. For a pair of coprime natural numbers, `m` and `n`, the rings `zmod (m * n)` and `zmod m × zmod n` are isomorphic. See `ideal.quotient_inf_ring_equiv_pi_quotient` for the Chinese remainder theorem for ideals in any ring. -/ def chinese_remainder {m n : ℕ} (h : m.coprime n) : zmod (m * n) ≃+* zmod m × zmod n := let to_fun : zmod (m * n) → zmod m × zmod n := zmod.cast_hom (show m.lcm n ∣ m * n, by simp [nat.lcm_dvd_iff]) (zmod m × zmod n) in let inv_fun : zmod m × zmod n → zmod (m * n) := λ x, if m * n = 0 then if m = 1 then ring_hom.snd _ _ x else ring_hom.fst _ _ x else nat.chinese_remainder h x.1.val x.2.val in have inv : function.left_inverse inv_fun to_fun ∧ function.right_inverse inv_fun to_fun := if hmn0 : m * n = 0 then begin rcases h.eq_of_mul_eq_zero hmn0 with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩; simp [inv_fun, to_fun, function.left_inverse, function.right_inverse, eq_int_cast, prod.ext_iff] end else begin haveI : ne_zero (m * n) := ⟨hmn0⟩, haveI : ne_zero m := ⟨left_ne_zero_of_mul hmn0⟩, haveI : ne_zero n := ⟨right_ne_zero_of_mul hmn0⟩, have left_inv : function.left_inverse inv_fun to_fun, { intro x, dsimp only [dvd_mul_left, dvd_mul_right, zmod.cast_hom_apply, coe_coe, inv_fun, to_fun], conv_rhs { rw ← zmod.nat_cast_zmod_val x }, rw [if_neg hmn0, zmod.eq_iff_modeq_nat, ← nat.modeq_and_modeq_iff_modeq_mul h, prod.fst_zmod_cast, prod.snd_zmod_cast], refine ⟨(nat.chinese_remainder h (x : zmod m).val (x : zmod n).val).2.left.trans _, (nat.chinese_remainder h (x : zmod m).val (x : zmod n).val).2.right.trans _⟩, { rw [← zmod.eq_iff_modeq_nat, zmod.nat_cast_zmod_val, zmod.nat_cast_val] }, { rw [← zmod.eq_iff_modeq_nat, zmod.nat_cast_zmod_val, zmod.nat_cast_val] } }, exact ⟨left_inv, left_inv.right_inverse_of_card_le (by simp)⟩, end, { to_fun := to_fun, inv_fun := inv_fun, map_mul' := ring_hom.map_mul _, map_add' := ring_hom.map_add _, left_inv := inv.1, right_inv := inv.2 } -- todo: this can be made a `unique` instance. instance subsingleton_units : subsingleton ((zmod 2)ˣ) := ⟨dec_trivial⟩ lemma le_div_two_iff_lt_neg (n : ℕ) [hn : fact ((n : ℕ) % 2 = 1)] {x : zmod n} (hx0 : x ≠ 0) : x.val ≤ (n / 2 : ℕ) ↔ (n / 2 : ℕ) < (-x).val := begin haveI npos : ne_zero n := ⟨by { unfreezingI { rintro rfl }, simpa [fact_iff] using hn, }⟩, have hn2 : (n : ℕ) / 2 < n := nat.div_lt_of_lt_mul ((lt_mul_iff_one_lt_left $ ne_zero.pos n).2 dec_trivial), have hn2' : (n : ℕ) - n / 2 = n / 2 + 1, { conv {to_lhs, congr, rw [← nat.succ_sub_one n, nat.succ_sub $ ne_zero.pos n]}, rw [← nat.two_mul_odd_div_two hn.1, two_mul, ← nat.succ_add, add_tsub_cancel_right], }, have hxn : (n : ℕ) - x.val < n, { rw [tsub_lt_iff_tsub_lt x.val_le le_rfl, tsub_self], rw ← zmod.nat_cast_zmod_val x at hx0, exact nat.pos_of_ne_zero (λ h, by simpa [h] using hx0) }, by conv {to_rhs, rw [← nat.succ_le_iff, nat.succ_eq_add_one, ← hn2', ← zero_add (- x), ← zmod.nat_cast_self, ← sub_eq_add_neg, ← zmod.nat_cast_zmod_val x, ← nat.cast_sub x.val_le, zmod.val_nat_cast, nat.mod_eq_of_lt hxn, tsub_le_tsub_iff_left x.val_le] } end lemma ne_neg_self (n : ℕ) [hn : fact ((n : ℕ) % 2 = 1)] {a : zmod n} (ha : a ≠ 0) : a ≠ -a := λ h, have a.val ≤ n / 2 ↔ (n : ℕ) / 2 < (-a).val := le_div_two_iff_lt_neg n ha, by rwa [← h, ← not_lt, not_iff_self] at this lemma neg_one_ne_one {n : ℕ} [fact (2 < n)] : (-1 : zmod n) ≠ 1 := char_p.neg_one_ne_one (zmod n) n lemma neg_eq_self_mod_two (a : zmod 2) : -a = a := by fin_cases a; ext; simp [fin.coe_neg, int.nat_mod]; norm_num @[simp] lemma nat_abs_mod_two (a : ℤ) : (a.nat_abs : zmod 2) = a := begin cases a, { simp only [int.nat_abs_of_nat, int.cast_coe_nat, int.of_nat_eq_coe] }, { simp only [neg_eq_self_mod_two, nat.cast_succ, int.nat_abs, int.cast_neg_succ_of_nat] } end @[simp] lemma val_eq_zero : ∀ {n : ℕ} (a : zmod n), a.val = 0 ↔ a = 0 | 0 a := int.nat_abs_eq_zero | (n+1) a := by { rw fin.ext_iff, exact iff.rfl } lemma val_cast_of_lt {n : ℕ} {a : ℕ} (h : a < n) : (a : zmod n).val = a := by rw [val_nat_cast, nat.mod_eq_of_lt h] lemma neg_val' {n : ℕ} [ne_zero n] (a : zmod n) : (-a).val = (n - a.val) % n := calc (-a).val = val (-a) % n : by rw nat.mod_eq_of_lt ((-a).val_lt) ... = (n - val a) % n : nat.modeq.add_right_cancel' _ (by rw [nat.modeq, ←val_add, add_left_neg, tsub_add_cancel_of_le a.val_le, nat.mod_self, val_zero]) lemma neg_val {n : ℕ} [ne_zero n] (a : zmod n) : (-a).val = if a = 0 then 0 else n - a.val := begin rw neg_val', by_cases h : a = 0, { rw [if_pos h, h, val_zero, tsub_zero, nat.mod_self] }, rw if_neg h, apply nat.mod_eq_of_lt, apply nat.sub_lt (ne_zero.pos n), contrapose! h, rwa [le_zero_iff, val_eq_zero] at h, end /-- `val_min_abs x` returns the integer in the same equivalence class as `x` that is closest to `0`, The result will be in the interval `(-n/2, n/2]`. -/ def val_min_abs : Π {n : ℕ}, zmod n → ℤ | 0 x := x | n@(_+1) x := if x.val ≤ n / 2 then x.val else (x.val : ℤ) - n @[simp] lemma val_min_abs_def_zero (x : zmod 0) : val_min_abs x = x := rfl lemma val_min_abs_def_pos {n : ℕ} [ne_zero n] (x : zmod n) : val_min_abs x = if x.val ≤ n / 2 then x.val else x.val - n := begin casesI n, { cases ne_zero.ne 0 rfl }, { refl } end @[simp] lemma coe_val_min_abs : ∀ {n : ℕ} (x : zmod n), (x.val_min_abs : zmod n) = x | 0 x := int.cast_id x | k@(n+1) x := begin rw val_min_abs_def_pos, split_ifs, { rw [int.cast_coe_nat, nat_cast_zmod_val] }, { rw [int.cast_sub, int.cast_coe_nat, nat_cast_zmod_val, int.cast_coe_nat, nat_cast_self, sub_zero] } end lemma nat_abs_val_min_abs_le {n : ℕ} [ne_zero n] (x : zmod n) : x.val_min_abs.nat_abs ≤ n / 2 := begin rw zmod.val_min_abs_def_pos, split_ifs with h, { exact h }, have : (x.val - n : ℤ) ≤ 0, { rw [sub_nonpos, int.coe_nat_le], exact x.val_le, }, rw [← int.coe_nat_le, int.of_nat_nat_abs_of_nonpos this, neg_sub], conv_lhs { congr, rw [← nat.mod_add_div n 2, int.coe_nat_add, int.coe_nat_mul, int.coe_nat_bit0, int.coe_nat_one] }, suffices : ((n % 2 : ℕ) + (n / 2) : ℤ) ≤ (val x), { rw ← sub_nonneg at this ⊢, apply le_trans this (le_of_eq _), ring }, norm_cast, calc (n : ℕ) % 2 + n / 2 ≤ 1 + n / 2 : nat.add_le_add_right (nat.le_of_lt_succ (nat.mod_lt _ dec_trivial)) _ ... ≤ x.val : by { rw add_comm, exact nat.succ_le_of_lt (lt_of_not_ge h) } end @[simp] lemma val_min_abs_zero : ∀ n, (0 : zmod n).val_min_abs = 0 | 0 := by simp only [val_min_abs_def_zero] | (n+1) := by simp only [val_min_abs_def_pos, if_true, int.coe_nat_zero, zero_le, val_zero] @[simp] lemma val_min_abs_eq_zero {n : ℕ} (x : zmod n) : x.val_min_abs = 0 ↔ x = 0 := begin cases n, { simp }, split, { simp only [val_min_abs_def_pos, int.coe_nat_succ], split_ifs with h h; assume h0, { apply val_injective, rwa [int.coe_nat_eq_zero] at h0, }, { apply absurd h0, rw sub_eq_zero, apply ne_of_lt, exact_mod_cast x.val_lt } }, { rintro rfl, rw val_min_abs_zero } end lemma nat_cast_nat_abs_val_min_abs {n : ℕ} [ne_zero n] (a : zmod n) : (a.val_min_abs.nat_abs : zmod n) = if a.val ≤ (n : ℕ) / 2 then a else -a := begin have : (a.val : ℤ) - n ≤ 0, by { erw [sub_nonpos, int.coe_nat_le], exact a.val_le, }, rw [zmod.val_min_abs_def_pos], split_ifs, { rw [int.nat_abs_of_nat, nat_cast_zmod_val] }, { rw [← int.cast_coe_nat, int.of_nat_nat_abs_of_nonpos this, int.cast_neg, int.cast_sub], rw [int.cast_coe_nat, int.cast_coe_nat, nat_cast_self, sub_zero, nat_cast_zmod_val], } end @[simp] lemma nat_abs_val_min_abs_neg {n : ℕ} (a : zmod n) : (-a).val_min_abs.nat_abs = a.val_min_abs.nat_abs := begin cases n, { simp only [int.nat_abs_neg, val_min_abs_def_zero], }, by_cases ha0 : a = 0, { rw [ha0, neg_zero] }, by_cases haa : -a = a, { rw [haa] }, suffices hpa : (n+1 : ℕ) - a.val ≤ (n+1) / 2 ↔ (n+1 : ℕ) / 2 < a.val, { rw [val_min_abs_def_pos, val_min_abs_def_pos], rw ← not_le at hpa, simp only [if_neg ha0, neg_val, hpa, int.coe_nat_sub a.val_le], split_ifs, all_goals { rw [← int.nat_abs_neg], congr' 1, ring } }, suffices : (((n+1 : ℕ) % 2) + 2 * ((n + 1) / 2)) - a.val ≤ (n+1) / 2 ↔ (n+1 : ℕ) / 2 < a.val, by rwa [nat.mod_add_div] at this, suffices : (n + 1) % 2 + (n + 1) / 2 ≤ val a ↔ (n + 1) / 2 < val a, by rw [tsub_le_iff_tsub_le, two_mul, ← add_assoc, add_tsub_cancel_right, this], cases (n + 1 : ℕ).mod_two_eq_zero_or_one with hn0 hn1, { split, { assume h, apply lt_of_le_of_ne (le_trans (nat.le_add_left _ _) h), contrapose! haa, rw [← zmod.nat_cast_zmod_val a, ← haa, neg_eq_iff_add_eq_zero, ← nat.cast_add], rw [char_p.cast_eq_zero_iff (zmod (n+1)) (n+1)], rw [← two_mul, ← zero_add (2 * _), ← hn0, nat.mod_add_div] }, { rw [hn0, zero_add], exact le_of_lt } }, { rw [hn1, add_comm, nat.succ_le_iff] } end lemma val_eq_ite_val_min_abs {n : ℕ} [ne_zero n] (a : zmod n) : (a.val : ℤ) = a.val_min_abs + if a.val ≤ n / 2 then 0 else n := by { rw [zmod.val_min_abs_def_pos], split_ifs; simp only [add_zero, sub_add_cancel] } lemma prime_ne_zero (p q : ℕ) [hp : fact p.prime] [hq : fact q.prime] (hpq : p ≠ q) : (q : zmod p) ≠ 0 := by rwa [← nat.cast_zero, ne.def, eq_iff_modeq_nat, nat.modeq_zero_iff_dvd, ← hp.1.coprime_iff_not_dvd, nat.coprime_primes hp.1 hq.1] end zmod namespace zmod variables (p : ℕ) [fact p.prime] private lemma mul_inv_cancel_aux (a : zmod p) (h : a ≠ 0) : a * a⁻¹ = 1 := begin obtain ⟨k, rfl⟩ := nat_cast_zmod_surjective a, apply coe_mul_inv_eq_one, apply nat.coprime.symm, rwa [nat.prime.coprime_iff_not_dvd (fact.out p.prime), ← char_p.cast_eq_zero_iff (zmod p)] end /-- Field structure on `zmod p` if `p` is prime. -/ instance : field (zmod p) := { mul_inv_cancel := mul_inv_cancel_aux p, inv_zero := inv_zero p, .. zmod.comm_ring p, .. zmod.has_inv p, .. zmod.nontrivial p } /-- `zmod p` is an integral domain when `p` is prime. -/ instance (p : ℕ) [hp : fact p.prime] : is_domain (zmod p) := begin -- We need `cases p` here in order to resolve which `comm_ring` instance is being used. unfreezingI { cases p, { exact (nat.not_prime_zero hp.out).elim }, }, exact @field.is_domain (zmod _) (zmod.field _) end end zmod lemma ring_hom.ext_zmod {n : ℕ} {R : Type*} [semiring R] (f g : (zmod n) →+* R) : f = g := begin ext a, obtain ⟨k, rfl⟩ := zmod.int_cast_surjective a, let φ : ℤ →+* R := f.comp (int.cast_ring_hom (zmod n)), let ψ : ℤ →+* R := g.comp (int.cast_ring_hom (zmod n)), show φ k = ψ k, rw φ.ext_int ψ, end namespace zmod variables {n : ℕ} {R : Type*} instance subsingleton_ring_hom [semiring R] : subsingleton ((zmod n) →+* R) := ⟨ring_hom.ext_zmod⟩ instance subsingleton_ring_equiv [semiring R] : subsingleton (zmod n ≃+* R) := ⟨λ f g, by { rw ring_equiv.coe_ring_hom_inj_iff, apply ring_hom.ext_zmod _ _ }⟩ @[simp] lemma ring_hom_map_cast [ring R] (f : R →+* (zmod n)) (k : zmod n) : f k = k := by { cases n; simp } lemma ring_hom_right_inverse [ring R] (f : R →+* (zmod n)) : function.right_inverse (coe : zmod n → R) f := ring_hom_map_cast f lemma ring_hom_surjective [ring R] (f : R →+* (zmod n)) : function.surjective f := (ring_hom_right_inverse f).surjective lemma ring_hom_eq_of_ker_eq [comm_ring R] (f g : R →+* (zmod n)) (h : f.ker = g.ker) : f = g := begin have := f.lift_of_right_inverse_comp _ (zmod.ring_hom_right_inverse f) ⟨g, le_of_eq h⟩, rw subtype.coe_mk at this, rw [←this, ring_hom.ext_zmod (f.lift_of_right_inverse _ _ ⟨g, _⟩) _, ring_hom.id_comp], end section lift variables (n) {A : Type*} [add_group A] /-- The map from `zmod n` induced by `f : ℤ →+ A` that maps `n` to `0`. -/ @[simps] def lift : {f : ℤ →+ A // f n = 0} ≃ (zmod n →+ A) := (equiv.subtype_equiv_right $ begin intro f, rw ker_int_cast_add_hom, split, { rintro hf _ ⟨x, rfl⟩, simp only [f.map_zsmul, zsmul_zero, f.mem_ker, hf] }, { intro h, refine h (add_subgroup.mem_zmultiples _) } end).trans $ ((int.cast_add_hom (zmod n)).lift_of_right_inverse coe int_cast_zmod_cast) variables (f : {f : ℤ →+ A // f n = 0}) @[simp] lemma lift_coe (x : ℤ) : lift n f (x : zmod n) = f x := add_monoid_hom.lift_of_right_inverse_comp_apply _ _ _ _ _ lemma lift_cast_add_hom (x : ℤ) : lift n f (int.cast_add_hom (zmod n) x) = f x := add_monoid_hom.lift_of_right_inverse_comp_apply _ _ _ _ _ @[simp] lemma lift_comp_coe : zmod.lift n f ∘ coe = f := funext $ lift_coe _ _ @[simp] lemma lift_comp_cast_add_hom : (zmod.lift n f).comp (int.cast_add_hom (zmod n)) = f := add_monoid_hom.ext $ lift_cast_add_hom _ _ end lift end zmod
eb2b1d97cd675957f3d943a18ea3458225beb805
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/algebra/opposites.lean
9948bcb52b08681e136bd5a62f26e8e8d4b9255d
[ "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
23,685
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import data.opposite import algebra.field import algebra.group.commute import group_theory.group_action.defs import data.equiv.mul_add /-! # Algebraic operations on `αᵒᵖ` This file records several basic facts about the opposite of an algebraic structure, e.g. the opposite of a ring is a ring (with multiplication `x * y = yx`). Use is made of the identity functions `op : α → αᵒᵖ` and `unop : αᵒᵖ → α`. -/ namespace opposite universes u variables (α : Type u) instance [has_zero α] : has_zero (opposite α) := { zero := op 0 } instance [has_one α] : has_one (opposite α) := { one := op 1 } instance [has_add α] : has_add (opposite α) := { add := λ x y, op (unop x + unop y) } instance [has_sub α] : has_sub (opposite α) := { sub := λ x y, op (unop x - unop y) } instance [has_neg α] : has_neg (opposite α) := { neg := λ x, op $ -(unop x) } instance [has_mul α] : has_mul (opposite α) := { mul := λ x y, op (unop y * unop x) } instance [has_inv α] : has_inv (opposite α) := { inv := λ x, op $ (unop x)⁻¹ } instance (R : Type*) [has_scalar R α] : has_scalar R (opposite α) := { smul := λ c x, op (c • unop x) } section variables (α) @[simp] lemma op_zero [has_zero α] : op (0 : α) = 0 := rfl @[simp] lemma unop_zero [has_zero α] : unop (0 : αᵒᵖ) = 0 := rfl @[simp] lemma op_one [has_one α] : op (1 : α) = 1 := rfl @[simp] lemma unop_one [has_one α] : unop (1 : αᵒᵖ) = 1 := rfl variable {α} @[simp] lemma op_add [has_add α] (x y : α) : op (x + y) = op x + op y := rfl @[simp] lemma unop_add [has_add α] (x y : αᵒᵖ) : unop (x + y) = unop x + unop y := rfl @[simp] lemma op_neg [has_neg α] (x : α) : op (-x) = -op x := rfl @[simp] lemma unop_neg [has_neg α] (x : αᵒᵖ) : unop (-x) = -unop x := rfl @[simp] lemma op_mul [has_mul α] (x y : α) : op (x * y) = op y * op x := rfl @[simp] lemma unop_mul [has_mul α] (x y : αᵒᵖ) : unop (x * y) = unop y * unop x := rfl @[simp] lemma op_inv [has_inv α] (x : α) : op (x⁻¹) = (op x)⁻¹ := rfl @[simp] lemma unop_inv [has_inv α] (x : αᵒᵖ) : unop (x⁻¹) = (unop x)⁻¹ := rfl @[simp] lemma op_sub [add_group α] (x y : α) : op (x - y) = op x - op y := rfl @[simp] lemma unop_sub [add_group α] (x y : αᵒᵖ) : unop (x - y) = unop x - unop y := rfl @[simp] lemma op_smul {R : Type*} [has_scalar R α] (c : R) (a : α) : op (c • a) = c • op a := rfl @[simp] lemma unop_smul {R : Type*} [has_scalar R α] (c : R) (a : αᵒᵖ) : unop (c • a) = c • unop a := rfl end instance [add_semigroup α] : add_semigroup (opposite α) := { add_assoc := λ x y z, unop_injective $ add_assoc (unop x) (unop y) (unop z), .. opposite.has_add α } instance [add_left_cancel_semigroup α] : add_left_cancel_semigroup (opposite α) := { add_left_cancel := λ x y z H, unop_injective $ add_left_cancel $ op_injective H, .. opposite.add_semigroup α } instance [add_right_cancel_semigroup α] : add_right_cancel_semigroup (opposite α) := { add_right_cancel := λ x y z H, unop_injective $ add_right_cancel $ op_injective H, .. opposite.add_semigroup α } instance [add_comm_semigroup α] : add_comm_semigroup (opposite α) := { add_comm := λ x y, unop_injective $ add_comm (unop x) (unop y), .. opposite.add_semigroup α } instance [nontrivial α] : nontrivial (opposite α) := let ⟨x, y, h⟩ := exists_pair_ne α in nontrivial_of_ne (op x) (op y) (op_injective.ne h) section local attribute [semireducible] opposite @[simp] lemma unop_eq_zero_iff {α} [has_zero α] (a : αᵒᵖ) : a.unop = (0 : α) ↔ a = (0 : αᵒᵖ) := iff.refl _ @[simp] lemma op_eq_zero_iff {α} [has_zero α] (a : α) : op a = (0 : αᵒᵖ) ↔ a = (0 : α) := iff.refl _ end lemma unop_ne_zero_iff {α} [has_zero α] (a : αᵒᵖ) : a.unop ≠ (0 : α) ↔ a ≠ (0 : αᵒᵖ) := not_iff_not.mpr $ unop_eq_zero_iff a lemma op_ne_zero_iff {α} [has_zero α] (a : α) : op a ≠ (0 : αᵒᵖ) ↔ a ≠ (0 : α) := not_iff_not.mpr $ op_eq_zero_iff a instance [add_zero_class α] : add_zero_class (opposite α) := { zero_add := λ x, unop_injective $ zero_add $ unop x, add_zero := λ x, unop_injective $ add_zero $ unop x, .. opposite.has_add α, .. opposite.has_zero α } instance [add_monoid α] : add_monoid (opposite α) := { .. opposite.add_semigroup α, .. opposite.add_zero_class α } instance [add_comm_monoid α] : add_comm_monoid (opposite α) := { .. opposite.add_monoid α, .. opposite.add_comm_semigroup α } instance [add_group α] : add_group (opposite α) := { add_left_neg := λ x, unop_injective $ add_left_neg $ unop x, sub_eq_add_neg := λ x y, unop_injective $ sub_eq_add_neg (unop x) (unop y), .. opposite.add_monoid α, .. opposite.has_neg α, .. opposite.has_sub α } instance [add_comm_group α] : add_comm_group (opposite α) := { .. opposite.add_group α, .. opposite.add_comm_monoid α } instance [semigroup α] : semigroup (opposite α) := { mul_assoc := λ x y z, unop_injective $ eq.symm $ mul_assoc (unop z) (unop y) (unop x), .. opposite.has_mul α } instance [right_cancel_semigroup α] : left_cancel_semigroup (opposite α) := { mul_left_cancel := λ x y z H, unop_injective $ mul_right_cancel $ op_injective H, .. opposite.semigroup α } instance [left_cancel_semigroup α] : right_cancel_semigroup (opposite α) := { mul_right_cancel := λ x y z H, unop_injective $ mul_left_cancel $ op_injective H, .. opposite.semigroup α } instance [comm_semigroup α] : comm_semigroup (opposite α) := { mul_comm := λ x y, unop_injective $ mul_comm (unop y) (unop x), .. opposite.semigroup α } section local attribute [semireducible] opposite @[simp] lemma unop_eq_one_iff {α} [has_one α] (a : αᵒᵖ) : a.unop = 1 ↔ a = 1 := iff.refl _ @[simp] lemma op_eq_one_iff {α} [has_one α] (a : α) : op a = 1 ↔ a = 1 := iff.refl _ end instance [mul_one_class α] : mul_one_class (opposite α) := { one_mul := λ x, unop_injective $ mul_one $ unop x, mul_one := λ x, unop_injective $ one_mul $ unop x, .. opposite.has_mul α, .. opposite.has_one α } instance [monoid α] : monoid (opposite α) := { npow := λ n x, op $ monoid.npow n x.unop, npow_zero' := λ x, unop_injective $ monoid.npow_zero' x.unop, npow_succ' := λ n x, unop_injective $ (monoid.npow_succ' n x.unop).trans begin dsimp, induction n with n ih, { rw [monoid.npow_zero', one_mul, mul_one] }, { rw [monoid.npow_succ' n x.unop, mul_assoc, ih], }, end, .. opposite.semigroup α, .. opposite.mul_one_class α } instance [right_cancel_monoid α] : left_cancel_monoid (opposite α) := { .. opposite.left_cancel_semigroup α, ..opposite.monoid α } instance [left_cancel_monoid α] : right_cancel_monoid (opposite α) := { .. opposite.right_cancel_semigroup α, ..opposite.monoid α } instance [cancel_monoid α] : cancel_monoid (opposite α) := { .. opposite.right_cancel_monoid α, ..opposite.left_cancel_monoid α } instance [comm_monoid α] : comm_monoid (opposite α) := { .. opposite.monoid α, .. opposite.comm_semigroup α } instance [cancel_comm_monoid α] : cancel_comm_monoid (opposite α) := { .. opposite.cancel_monoid α, ..opposite.comm_monoid α } instance [div_inv_monoid α] : div_inv_monoid (opposite α) := { gpow := λ n x, op $ div_inv_monoid.gpow n x.unop, gpow_zero' := λ x, unop_injective $ div_inv_monoid.gpow_zero' x.unop, gpow_succ' := λ n x, unop_injective $ (div_inv_monoid.gpow_succ' n x.unop).trans begin dsimp, induction n with n ih, { rw [int.of_nat_zero, div_inv_monoid.gpow_zero', one_mul, mul_one] }, { rw [div_inv_monoid.gpow_succ' n x.unop, mul_assoc, ih], }, end, gpow_neg' := λ z x, unop_injective $ div_inv_monoid.gpow_neg' z x.unop, .. opposite.monoid α, .. opposite.has_inv α } instance [group α] : group (opposite α) := { mul_left_inv := λ x, unop_injective $ mul_inv_self $ unop x, .. opposite.div_inv_monoid α, } instance [comm_group α] : comm_group (opposite α) := { .. opposite.group α, .. opposite.comm_monoid α } instance [distrib α] : distrib (opposite α) := { left_distrib := λ x y z, unop_injective $ add_mul (unop y) (unop z) (unop x), right_distrib := λ x y z, unop_injective $ mul_add (unop z) (unop x) (unop y), .. opposite.has_add α, .. opposite.has_mul α } instance [mul_zero_class α] : mul_zero_class (opposite α) := { zero := 0, mul := (*), zero_mul := λ x, unop_injective $ mul_zero $ unop x, mul_zero := λ x, unop_injective $ zero_mul $ unop x } instance [mul_zero_one_class α] : mul_zero_one_class (opposite α) := { .. opposite.mul_zero_class α, .. opposite.mul_one_class α } instance [semigroup_with_zero α] : semigroup_with_zero (opposite α) := { .. opposite.semigroup α, .. opposite.mul_zero_class α } instance [monoid_with_zero α] : monoid_with_zero (opposite α) := { .. opposite.monoid α, .. opposite.mul_zero_one_class α } instance [non_unital_non_assoc_semiring α] : non_unital_non_assoc_semiring (opposite α) := { .. opposite.add_comm_monoid α, .. opposite.mul_zero_class α, .. opposite.distrib α } instance [non_unital_semiring α] : non_unital_semiring (opposite α) := { .. opposite.semigroup_with_zero α, .. opposite.non_unital_non_assoc_semiring α } instance [non_assoc_semiring α] : non_assoc_semiring (opposite α) := { .. opposite.mul_zero_one_class α, .. opposite.non_unital_non_assoc_semiring α } instance [semiring α] : semiring (opposite α) := { .. opposite.non_unital_semiring α, .. opposite.non_assoc_semiring α, .. opposite.monoid_with_zero α } instance [comm_semiring α] : comm_semiring (opposite α) := { .. opposite.semiring α, .. opposite.comm_semigroup α } instance [ring α] : ring (opposite α) := { .. opposite.add_comm_group α, .. opposite.monoid α, .. opposite.semiring α } instance [comm_ring α] : comm_ring (opposite α) := { .. opposite.ring α, .. opposite.comm_semiring α } instance [has_zero α] [has_mul α] [no_zero_divisors α] : no_zero_divisors (opposite α) := { eq_zero_or_eq_zero_of_mul_eq_zero := λ x y (H : op (_ * _) = op (0:α)), or.cases_on (eq_zero_or_eq_zero_of_mul_eq_zero $ op_injective H) (λ hy, or.inr $ unop_injective $ hy) (λ hx, or.inl $ unop_injective $ hx), } instance [ring α] [is_domain α] : is_domain (opposite α) := { .. opposite.no_zero_divisors α, .. opposite.ring α, .. opposite.nontrivial α } instance [group_with_zero α] : group_with_zero (opposite α) := { mul_inv_cancel := λ x hx, unop_injective $ inv_mul_cancel $ unop_injective.ne hx, inv_zero := unop_injective inv_zero, .. opposite.monoid_with_zero α, .. opposite.div_inv_monoid α, .. opposite.nontrivial α } instance [division_ring α] : division_ring (opposite α) := { .. opposite.group_with_zero α, .. opposite.ring α } instance [field α] : field (opposite α) := { .. opposite.division_ring α, .. opposite.comm_ring α } instance (R : Type*) [monoid R] [mul_action R α] : mul_action R (opposite α) := { one_smul := λ x, unop_injective $ one_smul R (unop x), mul_smul := λ r₁ r₂ x, unop_injective $ mul_smul r₁ r₂ (unop x), ..opposite.has_scalar α R } instance (R : Type*) [monoid R] [add_monoid α] [distrib_mul_action R α] : distrib_mul_action R (opposite α) := { smul_add := λ r x₁ x₂, unop_injective $ smul_add r (unop x₁) (unop x₂), smul_zero := λ r, unop_injective $ smul_zero r, ..opposite.mul_action α R } instance (R : Type*) [monoid R] [monoid α] [mul_distrib_mul_action R α] : mul_distrib_mul_action R (opposite α) := { smul_mul := λ r x₁ x₂, unop_injective $ smul_mul' r (unop x₂) (unop x₁), smul_one := λ r, unop_injective $ smul_one r, ..opposite.mul_action α R } instance {M N} [has_scalar M N] [has_scalar M α] [has_scalar N α] [is_scalar_tower M N α] : is_scalar_tower M N αᵒᵖ := ⟨λ x y z, unop_injective $ smul_assoc _ _ _⟩ instance {M N} [has_scalar M α] [has_scalar N α] [smul_comm_class M N α] : smul_comm_class M N αᵒᵖ := ⟨λ x y z, unop_injective $ smul_comm _ _ _⟩ /-- Like `has_mul.to_has_scalar`, but multiplies on the right. See also `monoid.to_opposite_mul_action` and `monoid_with_zero.to_opposite_mul_action`. -/ instance _root_.has_mul.to_has_opposite_scalar [has_mul α] : has_scalar (opposite α) α := { smul := λ c x, x * c.unop } @[simp] lemma op_smul_eq_mul [has_mul α] {a a' : α} : op a • a' = a' * a := rfl instance _root_.semigroup.opposite_smul_comm_class [semigroup α] : smul_comm_class (opposite α) α α := { smul_comm := λ x y z, (mul_assoc _ _ _) } instance _root_.semigroup.opposite_smul_comm_class' [semigroup α] : smul_comm_class α (opposite α) α := { smul_comm := λ x y z, (mul_assoc _ _ _).symm } /-- Like `monoid.to_mul_action`, but multiplies on the right. -/ instance _root_.monoid.to_opposite_mul_action [monoid α] : mul_action (opposite α) α := { smul := (•), one_smul := mul_one, mul_smul := λ x y r, (mul_assoc _ _ _).symm } instance _root_.is_scalar_tower.opposite_mid {M N} [monoid N] [has_scalar M N] [smul_comm_class M N N] : is_scalar_tower M Nᵒᵖ N := ⟨λ x y z, mul_smul_comm _ _ _⟩ instance _root_.smul_comm_class.opposite_mid {M N} [monoid N] [has_scalar M N] [is_scalar_tower M N N] : smul_comm_class M Nᵒᵖ N := ⟨λ x y z, by { induction y using opposite.rec, simp [smul_mul_assoc] }⟩ -- The above instance does not create an unwanted diamond, the two paths to -- `mul_action (opposite α) (opposite α)` are defeq. example [monoid α] : monoid.to_mul_action (opposite α) = opposite.mul_action α (opposite α) := rfl /-- `monoid.to_opposite_mul_action` is faithful on cancellative monoids. -/ instance _root_.left_cancel_monoid.to_has_faithful_opposite_scalar [left_cancel_monoid α] : has_faithful_scalar (opposite α) α := ⟨λ x y h, unop_injective $ mul_left_cancel (h 1)⟩ /-- `monoid.to_opposite_mul_action` is faithful on nontrivial cancellative monoids with zero. -/ instance _root_.cancel_monoid_with_zero.to_has_faithful_opposite_scalar [cancel_monoid_with_zero α] [nontrivial α] : has_faithful_scalar (opposite α) α := ⟨λ x y h, unop_injective $ mul_left_cancel₀ one_ne_zero (h 1)⟩ variable {α} lemma semiconj_by.op [has_mul α] {a x y : α} (h : semiconj_by a x y) : semiconj_by (op a) (op y) (op x) := begin dunfold semiconj_by, rw [← op_mul, ← op_mul, h.eq] end lemma semiconj_by.unop [has_mul α] {a x y : αᵒᵖ} (h : semiconj_by a x y) : semiconj_by (unop a) (unop y) (unop x) := begin dunfold semiconj_by, rw [← unop_mul, ← unop_mul, h.eq] end @[simp] lemma semiconj_by_op [has_mul α] {a x y : α} : semiconj_by (op a) (op y) (op x) ↔ semiconj_by a x y := begin split, { intro h, rw [← unop_op a, ← unop_op x, ← unop_op y], exact semiconj_by.unop h }, { intro h, exact semiconj_by.op h } end @[simp] lemma semiconj_by_unop [has_mul α] {a x y : αᵒᵖ} : semiconj_by (unop a) (unop y) (unop x) ↔ semiconj_by a x y := by conv_rhs { rw [← op_unop a, ← op_unop x, ← op_unop y, semiconj_by_op] } lemma commute.op [has_mul α] {x y : α} (h : commute x y) : commute (op x) (op y) := begin dunfold commute at h ⊢, exact semiconj_by.op h end lemma commute.unop [has_mul α] {x y : αᵒᵖ} (h : commute x y) : commute (unop x) (unop y) := begin dunfold commute at h ⊢, exact semiconj_by.unop h end @[simp] lemma commute_op [has_mul α] {x y : α} : commute (op x) (op y) ↔ commute x y := begin dunfold commute, rw semiconj_by_op end @[simp] lemma commute_unop [has_mul α] {x y : αᵒᵖ} : commute (unop x) (unop y) ↔ commute x y := begin dunfold commute, rw semiconj_by_unop end /-- The function `op` is an additive equivalence. -/ def op_add_equiv [has_add α] : α ≃+ αᵒᵖ := { map_add' := λ a b, rfl, .. equiv_to_opposite } @[simp] lemma coe_op_add_equiv [has_add α] : (op_add_equiv : α → αᵒᵖ) = op := rfl @[simp] lemma coe_op_add_equiv_symm [has_add α] : (op_add_equiv.symm : αᵒᵖ → α) = unop := rfl @[simp] lemma op_add_equiv_to_equiv [has_add α] : (op_add_equiv : α ≃+ αᵒᵖ).to_equiv = equiv_to_opposite := rfl end opposite open opposite /-- Inversion on a group is a `mul_equiv` to the opposite group. When `G` is commutative, there is `mul_equiv.inv`. -/ @[simps {fully_applied := ff}] def mul_equiv.inv' (G : Type*) [group G] : G ≃* Gᵒᵖ := { to_fun := opposite.op ∘ has_inv.inv, inv_fun := has_inv.inv ∘ opposite.unop, map_mul' := λ x y, unop_injective $ mul_inv_rev x y, ..(equiv.inv G).trans equiv_to_opposite} /-- A monoid homomorphism `f : R →* S` such that `f x` commutes with `f y` for all `x, y` defines a monoid homomorphism to `Sᵒᵖ`. -/ @[simps {fully_applied := ff}] def monoid_hom.to_opposite {R S : Type*} [mul_one_class R] [mul_one_class S] (f : R →* S) (hf : ∀ x y, commute (f x) (f y)) : R →* Sᵒᵖ := { to_fun := opposite.op ∘ f, map_one' := congr_arg op f.map_one, map_mul' := λ x y, by simp [(hf x y).eq] } /-- A ring homomorphism `f : R →+* S` such that `f x` commutes with `f y` for all `x, y` defines a ring homomorphism to `Sᵒᵖ`. -/ @[simps {fully_applied := ff}] def ring_hom.to_opposite {R S : Type*} [semiring R] [semiring S] (f : R →+* S) (hf : ∀ x y, commute (f x) (f y)) : R →+* Sᵒᵖ := { to_fun := opposite.op ∘ f, .. ((opposite.op_add_equiv : S ≃+ Sᵒᵖ).to_add_monoid_hom.comp ↑f : R →+ Sᵒᵖ), .. f.to_monoid_hom.to_opposite hf } /-- The units of the opposites are equivalent to the opposites of the units. -/ def units.op_equiv {R} [monoid R] : units Rᵒᵖ ≃* (units R)ᵒᵖ := { to_fun := λ u, op ⟨unop u, unop ↑(u⁻¹), op_injective u.4, op_injective u.3⟩, inv_fun := opposite.rec $ λ u, ⟨op ↑(u), op ↑(u⁻¹), unop_injective $ u.4, unop_injective u.3⟩, map_mul' := λ x y, unop_injective $ units.ext $ rfl, left_inv := λ x, units.ext $ rfl, right_inv := λ x, unop_injective $ units.ext $ rfl } @[simp] lemma units.coe_unop_op_equiv {R} [monoid R] (u : units Rᵒᵖ) : ((units.op_equiv u).unop : R) = unop (u : Rᵒᵖ) := rfl @[simp] lemma units.coe_op_equiv_symm {R} [monoid R] (u : (units R)ᵒᵖ) : (units.op_equiv.symm u : Rᵒᵖ) = op (u.unop : R) := rfl /-- A hom `α →* β` can equivalently be viewed as a hom `αᵒᵖ →* βᵒᵖ`. This is the action of the (fully faithful) `ᵒᵖ`-functor on morphisms. -/ @[simps] def monoid_hom.op {α β} [mul_one_class α] [mul_one_class β] : (α →* β) ≃ (αᵒᵖ →* βᵒᵖ) := { to_fun := λ f, { to_fun := op ∘ f ∘ unop, map_one' := unop_injective f.map_one, map_mul' := λ x y, unop_injective (f.map_mul y.unop x.unop) }, inv_fun := λ f, { to_fun := unop ∘ f ∘ op, map_one' := congr_arg unop f.map_one, map_mul' := λ x y, congr_arg unop (f.map_mul (op y) (op x)) }, left_inv := λ f, by { ext, refl }, right_inv := λ f, by { ext, refl } } /-- The 'unopposite' of a monoid hom `αᵒᵖ →* βᵒᵖ`. Inverse to `monoid_hom.op`. -/ @[simp] def monoid_hom.unop {α β} [mul_one_class α] [mul_one_class β] : (αᵒᵖ →* βᵒᵖ) ≃ (α →* β) := monoid_hom.op.symm /-- A hom `α →+ β` can equivalently be viewed as a hom `αᵒᵖ →+ βᵒᵖ`. This is the action of the (fully faithful) `ᵒᵖ`-functor on morphisms. -/ @[simps] def add_monoid_hom.op {α β} [add_zero_class α] [add_zero_class β] : (α →+ β) ≃ (αᵒᵖ →+ βᵒᵖ) := { to_fun := λ f, { to_fun := op ∘ f ∘ unop, map_zero' := unop_injective f.map_zero, map_add' := λ x y, unop_injective (f.map_add x.unop y.unop) }, inv_fun := λ f, { to_fun := unop ∘ f ∘ op, map_zero' := congr_arg unop f.map_zero, map_add' := λ x y, congr_arg unop (f.map_add (op x) (op y)) }, left_inv := λ f, by { ext, refl }, right_inv := λ f, by { ext, refl } } /-- The 'unopposite' of an additive monoid hom `αᵒᵖ →+ βᵒᵖ`. Inverse to `add_monoid_hom.op`. -/ @[simp] def add_monoid_hom.unop {α β} [add_zero_class α] [add_zero_class β] : (αᵒᵖ →+ βᵒᵖ) ≃ (α →+ β) := add_monoid_hom.op.symm /-- A ring hom `α →+* β` can equivalently be viewed as a ring hom `αᵒᵖ →+* βᵒᵖ`. This is the action of the (fully faithful) `ᵒᵖ`-functor on morphisms. -/ @[simps] def ring_hom.op {α β} [non_assoc_semiring α] [non_assoc_semiring β] : (α →+* β) ≃ (αᵒᵖ →+* βᵒᵖ) := { to_fun := λ f, { ..f.to_add_monoid_hom.op, ..f.to_monoid_hom.op }, inv_fun := λ f, { ..f.to_add_monoid_hom.unop, ..f.to_monoid_hom.unop }, left_inv := λ f, by { ext, refl }, right_inv := λ f, by { ext, refl } } /-- The 'unopposite' of a ring hom `αᵒᵖ →+* βᵒᵖ`. Inverse to `ring_hom.op`. -/ @[simp] def ring_hom.unop {α β} [non_assoc_semiring α] [non_assoc_semiring β] : (αᵒᵖ →+* βᵒᵖ) ≃ (α →+* β) := ring_hom.op.symm /-- A iso `α ≃+ β` can equivalently be viewed as an iso `αᵒᵖ ≃+ βᵒᵖ`. -/ @[simps] def add_equiv.op {α β} [has_add α] [has_add β] : (α ≃+ β) ≃ (αᵒᵖ ≃+ βᵒᵖ) := { to_fun := λ f, op_add_equiv.symm.trans (f.trans op_add_equiv), inv_fun := λ f, op_add_equiv.trans (f.trans op_add_equiv.symm), left_inv := λ f, by { ext, refl }, right_inv := λ f, by { ext, refl } } /-- The 'unopposite' of an iso `αᵒᵖ ≃+ βᵒᵖ`. Inverse to `add_equiv.op`. -/ @[simp] def add_equiv.unop {α β} [has_add α] [has_add β] : (αᵒᵖ ≃+ βᵒᵖ) ≃ (α ≃+ β) := add_equiv.op.symm /-- A iso `α ≃* β` can equivalently be viewed as an iso `αᵒᵖ ≃+ βᵒᵖ`. -/ @[simps] def mul_equiv.op {α β} [has_mul α] [has_mul β] : (α ≃* β) ≃ (αᵒᵖ ≃* βᵒᵖ) := { to_fun := λ f, { to_fun := op ∘ f ∘ unop, inv_fun := op ∘ f.symm ∘ unop, left_inv := λ x, unop_injective (f.symm_apply_apply x.unop), right_inv := λ x, unop_injective (f.apply_symm_apply x.unop), map_mul' := λ x y, unop_injective (f.map_mul y.unop x.unop) }, inv_fun := λ f, { to_fun := unop ∘ f ∘ op, inv_fun := unop ∘ f.symm ∘ op, left_inv := λ x, op_injective (f.symm_apply_apply (op x)), right_inv := λ x, op_injective (f.apply_symm_apply (op x)), map_mul' := λ x y, congr_arg unop (f.map_mul (op y) (op x)) }, left_inv := λ f, by { ext, refl }, right_inv := λ f, by { ext, refl } } /-- The 'unopposite' of an iso `αᵒᵖ ≃* βᵒᵖ`. Inverse to `mul_equiv.op`. -/ @[simp] def mul_equiv.unop {α β} [has_mul α] [has_mul β] : (αᵒᵖ ≃* βᵒᵖ) ≃ (α ≃* β) := mul_equiv.op.symm section ext /-- This ext lemma change equalities on `αᵒᵖ →+ β` to equalities on `α →+ β`. This is useful because there are often ext lemmas for specific `α`s that will apply to an equality of `α →+ β` such as `finsupp.add_hom_ext'`. -/ @[ext] lemma add_monoid_hom.op_ext {α β} [add_zero_class α] [add_zero_class β] (f g : αᵒᵖ →+ β) (h : f.comp (op_add_equiv : α ≃+ αᵒᵖ).to_add_monoid_hom = g.comp (op_add_equiv : α ≃+ αᵒᵖ).to_add_monoid_hom) : f = g := add_monoid_hom.ext $ λ x, (add_monoid_hom.congr_fun h : _) x.unop end ext
add6de4c98612fa329c0224eb4735eb9cbbc2f46
da23b545e1653cafd4ab88b3a42b9115a0b1355f
/src/tidy/rewrite_search/tracer/unit.lean
6ca960de5bd8b6a4a5115bcd36ef9943f4ffe48e
[]
no_license
minchaowu/lean-tidy
137f5058896e0e81dae84bf8d02b74101d21677a
2d4c52d66cf07c59f8746e405ba861b4fa0e3835
refs/heads/master
1,585,283,406,120
1,535,094,033,000
1,535,094,033,000
145,945,792
0
0
null
null
null
null
UTF-8
Lean
false
false
1,144
lean
import tidy.rewrite_search.engine open tidy.rewrite_search namespace tidy.rewrite_search.tracer.unit open tactic meta def unit_tracer_init : tactic (init_result unit) := return (init_result.success ()) meta def unit_tracer_publish_vertex (_ : unit) (_ : vertex) : tactic unit := skip meta def unit_tracer_publish_edge (_ : unit) (_ : edge) : tactic unit := skip meta def unit_tracer_publish_pair (_ : unit) (_ _ : vertex_ref) : tactic unit := skip meta def unit_tracer_publish_visited (_ : unit) (_ : vertex) : tactic unit := skip meta def unit_tracer_publish_finished (_ : unit) (_ : list edge) : tactic unit := skip meta def unit_tracer_dump (_ : unit) (_ : string) : tactic unit := skip meta def unit_tracer_pause (_ : unit) : tactic unit := skip end tidy.rewrite_search.tracer.unit namespace tidy.rewrite_search.tracer open tidy.rewrite_search.tracer.unit meta def unit_tracer : tracer unit := ⟨ unit_tracer_init, unit_tracer_publish_vertex, unit_tracer_publish_edge, unit_tracer_publish_pair, unit_tracer_publish_visited, unit_tracer_publish_finished, unit_tracer_dump, unit_tracer_pause ⟩ end tidy.rewrite_search.tracer
eb0d4ca0f5bc7e1b7ffd82386f55abac1c7e1d3b
8eeb99d0fdf8125f5d39a0ce8631653f588ee817
/src/topology/uniform_space/completion.lean
aabb3aa79348c30ca78cd13c8aec75ea85afea4d
[ "Apache-2.0" ]
permissive
jesse-michael-han/mathlib
a15c58378846011b003669354cbab7062b893cfe
fa6312e4dc971985e6b7708d99a5bc3062485c89
refs/heads/master
1,625,200,760,912
1,602,081,753,000
1,602,081,753,000
181,787,230
0
0
null
1,555,460,682,000
1,555,460,682,000
null
UTF-8
Lean
false
false
23,312
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Johannes Hölzl -/ import topology.uniform_space.abstract_completion /-! # Hausdorff completions of uniform spaces The goal is to construct a left-adjoint to the inclusion of complete Hausdorff uniform spaces into all uniform spaces. Any uniform space `α` gets a completion `completion α` and a morphism (ie. uniformly continuous map) `coe : α → completion α` which solves the universal mapping problem of factorizing morphisms from `α` to any complete Hausdorff uniform space `β`. It means any uniformly continuous `f : α → β` gives rise to a unique morphism `completion.extension f : completion α → β` such that `f = completion.extension f ∘ coe`. Actually `completion.extension f` is defined for all maps from `α` to `β` but it has the desired properties only if `f` is uniformly continuous. Beware that `coe` is not injective if `α` is not Hausdorff. But its image is always dense. The adjoint functor acting on morphisms is then constructed by the usual abstract nonsense. For every uniform spaces `α` and `β`, it turns `f : α → β` into a morphism `completion.map f : completion α → completion β` such that `coe ∘ f = (completion.map f) ∘ coe` provided `f` is uniformly continuous. This construction is compatible with composition. In this file we introduce the following concepts: * `Cauchy α` the uniform completion of the uniform space `α` (using Cauchy filters). These are not minimal filters. * `completion α := quotient (separation_setoid (Cauchy α))` the Hausdorff completion. ## References This formalization is mostly based on N. Bourbaki: General Topology I. M. James: Topologies and Uniformities From a slightly different perspective in order to reuse material in topology.uniform_space.basic. -/ noncomputable theory open filter set universes u v w x open_locale uniformity classical topological_space filter /-- Space of Cauchy filters This is essentially the completion of a uniform space. The embeddings are the neighbourhood filters. This space is not minimal, the separated uniform space (i.e. quotiented on the intersection of all entourages) is necessary for this. -/ def Cauchy (α : Type u) [uniform_space α] : Type u := { f : filter α // cauchy f } namespace Cauchy section parameters {α : Type u} [uniform_space α] variables {β : Type v} {γ : Type w} variables [uniform_space β] [uniform_space γ] def gen (s : set (α × α)) : set (Cauchy α × Cauchy α) := {p | s ∈ p.1.val ×ᶠ p.2.val } lemma monotone_gen : monotone gen := monotone_set_of $ assume p, @monotone_mem_sets (α×α) (p.1.val ×ᶠ p.2.val) private lemma symm_gen : map prod.swap ((𝓤 α).lift' gen) ≤ (𝓤 α).lift' gen := calc map prod.swap ((𝓤 α).lift' gen) = (𝓤 α).lift' (λs:set (α×α), {p | s ∈ p.2.val ×ᶠ p.1.val }) : begin delta gen, simp [map_lift'_eq, monotone_set_of, monotone_mem_sets, function.comp, image_swap_eq_preimage_swap, -subtype.val_eq_coe] end ... ≤ (𝓤 α).lift' gen : uniformity_lift_le_swap (monotone_principal.comp (monotone_set_of $ assume p, @monotone_mem_sets (α×α) (p.2.val ×ᶠ p.1.val))) begin have h := λ(p:Cauchy α×Cauchy α), @filter.prod_comm _ _ (p.2.val) (p.1.val), simp [function.comp, h, -subtype.val_eq_coe], exact le_refl _ end private lemma comp_rel_gen_gen_subset_gen_comp_rel {s t : set (α×α)} : comp_rel (gen s) (gen t) ⊆ (gen (comp_rel s t) : set (Cauchy α × Cauchy α)) := assume ⟨f, g⟩ ⟨h, h₁, h₂⟩, let ⟨t₁, (ht₁ : t₁ ∈ f.val), t₂, (ht₂ : t₂ ∈ h.val), (h₁ : set.prod t₁ t₂ ⊆ s)⟩ := mem_prod_iff.mp h₁ in let ⟨t₃, (ht₃ : t₃ ∈ h.val), t₄, (ht₄ : t₄ ∈ g.val), (h₂ : set.prod t₃ t₄ ⊆ t)⟩ := mem_prod_iff.mp h₂ in have t₂ ∩ t₃ ∈ h.val, from inter_mem_sets ht₂ ht₃, let ⟨x, xt₂, xt₃⟩ := h.property.left.nonempty_of_mem this in (f.val ×ᶠ g.val).sets_of_superset (prod_mem_prod ht₁ ht₄) (assume ⟨a, b⟩ ⟨(ha : a ∈ t₁), (hb : b ∈ t₄)⟩, ⟨x, h₁ (show (a, x) ∈ set.prod t₁ t₂, from ⟨ha, xt₂⟩), h₂ (show (x, b) ∈ set.prod t₃ t₄, from ⟨xt₃, hb⟩)⟩) private lemma comp_gen : ((𝓤 α).lift' gen).lift' (λs, comp_rel s s) ≤ (𝓤 α).lift' gen := calc ((𝓤 α).lift' gen).lift' (λs, comp_rel s s) = (𝓤 α).lift' (λs, comp_rel (gen s) (gen s)) : begin rw [lift'_lift'_assoc], exact monotone_gen, exact (monotone_comp_rel monotone_id monotone_id) end ... ≤ (𝓤 α).lift' (λs, gen $ comp_rel s s) : lift'_mono' $ assume s hs, comp_rel_gen_gen_subset_gen_comp_rel ... = ((𝓤 α).lift' $ λs:set(α×α), comp_rel s s).lift' gen : begin rw [lift'_lift'_assoc], exact (monotone_comp_rel monotone_id monotone_id), exact monotone_gen end ... ≤ (𝓤 α).lift' gen : lift'_mono comp_le_uniformity (le_refl _) instance : uniform_space (Cauchy α) := uniform_space.of_core { uniformity := (𝓤 α).lift' gen, refl := principal_le_lift' $ assume s hs ⟨a, b⟩ (a_eq_b : a = b), a_eq_b ▸ a.property.right hs, symm := symm_gen, comp := comp_gen } theorem mem_uniformity {s : set (Cauchy α × Cauchy α)} : s ∈ 𝓤 (Cauchy α) ↔ ∃ t ∈ 𝓤 α, gen t ⊆ s := mem_lift'_sets monotone_gen theorem mem_uniformity' {s : set (Cauchy α × Cauchy α)} : s ∈ 𝓤 (Cauchy α) ↔ ∃ t ∈ 𝓤 α, ∀ f g : Cauchy α, t ∈ f.1 ×ᶠ g.1 → (f, g) ∈ s := mem_uniformity.trans $ bex_congr $ λ t h, prod.forall /-- Embedding of `α` into its completion `Cauchy α` -/ def pure_cauchy (a : α) : Cauchy α := ⟨pure a, cauchy_pure⟩ lemma uniform_inducing_pure_cauchy : uniform_inducing (pure_cauchy : α → Cauchy α) := ⟨have (preimage (λ (x : α × α), (pure_cauchy (x.fst), pure_cauchy (x.snd))) ∘ gen) = id, from funext $ assume s, set.ext $ assume ⟨a₁, a₂⟩, by simp [preimage, gen, pure_cauchy, prod_principal_principal], calc comap (λ (x : α × α), (pure_cauchy (x.fst), pure_cauchy (x.snd))) ((𝓤 α).lift' gen) = (𝓤 α).lift' (preimage (λ (x : α × α), (pure_cauchy (x.fst), pure_cauchy (x.snd))) ∘ gen) : comap_lift'_eq monotone_gen ... = 𝓤 α : by simp [this]⟩ lemma uniform_embedding_pure_cauchy : uniform_embedding (pure_cauchy : α → Cauchy α) := { inj := assume a₁ a₂ h, pure_injective $ subtype.ext_iff_val.1 h, ..uniform_inducing_pure_cauchy } lemma pure_cauchy_dense : ∀x, x ∈ closure (range pure_cauchy) := assume f, have h_ex : ∀ s ∈ 𝓤 (Cauchy α), ∃y:α, (f, pure_cauchy y) ∈ s, from assume s hs, let ⟨t'', ht''₁, (ht''₂ : gen t'' ⊆ s)⟩ := (mem_lift'_sets monotone_gen).mp hs in let ⟨t', ht'₁, ht'₂⟩ := comp_mem_uniformity_sets ht''₁ in have t' ∈ f.val ×ᶠ f.val, from f.property.right ht'₁, let ⟨t, ht, (h : set.prod t t ⊆ t')⟩ := mem_prod_same_iff.mp this in let ⟨x, (hx : x ∈ t)⟩ := f.property.left.nonempty_of_mem ht in have t'' ∈ f.val ×ᶠ pure x, from mem_prod_iff.mpr ⟨t, ht, {y:α | (x, y) ∈ t'}, h $ mk_mem_prod hx hx, assume ⟨a, b⟩ ⟨(h₁ : a ∈ t), (h₂ : (x, b) ∈ t')⟩, ht'₂ $ prod_mk_mem_comp_rel (@h (a, x) ⟨h₁, hx⟩) h₂⟩, ⟨x, ht''₂ $ by dsimp [gen]; exact this⟩, begin simp [closure_eq_cluster_pts, cluster_pt, nhds_eq_uniformity, lift'_inf_principal_eq, set.inter_comm], exact (lift'_ne_bot_iff $ monotone_inter monotone_const monotone_preimage).mpr (assume s hs, let ⟨y, hy⟩ := h_ex s hs in have pure_cauchy y ∈ range pure_cauchy ∩ {y : Cauchy α | (f, y) ∈ s}, from ⟨mem_range_self y, hy⟩, ⟨_, this⟩) end lemma dense_inducing_pure_cauchy : dense_inducing pure_cauchy := uniform_inducing_pure_cauchy.dense_inducing pure_cauchy_dense lemma dense_embedding_pure_cauchy : dense_embedding pure_cauchy := uniform_embedding_pure_cauchy.dense_embedding pure_cauchy_dense lemma nonempty_Cauchy_iff : nonempty (Cauchy α) ↔ nonempty α := begin split ; rintro ⟨c⟩, { have := eq_univ_iff_forall.1 dense_embedding_pure_cauchy.to_dense_inducing.closure_range c, obtain ⟨_, ⟨_, a, _⟩⟩ := mem_closure_iff.1 this _ is_open_univ trivial, exact ⟨a⟩ }, { exact ⟨pure_cauchy c⟩ } end section set_option eqn_compiler.zeta true instance : complete_space (Cauchy α) := complete_space_extension uniform_inducing_pure_cauchy pure_cauchy_dense $ assume f hf, let f' : Cauchy α := ⟨f, hf⟩ in have map pure_cauchy f ≤ (𝓤 $ Cauchy α).lift' (preimage (prod.mk f')), from le_lift' $ assume s hs, let ⟨t, ht₁, (ht₂ : gen t ⊆ s)⟩ := (mem_lift'_sets monotone_gen).mp hs in let ⟨t', ht', (h : set.prod t' t' ⊆ t)⟩ := mem_prod_same_iff.mp (hf.right ht₁) in have t' ⊆ { y : α | (f', pure_cauchy y) ∈ gen t }, from assume x hx, (f ×ᶠ pure x).sets_of_superset (prod_mem_prod ht' hx) h, f.sets_of_superset ht' $ subset.trans this (preimage_mono ht₂), ⟨f', by simp [nhds_eq_uniformity]; assumption⟩ end instance [inhabited α] : inhabited (Cauchy α) := ⟨pure_cauchy $ default α⟩ instance [h : nonempty α] : nonempty (Cauchy α) := h.rec_on $ assume a, nonempty.intro $ Cauchy.pure_cauchy a section extend def extend (f : α → β) : (Cauchy α → β) := if uniform_continuous f then dense_inducing_pure_cauchy.extend f else λ x, f (classical.inhabited_of_nonempty $ nonempty_Cauchy_iff.1 ⟨x⟩).default variables [separated_space β] lemma extend_pure_cauchy {f : α → β} (hf : uniform_continuous f) (a : α) : extend f (pure_cauchy a) = f a := begin rw [extend, if_pos hf], exact uniformly_extend_of_ind uniform_inducing_pure_cauchy pure_cauchy_dense hf _ end variables [_root_.complete_space β] lemma uniform_continuous_extend {f : α → β} : uniform_continuous (extend f) := begin by_cases hf : uniform_continuous f, { rw [extend, if_pos hf], exact uniform_continuous_uniformly_extend uniform_inducing_pure_cauchy pure_cauchy_dense hf }, { rw [extend, if_neg hf], exact uniform_continuous_of_const (assume a b, by congr) } end end extend end theorem Cauchy_eq {α : Type*} [inhabited α] [uniform_space α] [complete_space α] [separated_space α] {f g : Cauchy α} : Lim f.1 = Lim g.1 ↔ (f, g) ∈ separation_rel (Cauchy α) := begin split, { intros e s hs, rcases Cauchy.mem_uniformity'.1 hs with ⟨t, tu, ts⟩, apply ts, rcases comp_mem_uniformity_sets tu with ⟨d, du, dt⟩, refine mem_prod_iff.2 ⟨_, f.2.le_nhds_Lim (mem_nhds_right (Lim f.1) du), _, g.2.le_nhds_Lim (mem_nhds_left (Lim g.1) du), λ x h, _⟩, cases x with a b, cases h with h₁ h₂, rw ← e at h₂, exact dt ⟨_, h₁, h₂⟩ }, { intros H, refine separated_def.1 (by apply_instance) _ _ (λ t tu, _), rcases mem_uniformity_is_closed tu with ⟨d, du, dc, dt⟩, refine H {p | (Lim p.1.1, Lim p.2.1) ∈ t} (Cauchy.mem_uniformity'.2 ⟨d, du, λ f g h, _⟩), rcases mem_prod_iff.1 h with ⟨x, xf, y, yg, h⟩, have limc : ∀ (f : Cauchy α) (x ∈ f.1), Lim f.1 ∈ closure x, { intros f x xf, rw closure_eq_cluster_pts, exact ne_bot_of_le_ne_bot f.2.1 (le_inf f.2.le_nhds_Lim (le_principal_iff.2 xf)) }, have := dc.closure_subset_iff.2 h, rw closure_prod_eq at this, refine dt (this ⟨_, _⟩); dsimp; apply limc; assumption } end section local attribute [instance] uniform_space.separation_setoid lemma separated_pure_cauchy_injective {α : Type*} [uniform_space α] [s : separated_space α] : function.injective (λa:α, ⟦pure_cauchy a⟧) | a b h := separated_def.1 s _ _ $ assume s hs, let ⟨t, ht, hts⟩ := by rw [← (@uniform_embedding_pure_cauchy α _).comap_uniformity, filter.mem_comap_sets] at hs; exact hs in have (pure_cauchy a, pure_cauchy b) ∈ t, from quotient.exact h t ht, @hts (a, b) this end end Cauchy local attribute [instance] uniform_space.separation_setoid open Cauchy set namespace uniform_space variables (α : Type*) [uniform_space α] variables {β : Type*} [uniform_space β] variables {γ : Type*} [uniform_space γ] instance complete_space_separation [h : complete_space α] : complete_space (quotient (separation_setoid α)) := ⟨assume f, assume hf : cauchy f, have cauchy (f.comap (λx, ⟦x⟧)), from hf.comap' comap_quotient_le_uniformity $ hf.left.comap_of_surj $ assume b, quotient.exists_rep _, let ⟨x, (hx : f.comap (λx, ⟦x⟧) ≤ 𝓝 x)⟩ := complete_space.complete this in ⟨⟦x⟧, calc f = map (λx, ⟦x⟧) (f.comap (λx, ⟦x⟧)) : (map_comap $ univ_mem_sets' $ assume b, quotient.exists_rep _).symm ... ≤ map (λx, ⟦x⟧) (𝓝 x) : map_mono hx ... ≤ _ : continuous_iff_continuous_at.mp uniform_continuous_quotient_mk.continuous _⟩⟩ /-- Hausdorff completion of `α` -/ def completion := quotient (separation_setoid $ Cauchy α) namespace completion instance [inhabited α] : inhabited (completion α) := by unfold completion; apply_instance @[priority 50] instance : uniform_space (completion α) := by dunfold completion ; apply_instance instance : complete_space (completion α) := by dunfold completion ; apply_instance instance : separated_space (completion α) := by dunfold completion ; apply_instance instance : regular_space (completion α) := separated_regular /-- Automatic coercion from `α` to its completion. Not always injective. -/ instance : has_coe_t α (completion α) := ⟨quotient.mk ∘ pure_cauchy⟩ -- note [use has_coe_t] protected lemma coe_eq : (coe : α → completion α) = quotient.mk ∘ pure_cauchy := rfl lemma comap_coe_eq_uniformity : (𝓤 _).comap (λ(p:α×α), ((p.1 : completion α), (p.2 : completion α))) = 𝓤 α := begin have : (λx:α×α, ((x.1 : completion α), (x.2 : completion α))) = (λx:(Cauchy α)×(Cauchy α), (⟦x.1⟧, ⟦x.2⟧)) ∘ (λx:α×α, (pure_cauchy x.1, pure_cauchy x.2)), { ext ⟨a, b⟩; simp; refl }, rw [this, ← filter.comap_comap], change filter.comap _ (filter.comap _ (𝓤 $ quotient $ separation_setoid $ Cauchy α)) = 𝓤 α, rw [comap_quotient_eq_uniformity, uniform_embedding_pure_cauchy.comap_uniformity] end lemma uniform_inducing_coe : uniform_inducing (coe : α → completion α) := ⟨comap_coe_eq_uniformity α⟩ variables {α} lemma dense : dense_range (coe : α → completion α) := begin rw [dense_range_iff_closure_range, completion.coe_eq, range_comp], exact quotient_dense_of_dense pure_cauchy_dense end variables (α) def cpkg {α : Type*} [uniform_space α] : abstract_completion α := { space := completion α, coe := coe, uniform_struct := by apply_instance, complete := by apply_instance, separation := by apply_instance, uniform_inducing := completion.uniform_inducing_coe α, dense := completion.dense } instance abstract_completion.inhabited : inhabited (abstract_completion α) := ⟨cpkg⟩ local attribute [instance] abstract_completion.uniform_struct abstract_completion.complete abstract_completion.separation lemma nonempty_completion_iff : nonempty (completion α) ↔ nonempty α := (dense_range.nonempty (cpkg.dense)).symm lemma uniform_continuous_coe : uniform_continuous (coe : α → completion α) := cpkg.uniform_continuous_coe lemma continuous_coe : continuous (coe : α → completion α) := cpkg.continuous_coe lemma uniform_embedding_coe [separated_space α] : uniform_embedding (coe : α → completion α) := { comap_uniformity := comap_coe_eq_uniformity α, inj := separated_pure_cauchy_injective } variable {α} lemma dense_inducing_coe : dense_inducing (coe : α → completion α) := { dense := dense, ..(uniform_inducing_coe α).inducing } open topological_space instance separable_space_completion [separable_space α] : separable_space (completion α) := completion.dense_inducing_coe.separable lemma dense_embedding_coe [separated_space α]: dense_embedding (coe : α → completion α) := { inj := separated_pure_cauchy_injective, ..dense_inducing_coe } lemma dense₂ : dense_range (λx:α × β, ((x.1 : completion α), (x.2 : completion β))) := dense.prod dense lemma dense₃ : dense_range (λx:α × (β × γ), ((x.1 : completion α), ((x.2.1 : completion β), (x.2.2 : completion γ)))) := dense.prod dense₂ @[elab_as_eliminator] lemma induction_on {p : completion α → Prop} (a : completion α) (hp : is_closed {a | p a}) (ih : ∀a:α, p a) : p a := is_closed_property dense hp ih a @[elab_as_eliminator] lemma induction_on₂ {p : completion α → completion β → Prop} (a : completion α) (b : completion β) (hp : is_closed {x : completion α × completion β | p x.1 x.2}) (ih : ∀(a:α) (b:β), p a b) : p a b := have ∀x : completion α × completion β, p x.1 x.2, from is_closed_property dense₂ hp $ assume ⟨a, b⟩, ih a b, this (a, b) @[elab_as_eliminator] lemma induction_on₃ {p : completion α → completion β → completion γ → Prop} (a : completion α) (b : completion β) (c : completion γ) (hp : is_closed {x : completion α × completion β × completion γ | p x.1 x.2.1 x.2.2}) (ih : ∀(a:α) (b:β) (c:γ), p a b c) : p a b c := have ∀x : completion α × completion β × completion γ, p x.1 x.2.1 x.2.2, from is_closed_property dense₃ hp $ assume ⟨a, b, c⟩, ih a b c, this (a, b, c) lemma ext [t2_space β] {f g : completion α → β} (hf : continuous f) (hg : continuous g) (h : ∀a:α, f a = g a) : f = g := cpkg.funext hf hg h section extension variables {f : α → β} /-- "Extension" to the completion. It is defined for any map `f` but returns an arbitrary constant value if `f` is not uniformly continuous -/ protected def extension (f : α → β) : completion α → β := cpkg.extend f variables [separated_space β] @[simp] lemma extension_coe (hf : uniform_continuous f) (a : α) : (completion.extension f) a = f a := cpkg.extend_coe hf a variables [complete_space β] lemma uniform_continuous_extension : uniform_continuous (completion.extension f) := cpkg.uniform_continuous_extend lemma continuous_extension : continuous (completion.extension f) := cpkg.continuous_extend lemma extension_unique (hf : uniform_continuous f) {g : completion α → β} (hg : uniform_continuous g) (h : ∀ a : α, f a = g (a : completion α)) : completion.extension f = g := cpkg.extend_unique hf hg h @[simp] lemma extension_comp_coe {f : completion α → β} (hf : uniform_continuous f) : completion.extension (f ∘ coe) = f := cpkg.extend_comp_coe hf end extension section map variables {f : α → β} /-- Completion functor acting on morphisms -/ protected def map (f : α → β) : completion α → completion β := cpkg.map cpkg f lemma uniform_continuous_map : uniform_continuous (completion.map f) := cpkg.uniform_continuous_map cpkg f lemma continuous_map : continuous (completion.map f) := cpkg.continuous_map cpkg f @[simp] lemma map_coe (hf : uniform_continuous f) (a : α) : (completion.map f) a = f a := cpkg.map_coe cpkg hf a lemma map_unique {f : α → β} {g : completion α → completion β} (hg : uniform_continuous g) (h : ∀a:α, ↑(f a) = g a) : completion.map f = g := cpkg.map_unique cpkg hg h @[simp] lemma map_id : completion.map (@id α) = id := cpkg.map_id lemma extension_map [complete_space γ] [separated_space γ] {f : β → γ} {g : α → β} (hf : uniform_continuous f) (hg : uniform_continuous g) : completion.extension f ∘ completion.map g = completion.extension (f ∘ g) := completion.ext (continuous_extension.comp continuous_map) continuous_extension $ by intro a; simp only [hg, hf, hf.comp hg, (∘), map_coe, extension_coe] lemma map_comp {g : β → γ} {f : α → β} (hg : uniform_continuous g) (hf : uniform_continuous f) : completion.map g ∘ completion.map f = completion.map (g ∘ f) := extension_map ((uniform_continuous_coe _).comp hg) hf end map /- In this section we construct isomorphisms between the completion of a uniform space and the completion of its separation quotient -/ section separation_quotient_completion def completion_separation_quotient_equiv (α : Type u) [uniform_space α] : completion (separation_quotient α) ≃ completion α := begin refine ⟨completion.extension (separation_quotient.lift (coe : α → completion α)), completion.map quotient.mk, _, _⟩, { assume a, refine induction_on a (is_closed_eq (continuous_map.comp continuous_extension) continuous_id) _, rintros ⟨a⟩, show completion.map quotient.mk (completion.extension (separation_quotient.lift coe) ↑⟦a⟧) = ↑⟦a⟧, rw [extension_coe (separation_quotient.uniform_continuous_lift _), separation_quotient.lift_mk (uniform_continuous_coe α), completion.map_coe uniform_continuous_quotient_mk] ; apply_instance }, { assume a, refine completion.induction_on a (is_closed_eq (continuous_extension.comp continuous_map) continuous_id) _, assume a, rw [map_coe uniform_continuous_quotient_mk, extension_coe (separation_quotient.uniform_continuous_lift _), separation_quotient.lift_mk (uniform_continuous_coe α) _] ; apply_instance } end lemma uniform_continuous_completion_separation_quotient_equiv : uniform_continuous ⇑(completion_separation_quotient_equiv α) := uniform_continuous_extension lemma uniform_continuous_completion_separation_quotient_equiv_symm : uniform_continuous ⇑(completion_separation_quotient_equiv α).symm := uniform_continuous_map end separation_quotient_completion section extension₂ variables (f : α → β → γ) open function protected def extension₂ (f : α → β → γ) : completion α → completion β → γ := cpkg.extend₂ cpkg f variables [separated_space γ] {f} @[simp] lemma extension₂_coe_coe (hf : uniform_continuous₂ f) (a : α) (b : β) : completion.extension₂ f a b = f a b := cpkg.extension₂_coe_coe cpkg hf a b variables [complete_space γ] (f) lemma uniform_continuous_extension₂ : uniform_continuous₂ (completion.extension₂ f) := cpkg.uniform_continuous_extension₂ cpkg f end extension₂ section map₂ open function protected def map₂ (f : α → β → γ) : completion α → completion β → completion γ := cpkg.map₂ cpkg cpkg f lemma uniform_continuous_map₂ (f : α → β → γ) : uniform_continuous₂ (completion.map₂ f) := cpkg.uniform_continuous_map₂ cpkg cpkg f lemma continuous_map₂ {δ} [topological_space δ] {f : α → β → γ} {a : δ → completion α} {b : δ → completion β} (ha : continuous a) (hb : continuous b) : continuous (λd:δ, completion.map₂ f (a d) (b d)) := cpkg.continuous_map₂ cpkg cpkg ha hb lemma map₂_coe_coe (a : α) (b : β) (f : α → β → γ) (hf : uniform_continuous₂ f) : completion.map₂ f (a : completion α) (b : completion β) = f a b := cpkg.map₂_coe_coe cpkg cpkg a b f hf end map₂ end completion end uniform_space
54a504c9004d32b5bc861605e2f7ef2f3c0dd202
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/real/irrational_auto.lean
5561a4b88068cd69cd38e1a0cf73abd5923ef778
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
7,394
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne, Yury Kudryashov. -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.real.sqrt import Mathlib.data.rat.sqrt import Mathlib.ring_theory.int.basic import Mathlib.data.polynomial.eval import Mathlib.data.polynomial.degree.default import Mathlib.tactic.interval_cases import Mathlib.PostPort namespace Mathlib /-! # Irrational real numbers In this file we define a predicate `irrational` on `ℝ`, prove that the `n`-th root of an integer number is irrational if it is not integer, and that `sqrt q` is irrational if and only if `rat.sqrt q * rat.sqrt q ≠ q ∧ 0 ≤ q`. We also provide dot-style constructors like `irrational.add_rat`, `irrational.rat_sub` etc. -/ /-- A real number is irrational if it is not equal to any rational number. -/ def irrational (x : ℝ) := ¬x ∈ set.range coe theorem irrational_iff_ne_rational (x : ℝ) : irrational x ↔ ∀ (a b : ℤ), x ≠ ↑a / ↑b := sorry /-! ### Irrationality of roots of integer and rational numbers -/ /-- If `x^n`, `n > 0`, is integer and is not the `n`-th power of an integer, then `x` is irrational. -/ theorem irrational_nrt_of_notint_nrt {x : ℝ} (n : ℕ) (m : ℤ) (hxr : x ^ n = ↑m) (hv : ¬∃ (y : ℤ), x = ↑y) (hnpos : 0 < n) : irrational x := sorry /-- If `x^n = m` is an integer and `n` does not divide the `multiplicity p m`, then `x` is irrational. -/ theorem irrational_nrt_of_n_not_dvd_multiplicity {x : ℝ} (n : ℕ) {m : ℤ} (hm : m ≠ 0) (p : ℕ) [hp : fact (nat.prime p)] (hxr : x ^ n = ↑m) (hv : roption.get (multiplicity (↑p) m) (iff.mpr multiplicity.finite_int_iff { left := nat.prime.ne_one hp, right := hm }) % n ≠ 0) : irrational x := sorry theorem irrational_sqrt_of_multiplicity_odd (m : ℤ) (hm : 0 < m) (p : ℕ) [hp : fact (nat.prime p)] (Hpv : roption.get (multiplicity (↑p) m) (iff.mpr multiplicity.finite_int_iff { left := nat.prime.ne_one hp, right := ne.symm (ne_of_lt hm) }) % bit0 1 = 1) : irrational (real.sqrt ↑m) := sorry theorem nat.prime.irrational_sqrt {p : ℕ} (hp : nat.prime p) : irrational (real.sqrt ↑p) := sorry theorem irrational_sqrt_two : irrational (real.sqrt (bit0 1)) := sorry theorem irrational_sqrt_rat_iff (q : ℚ) : irrational (real.sqrt ↑q) ↔ rat.sqrt q * rat.sqrt q ≠ q ∧ 0 ≤ q := sorry protected instance irrational.decidable (q : ℚ) : Decidable (irrational (real.sqrt ↑q)) := decidable_of_iff' (rat.sqrt q * rat.sqrt q ≠ q ∧ 0 ≤ q) (irrational_sqrt_rat_iff q) /-! ### Adding/subtracting/multiplying by rational numbers -/ theorem rat.not_irrational (q : ℚ) : ¬irrational ↑q := fun (h : irrational ↑q) => h (Exists.intro q rfl) namespace irrational theorem add_cases {x : ℝ} {y : ℝ} : irrational (x + y) → irrational x ∨ irrational y := sorry theorem of_rat_add (q : ℚ) {x : ℝ} (h : irrational (↑q + x)) : irrational x := or.elim (add_cases h) (fun (h : irrational ↑q) => absurd h (rat.not_irrational q)) id theorem rat_add (q : ℚ) {x : ℝ} (h : irrational x) : irrational (↑q + x) := of_rat_add (-q) (eq.mpr (id (Eq._oldrec (Eq.refl (irrational (↑(-q) + (↑q + x)))) (rat.cast_neg q))) (eq.mpr (id (Eq._oldrec (Eq.refl (irrational (-↑q + (↑q + x)))) (neg_add_cancel_left (↑q) x))) h)) theorem of_add_rat (q : ℚ) {x : ℝ} : irrational (x + ↑q) → irrational x := add_comm (↑q) x ▸ of_rat_add q theorem add_rat (q : ℚ) {x : ℝ} (h : irrational x) : irrational (x + ↑q) := add_comm (↑q) x ▸ rat_add q h theorem of_neg {x : ℝ} (h : irrational (-x)) : irrational x := sorry protected theorem neg {x : ℝ} (h : irrational x) : irrational (-x) := of_neg (eq.mpr (id (Eq._oldrec (Eq.refl (irrational ( --x))) (neg_neg x))) h) theorem sub_rat (q : ℚ) {x : ℝ} (h : irrational x) : irrational (x - ↑q) := sorry theorem rat_sub (q : ℚ) {x : ℝ} (h : irrational x) : irrational (↑q - x) := eq.mpr (id ((fun (x x_1 : ℝ) (e_1 : x = x_1) => congr_arg irrational e_1) (↑q - x) (↑q + -x) (sub_eq_add_neg (↑q) x))) (eq.mp (Eq.refl (irrational (↑q + -x))) (rat_add q (irrational.neg h))) theorem of_sub_rat (q : ℚ) {x : ℝ} (h : irrational (x - ↑q)) : irrational x := sorry theorem of_rat_sub (q : ℚ) {x : ℝ} (h : irrational (↑q - x)) : irrational x := sorry theorem mul_cases {x : ℝ} {y : ℝ} : irrational (x * y) → irrational x ∨ irrational y := sorry theorem of_mul_rat (q : ℚ) {x : ℝ} (h : irrational (x * ↑q)) : irrational x := or.elim (mul_cases h) id fun (h : irrational ↑q) => absurd h (rat.not_irrational q) theorem mul_rat {x : ℝ} (h : irrational x) {q : ℚ} (hq : q ≠ 0) : irrational (x * ↑q) := sorry theorem of_rat_mul (q : ℚ) {x : ℝ} : irrational (↑q * x) → irrational x := mul_comm x ↑q ▸ of_mul_rat q theorem rat_mul {x : ℝ} (h : irrational x) {q : ℚ} (hq : q ≠ 0) : irrational (↑q * x) := mul_comm x ↑q ▸ mul_rat h hq theorem of_mul_self {x : ℝ} (h : irrational (x * x)) : irrational x := or.elim (mul_cases h) id id theorem of_inv {x : ℝ} (h : irrational (x⁻¹)) : irrational x := sorry protected theorem inv {x : ℝ} (h : irrational x) : irrational (x⁻¹) := of_inv (eq.mpr (id (Eq._oldrec (Eq.refl (irrational (x⁻¹⁻¹))) (inv_inv' x))) h) theorem div_cases {x : ℝ} {y : ℝ} (h : irrational (x / y)) : irrational x ∨ irrational y := or.imp id of_inv (mul_cases h) theorem of_rat_div (q : ℚ) {x : ℝ} (h : irrational (↑q / x)) : irrational x := of_inv (of_rat_mul q h) theorem of_one_div {x : ℝ} (h : irrational (1 / x)) : irrational x := of_rat_div 1 (eq.mpr (id (Eq._oldrec (Eq.refl (irrational (↑1 / x))) rat.cast_one)) h) theorem of_pow {x : ℝ} (n : ℕ) : irrational (x ^ n) → irrational x := sorry theorem of_fpow {x : ℝ} (m : ℤ) : irrational (x ^ m) → irrational x := sorry end irrational theorem one_lt_nat_degree_of_irrational_root (x : ℝ) (p : polynomial ℤ) (hx : irrational x) (p_nonzero : p ≠ 0) (x_is_root : coe_fn (polynomial.aeval x) p = 0) : 1 < polynomial.nat_degree p := sorry @[simp] theorem irrational_rat_add_iff {q : ℚ} {x : ℝ} : irrational (↑q + x) ↔ irrational x := { mp := irrational.of_rat_add q, mpr := irrational.rat_add q } @[simp] theorem irrational_add_rat_iff {q : ℚ} {x : ℝ} : irrational (x + ↑q) ↔ irrational x := { mp := irrational.of_add_rat q, mpr := irrational.add_rat q } @[simp] theorem irrational_rat_sub_iff {q : ℚ} {x : ℝ} : irrational (↑q - x) ↔ irrational x := { mp := irrational.of_rat_sub q, mpr := irrational.rat_sub q } @[simp] theorem irrational_sub_rat_iff {q : ℚ} {x : ℝ} : irrational (x - ↑q) ↔ irrational x := { mp := irrational.of_sub_rat q, mpr := irrational.sub_rat q } @[simp] theorem irrational_neg_iff {x : ℝ} : irrational (-x) ↔ irrational x := { mp := irrational.of_neg, mpr := irrational.neg } @[simp] theorem irrational_inv_iff {x : ℝ} : irrational (x⁻¹) ↔ irrational x := { mp := irrational.of_inv, mpr := irrational.inv } end Mathlib
41806ee92886145ca24a154d23989a552e845dee
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/simp_trace.lean
b763b97017f3dac27a347d12c2ce30a2a0a604e7
[ "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
2,011
lean
set_option tactic.simp.trace true set_option trace.Meta.Tactic.simp.rewrite true def f (x : α) := x example (a : α) (b : List α) : f (a::b = []) = False := by simp [f] def length : List α → Nat | [] => 0 | a::as => length as + 1 example (a b c : α) (as : List α) : length (a :: b :: as) > length as := by simp [length] apply Nat.lt.step apply Nat.lt_succ_self def fact : Nat → Nat | 0 => 1 | x+1 => (x+1) * fact x theorem ex3 : fact x > 0 := by induction x with | zero => decide | succ x ih => simp [fact] apply Nat.mul_pos apply Nat.zero_lt_succ apply ih def head [Inhabited α] : List α → α | [] => default | a::_ => a example [Inhabited α] (a : α) (as : List α) : head (a::as) = a := by simp [head] def foo := 10 example (x : Nat) : foo + x = 10 + x := by simp [foo] done def g (x : Nat) : Nat := Id.run <| do let x := x return x example : g x = x := by simp [g, bind, pure] rfl def f1 : StateM Nat Unit := do modify fun x => g x def f2 : StateM Nat Unit := do let s ← get set <| g s example : f1 = f2 := by simp [f1, f2, bind, StateT.bind, get, getThe, MonadStateOf.get, StateT.get, pure, set, StateT.set, modify, modifyGet, MonadStateOf.modifyGet, StateT.modifyGet] def h (x : Nat) : Sum (Nat × Nat) Nat := Sum.inl (x, x) def bla (x : Nat) := match h x with | Sum.inl (y, z) => y + z | Sum.inr _ => 0 example (x : Nat) : bla x = x + x := by simp [bla, h] example (x : Nat) (h : 1 ≤ x) : x - 1 + 1 + 2 = x + 2 := by simp [h, Nat.sub_add_cancel] example (x : Nat) : (if h : 1 ≤ x then x - 1 + 1 else 0) = (if _h : 1 ≤ x then x else 0) := by simp (config := {contextual := true}) [h, Nat.sub_add_cancel] theorem my_thm : a ∧ a ↔ a := ⟨fun h => h.1, fun h => ⟨h, h⟩⟩ example : a ∧ (b ∧ b) ↔ a ∧ b := by simp [my_thm] example : (a ∧ (b ∧ b)) = (a ∧ b) := by simp only [my_thm] example : x - 1 + 1 = x := by simp (discharger := sorry) [Nat.sub_add_cancel]
d0a5683847d7be813c72c54bf31c7e63691c77a6
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/data/int/basic.lean
187da287cfc0c3b7f5389d2e961d83f7f1d9ceb1
[ "Apache-2.0" ]
permissive
hikari0108/mathlib
b7ea2b7350497ab1a0b87a09d093ecc025a50dfa
a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901
refs/heads/master
1,690,483,608,260
1,631,541,580,000
1,631,541,580,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
55,390
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad -/ import algebra.order_functions import data.nat.pow /-! # Basic operations on the integers This file contains: * instances on `ℤ`. The stronger one is `int.linear_ordered_comm_ring`. * some basic lemmas about integers ## Recursors * `int.rec`: Sign disjunction. Something is true/defined on `ℤ` if it's true/defined for nonnegative and for negative values. * `int.bit_cases_on`: Parity disjunction. Something is true/defined on `ℤ` if it's true/defined for even and for odd values. * `int.induction_on`: Simple growing induction on positive numbers, plus simple decreasing induction on negative numbers. Note that this recursor is currently only `Prop`-valued. * `int.induction_on'`: Simple growing induction for numbers greater than `b`, plus simple decreasing induction on numbers less than `b`. -/ open nat namespace int instance : inhabited ℤ := ⟨int.zero⟩ instance : nontrivial ℤ := ⟨⟨0, 1, int.zero_ne_one⟩⟩ instance : comm_ring int := { add := int.add, add_assoc := int.add_assoc, zero := int.zero, zero_add := int.zero_add, add_zero := int.add_zero, neg := int.neg, add_left_neg := int.add_left_neg, add_comm := int.add_comm, mul := int.mul, mul_assoc := int.mul_assoc, one := int.one, one_mul := int.one_mul, mul_one := int.mul_one, sub := int.sub, left_distrib := int.distrib_left, right_distrib := int.distrib_right, mul_comm := int.mul_comm, gsmul := (*), gsmul_zero' := int.zero_mul, gsmul_succ' := λ n x, by rw [succ_eq_one_add, of_nat_add, int.distrib_right, of_nat_one, int.one_mul], gsmul_neg' := λ n x, neg_mul_eq_neg_mul_symm (n.succ : ℤ) x } /-! ### Extra instances to short-circuit type class resolution -/ -- instance : has_sub int := by apply_instance -- This is in core instance : add_comm_monoid int := by apply_instance instance : add_monoid int := by apply_instance instance : monoid int := by apply_instance instance : comm_monoid int := by apply_instance instance : comm_semigroup int := by apply_instance instance : semigroup int := by apply_instance instance : add_comm_semigroup int := by apply_instance instance : add_semigroup int := by apply_instance instance : comm_semiring int := by apply_instance instance : semiring int := by apply_instance instance : ring int := by apply_instance instance : distrib int := by apply_instance instance : linear_ordered_comm_ring int := { add_le_add_left := @int.add_le_add_left, mul_pos := @int.mul_pos, zero_le_one := le_of_lt int.zero_lt_one, .. int.comm_ring, .. int.linear_order, .. int.nontrivial } instance : linear_ordered_add_comm_group int := by apply_instance @[simp] lemma add_neg_one (i : ℤ) : i + -1 = i - 1 := rfl theorem abs_eq_nat_abs : ∀ a : ℤ, abs a = nat_abs a | (n : ℕ) := abs_of_nonneg $ coe_zero_le _ | -[1+ n] := abs_of_nonpos $ le_of_lt $ neg_succ_lt_zero _ theorem nat_abs_abs (a : ℤ) : nat_abs (abs a) = nat_abs a := by rw [abs_eq_nat_abs]; refl theorem sign_mul_abs (a : ℤ) : sign a * abs a = a := by rw [abs_eq_nat_abs, sign_mul_nat_abs] @[simp] lemma default_eq_zero : default ℤ = 0 := rfl meta instance : has_to_format ℤ := ⟨λ z, to_string z⟩ meta instance : has_reflect ℤ := by tactic.mk_has_reflect_instance attribute [simp] int.coe_nat_add int.coe_nat_mul int.coe_nat_zero int.coe_nat_one int.coe_nat_succ attribute [simp] int.of_nat_eq_coe int.bodd @[simp] theorem add_def {a b : ℤ} : int.add a b = a + b := rfl @[simp] theorem mul_def {a b : ℤ} : int.mul a b = a * b := rfl @[simp] lemma neg_succ_not_nonneg (n : ℕ) : 0 ≤ -[1+ n] ↔ false := by { simp only [not_le, iff_false], exact int.neg_succ_lt_zero n, } @[simp] lemma neg_succ_not_pos (n : ℕ) : 0 < -[1+ n] ↔ false := by simp only [not_lt, iff_false] @[simp] lemma neg_succ_sub_one (n : ℕ) : -[1+ n] - 1 = -[1+ (n+1)] := rfl @[simp] theorem coe_nat_mul_neg_succ (m n : ℕ) : (m : ℤ) * -[1+ n] = -(m * succ n) := rfl @[simp] theorem neg_succ_mul_coe_nat (m n : ℕ) : -[1+ m] * n = -(succ m * n) := rfl @[simp] theorem neg_succ_mul_neg_succ (m n : ℕ) : -[1+ m] * -[1+ n] = succ m * succ n := rfl @[simp, norm_cast] theorem coe_nat_le {m n : ℕ} : (↑m : ℤ) ≤ ↑n ↔ m ≤ n := coe_nat_le_coe_nat_iff m n @[simp, norm_cast] theorem coe_nat_lt {m n : ℕ} : (↑m : ℤ) < ↑n ↔ m < n := coe_nat_lt_coe_nat_iff m n @[simp, norm_cast] theorem coe_nat_inj' {m n : ℕ} : (↑m : ℤ) = ↑n ↔ m = n := int.coe_nat_eq_coe_nat_iff m n @[simp] theorem coe_nat_pos {n : ℕ} : (0 : ℤ) < n ↔ 0 < n := by rw [← int.coe_nat_zero, coe_nat_lt] @[simp] theorem coe_nat_eq_zero {n : ℕ} : (n : ℤ) = 0 ↔ n = 0 := by rw [← int.coe_nat_zero, coe_nat_inj'] theorem coe_nat_ne_zero {n : ℕ} : (n : ℤ) ≠ 0 ↔ n ≠ 0 := not_congr coe_nat_eq_zero @[simp] lemma coe_nat_nonneg (n : ℕ) : 0 ≤ (n : ℤ) := coe_nat_le.2 (nat.zero_le _) lemma coe_nat_ne_zero_iff_pos {n : ℕ} : (n : ℤ) ≠ 0 ↔ 0 < n := ⟨λ h, nat.pos_of_ne_zero (coe_nat_ne_zero.1 h), λ h, (ne_of_lt (coe_nat_lt.2 h)).symm⟩ lemma coe_nat_succ_pos (n : ℕ) : 0 < (n.succ : ℤ) := int.coe_nat_pos.2 (succ_pos n) @[simp, norm_cast] theorem coe_nat_abs (n : ℕ) : abs (n : ℤ) = n := abs_of_nonneg (coe_nat_nonneg n) /-! ### succ and pred -/ /-- Immediate successor of an integer: `succ n = n + 1` -/ def succ (a : ℤ) := a + 1 /-- Immediate predecessor of an integer: `pred n = n - 1` -/ def pred (a : ℤ) := a - 1 theorem nat_succ_eq_int_succ (n : ℕ) : (nat.succ n : ℤ) = int.succ n := rfl theorem pred_succ (a : ℤ) : pred (succ a) = a := add_sub_cancel _ _ theorem succ_pred (a : ℤ) : succ (pred a) = a := sub_add_cancel _ _ theorem neg_succ (a : ℤ) : -succ a = pred (-a) := neg_add _ _ theorem succ_neg_succ (a : ℤ) : succ (-succ a) = -a := by rw [neg_succ, succ_pred] theorem neg_pred (a : ℤ) : -pred a = succ (-a) := by rw [eq_neg_of_eq_neg (neg_succ (-a)).symm, neg_neg] theorem pred_neg_pred (a : ℤ) : pred (-pred a) = -a := by rw [neg_pred, pred_succ] theorem pred_nat_succ (n : ℕ) : pred (nat.succ n) = n := pred_succ n theorem neg_nat_succ (n : ℕ) : -(nat.succ n : ℤ) = pred (-n) := neg_succ n theorem succ_neg_nat_succ (n : ℕ) : succ (-nat.succ n) = -n := succ_neg_succ n theorem lt_succ_self (a : ℤ) : a < succ a := lt_add_of_pos_right _ zero_lt_one theorem pred_self_lt (a : ℤ) : pred a < a := sub_lt_self _ zero_lt_one theorem add_one_le_iff {a b : ℤ} : a + 1 ≤ b ↔ a < b := iff.rfl theorem lt_add_one_iff {a b : ℤ} : a < b + 1 ↔ a ≤ b := add_le_add_iff_right _ @[simp] lemma succ_coe_nat_pos (n : ℕ) : 0 < (n : ℤ) + 1 := lt_add_one_iff.mpr (by simp) @[norm_cast] lemma coe_pred_of_pos {n : ℕ} (h : 0 < n) : ((n - 1 : ℕ) : ℤ) = (n : ℤ) - 1 := by { cases n, cases h, simp, } lemma le_add_one {a b : ℤ} (h : a ≤ b) : a ≤ b + 1 := le_of_lt (int.lt_add_one_iff.mpr h) theorem sub_one_lt_iff {a b : ℤ} : a - 1 < b ↔ a ≤ b := sub_lt_iff_lt_add.trans lt_add_one_iff theorem le_sub_one_iff {a b : ℤ} : a ≤ b - 1 ↔ a < b := le_sub_iff_add_le @[simp] lemma eq_zero_iff_abs_lt_one {a : ℤ} : abs a < 1 ↔ a = 0 := ⟨λ a0, let ⟨hn, hp⟩ := abs_lt.mp a0 in (le_of_lt_add_one (by exact hp)).antisymm hn, λ a0, (abs_eq_zero.mpr a0).le.trans_lt zero_lt_one⟩ @[elab_as_eliminator] protected lemma induction_on {p : ℤ → Prop} (i : ℤ) (hz : p 0) (hp : ∀ i : ℕ, p i → p (i + 1)) (hn : ∀ i : ℕ, p (-i) → p (-i - 1)) : p i := begin induction i, { induction i, { exact hz }, { exact hp _ i_ih } }, { have : ∀ n:ℕ, p (- n), { intro n, induction n, { simp [hz] }, { convert hn _ n_ih using 1, simp [sub_eq_neg_add] } }, exact this (i + 1) } end /-- Inductively define a function on `ℤ` by defining it at `b`, for the `succ` of a number greater than `b`, and the `pred` of a number less than `b`. -/ protected def induction_on' {C : ℤ → Sort*} (z : ℤ) (b : ℤ) : C b → (∀ k, b ≤ k → C k → C (k + 1)) → (∀ k ≤ b, C k → C (k - 1)) → C z := λ H0 Hs Hp, begin rw ←sub_add_cancel z b, induction (z - b) with n n, { induction n with n ih, { rwa [of_nat_zero, zero_add] }, rw [of_nat_succ, add_assoc, add_comm 1 b, ←add_assoc], exact Hs _ (le_add_of_nonneg_left (of_nat_nonneg _)) ih }, { induction n with n ih, { rw [neg_succ_of_nat_eq, ←of_nat_eq_coe, of_nat_zero, zero_add, neg_add_eq_sub], exact Hp _ (le_refl _) H0 }, { rw [neg_succ_of_nat_coe', nat.succ_eq_add_one, ←neg_succ_of_nat_coe, sub_add_eq_add_sub], exact Hp _ (le_of_lt (add_lt_of_neg_of_le (neg_succ_lt_zero _) (le_refl _))) ih } } end /-! ### nat abs -/ attribute [simp] nat_abs nat_abs_of_nat nat_abs_zero nat_abs_one theorem nat_abs_add_le (a b : ℤ) : nat_abs (a + b) ≤ nat_abs a + nat_abs b := begin have : ∀ (a b : ℕ), nat_abs (sub_nat_nat a (nat.succ b)) ≤ nat.succ (a + b), { refine (λ a b : ℕ, sub_nat_nat_elim a b.succ (λ m n i, n = b.succ → nat_abs i ≤ (m + b).succ) _ _ rfl); intros i n e, { subst e, rw [add_comm _ i, add_assoc], exact nat.le_add_right i (b.succ + b).succ }, { apply succ_le_succ, rw [← succ.inj e, ← add_assoc, add_comm], apply nat.le_add_right } }, cases a; cases b with b b; simp [nat_abs, nat.succ_add]; try {refl}; [skip, rw add_comm a b]; apply this end lemma nat_abs_sub_le (a b : ℤ) : nat_abs (a - b) ≤ nat_abs a + nat_abs b := by { rw [sub_eq_add_neg, ← int.nat_abs_neg b], apply nat_abs_add_le } theorem nat_abs_neg_of_nat (n : ℕ) : nat_abs (neg_of_nat n) = n := by cases n; refl theorem nat_abs_mul (a b : ℤ) : nat_abs (a * b) = (nat_abs a) * (nat_abs b) := by cases a; cases b; simp only [← int.mul_def, int.mul, nat_abs_neg_of_nat, eq_self_iff_true, int.nat_abs] lemma nat_abs_mul_nat_abs_eq {a b : ℤ} {c : ℕ} (h : a * b = (c : ℤ)) : a.nat_abs * b.nat_abs = c := by rw [← nat_abs_mul, h, nat_abs_of_nat] @[simp] lemma nat_abs_mul_self' (a : ℤ) : (nat_abs a * nat_abs a : ℤ) = a * a := by rw [← int.coe_nat_mul, nat_abs_mul_self] theorem neg_succ_of_nat_eq' (m : ℕ) : -[1+ m] = -m - 1 := by simp [neg_succ_of_nat_eq, sub_eq_neg_add] lemma nat_abs_ne_zero_of_ne_zero {z : ℤ} (hz : z ≠ 0) : z.nat_abs ≠ 0 := λ h, hz $ int.eq_zero_of_nat_abs_eq_zero h @[simp] lemma nat_abs_eq_zero {a : ℤ} : a.nat_abs = 0 ↔ a = 0 := ⟨int.eq_zero_of_nat_abs_eq_zero, λ h, h.symm ▸ rfl⟩ lemma nat_abs_ne_zero {a : ℤ} : a.nat_abs ≠ 0 ↔ a ≠ 0 := not_congr int.nat_abs_eq_zero lemma nat_abs_lt_nat_abs_of_nonneg_of_lt {a b : ℤ} (w₁ : 0 ≤ a) (w₂ : a < b) : a.nat_abs < b.nat_abs := begin lift b to ℕ using le_trans w₁ (le_of_lt w₂), lift a to ℕ using w₁, simpa using w₂, end lemma nat_abs_eq_nat_abs_iff {a b : ℤ} : a.nat_abs = b.nat_abs ↔ a = b ∨ a = -b := begin split; intro h, { cases int.nat_abs_eq a with h₁ h₁; cases int.nat_abs_eq b with h₂ h₂; rw [h₁, h₂]; simp [h], }, { cases h; rw h, rw int.nat_abs_neg, }, end lemma nat_abs_eq_iff {a : ℤ} {n : ℕ} : a.nat_abs = n ↔ a = n ∨ a = -n := by rw [←int.nat_abs_eq_nat_abs_iff, int.nat_abs_of_nat] lemma nat_abs_eq_iff_mul_self_eq {a b : ℤ} : a.nat_abs = b.nat_abs ↔ a * a = b * b := begin rw [← abs_eq_iff_mul_self_eq, abs_eq_nat_abs, abs_eq_nat_abs], exact int.coe_nat_inj'.symm end lemma nat_abs_lt_iff_mul_self_lt {a b : ℤ} : a.nat_abs < b.nat_abs ↔ a * a < b * b := begin rw [← abs_lt_iff_mul_self_lt, abs_eq_nat_abs, abs_eq_nat_abs], exact int.coe_nat_lt.symm end lemma nat_abs_le_iff_mul_self_le {a b : ℤ} : a.nat_abs ≤ b.nat_abs ↔ a * a ≤ b * b := begin rw [← abs_le_iff_mul_self_le, abs_eq_nat_abs, abs_eq_nat_abs], exact int.coe_nat_le.symm end lemma nat_abs_eq_iff_sq_eq {a b : ℤ} : a.nat_abs = b.nat_abs ↔ a ^ 2 = b ^ 2 := by { rw [sq, sq], exact nat_abs_eq_iff_mul_self_eq } lemma nat_abs_lt_iff_sq_lt {a b : ℤ} : a.nat_abs < b.nat_abs ↔ a ^ 2 < b ^ 2 := by { rw [sq, sq], exact nat_abs_lt_iff_mul_self_lt } lemma nat_abs_le_iff_sq_le {a b : ℤ} : a.nat_abs ≤ b.nat_abs ↔ a ^ 2 ≤ b ^ 2 := by { rw [sq, sq], exact nat_abs_le_iff_mul_self_le } /-! ### `/` -/ @[simp] theorem of_nat_div (m n : ℕ) : of_nat (m / n) = (of_nat m) / (of_nat n) := rfl @[simp, norm_cast] theorem coe_nat_div (m n : ℕ) : ((m / n : ℕ) : ℤ) = m / n := rfl theorem neg_succ_of_nat_div (m : ℕ) {b : ℤ} (H : 0 < b) : -[1+m] / b = -(m / b + 1) := match b, eq_succ_of_zero_lt H with ._, ⟨n, rfl⟩ := rfl end -- Will be generalized to Euclidean domains. local attribute [simp] protected theorem zero_div : ∀ (b : ℤ), 0 / b = 0 | 0 := show of_nat _ = _, by simp | (n+1:ℕ) := show of_nat _ = _, by simp | -[1+ n] := show -of_nat _ = _, by simp local attribute [simp] -- Will be generalized to Euclidean domains. protected theorem div_zero : ∀ (a : ℤ), a / 0 = 0 | 0 := show of_nat _ = _, by simp | (n+1:ℕ) := show of_nat _ = _, by simp | -[1+ n] := rfl @[simp] protected theorem div_neg : ∀ (a b : ℤ), a / -b = -(a / b) | (m : ℕ) 0 := show of_nat (m / 0) = -(m / 0 : ℕ), by rw nat.div_zero; refl | (m : ℕ) (n+1:ℕ) := rfl | 0 -[1+ n] := by simp | (m+1:ℕ) -[1+ n] := (neg_neg _).symm | -[1+ m] 0 := rfl | -[1+ m] (n+1:ℕ) := rfl | -[1+ m] -[1+ n] := rfl theorem div_of_neg_of_pos {a b : ℤ} (Ha : a < 0) (Hb : 0 < b) : a / b = -((-a - 1) / b + 1) := match a, b, eq_neg_succ_of_lt_zero Ha, eq_succ_of_zero_lt Hb with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩ := by change (- -[1+ m] : ℤ) with (m+1 : ℤ); rw add_sub_cancel; refl end protected theorem div_nonneg {a b : ℤ} (Ha : 0 ≤ a) (Hb : 0 ≤ b) : 0 ≤ a / b := match a, b, eq_coe_of_zero_le Ha, eq_coe_of_zero_le Hb with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩ := coe_zero_le _ end protected theorem div_nonpos {a b : ℤ} (Ha : 0 ≤ a) (Hb : b ≤ 0) : a / b ≤ 0 := nonpos_of_neg_nonneg $ by rw [← int.div_neg]; exact int.div_nonneg Ha (neg_nonneg_of_nonpos Hb) theorem div_neg' {a b : ℤ} (Ha : a < 0) (Hb : 0 < b) : a / b < 0 := match a, b, eq_neg_succ_of_lt_zero Ha, eq_succ_of_zero_lt Hb with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩ := neg_succ_lt_zero _ end @[simp] protected theorem div_one : ∀ (a : ℤ), a / 1 = a | 0 := show of_nat _ = _, by simp | (n+1:ℕ) := congr_arg of_nat (nat.div_one _) | -[1+ n] := congr_arg neg_succ_of_nat (nat.div_one _) theorem div_eq_zero_of_lt {a b : ℤ} (H1 : 0 ≤ a) (H2 : a < b) : a / b = 0 := match a, b, eq_coe_of_zero_le H1, eq_succ_of_zero_lt (lt_of_le_of_lt H1 H2), H2 with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩, H2 := congr_arg of_nat $ nat.div_eq_of_lt $ lt_of_coe_nat_lt_coe_nat H2 end theorem div_eq_zero_of_lt_abs {a b : ℤ} (H1 : 0 ≤ a) (H2 : a < abs b) : a / b = 0 := match b, abs b, abs_eq_nat_abs b, H2 with | (n : ℕ), ._, rfl, H2 := div_eq_zero_of_lt H1 H2 | -[1+ n], ._, rfl, H2 := neg_injective $ by rw [← int.div_neg]; exact div_eq_zero_of_lt H1 H2 end protected theorem add_mul_div_right (a b : ℤ) {c : ℤ} (H : c ≠ 0) : (a + b * c) / c = a / c + b := have ∀ {k n : ℕ} {a : ℤ}, (a + n * k.succ) / k.succ = a / k.succ + n, from λ k n a, match a with | (m : ℕ) := congr_arg of_nat $ nat.add_mul_div_right _ _ k.succ_pos | -[1+ m] := show ((n * k.succ:ℕ) - m.succ : ℤ) / k.succ = n - (m / k.succ + 1 : ℕ), begin cases lt_or_ge m (n*k.succ) with h h, { rw [← int.coe_nat_sub h, ← int.coe_nat_sub ((nat.div_lt_iff_lt_mul _ _ k.succ_pos).2 h)], apply congr_arg of_nat, rw [mul_comm, nat.mul_sub_div], rwa mul_comm }, { change (↑(n * nat.succ k) - (m + 1) : ℤ) / ↑(nat.succ k) = ↑n - ((m / nat.succ k : ℕ) + 1), rw [← sub_sub, ← sub_sub, ← neg_sub (m:ℤ), ← neg_sub _ (n:ℤ), ← int.coe_nat_sub h, ← int.coe_nat_sub ((nat.le_div_iff_mul_le _ _ k.succ_pos).2 h), ← neg_succ_of_nat_coe', ← neg_succ_of_nat_coe'], { apply congr_arg neg_succ_of_nat, rw [mul_comm, nat.sub_mul_div], rwa mul_comm } } end end, have ∀ {a b c : ℤ}, 0 < c → (a + b * c) / c = a / c + b, from λ a b c H, match c, eq_succ_of_zero_lt H, b with | ._, ⟨k, rfl⟩, (n : ℕ) := this | ._, ⟨k, rfl⟩, -[1+ n] := show (a - n.succ * k.succ) / k.succ = (a / k.succ) - n.succ, from eq_sub_of_add_eq $ by rw [← this, sub_add_cancel] end, match lt_trichotomy c 0 with | or.inl hlt := neg_inj.1 $ by rw [← int.div_neg, neg_add, ← int.div_neg, ← neg_mul_neg]; apply this (neg_pos_of_neg hlt) | or.inr (or.inl heq) := absurd heq H | or.inr (or.inr hgt) := this hgt end protected theorem add_mul_div_left (a : ℤ) {b : ℤ} (c : ℤ) (H : b ≠ 0) : (a + b * c) / b = a / b + c := by rw [mul_comm, int.add_mul_div_right _ _ H] protected theorem add_div_of_dvd_right {a b c : ℤ} (H : c ∣ b) : (a + b) / c = a / c + b / c := begin by_cases h1 : c = 0, { simp [h1] }, cases H with k hk, rw hk, change c ≠ 0 at h1, rw [mul_comm c k, int.add_mul_div_right _ _ h1, ←zero_add (k * c), int.add_mul_div_right _ _ h1, int.zero_div, zero_add] end protected theorem add_div_of_dvd_left {a b c : ℤ} (H : c ∣ a) : (a + b) / c = a / c + b / c := by rw [add_comm, int.add_div_of_dvd_right H, add_comm] @[simp] protected theorem mul_div_cancel (a : ℤ) {b : ℤ} (H : b ≠ 0) : a * b / b = a := by have := int.add_mul_div_right 0 a H; rwa [zero_add, int.zero_div, zero_add] at this @[simp] protected theorem mul_div_cancel_left {a : ℤ} (b : ℤ) (H : a ≠ 0) : a * b / a = b := by rw [mul_comm, int.mul_div_cancel _ H] @[simp] protected theorem div_self {a : ℤ} (H : a ≠ 0) : a / a = 1 := by have := int.mul_div_cancel 1 H; rwa one_mul at this /-! ### mod -/ theorem of_nat_mod (m n : nat) : (m % n : ℤ) = of_nat (m % n) := rfl @[simp, norm_cast] theorem coe_nat_mod (m n : ℕ) : (↑(m % n) : ℤ) = ↑m % ↑n := rfl theorem neg_succ_of_nat_mod (m : ℕ) {b : ℤ} (bpos : 0 < b) : -[1+m] % b = b - 1 - m % b := by rw [sub_sub, add_comm]; exact match b, eq_succ_of_zero_lt bpos with ._, ⟨n, rfl⟩ := rfl end @[simp] theorem mod_neg : ∀ (a b : ℤ), a % -b = a % b | (m : ℕ) n := @congr_arg ℕ ℤ _ _ (λ i, ↑(m % i)) (nat_abs_neg _) | -[1+ m] n := @congr_arg ℕ ℤ _ _ (λ i, sub_nat_nat i (nat.succ (m % i))) (nat_abs_neg _) @[simp] theorem mod_abs (a b : ℤ) : a % (abs b) = a % b := abs_by_cases (λ i, a % i = a % b) rfl (mod_neg _ _) local attribute [simp] -- Will be generalized to Euclidean domains. theorem zero_mod (b : ℤ) : 0 % b = 0 := rfl local attribute [simp] -- Will be generalized to Euclidean domains. theorem mod_zero : ∀ (a : ℤ), a % 0 = a | (m : ℕ) := congr_arg of_nat $ nat.mod_zero _ | -[1+ m] := congr_arg neg_succ_of_nat $ nat.mod_zero _ local attribute [simp] -- Will be generalized to Euclidean domains. theorem mod_one : ∀ (a : ℤ), a % 1 = 0 | (m : ℕ) := congr_arg of_nat $ nat.mod_one _ | -[1+ m] := show (1 - (m % 1).succ : ℤ) = 0, by rw nat.mod_one; refl theorem mod_eq_of_lt {a b : ℤ} (H1 : 0 ≤ a) (H2 : a < b) : a % b = a := match a, b, eq_coe_of_zero_le H1, eq_coe_of_zero_le (le_trans H1 (le_of_lt H2)), H2 with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩, H2 := congr_arg of_nat $ nat.mod_eq_of_lt (lt_of_coe_nat_lt_coe_nat H2) end theorem mod_nonneg : ∀ (a : ℤ) {b : ℤ}, b ≠ 0 → 0 ≤ a % b | (m : ℕ) n H := coe_zero_le _ | -[1+ m] n H := sub_nonneg_of_le $ coe_nat_le_coe_nat_of_le $ nat.mod_lt _ (nat_abs_pos_of_ne_zero H) theorem mod_lt_of_pos (a : ℤ) {b : ℤ} (H : 0 < b) : a % b < b := match a, b, eq_succ_of_zero_lt H with | (m : ℕ), ._, ⟨n, rfl⟩ := coe_nat_lt_coe_nat_of_lt (nat.mod_lt _ (nat.succ_pos _)) | -[1+ m], ._, ⟨n, rfl⟩ := sub_lt_self _ (coe_nat_lt_coe_nat_of_lt $ nat.succ_pos _) end theorem mod_lt (a : ℤ) {b : ℤ} (H : b ≠ 0) : a % b < abs b := by rw [← mod_abs]; exact mod_lt_of_pos _ (abs_pos.2 H) theorem mod_add_div_aux (m n : ℕ) : (n - (m % n + 1) - (n * (m / n) + n) : ℤ) = -[1+ m] := begin rw [← sub_sub, neg_succ_of_nat_coe, sub_sub (n:ℤ)], apply eq_neg_of_eq_neg, rw [neg_sub, sub_sub_self, add_right_comm], exact @congr_arg ℕ ℤ _ _ (λi, (i + 1 : ℤ)) (nat.mod_add_div _ _).symm end theorem mod_add_div : ∀ (a b : ℤ), a % b + b * (a / b) = a | (m : ℕ) 0 := congr_arg of_nat (nat.mod_add_div _ _) | (m : ℕ) (n+1:ℕ) := congr_arg of_nat (nat.mod_add_div _ _) | 0 -[1+ n] := by simp | (m+1:ℕ) -[1+ n] := show (_ + -(n+1) * -((m + 1) / (n + 1) : ℕ) : ℤ) = _, by rw [neg_mul_neg]; exact congr_arg of_nat (nat.mod_add_div _ _) | -[1+ m] 0 := by rw [mod_zero, int.div_zero]; refl | -[1+ m] (n+1:ℕ) := mod_add_div_aux m n.succ | -[1+ m] -[1+ n] := mod_add_div_aux m n.succ theorem div_add_mod (a b : ℤ) : b * (a / b) + a % b = a := (add_comm _ _).trans (mod_add_div _ _) lemma mod_add_div' (m k : ℤ) : m % k + (m / k) * k = m := by { rw mul_comm, exact mod_add_div _ _ } lemma div_add_mod' (m k : ℤ) : (m / k) * k + m % k = m := by { rw mul_comm, exact div_add_mod _ _ } theorem mod_def (a b : ℤ) : a % b = a - b * (a / b) := eq_sub_of_add_eq (mod_add_div _ _) @[simp] theorem add_mul_mod_self {a b c : ℤ} : (a + b * c) % c = a % c := if cz : c = 0 then by rw [cz, mul_zero, add_zero] else by rw [mod_def, mod_def, int.add_mul_div_right _ _ cz, mul_add, mul_comm, add_sub_add_right_eq_sub] @[simp] theorem add_mul_mod_self_left (a b c : ℤ) : (a + b * c) % b = a % b := by rw [mul_comm, add_mul_mod_self] @[simp] theorem add_mod_self {a b : ℤ} : (a + b) % b = a % b := by have := add_mul_mod_self_left a b 1; rwa mul_one at this @[simp] theorem add_mod_self_left {a b : ℤ} : (a + b) % a = b % a := by rw [add_comm, add_mod_self] @[simp] theorem mod_add_mod (m n k : ℤ) : (m % n + k) % n = (m + k) % n := by have := (add_mul_mod_self_left (m % n + k) n (m / n)).symm; rwa [add_right_comm, mod_add_div] at this @[simp] theorem add_mod_mod (m n k : ℤ) : (m + n % k) % k = (m + n) % k := by rw [add_comm, mod_add_mod, add_comm] lemma add_mod (a b n : ℤ) : (a + b) % n = ((a % n) + (b % n)) % n := by rw [add_mod_mod, mod_add_mod] theorem add_mod_eq_add_mod_right {m n k : ℤ} (i : ℤ) (H : m % n = k % n) : (m + i) % n = (k + i) % n := by rw [← mod_add_mod, ← mod_add_mod k, H] theorem add_mod_eq_add_mod_left {m n k : ℤ} (i : ℤ) (H : m % n = k % n) : (i + m) % n = (i + k) % n := by rw [add_comm, add_mod_eq_add_mod_right _ H, add_comm] theorem mod_add_cancel_right {m n k : ℤ} (i) : (m + i) % n = (k + i) % n ↔ m % n = k % n := ⟨λ H, by have := add_mod_eq_add_mod_right (-i) H; rwa [add_neg_cancel_right, add_neg_cancel_right] at this, add_mod_eq_add_mod_right _⟩ theorem mod_add_cancel_left {m n k i : ℤ} : (i + m) % n = (i + k) % n ↔ m % n = k % n := by rw [add_comm, add_comm i, mod_add_cancel_right] theorem mod_sub_cancel_right {m n k : ℤ} (i) : (m - i) % n = (k - i) % n ↔ m % n = k % n := mod_add_cancel_right _ theorem mod_eq_mod_iff_mod_sub_eq_zero {m n k : ℤ} : m % n = k % n ↔ (m - k) % n = 0 := (mod_sub_cancel_right k).symm.trans $ by simp @[simp] theorem mul_mod_left (a b : ℤ) : (a * b) % b = 0 := by rw [← zero_add (a * b), add_mul_mod_self, zero_mod] @[simp] theorem mul_mod_right (a b : ℤ) : (a * b) % a = 0 := by rw [mul_comm, mul_mod_left] lemma mul_mod (a b n : ℤ) : (a * b) % n = ((a % n) * (b % n)) % n := begin conv_lhs { rw [←mod_add_div a n, ←mod_add_div' b n, right_distrib, left_distrib, left_distrib, mul_assoc, mul_assoc, ←left_distrib n _ _, add_mul_mod_self_left, ← mul_assoc, add_mul_mod_self] } end @[simp] lemma neg_mod_two (i : ℤ) : (-i) % 2 = i % 2 := begin apply int.mod_eq_mod_iff_mod_sub_eq_zero.mpr, convert int.mul_mod_right 2 (-i), simp only [two_mul, sub_eq_add_neg] end local attribute [simp] -- Will be generalized to Euclidean domains. theorem mod_self {a : ℤ} : a % a = 0 := by have := mul_mod_left 1 a; rwa one_mul at this @[simp] theorem mod_mod_of_dvd (n : ℤ) {m k : ℤ} (h : m ∣ k) : n % k % m = n % m := begin conv { to_rhs, rw ←mod_add_div n k }, rcases h with ⟨t, rfl⟩, rw [mul_assoc, add_mul_mod_self_left] end @[simp] theorem mod_mod (a b : ℤ) : a % b % b = a % b := by conv {to_rhs, rw [← mod_add_div a b, add_mul_mod_self_left]} lemma sub_mod (a b n : ℤ) : (a - b) % n = ((a % n) - (b % n)) % n := begin apply (mod_add_cancel_right b).mp, rw [sub_add_cancel, ← add_mod_mod, sub_add_cancel, mod_mod] end /-! ### properties of `/` and `%` -/ @[simp] theorem mul_div_mul_of_pos {a : ℤ} (b c : ℤ) (H : 0 < a) : a * b / (a * c) = b / c := suffices ∀ (m k : ℕ) (b : ℤ), (m.succ * b / (m.succ * k) : ℤ) = b / k, from match a, eq_succ_of_zero_lt H, c, eq_coe_or_neg c with | ._, ⟨m, rfl⟩, ._, ⟨k, or.inl rfl⟩ := this _ _ _ | ._, ⟨m, rfl⟩, ._, ⟨k, or.inr rfl⟩ := by rw [← neg_mul_eq_mul_neg, int.div_neg, int.div_neg]; apply congr_arg has_neg.neg; apply this end, λ m k b, match b, k with | (n : ℕ), k := congr_arg of_nat (nat.mul_div_mul _ _ m.succ_pos) | -[1+ n], 0 := by rw [int.coe_nat_zero, mul_zero, int.div_zero, int.div_zero] | -[1+ n], k+1 := congr_arg neg_succ_of_nat $ show (m.succ * n + m) / (m.succ * k.succ) = n / k.succ, begin apply nat.div_eq_of_lt_le, { refine le_trans _ (nat.le_add_right _ _), rw [← nat.mul_div_mul _ _ m.succ_pos], apply nat.div_mul_le_self }, { change m.succ * n.succ ≤ _, rw [mul_left_comm], apply nat.mul_le_mul_left, apply (nat.div_lt_iff_lt_mul _ _ k.succ_pos).1, apply nat.lt_succ_self } end end @[simp] theorem mul_div_mul_of_pos_left (a : ℤ) {b : ℤ} (H : 0 < b) (c : ℤ) : a * b / (c * b) = a / c := by rw [mul_comm, mul_comm c, mul_div_mul_of_pos _ _ H] @[simp] theorem mul_mod_mul_of_pos {a : ℤ} (H : 0 < a) (b c : ℤ) : a * b % (a * c) = a * (b % c) := by rw [mod_def, mod_def, mul_div_mul_of_pos _ _ H, mul_sub_left_distrib, mul_assoc] theorem lt_div_add_one_mul_self (a : ℤ) {b : ℤ} (H : 0 < b) : a < (a / b + 1) * b := by { rw [add_mul, one_mul, mul_comm, ← sub_lt_iff_lt_add', ← mod_def], exact mod_lt_of_pos _ H } theorem abs_div_le_abs : ∀ (a b : ℤ), abs (a / b) ≤ abs a := suffices ∀ (a : ℤ) (n : ℕ), abs (a / n) ≤ abs a, from λ a b, match b, eq_coe_or_neg b with | ._, ⟨n, or.inl rfl⟩ := this _ _ | ._, ⟨n, or.inr rfl⟩ := by rw [int.div_neg, abs_neg]; apply this end, λ a n, by rw [abs_eq_nat_abs, abs_eq_nat_abs]; exact coe_nat_le_coe_nat_of_le (match a, n with | (m : ℕ), n := nat.div_le_self _ _ | -[1+ m], 0 := nat.zero_le _ | -[1+ m], n+1 := nat.succ_le_succ (nat.div_le_self _ _) end) theorem div_le_self {a : ℤ} (b : ℤ) (Ha : 0 ≤ a) : a / b ≤ a := by have := le_trans (le_abs_self _) (abs_div_le_abs a b); rwa [abs_of_nonneg Ha] at this theorem mul_div_cancel_of_mod_eq_zero {a b : ℤ} (H : a % b = 0) : b * (a / b) = a := by have := mod_add_div a b; rwa [H, zero_add] at this theorem div_mul_cancel_of_mod_eq_zero {a b : ℤ} (H : a % b = 0) : a / b * b = a := by rw [mul_comm, mul_div_cancel_of_mod_eq_zero H] lemma mod_two_eq_zero_or_one (n : ℤ) : n % 2 = 0 ∨ n % 2 = 1 := have h : n % 2 < 2 := abs_of_nonneg (show 0 ≤ (2 : ℤ), from dec_trivial) ▸ int.mod_lt _ dec_trivial, have h₁ : 0 ≤ n % 2 := int.mod_nonneg _ dec_trivial, match (n % 2), h, h₁ with | (0 : ℕ) := λ _ _, or.inl rfl | (1 : ℕ) := λ _ _, or.inr rfl | (k + 2 : ℕ) := λ h _, absurd h dec_trivial | -[1+ a] := λ _ h₁, absurd h₁ dec_trivial end /-! ### dvd -/ @[norm_cast] theorem coe_nat_dvd {m n : ℕ} : (↑m : ℤ) ∣ ↑n ↔ m ∣ n := ⟨λ ⟨a, ae⟩, m.eq_zero_or_pos.elim (λm0, by simp [m0] at ae; simp [ae, m0]) (λm0l, by { cases eq_coe_of_zero_le (@nonneg_of_mul_nonneg_left ℤ _ m a (by simp [ae.symm]) (by simpa using m0l)) with k e, subst a, exact ⟨k, int.coe_nat_inj ae⟩ }), λ ⟨k, e⟩, dvd.intro k $ by rw [e, int.coe_nat_mul]⟩ theorem coe_nat_dvd_left {n : ℕ} {z : ℤ} : (↑n : ℤ) ∣ z ↔ n ∣ z.nat_abs := by rcases nat_abs_eq z with eq | eq; rw eq; simp [coe_nat_dvd] theorem coe_nat_dvd_right {n : ℕ} {z : ℤ} : z ∣ (↑n : ℤ) ↔ z.nat_abs ∣ n := by rcases nat_abs_eq z with eq | eq; rw eq; simp [coe_nat_dvd] theorem dvd_antisymm {a b : ℤ} (H1 : 0 ≤ a) (H2 : 0 ≤ b) : a ∣ b → b ∣ a → a = b := begin rw [← abs_of_nonneg H1, ← abs_of_nonneg H2, abs_eq_nat_abs, abs_eq_nat_abs], rw [coe_nat_dvd, coe_nat_dvd, coe_nat_inj'], apply nat.dvd_antisymm end theorem dvd_of_mod_eq_zero {a b : ℤ} (H : b % a = 0) : a ∣ b := ⟨b / a, (mul_div_cancel_of_mod_eq_zero H).symm⟩ theorem mod_eq_zero_of_dvd : ∀ {a b : ℤ}, a ∣ b → b % a = 0 | a ._ ⟨c, rfl⟩ := mul_mod_right _ _ theorem dvd_iff_mod_eq_zero (a b : ℤ) : a ∣ b ↔ b % a = 0 := ⟨mod_eq_zero_of_dvd, dvd_of_mod_eq_zero⟩ /-- If `a % b = c` then `b` divides `a - c`. -/ lemma dvd_sub_of_mod_eq {a b c : ℤ} (h : a % b = c) : b ∣ a - c := begin have hx : a % b % b = c % b, { rw h }, rw [mod_mod, ←mod_sub_cancel_right c, sub_self, zero_mod] at hx, exact dvd_of_mod_eq_zero hx end theorem nat_abs_dvd {a b : ℤ} : (a.nat_abs : ℤ) ∣ b ↔ a ∣ b := (nat_abs_eq a).elim (λ e, by rw ← e) (λ e, by rw [← neg_dvd, ← e]) theorem dvd_nat_abs {a b : ℤ} : a ∣ b.nat_abs ↔ a ∣ b := (nat_abs_eq b).elim (λ e, by rw ← e) (λ e, by rw [← dvd_neg, ← e]) instance decidable_dvd : @decidable_rel ℤ (∣) := assume a n, decidable_of_decidable_of_iff (by apply_instance) (dvd_iff_mod_eq_zero _ _).symm protected theorem div_mul_cancel {a b : ℤ} (H : b ∣ a) : a / b * b = a := div_mul_cancel_of_mod_eq_zero (mod_eq_zero_of_dvd H) protected theorem mul_div_cancel' {a b : ℤ} (H : a ∣ b) : a * (b / a) = b := by rw [mul_comm, int.div_mul_cancel H] protected theorem mul_div_assoc (a : ℤ) : ∀ {b c : ℤ}, c ∣ b → (a * b) / c = a * (b / c) | ._ c ⟨d, rfl⟩ := if cz : c = 0 then by simp [cz] else by rw [mul_left_comm, int.mul_div_cancel_left _ cz, int.mul_div_cancel_left _ cz] protected theorem mul_div_assoc' (b : ℤ) {a c : ℤ} (h : c ∣ a) : a * b / c = a / c * b := by rw [mul_comm, int.mul_div_assoc _ h, mul_comm] theorem div_dvd_div : ∀ {a b c : ℤ} (H1 : a ∣ b) (H2 : b ∣ c), b / a ∣ c / a | a ._ ._ ⟨b, rfl⟩ ⟨c, rfl⟩ := if az : a = 0 then by simp [az] else by rw [int.mul_div_cancel_left _ az, mul_assoc, int.mul_div_cancel_left _ az]; apply dvd_mul_right protected theorem eq_mul_of_div_eq_right {a b c : ℤ} (H1 : b ∣ a) (H2 : a / b = c) : a = b * c := by rw [← H2, int.mul_div_cancel' H1] protected theorem div_eq_of_eq_mul_right {a b c : ℤ} (H1 : b ≠ 0) (H2 : a = b * c) : a / b = c := by rw [H2, int.mul_div_cancel_left _ H1] protected theorem eq_div_of_mul_eq_right {a b c : ℤ} (H1 : a ≠ 0) (H2 : a * b = c) : b = c / a := eq.symm $ int.div_eq_of_eq_mul_right H1 H2.symm protected theorem div_eq_iff_eq_mul_right {a b c : ℤ} (H : b ≠ 0) (H' : b ∣ a) : a / b = c ↔ a = b * c := ⟨int.eq_mul_of_div_eq_right H', int.div_eq_of_eq_mul_right H⟩ protected theorem div_eq_iff_eq_mul_left {a b c : ℤ} (H : b ≠ 0) (H' : b ∣ a) : a / b = c ↔ a = c * b := by rw mul_comm; exact int.div_eq_iff_eq_mul_right H H' protected theorem eq_mul_of_div_eq_left {a b c : ℤ} (H1 : b ∣ a) (H2 : a / b = c) : a = c * b := by rw [mul_comm, int.eq_mul_of_div_eq_right H1 H2] protected theorem div_eq_of_eq_mul_left {a b c : ℤ} (H1 : b ≠ 0) (H2 : a = c * b) : a / b = c := int.div_eq_of_eq_mul_right H1 (by rw [mul_comm, H2]) protected lemma eq_zero_of_div_eq_zero {d n : ℤ} (h : d ∣ n) (H : n / d = 0) : n = 0 := by rw [← int.mul_div_cancel' h, H, mul_zero] theorem neg_div_of_dvd : ∀ {a b : ℤ} (H : b ∣ a), -a / b = -(a / b) | ._ b ⟨c, rfl⟩ := if bz : b = 0 then by simp [bz] else by rw [neg_mul_eq_mul_neg, int.mul_div_cancel_left _ bz, int.mul_div_cancel_left _ bz] lemma sub_div_of_dvd (a : ℤ) {b c : ℤ} (hcb : c ∣ b) : (a - b) / c = a / c - b / c := begin rw [sub_eq_add_neg, sub_eq_add_neg, int.add_div_of_dvd_right ((dvd_neg c b).mpr hcb)], congr, exact neg_div_of_dvd hcb, end lemma sub_div_of_dvd_sub {a b c : ℤ} (hcab : c ∣ (a - b)) : (a - b) / c = a / c - b / c := by rw [eq_sub_iff_add_eq, ← int.add_div_of_dvd_left hcab, sub_add_cancel] theorem div_sign : ∀ a b, a / sign b = a * sign b | a (n+1:ℕ) := by unfold sign; simp | a 0 := by simp [sign] | a -[1+ n] := by simp [sign] @[simp] theorem sign_mul : ∀ a b, sign (a * b) = sign a * sign b | a 0 := by simp | 0 b := by simp | (m+1:ℕ) (n+1:ℕ) := rfl | (m+1:ℕ) -[1+ n] := rfl | -[1+ m] (n+1:ℕ) := rfl | -[1+ m] -[1+ n] := rfl protected theorem sign_eq_div_abs (a : ℤ) : sign a = a / (abs a) := if az : a = 0 then by simp [az] else (int.div_eq_of_eq_mul_left (mt abs_eq_zero.1 az) (sign_mul_abs _).symm).symm theorem mul_sign : ∀ (i : ℤ), i * sign i = nat_abs i | (n+1:ℕ) := mul_one _ | 0 := mul_zero _ | -[1+ n] := mul_neg_one _ @[simp] theorem sign_pow_bit1 (k : ℕ) : ∀ n : ℤ, n.sign ^ (bit1 k) = n.sign | (n+1:ℕ) := one_pow (bit1 k) | 0 := zero_pow (nat.zero_lt_bit1 k) | -[1+ n] := (neg_pow_bit1 1 k).trans (congr_arg (λ x, -x) (one_pow (bit1 k))) theorem le_of_dvd {a b : ℤ} (bpos : 0 < b) (H : a ∣ b) : a ≤ b := match a, b, eq_succ_of_zero_lt bpos, H with | (m : ℕ), ._, ⟨n, rfl⟩, H := coe_nat_le_coe_nat_of_le $ nat.le_of_dvd n.succ_pos $ coe_nat_dvd.1 H | -[1+ m], ._, ⟨n, rfl⟩, _ := le_trans (le_of_lt $ neg_succ_lt_zero _) (coe_zero_le _) end theorem eq_one_of_dvd_one {a : ℤ} (H : 0 ≤ a) (H' : a ∣ 1) : a = 1 := match a, eq_coe_of_zero_le H, H' with | ._, ⟨n, rfl⟩, H' := congr_arg coe $ nat.eq_one_of_dvd_one $ coe_nat_dvd.1 H' end theorem eq_one_of_mul_eq_one_right {a b : ℤ} (H : 0 ≤ a) (H' : a * b = 1) : a = 1 := eq_one_of_dvd_one H ⟨b, H'.symm⟩ theorem eq_one_of_mul_eq_one_left {a b : ℤ} (H : 0 ≤ b) (H' : a * b = 1) : b = 1 := eq_one_of_mul_eq_one_right H (by rw [mul_comm, H']) lemma of_nat_dvd_of_dvd_nat_abs {a : ℕ} : ∀ {z : ℤ} (haz : a ∣ z.nat_abs), ↑a ∣ z | (int.of_nat _) haz := int.coe_nat_dvd.2 haz | -[1+k] haz := begin change ↑a ∣ -(k+1 : ℤ), apply dvd_neg_of_dvd, apply int.coe_nat_dvd.2, exact haz end lemma dvd_nat_abs_of_of_nat_dvd {a : ℕ} : ∀ {z : ℤ} (haz : ↑a ∣ z), a ∣ z.nat_abs | (int.of_nat _) haz := int.coe_nat_dvd.1 (int.dvd_nat_abs.2 haz) | -[1+k] haz := have haz' : (↑a:ℤ) ∣ (↑(k+1):ℤ), from dvd_of_dvd_neg haz, int.coe_nat_dvd.1 haz' lemma pow_dvd_of_le_of_pow_dvd {p m n : ℕ} {k : ℤ} (hmn : m ≤ n) (hdiv : ↑(p ^ n) ∣ k) : ↑(p ^ m) ∣ k := begin induction k, { apply int.coe_nat_dvd.2, apply pow_dvd_of_le_of_pow_dvd hmn, apply int.coe_nat_dvd.1 hdiv }, change -[1+k] with -(↑(k+1) : ℤ), apply dvd_neg_of_dvd, apply int.coe_nat_dvd.2, apply pow_dvd_of_le_of_pow_dvd hmn, apply int.coe_nat_dvd.1, apply dvd_of_dvd_neg, exact hdiv, end lemma dvd_of_pow_dvd {p k : ℕ} {m : ℤ} (hk : 1 ≤ k) (hpk : ↑(p^k) ∣ m) : ↑p ∣ m := by rw ←pow_one p; exact pow_dvd_of_le_of_pow_dvd hk hpk /-- If `n > 0` then `m` is not divisible by `n` iff it is between `n * k` and `n * (k + 1)` for some `k`. -/ lemma exists_lt_and_lt_iff_not_dvd (m : ℤ) {n : ℤ} (hn : 0 < n) : (∃ k, n * k < m ∧ m < n * (k + 1)) ↔ ¬ n ∣ m := begin split, { rintro ⟨k, h1k, h2k⟩ ⟨l, rfl⟩, rw [mul_lt_mul_left hn] at h1k h2k, rw [lt_add_one_iff, ← not_lt] at h2k, exact h2k h1k }, { intro h, rw [dvd_iff_mod_eq_zero, ← ne.def] at h, have := (mod_nonneg m hn.ne.symm).lt_of_ne h.symm, simp only [← mod_add_div m n] {single_pass := tt}, refine ⟨m / n, lt_add_of_pos_left _ this, _⟩, rw [add_comm _ (1 : ℤ), left_distrib, mul_one], exact add_lt_add_right (mod_lt_of_pos _ hn) _ } end /-! ### `/` and ordering -/ protected theorem div_mul_le (a : ℤ) {b : ℤ} (H : b ≠ 0) : a / b * b ≤ a := le_of_sub_nonneg $ by rw [mul_comm, ← mod_def]; apply mod_nonneg _ H protected theorem div_le_of_le_mul {a b c : ℤ} (H : 0 < c) (H' : a ≤ b * c) : a / c ≤ b := le_of_mul_le_mul_right (le_trans (int.div_mul_le _ (ne_of_gt H)) H') H protected theorem mul_lt_of_lt_div {a b c : ℤ} (H : 0 < c) (H3 : a < b / c) : a * c < b := lt_of_not_ge $ mt (int.div_le_of_le_mul H) (not_le_of_gt H3) protected theorem mul_le_of_le_div {a b c : ℤ} (H1 : 0 < c) (H2 : a ≤ b / c) : a * c ≤ b := le_trans (decidable.mul_le_mul_of_nonneg_right H2 (le_of_lt H1)) (int.div_mul_le _ (ne_of_gt H1)) protected theorem le_div_of_mul_le {a b c : ℤ} (H1 : 0 < c) (H2 : a * c ≤ b) : a ≤ b / c := le_of_lt_add_one $ lt_of_mul_lt_mul_right (lt_of_le_of_lt H2 (lt_div_add_one_mul_self _ H1)) (le_of_lt H1) protected theorem le_div_iff_mul_le {a b c : ℤ} (H : 0 < c) : a ≤ b / c ↔ a * c ≤ b := ⟨int.mul_le_of_le_div H, int.le_div_of_mul_le H⟩ protected theorem div_le_div {a b c : ℤ} (H : 0 < c) (H' : a ≤ b) : a / c ≤ b / c := int.le_div_of_mul_le H (le_trans (int.div_mul_le _ (ne_of_gt H)) H') protected theorem div_lt_of_lt_mul {a b c : ℤ} (H : 0 < c) (H' : a < b * c) : a / c < b := lt_of_not_ge $ mt (int.mul_le_of_le_div H) (not_le_of_gt H') protected theorem lt_mul_of_div_lt {a b c : ℤ} (H1 : 0 < c) (H2 : a / c < b) : a < b * c := lt_of_not_ge $ mt (int.le_div_of_mul_le H1) (not_le_of_gt H2) protected theorem div_lt_iff_lt_mul {a b c : ℤ} (H : 0 < c) : a / c < b ↔ a < b * c := ⟨int.lt_mul_of_div_lt H, int.div_lt_of_lt_mul H⟩ protected theorem le_mul_of_div_le {a b c : ℤ} (H1 : 0 ≤ b) (H2 : b ∣ a) (H3 : a / b ≤ c) : a ≤ c * b := by rw [← int.div_mul_cancel H2]; exact decidable.mul_le_mul_of_nonneg_right H3 H1 protected theorem lt_div_of_mul_lt {a b c : ℤ} (H1 : 0 ≤ b) (H2 : b ∣ c) (H3 : a * b < c) : a < c / b := lt_of_not_ge $ mt (int.le_mul_of_div_le H1 H2) (not_le_of_gt H3) protected theorem lt_div_iff_mul_lt {a b : ℤ} (c : ℤ) (H : 0 < c) (H' : c ∣ b) : a < b / c ↔ a * c < b := ⟨int.mul_lt_of_lt_div H, int.lt_div_of_mul_lt (le_of_lt H) H'⟩ theorem div_pos_of_pos_of_dvd {a b : ℤ} (H1 : 0 < a) (H2 : 0 ≤ b) (H3 : b ∣ a) : 0 < a / b := int.lt_div_of_mul_lt H2 H3 (by rwa zero_mul) theorem div_eq_div_of_mul_eq_mul {a b c d : ℤ} (H2 : d ∣ c) (H3 : b ≠ 0) (H4 : d ≠ 0) (H5 : a * d = b * c) : a / b = c / d := int.div_eq_of_eq_mul_right H3 $ by rw [← int.mul_div_assoc _ H2]; exact (int.div_eq_of_eq_mul_left H4 H5.symm).symm theorem eq_mul_div_of_mul_eq_mul_of_dvd_left {a b c d : ℤ} (hb : b ≠ 0) (hbc : b ∣ c) (h : b * a = c * d) : a = c / b * d := begin cases hbc with k hk, subst hk, rw [int.mul_div_cancel_left _ hb], rw mul_assoc at h, apply mul_left_cancel' hb h end /-- If an integer with larger absolute value divides an integer, it is zero. -/ lemma eq_zero_of_dvd_of_nat_abs_lt_nat_abs {a b : ℤ} (w : a ∣ b) (h : nat_abs b < nat_abs a) : b = 0 := begin rw [←nat_abs_dvd, ←dvd_nat_abs, coe_nat_dvd] at w, rw ←nat_abs_eq_zero, exact eq_zero_of_dvd_of_lt w h end lemma eq_zero_of_dvd_of_nonneg_of_lt {a b : ℤ} (w₁ : 0 ≤ a) (w₂ : a < b) (h : b ∣ a) : a = 0 := eq_zero_of_dvd_of_nat_abs_lt_nat_abs h (nat_abs_lt_nat_abs_of_nonneg_of_lt w₁ w₂) /-- If two integers are congruent to a sufficiently large modulus, they are equal. -/ lemma eq_of_mod_eq_of_nat_abs_sub_lt_nat_abs {a b c : ℤ} (h1 : a % b = c) (h2 : nat_abs (a - c) < nat_abs b) : a = c := eq_of_sub_eq_zero (eq_zero_of_dvd_of_nat_abs_lt_nat_abs (dvd_sub_of_mod_eq h1) h2) theorem of_nat_add_neg_succ_of_nat_of_lt {m n : ℕ} (h : m < n.succ) : of_nat m + -[1+n] = -[1+ n - m] := begin change sub_nat_nat _ _ = _, have h' : n.succ - m = (n - m).succ, apply succ_sub, apply le_of_lt_succ h, simp [*, sub_nat_nat] end theorem of_nat_add_neg_succ_of_nat_of_ge {m n : ℕ} (h : n.succ ≤ m) : of_nat m + -[1+n] = of_nat (m - n.succ) := begin change sub_nat_nat _ _ = _, have h' : n.succ - m = 0, apply nat.sub_eq_zero_of_le h, simp [*, sub_nat_nat] end @[simp] theorem neg_add_neg (m n : ℕ) : -[1+m] + -[1+n] = -[1+nat.succ(m+n)] := rfl /-! ### to_nat -/ theorem to_nat_eq_max : ∀ (a : ℤ), (to_nat a : ℤ) = max a 0 | (n : ℕ) := (max_eq_left (coe_zero_le n)).symm | -[1+ n] := (max_eq_right (le_of_lt (neg_succ_lt_zero n))).symm @[simp] lemma to_nat_zero : (0 : ℤ).to_nat = 0 := rfl @[simp] lemma to_nat_one : (1 : ℤ).to_nat = 1 := rfl @[simp] theorem to_nat_of_nonneg {a : ℤ} (h : 0 ≤ a) : (to_nat a : ℤ) = a := by rw [to_nat_eq_max, max_eq_left h] @[simp] lemma to_nat_sub_of_le {a b : ℤ} (h : b ≤ a) : (to_nat (a - b) : ℤ) = a - b := int.to_nat_of_nonneg (sub_nonneg_of_le h) @[simp] theorem to_nat_coe_nat (n : ℕ) : to_nat ↑n = n := rfl @[simp] lemma to_nat_coe_nat_add_one {n : ℕ} : ((n : ℤ) + 1).to_nat = n + 1 := rfl theorem le_to_nat (a : ℤ) : a ≤ to_nat a := by rw [to_nat_eq_max]; apply le_max_left @[simp] theorem to_nat_le {a : ℤ} {n : ℕ} : to_nat a ≤ n ↔ a ≤ n := by rw [(coe_nat_le_coe_nat_iff _ _).symm, to_nat_eq_max, max_le_iff]; exact and_iff_left (coe_zero_le _) @[simp] theorem lt_to_nat {n : ℕ} {a : ℤ} : n < to_nat a ↔ (n : ℤ) < a := le_iff_le_iff_lt_iff_lt.1 to_nat_le theorem to_nat_le_to_nat {a b : ℤ} (h : a ≤ b) : to_nat a ≤ to_nat b := by rw to_nat_le; exact le_trans h (le_to_nat b) theorem to_nat_lt_to_nat {a b : ℤ} (hb : 0 < b) : to_nat a < to_nat b ↔ a < b := ⟨λ h, begin cases a, exact lt_to_nat.1 h, exact lt_trans (neg_succ_of_nat_lt_zero a) hb, end, λ h, begin rw lt_to_nat, cases a, exact h, exact hb end⟩ theorem lt_of_to_nat_lt {a b : ℤ} (h : to_nat a < to_nat b) : a < b := (to_nat_lt_to_nat $ lt_to_nat.1 $ lt_of_le_of_lt (nat.zero_le _) h).1 h lemma to_nat_add {a b : ℤ} (ha : 0 ≤ a) (hb : 0 ≤ b) : (a + b).to_nat = a.to_nat + b.to_nat := begin lift a to ℕ using ha, lift b to ℕ using hb, norm_cast, end lemma to_nat_add_nat {a : ℤ} (ha : 0 ≤ a) (n : ℕ) : (a + n).to_nat = a.to_nat + n := begin lift a to ℕ using ha, norm_cast, end @[simp] lemma pred_to_nat : ∀ (i : ℤ), (i - 1).to_nat = i.to_nat - 1 | (0:ℕ) := rfl | (n+1:ℕ) := by simp | -[1+ n] := rfl @[simp] lemma to_nat_pred_coe_of_pos {i : ℤ} (h : 0 < i) : ((i.to_nat - 1 : ℕ) : ℤ) = i - 1 := by simp [h, le_of_lt h] with push_cast @[simp] lemma to_nat_sub_to_nat_neg : ∀ (n : ℤ), ↑n.to_nat - ↑((-n).to_nat) = n | (0 : ℕ) := rfl | (n+1 : ℕ) := show ↑(n+1) - (0:ℤ) = n+1, from sub_zero _ | -[1+ n] := show 0 - (n+1 : ℤ) = _, from zero_sub _ @[simp] lemma to_nat_add_to_nat_neg_eq_nat_abs : ∀ (n : ℤ), (n.to_nat) + ((-n).to_nat) = n.nat_abs | (0 : ℕ) := rfl | (n+1 : ℕ) := show (n+1) + 0 = n+1, from add_zero _ | -[1+ n] := show 0 + (n+1) = n+1, from zero_add _ /-- If `n : ℕ`, then `int.to_nat' n = some n`, if `n : ℤ` is negative, then `int.to_nat' n = none`. -/ def to_nat' : ℤ → option ℕ | (n : ℕ) := some n | -[1+ n] := none theorem mem_to_nat' : ∀ (a : ℤ) (n : ℕ), n ∈ to_nat' a ↔ a = n | (m : ℕ) n := option.some_inj.trans coe_nat_inj'.symm | -[1+ m] n := by split; intro h; cases h lemma to_nat_of_nonpos : ∀ {z : ℤ}, z ≤ 0 → z.to_nat = 0 | (0 : ℕ) := λ _, rfl | (n + 1 : ℕ) := λ h, (h.not_lt (by { exact_mod_cast nat.succ_pos n })).elim | (-[1+ n]) := λ _, rfl /-! ### units -/ @[simp] theorem units_nat_abs (u : units ℤ) : nat_abs u = 1 := units.ext_iff.1 $ nat.units_eq_one ⟨nat_abs u, nat_abs ↑u⁻¹, by rw [← nat_abs_mul, units.mul_inv]; refl, by rw [← nat_abs_mul, units.inv_mul]; refl⟩ theorem units_eq_one_or (u : units ℤ) : u = 1 ∨ u = -1 := by simpa only [units.ext_iff, units_nat_abs] using nat_abs_eq u lemma is_unit_eq_one_or {a : ℤ} : is_unit a → a = 1 ∨ a = -1 | ⟨x, hx⟩ := hx ▸ (units_eq_one_or _).imp (congr_arg coe) (congr_arg coe) lemma is_unit_iff {a : ℤ} : is_unit a ↔ a = 1 ∨ a = -1 := begin refine ⟨λ h, is_unit_eq_one_or h, λ h, _⟩, rcases h with rfl | rfl, { exact is_unit_one }, { exact is_unit_one.neg } end theorem is_unit_iff_nat_abs_eq {n : ℤ} : is_unit n ↔ n.nat_abs = 1 := by simp [nat_abs_eq_iff, is_unit_iff] lemma units_inv_eq_self (u : units ℤ) : u⁻¹ = u := (units_eq_one_or u).elim (λ h, h.symm ▸ rfl) (λ h, h.symm ▸ rfl) @[simp] lemma units_mul_self (u : units ℤ) : u * u = 1 := (units_eq_one_or u).elim (λ h, h.symm ▸ rfl) (λ h, h.symm ▸ rfl) -- `units.coe_mul` is a "wrong turn" for the simplifier, this undoes it and simplifies further @[simp] lemma units_coe_mul_self (u : units ℤ) : (u * u : ℤ) = 1 := by rw [←units.coe_mul, units_mul_self, units.coe_one] @[simp] lemma neg_one_pow_ne_zero {n : ℕ} : (-1 : ℤ)^n ≠ 0 := pow_ne_zero _ (abs_pos.mp trivial) /-! ### bitwise ops -/ @[simp] lemma bodd_zero : bodd 0 = ff := rfl @[simp] lemma bodd_one : bodd 1 = tt := rfl lemma bodd_two : bodd 2 = ff := rfl @[simp, norm_cast] lemma bodd_coe (n : ℕ) : int.bodd n = nat.bodd n := rfl @[simp] lemma bodd_sub_nat_nat (m n : ℕ) : bodd (sub_nat_nat m n) = bxor m.bodd n.bodd := by apply sub_nat_nat_elim m n (λ m n i, bodd i = bxor m.bodd n.bodd); intros; simp; cases i.bodd; simp @[simp] lemma bodd_neg_of_nat (n : ℕ) : bodd (neg_of_nat n) = n.bodd := by cases n; simp; refl @[simp] lemma bodd_neg (n : ℤ) : bodd (-n) = bodd n := by cases n; simp [has_neg.neg, int.coe_nat_eq, int.neg, bodd, -of_nat_eq_coe] @[simp] lemma bodd_add (m n : ℤ) : bodd (m + n) = bxor (bodd m) (bodd n) := by cases m with m m; cases n with n n; unfold has_add.add; simp [int.add, -of_nat_eq_coe, bool.bxor_comm] @[simp] lemma bodd_mul (m n : ℤ) : bodd (m * n) = bodd m && bodd n := by cases m with m m; cases n with n n; simp [← int.mul_def, int.mul, -of_nat_eq_coe, bool.bxor_comm] theorem bodd_add_div2 : ∀ n, cond (bodd n) 1 0 + 2 * div2 n = n | (n : ℕ) := by rw [show (cond (bodd n) 1 0 : ℤ) = (cond (bodd n) 1 0 : ℕ), by cases bodd n; refl]; exact congr_arg of_nat n.bodd_add_div2 | -[1+ n] := begin refine eq.trans _ (congr_arg neg_succ_of_nat n.bodd_add_div2), dsimp [bodd], cases nat.bodd n; dsimp [cond, bnot, div2, int.mul], { change -[1+ 2 * nat.div2 n] = _, rw zero_add }, { rw [zero_add, add_comm], refl } end theorem div2_val : ∀ n, div2 n = n / 2 | (n : ℕ) := congr_arg of_nat n.div2_val | -[1+ n] := congr_arg neg_succ_of_nat n.div2_val lemma bit0_val (n : ℤ) : bit0 n = 2 * n := (two_mul _).symm lemma bit1_val (n : ℤ) : bit1 n = 2 * n + 1 := congr_arg (+(1:ℤ)) (bit0_val _) lemma bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by { cases b, apply (bit0_val n).trans (add_zero _).symm, apply bit1_val } lemma bit_decomp (n : ℤ) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans $ (add_comm _ _).trans $ bodd_add_div2 _ /-- Defines a function from `ℤ` conditionally, if it is defined for odd and even integers separately using `bit`. -/ def {u} bit_cases_on {C : ℤ → Sort u} (n) (h : ∀ b n, C (bit b n)) : C n := by rw [← bit_decomp n]; apply h @[simp] lemma bit_zero : bit ff 0 = 0 := rfl @[simp] lemma bit_coe_nat (b) (n : ℕ) : bit b n = nat.bit b n := by rw [bit_val, nat.bit_val]; cases b; refl @[simp] lemma bit_neg_succ (b) (n : ℕ) : bit b -[1+ n] = -[1+ nat.bit (bnot b) n] := by rw [bit_val, nat.bit_val]; cases b; refl @[simp] lemma bodd_bit (b n) : bodd (bit b n) = b := by rw bit_val; simp; cases b; cases bodd n; refl @[simp] lemma bodd_bit0 (n : ℤ) : bodd (bit0 n) = ff := bodd_bit ff n @[simp] lemma bodd_bit1 (n : ℤ) : bodd (bit1 n) = tt := bodd_bit tt n @[simp] lemma div2_bit (b n) : div2 (bit b n) = n := begin rw [bit_val, div2_val, add_comm, int.add_mul_div_left, (_ : (_/2:ℤ) = 0), zero_add], cases b, { simp }, { show of_nat _ = _, rw nat.div_eq_zero; simp }, { cc } end lemma bit0_ne_bit1 (m n : ℤ) : bit0 m ≠ bit1 n := mt (congr_arg bodd) $ by simp lemma bit1_ne_bit0 (m n : ℤ) : bit1 m ≠ bit0 n := (bit0_ne_bit1 _ _).symm lemma bit1_ne_zero (m : ℤ) : bit1 m ≠ 0 := by simpa only [bit0_zero] using bit1_ne_bit0 m 0 @[simp] lemma test_bit_zero (b) : ∀ n, test_bit (bit b n) 0 = b | (n : ℕ) := by rw [bit_coe_nat]; apply nat.test_bit_zero | -[1+ n] := by rw [bit_neg_succ]; dsimp [test_bit]; rw [nat.test_bit_zero]; clear test_bit_zero; cases b; refl @[simp] lemma test_bit_succ (m b) : ∀ n, test_bit (bit b n) (nat.succ m) = test_bit n m | (n : ℕ) := by rw [bit_coe_nat]; apply nat.test_bit_succ | -[1+ n] := by rw [bit_neg_succ]; dsimp [test_bit]; rw [nat.test_bit_succ] private meta def bitwise_tac : tactic unit := `[ funext m, funext n, cases m with m m; cases n with n n; try {refl}, all_goals { apply congr_arg of_nat <|> apply congr_arg neg_succ_of_nat, try {dsimp [nat.land, nat.ldiff, nat.lor]}, try {rw [ show nat.bitwise (λ a b, a && bnot b) n m = nat.bitwise (λ a b, b && bnot a) m n, from congr_fun (congr_fun (@nat.bitwise_swap (λ a b, b && bnot a) rfl) n) m]}, apply congr_arg (λ f, nat.bitwise f m n), funext a, funext b, cases a; cases b; refl }, all_goals {unfold nat.land nat.ldiff nat.lor} ] theorem bitwise_or : bitwise bor = lor := by bitwise_tac theorem bitwise_and : bitwise band = land := by bitwise_tac theorem bitwise_diff : bitwise (λ a b, a && bnot b) = ldiff := by bitwise_tac theorem bitwise_xor : bitwise bxor = lxor := by bitwise_tac @[simp] lemma bitwise_bit (f : bool → bool → bool) (a m b n) : bitwise f (bit a m) (bit b n) = bit (f a b) (bitwise f m n) := begin cases m with m m; cases n with n n; repeat { rw [← int.coe_nat_eq] <|> rw bit_coe_nat <|> rw bit_neg_succ }; unfold bitwise nat_bitwise bnot; [ induction h : f ff ff, induction h : f ff tt, induction h : f tt ff, induction h : f tt tt ], all_goals { unfold cond, rw nat.bitwise_bit, repeat { rw bit_coe_nat <|> rw bit_neg_succ <|> rw bnot_bnot } }, all_goals { unfold bnot {fail_if_unchanged := ff}; rw h; refl } end @[simp] lemma lor_bit (a m b n) : lor (bit a m) (bit b n) = bit (a || b) (lor m n) := by rw [← bitwise_or, bitwise_bit] @[simp] lemma land_bit (a m b n) : land (bit a m) (bit b n) = bit (a && b) (land m n) := by rw [← bitwise_and, bitwise_bit] @[simp] lemma ldiff_bit (a m b n) : ldiff (bit a m) (bit b n) = bit (a && bnot b) (ldiff m n) := by rw [← bitwise_diff, bitwise_bit] @[simp] lemma lxor_bit (a m b n) : lxor (bit a m) (bit b n) = bit (bxor a b) (lxor m n) := by rw [← bitwise_xor, bitwise_bit] @[simp] lemma lnot_bit (b) : ∀ n, lnot (bit b n) = bit (bnot b) (lnot n) | (n : ℕ) := by simp [lnot] | -[1+ n] := by simp [lnot] @[simp] lemma test_bit_bitwise (f : bool → bool → bool) (m n k) : test_bit (bitwise f m n) k = f (test_bit m k) (test_bit n k) := begin induction k with k IH generalizing m n; apply bit_cases_on m; intros a m'; apply bit_cases_on n; intros b n'; rw bitwise_bit, { simp [test_bit_zero] }, { simp [test_bit_succ, IH] } end @[simp] lemma test_bit_lor (m n k) : test_bit (lor m n) k = test_bit m k || test_bit n k := by rw [← bitwise_or, test_bit_bitwise] @[simp] lemma test_bit_land (m n k) : test_bit (land m n) k = test_bit m k && test_bit n k := by rw [← bitwise_and, test_bit_bitwise] @[simp] lemma test_bit_ldiff (m n k) : test_bit (ldiff m n) k = test_bit m k && bnot (test_bit n k) := by rw [← bitwise_diff, test_bit_bitwise] @[simp] lemma test_bit_lxor (m n k) : test_bit (lxor m n) k = bxor (test_bit m k) (test_bit n k) := by rw [← bitwise_xor, test_bit_bitwise] @[simp] lemma test_bit_lnot : ∀ n k, test_bit (lnot n) k = bnot (test_bit n k) | (n : ℕ) k := by simp [lnot, test_bit] | -[1+ n] k := by simp [lnot, test_bit] lemma shiftl_add : ∀ (m : ℤ) (n : ℕ) (k : ℤ), shiftl m (n + k) = shiftl (shiftl m n) k | (m : ℕ) n (k:ℕ) := congr_arg of_nat (nat.shiftl_add _ _ _) | -[1+ m] n (k:ℕ) := congr_arg neg_succ_of_nat (nat.shiftl'_add _ _ _ _) | (m : ℕ) n -[1+k] := sub_nat_nat_elim n k.succ (λ n k i, shiftl ↑m i = nat.shiftr (nat.shiftl m n) k) (λ i n, congr_arg coe $ by rw [← nat.shiftl_sub, nat.add_sub_cancel_left]; apply nat.le_add_right) (λ i n, congr_arg coe $ by rw [add_assoc, nat.shiftr_add, ← nat.shiftl_sub, nat.sub_self]; refl) | -[1+ m] n -[1+k] := sub_nat_nat_elim n k.succ (λ n k i, shiftl -[1+ m] i = -[1+ nat.shiftr (nat.shiftl' tt m n) k]) (λ i n, congr_arg neg_succ_of_nat $ by rw [← nat.shiftl'_sub, nat.add_sub_cancel_left]; apply nat.le_add_right) (λ i n, congr_arg neg_succ_of_nat $ by rw [add_assoc, nat.shiftr_add, ← nat.shiftl'_sub, nat.sub_self]; refl) lemma shiftl_sub (m : ℤ) (n : ℕ) (k : ℤ) : shiftl m (n - k) = shiftr (shiftl m n) k := shiftl_add _ _ _ @[simp] lemma shiftl_neg (m n : ℤ) : shiftl m (-n) = shiftr m n := rfl @[simp] lemma shiftr_neg (m n : ℤ) : shiftr m (-n) = shiftl m n := by rw [← shiftl_neg, neg_neg] @[simp] lemma shiftl_coe_nat (m n : ℕ) : shiftl m n = nat.shiftl m n := rfl @[simp] lemma shiftr_coe_nat (m n : ℕ) : shiftr m n = nat.shiftr m n := by cases n; refl @[simp] lemma shiftl_neg_succ (m n : ℕ) : shiftl -[1+ m] n = -[1+ nat.shiftl' tt m n] := rfl @[simp] lemma shiftr_neg_succ (m n : ℕ) : shiftr -[1+ m] n = -[1+ nat.shiftr m n] := by cases n; refl lemma shiftr_add : ∀ (m : ℤ) (n k : ℕ), shiftr m (n + k) = shiftr (shiftr m n) k | (m : ℕ) n k := by rw [shiftr_coe_nat, shiftr_coe_nat, ← int.coe_nat_add, shiftr_coe_nat, nat.shiftr_add] | -[1+ m] n k := by rw [shiftr_neg_succ, shiftr_neg_succ, ← int.coe_nat_add, shiftr_neg_succ, nat.shiftr_add] lemma shiftl_eq_mul_pow : ∀ (m : ℤ) (n : ℕ), shiftl m n = m * ↑(2 ^ n) | (m : ℕ) n := congr_arg coe (nat.shiftl_eq_mul_pow _ _) | -[1+ m] n := @congr_arg ℕ ℤ _ _ (λi, -i) (nat.shiftl'_tt_eq_mul_pow _ _) lemma shiftr_eq_div_pow : ∀ (m : ℤ) (n : ℕ), shiftr m n = m / ↑(2 ^ n) | (m : ℕ) n := by rw shiftr_coe_nat; exact congr_arg coe (nat.shiftr_eq_div_pow _ _) | -[1+ m] n := begin rw [shiftr_neg_succ, neg_succ_of_nat_div, nat.shiftr_eq_div_pow], refl, exact coe_nat_lt_coe_nat_of_lt (pow_pos dec_trivial _) end lemma one_shiftl (n : ℕ) : shiftl 1 n = (2 ^ n : ℕ) := congr_arg coe (nat.one_shiftl _) @[simp] lemma zero_shiftl : ∀ n : ℤ, shiftl 0 n = 0 | (n : ℕ) := congr_arg coe (nat.zero_shiftl _) | -[1+ n] := congr_arg coe (nat.zero_shiftr _) @[simp] lemma zero_shiftr (n) : shiftr 0 n = 0 := zero_shiftl _ end int attribute [irreducible] int.nonneg
bbbe12b10bfc690ff0bc33880f656b3946370e7f
b1e80085f6d9158c0b431ffc4fa9d0d53cbac8e4
/src/quiver.lean
8bb268b904e14a6447dcad1f04343ef73d05a1bc
[]
no_license
dwarn/nielsen-schreier-2
d73c20e4d2a8ae537fe4f8063272d0b72c58276a
e51a8c6511d374dc584698c7fa236a5be47e7dbe
refs/heads/master
1,679,911,740,113
1,615,656,058,000
1,615,656,058,000
344,111,212
1
0
null
null
null
null
UTF-8
Lean
false
false
1,091
lean
import category_theory.single_obj misc open category_theory universes v u def quiver (G : Type u) := G → G → Type v def valu {G} (p : quiver G) (X) := Π {a b}, p a b → X def subquiver {G} (p : quiver G) := Π a b : G, set (p a b) def quiver_of_sub {G} {q : quiver G} (p : subquiver q) : quiver G := λ a b, { e : q a b // e ∈ p a b } notation `¡` p := quiver_of_sub p namespace quiver @[ext] structure total {G} (q : quiver G) := (source : G) (target : G) (edge : q source target) inductive path {G} (p : quiver.{u v} G) (a : G) : G → Type (max u v) | nil : path a | cons : Π {b c : G}, path b → p b c → path c end quiver class is_arbor {G} [inhabited G] (p : quiver G) := (unique_path (b : G) : unique (p.path (default G) b)) attribute [instance] is_arbor.unique_path def symmy {G} (p : quiver G) : quiver G := λ a b, (p a b) ⊕ (p b a) def tree_symmy {G} [inhabited G] {p : quiver.{v u} G} (t : subquiver (symmy p)) [is_arbor ¡t] : set p.total := { tp | sum.inl tp.edge ∈ t tp.source tp.target ∨ sum.inr tp.edge ∈ t tp.target tp.source }
b5af13cbb85eee11998618a4a6e25a61c70dbf03
b6f0d4562078d09b2d51c6aa5216cf0e07e8090f
/LeanRanges/FinRange.lean
fe5751fb49e77aaf2b7f3f6a68564df03c56a2e1
[ "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
3,559
lean
import LeanRanges.ToRange structure FinRange (n : Nat) where start : Nat stop : Nat step : Nat ascending : Bool instance : ToRange (Fin n) (FinRange n) where toRange start stop step := ⟨start, stop, step.natAbs, step >= 0⟩ toRangeEq start stop step := ⟨start, stop+1, step.natAbs, step >= 0⟩ -- Lemma to help with FinRange implementation theorem Nat.ltAddRight (a b c : Nat) (h : a < b) : a < b + c := match c with | 0 => h | c'+1 => have h₁ : a < (b + c') + 1 := Nat.lt.step (ltAddRight a b c' h) have h₂ : (b + c') + 1 = b + (c' + 1) from Nat.add_assoc .. cast (congrArg (λ x => a < x) h₂) h₁ namespace Fin /-- An exclusive range `[start, stop)` of `Fin n` values separated by `step`. If `n` is specified and `n < stop` then the range will be truncated to `[start, n)`. -/ def range (start stop : Nat) (step : Nat := 1) (ascending : Bool := true) (n : Nat := stop) : FinRange n := ⟨start, if n < stop then n else stop, step, ascending⟩ /-- An inclusive range `[start, stop]` of `Fin n` values separated by `step`. If `n` is specified and `n ≤ stop` then the range will be truncated to `[start, n-1]`. -/ def rangeEq (start stop : Nat) (step : Nat := 1) (ascending : Bool := true) (n : Nat := (Nat.succ stop)) : FinRange n := ⟨start, if n ≤ stop then n else stop+1, step, ascending⟩ -- Fin helpers for FinRange implementation def nonZero : {n : Nat} → Fin n → n > 0 | 0, x => Fin.elim0 x | n+1, _ => Nat.succPos n def lift {n : Nat} (m : Nat) : Fin n → Fin (n+m) | ⟨v, h⟩ => ⟨v, Nat.ltAddRight v n m h⟩ instance : HasMin (Fin n) where minOf x := Fin.ofNat' 0 (nonZero x) instance : HasMax (Fin n) where maxOf x := Fin.ofNat' (n-1) (nonZero x) end Fin namespace FinRange universes u v @[inline] def steps (r : FinRange n) : 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 : FinRange n) (i : Fin n) : Fin n := if r.ascending then Fin.ofNat' (i.val + r.step) (Fin.nonZero i) else Fin.ofNat' (i.val - r.step) (Fin.nonZero i) def first? (r : FinRange n) : Option (Fin n) := if h : n > 0 then if r.ascending then Fin.ofNat' r.start h else Fin.ofNat' (r.start + (r.step * (r.steps - 1))) h else none def reverse (r : FinRange n) : FinRange n := {r with ascending := !r.ascending} @[inline] def forIn {β : Type u} {m : Type u → Type v} [Monad m] (r : FinRange n) (init : β) (f : Fin n → β → m (ForInStep β)) : m β := let rec @[specialize] loop (fuel : Nat) (i : Fin n) (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 match r.first? with | none => init | some first => loop r.steps first init instance : ForIn m (FinRange n) (Fin n) where forIn := forIn @[inline] protected def forM {m : Type u → Type v} [Monad m] (r : FinRange n) (f : Nat → m PUnit) : m PUnit := let rec @[specialize] loop (fuel : Nat) (i : Fin n) : m PUnit := do match fuel with | 0 => pure ⟨⟩ | fuel+1 => f i; loop fuel (r.next i) match r.first? with | none => pure ⟨⟩ | some first => loop r.steps first def toArray (r : FinRange n) : Array (Fin n) := do let mut arr := #[] for i in r do arr := arr.push i arr def toList (r : FinRange n) : List (Fin n) := do let mut l := [] for i in r.reverse do l := i::l l end FinRange
116406215e7ac217004ef32a3559833aa2b3c746
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/analysis/analytic/composition.lean
49f6754c92942aeec5bf1f0fccdcde7d86dd5a02
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
56,734
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, Johan Commelin -/ import analysis.analytic.basic import combinatorics.composition /-! # Composition of analytic functions in this file we prove that the composition of analytic functions is analytic. The argument is the following. Assume `g z = ∑' qₙ (z, ..., z)` and `f y = ∑' pₖ (y, ..., y)`. Then `g (f y) = ∑' qₙ (∑' pₖ (y, ..., y), ..., ∑' pₖ (y, ..., y)) = ∑' qₙ (p_{i₁} (y, ..., y), ..., p_{iₙ} (y, ..., y))`. For each `n` and `i₁, ..., iₙ`, define a `i₁ + ... + iₙ` multilinear function mapping `(y₀, ..., y_{i₁ + ... + iₙ - 1})` to `qₙ (p_{i₁} (y₀, ..., y_{i₁-1}), p_{i₂} (y_{i₁}, ..., y_{i₁ + i₂ - 1}), ..., p_{iₙ} (....)))`. Then `g ∘ f` is obtained by summing all these multilinear functions. To formalize this, we use compositions of an integer `N`, i.e., its decompositions into a sum `i₁ + ... + iₙ` of positive integers. Given such a composition `c` and two formal multilinear series `q` and `p`, let `q.comp_along_composition p c` be the above multilinear function. Then the `N`-th coefficient in the power series expansion of `g ∘ f` is the sum of these terms over all `c : composition N`. To complete the proof, we need to show that this power series has a positive radius of convergence. This follows from the fact that `composition N` has cardinality `2^(N-1)` and estimates on the norm of `qₙ` and `pₖ`, which give summability. We also need to show that it indeed converges to `g ∘ f`. For this, we note that the composition of partial sums converges to `g ∘ f`, and that it corresponds to a part of the whole sum, on a subset that increases to the whole space. By summability of the norms, this implies the overall convergence. ## Main results * `q.comp p` is the formal composition of the formal multilinear series `q` and `p`. * `has_fpower_series_at.comp` states that if two functions `g` and `f` admit power series expansions `q` and `p`, then `g ∘ f` admits a power series expansion given by `q.comp p`. * `analytic_at.comp` states that the composition of analytic functions is analytic. * `formal_multilinear_series.comp_assoc` states that composition is associative on formal multilinear series. ## Implementation details The main technical difficulty is to write down things. In particular, we need to define precisely `q.comp_along_composition p c` and to show that it is indeed a continuous multilinear function. This requires a whole interface built on the class `composition`. Once this is set, the main difficulty is to reorder the sums, writing the composition of the partial sums as a sum over some subset of `Σ n, composition n`. We need to check that the reordering is a bijection, running over difficulties due to the dependent nature of the types under consideration, that are controlled thanks to the interface for `composition`. The associativity of composition on formal multilinear series is a nontrivial result: it does not follow from the associativity of composition of analytic functions, as there is no uniqueness for the formal multilinear series representing a function (and also, it holds even when the radius of convergence of the series is `0`). Instead, we give a direct proof, which amounts to reordering double sums in a careful way. The change of variables is a canonical (combinatorial) bijection `composition.sigma_equiv_sigma_pi` between `(Σ (a : composition n), composition a.length)` and `(Σ (c : composition n), Π (i : fin c.length), composition (c.blocks_fun i))`, and is described in more details below in the paragraph on associativity. -/ noncomputable theory variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {F : Type*} [normed_group F] [normed_space 𝕜 F] {G : Type*} [normed_group G] [normed_space 𝕜 G] {H : Type*} [normed_group H] [normed_space 𝕜 H] open filter list open_locale topological_space big_operators classical /-! ### Composing formal multilinear series -/ namespace formal_multilinear_series /-! In this paragraph, we define the composition of formal multilinear series, by summing over all possible compositions of `n`. -/ /-- Given a formal multilinear series `p`, a composition `c` of `n` and the index `i` of a block of `c`, we may define a function on `fin n → E` by picking the variables in the `i`-th block of `n`, and applying the corresponding coefficient of `p` to these variables. This function is called `p.apply_composition c v i` for `v : fin n → E` and `i : fin c.length`. -/ def apply_composition (p : formal_multilinear_series 𝕜 E F) {n : ℕ} (c : composition n) : (fin n → E) → (fin (c.length) → F) := λ v i, p (c.blocks_fun i) (v ∘ (c.embedding i)) lemma apply_composition_ones (p : formal_multilinear_series 𝕜 E F) (n : ℕ) : apply_composition p (composition.ones n) = λ v i, p 1 (λ _, v (i.cast_le (composition.length_le _))) := begin funext v i, apply p.congr (composition.ones_blocks_fun _ _), intros j hjn hj1, obtain rfl : j = 0, { linarith }, refine congr_arg v _, rw [fin.ext_iff, fin.coe_cast_le, composition.ones_embedding, fin.coe_mk], end /-- Technical lemma stating how `p.apply_composition` commutes with updating variables. This will be the key point to show that functions constructed from `apply_composition` retain multilinearity. -/ lemma apply_composition_update (p : formal_multilinear_series 𝕜 E F) {n : ℕ} (c : composition n) (j : fin n) (v : fin n → E) (z : E) : p.apply_composition c (function.update v j z) = function.update (p.apply_composition c v) (c.index j) (p (c.blocks_fun (c.index j)) (function.update (v ∘ (c.embedding (c.index j))) (c.inv_embedding j) z)) := begin ext k, by_cases h : k = c.index j, { rw h, let r : fin (c.blocks_fun (c.index j)) → fin n := c.embedding (c.index j), simp only [function.update_same], change p (c.blocks_fun (c.index j)) ((function.update v j z) ∘ r) = _, let j' := c.inv_embedding j, suffices B : (function.update v j z) ∘ r = function.update (v ∘ r) j' z, by rw B, suffices C : (function.update v (r j') z) ∘ r = function.update (v ∘ r) j' z, by { convert C, exact (c.embedding_comp_inv j).symm }, exact function.update_comp_eq_of_injective _ (c.embedding_injective _) _ _ }, { simp only [h, function.update_eq_self, function.update_noteq, ne.def, not_false_iff], let r : fin (c.blocks_fun k) → fin n := c.embedding k, change p (c.blocks_fun k) ((function.update v j z) ∘ r) = p (c.blocks_fun k) (v ∘ r), suffices B : (function.update v j z) ∘ r = v ∘ r, by rw B, apply function.update_comp_eq_of_not_mem_range, rwa c.mem_range_embedding_iff' } end /-- Given two formal multilinear series `q` and `p` and a composition `c` of `n`, one may form a multilinear map in `n` variables by applying the right coefficient of `p` to each block of the composition, and then applying `q c.length` to the resulting vector. It is called `q.comp_along_composition_multilinear p c`. This function admits a version as a continuous multilinear map, called `q.comp_along_composition p c` below. -/ def comp_along_composition_multilinear {n : ℕ} (q : formal_multilinear_series 𝕜 F G) (p : formal_multilinear_series 𝕜 E F) (c : composition n) : multilinear_map 𝕜 (λ i : fin n, E) G := { to_fun := λ v, q c.length (p.apply_composition c v), map_add' := λ v i x y, by simp only [apply_composition_update, continuous_multilinear_map.map_add], map_smul' := λ v i c x, by simp only [apply_composition_update, continuous_multilinear_map.map_smul] } /-- The norm of `q.comp_along_composition_multilinear p c` is controlled by the product of the norms of the relevant bits of `q` and `p`. -/ lemma comp_along_composition_multilinear_bound {n : ℕ} (q : formal_multilinear_series 𝕜 F G) (p : formal_multilinear_series 𝕜 E F) (c : composition n) (v : fin n → E) : ∥q.comp_along_composition_multilinear p c v∥ ≤ ∥q c.length∥ * (∏ i, ∥p (c.blocks_fun i)∥) * (∏ i : fin n, ∥v i∥) := calc ∥q.comp_along_composition_multilinear p c v∥ = ∥q c.length (p.apply_composition c v)∥ : rfl ... ≤ ∥q c.length∥ * ∏ i, ∥p.apply_composition c v i∥ : continuous_multilinear_map.le_op_norm _ _ ... ≤ ∥q c.length∥ * ∏ i, ∥p (c.blocks_fun i)∥ * ∏ j : fin (c.blocks_fun i), ∥(v ∘ (c.embedding i)) j∥ : begin apply mul_le_mul_of_nonneg_left _ (norm_nonneg _), refine finset.prod_le_prod (λ i hi, norm_nonneg _) (λ i hi, _), apply continuous_multilinear_map.le_op_norm, end ... = ∥q c.length∥ * (∏ i, ∥p (c.blocks_fun i)∥) * ∏ i (j : fin (c.blocks_fun i)), ∥(v ∘ (c.embedding i)) j∥ : by rw [finset.prod_mul_distrib, mul_assoc] ... = ∥q c.length∥ * (∏ i, ∥p (c.blocks_fun i)∥) * (∏ i : fin n, ∥v i∥) : by { rw [← finset.prod_equiv c.blocks_fin_equiv, ← finset.univ_sigma_univ, finset.prod_sigma], congr } /-- Given two formal multilinear series `q` and `p` and a composition `c` of `n`, one may form a continuous multilinear map in `n` variables by applying the right coefficient of `p` to each block of the composition, and then applying `q c.length` to the resulting vector. It is called `q.comp_along_composition p c`. It is constructed from the analogous multilinear function `q.comp_along_composition_multilinear p c`, together with a norm control to get the continuity. -/ def comp_along_composition {n : ℕ} (q : formal_multilinear_series 𝕜 F G) (p : formal_multilinear_series 𝕜 E F) (c : composition n) : continuous_multilinear_map 𝕜 (λ i : fin n, E) G := (q.comp_along_composition_multilinear p c).mk_continuous _ (q.comp_along_composition_multilinear_bound p c) @[simp] lemma comp_along_composition_apply {n : ℕ} (q : formal_multilinear_series 𝕜 F G) (p : formal_multilinear_series 𝕜 E F) (c : composition n) (v : fin n → E) : (q.comp_along_composition p c) v = q c.length (p.apply_composition c v) := rfl /-- The norm of `q.comp_along_composition p c` is controlled by the product of the norms of the relevant bits of `q` and `p`. -/ lemma comp_along_composition_norm {n : ℕ} (q : formal_multilinear_series 𝕜 F G) (p : formal_multilinear_series 𝕜 E F) (c : composition n) : ∥q.comp_along_composition p c∥ ≤ ∥q c.length∥ * ∏ i, ∥p (c.blocks_fun i)∥ := multilinear_map.mk_continuous_norm_le _ (mul_nonneg (norm_nonneg _) (finset.prod_nonneg (λ i hi, norm_nonneg _))) _ lemma comp_along_composition_nnnorm {n : ℕ} (q : formal_multilinear_series 𝕜 F G) (p : formal_multilinear_series 𝕜 E F) (c : composition n) : nnnorm (q.comp_along_composition p c) ≤ nnnorm (q c.length) * ∏ i, nnnorm (p (c.blocks_fun i)) := by simpa only [← nnreal.coe_le_coe, coe_nnnorm, nnreal.coe_mul, coe_nnnorm, nnreal.coe_prod, coe_nnnorm] using q.comp_along_composition_norm p c /-- Formal composition of two formal multilinear series. The `n`-th coefficient in the composition is defined to be the sum of `q.comp_along_composition p c` over all compositions of `n`. In other words, this term (as a multilinear function applied to `v_0, ..., v_{n-1}`) is `∑'_{k} ∑'_{i₁ + ... + iₖ = n} pₖ (q_{i_1} (...), ..., q_{i_k} (...))`, where one puts all variables `v_0, ..., v_{n-1}` in increasing order in the dots.-/ protected def comp (q : formal_multilinear_series 𝕜 F G) (p : formal_multilinear_series 𝕜 E F) : formal_multilinear_series 𝕜 E G := λ n, ∑ c : composition n, q.comp_along_composition p c /-- The `0`-th coefficient of `q.comp p` is `q 0`. Since these maps are multilinear maps in zero variables, but on different spaces, we can not state this directly, so we state it when applied to arbitrary vectors (which have to be the zero vector). -/ lemma comp_coeff_zero (q : formal_multilinear_series 𝕜 F G) (p : formal_multilinear_series 𝕜 E F) (v : fin 0 → E) (v' : fin 0 → F) : (q.comp p) 0 v = q 0 v' := begin let c : composition 0 := composition.ones 0, dsimp [formal_multilinear_series.comp], have : {c} = (finset.univ : finset (composition 0)), { apply finset.eq_of_subset_of_card_le; simp [finset.card_univ, composition_card 0] }, rw ← this, simp only [finset.sum_singleton, continuous_multilinear_map.sum_apply], change q c.length (p.apply_composition c v) = q 0 v', congr' with i, simp only [composition.ones_length] at i, exact fin_zero_elim i end @[simp] lemma comp_coeff_zero' (q : formal_multilinear_series 𝕜 F G) (p : formal_multilinear_series 𝕜 E F) (v : fin 0 → E) : (q.comp p) 0 v = q 0 (λ i, 0) := q.comp_coeff_zero p v _ /-- The `0`-th coefficient of `q.comp p` is `q 0`. When `p` goes from `E` to `E`, this can be expressed as a direct equality -/ lemma comp_coeff_zero'' (q : formal_multilinear_series 𝕜 E F) (p : formal_multilinear_series 𝕜 E E) : (q.comp p) 0 = q 0 := by { ext v, exact q.comp_coeff_zero p _ _ } /-! ### The identity formal power series We will now define the identity power series, and show that it is a neutral element for left and right composition. -/ section variables (𝕜 E) /-- The identity formal multilinear series, with all coefficients equal to `0` except for `n = 1` where it is (the continuous multilinear version of) the identity. -/ def id : formal_multilinear_series 𝕜 E E | 0 := 0 | 1 := (continuous_multilinear_curry_fin1 𝕜 E E).symm (continuous_linear_map.id 𝕜 E) | _ := 0 /-- The first coefficient of `id 𝕜 E` is the identity. -/ @[simp] lemma id_apply_one (v : fin 1 → E) : (formal_multilinear_series.id 𝕜 E) 1 v = v 0 := rfl /-- The `n`th coefficient of `id 𝕜 E` is the identity when `n = 1`. We state this in a dependent way, as it will often appear in this form. -/ lemma id_apply_one' {n : ℕ} (h : n = 1) (v : fin n → E) : (id 𝕜 E) n v = v ⟨0, h.symm ▸ zero_lt_one⟩ := begin let w : fin 1 → E := λ i, v ⟨i.1, h.symm ▸ i.2⟩, have : v ⟨0, h.symm ▸ zero_lt_one⟩ = w 0 := rfl, rw [this, ← id_apply_one 𝕜 E w], apply congr _ h, intros, obtain rfl : i = 0, { linarith }, exact this, end /-- For `n ≠ 1`, the `n`-th coefficient of `id 𝕜 E` is zero, by definition. -/ @[simp] lemma id_apply_ne_one {n : ℕ} (h : n ≠ 1) : (formal_multilinear_series.id 𝕜 E) n = 0 := by { cases n, { refl }, cases n, { contradiction }, refl } end @[simp] theorem comp_id (p : formal_multilinear_series 𝕜 E F) : p.comp (id 𝕜 E) = p := begin ext1 n, dsimp [formal_multilinear_series.comp], rw finset.sum_eq_single (composition.ones n), show comp_along_composition p (id 𝕜 E) (composition.ones n) = p n, { ext v, rw comp_along_composition_apply, apply p.congr (composition.ones_length n), intros, rw apply_composition_ones, refine congr_arg v _, rw [fin.ext_iff, fin.coe_cast_le, fin.coe_mk, fin.coe_mk], }, show ∀ (b : composition n), b ∈ finset.univ → b ≠ composition.ones n → comp_along_composition p (id 𝕜 E) b = 0, { assume b _ hb, obtain ⟨k, hk, lt_k⟩ : ∃ (k : ℕ) (H : k ∈ composition.blocks b), 1 < k := composition.ne_ones_iff.1 hb, obtain ⟨i, i_lt, hi⟩ : ∃ (i : ℕ) (h : i < b.blocks.length), b.blocks.nth_le i h = k := nth_le_of_mem hk, let j : fin b.length := ⟨i, b.blocks_length ▸ i_lt⟩, have A : 1 < b.blocks_fun j := by convert lt_k, ext v, rw [comp_along_composition_apply, continuous_multilinear_map.zero_apply], apply continuous_multilinear_map.map_coord_zero _ j, dsimp [apply_composition], rw id_apply_ne_one _ _ (ne_of_gt A), refl }, { simp } end theorem id_comp (p : formal_multilinear_series 𝕜 E F) (h : p 0 = 0) : (id 𝕜 F).comp p = p := begin ext1 n, by_cases hn : n = 0, { rw [hn, h], ext v, rw [comp_coeff_zero', id_apply_ne_one _ _ zero_ne_one], refl }, { dsimp [formal_multilinear_series.comp], have n_pos : 0 < n := bot_lt_iff_ne_bot.mpr hn, rw finset.sum_eq_single (composition.single n n_pos), show comp_along_composition (id 𝕜 F) p (composition.single n n_pos) = p n, { ext v, rw [comp_along_composition_apply, id_apply_one' _ _ (composition.single_length n_pos)], dsimp [apply_composition], apply p.congr rfl, intros, rw [function.comp_app, composition.single_embedding] }, show ∀ (b : composition n), b ∈ finset.univ → b ≠ composition.single n n_pos → comp_along_composition (id 𝕜 F) p b = 0, { assume b _ hb, have A : b.length ≠ 1, by simpa [composition.eq_single_iff] using hb, ext v, rw [comp_along_composition_apply, id_apply_ne_one _ _ A], refl }, { simp } } end /-! ### Summability properties of the composition of formal power series-/ /-- If two formal multilinear series have positive radius of convergence, then the terms appearing in the definition of their composition are also summable (when multiplied by a suitable positive geometric term). -/ theorem comp_summable_nnreal (q : formal_multilinear_series 𝕜 F G) (p : formal_multilinear_series 𝕜 E F) (hq : 0 < q.radius) (hp : 0 < p.radius) : ∃ (r : nnreal), 0 < r ∧ summable (λ i, nnnorm (q.comp_along_composition p i.2) * r ^ i.1 : (Σ n, composition n) → nnreal) := begin /- This follows from the fact that the growth rate of `∥qₙ∥` and `∥pₙ∥` is at most geometric, giving a geometric bound on each `∥q.comp_along_composition p op∥`, together with the fact that there are `2^(n-1)` compositions of `n`, giving at most a geometric loss. -/ rcases ennreal.lt_iff_exists_nnreal_btwn.1 hq with ⟨rq, rq_pos, hrq⟩, rcases ennreal.lt_iff_exists_nnreal_btwn.1 hp with ⟨rp, rp_pos, hrp⟩, obtain ⟨Cq, hCq⟩ : ∃ (Cq : nnreal), ∀ n, nnnorm (q n) * rq^n ≤ Cq := q.bound_of_lt_radius hrq, obtain ⟨Cp, hCp⟩ : ∃ (Cp : nnreal), ∀ n, nnnorm (p n) * rp^n ≤ Cp := p.bound_of_lt_radius hrp, let r0 : nnreal := (4 * max Cp 1)⁻¹, set r := min rp 1 * min rq 1 * r0, have r_pos : 0 < r, { apply mul_pos (mul_pos _ _), { rw [nnreal.inv_pos], apply mul_pos, { norm_num }, { exact lt_of_lt_of_le zero_lt_one (le_max_right _ _) } }, { rw ennreal.coe_pos at rp_pos, simp [rp_pos, zero_lt_one] }, { rw ennreal.coe_pos at rq_pos, simp [rq_pos, zero_lt_one] } }, let a : ennreal := ((4 : nnreal) ⁻¹ : nnreal), have two_a : 2 * a < 1, { change ((2 : nnreal) : ennreal) * ((4 : nnreal) ⁻¹ : nnreal) < (1 : nnreal), rw [← ennreal.coe_mul, ennreal.coe_lt_coe, ← nnreal.coe_lt_coe, nnreal.coe_mul], change (2 : ℝ) * (4 : ℝ)⁻¹ < 1, norm_num }, have I : ∀ (i : Σ (n : ℕ), composition n), ↑(nnnorm (q.comp_along_composition p i.2) * r ^ i.1) ≤ (Cq : ennreal) * a ^ i.1, { rintros ⟨n, c⟩, rw [← ennreal.coe_pow, ← ennreal.coe_mul, ennreal.coe_le_coe], calc nnnorm (q.comp_along_composition p c) * r ^ n ≤ (nnnorm (q c.length) * ∏ i, nnnorm (p (c.blocks_fun i))) * r ^ n : mul_le_mul_of_nonneg_right (q.comp_along_composition_nnnorm p c) (bot_le) ... = (nnnorm (q c.length) * (min rq 1)^n) * ((∏ i, nnnorm (p (c.blocks_fun i))) * (min rp 1) ^ n) * r0 ^ n : by { dsimp [r], ring_exp } ... ≤ (nnnorm (q c.length) * (min rq 1) ^ c.length) * (∏ i, nnnorm (p (c.blocks_fun i)) * (min rp 1) ^ (c.blocks_fun i)) * r0 ^ n : begin apply_rules [mul_le_mul, bot_le, le_refl, pow_le_pow_of_le_one, min_le_right, c.length_le], apply le_of_eq, rw finset.prod_mul_distrib, congr' 1, conv_lhs { rw [← c.sum_blocks_fun, ← finset.prod_pow_eq_pow_sum] }, end ... ≤ Cq * (∏ i : fin c.length, Cp) * r0 ^ n : begin apply_rules [mul_le_mul, bot_le, le_trans _ (hCq c.length), le_refl, finset.prod_le_prod', pow_le_pow_of_le_left, min_le_left], assume i hi, refine le_trans (mul_le_mul (le_refl _) _ bot_le bot_le) (hCp (c.blocks_fun i)), exact pow_le_pow_of_le_left bot_le (min_le_left _ _) _ end ... ≤ Cq * (max Cp 1) ^ n * r0 ^ n : begin apply_rules [mul_le_mul, bot_le, le_refl], simp only [finset.card_fin, finset.prod_const], refine le_trans (pow_le_pow_of_le_left bot_le (le_max_left Cp 1) c.length) _, apply pow_le_pow (le_max_right Cp 1) c.length_le, end ... = Cq * 4⁻¹ ^ n : begin dsimp [r0], have A : (4 : nnreal) ≠ 0, by norm_num, have B : max Cp 1 ≠ 0 := ne_of_gt (lt_of_lt_of_le zero_lt_one (le_max_right Cp 1)), field_simp [A, B], ring_exp end }, refine ⟨r, r_pos, _⟩, rw [← ennreal.tsum_coe_ne_top_iff_summable], apply ne_of_lt, calc (∑' (i : Σ (n : ℕ), composition n), ↑(nnnorm (q.comp_along_composition p i.2) * r ^ i.1)) ≤ (∑' (i : Σ (n : ℕ), composition n), (Cq : ennreal) * a ^ i.1) : ennreal.tsum_le_tsum I ... = (∑' (n : ℕ), (∑' (c : composition n), (Cq : ennreal) * a ^ n)) : ennreal.tsum_sigma' _ ... = (∑' (n : ℕ), ↑(fintype.card (composition n)) * (Cq : ennreal) * a ^ n) : begin congr' 1 with n : 1, rw [tsum_fintype, finset.sum_const, nsmul_eq_mul, finset.card_univ, mul_assoc] end ... ≤ (∑' (n : ℕ), (2 : ennreal) ^ n * (Cq : ennreal) * a ^ n) : begin apply ennreal.tsum_le_tsum (λ n, _), apply ennreal.mul_le_mul (ennreal.mul_le_mul _ (le_refl _)) (le_refl _), rw composition_card, simp only [nat.cast_bit0, nat.cast_one, nat.cast_pow], apply ennreal.pow_le_pow _ (nat.sub_le n 1), have : (1 : nnreal) ≤ (2 : nnreal), by norm_num, rw ← ennreal.coe_le_coe at this, exact this end ... = (∑' (n : ℕ), (Cq : ennreal) * (2 * a) ^ n) : by { congr' 1 with n : 1, rw mul_pow, ring } ... = (Cq : ennreal) * (1 - 2 * a) ⁻¹ : by rw [ennreal.tsum_mul_left, ennreal.tsum_geometric] ... < ⊤ : by simp [lt_top_iff_ne_top, ennreal.mul_eq_top, two_a] end /-- Bounding below the radius of the composition of two formal multilinear series assuming summability over all compositions. -/ theorem le_comp_radius_of_summable (q : formal_multilinear_series 𝕜 F G) (p : formal_multilinear_series 𝕜 E F) (r : nnreal) (hr : summable (λ i, nnnorm (q.comp_along_composition p i.2) * r ^ i.1 : (Σ n, composition n) → nnreal)) : (r : ennreal) ≤ (q.comp p).radius := begin apply le_radius_of_bound _ (tsum (λ (i : Σ (n : ℕ), composition n), (nnnorm (comp_along_composition q p i.snd) * r ^ i.fst))), assume n, calc nnnorm (formal_multilinear_series.comp q p n) * r ^ n ≤ ∑' (c : composition n), nnnorm (comp_along_composition q p c) * r ^ n : begin rw [tsum_fintype, ← finset.sum_mul], exact mul_le_mul_of_nonneg_right (nnnorm_sum_le _ _) bot_le end ... ≤ ∑' (i : Σ (n : ℕ), composition n), nnnorm (comp_along_composition q p i.snd) * r ^ i.fst : begin let f : composition n → (Σ (n : ℕ), composition n) := λ c, ⟨n, c⟩, have : function.injective f, by tidy, convert nnreal.tsum_comp_le_tsum_of_inj hr this end end /-! ### Composing analytic functions Now, we will prove that the composition of the partial sums of `q` and `p` up to order `N` is given by a sum over some large subset of `Σ n, composition n` of `q.comp_along_composition p`, to deduce that the series for `q.comp p` indeed converges to `g ∘ f` when `q` is a power series for `g` and `p` is a power series for `f`. This proof is a big reindexing argument of a sum. Since it is a bit involved, we define first the source of the change of variables (`comp_partial_source`), its target (`comp_partial_target`) and the change of variables itself (`comp_change_of_variables`) before giving the main statement in `comp_partial_sum`. -/ /-- Source set in the change of variables to compute the composition of partial sums of formal power series. See also `comp_partial_sum`. -/ def comp_partial_sum_source (N : ℕ) : finset (Σ n, (fin n) → ℕ) := finset.sigma (finset.range N) (λ (n : ℕ), fintype.pi_finset (λ (i : fin n), finset.Ico 1 N) : _) @[simp] lemma mem_comp_partial_sum_source_iff (N : ℕ) (i : Σ n, (fin n) → ℕ) : i ∈ comp_partial_sum_source N ↔ i.1 < N ∧ ∀ (a : fin i.1), 1 ≤ i.2 a ∧ i.2 a < N := by simp only [comp_partial_sum_source, finset.Ico.mem, fintype.mem_pi_finset, finset.mem_sigma, finset.mem_range] /-- Change of variables appearing to compute the composition of partial sums of formal power series -/ def comp_change_of_variables (N : ℕ) (i : Σ n, (fin n) → ℕ) (hi : i ∈ comp_partial_sum_source N) : (Σ n, composition n) := begin rcases i with ⟨n, f⟩, rw mem_comp_partial_sum_source_iff at hi, refine ⟨∑ j, f j, of_fn (λ a, f a), λ i hi', _, by simp [sum_of_fn]⟩, obtain ⟨j, rfl⟩ : ∃ (j : fin n), f j = i, by rwa [mem_of_fn, set.mem_range] at hi', exact (hi.2 j).1 end @[simp] lemma comp_change_of_variables_length (N : ℕ) {i : Σ n, (fin n) → ℕ} (hi : i ∈ comp_partial_sum_source N) : composition.length (comp_change_of_variables N i hi).2 = i.1 := begin rcases i with ⟨k, blocks_fun⟩, dsimp [comp_change_of_variables], simp only [composition.length, map_of_fn, length_of_fn] end lemma comp_change_of_variables_blocks_fun (N : ℕ) {i : Σ n, (fin n) → ℕ} (hi : i ∈ comp_partial_sum_source N) (j : fin i.1) : (comp_change_of_variables N i hi).2.blocks_fun ⟨j, (comp_change_of_variables_length N hi).symm ▸ j.2⟩ = i.2 j := begin rcases i with ⟨n, f⟩, dsimp [composition.blocks_fun, composition.blocks, comp_change_of_variables], simp only [map_of_fn, nth_le_of_fn', function.comp_app], apply congr_arg, rw [fin.ext_iff, fin.mk_coe] end /-- Target set in the change of variables to compute the composition of partial sums of formal power series, here given a a set. -/ def comp_partial_sum_target_set (N : ℕ) : set (Σ n, composition n) := {i | (i.2.length < N) ∧ (∀ (j : fin i.2.length), i.2.blocks_fun j < N)} lemma comp_partial_sum_target_subset_image_comp_partial_sum_source (N : ℕ) (i : Σ n, composition n) (hi : i ∈ comp_partial_sum_target_set N) : ∃ j (hj : j ∈ comp_partial_sum_source N), i = comp_change_of_variables N j hj := begin rcases i with ⟨n, c⟩, refine ⟨⟨c.length, c.blocks_fun⟩, _, _⟩, { simp only [comp_partial_sum_target_set, set.mem_set_of_eq] at hi, simp only [mem_comp_partial_sum_source_iff, hi.left, hi.right, true_and, and_true], exact λ a, c.one_le_blocks' _ }, { dsimp [comp_change_of_variables], rw composition.sigma_eq_iff_blocks_eq, simp only [composition.blocks_fun, composition.blocks, subtype.coe_eta, nth_le_map'], conv_lhs { rw ← of_fn_nth_le c.blocks }, simp only [fin.val_eq_coe], refl, /- where does this fin.val come from? -/ } end /-- Target set in the change of variables to compute the composition of partial sums of formal power series, here given a a finset. See also `comp_partial_sum`. -/ def comp_partial_sum_target (N : ℕ) : finset (Σ n, composition n) := set.finite.to_finset $ (finset.finite_to_set _).dependent_image (comp_partial_sum_target_subset_image_comp_partial_sum_source N) @[simp] lemma mem_comp_partial_sum_target_iff {N : ℕ} {a : Σ n, composition n} : a ∈ comp_partial_sum_target N ↔ a.2.length < N ∧ (∀ (j : fin a.2.length), a.2.blocks_fun j < N) := by simp [comp_partial_sum_target, comp_partial_sum_target_set] /-- The auxiliary set corresponding to the composition of partial sums asymptotically contains all possible compositions. -/ lemma comp_partial_sum_target_tendsto_at_top : tendsto comp_partial_sum_target at_top at_top := begin apply monotone.tendsto_at_top_finset, { assume m n hmn a ha, have : ∀ i, i < m → i < n := λ i hi, lt_of_lt_of_le hi hmn, tidy }, { rintros ⟨n, c⟩, simp only [mem_comp_partial_sum_target_iff], obtain ⟨n, hn⟩ : bdd_above ↑(finset.univ.image (λ (i : fin c.length), c.blocks_fun i)) := finset.bdd_above _, refine ⟨max n c.length + 1, lt_of_le_of_lt (le_max_right n c.length) (lt_add_one _), λ j, lt_of_le_of_lt (le_trans _ (le_max_left _ _)) (lt_add_one _)⟩, apply hn, simp only [finset.mem_image_of_mem, finset.mem_coe, finset.mem_univ] } end /-- Composing the partial sums of two multilinear series coincides with the sum over all compositions in `comp_partial_sum_target N`. This is precisely the motivation for the definition of `comp_partial_sum_target N`. -/ lemma comp_partial_sum (q : formal_multilinear_series 𝕜 F G) (p : formal_multilinear_series 𝕜 E F) (N : ℕ) (z : E) : q.partial_sum N (∑ i in finset.Ico 1 N, p i (λ j, z)) = ∑ i in comp_partial_sum_target N, q.comp_along_composition_multilinear p i.2 (λ j, z) := begin -- we expand the composition, using the multilinearity of `q` to expand along each coordinate. suffices H : ∑ n in finset.range N, ∑ r in fintype.pi_finset (λ (i : fin n), finset.Ico 1 N), q n (λ (i : fin n), p (r i) (λ j, z)) = ∑ i in comp_partial_sum_target N, q.comp_along_composition_multilinear p i.2 (λ j, z), by simpa only [formal_multilinear_series.partial_sum, continuous_multilinear_map.map_sum_finset] using H, -- rewrite the first sum as a big sum over a sigma type rw ← @finset.sum_sigma _ _ _ _ (finset.range N) (λ (n : ℕ), (fintype.pi_finset (λ (i : fin n), finset.Ico 1 N)) : _) (λ i, q i.1 (λ (j : fin i.1), p (i.2 j) (λ (k : fin (i.2 j)), z))), show ∑ i in comp_partial_sum_source N, q i.1 (λ (j : fin i.1), p (i.2 j) (λ (k : fin (i.2 j)), z)) = ∑ i in comp_partial_sum_target N, q.comp_along_composition_multilinear p i.2 (λ j, z), -- show that the two sums correspond to each other by reindexing the variables. apply finset.sum_bij (comp_change_of_variables N), -- To conclude, we should show that the correspondance we have set up is indeed a bijection -- between the index sets of the two sums. -- 1 - show that the image belongs to `comp_partial_sum_target N` { rintros ⟨k, blocks_fun⟩ H, rw mem_comp_partial_sum_source_iff at H, simp only [mem_comp_partial_sum_target_iff, composition.length, composition.blocks, H.left, map_of_fn, length_of_fn, true_and, comp_change_of_variables], assume j, simp only [composition.blocks_fun, (H.right _).right, nth_le_of_fn'] }, -- 2 - show that the composition gives the `comp_along_composition` application { rintros ⟨k, blocks_fun⟩ H, apply congr _ (comp_change_of_variables_length N H).symm, intros, rw ← comp_change_of_variables_blocks_fun N H, refl }, -- 3 - show that the map is injective { rintros ⟨k, blocks_fun⟩ ⟨k', blocks_fun'⟩ H H' heq, obtain rfl : k = k', { have := (comp_change_of_variables_length N H).symm, rwa [heq, comp_change_of_variables_length] at this, }, congr, funext i, calc blocks_fun i = (comp_change_of_variables N _ H).2.blocks_fun _ : (comp_change_of_variables_blocks_fun N H i).symm ... = (comp_change_of_variables N _ H').2.blocks_fun _ : begin apply composition.blocks_fun_congr; try { rw heq }, refl end ... = blocks_fun' i : comp_change_of_variables_blocks_fun N H' i }, -- 4 - show that the map is surjective { assume i hi, apply comp_partial_sum_target_subset_image_comp_partial_sum_source N i, simpa [comp_partial_sum_target] using hi } end end formal_multilinear_series open formal_multilinear_series /-- If two functions `g` and `f` have power series `q` and `p` respectively at `f x` and `x`, then `g ∘ f` admits the power series `q.comp p` at `x`. -/ theorem has_fpower_series_at.comp {g : F → G} {f : E → F} {q : formal_multilinear_series 𝕜 F G} {p : formal_multilinear_series 𝕜 E F} {x : E} (hg : has_fpower_series_at g q (f x)) (hf : has_fpower_series_at f p x) : has_fpower_series_at (g ∘ f) (q.comp p) x := begin /- Consider `rf` and `rg` such that `f` and `g` have power series expansion on the disks of radius `rf` and `rg`. -/ rcases hg with ⟨rg, Hg⟩, rcases hf with ⟨rf, Hf⟩, /- The terms defining `q.comp p` are geometrically summable in a disk of some radius `r`. -/ rcases q.comp_summable_nnreal p Hg.radius_pos Hf.radius_pos with ⟨r, r_pos, hr⟩, /- We will consider `y` which is smaller than `r` and `rf`, and also small enough that `f (x + y)` is close enough to `f x` to be in the disk where `g` is well behaved. Let `min (r, rf, δ)` be this new radius.-/ have : continuous_at f x := Hf.analytic_at.continuous_at, obtain ⟨δ, δpos, hδ⟩ : ∃ (δ : ennreal) (H : 0 < δ), ∀ {z : E}, z ∈ emetric.ball x δ → f z ∈ emetric.ball (f x) rg, { have : emetric.ball (f x) rg ∈ 𝓝 (f x) := emetric.ball_mem_nhds _ Hg.r_pos, rcases emetric.mem_nhds_iff.1 (Hf.analytic_at.continuous_at this) with ⟨δ, δpos, Hδ⟩, exact ⟨δ, δpos, λ z hz, Hδ hz⟩ }, let rf' := min rf δ, have min_pos : 0 < min rf' r, by simp only [r_pos, Hf.r_pos, δpos, lt_min_iff, ennreal.coe_pos, and_self], /- We will show that `g ∘ f` admits the power series `q.comp p` in the disk of radius `min (r, rf', δ)`. -/ refine ⟨min rf' r, _⟩, refine ⟨le_trans (min_le_right rf' r) (formal_multilinear_series.le_comp_radius_of_summable q p r hr), min_pos, λ y hy, _⟩, /- Let `y` satisfy `∥y∥ < min (r, rf', δ)`. We want to show that `g (f (x + y))` is the sum of `q.comp p` applied to `y`. -/ -- First, check that `y` is small enough so that estimates for `f` and `g` apply. have y_mem : y ∈ emetric.ball (0 : E) rf := (emetric.ball_subset_ball (le_trans (min_le_left _ _) (min_le_left _ _))) hy, have fy_mem : f (x + y) ∈ emetric.ball (f x) rg, { apply hδ, have : y ∈ emetric.ball (0 : E) δ := (emetric.ball_subset_ball (le_trans (min_le_left _ _) (min_le_right _ _))) hy, simpa [edist_eq_coe_nnnorm_sub, edist_eq_coe_nnnorm] }, /- Now the proof starts. To show that the sum of `q.comp p` at `y` is `g (f (x + y))`, we will write `q.comp p` applied to `y` as a big sum over all compositions. Since the sum is summable, to get its convergence it suffices to get the convergence along some increasing sequence of sets. We will use the sequence of sets `comp_partial_sum_target n`, along which the sum is exactly the composition of the partial sums of `q` and `p`, by design. To show that it converges to `g (f (x + y))`, pointwise convergence would not be enough, but we have uniform convergence to save the day. -/ -- First step: the partial sum of `p` converges to `f (x + y)`. have A : tendsto (λ n, ∑ a in finset.Ico 1 n, p a (λ b, y)) at_top (𝓝 (f (x + y) - f x)), { have L : ∀ᶠ n in at_top, ∑ a in finset.range n, p a (λ b, y) - f x = ∑ a in finset.Ico 1 n, p a (λ b, y), { rw eventually_at_top, refine ⟨1, λ n hn, _⟩, symmetry, rw [eq_sub_iff_add_eq', finset.range_eq_Ico, ← Hf.coeff_zero (λi, y), finset.sum_eq_sum_Ico_succ_bot hn] }, have : tendsto (λ n, ∑ a in finset.range n, p a (λ b, y) - f x) at_top (𝓝 (f (x + y) - f x)) := (Hf.has_sum y_mem).tendsto_sum_nat.sub tendsto_const_nhds, exact tendsto.congr' L this }, -- Second step: the composition of the partial sums of `q` and `p` converges to `g (f (x + y))`. have B : tendsto (λ n, q.partial_sum n (∑ a in finset.Ico 1 n, p a (λ b, y))) at_top (𝓝 (g (f (x + y)))), { -- we use the fact that the partial sums of `q` converge locally uniformly to `g`, and that -- composition passes to the limit under locally uniform convergence. have B₁ : continuous_at (λ (z : F), g (f x + z)) (f (x + y) - f x), { refine continuous_at.comp _ (continuous_const.add continuous_id).continuous_at, simp only [add_sub_cancel'_right, id.def], exact Hg.continuous_on.continuous_at (mem_nhds_sets (emetric.is_open_ball) fy_mem) }, have B₂ : f (x + y) - f x ∈ emetric.ball (0 : F) rg, by simpa [edist_eq_coe_nnnorm, edist_eq_coe_nnnorm_sub] using fy_mem, rw [← nhds_within_eq_of_open B₂ emetric.is_open_ball] at A, convert Hg.tendsto_locally_uniformly_on.tendsto_comp B₁.continuous_within_at B₂ A, simp only [add_sub_cancel'_right] }, -- Third step: the sum over all compositions in `comp_partial_sum_target n` converges to -- `g (f (x + y))`. As this sum is exactly the composition of the partial sum, this is a direct -- consequence of the second step have C : tendsto (λ n, ∑ i in comp_partial_sum_target n, q.comp_along_composition_multilinear p i.2 (λ j, y)) at_top (𝓝 (g (f (x + y)))), by simpa [comp_partial_sum] using B, -- Fourth step: the sum over all compositions is `g (f (x + y))`. This follows from the -- convergence along a subsequence proved in the third step, and the fact that the sum is Cauchy -- thanks to the summability properties. have D : has_sum (λ i : (Σ n, composition n), q.comp_along_composition_multilinear p i.2 (λ j, y)) (g (f (x + y))), { have cau : cauchy_seq (λ (s : finset (Σ n, composition n)), ∑ i in s, q.comp_along_composition_multilinear p i.2 (λ j, y)), { apply cauchy_seq_finset_of_norm_bounded _ (nnreal.summable_coe.2 hr) _, simp only [coe_nnnorm, nnreal.coe_mul, nnreal.coe_pow], rintros ⟨n, c⟩, calc ∥(comp_along_composition q p c) (λ (j : fin n), y)∥ ≤ ∥comp_along_composition q p c∥ * ∏ j : fin n, ∥y∥ : by apply continuous_multilinear_map.le_op_norm ... ≤ ∥comp_along_composition q p c∥ * (r : ℝ) ^ n : begin apply mul_le_mul_of_nonneg_left _ (norm_nonneg _), rw [finset.prod_const, finset.card_fin], apply pow_le_pow_of_le_left (norm_nonneg _), rw [emetric.mem_ball, edist_eq_coe_nnnorm] at hy, have := (le_trans (le_of_lt hy) (min_le_right _ _)), rwa [ennreal.coe_le_coe, ← nnreal.coe_le_coe, coe_nnnorm] at this end }, exact tendsto_nhds_of_cauchy_seq_of_subseq cau comp_partial_sum_target_tendsto_at_top C }, -- Fifth step: the sum over `n` of `q.comp p n` can be expressed as a particular resummation of -- the sum over all compositions, by grouping together the compositions of the same -- integer `n`. The convergence of the whole sum therefore implies the converence of the sum -- of `q.comp p n` have E : has_sum (λ n, (q.comp p) n (λ j, y)) (g (f (x + y))), { apply D.sigma, assume n, dsimp [formal_multilinear_series.comp], convert has_sum_fintype _, simp only [continuous_multilinear_map.sum_apply], refl }, exact E end /-- If two functions `g` and `f` are analytic respectively at `f x` and `x`, then `g ∘ f` is analytic at `x`. -/ theorem analytic_at.comp {g : F → G} {f : E → F} {x : E} (hg : analytic_at 𝕜 g (f x)) (hf : analytic_at 𝕜 f x) : analytic_at 𝕜 (g ∘ f) x := let ⟨q, hq⟩ := hg, ⟨p, hp⟩ := hf in (hq.comp hp).analytic_at /-! ### Associativity of the composition of formal multilinear series In this paragraph, we us prove the associativity of the composition of formal power series. By definition, ``` (r.comp q).comp p n v = ∑_{i₁ + ... + iₖ = n} (r.comp q)ₖ (p_{i₁} (v₀, ..., v_{i₁ -1}), p_{i₂} (...), ..., p_{iₖ}(...)) = ∑_{a : composition n} (r.comp q) a.length (apply_composition p a v) ``` decomposing `r.comp q` in the same way, we get ``` (r.comp q).comp p n v = ∑_{a : composition n} ∑_{b : composition a.length} r b.length (apply_composition q b (apply_composition p a v)) ``` On the other hand, ``` r.comp (q.comp p) n v = ∑_{c : composition n} r c.length (apply_composition (q.comp p) c v) ``` Here, `apply_composition (q.comp p) c v` is a vector of length `c.length`, whose `i`-th term is given by `(q.comp p) (c.blocks_fun i) (v_l, v_{l+1}, ..., v_{m-1})` where `{l, ..., m-1}` is the `i`-th block in the composition `c`, of length `c.blocks_fun i` by definition. To compute this term, we expand it as `∑_{dᵢ : composition (c.blocks_fun i)} q dᵢ.length (apply_composition p dᵢ v')`, where `v' = (v_l, v_{l+1}, ..., v_{m-1})`. Therefore, we get ``` r.comp (q.comp p) n v = ∑_{c : composition n} ∑_{d₀ : composition (c.blocks_fun 0), ..., d_{c.length - 1} : composition (c.blocks_fun (c.length - 1))} r c.length (λ i, q dᵢ.length (apply_composition p dᵢ v'ᵢ)) ``` To show that these terms coincide, we need to explain how to reindex the sums to put them in bijection (and then the terms we are summing will correspond to each other). Suppose we have a composition `a` of `n`, and a composition `b` of `a.length`. Then `b` indicates how to group together some blocks of `a`, giving altogether `b.length` blocks of blocks. These blocks of blocks can be called `d₀, ..., d_{a.length - 1}`, and one obtains a composition `c` of `n` by saying that each `dᵢ` is one single block. Conversely, if one starts from `c` and the `dᵢ`s, one can concatenate the `dᵢ`s to obtain a composition `a` of `n`, and register the lengths of the `dᵢ`s in a composition `b` of `a.length`. An example might be enlightening. Suppose `a = [2, 2, 3, 4, 2]`. It is a composition of length 5 of 13. The content of the blocks may be represented as `0011222333344`. Now take `b = [2, 3]` as a composition of `a.length = 5`. It says that the first 2 blocks of `a` should be merged, and the last 3 blocks of `a` should be merged, giving a new composition of `13` made of two blocks of length `4` and `9`, i.e., `c = [4, 9]`. But one can also remember that the new first block was initially made of two blocks of size `2`, so `d₀ = [2, 2]`, and the new second block was initially made of three blocks of size `3`, `4` and `2`, so `d₁ = [3, 4, 2]`. This equivalence is called `composition.sigma_equiv_sigma_pi n` below. We start with preliminary results on compositions, of a very specialized nature, then define the equivalence `composition.sigma_equiv_sigma_pi n`, and we deduce finally the associativity of composition of formal multilinear series in `formal_multilinear_series.comp_assoc`. -/ namespace composition variable {n : ℕ} /-- Rewriting equality in the dependent type `Σ (a : composition n), composition a.length)` in non-dependent terms with lists, requiring that the blocks coincide. -/ lemma sigma_composition_eq_iff (i j : Σ (a : composition n), composition a.length) : i = j ↔ i.1.blocks = j.1.blocks ∧ i.2.blocks = j.2.blocks := begin refine ⟨by rintro rfl; exact ⟨rfl, rfl⟩, _⟩, rcases i with ⟨a, b⟩, rcases j with ⟨a', b'⟩, rintros ⟨h, h'⟩, have H : a = a', by { ext1, exact h }, induction H, congr, ext1, exact h' end /-- Rewriting equality in the dependent type `Σ (c : composition n), Π (i : fin c.length), composition (c.blocks_fun i)` in non-dependent terms with lists, requiring that the lists of blocks coincide. -/ lemma sigma_pi_composition_eq_iff (u v : Σ (c : composition n), Π (i : fin c.length), composition (c.blocks_fun i)) : u = v ↔ of_fn (λ i, (u.2 i).blocks) = of_fn (λ i, (v.2 i).blocks) := begin refine ⟨λ H, by rw H, λ H, _⟩, rcases u with ⟨a, b⟩, rcases v with ⟨a', b'⟩, dsimp at H, have h : a = a', { ext1, have : map list.sum (of_fn (λ (i : fin (composition.length a)), (b i).blocks)) = map list.sum (of_fn (λ (i : fin (composition.length a')), (b' i).blocks)), by rw H, simp only [map_of_fn] at this, change of_fn (λ (i : fin (composition.length a)), (b i).blocks.sum) = of_fn (λ (i : fin (composition.length a')), (b' i).blocks.sum) at this, simpa [composition.blocks_sum, composition.of_fn_blocks_fun] using this }, induction h, simp only [true_and, eq_self_iff_true, heq_iff_eq], ext i : 2, have : nth_le (of_fn (λ (i : fin (composition.length a)), (b i).blocks)) i (by simp [i.is_lt]) = nth_le (of_fn (λ (i : fin (composition.length a)), (b' i).blocks)) i (by simp [i.is_lt]) := nth_le_of_eq H _, rwa [nth_le_of_fn, nth_le_of_fn] at this end /-- When `a` is a composition of `n` and `b` is a composition of `a.length`, `a.gather b` is the composition of `n` obtained by gathering all the blocks of `a` corresponding to a block of `b`. For instance, if `a = [6, 5, 3, 5, 2]` and `b = [2, 3]`, one should gather together the first two blocks of `a` and its last three blocks, giving `a.gather b = [11, 10]`. -/ def gather (a : composition n) (b : composition a.length) : composition n := { blocks := (a.blocks.split_wrt_composition b).map sum, blocks_pos := begin rw forall_mem_map_iff, intros j hj, suffices H : ∀ i ∈ j, 1 ≤ i, from calc 0 < j.length : length_pos_of_mem_split_wrt_composition hj ... ≤ j.sum : length_le_sum_of_one_le _ H, intros i hi, apply a.one_le_blocks, rw ← a.blocks.join_split_wrt_composition b, exact mem_join_of_mem hj hi, end, blocks_sum := by { rw [← sum_join, join_split_wrt_composition, a.blocks_sum] } } lemma length_gather (a : composition n) (b : composition a.length) : length (a.gather b) = b.length := show (map list.sum (a.blocks.split_wrt_composition b)).length = b.blocks.length, by rw [length_map, length_split_wrt_composition] /-- An auxiliary function used in the definition of `sigma_equiv_sigma_pi` below, associating to two compositions `a` of `n` and `b` of `a.length`, and an index `i` bounded by the length of `a.gather b`, the subcomposition of `a` made of those blocks belonging to the `i`-th block of `a.gather b`. -/ def sigma_composition_aux (a : composition n) (b : composition a.length) (i : fin (a.gather b).length) : composition ((a.gather b).blocks_fun i) := { blocks := nth_le (a.blocks.split_wrt_composition b) i (by { rw [length_split_wrt_composition, ← length_gather], exact i.2 }), blocks_pos := assume i hi, a.blocks_pos (by { rw ← a.blocks.join_split_wrt_composition b, exact mem_join_of_mem (nth_le_mem _ _ _) hi }), blocks_sum := by simp only [composition.blocks_fun, nth_le_map', composition.gather, fin.val_eq_coe] } /- Where did the fin.val come from in the proof on the preceding line? -/ lemma length_sigma_composition_aux (a : composition n) (b : composition a.length) (i : fin b.length) : composition.length (composition.sigma_composition_aux a b ⟨i, (length_gather a b).symm ▸ i.2⟩) = composition.blocks_fun b i := show list.length (nth_le (split_wrt_composition a.blocks b) i _) = blocks_fun b i, by { rw [nth_le_map_rev list.length, nth_le_of_eq (map_length_split_wrt_composition _ _)], refl } lemma blocks_fun_sigma_composition_aux (a : composition n) (b : composition a.length) (i : fin b.length) (j : fin (blocks_fun b i)) : blocks_fun (sigma_composition_aux a b ⟨i, (length_gather a b).symm ▸ i.2⟩) ⟨j, (length_sigma_composition_aux a b i).symm ▸ j.2⟩ = blocks_fun a (embedding b i j) := show nth_le (nth_le _ _ _) _ _ = nth_le a.blocks _ _, by { rw [nth_le_of_eq (nth_le_split_wrt_composition _ _ _), nth_le_drop', nth_le_take'], refl } /-- Auxiliary lemma to prove that the composition of formal multilinear series is associative. Consider a composition `a` of `n` and a composition `b` of `a.length`. Grouping together some blocks of `a` according to `b` as in `a.gather b`, one can compute the total size of the blocks of `a` up to an index `size_up_to b i + j` (where the `j` corresponds to a set of blocks of `a` that do not fill a whole block of `a.gather b`). The first part corresponds to a sum of blocks in `a.gather b`, and the second one to a sum of blocks in the next block of `sigma_composition_aux a b`. This is the content of this lemma. -/ lemma size_up_to_size_up_to_add (a : composition n) (b : composition a.length) {i j : ℕ} (hi : i < b.length) (hj : j < blocks_fun b ⟨i, hi⟩) : size_up_to a (size_up_to b i + j) = size_up_to (a.gather b) i + (size_up_to (sigma_composition_aux a b ⟨i, (length_gather a b).symm ▸ hi⟩) j) := begin induction j with j IHj, { show sum (take ((b.blocks.take i).sum) a.blocks) = sum (take i (map sum (split_wrt_composition a.blocks b))), induction i with i IH, { refl }, { have A : i < b.length := nat.lt_of_succ_lt hi, have B : i < list.length (map list.sum (split_wrt_composition a.blocks b)), by simp [A], have C : 0 < blocks_fun b ⟨i, A⟩ := composition.blocks_pos' _ _ _, rw [sum_take_succ _ _ B, ← IH A C], have : take (sum (take i b.blocks)) a.blocks = take (sum (take i b.blocks)) (take (sum (take (i+1) b.blocks)) a.blocks), { rw [take_take, min_eq_left], apply monotone_sum_take _ (nat.le_succ _) }, rw [this, nth_le_map', nth_le_split_wrt_composition, ← take_append_drop (sum (take i b.blocks)) ((take (sum (take (nat.succ i) b.blocks)) a.blocks)), sum_append], congr, rw [take_append_drop] } }, { have A : j < blocks_fun b ⟨i, hi⟩ := lt_trans (lt_add_one j) hj, have B : j < length (sigma_composition_aux a b ⟨i, (length_gather a b).symm ▸ hi⟩), by { convert A, rw [← length_sigma_composition_aux], refl }, have C : size_up_to b i + j < size_up_to b (i + 1), { simp only [size_up_to_succ b hi, add_lt_add_iff_left], exact A }, have D : size_up_to b i + j < length a := lt_of_lt_of_le C (b.size_up_to_le _), have : size_up_to b i + nat.succ j = (size_up_to b i + j).succ := rfl, rw [this, size_up_to_succ _ D, IHj A, size_up_to_succ _ B], simp only [sigma_composition_aux, add_assoc, add_left_inj, fin.coe_mk], rw [nth_le_of_eq (nth_le_split_wrt_composition _ _ _), nth_le_drop', nth_le_take _ _ C] } end /-- Natural equivalence between `(Σ (a : composition n), composition a.length)` and `(Σ (c : composition n), Π (i : fin c.length), composition (c.blocks_fun i))`, that shows up as a change of variables in the proof that composition of formal multilinear series is associative. Consider a composition `a` of `n` and a composition `b` of `a.length`. Then `b` indicates how to group together some blocks of `a`, giving altogether `b.length` blocks of blocks. These blocks of blocks can be called `d₀, ..., d_{a.length - 1}`, and one obtains a composition `c` of `n` by saying that each `dᵢ` is one single block. The map `⟨a, b⟩ → ⟨c, (d₀, ..., d_{a.length - 1})⟩` is the direct map in the equiv. Conversely, if one starts from `c` and the `dᵢ`s, one can join the `dᵢ`s to obtain a composition `a` of `n`, and register the lengths of the `dᵢ`s in a composition `b` of `a.length`. This is the inverse map of the equiv. -/ def sigma_equiv_sigma_pi (n : ℕ) : (Σ (a : composition n), composition a.length) ≃ (Σ (c : composition n), Π (i : fin c.length), composition (c.blocks_fun i)) := { to_fun := λ i, ⟨i.1.gather i.2, i.1.sigma_composition_aux i.2⟩, inv_fun := λ i, ⟨ { blocks := (of_fn (λ j, (i.2 j).blocks)).join, blocks_pos := begin simp only [and_imp, mem_join, exists_imp_distrib, forall_mem_of_fn_iff], exact λ i j hj, composition.blocks_pos _ hj end, blocks_sum := by simp [sum_of_fn, composition.blocks_sum, composition.sum_blocks_fun] }, { blocks := of_fn (λ j, (i.2 j).length), blocks_pos := forall_mem_of_fn_iff.2 (λ j, composition.length_pos_of_pos _ (composition.blocks_pos' _ _ _)), blocks_sum := by { dsimp only [composition.length], simp [sum_of_fn] } }⟩, left_inv := begin -- the fact that we have a left inverse is essentially `join_split_wrt_composition`, -- but we need to massage it to take care of the dependent setting. rintros ⟨a, b⟩, rw sigma_composition_eq_iff, dsimp, split, { have A := length_map list.sum (split_wrt_composition a.blocks b), conv_rhs { rw [← join_split_wrt_composition a.blocks b, ← of_fn_nth_le (split_wrt_composition a.blocks b)] }, congr, { exact A }, { exact (fin.heq_fun_iff A).2 (λ i, rfl) } }, { have B : composition.length (composition.gather a b) = list.length b.blocks := composition.length_gather _ _, conv_rhs { rw [← of_fn_nth_le b.blocks] }, congr' 1, { exact B }, { apply (fin.heq_fun_iff B).2 (λ i, _), rw [sigma_composition_aux, composition.length, nth_le_map_rev list.length, nth_le_of_eq (map_length_split_wrt_composition _ _)], refl } } end, right_inv := begin -- the fact that we have a right inverse is essentially `split_wrt_composition_join`, -- but we need to massage it to take care of the dependent setting. rintros ⟨c, d⟩, have : map list.sum (of_fn (λ (i : fin (composition.length c)), (d i).blocks)) = c.blocks, by simp [map_of_fn, (∘), composition.blocks_sum, composition.of_fn_blocks_fun], rw sigma_pi_composition_eq_iff, dsimp, congr, { ext1, dsimp [composition.gather], rwa split_wrt_composition_join, simp only [map_of_fn] }, { rw fin.heq_fun_iff, { assume i, dsimp [composition.sigma_composition_aux], rw [nth_le_of_eq (split_wrt_composition_join _ _ _)], { simp only [nth_le_of_fn'] }, { simp only [map_of_fn] } }, { congr, ext1, dsimp [composition.gather], rwa split_wrt_composition_join, simp only [map_of_fn] } } end } end composition namespace formal_multilinear_series open composition theorem comp_assoc (r : formal_multilinear_series 𝕜 G H) (q : formal_multilinear_series 𝕜 F G) (p : formal_multilinear_series 𝕜 E F) : (r.comp q).comp p = r.comp (q.comp p) := begin ext n v, /- First, rewrite the two compositions appearing in the theorem as two sums over complicated sigma types, as in the description of the proof above. -/ let f : (Σ (a : composition n), composition a.length) → H := λ ⟨a, b⟩, r b.length (apply_composition q b (apply_composition p a v)), let g : (Σ (c : composition n), Π (i : fin c.length), composition (c.blocks_fun i)) → H := λ ⟨c, d⟩, r c.length (λ (i : fin c.length), q (d i).length (apply_composition p (d i) (v ∘ c.embedding i))), suffices A : ∑ c, f c = ∑ c, g c, { dsimp [formal_multilinear_series.comp], simp only [continuous_multilinear_map.sum_apply, comp_along_composition_apply], rw ← @finset.sum_sigma _ _ _ _ (finset.univ : finset (composition n)) _ f, dsimp [apply_composition], simp only [continuous_multilinear_map.sum_apply, comp_along_composition_apply, continuous_multilinear_map.map_sum], rw ← @finset.sum_sigma _ _ _ _ (finset.univ : finset (composition n)) _ g, exact A }, /- Now, we use `composition.sigma_equiv_sigma_pi n` to change variables in the second sum, and check that we get exactly the same sums. -/ rw ← finset.sum_equiv (sigma_equiv_sigma_pi n), /- To check that we have the same terms, we should check that we apply the same component of `r`, and the same component of `q`, and the same component of `p`, to the same coordinate of `v`. This is true by definition, but at each step one needs to convince Lean that the types one considers are the same, using a suitable congruence lemma to avoid dependent type issues. This dance has to be done three times, one for `r`, one for `q` and one for `p`.-/ apply finset.sum_congr rfl, rintros ⟨a, b⟩ _, dsimp [f, g, sigma_equiv_sigma_pi], -- check that the `r` components are the same. Based on `composition.length_gather` apply r.congr (composition.length_gather a b).symm, intros i hi1 hi2, -- check that the `q` components are the same. Based on `length_sigma_composition_aux` apply q.congr (length_sigma_composition_aux a b _).symm, intros j hj1 hj2, -- check that the `p` components are the same. Based on `blocks_fun_sigma_composition_aux` apply p.congr (blocks_fun_sigma_composition_aux a b _ _).symm, intros k hk1 hk2, -- finally, check that the coordinates of `v` one is using are the same. Based on -- `size_up_to_size_up_to_add`. refine congr_arg v (fin.eq_of_veq _), dsimp [composition.embedding], rw [size_up_to_size_up_to_add _ _ hi1 hj1, add_assoc], end end formal_multilinear_series
31959be51913dfa04d61034c04eac5458d559db3
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/group_theory/submonoid/center.lean
c41a592df9157009d7f063d71febaaa302aab2d8
[ "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,526
lean
/- Copyright (c) 2021 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import group_theory.submonoid.operations import group_theory.subsemigroup.center /-! # Centers of monoids ## Main definitions * `submonoid.center`: the center of a monoid * `add_submonoid.center`: the center of an additive monoid We provide `subgroup.center`, `add_subgroup.center`, `subsemiring.center`, and `subring.center` in other files. -/ namespace submonoid section variables (M : Type*) [monoid M] /-- The center of a monoid `M` is the set of elements that commute with everything in `M` -/ @[to_additive "The center of a monoid `M` is the set of elements that commute with everything in `M`"] def center : submonoid M := { carrier := set.center M, one_mem' := set.one_mem_center M, mul_mem' := λ a b, set.mul_mem_center } @[to_additive] lemma coe_center : ↑(center M) = set.center M := rfl @[simp] lemma center_to_subsemigroup : (center M).to_subsemigroup = subsemigroup.center M := rfl lemma _root_.add_submonoid.center_to_add_subsemigroup (M) [add_monoid M] : (add_submonoid.center M).to_add_subsemigroup = add_subsemigroup.center M := rfl attribute [to_additive add_submonoid.center_to_add_subsemigroup] submonoid.center_to_subsemigroup variables {M} @[to_additive] lemma mem_center_iff {z : M} : z ∈ center M ↔ ∀ g, g * z = z * g := iff.rfl @[to_additive] instance decidable_mem_center (a) [decidable $ ∀ b : M, b * a = a * b] : decidable (a ∈ center M) := decidable_of_iff' _ mem_center_iff /-- The center of a monoid is commutative. -/ instance : comm_monoid (center M) := { mul_comm := λ a b, subtype.ext $ b.prop _, .. (center M).to_monoid } /-- The center of a monoid acts commutatively on that monoid. -/ instance center.smul_comm_class_left : smul_comm_class (center M) M M := { smul_comm := λ m x y, (commute.left_comm (m.prop x) y).symm } /-- The center of a monoid acts commutatively on that monoid. -/ instance center.smul_comm_class_right : smul_comm_class M (center M) M := smul_comm_class.symm _ _ _ /-! Note that `smul_comm_class (center M) (center M) M` is already implied by `submonoid.smul_comm_class_right` -/ example : smul_comm_class (center M) (center M) M := by apply_instance end section variables (M : Type*) [comm_monoid M] @[simp] lemma center_eq_top : center M = ⊤ := set_like.coe_injective (set.center_eq_univ M) end end submonoid -- Guard against import creep assert_not_exists finset
cff9f92dcc1e759acf42ce463dd6e856823ab5d6
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/algebra/euclidean_domain.lean
6247db4be0f3ac873f6afc7c249c6cb683b7dc7f
[ "Apache-2.0" ]
permissive
waynemunro/mathlib
e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552
065a70810b5480d584033f7bbf8e0409480c2118
refs/heads/master
1,693,417,182,397
1,634,644,781,000
1,634,644,781,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
17,123
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 -/ import data.int.basic import algebra.field /-! # Euclidean domains This file introduces Euclidean domains and provides the extended Euclidean algorithm. To be precise, a slightly more general version is provided which is sometimes called a transfinite Euclidean domain and differs in the fact that the degree function need not take values in `ℕ` but can take values in any well-ordered set. Transfinite Euclidean domains were introduced by Motzkin and examples which don't satisfy the classical notion were provided independently by Hiblot and Nagata. ## Main definitions * `euclidean_domain`: Defines Euclidean domain with functions `quotient` and `remainder`. Instances of `has_div` and `has_mod` are provided, so that one can write `a = b * (a / b) + a % b`. * `gcd`: defines the greatest common divisors of two elements of a Euclidean domain. * `xgcd`: given two elements `a b : R`, `xgcd a b` defines the pair `(x, y)` such that `x * a + y * b = gcd a b`. * `lcm`: defines the lowest common multiple of two elements `a` and `b` of a Euclidean domain as `a * b / (gcd a b)` ## Main statements * `gcd_eq_gcd_ab`: states Bézout's lemma for Euclidean domains. * `int.euclidean_domain`: shows that `ℤ` is a Euclidean domain. * `field.to_euclidean_domain`: shows that any field is a Euclidean domain. ## Notation `≺` denotes the well founded relation on the Euclidean domain, e.g. in the example of the polynomial ring over a field, `p ≺ q` for polynomials `p` and `q` if and only if the degree of `p` is less than the degree of `q`. ## Implementation details Instead of working with a valuation, `euclidean_domain` is implemented with the existence of a well founded relation `r` on the integral domain `R`, which in the example of `ℤ` would correspond to setting `i ≺ j` for integers `i` and `j` if the absolute value of `i` is smaller than the absolute value of `j`. ## References * [Th. Motzkin, *The Euclidean algorithm*][MR32592] * [J.-J. Hiblot, *Des anneaux euclidiens dont le plus petit algorithme n'est pas à valeurs finies*] [MR399081] * [M. Nagata, *On Euclid algorithm*][MR541021] ## Tags Euclidean domain, transfinite Euclidean domain, Bézout's lemma -/ universe u /-- 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. -/ @[protect_proj without mul_left_not_lt r_well_founded] class euclidean_domain (R : Type u) extends comm_ring R, nontrivial R := (quotient : R → R → R) (quotient_zero : ∀ a, quotient a 0 = 0) (remainder : R → R → R) (quotient_mul_add_remainder_eq : ∀ a b, b * quotient a b + remainder a b = a) (r : R → R → Prop) (r_well_founded : well_founded r) (remainder_lt : ∀ a {b}, b ≠ 0 → r (remainder a b) b) (mul_left_not_lt : ∀ a {b}, b ≠ 0 → ¬r (a * b) a) namespace euclidean_domain variable {R : Type u} variables [euclidean_domain R] local infix ` ≺ `:50 := euclidean_domain.r @[priority 70] -- see Note [lower instance priority] instance : has_div R := ⟨euclidean_domain.quotient⟩ @[priority 70] -- see Note [lower instance priority] instance : has_mod R := ⟨euclidean_domain.remainder⟩ theorem div_add_mod (a b : R) : b * (a / b) + a % b = a := euclidean_domain.quotient_mul_add_remainder_eq _ _ lemma mod_add_div (a b : R) : a % b + b * (a / b) = a := (add_comm _ _).trans (div_add_mod _ _) lemma mod_add_div' (m k : R) : m % k + (m / k) * k = m := by { rw mul_comm, exact mod_add_div _ _ } lemma div_add_mod' (m k : R) : (m / k) * k + m % k = m := by { rw mul_comm, exact div_add_mod _ _ } lemma mod_eq_sub_mul_div {R : Type*} [euclidean_domain R] (a b : R) : a % b = a - b * (a / b) := calc a % b = b * (a / b) + a % b - b * (a / b) : (add_sub_cancel' _ _).symm ... = a - b * (a / b) : by rw div_add_mod theorem mod_lt : ∀ a {b : R}, b ≠ 0 → (a % b) ≺ b := euclidean_domain.remainder_lt theorem mul_right_not_lt {a : R} (b) (h : a ≠ 0) : ¬(a * b) ≺ b := by { rw mul_comm, exact mul_left_not_lt b h } lemma mul_div_cancel_left {a : R} (b) (a0 : a ≠ 0) : a * b / a = b := eq.symm $ eq_of_sub_eq_zero $ classical.by_contradiction $ λ h, begin have := mul_left_not_lt a h, rw [mul_sub, sub_eq_iff_eq_add'.2 (div_add_mod (a*b) a).symm] at this, exact this (mod_lt _ a0) end lemma mul_div_cancel (a) {b : R} (b0 : b ≠ 0) : a * b / b = a := by { rw mul_comm, exact mul_div_cancel_left a b0 } @[simp] lemma mod_zero (a : R) : a % 0 = a := by simpa only [zero_mul, zero_add] using div_add_mod a 0 @[simp] lemma mod_eq_zero {a b : R} : a % b = 0 ↔ b ∣ a := ⟨λ h, by { rw [← div_add_mod a b, h, add_zero], exact dvd_mul_right _ _ }, λ ⟨c, e⟩, begin rw [e, ← add_left_cancel_iff, div_add_mod, add_zero], haveI := classical.dec, by_cases b0 : b = 0, { simp only [b0, zero_mul] }, { rw [mul_div_cancel_left _ b0] } end⟩ @[simp] lemma mod_self (a : R) : a % a = 0 := mod_eq_zero.2 dvd_rfl lemma dvd_mod_iff {a b c : R} (h : c ∣ b) : c ∣ a % b ↔ c ∣ a := by rw [dvd_add_iff_right (h.mul_right _), div_add_mod] lemma lt_one (a : R) : a ≺ (1:R) → a = 0 := by { haveI := classical.dec, exact not_imp_not.1 (λ h, by simpa only [one_mul] using mul_left_not_lt 1 h) } lemma val_dvd_le : ∀ a b : R, b ∣ a → a ≠ 0 → ¬a ≺ b | _ b ⟨d, rfl⟩ ha := mul_left_not_lt b (mt (by { rintro rfl, exact mul_zero _ }) ha) @[simp] lemma mod_one (a : R) : a % 1 = 0 := mod_eq_zero.2 (one_dvd _) @[simp] lemma zero_mod (b : R) : 0 % b = 0 := mod_eq_zero.2 (dvd_zero _) @[simp, priority 900] lemma div_zero (a : R) : a / 0 = 0 := euclidean_domain.quotient_zero a @[simp, priority 900] lemma zero_div {a : R} : 0 / a = 0 := classical.by_cases (λ a0 : a = 0, a0.symm ▸ div_zero 0) (λ a0, by simpa only [zero_mul] using mul_div_cancel 0 a0) @[simp, priority 900] lemma div_self {a : R} (a0 : a ≠ 0) : a / a = 1 := by simpa only [one_mul] using mul_div_cancel 1 a0 lemma eq_div_of_mul_eq_left {a b c : R} (hb : b ≠ 0) (h : a * b = c) : a = c / b := by rw [← h, mul_div_cancel _ hb] lemma eq_div_of_mul_eq_right {a b c : R} (ha : a ≠ 0) (h : a * b = c) : b = c / a := by rw [← h, mul_div_cancel_left _ ha] theorem mul_div_assoc (x : R) {y z : R} (h : z ∣ y) : x * y / z = x * (y / z) := begin classical, by_cases hz : z = 0, { subst hz, rw [div_zero, div_zero, mul_zero] }, rcases h with ⟨p, rfl⟩, rw [mul_div_cancel_left _ hz, mul_left_comm, mul_div_cancel_left _ hz] end section open_locale classical @[elab_as_eliminator] theorem gcd.induction {P : R → R → Prop} : ∀ a b : R, (∀ x, P 0 x) → (∀ a b, a ≠ 0 → P (b % a) a → P a b) → P a b | a := λ b H0 H1, if a0 : a = 0 then a0.symm ▸ H0 _ else have h:_ := mod_lt b a0, H1 _ _ a0 (gcd.induction (b%a) a H0 H1) using_well_founded {dec_tac := tactic.assumption, rel_tac := λ _ _, `[exact ⟨_, r_well_founded⟩]} end section gcd variable [decidable_eq R] /-- `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 → R → R | a := λ b, if a0 : a = 0 then b else have h:_ := mod_lt b a0, gcd (b%a) a using_well_founded {dec_tac := tactic.assumption, rel_tac := λ _ _, `[exact ⟨_, r_well_founded⟩]} @[simp] theorem gcd_zero_left (a : R) : gcd 0 a = a := by { rw gcd, exact if_pos rfl } @[simp] theorem gcd_zero_right (a : R) : gcd a 0 = a := by { rw gcd, split_ifs; simp only [h, zero_mod, gcd_zero_left] } theorem gcd_val (a b : R) : gcd a b = gcd (b % a) a := by { rw gcd, split_ifs; [simp only [h, mod_zero, gcd_zero_right], refl]} theorem gcd_dvd (a b : R) : gcd a b ∣ a ∧ gcd a b ∣ b := gcd.induction a b (λ b, by { rw gcd_zero_left, exact ⟨dvd_zero _, dvd_rfl⟩ }) (λ a b aneq ⟨IH₁, IH₂⟩, by { rw gcd_val, exact ⟨IH₂, (dvd_mod_iff IH₂).1 IH₁⟩ }) theorem gcd_dvd_left (a b : R) : gcd a b ∣ a := (gcd_dvd a b).left theorem gcd_dvd_right (a b : R) : gcd a b ∣ b := (gcd_dvd a b).right protected theorem gcd_eq_zero_iff {a b : R} : gcd a b = 0 ↔ a = 0 ∧ b = 0 := ⟨λ h, by simpa [h] using gcd_dvd a b, by { rintro ⟨rfl, rfl⟩, exact gcd_zero_right _ }⟩ theorem dvd_gcd {a b c : R} : c ∣ a → c ∣ b → c ∣ gcd a b := gcd.induction a b (λ _ _ H, by simpa only [gcd_zero_left] using H) (λ a b a0 IH ca cb, by { rw gcd_val, exact IH ((dvd_mod_iff ca).2 cb) ca }) theorem gcd_eq_left {a b : R} : gcd a b = a ↔ a ∣ b := ⟨λ h, by {rw ← h, apply gcd_dvd_right }, λ h, by rw [gcd_val, mod_eq_zero.2 h, gcd_zero_left]⟩ @[simp] theorem gcd_one_left (a : R) : gcd 1 a = 1 := gcd_eq_left.2 (one_dvd _) @[simp] theorem gcd_self (a : R) : gcd a a = a := gcd_eq_left.2 dvd_rfl /-- 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 → R → R → R → R → R → R × R × R | r := λ s t r' s' t', if hr : r = 0 then (r', s', t') else have r' % r ≺ r, from mod_lt _ hr, let q := r' / r in xgcd_aux (r' % r) (s' - q * s) (t' - q * t) r s t using_well_founded {dec_tac := tactic.assumption, rel_tac := λ _ _, `[exact ⟨_, r_well_founded⟩]} @[simp] theorem xgcd_zero_left {s t r' s' t' : R} : xgcd_aux 0 s t r' s' t' = (r', s', t') := by { unfold xgcd_aux, exact if_pos rfl } theorem xgcd_aux_rec {r s t r' s' 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 := by { conv {to_lhs, rw [xgcd_aux]}, exact if_neg h} /-- Use the extended GCD algorithm to generate the `a` and `b` values satisfying `gcd x y = x * a + y * b`. -/ def xgcd (x y : R) : R × R := (xgcd_aux x 1 0 y 0 1).2 /-- The extended GCD `a` value in the equation `gcd x y = x * a + y * b`. -/ def gcd_a (x y : R) : R := (xgcd x y).1 /-- The extended GCD `b` value in the equation `gcd x y = x * a + y * b`. -/ def gcd_b (x y : R) : R := (xgcd x y).2 @[simp] theorem gcd_a_zero_left {s : R} : gcd_a 0 s = 0 := by { unfold gcd_a, rw [xgcd, xgcd_zero_left] } @[simp] theorem gcd_b_zero_left {s : R} : gcd_b 0 s = 1 := by { unfold gcd_b, rw [xgcd, xgcd_zero_left] } @[simp] theorem xgcd_aux_fst (x y : R) : ∀ s t s' t', (xgcd_aux x s t y s' t').1 = gcd x y := gcd.induction x y (by { intros, rw [xgcd_zero_left, gcd_zero_left] }) (λ x y h IH s t s' t', by { simp only [xgcd_aux_rec h, if_neg h, IH], rw ← gcd_val }) theorem xgcd_aux_val (x y : R) : xgcd_aux x 1 0 y 0 1 = (gcd x y, xgcd x y) := by rw [xgcd, ← xgcd_aux_fst x y 1 0 0 1, prod.mk.eta] theorem xgcd_val (x y : R) : xgcd x y = (gcd_a x y, gcd_b x y) := prod.mk.eta.symm private def P (a b : R) : R × R × R → Prop | (r, s, t) := (r : R) = a * s + b * t theorem xgcd_aux_P (a b : R) {r r' : R} : ∀ {s t s' t'}, P a b (r, s, t) → P a b (r', s', t') → P a b (xgcd_aux r s t r' s' t') := gcd.induction r r' (by { intros, simpa only [xgcd_zero_left] }) $ λ x y h IH s t s' t' p p', begin rw [xgcd_aux_rec h], refine IH _ p, unfold P at p p' ⊢, rw [mul_sub, mul_sub, add_sub, sub_add_eq_add_sub, ← p', sub_sub, mul_comm _ s, ← mul_assoc, mul_comm _ t, ← mul_assoc, ← add_mul, ← p, mod_eq_sub_mul_div] end /-- An explicit version of **Bézout's lemma** for Euclidean domains. -/ theorem gcd_eq_gcd_ab (a b : R) : (gcd a b : R) = a * gcd_a a b + b * gcd_b a b := by { have := @xgcd_aux_P _ _ _ a b a b 1 0 0 1 (by rw [P, mul_one, mul_zero, add_zero]) (by rw [P, mul_one, mul_zero, zero_add]), rwa [xgcd_aux_val, xgcd_val] at this } @[priority 70] -- see Note [lower instance priority] instance (R : Type*) [e : euclidean_domain R] : integral_domain R := by { haveI := classical.dec_eq R, exact { eq_zero_or_eq_zero_of_mul_eq_zero := λ a b h, (or_iff_not_and_not.2 $ λ h0, h0.1 $ by rw [← mul_div_cancel a h0.2, h, zero_div]), ..e }} end gcd section lcm variables [decidable_eq R] /-- `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 (x y : R) : R := x * y / gcd x y theorem dvd_lcm_left (x y : R) : x ∣ lcm x y := classical.by_cases (assume hxy : gcd x y = 0, by { rw [lcm, hxy, div_zero], exact dvd_zero _ }) (λ hxy, let ⟨z, hz⟩ := (gcd_dvd x y).2 in ⟨z, eq.symm $ eq_div_of_mul_eq_left hxy $ by rw [mul_right_comm, mul_assoc, ← hz]⟩) theorem dvd_lcm_right (x y : R) : y ∣ lcm x y := classical.by_cases (assume hxy : gcd x y = 0, by { rw [lcm, hxy, div_zero], exact dvd_zero _ }) (λ hxy, let ⟨z, hz⟩ := (gcd_dvd x y).1 in ⟨z, eq.symm $ eq_div_of_mul_eq_right hxy $ by rw [← mul_assoc, mul_right_comm, ← hz]⟩) theorem lcm_dvd {x y z : R} (hxz : x ∣ z) (hyz : y ∣ z) : lcm x y ∣ z := begin rw lcm, by_cases hxy : gcd x y = 0, { rw [hxy, div_zero], rw euclidean_domain.gcd_eq_zero_iff at hxy, rwa hxy.1 at hxz }, rcases gcd_dvd x y with ⟨⟨r, hr⟩, ⟨s, hs⟩⟩, suffices : x * y ∣ z * gcd x y, { cases this with p hp, use p, generalize_hyp : gcd x y = g at hxy hs hp ⊢, subst hs, rw [mul_left_comm, mul_div_cancel_left _ hxy, ← mul_left_inj' hxy, hp], rw [← mul_assoc], simp only [mul_right_comm] }, rw [gcd_eq_gcd_ab, mul_add], apply dvd_add, { rw mul_left_comm, exact mul_dvd_mul_left _ (hyz.mul_right _) }, { rw [mul_left_comm, mul_comm], exact mul_dvd_mul_left _ (hxz.mul_right _) } end @[simp] lemma lcm_dvd_iff {x y z : R} : lcm x y ∣ z ↔ x ∣ z ∧ y ∣ z := ⟨λ hz, ⟨(dvd_lcm_left _ _).trans hz, (dvd_lcm_right _ _).trans hz⟩, λ ⟨hxz, hyz⟩, lcm_dvd hxz hyz⟩ @[simp] lemma lcm_zero_left (x : R) : lcm 0 x = 0 := by rw [lcm, zero_mul, zero_div] @[simp] lemma lcm_zero_right (x : R) : lcm x 0 = 0 := by rw [lcm, mul_zero, zero_div] @[simp] lemma lcm_eq_zero_iff {x y : R} : lcm x y = 0 ↔ x = 0 ∨ y = 0 := begin split, { intro hxy, rw [lcm, mul_div_assoc _ (gcd_dvd_right _ _), mul_eq_zero] at hxy, apply or_of_or_of_imp_right hxy, intro hy, by_cases hgxy : gcd x y = 0, { rw euclidean_domain.gcd_eq_zero_iff at hgxy, exact hgxy.2 }, { rcases gcd_dvd x y with ⟨⟨r, hr⟩, ⟨s, hs⟩⟩, generalize_hyp : gcd x y = g at hr hs hy hgxy ⊢, subst hs, rw [mul_div_cancel_left _ hgxy] at hy, rw [hy, mul_zero] } }, rintro (hx | hy), { rw [hx, lcm_zero_left] }, { rw [hy, lcm_zero_right] } end @[simp] lemma gcd_mul_lcm (x y : R) : gcd x y * lcm x y = x * y := begin rw lcm, by_cases h : gcd x y = 0, { rw [h, zero_mul], rw euclidean_domain.gcd_eq_zero_iff at h, rw [h.1, zero_mul] }, rcases gcd_dvd x y with ⟨⟨r, hr⟩, ⟨s, hs⟩⟩, generalize_hyp : gcd x y = g at h hr ⊢, subst hr, rw [mul_assoc, mul_div_cancel_left _ h] end end lcm end euclidean_domain instance int.euclidean_domain : euclidean_domain ℤ := { add := (+), mul := (*), one := 1, zero := 0, neg := has_neg.neg, quotient := (/), quotient_zero := int.div_zero, remainder := (%), quotient_mul_add_remainder_eq := λ a b, int.div_add_mod _ _, r := λ a b, a.nat_abs < b.nat_abs, r_well_founded := measure_wf (λ a, int.nat_abs a), remainder_lt := λ a b b0, int.coe_nat_lt.1 $ by { rw [int.nat_abs_of_nonneg (int.mod_nonneg _ b0), ← int.abs_eq_nat_abs], exact int.mod_lt _ b0 }, mul_left_not_lt := λ a b b0, not_lt_of_ge $ by {rw [← mul_one a.nat_abs, int.nat_abs_mul], exact mul_le_mul_of_nonneg_left (int.nat_abs_pos_of_ne_zero b0) (nat.zero_le _) }, .. int.comm_ring, .. int.nontrivial } @[priority 100] -- see Note [lower instance priority] instance field.to_euclidean_domain {K : Type u} [field K] : euclidean_domain K := { add := (+), mul := (*), one := 1, zero := 0, neg := has_neg.neg, quotient := (/), remainder := λ a b, a - a * b / b, quotient_zero := div_zero, quotient_mul_add_remainder_eq := λ a b, by { classical, by_cases b = 0; simp [h, mul_div_cancel'] }, r := λ a b, a = 0 ∧ b ≠ 0, r_well_founded := well_founded.intro $ λ a, acc.intro _ $ λ b ⟨hb, hna⟩, acc.intro _ $ λ c ⟨hc, hnb⟩, false.elim $ hnb hb, remainder_lt := λ a b hnb, by simp [hnb], mul_left_not_lt := λ a b hnb ⟨hab, hna⟩, or.cases_on (mul_eq_zero.1 hab) hna hnb, .. ‹field K› }
76019b15d43a86a36cbfb0a8e5b2df6e196e9287
367134ba5a65885e863bdc4507601606690974c1
/src/linear_algebra/projection.lean
7ee6d0117157e25a70cbad6451fc323d557386b3
[ "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
12,966
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Yury Kudryashov -/ import linear_algebra.basic import linear_algebra.prod /-! # Projection to a subspace In this file we define * `linear_proj_of_is_compl (p q : submodule R E) (h : is_compl p q)`: the projection of a module `E` to a submodule `p` along its complement `q`; it is the unique linear map `f : E → p` such that `f x = x` for `x ∈ p` and `f x = 0` for `x ∈ q`. * `is_compl_equiv_proj p`: equivalence between submodules `q` such that `is_compl p q` and projections `f : E → p`, `∀ x ∈ p, f x = x`. We also provide some lemmas justifying correctness of our definitions. ## Tags projection, complement subspace -/ variables {R : Type*} [ring R] {E : Type*} [add_comm_group E] [module R E] {F : Type*} [add_comm_group F] [module R F] {G : Type*} [add_comm_group G] [module R G] (p q : submodule R E) noncomputable theory namespace linear_map variable {p} open submodule lemma ker_id_sub_eq_of_proj {f : E →ₗ[R] p} (hf : ∀ x : p, f x = x) : ker (id - p.subtype.comp f) = p := begin ext x, simp only [comp_apply, mem_ker, subtype_apply, sub_apply, id_apply, sub_eq_zero], exact ⟨λ h, h.symm ▸ submodule.coe_mem _, λ hx, by erw [hf ⟨x, hx⟩, subtype.coe_mk]⟩ end lemma range_eq_of_proj {f : E →ₗ[R] p} (hf : ∀ x : p, f x = x) : range f = ⊤ := range_eq_top.2 $ λ x, ⟨x, hf x⟩ lemma is_compl_of_proj {f : E →ₗ[R] p} (hf : ∀ x : p, f x = x) : is_compl p f.ker := begin split, { rintros x ⟨hpx, hfx⟩, erw [mem_coe, mem_ker, hf ⟨x, hpx⟩, mk_eq_zero] at hfx, simp only [hfx, mem_coe, zero_mem] }, { intros x hx, rw [mem_sup'], refine ⟨f x, ⟨x - f x, _⟩, add_sub_cancel'_right _ _⟩, rw [mem_ker, linear_map.map_sub, hf, sub_self] } end end linear_map namespace submodule open linear_map /-- If `q` is a complement of `p`, then `M/p ≃ q`. -/ def quotient_equiv_of_is_compl (h : is_compl p q) : p.quotient ≃ₗ[R] q := linear_equiv.symm $ linear_equiv.of_bijective (p.mkq.comp q.subtype) (by simp only [ker_comp, ker_mkq, disjoint_iff_comap_eq_bot.1 h.symm.disjoint]) (by simp only [range_comp, range_subtype, map_mkq_eq_top, h.sup_eq_top]) @[simp] lemma quotient_equiv_of_is_compl_symm_apply (h : is_compl p q) (x : q) : (quotient_equiv_of_is_compl p q h).symm x = quotient.mk x := rfl @[simp] lemma quotient_equiv_of_is_compl_apply_mk_coe (h : is_compl p q) (x : q) : quotient_equiv_of_is_compl p q h (quotient.mk x) = x := (quotient_equiv_of_is_compl p q h).apply_symm_apply x @[simp] lemma mk_quotient_equiv_of_is_compl_apply (h : is_compl p q) (x : p.quotient) : (quotient.mk (quotient_equiv_of_is_compl p q h x) : p.quotient) = x := (quotient_equiv_of_is_compl p q h).symm_apply_apply x /-- If `q` is a complement of `p`, then `p × q` is isomorphic to `E`. It is the unique linear map `f : E → p` such that `f x = x` for `x ∈ p` and `f x = 0` for `x ∈ q`. -/ def prod_equiv_of_is_compl (h : is_compl p q) : (p × q) ≃ₗ[R] E := begin apply linear_equiv.of_bijective (p.subtype.coprod q.subtype), { simp only [ker_eq_bot', prod.forall, subtype_apply, prod.mk_eq_zero, coprod_apply], -- TODO: if I add `submodule.forall`, it unfolds the outer `∀` but not the inner one. rintros ⟨x, hx⟩ ⟨y, hy⟩, simp only [coe_mk, mk_eq_zero, ← eq_neg_iff_add_eq_zero], rintro rfl, rw [neg_mem_iff] at hx, simp [disjoint_def.1 h.disjoint y hx hy] }, { rw [← sup_eq_range, h.sup_eq_top] } end @[simp] lemma coe_prod_equiv_of_is_compl (h : is_compl p q) : (prod_equiv_of_is_compl p q h : (p × q) →ₗ[R] E) = p.subtype.coprod q.subtype := rfl @[simp] lemma coe_prod_equiv_of_is_compl' (h : is_compl p q) (x : p × q) : prod_equiv_of_is_compl p q h x = x.1 + x.2 := rfl @[simp] lemma prod_equiv_of_is_compl_symm_apply_left (h : is_compl p q) (x : p) : (prod_equiv_of_is_compl p q h).symm x = (x, 0) := (prod_equiv_of_is_compl p q h).symm_apply_eq.2 $ by simp @[simp] lemma prod_equiv_of_is_compl_symm_apply_right (h : is_compl p q) (x : q) : (prod_equiv_of_is_compl p q h).symm x = (0, x) := (prod_equiv_of_is_compl p q h).symm_apply_eq.2 $ by simp @[simp] lemma prod_equiv_of_is_compl_symm_apply_fst_eq_zero (h : is_compl p q) {x : E} : ((prod_equiv_of_is_compl p q h).symm x).1 = 0 ↔ x ∈ q := begin conv_rhs { rw [← (prod_equiv_of_is_compl p q h).apply_symm_apply x] }, rw [coe_prod_equiv_of_is_compl', submodule.add_mem_iff_left _ (submodule.coe_mem _), mem_right_iff_eq_zero_of_disjoint h.disjoint] end @[simp] lemma prod_equiv_of_is_compl_symm_apply_snd_eq_zero (h : is_compl p q) {x : E} : ((prod_equiv_of_is_compl p q h).symm x).2 = 0 ↔ x ∈ p := begin conv_rhs { rw [← (prod_equiv_of_is_compl p q h).apply_symm_apply x] }, rw [coe_prod_equiv_of_is_compl', submodule.add_mem_iff_right _ (submodule.coe_mem _), mem_left_iff_eq_zero_of_disjoint h.disjoint] end /-- Projection to a submodule along its complement. -/ def linear_proj_of_is_compl (h : is_compl p q) : E →ₗ[R] p := (linear_map.fst R p q).comp $ (prod_equiv_of_is_compl p q h).symm variables {p q} @[simp] lemma linear_proj_of_is_compl_apply_left (h : is_compl p q) (x : p) : linear_proj_of_is_compl p q h x = x := by simp [linear_proj_of_is_compl] @[simp] lemma linear_proj_of_is_compl_range (h : is_compl p q) : (linear_proj_of_is_compl p q h).range = ⊤ := range_eq_of_proj (linear_proj_of_is_compl_apply_left h) @[simp] lemma linear_proj_of_is_compl_apply_eq_zero_iff (h : is_compl p q) {x : E} : linear_proj_of_is_compl p q h x = 0 ↔ x ∈ q:= by simp [linear_proj_of_is_compl] lemma linear_proj_of_is_compl_apply_right' (h : is_compl p q) (x : E) (hx : x ∈ q) : linear_proj_of_is_compl p q h x = 0 := (linear_proj_of_is_compl_apply_eq_zero_iff h).2 hx @[simp] lemma linear_proj_of_is_compl_apply_right (h : is_compl p q) (x : q) : linear_proj_of_is_compl p q h x = 0 := linear_proj_of_is_compl_apply_right' h x x.2 @[simp] lemma linear_proj_of_is_compl_ker (h : is_compl p q) : (linear_proj_of_is_compl p q h).ker = q := ext $ λ x, mem_ker.trans (linear_proj_of_is_compl_apply_eq_zero_iff h) lemma linear_proj_of_is_compl_comp_subtype (h : is_compl p q) : (linear_proj_of_is_compl p q h).comp p.subtype = id := linear_map.ext $ linear_proj_of_is_compl_apply_left h lemma linear_proj_of_is_compl_idempotent (h : is_compl p q) (x : E) : linear_proj_of_is_compl p q h (linear_proj_of_is_compl p q h x) = linear_proj_of_is_compl p q h x := linear_proj_of_is_compl_apply_left h _ lemma exists_unique_add_of_is_compl_prod (hc : is_compl p q) (x : E) : ∃! (u : p × q), (u.fst : E) + u.snd = x := (prod_equiv_of_is_compl _ _ hc).to_equiv.bijective.exists_unique _ lemma exists_unique_add_of_is_compl (hc : is_compl p q) (x : E) : ∃ (u : p) (v : q), ((u : E) + v = x ∧ ∀ (r : p) (s : q), (r : E) + s = x → r = u ∧ s = v) := let ⟨u, hu₁, hu₂⟩ := exists_unique_add_of_is_compl_prod hc x in ⟨u.1, u.2, hu₁, λ r s hrs, prod.eq_iff_fst_eq_snd_eq.1 (hu₂ ⟨r, s⟩ hrs)⟩ end submodule namespace linear_map open submodule /-- Given linear maps `φ` and `ψ` from complement submodules, `of_is_compl` is the induced linear map over the entire module. -/ def of_is_compl {p q : submodule R E} (h : is_compl p q) (φ : p →ₗ[R] F) (ψ : q →ₗ[R] F) : E →ₗ[R] F := (linear_map.coprod φ ψ).comp (submodule.prod_equiv_of_is_compl _ _ h).symm variables {p q} @[simp] lemma of_is_compl_left_apply (h : is_compl p q) {φ : p →ₗ[R] F} {ψ : q →ₗ[R] F} (u : p) : of_is_compl h φ ψ (u : E) = φ u := by simp [of_is_compl] @[simp] lemma of_is_compl_right_apply (h : is_compl p q) {φ : p →ₗ[R] F} {ψ : q →ₗ[R] F} (v : q) : of_is_compl h φ ψ (v : E) = ψ v := by simp [of_is_compl] lemma of_is_compl_eq (h : is_compl p q) {φ : p →ₗ[R] F} {ψ : q →ₗ[R] F} {χ : E →ₗ[R] F} (hφ : ∀ u, φ u = χ u) (hψ : ∀ u, ψ u = χ u) : of_is_compl h φ ψ = χ := begin ext x, obtain ⟨_, _, rfl, _⟩ := exists_unique_add_of_is_compl h x, simp [of_is_compl, hφ, hψ] end lemma of_is_compl_eq' (h : is_compl p q) {φ : p →ₗ[R] F} {ψ : q →ₗ[R] F} {χ : E →ₗ[R] F} (hφ : φ = χ.comp p.subtype) (hψ : ψ = χ.comp q.subtype) : of_is_compl h φ ψ = χ := of_is_compl_eq h (λ _, hφ.symm ▸ rfl) (λ _, hψ.symm ▸ rfl) @[simp] lemma of_is_compl_zero (h : is_compl p q) : (of_is_compl h 0 0 : E →ₗ[R] F) = 0 := of_is_compl_eq _ (λ _, rfl) (λ _, rfl) @[simp] lemma of_is_compl_add (h : is_compl p q) {φ₁ φ₂ : p →ₗ[R] F} {ψ₁ ψ₂ : q →ₗ[R] F} : of_is_compl h (φ₁ + φ₂) (ψ₁ + ψ₂) = of_is_compl h φ₁ ψ₁ + of_is_compl h φ₂ ψ₂ := of_is_compl_eq _ (by simp) (by simp) @[simp] lemma of_is_compl_smul {R : Type*} [comm_ring R] {E : Type*} [add_comm_group E] [module R E] {F : Type*} [add_comm_group F] [module R F] {p q : submodule R E} (h : is_compl p q) {φ : p →ₗ[R] F} {ψ : q →ₗ[R] F} (c : R) : of_is_compl h (c • φ) (c • ψ) = c • of_is_compl h φ ψ := of_is_compl_eq _ (by simp) (by simp) section variables {R₁ : Type*} [comm_ring R₁] [module R₁ E] [module R₁ F] /-- The linear map from `(p →ₗ[R₁] F) × (q →ₗ[R₁] F)` to `E →ₗ[R₁] F`. -/ def of_is_compl_prod {p q : submodule R₁ E} (h : is_compl p q) : ((p →ₗ[R₁] F) × (q →ₗ[R₁] F)) →ₗ[R₁] (E →ₗ[R₁] F) := { to_fun := λ φ, of_is_compl h φ.1 φ.2, map_add' := by { intros φ ψ, rw [prod.snd_add, prod.fst_add, of_is_compl_add] }, map_smul' := by { intros c φ, rw [prod.smul_snd, prod.smul_fst, of_is_compl_smul] } } @[simp] lemma of_is_compl_prod_apply {p q : submodule R₁ E} (h : is_compl p q) (φ : (p →ₗ[R₁] F) × (q →ₗ[R₁] F)) : of_is_compl_prod h φ = of_is_compl h φ.1 φ.2 := rfl /-- The natural linear equivalence between `(p →ₗ[R₁] F) × (q →ₗ[R₁] F)` and `E →ₗ[R₁] F`. -/ def of_is_compl_prod_equiv {p q : submodule R₁ E} (h : is_compl p q) : ((p →ₗ[R₁] F) × (q →ₗ[R₁] F)) ≃ₗ[R₁] (E →ₗ[R₁] F) := { inv_fun := λ φ, ⟨φ.dom_restrict p, φ.dom_restrict q⟩, left_inv := begin intros φ, ext, { exact of_is_compl_left_apply h x }, { exact of_is_compl_right_apply h x } end, right_inv := begin intro φ, ext, obtain ⟨a, b, hab, _⟩ := exists_unique_add_of_is_compl h x, rw [← hab], simp, end, .. of_is_compl_prod h } end @[simp] lemma linear_proj_of_is_compl_of_proj (f : E →ₗ[R] p) (hf : ∀ x : p, f x = x) : p.linear_proj_of_is_compl f.ker (is_compl_of_proj hf) = f := begin ext x, have : x ∈ p ⊔ f.ker, { simp only [(is_compl_of_proj hf).sup_eq_top, mem_top] }, rcases mem_sup'.1 this with ⟨x, y, rfl⟩, simp [hf] end /-- If `f : E →ₗ[R] F` and `g : E →ₗ[R] G` are two surjective linear maps and their kernels are complement of each other, then `x ↦ (f x, g x)` defines a linear equivalence `E ≃ₗ[R] F × G`. -/ def equiv_prod_of_surjective_of_is_compl (f : E →ₗ[R] F) (g : E →ₗ[R] G) (hf : f.range = ⊤) (hg : g.range = ⊤) (hfg : is_compl f.ker g.ker) : E ≃ₗ[R] F × G := linear_equiv.of_bijective (f.prod g) (by simp [hfg.inf_eq_bot]) (by simp [range_prod_eq hfg.sup_eq_top, *]) @[simp] lemma coe_equiv_prod_of_surjective_of_is_compl {f : E →ₗ[R] F} {g : E →ₗ[R] G} (hf : f.range = ⊤) (hg : g.range = ⊤) (hfg : is_compl f.ker g.ker) : (equiv_prod_of_surjective_of_is_compl f g hf hg hfg : E →ₗ[R] F × G) = f.prod g := rfl @[simp] lemma equiv_prod_of_surjective_of_is_compl_apply {f : E →ₗ[R] F} {g : E →ₗ[R] G} (hf : f.range = ⊤) (hg : g.range = ⊤) (hfg : is_compl f.ker g.ker) (x : E): equiv_prod_of_surjective_of_is_compl f g hf hg hfg x = (f x, g x) := rfl end linear_map namespace submodule open linear_map /-- Equivalence between submodules `q` such that `is_compl p q` and linear maps `f : E →ₗ[R] p` such that `∀ x : p, f x = x`. -/ def is_compl_equiv_proj : {q // is_compl p q} ≃ {f : E →ₗ[R] p // ∀ x : p, f x = x} := { to_fun := λ q, ⟨linear_proj_of_is_compl p q q.2, linear_proj_of_is_compl_apply_left q.2⟩, inv_fun := λ f, ⟨(f : E →ₗ[R] p).ker, is_compl_of_proj f.2⟩, left_inv := λ ⟨q, hq⟩, by simp only [linear_proj_of_is_compl_ker, subtype.coe_mk], right_inv := λ ⟨f, hf⟩, subtype.eq $ f.linear_proj_of_is_compl_of_proj hf } @[simp] lemma coe_is_compl_equiv_proj_apply (q : {q // is_compl p q}) : (p.is_compl_equiv_proj q : E →ₗ[R] p) = linear_proj_of_is_compl p q q.2 := rfl @[simp] lemma coe_is_compl_equiv_proj_symm_apply (f : {f : E →ₗ[R] p // ∀ x : p, f x = x}) : (p.is_compl_equiv_proj.symm f : submodule R E) = (f : E →ₗ[R] p).ker := rfl end submodule
66684b2ec78b976864fb7e6706a3579bfb5f38b2
ce6917c5bacabee346655160b74a307b4a5ab620
/src/ch6/ex0305.lean
371863fa527455812ca4939077efede0ad1098ac
[]
no_license
Ailrun/Theorem_Proving_in_Lean
ae6a23f3c54d62d401314d6a771e8ff8b4132db2
2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68
refs/heads/master
1,609,838,270,467
1,586,846,743,000
1,586,846,743,000
240,967,761
1
0
null
null
null
null
UTF-8
Lean
false
false
24
lean
open nat (succ add sub)
5d3a38c26a2cea78387f7d5166ad65c1bdf700d1
8cb37a089cdb4af3af9d8bf1002b417e407a8e9e
/library/init/data/set.lean
e0a352b094be6877b477e2ff6851fecc52dd932c
[ "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
2,292
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.interactive import init.control.lawful universes u v def set (α : Type u) := α → Prop def set_of {α : Type u} (p : α → Prop) : set α := p namespace set variables {α : Type u} {β : Type v} protected def mem (a : α) (s : set α) := s a instance : has_mem α (set α) := ⟨set.mem⟩ protected def subset (s₁ s₂ : set α) := ∀ ⦃a⦄, a ∈ s₁ → a ∈ s₂ instance : has_subset (set α) := ⟨set.subset⟩ protected def sep (p : α → Prop) (s : set α) : set α := {a | a ∈ s ∧ p a} instance : has_sep α (set α) := ⟨set.sep⟩ instance : has_emptyc (set α) := ⟨λ a, false⟩ def univ : set α := λ a, true protected def insert (a : α) (s : set α) : set α := {b | b = a ∨ b ∈ s} instance : has_insert α (set α) := ⟨set.insert⟩ instance : has_singleton α (set α) := ⟨λ a, {b | b = a}⟩ instance : is_lawful_singleton α (set α) := ⟨λ a, funext $ λ b, propext $ or_false _⟩ protected def union (s₁ s₂ : set α) : set α := {a | a ∈ s₁ ∨ a ∈ s₂} instance : has_union (set α) := ⟨set.union⟩ protected def inter (s₁ s₂ : set α) : set α := {a | a ∈ s₁ ∧ a ∈ s₂} instance : has_inter (set α) := ⟨set.inter⟩ def compl (s : set α) : set α := {a | a ∉ s} protected def diff (s t : set α) : set α := {a ∈ s | a ∉ t} instance : has_sdiff (set α) := ⟨set.diff⟩ def powerset (s : set α) : set (set α) := {t | t ⊆ s} prefix `𝒫`:100 := powerset @[reducible] def sUnion (s : set (set α)) : set α := {t | ∃ a ∈ s, t ∈ a} prefix `⋃₀`:110 := sUnion def image (f : α → β) (s : set α) : set β := {b | ∃ a, a ∈ s ∧ f a = b} instance : functor set := { map := @set.image } instance : is_lawful_functor set := { id_map := λ _ s, funext $ λ b, propext ⟨λ ⟨_, sb, rfl⟩, sb, λ sb, ⟨_, sb, rfl⟩⟩, comp_map := λ _ _ _ g h s, funext $ λ c, propext ⟨λ ⟨a, ⟨h₁, h₂⟩⟩, ⟨g a, ⟨⟨a, ⟨h₁, rfl⟩⟩, h₂⟩⟩, λ ⟨b, ⟨⟨a, ⟨h₁, h₂⟩⟩, h₃⟩⟩, ⟨a, ⟨h₁, by dsimp; cc⟩⟩⟩ } end set
ea9aa49c6b3b25a6c543ad3630c71b83775b404a
1b8f093752ba748c5ca0083afef2959aaa7dace5
/src/category_theory/graphs/default.lean
6e01b34f6907c52b1461ef2c52c71d1e577665d5
[]
no_license
khoek/lean-category-theory
7ec4cda9cc64a5a4ffeb84712ac7d020dbbba386
63dcb598e9270a3e8b56d1769eb4f825a177cd95
refs/heads/master
1,585,251,725,759
1,539,344,445,000
1,539,344,445,000
145,281,070
0
0
null
1,534,662,376,000
1,534,662,376,000
null
UTF-8
Lean
false
false
2,689
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Stephen Morgan and Scott Morrison import category_theory.tactics.obviously import tidy.auto_cast namespace category_theory.graphs universes u₁ u₂ class graph (vertices : Type u₁) := (edges : vertices → vertices → Type u₁) variable {C : Type u₁} variables {W X Y Z : C} variable [graph C] def edges : C → C → Type u₁ := graph.edges structure graph_homomorphism (G : Type u₁) [graph G] (H : Type u₂) [graph H] := (onVertices : G → H) (onEdges : ∀ {X Y : G}, edges X Y → edges (onVertices X) (onVertices Y)) variable {G : Type u₁} variable [graph G] variable {H : Type u₂} variable [graph H] @[extensionality] lemma graph_homomorphisms_pointwise_equal {p q : graph_homomorphism G H} (vertexWitness : ∀ X : G, p.onVertices X = q.onVertices X) (edgeWitness : ∀ X Y : G, ∀ f : edges X Y, ⟬ p.onEdges f ⟭ = q.onEdges f) : p = q := begin induction p with p_onVertices p_onEdges, induction q with q_onVertices q_onEdges, have h_vertices : p_onVertices = q_onVertices, exact funext vertexWitness, subst h_vertices, have h_edges : @p_onEdges = @q_onEdges, apply funext, intro X, apply funext, intro Y, apply funext, intro f, exact edgeWitness X Y f, subst h_edges end inductive path : G → G → Type u₁ | nil : Π (h : G), path h h | cons : Π {h s t : G} (e : edges h s) (l : path s t), path h t notation a :: b := path.cons a b notation `p[` l:(foldr `, ` (h t, path.cons h t) path.nil _ `]`) := l inductive path_of_paths : G → G → Type (u₁+1) | nil : Π (h : G), path_of_paths h h | cons : Π {h s t : G} (e : path h s) (l : path_of_paths s t), path_of_paths h t notation a :: b := path_of_paths.cons a b notation `pp[` l:(foldr `, ` (h t, path_of_paths.cons h t) path_of_paths.nil _ `]`) := l -- The pattern matching trick used here was explained by Jeremy Avigad at https://groups.google.com/d/msg/lean-user/JqaI12tdk3g/F9MZDxkFDAAJ def concatenate_paths : Π {x y z : G}, path x y → path y z → path x z | ._ ._ _ (path.nil _) q := q | ._ ._ _ (@path.cons ._ _ _ _ _ e p') q := path.cons e (concatenate_paths p' q) @[simp] lemma concatenate_paths' {x' x y z : G} (e : edges x' x) (p : path x y) (q : path y z) : concatenate_paths (e :: p) q = e :: (concatenate_paths p q) := rfl def concatenate_path_of_paths : Π {x y : G}, path_of_paths x y → path x y | ._ ._ (path_of_paths.nil X) := path.nil X | ._ ._ (@path_of_paths.cons ._ _ _ _ _ e p') := concatenate_paths e (concatenate_path_of_paths p') end category_theory.graphs
a686d8eb5b9553779fa6fefdbd1d3538b4d3fa4c
26ac254ecb57ffcb886ff709cf018390161a9225
/src/category_theory/natural_isomorphism.lean
210660ea453dcbeb8608f7b74d87eef80d896178
[ "Apache-2.0" ]
permissive
eric-wieser/mathlib
42842584f584359bbe1fc8b88b3ff937c8acd72d
d0df6b81cd0920ad569158c06a3fd5abb9e63301
refs/heads/master
1,669,546,404,255
1,595,254,668,000
1,595,254,668,000
281,173,504
0
0
Apache-2.0
1,595,263,582,000
1,595,263,581,000
null
UTF-8
Lean
false
false
4,661
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Tim Baumann, Stephen Morgan, Scott Morrison, Floris van Doorn -/ import category_theory.functor_category import category_theory.isomorphism open category_theory -- declare the `v`'s first; see `category_theory.category` for an explanation universes v₁ v₂ v₃ v₄ u₁ u₂ u₃ u₄ namespace category_theory open nat_trans variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D] {E : Type u₃} [category.{v₃} E] namespace iso /-- The application of a natural isomorphism to an object. We put this definition in a different namespace, so that we can use `α.app` -/ @[simp, reducible] def app {F G : C ⥤ D} (α : F ≅ G) (X : C) : F.obj X ≅ G.obj X := { hom := α.hom.app X, inv := α.inv.app X, hom_inv_id' := begin rw [← comp_app, iso.hom_inv_id], refl end, inv_hom_id' := begin rw [← comp_app, iso.inv_hom_id], refl end } @[simp, reassoc] lemma hom_inv_id_app {F G : C ⥤ D} (α : F ≅ G) (X : C) : α.hom.app X ≫ α.inv.app X = 𝟙 (F.obj X) := congr_fun (congr_arg nat_trans.app α.hom_inv_id) X @[simp, reassoc] lemma inv_hom_id_app {F G : C ⥤ D} (α : F ≅ G) (X : C) : α.inv.app X ≫ α.hom.app X = 𝟙 (G.obj X) := congr_fun (congr_arg nat_trans.app α.inv_hom_id) X end iso namespace nat_iso open category_theory.category category_theory.functor @[simp] lemma trans_app {F G H : C ⥤ D} (α : F ≅ G) (β : G ≅ H) (X : C) : (α ≪≫ β).app X = α.app X ≪≫ β.app X := rfl lemma app_hom {F G : C ⥤ D} (α : F ≅ G) (X : C) : (α.app X).hom = α.hom.app X := rfl lemma app_inv {F G : C ⥤ D} (α : F ≅ G) (X : C) : (α.app X).inv = α.inv.app X := rfl variables {F G : C ⥤ D} instance hom_app_is_iso (α : F ≅ G) (X : C) : is_iso (α.hom.app X) := { inv := α.inv.app X, hom_inv_id' := begin rw [←comp_app, iso.hom_inv_id, ←id_app] end, inv_hom_id' := begin rw [←comp_app, iso.inv_hom_id, ←id_app] end } instance inv_app_is_iso (α : F ≅ G) (X : C) : is_iso (α.inv.app X) := { inv := α.hom.app X, hom_inv_id' := begin rw [←comp_app, iso.inv_hom_id, ←id_app] end, inv_hom_id' := begin rw [←comp_app, iso.hom_inv_id, ←id_app] end } variables {X Y : C} lemma naturality_1 (α : F ≅ G) (f : X ⟶ Y) : (α.inv.app X) ≫ (F.map f) ≫ (α.hom.app Y) = G.map f := begin erw [naturality, ←category.assoc, is_iso.hom_inv_id, category.id_comp] end lemma naturality_2 (α : F ≅ G) (f : X ⟶ Y) : (α.hom.app X) ≫ (G.map f) ≫ (α.inv.app Y) = F.map f := begin erw [naturality, ←category.assoc, is_iso.hom_inv_id, category.id_comp] end def is_iso_of_is_iso_app (α : F ⟶ G) [∀ X : C, is_iso (α.app X)] : is_iso α := { inv := { app := λ X, inv (α.app X), naturality' := λ X Y f, begin have h := congr_arg (λ f, inv (α.app X) ≫ (f ≫ inv (α.app Y))) (α.naturality f).symm, simp only [is_iso.inv_hom_id_assoc, is_iso.hom_inv_id, assoc, comp_id, cancel_mono] at h, exact h end } } instance is_iso_of_is_iso_app' (α : F ⟶ G) [H : ∀ X : C, is_iso (nat_trans.app α X)] : is_iso α := @nat_iso.is_iso_of_is_iso_app _ _ _ _ _ _ α H -- TODO can we make this an instance? def is_iso_app_of_is_iso (α : F ⟶ G) [is_iso α] (X) : is_iso (α.app X) := { inv := (inv α).app X, hom_inv_id' := congr_fun (congr_arg nat_trans.app (is_iso.hom_inv_id α)) X, inv_hom_id' := congr_fun (congr_arg nat_trans.app (is_iso.inv_hom_id α)) X } def of_components (app : ∀ X : C, F.obj X ≅ G.obj X) (naturality : ∀ {X Y : C} (f : X ⟶ Y), F.map f ≫ (app Y).hom = (app X).hom ≫ G.map f) : F ≅ G := as_iso { app := λ X, (app X).hom } @[simp] lemma of_components.app (app' : ∀ X : C, F.obj X ≅ G.obj X) (naturality) (X) : (of_components app' naturality).app X = app' X := by tidy @[simp] lemma of_components.hom_app (app : ∀ X : C, F.obj X ≅ G.obj X) (naturality) (X) : (of_components app naturality).hom.app X = (app X).hom := rfl @[simp] lemma of_components.inv_app (app : ∀ X : C, F.obj X ≅ G.obj X) (naturality) (X) : (of_components app naturality).inv.app X = (app X).inv := rfl def hcomp {F G : C ⥤ D} {H I : D ⥤ E} (α : F ≅ G) (β : H ≅ I) : F ⋙ H ≅ G ⋙ I := begin refine ⟨α.hom ◫ β.hom, α.inv ◫ β.inv, _, _⟩, { ext, rw [←nat_trans.exchange], simp, refl }, ext, rw [←nat_trans.exchange], simp, refl end -- declare local notation for nat_iso.hcomp localized "infix ` ■ `:80 := category_theory.nat_iso.hcomp" in category end nat_iso end category_theory
4ae83269e61bbb991899a3d6d6f2c3840bc0f60b
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/tactic/localized.lean
c753b7210a5fc2753c3904d5e56a35597905f963
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,550
lean
/- Copyright (c) 2019 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import tactic.core /-! # Localized notation This consists of two user-commands which allow you to declare notation and commands localized to a locale. See the tactic doc entry below for more information. The code is inspired by code from Gabriel Ebner from the [hott3 repository](https://github.com/gebner/hott3). -/ open lean lean.parser interactive tactic native @[user_attribute] meta def localized_attr : user_attribute (rb_lmap name string) unit := { name := "_localized", descr := "(interal) attribute that flags localized commands", parser := failed, cache_cfg := ⟨λ ns, (do dcls ← ns.mmap (λ n, mk_const n >>= eval_expr (name × string)), return $ rb_lmap.of_list dcls), []⟩ } /-- Get all commands in the given locale and return them as a list of strings -/ meta def get_localized (ns : list name) : tactic (list string) := do m ← localized_attr.get_cache, ns.mfoldl (λ l nm, match m.find nm with | [] := fail format!"locale {nm} does not exist" | new_l := return $ l.append new_l end) [] /-- Execute all commands in the given locale -/ @[user_command] meta def open_locale_cmd (_ : parse $ tk "open_locale") : parser unit := do ns ← many ident, cmds ← get_localized ns, cmds.mmap' emit_code_here /-- Add a new command to a locale and execute it right now. The new command is added as a declaration to the environment with name `_localized_decl.<number>`. This declaration has attribute `_localized` and as value a name-string pair. -/ @[user_command] meta def localized_cmd (_ : parse $ tk "localized") : parser unit := do cmd ← parser.pexpr, cmd ← i_to_expr cmd, cmd ← eval_expr string cmd, let cmd := "local " ++ cmd, emit_code_here cmd, tk "in", nm ← ident, env ← get_env, let dummy_decl_name := mk_num_name `_localized_decl ((string.hash (cmd ++ nm.to_string) + env.fingerprint) % unsigned_sz), add_decl (declaration.defn dummy_decl_name [] `(name × string) (reflect (⟨nm, cmd⟩ : name × string)) (reducibility_hints.regular 1 tt) ff), localized_attr.set dummy_decl_name unit.star tt /-- This consists of two user-commands which allow you to declare notation and commands localized to a locale. * Declare notation which is localized to a locale using: ```lean localized "infix ` ⊹ `:60 := my_add" in my.add ``` * After this command it will be available in the same section/namespace/file, just as if you wrote `local infix ` ⊹ `:60 := my_add` * You can open it in other places. The following command will declare the notation again as local notation in that section/namespace/file: ```lean open_locale my.add ``` * More generally, the following will declare all localized notation in the specified locales. ```lean open_locale locale1 locale2 ... ``` * You can also declare other localized commands, like local attributes ```lean localized "attribute [simp] le_refl" in le ``` * To see all localized commands in a given locale, run: ```lean run_cmd print_localized_commands [`my.add]. ``` * To see a list of all locales with localized commands, run: ```lean run_cmd do m ← localized_attr.get_cache, tactic.trace m.keys -- change to `tactic.trace m.to_list` to list all the commands in each locale ``` * Warning: You have to give full names of all declarations used in localized notation, so that the localized notation also works when the appropriate namespaces are not opened. -/ add_tactic_doc { name := "localized notation", category := doc_category.cmd, decl_names := [`localized_cmd, `open_locale_cmd], tags := ["notation", "type classes"] } /-- Print all commands in a given locale -/ meta def print_localized_commands (ns : list name) : tactic unit := do cmds ← get_localized ns, cmds.mmap' trace -- you can run `open_locale classical` to get the decidability of all propositions, and downgrade -- the priority of decidability instances that make Lean run through all the algebraic hierarchy -- whenever it wants to solve a decidability question localized "attribute [instance, priority 9] classical.prop_decidable" in classical localized "attribute [instance, priority 8] eq.decidable decidable_eq_of_decidable_le" in classical localized "postfix `?`:9001 := optional" in parser localized "postfix *:9001 := lean.parser.many" in parser
a3403772c27a91cbaa562e89c98be6f1bba34ba8
737dc4b96c97368cb66b925eeea3ab633ec3d702
/tests/leanpkg/user_ext/UserExt/Tst1.lean
df3f4ff5ce58d8adccef994df576a1cc6b56a233
[ "Apache-2.0" ]
permissive
Bioye97/lean4
1ace34638efd9913dc5991443777b01a08983289
bc3900cbb9adda83eed7e6affeaade7cfd07716d
refs/heads/master
1,690,589,820,211
1,631,051,000,000
1,631,067,598,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
121
lean
import UserExt.FooExt import UserExt.BlaExt insert_foo hello insert_foo world show_foo_set insert_bla abc show_bla_set
3a8cca2442d951e9c18bf361896c82e6ab93b2f1
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/data/fintype/card.lean
3f493c3334d52f00820f6fffe60da48d4d5a421d
[ "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
6,536
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro -/ import data.fintype.basic algebra.big_operators data.nat.choose tactic.ring /-! Results about "big operations" over a `fintype`, and consequent results about cardinalities of certain types. ## Implementation note This content had previously been in `data.fintype`, but was moved here to avoid requiring `algebra.big_operators` (and hence many other imports) as a dependency of `fintype`. -/ universes u v variables {α : Type*} {β : Type*} {γ : Type*} namespace fintype lemma card_eq_sum_ones {α} [fintype α] : fintype.card α = (finset.univ : finset α).sum (λ _, 1) := finset.card_eq_sum_ones _ end fintype open finset theorem fin.prod_univ_succ [comm_monoid β] {n:ℕ} (f : fin n.succ → β) : univ.prod f = f 0 * univ.prod (λ i:fin n, f i.succ) := begin rw [fin.univ_succ, prod_insert, prod_image], { intros x _ y _ hxy, exact fin.succ.inj hxy }, { simpa using fin.succ_ne_zero } end @[simp, to_additive] theorem fin.prod_univ_zero [comm_monoid β] (f : fin 0 → β) : univ.prod f = 1 := rfl theorem fin.sum_univ_succ [add_comm_monoid β] {n:ℕ} (f : fin n.succ → β) : univ.sum f = f 0 + univ.sum (λ i:fin n, f i.succ) := by apply @fin.prod_univ_succ (multiplicative β) attribute [to_additive] fin.prod_univ_succ theorem fin.prod_univ_cast_succ [comm_monoid β] {n:ℕ} (f : fin n.succ → β) : univ.prod f = univ.prod (λ i:fin n, f i.cast_succ) * f (fin.last n) := begin rw [fin.univ_cast_succ, prod_insert, prod_image, mul_comm], { intros x _ y _ hxy, exact fin.cast_succ_inj.mp hxy }, { simpa using fin.cast_succ_ne_last } end theorem fin.sum_univ_cast_succ [add_comm_monoid β] {n:ℕ} (f : fin n.succ → β) : univ.sum f = univ.sum (λ i:fin n, f i.cast_succ) + f (fin.last n) := by apply @fin.prod_univ_cast_succ (multiplicative β) attribute [to_additive] fin.prod_univ_cast_succ @[simp] theorem fintype.card_sigma {α : Type*} (β : α → Type*) [fintype α] [∀ a, fintype (β a)] : fintype.card (sigma β) = univ.sum (λ a, fintype.card (β a)) := card_sigma _ _ -- FIXME ouch, this should be in the main file. @[simp] theorem fintype.card_sum (α β : Type*) [fintype α] [fintype β] : fintype.card (α ⊕ β) = fintype.card α + fintype.card β := by rw [sum.fintype, fintype.of_equiv_card]; simp @[simp] lemma fintype.card_pi_finset [decidable_eq α] [fintype α] {δ : α → Type*} (t : Π a, finset (δ a)) : (fintype.pi_finset t).card = finset.univ.prod (λ a, card (t a)) := by simp [fintype.pi_finset, card_map] @[simp] lemma fintype.card_pi {β : α → Type*} [fintype α] [decidable_eq α] [f : Π a, fintype (β a)] : fintype.card (Π a, β a) = univ.prod (λ a, fintype.card (β a)) := fintype.card_pi_finset _ -- FIXME ouch, this should be in the main file. @[simp] lemma fintype.card_fun [fintype α] [decidable_eq α] [fintype β] : fintype.card (α → β) = fintype.card β ^ fintype.card α := by rw [fintype.card_pi, finset.prod_const, nat.pow_eq_pow]; refl @[simp] lemma card_vector [fintype α] (n : ℕ) : fintype.card (vector α n) = fintype.card α ^ n := by rw fintype.of_equiv_card; simp @[simp, to_additive] lemma finset.prod_attach_univ [fintype α] [comm_monoid β] (f : {a : α // a ∈ @univ α _} → β) : univ.attach.prod (λ x, f x) = univ.prod (λ x, f ⟨x, (mem_univ _)⟩) := prod_bij (λ x _, x.1) (λ _ _, mem_univ _) (λ _ _ , by simp) (by simp) (λ b _, ⟨⟨b, mem_univ _⟩, by simp⟩) @[to_additive] lemma finset.range_prod_eq_univ_prod [comm_monoid β] (n : ℕ) (f : ℕ → β) : (range n).prod f = univ.prod (λ (k : fin n), f k) := begin symmetry, refine prod_bij (λ k hk, k) _ _ _ _, { rintro ⟨k, hk⟩ _, simp * }, { rintro ⟨k, hk⟩ _, simp * }, { intros, rwa fin.eq_iff_veq }, { intros k hk, rw mem_range at hk, exact ⟨⟨k, hk⟩, mem_univ _, rfl⟩ } end /-- Taking a product over `univ.pi t` is the same as taking the product over `fintype.pi_finset t`. `univ.pi t` and `fintype.pi_finset t` are essentially the same `finset`, but differ in the type of their element, `univ.pi t` is a `finset (Π a ∈ univ, t a)` and `fintype.pi_finset t` is a `finset (Π a, t a)`. -/ @[to_additive "Taking a sum over `univ.pi t` is the same as taking the sum over `fintype.pi_finset t`. `univ.pi t` and `fintype.pi_finset t` are essentially the same `finset`, but differ in the type of their element, `univ.pi t` is a `finset (Π a ∈ univ, t a)` and `fintype.pi_finset t` is a `finset (Π a, t a)`."] lemma finset.prod_univ_pi [decidable_eq α] [fintype α] [comm_monoid β] {δ : α → Type*} {t : Π (a : α), finset (δ a)} (f : (Π (a : α), a ∈ (univ : finset α) → δ a) → β) : (univ.pi t).prod f = (fintype.pi_finset t).prod (λ x, f (λ a _, x a)) := prod_bij (λ x _ a, x a (mem_univ _)) (by simp) (by simp) (by simp [function.funext_iff] {contextual := tt}) (λ x hx, ⟨λ a _, x a, by simp * at *⟩) /-- The product over `univ` of a sum can be written as a sum over the product of sets, `fintype.pi_finset`. `finset.prod_sum` is an alternative statement when the product is not over `univ` -/ lemma finset.prod_univ_sum [decidable_eq α] [fintype α] [comm_semiring β] {δ : α → Type u_1} [Π (a : α), decidable_eq (δ a)] {t : Π (a : α), finset (δ a)} {f : Π (a : α), δ a → β} : univ.prod (λ a, (t a).sum (λ b, f a b)) = (fintype.pi_finset t).sum (λ p, univ.prod (λ x, f x (p x))) := by simp only [finset.prod_attach_univ, prod_sum, finset.sum_univ_pi] /-- Summing `a^s.card * b^(n-s.card)` over all finite subsets `s` of a fintype of cardinality `n` gives `(a + b)^n`. The "good" proof involves expanding along all coordinates using the fact that `x^n` is multilinear, but multilinear maps are only available now over rings, so we give instead a proof reducing to the usual binomial theorem to have a result over semirings. -/ lemma fintype.sum_pow_mul_eq_add_pow (α : Type*) [fintype α] {R : Type*} [comm_semiring R] (a b : R) : finset.univ.sum (λ (s : finset α), a ^ s.card * b ^ (fintype.card α - s.card)) = (a + b) ^ (fintype.card α) := finset.sum_pow_mul_eq_add_pow _ _ _ lemma fin.sum_pow_mul_eq_add_pow {n : ℕ} {R : Type*} [comm_semiring R] (a b : R) : finset.univ.sum (λ (s : finset (fin n)), a ^ s.card * b ^ (n - s.card)) = (a + b) ^ n := by simpa using fintype.sum_pow_mul_eq_add_pow (fin n) a b
9e8bfc41a4f5f3ae644f196e8f7530bbd035af7c
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/combinatorics/composition.lean
f3935977c485306223cbba6431ac484c58f96203
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
28,263
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 Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.fintype.card import Mathlib.data.finset.sort import Mathlib.tactic.omega.default import Mathlib.algebra.big_operators.order import Mathlib.PostPort universes l u_1 namespace Mathlib /-! # Compositions A composition of a natural number `n` is a decomposition `n = i₀ + ... + i_{k-1}` of `n` into a sum of positive integers. Combinatorially, it corresponds to a decomposition of `{0, ..., n-1}` into non-empty blocks of consecutive integers, where the `iⱼ` are the lengths of the blocks. This notion is closely related to that of a partition of `n`, but in a composition of `n` the order of the `iⱼ`s matters. We implement two different structures covering these two viewpoints on compositions. The first one, made of a list of positive integers summing to `n`, is the main one and is called `composition n`. The second one is useful for combinatorial arguments (for instance to show that the number of compositions of `n` is `2^(n-1)`). It is given by a subset of `{0, ..., n}` containing `0` and `n`, where the elements of the subset (other than `n`) correspond to the leftmost points of each block. The main API is built on `composition n`, and we provide an equivalence between the two types. ## Main functions * `c : composition n` is a structure, made of a list of integers which are all positive and add up to `n`. * `composition_card` states that the cardinality of `composition n` is exactly `2^(n-1)`, which is proved by constructing an equiv with `composition_as_set n` (see below), which is itself in bijection with the subsets of `fin (n-1)` (this holds even for `n = 0`, where `-` is nat subtraction). Let `c : composition n` be a composition of `n`. Then * `c.blocks` is the list of blocks in `c`. * `c.length` is the number of blocks in the composition. * `c.blocks_fun : fin c.length → ℕ` is the realization of `c.blocks` as a function on `fin c.length`. This is the main object when using compositions to understand the composition of analytic functions. * `c.size_up_to : ℕ → ℕ` is the sum of the size of the blocks up to `i`.; * `c.embedding i : fin (c.blocks_fun i) → fin n` is the increasing embedding of the `i`-th block in `fin n`; * `c.index j`, for `j : fin n`, is the index of the block containing `j`. * `composition.ones n` is the composition of `n` made of ones, i.e., `[1, ..., 1]`. * `composition.single n (hn : 0 < n)` is the composition of `n` made of a single block of size `n`. Compositions can also be used to split lists. Let `l` be a list of length `n` and `c` a composition of `n`. * `l.split_wrt_composition c` is a list of lists, made of the slices of `l` corresponding to the blocks of `c`. * `join_split_wrt_composition` states that splitting a list and then joining it gives back the original list. * `split_wrt_composition_join` states that joining a list of lists, and then splitting it back according to the right composition, gives back the original list of lists. We turn to the second viewpoint on compositions, that we realize as a finset of `fin (n+1)`. `c : composition_as_set n` is a structure made of a finset of `fin (n+1)` called `c.boundaries` and proofs that it contains `0` and `n`. (Taking a finset of `fin n` containing `0` would not make sense in the edge case `n = 0`, while the previous description works in all cases). The elements of this set (other than `n`) correspond to leftmost points of blocks. Thus, there is an equiv between `composition n` and `composition_as_set n`. We only construct basic API on `composition_as_set` (notably `c.length` and `c.blocks`) to be able to construct this equiv, called `composition_equiv n`. Since there is a straightforward equiv between `composition_as_set n` and finsets of `{1, ..., n-1}` (obtained by removing `0` and `n` from a `composition_as_set` and called `composition_as_set_equiv n`), we deduce that `composition_as_set n` and `composition n` are both fintypes of cardinality `2^(n - 1)` (see `composition_as_set_card` and `composition_card`). ## Implementation details The main motivation for this structure and its API is in the construction of the composition of formal multilinear series, and the proof that the composition of analytic functions is analytic. The representation of a composition as a list is very handy as lists are very flexible and already have a well-developed API. ## Tags Composition, partition ## References <https://en.wikipedia.org/wiki/Composition_(combinatorics)> -/ /-- A composition of `n` is a list of positive integers summing to `n`. -/ structure composition (n : ℕ) where blocks : List ℕ blocks_pos : ∀ {i : ℕ}, i ∈ blocks → 0 < i blocks_sum : list.sum blocks = n /-- Combinatorial viewpoint on a composition of `n`, by seeing it as non-empty blocks of consecutive integers in `{0, ..., n-1}`. We register every block by its left end-point, yielding a finset containing `0`. As this does not make sense for `n = 0`, we add `n` to this finset, and get a finset of `{0, ..., n}` containing `0` and `n`. This is the data in the structure `composition_as_set n`. -/ structure composition_as_set (n : ℕ) where boundaries : finset (fin (Nat.succ n)) zero_mem : 0 ∈ boundaries last_mem : fin.last n ∈ boundaries protected instance composition_as_set.inhabited {n : ℕ} : Inhabited (composition_as_set n) := { default := composition_as_set.mk finset.univ sorry sorry } /-! ### Compositions A composition of an integer `n` is a decomposition `n = i₀ + ... + i_{k-1}` of `n` into a sum of positive integers. -/ namespace composition protected instance has_to_string (n : ℕ) : has_to_string (composition n) := has_to_string.mk fun (c : composition n) => to_string (blocks c) /-- The length of a composition, i.e., the number of blocks in the composition. -/ def length {n : ℕ} (c : composition n) : ℕ := list.length (blocks c) theorem blocks_length {n : ℕ} (c : composition n) : list.length (blocks c) = length c := rfl /-- The blocks of a composition, seen as a function on `fin c.length`. When composing analytic functions using compositions, this is the main player. -/ def blocks_fun {n : ℕ} (c : composition n) : fin (length c) → ℕ := fun (i : fin (length c)) => list.nth_le (blocks c) ↑i sorry theorem of_fn_blocks_fun {n : ℕ} (c : composition n) : list.of_fn (blocks_fun c) = blocks c := list.of_fn_nth_le (blocks c) theorem sum_blocks_fun {n : ℕ} (c : composition n) : (finset.sum finset.univ fun (i : fin (length c)) => blocks_fun c i) = n := sorry theorem blocks_fun_mem_blocks {n : ℕ} (c : composition n) (i : fin (length c)) : blocks_fun c i ∈ blocks c := list.nth_le_mem (blocks c) (↑i) (blocks_fun._proof_1 c i) @[simp] theorem one_le_blocks {n : ℕ} (c : composition n) {i : ℕ} (h : i ∈ blocks c) : 1 ≤ i := blocks_pos c h @[simp] theorem one_le_blocks' {n : ℕ} (c : composition n) {i : ℕ} (h : i < length c) : 1 ≤ list.nth_le (blocks c) i h := one_le_blocks c (list.nth_le_mem (blocks c) i h) @[simp] theorem blocks_pos' {n : ℕ} (c : composition n) (i : ℕ) (h : i < length c) : 0 < list.nth_le (blocks c) i h := one_le_blocks' c h theorem one_le_blocks_fun {n : ℕ} (c : composition n) (i : fin (length c)) : 1 ≤ blocks_fun c i := one_le_blocks c (blocks_fun_mem_blocks c i) theorem length_le {n : ℕ} (c : composition n) : length c ≤ n := sorry theorem length_pos_of_pos {n : ℕ} (c : composition n) (h : 0 < n) : 0 < length c := sorry /-- The sum of the sizes of the blocks in a composition up to `i`. -/ def size_up_to {n : ℕ} (c : composition n) (i : ℕ) : ℕ := list.sum (list.take i (blocks c)) @[simp] theorem size_up_to_zero {n : ℕ} (c : composition n) : size_up_to c 0 = 0 := sorry theorem size_up_to_of_length_le {n : ℕ} (c : composition n) (i : ℕ) (h : length c ≤ i) : size_up_to c i = n := sorry @[simp] theorem size_up_to_length {n : ℕ} (c : composition n) : size_up_to c (length c) = n := size_up_to_of_length_le c (length c) (le_refl (length c)) theorem size_up_to_le {n : ℕ} (c : composition n) (i : ℕ) : size_up_to c i ≤ n := sorry theorem size_up_to_succ {n : ℕ} (c : composition n) {i : ℕ} (h : i < length c) : size_up_to c (i + 1) = size_up_to c i + list.nth_le (blocks c) i h := sorry theorem size_up_to_succ' {n : ℕ} (c : composition n) (i : fin (length c)) : size_up_to c (↑i + 1) = size_up_to c ↑i + blocks_fun c i := size_up_to_succ c (subtype.property i) theorem size_up_to_strict_mono {n : ℕ} (c : composition n) {i : ℕ} (h : i < length c) : size_up_to c i < size_up_to c (i + 1) := sorry theorem monotone_size_up_to {n : ℕ} (c : composition n) : monotone (size_up_to c) := list.monotone_sum_take (blocks c) /-- The `i`-th boundary of a composition, i.e., the leftmost point of the `i`-th block. We include a virtual point at the right of the last block, to make for a nice equiv with `composition_as_set n`. -/ def boundary {n : ℕ} (c : composition n) : fin (length c + 1) ↪o fin (n + 1) := order_embedding.of_strict_mono (fun (i : fin (length c + 1)) => { val := size_up_to c ↑i, property := sorry }) sorry @[simp] theorem boundary_zero {n : ℕ} (c : composition n) : coe_fn (boundary c) 0 = 0 := sorry @[simp] theorem boundary_last {n : ℕ} (c : composition n) : coe_fn (boundary c) (fin.last (length c)) = fin.last n := sorry /-- The boundaries of a composition, i.e., the leftmost point of all the blocks. We include a virtual point at the right of the last block, to make for a nice equiv with `composition_as_set n`. -/ def boundaries {n : ℕ} (c : composition n) : finset (fin (n + 1)) := finset.map (rel_embedding.to_embedding (boundary c)) finset.univ theorem card_boundaries_eq_succ_length {n : ℕ} (c : composition n) : finset.card (boundaries c) = length c + 1 := sorry /-- To `c : composition n`, one can associate a `composition_as_set n` by registering the leftmost point of each block, and adding a virtual point at the right of the last block. -/ def to_composition_as_set {n : ℕ} (c : composition n) : composition_as_set n := composition_as_set.mk (boundaries c) sorry sorry /-- The canonical increasing bijection between `fin (c.length + 1)` and `c.boundaries` is exactly `c.boundary`. -/ theorem order_emb_of_fin_boundaries {n : ℕ} (c : composition n) : finset.order_emb_of_fin (boundaries c) (card_boundaries_eq_succ_length c) = boundary c := sorry /-- Embedding the `i`-th block of a composition (identified with `fin (c.blocks_fun i)`) into `fin n` at the relevant position. -/ def embedding {n : ℕ} (c : composition n) (i : fin (length c)) : fin (blocks_fun c i) ↪o fin n := rel_embedding.trans (fin.nat_add (size_up_to c ↑i)) (fin.cast_le sorry) @[simp] theorem coe_embedding {n : ℕ} (c : composition n) (i : fin (length c)) (j : fin (blocks_fun c i)) : ↑(coe_fn (embedding c i) j) = size_up_to c ↑i + ↑j := rfl /-- `index_exists` asserts there is some `i` with `j < c.size_up_to (i+1)`. In the next definition `index` we use `nat.find` to produce the minimal such index. -/ theorem index_exists {n : ℕ} (c : composition n) {j : ℕ} (h : j < n) : ∃ (i : ℕ), j < size_up_to c (Nat.succ i) ∧ i < length c := sorry /-- `c.index j` is the index of the block in the composition `c` containing `j`. -/ def index {n : ℕ} (c : composition n) (j : fin n) : fin (length c) := { val := nat.find sorry, property := sorry } theorem lt_size_up_to_index_succ {n : ℕ} (c : composition n) (j : fin n) : ↑j < size_up_to c ↑(fin.succ (index c j)) := and.left (nat.find_spec (index_exists c (subtype.property j))) theorem size_up_to_index_le {n : ℕ} (c : composition n) (j : fin n) : size_up_to c ↑(index c j) ≤ ↑j := sorry /-- Mapping an element `j` of `fin n` to the element in the block containing it, identified with `fin (c.blocks_fun (c.index j))` through the canonical increasing bijection. -/ def inv_embedding {n : ℕ} (c : composition n) (j : fin n) : fin (blocks_fun c (index c j)) := { val := ↑j - size_up_to c ↑(index c j), property := sorry } @[simp] theorem coe_inv_embedding {n : ℕ} (c : composition n) (j : fin n) : ↑(inv_embedding c j) = ↑j - size_up_to c ↑(index c j) := rfl theorem embedding_comp_inv {n : ℕ} (c : composition n) (j : fin n) : coe_fn (embedding c (index c j)) (inv_embedding c j) = j := sorry theorem mem_range_embedding_iff {n : ℕ} (c : composition n) {j : fin n} {i : fin (length c)} : j ∈ set.range ⇑(embedding c i) ↔ size_up_to c ↑i ≤ ↑j ∧ ↑j < size_up_to c (Nat.succ ↑i) := sorry /-- The embeddings of different blocks of a composition are disjoint. -/ theorem disjoint_range {n : ℕ} (c : composition n) {i₁ : fin (length c)} {i₂ : fin (length c)} (h : i₁ ≠ i₂) : disjoint (set.range ⇑(embedding c i₁)) (set.range ⇑(embedding c i₂)) := sorry theorem mem_range_embedding {n : ℕ} (c : composition n) (j : fin n) : j ∈ set.range ⇑(embedding c (index c j)) := sorry theorem mem_range_embedding_iff' {n : ℕ} (c : composition n) {j : fin n} {i : fin (length c)} : j ∈ set.range ⇑(embedding c i) ↔ i = index c j := sorry theorem index_embedding {n : ℕ} (c : composition n) (i : fin (length c)) (j : fin (blocks_fun c i)) : index c (coe_fn (embedding c i) j) = i := sorry theorem inv_embedding_comp {n : ℕ} (c : composition n) (i : fin (length c)) (j : fin (blocks_fun c i)) : ↑(inv_embedding c (coe_fn (embedding c i) j)) = ↑j := sorry /-- Equivalence between the disjoint union of the blocks (each of them seen as `fin (c.blocks_fun i)`) with `fin n`. -/ def blocks_fin_equiv {n : ℕ} (c : composition n) : (sigma fun (i : fin (length c)) => fin (blocks_fun c i)) ≃ fin n := equiv.mk (fun (x : sigma fun (i : fin (length c)) => fin (blocks_fun c i)) => coe_fn (embedding c (sigma.fst x)) (sigma.snd x)) (fun (j : fin n) => sigma.mk (index c j) (inv_embedding c j)) sorry sorry theorem blocks_fun_congr {n₁ : ℕ} {n₂ : ℕ} (c₁ : composition n₁) (c₂ : composition n₂) (i₁ : fin (length c₁)) (i₂ : fin (length c₂)) (hn : n₁ = n₂) (hc : blocks c₁ = blocks c₂) (hi : ↑i₁ = ↑i₂) : blocks_fun c₁ i₁ = blocks_fun c₂ i₂ := sorry /-- Two compositions (possibly of different integers) coincide if and only if they have the same sequence of blocks. -/ theorem sigma_eq_iff_blocks_eq {c : sigma fun (n : ℕ) => composition n} {c' : sigma fun (n : ℕ) => composition n} : c = c' ↔ blocks (sigma.snd c) = blocks (sigma.snd c') := sorry /-! ### The composition `composition.ones` -/ /-- The composition made of blocks all of size `1`. -/ def ones (n : ℕ) : composition n := mk (list.repeat 1 n) sorry sorry protected instance inhabited {n : ℕ} : Inhabited (composition n) := { default := ones n } @[simp] theorem ones_length (n : ℕ) : length (ones n) = n := list.length_repeat 1 n @[simp] theorem ones_blocks (n : ℕ) : blocks (ones n) = list.repeat 1 n := rfl @[simp] theorem ones_blocks_fun (n : ℕ) (i : fin (length (ones n))) : blocks_fun (ones n) i = 1 := sorry @[simp] theorem ones_size_up_to (n : ℕ) (i : ℕ) : size_up_to (ones n) i = min i n := sorry @[simp] theorem ones_embedding {n : ℕ} (i : fin (length (ones n))) (h : 0 < blocks_fun (ones n) i) : coe_fn (embedding (ones n) i) { val := 0, property := h } = { val := ↑i, property := lt_of_lt_of_le (subtype.property i) (length_le (ones n)) } := sorry theorem eq_ones_iff {n : ℕ} {c : composition n} : c = ones n ↔ ∀ (i : ℕ), i ∈ blocks c → i = 1 := sorry theorem ne_ones_iff {n : ℕ} {c : composition n} : c ≠ ones n ↔ ∃ (i : ℕ), ∃ (H : i ∈ blocks c), 1 < i := sorry theorem eq_ones_iff_length {n : ℕ} {c : composition n} : c = ones n ↔ length c = n := sorry theorem eq_ones_iff_le_length {n : ℕ} {c : composition n} : c = ones n ↔ n ≤ length c := sorry /-! ### The composition `composition.single` -/ /-- The composition made of a single block of size `n`. -/ def single (n : ℕ) (h : 0 < n) : composition n := mk [n] sorry sorry @[simp] theorem single_length {n : ℕ} (h : 0 < n) : length (single n h) = 1 := rfl @[simp] theorem single_blocks {n : ℕ} (h : 0 < n) : blocks (single n h) = [n] := rfl @[simp] theorem single_blocks_fun {n : ℕ} (h : 0 < n) (i : fin (length (single n h))) : blocks_fun (single n h) i = n := sorry @[simp] theorem single_embedding {n : ℕ} (h : 0 < n) (i : fin n) : coe_fn (embedding (single n h) { val := 0, property := single_length h ▸ zero_lt_one }) i = i := sorry theorem eq_single_iff_length {n : ℕ} (h : 0 < n) {c : composition n} : c = single n h ↔ length c = 1 := sorry theorem ne_single_iff {n : ℕ} (hn : 0 < n) {c : composition n} : c ≠ single n hn ↔ ∀ (i : fin (length c)), blocks_fun c i < n := sorry end composition /-! ### Splitting a list Given a list of length `n` and a composition `c` of `n`, one can split `l` into `c.length` sublists of respective lengths `c.blocks_fun 0`, ..., `c.blocks_fun (c.length-1)`. This is inverse to the join operation. -/ namespace list /-- Auxiliary for `list.split_wrt_composition`. -/ def split_wrt_composition_aux {α : Type u_1} : List α → List ℕ → List (List α) := sorry /-- Given a list of length `n` and a composition `[i₁, ..., iₖ]` of `n`, split `l` into a list of `k` lists corresponding to the blocks of the composition, of respective lengths `i₁`, ..., `iₖ`. This makes sense mostly when `n = l.length`, but this is not necessary for the definition. -/ def split_wrt_composition {n : ℕ} {α : Type u_1} (l : List α) (c : composition n) : List (List α) := split_wrt_composition_aux l (composition.blocks c) theorem split_wrt_composition_aux_cons {α : Type u_1} (l : List α) (n : ℕ) (ns : List ℕ) : split_wrt_composition_aux l (n :: ns) = take n l :: split_wrt_composition_aux (drop n l) ns := sorry theorem length_split_wrt_composition_aux {α : Type u_1} (l : List α) (ns : List ℕ) : length (split_wrt_composition_aux l ns) = length ns := sorry /-- When one splits a list along a composition `c`, the number of sublists thus created is `c.length`. -/ @[simp] theorem length_split_wrt_composition {n : ℕ} {α : Type u_1} (l : List α) (c : composition n) : length (split_wrt_composition l c) = composition.length c := length_split_wrt_composition_aux l (composition.blocks c) theorem map_length_split_wrt_composition_aux {α : Type u_1} {ns : List ℕ} {l : List α} : sum ns ≤ length l → map length (split_wrt_composition_aux l ns) = ns := sorry /-- When one splits a list along a composition `c`, the lengths of the sublists thus created are given by the block sizes in `c`. -/ theorem map_length_split_wrt_composition {α : Type u_1} (l : List α) (c : composition (length l)) : map length (split_wrt_composition l c) = composition.blocks c := map_length_split_wrt_composition_aux (le_of_eq (composition.blocks_sum c)) theorem length_pos_of_mem_split_wrt_composition {α : Type u_1} {l : List α} {l' : List α} {c : composition (length l)} (h : l' ∈ split_wrt_composition l c) : 0 < length l' := sorry theorem sum_take_map_length_split_wrt_composition {α : Type u_1} (l : List α) (c : composition (length l)) (i : ℕ) : sum (take i (map length (split_wrt_composition l c))) = composition.size_up_to c i := sorry theorem nth_le_split_wrt_composition_aux {α : Type u_1} (l : List α) (ns : List ℕ) {i : ℕ} (hi : i < length (split_wrt_composition_aux l ns)) : nth_le (split_wrt_composition_aux l ns) i hi = drop (sum (take i ns)) (take (sum (take (i + 1) ns)) l) := sorry /-- The `i`-th sublist in the splitting of a list `l` along a composition `c`, is the slice of `l` between the indices `c.size_up_to i` and `c.size_up_to (i+1)`, i.e., the indices in the `i`-th block of the composition. -/ theorem nth_le_split_wrt_composition {n : ℕ} {α : Type u_1} (l : List α) (c : composition n) {i : ℕ} (hi : i < length (split_wrt_composition l c)) : nth_le (split_wrt_composition l c) i hi = drop (composition.size_up_to c i) (take (composition.size_up_to c (i + 1)) l) := nth_le_split_wrt_composition_aux l (composition.blocks c) hi theorem join_split_wrt_composition_aux {α : Type u_1} {ns : List ℕ} {l : List α} : sum ns = length l → join (split_wrt_composition_aux l ns) = l := sorry /-- If one splits a list along a composition, and then joins the sublists, one gets back the original list. -/ @[simp] theorem join_split_wrt_composition {α : Type u_1} (l : List α) (c : composition (length l)) : join (split_wrt_composition l c) = l := join_split_wrt_composition_aux (composition.blocks_sum c) /-- If one joins a list of lists and then splits the join along the right composition, one gets back the original list of lists. -/ @[simp] theorem split_wrt_composition_join {α : Type u_1} (L : List (List α)) (c : composition (length (join L))) (h : map length L = composition.blocks c) : split_wrt_composition (join L) c = L := sorry end list /-! ### Compositions as sets Combinatorial viewpoints on compositions, seen as finite subsets of `fin (n+1)` containing `0` and `n`, where the points of the set (other than `n`) correspond to the leftmost points of each block. -/ /-- Bijection between compositions of `n` and subsets of `{0, ..., n-2}`, defined by considering the restriction of the subset to `{1, ..., n-1}` and shifting to the left by one. -/ def composition_as_set_equiv (n : ℕ) : composition_as_set n ≃ finset (fin (n - 1)) := equiv.mk (fun (c : composition_as_set n) => set.to_finset (set_of fun (i : fin (n - 1)) => { val := 1 + ↑i, property := sorry } ∈ composition_as_set.boundaries c)) (fun (s : finset (fin (n - 1))) => composition_as_set.mk (set.to_finset (set_of fun (i : fin (Nat.succ n)) => i = 0 ∨ i = fin.last n ∨ ∃ (j : fin (n - 1)), ∃ (hj : j ∈ s), ↑i = ↑j + 1)) sorry sorry) sorry sorry protected instance composition_as_set_fintype (n : ℕ) : fintype (composition_as_set n) := fintype.of_equiv (finset (fin (n - 1))) (equiv.symm (composition_as_set_equiv n)) theorem composition_as_set_card (n : ℕ) : fintype.card (composition_as_set n) = bit0 1 ^ (n - 1) := sorry namespace composition_as_set theorem boundaries_nonempty {n : ℕ} (c : composition_as_set n) : finset.nonempty (boundaries c) := Exists.intro 0 (zero_mem c) theorem card_boundaries_pos {n : ℕ} (c : composition_as_set n) : 0 < finset.card (boundaries c) := iff.mpr finset.card_pos (boundaries_nonempty c) /-- Number of blocks in a `composition_as_set`. -/ def length {n : ℕ} (c : composition_as_set n) : ℕ := finset.card (boundaries c) - 1 theorem card_boundaries_eq_succ_length {n : ℕ} (c : composition_as_set n) : finset.card (boundaries c) = length c + 1 := iff.mp (nat.sub_eq_iff_eq_add (card_boundaries_pos c)) rfl theorem length_lt_card_boundaries {n : ℕ} (c : composition_as_set n) : length c < finset.card (boundaries c) := eq.mpr (id (Eq._oldrec (Eq.refl (length c < finset.card (boundaries c))) (card_boundaries_eq_succ_length c))) (lt_add_one (length c)) theorem lt_length {n : ℕ} (c : composition_as_set n) (i : fin (length c)) : ↑i + 1 < finset.card (boundaries c) := nat.add_lt_of_lt_sub_right (subtype.property i) theorem lt_length' {n : ℕ} (c : composition_as_set n) (i : fin (length c)) : ↑i < finset.card (boundaries c) := lt_of_le_of_lt (nat.le_succ ↑i) (lt_length c i) /-- Canonical increasing bijection from `fin c.boundaries.card` to `c.boundaries`. -/ def boundary {n : ℕ} (c : composition_as_set n) : fin (finset.card (boundaries c)) ↪o fin (n + 1) := finset.order_emb_of_fin (boundaries c) sorry @[simp] theorem boundary_zero {n : ℕ} (c : composition_as_set n) : coe_fn (boundary c) { val := 0, property := card_boundaries_pos c } = 0 := sorry @[simp] theorem boundary_length {n : ℕ} (c : composition_as_set n) : coe_fn (boundary c) { val := length c, property := length_lt_card_boundaries c } = fin.last n := sorry /-- Size of the `i`-th block in a `composition_as_set`, seen as a function on `fin c.length`. -/ def blocks_fun {n : ℕ} (c : composition_as_set n) (i : fin (length c)) : ℕ := ↑(coe_fn (boundary c) { val := ↑i + 1, property := lt_length c i }) - ↑(coe_fn (boundary c) { val := ↑i, property := lt_length' c i }) theorem blocks_fun_pos {n : ℕ} (c : composition_as_set n) (i : fin (length c)) : 0 < blocks_fun c i := nat.lt_sub_left_of_add_lt (order_embedding.strict_mono (finset.order_emb_of_fin (boundaries c) rfl) (nat.lt_succ_self (subtype.val { val := ↑i, property := lt_length' c i }))) /-- List of the sizes of the blocks in a `composition_as_set`. -/ def blocks {n : ℕ} (c : composition_as_set n) : List ℕ := list.of_fn (blocks_fun c) @[simp] theorem blocks_length {n : ℕ} (c : composition_as_set n) : list.length (blocks c) = length c := list.length_of_fn (blocks_fun c) theorem blocks_partial_sum {n : ℕ} (c : composition_as_set n) {i : ℕ} (h : i < finset.card (boundaries c)) : list.sum (list.take i (blocks c)) = ↑(coe_fn (boundary c) { val := i, property := h }) := sorry theorem mem_boundaries_iff_exists_blocks_sum_take_eq {n : ℕ} (c : composition_as_set n) {j : fin (n + 1)} : j ∈ boundaries c ↔ ∃ (i : ℕ), ∃ (H : i < finset.card (boundaries c)), list.sum (list.take i (blocks c)) = ↑j := sorry theorem blocks_sum {n : ℕ} (c : composition_as_set n) : list.sum (blocks c) = n := sorry /-- Associating a `composition n` to a `composition_as_set n`, by registering the sizes of the blocks as a list of positive integers. -/ def to_composition {n : ℕ} (c : composition_as_set n) : composition n := composition.mk (blocks c) sorry (blocks_sum c) end composition_as_set /-! ### Equivalence between compositions and compositions as sets In this section, we explain how to go back and forth between a `composition` and a `composition_as_set`, by showing that their `blocks` and `length` and `boundaries` correspond to each other, and construct an equivalence between them called `composition_equiv`. -/ @[simp] theorem composition.to_composition_as_set_length {n : ℕ} (c : composition n) : composition_as_set.length (composition.to_composition_as_set c) = composition.length c := sorry @[simp] theorem composition_as_set.to_composition_length {n : ℕ} (c : composition_as_set n) : composition.length (composition_as_set.to_composition c) = composition_as_set.length c := sorry @[simp] theorem composition.to_composition_as_set_blocks {n : ℕ} (c : composition n) : composition_as_set.blocks (composition.to_composition_as_set c) = composition.blocks c := sorry @[simp] theorem composition_as_set.to_composition_blocks {n : ℕ} (c : composition_as_set n) : composition.blocks (composition_as_set.to_composition c) = composition_as_set.blocks c := rfl @[simp] theorem composition_as_set.to_composition_boundaries {n : ℕ} (c : composition_as_set n) : composition.boundaries (composition_as_set.to_composition c) = composition_as_set.boundaries c := sorry @[simp] theorem composition.to_composition_as_set_boundaries {n : ℕ} (c : composition n) : composition_as_set.boundaries (composition.to_composition_as_set c) = composition.boundaries c := rfl /-- Equivalence between `composition n` and `composition_as_set n`. -/ def composition_equiv (n : ℕ) : composition n ≃ composition_as_set n := equiv.mk (fun (c : composition n) => composition.to_composition_as_set c) (fun (c : composition_as_set n) => composition_as_set.to_composition c) sorry sorry protected instance composition_fintype (n : ℕ) : fintype (composition n) := fintype.of_equiv (composition_as_set n) (equiv.symm (composition_equiv n)) theorem composition_card (n : ℕ) : fintype.card (composition n) = bit0 1 ^ (n - 1) := eq.mpr (id (Eq._oldrec (Eq.refl (fintype.card (composition n) = bit0 1 ^ (n - 1))) (Eq.symm (composition_as_set_card n)))) (fintype.card_congr (composition_equiv n))
195558eda4e3a8df3d4e838ce8416bcc0bb98060
6cc23d886ccf271bfd0c9ca5d29cafb9c0be7bf5
/src/Blake3.lean
b83904537e0426ede7753589708d671ba0afc9d9
[]
no_license
Anderssorby/LeanPlay
d39b15dbc3441c2be5a4ea5adf8fe70c4648737b
8fd63d98d02490060323f4c5117b9a7e8da50813
refs/heads/main
1,692,004,930,931
1,633,974,729,000
1,633,974,729,000
368,654,414
0
0
null
null
null
null
UTF-8
Lean
false
false
1,570
lean
/- Bindings to the Blake3 hashing library. -/ namespace Blake3 constant BLAKE3_KEY_LEN: UInt16 := 32 constant BLAKE3_OUT_LEN: UInt16 := 32 constant BLAKE3_BLOCK_LEN: UInt16 := 64 constant BLAKE3_CHUNK_LEN: UInt16 := 1024 constant BLAKE3_MAX_DEPTH: UInt16 := 54 @[extern "blake3_hasher"] constant Blake3Hasher : Type := Unit /- Version of the linked BLAKE3 implementation library. -/ @[extern "blake3_version"] constant internalVersion : Unit → String constant version : String := internalVersion Unit.unit /- Initialize a hasher. -/ @[extern "blake3_hasher_init"] constant initHasher : IO Blake3Hasher @[extern "blake3_hasher_init_keyed"] constant initHasherKeyed (key: Array UInt8) : IO Blake3Hasher @[extern "blake3_hasher_init_derive_key"] constant initHasherDeriveKey (context: String) : IO Blake3Hasher @[extern "blake3_hasher_init_derive_key_raw"] constant initHasherDeriveKeyRaw (context: String) (contextLength : USize) : IO Blake3Hasher /- Put more data into the hasher. This can be called several times. -/ @[extern "blake3_hasher_update"] constant hasherUpdate : (input : ByteArray) → (length : USize) → IO Blake3Hasher /- Finalize the hasher and write the output to an initialized array. -/ @[extern "blake3_hasher_finalize"] constant hasherFinalize : (output : ByteArray) → (length : USize) → IO Blake3Hasher /- Finalize the hasher and write the output to an initialized array. -/ @[extern "blake3_hasher_finalize_seek"] constant hasherFinalizeSeek : (seek : UInt64) → (output : ByteArray) → (length : USize) → IO Blake3Hasher
e7b3829e34128d31c48eb698b54225912af26088
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/linear_algebra/tensor_product.lean
6811aaf0165d51b7201962b7c9048b47b6920ccc
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
17,631
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Mario Carneiro Tensor product of modules over commutative rings. -/ import group_theory.free_abelian_group import linear_algebra.direct_sum_module variables {R : Type*} [comm_ring R] variables {M : Type*} {N : Type*} {P : Type*} {Q : Type*} {S : Type*} variables [add_comm_group M] [add_comm_group N] [add_comm_group P] [add_comm_group Q] [add_comm_group S] variables [module R M] [module R N] [module R P] [module R Q] [module R S] include R set_option class.instance_max_depth 100 namespace linear_map variables (R) def mk₂ (f : M → N → P) (H1 : ∀ m₁ m₂ n, f (m₁ + m₂) n = f m₁ n + f m₂ n) (H2 : ∀ (c:R) m n, f (c • m) n = c • f m n) (H3 : ∀ m n₁ n₂, f m (n₁ + n₂) = f m n₁ + f m n₂) (H4 : ∀ (c:R) m n, f m (c • n) = c • f m n) : M →ₗ N →ₗ P := ⟨λ m, ⟨f m, H3 m, λ c, H4 c m⟩, λ m₁ m₂, linear_map.ext $ H1 m₁ m₂, λ c m, linear_map.ext $ H2 c m⟩ variables {R} @[simp] theorem mk₂_apply (f : M → N → P) {H1 H2 H3 H4} (m : M) (n : N) : (mk₂ R f H1 H2 H3 H4 : M →ₗ[R] N →ₗ P) m n = f m n := rfl variables (f : M →ₗ[R] N →ₗ[R] P) theorem ext₂ {f g : M →ₗ[R] N →ₗ[R] P} (H : ∀ m n, f m n = g m n) : f = g := linear_map.ext (λ m, linear_map.ext $ λ n, H m n) def flip : N →ₗ M →ₗ P := mk₂ R (λ n m, f m n) (λ n₁ n₂ m, (f m).map_add _ _) (λ c n m, (f m).map_smul _ _) (λ n m₁ m₂, by rw f.map_add; refl) (λ c n m, by rw f.map_smul; refl) @[simp] theorem flip_apply (m : M) (n : N) : flip f n m = f m n := rfl variables {R} theorem flip_inj {f g : M →ₗ[R] N →ₗ P} (H : flip f = flip g) : f = g := ext₂ $ λ m n, show flip f n m = flip g n m, by rw H variables (R M N P) def lflip : (M →ₗ[R] N →ₗ P) →ₗ[R] N →ₗ M →ₗ P := ⟨flip, λ _ _, rfl, λ _ _, rfl⟩ variables {R M N P} @[simp] theorem lflip_apply (m : M) (n : N) : lflip R M N P f n m = f m n := rfl theorem map_zero₂ (y) : f 0 y = 0 := (flip f y).map_zero theorem map_neg₂ (x y) : f (-x) y = -f x y := (flip f y).map_neg _ theorem map_add₂ (x₁ x₂ y) : f (x₁ + x₂) y = f x₁ y + f x₂ y := (flip f y).map_add _ _ theorem map_smul₂ (r:R) (x y) : f (r • x) y = r • f x y := (flip f y).map_smul _ _ variables (R P) def lcomp (f : M →ₗ[R] N) : (N →ₗ P) →ₗ M →ₗ P := flip $ (flip id).comp f variables {R P} @[simp] theorem lcomp_apply (f : M →ₗ[R] N) (g : N →ₗ P) (x : M) : lcomp R P f g x = g (f x) := rfl variables (R M N P) def llcomp : (N →ₗ[R] P) →ₗ[R] (M →ₗ[R] N) →ₗ M →ₗ P := flip ⟨lcomp R P, λ f f', ext₂ $ λ g x, g.map_add _ _, λ c f, ext₂ $ λ g x, g.map_smul _ _⟩ variables {R M N P} section @[simp] theorem llcomp_apply (f : N →ₗ[R] P) (g : M →ₗ[R] N) (x : M) : llcomp R M N P f g x = f (g x) := rfl end def compl₂ (g : Q →ₗ N) : M →ₗ Q →ₗ P := (lcomp R _ g).comp f @[simp] theorem compl₂_apply (g : Q →ₗ[R] N) (m : M) (q : Q) : f.compl₂ g m q = f m (g q) := rfl def compr₂ (g : P →ₗ Q) : M →ₗ N →ₗ Q := linear_map.comp (llcomp R N P Q g) f @[simp] theorem compr₂_apply (g : P →ₗ[R] Q) (m : M) (n : N) : f.compr₂ g m n = g (f m n) := rfl variables (R M) def lsmul : R →ₗ M →ₗ M := mk₂ R (•) add_smul (λ _ _ _, mul_smul _ _ _) smul_add (λ r s m, by simp only [smul_smul, smul_eq_mul, mul_comm]) variables {R M} @[simp] theorem lsmul_apply (r : R) (m : M) : lsmul R M r m = r • m := rfl end linear_map variables (M N) namespace tensor_product section open free_abelian_group variables (R) def relators : set (free_abelian_group (M × N)) := add_group.closure { x : free_abelian_group (M × N) | (∃ (m₁ m₂ : M) (n : N), x = of (m₁, n) + of (m₂, n) - of (m₁ + m₂, n)) ∨ (∃ (m : M) (n₁ n₂ : N), x = of (m, n₁) + of (m, n₂) - of (m, n₁ + n₂)) ∨ (∃ (r : R) (m : M) (n : N), x = of (r • m, n) - of (m, r • n)) } end namespace relators instance : normal_add_subgroup (relators R M N) := by unfold relators; apply normal_add_subgroup_of_add_comm_group end relators end tensor_product variables (R) def tensor_product : Type* := quotient_add_group.quotient (tensor_product.relators R M N) variables {R} localized "infix ` ⊗ `:100 := tensor_product _" in tensor_product localized "notation M ` ⊗[`:100 R `] ` N:100 := tensor_product R M N" in tensor_product namespace tensor_product section module local attribute [instance] quotient_add_group.left_rel normal_add_subgroup.to_is_add_subgroup instance : add_comm_group (M ⊗[R] N) := quotient_add_group.add_comm_group _ instance : inhabited (M ⊗[R] N) := ⟨0⟩ instance quotient.mk.is_add_group_hom : is_add_group_hom (quotient.mk : free_abelian_group (M × N) → M ⊗ N) := quotient_add_group.is_add_group_hom _ variables (R) {M N} def tmul (m : M) (n : N) : M ⊗[R] N := quotient_add_group.mk $ free_abelian_group.of (m, n) variables {R} infix ` ⊗ₜ `:100 := tmul _ notation x ` ⊗ₜ[`:100 R `] ` y := tmul R x y lemma add_tmul (m₁ m₂ : M) (n : N) : (m₁ + m₂) ⊗ₜ n = m₁ ⊗ₜ n + m₂ ⊗ₜ[R] n := eq.symm $ sub_eq_zero.1 $ eq.symm $ quotient.sound $ add_group.in_closure.basic $ or.inl $ ⟨m₁, m₂, n, rfl⟩ lemma tmul_add (m : M) (n₁ n₂ : N) : m ⊗ₜ (n₁ + n₂) = m ⊗ₜ n₁ + m ⊗ₜ[R] n₂ := eq.symm $ sub_eq_zero.1 $ eq.symm $ quotient.sound $ add_group.in_closure.basic $ or.inr $ or.inl $ ⟨m, n₁, n₂, rfl⟩ lemma smul_tmul (r : R) (m : M) (n : N) : (r • m) ⊗ₜ n = m ⊗ₜ[R] (r • n) := sub_eq_zero.1 $ eq.symm $ quotient.sound $ add_group.in_closure.basic $ or.inr $ or.inr $ ⟨r, m, n, rfl⟩ local attribute [instance] quotient_add_group.is_add_group_hom_quotient_lift def smul.aux (r : R) : free_abelian_group (M × N) → M ⊗[R] N := free_abelian_group.lift (λ (y : M × N), (r • y.1) ⊗ₜ y.2) instance (r : R) : is_add_group_hom (smul.aux r : _ → M ⊗ N) := by unfold smul.aux; apply_instance instance : has_scalar R (M ⊗ N) := ⟨λ r, quotient_add_group.lift _ (smul.aux r) $ λ x hx, begin refine (is_add_group_hom.mem_ker (smul.aux r : _ → M ⊗ N)).1 (add_group.closure_subset _ hx), clear hx x, rintro x (⟨m₁, m₂, n, rfl⟩ | ⟨m, n₁, n₂, rfl⟩ | ⟨q, m, n, rfl⟩); simp only [smul.aux, is_add_group_hom.mem_ker, -sub_eq_add_neg, sub_self, add_tmul, tmul_add, smul_tmul, smul_add, smul_smul, mul_comm, free_abelian_group.lift.of, free_abelian_group.lift.add, free_abelian_group.lift.sub] end⟩ instance smul.is_add_group_hom (r : R) : is_add_group_hom ((•) r : M ⊗[R] N → M ⊗[R] N) := by unfold has_scalar.smul; apply_instance protected theorem smul_add (r : R) (x y : M ⊗[R] N) : r • (x + y) = r • x + r • y := is_add_hom.map_add _ _ _ instance : module R (M ⊗ N) := module.of_core { smul := (•), smul_add := tensor_product.smul_add, add_smul := begin intros r s x, apply quotient_add_group.induction_on' x, intro z, symmetry, refine @free_abelian_group.lift.unique _ _ _ _ _ (is_add_group_hom.mk' $ λ p q, _) _ z, { simp [tensor_product.smul_add, add_comm, add_left_comm] }, rintro ⟨m, n⟩, change (r • m) ⊗ₜ n + (s • m) ⊗ₜ n = ((r + s) • m) ⊗ₜ n, rw [add_smul, add_tmul] end, mul_smul := begin intros r s x, apply quotient_add_group.induction_on' x, intro z, symmetry, refine @free_abelian_group.lift.unique _ _ _ _ _ (is_add_group_hom.mk' $ λ p q, _) _ z, { simp [tensor_product.smul_add] }, rintro ⟨m, n⟩, change r • s • (m ⊗ₜ n) = ((r * s) • m) ⊗ₜ n, rw mul_smul, refl end, one_smul := λ x, quotient.induction_on x $ λ _, eq.symm $ free_abelian_group.lift.unique _ _ $ λ ⟨p, q⟩, by rw one_smul; refl } @[simp] lemma tmul_smul (r : R) (x : M) (y : N) : x ⊗ₜ (r • y) = r • (x ⊗ₜ[R] y) := (smul_tmul _ _ _).symm variables (R M N) def mk : M →ₗ N →ₗ M ⊗ N := linear_map.mk₂ R (⊗ₜ) add_tmul (λ c m n, by rw [smul_tmul, tmul_smul]) tmul_add tmul_smul variables {R M N} @[simp] lemma mk_apply (m : M) (n : N) : mk R M N m n = m ⊗ₜ n := rfl lemma zero_tmul (n : N) : (0 ⊗ₜ[R] n : M ⊗ N) = 0 := (mk R M N).map_zero₂ _ lemma tmul_zero (m : M) : (m ⊗ₜ[R] 0 : M ⊗ N) = 0 := (mk R M N _).map_zero lemma neg_tmul (m : M) (n : N) : (-m) ⊗ₜ n = -(m ⊗ₜ[R] n) := (mk R M N).map_neg₂ _ _ lemma tmul_neg (m : M) (n : N) : m ⊗ₜ (-n) = -(m ⊗ₜ[R] n) := (mk R M N _).map_neg _ end module local attribute [instance] quotient_add_group.left_rel normal_add_subgroup.to_is_add_subgroup @[elab_as_eliminator] protected theorem induction_on {C : (M ⊗[R] N) → Prop} (z : M ⊗[R] N) (C0 : C 0) (C1 : ∀ x y, C $ x ⊗ₜ[R] y) (Cp : ∀ x y, C x → C y → C (x + y)) : C z := quotient.induction_on z $ λ x, free_abelian_group.induction_on x C0 (λ ⟨p, q⟩, C1 p q) (λ ⟨p, q⟩ _, show C (-(p ⊗ₜ q)), by rw ← neg_tmul; from C1 (-p) q) (λ _ _, Cp _ _) section UMP variables {M N P Q} variables (f : M →ₗ[R] N →ₗ[R] P) local attribute [instance] free_abelian_group.lift.is_add_group_hom def lift_aux : (M ⊗[R] N) → P := quotient_add_group.lift _ (free_abelian_group.lift $ λ z, f z.1 z.2) $ λ x hx, begin refine (is_add_group_hom.mem_ker _).1 (add_group.closure_subset _ hx), clear hx x, rintro x (⟨m₁, m₂, n, rfl⟩ | ⟨m, n₁, n₂, rfl⟩ | ⟨q, m, n, rfl⟩); simp [is_add_group_hom.mem_ker, -sub_eq_add_neg, f.map_add, f.map_add₂, f.map_smul, f.map_smul₂, sub_self], end variable {f} local attribute [instance] quotient_add_group.left_rel normal_add_subgroup.to_is_add_subgroup @[simp] lemma lift_aux.add (x y) : lift_aux f (x + y) = lift_aux f x + lift_aux f y := quotient.induction_on₂ x y $ λ m n, free_abelian_group.lift.add _ _ _ @[simp] lemma lift_aux.smul (r:R) (x) : lift_aux f (r • x) = r • lift_aux f x := tensor_product.induction_on _ _ x (smul_zero _).symm (λ p q, by rw [← tmul_smul]; simp [lift_aux, tmul]) (λ p q ih1 ih2, by simp [@smul_add _ _ _ _ _ _ p _, lift_aux.add, ih1, ih2, smul_add]) variable (f) def lift : M ⊗ N →ₗ P := { to_fun := lift_aux f, add := lift_aux.add, smul := lift_aux.smul } variable {f} @[simp] lemma lift.tmul (x y) : lift f (x ⊗ₜ y) = f x y := zero_add _ @[simp] lemma lift.tmul' (x y) : (lift f).1 (x ⊗ₜ y) = f x y := lift.tmul _ _ theorem lift.unique {g : (M ⊗[R] N) →ₗ[R] P} (H : ∀ x y, g (x ⊗ₜ y) = f x y) : g = lift f := linear_map.ext $ λ z, begin apply quotient_add_group.induction_on' z, intro z, refine @free_abelian_group.lift.unique _ _ _ _ _ (is_add_group_hom.mk' $ λ p q, _) _ z, { simp [g.2] }, exact λ ⟨m, n⟩, H m n end theorem lift_mk : lift (mk R M N) = linear_map.id := eq.symm $ lift.unique $ λ x y, rfl theorem lift_compr₂ (g : P →ₗ Q) : lift (f.compr₂ g) = g.comp (lift f) := eq.symm $ lift.unique $ λ x y, by simp theorem lift_mk_compr₂ (f : M ⊗ N →ₗ P) : lift ((mk R M N).compr₂ f) = f := by rw [lift_compr₂, lift_mk, linear_map.comp_id] @[ext] theorem ext {g h : (M ⊗[R] N) →ₗ[R] P} (H : ∀ x y, g (x ⊗ₜ y) = h (x ⊗ₜ y)) : g = h := by rw ← lift_mk_compr₂ h; exact lift.unique H theorem mk_compr₂_inj {g h : M ⊗ N →ₗ P} (H : (mk R M N).compr₂ g = (mk R M N).compr₂ h) : g = h := by rw [← lift_mk_compr₂ g, H, lift_mk_compr₂] example : M → N → (M → N → P) → P := λ m, flip $ λ f, f m variables (R M N P) def uncurry : (M →ₗ N →ₗ[R] P) →ₗ M ⊗ N →ₗ P := linear_map.flip $ lift $ (linear_map.lflip _ _ _ _).comp linear_map.id.flip variables {R M N P} @[simp] theorem uncurry_apply (f : M →ₗ[R] N →ₗ[R] P) (m : M) (n : N) : uncurry R M N P f (m ⊗ₜ n) = f m n := by rw [uncurry, linear_map.flip_apply, lift.tmul]; refl variables (R M N P) def lift.equiv : (M →ₗ N →ₗ P) ≃ₗ (M ⊗ N →ₗ P) := { inv_fun := λ f, (mk R M N).compr₂ f, left_inv := λ f, linear_map.ext₂ $ λ m n, lift.tmul _ _, right_inv := λ f, ext $ λ m n, lift.tmul _ _, .. uncurry R M N P } def lcurry : (M ⊗[R] N →ₗ[R] P) →ₗ[R] M →ₗ[R] N →ₗ[R] P := (lift.equiv R M N P).symm variables {R M N P} @[simp] theorem lcurry_apply (f : M ⊗[R] N →ₗ[R] P) (m : M) (n : N) : lcurry R M N P f m n = f (m ⊗ₜ n) := rfl def curry (f : M ⊗ N →ₗ P) : M →ₗ N →ₗ P := lcurry R M N P f @[simp] theorem curry_apply (f : M ⊗ N →ₗ[R] P) (m : M) (n : N) : curry f m n = f (m ⊗ₜ n) := rfl theorem ext_threefold {g h : (M ⊗[R] N) ⊗[R] P →ₗ[R] Q} (H : ∀ x y z, g ((x ⊗ₜ y) ⊗ₜ z) = h ((x ⊗ₜ y) ⊗ₜ z)) : g = h := begin let e := linear_equiv.to_equiv (lift.equiv R (M ⊗[R] N) P Q), apply e.symm.injective, ext x y z, exact H x y z, end -- We'll need this one for checking the pentagon identity! theorem ext_fourfold {g h : ((M ⊗[R] N) ⊗[R] P) ⊗[R] Q →ₗ[R] S} (H : ∀ w x y z, g (((w ⊗ₜ x) ⊗ₜ y) ⊗ₜ z) = h (((w ⊗ₜ x) ⊗ₜ y) ⊗ₜ z)) : g = h := begin let e := linear_equiv.to_equiv (lift.equiv R ((M ⊗[R] N) ⊗[R] P) Q S), apply e.symm.injective, apply ext_threefold, intros x y z, ext w, exact H x y z w, end end UMP variables {M N} section variables (R M) /-- The base ring is a left identity for the tensor product of modules, up to linear equivalence. -/ protected def lid : R ⊗ M ≃ₗ M := linear_equiv.of_linear (lift $ linear_map.lsmul R M) (mk R R M 1) (linear_map.ext $ λ _, by simp) (ext $ λ r m, by simp; rw [← tmul_smul, ← smul_tmul, smul_eq_mul, mul_one]) end @[simp] theorem lid_tmul (m : M) (r : R) : ((tensor_product.lid R M) : (R ⊗ M → M)) (r ⊗ₜ m) = r • m := begin dsimp [tensor_product.lid], simp, end section variables (R M N) /-- The tensor product of modules is commutative, up to linear equivalence. -/ protected def comm : M ⊗ N ≃ₗ N ⊗ M := linear_equiv.of_linear (lift (mk R N M).flip) (lift (mk R M N).flip) (ext $ λ m n, rfl) (ext $ λ m n, rfl) end section variables (R M) /-- The base ring is a right identity for the tensor product of modules, up to linear equivalence. -/ protected def rid : M ⊗[R] R ≃ₗ M := linear_equiv.trans (tensor_product.comm R M R) (tensor_product.lid R M) end @[simp] theorem rid_tmul (m : M) (r : R) : ((tensor_product.rid R M) : (M ⊗ R → M)) (m ⊗ₜ r) = r • m := begin dsimp [tensor_product.rid, tensor_product.comm, tensor_product.lid], simp, end open linear_map section variables (R M N P) /-- The associator for tensor product of R-modules, as a linear equivalence. -/ protected def assoc : (M ⊗[R] N) ⊗[R] P ≃ₗ[R] M ⊗[R] (N ⊗[R] P) := begin refine linear_equiv.of_linear (lift $ lift $ comp (lcurry R _ _ _) $ mk _ _ _) (lift $ comp (uncurry R _ _ _) $ curry $ mk _ _ _) (mk_compr₂_inj $ linear_map.ext $ λ m, ext $ λ n p, _) (mk_compr₂_inj $ flip_inj $ linear_map.ext $ λ p, ext $ λ m n, _); repeat { rw lift.tmul <|> rw compr₂_apply <|> rw comp_apply <|> rw mk_apply <|> rw flip_apply <|> rw lcurry_apply <|> rw uncurry_apply <|> rw curry_apply <|> rw id_apply } end end @[simp] theorem assoc_tmul (m : M) (n : N) (p : P) : ((tensor_product.assoc R M N P) : (M ⊗[R] N) ⊗[R] P → M ⊗[R] (N ⊗[R] P)) ((m ⊗ₜ n) ⊗ₜ p) = m ⊗ₜ (n ⊗ₜ p) := rfl /-- The tensor product of a pair of linear maps between modules. -/ def map (f : M →ₗ[R] P) (g : N →ₗ Q) : M ⊗ N →ₗ P ⊗ Q := lift $ comp (compl₂ (mk _ _ _) g) f @[simp] theorem map_tmul (f : M →ₗ[R] P) (g : N →ₗ[R] Q) (m : M) (n : N) : map f g (m ⊗ₜ n) = f m ⊗ₜ g n := rfl def congr (f : M ≃ₗ[R] P) (g : N ≃ₗ[R] Q) : M ⊗ N ≃ₗ[R] P ⊗ Q := linear_equiv.of_linear (map f g) (map f.symm g.symm) (ext $ λ m n, by simp; simp only [linear_equiv.apply_symm_apply]) (ext $ λ m n, by simp; simp only [linear_equiv.symm_apply_apply]) variables (ι₁ : Type*) (ι₂ : Type*) variables [decidable_eq ι₁] [decidable_eq ι₂] variables (M₁ : ι₁ → Type*) (M₂ : ι₂ → Type*) variables [Π i₁, add_comm_group (M₁ i₁)] [Π i₂, add_comm_group (M₂ i₂)] variables [Π i₁, module R (M₁ i₁)] [Π i₂, module R (M₂ i₂)] def direct_sum : direct_sum ι₁ M₁ ⊗[R] direct_sum ι₂ M₂ ≃ₗ[R] direct_sum (ι₁ × ι₂) (λ i, M₁ i.1 ⊗[R] M₂ i.2) := begin refine linear_equiv.of_linear (lift $ direct_sum.to_module R _ _ $ λ i₁, flip $ direct_sum.to_module R _ _ $ λ i₂, flip $ curry $ direct_sum.lof R (ι₁ × ι₂) (λ i, M₁ i.1 ⊗[R] M₂ i.2) (i₁, i₂)) (direct_sum.to_module R _ _ $ λ i, map (direct_sum.lof R _ _ _) (direct_sum.lof R _ _ _)) (linear_map.ext $ direct_sum.to_module.ext _ $ λ i, mk_compr₂_inj $ linear_map.ext $ λ x₁, linear_map.ext $ λ x₂, _) (mk_compr₂_inj $ linear_map.ext $ direct_sum.to_module.ext _ $ λ i₁, linear_map.ext $ λ x₁, linear_map.ext $ direct_sum.to_module.ext _ $ λ i₂, linear_map.ext $ λ x₂, _); repeat { rw compr₂_apply <|> rw comp_apply <|> rw id_apply <|> rw mk_apply <|> rw direct_sum.to_module_lof <|> rw map_tmul <|> rw lift.tmul <|> rw flip_apply <|> rw curry_apply }, cases i; refl end end tensor_product
02878a21a0e92d214210b1c0383b72bb139b3aa5
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/lake/examples/deps/foo/Foo/Foo.lean
060b281a849c182b41e8941da1fcd81219cb0c62
[ "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
39
lean
def foo := "foo" --#eval IO.sleep 3000
6541af8ef14f189e71f344dc5cc7824bd5f8a85c
c777c32c8e484e195053731103c5e52af26a25d1
/src/algebra/hom/equiv/basic.lean
423615c762cc7b23e077ac570fe99019e7b35f3f
[ "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
22,257
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, Callum Sutton, Yury Kudryashov -/ import algebra.hom.group import data.fun_like.equiv import logic.equiv.basic import data.pi.algebra /-! # Multiplicative and additive equivs > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we define two extensions of `equiv` called `add_equiv` and `mul_equiv`, which are datatypes representing isomorphisms of `add_monoid`s/`add_group`s and `monoid`s/`group`s. ## Notations * ``infix ` ≃* `:25 := mul_equiv`` * ``infix ` ≃+ `:25 := add_equiv`` The extended equivs all have coercions to functions, and the coercions are the canonical notation when treating the isomorphisms as maps. ## Implementation notes The fields for `mul_equiv`, `add_equiv` now avoid the unbundled `is_mul_hom` and `is_add_hom`, as these are deprecated. ## Tags equiv, mul_equiv, add_equiv -/ variables {F α β A B M N P Q G H : Type*} /-- Makes a multiplicative inverse from a bijection which preserves multiplication. -/ @[to_additive "Makes an additive inverse from a bijection which preserves addition."] def mul_hom.inverse [has_mul M] [has_mul N] (f : M →ₙ* N) (g : N → M) (h₁ : function.left_inverse g f) (h₂ : function.right_inverse g f) : N →ₙ* M := { to_fun := g, map_mul' := λ x y, calc g (x * y) = g (f (g x) * f (g y)) : by rw [h₂ x, h₂ y] ... = g (f (g x * g y)) : by rw f.map_mul ... = g x * g y : h₁ _, } /-- The inverse of a bijective `monoid_hom` is a `monoid_hom`. -/ @[to_additive "The inverse of a bijective `add_monoid_hom` is an `add_monoid_hom`.", simps] def monoid_hom.inverse {A B : Type*} [monoid A] [monoid B] (f : A →* B) (g : B → A) (h₁ : function.left_inverse g f) (h₂ : function.right_inverse g f) : B →* A := { to_fun := g, map_one' := by rw [← f.map_one, h₁], .. (f : A →ₙ* B).inverse g h₁ h₂, } set_option old_structure_cmd true /-- add_equiv α β is the type of an equiv α ≃ β which preserves addition. -/ @[ancestor equiv add_hom] structure add_equiv (A B : Type*) [has_add A] [has_add B] extends A ≃ B, add_hom A B /-- `add_equiv_class F A B` states that `F` is a type of addition-preserving morphisms. You should extend this class when you extend `add_equiv`. -/ class add_equiv_class (F A B : Type*) [has_add A] [has_add B] extends equiv_like F A B := (map_add : ∀ (f : F) a b, f (a + b) = f a + f b) /-- The `equiv` underlying an `add_equiv`. -/ add_decl_doc add_equiv.to_equiv /-- The `add_hom` underlying a `add_equiv`. -/ add_decl_doc add_equiv.to_add_hom /-- `mul_equiv α β` is the type of an equiv `α ≃ β` which preserves multiplication. -/ @[ancestor equiv mul_hom, to_additive] structure mul_equiv (M N : Type*) [has_mul M] [has_mul N] extends M ≃ N, M →ₙ* N /-- The `equiv` underlying a `mul_equiv`. -/ add_decl_doc mul_equiv.to_equiv /-- The `mul_hom` underlying a `mul_equiv`. -/ add_decl_doc mul_equiv.to_mul_hom /-- `mul_equiv_class F A B` states that `F` is a type of multiplication-preserving morphisms. You should extend this class when you extend `mul_equiv`. -/ @[to_additive] class mul_equiv_class (F A B : Type*) [has_mul A] [has_mul B] extends equiv_like F A B := (map_mul : ∀ (f : F) a b, f (a * b) = f a * f b) infix ` ≃* `:25 := mul_equiv infix ` ≃+ `:25 := add_equiv namespace mul_equiv_class variables (F) @[priority 100, -- See note [lower instance priority] to_additive] instance [has_mul M] [has_mul N] [h : mul_equiv_class F M N] : mul_hom_class F M N := { coe := (coe : F → M → N), coe_injective' := @fun_like.coe_injective F _ _ _, .. h } @[priority 100, -- See note [lower instance priority] to_additive] instance [mul_one_class M] [mul_one_class N] [mul_equiv_class F M N] : monoid_hom_class F M N := { coe := (coe : F → M → N), map_one := λ e, calc e 1 = e 1 * 1 : (mul_one _).symm ... = e 1 * e (inv e (1 : N) : M) : congr_arg _ (right_inv e 1).symm ... = e (inv e (1 : N)) : by rw [← map_mul, one_mul] ... = 1 : right_inv e 1, .. mul_equiv_class.mul_hom_class F } @[priority 100] -- See note [lower instance priority] instance to_monoid_with_zero_hom_class {α β : Type*} [mul_zero_one_class α] [mul_zero_one_class β] [mul_equiv_class F α β] : monoid_with_zero_hom_class F α β := { map_zero := λ e, calc e 0 = e 0 * e (equiv_like.inv e 0) : by rw [←map_mul, zero_mul] ... = 0 : by { convert mul_zero _, exact equiv_like.right_inv e _ } ..mul_equiv_class.monoid_hom_class _ } variables {F} @[simp, to_additive] lemma map_eq_one_iff {M N} [mul_one_class M] [mul_one_class N] [mul_equiv_class F M N] (h : F) {x : M} : h x = 1 ↔ x = 1 := map_eq_one_iff h (equiv_like.injective h) @[to_additive] lemma map_ne_one_iff {M N} [mul_one_class M] [mul_one_class N] [mul_equiv_class F M N] (h : F) {x : M} : h x ≠ 1 ↔ x ≠ 1 := map_ne_one_iff h (equiv_like.injective h) end mul_equiv_class @[to_additive] instance [has_mul α] [has_mul β] [mul_equiv_class F α β] : has_coe_t F (α ≃* β) := ⟨λ f, { to_fun := f, inv_fun := equiv_like.inv f, left_inv := equiv_like.left_inv f, right_inv := equiv_like.right_inv f, map_mul' := map_mul f }⟩ namespace mul_equiv @[to_additive] instance [has_mul M] [has_mul N] : has_coe_to_fun (M ≃* N) (λ _, M → N) := ⟨mul_equiv.to_fun⟩ @[to_additive] instance [has_mul M] [has_mul N] : mul_equiv_class (M ≃* N) M N := { coe := to_fun, inv := inv_fun, left_inv := left_inv, right_inv := right_inv, coe_injective' := λ f g h₁ h₂, by { cases f, cases g, congr' }, map_mul := map_mul' } variables [has_mul M] [has_mul N] [has_mul P] [has_mul Q] @[simp, to_additive] lemma to_equiv_eq_coe (f : M ≃* N) : f.to_equiv = f := rfl @[simp, to_additive] lemma to_fun_eq_coe {f : M ≃* N} : f.to_fun = f := rfl @[simp, to_additive] lemma coe_to_equiv {f : M ≃* N} : ⇑(f : M ≃ N) = f := rfl @[simp, to_additive] lemma coe_to_mul_hom {f : M ≃* N} : ⇑f.to_mul_hom = f := rfl /-- A multiplicative isomorphism preserves multiplication. -/ @[to_additive "An additive isomorphism preserves addition."] protected lemma map_mul (f : M ≃* N) : ∀ x y, f (x * y) = f x * f y := map_mul f /-- Makes a multiplicative isomorphism from a bijection which preserves multiplication. -/ @[to_additive "Makes an additive isomorphism from a bijection which preserves addition."] def mk' (f : M ≃ N) (h : ∀ x y, f (x * y) = f x * f y) : M ≃* N := ⟨f.1, f.2, f.3, f.4, h⟩ @[to_additive] protected lemma bijective (e : M ≃* N) : function.bijective e := equiv_like.bijective e @[to_additive] protected lemma injective (e : M ≃* N) : function.injective e := equiv_like.injective e @[to_additive] protected lemma surjective (e : M ≃* N) : function.surjective e := equiv_like.surjective e /-- The identity map is a multiplicative isomorphism. -/ @[refl, to_additive "The identity map is an additive isomorphism."] def refl (M : Type*) [has_mul M] : M ≃* M := { map_mul' := λ _ _, rfl, ..equiv.refl _} @[to_additive] instance : inhabited (M ≃* M) := ⟨refl M⟩ /-- The inverse of an isomorphism is an isomorphism. -/ @[symm, to_additive "The inverse of an isomorphism is an isomorphism."] def symm (h : M ≃* N) : N ≃* M := { map_mul' := (h.to_mul_hom.inverse h.to_equiv.symm h.left_inv h.right_inv).map_mul, .. h.to_equiv.symm} @[simp, to_additive] lemma inv_fun_eq_symm {f : M ≃* N} : f.inv_fun = f.symm := rfl /-- See Note [custom simps projection] -/ -- we don't hyperlink the note in the additive version, since that breaks syntax highlighting -- in the whole file. @[to_additive "See Note custom simps projection"] def simps.symm_apply (e : M ≃* N) : N → M := e.symm initialize_simps_projections add_equiv (to_fun → apply, inv_fun → symm_apply) initialize_simps_projections mul_equiv (to_fun → apply, inv_fun → symm_apply) @[simp, to_additive] theorem to_equiv_symm (f : M ≃* N) : f.symm.to_equiv = f.to_equiv.symm := rfl @[simp, to_additive] theorem coe_mk (f : M → N) (g h₁ h₂ h₃) : ⇑(mul_equiv.mk f g h₁ h₂ h₃) = f := rfl @[simp, to_additive] lemma to_equiv_mk (f : M → N) (g : N → M) (h₁ h₂ h₃) : (mk f g h₁ h₂ h₃).to_equiv = ⟨f, g, h₁, h₂⟩ := rfl @[simp, to_additive] lemma symm_symm : ∀ (f : M ≃* N), f.symm.symm = f | ⟨f, g, h₁, h₂, h₃⟩ := rfl @[to_additive] lemma symm_bijective : function.bijective (symm : (M ≃* N) → (N ≃* M)) := equiv.bijective ⟨symm, symm, symm_symm, symm_symm⟩ @[simp, to_additive] theorem symm_mk (f : M → N) (g h₁ h₂ h₃) : (mul_equiv.mk f g h₁ h₂ h₃).symm = { to_fun := g, inv_fun := f, ..(mul_equiv.mk f g h₁ h₂ h₃).symm} := rfl @[simp, to_additive] theorem refl_symm : (refl M).symm = refl M := rfl /-- Transitivity of multiplication-preserving isomorphisms -/ @[trans, to_additive "Transitivity of addition-preserving isomorphisms"] def trans (h1 : M ≃* N) (h2 : N ≃* P) : (M ≃* P) := { map_mul' := λ x y, show h2 (h1 (x * y)) = h2 (h1 x) * h2 (h1 y), by rw [h1.map_mul, h2.map_mul], ..h1.to_equiv.trans h2.to_equiv } /-- `e.symm` is a right inverse of `e`, written as `e (e.symm y) = y`. -/ @[simp, to_additive "`e.symm` is a right inverse of `e`, written as `e (e.symm y) = y`."] lemma apply_symm_apply (e : M ≃* N) (y : N) : e (e.symm y) = y := e.to_equiv.apply_symm_apply y /-- `e.symm` is a left inverse of `e`, written as `e.symm (e y) = y`. -/ @[simp, to_additive "`e.symm` is a left inverse of `e`, written as `e.symm (e y) = y`."] lemma symm_apply_apply (e : M ≃* N) (x : M) : e.symm (e x) = x := e.to_equiv.symm_apply_apply x @[simp, to_additive] theorem symm_comp_self (e : M ≃* N) : e.symm ∘ e = id := funext e.symm_apply_apply @[simp, to_additive] theorem self_comp_symm (e : M ≃* N) : e ∘ e.symm = id := funext e.apply_symm_apply @[simp, to_additive] theorem coe_refl : ⇑(refl M) = id := rfl @[simp, to_additive] theorem refl_apply (m : M) : refl M m = m := rfl @[simp, to_additive] theorem coe_trans (e₁ : M ≃* N) (e₂ : N ≃* P) : ⇑(e₁.trans e₂) = e₂ ∘ e₁ := rfl @[simp, to_additive] theorem trans_apply (e₁ : M ≃* N) (e₂ : N ≃* P) (m : M) : e₁.trans e₂ m = e₂ (e₁ m) := rfl @[simp, to_additive] theorem symm_trans_apply (e₁ : M ≃* N) (e₂ : N ≃* P) (p : P) : (e₁.trans e₂).symm p = e₁.symm (e₂.symm p) := rfl @[simp, to_additive] theorem apply_eq_iff_eq (e : M ≃* N) {x y : M} : e x = e y ↔ x = y := e.injective.eq_iff @[to_additive] lemma apply_eq_iff_symm_apply (e : M ≃* N) {x : M} {y : N} : e x = y ↔ x = e.symm y := e.to_equiv.apply_eq_iff_eq_symm_apply @[to_additive] lemma symm_apply_eq (e : M ≃* N) {x y} : e.symm x = y ↔ x = e y := e.to_equiv.symm_apply_eq @[to_additive] lemma eq_symm_apply (e : M ≃* N) {x y} : y = e.symm x ↔ e y = x := e.to_equiv.eq_symm_apply @[to_additive] lemma eq_comp_symm {α : Type*} (e : M ≃* N) (f : N → α) (g : M → α) : f = g ∘ e.symm ↔ f ∘ e = g := e.to_equiv.eq_comp_symm f g @[to_additive] lemma comp_symm_eq {α : Type*} (e : M ≃* N) (f : N → α) (g : M → α) : g ∘ e.symm = f ↔ g = f ∘ e := e.to_equiv.comp_symm_eq f g @[to_additive] lemma eq_symm_comp {α : Type*} (e : M ≃* N) (f : α → M) (g : α → N) : f = e.symm ∘ g ↔ e ∘ f = g := e.to_equiv.eq_symm_comp f g @[to_additive] lemma symm_comp_eq {α : Type*} (e : M ≃* N) (f : α → M) (g : α → N) : e.symm ∘ g = f ↔ g = e ∘ f := e.to_equiv.symm_comp_eq f g @[simp, to_additive] theorem symm_trans_self (e : M ≃* N) : e.symm.trans e = refl N := fun_like.ext _ _ e.apply_symm_apply @[simp, to_additive] theorem self_trans_symm (e : M ≃* N) : e.trans e.symm = refl M := fun_like.ext _ _ e.symm_apply_apply @[to_additive, simp] lemma coe_monoid_hom_refl {M} [mul_one_class M] : (refl M : M →* M) = monoid_hom.id M := rfl @[to_additive, simp] lemma coe_monoid_hom_trans {M N P} [mul_one_class M] [mul_one_class N] [mul_one_class P] (e₁ : M ≃* N) (e₂ : N ≃* P) : (e₁.trans e₂ : M →* P) = (e₂ : N →* P).comp ↑e₁ := rfl /-- Two multiplicative isomorphisms agree if they are defined by the same underlying function. -/ @[ext, to_additive "Two additive isomorphisms agree if they are defined by the same underlying function."] lemma ext {f g : mul_equiv M N} (h : ∀ x, f x = g x) : f = g := fun_like.ext f g h @[to_additive] lemma ext_iff {f g : mul_equiv M N} : f = g ↔ ∀ x, f x = g x := fun_like.ext_iff @[simp, to_additive] lemma mk_coe (e : M ≃* N) (e' h₁ h₂ h₃) : (⟨e, e', h₁, h₂, h₃⟩ : M ≃* N) = e := ext $ λ _, rfl @[simp, to_additive] lemma mk_coe' (e : M ≃* N) (f h₁ h₂ h₃) : (mul_equiv.mk f ⇑e h₁ h₂ h₃ : N ≃* M) = e.symm := symm_bijective.injective $ ext $ λ x, rfl @[to_additive] protected lemma congr_arg {f : mul_equiv M N} {x x' : M} : x = x' → f x = f x' := fun_like.congr_arg f @[to_additive] protected lemma congr_fun {f g : mul_equiv M N} (h : f = g) (x : M) : f x = g x := fun_like.congr_fun h x /-- The `mul_equiv` between two monoids with a unique element. -/ @[to_additive "The `add_equiv` between two add_monoids with a unique element."] def mul_equiv_of_unique {M N} [unique M] [unique N] [has_mul M] [has_mul N] : M ≃* N := { map_mul' := λ _ _, subsingleton.elim _ _, ..equiv.equiv_of_unique M N } /-- There is a unique monoid homomorphism between two monoids with a unique element. -/ @[to_additive "There is a unique additive monoid homomorphism between two additive monoids with a unique element."] instance {M N} [unique M] [unique N] [has_mul M] [has_mul N] : unique (M ≃* N) := { default := mul_equiv_of_unique , uniq := λ _, ext $ λ x, subsingleton.elim _ _} /-! ## Monoids -/ /-- A multiplicative isomorphism of monoids sends `1` to `1` (and is hence a monoid isomorphism). -/ @[to_additive "An additive isomorphism of additive monoids sends `0` to `0` (and is hence an additive monoid isomorphism)."] protected lemma map_one {M N} [mul_one_class M] [mul_one_class N] (h : M ≃* N) : h 1 = 1 := map_one h @[to_additive] protected lemma map_eq_one_iff {M N} [mul_one_class M] [mul_one_class N] (h : M ≃* N) {x : M} : h x = 1 ↔ x = 1 := mul_equiv_class.map_eq_one_iff h @[to_additive] lemma map_ne_one_iff {M N} [mul_one_class M] [mul_one_class N] (h : M ≃* N) {x : M} : h x ≠ 1 ↔ x ≠ 1 := mul_equiv_class.map_ne_one_iff h /-- A bijective `semigroup` homomorphism is an isomorphism -/ @[to_additive "A bijective `add_semigroup` homomorphism is an isomorphism", simps apply] noncomputable def of_bijective {M N F} [has_mul M] [has_mul N] [mul_hom_class F M N] (f : F) (hf : function.bijective f) : M ≃* N := { map_mul' := map_mul f, ..equiv.of_bijective f hf } @[simp] lemma of_bijective_apply_symm_apply {M N} [mul_one_class M] [mul_one_class N] {n : N} (f : M →* N) (hf : function.bijective f) : f ((equiv.of_bijective f hf).symm n) = n := (mul_equiv.of_bijective f hf).apply_symm_apply n /-- Extract the forward direction of a multiplicative equivalence as a multiplication-preserving function. -/ @[to_additive "Extract the forward direction of an additive equivalence as an addition-preserving function."] def to_monoid_hom {M N} [mul_one_class M] [mul_one_class N] (h : M ≃* N) : (M →* N) := { map_one' := h.map_one, .. h } @[simp, to_additive] lemma coe_to_monoid_hom {M N} [mul_one_class M] [mul_one_class N] (e : M ≃* N) : ⇑e.to_monoid_hom = e := rfl @[to_additive] lemma to_monoid_hom_injective {M N} [mul_one_class M] [mul_one_class N] : function.injective (to_monoid_hom : (M ≃* N) → M →* N) := λ f g h, mul_equiv.ext (monoid_hom.ext_iff.1 h) /-- A multiplicative analogue of `equiv.arrow_congr`, where the equivalence between the targets is multiplicative. -/ @[to_additive "An additive analogue of `equiv.arrow_congr`, where the equivalence between the targets is additive.", simps apply] def arrow_congr {M N P Q : Type*} [has_mul P] [has_mul Q] (f : M ≃ N) (g : P ≃* Q) : (M → P) ≃* (N → Q) := { to_fun := λ h n, g (h (f.symm n)), inv_fun := λ k m, g.symm (k (f m)), left_inv := λ h, by { ext, simp, }, right_inv := λ k, by { ext, simp, }, map_mul' := λ h k, by { ext, simp, }, } /-- A multiplicative analogue of `equiv.arrow_congr`, for multiplicative maps from a monoid to a commutative monoid. -/ @[to_additive "An additive analogue of `equiv.arrow_congr`, for additive maps from an additive monoid to a commutative additive monoid.", simps apply] def monoid_hom_congr {M N P Q} [mul_one_class M] [mul_one_class N] [comm_monoid P] [comm_monoid Q] (f : M ≃* N) (g : P ≃* Q) : (M →* P) ≃* (N →* Q) := { to_fun := λ h, g.to_monoid_hom.comp (h.comp f.symm.to_monoid_hom), inv_fun := λ k, g.symm.to_monoid_hom.comp (k.comp f.to_monoid_hom), left_inv := λ h, by { ext, simp, }, right_inv := λ k, by { ext, simp, }, map_mul' := λ h k, by { ext, simp, }, } /-- A family of multiplicative equivalences `Π j, (Ms j ≃* Ns j)` generates a multiplicative equivalence between `Π j, Ms j` and `Π j, Ns j`. This is the `mul_equiv` version of `equiv.Pi_congr_right`, and the dependent version of `mul_equiv.arrow_congr`. -/ @[to_additive add_equiv.Pi_congr_right "A family of additive equivalences `Π j, (Ms j ≃+ Ns j)` generates an additive equivalence between `Π j, Ms j` and `Π j, Ns j`. This is the `add_equiv` version of `equiv.Pi_congr_right`, and the dependent version of `add_equiv.arrow_congr`.", simps apply] def Pi_congr_right {η : Type*} {Ms Ns : η → Type*} [Π j, has_mul (Ms j)] [Π j, has_mul (Ns j)] (es : ∀ j, Ms j ≃* Ns j) : (Π j, Ms j) ≃* (Π j, Ns j) := { to_fun := λ x j, es j (x j), inv_fun := λ x j, (es j).symm (x j), map_mul' := λ x y, funext $ λ j, (es j).map_mul (x j) (y j), .. equiv.Pi_congr_right (λ j, (es j).to_equiv) } @[simp, to_additive] lemma Pi_congr_right_refl {η : Type*} {Ms : η → Type*} [Π j, has_mul (Ms j)] : Pi_congr_right (λ j, mul_equiv.refl (Ms j)) = mul_equiv.refl _ := rfl @[simp, to_additive] lemma Pi_congr_right_symm {η : Type*} {Ms Ns : η → Type*} [Π j, has_mul (Ms j)] [Π j, has_mul (Ns j)] (es : ∀ j, Ms j ≃* Ns j) : (Pi_congr_right es).symm = (Pi_congr_right $ λ i, (es i).symm) := rfl @[simp, to_additive] lemma Pi_congr_right_trans {η : Type*} {Ms Ns Ps : η → Type*} [Π j, has_mul (Ms j)] [Π j, has_mul (Ns j)] [Π j, has_mul (Ps j)] (es : ∀ j, Ms j ≃* Ns j) (fs : ∀ j, Ns j ≃* Ps j) : (Pi_congr_right es).trans (Pi_congr_right fs) = (Pi_congr_right $ λ i, (es i).trans (fs i)) := rfl /-- A family indexed by a nonempty subsingleton type is equivalent to the element at the single index. -/ @[to_additive add_equiv.Pi_subsingleton "A family indexed by a nonempty subsingleton type is equivalent to the element at the single index.", simps] def Pi_subsingleton {ι : Type*} (M : ι → Type*) [Π j, has_mul (M j)] [subsingleton ι] (i : ι) : (Π j, M j) ≃* M i := { map_mul' := λ f1 f2, pi.mul_apply _ _ _, ..equiv.Pi_subsingleton M i } /-! # Groups -/ /-- A multiplicative equivalence of groups preserves inversion. -/ @[to_additive "An additive equivalence of additive groups preserves negation."] protected lemma map_inv [group G] [division_monoid H] (h : G ≃* H) (x : G) : h x⁻¹ = (h x)⁻¹ := map_inv h x /-- A multiplicative equivalence of groups preserves division. -/ @[to_additive "An additive equivalence of additive groups preserves subtractions."] protected lemma map_div [group G] [division_monoid H] (h : G ≃* H) (x y : G) : h (x / y) = h x / h y := map_div h x y end mul_equiv /-- Given a pair of multiplicative homomorphisms `f`, `g` such that `g.comp f = id` and `f.comp g = id`, returns an multiplicative equivalence with `to_fun = f` and `inv_fun = g`. This constructor is useful if the underlying type(s) have specialized `ext` lemmas for multiplicative homomorphisms. -/ @[to_additive /-"Given a pair of additive homomorphisms `f`, `g` such that `g.comp f = id` and `f.comp g = id`, returns an additive equivalence with `to_fun = f` and `inv_fun = g`. This constructor is useful if the underlying type(s) have specialized `ext` lemmas for additive homomorphisms."-/, simps {fully_applied := ff}] def mul_hom.to_mul_equiv [has_mul M] [has_mul N] (f : M →ₙ* N) (g : N →ₙ* M) (h₁ : g.comp f = mul_hom.id _) (h₂ : f.comp g = mul_hom.id _) : M ≃* N := { to_fun := f, inv_fun := g, left_inv := mul_hom.congr_fun h₁, right_inv := mul_hom.congr_fun h₂, map_mul' := f.map_mul } /-- Given a pair of monoid homomorphisms `f`, `g` such that `g.comp f = id` and `f.comp g = id`, returns an multiplicative equivalence with `to_fun = f` and `inv_fun = g`. This constructor is useful if the underlying type(s) have specialized `ext` lemmas for monoid homomorphisms. -/ @[to_additive /-"Given a pair of additive monoid homomorphisms `f`, `g` such that `g.comp f = id` and `f.comp g = id`, returns an additive equivalence with `to_fun = f` and `inv_fun = g`. This constructor is useful if the underlying type(s) have specialized `ext` lemmas for additive monoid homomorphisms."-/, simps {fully_applied := ff}] def monoid_hom.to_mul_equiv [mul_one_class M] [mul_one_class N] (f : M →* N) (g : N →* M) (h₁ : g.comp f = monoid_hom.id _) (h₂ : f.comp g = monoid_hom.id _) : M ≃* N := { to_fun := f, inv_fun := g, left_inv := monoid_hom.congr_fun h₁, right_inv := monoid_hom.congr_fun h₂, map_mul' := f.map_mul } namespace equiv section has_involutive_neg variables (G) [has_involutive_inv G] /-- Inversion on a `group` or `group_with_zero` is a permutation of the underlying type. -/ @[to_additive "Negation on an `add_group` is a permutation of the underlying type.", simps apply {fully_applied := ff}] protected def inv : perm G := inv_involutive.to_perm _ variable {G} @[simp, to_additive] lemma inv_symm : (equiv.inv G).symm = equiv.inv G := rfl end has_involutive_neg end equiv
b262dfc0f35770a44d2cf76b33b7731e1abfc891
ce6917c5bacabee346655160b74a307b4a5ab620
/src/ch3/ex0207.lean
906d593b0220bb5a871b8e3f755506608d85e4f3
[]
no_license
Ailrun/Theorem_Proving_in_Lean
ae6a23f3c54d62d401314d6a771e8ff8b4132db2
2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68
refs/heads/master
1,609,838,270,467
1,586,846,743,000
1,586,846,743,000
240,967,761
1
0
null
null
null
null
UTF-8
Lean
false
false
106
lean
constants p q : Prop theorem t1 (hp : p) (hq : q) : p := hp axiom hp : p theorem t2 : q → p := t1 hp
fb3d363d9821f6035c4383ad0cc12e5308582911
b7f22e51856f4989b970961f794f1c435f9b8f78
/library/data/int/order.lean
81052110d7a44a586584b2ec5383e9205c01a6a1
[ "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
17,656
lean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Jeremy Avigad The order relation on the integers. We show that int is an instance of linear_comm_ordered_ring and transfer the results. -/ import .basic algebra.ordered_ring open nat open decidable open int eq.ops namespace int private definition nonneg (a : ℤ) : Prop := int.cases_on a (take n, true) (take n, false) protected definition le (a b : ℤ) : Prop := nonneg (b - a) definition int_has_le [instance] [priority int.prio]: has_le int := has_le.mk int.le protected definition lt (a b : ℤ) : Prop := (a + 1) ≤ b definition int_has_lt [instance] [priority int.prio]: has_lt int := has_lt.mk int.lt local attribute nonneg [reducible] private definition decidable_nonneg [instance] (a : ℤ) : decidable (nonneg a) := int.cases_on a _ _ definition decidable_le [instance] (a b : ℤ) : decidable (a ≤ b) := decidable_nonneg _ definition decidable_lt [instance] (a b : ℤ) : decidable (a < b) := decidable_nonneg _ private theorem nonneg.elim {a : ℤ} : nonneg a → ∃n : ℕ, a = n := int.cases_on a (take n H, exists.intro n rfl) (take n', false.elim) private theorem nonneg_or_nonneg_neg (a : ℤ) : nonneg a ∨ nonneg (-a) := int.cases_on a (take n, or.inl trivial) (take n, or.inr trivial) theorem le.intro {a b : ℤ} {n : ℕ} (H : a + n = b) : a ≤ b := have n = b - a, from eq_add_neg_of_add_eq (begin rewrite [add.comm, H] end), -- !add.comm ▸ H), show nonneg (b - a), from this ▸ trivial theorem le.elim {a b : ℤ} (H : a ≤ b) : ∃n : ℕ, a + n = b := obtain (n : ℕ) (H1 : b - a = n), from nonneg.elim H, exists.intro n (!add.comm ▸ iff.mpr !add_eq_iff_eq_add_neg (H1⁻¹)) protected theorem le_total (a b : ℤ) : a ≤ b ∨ b ≤ a := or.imp_right (assume H : nonneg (-(b - a)), have -(b - a) = a - b, from !neg_sub, show nonneg (a - b), from this ▸ H) (nonneg_or_nonneg_neg (b - a)) theorem of_nat_le_of_nat_of_le {m n : ℕ} (H : #nat m ≤ n) : of_nat m ≤ of_nat n := obtain (k : ℕ) (Hk : m + k = n), from nat.le.elim H, le.intro (Hk ▸ (of_nat_add m k)⁻¹) theorem le_of_of_nat_le_of_nat {m n : ℕ} (H : of_nat m ≤ of_nat n) : (#nat m ≤ n) := obtain (k : ℕ) (Hk : of_nat m + of_nat k = of_nat n), from le.elim H, have m + k = n, from of_nat.inj (of_nat_add m k ⬝ Hk), nat.le.intro this theorem of_nat_le_of_nat_iff (m n : ℕ) : of_nat m ≤ of_nat n ↔ m ≤ n := iff.intro le_of_of_nat_le_of_nat of_nat_le_of_nat_of_le theorem lt_add_succ (a : ℤ) (n : ℕ) : a < a + succ n := le.intro (show a + 1 + n = a + succ n, from calc a + 1 + n = a + (1 + n) : add.assoc ... = a + (n + 1) : by rewrite (int.add_comm 1 n) ... = a + succ n : rfl) theorem lt.intro {a b : ℤ} {n : ℕ} (H : a + succ n = b) : a < b := H ▸ lt_add_succ a n theorem lt.elim {a b : ℤ} (H : a < b) : ∃n : ℕ, a + succ n = b := obtain (n : ℕ) (Hn : a + 1 + n = b), from le.elim H, have a + succ n = b, from calc a + succ n = a + 1 + n : by rewrite [add.assoc, int.add_comm 1 n] ... = b : Hn, exists.intro n this theorem of_nat_lt_of_nat_iff (n m : ℕ) : of_nat n < of_nat m ↔ n < m := calc of_nat n < of_nat m ↔ of_nat n + 1 ≤ of_nat m : iff.refl ... ↔ of_nat (nat.succ n) ≤ of_nat m : of_nat_succ n ▸ !iff.refl ... ↔ nat.succ n ≤ m : of_nat_le_of_nat_iff ... ↔ n < m : iff.symm (lt_iff_succ_le _ _) theorem lt_of_of_nat_lt_of_nat {m n : ℕ} (H : of_nat m < of_nat n) : #nat m < n := iff.mp !of_nat_lt_of_nat_iff H theorem of_nat_lt_of_nat_of_lt {m n : ℕ} (H : #nat m < n) : of_nat m < of_nat n := iff.mpr !of_nat_lt_of_nat_iff H /- show that the integers form an ordered additive group -/ protected theorem le_refl (a : ℤ) : a ≤ a := le.intro (add_zero a) protected theorem le_trans {a b c : ℤ} (H1 : a ≤ b) (H2 : b ≤ c) : a ≤ c := obtain (n : ℕ) (Hn : a + n = b), from le.elim H1, obtain (m : ℕ) (Hm : b + m = c), from le.elim H2, have a + of_nat (n + m) = c, from calc a + of_nat (n + m) = a + (of_nat n + m) : {of_nat_add n m} ... = a + n + m : (add.assoc a n m)⁻¹ ... = b + m : {Hn} ... = c : Hm, le.intro this protected theorem le_antisymm : ∀ {a b : ℤ}, a ≤ b → b ≤ a → a = b := take a b : ℤ, assume (H₁ : a ≤ b) (H₂ : b ≤ a), obtain (n : ℕ) (Hn : a + n = b), from le.elim H₁, obtain (m : ℕ) (Hm : b + m = a), from le.elim H₂, have a + of_nat (n + m) = a + 0, from calc a + of_nat (n + m) = a + (of_nat n + m) : by rewrite of_nat_add ... = a + n + m : by rewrite add.assoc ... = b + m : by rewrite Hn ... = a : by rewrite Hm ... = a + 0 : by rewrite add_zero, have of_nat (n + m) = of_nat 0, from add.left_cancel this, have n + m = 0, from of_nat.inj this, have n = 0, from nat.eq_zero_of_add_eq_zero_right this, show a = b, from calc a = a + 0 : add_zero ... = a + n : by rewrite this ... = b : Hn protected theorem lt_irrefl (a : ℤ) : ¬ a < a := (suppose a < a, obtain (n : ℕ) (Hn : a + succ n = a), from lt.elim this, have a + succ n = a + 0, from Hn ⬝ !add_zero⁻¹, !succ_ne_zero (of_nat.inj (add.left_cancel this))) protected theorem ne_of_lt {a b : ℤ} (H : a < b) : a ≠ b := (suppose a = b, absurd (this ▸ H) (int.lt_irrefl b)) theorem le_of_lt {a b : ℤ} (H : a < b) : a ≤ b := obtain (n : ℕ) (Hn : a + succ n = b), from lt.elim H, le.intro Hn protected theorem lt_iff_le_and_ne (a b : ℤ) : a < b ↔ (a ≤ b ∧ a ≠ b) := iff.intro (assume H, and.intro (le_of_lt H) (int.ne_of_lt H)) (assume H, have a ≤ b, from and.elim_left H, have a ≠ b, from and.elim_right H, obtain (n : ℕ) (Hn : a + n = b), from le.elim `a ≤ b`, have n ≠ 0, from (assume H' : n = 0, `a ≠ b` (!add_zero ▸ H' ▸ Hn)), obtain (k : ℕ) (Hk : n = nat.succ k), from nat.exists_eq_succ_of_ne_zero this, lt.intro (Hk ▸ Hn)) protected theorem le_iff_lt_or_eq (a b : ℤ) : a ≤ b ↔ (a < b ∨ a = b) := iff.intro (assume H, by_cases (suppose a = b, or.inr this) (suppose a ≠ b, obtain (n : ℕ) (Hn : a + n = b), from le.elim H, have n ≠ 0, from (assume H' : n = 0, `a ≠ b` (!add_zero ▸ H' ▸ Hn)), obtain (k : ℕ) (Hk : n = nat.succ k), from nat.exists_eq_succ_of_ne_zero this, or.inl (lt.intro (Hk ▸ Hn)))) (assume H, or.elim H (assume H1, le_of_lt H1) (assume H1, H1 ▸ !int.le_refl)) theorem lt_succ (a : ℤ) : a < a + 1 := int.le_refl (a + 1) protected theorem add_le_add_left {a b : ℤ} (H : a ≤ b) (c : ℤ) : c + a ≤ c + b := obtain (n : ℕ) (Hn : a + n = b), from le.elim H, have H2 : c + a + n = c + b, from calc c + a + n = c + (a + n) : add.assoc c a n ... = c + b : {Hn}, le.intro H2 protected theorem add_lt_add_left {a b : ℤ} (H : a < b) (c : ℤ) : c + a < c + b := let H' := le_of_lt H in (iff.mpr (int.lt_iff_le_and_ne _ _)) (and.intro (int.add_le_add_left H' _) (take Heq, let Heq' := add_left_cancel Heq in !int.lt_irrefl (Heq' ▸ H))) protected theorem mul_nonneg {a b : ℤ} (Ha : 0 ≤ a) (Hb : 0 ≤ b) : 0 ≤ a * b := obtain (n : ℕ) (Hn : 0 + n = a), from le.elim Ha, obtain (m : ℕ) (Hm : 0 + m = b), from le.elim Hb, le.intro (eq.symm (calc a * b = (0 + n) * b : by rewrite Hn ... = n * b : by rewrite zero_add ... = n * (0 + m) : by rewrite Hm ... = n * m : by rewrite zero_add ... = 0 + n * m : by rewrite zero_add)) protected theorem mul_pos {a b : ℤ} (Ha : 0 < a) (Hb : 0 < b) : 0 < a * b := obtain (n : ℕ) (Hn : 0 + nat.succ n = a), from lt.elim Ha, obtain (m : ℕ) (Hm : 0 + nat.succ m = b), from lt.elim Hb, lt.intro (eq.symm (calc a * b = (0 + nat.succ n) * b : by rewrite Hn ... = nat.succ n * b : by rewrite zero_add ... = nat.succ n * (0 + nat.succ m) : by rewrite Hm ... = nat.succ n * nat.succ m : by rewrite zero_add ... = of_nat (nat.succ n * nat.succ m) : by rewrite of_nat_mul ... = of_nat (nat.succ n * m + nat.succ n) : by rewrite nat.mul_succ ... = of_nat (nat.succ (nat.succ n * m + n)) : by rewrite nat.add_succ ... = 0 + nat.succ (nat.succ n * m + n) : by rewrite zero_add)) protected theorem zero_lt_one : (0 : ℤ) < 1 := trivial protected theorem not_le_of_gt {a b : ℤ} (H : a < b) : ¬ b ≤ a := assume Hba, let Heq := int.le_antisymm (le_of_lt H) Hba in !int.lt_irrefl (Heq ▸ H) protected theorem lt_of_lt_of_le {a b c : ℤ} (Hab : a < b) (Hbc : b ≤ c) : a < c := let Hab' := le_of_lt Hab in let Hac := int.le_trans Hab' Hbc in (iff.mpr !int.lt_iff_le_and_ne) (and.intro Hac (assume Heq, int.not_le_of_gt (Heq ▸ Hab) Hbc)) protected theorem lt_of_le_of_lt {a b c : ℤ} (Hab : a ≤ b) (Hbc : b < c) : a < c := let Hbc' := le_of_lt Hbc in let Hac := int.le_trans Hab Hbc' in (iff.mpr !int.lt_iff_le_and_ne) (and.intro Hac (assume Heq, int.not_le_of_gt (Heq⁻¹ ▸ Hbc) Hab)) protected definition linear_ordered_comm_ring [trans_instance] : linear_ordered_comm_ring int := ⦃linear_ordered_comm_ring, int.integral_domain, le := int.le, le_refl := int.le_refl, le_trans := @int.le_trans, le_antisymm := @int.le_antisymm, lt := int.lt, le_of_lt := @int.le_of_lt, lt_irrefl := int.lt_irrefl, lt_of_lt_of_le := @int.lt_of_lt_of_le, lt_of_le_of_lt := @int.lt_of_le_of_lt, add_le_add_left := @int.add_le_add_left, mul_nonneg := @int.mul_nonneg, mul_pos := @int.mul_pos, le_iff_lt_or_eq := int.le_iff_lt_or_eq, le_total := int.le_total, zero_ne_one := int.zero_ne_one, zero_lt_one := int.zero_lt_one, add_lt_add_left := @int.add_lt_add_left⦄ protected definition decidable_linear_ordered_comm_ring [instance] : decidable_linear_ordered_comm_ring int := ⦃decidable_linear_ordered_comm_ring, int.linear_ordered_comm_ring, decidable_lt := decidable_lt⦄ /- more facts specific to int -/ theorem of_nat_nonneg (n : ℕ) : 0 ≤ of_nat n := trivial theorem of_nat_pos {n : ℕ} (Hpos : #nat n > 0) : of_nat n > 0 := of_nat_lt_of_nat_of_lt Hpos theorem of_nat_succ_pos (n : nat) : of_nat (nat.succ n) > 0 := of_nat_pos !nat.succ_pos theorem exists_eq_of_nat {a : ℤ} (H : 0 ≤ a) : ∃n : ℕ, a = of_nat n := obtain (n : ℕ) (H1 : 0 + of_nat n = a), from le.elim H, exists.intro n (!zero_add ▸ (H1⁻¹)) theorem exists_eq_neg_of_nat {a : ℤ} (H : a ≤ 0) : ∃n : ℕ, a = -(of_nat n) := have -a ≥ 0, from iff.mpr !neg_nonneg_iff_nonpos H, obtain (n : ℕ) (Hn : -a = of_nat n), from exists_eq_of_nat this, exists.intro n (eq_neg_of_eq_neg (Hn⁻¹)) theorem of_nat_nat_abs_of_nonneg {a : ℤ} (H : a ≥ 0) : of_nat (nat_abs a) = a := obtain (n : ℕ) (Hn : a = of_nat n), from exists_eq_of_nat H, Hn⁻¹ ▸ congr_arg of_nat (nat_abs_of_nat n) theorem of_nat_nat_abs_of_nonpos {a : ℤ} (H : a ≤ 0) : of_nat (nat_abs a) = -a := have -a ≥ 0, from iff.mpr !neg_nonneg_iff_nonpos H, calc of_nat (nat_abs a) = of_nat (nat_abs (-a)) : nat_abs_neg ... = -a : of_nat_nat_abs_of_nonneg this theorem of_nat_nat_abs (b : ℤ) : nat_abs b = abs b := or.elim (le.total 0 b) (assume H : b ≥ 0, of_nat_nat_abs_of_nonneg H ⬝ (abs_of_nonneg H)⁻¹) (assume H : b ≤ 0, of_nat_nat_abs_of_nonpos H ⬝ (abs_of_nonpos H)⁻¹) theorem nat_abs_abs (a : ℤ) : nat_abs (abs a) = nat_abs a := abs.by_cases rfl !nat_abs_neg theorem lt_of_add_one_le {a b : ℤ} (H : a + 1 ≤ b) : a < b := obtain (n : nat) (H1 : a + 1 + n = b), from le.elim H, have a + succ n = b, by rewrite [-H1, add.assoc, add.comm 1], lt.intro this theorem add_one_le_of_lt {a b : ℤ} (H : a < b) : a + 1 ≤ b := obtain (n : nat) (H1 : a + succ n = b), from lt.elim H, have a + 1 + n = b, by rewrite [-H1, add.assoc, add.comm 1], le.intro this theorem lt_add_one_of_le {a b : ℤ} (H : a ≤ b) : a < b + 1 := lt_add_of_le_of_pos H trivial theorem le_of_lt_add_one {a b : ℤ} (H : a < b + 1) : a ≤ b := have H1 : a + 1 ≤ b + 1, from add_one_le_of_lt H, le_of_add_le_add_right H1 theorem sub_one_le_of_lt {a b : ℤ} (H : a ≤ b) : a - 1 < b := lt_of_add_one_le (begin rewrite sub_add_cancel, exact H end) theorem lt_of_sub_one_le {a b : ℤ} (H : a - 1 < b) : a ≤ b := !sub_add_cancel ▸ add_one_le_of_lt H theorem le_sub_one_of_lt {a b : ℤ} (H : a < b) : a ≤ b - 1 := le_of_lt_add_one begin rewrite sub_add_cancel, exact H end theorem lt_of_le_sub_one {a b : ℤ} (H : a ≤ b - 1) : a < b := !sub_add_cancel ▸ (lt_add_one_of_le H) theorem sign_of_succ (n : nat) : sign (nat.succ n) = 1 := sign_of_pos (of_nat_pos !nat.succ_pos) theorem exists_eq_neg_succ_of_nat {a : ℤ} : a < 0 → ∃m : ℕ, a = -[1+m] := int.cases_on a (take (m : nat) H, absurd (of_nat_nonneg m : 0 ≤ m) (not_le_of_gt H)) (take (m : nat) H, exists.intro m rfl) theorem eq_one_of_mul_eq_one_right {a b : ℤ} (H : a ≥ 0) (H' : a * b = 1) : a = 1 := have a * b > 0, by rewrite H'; apply trivial, have b > 0, from pos_of_mul_pos_left this H, have a > 0, from pos_of_mul_pos_right `a * b > 0` (le_of_lt `b > 0`), or.elim (le_or_gt a 1) (suppose a ≤ 1, show a = 1, from le.antisymm this (add_one_le_of_lt `a > 0`)) (suppose a > 1, have a * b ≥ 2 * 1, from mul_le_mul (add_one_le_of_lt `a > 1`) (add_one_le_of_lt `b > 0`) trivial H, have false, by rewrite [H' at this]; exact this, false.elim this) theorem eq_one_of_mul_eq_one_left {a b : ℤ} (H : b ≥ 0) (H' : a * b = 1) : b = 1 := eq_one_of_mul_eq_one_right H (!mul.comm ▸ H') theorem eq_one_of_mul_eq_self_left {a b : ℤ} (Hpos : a ≠ 0) (H : b * a = a) : b = 1 := eq_of_mul_eq_mul_right Hpos (H ⬝ (one_mul a)⁻¹) theorem eq_one_of_mul_eq_self_right {a b : ℤ} (Hpos : b ≠ 0) (H : b * a = b) : a = 1 := eq_one_of_mul_eq_self_left Hpos (!mul.comm ▸ H) theorem eq_one_of_dvd_one {a : ℤ} (H : a ≥ 0) (H' : a ∣ 1) : a = 1 := dvd.elim H' (take b, suppose 1 = a * b, eq_one_of_mul_eq_one_right H this⁻¹) theorem exists_least_of_bdd {P : ℤ → Prop} [HP : decidable_pred P] (Hbdd : ∃ b : ℤ, ∀ z : ℤ, z ≤ b → ¬ P z) (Hinh : ∃ z : ℤ, P z) : ∃ lb : ℤ, P lb ∧ (∀ z : ℤ, z < lb → ¬ P z) := begin cases Hbdd with [b, Hb], cases Hinh with [elt, Helt], existsi b + of_nat (least (λ n, P (b + of_nat n)) (nat.succ (nat_abs (elt - b)))), have Heltb : elt > b, begin apply lt_of_not_ge, intro Hge, apply (Hb _ Hge) Helt end, have H' : P (b + of_nat (nat_abs (elt - b))), begin rewrite [of_nat_nat_abs_of_nonneg (int.le_of_lt (iff.mpr !sub_pos_iff_lt Heltb)), add.comm, sub_add_cancel], apply Helt end, apply and.intro, apply least_of_lt _ !lt_succ_self H', intros z Hz, cases em (z ≤ b) with [Hzb, Hzb], apply Hb _ Hzb, let Hzb' := lt_of_not_ge Hzb, let Hpos := iff.mpr !sub_pos_iff_lt Hzb', have Hzbk : z = b + of_nat (nat_abs (z - b)), by rewrite [of_nat_nat_abs_of_nonneg (int.le_of_lt Hpos), int.add_comm, sub_add_cancel], have Hk : nat_abs (z - b) < least (λ n, P (b + of_nat n)) (nat.succ (nat_abs (elt - b))), begin note Hz' := iff.mp !lt_add_iff_sub_lt_left Hz, rewrite [-of_nat_nat_abs_of_nonneg (int.le_of_lt Hpos) at Hz'], apply lt_of_of_nat_lt_of_nat Hz' end, let Hk' := not_le_of_gt Hk, rewrite Hzbk, apply λ p, mt (ge_least_of_lt _ p) Hk', apply nat.lt_trans Hk, apply least_lt _ !lt_succ_self H' end theorem exists_greatest_of_bdd {P : ℤ → Prop} [HP : decidable_pred P] (Hbdd : ∃ b : ℤ, ∀ z : ℤ, z ≥ b → ¬ P z) (Hinh : ∃ z : ℤ, P z) : ∃ ub : ℤ, P ub ∧ (∀ z : ℤ, z > ub → ¬ P z) := begin cases Hbdd with [b, Hb], cases Hinh with [elt, Helt], existsi b - of_nat (least (λ n, P (b - of_nat n)) (nat.succ (nat_abs (b - elt)))), have Heltb : elt < b, begin apply lt_of_not_ge, intro Hge, apply (Hb _ Hge) Helt end, have H' : P (b - of_nat (nat_abs (b - elt))), begin rewrite [of_nat_nat_abs_of_nonneg (int.le_of_lt (iff.mpr !sub_pos_iff_lt Heltb)), sub_sub_self], apply Helt end, apply and.intro, apply least_of_lt _ !lt_succ_self H', intros z Hz, cases em (z ≥ b) with [Hzb, Hzb], apply Hb _ Hzb, let Hzb' := lt_of_not_ge Hzb, let Hpos := iff.mpr !sub_pos_iff_lt Hzb', have Hzbk : z = b - of_nat (nat_abs (b - z)), by rewrite [of_nat_nat_abs_of_nonneg (int.le_of_lt Hpos), sub_sub_self], have Hk : nat_abs (b - z) < least (λ n, P (b - of_nat n)) (nat.succ (nat_abs (b - elt))), begin note Hz' := iff.mp !lt_add_iff_sub_lt_left (iff.mpr !lt_add_iff_sub_lt_right Hz), rewrite [-of_nat_nat_abs_of_nonneg (int.le_of_lt Hpos) at Hz'], apply lt_of_of_nat_lt_of_nat Hz' end, let Hk' := not_le_of_gt Hk, rewrite Hzbk, apply λ p, mt (ge_least_of_lt _ p) Hk', apply nat.lt_trans Hk, apply least_lt _ !lt_succ_self H' end end int
b5031423f792ec565874655ba642611962460512
36938939954e91f23dec66a02728db08a7acfcf9
/old-lean4/rbnode.lean
f40aaf1319e2a0ac7988e6fe06844f2b2dff6f50
[]
no_license
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
581
lean
namespace RBNode variable {α : Type _} variable {β : Type _} variable (lt : α → α → Bool) def ofList' : RBNode α (λ_, β) → List (α × β) → RBNode α (λ_, β) | l [] := l | l ((k,v) :: r) := ofList' (l.insert lt k v) r def ofList (l:List (α × β)) : RBNode α (λ_, β) := ofList' lt leaf l section variable {A:Type _} variable {B:A → Type _} variable {σ : Type _} variable (f : Π(k:A), B k → σ → σ) def foldRight : RBNode A B → σ → σ | RBNode.leaf s := s | (RBNode.node _ l k v r) s := foldRight l (f k v (foldRight r s)) end end RBNode
d36ab8be7ffa418c0fd2dc7a733d889c75a17b42
54c9ed381c63410c9b6af3b0a1722c41152f037f
/Binport.lean
80f4054f70aba68e10006b47a25729314301ea9a
[ "Apache-2.0" ]
permissive
dselsam/binport
0233f1aa961a77c4fc96f0dccc780d958c5efc6c
aef374df0e169e2c3f1dc911de240c076315805c
refs/heads/master
1,687,453,448,108
1,627,483,296,000
1,627,483,296,000
333,825,622
0
0
null
null
null
null
UTF-8
Lean
false
false
1,189
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Daniel Selsam -/ import Binport.Run import Binport.Path import Lean open Lean open Lean.Meta open Binport unsafe def main (args : List String) : IO Unit := do match args with | [proofs, lib] => let proofs : Bool ← match proofs.toNat? with -- TODO: why is the : Bool annotation ignored? | some k => if k > 0 then true else false | none => throw $ IO.userError s!"First argument <proof> must be 0 or 1" match lib with | "lean3" => Binport.run proofs $ Path34.mk MODULES[1] ⟨"all"⟩ | "mathlib" => Binport.run proofs $ Path34.mk MODULES[0] ⟨"all"⟩ | "nullstellensatz" => Binport.run proofs $ Path34.mk MODULES[0] ⟨"ring_theory/nullstellensatz"⟩ | "bounded" => Binport.run proofs $ Path34.mk MODULES[0] ⟨"topology/continuous_function/bounded_mini2"⟩ | "prime" => Binport.run proofs $ Path34.mk MODULES[0] ⟨"data/nat/prime"⟩ | _ => throw $ IO.userError "Second argument <lib> must be 'lean3' or 'mathlib'" | _ => throw $ IO.userError "Expected <proof> <lib>"
285c901ab3e79ecd63a2be902583c16e68d7b082
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/notation7.lean
9792c8c3c803b86f59101944f791ed13ba608876
[ "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
220
lean
import logic data.num open num constant f : num → num constant g : num → num → num notation A `:+1`:100000 := f A check g 0:+1:+1 (1:+1 + 2:+1):+1 set_option pp.notation false check g 0:+1:+1 (1:+1 + 2:+1):+1
2d067edc0cb88f4124c7b20fb7ca627c90e885c9
30b012bb72d640ec30c8fdd4c45fdfa67beb012c
/group_theory/coset.lean
39d82b3ce78aae9cea7f6acde95424948f4342ce
[ "Apache-2.0" ]
permissive
kckennylau/mathlib
21fb810b701b10d6606d9002a4004f7672262e83
47b3477e20ffb5a06588dd3abb01fe0fe3205646
refs/heads/master
1,634,976,409,281
1,542,042,832,000
1,542,319,733,000
109,560,458
0
0
Apache-2.0
1,542,369,208,000
1,509,867,494,000
Lean
UTF-8
Lean
false
false
10,462
lean
/- Copyright (c) 2018 Mitchell Rowett. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mitchell Rowett, Scott Morrison -/ import group_theory.subgroup data.equiv.basic data.quot open set function variable {α : Type*} @[to_additive left_add_coset] def left_coset [has_mul α] (a : α) (s : set α) : set α := (λ x, a * x) '' s attribute [to_additive left_add_coset.equations._eqn_1] left_coset.equations._eqn_1 @[to_additive right_add_coset] def right_coset [has_mul α] (s : set α) (a : α) : set α := (λ x, x * a) '' s attribute [to_additive right_add_coset.equations._eqn_1] right_coset.equations._eqn_1 local infix ` *l `:70 := left_coset local infix ` +l `:70 := left_add_coset local infix ` *r `:70 := right_coset local infix ` +r `:70 := right_add_coset section coset_mul variable [has_mul α] @[to_additive mem_left_add_coset] lemma mem_left_coset {s : set α} {x : α} (a : α) (hxS : x ∈ s) : a * x ∈ a *l s := mem_image_of_mem (λ b : α, a * b) hxS @[to_additive mem_right_add_coset] lemma mem_right_coset {s : set α} {x : α} (a : α) (hxS : x ∈ s) : x * a ∈ s *r a := mem_image_of_mem (λ b : α, b * a) hxS @[to_additive left_add_coset_equiv] def left_coset_equiv (s : set α) (a b : α) := a *l s = b *l s @[to_additive left_add_coset_equiv_rel] lemma left_coset_equiv_rel (s : set α) : equivalence (left_coset_equiv s) := mk_equivalence (left_coset_equiv s) (λ a, rfl) (λ a b, eq.symm) (λ a b c, eq.trans) end coset_mul section coset_semigroup variable [semigroup α] @[simp] lemma left_coset_assoc (s : set α) (a b : α) : a *l (b *l s) = (a * b) *l s := by simp [left_coset, right_coset, (image_comp _ _ _).symm, function.comp, mul_assoc] attribute [to_additive left_add_coset_assoc] left_coset_assoc @[simp] lemma right_coset_assoc (s : set α) (a b : α) : s *r a *r b = s *r (a * b) := by simp [left_coset, right_coset, (image_comp _ _ _).symm, function.comp, mul_assoc] attribute [to_additive right_add_coset_assoc] right_coset_assoc @[to_additive left_add_coset_right_add_coset] lemma left_coset_right_coset (s : set α) (a b : α) : a *l s *r b = a *l (s *r b) := by simp [left_coset, right_coset, (image_comp _ _ _).symm, function.comp, mul_assoc] end coset_semigroup section coset_monoid variables [monoid α] (s : set α) @[simp] lemma one_left_coset : 1 *l s = s := set.ext $ by simp [left_coset] attribute [to_additive zero_left_add_coset] one_left_coset @[simp] lemma right_coset_one : s *r 1 = s := set.ext $ by simp [right_coset] attribute [to_additive right_add_coset_zero] right_coset_one end coset_monoid section coset_submonoid open is_submonoid variables [monoid α] (s : set α) [is_submonoid s] @[to_additive mem_own_left_add_coset] lemma mem_own_left_coset (a : α) : a ∈ a *l s := suffices a * 1 ∈ a *l s, by simpa, mem_left_coset a (one_mem s) @[to_additive mem_own_right_add_coset] lemma mem_own_right_coset (a : α) : a ∈ s *r a := suffices 1 * a ∈ s *r a, by simpa, mem_right_coset a (one_mem s) @[to_additive mem_left_add_coset_left_add_coset] lemma mem_left_coset_left_coset {a : α} (ha : a *l s = s) : a ∈ s := by rw [←ha]; exact mem_own_left_coset s a @[to_additive mem_right_add_coset_right_add_coset] lemma mem_right_coset_right_coset {a : α} (ha : s *r a = s) : a ∈ s := by rw [←ha]; exact mem_own_right_coset s a end coset_submonoid section coset_group variables [group α] {s : set α} {x : α} @[to_additive mem_left_add_coset_iff] lemma mem_left_coset_iff (a : α) : x ∈ a *l s ↔ a⁻¹ * x ∈ s := iff.intro (assume ⟨b, hb, eq⟩, by simp [eq.symm, hb]) (assume h, ⟨a⁻¹ * x, h, by simp⟩) @[to_additive mem_right_add_coset_iff] lemma mem_right_coset_iff (a : α) : x ∈ s *r a ↔ x * a⁻¹ ∈ s := iff.intro (assume ⟨b, hb, eq⟩, by simp [eq.symm, hb]) (assume h, ⟨x * a⁻¹, h, by simp⟩) end coset_group section coset_subgroup open is_submonoid open is_subgroup variables [group α] (s : set α) [is_subgroup s] @[to_additive left_add_coset_mem_left_add_coset] lemma left_coset_mem_left_coset {a : α} (ha : a ∈ s) : a *l s = s := set.ext $ by simp [mem_left_coset_iff, mul_mem_cancel_right s (inv_mem ha)] @[to_additive right_add_coset_mem_right_add_coset] lemma right_coset_mem_right_coset {a : α} (ha : a ∈ s) : s *r a = s := set.ext $ assume b, by simp [mem_right_coset_iff, mul_mem_cancel_left s (inv_mem ha)] @[to_additive normal_of_eq_add_cosets] theorem normal_of_eq_cosets [normal_subgroup s] (g : α) : g *l s = s *r g := set.ext $ assume a, by simp [mem_left_coset_iff, mem_right_coset_iff]; rw [mem_norm_comm_iff] @[to_additive eq_add_cosets_of_normal] theorem eq_cosets_of_normal (h : ∀ g, g *l s = s *r g) : normal_subgroup s := ⟨assume a ha g, show g * a * g⁻¹ ∈ s, by rw [← mem_right_coset_iff, ← h]; exact mem_left_coset g ha⟩ @[to_additive normal_iff_eq_add_cosets] theorem normal_iff_eq_cosets : normal_subgroup s ↔ ∀ g, g *l s = s *r g := ⟨@normal_of_eq_cosets _ _ s _, eq_cosets_of_normal s⟩ end coset_subgroup namespace quotient_group def left_rel [group α] (s : set α) [is_subgroup s] : setoid α := ⟨λ x y, x⁻¹ * y ∈ s, assume x, by simp [is_submonoid.one_mem], assume x y hxy, have (x⁻¹ * y)⁻¹ ∈ s, from is_subgroup.inv_mem hxy, by simpa using this, assume x y z hxy hyz, have x⁻¹ * y * (y⁻¹ * z) ∈ s, from is_submonoid.mul_mem hxy hyz, by simpa [mul_assoc] using this⟩ attribute [to_additive quotient_add_group.left_rel._proof_1] left_rel._proof_1 attribute [to_additive quotient_add_group.left_rel] left_rel attribute [to_additive quotient_add_group.left_rel.equations._eqn_1] left_rel.equations._eqn_1 /-- `quotient s` is the quotient type representing the left cosets of `s`. If `s` is a normal subgroup, `quotient s` is a group -/ def quotient [group α] (s : set α) [is_subgroup s] : Type* := quotient (left_rel s) attribute [to_additive quotient_add_group.quotient] quotient attribute [to_additive quotient_add_group.quotient.equations._eqn_1] quotient.equations._eqn_1 variables [group α] {s : set α} [is_subgroup s] @[to_additive quotient_add_group.mk] def mk (a : α) : quotient s := quotient.mk' a attribute [to_additive quotient_add_group.mk.equations._eqn_1] mk.equations._eqn_1 @[elab_as_eliminator, elab_strategy, to_additive quotient_add_group.induction_on] lemma induction_on {C : quotient s → Prop} (x : quotient s) (H : ∀ z, C (quotient_group.mk z)) : C x := quotient.induction_on' x H attribute [elab_as_eliminator, elab_strategy] quotient_add_group.induction_on @[to_additive quotient_add_group.has_coe] instance : has_coe α (quotient s) := ⟨mk⟩ attribute [to_additive quotient_add_group.has_coe.equations._eqn_1] has_coe.equations._eqn_1 @[elab_as_eliminator, elab_strategy, to_additive quotient_add_group.induction_on'] lemma induction_on' {C : quotient s → Prop} (x : quotient s) (H : ∀ z : α, C z) : C x := quotient.induction_on' x H attribute [elab_as_eliminator, elab_strategy] quotient_add_group.induction_on' @[to_additive quotient_add_group.inhabited] instance [group α] (s : set α) [is_subgroup s] : inhabited (quotient s) := ⟨((1 : α) : quotient s)⟩ attribute [to_additive quotient_add_group.inhabited.equations._eqn_1] inhabited.equations._eqn_1 @[to_additive quotient_add_group.eq] protected lemma eq {a b : α} : (a : quotient s) = b ↔ a⁻¹ * b ∈ s := quotient.eq' @[to_additive quotient_add_group.eq_class_eq_left_coset] lemma eq_class_eq_left_coset [group α] (s : set α) [is_subgroup s] (g : α) : {x : α | (x : quotient s) = g} = left_coset g s := set.ext $ λ z, by rw [mem_left_coset_iff, set.mem_set_of_eq, eq_comm, quotient_group.eq] end quotient_group namespace is_subgroup open quotient_group variables [group α] {s : set α} def left_coset_equiv_subgroup (g : α) : left_coset g s ≃ s := ⟨λ x, ⟨g⁻¹ * x.1, (mem_left_coset_iff _).1 x.2⟩, λ x, ⟨g * x.1, x.1, x.2, rfl⟩, λ ⟨x, hx⟩, subtype.eq $ by simp, λ ⟨g, hg⟩, subtype.eq $ by simp⟩ attribute [to_additive is_add_subgroup.left_add_coset_equiv_subgroup._match_2] left_coset_equiv_subgroup._match_2 attribute [to_additive is_add_subgroup.left_add_coset_equiv_subgroup._match_1] left_coset_equiv_subgroup._match_1 attribute [to_additive is_add_subgroup.left_add_coset_equiv_subgroup._proof_4] left_coset_equiv_subgroup._proof_4 attribute [to_additive is_add_subgroup.left_add_coset_equiv_subgroup._proof_3] left_coset_equiv_subgroup._proof_3 attribute [to_additive is_add_subgroup.left_add_coset_equiv_subgroup._proof_2] left_coset_equiv_subgroup._proof_2 attribute [to_additive is_add_subgroup.left_add_coset_equiv_subgroup._proof_1] left_coset_equiv_subgroup._proof_1 attribute [to_additive is_add_subgroup.left_add_coset_equiv_subgroup] left_coset_equiv_subgroup attribute [to_additive is_add_subgroup.left_add_coset_equiv_subgroup.equations._eqn_1] left_coset_equiv_subgroup.equations._eqn_1 attribute [to_additive is_add_subgroup.left_add_coset_equiv_subgroup._match_1.equations._eqn_1] left_coset_equiv_subgroup._match_1.equations._eqn_1 attribute [to_additive is_add_subgroup.left_add_coset_equiv_subgroup._match_2.equations._eqn_1] left_coset_equiv_subgroup._match_2.equations._eqn_1 noncomputable def group_equiv_quotient_times_subgroup (hs : is_subgroup s) : α ≃ (quotient s × s) := calc α ≃ Σ L : quotient s, {x : α // (x : quotient s)= L} : equiv.equiv_fib quotient_group.mk ... ≃ Σ L : quotient s, left_coset (quotient.out' L) s : equiv.sigma_congr_right (λ L, begin rw ← eq_class_eq_left_coset, show {x // quotient.mk' x = L} ≃ {x : α // quotient.mk' x = quotient.mk' _}, simp [-quotient.eq'] end) ... ≃ Σ L : quotient s, s : equiv.sigma_congr_right (λ L, left_coset_equiv_subgroup _) ... ≃ (quotient s × s) : equiv.sigma_equiv_prod _ _ attribute [to_additive is_add_subgroup.add_group_equiv_quotient_times_subgroup._proof_2] group_equiv_quotient_times_subgroup._proof_2 attribute [to_additive is_add_subgroup.add_group_equiv_quotient_times_subgroup._proof_1] group_equiv_quotient_times_subgroup._proof_1 attribute [to_additive is_add_subgroup.add_group_equiv_quotient_times_subgroup] group_equiv_quotient_times_subgroup attribute [to_additive is_add_subgroup.add_group_equiv_quotient_times_subgroup.equations._eqn_1] group_equiv_quotient_times_subgroup.equations._eqn_1 end is_subgroup
39727bd33ba9d777094e53b84a8954d604334e3e
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/measure_theory/integral/riesz_markov_kakutani.lean
24aeedef3703a7b36fb182fcf31b971dafe5b5a3
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
5,035
lean
/- Copyright (c) 2022 Jesse Reimann. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jesse Reimann, Kalle Kytölä -/ import topology.continuous_function.bounded import topology.sets.compacts /-! # Riesz–Markov–Kakutani representation theorem This file will prove different versions of the Riesz-Markov-Kakutani representation theorem. The theorem is first proven for compact spaces, from which the statements about linear functionals on bounded continuous functions or compactly supported functions on locally compact spaces follow. To make use of the existing API, the measure is constructed from a content `λ` on the compact subsets of the space X, rather than the usual construction of open sets in the literature. ## References * [Walter Rudin, Real and Complex Analysis.][Rud87] -/ noncomputable theory open_locale bounded_continuous_function nnreal ennreal open set function topological_space variables {X : Type*} [topological_space X] variables (Λ : (X →ᵇ ℝ≥0) →ₗ[ℝ≥0] ℝ≥0) /-! ### Construction of the content: -/ /-- Given a positive linear functional Λ on X, for `K ⊆ X` compact define `λ(K) = inf {Λf | 1≤f on K}`. When X is a compact Hausdorff space, this will be shown to be a content, and will be shown to agree with the Riesz measure on the compact subsets `K ⊆ X`. -/ def riesz_content_aux : (compacts X) → ℝ≥0 := λ K, Inf (Λ '' {f : X →ᵇ ℝ≥0 | ∀ x ∈ K, (1 : ℝ≥0) ≤ f x}) section riesz_monotone /-- For any compact subset `K ⊆ X`, there exist some bounded continuous nonnegative functions f on X such that `f ≥ 1` on K. -/ lemma riesz_content_aux_image_nonempty (K : compacts X) : (Λ '' {f : X →ᵇ ℝ≥0 | ∀ x ∈ K, (1 : ℝ≥0) ≤ f x}).nonempty := begin rw nonempty_image_iff, use (1 : X →ᵇ ℝ≥0), intros x x_in_K, simp only [bounded_continuous_function.coe_one, pi.one_apply], end /-- Riesz content λ (associated with a positive linear functional Λ) is monotone: if `K₁ ⊆ K₂` are compact subsets in X, then `λ(K₁) ≤ λ(K₂)`. -/ lemma riesz_content_aux_mono {K₁ K₂ : compacts X} (h : K₁ ≤ K₂) : riesz_content_aux Λ K₁ ≤ riesz_content_aux Λ K₂ := cInf_le_cInf (order_bot.bdd_below _) (riesz_content_aux_image_nonempty Λ K₂) (image_subset Λ (set_of_subset_set_of.mpr (λ f f_hyp x x_in_K₁, f_hyp x (h x_in_K₁)))) end riesz_monotone section riesz_subadditive /-- Any bounded continuous nonnegative f such that `f ≥ 1` on K gives an upper bound on the content of K; namely `λ(K) ≤ Λ f`. -/ lemma riesz_content_aux_le {K : compacts X} {f : X →ᵇ ℝ≥0} (h : ∀ x ∈ K, (1 : ℝ≥0) ≤ f x) : riesz_content_aux Λ K ≤ Λ f := cInf_le (order_bot.bdd_below _) ⟨f, ⟨h, rfl⟩⟩ /-- The Riesz content can be approximated arbitrarily well by evaluating the positive linear functional on test functions: for any `ε > 0`, there exists a bounded continuous nonnegative function f on X such that `f ≥ 1` on K and such that `λ(K) ≤ Λ f < λ(K) + ε`. -/ lemma exists_lt_riesz_content_aux_add_pos (K : compacts X) {ε : ℝ≥0} (εpos : 0 < ε) : ∃ (f : X →ᵇ ℝ≥0), (∀ x ∈ K, (1 : ℝ≥0) ≤ f x) ∧ Λ f < riesz_content_aux Λ K + ε := begin --choose a test function `f` s.t. `Λf = α < λ(K) + ε` obtain ⟨α, ⟨⟨f, f_hyp⟩, α_hyp⟩⟩ := exists_lt_of_cInf_lt (riesz_content_aux_image_nonempty Λ K) (lt_add_of_pos_right (riesz_content_aux Λ K) εpos), refine ⟨f, f_hyp.left, _ ⟩, rw f_hyp.right, exact α_hyp, end /-- The Riesz content λ associated to a given positive linear functional Λ is finitely subadditive: `λ(K₁ ∪ K₂) ≤ λ(K₁) + λ(K₂)` for any compact subsets `K₁, K₂ ⊆ X`. -/ lemma riesz_content_aux_sup_le (K1 K2 : compacts X) : riesz_content_aux Λ (K1 ⊔ K2) ≤ riesz_content_aux Λ (K1) + riesz_content_aux Λ (K2) := begin apply nnreal.le_of_forall_pos_le_add, intros ε εpos, --get test functions s.t. `λ(Ki) ≤ Λfi ≤ λ(Ki) + ε/2, i=1,2` obtain ⟨f1, f_test_function_K1⟩ := exists_lt_riesz_content_aux_add_pos Λ K1 (nnreal.half_pos εpos), obtain ⟨f2, f_test_function_K2⟩ := exists_lt_riesz_content_aux_add_pos Λ K2 (nnreal.half_pos εpos), --let `f := f1 + f2` test function for the content of `K` have f_test_function_union : (∀ x ∈ (K1 ⊔ K2), (1 : ℝ≥0) ≤ (f1 + f2) x), { rintros x (x_in_K1 | x_in_K2), { exact le_add_right (f_test_function_K1.left x x_in_K1) }, { exact le_add_left (f_test_function_K2.left x x_in_K2) }}, --use that `Λf` is an upper bound for `λ(K1⊔K2)` apply (riesz_content_aux_le Λ f_test_function_union).trans (le_of_lt _), rw map_add, --use that `Λfi` are lower bounds for `λ(Ki) + ε/2` apply lt_of_lt_of_le (add_lt_add f_test_function_K1.right f_test_function_K2.right) (le_of_eq _), rw [add_assoc, add_comm (ε/2), add_assoc, nnreal.add_halves ε, add_assoc], end end riesz_subadditive
8719abdd12d381d453a3270d0d99c08e89c6ad6c
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/dynamics/omega_limit_auto.lean
207980ccb136c084726cff987fb50cb08e5f753d
[]
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
15,428
lean
/- Copyright (c) 2020 Jean Lo. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jean Lo -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.dynamics.flow import Mathlib.PostPort universes u_1 u_2 u_3 u_4 u_5 namespace Mathlib /-! # ω-limits For a function `ϕ : τ → α → β` where `β` is a topological space, we define the ω-limit under `ϕ` of a set `s` in `α` with respect to filter `f` on `τ`: an element `y : β` is in the ω-limit of `s` if the forward images of `s` intersect arbitrarily small neighbourhoods of `y` frequently "in the direction of `f`". In practice `ϕ` is often a continuous monoid-act, but the definition requires only that `ϕ` has a coercion to the appropriate function type. In the case where `τ` is `ℕ` or `ℝ` and `f` is `at_top`, we recover the usual definition of the ω-limit set as the set of all `y` such that there exist sequences `(tₙ)`, `(xₙ)` such that `ϕ tₙ xₙ ⟶ y` as `n ⟶ ∞`. ## Notations The `omega_limit` locale provides the localised notation `ω` for `omega_limit`, as well as `ω⁺` and `ω⁻` for `omega_limit at_top` and `omega_limit at_bot` respectively for when the acting monoid is endowed with an order. -/ /-! ### Definition and notation -/ /-- The ω-limit of a set `s` under `ϕ` with respect to a filter `f` is ⋂ u ∈ f, cl (ϕ u s). -/ def omega_limit {τ : Type u_1} {α : Type u_2} {β : Type u_3} [topological_space β] (f : filter τ) (ϕ : τ → α → β) (s : set α) : set β := set.Inter fun (u : set τ) => set.Inter fun (H : u ∈ f) => closure (set.image2 ϕ u s) /-! ### Elementary properties -/ theorem omega_limit_def {τ : Type u_1} {α : Type u_2} {β : Type u_3} [topological_space β] (f : filter τ) (ϕ : τ → α → β) (s : set α) : omega_limit f ϕ s = set.Inter fun (u : set τ) => set.Inter fun (H : u ∈ f) => closure (set.image2 ϕ u s) := rfl theorem omega_limit_subset_of_tendsto {τ : Type u_1} {α : Type u_2} {β : Type u_3} [topological_space β] (ϕ : τ → α → β) (s : set α) {m : τ → τ} {f₁ : filter τ} {f₂ : filter τ} (hf : filter.tendsto m f₁ f₂) : omega_limit f₁ (fun (t : τ) (x : α) => ϕ (m t) x) s ⊆ omega_limit f₂ ϕ s := sorry theorem omega_limit_mono_left {τ : Type u_1} {α : Type u_2} {β : Type u_3} [topological_space β] (ϕ : τ → α → β) (s : set α) {f₁ : filter τ} {f₂ : filter τ} (hf : f₁ ≤ f₂) : omega_limit f₁ ϕ s ⊆ omega_limit f₂ ϕ s := omega_limit_subset_of_tendsto ϕ s (filter.tendsto_id' hf) theorem omega_limit_mono_right {τ : Type u_1} {α : Type u_2} {β : Type u_3} [topological_space β] (f : filter τ) (ϕ : τ → α → β) {s₁ : set α} {s₂ : set α} (hs : s₁ ⊆ s₂) : omega_limit f ϕ s₁ ⊆ omega_limit f ϕ s₂ := set.bInter_subset_bInter_right fun (u : set τ) (hu : u ∈ fun (u : set τ) => u ∈ filter.sets f) => closure_mono (set.image2_subset set.subset.rfl hs) theorem is_closed_omega_limit {τ : Type u_1} {α : Type u_2} {β : Type u_3} [topological_space β] (f : filter τ) (ϕ : τ → α → β) (s : set α) : is_closed (omega_limit f ϕ s) := is_closed_Inter fun (u : set τ) => is_closed_Inter fun (hu : u ∈ f) => is_closed_closure theorem maps_to_omega_limit' {τ : Type u_1} {α : Type u_2} {β : Type u_3} [topological_space β] (s : set α) {α' : Type u_4} {β' : Type u_5} [topological_space β'] {f : filter τ} {ϕ : τ → α → β} {ϕ' : τ → α' → β'} {ga : α → α'} {s' : set α'} (hs : set.maps_to ga s s') {gb : β → β'} (hg : filter.eventually (fun (t : τ) => set.eq_on (gb ∘ ϕ t) (ϕ' t ∘ ga) s) f) (hgc : continuous gb) : set.maps_to gb (omega_limit f ϕ s) (omega_limit f ϕ' s') := sorry theorem maps_to_omega_limit {τ : Type u_1} {α : Type u_2} {β : Type u_3} [topological_space β] (s : set α) {α' : Type u_4} {β' : Type u_5} [topological_space β'] {f : filter τ} {ϕ : τ → α → β} {ϕ' : τ → α' → β'} {ga : α → α'} {s' : set α'} (hs : set.maps_to ga s s') {gb : β → β'} (hg : ∀ (t : τ) (x : α), gb (ϕ t x) = ϕ' t (ga x)) (hgc : continuous gb) : set.maps_to gb (omega_limit f ϕ s) (omega_limit f ϕ' s') := maps_to_omega_limit' s hs (filter.eventually_of_forall fun (t : τ) (x : α) (hx : x ∈ s) => hg t x) hgc theorem omega_limit_image_eq {τ : Type u_1} {α : Type u_2} {β : Type u_3} [topological_space β] (s : set α) {α' : Type u_4} (ϕ : τ → α' → β) (f : filter τ) (g : α → α') : omega_limit f ϕ (g '' s) = omega_limit f (fun (t : τ) (x : α) => ϕ t (g x)) s := sorry theorem omega_limit_preimage_subset {τ : Type u_1} {α : Type u_2} {β : Type u_3} [topological_space β] {α' : Type u_4} (ϕ : τ → α' → β) (s : set α') (f : filter τ) (g : α → α') : omega_limit f (fun (t : τ) (x : α) => ϕ t (g x)) (g ⁻¹' s) ⊆ omega_limit f ϕ s := maps_to_omega_limit (g ⁻¹' s) (set.maps_to_preimage g s) (fun (t : τ) (x : α) => rfl) continuous_id /-! ### Equivalent definitions of the omega limit The next few lemmas are various versions of the property characterising ω-limits: -/ /-- An element `y` is in the ω-limit set of `s` w.r.t. `f` if the preimages of an arbitrary neighbourhood of `y` frequently (w.r.t. `f`) intersects of `s`. -/ theorem mem_omega_limit_iff_frequently {τ : Type u_1} {α : Type u_2} {β : Type u_3} [topological_space β] (f : filter τ) (ϕ : τ → α → β) (s : set α) (y : β) : y ∈ omega_limit f ϕ s ↔ ∀ (n : set β), n ∈ nhds y → filter.frequently (fun (t : τ) => set.nonempty (s ∩ ϕ t ⁻¹' n)) f := sorry /-- An element `y` is in the ω-limit set of `s` w.r.t. `f` if the forward images of `s` frequently (w.r.t. `f`) intersect arbitrary neighbourhoods of `y`. -/ theorem mem_omega_limit_iff_frequently₂ {τ : Type u_1} {α : Type u_2} {β : Type u_3} [topological_space β] (f : filter τ) (ϕ : τ → α → β) (s : set α) (y : β) : y ∈ omega_limit f ϕ s ↔ ∀ (n : set β), n ∈ nhds y → filter.frequently (fun (t : τ) => set.nonempty (ϕ t '' s ∩ n)) f := sorry /-- An element `y` is in the ω-limit of `x` w.r.t. `f` if the forward images of `x` frequently (w.r.t. `f`) falls within an arbitrary neighbourhood of `y`. -/ theorem mem_omega_limit_singleton_iff_map_cluster_point {τ : Type u_1} {α : Type u_2} {β : Type u_3} [topological_space β] (f : filter τ) (ϕ : τ → α → β) (x : α) (y : β) : y ∈ omega_limit f ϕ (singleton x) ↔ map_cluster_pt y f fun (t : τ) => ϕ t x := sorry /-! ### Set operations and omega limits -/ theorem omega_limit_inter {τ : Type u_1} {α : Type u_2} {β : Type u_3} [topological_space β] (f : filter τ) (ϕ : τ → α → β) (s₁ : set α) (s₂ : set α) : omega_limit f ϕ (s₁ ∩ s₂) ⊆ omega_limit f ϕ s₁ ∩ omega_limit f ϕ s₂ := set.subset_inter (omega_limit_mono_right f ϕ (set.inter_subset_left s₁ s₂)) (omega_limit_mono_right f ϕ (set.inter_subset_right s₁ s₂)) theorem omega_limit_Inter {τ : Type u_1} {α : Type u_2} {β : Type u_3} {ι : Type u_4} [topological_space β] (f : filter τ) (ϕ : τ → α → β) (p : ι → set α) : omega_limit f ϕ (set.Inter fun (i : ι) => p i) ⊆ set.Inter fun (i : ι) => omega_limit f ϕ (p i) := set.subset_Inter fun (i : ι) => omega_limit_mono_right f ϕ (set.Inter_subset (fun (i : ι) => p i) i) theorem omega_limit_union {τ : Type u_1} {α : Type u_2} {β : Type u_3} [topological_space β] (f : filter τ) (ϕ : τ → α → β) (s₁ : set α) (s₂ : set α) : omega_limit f ϕ (s₁ ∪ s₂) = omega_limit f ϕ s₁ ∪ omega_limit f ϕ s₂ := sorry theorem omega_limit_Union {τ : Type u_1} {α : Type u_2} {β : Type u_3} {ι : Type u_4} [topological_space β] (f : filter τ) (ϕ : τ → α → β) (p : ι → set α) : (set.Union fun (i : ι) => omega_limit f ϕ (p i)) ⊆ omega_limit f ϕ (set.Union fun (i : ι) => p i) := sorry /-! Different expressions for omega limits, useful for rewrites. In particular, one may restrict the intersection to sets in `f` which are subsets of some set `v` also in `f`. -/ theorem omega_limit_eq_Inter {τ : Type u_1} {α : Type u_2} {β : Type u_3} [topological_space β] (f : filter τ) (ϕ : τ → α → β) (s : set α) : omega_limit f ϕ s = set.Inter fun (u : ↥(filter.sets f)) => closure (set.image2 ϕ (↑u) s) := set.bInter_eq_Inter (fun (u : set τ) => u ∈ filter.sets f) fun (u : set τ) (H : u ∈ f) => closure (set.image2 ϕ u s) theorem omega_limit_eq_bInter_inter {τ : Type u_1} {α : Type u_2} {β : Type u_3} [topological_space β] (f : filter τ) (ϕ : τ → α → β) (s : set α) {v : set τ} (hv : v ∈ f) : omega_limit f ϕ s = set.Inter fun (u : set τ) => set.Inter fun (H : u ∈ f) => closure (set.image2 ϕ (u ∩ v) s) := sorry theorem omega_limit_eq_Inter_inter {τ : Type u_1} {α : Type u_2} {β : Type u_3} [topological_space β] (f : filter τ) (ϕ : τ → α → β) (s : set α) {v : set τ} (hv : v ∈ f) : omega_limit f ϕ s = set.Inter fun (u : ↥(filter.sets f)) => closure (set.image2 ϕ (↑u ∩ v) s) := sorry theorem omega_limit_subset_closure_fw_image {τ : Type u_1} {α : Type u_2} {β : Type u_3} [topological_space β] (f : filter τ) (ϕ : τ → α → β) (s : set α) {u : set τ} (hu : u ∈ f) : omega_limit f ϕ s ⊆ closure (set.image2 ϕ u s) := sorry /-! ### `ω-limits and compactness -/ /-- A set is eventually carried into any open neighbourhood of its ω-limit: if `c` is a compact set such that `closure {ϕ t x | t ∈ v, x ∈ s} ⊆ c` for some `v ∈ f` and `n` is an open neighbourhood of `ω f ϕ s`, then for some `u ∈ f` we have `closure {ϕ t x | t ∈ u, x ∈ s} ⊆ n`. -/ theorem eventually_closure_subset_of_is_compact_absorbing_of_is_open_of_omega_limit_subset' {τ : Type u_1} {α : Type u_2} {β : Type u_3} [topological_space β] (f : filter τ) (ϕ : τ → α → β) (s : set α) {c : set β} (hc₁ : is_compact c) (hc₂ : ∃ (v : set τ), ∃ (H : v ∈ f), closure (set.image2 ϕ v s) ⊆ c) {n : set β} (hn₁ : is_open n) (hn₂ : omega_limit f ϕ s ⊆ n) : ∃ (u : set τ), ∃ (H : u ∈ f), closure (set.image2 ϕ u s) ⊆ n := sorry /-- A set is eventually carried into any open neighbourhood of its ω-limit: if `c` is a compact set such that `closure {ϕ t x | t ∈ v, x ∈ s} ⊆ c` for some `v ∈ f` and `n` is an open neighbourhood of `ω f ϕ s`, then for some `u ∈ f` we have `closure {ϕ t x | t ∈ u, x ∈ s} ⊆ n`. -/ theorem eventually_closure_subset_of_is_compact_absorbing_of_is_open_of_omega_limit_subset {τ : Type u_1} {α : Type u_2} {β : Type u_3} [topological_space β] (f : filter τ) (ϕ : τ → α → β) (s : set α) [t2_space β] {c : set β} (hc₁ : is_compact c) (hc₂ : filter.eventually (fun (t : τ) => set.maps_to (ϕ t) s c) f) {n : set β} (hn₁ : is_open n) (hn₂ : omega_limit f ϕ s ⊆ n) : ∃ (u : set τ), ∃ (H : u ∈ f), closure (set.image2 ϕ u s) ⊆ n := sorry theorem eventually_maps_to_of_is_compact_absorbing_of_is_open_of_omega_limit_subset {τ : Type u_1} {α : Type u_2} {β : Type u_3} [topological_space β] (f : filter τ) (ϕ : τ → α → β) (s : set α) [t2_space β] {c : set β} (hc₁ : is_compact c) (hc₂ : filter.eventually (fun (t : τ) => set.maps_to (ϕ t) s c) f) {n : set β} (hn₁ : is_open n) (hn₂ : omega_limit f ϕ s ⊆ n) : filter.eventually (fun (t : τ) => set.maps_to (ϕ t) s n) f := sorry theorem eventually_closure_subset_of_is_open_of_omega_limit_subset {τ : Type u_1} {α : Type u_2} {β : Type u_3} [topological_space β] (f : filter τ) (ϕ : τ → α → β) (s : set α) [compact_space β] {v : set β} (hv₁ : is_open v) (hv₂ : omega_limit f ϕ s ⊆ v) : ∃ (u : set τ), ∃ (H : u ∈ f), closure (set.image2 ϕ u s) ⊆ v := eventually_closure_subset_of_is_compact_absorbing_of_is_open_of_omega_limit_subset' f ϕ s compact_univ (Exists.intro set.univ (Exists.intro filter.univ_mem_sets (set.subset_univ (closure (set.image2 ϕ set.univ s))))) hv₁ hv₂ theorem eventually_maps_to_of_is_open_of_omega_limit_subset {τ : Type u_1} {α : Type u_2} {β : Type u_3} [topological_space β] (f : filter τ) (ϕ : τ → α → β) (s : set α) [compact_space β] {v : set β} (hv₁ : is_open v) (hv₂ : omega_limit f ϕ s ⊆ v) : filter.eventually (fun (t : τ) => set.maps_to (ϕ t) s v) f := sorry /-- The ω-limit of a nonempty set w.r.t. a nontrivial filter is nonempty. -/ theorem nonempty_omega_limit_of_is_compact_absorbing {τ : Type u_1} {α : Type u_2} {β : Type u_3} [topological_space β] (f : filter τ) (ϕ : τ → α → β) (s : set α) [filter.ne_bot f] {c : set β} (hc₁ : is_compact c) (hc₂ : ∃ (v : set τ), ∃ (H : v ∈ f), closure (set.image2 ϕ v s) ⊆ c) (hs : set.nonempty s) : set.nonempty (omega_limit f ϕ s) := sorry theorem nonempty_omega_limit {τ : Type u_1} {α : Type u_2} {β : Type u_3} [topological_space β] (f : filter τ) (ϕ : τ → α → β) (s : set α) [compact_space β] [filter.ne_bot f] (hs : set.nonempty s) : set.nonempty (omega_limit f ϕ s) := nonempty_omega_limit_of_is_compact_absorbing f ϕ s compact_univ (Exists.intro set.univ (Exists.intro filter.univ_mem_sets (set.subset_univ (closure (set.image2 ϕ set.univ s))))) hs /-! ### ω-limits of Flows by a Monoid -/ namespace flow theorem is_invariant_omega_limit {τ : Type u_1} [topological_space τ] [add_monoid τ] [has_continuous_add τ] {α : Type u_2} [topological_space α] (f : filter τ) (ϕ : flow τ α) (s : set α) (hf : ∀ (t : τ), filter.tendsto (Add.add t) f f) : is_invariant (⇑ϕ) (omega_limit f (⇑ϕ) s) := sorry theorem omega_limit_image_subset {τ : Type u_1} [topological_space τ] [add_monoid τ] [has_continuous_add τ] {α : Type u_2} [topological_space α] (f : filter τ) (ϕ : flow τ α) (s : set α) (t : τ) (ht : filter.tendsto (fun (_x : τ) => _x + t) f f) : omega_limit f (⇑ϕ) (coe_fn ϕ t '' s) ⊆ omega_limit f (⇑ϕ) s := sorry end flow /-! ### ω-limits of Flows by a Group -/ namespace flow /-- the ω-limit of a forward image of `s` is the same as the ω-limit of `s`. -/ @[simp] theorem omega_limit_image_eq {τ : Type u_1} [topological_space τ] [add_comm_group τ] [topological_add_group τ] {α : Type u_2} [topological_space α] (f : filter τ) (ϕ : flow τ α) (s : set α) (hf : ∀ (t : τ), filter.tendsto (fun (_x : τ) => _x + t) f f) (t : τ) : omega_limit f (⇑ϕ) (coe_fn ϕ t '' s) = omega_limit f (⇑ϕ) s := sorry theorem omega_limit_omega_limit {τ : Type u_1} [topological_space τ] [add_comm_group τ] [topological_add_group τ] {α : Type u_2} [topological_space α] (f : filter τ) (ϕ : flow τ α) (s : set α) (hf : ∀ (t : τ), filter.tendsto (Add.add t) f f) : omega_limit f (⇑ϕ) (omega_limit f (⇑ϕ) s) ⊆ omega_limit f (⇑ϕ) s := sorry end Mathlib
b6b9a522a441f5798b15b7c035961e4fdcb7078d
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/pkg/user_attr_app/lakefile.lean
0196ebab36623b6e0c073bcab27c546019c3f90f
[ "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
160
lean
import Lake open System Lake DSL package user_attr lean_lib UserAttr @[default_target] lean_exe user_attr where root := `Main supportInterpreter := true
68bb56c9c508ed08229c661b9252e4f9eb84b50b
a4673261e60b025e2c8c825dfa4ab9108246c32e
/tests/lean/namelit.lean
f6ecef7e407f00720eb3f5bec42c01f43f3e40fa
[ "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
258
lean
#check `foo #check `foo.bla #check `«foo bla» #check `«foo bla».«hello world» #check `«foo bla».boo.«hello world» #check `foo.«hello» macro dummy1 : term => `(`hello) macro dummy2 : term => `(`hello.«world !!!») #check dummy1 #check dummy2
3c115232fc1bfc8dd34c50473765a98a3b0c1866
9dc8cecdf3c4634764a18254e94d43da07142918
/src/algebra/euclidean_domain.lean
a4709703046c1b03addc8d816e2512dc361a7df0
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
18,650
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 -/ import data.int.basic import algebra.field.basic /-! # Euclidean domains This file introduces Euclidean domains and provides the extended Euclidean algorithm. To be precise, a slightly more general version is provided which is sometimes called a transfinite Euclidean domain and differs in the fact that the degree function need not take values in `ℕ` but can take values in any well-ordered set. Transfinite Euclidean domains were introduced by Motzkin and examples which don't satisfy the classical notion were provided independently by Hiblot and Nagata. ## Main definitions * `euclidean_domain`: Defines Euclidean domain with functions `quotient` and `remainder`. Instances of `has_div` and `has_mod` are provided, so that one can write `a = b * (a / b) + a % b`. * `gcd`: defines the greatest common divisors of two elements of a Euclidean domain. * `xgcd`: given two elements `a b : R`, `xgcd a b` defines the pair `(x, y)` such that `x * a + y * b = gcd a b`. * `lcm`: defines the lowest common multiple of two elements `a` and `b` of a Euclidean domain as `a * b / (gcd a b)` ## Main statements * `gcd_eq_gcd_ab`: states Bézout's lemma for Euclidean domains. * `int.euclidean_domain`: shows that `ℤ` is a Euclidean domain. * `field.to_euclidean_domain`: shows that any field is a Euclidean domain. ## Notation `≺` denotes the well founded relation on the Euclidean domain, e.g. in the example of the polynomial ring over a field, `p ≺ q` for polynomials `p` and `q` if and only if the degree of `p` is less than the degree of `q`. ## Implementation details Instead of working with a valuation, `euclidean_domain` is implemented with the existence of a well founded relation `r` on the integral domain `R`, which in the example of `ℤ` would correspond to setting `i ≺ j` for integers `i` and `j` if the absolute value of `i` is smaller than the absolute value of `j`. ## References * [Th. Motzkin, *The Euclidean algorithm*][MR32592] * [J.-J. Hiblot, *Des anneaux euclidiens dont le plus petit algorithme n'est pas à valeurs finies*] [MR399081] * [M. Nagata, *On Euclid algorithm*][MR541021] ## Tags Euclidean domain, transfinite Euclidean domain, Bézout's lemma -/ universe u /-- A `euclidean_domain` is an non-trivial commutative ring 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. -/ @[protect_proj without mul_left_not_lt r_well_founded] class euclidean_domain (R : Type u) extends comm_ring R, nontrivial R := (quotient : R → R → R) (quotient_zero : ∀ a, quotient a 0 = 0) (remainder : R → R → R) (quotient_mul_add_remainder_eq : ∀ a b, b * quotient a b + remainder a b = a) (r : R → R → Prop) (r_well_founded : well_founded r) (remainder_lt : ∀ a {b}, b ≠ 0 → r (remainder a b) b) (mul_left_not_lt : ∀ a {b}, b ≠ 0 → ¬r (a * b) a) namespace euclidean_domain variable {R : Type u} variables [euclidean_domain R] local infix ` ≺ `:50 := euclidean_domain.r @[priority 70] -- see Note [lower instance priority] instance : has_div R := ⟨euclidean_domain.quotient⟩ @[priority 70] -- see Note [lower instance priority] instance : has_mod R := ⟨euclidean_domain.remainder⟩ theorem div_add_mod (a b : R) : b * (a / b) + a % b = a := euclidean_domain.quotient_mul_add_remainder_eq _ _ lemma mod_add_div (a b : R) : a % b + b * (a / b) = a := (add_comm _ _).trans (div_add_mod _ _) lemma mod_add_div' (m k : R) : m % k + (m / k) * k = m := by { rw mul_comm, exact mod_add_div _ _ } lemma div_add_mod' (m k : R) : (m / k) * k + m % k = m := by { rw mul_comm, exact div_add_mod _ _ } lemma mod_eq_sub_mul_div {R : Type*} [euclidean_domain R] (a b : R) : a % b = a - b * (a / b) := calc a % b = b * (a / b) + a % b - b * (a / b) : (add_sub_cancel' _ _).symm ... = a - b * (a / b) : by rw div_add_mod theorem mod_lt : ∀ a {b : R}, b ≠ 0 → (a % b) ≺ b := euclidean_domain.remainder_lt theorem mul_right_not_lt {a : R} (b) (h : a ≠ 0) : ¬(a * b) ≺ b := by { rw mul_comm, exact mul_left_not_lt b h } lemma mul_div_cancel_left {a : R} (b) (a0 : a ≠ 0) : a * b / a = b := eq.symm $ eq_of_sub_eq_zero $ classical.by_contradiction $ λ h, begin have := mul_left_not_lt a h, rw [mul_sub, sub_eq_iff_eq_add'.2 (div_add_mod (a*b) a).symm] at this, exact this (mod_lt _ a0) end lemma mul_div_cancel (a) {b : R} (b0 : b ≠ 0) : a * b / b = a := by { rw mul_comm, exact mul_div_cancel_left a b0 } @[simp] lemma mod_zero (a : R) : a % 0 = a := by simpa only [zero_mul, zero_add] using div_add_mod a 0 @[simp] lemma mod_eq_zero {a b : R} : a % b = 0 ↔ b ∣ a := ⟨λ h, by { rw [← div_add_mod a b, h, add_zero], exact dvd_mul_right _ _ }, λ ⟨c, e⟩, begin rw [e, ← add_left_cancel_iff, div_add_mod, add_zero], haveI := classical.dec, by_cases b0 : b = 0, { simp only [b0, zero_mul] }, { rw [mul_div_cancel_left _ b0] } end⟩ @[simp] lemma mod_self (a : R) : a % a = 0 := mod_eq_zero.2 dvd_rfl lemma dvd_mod_iff {a b c : R} (h : c ∣ b) : c ∣ a % b ↔ c ∣ a := by rw [dvd_add_iff_right (h.mul_right _), div_add_mod] lemma lt_one (a : R) : a ≺ (1:R) → a = 0 := by { haveI := classical.dec, exact not_imp_not.1 (λ h, by simpa only [one_mul] using mul_left_not_lt 1 h) } lemma val_dvd_le : ∀ a b : R, b ∣ a → a ≠ 0 → ¬a ≺ b | _ b ⟨d, rfl⟩ ha := mul_left_not_lt b (mt (by { rintro rfl, exact mul_zero _ }) ha) @[simp] lemma mod_one (a : R) : a % 1 = 0 := mod_eq_zero.2 (one_dvd _) @[simp] lemma zero_mod (b : R) : 0 % b = 0 := mod_eq_zero.2 (dvd_zero _) @[simp, priority 900] lemma div_zero (a : R) : a / 0 = 0 := euclidean_domain.quotient_zero a @[simp, priority 900] lemma zero_div {a : R} : 0 / a = 0 := classical.by_cases (λ a0 : a = 0, a0.symm ▸ div_zero 0) (λ a0, by simpa only [zero_mul] using mul_div_cancel 0 a0) @[simp, priority 900] lemma div_self {a : R} (a0 : a ≠ 0) : a / a = 1 := by simpa only [one_mul] using mul_div_cancel 1 a0 lemma eq_div_of_mul_eq_left {a b c : R} (hb : b ≠ 0) (h : a * b = c) : a = c / b := by rw [← h, mul_div_cancel _ hb] lemma eq_div_of_mul_eq_right {a b c : R} (ha : a ≠ 0) (h : a * b = c) : b = c / a := by rw [← h, mul_div_cancel_left _ ha] theorem mul_div_assoc (x : R) {y z : R} (h : z ∣ y) : x * y / z = x * (y / z) := begin classical, by_cases hz : z = 0, { subst hz, rw [div_zero, div_zero, mul_zero] }, rcases h with ⟨p, rfl⟩, rw [mul_div_cancel_left _ hz, mul_left_comm, mul_div_cancel_left _ hz] end @[simp, priority 900] -- This generalizes `int.div_one`, see note [simp-normal form] lemma div_one (p : R) : p / 1 = p := (euclidean_domain.eq_div_of_mul_eq_left (@one_ne_zero R _ _) (mul_one p)).symm lemma div_dvd_of_dvd {p q : R} (hpq : q ∣ p) : p / q ∣ p := begin by_cases hq : q = 0, { rw [hq, zero_dvd_iff] at hpq, rw hpq, exact dvd_zero _ }, use q, rw [mul_comm, ← euclidean_domain.mul_div_assoc _ hpq, mul_comm, euclidean_domain.mul_div_cancel _ hq] end lemma dvd_div_of_mul_dvd {a b c : R} (h : a * b ∣ c) : b ∣ c / a := begin rcases eq_or_ne a 0 with rfl | ha, { simp only [div_zero, dvd_zero] }, rcases h with ⟨d, rfl⟩, refine ⟨d, _⟩, rw [mul_assoc, mul_div_cancel_left _ ha] end section open_locale classical @[elab_as_eliminator] theorem gcd.induction {P : R → R → Prop} : ∀ a b : R, (∀ x, P 0 x) → (∀ a b, a ≠ 0 → P (b % a) a → P a b) → P a b | a := λ b H0 H1, if a0 : a = 0 then a0.symm ▸ H0 _ else have h:_ := mod_lt b a0, H1 _ _ a0 (gcd.induction (b%a) a H0 H1) using_well_founded {dec_tac := tactic.assumption, rel_tac := λ _ _, `[exact ⟨_, r_well_founded⟩]} end section gcd variable [decidable_eq R] /-- `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 → R → R | a := λ b, if a0 : a = 0 then b else have h:_ := mod_lt b a0, gcd (b%a) a using_well_founded {dec_tac := tactic.assumption, rel_tac := λ _ _, `[exact ⟨_, r_well_founded⟩]} @[simp] theorem gcd_zero_left (a : R) : gcd 0 a = a := by { rw gcd, exact if_pos rfl } @[simp] theorem gcd_zero_right (a : R) : gcd a 0 = a := by { rw gcd, split_ifs; simp only [h, zero_mod, gcd_zero_left] } theorem gcd_val (a b : R) : gcd a b = gcd (b % a) a := by { rw gcd, split_ifs; [simp only [h, mod_zero, gcd_zero_right], refl]} theorem gcd_dvd (a b : R) : gcd a b ∣ a ∧ gcd a b ∣ b := gcd.induction a b (λ b, by { rw gcd_zero_left, exact ⟨dvd_zero _, dvd_rfl⟩ }) (λ a b aneq ⟨IH₁, IH₂⟩, by { rw gcd_val, exact ⟨IH₂, (dvd_mod_iff IH₂).1 IH₁⟩ }) theorem gcd_dvd_left (a b : R) : gcd a b ∣ a := (gcd_dvd a b).left theorem gcd_dvd_right (a b : R) : gcd a b ∣ b := (gcd_dvd a b).right protected theorem gcd_eq_zero_iff {a b : R} : gcd a b = 0 ↔ a = 0 ∧ b = 0 := ⟨λ h, by simpa [h] using gcd_dvd a b, by { rintro ⟨rfl, rfl⟩, exact gcd_zero_right _ }⟩ theorem dvd_gcd {a b c : R} : c ∣ a → c ∣ b → c ∣ gcd a b := gcd.induction a b (λ _ _ H, by simpa only [gcd_zero_left] using H) (λ a b a0 IH ca cb, by { rw gcd_val, exact IH ((dvd_mod_iff ca).2 cb) ca }) theorem gcd_eq_left {a b : R} : gcd a b = a ↔ a ∣ b := ⟨λ h, by {rw ← h, apply gcd_dvd_right }, λ h, by rw [gcd_val, mod_eq_zero.2 h, gcd_zero_left]⟩ @[simp] theorem gcd_one_left (a : R) : gcd 1 a = 1 := gcd_eq_left.2 (one_dvd _) @[simp] theorem gcd_self (a : R) : gcd a a = a := gcd_eq_left.2 dvd_rfl /-- 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 → R → R → R → R → R → R × R × R | r := λ s t r' s' t', if hr : r = 0 then (r', s', t') else have r' % r ≺ r, from mod_lt _ hr, let q := r' / r in xgcd_aux (r' % r) (s' - q * s) (t' - q * t) r s t using_well_founded {dec_tac := tactic.assumption, rel_tac := λ _ _, `[exact ⟨_, r_well_founded⟩]} @[simp] theorem xgcd_zero_left {s t r' s' t' : R} : xgcd_aux 0 s t r' s' t' = (r', s', t') := by { unfold xgcd_aux, exact if_pos rfl } theorem xgcd_aux_rec {r s t r' s' 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 := by { conv {to_lhs, rw [xgcd_aux]}, exact if_neg h} /-- Use the extended GCD algorithm to generate the `a` and `b` values satisfying `gcd x y = x * a + y * b`. -/ def xgcd (x y : R) : R × R := (xgcd_aux x 1 0 y 0 1).2 /-- The extended GCD `a` value in the equation `gcd x y = x * a + y * b`. -/ def gcd_a (x y : R) : R := (xgcd x y).1 /-- The extended GCD `b` value in the equation `gcd x y = x * a + y * b`. -/ def gcd_b (x y : R) : R := (xgcd x y).2 @[simp] theorem gcd_a_zero_left {s : R} : gcd_a 0 s = 0 := by { unfold gcd_a, rw [xgcd, xgcd_zero_left] } @[simp] theorem gcd_b_zero_left {s : R} : gcd_b 0 s = 1 := by { unfold gcd_b, rw [xgcd, xgcd_zero_left] } @[simp] theorem xgcd_aux_fst (x y : R) : ∀ s t s' t', (xgcd_aux x s t y s' t').1 = gcd x y := gcd.induction x y (by { intros, rw [xgcd_zero_left, gcd_zero_left] }) (λ x y h IH s t s' t', by { simp only [xgcd_aux_rec h, if_neg h, IH], rw ← gcd_val }) theorem xgcd_aux_val (x y : R) : xgcd_aux x 1 0 y 0 1 = (gcd x y, xgcd x y) := by rw [xgcd, ← xgcd_aux_fst x y 1 0 0 1, prod.mk.eta] theorem xgcd_val (x y : R) : xgcd x y = (gcd_a x y, gcd_b x y) := prod.mk.eta.symm private def P (a b : R) : R × R × R → Prop | (r, s, t) := (r : R) = a * s + b * t theorem xgcd_aux_P (a b : R) {r r' : R} : ∀ {s t s' t'}, P a b (r, s, t) → P a b (r', s', t') → P a b (xgcd_aux r s t r' s' t') := gcd.induction r r' (by { intros, simpa only [xgcd_zero_left] }) $ λ x y h IH s t s' t' p p', begin rw [xgcd_aux_rec h], refine IH _ p, unfold P at p p' ⊢, rw [mul_sub, mul_sub, add_sub, sub_add_eq_add_sub, ← p', sub_sub, mul_comm _ s, ← mul_assoc, mul_comm _ t, ← mul_assoc, ← add_mul, ← p, mod_eq_sub_mul_div] end /-- An explicit version of **Bézout's lemma** for Euclidean domains. -/ theorem gcd_eq_gcd_ab (a b : R) : (gcd a b : R) = a * gcd_a a b + b * gcd_b a b := by { have := @xgcd_aux_P _ _ _ a b a b 1 0 0 1 (by rw [P, mul_one, mul_zero, add_zero]) (by rw [P, mul_one, mul_zero, zero_add]), rwa [xgcd_aux_val, xgcd_val] at this } @[priority 70] -- see Note [lower instance priority] instance (R : Type*) [e : euclidean_domain R] : is_domain R := by { haveI := classical.dec_eq R, exact { eq_zero_or_eq_zero_of_mul_eq_zero := λ a b h, (or_iff_not_and_not.2 $ λ h0, h0.1 $ by rw [← mul_div_cancel a h0.2, h, zero_div]), ..e }} end gcd section lcm variables [decidable_eq R] /-- `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 (x y : R) : R := x * y / gcd x y theorem dvd_lcm_left (x y : R) : x ∣ lcm x y := classical.by_cases (assume hxy : gcd x y = 0, by { rw [lcm, hxy, div_zero], exact dvd_zero _ }) (λ hxy, let ⟨z, hz⟩ := (gcd_dvd x y).2 in ⟨z, eq.symm $ eq_div_of_mul_eq_left hxy $ by rw [mul_right_comm, mul_assoc, ← hz]⟩) theorem dvd_lcm_right (x y : R) : y ∣ lcm x y := classical.by_cases (assume hxy : gcd x y = 0, by { rw [lcm, hxy, div_zero], exact dvd_zero _ }) (λ hxy, let ⟨z, hz⟩ := (gcd_dvd x y).1 in ⟨z, eq.symm $ eq_div_of_mul_eq_right hxy $ by rw [← mul_assoc, mul_right_comm, ← hz]⟩) theorem lcm_dvd {x y z : R} (hxz : x ∣ z) (hyz : y ∣ z) : lcm x y ∣ z := begin rw lcm, by_cases hxy : gcd x y = 0, { rw [hxy, div_zero], rw euclidean_domain.gcd_eq_zero_iff at hxy, rwa hxy.1 at hxz }, rcases gcd_dvd x y with ⟨⟨r, hr⟩, ⟨s, hs⟩⟩, suffices : x * y ∣ z * gcd x y, { cases this with p hp, use p, generalize_hyp : gcd x y = g at hxy hs hp ⊢, subst hs, rw [mul_left_comm, mul_div_cancel_left _ hxy, ← mul_left_inj' hxy, hp], rw [← mul_assoc], simp only [mul_right_comm] }, rw [gcd_eq_gcd_ab, mul_add], apply dvd_add, { rw mul_left_comm, exact mul_dvd_mul_left _ (hyz.mul_right _) }, { rw [mul_left_comm, mul_comm], exact mul_dvd_mul_left _ (hxz.mul_right _) } end @[simp] lemma lcm_dvd_iff {x y z : R} : lcm x y ∣ z ↔ x ∣ z ∧ y ∣ z := ⟨λ hz, ⟨(dvd_lcm_left _ _).trans hz, (dvd_lcm_right _ _).trans hz⟩, λ ⟨hxz, hyz⟩, lcm_dvd hxz hyz⟩ @[simp] lemma lcm_zero_left (x : R) : lcm 0 x = 0 := by rw [lcm, zero_mul, zero_div] @[simp] lemma lcm_zero_right (x : R) : lcm x 0 = 0 := by rw [lcm, mul_zero, zero_div] @[simp] lemma lcm_eq_zero_iff {x y : R} : lcm x y = 0 ↔ x = 0 ∨ y = 0 := begin split, { intro hxy, rw [lcm, mul_div_assoc _ (gcd_dvd_right _ _), mul_eq_zero] at hxy, apply or_of_or_of_imp_right hxy, intro hy, by_cases hgxy : gcd x y = 0, { rw euclidean_domain.gcd_eq_zero_iff at hgxy, exact hgxy.2 }, { rcases gcd_dvd x y with ⟨⟨r, hr⟩, ⟨s, hs⟩⟩, generalize_hyp : gcd x y = g at hr hs hy hgxy ⊢, subst hs, rw [mul_div_cancel_left _ hgxy] at hy, rw [hy, mul_zero] } }, rintro (hx | hy), { rw [hx, lcm_zero_left] }, { rw [hy, lcm_zero_right] } end @[simp] lemma gcd_mul_lcm (x y : R) : gcd x y * lcm x y = x * y := begin rw lcm, by_cases h : gcd x y = 0, { rw [h, zero_mul], rw euclidean_domain.gcd_eq_zero_iff at h, rw [h.1, zero_mul] }, rcases gcd_dvd x y with ⟨⟨r, hr⟩, ⟨s, hs⟩⟩, generalize_hyp : gcd x y = g at h hr ⊢, subst hr, rw [mul_assoc, mul_div_cancel_left _ h] end end lcm section div lemma mul_div_mul_cancel {a b c : R} (ha : a ≠ 0) (hcb : c ∣ b) : a * b / (a * c) = b / c := begin by_cases hc : c = 0, { simp [hc] }, refine eq_div_of_mul_eq_right hc (mul_left_cancel₀ ha _), rw [← mul_assoc, ← mul_div_assoc _ (mul_dvd_mul_left a hcb), mul_div_cancel_left _ (mul_ne_zero ha hc)] end lemma mul_div_mul_comm_of_dvd_dvd {a b c d : R} (hac : c ∣ a) (hbd : d ∣ b) : a * b / (c * d) = a / c * (b / d) := begin rcases eq_or_ne c 0 with rfl | hc0, { simp }, rcases eq_or_ne d 0 with rfl | hd0, { simp }, obtain ⟨k1, rfl⟩ := hac, obtain ⟨k2, rfl⟩ := hbd, rw [mul_div_cancel_left _ hc0, mul_div_cancel_left _ hd0, mul_mul_mul_comm, mul_div_cancel_left _ (mul_ne_zero hc0 hd0)], end end div end euclidean_domain instance int.euclidean_domain : euclidean_domain ℤ := { add := (+), mul := (*), one := 1, zero := 0, neg := has_neg.neg, quotient := (/), quotient_zero := int.div_zero, remainder := (%), quotient_mul_add_remainder_eq := λ a b, int.div_add_mod _ _, r := λ a b, a.nat_abs < b.nat_abs, r_well_founded := measure_wf (λ a, int.nat_abs a), remainder_lt := λ a b b0, int.coe_nat_lt.1 $ by { rw [int.nat_abs_of_nonneg (int.mod_nonneg _ b0), ← int.abs_eq_nat_abs], exact int.mod_lt _ b0 }, mul_left_not_lt := λ a b b0, not_lt_of_ge $ by {rw [← mul_one a.nat_abs, int.nat_abs_mul], exact mul_le_mul_of_nonneg_left (int.nat_abs_pos_of_ne_zero b0) (nat.zero_le _) }, .. int.comm_ring, .. int.nontrivial } @[priority 100] -- see Note [lower instance priority] instance field.to_euclidean_domain {K : Type u} [field K] : euclidean_domain K := { add := (+), mul := (*), one := 1, zero := 0, neg := has_neg.neg, quotient := (/), remainder := λ a b, a - a * b / b, quotient_zero := div_zero, quotient_mul_add_remainder_eq := λ a b, by { classical, by_cases b = 0; simp [h, mul_div_cancel'] }, r := λ a b, a = 0 ∧ b ≠ 0, r_well_founded := well_founded.intro $ λ a, acc.intro _ $ λ b ⟨hb, hna⟩, acc.intro _ $ λ c ⟨hc, hnb⟩, false.elim $ hnb hb, remainder_lt := λ a b hnb, by simp [hnb], mul_left_not_lt := λ a b hnb ⟨hab, hna⟩, or.cases_on (mul_eq_zero.1 hab) hna hnb, .. ‹field K› }
76fd328da4937aaca04b75958eed18d2360fb5c3
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/category_theory/generator.lean
e398a80921da10eeeff500f8a488924e6c24a3ff
[ "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
26,589
lean
/- Copyright (c) 2022 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel -/ import category_theory.balanced import category_theory.limits.essentially_small import category_theory.limits.opposites import category_theory.limits.shapes.zero_morphisms import category_theory.subobject.lattice import category_theory.subobject.well_powered import data.set.opposite /-! # Separating and detecting sets There are several non-equivalent notions of a generator of a category. Here, we consider two of them: * We say that `𝒢` is a separating set if the functors `C(G, -)` for `G ∈ 𝒢` are collectively faithful, i.e., if `h ≫ f = h ≫ g` for all `h` with domain in `𝒢` implies `f = g`. * We say that `𝒢` is a detecting set if the functors `C(G, -)` collectively reflect isomorphisms, i.e., if any `h` with domain in `𝒢` uniquely factors through `f`, then `f` is an isomorphism. There are, of course, also the dual notions of coseparating and codetecting sets. ## Main results We * define predicates `is_separating`, `is_coseparating`, `is_detecting` and `is_codetecting` on sets of objects; * show that separating and coseparating are dual notions; * show that detecting and codetecting are dual notions; * show that if `C` has equalizers, then detecting implies separating; * show that if `C` has coequalizers, then codetecting implies separating; * show that if `C` is balanced, then separating implies detecting and coseparating implies codetecting; * show that `∅` is separating if and only if `∅` is coseparating if and only if `C` is thin; * show that `∅` is detecting if and only if `∅` is codetecting if and only if `C` is a groupoid; * define predicates `is_separator`, `is_coseparator`, `is_detector` and `is_codetector` as the singleton counterparts to the definitions for sets above and restate the above results in this situation; * show that `G` is a separator if and only if `coyoneda.obj (op G)` is faithful (and the dual); * show that `G` is a detector if and only if `coyoneda.obj (op G)` reflects isomorphisms (and the dual). ## Future work * We currently don't have any examples yet. * We will want typeclasses `has_separator C` and similar. -/ universes w v u open category_theory.limits opposite namespace category_theory variables {C : Type u} [category.{v} C] /-- We say that `𝒢` is a separating set if the functors `C(G, -)` for `G ∈ 𝒢` are collectively faithful, i.e., if `h ≫ f = h ≫ g` for all `h` with domain in `𝒢` implies `f = g`. -/ def is_separating (𝒢 : set C) : Prop := ∀ ⦃X Y : C⦄ (f g : X ⟶ Y), (∀ (G ∈ 𝒢) (h : G ⟶ X), h ≫ f = h ≫ g) → f = g /-- We say that `𝒢` is a coseparating set if the functors `C(-, G)` for `G ∈ 𝒢` are collectively faithful, i.e., if `f ≫ h = g ≫ h` for all `h` with codomain in `𝒢` implies `f = g`. -/ def is_coseparating (𝒢 : set C) : Prop := ∀ ⦃X Y : C⦄ (f g : X ⟶ Y), (∀ (G ∈ 𝒢) (h : Y ⟶ G), f ≫ h = g ≫ h) → f = g /-- We say that `𝒢` is a detecting set if the functors `C(G, -)` collectively reflect isomorphisms, i.e., if any `h` with domain in `𝒢` uniquely factors through `f`, then `f` is an isomorphism. -/ def is_detecting (𝒢 : set C) : Prop := ∀ ⦃X Y : C⦄ (f : X ⟶ Y), (∀ (G ∈ 𝒢) (h : G ⟶ Y), ∃! (h' : G ⟶ X), h' ≫ f = h) → is_iso f /-- We say that `𝒢` is a codetecting set if the functors `C(-, G)` collectively reflect isomorphisms, i.e., if any `h` with codomain in `G` uniquely factors through `f`, then `f` is an isomorphism. -/ def is_codetecting (𝒢 : set C) : Prop := ∀ ⦃X Y : C⦄ (f : X ⟶ Y), (∀ (G ∈ 𝒢) (h : X ⟶ G), ∃! (h' : Y ⟶ G), f ≫ h' = h) → is_iso f section dual lemma is_separating_op_iff (𝒢 : set C) : is_separating 𝒢.op ↔ is_coseparating 𝒢 := begin refine ⟨λ h𝒢 X Y f g hfg, _, λ h𝒢 X Y f g hfg, _⟩, { refine quiver.hom.op_inj (h𝒢 _ _ (λ G hG h, quiver.hom.unop_inj _)), simpa only [unop_comp, quiver.hom.unop_op] using hfg _ (set.mem_op.1 hG) _ }, { refine quiver.hom.unop_inj (h𝒢 _ _ (λ G hG h, quiver.hom.op_inj _)), simpa only [op_comp, quiver.hom.op_unop] using hfg _ (set.op_mem_op.2 hG) _ } end lemma is_coseparating_op_iff (𝒢 : set C) : is_coseparating 𝒢.op ↔ is_separating 𝒢 := begin refine ⟨λ h𝒢 X Y f g hfg, _, λ h𝒢 X Y f g hfg, _⟩, { refine quiver.hom.op_inj (h𝒢 _ _ (λ G hG h, quiver.hom.unop_inj _)), simpa only [unop_comp, quiver.hom.unop_op] using hfg _ (set.mem_op.1 hG) _ }, { refine quiver.hom.unop_inj (h𝒢 _ _ (λ G hG h, quiver.hom.op_inj _)), simpa only [op_comp, quiver.hom.op_unop] using hfg _ (set.op_mem_op.2 hG) _ } end lemma is_coseparating_unop_iff (𝒢 : set Cᵒᵖ) : is_coseparating 𝒢.unop ↔ is_separating 𝒢 := by rw [← is_separating_op_iff, set.unop_op] lemma is_separating_unop_iff (𝒢 : set Cᵒᵖ) : is_separating 𝒢.unop ↔ is_coseparating 𝒢 := by rw [← is_coseparating_op_iff, set.unop_op] lemma is_detecting_op_iff (𝒢 : set C) : is_detecting 𝒢.op ↔ is_codetecting 𝒢 := begin refine ⟨λ h𝒢 X Y f hf, _, λ h𝒢 X Y f hf, _⟩, { refine (is_iso_op_iff _).1 (h𝒢 _ (λ G hG h, _)), obtain ⟨t, ht, ht'⟩ := hf (unop G) (set.mem_op.1 hG) h.unop, exact ⟨t.op, quiver.hom.unop_inj ht, λ y hy, quiver.hom.unop_inj (ht' _ (quiver.hom.op_inj hy))⟩ }, { refine (is_iso_unop_iff _).1 (h𝒢 _ (λ G hG h, _)), obtain ⟨t, ht, ht'⟩ := hf (op G) (set.op_mem_op.2 hG) h.op, refine ⟨t.unop, quiver.hom.op_inj ht, λ y hy, quiver.hom.op_inj (ht' _ _)⟩, exact quiver.hom.unop_inj (by simpa only using hy) } end lemma is_codetecting_op_iff (𝒢 : set C) : is_codetecting 𝒢.op ↔ is_detecting 𝒢 := begin refine ⟨λ h𝒢 X Y f hf, _, λ h𝒢 X Y f hf, _⟩, { refine (is_iso_op_iff _).1 (h𝒢 _ (λ G hG h, _)), obtain ⟨t, ht, ht'⟩ := hf (unop G) (set.mem_op.1 hG) h.unop, exact ⟨t.op, quiver.hom.unop_inj ht, λ y hy, quiver.hom.unop_inj (ht' _ (quiver.hom.op_inj hy))⟩ }, { refine (is_iso_unop_iff _).1 (h𝒢 _ (λ G hG h, _)), obtain ⟨t, ht, ht'⟩ := hf (op G) (set.op_mem_op.2 hG) h.op, refine ⟨t.unop, quiver.hom.op_inj ht, λ y hy, quiver.hom.op_inj (ht' _ _)⟩, exact quiver.hom.unop_inj (by simpa only using hy) } end lemma is_detecting_unop_iff (𝒢 : set Cᵒᵖ) : is_detecting 𝒢.unop ↔ is_codetecting 𝒢 := by rw [← is_codetecting_op_iff, set.unop_op] lemma is_codetecting_unop_iff {𝒢 : set Cᵒᵖ} : is_codetecting 𝒢.unop ↔ is_detecting 𝒢 := by rw [← is_detecting_op_iff, set.unop_op] end dual lemma is_detecting.is_separating [has_equalizers C] {𝒢 : set C} (h𝒢 : is_detecting 𝒢) : is_separating 𝒢 := λ X Y f g hfg, have is_iso (equalizer.ι f g), from h𝒢 _ (λ G hG h, equalizer.exists_unique _ (hfg _ hG _)), by exactI eq_of_epi_equalizer section local attribute [instance] has_equalizers_opposite lemma is_codetecting.is_coseparating [has_coequalizers C] {𝒢 : set C} : is_codetecting 𝒢 → is_coseparating 𝒢 := by simpa only [← is_separating_op_iff, ← is_detecting_op_iff] using is_detecting.is_separating end lemma is_separating.is_detecting [balanced C] {𝒢 : set C} (h𝒢 : is_separating 𝒢) : is_detecting 𝒢 := begin intros X Y f hf, refine (is_iso_iff_mono_and_epi _).2 ⟨⟨λ Z g h hgh, h𝒢 _ _ (λ G hG i, _)⟩, ⟨λ Z g h hgh, _⟩⟩, { obtain ⟨t, -, ht⟩ := hf G hG (i ≫ g ≫ f), rw [ht (i ≫ g) (category.assoc _ _ _), ht (i ≫ h) (hgh.symm ▸ category.assoc _ _ _)] }, { refine h𝒢 _ _ (λ G hG i, _), obtain ⟨t, rfl, -⟩ := hf G hG i, rw [category.assoc, hgh, category.assoc] } end section local attribute [instance] balanced_opposite lemma is_coseparating.is_codetecting [balanced C] {𝒢 : set C} : is_coseparating 𝒢 → is_codetecting 𝒢 := by simpa only [← is_detecting_op_iff, ← is_separating_op_iff] using is_separating.is_detecting end lemma is_detecting_iff_is_separating [has_equalizers C] [balanced C] (𝒢 : set C) : is_detecting 𝒢 ↔ is_separating 𝒢 := ⟨is_detecting.is_separating, is_separating.is_detecting⟩ lemma is_codetecting_iff_is_coseparating [has_coequalizers C] [balanced C] {𝒢 : set C} : is_codetecting 𝒢 ↔ is_coseparating 𝒢 := ⟨is_codetecting.is_coseparating, is_coseparating.is_codetecting⟩ section mono lemma is_separating.mono {𝒢 : set C} (h𝒢 : is_separating 𝒢) {ℋ : set C} (h𝒢ℋ : 𝒢 ⊆ ℋ) : is_separating ℋ := λ X Y f g hfg, h𝒢 _ _ $ λ G hG h, hfg _ (h𝒢ℋ hG) _ lemma is_coseparating.mono {𝒢 : set C} (h𝒢 : is_coseparating 𝒢) {ℋ : set C} (h𝒢ℋ : 𝒢 ⊆ ℋ) : is_coseparating ℋ := λ X Y f g hfg, h𝒢 _ _ $ λ G hG h, hfg _ (h𝒢ℋ hG) _ lemma is_detecting.mono {𝒢 : set C} (h𝒢 : is_detecting 𝒢) {ℋ : set C} (h𝒢ℋ : 𝒢 ⊆ ℋ) : is_detecting ℋ := λ X Y f hf, h𝒢 _ $ λ G hG h, hf _ (h𝒢ℋ hG) _ lemma is_codetecting.mono {𝒢 : set C} (h𝒢 : is_codetecting 𝒢) {ℋ : set C} (h𝒢ℋ : 𝒢 ⊆ ℋ) : is_codetecting ℋ := λ X Y f hf, h𝒢 _ $ λ G hG h, hf _ (h𝒢ℋ hG) _ end mono section empty lemma thin_of_is_separating_empty (h : is_separating (∅ : set C)) (X Y : C) : subsingleton (X ⟶ Y) := ⟨λ f g, h _ _ $ λ G, false.elim⟩ lemma is_separating_empty_of_thin [∀ X Y : C, subsingleton (X ⟶ Y)] : is_separating (∅ : set C) := λ X Y f g hfg, subsingleton.elim _ _ lemma thin_of_is_coseparating_empty (h : is_coseparating (∅ : set C)) (X Y : C) : subsingleton (X ⟶ Y) := ⟨λ f g, h _ _ $ λ G, false.elim⟩ lemma is_coseparating_empty_of_thin [∀ X Y : C, subsingleton (X ⟶ Y)] : is_coseparating (∅ : set C) := λ X Y f g hfg, subsingleton.elim _ _ lemma groupoid_of_is_detecting_empty (h : is_detecting (∅ : set C)) {X Y : C} (f : X ⟶ Y) : is_iso f := h _ $ λ G, false.elim lemma is_detecting_empty_of_groupoid [∀ {X Y : C} (f : X ⟶ Y), is_iso f] : is_detecting (∅ : set C) := λ X Y f hf, infer_instance lemma groupoid_of_is_codetecting_empty (h : is_codetecting (∅ : set C)) {X Y : C} (f : X ⟶ Y) : is_iso f := h _ $ λ G, false.elim lemma is_codetecting_empty_of_groupoid [∀ {X Y : C} (f : X ⟶ Y), is_iso f] : is_codetecting (∅ : set C) := λ X Y f hf, infer_instance end empty lemma is_separating_iff_epi (𝒢 : set C) [Π (A : C), has_coproduct (λ f : Σ G : 𝒢, (G : C) ⟶ A, (f.1 : C))] : is_separating 𝒢 ↔ ∀ A : C, epi (sigma.desc (@sigma.snd 𝒢 (λ G, (G : C) ⟶ A))) := begin refine ⟨λ h A, ⟨λ Z u v huv, h _ _ (λ G hG f, _)⟩, λ h X Y f g hh, _⟩, { simpa using (sigma.ι (λ f : Σ G : 𝒢, (G : C) ⟶ A, (f.1 : C)) ⟨⟨G, hG⟩, f⟩) ≫= huv }, { haveI := h X, refine (cancel_epi (sigma.desc (@sigma.snd 𝒢 (λ G, (G : C) ⟶ X)))).1 (colimit.hom_ext (λ j, _)), simpa using hh j.as.1.1 j.as.1.2 j.as.2 } end lemma is_coseparating_iff_mono (𝒢 : set C) [Π (A : C), has_product (λ f : Σ G : 𝒢, A ⟶ (G : C), (f.1 : C))] : is_coseparating 𝒢 ↔ ∀ A : C, mono (pi.lift (@sigma.snd 𝒢 (λ G, A ⟶ (G : C)))) := begin refine ⟨λ h A, ⟨λ Z u v huv, h _ _ (λ G hG f, _)⟩, λ h X Y f g hh, _⟩, { simpa using huv =≫ (pi.π (λ f : Σ G : 𝒢, A ⟶ (G : C), (f.1 : C)) ⟨⟨G, hG⟩, f⟩) }, { haveI := h Y, refine (cancel_mono (pi.lift (@sigma.snd 𝒢 (λ G, Y ⟶ (G : C))))).1 (limit.hom_ext (λ j, _)), simpa using hh j.as.1.1 j.as.1.2 j.as.2 } end /-- An ingredient of the proof of the Special Adjoint Functor Theorem: a complete well-powered category with a small coseparating set has an initial object. In fact, it follows from the Special Adjoint Functor Theorem that `C` is already cocomplete. -/ lemma has_initial_of_is_cosepatating [well_powered C] [has_limits C] {𝒢 : set C} [small.{v} 𝒢] (h𝒢 : is_coseparating 𝒢) : has_initial C := begin haveI := has_products_of_shape_of_small C 𝒢, haveI := λ A, has_products_of_shape_of_small.{v} C (Σ G : 𝒢, A ⟶ (G : C)), letI := complete_lattice_of_complete_semilattice_Inf (subobject (pi_obj (coe : 𝒢 → C))), suffices : ∀ A : C, unique (((⊥ : subobject (pi_obj (coe : 𝒢 → C))) : C) ⟶ A), { exactI has_initial_of_unique ((⊥ : subobject (pi_obj (coe : 𝒢 → C))) : C) }, refine λ A, ⟨⟨_⟩, λ f, _⟩, { let s := pi.lift (λ f : Σ G : 𝒢, A ⟶ (G : C), id (pi.π (coe : 𝒢 → C)) f.1), let t := pi.lift (@sigma.snd 𝒢 (λ G, A ⟶ (G : C))), haveI : mono t := (is_coseparating_iff_mono 𝒢).1 h𝒢 A, exact subobject.of_le_mk _ (pullback.fst : pullback s t ⟶ _) bot_le ≫ pullback.snd }, { generalize : default = g, suffices : is_split_epi (equalizer.ι f g), { exactI eq_of_epi_equalizer }, exact is_split_epi.mk' ⟨subobject.of_le_mk _ (equalizer.ι f g ≫ subobject.arrow _) bot_le, by { ext, simp }⟩ } end /-- An ingredient of the proof of the Special Adjoint Functor Theorem: a cocomplete well-copowered category with a small separating set has a terminal object. In fact, it follows from the Special Adjoint Functor Theorem that `C` is already complete. -/ lemma has_terminal_of_is_separating [well_powered Cᵒᵖ] [has_colimits C] {𝒢 : set C} [small.{v} 𝒢] (h𝒢 : is_separating 𝒢) : has_terminal C := begin haveI : has_limits Cᵒᵖ := has_limits_op_of_has_colimits, haveI : small.{v} 𝒢.op := small_of_injective (set.op_equiv_self 𝒢).injective, haveI : has_initial Cᵒᵖ := has_initial_of_is_cosepatating ((is_coseparating_op_iff _).2 h𝒢), exact has_terminal_of_has_initial_op end section well_powered namespace subobject lemma eq_of_le_of_is_detecting {𝒢 : set C} (h𝒢 : is_detecting 𝒢) {X : C} (P Q : subobject X) (h₁ : P ≤ Q) (h₂ : ∀ (G ∈ 𝒢) {f : G ⟶ X}, Q.factors f → P.factors f) : P = Q := begin suffices : is_iso (of_le _ _ h₁), { exactI le_antisymm h₁ (le_of_comm (inv (of_le _ _ h₁)) (by simp)) }, refine h𝒢 _ (λ G hG f, _), have : P.factors (f ≫ Q.arrow) := h₂ _ hG ((factors_iff _ _).2 ⟨_, rfl⟩), refine ⟨factor_thru _ _ this, _, λ g (hg : g ≫ _ = f), _⟩, { simp only [← cancel_mono Q.arrow, category.assoc, of_le_arrow, factor_thru_arrow] }, { simp only [← cancel_mono (subobject.of_le _ _ h₁), ← cancel_mono Q.arrow, hg, category.assoc, of_le_arrow, factor_thru_arrow] } end lemma inf_eq_of_is_detecting [has_pullbacks C] {𝒢 : set C} (h𝒢 : is_detecting 𝒢) {X : C} (P Q : subobject X) (h : ∀ (G ∈ 𝒢) {f : G ⟶ X}, P.factors f → Q.factors f) : P ⊓ Q = P := eq_of_le_of_is_detecting h𝒢 _ _ _root_.inf_le_left (λ G hG f hf, (inf_factors _).2 ⟨hf, h _ hG hf⟩) lemma eq_of_is_detecting [has_pullbacks C] {𝒢 : set C} (h𝒢 : is_detecting 𝒢) {X : C} (P Q : subobject X) (h : ∀ (G ∈ 𝒢) {f : G ⟶ X}, P.factors f ↔ Q.factors f) : P = Q := calc P = P ⊓ Q : eq.symm $ inf_eq_of_is_detecting h𝒢 _ _ $ λ G hG f hf, (h G hG).1 hf ... = Q ⊓ P : inf_comm ... = Q : inf_eq_of_is_detecting h𝒢 _ _ $ λ G hG f hf, (h G hG).2 hf end subobject /-- A category with pullbacks and a small detecting set is well-powered. -/ lemma well_powered_of_is_detecting [has_pullbacks C] {𝒢 : set C} [small.{v} 𝒢] (h𝒢 : is_detecting 𝒢) : well_powered C := ⟨λ X, @small_of_injective _ _ _ (λ P : subobject X, { f : Σ G : 𝒢, G.1 ⟶ X | P.factors f.2 }) $ λ P Q h, subobject.eq_of_is_detecting h𝒢 _ _ (by simpa [set.ext_iff] using h)⟩ end well_powered /-- We say that `G` is a separator if the functor `C(G, -)` is faithful. -/ def is_separator (G : C) : Prop := is_separating ({G} : set C) /-- We say that `G` is a coseparator if the functor `C(-, G)` is faithful. -/ def is_coseparator (G : C) : Prop := is_coseparating ({G} : set C) /-- We say that `G` is a detector if the functor `C(G, -)` reflects isomorphisms. -/ def is_detector (G : C) : Prop := is_detecting ({G} : set C) /-- We say that `G` is a codetector if the functor `C(-, G)` reflects isomorphisms. -/ def is_codetector (G : C) : Prop := is_codetecting ({G} : set C) section dual lemma is_separator_op_iff (G : C) : is_separator (op G) ↔ is_coseparator G := by rw [is_separator, is_coseparator, ← is_separating_op_iff, set.singleton_op] lemma is_coseparator_op_iff (G : C) : is_coseparator (op G) ↔ is_separator G := by rw [is_separator, is_coseparator, ← is_coseparating_op_iff, set.singleton_op] lemma is_coseparator_unop_iff (G : Cᵒᵖ) : is_coseparator (unop G) ↔ is_separator G := by rw [is_separator, is_coseparator, ← is_coseparating_unop_iff, set.singleton_unop] lemma is_separator_unop_iff (G : Cᵒᵖ) : is_separator (unop G) ↔ is_coseparator G := by rw [is_separator, is_coseparator, ← is_separating_unop_iff, set.singleton_unop] lemma is_detector_op_iff (G : C) : is_detector (op G) ↔ is_codetector G := by rw [is_detector, is_codetector, ← is_detecting_op_iff, set.singleton_op] lemma is_codetector_op_iff (G : C) : is_codetector (op G) ↔ is_detector G := by rw [is_detector, is_codetector, ← is_codetecting_op_iff, set.singleton_op] lemma is_codetector_unop_iff (G : Cᵒᵖ) : is_codetector (unop G) ↔ is_detector G := by rw [is_detector, is_codetector, ← is_codetecting_unop_iff, set.singleton_unop] lemma is_detector_unop_iff (G : Cᵒᵖ) : is_detector (unop G) ↔ is_codetector G := by rw [is_detector, is_codetector, ← is_detecting_unop_iff, set.singleton_unop] end dual lemma is_detector.is_separator [has_equalizers C] {G : C} : is_detector G → is_separator G := is_detecting.is_separating lemma is_codetector.is_coseparator [has_coequalizers C] {G : C} : is_codetector G → is_coseparator G := is_codetecting.is_coseparating lemma is_separator.is_detector [balanced C] {G : C} : is_separator G → is_detector G := is_separating.is_detecting lemma is_cospearator.is_codetector [balanced C] {G : C} : is_coseparator G → is_codetector G := is_coseparating.is_codetecting lemma is_separator_def (G : C) : is_separator G ↔ ∀ ⦃X Y : C⦄ (f g : X ⟶ Y), (∀ h : G ⟶ X, h ≫ f = h ≫ g) → f = g := ⟨λ hG X Y f g hfg, hG _ _ $ λ H hH h, by { obtain rfl := set.mem_singleton_iff.1 hH, exact hfg h }, λ hG X Y f g hfg, hG _ _ $ λ h, hfg _ (set.mem_singleton _) _⟩ lemma is_separator.def {G : C} : is_separator G → ∀ ⦃X Y : C⦄ (f g : X ⟶ Y), (∀ h : G ⟶ X, h ≫ f = h ≫ g) → f = g := (is_separator_def _).1 lemma is_coseparator_def (G : C) : is_coseparator G ↔ ∀ ⦃X Y : C⦄ (f g : X ⟶ Y), (∀ h : Y ⟶ G, f ≫ h = g ≫ h) → f = g := ⟨λ hG X Y f g hfg, hG _ _ $ λ H hH h, by { obtain rfl := set.mem_singleton_iff.1 hH, exact hfg h }, λ hG X Y f g hfg, hG _ _ $ λ h, hfg _ (set.mem_singleton _) _⟩ lemma is_coseparator.def {G : C} : is_coseparator G → ∀ ⦃X Y : C⦄ (f g : X ⟶ Y), (∀ h : Y ⟶ G, f ≫ h = g ≫ h) → f = g := (is_coseparator_def _).1 lemma is_detector_def (G : C) : is_detector G ↔ ∀ ⦃X Y : C⦄ (f : X ⟶ Y), (∀ h : G ⟶ Y, ∃! h', h' ≫ f = h) → is_iso f := ⟨λ hG X Y f hf, hG _ $ λ H hH h, by { obtain rfl := set.mem_singleton_iff.1 hH, exact hf h }, λ hG X Y f hf, hG _ $ λ h, hf _ (set.mem_singleton _) _⟩ lemma is_detector.def {G : C} : is_detector G → ∀ ⦃X Y : C⦄ (f : X ⟶ Y), (∀ h : G ⟶ Y, ∃! h', h' ≫ f = h) → is_iso f := (is_detector_def _).1 lemma is_codetector_def (G : C) : is_codetector G ↔ ∀ ⦃X Y : C⦄ (f : X ⟶ Y), (∀ h : X ⟶ G, ∃! h', f ≫ h' = h) → is_iso f := ⟨λ hG X Y f hf, hG _ $ λ H hH h, by { obtain rfl := set.mem_singleton_iff.1 hH, exact hf h }, λ hG X Y f hf, hG _ $ λ h, hf _ (set.mem_singleton _) _⟩ lemma is_codetector.def {G : C} : is_codetector G → ∀ ⦃X Y : C⦄ (f : X ⟶ Y), (∀ h : X ⟶ G, ∃! h', f ≫ h' = h) → is_iso f := (is_codetector_def _).1 lemma is_separator_iff_faithful_coyoneda_obj (G : C) : is_separator G ↔ faithful (coyoneda.obj (op G)) := ⟨λ hG, ⟨λ X Y f g hfg, hG.def _ _ (congr_fun hfg)⟩, λ h, (is_separator_def _).2 $ λ X Y f g hfg, by exactI (coyoneda.obj (op G)).map_injective (funext hfg)⟩ lemma is_coseparator_iff_faithful_yoneda_obj (G : C) : is_coseparator G ↔ faithful (yoneda.obj G) := ⟨λ hG, ⟨λ X Y f g hfg, quiver.hom.unop_inj (hG.def _ _ (congr_fun hfg))⟩, λ h, (is_coseparator_def _).2 $ λ X Y f g hfg, quiver.hom.op_inj $ by exactI (yoneda.obj G).map_injective (funext hfg)⟩ lemma is_separator_iff_epi (G : C) [Π A : C, has_coproduct (λ (f : G ⟶ A), G)] : is_separator G ↔ ∀ (A : C), epi (sigma.desc (λ (f : G ⟶ A), f)) := begin rw is_separator_def, refine ⟨λ h A, ⟨λ Z u v huv, h _ _ (λ i, _)⟩, λ h X Y f g hh, _⟩, { simpa using (sigma.ι _ i) ≫= huv }, { haveI := h X, refine (cancel_epi (sigma.desc (λ (f : G ⟶ X), f))).1 (colimit.hom_ext (λ j, _)), simpa using hh j.as } end lemma is_coseparator_iff_mono (G : C) [Π A : C, has_product (λ (f : A ⟶ G), G)] : is_coseparator G ↔ ∀ (A : C), mono (pi.lift (λ (f : A ⟶ G), f)) := begin rw is_coseparator_def, refine ⟨λ h A, ⟨λ Z u v huv, h _ _ (λ i, _)⟩, λ h X Y f g hh, _⟩, { simpa using huv =≫ (pi.π _ i) }, { haveI := h Y, refine (cancel_mono (pi.lift (λ (f : Y ⟶ G), f))).1 (limit.hom_ext (λ j, _)), simpa using hh j.as } end section zero_morphisms variables [has_zero_morphisms C] lemma is_separator_coprod (G H : C) [has_binary_coproduct G H] : is_separator (G ⨿ H) ↔ is_separating ({G, H} : set C) := begin refine ⟨λ h X Y u v huv, _, λ h, (is_separator_def _).2 (λ X Y u v huv, h _ _ (λ Z hZ g, _))⟩, { refine h.def _ _ (λ g, coprod.hom_ext _ _), { simpa using huv G (by simp) (coprod.inl ≫ g) }, { simpa using huv H (by simp) (coprod.inr ≫ g) } }, { simp only [set.mem_insert_iff, set.mem_singleton_iff] at hZ, unfreezingI { rcases hZ with rfl|rfl }, { simpa using coprod.inl ≫= huv (coprod.desc g 0) }, { simpa using coprod.inr ≫= huv (coprod.desc 0 g) } } end lemma is_separator_coprod_of_is_separator_left (G H : C) [has_binary_coproduct G H] (hG : is_separator G) : is_separator (G ⨿ H) := (is_separator_coprod _ _).2 $ is_separating.mono hG $ by simp lemma is_separator_coprod_of_is_separator_right (G H : C) [has_binary_coproduct G H] (hH : is_separator H) : is_separator (G ⨿ H) := (is_separator_coprod _ _).2 $ is_separating.mono hH $ by simp lemma is_separator_sigma {β : Type w} (f : β → C) [has_coproduct f] : is_separator (∐ f) ↔ is_separating (set.range f) := begin refine ⟨λ h X Y u v huv, _, λ h, (is_separator_def _).2 (λ X Y u v huv, h _ _ (λ Z hZ g, _))⟩, { refine h.def _ _ (λ g, colimit.hom_ext (λ b, _)), simpa using huv (f b.as) (by simp) (colimit.ι (discrete.functor f) _ ≫ g) }, { obtain ⟨b, rfl⟩ := set.mem_range.1 hZ, classical, simpa using sigma.ι f b ≫= huv (sigma.desc (pi.single b g)) } end lemma is_separator_sigma_of_is_separator {β : Type w} (f : β → C) [has_coproduct f] (b : β) (hb : is_separator (f b)) : is_separator (∐ f) := (is_separator_sigma _).2 $ is_separating.mono hb $ by simp lemma is_coseparator_prod (G H : C) [has_binary_product G H] : is_coseparator (G ⨯ H) ↔ is_coseparating ({G, H} : set C) := begin refine ⟨λ h X Y u v huv, _, λ h, (is_coseparator_def _).2 (λ X Y u v huv, h _ _ (λ Z hZ g, _))⟩, { refine h.def _ _ (λ g, prod.hom_ext _ _), { simpa using huv G (by simp) (g ≫ limits.prod.fst) }, { simpa using huv H (by simp) (g ≫ limits.prod.snd) } }, { simp only [set.mem_insert_iff, set.mem_singleton_iff] at hZ, unfreezingI { rcases hZ with rfl|rfl }, { simpa using huv (prod.lift g 0) =≫ limits.prod.fst }, { simpa using huv (prod.lift 0 g) =≫ limits.prod.snd } } end lemma is_coseparator_prod_of_is_coseparator_left (G H : C) [has_binary_product G H] (hG : is_coseparator G) : is_coseparator (G ⨯ H) := (is_coseparator_prod _ _).2 $ is_coseparating.mono hG $ by simp lemma is_coseparator_prod_of_is_coseparator_right (G H : C) [has_binary_product G H] (hH : is_coseparator H) : is_coseparator (G ⨯ H) := (is_coseparator_prod _ _).2 $ is_coseparating.mono hH $ by simp lemma is_coseparator_pi {β : Type w} (f : β → C) [has_product f] : is_coseparator (∏ f) ↔ is_coseparating (set.range f) := begin refine ⟨λ h X Y u v huv, _, λ h, (is_coseparator_def _).2 (λ X Y u v huv, h _ _ (λ Z hZ g, _))⟩, { refine h.def _ _ (λ g, limit.hom_ext (λ b, _)), simpa using huv (f b.as) (by simp) (g ≫ limit.π (discrete.functor f) _ ) }, { obtain ⟨b, rfl⟩ := set.mem_range.1 hZ, classical, simpa using huv (pi.lift (pi.single b g)) =≫ pi.π f b } end lemma is_coseparator_pi_of_is_coseparator {β : Type w} (f : β → C) [has_product f] (b : β) (hb : is_coseparator (f b)) : is_coseparator (∏ f) := (is_coseparator_pi _).2 $ is_coseparating.mono hb $ by simp end zero_morphisms lemma is_detector_iff_reflects_isomorphisms_coyoneda_obj (G : C) : is_detector G ↔ reflects_isomorphisms (coyoneda.obj (op G)) := begin refine ⟨λ hG, ⟨λ X Y f hf, hG.def _ (λ h, _)⟩, λ h, (is_detector_def _).2 (λ X Y f hf, _)⟩, { rw [is_iso_iff_bijective, function.bijective_iff_exists_unique] at hf, exact hf h }, { suffices : is_iso ((coyoneda.obj (op G)).map f), { exactI @is_iso_of_reflects_iso _ _ _ _ _ _ _ (coyoneda.obj (op G)) _ h }, rwa [is_iso_iff_bijective, function.bijective_iff_exists_unique] } end lemma is_codetector_iff_reflects_isomorphisms_yoneda_obj (G : C) : is_codetector G ↔ reflects_isomorphisms (yoneda.obj G) := begin refine ⟨λ hG, ⟨λ X Y f hf, _ ⟩, λ h, (is_codetector_def _).2 (λ X Y f hf, _)⟩, { refine (is_iso_unop_iff _).1 (hG.def _ _), rwa [is_iso_iff_bijective, function.bijective_iff_exists_unique] at hf }, { rw ← is_iso_op_iff, suffices : is_iso ((yoneda.obj G).map f.op), { exactI @is_iso_of_reflects_iso _ _ _ _ _ _ _ (yoneda.obj G) _ h }, rwa [is_iso_iff_bijective, function.bijective_iff_exists_unique] } end lemma well_powered_of_is_detector [has_pullbacks C] (G : C) (hG : is_detector G) : well_powered C := well_powered_of_is_detecting hG end category_theory
ad13f236ec244d2ff7b59390758356c6087ebc4d
4727251e0cd73359b15b664c3170e5d754078599
/src/set_theory/game/pgame.lean
00924a6c460fb056bab7bcd497157732fd438d14
[ "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
51,011
lean
/- Copyright (c) 2019 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton, Mario Carneiro, Isabel Longbottom, Scott Morrison -/ import data.fin.basic import data.list.basic import logic.relation /-! # Combinatorial (pre-)games. The basic theory of combinatorial games, following Conway's book `On Numbers and Games`. We construct "pregames", define an ordering and arithmetic operations on them, then show that the operations descend to "games", defined via the equivalence relation `p ≈ q ↔ p ≤ q ∧ q ≤ p`. The surreal numbers will be built as a quotient of a subtype of pregames. A pregame (`pgame` below) is axiomatised via an inductive type, whose sole constructor takes two types (thought of as indexing the possible moves for the players Left and Right), and a pair of functions out of these types to `pgame` (thought of as describing the resulting game after making a move). Combinatorial games themselves, as a quotient of pregames, are constructed in `game.lean`. ## Conway induction By construction, the induction principle for pregames is exactly "Conway induction". That is, to prove some predicate `pgame → Prop` holds for all pregames, it suffices to prove that for every pregame `g`, if the predicate holds for every game resulting from making a move, then it also holds for `g`. While it is often convenient to work "by induction" on pregames, in some situations this becomes awkward, so we also define accessor functions `pgame.left_moves`, `pgame.right_moves`, `pgame.move_left` and `pgame.move_right`. There is a relation `pgame.subsequent p q`, saying that `p` can be reached by playing some non-empty sequence of moves starting from `q`, an instance `well_founded subsequent`, and a local tactic `pgame_wf_tac` which is helpful for discharging proof obligations in inductive proofs relying on this relation. ## Order properties Pregames have both a `≤` and a `<` relation, which are related in quite a subtle way. In particular, it is worth noting that in Lean's (perhaps unfortunate?) definition of a `preorder`, we have `lt_iff_le_not_le : ∀ a b : α, a < b ↔ (a ≤ b ∧ ¬ b ≤ a)`, but this is _not_ satisfied by the usual `≤` and `<` relations on pregames. (It is satisfied once we restrict to the surreal numbers.) In particular, `<` is not transitive; there is an example below showing `0 < star ∧ star < 0`. We do have ``` theorem not_le {x y : pgame} : ¬ x ≤ y ↔ y < x := ... theorem not_lt {x y : pgame} : ¬ x < y ↔ y ≤ x := ... ``` The statement `0 ≤ x` means that Left has a good response to any move by Right; in particular, the theorem `zero_le` below states ``` 0 ≤ x ↔ ∀ j : x.right_moves, ∃ i : (x.move_right j).left_moves, 0 ≤ (x.move_right j).move_left i ``` On the other hand the statement `0 < x` means that Left has a good move right now; in particular the theorem `zero_lt` below states ``` 0 < x ↔ ∃ i : left_moves x, ∀ j : right_moves (x.move_left i), 0 < (x.move_left i).move_right j ``` The theorems `le_def`, `lt_def`, give a recursive characterisation of each relation, in terms of themselves two moves later. The theorems `le_def_lt` and `lt_def_lt` give recursive characterisations of each relation in terms of the other relation one move later. We define an equivalence relation `equiv p q ↔ p ≤ q ∧ q ≤ p`. Later, games will be defined as the quotient by this relation. ## Algebraic structures We next turn to defining the operations necessary to make games into a commutative additive group. Addition is defined for $x = \{xL | xR\}$ and $y = \{yL | yR\}$ by $x + y = \{xL + y, x + yL | xR + y, x + yR\}$. Negation is defined by $\{xL | xR\} = \{-xR | -xL\}$. The order structures interact in the expected way with addition, so we have ``` theorem le_iff_sub_nonneg {x y : pgame} : x ≤ y ↔ 0 ≤ y - x := sorry theorem lt_iff_sub_pos {x y : pgame} : x < y ↔ 0 < y - x := sorry ``` We show that these operations respect the equivalence relation, and hence descend to games. At the level of games, these operations satisfy all the laws of a commutative group. To prove the necessary equivalence relations at the level of pregames, we introduce the notion of a `relabelling` of a game, and show, for example, that there is a relabelling between `x + (y + z)` and `(x + y) + z`. ## Future work * The theory of dominated and reversible positions, and unique normal form for short games. * Analysis of basic domineering positions. * Hex. * Temperature. * The development of surreal numbers, based on this development of combinatorial games, is still quite incomplete. ## References The material here is all drawn from * [Conway, *On numbers and games*][conway2001] An interested reader may like to formalise some of the material from * [Andreas Blass, *A game semantics for linear logic*][MR1167694] * [André Joyal, *Remarques sur la théorie des jeux à deux personnes*][joyal1997] -/ open function relation universes u /-- The type of pre-games, before we have quotiented by equivalence (`pgame.setoid`). In ZFC, a combinatorial game is constructed from two sets of combinatorial games that have been constructed at an earlier stage. To do this in type theory, we say that a pre-game is built inductively from two families of pre-games indexed over any type in Type u. The resulting type `pgame.{u}` lives in `Type (u+1)`, reflecting that it is a proper class in ZFC. -/ inductive pgame : Type (u+1) | mk : ∀ α β : Type u, (α → pgame) → (β → pgame) → pgame namespace pgame /-- The indexing type for allowable moves by Left. -/ def left_moves : pgame → Type u | (mk l _ _ _) := l /-- The indexing type for allowable moves by Right. -/ def right_moves : pgame → Type u | (mk _ r _ _) := r /-- The new game after Left makes an allowed move. -/ def move_left : Π (g : pgame), left_moves g → pgame | (mk l _ L _) := L /-- The new game after Right makes an allowed move. -/ def move_right : Π (g : pgame), right_moves g → pgame | (mk _ r _ R) := R @[simp] lemma left_moves_mk {xl xr xL xR} : (⟨xl, xr, xL, xR⟩ : pgame).left_moves = xl := rfl @[simp] lemma move_left_mk {xl xr xL xR} : (⟨xl, xr, xL, xR⟩ : pgame).move_left = xL := rfl @[simp] lemma right_moves_mk {xl xr xL xR} : (⟨xl, xr, xL, xR⟩ : pgame).right_moves = xr := rfl @[simp] lemma move_right_mk {xl xr xL xR} : (⟨xl, xr, xL, xR⟩ : pgame).move_right = xR := rfl /-- Construct a pre-game from list of pre-games describing the available moves for Left and Right. -/ -- TODO define this at the level of games, as well, and perhaps also for finsets of games. def of_lists (L R : list pgame.{u}) : pgame.{u} := mk (ulift (fin L.length)) (ulift (fin R.length)) (λ i, L.nth_le i.down i.down.is_lt) (λ j, R.nth_le j.down j.down.prop) lemma left_moves_of_lists (L R : list pgame) : (of_lists L R).left_moves = ulift (fin L.length) := rfl lemma right_moves_of_lists (L R : list pgame) : (of_lists L R).right_moves = ulift (fin R.length) := rfl /-- Converts a number into a left move for `of_lists`. -/ def to_of_lists_left_moves {L R : list pgame} : fin L.length ≃ (of_lists L R).left_moves := ((equiv.cast (left_moves_of_lists L R).symm).trans equiv.ulift).symm /-- Converts a number into a right move for `of_lists`. -/ def to_of_lists_right_moves {L R : list pgame} : fin R.length ≃ (of_lists L R).right_moves := ((equiv.cast (right_moves_of_lists L R).symm).trans equiv.ulift).symm theorem of_lists_move_left {L R : list pgame} (i : fin L.length) : (of_lists L R).move_left (to_of_lists_left_moves i) = L.nth_le i i.is_lt := rfl @[simp] theorem of_lists_move_left' {L R : list pgame} (i : (of_lists L R).left_moves) : (of_lists L R).move_left i = L.nth_le (to_of_lists_left_moves.symm i) (to_of_lists_left_moves.symm i).is_lt := rfl theorem of_lists_move_right {L R : list pgame} (i : fin R.length) : (of_lists L R).move_right (to_of_lists_right_moves i) = R.nth_le i i.is_lt := rfl @[simp] theorem of_lists_move_right' {L R : list pgame} (i : (of_lists L R).right_moves) : (of_lists L R).move_right i = R.nth_le (to_of_lists_right_moves.symm i) (to_of_lists_right_moves.symm i).is_lt := rfl /-- A variant of `pgame.rec_on` expressed in terms of `pgame.move_left` and `pgame.move_right`. Both this and `pgame.rec_on` describe Conway induction on games. -/ @[elab_as_eliminator] def move_rec_on {C : pgame → Sort*} (x : pgame) (IH : ∀ (y : pgame), (∀ i, C (y.move_left i)) → (∀ j, C (y.move_right j)) → C y) : C x := x.rec_on $ λ yl yr yL yR, IH (mk yl yr yL yR) /-- `is_option x y` means that `x` is either a left or right option for `y`. -/ @[mk_iff] inductive is_option : pgame → pgame → Prop | move_left {x : pgame} (i : x.left_moves) : is_option (x.move_left i) x | move_right {x : pgame} (i : x.right_moves) : is_option (x.move_right i) x theorem is_option.mk_left {xl xr : Type u} (xL : xl → pgame) (xR : xr → pgame) (i : xl) : (xL i).is_option (mk xl xr xL xR) := @is_option.move_left (mk _ _ _ _) i theorem is_option.mk_right {xl xr : Type u} (xL : xl → pgame) (xR : xr → pgame) (i : xr) : (xR i).is_option (mk xl xr xL xR) := @is_option.move_right (mk _ _ _ _) i theorem wf_is_option : well_founded is_option := ⟨λ x, move_rec_on x $ λ x IHl IHr, acc.intro x $ λ y h, begin induction h with _ i _ j, { exact IHl i }, { exact IHr j } end⟩ /-- `subsequent x y` says that `x` can be obtained by playing some nonempty sequence of moves from `y`. It is the transitive closure of `is_option`. -/ def subsequent : pgame → pgame → Prop := trans_gen is_option instance : is_trans _ subsequent := trans_gen.is_trans @[trans] theorem subsequent.trans : ∀ {x y z}, subsequent x y → subsequent y z → subsequent x z := @trans_gen.trans _ _ theorem wf_subsequent : well_founded subsequent := wf_is_option.trans_gen instance : has_well_founded pgame := ⟨_, wf_subsequent⟩ lemma subsequent.move_left {x : pgame} (i : x.left_moves) : subsequent (x.move_left i) x := trans_gen.single (is_option.move_left i) lemma subsequent.move_right {x : pgame} (j : x.right_moves) : subsequent (x.move_right j) x := trans_gen.single (is_option.move_right j) lemma subsequent.mk_left {xl xr} (xL : xl → pgame) (xR : xr → pgame) (i : xl) : subsequent (xL i) (mk xl xr xL xR) := @subsequent.move_left (mk _ _ _ _) i lemma subsequent.mk_right {xl xr} (xL : xl → pgame) (xR : xr → pgame) (j : xr) : subsequent (xR j) (mk xl xr xL xR) := @subsequent.move_right (mk _ _ _ _) j /-- A local tactic for proving well-foundedness of recursive definitions involving pregames. -/ meta def pgame_wf_tac := `[solve_by_elim [psigma.lex.left, psigma.lex.right, subsequent.move_left, subsequent.move_right, subsequent.mk_left, subsequent.mk_right, subsequent.trans] { max_depth := 6 }] /-- The pre-game `zero` is defined by `0 = { | }`. -/ instance : has_zero pgame := ⟨⟨pempty, pempty, pempty.elim, pempty.elim⟩⟩ @[simp] lemma zero_left_moves : left_moves 0 = pempty := rfl @[simp] lemma zero_right_moves : right_moves 0 = pempty := rfl instance is_empty_zero_left_moves : is_empty (left_moves 0) := pempty.is_empty instance is_empty_zero_right_moves : is_empty (right_moves 0) := pempty.is_empty instance : inhabited pgame := ⟨0⟩ /-- The pre-game `one` is defined by `1 = { 0 | }`. -/ instance : has_one pgame := ⟨⟨punit, pempty, λ _, 0, pempty.elim⟩⟩ @[simp] lemma one_left_moves : left_moves 1 = punit := rfl @[simp] lemma one_move_left (x) : move_left 1 x = 0 := rfl @[simp] lemma one_right_moves : right_moves 1 = pempty := rfl instance unique_one_left_moves : unique (left_moves 1) := punit.unique instance is_empty_one_right_moves : is_empty (right_moves 1) := pempty.is_empty /-- Define simultaneously by mutual induction the `<=` and `<` relation on pre-games. The ZFC definition says that `x = {xL | xR}` is less or equal to `y = {yL | yR}` if `∀ x₁ ∈ xL, x₁ < y` and `∀ y₂ ∈ yR, x < y₂`, where `x < y` is the same as `¬ y <= x`. This is a tricky induction because it only decreases one side at a time, and it also swaps the arguments in the definition of `<`. The solution is to define `x < y` and `x <= y` simultaneously. -/ def le_lt : Π (x y : pgame), Prop × Prop | (mk xl xr xL xR) (mk yl yr yL yR) := -- the orderings of the clauses here are carefully chosen so that -- and.left/or.inl refer to moves by Left, and -- and.right/or.inr refer to moves by Right. ((∀ i : xl, (le_lt (xL i) ⟨yl, yr, yL, yR⟩).2) ∧ (∀ j : yr, (le_lt ⟨xl, xr, xL, xR⟩ (yR j)).2), (∃ i : yl, (le_lt ⟨xl, xr, xL, xR⟩ (yL i)).1) ∨ (∃ j : xr, (le_lt (xR j) ⟨yl, yr, yL, yR⟩).1)) using_well_founded { dec_tac := pgame_wf_tac } instance : has_le pgame := ⟨λ x y, (le_lt x y).1⟩ instance : has_lt pgame := ⟨λ x y, (le_lt x y).2⟩ /-- Definition of `x ≤ y` on pre-games built using the constructor. -/ @[simp] theorem mk_le_mk {xl xr xL xR yl yr yL yR} : (⟨xl, xr, xL, xR⟩ : pgame) ≤ ⟨yl, yr, yL, yR⟩ ↔ (∀ i, xL i < ⟨yl, yr, yL, yR⟩) ∧ (∀ j, (⟨xl, xr, xL, xR⟩ : pgame) < yR j) := show (le_lt _ _).1 ↔ _, by { rw le_lt, refl } /-- Definition of `x ≤ y` on pre-games, in terms of `<` -/ theorem le_def_lt {x y : pgame} : x ≤ y ↔ (∀ i : x.left_moves, x.move_left i < y) ∧ (∀ j : y.right_moves, x < y.move_right j) := by { cases x, cases y, rw mk_le_mk, refl } /-- Definition of `x < y` on pre-games built using the constructor. -/ @[simp] theorem mk_lt_mk {xl xr xL xR yl yr yL yR} : (⟨xl, xr, xL, xR⟩ : pgame) < ⟨yl, yr, yL, yR⟩ ↔ (∃ i, (⟨xl, xr, xL, xR⟩ : pgame) ≤ yL i) ∨ (∃ j, xR j ≤ ⟨yl, yr, yL, yR⟩) := show (le_lt _ _).2 ↔ _, by { rw le_lt, refl } /-- Definition of `x < y` on pre-games, in terms of `≤` -/ theorem lt_def_le {x y : pgame} : x < y ↔ (∃ i : y.left_moves, x ≤ y.move_left i) ∨ (∃ j : x.right_moves, x.move_right j ≤ y) := by { cases x, cases y, rw mk_lt_mk, refl } /-- The definition of `x ≤ y` on pre-games, in terms of `≤` two moves later. -/ theorem le_def {x y : pgame} : x ≤ y ↔ (∀ i : x.left_moves, (∃ i' : y.left_moves, x.move_left i ≤ y.move_left i') ∨ (∃ j : (x.move_left i).right_moves, (x.move_left i).move_right j ≤ y)) ∧ (∀ j : y.right_moves, (∃ i : (y.move_right j).left_moves, x ≤ (y.move_right j).move_left i) ∨ (∃ j' : x.right_moves, x.move_right j' ≤ y.move_right j)) := begin rw [le_def_lt], conv { to_lhs, simp only [lt_def_le] }, end /-- The definition of `x < y` on pre-games, in terms of `<` two moves later. -/ theorem lt_def {x y : pgame} : x < y ↔ (∃ i : y.left_moves, (∀ i' : x.left_moves, x.move_left i' < y.move_left i) ∧ (∀ j : (y.move_left i).right_moves, x < (y.move_left i).move_right j)) ∨ (∃ j : x.right_moves, (∀ i : (x.move_right j).left_moves, (x.move_right j).move_left i < y) ∧ (∀ j' : y.right_moves, x.move_right j < y.move_right j')) := begin rw [lt_def_le], conv { to_lhs, simp only [le_def_lt] }, end /-- The definition of `x ≤ 0` on pre-games, in terms of `≤ 0` two moves later. -/ theorem le_zero {x : pgame} : x ≤ 0 ↔ ∀ i : x.left_moves, ∃ j : (x.move_left i).right_moves, (x.move_left i).move_right j ≤ 0 := by { rw le_def, dsimp, simp [forall_pempty, exists_pempty] } /-- The definition of `0 ≤ x` on pre-games, in terms of `0 ≤` two moves later. -/ theorem zero_le {x : pgame} : 0 ≤ x ↔ ∀ j : x.right_moves, ∃ i : (x.move_right j).left_moves, 0 ≤ (x.move_right j).move_left i := by { rw le_def, dsimp, simp [forall_pempty, exists_pempty] } /-- The definition of `x < 0` on pre-games, in terms of `< 0` two moves later. -/ theorem lt_zero {x : pgame} : x < 0 ↔ ∃ j : x.right_moves, ∀ i : (x.move_right j).left_moves, (x.move_right j).move_left i < 0 := by { rw lt_def, dsimp, simp [forall_pempty, exists_pempty] } /-- The definition of `0 < x` on pre-games, in terms of `< x` two moves later. -/ theorem zero_lt {x : pgame} : 0 < x ↔ ∃ i : x.left_moves, ∀ j : (x.move_left i).right_moves, 0 < (x.move_left i).move_right j := by { rw lt_def, dsimp, simp [forall_pempty, exists_pempty] } @[simp] theorem le_zero_of_is_empty_left_moves (x : pgame) [is_empty x.left_moves] : x ≤ 0 := le_zero.2 is_empty_elim @[simp] theorem zero_le_of_is_empty_right_moves (x : pgame) [is_empty x.right_moves] : 0 ≤ x := zero_le.2 is_empty_elim /-- Given a right-player-wins game, provide a response to any move by left. -/ noncomputable def right_response {x : pgame} (h : x ≤ 0) (i : x.left_moves) : (x.move_left i).right_moves := classical.some $ (le_zero.1 h) i /-- Show that the response for right provided by `right_response` preserves the right-player-wins condition. -/ lemma right_response_spec {x : pgame} (h : x ≤ 0) (i : x.left_moves) : (x.move_left i).move_right (right_response h i) ≤ 0 := classical.some_spec $ (le_zero.1 h) i /-- Given a left-player-wins game, provide a response to any move by right. -/ noncomputable def left_response {x : pgame} (h : 0 ≤ x) (j : x.right_moves) : (x.move_right j).left_moves := classical.some $ (zero_le.1 h) j /-- Show that the response for left provided by `left_response` preserves the left-player-wins condition. -/ lemma left_response_spec {x : pgame} (h : 0 ≤ x) (j : x.right_moves) : 0 ≤ (x.move_right j).move_left (left_response h j) := classical.some_spec $ (zero_le.1 h) j theorem lt_of_le_mk {xl xr xL xR y i} : (⟨xl, xr, xL, xR⟩ : pgame) ≤ y → xL i < y := by { cases y, rw mk_le_mk, tauto } theorem lt_of_mk_le {x : pgame} {yl yr yL yR i} : x ≤ ⟨yl, yr, yL, yR⟩ → x < yR i := by { cases x, rw mk_le_mk, tauto } theorem mk_lt_of_le {xl xr xL xR y i} : ((xR : xr → pgame) i ≤ y) → (⟨xl, xr, xL, xR⟩ : pgame) < y := by { cases y, rw mk_lt_mk, tauto } theorem lt_mk_of_le {x : pgame} {yl yr : Type*} {yL : yl → pgame} {yR i} : (x ≤ yL i) → x < ⟨yl, yr, yL, yR⟩ := by { cases x, rw mk_lt_mk, exact λ h, or.inl ⟨_, h⟩ } theorem move_left_lt_of_le {x y : pgame} {i} : x ≤ y → x.move_left i < y := by { cases x, exact lt_of_le_mk } theorem lt_move_right_of_le {x y : pgame} {i} : x ≤ y → x < y.move_right i := by { cases y, exact lt_of_mk_le } theorem lt_of_move_right_le {x y : pgame} {i} : x.move_right i ≤ y → x < y := by { cases x, rw move_right_mk, exact mk_lt_of_le } theorem lt_of_le_move_left {x y : pgame} {i} : x ≤ y.move_left i → x < y := by { cases y, rw move_left_mk, exact lt_mk_of_le } private theorem not_le_lt {x y : pgame} : (¬ x ≤ y ↔ y < x) ∧ (¬ x < y ↔ y ≤ x) := begin induction x with xl xr xL xR IHxl IHxr generalizing y, induction y with yl yr yL yR IHyl IHyr, classical, simp only [mk_le_mk, mk_lt_mk, not_and_distrib, not_or_distrib, not_forall, not_exists, and_comm, or_comm, IHxl, IHxr, IHyl, IHyr, iff_self, and_self] end @[simp] theorem not_le {x y : pgame} : ¬ x ≤ y ↔ y < x := not_le_lt.1 @[simp] theorem not_lt {x y : pgame} : ¬ x < y ↔ y ≤ x := not_le_lt.2 @[refl] protected theorem le_rfl : ∀ {x : pgame}, x ≤ x | ⟨l, r, L, R⟩ := by rw mk_le_mk; exact ⟨λ i, lt_mk_of_le le_rfl, λ i, mk_lt_of_le le_rfl⟩ protected theorem le_refl (x : pgame) : x ≤ x := pgame.le_rfl protected theorem lt_irrefl (x : pgame) : ¬ x < x := not_lt.2 pgame.le_rfl protected theorem ne_of_lt : ∀ {x y : pgame}, x < y → x ≠ y | x _ h rfl := pgame.lt_irrefl x h /-- In general, `xL i ≤ x` isn't true. It is true however for `numeric` games, see `numeric.move_left_le`. -/ theorem lt_mk {xl xr : Type u} {xL : xl → pgame} {xR : xr → pgame} (i) : xL i < mk xl xr xL xR := lt_mk_of_le pgame.le_rfl /-- In general, `x ≤ xR i` isn't true. It is true however for `numeric` games, see `numeric.move_right_le`. -/ theorem mk_lt {xl xr : Type u} {xL : xl → pgame} {xR : xr → pgame} (i) : mk xl xr xL xR < xR i := mk_lt_of_le pgame.le_rfl /-- In general, `x.move_left i ≤ x` isn't true. It is true however for `numeric` games, see `numeric.move_left_le`. -/ theorem move_left_lt {x : pgame} (i) : x.move_left i < x := move_left_lt_of_le pgame.le_rfl /-- In general, `x ≤ x.move_right i` isn't true. It is true however for `numeric` games, see `numeric.move_right_le`. -/ theorem lt_move_right {x : pgame} (i) : x < x.move_right i := lt_move_right_of_le pgame.le_rfl theorem le_trans_aux {xl xr} {xL : xl → pgame} {xR : xr → pgame} {yl yr} {yL : yl → pgame} {yR : yr → pgame} {zl zr} {zL : zl → pgame} {zR : zr → pgame} (h₁ : ∀ i, mk yl yr yL yR ≤ mk zl zr zL zR → mk zl zr zL zR ≤ xL i → mk yl yr yL yR ≤ xL i) (h₂ : ∀ i, zR i ≤ mk xl xr xL xR → mk xl xr xL xR ≤ mk yl yr yL yR → zR i ≤ mk yl yr yL yR) : mk xl xr xL xR ≤ mk yl yr yL yR → mk yl yr yL yR ≤ mk zl zr zL zR → mk xl xr xL xR ≤ mk zl zr zL zR := by simp only [mk_le_mk] at *; exact λ ⟨xLy, xyR⟩ ⟨yLz, yzR⟩, ⟨ λ i, not_le.1 (λ h, not_lt.2 (h₁ _ ⟨yLz, yzR⟩ h) (xLy _)), λ i, not_le.1 (λ h, not_lt.2 (h₂ _ h ⟨xLy, xyR⟩) (yzR _))⟩ @[trans] theorem le_trans {x y z : pgame} : x ≤ y → y ≤ z → x ≤ z := suffices ∀ {x y z : pgame}, (x ≤ y → y ≤ z → x ≤ z) ∧ (y ≤ z → z ≤ x → y ≤ x) ∧ (z ≤ x → x ≤ y → z ≤ y), from this.1, begin clear x y z, intros, induction x with xl xr xL xR IHxl IHxr generalizing y z, induction y with yl yr yL yR IHyl IHyr generalizing z, induction z with zl zr zL zR IHzl IHzr, exact ⟨ le_trans_aux (λ i, (IHxl _).2.1) (λ i, (IHzr _).2.2), le_trans_aux (λ i, (IHyl _).2.2) (λ i, (IHxr _).1), le_trans_aux (λ i, (IHzl _).1) (λ i, (IHyr _).2.1)⟩, end @[trans] theorem lt_of_le_of_lt {x y z : pgame} (hxy : x ≤ y) (hyz : y < z) : x < z := begin rw ←not_le at ⊢ hyz, exact mt (λ H, le_trans H hxy) hyz end @[trans] theorem lt_of_lt_of_le {x y z : pgame} (hxy : x < y) (hyz : y ≤ z) : x < z := begin rw ←not_le at ⊢ hxy, exact mt (λ H, le_trans hyz H) hxy end /-- Define the equivalence relation on pre-games. Two pre-games `x`, `y` are equivalent if `x ≤ y` and `y ≤ x`. -/ def equiv (x y : pgame) : Prop := x ≤ y ∧ y ≤ x local infix ` ≈ ` := pgame.equiv @[refl, simp] theorem equiv_rfl {x} : x ≈ x := ⟨pgame.le_rfl, pgame.le_rfl⟩ theorem equiv_refl (x) : x ≈ x := equiv_rfl @[symm] theorem equiv_symm {x y} : x ≈ y → y ≈ x | ⟨xy, yx⟩ := ⟨yx, xy⟩ @[trans] theorem equiv_trans {x y z} : x ≈ y → y ≈ z → x ≈ z | ⟨xy, yx⟩ ⟨yz, zy⟩ := ⟨le_trans xy yz, le_trans zy yx⟩ @[trans] theorem lt_of_lt_of_equiv {x y z} (h₁ : x < y) (h₂ : y ≈ z) : x < z := lt_of_lt_of_le h₁ h₂.1 @[trans] theorem le_of_le_of_equiv {x y z} (h₁ : x ≤ y) (h₂ : y ≈ z) : x ≤ z := le_trans h₁ h₂.1 @[trans] theorem lt_of_equiv_of_lt {x y z} (h₁ : x ≈ y) (h₂ : y < z) : x < z := lt_of_le_of_lt h₁.1 h₂ @[trans] theorem le_of_equiv_of_le {x y z} (h₁ : x ≈ y) (h₂ : y ≤ z) : x ≤ z := le_trans h₁.1 h₂ theorem le_congr {x₁ y₁ x₂ y₂} : x₁ ≈ x₂ → y₁ ≈ y₂ → (x₁ ≤ y₁ ↔ x₂ ≤ y₂) | ⟨x12, x21⟩ ⟨y12, y21⟩ := ⟨λ h, le_trans x21 (le_trans h y12), λ h, le_trans x12 (le_trans h y21)⟩ theorem lt_congr {x₁ y₁ x₂ y₂} (hx : x₁ ≈ x₂) (hy : y₁ ≈ y₂) : x₁ < y₁ ↔ x₂ < y₂ := not_le.symm.trans $ (not_congr (le_congr hy hx)).trans not_le theorem lt_or_equiv_of_le {x y : pgame} (h : x ≤ y) : x < y ∨ x ≈ y := or_iff_not_imp_left.2 $ λ h', ⟨h, not_lt.1 h'⟩ theorem lt_or_equiv_or_gt (x y : pgame) : x < y ∨ x ≈ y ∨ y < x := begin by_cases h : x < y, { exact or.inl h }, { right, cases (lt_or_equiv_of_le (not_lt.1 h)) with h' h', { exact or.inr h' }, { exact or.inl h'.symm } } end theorem equiv_congr_left {y₁ y₂} : y₁ ≈ y₂ ↔ ∀ x₁, x₁ ≈ y₁ ↔ x₁ ≈ y₂ := ⟨λ h x₁, ⟨λ h', equiv_trans h' h, λ h', equiv_trans h' (equiv_symm h)⟩, λ h, (h y₁).1 $ equiv_refl _⟩ theorem equiv_congr_right {x₁ x₂} : x₁ ≈ x₂ ↔ ∀ y₁, x₁ ≈ y₁ ↔ x₂ ≈ y₁ := ⟨λ h y₁, ⟨λ h', equiv_trans (equiv_symm h) h', λ h', equiv_trans h h'⟩, λ h, (h x₂).2 $ equiv_refl _⟩ theorem equiv_of_mk_equiv {x y : pgame} (L : x.left_moves ≃ y.left_moves) (R : x.right_moves ≃ y.right_moves) (hl : ∀ (i : x.left_moves), x.move_left i ≈ y.move_left (L i)) (hr : ∀ (j : y.right_moves), x.move_right (R.symm j) ≈ y.move_right j) : x ≈ y := begin fsplit; rw le_def, { exact ⟨λ i, or.inl ⟨L i, (hl i).1⟩, λ j, or.inr ⟨R.symm j, (hr j).1⟩⟩ }, { fsplit, { intro i, left, specialize hl (L.symm i), simp only [move_left_mk, equiv.apply_symm_apply] at hl, use ⟨L.symm i, hl.2⟩ }, { intro j, right, specialize hr (R j), simp only [move_right_mk, equiv.symm_apply_apply] at hr, use ⟨R j, hr.2⟩ } } end /-- `restricted x y` says that Left always has no more moves in `x` than in `y`, and Right always has no more moves in `y` than in `x` -/ inductive restricted : pgame.{u} → pgame.{u} → Type (u+1) | mk : Π {x y : pgame} (L : x.left_moves → y.left_moves) (R : y.right_moves → x.right_moves), (∀ (i : x.left_moves), restricted (x.move_left i) (y.move_left (L i))) → (∀ (j : y.right_moves), restricted (x.move_right (R j)) (y.move_right j)) → restricted x y /-- The identity restriction. -/ @[refl] def restricted.refl : Π (x : pgame), restricted x x | (mk xl xr xL xR) := restricted.mk id id (λ i, restricted.refl _) (λ j, restricted.refl _) using_well_founded { dec_tac := pgame_wf_tac } instance (x : pgame) : inhabited (restricted x x) := ⟨restricted.refl _⟩ -- TODO trans for restricted theorem restricted.le : Π {x y : pgame} (r : restricted x y), x ≤ y | (mk xl xr xL xR) (mk yl yr yL yR) (restricted.mk L_embedding R_embedding L_restriction R_restriction) := begin rw le_def, exact ⟨λ i, or.inl ⟨L_embedding i, (L_restriction i).le⟩, λ i, or.inr ⟨R_embedding i, (R_restriction i).le⟩⟩ end /-- `relabelling x y` says that `x` and `y` are really the same game, just dressed up differently. Specifically, there is a bijection between the moves for Left in `x` and in `y`, and similarly for Right, and under these bijections we inductively have `relabelling`s for the consequent games. In ZFC, relabellings would indeed be the same games. -/ inductive relabelling : pgame.{u} → pgame.{u} → Type (u+1) | mk : Π {x y : pgame} (L : x.left_moves ≃ y.left_moves) (R : x.right_moves ≃ y.right_moves), (∀ (i : x.left_moves), relabelling (x.move_left i) (y.move_left (L i))) → (∀ (j : y.right_moves), relabelling (x.move_right (R.symm j)) (y.move_right j)) → relabelling x y /-- If `x` is a relabelling of `y`, then Left and Right have the same moves in either game, so `x` is a restriction of `y`. -/ def relabelling.restricted : Π {x y : pgame} (r : relabelling x y), restricted x y | (mk xl xr xL xR) (mk yl yr yL yR) (relabelling.mk L_equiv R_equiv L_relabelling R_relabelling) := restricted.mk L_equiv.to_embedding R_equiv.symm.to_embedding (λ i, (L_relabelling i).restricted) (λ j, (R_relabelling j).restricted) -- It's not the case that `restricted x y → restricted y x → relabelling x y`, -- but if we insisted that the maps in a restriction were injective, then one -- could use Schröder-Bernstein for do this. /-- The identity relabelling. -/ @[refl] def relabelling.refl : Π (x : pgame), relabelling x x | (mk xl xr xL xR) := relabelling.mk (equiv.refl _) (equiv.refl _) (λ i, relabelling.refl _) (λ j, relabelling.refl _) using_well_founded { dec_tac := pgame_wf_tac } instance (x : pgame) : inhabited (relabelling x x) := ⟨relabelling.refl _⟩ /-- Reverse a relabelling. -/ @[symm] def relabelling.symm : Π {x y : pgame}, relabelling x y → relabelling y x | (mk xl xr xL xR) (mk yl yr yL yR) (relabelling.mk L_equiv R_equiv L_relabelling R_relabelling) := begin refine relabelling.mk L_equiv.symm R_equiv.symm _ _, { intro i, simpa using (L_relabelling (L_equiv.symm i)).symm }, { intro j, simpa using (R_relabelling (R_equiv j)).symm } end /-- Transitivity of relabelling -/ @[trans] def relabelling.trans : Π {x y z : pgame}, relabelling x y → relabelling y z → relabelling x z | (mk xl xr xL xR) (mk yl yr yL yR) (mk zl zr zL zR) (relabelling.mk L_equiv₁ R_equiv₁ L_relabelling₁ R_relabelling₁) (relabelling.mk L_equiv₂ R_equiv₂ L_relabelling₂ R_relabelling₂) := begin refine relabelling.mk (L_equiv₁.trans L_equiv₂) (R_equiv₁.trans R_equiv₂) _ _, { intro i, simpa using (L_relabelling₁ _).trans (L_relabelling₂ _) }, { intro j, simpa using (R_relabelling₁ _).trans (R_relabelling₂ _) }, end /-- Any game without left or right moves is a relabelling of 0. -/ def relabelling.is_empty (x : pgame) [is_empty (x.left_moves)] [is_empty (x.right_moves)] : relabelling x 0 := ⟨equiv.equiv_pempty _, equiv.equiv_pempty _, is_empty_elim, is_empty_elim⟩ theorem relabelling.le {x y : pgame} (r : relabelling x y) : x ≤ y := r.restricted.le /-- A relabelling lets us prove equivalence of games. -/ theorem relabelling.equiv {x y : pgame} (r : relabelling x y) : x ≈ y := ⟨r.le, r.symm.le⟩ instance {x y : pgame} : has_coe (relabelling x y) (x ≈ y) := ⟨relabelling.equiv⟩ /-- Replace the types indexing the next moves for Left and Right by equivalent types. -/ def relabel {x : pgame} {xl' xr'} (el : x.left_moves ≃ xl') (er : x.right_moves ≃ xr') := pgame.mk xl' xr' (λ i, x.move_left (el.symm i)) (λ j, x.move_right (er.symm j)) @[simp] lemma relabel_move_left' {x : pgame} {xl' xr'} (el : x.left_moves ≃ xl') (er : x.right_moves ≃ xr') (i : xl') : move_left (relabel el er) i = x.move_left (el.symm i) := rfl @[simp] lemma relabel_move_left {x : pgame} {xl' xr'} (el : x.left_moves ≃ xl') (er : x.right_moves ≃ xr') (i : x.left_moves) : move_left (relabel el er) (el i) = x.move_left i := by simp @[simp] lemma relabel_move_right' {x : pgame} {xl' xr'} (el : x.left_moves ≃ xl') (er : x.right_moves ≃ xr') (j : xr') : move_right (relabel el er) j = x.move_right (er.symm j) := rfl @[simp] lemma relabel_move_right {x : pgame} {xl' xr'} (el : x.left_moves ≃ xl') (er : x.right_moves ≃ xr') (j : x.right_moves) : move_right (relabel el er) (er j) = x.move_right j := by simp /-- The game obtained by relabelling the next moves is a relabelling of the original game. -/ def relabel_relabelling {x : pgame} {xl' xr'} (el : x.left_moves ≃ xl') (er : x.right_moves ≃ xr') : relabelling x (relabel el er) := relabelling.mk el er (λ i, by simp) (λ j, by simp) /-- The negation of `{L | R}` is `{-R | -L}`. -/ def neg : pgame → pgame | ⟨l, r, L, R⟩ := ⟨r, l, λ i, neg (R i), λ i, neg (L i)⟩ instance : has_neg pgame := ⟨neg⟩ @[simp] lemma neg_def {xl xr xL xR} : -(mk xl xr xL xR) = mk xr xl (λ j, -(xR j)) (λ i, -(xL i)) := rfl instance : has_involutive_neg pgame := { neg_neg := λ x, begin induction x with xl xr xL xR ihL ihR, simp_rw [neg_def, ihL, ihR], exact ⟨rfl, rfl, heq.rfl, heq.rfl⟩, end, ..pgame.has_neg } @[simp] protected lemma neg_zero : -(0 : pgame) = 0 := begin dsimp [has_zero.zero, has_neg.neg, neg], congr; funext i; cases i end @[simp] lemma neg_of_lists (L R : list pgame) : -of_lists L R = of_lists (R.map (λ x, -x)) (L.map (λ x, -x)) := begin simp only [of_lists, neg_def, list.length_map, list.nth_le_map', eq_self_iff_true, true_and], split, all_goals { apply hfunext, { simp }, { intros a a' ha, congr' 2, have : ∀ {m n} (h₁ : m = n) {b : ulift (fin m)} {c : ulift (fin n)} (h₂ : b == c), (b.down : ℕ) = ↑c.down, { rintros m n rfl b c rfl, refl }, exact this (list.length_map _ _).symm ha } } end theorem left_moves_neg : ∀ x : pgame, (-x).left_moves = x.right_moves | ⟨_, _, _, _⟩ := rfl theorem right_moves_neg : ∀ x : pgame, (-x).right_moves = x.left_moves | ⟨_, _, _, _⟩ := rfl /-- Turns a right move for `x` into a left move for `-x` and vice versa. Even though these types are the same (not definitionally so), this is the preferred way to convert between them. -/ def to_left_moves_neg {x : pgame} : x.right_moves ≃ (-x).left_moves := equiv.cast (left_moves_neg x).symm /-- Turns a left move for `x` into a right move for `-x` and vice versa. Even though these types are the same (not definitionally so), this is the preferred way to convert between them. -/ def to_right_moves_neg {x : pgame} : x.left_moves ≃ (-x).right_moves := equiv.cast (right_moves_neg x).symm lemma move_left_neg {x : pgame} (i) : (-x).move_left (to_left_moves_neg i) = -x.move_right i := by { cases x, refl } @[simp] lemma move_left_neg' {x : pgame} (i) : (-x).move_left i = -x.move_right (to_left_moves_neg.symm i) := by { cases x, refl } lemma move_right_neg {x : pgame} (i) : (-x).move_right (to_right_moves_neg i) = -(x.move_left i) := by { cases x, refl } @[simp] lemma move_right_neg' {x : pgame} (i) : (-x).move_right i = -x.move_left (to_right_moves_neg.symm i) := by { cases x, refl } lemma move_left_neg_symm {x : pgame} (i) : x.move_left (to_right_moves_neg.symm i) = -(-x).move_right i := by simp lemma move_left_neg_symm' {x : pgame} (i) : x.move_left i = -(-x).move_right (to_right_moves_neg i) := by simp lemma move_right_neg_symm {x : pgame} (i) : x.move_right (to_left_moves_neg.symm i) = -(-x).move_left i := by simp lemma move_right_neg_symm' {x : pgame} (i) : x.move_right i = -(-x).move_left (to_left_moves_neg i) := by simp /-- If `x` has the same moves as `y`, then `-x` has the sames moves as `-y`. -/ def relabelling.neg_congr : ∀ {x y : pgame}, x.relabelling y → (-x).relabelling (-y) | (mk xl xr xL xR) (mk yl yr yL yR) ⟨L_equiv, R_equiv, L_relabelling, R_relabelling⟩ := ⟨R_equiv, L_equiv, λ i, relabelling.neg_congr (by simpa using R_relabelling (R_equiv i)), λ i, relabelling.neg_congr (by simpa using L_relabelling (L_equiv.symm i))⟩ theorem le_iff_neg_ge : Π {x y : pgame}, x ≤ y ↔ -y ≤ -x | (mk xl xr xL xR) (mk yl yr yL yR) := begin rw [le_def, le_def], dsimp [neg], refine ⟨λ h, ⟨λ i, _, λ j, _⟩, λ h, ⟨λ i, _, λ j, _⟩⟩, { rcases h.right i with ⟨w, h⟩ | ⟨w, h⟩, { refine or.inr ⟨to_right_moves_neg w, _⟩, convert le_iff_neg_ge.1 h, rw move_right_neg }, { exact or.inl ⟨w, le_iff_neg_ge.1 h⟩ } }, { rcases h.left j with ⟨w, h⟩ | ⟨w, h⟩, { exact or.inr ⟨w, le_iff_neg_ge.1 h⟩ }, { refine or.inl ⟨to_left_moves_neg w, _⟩, convert le_iff_neg_ge.1 h, rw move_left_neg } }, { rcases h.right i with ⟨w, h⟩ | ⟨w, h⟩, { refine or.inr ⟨to_left_moves_neg.symm w, le_iff_neg_ge.2 _⟩, rwa [move_right_neg_symm, neg_neg] }, { exact or.inl ⟨w, le_iff_neg_ge.2 h⟩ } }, { rcases h.left j with ⟨w, h⟩ | ⟨w, h⟩, { exact or.inr ⟨w, le_iff_neg_ge.2 h⟩ }, { refine or.inl ⟨to_right_moves_neg.symm w, le_iff_neg_ge.2 _⟩, rwa [move_left_neg_symm, neg_neg] } }, end using_well_founded { dec_tac := pgame_wf_tac } theorem neg_congr {x y : pgame} (h : x ≈ y) : -x ≈ -y := ⟨le_iff_neg_ge.1 h.2, le_iff_neg_ge.1 h.1⟩ theorem lt_iff_neg_gt : Π {x y : pgame}, x < y ↔ -y < -x := begin classical, intros, rw [←not_le, ←not_le, not_iff_not], apply le_iff_neg_ge end theorem zero_le_iff_neg_le_zero {x : pgame} : 0 ≤ x ↔ -x ≤ 0 := begin convert le_iff_neg_ge, rw pgame.neg_zero end theorem le_zero_iff_zero_le_neg {x : pgame} : x ≤ 0 ↔ 0 ≤ -x := begin convert le_iff_neg_ge, rw pgame.neg_zero end /-- The sum of `x = {xL | xR}` and `y = {yL | yR}` is `{xL + y, x + yL | xR + y, x + yR}`. -/ def add (x y : pgame) : pgame := begin induction x with xl xr xL xR IHxl IHxr generalizing y, induction y with yl yr yL yR IHyl IHyr, have y := mk yl yr yL yR, refine ⟨xl ⊕ yl, xr ⊕ yr, sum.rec _ _, sum.rec _ _⟩, { exact λ i, IHxl i y }, { exact λ i, IHyl i }, { exact λ i, IHxr i y }, { exact λ i, IHyr i } end instance : has_add pgame := ⟨add⟩ @[simp] theorem nat_one : ((1 : ℕ) : pgame) = 0 + 1 := rfl /-- `x + 0` has exactly the same moves as `x`. -/ def add_zero_relabelling : Π (x : pgame.{u}), relabelling (x + 0) x | (mk xl xr xL xR) := begin refine ⟨equiv.sum_empty xl pempty, equiv.sum_empty xr pempty, _, _⟩, { rintro (⟨i⟩|⟨⟨⟩⟩), apply add_zero_relabelling, }, { rintro j, apply add_zero_relabelling, } end /-- `x + 0` is equivalent to `x`. -/ lemma add_zero_equiv (x : pgame.{u}) : x + 0 ≈ x := (add_zero_relabelling x).equiv /-- `0 + x` has exactly the same moves as `x`. -/ def zero_add_relabelling : Π (x : pgame.{u}), relabelling (0 + x) x | (mk xl xr xL xR) := begin refine ⟨equiv.empty_sum pempty xl, equiv.empty_sum pempty xr, _, _⟩, { rintro (⟨⟨⟩⟩|⟨i⟩), apply zero_add_relabelling, }, { rintro j, apply zero_add_relabelling, } end /-- `0 + x` is equivalent to `x`. -/ lemma zero_add_equiv (x : pgame.{u}) : 0 + x ≈ x := (zero_add_relabelling x).equiv /-- An explicit equivalence between the moves for Left in `x + y` and the type-theory sum of the moves for Left in `x` and in `y`. -/ def left_moves_add (x y : pgame) : (x + y).left_moves ≃ x.left_moves ⊕ y.left_moves := by { cases x, cases y, refl, } /-- An explicit equivalence between the moves for Right in `x + y` and the type-theory sum of the moves for Right in `x` and in `y`. -/ def right_moves_add (x y : pgame) : (x + y).right_moves ≃ x.right_moves ⊕ y.right_moves := by { cases x, cases y, refl, } @[simp] lemma mk_add_move_left_inl {xl xr yl yr} {xL xR yL yR} {i} : (mk xl xr xL xR + mk yl yr yL yR).move_left (sum.inl i) = (mk xl xr xL xR).move_left i + (mk yl yr yL yR) := rfl @[simp] lemma add_move_left_inl {x y : pgame} {i} : (x + y).move_left ((@left_moves_add x y).symm (sum.inl i)) = x.move_left i + y := by { cases x, cases y, refl, } @[simp] lemma mk_add_move_right_inl {xl xr yl yr} {xL xR yL yR} {i} : (mk xl xr xL xR + mk yl yr yL yR).move_right (sum.inl i) = (mk xl xr xL xR).move_right i + (mk yl yr yL yR) := rfl @[simp] lemma add_move_right_inl {x y : pgame} {i} : (x + y).move_right ((@right_moves_add x y).symm (sum.inl i)) = x.move_right i + y := by { cases x, cases y, refl, } @[simp] lemma mk_add_move_left_inr {xl xr yl yr} {xL xR yL yR} {i} : (mk xl xr xL xR + mk yl yr yL yR).move_left (sum.inr i) = (mk xl xr xL xR) + (mk yl yr yL yR).move_left i := rfl @[simp] lemma add_move_left_inr {x y : pgame} {i : y.left_moves} : (x + y).move_left ((@left_moves_add x y).symm (sum.inr i)) = x + y.move_left i := by { cases x, cases y, refl, } @[simp] lemma mk_add_move_right_inr {xl xr yl yr} {xL xR yL yR} {i} : (mk xl xr xL xR + mk yl yr yL yR).move_right (sum.inr i) = (mk xl xr xL xR) + (mk yl yr yL yR).move_right i := rfl @[simp] lemma add_move_right_inr {x y : pgame} {i} : (x + y).move_right ((@right_moves_add x y).symm (sum.inr i)) = x + y.move_right i := by { cases x, cases y, refl, } instance is_empty_nat_right_moves : ∀ n : ℕ, is_empty (right_moves n) | 0 := pempty.is_empty | (n + 1) := begin haveI := is_empty_nat_right_moves n, rw nat.cast_succ, exact (right_moves_add _ _).is_empty end /-- If `w` has the same moves as `x` and `y` has the same moves as `z`, then `w + y` has the same moves as `x + z`. -/ def relabelling.add_congr : ∀ {w x y z : pgame.{u}}, w.relabelling x → y.relabelling z → (w + y).relabelling (x + z) | (mk wl wr wL wR) (mk xl xr xL xR) (mk yl yr yL yR) (mk zl zr zL zR) ⟨L_equiv₁, R_equiv₁, L_relabelling₁, R_relabelling₁⟩ ⟨L_equiv₂, R_equiv₂, L_relabelling₂, R_relabelling₂⟩ := begin refine ⟨equiv.sum_congr L_equiv₁ L_equiv₂, equiv.sum_congr R_equiv₁ R_equiv₂, _, _⟩, { rintro (i|j), { exact relabelling.add_congr (L_relabelling₁ i) (⟨L_equiv₂, R_equiv₂, L_relabelling₂, R_relabelling₂⟩) }, { exact relabelling.add_congr (⟨L_equiv₁, R_equiv₁, L_relabelling₁, R_relabelling₁⟩) (L_relabelling₂ j) }}, { rintro (i|j), { exact relabelling.add_congr (R_relabelling₁ i) (⟨L_equiv₂, R_equiv₂, L_relabelling₂, R_relabelling₂⟩) }, { exact relabelling.add_congr (⟨L_equiv₁, R_equiv₁, L_relabelling₁, R_relabelling₁⟩) (R_relabelling₂ j) }} end using_well_founded { dec_tac := pgame_wf_tac } instance : has_sub pgame := ⟨λ x y, x + -y⟩ @[simp] theorem sub_zero (x : pgame) : x - 0 = x + 0 := show x + -0 = x + 0, by rw pgame.neg_zero /-- If `w` has the same moves as `x` and `y` has the same moves as `z`, then `w - y` has the same moves as `x - z`. -/ def relabelling.sub_congr {w x y z : pgame} (h₁ : w.relabelling x) (h₂ : y.relabelling z) : (w - y).relabelling (x - z) := h₁.add_congr h₂.neg_congr /-- `-(x+y)` has exactly the same moves as `-x + -y`. -/ def neg_add_relabelling : Π (x y : pgame), relabelling (-(x + y)) (-x + -y) | (mk xl xr xL xR) (mk yl yr yL yR) := ⟨equiv.refl _, equiv.refl _, λ j, sum.cases_on j (λ j, neg_add_relabelling (xR j) (mk yl yr yL yR)) (λ j, neg_add_relabelling (mk xl xr xL xR) (yR j)), λ i, sum.cases_on i (λ i, neg_add_relabelling (xL i) (mk yl yr yL yR)) (λ i, neg_add_relabelling (mk xl xr xL xR) (yL i))⟩ using_well_founded { dec_tac := pgame_wf_tac } theorem neg_add_le {x y : pgame} : -(x + y) ≤ -x + -y := (neg_add_relabelling x y).le /-- `x + y` has exactly the same moves as `y + x`. -/ def add_comm_relabelling : Π (x y : pgame.{u}), relabelling (x + y) (y + x) | (mk xl xr xL xR) (mk yl yr yL yR) := begin refine ⟨equiv.sum_comm _ _, equiv.sum_comm _ _, _, _⟩; rintros (_|_); { simp [left_moves_add, right_moves_add], apply add_comm_relabelling } end using_well_founded { dec_tac := pgame_wf_tac } theorem add_comm_le {x y : pgame} : x + y ≤ y + x := (add_comm_relabelling x y).le theorem add_comm_equiv {x y : pgame} : x + y ≈ y + x := (add_comm_relabelling x y).equiv /-- `(x + y) + z` has exactly the same moves as `x + (y + z)`. -/ def add_assoc_relabelling : Π (x y z : pgame.{u}), relabelling ((x + y) + z) (x + (y + z)) | (mk xl xr xL xR) (mk yl yr yL yR) (mk zl zr zL zR) := begin refine ⟨equiv.sum_assoc _ _ _, equiv.sum_assoc _ _ _, _, _⟩, { rintro (⟨i|i⟩|i), { apply add_assoc_relabelling, }, { change relabelling (mk xl xr xL xR + yL i + mk zl zr zL zR) (mk xl xr xL xR + (yL i + mk zl zr zL zR)), apply add_assoc_relabelling, }, { change relabelling (mk xl xr xL xR + mk yl yr yL yR + zL i) (mk xl xr xL xR + (mk yl yr yL yR + zL i)), apply add_assoc_relabelling, } }, { rintro (j|⟨j|j⟩), { apply add_assoc_relabelling, }, { change relabelling (mk xl xr xL xR + yR j + mk zl zr zL zR) (mk xl xr xL xR + (yR j + mk zl zr zL zR)), apply add_assoc_relabelling, }, { change relabelling (mk xl xr xL xR + mk yl yr yL yR + zR j) (mk xl xr xL xR + (mk yl yr yL yR + zR j)), apply add_assoc_relabelling, } }, end using_well_founded { dec_tac := pgame_wf_tac } theorem add_assoc_equiv {x y z : pgame} : (x + y) + z ≈ x + (y + z) := (add_assoc_relabelling x y z).equiv private lemma add_le_add_right : ∀ {x y z : pgame} (h : x ≤ y), x + z ≤ y + z | (mk xl xr xL xR) (mk yl yr yL yR) (mk zl zr zL zR) := begin intros h, rw le_def, split, { -- if Left plays first intros i, change xl ⊕ zl at i, cases i, { -- either they play in x rw le_def at h, cases h, have t := h_left i, rcases t with ⟨i', ih⟩ | ⟨j, jh⟩, { left, refine ⟨(left_moves_add _ _).inv_fun (sum.inl i'), _⟩, exact add_le_add_right ih, }, { right, refine ⟨(right_moves_add _ _).inv_fun (sum.inl j), _⟩, convert add_le_add_right jh, apply add_move_right_inl } }, { -- or play in z left, refine ⟨(left_moves_add _ _).inv_fun (sum.inr i), _⟩, exact add_le_add_right h, }, }, { -- if Right plays first intros j, change yr ⊕ zr at j, cases j, { -- either they play in y rw le_def at h, cases h, have t := h_right j, rcases t with ⟨i, ih⟩ | ⟨j', jh⟩, { left, refine ⟨(left_moves_add _ _).inv_fun (sum.inl i), _⟩, convert add_le_add_right ih, apply add_move_left_inl }, { right, refine ⟨(right_moves_add _ _).inv_fun (sum.inl j'), _⟩, exact add_le_add_right jh } }, { -- or play in z right, refine ⟨(right_moves_add _ _).inv_fun (sum.inr j), _⟩, exact add_le_add_right h } } end using_well_founded { dec_tac := pgame_wf_tac } instance covariant_class_swap_add_le : covariant_class pgame pgame (swap (+)) (≤) := ⟨λ x y z, add_le_add_right⟩ instance covariant_class_add_le : covariant_class pgame pgame (+) (≤) := ⟨λ x y z h, calc x + y ≤ y + x : add_comm_le ... ≤ z + x : add_le_add_right h _ ... ≤ x + z : add_comm_le⟩ theorem add_congr {w x y z : pgame} (h₁ : w ≈ x) (h₂ : y ≈ z) : w + y ≈ x + z := ⟨le_trans (add_le_add_left h₂.1 w) (add_le_add_right h₁.1 z), le_trans (add_le_add_left h₂.2 x) (add_le_add_right h₁.2 y)⟩ theorem add_congr_left {x y z : pgame} (h : x ≈ y) : x + z ≈ y + z := add_congr h equiv_rfl theorem add_congr_right {x y z : pgame} : y ≈ z → x + y ≈ x + z := add_congr equiv_rfl theorem sub_congr {w x y z : pgame} (h₁ : w ≈ x) (h₂ : y ≈ z) : w - y ≈ x - z := add_congr h₁ (neg_congr h₂) theorem sub_congr_left {x y z : pgame} (h : x ≈ y) : x - z ≈ y - z := sub_congr h equiv_rfl theorem sub_congr_right {x y z : pgame} : y ≈ z → x - y ≈ x - z := sub_congr equiv_rfl theorem add_left_neg_le_zero : ∀ (x : pgame), -x + x ≤ 0 | ⟨xl, xr, xL, xR⟩ := begin rw [le_def], split, { intro i, change xr ⊕ xl at i, cases i, { -- If Left played in -x, Right responds with the same move in x. right, refine ⟨(right_moves_add _ _).inv_fun (sum.inr i), _⟩, convert @add_left_neg_le_zero (xR i), exact add_move_right_inr }, { -- If Left in x, Right responds with the same move in -x. right, dsimp, refine ⟨(right_moves_add _ _).inv_fun (sum.inl i), _⟩, convert @add_left_neg_le_zero (xL i), exact add_move_right_inl }, }, { rintro ⟨⟩, } end using_well_founded { dec_tac := pgame_wf_tac } theorem zero_le_add_left_neg (x : pgame) : 0 ≤ -x + x := begin rw [le_iff_neg_ge, pgame.neg_zero], exact le_trans neg_add_le (add_left_neg_le_zero _) end theorem add_left_neg_equiv (x : pgame) : -x + x ≈ 0 := ⟨add_left_neg_le_zero x, zero_le_add_left_neg x⟩ theorem add_right_neg_le_zero (x : pgame) : x + -x ≤ 0 := le_trans add_comm_le (add_left_neg_le_zero x) theorem zero_le_add_right_neg (x : pgame) : 0 ≤ x + -x := le_trans (zero_le_add_left_neg x) add_comm_le theorem add_right_neg_equiv (x : pgame) : x + -x ≈ 0 := ⟨add_right_neg_le_zero x, zero_le_add_right_neg x⟩ instance covariant_class_swap_add_lt : covariant_class pgame pgame (swap (+)) (<) := ⟨λ x y z h, suffices z + x ≤ y + x → z ≤ y, by { rw ←not_le at ⊢ h, exact mt this h }, λ w, calc z ≤ z + 0 : (add_zero_relabelling _).symm.le ... ≤ z + (x + -x) : add_le_add_left (zero_le_add_right_neg x) _ ... ≤ z + x + -x : (add_assoc_relabelling _ _ _).symm.le ... ≤ y + x + -x : add_le_add_right w _ ... ≤ y + (x + -x) : (add_assoc_relabelling _ _ _).le ... ≤ y + 0 : add_le_add_left (add_right_neg_le_zero x) _ ... ≤ y : (add_zero_relabelling _).le⟩ instance covariant_class_add_lt : covariant_class pgame pgame (+) (<) := ⟨λ x y z h, calc x + y ≤ y + x : add_comm_le ... < z + x : add_lt_add_right h _ ... ≤ x + z : add_comm_le⟩ theorem le_iff_sub_nonneg {x y : pgame} : x ≤ y ↔ 0 ≤ y - x := ⟨λ h, le_trans (zero_le_add_right_neg x) (add_le_add_right h _), λ h, calc x ≤ 0 + x : (zero_add_relabelling x).symm.le ... ≤ y - x + x : add_le_add_right h _ ... ≤ y + (-x + x) : (add_assoc_relabelling _ _ _).le ... ≤ y + 0 : add_le_add_left (add_left_neg_le_zero x) _ ... ≤ y : (add_zero_relabelling y).le⟩ theorem lt_iff_sub_pos {x y : pgame} : x < y ↔ 0 < y - x := ⟨λ h, lt_of_le_of_lt (zero_le_add_right_neg x) (add_lt_add_right h _), λ h, calc x ≤ 0 + x : (zero_add_relabelling x).symm.le ... < y - x + x : add_lt_add_right h _ ... ≤ y + (-x + x) : (add_assoc_relabelling _ _ _).le ... ≤ y + 0 : add_le_add_left (add_left_neg_le_zero x) _ ... ≤ y : (add_zero_relabelling y).le⟩ /-- The pre-game `star`, which is fuzzy/confused with zero. -/ def star : pgame.{u} := ⟨punit, punit, λ _, 0, λ _, 0⟩ @[simp] theorem star_left_moves : star.left_moves = punit := rfl @[simp] theorem star_right_moves : star.right_moves = punit := rfl @[simp] theorem star_move_left (x) : star.move_left x = 0 := rfl @[simp] theorem star_move_right (x) : star.move_right x = 0 := rfl instance unique_star_left_moves : unique star.left_moves := punit.unique instance unique_star_right_moves : unique star.right_moves := punit.unique theorem star_lt_zero : star < 0 := by { rw lt_zero, use default, rintros ⟨⟩ } theorem zero_lt_star : 0 < star := by { rw zero_lt, use default, rintros ⟨⟩ } @[simp] theorem neg_star : -star = star := by simp [star] @[simp] theorem zero_lt_one : (0 : pgame) < 1 := by { rw zero_lt, use default, rintro ⟨⟩ } theorem zero_le_one : (0 : pgame) ≤ 1 := zero_le_of_is_empty_right_moves 1 /-- The pre-game `half` is defined as `{0 | 1}`. -/ def half : pgame := ⟨punit, punit, 0, 1⟩ @[simp] theorem half_left_moves : half.left_moves = punit := rfl @[simp] theorem half_right_moves : half.right_moves = punit := rfl @[simp] lemma half_move_left (x) : half.move_left x = 0 := rfl @[simp] lemma half_move_right (x) : half.move_right x = 1 := rfl instance unique_half_left_moves : unique half.left_moves := punit.unique instance unique_half_right_moves : unique half.right_moves := punit.unique protected theorem zero_lt_half : 0 < half := by { rw zero_lt, use default, rintro ⟨⟩ } theorem half_lt_one : half < 1 := begin rw lt_def, right, use default, split; rintro ⟨⟩, exact zero_lt_one end end pgame
4f12043d5984ecd588dcdb93b6c93596ebee36a7
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/limits/preserves/limits.lean
4798fffae9805666ef845209afe56907f9074588
[ "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,044
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Bhavik Mehta -/ import category_theory.limits.preserves.basic /-! # Isomorphisms about functors which preserve (co)limits > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. If `G` preserves limits, and `C` and `D` have limits, then for any diagram `F : J ⥤ C` we have a canonical isomorphism `preserves_limit_iso : G.obj (limit F) ≅ limit (F ⋙ G)`. We also show that we can commute `is_limit.lift` of a preserved limit with `functor.map_cone`: `(preserves_limit.preserves t).lift (G.map_cone c₂) = G.map (t.lift c₂)`. The duals of these are also given. For functors which preserve (co)limits of specific shapes, see `preserves/shapes.lean`. -/ universes w' w v₁ v₂ u₁ u₂ noncomputable theory namespace category_theory open category limits variables {C : Type u₁} [category.{v₁} C] variables {D : Type u₂} [category.{v₂} D] variables (G : C ⥤ D) variables {J : Type w} [category.{w'} J] variables (F : J ⥤ C) section variables [preserves_limit F G] @[simp] lemma preserves_lift_map_cone (c₁ c₂ : cone F) (t : is_limit c₁) : (preserves_limit.preserves t).lift (G.map_cone c₂) = G.map (t.lift c₂) := ((preserves_limit.preserves t).uniq (G.map_cone c₂) _ (by simp [← G.map_comp])).symm variables [has_limit F] [has_limit (F ⋙ G)] /-- If `G` preserves limits, we have an isomorphism from the image of the limit of a functor `F` to the limit of the functor `F ⋙ G`. -/ def preserves_limit_iso : G.obj (limit F) ≅ limit (F ⋙ G) := (preserves_limit.preserves (limit.is_limit _)).cone_point_unique_up_to_iso (limit.is_limit _) @[simp, reassoc] lemma preserves_limits_iso_hom_π (j) : (preserves_limit_iso G F).hom ≫ limit.π _ j = G.map (limit.π F j) := is_limit.cone_point_unique_up_to_iso_hom_comp _ _ j @[simp, reassoc] lemma preserves_limits_iso_inv_π (j) : (preserves_limit_iso G F).inv ≫ G.map (limit.π F j) = limit.π _ j := is_limit.cone_point_unique_up_to_iso_inv_comp _ _ j @[simp, reassoc] lemma lift_comp_preserves_limits_iso_hom (t : cone F) : G.map (limit.lift _ t) ≫ (preserves_limit_iso G F).hom = limit.lift (F ⋙ G) (G.map_cone _) := by { ext, simp [← G.map_comp] } variables [preserves_limits_of_shape J G] [has_limits_of_shape J D] [has_limits_of_shape J C] /-- If `C, D` has all limits of shape `J`, and `G` preserves them, then `preserves_limit_iso` is functorial wrt `F`. -/ @[simps] def preserves_limit_nat_iso : lim ⋙ G ≅ (whiskering_right J C D).obj G ⋙ lim := nat_iso.of_components (λ F, preserves_limit_iso G F) begin intros _ _ f, ext, dsimp, simp only [preserves_limits_iso_hom_π, whisker_right_app, lim_map_π, category.assoc, preserves_limits_iso_hom_π_assoc, ← G.map_comp] end end section variables [preserves_colimit F G] @[simp] lemma preserves_desc_map_cocone (c₁ c₂ : cocone F) (t : is_colimit c₁) : (preserves_colimit.preserves t).desc (G.map_cocone _) = G.map (t.desc c₂) := ((preserves_colimit.preserves t).uniq (G.map_cocone _) _ (by simp [← G.map_comp])).symm variables [has_colimit F] [has_colimit (F ⋙ G)] /-- If `G` preserves colimits, we have an isomorphism from the image of the colimit of a functor `F` to the colimit of the functor `F ⋙ G`. -/ -- TODO: think about swapping the order here def preserves_colimit_iso : G.obj (colimit F) ≅ colimit (F ⋙ G) := (preserves_colimit.preserves (colimit.is_colimit _)).cocone_point_unique_up_to_iso (colimit.is_colimit _) @[simp, reassoc] lemma ι_preserves_colimits_iso_inv (j : J) : colimit.ι _ j ≫ (preserves_colimit_iso G F).inv = G.map (colimit.ι F j) := is_colimit.comp_cocone_point_unique_up_to_iso_inv _ (colimit.is_colimit (F ⋙ G)) j @[simp, reassoc] lemma ι_preserves_colimits_iso_hom (j : J) : G.map (colimit.ι F j) ≫ (preserves_colimit_iso G F).hom = colimit.ι (F ⋙ G) j := (preserves_colimit.preserves (colimit.is_colimit _)).comp_cocone_point_unique_up_to_iso_hom _ j @[simp, reassoc] lemma preserves_colimits_iso_inv_comp_desc (t : cocone F) : (preserves_colimit_iso G F).inv ≫ G.map (colimit.desc _ t) = colimit.desc _ (G.map_cocone t) := by { ext, simp [← G.map_comp] } variables [preserves_colimits_of_shape J G] [has_colimits_of_shape J D] [has_colimits_of_shape J C] /-- If `C, D` has all colimits of shape `J`, and `G` preserves them, then `preserves_colimit_iso` is functorial wrt `F`. -/ @[simps] def preserves_colimit_nat_iso : colim ⋙ G ≅ (whiskering_right J C D).obj G ⋙ colim := nat_iso.of_components (λ F, preserves_colimit_iso G F) begin intros _ _ f, rw [← iso.inv_comp_eq, ← category.assoc, ← iso.eq_comp_inv], ext, dsimp, erw ι_colim_map_assoc, simp only [ι_preserves_colimits_iso_inv, whisker_right_app, category.assoc, ι_preserves_colimits_iso_inv_assoc, ← G.map_comp], erw ι_colim_map end end end category_theory
627c836b9c939cae0e06e7cca2fffda591e494d6
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/tree_subterm_pred.lean
95feacd305e5785b27f35ccb0b80081582c85db7
[ "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
2,158
lean
import logic open eq.ops inductive tree (A : Type) := | leaf : A → tree A | node : tree A → tree A → tree A namespace tree inductive direct_subterm {A : Type} : tree A → tree A → Prop := | node_l : Π (l r : tree A), direct_subterm l (node l r) | node_r : Π (l r : tree A), direct_subterm r (node l r) definition direct_subterm.wf {A : Type} : well_founded (@direct_subterm A) := well_founded.intro (λ t : tree A, tree.rec_on t (λ (a : A), acc.intro (leaf a) (λ (s : tree A) (H : direct_subterm s (leaf a)), have gen : ∀ r : tree A, direct_subterm s r → r = leaf a → acc direct_subterm s, from λ r H, direct_subterm.rec_on H (λ l r e, tree.no_confusion e) (λ l r e, tree.no_confusion e), gen (leaf a) H rfl)) (λ (l r : tree A) (ihl : acc direct_subterm l) (ihr : acc direct_subterm r), acc.intro (node l r) (λ (s : tree A) (H : direct_subterm s (node l r)), have gen : ∀ n₁ : tree A, direct_subterm s n₁ → node l r = n₁ → acc direct_subterm s, from λ n₁ H, direct_subterm.rec_on H (λ (l' r' : tree A) (Heq : node l r = node l' r'), tree.no_confusion Heq (λ leq req, eq.rec_on leq ihl)) (λ (l' r' : tree A) (Heq : node l r = node l' r'), tree.no_confusion Heq (λ leq req, eq.rec_on req ihr)), gen (node l r) H rfl))) definition direct_subterm.wf₂ {A : Type} : well_founded (@direct_subterm A) := begin constructor, intro t, induction t, repeat (constructor; intro y hlt; cases hlt; repeat assumption) end definition subterm {A : Type} : tree A → tree A → Prop := tc (@direct_subterm A) definition subterm.wf {A : Type} : well_founded (@subterm A) := tc.wf (@direct_subterm.wf A) open nat example : subterm (leaf (2:nat)) (node (leaf 1) (leaf 2)) := !tc.base !direct_subterm.node_r example : subterm (leaf (2:nat)) (node (node (leaf 1) (leaf 2)) (leaf 3)) := have s₁ : subterm (leaf 2) (node (leaf 1) (leaf 2)), from !tc.base !direct_subterm.node_r, have s₂ : subterm (node (leaf 1) (leaf 2)) (node (node (leaf 1) (leaf 2)) (leaf 3)), from !tc.base !direct_subterm.node_l, !tc.trans s₁ s₂ end tree
33a341392602472a1ffd23671d772cb4d49389c9
9dc8cecdf3c4634764a18254e94d43da07142918
/src/topology/metric_space/metric_separated.lean
1c2d35184ac709b7352a67f2b09be4559f1df836
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
4,415
lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import topology.metric_space.emetric_space /-! # Metric separated pairs of sets In this file we define the predicate `is_metric_separated`. We say that two sets in an (extended) metric space are *metric separated* if the (extended) distance between `x ∈ s` and `y ∈ t` is bounded from below by a positive constant. This notion is useful, e.g., to define metric outer measures. -/ open emetric set noncomputable theory /-- Two sets in an (extended) metric space are called *metric separated* if the (extended) distance between `x ∈ s` and `y ∈ t` is bounded from below by a positive constant. -/ def is_metric_separated {X : Type*} [emetric_space X] (s t : set X) := ∃ r ≠ 0, ∀ (x ∈ s) (y ∈ t), r ≤ edist x y namespace is_metric_separated variables {X : Type*} [emetric_space X] {s t : set X} {x y : X} @[symm] lemma symm (h : is_metric_separated s t) : is_metric_separated t s := let ⟨r, r0, hr⟩ := h in ⟨r, r0, λ y hy x hx, edist_comm x y ▸ hr x hx y hy⟩ lemma comm : is_metric_separated s t ↔ is_metric_separated t s := ⟨symm, symm⟩ @[simp] lemma empty_left (s : set X) : is_metric_separated ∅ s := ⟨1, ennreal.zero_lt_one.ne', λ x, false.elim⟩ @[simp] lemma empty_right (s : set X) : is_metric_separated s ∅ := (empty_left s).symm protected lemma disjoint (h : is_metric_separated s t) : disjoint s t := let ⟨r, r0, hr⟩ := h in λ x hx, r0 $ by simpa using hr x hx.1 x hx.2 lemma subset_compl_right (h : is_metric_separated s t) : s ⊆ tᶜ := λ x hs ht, h.disjoint ⟨hs, ht⟩ @[mono] lemma mono {s' t'} (hs : s ⊆ s') (ht : t ⊆ t') : is_metric_separated s' t' → is_metric_separated s t := λ ⟨r, r0, hr⟩, ⟨r, r0, λ x hx y hy, hr x (hs hx) y (ht hy)⟩ lemma mono_left {s'} (h' : is_metric_separated s' t) (hs : s ⊆ s') : is_metric_separated s t := h'.mono hs subset.rfl lemma mono_right {t'} (h' : is_metric_separated s t') (ht : t ⊆ t') : is_metric_separated s t := h'.mono subset.rfl ht lemma union_left {s'} (h : is_metric_separated s t) (h' : is_metric_separated s' t) : is_metric_separated (s ∪ s') t := begin rcases ⟨h, h'⟩ with ⟨⟨r, r0, hr⟩, ⟨r', r0', hr'⟩⟩, refine ⟨min r r', _, λ x hx y hy, hx.elim _ _⟩, { rw [← pos_iff_ne_zero] at r0 r0' ⊢, exact lt_min r0 r0' }, { exact λ hx, (min_le_left _ _).trans (hr _ hx _ hy) }, { exact λ hx, (min_le_right _ _).trans (hr' _ hx _ hy) } end @[simp] lemma union_left_iff {s'} : is_metric_separated (s ∪ s') t ↔ is_metric_separated s t ∧ is_metric_separated s' t := ⟨λ h, ⟨h.mono_left (subset_union_left _ _), h.mono_left (subset_union_right _ _)⟩, λ h, h.1.union_left h.2⟩ lemma union_right {t'} (h : is_metric_separated s t) (h' : is_metric_separated s t') : is_metric_separated s (t ∪ t') := (h.symm.union_left h'.symm).symm @[simp] lemma union_right_iff {t'} : is_metric_separated s (t ∪ t') ↔ is_metric_separated s t ∧ is_metric_separated s t' := comm.trans $ union_left_iff.trans $ and_congr comm comm lemma finite_Union_left_iff {ι : Type*} {I : set ι} (hI : I.finite) {s : ι → set X} {t : set X} : is_metric_separated (⋃ i ∈ I, s i) t ↔ ∀ i ∈ I, is_metric_separated (s i) t := begin refine finite.induction_on hI (by simp) (λ i I hi _ hI, _), rw [bUnion_insert, ball_insert_iff, union_left_iff, hI] end alias finite_Union_left_iff ↔ _ finite_Union_left lemma finite_Union_right_iff {ι : Type*} {I : set ι} (hI : I.finite) {s : set X} {t : ι → set X} : is_metric_separated s (⋃ i ∈ I, t i) ↔ ∀ i ∈ I, is_metric_separated s (t i) := by simpa only [@comm _ _ s] using finite_Union_left_iff hI @[simp] lemma finset_Union_left_iff {ι : Type*} {I : finset ι} {s : ι → set X} {t : set X} : is_metric_separated (⋃ i ∈ I, s i) t ↔ ∀ i ∈ I, is_metric_separated (s i) t := finite_Union_left_iff I.finite_to_set alias finset_Union_left_iff ↔ _ finset_Union_left @[simp] lemma finset_Union_right_iff {ι : Type*} {I : finset ι} {s : set X} {t : ι → set X} : is_metric_separated s (⋃ i ∈ I, t i) ↔ ∀ i ∈ I, is_metric_separated s (t i) := finite_Union_right_iff I.finite_to_set alias finset_Union_right_iff ↔ _ finset_Union_right end is_metric_separated
8be5c1fd9baa6b35f87759f70f273709c61a03fe
c777c32c8e484e195053731103c5e52af26a25d1
/src/linear_algebra/matrix/special_linear_group.lean
82548fa6bd476ccfba5e119af66ff76db141344b
[ "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
11,904
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import linear_algebra.general_linear_group import linear_algebra.matrix.adjugate import linear_algebra.matrix.to_lin /-! # The Special Linear group $SL(n, R)$ This file defines the elements of the Special Linear group `special_linear_group n R`, consisting of all square `R`-matrices with determinant `1` on the fintype `n` by `n`. In addition, we define the group structure on `special_linear_group n R` and the embedding into the general linear group `general_linear_group R (n → R)`. ## Main definitions * `matrix.special_linear_group` is the type of matrices with determinant 1 * `matrix.special_linear_group.group` gives the group structure (under multiplication) * `matrix.special_linear_group.to_GL` is the embedding `SLₙ(R) → GLₙ(R)` ## Notation For `m : ℕ`, we introduce the notation `SL(m,R)` for the special linear group on the fintype `n = fin m`, in the locale `matrix_groups`. ## Implementation notes The inverse operation in the `special_linear_group` is defined to be the adjugate matrix, so that `special_linear_group n R` has a group structure for all `comm_ring R`. We define the elements of `special_linear_group` to be matrices, since we need to compute their determinant. This is in contrast with `general_linear_group R M`, which consists of invertible `R`-linear maps on `M`. We provide `matrix.special_linear_group.has_coe_to_fun` for convenience, but do not state any lemmas about it, and use `matrix.special_linear_group.coe_fn_eq_coe` to eliminate it `⇑` in favor of a regular `↑` coercion. ## References * https://en.wikipedia.org/wiki/Special_linear_group ## Tags matrix group, group, matrix inverse -/ namespace matrix universes u v open_locale matrix open linear_map section variables (n : Type u) [decidable_eq n] [fintype n] (R : Type v) [comm_ring R] /-- `special_linear_group n R` is the group of `n` by `n` `R`-matrices with determinant equal to 1. -/ def special_linear_group := { A : matrix n n R // A.det = 1 } end localized "notation (name := special_linear_group.fin) `SL(`n`, `R`)`:= matrix.special_linear_group (fin n) R" in matrix_groups namespace special_linear_group variables {n : Type u} [decidable_eq n] [fintype n] {R : Type v} [comm_ring R] instance has_coe_to_matrix : has_coe (special_linear_group n R) (matrix n n R) := ⟨λ A, A.val⟩ /- In this file, Lean often has a hard time working out the values of `n` and `R` for an expression like `det ↑A`. Rather than writing `(A : matrix n n R)` everywhere in this file which is annoyingly verbose, or `A.val` which is not the simp-normal form for subtypes, we create a local notation `↑ₘA`. This notation references the local `n` and `R` variables, so is not valid as a global notation. -/ local prefix `↑ₘ`:1024 := @coe _ (matrix n n R) _ lemma ext_iff (A B : special_linear_group n R) : A = B ↔ (∀ i j, ↑ₘA i j = ↑ₘB i j) := subtype.ext_iff.trans matrix.ext_iff.symm @[ext] lemma ext (A B : special_linear_group n R) : (∀ i j, ↑ₘA i j = ↑ₘB i j) → A = B := (special_linear_group.ext_iff A B).mpr instance has_inv : has_inv (special_linear_group n R) := ⟨λ A, ⟨adjugate A, by rw [det_adjugate, A.prop, one_pow]⟩⟩ instance has_mul : has_mul (special_linear_group n R) := ⟨λ A B, ⟨A.1 ⬝ B.1, by erw [det_mul, A.2, B.2, one_mul]⟩⟩ instance has_one : has_one (special_linear_group n R) := ⟨⟨1, det_one⟩⟩ instance : has_pow (special_linear_group n R) ℕ := { pow := λ x n, ⟨x ^ n, (det_pow _ _).trans $ x.prop.symm ▸ one_pow _⟩} instance : inhabited (special_linear_group n R) := ⟨1⟩ section coe_lemmas variables (A B : special_linear_group n R) @[simp] lemma coe_mk (A : matrix n n R) (h : det A = 1) : ↑(⟨A, h⟩ : special_linear_group n R) = A := rfl @[simp] lemma coe_inv : ↑ₘ(A⁻¹) = adjugate A := rfl @[simp] lemma coe_mul : ↑ₘ(A * B) = ↑ₘA ⬝ ↑ₘB := rfl @[simp] lemma coe_one : ↑ₘ(1 : special_linear_group n R) = (1 : matrix n n R) := rfl @[simp] lemma det_coe : det ↑ₘA = 1 := A.2 @[simp] lemma coe_pow (m : ℕ) : ↑ₘ(A ^ m) = ↑ₘA ^ m := rfl lemma det_ne_zero [nontrivial R] (g : special_linear_group n R) : det ↑ₘg ≠ 0 := by { rw g.det_coe, norm_num } lemma row_ne_zero [nontrivial R] (g : special_linear_group n R) (i : n): ↑ₘg i ≠ 0 := λ h, g.det_ne_zero $ det_eq_zero_of_row_eq_zero i $ by simp [h] end coe_lemmas instance : monoid (special_linear_group n R) := function.injective.monoid coe subtype.coe_injective coe_one coe_mul coe_pow instance : group (special_linear_group n R) := { mul_left_inv := λ A, by { ext1, simp [adjugate_mul] }, ..special_linear_group.monoid, ..special_linear_group.has_inv } /-- A version of `matrix.to_lin' A` that produces linear equivalences. -/ def to_lin' : special_linear_group n R →* (n → R) ≃ₗ[R] (n → R) := { to_fun := λ A, linear_equiv.of_linear (matrix.to_lin' ↑ₘA) (matrix.to_lin' ↑ₘ(A⁻¹)) (by rw [←to_lin'_mul, ←coe_mul, mul_right_inv, coe_one, to_lin'_one]) (by rw [←to_lin'_mul, ←coe_mul, mul_left_inv, coe_one, to_lin'_one]), map_one' := linear_equiv.to_linear_map_injective matrix.to_lin'_one, map_mul' := λ A B, linear_equiv.to_linear_map_injective $ matrix.to_lin'_mul A B } lemma to_lin'_apply (A : special_linear_group n R) (v : n → R) : special_linear_group.to_lin' A v = matrix.to_lin' ↑ₘA v := rfl lemma to_lin'_to_linear_map (A : special_linear_group n R) : ↑(special_linear_group.to_lin' A) = matrix.to_lin' ↑ₘA := rfl lemma to_lin'_symm_apply (A : special_linear_group n R) (v : n → R) : A.to_lin'.symm v = matrix.to_lin' ↑ₘ(A⁻¹) v := rfl lemma to_lin'_symm_to_linear_map (A : special_linear_group n R) : ↑(A.to_lin'.symm) = matrix.to_lin' ↑ₘ(A⁻¹) := rfl lemma to_lin'_injective : function.injective ⇑(to_lin' : special_linear_group n R →* (n → R) ≃ₗ[R] (n → R)) := λ A B h, subtype.coe_injective $ matrix.to_lin'.injective $ linear_equiv.to_linear_map_injective.eq_iff.mpr h /-- `to_GL` is the map from the special linear group to the general linear group -/ def to_GL : special_linear_group n R →* general_linear_group R (n → R) := (general_linear_group.general_linear_equiv _ _).symm.to_monoid_hom.comp to_lin' lemma coe_to_GL (A : special_linear_group n R) : ↑A.to_GL = A.to_lin'.to_linear_map := rfl variables {S : Type*} [comm_ring S] /-- A ring homomorphism from `R` to `S` induces a group homomorphism from `special_linear_group n R` to `special_linear_group n S`. -/ @[simps] def map (f : R →+* S) : special_linear_group n R →* special_linear_group n S := { to_fun := λ g, ⟨f.map_matrix ↑g, by { rw ← f.map_det, simp [g.2] }⟩, map_one' := subtype.ext $ f.map_matrix.map_one, map_mul' := λ x y, subtype.ext $ f.map_matrix.map_mul x y } section cast /-- Coercion of SL `n` `ℤ` to SL `n` `R` for a commutative ring `R`. -/ instance : has_coe (special_linear_group n ℤ) (special_linear_group n R) := ⟨λ x, map (int.cast_ring_hom R) x⟩ @[simp] lemma coe_matrix_coe (g : special_linear_group n ℤ) : ↑(g : special_linear_group n R) = (↑g : matrix n n ℤ).map (int.cast_ring_hom R) := map_apply_coe (int.cast_ring_hom R) g end cast section has_neg variables [fact (even (fintype.card n))] /-- Formal operation of negation on special linear group on even cardinality `n` given by negating each element. -/ instance : has_neg (special_linear_group n R) := ⟨λ g, ⟨- g, by simpa [(fact.out $ even $ fintype.card n).neg_one_pow, g.det_coe] using det_smul ↑ₘg (-1)⟩⟩ @[simp] lemma coe_neg (g : special_linear_group n R) : ↑(- g) = - (g : matrix n n R) := rfl instance : has_distrib_neg (special_linear_group n R) := function.injective.has_distrib_neg _ subtype.coe_injective coe_neg coe_mul @[simp] lemma coe_int_neg (g : special_linear_group n ℤ) : ↑(-g) = (-↑g : special_linear_group n R) := subtype.ext $ (@ring_hom.map_matrix n _ _ _ _ _ _ (int.cast_ring_hom R)).map_neg ↑g end has_neg section special_cases lemma SL2_inv_expl_det (A : SL(2,R)) : det ![![A.1 1 1, -A.1 0 1], ![-A.1 1 0 , A.1 0 0]] = 1 := begin rw [matrix.det_fin_two, mul_comm], simp only [subtype.val_eq_coe, cons_val_zero, cons_val_one, head_cons, mul_neg, neg_mul, neg_neg], have := A.2, rw matrix.det_fin_two at this, convert this, end lemma SL2_inv_expl (A : SL(2, R)) : A⁻¹ = ⟨![![A.1 1 1, -A.1 0 1], ![-A.1 1 0 , A.1 0 0]], SL2_inv_expl_det A⟩ := begin ext, have := matrix.adjugate_fin_two A.1, simp only [subtype.val_eq_coe] at this, rw [coe_inv, this], refl, end lemma fin_two_induction (P : SL(2, R) → Prop) (h : ∀ (a b c d : R) (hdet : a * d - b * c = 1), P ⟨!![a, b; c, d], by rwa [det_fin_two_of]⟩) (g : SL(2, R)) : P g := begin obtain ⟨m, hm⟩ := g, convert h (m 0 0) (m 0 1) (m 1 0) (m 1 1) (by rwa det_fin_two at hm), ext i j, fin_cases i; fin_cases j; refl, end lemma fin_two_exists_eq_mk_of_apply_zero_one_eq_zero {R : Type*} [field R] (g : SL(2, R)) (hg : (g : matrix (fin 2) (fin 2) R) 1 0 = 0) : ∃ (a b : R) (h : a ≠ 0), g = (⟨!![a, b; 0, a⁻¹], by simp [h]⟩ : SL(2, R)) := begin induction g using matrix.special_linear_group.fin_two_induction with a b c d h_det, replace hg : c = 0 := by simpa using hg, have had : a * d = 1 := by rwa [hg, mul_zero, sub_zero] at h_det, refine ⟨a, b, left_ne_zero_of_mul_eq_one had, _⟩, simp_rw [eq_inv_of_mul_eq_one_right had, hg], end end special_cases -- this section should be last to ensure we do not use it in lemmas section coe_fn_instance /-- This instance is here for convenience, but is not the simp-normal form. -/ instance : has_coe_to_fun (special_linear_group n R) (λ _, n → n → R) := { coe := λ A, A.val } @[simp] lemma coe_fn_eq_coe (s : special_linear_group n R) : ⇑s = ↑ₘs := rfl end coe_fn_instance end special_linear_group end matrix namespace modular_group open_locale matrix_groups open matrix matrix.special_linear_group local prefix `↑ₘ`:1024 := @coe _ (matrix (fin 2) (fin 2) ℤ) _ /-- The matrix `S = [[0, -1], [1, 0]]` as an element of `SL(2, ℤ)`. This element acts naturally on the Euclidean plane as a rotation about the origin by `π / 2`. This element also acts naturally on the hyperbolic plane as rotation about `i` by `π`. It represents the Mobiüs transformation `z ↦ -1/z` and is an involutive elliptic isometry. -/ def S : SL(2, ℤ) := ⟨!![0, -1; 1, 0], by norm_num [matrix.det_fin_two_of]⟩ /-- The matrix `T = [[1, 1], [0, 1]]` as an element of `SL(2, ℤ)` -/ def T : SL(2, ℤ) := ⟨!![1, 1; 0, 1], by norm_num [matrix.det_fin_two_of]⟩ lemma coe_S : ↑ₘS = !![0, -1; 1, 0] := rfl lemma coe_T : ↑ₘT = !![1, 1; 0, 1] := rfl lemma coe_T_inv : ↑ₘ(T⁻¹) = !![1, -1; 0, 1] := by simp [coe_inv, coe_T, adjugate_fin_two] lemma coe_T_zpow (n : ℤ) : ↑ₘ(T ^ n) = !![1, n; 0, 1] := begin induction n using int.induction_on with n h n h, { rw [zpow_zero, coe_one, matrix.one_fin_two] }, { simp_rw [zpow_add, zpow_one, coe_mul, h, coe_T, matrix.mul_fin_two], congrm !![_, _; _, _], rw [mul_one, mul_one, add_comm] }, { simp_rw [zpow_sub, zpow_one, coe_mul, h, coe_T_inv, matrix.mul_fin_two], congrm !![_, _; _, _]; ring }, end @[simp] lemma T_pow_mul_apply_one (n : ℤ) (g : SL(2, ℤ)) : ↑ₘ(T ^ n * g) 1 = ↑ₘg 1 := by simp [coe_T_zpow, matrix.mul, matrix.dot_product, fin.sum_univ_succ] @[simp] lemma T_mul_apply_one (g : SL(2, ℤ)) : ↑ₘ(T * g) 1 = ↑ₘg 1 := by simpa using T_pow_mul_apply_one 1 g @[simp] lemma T_inv_mul_apply_one (g : SL(2, ℤ)) : ↑ₘ(T⁻¹ * g) 1 = ↑ₘg 1 := by simpa using T_pow_mul_apply_one (-1) g end modular_group
4100c990376e99f77427f8d40dfa4272638b4369
32fa6b3db8c34b5b2996ed46f2eef23e6cd58023
/list.lean
1d6880a355d524e96fdca0463f9b53e41f50574e
[]
no_license
skbaek/strassen
3568459f9aa85beb9d3a653e92225bd9518985a5
396c94805360b10896d436813c1e4d0190885840
refs/heads/master
1,587,522,553,720
1,549,860,051,000
1,549,860,051,000
170,051,087
0
0
null
null
null
null
UTF-8
Lean
false
false
1,753
lean
namespace list variables {α : Type} lemma append_take_drop : ∀ k (as : list α), as.take k ++ as.drop k = as | 0 as := rfl | k [] := begin cases k; simp end | (k+1) (a::as) := begin simp [append_take_drop] end #exit def split : nat → list α → (list α × list α) | 0 [] := ([],[]) | (k+1) [] := ([],[]) | 0 (a::as) := ⟨[], a::as⟩ | (k+1) (a::as) := let (as1,as2) := as.split k in (a::as1,as2) lemma split_succ_cons (k a) (as : list α) : split (k+1) (a::as) = (a::(split k as).fst, (split k as).snd) := begin simp only [split], cases (split k as) with as1 as2, refl end lemma append_split : ∀ (k) (as : list α), (split k as).fst ++ (split k as).snd = as | k [] := begin cases k; refl end | 0 as := begin cases as with a as; refl end | (k+1) (a::as) := begin rw split_succ_cons, simp [append_split] end lemma length_fst_split : ∀ (m n) (as : list α), as.length = m + n → (split m as).fst.length = m | 0 n as h := begin cases as with a as; refl end | (m+1) n as h := begin cases as with a as, { simp at h, cases h }, { rw split_succ_cons, simp, rw length_fst_split m n, simp, rw [length_cons, add_comm m 1, add_assoc, nat.add_one, nat.one_add] at h, simp at h, assumption } end lemma length_snd_split : ∀ (m n) (as : list α), as.length = m + n → (split m as).snd.length = n | 0 n [] h := begin simp at h, simp [split], assumption end | 0 n (a::as) h := begin simp at h, simp [split], assumption end | (m+1) n [] h := begin simp at h, cases h end | (m+1) n (a::as) h := begin simp at h, rw split_succ_cons, simp, apply length_snd_split, rw [← add_assoc, nat.add_one, nat.one_add] at h, simp at h, assumption end end list
adea3d0cdbf01e0f62397c5bbd7ffc7dc1085b49
10c7c971a1902d76057c52ce0529ebb491a69c44
/Quotients.lean
eb5c8e2d3d74d12b06afaee85d3eee4b9c08759a
[]
no_license
SzymonKubica/Lean
5f6122e8dd9171239b36a9ce0515f6acbc49781a
627bff2f001ba3f009c112c9332093e8de84863c
refs/heads/main
1,675,563,490,768
1,608,538,609,000
1,608,538,609,000
307,184,347
0
0
null
null
null
null
UTF-8
Lean
false
false
9,107
lean
import tactic /-- The equivalence relation on ℕ² such that equivalence classes are ℤ -/ def nat2.R (a b : ℕ × ℕ) : Prop := a.1 + b.2 = b.1 + a.2 -- here a and b are pairs, so a = (a.1, a.2) etc. -- introduce ≈ (type with `\~~`) notation for this relation instance : has_equiv (ℕ × ℕ) := ⟨nat2.R⟩ -- let's prove some lemmas about this binary relation namespace nat2.R -- The following lemma is true by definition, but it's useful to -- have it around so you can rewrite with it lemma equiv_def {i j k l : ℕ} : (i, j) ≈ (k, l) ↔ i + l = k + j := begin refl end -- try rewriting `equiv_def` lemma practice : (3, 5) ≈ (4, 6) := begin sorry end -- Now let's prove that this binary relation is an equivalence relation lemma reflexive : ∀ x : ℕ × ℕ, x ≈ x := begin -- let x be (i,j) rintro ⟨i, j⟩, sorry end lemma symmetric : ∀ x y : ℕ × ℕ, (x ≈ y) → (y ≈ x) := begin -- here are a couple of tricks rintro ⟨i, j⟩ ⟨k, l⟩ h, -- type `⊢` with `\|-` rw equiv_def at h ⊢, sorry end -- sub-boss lemma transitive : ∀ x y z : ℕ × ℕ, (x ≈ y) → (y ≈ z) → (x ≈ z) := begin -- this is a little trickier -- recall `add_left_inj a` says `b + a = c + a ↔ b = c` -- and you might want to consider rewriting it in the ← direction sorry end -- This line tells Lean that the binary relation is an equivalence -- relation and hence we can take the "quotient", i.e. the -- type of equivalence classes instance setoid : setoid (ℕ × ℕ) := { r := nat2.R, iseqv := ⟨reflexive, symmetric, transitive⟩ } -- end of lemmas about the binary relation end nat2.R -- ...but we're still going to be using them open nat2.R /-- The integers are the equivalence classes of the equivalence relation we just defined on ℕ² -/ def myint := quotient nat2.R.setoid -- let's make some definitions, and prove some theorems, about integers namespace myint -- The first goal is to get a good interface for addition. -- To do this we need to define a+b, and -a, and 0. Let's do -- them in reverse order. /-! ## zero -/ -- Notation: ⟦(a,b)⟧ ∈ ℤ is the equivalence class of (a,b) ∈ ℕ² /-- 0 is the equivalence class of (0,0) -/ def zero := ⟦(0,0)⟧ -- Notation 0 for zero instance : has_zero myint := ⟨myint.zero⟩ -- true by definition lemma zero_def : (0 : myint) = ⟦(0, 0)⟧ := begin sorry end /-! ## negation (additive inverse) -/ -- First we define an "auxiliary" map from ℕ² to ℤ -- sending (a,b) to the equivalence class of (b,a). def neg_aux (x : ℕ × ℕ) : myint := ⟦(x.2, x.1)⟧ -- true by definition lemma neg_aux_def (i j : ℕ) : neg_aux (i, j) = ⟦(j, i)⟧ := begin sorry end /-! ### Well-definedness of negation OK now here's the concrete problem. We would like to define a negation map `ℤ → ℤ` sending `z` to `-z`. We want to do this in the following way: Say `z ∈ ℤ`. Choose `a=(i,j) ∈ ℕ²` representing `z` (i.e. such that `cl(i,j) = ⟦(i,j)⟧ = z`) Now apply `neg_aux` to `a`, and define `-z` to be the result. The problem with this is that what if `b` is a different element of the equivalence class? Then we also want `-z` to be `neg_aux b`. Indeed, in Lean this construction is called `quotient.lift`, and if you uncomment the below code -/ --def neg : myint → myint := --quotient.lift neg_aux _ /- you'll see an error, and if you put your cursor on the error you'll see that Lean wants a proof that if two elements `a` and `b` are in the same equivalence class, then `neg_aux a = neg_aux b`. So let's prove this now. You'll need to know `quotient.sound : a ≈ b → ⟦a⟧ = ⟦b⟧` -/ -- negation on the integers, defined via neg_aux, is well-defined. lemma neg_aux_lemma : ∀ x y : ℕ × ℕ, x ≈ y → neg_aux x = neg_aux y := begin rintro ⟨i,j⟩ ⟨k,l⟩ h, rw [neg_aux_def, neg_aux_def], -- ⊢ ⟦(j, i)⟧ = ⟦(l, k)⟧ -- next step: if ⟦a⟧=⟦b⟧ then a ≈ b apply quotient.sound, -- ⊢ (j, i) ≈ (l, k) -- take it from here. sorry end -- Note that we use `neg_aux_lemma` in the definition below -- to justify well-definedness of `neg` /-- Negation on on the integers. The function sending `z` to `-z`. -/ def neg : myint → myint := quotient.lift neg_aux neg_aux_lemma -- notation for negation instance : has_neg myint := ⟨neg⟩ -- We can now write `-z` if `z : myint` -- this is true by definition lemma neg_def (i j : ℕ) : (-⟦(i, j)⟧ : myint) = ⟦(j, i)⟧ := begin sorry end /-! ## addition Our final construction: we want to define addition on `myint`. Here we have the same problem. Say z₁ and z₂ are integers. Choose elements a₁=(i,j) and a₂=(k,l) in ℕ². We want to define z₁ + z₂ to be ⟦(i+k,j+l)⟧, the equivalence class of a₁ + a₂. We will need to check this is well-defined. -/ /-- An auxiliary function taking two elements of ℕ² and returning the equivalence class of their sum. -/ def add_aux (x y : ℕ × ℕ) : myint := ⟦(x.1 + y.1, x.2 + y.2)⟧ -- true by definition lemma add_aux_def (i j k l : ℕ) : add_aux (i, j) (k, l) = ⟦(i + k, j + l)⟧ := begin sorry end /- We want the definition of addition to look like the below. Uncomment it to see the problem. -/ --def add : myint → myint → myint := --quotient.lift₂ add_aux _ /- We had better check that choosing different elements in the same equivalence class gives the same definition. -/ lemma add_aux_lemma : ∀ x₁ x₂ y₁ y₂ : ℕ × ℕ, (x₁ ≈ y₁) → (x₂ ≈ y₂) → add_aux x₁ x₂ = add_aux y₁ y₂ := begin sorry, end -- Now this is checked, we can define addition: it's well-defined. /-- Addition on the integers -/ def add : myint → myint → myint := quotient.lift₂ add_aux add_aux_lemma -- notation for addition instance : has_add myint := ⟨add⟩ -- true by definition lemma add_def (i j k l : ℕ) : (⟦(i, j)⟧ + ⟦(k, l)⟧ : myint) = ⟦(i + k, j + l)⟧ := begin sorry end /- The four fundamental facts about addition on the integers are: 1) associativity 2) commutativity 3) zero is an additive identity 4) negation is an additive inverse. Let's prove these now. -/ lemma zero_add (x : myint) : 0 + x = x := begin -- need to get from ℤ back to ℕ² apply quotient.induction_on x, sorry, end lemma add_zero (x : myint) : x + 0 = x := begin sorry end lemma add_left_neg (x : myint) : -x + x = 0 := begin sorry end -- here we need to change both x and y into elements of ℕ² lemma add_comm (x y : myint) : x + y = y + x := begin apply quotient.induction_on₂ x y, sorry end lemma add_assoc (x y z : myint) : (x + y) + z = x + (y + z) := begin sorry, end -- The lemmas above are the axioms for a commutative group. -- Hence we just proved that the integers are a -- commutative group under addition! instance : add_comm_group myint := { add := (+), add_assoc := add_assoc, zero := 0, zero_add := zero_add, add_zero := add_zero, neg := has_neg.neg, add_left_neg := add_left_neg, add_comm := add_comm } -- woohoo! /-! ## multiplication What's left to define is 1 and multiplication (note that we don't need multiplicative inverses -- if a is a non-zero integer then a⁻¹ is typially not an integer) -/ def mul_aux (x y : ℕ × ℕ) : myint := ⟦(sorry, sorry)⟧ -- true by definition lemma mul_aux_def (i j k l : ℕ) : mul_aux (i, j) (k, l) = sorry := begin sorry end -- Boss level. -- Dr. Lawn: "We leave the similar verification for multiplication as an exercise." -- This is what we need to check for multiplication to "descend" (or "lift" as Lean -- calls it) to a well-defined function on the quotient. lemma mul_aux_lemma : ∀ x₁ x₂ y₁ y₂ : ℕ × ℕ, (x₁ ≈ y₁) → (x₂ ≈ y₂) → mul_aux x₁ x₂ = mul_aux y₁ y₂ := begin sorry end -- It's much easier from here on -- definition of multiplication def mul : myint → myint → myint := quotient.lift₂ mul_aux mul_aux_lemma instance : has_mul myint := ⟨mul⟩ -- true by definition lemma mul_def (i j k l : ℕ) : (⟦(i, j)⟧ * ⟦(k, l)⟧ : myint) = ⟦(sorry, sorry)⟧ := begin sorry end lemma mul_assoc (x y z : myint) : (x * y) * z = x * (y * z) := begin sorry end def one : myint := ⟦(sorry, sorry)⟧ instance : has_one myint := ⟨myint.one⟩ -- true by definition lemma one_def : (1 : myint) = sorry := begin sorry end lemma one_mul (x : myint) : 1 * x = x := begin sorry end lemma mul_one (x : myint) : x * 1 = x := begin sorry end lemma mul_comm (x y : myint) : x * y = y * x := begin sorry end lemma mul_add (x y z : myint) : x * (y + z) = x * y + x * z := begin sorry end lemma add_mul (x y z : myint) : (x + y) * z = x * z + y * z := begin sorry end -- The integers are a commutative ring -- (that is, they satisfy the axioms we just proved) instance : comm_ring myint := { mul := (*), mul_assoc := mul_assoc, one := 1, one_mul := one_mul, mul_one := mul_one, left_distrib := mul_add, right_distrib := add_mul, mul_comm := mul_comm, ..myint.add_comm_group } end myint
cff284ec1d93eac74d25f4f71396e5b136f255d0
87d5955e1100ba73f6b56149b659e85116fa215b
/group_theory/extra.lean
bb32325d79381db319a895f1a6e2dbb0d38751c7
[ "Apache-2.0" ]
permissive
avigad/leanproved
4d15a05f8544a67b9f589c5e656358e1485aef3c
3cf0f5dda3ace82f6b7cdc159528ce3ebaef159f
refs/heads/master
1,611,368,103,158
1,433,589,072,000
1,433,589,072,000
36,843,899
0
0
null
1,433,385,918,000
1,433,385,918,000
null
UTF-8
Lean
false
false
4,110
lean
/- Copyright (c) 2015 Haitao Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author : Haitao Zhang -/ -- These belong in the library somewhere. import data.list.basic data.finset.basic data.fintype.card -- renamed and_imp_eq -- theorem and_imp_curry (a b c : Prop) : (a ∧ b → c) = (a → b → c) := -- propext (iff.intro (λ Pl a b, Pl (and.intro a b)) -- (λ Pr Pand, Pr (and.left Pand) (and.right Pand))) -- changed to iff and named and_iff_right -- theorem and_discharge_left {a b : Prop} : a → (a ∧ b) = b := -- assume Pa, propext (iff.intro (assume Pab, and.elim_right Pab) -- (assume Pb, and.intro Pa Pb)) -- was already in the algebra.function as flip, but I renamed it to swap -- definition swap {A B C : Type} (f : A → B → C) : B → A → C := λ x y, f y x -- this is in algebra.order, not_lt_of_ge -- lemma not_lt_of_le {a b : nat} : a ≤ b → ¬ b < a := -- assume aleb, not.intro (assume blta, lt.irrefl a (lt_of_le_of_lt aleb blta)) -- made iff -- lemma injective_eq_inj_on_univ {f : A → B} : injective f = inj_on f univ := -- renamed to maps_to_univ_univ -- lemma univ_maps_to_univ {f : A → B} : maps_to f univ univ := -- note: I now made the first two arguments to "injective f" implicit in the library -- theorem injective_compose {g : B → C} {f : A → B} (Hg : injective g) (Hf : injective f) : injective (g ∘ f) := -- made iff -- lemma surjective_eq_surj_on_univ {f : A → B} : surjective f = surj_on f univ univ := -- made iff -- lemma bijective_eq_bij_on_univ {f : A → B} : bijective f = bij_on f univ univ := -- renamed injective_id, surjective_id, bijective_id -- theorem id_is_inj : injective (@id A) -- theorem id_is_surj : surjective (@id A) -- theorem id_is_bij : bijective (@id A) -- this doesn't require decidable equality, but it does require function extensionality -- lemma left_inv_of_right_inv_of_inj -- {A : Type} [h : decidable_eq A] {B : Type} {f : A → B} {g : B → A} -- : injective f → f∘g = id → g∘f = id -- instead, I put the version that does not require extensionality, in algebra.function, -- right_inverse_of_injective_of_left_inverse -- see also left_inverse_of_surjective_of_right_inverse -- I also did the corresponding versions in set.function and set.map -- this one seemed too special-purpose to me -- lemma comm_one (a : A) : a*1 = 1*a -- renamed mul_eq_one_iff_mul_eq_one -- lemma comm_mul_eq_one (a b : A) : a*b = 1 = (b*a = 1) := -- renamed find_not_mem to not_mem_of_find_eq_length -- renamed find_mem to find_lt_length namespace list -- useful for inverting function on a finite domain section kth open nat variable {A : Type} definition kth : ∀ k (l : list A), k < length l → A | k [] := begin rewrite length_nil, intro Pltz, exact absurd Pltz !not_lt_zero end | 0 (a::l) := λ P, a | (k+1) (a::l):= by rewrite length_cons; intro Plt; exact kth k l (lt_of_succ_lt_succ Plt) lemma kth_zero_of_cons {a} (l : list A) (P : 0 < length (a::l)) : kth 0 (a::l) P = a := rfl lemma kth_succ_of_cons {a} k (l : list A) (P : k+1 < length (a::l)) : kth (succ k) (a::l) P = kth k l (lt_of_succ_lt_succ P) := rfl variable [deceqA : decidable_eq A] include deceqA -- find_lt_length can be used to generate a proof of Found if a ∈ l. -- "kth (find a l) l Found" can be used to retrieve what is found. While that may seem -- silly since we already have what we are looking for in "a", -- "let elts := elems A, k := find b (map f elts) in kth k elts Found" -- would allow us to use it as a map to reverse a finite function. end kth end list -- less_than renamings (everything in namespace less_than) : -- lt_dinj -> dinj_lt -- lt_inv -> val_mk -- updto_nodup -> nodup_upto -- upto_complete -> mem_upto -- upto_nil -> upto_zero -- upto_map_eq_upto -> map_val_upto -- upto_length -> length_upto -- card_univ_lt_type -> card_less_than
e4e66d1bcd32e4e786c49863f0ac310b2528fc19
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/tactic/abel.lean
a7fb931bd1e6e0ce9661d0bebca7eddbcdca1f55
[ "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
17,321
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import tactic.norm_num /-! # The `abel` tactic Evaluate expressions in the language of additive, commutative monoids and groups. -/ namespace tactic namespace abel /-- The `context` for a call to `abel`. Stores a few options for this call, and caches some common subexpressions such as typeclass instances and `0 : α`. -/ meta structure context := (red : transparency) (α : expr) (univ : level) (α0 : expr) (is_group : bool) (inst : expr) /-- Populate a `context` object for evaluating `e`, up to reducibility level `red`. -/ meta def mk_context (red : transparency) (e : expr) : tactic context := do α ← infer_type e, c ← mk_app ``add_comm_monoid [α] >>= mk_instance, cg ← try_core (mk_app ``add_comm_group [α] >>= mk_instance), u ← mk_meta_univ, infer_type α >>= unify (expr.sort (level.succ u)), u ← get_univ_assignment u, α0 ← expr.of_nat α 0, match cg with | (some cg) := return ⟨red, α, u, α0, tt, cg⟩ | _ := return ⟨red, α, u, α0, ff, c⟩ end /-- Apply the function `n : ∀ {α} [inst : add_whatever α], _` to the implicit parameters in the context, and the given list of arguments. -/ meta def context.app (c : context) (n : name) (inst : expr) : list expr → expr := (@expr.const tt n [c.univ] c.α inst).mk_app /-- Apply the function `n : ∀ {α} [inst α], _` to the implicit parameters in the context, and the given list of arguments. Compared to `context.app`, this takes the name of the typeclass, rather than an inferred typeclass instance. -/ meta def context.mk_app (c : context) (n inst : name) (l : list expr) : tactic expr := do m ← mk_instance ((expr.const inst [c.univ] : expr) c.α), return $ c.app n m l /-- Add the letter "g" to the end of the name, e.g. turning `term` into `termg`. This is used to choose between declarations taking `add_comm_monoid` and those taking `add_comm_group` instances. -/ meta def add_g : name → name | (name.mk_string s p) := name.mk_string (s ++ "g") p | n := n /-- Apply the function `n : ∀ {α} [add_comm_{monoid,group} α]` to the given list of arguments. Will use the `add_comm_{monoid,group}` instance that has been cached in the context. -/ meta def context.iapp (c : context) (n : name) : list expr → expr := c.app (if c.is_group then add_g n else n) c.inst def term {α} [add_comm_monoid α] (n : ℕ) (x a : α) : α := n • x + a def termg {α} [add_comm_group α] (n : ℤ) (x a : α) : α := n • x + a /-- Evaluate a term with coefficient `n`, atom `x` and successor terms `a`. -/ meta def context.mk_term (c : context) (n x a : expr) : expr := c.iapp ``term [n, x, a] /-- Interpret an integer as a coefficient to a term. -/ meta def context.int_to_expr (c : context) (n : ℤ) : tactic expr := expr.of_int (if c.is_group then `(ℤ) else `(ℕ)) n meta inductive normal_expr : Type | zero (e : expr) : normal_expr | nterm (e : expr) (n : expr × ℤ) (x : expr) (a : normal_expr) : normal_expr meta def normal_expr.e : normal_expr → expr | (normal_expr.zero e) := e | (normal_expr.nterm e _ _ _) := e meta instance : has_coe normal_expr expr := ⟨normal_expr.e⟩ meta instance : has_coe_to_fun normal_expr (λ _, expr → expr) := ⟨λ e, ⇑(e : expr)⟩ meta def normal_expr.term' (c : context) (n : expr × ℤ) (x : expr) (a : normal_expr) : normal_expr := normal_expr.nterm (c.mk_term n.1 x a) n x a meta def normal_expr.zero' (c : context) : normal_expr := normal_expr.zero c.α0 meta def normal_expr.to_list : normal_expr → list (ℤ × expr) | (normal_expr.zero _) := [] | (normal_expr.nterm _ (_, n) x a) := (n, x) :: a.to_list open normal_expr meta def normal_expr.to_string (e : normal_expr) : string := " + ".intercalate $ (to_list e).map $ λ ⟨n, e⟩, to_string n ++ " • (" ++ to_string e ++ ")" meta def normal_expr.pp (e : normal_expr) : tactic format := do l ← (to_list e).mmap (λ ⟨n, e⟩, do pe ← pp e, return (to_fmt n ++ " • (" ++ pe ++ ")")), return $ format.join $ l.intersperse ↑" + " meta instance : has_to_tactic_format normal_expr := ⟨normal_expr.pp⟩ meta def normal_expr.refl_conv (e : normal_expr) : tactic (normal_expr × expr) := do p ← mk_eq_refl e, return (e, p) theorem const_add_term {α} [add_comm_monoid α] (k n x a a') (h : k + a = a') : k + @term α _ n x a = term n x a' := by simp [h.symm, term]; ac_refl theorem const_add_termg {α} [add_comm_group α] (k n x a a') (h : k + a = a') : k + @termg α _ n x a = termg n x a' := by simp [h.symm, termg]; ac_refl theorem term_add_const {α} [add_comm_monoid α] (n x a k a') (h : a + k = a') : @term α _ n x a + k = term n x a' := by simp [h.symm, term, add_assoc] theorem term_add_constg {α} [add_comm_group α] (n x a k a') (h : a + k = a') : @termg α _ n x a + k = termg n x a' := by simp [h.symm, termg, add_assoc] theorem term_add_term {α} [add_comm_monoid α] (n₁ x a₁ n₂ a₂ n' a') (h₁ : n₁ + n₂ = n') (h₂ : a₁ + a₂ = a') : @term α _ n₁ x a₁ + @term α _ n₂ x a₂ = term n' x a' := by simp [h₁.symm, h₂.symm, term, add_nsmul]; ac_refl theorem term_add_termg {α} [add_comm_group α] (n₁ x a₁ n₂ a₂ n' a') (h₁ : n₁ + n₂ = n') (h₂ : a₁ + a₂ = a') : @termg α _ n₁ x a₁ + @termg α _ n₂ x a₂ = termg n' x a' := by simp [h₁.symm, h₂.symm, termg, add_zsmul]; ac_refl theorem zero_term {α} [add_comm_monoid α] (x a) : @term α _ 0 x a = a := by simp [term, zero_nsmul, one_nsmul] theorem zero_termg {α} [add_comm_group α] (x a) : @termg α _ 0 x a = a := by simp [termg] meta def eval_add (c : context) : normal_expr → normal_expr → tactic (normal_expr × expr) | (zero _) e₂ := do p ← mk_app ``zero_add [e₂], return (e₂, p) | e₁ (zero _) := do p ← mk_app ``add_zero [e₁], return (e₁, p) | he₁@(nterm e₁ n₁ x₁ a₁) he₂@(nterm e₂ n₂ x₂ a₂) := (do is_def_eq x₁ x₂ c.red, (n', h₁) ← mk_app ``has_add.add [n₁.1, n₂.1] >>= norm_num.eval_field, (a', h₂) ← eval_add a₁ a₂, let k := n₁.2 + n₂.2, let p₁ := c.iapp ``term_add_term [n₁.1, x₁, a₁, n₂.1, a₂, n', a', h₁, h₂], if k = 0 then do p ← mk_eq_trans p₁ (c.iapp ``zero_term [x₁, a']), return (a', p) else return (term' c (n', k) x₁ a', p₁)) <|> if expr.lex_lt x₁ x₂ then do (a', h) ← eval_add a₁ he₂, return (term' c n₁ x₁ a', c.iapp ``term_add_const [n₁.1, x₁, a₁, e₂, a', h]) else do (a', h) ← eval_add he₁ a₂, return (term' c n₂ x₂ a', c.iapp ``const_add_term [e₁, n₂.1, x₂, a₂, a', h]) theorem term_neg {α} [add_comm_group α] (n x a n' a') (h₁ : -n = n') (h₂ : -a = a') : -@termg α _ n x a = termg n' x a' := by simp [h₂.symm, h₁.symm, termg]; ac_refl meta def eval_neg (c : context) : normal_expr → tactic (normal_expr × expr) | (zero e) := do p ← c.mk_app ``neg_zero ``neg_zero_class [], return (zero' c, p) | (nterm e n x a) := do (n', h₁) ← mk_app ``has_neg.neg [n.1] >>= norm_num.eval_field, (a', h₂) ← eval_neg a, return (term' c (n', -n.2) x a', c.app ``term_neg c.inst [n.1, x, a, n', a', h₁, h₂]) def nat_smul_inst {α} [add_comm_monoid α] : has_smul ℕ α := by apply_instance def nat_smul_instg {α} [add_comm_group α] : has_smul ℕ α := by apply_instance def int_smul_instg {α} [add_comm_group α] : has_smul ℤ α := by apply_instance def smul {α} [add_comm_monoid α] (n : ℕ) (x : α) : α := n • x def smulg {α} [add_comm_group α] (n : ℤ) (x : α) : α := n • x theorem zero_smul {α} [add_comm_monoid α] (c) : smul c (0 : α) = 0 := by simp [smul, nsmul_zero] theorem zero_smulg {α} [add_comm_group α] (c) : smulg c (0 : α) = 0 := by simp [smulg, zsmul_zero] theorem term_smul {α} [add_comm_monoid α] (c n x a n' a') (h₁ : c * n = n') (h₂ : smul c a = a') : smul c (@term α _ n x a) = term n' x a' := by simp [h₂.symm, h₁.symm, term, smul, nsmul_add, mul_nsmul] theorem term_smulg {α} [add_comm_group α] (c n x a n' a') (h₁ : c * n = n') (h₂ : smulg c a = a') : smulg c (@termg α _ n x a) = termg n' x a' := by simp [h₂.symm, h₁.symm, termg, smulg, zsmul_add, mul_zsmul] meta def eval_smul (c : context) (k : expr × ℤ) : normal_expr → tactic (normal_expr × expr) | (zero _) := return (zero' c, c.iapp ``zero_smul [k.1]) | (nterm e n x a) := do (n', h₁) ← mk_app ``has_mul.mul [k.1, n.1] >>= norm_num.eval_field, (a', h₂) ← eval_smul a, return (term' c (n', k.2 * n.2) x a', c.iapp ``term_smul [k.1, n.1, x, a, n', a', h₁, h₂]) theorem term_atom {α} [add_comm_monoid α] (x : α) : x = term 1 x 0 := by simp [term] theorem term_atomg {α} [add_comm_group α] (x : α) : x = termg 1 x 0 := by simp [termg] meta def eval_atom (c : context) (e : expr) : tactic (normal_expr × expr) := do n1 ← c.int_to_expr 1, return (term' c (n1, 1) e (zero' c), c.iapp ``term_atom [e]) lemma unfold_sub {α} [subtraction_monoid α] (a b c : α) (h : a + -b = c) : a - b = c := by rw [sub_eq_add_neg, h] theorem unfold_smul {α} [add_comm_monoid α] (n) (x y : α) (h : smul n x = y) : n • x = y := h theorem unfold_smulg {α} [add_comm_group α] (n : ℕ) (x y : α) (h : smulg (int.of_nat n) x = y) : (n : ℤ) • x = y := h theorem unfold_zsmul {α} [add_comm_group α] (n : ℤ) (x y : α) (h : smulg n x = y) : n • x = y := h lemma subst_into_smul {α} [add_comm_monoid α] (l r tl tr t) (prl : l = tl) (prr : r = tr) (prt : @smul α _ tl tr = t) : smul l r = t := by simp [prl, prr, prt] lemma subst_into_smulg {α} [add_comm_group α] (l r tl tr t) (prl : l = tl) (prr : r = tr) (prt : @smulg α _ tl tr = t) : smulg l r = t := by simp [prl, prr, prt] lemma subst_into_smul_upcast {α} [add_comm_group α] (l r tl zl tr t) (prl₁ : l = tl) (prl₂ : ↑tl = zl) (prr : r = tr) (prt : @smulg α _ zl tr = t) : smul l r = t := by simp [← prt, prl₁, ← prl₂, prr, smul, smulg] /-- Normalize a term `orig` of the form `smul e₁ e₂` or `smulg e₁ e₂`. Normalized terms use `smul` for monoids and `smulg` for groups, so there are actually four cases to handle: * Using `smul` in a monoid just simplifies the pieces using `subst_into_smul` * Using `smulg` in a group just simplifies the pieces using `subst_into_smulg` * Using `smul a b` in a group requires converting `a` from a nat to an int and then simplifying `smulg ↑a b` using `subst_into_smul_upcast` * Using `smulg` in a monoid is impossible (or at least out of scope), because you need a group argument to write a `smulg` term -/ meta def eval_smul' (c : context) (eval : expr → tactic (normal_expr × expr)) (is_smulg : bool) (orig e₁ e₂ : expr) : tactic (normal_expr × expr) := do (e₁', p₁) ← norm_num.derive e₁ <|> refl_conv e₁, match if is_smulg then e₁'.to_int else coe <$> e₁'.to_nat with | some n := do (e₂', p₂) ← eval e₂, if c.is_group = is_smulg then do (e', p) ← eval_smul c (e₁', n) e₂', return (e', c.iapp ``subst_into_smul [e₁, e₂, e₁', e₂', e', p₁, p₂, p]) else do guardb c.is_group, ic ← mk_instance_cache `(ℤ), nc ← mk_instance_cache `(ℕ), (ic, zl) ← ic.of_int n, (_, _, _, p₁') ← norm_num.prove_nat_uncast ic nc zl, (e', p) ← eval_smul c (zl, n) e₂', return (e', c.app ``subst_into_smul_upcast c.inst [e₁, e₂, e₁', zl, e₂', e', p₁, p₁', p₂, p]) | none := eval_atom c orig end meta def eval (c : context) : expr → tactic (normal_expr × expr) | `(%%e₁ + %%e₂) := do (e₁', p₁) ← eval e₁, (e₂', p₂) ← eval e₂, (e', p') ← eval_add c e₁' e₂', p ← c.mk_app ``norm_num.subst_into_add ``has_add [e₁, e₂, e₁', e₂', e', p₁, p₂, p'], return (e', p) | `(%%e₁ - %%e₂) := do e₂' ← mk_app ``has_neg.neg [e₂], e ← mk_app ``has_add.add [e₁, e₂'], (e', p) ← eval e, p' ← c.mk_app ``unfold_sub ``subtraction_monoid [e₁, e₂, e', p], return (e', p') | `(- %%e) := do (e₁, p₁) ← eval e, (e₂, p₂) ← eval_neg c e₁, p ← c.mk_app ``norm_num.subst_into_neg ``has_neg [e, e₁, e₂, p₁, p₂], return (e₂, p) | `(add_monoid.nsmul %%e₁ %%e₂) := do n ← if c.is_group then mk_app ``int.of_nat [e₁] else return e₁, (e', p) ← eval $ c.iapp ``smul [n, e₂], return (e', c.iapp ``unfold_smul [e₁, e₂, e', p]) | `(sub_neg_monoid.zsmul %%e₁ %%e₂) := do guardb c.is_group, (e', p) ← eval $ c.iapp ``smul [e₁, e₂], return (e', c.app ``unfold_zsmul c.inst [e₁, e₂, e', p]) | e@`(@has_smul.smul nat %%α %%inst %%e₁ %%e₂) := do let inst' := c.iapp ``nat_smul_inst [], mcond (succeeds (is_def_eq inst inst')) (eval_smul' c eval ff e e₁ e₂) (eval_atom c e) | e@`(@has_smul.smul int %%α %%inst %%e₁ %%e₂) := do -- if we're not a group there's no canonical instance available tt ← pure c.is_group | eval_atom c e, let inst' := c.app ``int_smul_instg c.inst [], mcond (succeeds (is_def_eq inst inst')) (eval_smul' c eval tt e e₁ e₂) (eval_atom c e) | e@`(smul %%e₁ %%e₂) := eval_smul' c eval ff e e₁ e₂ | e@`(smulg %%e₁ %%e₂) := eval_smul' c eval tt e e₁ e₂ | e@`(@has_zero.zero _ _) := mcond (succeeds (is_def_eq e c.α0)) (mk_eq_refl c.α0 >>= λ p, pure (zero' c, p)) (eval_atom c e) | e := eval_atom c e meta def eval' (c : context) (e : expr) : tactic (expr × expr) := do (e', p) ← eval c e, return (e', p) @[derive has_reflect] inductive normalize_mode | raw | term instance : inhabited normalize_mode := ⟨normalize_mode.term⟩ meta def normalize (red : transparency) (mode := normalize_mode.term) (e : expr) : tactic (expr × expr) := do pow_lemma ← simp_lemmas.mk.add_simp ``pow_one, let lemmas := match mode with | normalize_mode.term := [``term.equations._eqn_1, ``termg.equations._eqn_1, ``add_zero, ``one_nsmul, ``one_zsmul, ``zsmul_zero] | _ := [] end, lemmas ← lemmas.mfoldl simp_lemmas.add_simp simp_lemmas.mk, (_, e', pr) ← ext_simplify_core () {} simp_lemmas.mk (λ _, failed) (λ _ _ _ _ e, do c ← mk_context red e, (new_e, pr) ← match mode with | normalize_mode.raw := eval' c | normalize_mode.term := trans_conv (eval' c) (λ e, do (e', prf, _) ← simplify lemmas [] e, return (e', prf)) end e, guard (¬ new_e =ₐ e), return ((), new_e, some pr, ff)) (λ _ _ _ _ _, failed) `eq e, return (e', pr) end abel namespace interactive open tactic.abel setup_tactic_parser /-- Tactic for solving equations in the language of *additive*, commutative monoids and groups. This version of `abel` fails if the target is not an equality that is provable by the axioms of commutative monoids/groups. `abel1!` will use a more aggressive reducibility setting to identify atoms. This can prove goals that `abel` cannot, but is more expensive. -/ meta def abel1 (red : parse (tk "!")?) : tactic unit := do `(%%e₁ = %%e₂) ← target, c ← mk_context (if red.is_some then semireducible else reducible) e₁, (e₁', p₁) ← eval c e₁, (e₂', p₂) ← eval c e₂, is_def_eq e₁' e₂', p ← mk_eq_symm p₂ >>= mk_eq_trans p₁, tactic.exact p meta def abel.mode : lean.parser abel.normalize_mode := with_desc "(raw|term)?" $ do mode ← ident?, match mode with | none := return abel.normalize_mode.term | some `term := return abel.normalize_mode.term | some `raw := return abel.normalize_mode.raw | _ := failed end /-- Evaluate expressions in the language of *additive*, commutative monoids and groups. It attempts to prove the goal outright if there is no `at` specifier and the target is an equality, but if this fails, it falls back to rewriting all monoid expressions into a normal form. If there is an `at` specifier, it rewrites the given target into a normal form. `abel!` will use a more aggressive reducibility setting to identify atoms. This can prove goals that `abel` cannot, but is more expensive. ```lean example {α : Type*} {a b : α} [add_comm_monoid α] : a + (b + a) = a + a + b := by abel example {α : Type*} {a b : α} [add_comm_group α] : (a + b) - ((b + a) + a) = -a := by abel example {α : Type*} {a b : α} [add_comm_group α] (hyp : a + a - a = b - b) : a = 0 := by { abel at hyp, exact hyp } example {α : Type*} {a b : α} [add_comm_group α] : (a + b) - (id a + b) = 0 := by abel! ``` -/ meta def abel (red : parse (tk "!")?) (SOP : parse abel.mode) (loc : parse location) : tactic unit := match loc with | interactive.loc.ns [none] := abel1 red | _ := failed end <|> do ns ← loc.get_locals, let red := if red.is_some then semireducible else reducible, tt ← tactic.replace_at (normalize red SOP) ns loc.include_goal | fail "abel failed to simplify", when loc.include_goal $ try tactic.reflexivity add_tactic_doc { name := "abel", category := doc_category.tactic, decl_names := [`tactic.interactive.abel], tags := ["arithmetic", "decision procedure"] } end interactive end tactic
8a7b94143438739874aedc0e191a691a41773c54
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/category_theory/category/galois_connection.lean
2c369976fe89eb911c936475bf239841f16bfc07
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
1,343
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Stephen Morgan, Scott Morrison, Johannes Hölzl, Reid Barton -/ import category_theory.category.preorder import category_theory.adjunction.basic import order.galois_connection /-! # Galois connections between preorders are adjunctions. * `galois_connection.adjunction` is the adjunction associated to a galois connection. -/ universes u v section variables {X : Type u} {Y : Type v} [preorder X] [preorder Y] /-- A galois connection between preorders induces an adjunction between the associated categories. -/ def galois_connection.adjunction {l : X → Y} {u : Y → X} (gc : galois_connection l u) : gc.monotone_l.functor ⊣ gc.monotone_u.functor := category_theory.adjunction.mk_of_hom_equiv { hom_equiv := λ X Y, ⟨λ f, (gc.le_u f.le).hom, λ f, (gc.l_le f.le).hom, by tidy, by tidy⟩ } end namespace category_theory variables {X : Type u} {Y : Type v} [preorder X] [preorder Y] /-- An adjunction between preorder categories induces a galois connection. -/ lemma adjunction.gc {L : X ⥤ Y} {R : Y ⥤ X} (adj : L ⊣ R) : galois_connection L.obj R.obj := λ x y, ⟨λ h, ((adj.hom_equiv x y).to_fun h.hom).le, λ h, ((adj.hom_equiv x y).inv_fun h.hom).le⟩ end category_theory
74233bc1b3e73da65626ff619231b67c437ceedb
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/premises.lean
76b63046cfe3719ffcccc63a558386dbe5524d5d
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
156
lean
variable a : Prop variables b c : Prop premise Ha : a premises (Hb : b) (Hc : c) theorem tst : a ∧ b ∧ c := and.intro Ha (and.intro Hb Hc) check tst
5925de7bc02c6ff2a262043b0e784508f5e1942a
7007bb645068e0b6b859aab9da7cf5c1c79e98be
/tests/lean/widget/widget1.lean
f2d77e77389863102095f0e432dcc14fb9a9a086
[ "Apache-2.0" ]
permissive
EdAyers/lean
7d3fb852380bc386545ebc119b7d03c128c3ce1c
be72c8dc527a062e243a408480f487a55b06cb0a
refs/heads/master
1,624,443,179,694
1,592,837,958,000
1,592,837,958,000
154,972,348
0
0
Apache-2.0
1,557,768,267,000
1,540,649,772,000
C++
UTF-8
Lean
false
false
2,972
lean
open widget meta class has_show_html (π : Type) := (show_html' {α : Type}: π → html α) meta def show_html {π α : Type} [has_show_html π] : π → html α := has_show_html.show_html' meta instance string_show_html : has_show_html string := ⟨λ α p, html.of_string p⟩ meta class has_to_editor (π : Type) := (comp : π → html π) meta def to_editor (π : Type) [inhabited π] [has_to_editor π] : component unit π := component.mk π π (λ _ last, inhabited.default π <| last) (λ _ po pn, (pn, some pn)) (λ _ c, [has_to_editor.comp c]) (λ _ _, ff) meta instance string_editor : has_to_editor string := ⟨λ s, textbox s (λ s', s')⟩ inductive todo_list_action (α : Type) | insert : α → todo_list_action | delete : nat → todo_list_action meta def todo_list (α : Type) [inhabited α] [decidable_eq α] [has_show_html α] [has_to_editor α] (initial : list α) : component unit empty := let starts : list (ℕ × α) := initial.map_with_index prod.mk in component.mk (todo_list_action α) (nat × list (nat × α)) (λ _ last, ⟨starts.length, starts⟩ <| last) (λ ⟨⟩ ⟨i,items⟩ b, match b with | (todo_list_action.insert a) := ((i+1, items ++ [(i,a)]), none) | (todo_list_action.delete j) := ((i, items.filter (λ p, p.1 ≠ j)), none) end ) (λ ⟨⟩ ⟨i,items⟩, [ h "div" [className "mw6"] $ items.map (λ ⟨k,a⟩, h "div" [className "flex justify-between items-center w-100 bb b--black-20 pb2 mt2", key k] [ h "div" [className "dtc v-mid"] [show_html a], h "button" [ className "dtc v-mid f6 button-reset bg-white ba b--black-20 dim pointer pv1 w2", on_click (λ _, todo_list_action.delete $ k)] ["x"] ] ) ++ [ h "hr" [] [] , h "div" [className "flex justify-between items-center w-100 bb b--black-20 pb2 mt2", key "add row"] [ html.map_action (λ x, todo_list_action.insert x) $ html.of_component () $ component.mk (option α) α (λ p last, inhabited.default α <| last) (λ ⟨⟩ x b, match b with none := (inhabited.default α, some x) | (some x') := (x', none) end) (λ ⟨⟩ x, [ h "div" [className "dtc v-mid"] [html.map_action some $ has_to_editor.comp x] , h "button" [className "dtc v-mid f6 button-reset bg-white ba b--black-20 dim pointer pv1 w2" , on_click (λ _, none)] ["+"] ]) (λ _ _, ff)]]]) (λ _ _, ff) meta def string_todo_list : component tactic_state string := component.map_action (λ (o : empty), empty.rec (λ _, _) o) $ component.map_props (λ p, ()) $ todo_list string ["make some tasks", "delete some tasks"] #html string_todo_list
da1ca6499591d92d5ef01f1424c54d9fa1e2e87b
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/stage0/src/Lean/Elab/Deriving/Util.lean
0b8240ea150c05e955302803b38688e982f42830
[ "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
5,728
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.Parser.Term import Lean.Elab.Term namespace Lean.Elab.Deriving open Meta def implicitBinderF := Parser.Term.implicitBinder def instBinderF := Parser.Term.instBinder def explicitBinderF := Parser.Term.explicitBinder def mkInductArgNames (indVal : InductiveVal) : TermElabM (Array Name) := do forallTelescopeReducing indVal.type fun xs _ => do let mut argNames := #[] for x in xs do let localDecl ← getLocalDecl x.fvarId! let paramName ← mkFreshUserName localDecl.userName.eraseMacroScopes argNames := argNames.push paramName pure argNames def mkInductiveApp (indVal : InductiveVal) (argNames : Array Name) : TermElabM Syntax := let f := mkIdent indVal.name let args := argNames.map mkIdent `(@$f $args*) def mkImplicitBinders (argNames : Array Name) : TermElabM (Array Syntax) := argNames.mapM fun argName => `(implicitBinderF| { $(mkIdent argName) }) def mkInstImplicitBinders (className : Name) (indVal : InductiveVal) (argNames : Array Name) : TermElabM (Array Syntax) := forallBoundedTelescope indVal.type indVal.numParams fun xs _ => do let mut binders := #[] for i in [:xs.size] do try let x := xs[i] let c ← mkAppM className #[x] if (← isTypeCorrect c) then let argName := argNames[i] let binder ← `(instBinderF| [ $(mkIdent className):ident $(mkIdent argName):ident ]) binders := binders.push binder catch _ => pure () return binders structure Context where typeInfos : Array InductiveVal auxFunNames : Array Name usePartial : Bool def mkContext (fnPrefix : String) (typeName : Name) : TermElabM Context := do let indVal ← getConstInfoInduct typeName let mut typeInfos := #[] for typeName in indVal.all do typeInfos ← typeInfos.push (← getConstInfoInduct typeName) let mut auxFunNames := #[] for typeName in indVal.all do match typeName.eraseMacroScopes with | Name.str _ t _ => auxFunNames := auxFunNames.push (← mkFreshUserName <| Name.mkSimple <| fnPrefix ++ t) | _ => auxFunNames := auxFunNames.push (← mkFreshUserName `instFn) trace[Elab.Deriving.beq] "{auxFunNames}" let usePartial := indVal.isNested || typeInfos.size > 1 return { typeInfos := typeInfos auxFunNames := auxFunNames usePartial := usePartial } def mkLocalInstanceLetDecls (ctx : Context) (className : Name) (argNames : Array Name) : TermElabM (Array Syntax) := do let mut letDecls := #[] for i in [:ctx.typeInfos.size] do let indVal := ctx.typeInfos[i] let auxFunName := ctx.auxFunNames[i] let currArgNames ← mkInductArgNames indVal let numParams := indVal.numParams let currIndices := currArgNames[numParams:] let binders ← mkImplicitBinders currIndices let argNamesNew := argNames[:numParams] ++ currIndices let indType ← mkInductiveApp indVal argNamesNew let type ← `($(mkIdent className) $indType) let val ← `(⟨$(mkIdent auxFunName)⟩) let instName ← mkFreshUserName `localinst let letDecl ← `(Parser.Term.letDecl| $(mkIdent instName):ident $binders:implicitBinder* : $type := $val) letDecls := letDecls.push letDecl return letDecls def mkLet (letDecls : Array Syntax) (body : Syntax) : TermElabM Syntax := letDecls.foldrM (init := body) fun letDecl body => `(let $letDecl:letDecl; $body) def mkInstanceCmds (ctx : Context) (className : Name) (typeNames : Array Name) (useAnonCtor := true) : TermElabM (Array Syntax) := do let mut instances := #[] for i in [:ctx.typeInfos.size] do let indVal := ctx.typeInfos[i] if typeNames.contains indVal.name then let auxFunName := ctx.auxFunNames[i] let argNames ← mkInductArgNames indVal let binders ← mkImplicitBinders argNames let binders := binders ++ (← mkInstImplicitBinders className indVal argNames) let indType ← mkInductiveApp indVal argNames let type ← `($(mkIdent className) $indType) let val ← if useAnonCtor then `(⟨$(mkIdent auxFunName)⟩) else pure <| mkIdent auxFunName let instCmd ← `(instance $binders:implicitBinder* : $type := $val) instances := instances.push instCmd return instances def mkDiscr (varName : Name) : TermElabM Syntax := `(Parser.Term.matchDiscr| $(mkIdent varName):term) structure Header where binders : Array Syntax argNames : Array Name targetNames : Array Name def mkHeader (ctx : Context) (className : Name) (arity : Nat) (indVal : InductiveVal) : TermElabM Header := do let argNames ← mkInductArgNames indVal let binders ← mkImplicitBinders argNames let targetType ← mkInductiveApp indVal argNames let mut targetNames := #[] for i in [:arity] do targetNames := targetNames.push (← mkFreshUserName `x) let binders := binders ++ (← mkInstImplicitBinders className indVal argNames) let binders := binders ++ (← targetNames.mapM fun targetName => `(explicitBinderF| ($(mkIdent targetName) : $targetType))) return { binders := binders argNames := argNames targetNames := targetNames } def mkDiscrs (header : Header) (indVal : InductiveVal) : TermElabM (Array Syntax) := do let mut discrs := #[] -- add indices for argName in header.argNames[indVal.numParams:] do discrs := discrs.push (← mkDiscr argName) return discrs ++ (← header.targetNames.mapM mkDiscr) end Lean.Elab.Deriving
da1057daf5f1b430b25b316b58832807431bfa76
367134ba5a65885e863bdc4507601606690974c1
/src/algebra/group/commute.lean
bff5f4635db7712c97c89491fcaddc83a1f20d2e
[ "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
5,676
lean
/- Copyright (c) 2019 Neil Strickland. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Neil Strickland, Yury Kudryashov -/ import algebra.group.semiconj /-! # Commuting pairs of elements in monoids We define the predicate `commute a b := a * b = b * a` and provide some operations on terms `(h : commute a b)`. E.g., if `a`, `b`, and c are elements of a semiring, and that `hb : commute a b` and `hc : commute a c`. Then `hb.pow_left 5` proves `commute (a ^ 5) b` and `(hb.pow_right 2).add_right (hb.mul_right hc)` proves `commute a (b ^ 2 + b * c)`. Lean does not immediately recognise these terms as equations, so for rewriting we need syntax like `rw [(hb.pow_left 5).eq]` rather than just `rw [hb.pow_left 5]`. This file defines only a few operations (`mul_left`, `inv_right`, etc). Other operations (`pow_right`, field inverse etc) are in the files that define corresponding notions. ## Implementation details Most of the proofs come from the properties of `semiconj_by`. -/ /-- Two elements commute if `a * b = b * a`. -/ @[to_additive add_commute "Two elements additively commute if `a + b = b + a`"] def commute {S : Type*} [has_mul S] (a b : S) : Prop := semiconj_by a b b namespace commute section has_mul variables {S : Type*} [has_mul S] /-- Equality behind `commute a b`; useful for rewriting. -/ @[to_additive] protected theorem eq {a b : S} (h : commute a b) : a * b = b * a := h /-- Any element commutes with itself. -/ @[refl, simp, to_additive] protected theorem refl (a : S) : commute a a := eq.refl (a * a) /-- If `a` commutes with `b`, then `b` commutes with `a`. -/ @[symm, to_additive] protected theorem symm {a b : S} (h : commute a b) : commute b a := eq.symm h @[to_additive] protected theorem symm_iff {a b : S} : commute a b ↔ commute b a := ⟨commute.symm, commute.symm⟩ end has_mul section semigroup variables {S : Type*} [semigroup S] {a b c : S} /-- If `a` commutes with both `b` and `c`, then it commutes with their product. -/ @[simp, to_additive] theorem mul_right (hab : commute a b) (hac : commute a c) : commute a (b * c) := hab.mul_right hac /-- If both `a` and `b` commute with `c`, then their product commutes with `c`. -/ @[simp, to_additive] theorem mul_left (hac : commute a c) (hbc : commute b c) : commute (a * b) c := hac.mul_left hbc @[to_additive] protected lemma right_comm (h : commute b c) (a : S) : a * b * c = a * c * b := by simp only [mul_assoc, h.eq] @[to_additive] protected lemma left_comm (h : commute a b) (c) : a * (b * c) = b * (a * c) := by simp only [← mul_assoc, h.eq] end semigroup @[to_additive] protected theorem all {S : Type*} [comm_semigroup S] (a b : S) : commute a b := mul_comm a b section monoid variables {M : Type*} [monoid M] @[simp, to_additive] theorem one_right (a : M) : commute a 1 := semiconj_by.one_right a @[simp, to_additive] theorem one_left (a : M) : commute 1 a := semiconj_by.one_left a @[to_additive] theorem units_inv_right {a : M} {u : units M} : commute a u → commute a ↑u⁻¹ := semiconj_by.units_inv_right @[simp, to_additive] theorem units_inv_right_iff {a : M} {u : units M} : commute a ↑u⁻¹ ↔ commute a u := semiconj_by.units_inv_right_iff @[to_additive] theorem units_inv_left {u : units M} {a : M} : commute ↑u a → commute ↑u⁻¹ a := semiconj_by.units_inv_symm_left @[simp, to_additive] theorem units_inv_left_iff {u : units M} {a : M}: commute ↑u⁻¹ a ↔ commute ↑u a := semiconj_by.units_inv_symm_left_iff variables {u₁ u₂ : units M} @[to_additive] theorem units_coe : commute u₁ u₂ → commute (u₁ : M) u₂ := semiconj_by.units_coe @[to_additive] theorem units_of_coe : commute (u₁ : M) u₂ → commute u₁ u₂ := semiconj_by.units_of_coe @[simp, to_additive] theorem units_coe_iff : commute (u₁ : M) u₂ ↔ commute u₁ u₂ := semiconj_by.units_coe_iff end monoid section group variables {G : Type*} [group G] {a b : G} @[to_additive] theorem inv_right : commute a b → commute a b⁻¹ := semiconj_by.inv_right @[simp, to_additive] theorem inv_right_iff : commute a b⁻¹ ↔ commute a b := semiconj_by.inv_right_iff @[to_additive] theorem inv_left : commute a b → commute a⁻¹ b := semiconj_by.inv_symm_left @[simp, to_additive] theorem inv_left_iff : commute a⁻¹ b ↔ commute a b := semiconj_by.inv_symm_left_iff @[to_additive] theorem inv_inv : commute a b → commute a⁻¹ b⁻¹ := semiconj_by.inv_inv_symm @[simp, to_additive] theorem inv_inv_iff : commute a⁻¹ b⁻¹ ↔ commute a b := semiconj_by.inv_inv_symm_iff @[to_additive] protected theorem inv_mul_cancel (h : commute a b) : a⁻¹ * b * a = b := by rw [h.inv_left.eq, inv_mul_cancel_right] @[to_additive] theorem inv_mul_cancel_assoc (h : commute a b) : a⁻¹ * (b * a) = b := by rw [← mul_assoc, h.inv_mul_cancel] @[to_additive] protected theorem mul_inv_cancel (h : commute a b) : a * b * a⁻¹ = b := by rw [h.eq, mul_inv_cancel_right] @[to_additive] theorem mul_inv_cancel_assoc (h : commute a b) : a * (b * a⁻¹) = b := by rw [← mul_assoc, h.mul_inv_cancel] end group end commute section comm_group variables {G : Type*} [comm_group G] (a b : G) @[simp, to_additive] lemma mul_inv_cancel_comm : a * b * a⁻¹ = b := (commute.all a b).mul_inv_cancel @[simp, to_additive] lemma mul_inv_cancel_comm_assoc : a * (b * a⁻¹) = b := (commute.all a b).mul_inv_cancel_assoc @[simp, to_additive] lemma inv_mul_cancel_comm : a⁻¹ * b * a = b := (commute.all a b).inv_mul_cancel @[simp, to_additive] lemma inv_mul_cancel_comm_assoc : a⁻¹ * (b * a) = b := (commute.all a b).inv_mul_cancel_assoc end comm_group
fa30202402a503bebb68cbcdb399788e122190aa
957a80ea22c5abb4f4670b250d55534d9db99108
/tests/lean/run/listex2.lean
a12b3727b7770ddba9cc0f5756e84668554af865
[ "Apache-2.0" ]
permissive
GaloisInc/lean
aa1e64d604051e602fcf4610061314b9a37ab8cd
f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0
refs/heads/master
1,592,202,909,807
1,504,624,387,000
1,504,624,387,000
75,319,626
2
1
Apache-2.0
1,539,290,164,000
1,480,616,104,000
C++
UTF-8
Lean
false
false
2,400
lean
universe variable u constant in_tail {α : Type u} {a : α} (b : α) {l : list α} : a ∈ l → a ∈ b::l constant in_head {α : Type u} (a : α) (l : list α) : a ∈ a::l constant in_left {α : Type u} {a : α} {l : list α} (r : list α) : a ∈ l → a ∈ l ++ r constant in_right {α : Type u} {a : α} (l : list α) {r : list α} : a ∈ r → a ∈ l ++ r open expr tactic declare_trace search_mem_list meta def match_append (e : expr) : tactic (expr × expr) := do [_, _, l, r] ← match_app_of e ``has_append.append | failed, return (l, r) meta def match_cons (e : expr) : tactic (expr × expr) := do [_, a, t] ← match_app_of e ``list.cons | failed, return (a, t) meta def match_mem (e : expr) : tactic (expr × expr) := do [_, _, _, a, t] ← match_app_of e ``has_mem.mem | failed, return (a, t) meta def search_mem_list : expr → expr → tactic expr | a e := when_tracing `search_mem_list (do f₁ ← pp a, f₂ ← pp e, trace (to_fmt "search " ++ f₁ ++ to_fmt " in " ++ f₂)) >> (do m ← to_expr ``(%%a ∈ %%e), find_assumption m) <|> (do (l, r) ← match_append e, h ← search_mem_list a l, to_expr ``(in_left %%r %%h)) <|> (do (l, r) ← match_append e, h ← search_mem_list a r, to_expr ``(in_right %%l %%h)) <|> (do (b, t) ← match_cons e, is_def_eq a b, to_expr ``(in_head %%b %%t)) <|> (do (b, t) ← match_cons e, h ← search_mem_list a t, to_expr ``(in_tail %%b %%h)) meta def mk_mem_list : tactic unit := do t ← target, (a, l) ← match_mem t, search_mem_list a l >>= exact example (a b c : nat) : a ∈ [b, c] ++ [b, a, b] := by mk_mem_list example (a b c : nat) : a ∈ [b, c] ++ [b, a+0, b] := by mk_mem_list example (a b c : nat) : a ∈ [b, c] ++ [b, c, c] ++ [b, a+0, b] := by mk_mem_list example (a b c : nat) (l : list nat) : a ∈ l → a ∈ [b, c] ++ b::l := by tactic.intros >> mk_mem_list set_option trace.search_mem_list true lemma ex1 (a b c : nat) (l₁ l₂ : list nat) : a ∈ l₁ → a ∈ b::b::c::l₂ ++ b::c::l₁ ++ [c, c, b] := by tactic.intros >> mk_mem_list set_option trace.smt.ematch true /- Using ematching -/ lemma ex2 (a b c : nat) (l₁ l₂ : list nat) : a ∈ l₁ → a ∈ b::b::c::l₂ ++ b::c::l₁ ++ [c, c, b] := begin [smt] intros, add_lemma [in_left, in_right, in_head, in_tail], repeat {ematch} -- It will loop if there is a matching loop end
93f72a187d74ad64372ba1ec205bd861c409a9fa
ea5678cc400c34ff95b661fa26d15024e27ea8cd
/test4.lean
be732a0ccd2cd1d3ca0cf567cf92a1db9f18e0b4
[]
no_license
ChrisHughes24/leanstuff
dca0b5349c3ed893e8792ffbd98cbcadaff20411
9efa85f72efaccd1d540385952a6acc18fce8687
refs/heads/master
1,654,883,241,759
1,652,873,885,000
1,652,873,885,000
134,599,537
1
0
null
null
null
null
UTF-8
Lean
false
false
2,564
lean
-- let's define the real numbers to be a number system which satisfies -- the basic properties of the real numbers which we will need. noncomputable theory constant real : Type @[instance] constant real_field : linear_ordered_field real -- This piece of magic means that "real" now behaves a lot like -- the real numbers. In particular we now have a bunch -- of theorems: example : ∀ x y : real, x * y = y * x := mul_comm variable x : real variable n : nat -- We do _not_ have powers though. So we need to make them. open nat definition natural_power : real → nat → real | x 0 := 1 | x (succ n) := (natural_power x n) * x theorem T : ∀ x:real, ∀ m n:nat, natural_power x (m+n) = natural_power x m *natural_power x n := begin assume x m n, induction n with n H, unfold natural_power, rw [add_zero, mul_one], unfold natural_power, rw [H, mul_assoc], end theorem T2 : ∀ x: real, ∀ m n : nat, natural_power (natural_power x m) n = natural_power x (m*n) := begin assume x m n, induction n with n H, unfold natural_power, rw [mul_zero, eq_comm], unfold natural_power, rw [succ_eq_add_one, mul_add, mul_one, add_one], unfold natural_power, rw[T, H], end theorem T3 : ∀ x y:real, ∀ n:nat, (natural_power x n) * (natural_power y n) = natural_power (x*y) n := begin assume x y n, induction n with n H, unfold natural_power, simp, unfold natural_power, cc, end constant nth_root (x : real) (n : nat) : (x>0) → (n>0) → real axiom is_nth_root (x : real) (n : nat) (Hx : x>0) (Hn : n>0) : natural_power (nth_root x n Hx Hn) n = x definition rational_power_v0 (x : real) (n : nat) (d : nat) (Hx : x > 0) (Hd : d > 0) : real := natural_power (nth_root x d Hx Hd) n theorem T4 : ∀ x y:real,∀ Hx:x≥0,∀ Hy:y≥0,∀ n:nat,natural_power x (n+1) = natural_power y (n+1) → x = y:= begin assume x y Hx Hy n, induction n with n H, rw[add_one], unfold natural_power, rw[mul_comm,mul_one,mul_comm,mul_one], assume H1, exact(H1), unfold natural_power, assume H2, calc natural_power x n * x * x = natural_power y n * y * y : H2 ... = end /- theorem T5 : ∀ x:real, ∀ n d k:nat, ∀ Hx: x > 0, ∀ Hd: d >0, ∀ Hkd: (k*d) >0, rational_power_v0 x n d Hx Hd = rational_power_v0 x (k*n) (k*d) Hx Hkd:= begin assume x n d k Hx Hd Hkd, unfold rational_power_v0, rw[← T2], have H1 :natural_power (rational_power_v0 x n d Hx Hd) d= natural_power x n := begin unfold rational_power_v0, rw[T2, mul_comm,← T2,is_nth_root], end, let y:real := x, have H1 y=x, begin end end -/
c0ede06c6f1acd6329d8958dddcc7d9063a3dc5e
432d948a4d3d242fdfb44b81c9e1b1baacd58617
/src/category_theory/opposites.lean
54720541d047bb962dcf7b3992aafd9524245cb1
[ "Apache-2.0" ]
permissive
JLimperg/aesop3
306cc6570c556568897ed2e508c8869667252e8a
a4a116f650cc7403428e72bd2e2c4cda300fe03f
refs/heads/master
1,682,884,916,368
1,620,320,033,000
1,620,320,033,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,880
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 category_theory.types import category_theory.equivalence universes v₁ v₂ u₁ u₂ -- morphism levels before object levels. See note [category_theory universes]. open opposite variables {C : Type u₁} section quiver variables [quiver.{v₁} C] lemma quiver.hom.op_inj {X Y : C} : function.injective (quiver.hom.op : (X ⟶ Y) → (op Y ⟶ op X)) := λ _ _ H, congr_arg quiver.hom.unop H lemma quiver.hom.unop_inj {X Y : Cᵒᵖ} : function.injective (quiver.hom.unop : (X ⟶ Y) → (unop Y ⟶ unop X)) := λ _ _ H, congr_arg quiver.hom.op H @[simp] lemma quiver.hom.unop_op {X Y : C} {f : X ⟶ Y} : f.op.unop = f := rfl @[simp] lemma quiver.hom.op_unop {X Y : Cᵒᵖ} {f : X ⟶ Y} : f.unop.op = f := rfl end quiver namespace category_theory variables [category.{v₁} C] /-- The opposite category. See https://stacks.math.columbia.edu/tag/001M. -/ instance category.opposite : category.{v₁} Cᵒᵖ := { comp := λ _ _ _ f g, (g.unop ≫ f.unop).op, id := λ X, (𝟙 (unop X)).op } @[simp] lemma op_comp {X Y Z : C} {f : X ⟶ Y} {g : Y ⟶ Z} : (f ≫ g).op = g.op ≫ f.op := rfl @[simp] lemma op_id {X : C} : (𝟙 X).op = 𝟙 (op X) := rfl @[simp] lemma unop_comp {X Y Z : Cᵒᵖ} {f : X ⟶ Y} {g : Y ⟶ Z} : (f ≫ g).unop = g.unop ≫ f.unop := rfl @[simp] lemma unop_id {X : Cᵒᵖ} : (𝟙 X).unop = 𝟙 (unop X) := rfl @[simp] lemma unop_id_op {X : C} : (𝟙 (op X)).unop = 𝟙 X := rfl @[simp] lemma op_id_unop {X : Cᵒᵖ} : (𝟙 (unop X)).op = 𝟙 X := rfl section variables (C) /-- The functor from the double-opposite of a category to the underlying category. -/ @[simps] def op_op : (Cᵒᵖ)ᵒᵖ ⥤ C := { obj := λ X, unop (unop X), map := λ X Y f, f.unop.unop } /-- The functor from a category to its double-opposite. -/ @[simps] def unop_unop : C ⥤ Cᵒᵖᵒᵖ := { obj := λ X, op (op X), map := λ X Y f, f.op.op } /-- The double opposite category is equivalent to the original. -/ @[simps] def op_op_equivalence : Cᵒᵖᵒᵖ ≌ C := { functor := op_op C, inverse := unop_unop C, unit_iso := iso.refl (𝟭 Cᵒᵖᵒᵖ), counit_iso := iso.refl (unop_unop C ⋙ op_op C) } end /-- If `f.op` is an isomorphism `f` must be too. (This cannot be an instance as it would immediately loop!) -/ lemma is_iso_of_op {X Y : C} (f : X ⟶ Y) [is_iso f.op] : is_iso f := ⟨⟨(inv (f.op)).unop, ⟨quiver.hom.op_inj (by simp), quiver.hom.op_inj (by simp)⟩⟩⟩ namespace functor section variables {D : Type u₂} [category.{v₂} D] variables {C D} /-- The opposite of a functor, i.e. considering a functor `F : C ⥤ D` as a functor `Cᵒᵖ ⥤ Dᵒᵖ`. In informal mathematics no distinction is made between these. -/ @[simps] protected def op (F : C ⥤ D) : Cᵒᵖ ⥤ Dᵒᵖ := { obj := λ X, op (F.obj (unop X)), map := λ X Y f, (F.map f.unop).op } /-- Given a functor `F : Cᵒᵖ ⥤ Dᵒᵖ` we can take the "unopposite" functor `F : C ⥤ D`. In informal mathematics no distinction is made between these. -/ @[simps] protected def unop (F : Cᵒᵖ ⥤ Dᵒᵖ) : C ⥤ D := { obj := λ X, unop (F.obj (op X)), map := λ X Y f, (F.map f.op).unop } /-- The isomorphism between `F.op.unop` and `F`. -/ def op_unop_iso (F : C ⥤ D) : F.op.unop ≅ F := nat_iso.of_components (λ X, iso.refl _) (by tidy) /-- The isomorphism between `F.unop.op` and `F`. -/ def unop_op_iso (F : Cᵒᵖ ⥤ Dᵒᵖ) : F.unop.op ≅ F := nat_iso.of_components (λ X, iso.refl _) (by tidy) variables (C D) /-- Taking the opposite of a functor is functorial. -/ @[simps] def op_hom : (C ⥤ D)ᵒᵖ ⥤ (Cᵒᵖ ⥤ Dᵒᵖ) := { obj := λ F, (unop F).op, map := λ F G α, { app := λ X, (α.unop.app (unop X)).op, naturality' := λ X Y f, quiver.hom.unop_inj (α.unop.naturality f.unop).symm } } /-- Take the "unopposite" of a functor is functorial. -/ @[simps] def op_inv : (Cᵒᵖ ⥤ Dᵒᵖ) ⥤ (C ⥤ D)ᵒᵖ := { obj := λ F, op F.unop, map := λ F G α, quiver.hom.op { app := λ X, (α.app (op X)).unop, naturality' := λ X Y f, quiver.hom.op_inj $ (α.naturality f.op).symm } } -- TODO show these form an equivalence variables {C D} /-- Another variant of the opposite of functor, turning a functor `C ⥤ Dᵒᵖ` into a functor `Cᵒᵖ ⥤ D`. In informal mathematics no distinction is made. -/ @[simps] protected def left_op (F : C ⥤ Dᵒᵖ) : Cᵒᵖ ⥤ D := { obj := λ X, unop (F.obj (unop X)), map := λ X Y f, (F.map f.unop).unop } /-- Another variant of the opposite of functor, turning a functor `Cᵒᵖ ⥤ D` into a functor `C ⥤ Dᵒᵖ`. In informal mathematics no distinction is made. -/ @[simps] protected def right_op (F : Cᵒᵖ ⥤ D) : C ⥤ Dᵒᵖ := { obj := λ X, op (F.obj (op X)), map := λ X Y f, (F.map f.op).op } -- TODO show these form an equivalence instance {F : C ⥤ D} [full F] : full F.op := { preimage := λ X Y f, (F.preimage f.unop).op } instance {F : C ⥤ D} [faithful F] : faithful F.op := { map_injective' := λ X Y f g h, quiver.hom.unop_inj $ by simpa using map_injective F (quiver.hom.op_inj h) } /-- If F is faithful then the right_op of F is also faithful. -/ instance right_op_faithful {F : Cᵒᵖ ⥤ D} [faithful F] : faithful F.right_op := { map_injective' := λ X Y f g h, quiver.hom.op_inj (map_injective F (quiver.hom.op_inj h)) } /-- If F is faithful then the left_op of F is also faithful. -/ instance left_op_faithful {F : C ⥤ Dᵒᵖ} [faithful F] : faithful F.left_op := { map_injective' := λ X Y f g h, quiver.hom.unop_inj (map_injective F (quiver.hom.unop_inj h)) } end end functor namespace nat_trans variables {D : Type u₂} [category.{v₂} D] section variables {F G : C ⥤ D} local attribute [semireducible] quiver.opposite /-- The opposite of a natural transformation. -/ @[simps] protected def op (α : F ⟶ G) : G.op ⟶ F.op := { app := λ X, (α.app (unop X)).op, naturality' := begin tidy, erw α.naturality, refl, end } @[simp] lemma op_id (F : C ⥤ D) : nat_trans.op (𝟙 F) = 𝟙 (F.op) := rfl /-- The "unopposite" of a natural transformation. -/ @[simps] protected def unop {F G : Cᵒᵖ ⥤ Dᵒᵖ} (α : F ⟶ G) : G.unop ⟶ F.unop := { app := λ X, (α.app (op X)).unop, naturality' := begin tidy, erw α.naturality, refl, end } @[simp] lemma unop_id (F : Cᵒᵖ ⥤ Dᵒᵖ) : nat_trans.unop (𝟙 F) = 𝟙 (F.unop) := rfl /-- Given a natural transformation `α : F.op ⟶ G.op`, we can take the "unopposite" of each component obtaining a natural transformation `G ⟶ F`. -/ @[simps] protected def remove_op (α : F.op ⟶ G.op) : G ⟶ F := { app := λ X, (α.app (op X)).unop, naturality' := begin intros X Y f, have := congr_arg quiver.hom.op (α.naturality f.op), dsimp at this, erw this, refl, end } @[simp] lemma remove_op_id (F : C ⥤ D) : nat_trans.remove_op (𝟙 F.op) = 𝟙 F := rfl end section variables {F G : C ⥤ Dᵒᵖ} local attribute [semireducible] quiver.opposite /-- Given a natural transformation `α : F ⟶ G`, for `F G : C ⥤ Dᵒᵖ`, taking `unop` of each component gives a natural transformation `G.left_op ⟶ F.left_op`. -/ @[simps] protected def left_op (α : F ⟶ G) : G.left_op ⟶ F.left_op := { app := λ X, (α.app (unop X)).unop, naturality' := begin tidy, erw α.naturality, refl, end } /-- Given a natural transformation `α : F.left_op ⟶ G.left_op`, for `F G : C ⥤ Dᵒᵖ`, taking `op` of each component gives a natural transformation `G ⟶ F`. -/ @[simps] protected def remove_left_op (α : F.left_op ⟶ G.left_op) : G ⟶ F := { app := λ X, (α.app (op X)).op, naturality' := begin intros X Y f, have := congr_arg quiver.hom.op (α.naturality f.op), dsimp at this, erw this end } end end nat_trans namespace iso variables {X Y : C} /-- The opposite isomorphism. -/ @[simps] protected def op (α : X ≅ Y) : op Y ≅ op X := { hom := α.hom.op, inv := α.inv.op, hom_inv_id' := quiver.hom.unop_inj α.inv_hom_id, inv_hom_id' := quiver.hom.unop_inj α.hom_inv_id } end iso namespace nat_iso variables {D : Type u₂} [category.{v₂} D] variables {F G : C ⥤ D} /-- The natural isomorphism between opposite functors `G.op ≅ F.op` induced by a natural isomorphism between the original functors `F ≅ G`. -/ @[simps] protected def op (α : F ≅ G) : G.op ≅ F.op := { hom := nat_trans.op α.hom, inv := nat_trans.op α.inv, hom_inv_id' := begin ext, dsimp, rw ←op_comp, rw α.inv_hom_id_app, refl, end, inv_hom_id' := begin ext, dsimp, rw ←op_comp, rw α.hom_inv_id_app, refl, end } /-- The natural isomorphism between functors `G ≅ F` induced by a natural isomorphism between the opposite functors `F.op ≅ G.op`. -/ @[simps] protected def remove_op (α : F.op ≅ G.op) : G ≅ F := { hom := nat_trans.remove_op α.hom, inv := nat_trans.remove_op α.inv, hom_inv_id' := begin ext, dsimp, rw ←unop_comp, rw α.inv_hom_id_app, refl, end, inv_hom_id' := begin ext, dsimp, rw ←unop_comp, rw α.hom_inv_id_app, refl, end } /-- The natural isomorphism between functors `G.unop ≅ F.unop` induced by a natural isomorphism between the original functors `F ≅ G`. -/ @[simps] protected def unop {F G : Cᵒᵖ ⥤ Dᵒᵖ} (α : F ≅ G) : G.unop ≅ F.unop := { hom := nat_trans.unop α.hom, inv := nat_trans.unop α.inv, hom_inv_id' := begin ext, dsimp, rw ←unop_comp, rw α.inv_hom_id_app, refl, end, inv_hom_id' := begin ext, dsimp, rw ←unop_comp, rw α.hom_inv_id_app, refl, end } end nat_iso namespace equivalence variables {D : Type u₂} [category.{v₂} D] /-- An equivalence between categories gives an equivalence between the opposite categories. -/ @[simps] def op (e : C ≌ D) : Cᵒᵖ ≌ Dᵒᵖ := { functor := e.functor.op, inverse := e.inverse.op, unit_iso := (nat_iso.op e.unit_iso).symm, counit_iso := (nat_iso.op e.counit_iso).symm, functor_unit_iso_comp' := λ X, by { apply quiver.hom.unop_inj, dsimp, simp, }, } /-- An equivalence between opposite categories gives an equivalence between the original categories. -/ @[simps] def unop (e : Cᵒᵖ ≌ Dᵒᵖ) : C ≌ D := { functor := e.functor.unop, inverse := e.inverse.unop, unit_iso := (nat_iso.unop e.unit_iso).symm, counit_iso := (nat_iso.unop e.counit_iso).symm, functor_unit_iso_comp' := λ X, by { apply quiver.hom.op_inj, dsimp, simp, }, } end equivalence /-- The equivalence between arrows of the form `A ⟶ B` and `B.unop ⟶ A.unop`. Useful for building adjunctions. Note that this (definitionally) gives variants ``` def op_equiv' (A : C) (B : Cᵒᵖ) : (opposite.op A ⟶ B) ≃ (B.unop ⟶ A) := op_equiv _ _ def op_equiv'' (A : Cᵒᵖ) (B : C) : (A ⟶ opposite.op B) ≃ (B ⟶ A.unop) := op_equiv _ _ def op_equiv''' (A B : C) : (opposite.op A ⟶ opposite.op B) ≃ (B ⟶ A) := op_equiv _ _ ``` -/ @[simps] def op_equiv (A B : Cᵒᵖ) : (A ⟶ B) ≃ (B.unop ⟶ A.unop) := { to_fun := λ f, f.unop, inv_fun := λ g, g.op, left_inv := λ _, rfl, right_inv := λ _, rfl } instance subsingleton_of_unop (A B : Cᵒᵖ) [subsingleton (unop B ⟶ unop A)] : subsingleton (A ⟶ B) := (op_equiv A B).subsingleton instance decidable_eq_of_unop (A B : Cᵒᵖ) [decidable_eq (unop B ⟶ unop A)] : decidable_eq (A ⟶ B) := (op_equiv A B).decidable_eq universes v variables {α : Type v} [preorder α] /-- Construct a morphism in the opposite of a preorder category from an inequality. -/ def op_hom_of_le {U V : αᵒᵖ} (h : unop V ≤ unop U) : U ⟶ V := quiver.hom.op (hom_of_le h) lemma le_of_op_hom {U V : αᵒᵖ} (h : U ⟶ V) : unop V ≤ unop U := le_of_hom (h.unop) end category_theory
c06e1b4c5138326ec6cf167c1bbc64cc843bcd28
4fa161becb8ce7378a709f5992a594764699e268
/src/data/finsupp.lean
acf528c6aaf7086d8184451cb893c808827cfd7f
[ "Apache-2.0" ]
permissive
laughinggas/mathlib
e4aa4565ae34e46e834434284cb26bd9d67bc373
86dcd5cda7a5017c8b3c8876c89a510a19d49aad
refs/heads/master
1,669,496,232,688
1,592,831,995,000
1,592,831,995,000
274,155,979
0
0
Apache-2.0
1,592,835,190,000
1,592,835,189,000
null
UTF-8
Lean
false
false
70,986
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, Scott Morrison -/ import algebra.module import data.fintype.card /-! # Type of functions with finite support For any type `α` and a type `β` with zero, we define the type `finsupp α β` of finitely supported functions from `α` to `β`, i.e. the functions which are zero everywhere on `α` except on a finite set. We write this in infix notation as `α →₀ β`. Functions with finite support provide the basis for the following concrete instances: * `ℕ →₀ α`: Polynomials (where `α` is a ring) * `(σ →₀ ℕ) →₀ α`: Multivariate Polynomials (again `α` is a ring, and `σ` are variable names) * `α →₀ ℕ`: Multisets * `α →₀ ℤ`: Abelian groups freely generated by `α` * `β →₀ α`: Linear combinations over `β` where `α` is the scalar ring Most of the theory assumes that the range is a commutative monoid. This gives us the big sum operator as a powerful way to construct `finsupp` elements. A general piece of advice is to not use `α →₀ β` directly, as the type class setup might not be a good fit. Defining a copy and selecting the instances that are best suited for the application works better. ## Implementation notes This file is a `noncomputable theory` and uses classical logic throughout. ## Notation This file defines `α →₀ β` as notation for `finsupp α β`. -/ noncomputable theory open_locale classical big_operators open finset variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} {ι : Type*} {α₁ : Type*} {α₂ : Type*} {β₁ : Type*} {β₂ : Type*} /-- `finsupp α β`, denoted `α →₀ β`, is the type of functions `f : α → β` such that `f x = 0` for all but finitely many `x`. -/ structure finsupp (α : Type*) (β : Type*) [has_zero β] := (support : finset α) (to_fun : α → β) (mem_support_to_fun : ∀a, a ∈ support ↔ to_fun a ≠ 0) infixr ` →₀ `:25 := finsupp namespace finsupp /-! ### Basic declarations about `finsupp` -/ section basic variable [has_zero β] instance : has_coe_to_fun (α →₀ β) := ⟨λ_, α → β, to_fun⟩ instance : has_zero (α →₀ β) := ⟨⟨∅, (λ_, 0), λ _, ⟨false.elim, λ H, H rfl⟩⟩⟩ @[simp] lemma zero_apply {a : α} : (0 : α →₀ β) a = 0 := rfl @[simp] lemma support_zero : (0 : α →₀ β).support = ∅ := rfl instance : inhabited (α →₀ β) := ⟨0⟩ @[simp] lemma mem_support_iff {f : α →₀ β} : ∀{a:α}, a ∈ f.support ↔ f a ≠ 0 := f.mem_support_to_fun lemma not_mem_support_iff {f : α →₀ β} {a} : a ∉ f.support ↔ f a = 0 := not_iff_comm.1 mem_support_iff.symm @[ext] lemma ext : ∀{f g : α →₀ β}, (∀a, f a = g a) → f = g | ⟨s, f, hf⟩ ⟨t, g, hg⟩ h := begin have : f = g, { funext a, exact h a }, subst this, have : s = t, { ext a, exact (hf a).trans (hg a).symm }, subst this end lemma ext_iff {f g : α →₀ β} : f = g ↔ (∀a:α, f a = g a) := ⟨by rintros rfl a; refl, ext⟩ @[simp] lemma support_eq_empty {f : α →₀ β} : f.support = ∅ ↔ f = 0 := ⟨assume h, ext $ assume a, by_contradiction $ λ H, (finset.ext_iff.1 h a).1 $ mem_support_iff.2 H, by rintro rfl; refl⟩ instance finsupp.decidable_eq [decidable_eq α] [decidable_eq β] : decidable_eq (α →₀ β) := assume f g, decidable_of_iff (f.support = g.support ∧ (∀a∈f.support, f a = g a)) ⟨assume ⟨h₁, h₂⟩, ext $ assume a, if h : a ∈ f.support then h₂ a h else have hf : f a = 0, by rwa [mem_support_iff, not_not] at h, have hg : g a = 0, by rwa [h₁, mem_support_iff, not_not] at h, by rw [hf, hg], by rintro rfl; exact ⟨rfl, λ _ _, rfl⟩⟩ lemma finite_supp (f : α →₀ β) : set.finite {a | f a ≠ 0} := ⟨fintype.of_finset f.support (λ _, mem_support_iff)⟩ lemma support_subset_iff {s : set α} {f : α →₀ β} : ↑f.support ⊆ s ↔ (∀a∉s, f a = 0) := by simp only [set.subset_def, mem_coe, mem_support_iff]; exact forall_congr (assume a, @not_imp_comm _ _ (classical.dec _) (classical.dec _)) /-- Given `fintype α`, `equiv_fun_on_fintype` is the `equiv` between `α →₀ β` and `α → β`. (All functions on a finite type are finitely supported.) -/ def equiv_fun_on_fintype [fintype α] : (α →₀ β) ≃ (α → β) := ⟨λf a, f a, λf, mk (finset.univ.filter $ λa, f a ≠ 0) f (by simp only [true_and, finset.mem_univ, iff_self, finset.mem_filter, finset.filter_congr_decidable, forall_true_iff]), begin intro f, ext a, refl end, begin intro f, ext a, refl end⟩ end basic /-! ### Declarations about `single` -/ section single variables [has_zero β] {a a' : α} {b : β} /-- `single a b` is the finitely supported function which has value `b` at `a` and zero otherwise. -/ def single (a : α) (b : β) : α →₀ β := ⟨if b = 0 then ∅ else {a}, λ a', if a = a' then b else 0, λ a', begin by_cases hb : b = 0; by_cases a = a'; simp only [hb, h, if_pos, if_false, mem_singleton], { exact ⟨false.elim, λ H, H rfl⟩ }, { exact ⟨false.elim, λ H, H rfl⟩ }, { exact ⟨λ _, hb, λ _, rfl⟩ }, { exact ⟨λ H _, h H.symm, λ H, (H rfl).elim⟩ } end⟩ lemma single_apply : (single a b : α →₀ β) a' = if a = a' then b else 0 := rfl @[simp] lemma single_eq_same : (single a b : α →₀ β) a = b := if_pos rfl @[simp] lemma single_eq_of_ne (h : a ≠ a') : (single a b : α →₀ β) a' = 0 := if_neg h @[simp] lemma single_zero : (single a 0 : α →₀ β) = 0 := ext $ assume a', begin by_cases h : a = a', { rw [h, single_eq_same, zero_apply] }, { rw [single_eq_of_ne h, zero_apply] } end lemma support_single_ne_zero (hb : b ≠ 0) : (single a b).support = {a} := if_neg hb lemma support_single_subset : (single a b).support ⊆ {a} := show ite _ _ _ ⊆ _, by split_ifs; [exact empty_subset _, exact subset.refl _] lemma single_injective (a : α) : function.injective (single a : β → α →₀ β) := assume b₁ b₂ eq, have (single a b₁ : α →₀ β) a = (single a b₂ : α →₀ β) a, by rw eq, by rwa [single_eq_same, single_eq_same] at this lemma single_eq_single_iff (a₁ a₂ : α) (b₁ b₂ : β) : single a₁ b₁ = single a₂ b₂ ↔ ((a₁ = a₂ ∧ b₁ = b₂) ∨ (b₁ = 0 ∧ b₂ = 0)) := begin split, { assume eq, by_cases a₁ = a₂, { refine or.inl ⟨h, _⟩, rwa [h, (single_injective a₂).eq_iff] at eq }, { rw [ext_iff] at eq, have h₁ := eq a₁, have h₂ := eq a₂, simp only [single_eq_same, single_eq_of_ne h, single_eq_of_ne (ne.symm h)] at h₁ h₂, exact or.inr ⟨h₁, h₂.symm⟩ } }, { rintros (⟨rfl, rfl⟩ | ⟨rfl, rfl⟩), { refl }, { rw [single_zero, single_zero] } } end lemma single_left_inj (h : b ≠ 0) : single a b = single a' b ↔ a = a' := ⟨λ H, by simpa only [h, single_eq_single_iff, and_false, or_false, eq_self_iff_true, and_true] using H, λ H, by rw [H]⟩ lemma single_eq_zero : single a b = 0 ↔ b = 0 := ⟨λ h, by { rw ext_iff at h, simpa only [single_eq_same, zero_apply] using h a }, λ h, by rw [h, single_zero]⟩ lemma single_swap {α β : Type*} [has_zero β] (a₁ a₂ : α) (b : β) : (single a₁ b : α → β) a₂ = (single a₂ b : α → β) a₁ := by simp only [single_apply]; ac_refl lemma unique_single [unique α] (x : α →₀ β) : x = single (default α) (x (default α)) := by ext i; simp only [unique.eq_default i, single_eq_same] @[simp] lemma unique_single_eq_iff [unique α] {b' : β} : single a b = single a' b' ↔ b = b' := begin rw [single_eq_single_iff], split, { rintros (⟨_, rfl⟩ | ⟨rfl, rfl⟩); refl }, { intro h, left, exact ⟨subsingleton.elim _ _, h⟩ } end end single /-! ### Declarations about `on_finset` -/ section on_finset variables [has_zero β] /-- `on_finset s f hf` is the finsupp function representing `f` restricted to the finset `s`. The function needs to be `0` outside of `s`. Use this when the set needs to be filtered anyways, otherwise a better set representation is often available. -/ def on_finset (s : finset α) (f : α → β) (hf : ∀a, f a ≠ 0 → a ∈ s) : α →₀ β := ⟨s.filter (λa, f a ≠ 0), f, assume a, classical.by_cases (assume h : f a = 0, by rw mem_filter; exact ⟨and.right, λ H, (H h).elim⟩) (assume h : f a ≠ 0, by rw mem_filter; simp only [iff_true_intro h, hf a h, true_and])⟩ @[simp] lemma on_finset_apply {s : finset α} {f : α → β} {hf a} : (on_finset s f hf : α →₀ β) a = f a := rfl @[simp] lemma support_on_finset_subset {s : finset α} {f : α → β} {hf} : (on_finset s f hf).support ⊆ s := filter_subset _ end on_finset /-! ### Declarations about `map_range` -/ section map_range variables [has_zero β₁] [has_zero β₂] /-- The composition of `f : β₁ → β₂` and `g : α →₀ β₁` is `map_range f hf g : α →₀ β₂`, well-defined when `f 0 = 0`. -/ def map_range (f : β₁ → β₂) (hf : f 0 = 0) (g : α →₀ β₁) : α →₀ β₂ := on_finset g.support (f ∘ g) $ assume a, by rw [mem_support_iff, not_imp_not]; exact λ H, (congr_arg f H).trans hf @[simp] lemma map_range_apply {f : β₁ → β₂} {hf : f 0 = 0} {g : α →₀ β₁} {a : α} : map_range f hf g a = f (g a) := rfl @[simp] lemma map_range_zero {f : β₁ → β₂} {hf : f 0 = 0} : map_range f hf (0 : α →₀ β₁) = 0 := ext $ λ a, by simp only [hf, zero_apply, map_range_apply] lemma support_map_range {f : β₁ → β₂} {hf : f 0 = 0} {g : α →₀ β₁} : (map_range f hf g).support ⊆ g.support := support_on_finset_subset @[simp] lemma map_range_single {f : β₁ → β₂} {hf : f 0 = 0} {a : α} {b : β₁} : map_range f hf (single a b) = single a (f b) := ext $ λ a', show f (ite _ _ _) = ite _ _ _, by split_ifs; [refl, exact hf] end map_range /-! ### Declarations about `emb_domain` -/ section emb_domain variables [has_zero β] /-- Given `f : α₁ ↪ α₂` and `v : α₁ →₀ β`, `emb_domain f v : α₂ →₀ β` is the finitely supported function whose value at `f a : α₂` is `v a`. For a `b : α₂` outside the range of `f`, it is zero. -/ def emb_domain (f : α₁ ↪ α₂) (v : α₁ →₀ β) : α₂ →₀ β := begin refine ⟨v.support.map f, λa₂, if h : a₂ ∈ v.support.map f then v (v.support.choose (λa₁, f a₁ = a₂) _) else 0, _⟩, { rcases finset.mem_map.1 h with ⟨a, ha, rfl⟩, exact exists_unique.intro a ⟨ha, rfl⟩ (assume b ⟨_, hb⟩, f.injective hb) }, { assume a₂, split_ifs, { simp only [h, true_iff, ne.def], rw [← not_mem_support_iff, not_not], apply finset.choose_mem }, { simp only [h, ne.def, ne_self_iff_false] } } end lemma support_emb_domain (f : α₁ ↪ α₂) (v : α₁ →₀ β) : (emb_domain f v).support = v.support.map f := rfl lemma emb_domain_zero (f : α₁ ↪ α₂) : (emb_domain f 0 : α₂ →₀ β) = 0 := rfl lemma emb_domain_apply (f : α₁ ↪ α₂) (v : α₁ →₀ β) (a : α₁) : emb_domain f v (f a) = v a := begin change dite _ _ _ = _, split_ifs; rw [finset.mem_map' f] at h, { refine congr_arg (v : α₁ → β) (f.inj' _), exact finset.choose_property (λa₁, f a₁ = f a) _ _ }, { exact (not_mem_support_iff.1 h).symm } end lemma emb_domain_notin_range (f : α₁ ↪ α₂) (v : α₁ →₀ β) (a : α₂) (h : a ∉ set.range f) : emb_domain f v a = 0 := begin refine dif_neg (mt (assume h, _) h), rcases finset.mem_map.1 h with ⟨a, h, rfl⟩, exact set.mem_range_self a end lemma emb_domain_inj {f : α₁ ↪ α₂} {l₁ l₂ : α₁ →₀ β} : emb_domain f l₁ = emb_domain f l₂ ↔ l₁ = l₂ := ⟨λ h, ext $ λ a, by simpa only [emb_domain_apply] using ext_iff.1 h (f a), λ h, by rw h⟩ lemma emb_domain_map_range {β₁ β₂ : Type*} [has_zero β₁] [has_zero β₂] (f : α₁ ↪ α₂) (g : β₁ → β₂) (p : α₁ →₀ β₁) (hg : g 0 = 0) : emb_domain f (map_range g hg p) = map_range g hg (emb_domain f p) := begin ext a, by_cases a ∈ set.range f, { rcases h with ⟨a', rfl⟩, rw [map_range_apply, emb_domain_apply, emb_domain_apply, map_range_apply] }, { rw [map_range_apply, emb_domain_notin_range, emb_domain_notin_range, ← hg]; assumption } end lemma single_of_emb_domain_single (l : α₁ →₀ β) (f : α₁ ↪ α₂) (a : α₂) (b : β) (hb : b ≠ 0) (h : l.emb_domain f = single a b) : ∃ x, l = single x b ∧ f x = a := begin have h_map_support : finset.map f (l.support) = {a}, by rw [←support_emb_domain, h, support_single_ne_zero hb]; refl, have ha : a ∈ finset.map f (l.support), by simp only [h_map_support, finset.mem_singleton], rcases finset.mem_map.1 ha with ⟨c, hc₁, hc₂⟩, use c, split, { ext d, rw [← emb_domain_apply f l, h], by_cases h_cases : c = d, { simp only [eq.symm h_cases, hc₂, single_eq_same] }, { rw [single_apply, single_apply, if_neg, if_neg h_cases], by_contra hfd, exact h_cases (f.injective (hc₂.trans hfd)) } }, { exact hc₂ } end end emb_domain /-! ### Declarations about `zip_with` -/ section zip_with variables [has_zero β] [has_zero β₁] [has_zero β₂] /-- `zip_with f hf g₁ g₂` is the finitely supported function satisfying `zip_with f hf g₁ g₂ a = f (g₁ a) (g₂ a)`, and it is well-defined when `f 0 0 = 0`. -/ def zip_with (f : β₁ → β₂ → β) (hf : f 0 0 = 0) (g₁ : α →₀ β₁) (g₂ : α →₀ β₂) : (α →₀ β) := on_finset (g₁.support ∪ g₂.support) (λa, f (g₁ a) (g₂ a)) $ λ a H, begin simp only [mem_union, mem_support_iff, ne], rw [← not_and_distrib], rintro ⟨h₁, h₂⟩, rw [h₁, h₂] at H, exact H hf end @[simp] lemma zip_with_apply {f : β₁ → β₂ → β} {hf : f 0 0 = 0} {g₁ : α →₀ β₁} {g₂ : α →₀ β₂} {a : α} : zip_with f hf g₁ g₂ a = f (g₁ a) (g₂ a) := rfl lemma support_zip_with {f : β₁ → β₂ → β} {hf : f 0 0 = 0} {g₁ : α →₀ β₁} {g₂ : α →₀ β₂} : (zip_with f hf g₁ g₂).support ⊆ g₁.support ∪ g₂.support := support_on_finset_subset end zip_with /-! ### Declarations about `erase` -/ section erase /-- `erase a f` is the finitely supported function equal to `f` except at `a` where it is equal to `0`. -/ def erase [has_zero β] (a : α) (f : α →₀ β) : α →₀ β := ⟨f.support.erase a, (λa', if a' = a then 0 else f a'), assume a', by rw [mem_erase, mem_support_iff]; split_ifs; [exact ⟨λ H _, H.1 h, λ H, (H rfl).elim⟩, exact and_iff_right h]⟩ @[simp] lemma support_erase [has_zero β] {a : α} {f : α →₀ β} : (f.erase a).support = f.support.erase a := rfl @[simp] lemma erase_same [has_zero β] {a : α} {f : α →₀ β} : (f.erase a) a = 0 := if_pos rfl @[simp] lemma erase_ne [has_zero β] {a a' : α} {f : α →₀ β} (h : a' ≠ a) : (f.erase a) a' = f a' := if_neg h @[simp] lemma erase_single [has_zero β] {a : α} {b : β} : (erase a (single a b)) = 0 := begin ext s, by_cases hs : s = a, { rw [hs, erase_same], refl }, { rw [erase_ne hs], exact single_eq_of_ne (ne.symm hs) } end lemma erase_single_ne [has_zero β] {a a' : α} {b : β} (h : a ≠ a') : (erase a (single a' b)) = single a' b := begin ext s, by_cases hs : s = a, { rw [hs, erase_same, single_eq_of_ne (h.symm)] }, { rw [erase_ne hs] } end end erase /-! ### Declarations about `sum` and `prod` In most of this section, the domain `β` is assumed to be an `add_monoid`. -/ -- [to_additive sum] for finsupp.prod doesn't work, the equation lemmas are not generated /-- `sum f g` is the sum of `g a (f a)` over the support of `f`. -/ def sum [has_zero β] [add_comm_monoid γ] (f : α →₀ β) (g : α → β → γ) : γ := ∑ a in f.support, g a (f a) /-- `prod f g` is the product of `g a (f a)` over the support of `f`. -/ @[to_additive] def prod [has_zero β] [comm_monoid γ] (f : α →₀ β) (g : α → β → γ) : γ := ∏ a in f.support, g a (f a) @[to_additive] lemma prod_fintype [fintype α] [has_zero β] [comm_monoid γ] (f : α →₀ β) (g : α → β → γ) (h : ∀ i, g i 0 = 1) : f.prod g = ∏ i, g i (f i) := begin classical, rw [finsupp.prod, ← fintype.prod_extend_by_one, finset.prod_congr rfl], intros i hi, split_ifs with hif hif, { refl }, { rw finsupp.not_mem_support_iff at hif, rw [hif, h] } end @[to_additive] lemma prod_map_range_index [has_zero β₁] [has_zero β₂] [comm_monoid γ] {f : β₁ → β₂} {hf : f 0 = 0} {g : α →₀ β₁} {h : α → β₂ → γ} (h0 : ∀a, h a 0 = 1) : (map_range f hf g).prod h = g.prod (λa b, h a (f b)) := finset.prod_subset support_map_range $ λ _ _ H, by rw [not_mem_support_iff.1 H, h0] @[to_additive] lemma prod_zero_index [add_comm_monoid β] [comm_monoid γ] {h : α → β → γ} : (0 : α →₀ β).prod h = 1 := rfl @[to_additive] lemma prod_comm {α' : Type*} [has_zero β] {β' : Type*} [has_zero β'] (f : α →₀ β) (g : α' →₀ β') [comm_monoid γ] (h : α → β → α' → β' → γ) : f.prod (λ x v, g.prod (λ x' v', h x v x' v')) = g.prod (λ x' v', f.prod (λ x v, h x v x' v')) := begin dsimp [finsupp.prod], rw finset.prod_comm, end @[simp, to_additive] lemma prod_ite_eq [has_zero β] [comm_monoid γ] (f : α →₀ β) (a : α) (b : α → β → γ) : f.prod (λ x v, ite (a = x) (b x v) 1) = ite (a ∈ f.support) (b a (f a)) 1 := by { dsimp [finsupp.prod], rw f.support.prod_ite_eq, } /-- A restatement of `prod_ite_eq` with the equality test reversed. -/ @[simp, to_additive "A restatement of `sum_ite_eq` with the equality test reversed."] lemma prod_ite_eq' [has_zero β] [comm_monoid γ] (f : α →₀ β) (a : α) (b : α → β → γ) : f.prod (λ x v, ite (x = a) (b x v) 1) = ite (a ∈ f.support) (b a (f a)) 1 := by { dsimp [finsupp.prod], rw f.support.prod_ite_eq', } @[simp] lemma prod_pow [fintype α] [comm_monoid γ] (f : α →₀ ℕ) (g : α → γ) : f.prod (λ a b, g a ^ b) = ∏ a, g a ^ (f a) := begin apply prod_subset (finset.subset_univ _), intros a _ ha, simp only [finsupp.not_mem_support_iff.mp ha, pow_zero] end section add_monoid variables [add_monoid β] @[to_additive] lemma prod_single_index [comm_monoid γ] {a : α} {b : β} {h : α → β → γ} (h_zero : h a 0 = 1) : (single a b).prod h = h a b := begin by_cases h : b = 0, { simp only [h, h_zero, single_zero]; refl }, { simp only [finsupp.prod, support_single_ne_zero h, prod_singleton, single_eq_same] } end instance : has_add (α →₀ β) := ⟨zip_with (+) (add_zero 0)⟩ @[simp] lemma add_apply {g₁ g₂ : α →₀ β} {a : α} : (g₁ + g₂) a = g₁ a + g₂ a := rfl lemma support_add {g₁ g₂ : α →₀ β} : (g₁ + g₂).support ⊆ g₁.support ∪ g₂.support := support_zip_with lemma support_add_eq {g₁ g₂ : α →₀ β} (h : disjoint g₁.support g₂.support) : (g₁ + g₂).support = g₁.support ∪ g₂.support := le_antisymm support_zip_with $ assume a ha, (finset.mem_union.1 ha).elim (assume ha, have a ∉ g₂.support, from disjoint_left.1 h ha, by simp only [mem_support_iff, not_not] at *; simpa only [add_apply, this, add_zero]) (assume ha, have a ∉ g₁.support, from disjoint_right.1 h ha, by simp only [mem_support_iff, not_not] at *; simpa only [add_apply, this, zero_add]) @[simp] lemma single_add {a : α} {b₁ b₂ : β} : single a (b₁ + b₂) = single a b₁ + single a b₂ := ext $ assume a', begin by_cases h : a = a', { rw [h, add_apply, single_eq_same, single_eq_same, single_eq_same] }, { rw [add_apply, single_eq_of_ne h, single_eq_of_ne h, single_eq_of_ne h, zero_add] } end instance : add_monoid (α →₀ β) := { add_monoid . zero := 0, add := (+), add_assoc := assume ⟨s, f, hf⟩ ⟨t, g, hg⟩ ⟨u, h, hh⟩, ext $ assume a, add_assoc _ _ _, zero_add := assume ⟨s, f, hf⟩, ext $ assume a, zero_add _, add_zero := assume ⟨s, f, hf⟩, ext $ assume a, add_zero _ } /-- Evaluation of a function `f : α →₀ β` at a point as an additive monoid homomorphism. -/ def eval_add_hom (a : α) : (α →₀ β) →+ β := ⟨λ g, g a, zero_apply, λ _ _, add_apply⟩ @[simp] lemma eval_add_hom_apply (a : α) (g : α →₀ β) : eval_add_hom a g = g a := rfl lemma single_add_erase {a : α} {f : α →₀ β} : single a (f a) + f.erase a = f := ext $ λ a', if h : a = a' then by subst h; simp only [add_apply, single_eq_same, erase_same, add_zero] else by simp only [add_apply, single_eq_of_ne h, zero_add, erase_ne (ne.symm h)] lemma erase_add_single {a : α} {f : α →₀ β} : f.erase a + single a (f a) = f := ext $ λ a', if h : a = a' then by subst h; simp only [add_apply, single_eq_same, erase_same, zero_add] else by simp only [add_apply, single_eq_of_ne h, add_zero, erase_ne (ne.symm h)] @[simp] lemma erase_add (a : α) (f f' : α →₀ β) : erase a (f + f') = erase a f + erase a f' := begin ext s, by_cases hs : s = a, { rw [hs, add_apply, erase_same, erase_same, erase_same, add_zero] }, rw [add_apply, erase_ne hs, erase_ne hs, erase_ne hs, add_apply], end @[elab_as_eliminator] protected theorem induction {p : (α →₀ β) → Prop} (f : α →₀ β) (h0 : p 0) (ha : ∀a b (f : α →₀ β), a ∉ f.support → b ≠ 0 → p f → p (single a b + f)) : p f := suffices ∀s (f : α →₀ β), f.support = s → p f, from this _ _ rfl, assume s, finset.induction_on s (λ f hf, by rwa [support_eq_empty.1 hf]) $ assume a s has ih f hf, suffices p (single a (f a) + f.erase a), by rwa [single_add_erase] at this, begin apply ha, { rw [support_erase, mem_erase], exact λ H, H.1 rfl }, { rw [← mem_support_iff, hf], exact mem_insert_self _ _ }, { apply ih _ _, rw [support_erase, hf, finset.erase_insert has] } end lemma induction₂ {p : (α →₀ β) → Prop} (f : α →₀ β) (h0 : p 0) (ha : ∀a b (f : α →₀ β), a ∉ f.support → b ≠ 0 → p f → p (f + single a b)) : p f := suffices ∀s (f : α →₀ β), f.support = s → p f, from this _ _ rfl, assume s, finset.induction_on s (λ f hf, by rwa [support_eq_empty.1 hf]) $ assume a s has ih f hf, suffices p (f.erase a + single a (f a)), by rwa [erase_add_single] at this, begin apply ha, { rw [support_erase, mem_erase], exact λ H, H.1 rfl }, { rw [← mem_support_iff, hf], exact mem_insert_self _ _ }, { apply ih _ _, rw [support_erase, hf, finset.erase_insert has] } end lemma map_range_add [add_monoid β₁] [add_monoid β₂] {f : β₁ → β₂} {hf : f 0 = 0} (hf' : ∀ x y, f (x + y) = f x + f y) (v₁ v₂ : α →₀ β₁) : map_range f hf (v₁ + v₂) = map_range f hf v₁ + map_range f hf v₂ := ext $ λ a, by simp only [hf', add_apply, map_range_apply] end add_monoid section nat_sub instance nat_sub : has_sub (α →₀ ℕ) := ⟨zip_with (λ m n, m - n) (nat.sub_zero 0)⟩ @[simp] lemma nat_sub_apply {g₁ g₂ : α →₀ ℕ} {a : α} : (g₁ - g₂) a = g₁ a - g₂ a := rfl @[simp] lemma single_sub {a : α} {n₁ n₂ : ℕ} : single a (n₁ - n₂) = single a n₁ - single a n₂ := begin ext f, by_cases h : (a = f), { rw [h, nat_sub_apply, single_eq_same, single_eq_same, single_eq_same] }, rw [nat_sub_apply, single_eq_of_ne h, single_eq_of_ne h, single_eq_of_ne h] end -- These next two lemmas are used in developing -- the partial derivative on `mv_polynomial`. lemma sub_single_one_add {a : α} {u u' : α →₀ ℕ} (h : u a ≠ 0) : u - single a 1 + u' = u + u' - single a 1 := begin ext b, rw [add_apply, nat_sub_apply, nat_sub_apply, add_apply], by_cases h : a = b, { rw [←h, single_eq_same], cases (u a), { contradiction }, { simp }, }, { simp [h], } end lemma add_sub_single_one {a : α} {u u' : α →₀ ℕ} (h : u' a ≠ 0) : u + (u' - single a 1) = u + u' - single a 1 := begin ext b, rw [add_apply, nat_sub_apply, nat_sub_apply, add_apply], by_cases h : a = b, { rw [←h, single_eq_same], cases (u' a), { contradiction }, { simp }, }, { simp [h], } end end nat_sub instance [add_comm_monoid β] : add_comm_monoid (α →₀ β) := { add_comm := assume ⟨s, f, _⟩ ⟨t, g, _⟩, ext $ assume a, add_comm _ _, .. finsupp.add_monoid } instance [add_group β] : add_group (α →₀ β) := { neg := map_range (has_neg.neg) neg_zero, add_left_neg := assume ⟨s, f, _⟩, ext $ assume x, add_left_neg _, .. finsupp.add_monoid } lemma single_multiset_sum [add_comm_monoid β] (s : multiset β) (a : α) : single a s.sum = (s.map (single a)).sum := multiset.induction_on s single_zero $ λ a s ih, by rw [multiset.sum_cons, single_add, ih, multiset.map_cons, multiset.sum_cons] lemma single_finset_sum [add_comm_monoid β] (s : finset γ) (f : γ → β) (a : α) : single a (∑ b in s, f b) = ∑ b in s, single a (f b) := begin transitivity, apply single_multiset_sum, rw [multiset.map_map], refl end lemma single_sum [has_zero γ] [add_comm_monoid β] (s : δ →₀ γ) (f : δ → γ → β) (a : α) : single a (s.sum f) = s.sum (λd c, single a (f d c)) := single_finset_sum _ _ _ @[to_additive] lemma prod_neg_index [add_group β] [comm_monoid γ] {g : α →₀ β} {h : α → β → γ} (h0 : ∀a, h a 0 = 1) : (-g).prod h = g.prod (λa b, h a (- b)) := prod_map_range_index h0 @[simp] lemma neg_apply [add_group β] {g : α →₀ β} {a : α} : (- g) a = - g a := rfl @[simp] lemma sub_apply [add_group β] {g₁ g₂ : α →₀ β} {a : α} : (g₁ - g₂) a = g₁ a - g₂ a := rfl @[simp] lemma support_neg [add_group β] {f : α →₀ β} : support (-f) = support f := finset.subset.antisymm support_map_range (calc support f = support (- (- f)) : congr_arg support (neg_neg _).symm ... ⊆ support (- f) : support_map_range) instance [add_comm_group β] : add_comm_group (α →₀ β) := { add_comm := add_comm, ..finsupp.add_group } @[simp] lemma sum_apply [has_zero β₁] [add_comm_monoid β] {f : α₁ →₀ β₁} {g : α₁ → β₁ → α →₀ β} {a₂ : α} : (f.sum g) a₂ = f.sum (λa₁ b, g a₁ b a₂) := (eval_add_hom a₂ : (α →₀ β) →+ _).map_sum _ _ lemma support_sum [has_zero β₁] [add_comm_monoid β] {f : α₁ →₀ β₁} {g : α₁ → β₁ → (α →₀ β)} : (f.sum g).support ⊆ f.support.bind (λa, (g a (f a)).support) := have ∀a₁ : α, f.sum (λ (a : α₁) (b : β₁), (g a b) a₁) ≠ 0 → (∃ (a : α₁), f a ≠ 0 ∧ ¬ (g a (f a)) a₁ = 0), from assume a₁ h, let ⟨a, ha, ne⟩ := finset.exists_ne_zero_of_sum_ne_zero h in ⟨a, mem_support_iff.mp ha, ne⟩, by simpa only [finset.subset_iff, mem_support_iff, finset.mem_bind, sum_apply, exists_prop] using this @[simp] lemma sum_zero [add_comm_monoid β] [add_comm_monoid γ] {f : α →₀ β} : f.sum (λa b, (0 : γ)) = 0 := finset.sum_const_zero @[simp] lemma sum_add [add_comm_monoid β] [add_comm_monoid γ] {f : α →₀ β} {h₁ h₂ : α → β → γ} : f.sum (λa b, h₁ a b + h₂ a b) = f.sum h₁ + f.sum h₂ := finset.sum_add_distrib @[simp] lemma sum_neg [add_comm_monoid β] [add_comm_group γ] {f : α →₀ β} {h : α → β → γ} : f.sum (λa b, - h a b) = - f.sum h := f.support.sum_hom (@has_neg.neg γ _) @[simp] lemma sum_sub [add_comm_monoid β] [add_comm_group γ] {f : α →₀ β} {h₁ h₂ : α → β → γ} : f.sum (λa b, h₁ a b - h₂ a b) = f.sum h₁ - f.sum h₂ := by rw [sub_eq_add_neg, ←sum_neg, ←sum_add]; refl @[simp] lemma sum_single [add_comm_monoid β] (f : α →₀ β) : f.sum single = f := have ∀a:α, f.sum (λa' b, ite (a' = a) b 0) = ∑ a' in {a}, ite (a' = a) (f a') 0, begin intro a, by_cases h : a ∈ f.support, { have : ({a} : finset α) ⊆ f.support, { simpa only [finset.subset_iff, mem_singleton, forall_eq] }, refine (finset.sum_subset this (λ _ _ H, _)).symm, exact if_neg (mt mem_singleton.2 H) }, { transitivity (∑ a in f.support, (0 : β)), { refine (finset.sum_congr rfl $ λ a' ha', if_neg _), rintro rfl, exact h ha' }, { rw [sum_const_zero, sum_singleton, if_pos rfl, not_mem_support_iff.1 h] } } end, ext $ assume a, by simp only [sum_apply, single_apply, this, sum_singleton, if_pos] @[to_additive] lemma prod_add_index [add_comm_monoid β] [comm_monoid γ] {f g : α →₀ β} {h : α → β → γ} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : (f + g).prod h = f.prod h * g.prod h := have f_eq : ∏ a in f.support ∪ g.support, h a (f a) = f.prod h, from (finset.prod_subset (finset.subset_union_left _ _) $ by intros _ _ H; rw [not_mem_support_iff.1 H, h_zero]).symm, have g_eq : ∏ a in f.support ∪ g.support, h a (g a) = g.prod h, from (finset.prod_subset (finset.subset_union_right _ _) $ by intros _ _ H; rw [not_mem_support_iff.1 H, h_zero]).symm, calc ∏ a in (f + g).support, h a ((f + g) a) = ∏ a in f.support ∪ g.support, h a ((f + g) a) : finset.prod_subset support_add $ by intros _ _ H; rw [not_mem_support_iff.1 H, h_zero] ... = (∏ a in f.support ∪ g.support, h a (f a)) * (∏ a in f.support ∪ g.support, h a (g a)) : by simp only [add_apply, h_add, finset.prod_mul_distrib] ... = _ : by rw [f_eq, g_eq] lemma sum_sub_index [add_comm_group β] [add_comm_group γ] {f g : α →₀ β} {h : α → β → γ} (h_sub : ∀a b₁ b₂, h a (b₁ - b₂) = h a b₁ - h a b₂) : (f - g).sum h = f.sum h - g.sum h := have h_zero : ∀a, h a 0 = 0, from assume a, have h a (0 - 0) = h a 0 - h a 0, from h_sub a 0 0, by simpa only [sub_self] using this, have h_neg : ∀a b, h a (- b) = - h a b, from assume a b, have h a (0 - b) = h a 0 - h a b, from h_sub a 0 b, by simpa only [h_zero, zero_sub] using this, have h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ + h a b₂, from assume a b₁ b₂, have h a (b₁ - (- b₂)) = h a b₁ - h a (- b₂), from h_sub a b₁ (-b₂), by simpa only [h_neg, sub_neg_eq_add] using this, calc (f - g).sum h = (f + - g).sum h : rfl ... = f.sum h + - g.sum h : by simp only [sum_add_index h_zero h_add, sum_neg_index h_zero, h_neg, sum_neg] ... = f.sum h - g.sum h : rfl @[to_additive] lemma prod_finset_sum_index [add_comm_monoid β] [comm_monoid γ] {s : finset ι} {g : ι → α →₀ β} {h : α → β → γ} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : ∏ i in s, (g i).prod h = (∑ i in s, g i).prod h := finset.induction_on s rfl $ λ a s has ih, by rw [prod_insert has, ih, sum_insert has, prod_add_index h_zero h_add] @[to_additive] lemma prod_sum_index [add_comm_monoid β₁] [add_comm_monoid β] [comm_monoid γ] {f : α₁ →₀ β₁} {g : α₁ → β₁ → α →₀ β} {h : α → β → γ} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : (f.sum g).prod h = f.prod (λa b, (g a b).prod h) := (prod_finset_sum_index h_zero h_add).symm lemma multiset_sum_sum_index [add_comm_monoid β] [add_comm_monoid γ] (f : multiset (α →₀ β)) (h : α → β → γ) (h₀ : ∀a, h a 0 = 0) (h₁ : ∀ (a : α) (b₁ b₂ : β), h a (b₁ + b₂) = h a b₁ + h a b₂) : (f.sum.sum h) = (f.map $ λg:α →₀ β, g.sum h).sum := multiset.induction_on f rfl $ assume a s ih, by rw [multiset.sum_cons, multiset.map_cons, multiset.sum_cons, sum_add_index h₀ h₁, ih] lemma multiset_map_sum [has_zero β] {f : α →₀ β} {m : γ → δ} {h : α → β → multiset γ} : multiset.map m (f.sum h) = f.sum (λa b, (h a b).map m) := (f.support.sum_hom _).symm lemma multiset_sum_sum [has_zero β] [add_comm_monoid γ] {f : α →₀ β} {h : α → β → multiset γ} : multiset.sum (f.sum h) = f.sum (λa b, multiset.sum (h a b)) := (f.support.sum_hom multiset.sum).symm section map_range variables [add_comm_monoid β₁] [add_comm_monoid β₂] (f : β₁ → β₂) [hf : is_add_monoid_hom f] instance is_add_monoid_hom_map_range : is_add_monoid_hom (map_range f hf.map_zero : (α →₀ β₁) → (α →₀ β₂)) := { map_zero := map_range_zero, map_add := λ a b, map_range_add hf.map_add _ _ } lemma map_range_multiset_sum (m : multiset (α →₀ β₁)) : map_range f hf.map_zero m.sum = (m.map $ λx, map_range f hf.map_zero x).sum := (m.sum_hom (map_range f hf.map_zero)).symm lemma map_range_finset_sum {ι : Type*} (s : finset ι) (g : ι → (α →₀ β₁)) : map_range f hf.map_zero (∑ x in s, g x) = ∑ x in s, map_range f hf.map_zero (g x) := by rw [finset.sum.equations._eqn_1, map_range_multiset_sum, multiset.map_map]; refl end map_range /-! ### Declarations about `map_domain` -/ section map_domain variables [add_comm_monoid β] {v v₁ v₂ : α →₀ β} /-- Given `f : α₁ → α₂` and `v : α₁ →₀ β`, `map_domain f v : α₂ →₀ β` is the finitely supported function whose value at `a : α₂` is the sum of `v x` over all `x` such that `f x = a`. -/ def map_domain (f : α₁ → α₂) (v : α₁ →₀ β) : α₂ →₀ β := v.sum $ λa, single (f a) lemma map_domain_apply {f : α₁ → α₂} (hf : function.injective f) (x : α₁ →₀ β) (a : α₁) : map_domain f x (f a) = x a := begin rw [map_domain, sum_apply, sum, finset.sum_eq_single a, single_eq_same], { assume b _ hba, exact single_eq_of_ne (hf.ne hba) }, { simp only [(∉), (≠), not_not, mem_support_iff], assume h, rw [h, single_zero], refl } end lemma map_domain_notin_range {f : α₁ → α₂} (x : α₁ →₀ β) (a : α₂) (h : a ∉ set.range f) : map_domain f x a = 0 := begin rw [map_domain, sum_apply, sum], exact finset.sum_eq_zero (assume a' h', single_eq_of_ne $ assume eq, h $ eq ▸ set.mem_range_self _) end lemma map_domain_id : map_domain id v = v := sum_single _ lemma map_domain_comp {f : α → α₁} {g : α₁ → α₂} : map_domain (g ∘ f) v = map_domain g (map_domain f v) := begin refine ((sum_sum_index _ _).trans _).symm, { intros, exact single_zero }, { intros, exact single_add }, refine sum_congr rfl (λ _ _, sum_single_index _), { exact single_zero } end lemma map_domain_single {f : α → α₁} {a : α} {b : β} : map_domain f (single a b) = single (f a) b := sum_single_index single_zero @[simp] lemma map_domain_zero {f : α → α₂} : map_domain f 0 = (0 : α₂ →₀ β) := sum_zero_index lemma map_domain_congr {f g : α → α₂} (h : ∀x∈v.support, f x = g x) : v.map_domain f = v.map_domain g := finset.sum_congr rfl $ λ _ H, by simp only [h _ H] lemma map_domain_add {f : α → α₂} : map_domain f (v₁ + v₂) = map_domain f v₁ + map_domain f v₂ := sum_add_index (λ _, single_zero) (λ _ _ _, single_add) lemma map_domain_finset_sum {f : α → α₂} {s : finset ι} {v : ι → α →₀ β} : map_domain f (∑ i in s, v i) = ∑ i in s, map_domain f (v i) := eq.symm $ sum_finset_sum_index (λ _, single_zero) (λ _ _ _, single_add) lemma map_domain_sum [has_zero β₁] {f : α → α₂} {s : α →₀ β₁} {v : α → β₁ → α →₀ β} : map_domain f (s.sum v) = s.sum (λa b, map_domain f (v a b)) := eq.symm $ sum_finset_sum_index (λ _, single_zero) (λ _ _ _, single_add) lemma map_domain_support {f : α → α₂} {s : α →₀ β} : (s.map_domain f).support ⊆ s.support.image f := finset.subset.trans support_sum $ finset.subset.trans (finset.bind_mono $ assume a ha, support_single_subset) $ by rw [finset.bind_singleton]; exact subset.refl _ @[to_additive] lemma prod_map_domain_index [comm_monoid γ] {f : α → α₂} {s : α →₀ β} {h : α₂ → β → γ} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : (s.map_domain f).prod h = s.prod (λa b, h (f a) b) := (prod_sum_index h_zero h_add).trans $ prod_congr rfl $ λ _ _, prod_single_index (h_zero _) lemma emb_domain_eq_map_domain (f : α₁ ↪ α₂) (v : α₁ →₀ β) : emb_domain f v = map_domain f v := begin ext a, by_cases a ∈ set.range f, { rcases h with ⟨a, rfl⟩, rw [map_domain_apply f.injective, emb_domain_apply] }, { rw [map_domain_notin_range, emb_domain_notin_range]; assumption } end lemma map_domain_injective {f : α₁ → α₂} (hf : function.injective f) : function.injective (map_domain f : (α₁ →₀ β) → (α₂ →₀ β)) := begin assume v₁ v₂ eq, ext a, have : map_domain f v₁ (f a) = map_domain f v₂ (f a), { rw eq }, rwa [map_domain_apply hf, map_domain_apply hf] at this, end end map_domain /-! ### Declarations about `comap_domain` -/ section comap_domain /-- Given `f : α₁ → α₂`, `l : α₂ →₀ γ` and a proof `hf` that `f` is injective on the preimage of `l.support`, `comap_domain f l hf` is the finitely supported function from `α₁` to `γ` given by composing `l` with `f`. -/ def comap_domain {α₁ α₂ γ : Type*} [has_zero γ] (f : α₁ → α₂) (l : α₂ →₀ γ) (hf : set.inj_on f (f ⁻¹' ↑l.support)) : α₁ →₀ γ := { support := l.support.preimage hf, to_fun := (λ a, l (f a)), mem_support_to_fun := begin intros a, simp only [finset.mem_def.symm, finset.mem_preimage], exact l.mem_support_to_fun (f a), end } @[simp] lemma comap_domain_apply {α₁ α₂ γ : Type*} [has_zero γ] (f : α₁ → α₂) (l : α₂ →₀ γ) (hf : set.inj_on f (f ⁻¹' ↑l.support)) (a : α₁) : comap_domain f l hf a = l (f a) := rfl lemma sum_comap_domain {α₁ α₂ β γ : Type*} [has_zero β] [add_comm_monoid γ] (f : α₁ → α₂) (l : α₂ →₀ β) (g : α₂ → β → γ) (hf : set.bij_on f (f ⁻¹' ↑l.support) ↑l.support) : (comap_domain f l hf.inj_on).sum (g ∘ f) = l.sum g := begin simp [sum], simp [comap_domain, finset.sum_preimage f _ _ (λ (x : α₂), g x (l x))] end lemma eq_zero_of_comap_domain_eq_zero {α₁ α₂ γ : Type*} [add_comm_monoid γ] (f : α₁ → α₂) (l : α₂ →₀ γ) (hf : set.bij_on f (f ⁻¹' ↑l.support) ↑l.support) : comap_domain f l hf.inj_on = 0 → l = 0 := begin rw [← support_eq_empty, ← support_eq_empty, comap_domain], simp only [finset.ext_iff, finset.not_mem_empty, iff_false, mem_preimage], assume h a ha, cases hf.2.2 ha with b hb, exact h b (hb.2.symm ▸ ha) end lemma map_domain_comap_domain {α₁ α₂ γ : Type*} [add_comm_monoid γ] (f : α₁ → α₂) (l : α₂ →₀ γ) (hf : function.injective f) (hl : ↑l.support ⊆ set.range f): map_domain f (comap_domain f l (hf.inj_on _)) = l := begin ext a, by_cases h_cases: a ∈ set.range f, { rcases set.mem_range.1 h_cases with ⟨b, hb⟩, rw [hb.symm, map_domain_apply hf, comap_domain_apply] }, { rw map_domain_notin_range _ _ h_cases, by_contra h_contr, apply h_cases (hl $ finset.mem_coe.2 $ mem_support_iff.2 $ λ h, h_contr h.symm) } end end comap_domain /-! ### Declarations about `filter` -/ section filter section has_zero variables [has_zero β] (p : α → Prop) (f : α →₀ β) /-- `filter p f` is the function which is `f a` if `p a` is true and 0 otherwise. -/ def filter (p : α → Prop) (f : α →₀ β) : α →₀ β := on_finset f.support (λa, if p a then f a else 0) $ λ a H, mem_support_iff.2 $ λ h, by rw [h, if_t_t] at H; exact H rfl @[simp] lemma filter_apply_pos {a : α} (h : p a) : f.filter p a = f a := if_pos h @[simp] lemma filter_apply_neg {a : α} (h : ¬ p a) : f.filter p a = 0 := if_neg h @[simp] lemma support_filter : (f.filter p).support = f.support.filter p := finset.ext $ assume a, if H : p a then by simp only [mem_support_iff, filter_apply_pos _ _ H, mem_filter, H, and_true] else by simp only [mem_support_iff, filter_apply_neg _ _ H, mem_filter, H, and_false, ne.def, ne_self_iff_false] lemma filter_zero : (0 : α →₀ β).filter p = 0 := by rw [← support_eq_empty, support_filter, support_zero, finset.filter_empty] @[simp] lemma filter_single_of_pos {a : α} {b : β} (h : p a) : (single a b).filter p = single a b := ext $ λ x, begin by_cases h' : p x, { simp only [h', filter_apply_pos] }, { simp only [h', filter_apply_neg, not_false_iff], rw single_eq_of_ne, rintro rfl, exact h' h } end @[simp] lemma filter_single_of_neg {a : α} {b : β} (h : ¬ p a) : (single a b).filter p = 0 := ext $ λ x, begin by_cases h' : p x, { simp only [h', filter_apply_pos, zero_apply], rw single_eq_of_ne, rintro rfl, exact h h' }, { simp only [h', finsupp.zero_apply, not_false_iff, filter_apply_neg] } end end has_zero lemma filter_pos_add_filter_neg [add_monoid β] (f : α →₀ β) (p : α → Prop) : f.filter p + f.filter (λa, ¬ p a) = f := ext $ assume a, if H : p a then by simp only [add_apply, filter_apply_pos, filter_apply_neg, H, not_not, add_zero] else by simp only [add_apply, filter_apply_pos, filter_apply_neg, H, not_false_iff, zero_add] end filter /-! ### Declarations about `frange` -/ section frange variables [has_zero β] /-- `frange f` is the image of `f` on the support of `f`. -/ def frange (f : α →₀ β) : finset β := finset.image f f.support theorem mem_frange {f : α →₀ β} {y : β} : y ∈ f.frange ↔ y ≠ 0 ∧ ∃ x, f x = y := finset.mem_image.trans ⟨λ ⟨x, hx1, hx2⟩, ⟨hx2 ▸ mem_support_iff.1 hx1, x, hx2⟩, λ ⟨hy, x, hx⟩, ⟨x, mem_support_iff.2 (hx.symm ▸ hy), hx⟩⟩ theorem zero_not_mem_frange {f : α →₀ β} : (0:β) ∉ f.frange := λ H, (mem_frange.1 H).1 rfl theorem frange_single {x : α} {y : β} : frange (single x y) ⊆ {y} := λ r hr, let ⟨t, ht1, ht2⟩ := mem_frange.1 hr in ht2 ▸ (by rw single_apply at ht2 ⊢; split_ifs at ht2 ⊢; [exact finset.mem_singleton_self _, cc]) end frange /-! ### Declarations about `subtype_domain` -/ section subtype_domain variables {α' : Type*} [has_zero δ] {p : α → Prop} section zero variables [has_zero β] {v v' : α' →₀ β} /-- `subtype_domain p f` is the restriction of the finitely supported function `f` to the subtype `p`. -/ def subtype_domain (p : α → Prop) (f : α →₀ β) : (subtype p →₀ β) := ⟨f.support.subtype p, f ∘ subtype.val, λ a, by simp only [mem_subtype, mem_support_iff]⟩ @[simp] lemma support_subtype_domain {f : α →₀ β} : (subtype_domain p f).support = f.support.subtype p := rfl @[simp] lemma subtype_domain_apply {a : subtype p} {v : α →₀ β} : (subtype_domain p v) a = v (a.val) := rfl @[simp] lemma subtype_domain_zero : subtype_domain p (0 : α →₀ β) = 0 := rfl @[to_additive] lemma prod_subtype_domain_index [comm_monoid γ] {v : α →₀ β} {h : α → β → γ} (hp : ∀x∈v.support, p x) : (v.subtype_domain p).prod (λa b, h a.1 b) = v.prod h := prod_bij (λp _, p.val) (λ _, mem_subtype.1) (λ _ _, rfl) (λ _ _ _ _, subtype.eq) (λ b hb, ⟨⟨b, hp b hb⟩, mem_subtype.2 hb, rfl⟩) end zero section monoid variables [add_monoid β] {v v' : α' →₀ β} @[simp] lemma subtype_domain_add {v v' : α →₀ β} : (v + v').subtype_domain p = v.subtype_domain p + v'.subtype_domain p := ext $ λ _, rfl instance subtype_domain.is_add_monoid_hom : is_add_monoid_hom (subtype_domain p : (α →₀ β) → subtype p →₀ β) := { map_add := λ _ _, subtype_domain_add, map_zero := subtype_domain_zero } @[simp] lemma filter_add {v v' : α →₀ β} : (v + v').filter p = v.filter p + v'.filter p := ext $ λ a, begin by_cases p a, { simp only [h, filter_apply_pos, add_apply] }, { simp only [h, add_zero, add_apply, not_false_iff, filter_apply_neg] } end instance filter.is_add_monoid_hom (p : α → Prop) : is_add_monoid_hom (filter p : (α →₀ β) → (α →₀ β)) := { map_zero := filter_zero p, map_add := λ x y, filter_add } end monoid section comm_monoid variables [add_comm_monoid β] lemma subtype_domain_sum {s : finset γ} {h : γ → α →₀ β} : (∑ c in s, h c).subtype_domain p = ∑ c in s, (h c).subtype_domain p := eq.symm (s.sum_hom _) lemma subtype_domain_finsupp_sum {s : γ →₀ δ} {h : γ → δ → α →₀ β} : (s.sum h).subtype_domain p = s.sum (λc d, (h c d).subtype_domain p) := subtype_domain_sum lemma filter_sum (s : finset γ) (f : γ → α →₀ β) : (∑ a in s, f a).filter p = ∑ a in s, filter p (f a) := (s.sum_hom (filter p)).symm end comm_monoid section group variables [add_group β] {v v' : α' →₀ β} @[simp] lemma subtype_domain_neg {v : α →₀ β} : (- v).subtype_domain p = - v.subtype_domain p := ext $ λ _, rfl @[simp] lemma subtype_domain_sub {v v' : α →₀ β} : (v - v').subtype_domain p = v.subtype_domain p - v'.subtype_domain p := ext $ λ _, rfl end group end subtype_domain /-! ### Declarations relating `finsupp` to `multiset` -/ section multiset /-- Given `f : α →₀ ℕ`, `f.to_multiset` is the multiset with multiplicities given by the values of `f` on the elements of `α`. -/ def to_multiset (f : α →₀ ℕ) : multiset α := f.sum (λa n, n •ℕ {a}) lemma to_multiset_zero : (0 : α →₀ ℕ).to_multiset = 0 := rfl lemma to_multiset_add (m n : α →₀ ℕ) : (m + n).to_multiset = m.to_multiset + n.to_multiset := sum_add_index (assume a, zero_nsmul _) (assume a b₁ b₂, add_nsmul _ _ _) lemma to_multiset_single (a : α) (n : ℕ) : to_multiset (single a n) = n •ℕ {a} := by rw [to_multiset, sum_single_index]; apply zero_nsmul instance is_add_monoid_hom.to_multiset : is_add_monoid_hom (to_multiset : _ → multiset α) := { map_zero := to_multiset_zero, map_add := to_multiset_add } lemma card_to_multiset (f : α →₀ ℕ) : f.to_multiset.card = f.sum (λa, id) := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.card_zero, sum_zero_index] }, { assume a n f _ _ ih, rw [to_multiset_add, multiset.card_add, ih, sum_add_index, to_multiset_single, sum_single_index, multiset.card_smul, multiset.singleton_eq_singleton, multiset.card_singleton, mul_one]; intros; refl } end lemma to_multiset_map (f : α →₀ ℕ) (g : α → β) : f.to_multiset.map g = (f.map_domain g).to_multiset := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.map_zero, map_domain_zero, to_multiset_zero] }, { assume a n f _ _ ih, rw [to_multiset_add, multiset.map_add, ih, map_domain_add, map_domain_single, to_multiset_single, to_multiset_add, to_multiset_single, is_add_monoid_hom.map_nsmul (multiset.map g)], refl } end lemma prod_to_multiset [comm_monoid α] (f : α →₀ ℕ) : f.to_multiset.prod = f.prod (λa n, a ^ n) := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.prod_zero, finsupp.prod_zero_index] }, { assume a n f _ _ ih, rw [to_multiset_add, multiset.prod_add, ih, to_multiset_single, finsupp.prod_add_index, finsupp.prod_single_index, multiset.prod_smul, multiset.singleton_eq_singleton, multiset.prod_singleton], { exact pow_zero a }, { exact pow_zero }, { exact pow_add } } end lemma to_finset_to_multiset (f : α →₀ ℕ) : f.to_multiset.to_finset = f.support := begin refine f.induction _ _, { rw [to_multiset_zero, multiset.to_finset_zero, support_zero] }, { assume a n f ha hn ih, rw [to_multiset_add, multiset.to_finset_add, ih, to_multiset_single, support_add_eq, support_single_ne_zero hn, multiset.to_finset_nsmul _ _ hn, multiset.singleton_eq_singleton, multiset.to_finset_cons, multiset.to_finset_zero], refl, refine disjoint.mono_left support_single_subset _, rwa [finset.singleton_disjoint] } end @[simp] lemma count_to_multiset (f : α →₀ ℕ) (a : α) : f.to_multiset.count a = f a := calc f.to_multiset.count a = f.sum (λx n, (n •ℕ {x} : multiset α).count a) : (f.support.sum_hom $ multiset.count a).symm ... = f.sum (λx n, n * ({x} : multiset α).count a) : by simp only [multiset.count_smul] ... = f.sum (λx n, n * (x :: 0 : multiset α).count a) : rfl ... = f a * (a :: 0 : multiset α).count a : sum_eq_single _ (λ a' _ H, by simp only [multiset.count_cons_of_ne (ne.symm H), multiset.count_zero, mul_zero]) (λ H, by simp only [not_mem_support_iff.1 H, zero_mul]) ... = f a : by simp only [multiset.count_singleton, mul_one] /-- Given `m : multiset α`, `of_multiset m` is the finitely supported function from `α` to `ℕ` given by the multiplicities of the elements of `α` in `m`. -/ def of_multiset (m : multiset α) : α →₀ ℕ := on_finset m.to_finset (λa, m.count a) $ λ a H, multiset.mem_to_finset.2 $ by_contradiction (mt multiset.count_eq_zero.2 H) @[simp] lemma of_multiset_apply (m : multiset α) (a : α) : of_multiset m a = m.count a := rfl /-- `equiv_multiset` defines an `equiv` between finitely supported functions from `α` to `ℕ` and multisets on `α`. -/ def equiv_multiset : (α →₀ ℕ) ≃ (multiset α) := ⟨ to_multiset, of_multiset, assume f, finsupp.ext $ λ a, by rw [of_multiset_apply, count_to_multiset], assume m, multiset.ext.2 $ λ a, by rw [count_to_multiset, of_multiset_apply] ⟩ lemma mem_support_multiset_sum [add_comm_monoid β] {s : multiset (α →₀ β)} (a : α) : a ∈ s.sum.support → ∃f∈s, a ∈ (f : α →₀ β).support := multiset.induction_on s false.elim begin assume f s ih ha, by_cases a ∈ f.support, { exact ⟨f, multiset.mem_cons_self _ _, h⟩ }, { simp only [multiset.sum_cons, mem_support_iff, add_apply, not_mem_support_iff.1 h, zero_add] at ha, rcases ih (mem_support_iff.2 ha) with ⟨f', h₀, h₁⟩, exact ⟨f', multiset.mem_cons_of_mem h₀, h₁⟩ } end lemma mem_support_finset_sum [add_comm_monoid β] {s : finset γ} {h : γ → α →₀ β} (a : α) (ha : a ∈ (∑ c in s, h c).support) : ∃c∈s, a ∈ (h c).support := let ⟨f, hf, hfa⟩ := mem_support_multiset_sum a ha in let ⟨c, hc, eq⟩ := multiset.mem_map.1 hf in ⟨c, hc, eq.symm ▸ hfa⟩ lemma mem_support_single [has_zero β] (a a' : α) (b : β) : a ∈ (single a' b).support ↔ a = a' ∧ b ≠ 0 := ⟨λ H : (a ∈ ite _ _ _), if h : b = 0 then by rw if_pos h at H; exact H.elim else ⟨by rw if_neg h at H; exact mem_singleton.1 H, h⟩, λ ⟨h1, h2⟩, show a ∈ ite _ _ _, by rw [if_neg h2]; exact mem_singleton.2 h1⟩ end multiset /-! ### Declarations about `curry` and `uncurry` -/ section curry_uncurry /-- Given a finitely supported function `f` from a product type `α × β` to `γ`, `curry f` is the "curried" finitely supported function from `α` to the type of finitely supported functions from `β` to `γ`. -/ protected def curry [add_comm_monoid γ] (f : (α × β) →₀ γ) : α →₀ (β →₀ γ) := f.sum $ λp c, single p.1 (single p.2 c) lemma sum_curry_index [add_comm_monoid γ] [add_comm_monoid δ] (f : (α × β) →₀ γ) (g : α → β → γ → δ) (hg₀ : ∀ a b, g a b 0 = 0) (hg₁ : ∀a b c₀ c₁, g a b (c₀ + c₁) = g a b c₀ + g a b c₁) : f.curry.sum (λa f, f.sum (g a)) = f.sum (λp c, g p.1 p.2 c) := begin rw [finsupp.curry], transitivity, { exact sum_sum_index (assume a, sum_zero_index) (assume a b₀ b₁, sum_add_index (assume a, hg₀ _ _) (assume c d₀ d₁, hg₁ _ _ _ _)) }, congr, funext p c, transitivity, { exact sum_single_index sum_zero_index }, exact sum_single_index (hg₀ _ _) end /-- Given a finitely supported function `f` from `α` to the type of finitely supported functions from `β` to `γ`, `uncurry f` is the "uncurried" finitely supported function from `α × β` to `γ`. -/ protected def uncurry [add_comm_monoid γ] (f : α →₀ (β →₀ γ)) : (α × β) →₀ γ := f.sum $ λa g, g.sum $ λb c, single (a, b) c /-- `finsupp_prod_equiv` defines the `equiv` between `((α × β) →₀ γ)` and `(α →₀ (β →₀ γ))` given by currying and uncurrying. -/ def finsupp_prod_equiv [add_comm_monoid γ] : ((α × β) →₀ γ) ≃ (α →₀ (β →₀ γ)) := by refine ⟨finsupp.curry, finsupp.uncurry, λ f, _, λ f, _⟩; simp only [ finsupp.curry, finsupp.uncurry, sum_sum_index, sum_zero_index, sum_add_index, sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, prod.mk.eta, (single_sum _ _ _).symm, sum_single] lemma filter_curry [add_comm_monoid β] (f : α₁ × α₂ →₀ β) (p : α₁ → Prop) : (f.filter (λa:α₁×α₂, p a.1)).curry = f.curry.filter p := begin rw [finsupp.curry, finsupp.curry, finsupp.sum, finsupp.sum, @filter_sum _ (α₂ →₀ β) _ p _ f.support _], rw [support_filter, sum_filter], refine finset.sum_congr rfl _, rintros ⟨a₁, a₂⟩ ha, dsimp only, split_ifs, { rw [filter_apply_pos, filter_single_of_pos]; exact h }, { rwa [filter_single_of_neg] } end lemma support_curry [add_comm_monoid β] (f : α₁ × α₂ →₀ β) : f.curry.support ⊆ f.support.image prod.fst := begin rw ← finset.bind_singleton, refine finset.subset.trans support_sum _, refine finset.bind_mono (assume a _, support_single_subset) end end curry_uncurry section variables [group γ] [mul_action γ α] [add_comm_monoid β] /-- Scalar multiplication by a group element g, given by precomposition with the action of g⁻¹ on the domain. -/ def comap_has_scalar : has_scalar γ (α →₀ β) := { smul := λ g f, f.comap_domain (λ a, g⁻¹ • a) (λ a a' m m' h, by simpa [←mul_smul] using (congr_arg (λ a, g • a) h)) } local attribute [instance] comap_has_scalar /-- Scalar multiplication by a group element, given by precomposition with the action of g⁻¹ on the domain, is multiplicative in g. -/ def comap_mul_action : mul_action γ (α →₀ β) := { one_smul := λ f, by { ext, dsimp [(•)], simp, }, mul_smul := λ g g' f, by { ext, dsimp [(•)], simp [mul_smul], }, } local attribute [instance] comap_mul_action /-- Scalar multiplication by a group element, given by precomposition with the action of g⁻¹ on the domain, is additive in the second argument. -/ def comap_distrib_mul_action : distrib_mul_action γ (α →₀ β) := { smul_zero := λ g, by { ext, dsimp [(•)], simp, }, smul_add := λ g f f', by { ext, dsimp [(•)], simp, }, } /-- Scalar multiplication by a group element on finitely supported functions on a group, given by precomposition with the action of g⁻¹. -/ def comap_distrib_mul_action_self : distrib_mul_action γ (γ →₀ β) := @finsupp.comap_distrib_mul_action γ β γ _ (mul_action.regular γ) _ @[simp] lemma comap_smul_single (g : γ) (a : α) (b : β) : g • single a b = single (g • a) b := begin ext a', dsimp [(•)], by_cases h : g • a = a', { subst h, simp [←mul_smul], }, { simp [single_eq_of_ne h], rw [single_eq_of_ne], rintro rfl, simpa [←mul_smul] using h, } end @[simp] lemma comap_smul_apply (g : γ) (f : α →₀ β) (a : α) : (g • f) a = f (g⁻¹ • a) := rfl end section instance [semiring γ] [add_comm_monoid β] [semimodule γ β] : has_scalar γ (α →₀ β) := ⟨λa v, v.map_range ((•) a) (smul_zero _)⟩ variables (α β) @[simp] lemma smul_apply' {R:semiring γ} [add_comm_monoid β] [semimodule γ β] {a : α} {b : γ} {v : α →₀ β} : (b • v) a = b • (v a) := rfl instance [semiring γ] [add_comm_monoid β] [semimodule γ β] : semimodule γ (α →₀ β) := { smul := (•), smul_add := λ a x y, ext $ λ _, smul_add _ _ _, add_smul := λ a x y, ext $ λ _, add_smul _ _ _, one_smul := λ x, ext $ λ _, one_smul _ _, mul_smul := λ r s x, ext $ λ _, mul_smul _ _ _, zero_smul := λ x, ext $ λ _, zero_smul _ _, smul_zero := λ x, ext $ λ _, smul_zero _ } variables {α β} (γ) /-- Evaluation at point as a linear map. This version assumes that the codomain is a semimodule over some semiring. See also `leval`. -/ def leval' [semiring γ] [add_comm_monoid β] [semimodule γ β] (a : α) : (α →₀ β) →ₗ[γ] β := ⟨λ g, g a, λ _ _, add_apply, λ _ _, rfl⟩ @[simp] lemma coe_leval' [semiring γ] [add_comm_monoid β] [semimodule γ β] (a : α) (g : α →₀ β) : leval' γ a g = g a := rfl variable {γ} /-- Evaluation at point as a linear map. This version assumes that the codomain is a semiring. -/ def leval [semiring β] (a : α) : (α →₀ β) →ₗ[β] β := leval' β a @[simp] lemma coe_leval [semiring β] (a : α) (g : α →₀ β) : leval a g = g a := rfl lemma support_smul {R:semiring γ} [add_comm_monoid β] [semimodule γ β] {b : γ} {g : α →₀ β} : (b • g).support ⊆ g.support := λ a, by simp only [smul_apply', mem_support_iff, ne.def]; exact mt (λ h, h.symm ▸ smul_zero _) section variables {α' : Type*} [has_zero δ] {p : α → Prop} @[simp] lemma filter_smul {R : semiring γ} [add_comm_monoid β] [semimodule γ β] {b : γ} {v : α →₀ β} : (b • v).filter p = b • v.filter p := ext $ λ a, begin by_cases p a, { simp only [h, smul_apply', filter_apply_pos] }, { simp only [h, smul_apply', not_false_iff, filter_apply_neg, smul_zero] } end end lemma map_domain_smul {α'} {R : semiring γ} [add_comm_monoid β] [semimodule γ β] {f : α → α'} (b : γ) (v : α →₀ β) : map_domain f (b • v) = b • map_domain f v := begin change map_domain f (map_range _ _ _) = map_range _ _ _, apply finsupp.induction v, { simp only [map_domain_zero, map_range_zero] }, intros a b v' hv₁ hv₂ IH, rw [map_range_add, map_domain_add, IH, map_domain_add, map_range_add, map_range_single, map_domain_single, map_domain_single, map_range_single]; apply smul_add end @[simp] lemma smul_single {R : semiring γ} [add_comm_monoid β] [semimodule γ β] (c : γ) (a : α) (b : β) : c • finsupp.single a b = finsupp.single a (c • b) := ext $ λ a', by by_cases a = a'; [{ subst h, simp only [smul_apply', single_eq_same] }, simp only [h, smul_apply', ne.def, not_false_iff, single_eq_of_ne, smul_zero]] @[simp] lemma smul_single' {R : semiring γ} (c : γ) (a : α) (b : γ) : c • finsupp.single a b = finsupp.single a (c * b) := smul_single _ _ _ end @[simp] lemma smul_apply [semiring β] {a : α} {b : β} {v : α →₀ β} : (b • v) a = b • (v a) := rfl lemma sum_smul_index [ring β] [add_comm_monoid γ] {g : α →₀ β} {b : β} {h : α → β → γ} (h0 : ∀i, h i 0 = 0) : (b • g).sum h = g.sum (λi a, h i (b * a)) := finsupp.sum_map_range_index h0 section variables [semiring β] [semiring γ] lemma sum_mul (b : γ) (s : α →₀ β) {f : α → β → γ} : (s.sum f) * b = s.sum (λ a c, (f a (s a)) * b) := by simp only [finsupp.sum, finset.sum_mul] lemma mul_sum (b : γ) (s : α →₀ β) {f : α → β → γ} : b * (s.sum f) = s.sum (λ a c, b * (f a (s a))) := by simp only [finsupp.sum, finset.mul_sum] protected lemma eq_zero_of_zero_eq_one (zero_eq_one : (0 : β) = 1) (l : α →₀ β) : l = 0 := by ext i; simp only [eq_zero_of_zero_eq_one β zero_eq_one (l i), finsupp.zero_apply] end /-- Given an `add_comm_monoid β` and `s : set α`, `restrict_support_equiv s β` is the `equiv` between the subtype of finitely supported functions with support contained in `s` and the type of finitely supported functions from `s`. -/ def restrict_support_equiv (s : set α) (β : Type*) [add_comm_monoid β] : {f : α →₀ β // ↑f.support ⊆ s } ≃ (s →₀ β):= begin refine ⟨λf, subtype_domain (λx, x ∈ s) f.1, λ f, ⟨f.map_domain subtype.val, _⟩, _, _⟩, { refine set.subset.trans (finset.coe_subset.2 map_domain_support) _, rw [finset.coe_image, set.image_subset_iff], exact assume x hx, x.2 }, { rintros ⟨f, hf⟩, apply subtype.eq, ext a, dsimp only, refine classical.by_cases (assume h : a ∈ set.range (subtype.val : s → α), _) (assume h, _), { rcases h with ⟨x, rfl⟩, rw [map_domain_apply subtype.val_injective, subtype_domain_apply] }, { convert map_domain_notin_range _ _ h, rw [← not_mem_support_iff], refine mt _ h, exact assume ha, ⟨⟨a, hf ha⟩, rfl⟩ } }, { assume f, ext ⟨a, ha⟩, dsimp only, rw [subtype_domain_apply, map_domain_apply subtype.val_injective] } end /-- Given `add_comm_monoid β` and `e : α₁ ≃ α₂`, `dom_congr e` is the corresponding `equiv` between `α₁ →₀ β` and `α₂ →₀ β`. -/ protected def dom_congr [add_comm_monoid β] (e : α₁ ≃ α₂) : (α₁ →₀ β) ≃ (α₂ →₀ β) := ⟨map_domain e, map_domain e.symm, begin assume v, simp only [map_domain_comp.symm, (∘), equiv.symm_apply_apply], exact map_domain_id end, begin assume v, simp only [map_domain_comp.symm, (∘), equiv.apply_symm_apply], exact map_domain_id end⟩ /-! ### Declarations about sigma types -/ section sigma variables {αs : ι → Type*} [has_zero β] (l : (Σ i, αs i) →₀ β) /-- Given `l`, a finitely supported function from the sigma type `Σ (i : ι), αs i` to `β` and an index element `i : ι`, `split l i` is the `i`th component of `l`, a finitely supported function from `as i` to `β`. -/ def split (i : ι) : αs i →₀ β := l.comap_domain (sigma.mk i) (λ x1 x2 _ _ hx, heq_iff_eq.1 (sigma.mk.inj hx).2) lemma split_apply (i : ι) (x : αs i) : split l i x = l ⟨i, x⟩ := begin dunfold split, rw comap_domain_apply end /-- Given `l`, a finitely supported function from the sigma type `Σ (i : ι), αs i` to `β`, `split_support l` is the finset of indices in `ι` that appear in the support of `l`. -/ def split_support : finset ι := l.support.image sigma.fst lemma mem_split_support_iff_nonzero (i : ι) : i ∈ split_support l ↔ split l i ≠ 0 := begin rw [split_support, mem_image, ne.def, ← support_eq_empty, ← ne.def, ← finset.nonempty_iff_ne_empty, split, comap_domain, finset.nonempty], simp only [exists_prop, finset.mem_preimage, exists_and_distrib_right, exists_eq_right, mem_support_iff, sigma.exists, ne.def] end /-- Given `l`, a finitely supported function from the sigma type `Σ i, αs i` to `β` and an `ι`-indexed family `g` of functions from `(αs i →₀ β)` to `γ`, `split_comp` defines a finitely supported function from the index type `ι` to `γ` given by composing `g i` with `split l i`. -/ def split_comp [has_zero γ] (g : Π i, (αs i →₀ β) → γ) (hg : ∀ i x, x = 0 ↔ g i x = 0) : ι →₀ γ := { support := split_support l, to_fun := λ i, g i (split l i), mem_support_to_fun := begin intros i, rw [mem_split_support_iff_nonzero, not_iff_not, hg], end } lemma sigma_support : l.support = l.split_support.sigma (λ i, (l.split i).support) := by simp only [finset.ext_iff, split_support, split, comap_domain, mem_image, mem_preimage, sigma.forall, mem_sigma]; tauto lemma sigma_sum [add_comm_monoid γ] (f : (Σ (i : ι), αs i) → β → γ) : l.sum f = ∑ i in split_support l, (split l i).sum (λ (a : αs i) b, f ⟨i, a⟩ b) := by simp only [sum, sigma_support, sum_sigma, split_apply] end sigma end finsupp /-! ### Declarations relating `multiset` to `finsupp` -/ namespace multiset /-- Given a multiset `s`, `s.to_finsupp` returns the finitely supported function on `ℕ` given by the multiplicities of the elements of `s`. -/ def to_finsupp (s : multiset α) : α →₀ ℕ := { support := s.to_finset, to_fun := λ a, s.count a, mem_support_to_fun := λ a, begin rw mem_to_finset, convert not_iff_not_of_iff (count_eq_zero.symm), rw not_not end } @[simp] lemma to_finsupp_support (s : multiset α) : s.to_finsupp.support = s.to_finset := rfl @[simp] lemma to_finsupp_apply (s : multiset α) (a : α) : s.to_finsupp a = s.count a := rfl @[simp] lemma to_finsupp_zero : to_finsupp (0 : multiset α) = 0 := finsupp.ext $ λ a, count_zero a @[simp] lemma to_finsupp_add (s t : multiset α) : to_finsupp (s + t) = to_finsupp s + to_finsupp t := finsupp.ext $ λ a, count_add a s t lemma to_finsupp_singleton (a : α) : to_finsupp {a} = finsupp.single a 1 := finsupp.ext $ λ b, if h : a = b then by rw [to_finsupp_apply, finsupp.single_apply, h, if_pos rfl, singleton_eq_singleton, count_singleton] else begin rw [to_finsupp_apply, finsupp.single_apply, if_neg h, count_eq_zero, singleton_eq_singleton, mem_singleton], rintro rfl, exact h rfl end namespace to_finsupp instance : is_add_monoid_hom (to_finsupp : multiset α → α →₀ ℕ) := { map_zero := to_finsupp_zero, map_add := to_finsupp_add } end to_finsupp @[simp] lemma to_finsupp_to_multiset (s : multiset α) : s.to_finsupp.to_multiset = s := ext.2 $ λ a, by rw [finsupp.count_to_multiset, to_finsupp_apply] end multiset /-! ### Declarations about order(ed) instances on `finsupp` -/ namespace finsupp variables {σ : Type*} instance [preorder α] [has_zero α] : preorder (σ →₀ α) := { le := λ f g, ∀ s, f s ≤ g s, le_refl := λ f s, le_refl _, le_trans := λ f g h Hfg Hgh s, le_trans (Hfg s) (Hgh s) } instance [partial_order α] [has_zero α] : partial_order (σ →₀ α) := { le_antisymm := λ f g hfg hgf, ext $ λ s, le_antisymm (hfg s) (hgf s), .. finsupp.preorder } instance [ordered_cancel_add_comm_monoid α] : add_left_cancel_semigroup (σ →₀ α) := { add_left_cancel := λ a b c h, ext $ λ s, by { rw ext_iff at h, exact add_left_cancel (h s) }, .. finsupp.add_monoid } instance [ordered_cancel_add_comm_monoid α] : add_right_cancel_semigroup (σ →₀ α) := { add_right_cancel := λ a b c h, ext $ λ s, by { rw ext_iff at h, exact add_right_cancel (h s) }, .. finsupp.add_monoid } instance [ordered_cancel_add_comm_monoid α] : ordered_cancel_add_comm_monoid (σ →₀ α) := { add_le_add_left := λ a b h c s, add_le_add_left (h s) (c s), le_of_add_le_add_left := λ a b c h s, le_of_add_le_add_left (h s), .. finsupp.add_comm_monoid, .. finsupp.partial_order, .. finsupp.add_left_cancel_semigroup, .. finsupp.add_right_cancel_semigroup } lemma le_iff [canonically_ordered_add_monoid α] (f g : σ →₀ α) : f ≤ g ↔ ∀ s ∈ f.support, f s ≤ g s := ⟨λ h s hs, h s, λ h s, if H : s ∈ f.support then h s H else (not_mem_support_iff.1 H).symm ▸ zero_le (g s)⟩ @[simp] lemma add_eq_zero_iff [canonically_ordered_add_monoid α] (f g : σ →₀ α) : f + g = 0 ↔ f = 0 ∧ g = 0 := begin split, { assume h, split, all_goals { ext s, suffices H : f s + g s = 0, { rw add_eq_zero_iff at H, cases H, assumption }, show (f + g) s = 0, rw h, refl } }, { rintro ⟨rfl, rfl⟩, rw add_zero } end attribute [simp] to_multiset_zero to_multiset_add @[simp] lemma to_multiset_to_finsupp (f : σ →₀ ℕ) : f.to_multiset.to_finsupp = f := ext $ λ s, by rw [multiset.to_finsupp_apply, count_to_multiset] lemma to_multiset_strict_mono : strict_mono (@to_multiset σ) := λ m n h, begin rw lt_iff_le_and_ne at h ⊢, cases h with h₁ h₂, split, { rw multiset.le_iff_count, intro s, erw [count_to_multiset m s, count_to_multiset], exact h₁ s }, { intro H, apply h₂, replace H := congr_arg multiset.to_finsupp H, simpa only [to_multiset_to_finsupp] using H } end lemma sum_id_lt_of_lt (m n : σ →₀ ℕ) (h : m < n) : m.sum (λ _, id) < n.sum (λ _, id) := begin rw [← card_to_multiset, ← card_to_multiset], apply multiset.card_lt_of_lt, exact to_multiset_strict_mono h end variable (σ) /-- The order on `σ →₀ ℕ` is well-founded.-/ lemma lt_wf : well_founded (@has_lt.lt (σ →₀ ℕ) _) := subrelation.wf (sum_id_lt_of_lt) $ inv_image.wf _ nat.lt_wf instance decidable_le : decidable_rel (@has_le.le (σ →₀ ℕ) _) := λ m n, by rw le_iff; apply_instance variable {σ} /-- The `finsupp` counterpart of `multiset.antidiagonal`: the antidiagonal of `s : σ →₀ ℕ` consists of all pairs `(t₁, t₂) : (σ →₀ ℕ) × (σ →₀ ℕ)` such that `t₁ + t₂ = s`. The finitely supported function `antidiagonal s` is equal to the multiplicities of these pairs. -/ def antidiagonal (f : σ →₀ ℕ) : ((σ →₀ ℕ) × (σ →₀ ℕ)) →₀ ℕ := (f.to_multiset.antidiagonal.map (prod.map multiset.to_finsupp multiset.to_finsupp)).to_finsupp lemma mem_antidiagonal_support {f : σ →₀ ℕ} {p : (σ →₀ ℕ) × (σ →₀ ℕ)} : p ∈ (antidiagonal f).support ↔ p.1 + p.2 = f := begin erw [multiset.mem_to_finset, multiset.mem_map], split, { rintros ⟨⟨a, b⟩, h, rfl⟩, rw multiset.mem_antidiagonal at h, simpa only [to_multiset_to_finsupp, multiset.to_finsupp_add] using congr_arg multiset.to_finsupp h}, { intro h, refine ⟨⟨p.1.to_multiset, p.2.to_multiset⟩, _, _⟩, { simpa only [multiset.mem_antidiagonal, to_multiset_add] using congr_arg to_multiset h}, { rw [prod.map, to_multiset_to_finsupp, to_multiset_to_finsupp, prod.mk.eta] } } end @[simp] lemma antidiagonal_zero : antidiagonal (0 : σ →₀ ℕ) = single (0,0) 1 := by rw [← multiset.to_finsupp_singleton]; refl lemma swap_mem_antidiagonal_support {n : σ →₀ ℕ} {f} (hf : f ∈ (antidiagonal n).support) : f.swap ∈ (antidiagonal n).support := by simpa only [mem_antidiagonal_support, add_comm, prod.swap] using hf /-- Let `n : σ →₀ ℕ` be a finitely supported function. The set of `m : σ →₀ ℕ` that are coordinatewise less than or equal to `n`, is a finite set. -/ lemma finite_le_nat (n : σ →₀ ℕ) : set.finite {m | m ≤ n} := begin let I := {i // i ∈ n.support}, let k : ℕ := ∑ i in n.support, n i, let f : (σ →₀ ℕ) → (I → fin (k + 1)) := λ m i, m i, have hf : ∀ m ≤ n, ∀ i, (f m i : ℕ) = m i, { intros m hm i, apply fin.coe_coe_of_lt, calc m i ≤ n i : hm i ... < k + 1 : nat.lt_succ_iff.mpr (single_le_sum (λ _ _, nat.zero_le _) i.2) }, have f_im : set.finite (f '' {m | m ≤ n}) := set.finite.of_fintype _, suffices f_inj : set.inj_on f {m | m ≤ n}, { exact set.finite_of_finite_image f_inj f_im }, intros m₁ m₂ h₁ h₂ h, ext i, by_cases hi : i ∈ n.support, { replace h := congr_fun h ⟨i, hi⟩, rwa [fin.ext_iff, ← fin.coe_eq_val, ← fin.coe_eq_val, hf m₁ h₁, hf m₂ h₂] at h }, { rw not_mem_support_iff at hi, specialize h₁ i, specialize h₂ i, rw [hi, nat.le_zero_iff] at h₁ h₂, rw [h₁, h₂] } end /-- Let `n : σ →₀ ℕ` be a finitely supported function. The set of `m : σ →₀ ℕ` that are coordinatewise less than or equal to `n`, but not equal to `n` everywhere, is a finite set. -/ lemma finite_lt_nat (n : σ →₀ ℕ) : set.finite {m | m < n} := set.finite_subset (finite_le_nat n) $ λ m, le_of_lt end finsupp
f172d9fabd0742cb94c524078fa82e243a120328
63abd62053d479eae5abf4951554e1064a4c45b4
/src/data/hash_map.lean
53a2d86e74c7e3236ef6beaa5cb36077d26c0180
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
29,737
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Mario Carneiro -/ import data.pnat.basic import data.list.range import data.array.lemmas import algebra.group import data.sigma.basic /-! # Hash maps Defines a hash map data structure, representing a finite key-value map with a value type that may depend on the key type. The structure requires a `nat`-valued hash function to associate keys to buckets. ## Main definitions * `hash_map`: constructed with `mk_hash_map`. ## Implementation details A hash map with key type `α` and (dependent) value type `β : α → Type*` consists of an array of *buckets*, which are lists containing key/value pairs for that bucket. The hash function is taken modulo `n` to assign keys to their respective bucket. Because of this, some care should be put into the hash function to ensure it evenly distributes keys. The bucket array is an `array`. These have special VM support for in-place modification if there is only ever one reference to them. If one takes special care to never keep references to old versions of a hash map alive after updating it, then the hash map will be modified in-place. In this documentation, when we say a hash map is modified in-place, we are assuming the API is being used in this manner. When inserting (`hash_map.insert`), if the number of stored pairs (the *size*) is going to exceed the number of buckets, then a new hash map is first created with double the number of buckets and everything in the old hash map is reinserted along with the new key/value pair. Otherwise, the bucket array is modified in-place. The amortized running time of inserting $$n$$ elements into a hash map is $$O(n)$$. When removing (`hash_map.erase`), the hash map is modified in-place. The implementation does not reduce the number of buckets in the hash map if the size gets too low. ## Tags hash map -/ universes u v w /-- `bucket_array α β` is the underlying data type for `hash_map α β`, an array of linked lists of key-value pairs. -/ def bucket_array (α : Type u) (β : α → Type v) (n : ℕ+) := array n (list Σ a, β a) /-- Make a hash_map index from a `nat` hash value and a (positive) buffer size -/ def hash_map.mk_idx (n : ℕ+) (i : nat) : fin n := ⟨i % n, nat.mod_lt _ n.2⟩ namespace bucket_array section parameters {α : Type u} {β : α → Type v} (hash_fn : α → nat) variables {n : ℕ+} (data : bucket_array α β n) instance : inhabited (bucket_array α β n) := ⟨mk_array _ []⟩ /-- Read the bucket corresponding to an element -/ def read (a : α) : list Σ a, β a := let bidx := hash_map.mk_idx n (hash_fn a) in data.read bidx /-- Write the bucket corresponding to an element -/ def write (a : α) (l : list Σ a, β a) : bucket_array α β n := let bidx := hash_map.mk_idx n (hash_fn a) in data.write bidx l /-- Modify (read, apply `f`, and write) the bucket corresponding to an element -/ def modify (a : α) (f : list (Σ a, β a) → list (Σ a, β a)) : bucket_array α β n := let bidx := hash_map.mk_idx n (hash_fn a) in array.write data bidx (f (array.read data bidx)) /-- The list of all key-value pairs in the bucket list -/ def as_list : list Σ a, β a := data.to_list.join theorem mem_as_list {a : Σ a, β a} : a ∈ data.as_list ↔ ∃i, a ∈ array.read data i := have (∃ (l : list (Σ (a : α), β a)) (i : fin (n.val)), a ∈ l ∧ array.read data i = l) ↔ ∃ (i : fin (n.val)), a ∈ array.read data i, by rw exists_swap; exact exists_congr (λ i, by simp), by simp [as_list]; simpa [array.mem.def, and_comm] /-- Fold a function `f` over the key-value pairs in the bucket list -/ def foldl {δ : Type w} (d : δ) (f : δ → Π a, β a → δ) : δ := data.foldl d (λ b d, b.foldl (λ r a, f r a.1 a.2) d) theorem foldl_eq {δ : Type w} (d : δ) (f : δ → Π a, β a → δ) : data.foldl d f = data.as_list.foldl (λ r a, f r a.1 a.2) d := by rw [foldl, as_list, list.foldl_join, ← array.to_list_foldl] end end bucket_array namespace hash_map section parameters {α : Type u} {β : α → Type v} (hash_fn : α → nat) /-- Insert the pair `⟨a, b⟩` into the correct location in the bucket array (without checking for duplication) -/ def reinsert_aux {n} (data : bucket_array α β n) (a : α) (b : β a) : bucket_array α β n := data.modify hash_fn a (λl, ⟨a, b⟩ :: l) theorem mk_as_list (n : ℕ+) : bucket_array.as_list (mk_array n [] : bucket_array α β n) = [] := list.eq_nil_iff_forall_not_mem.mpr $ λ x m, let ⟨i, h⟩ := (bucket_array.mem_as_list _).1 m in h parameter [decidable_eq α] /-- Search a bucket for a key `a` and return the value -/ def find_aux (a : α) : list (Σ a, β a) → option (β a) | [] := none | (⟨a',b⟩::t) := if h : a' = a then some (eq.rec_on h b) else find_aux t theorem find_aux_iff {a : α} {b : β a} : Π {l : list Σ a, β a}, (l.map sigma.fst).nodup → (find_aux a l = some b ↔ sigma.mk a b ∈ l) | [] nd := ⟨λn, by injection n, false.elim⟩ | (⟨a',b'⟩::t) nd := begin by_cases a' = a, { clear find_aux_iff, subst h, suffices : b' = b ↔ b' = b ∨ sigma.mk a' b ∈ t, {simpa [find_aux, eq_comm]}, refine (or_iff_left_of_imp (λ m, _)).symm, have : a' ∉ t.map sigma.fst, from list.not_mem_of_nodup_cons nd, exact this.elim (list.mem_map_of_mem sigma.fst m) }, { have : sigma.mk a b ≠ ⟨a', b'⟩, { intro e, injection e with e, exact h e.symm }, simp at nd, simp [find_aux, h, ne.symm h, find_aux_iff, nd] } end /-- Returns `tt` if the bucket `l` contains the key `a` -/ def contains_aux (a : α) (l : list Σ a, β a) : bool := (find_aux a l).is_some theorem contains_aux_iff {a : α} {l : list Σ a, β a} (nd : (l.map sigma.fst).nodup) : contains_aux a l ↔ a ∈ l.map sigma.fst := begin unfold contains_aux, cases h : find_aux a l with b; simp, { assume (b : β a) (m : sigma.mk a b ∈ l), rw (find_aux_iff nd).2 m at h, contradiction }, { show ∃ (b : β a), sigma.mk a b ∈ l, exact ⟨_, (find_aux_iff nd).1 h⟩ }, end /-- Modify a bucket to replace a value in the list. Leaves the list unchanged if the key is not found. -/ def replace_aux (a : α) (b : β a) : list (Σ a, β a) → list (Σ a, β a) | [] := [] | (⟨a', b'⟩::t) := if a' = a then ⟨a, b⟩::t else ⟨a', b'⟩ :: replace_aux t /-- Modify a bucket to remove a key, if it exists. -/ def erase_aux (a : α) : list (Σ a, β a) → list (Σ a, β a) | [] := [] | (⟨a', b'⟩::t) := if a' = a then t else ⟨a', b'⟩ :: erase_aux t /-- The predicate `valid bkts sz` means that `bkts` satisfies the `hash_map` invariants: There are exactly `sz` elements in it, every pair is in the bucket determined by its key and the hash function, and no key appears multiple times in the list. -/ structure valid {n} (bkts : bucket_array α β n) (sz : nat) : Prop := (len : bkts.as_list.length = sz) (idx : ∀ {i} {a : Σ a, β a}, a ∈ array.read bkts i → mk_idx n (hash_fn a.1) = i) (nodup : ∀i, ((array.read bkts i).map sigma.fst).nodup) theorem valid.idx_enum {n} {bkts : bucket_array α β n} {sz : nat} (v : valid bkts sz) {i l} (he : (i, l) ∈ bkts.to_list.enum) {a} {b : β a} (hl : sigma.mk a b ∈ l) : ∃ h, mk_idx n (hash_fn a) = ⟨i, h⟩ := (array.mem_to_list_enum.mp he).imp (λ h e, by subst e; exact v.idx hl) theorem valid.idx_enum_1 {n} {bkts : bucket_array α β n} {sz : nat} (v : valid bkts sz) {i l} (he : (i, l) ∈ bkts.to_list.enum) {a} {b : β a} (hl : sigma.mk a b ∈ l) : (mk_idx n (hash_fn a)).1 = i := let ⟨h, e⟩ := v.idx_enum _ he hl in by rw e; refl theorem valid.as_list_nodup {n} {bkts : bucket_array α β n} {sz : nat} (v : valid bkts sz) : (bkts.as_list.map sigma.fst).nodup := begin suffices : (bkts.to_list.map (list.map sigma.fst)).pairwise list.disjoint, { simp [bucket_array.as_list, list.nodup_join, this], change ∀ l s, array.mem s bkts → list.map sigma.fst s = l → l.nodup, introv m e, subst e, cases m with i e, subst e, apply v.nodup }, rw [← list.enum_map_snd bkts.to_list, list.pairwise_map, list.pairwise_map], have : (bkts.to_list.enum.map prod.fst).nodup := by simp [list.nodup_range], refine list.pairwise.imp_of_mem _ ((list.pairwise_map _).1 this), rw prod.forall, intros i l₁, rw prod.forall, intros j l₂ me₁ me₂ ij, simp [list.disjoint], intros a b ml₁ b' ml₂, apply ij, rwa [← v.idx_enum_1 _ me₁ ml₁, ← v.idx_enum_1 _ me₂ ml₂] end theorem mk_valid (n : ℕ+) : @valid n (mk_array n []) 0 := ⟨by simp [mk_as_list], λ i a h, by cases h, λ i, list.nodup_nil⟩ theorem valid.find_aux_iff {n} {bkts : bucket_array α β n} {sz : nat} (v : valid bkts sz) {a : α} {b : β a} : find_aux a (bkts.read hash_fn a) = some b ↔ sigma.mk a b ∈ bkts.as_list := (find_aux_iff (v.nodup _)).trans $ by rw bkts.mem_as_list; exact ⟨λ h, ⟨_, h⟩, λ ⟨i, h⟩, (v.idx h).symm ▸ h⟩ theorem valid.contains_aux_iff {n} {bkts : bucket_array α β n} {sz : nat} (v : valid bkts sz) (a : α) : contains_aux a (bkts.read hash_fn a) ↔ a ∈ bkts.as_list.map sigma.fst := by simp [contains_aux, option.is_some_iff_exists, v.find_aux_iff hash_fn] section parameters {n : ℕ+} {bkts : bucket_array α β n} {bidx : fin n} {f : list (Σ a, β a) → list (Σ a, β a)} (u v1 v2 w : list Σ a, β a) local notation `L` := array.read bkts bidx private def bkts' : bucket_array α β n := array.write bkts bidx (f L) variables (hl : L = u ++ v1 ++ w) (hfl : f L = u ++ v2 ++ w) include hl hfl theorem append_of_modify : ∃ u' w', bkts.as_list = u' ++ v1 ++ w' ∧ bkts'.as_list = u' ++ v2 ++ w' := begin unfold bucket_array.as_list, have h : (bidx : ℕ) < bkts.to_list.length, { simp only [bidx.is_lt, array.to_list_length] }, refine ⟨(bkts.to_list.take bidx).join ++ u, w ++ (bkts.to_list.drop (bidx+1)).join, _, _⟩, { conv { to_lhs, rw [← list.take_append_drop bidx bkts.to_list, list.drop_eq_nth_le_cons h], simp [hl] }, simp }, { conv { to_lhs, rw [bkts', array.write_to_list, list.update_nth_eq_take_cons_drop _ h], simp [hfl] }, simp } end variables (hvnd : (v2.map sigma.fst).nodup) (hal : ∀ (a : Σ a, β a), a ∈ v2 → mk_idx n (hash_fn a.1) = bidx) (djuv : (u.map sigma.fst).disjoint (v2.map sigma.fst)) (djwv : (w.map sigma.fst).disjoint (v2.map sigma.fst)) include hvnd hal djuv djwv theorem valid.modify {sz : ℕ} (v : valid bkts sz) : v1.length ≤ sz + v2.length ∧ valid bkts' (sz + v2.length - v1.length) := begin rcases append_of_modify u v1 v2 w hl hfl with ⟨u', w', e₁, e₂⟩, rw [← v.len, e₁], suffices : valid bkts' (u' ++ v2 ++ w').length, { simpa [ge, add_comm, add_left_comm, nat.le_add_right, nat.add_sub_cancel_left] }, refine ⟨congr_arg _ e₂, λ i a, _, λ i, _⟩, { by_cases bidx = i, { subst i, rw [bkts', array.read_write, hfl], have := @valid.idx _ _ _ v bidx a, simp only [hl, list.mem_append, or_imp_distrib, forall_and_distrib] at this ⊢, exact ⟨⟨this.1.1, hal _⟩, this.2⟩ }, { rw [bkts', array.read_write_of_ne _ _ h], apply v.idx } }, { by_cases bidx = i, { subst i, rw [bkts', array.read_write, hfl], have := @valid.nodup _ _ _ v bidx, simp [hl, list.nodup_append] at this, simp [list.nodup_append, this, hvnd, djuv, djwv.symm] }, { rw [bkts', array.read_write_of_ne _ _ h], apply v.nodup } } end end theorem valid.replace_aux (a : α) (b : β a) : Π (l : list (Σ a, β a)), a ∈ l.map sigma.fst → ∃ (u w : list Σ a, β a) b', l = u ++ [⟨a, b'⟩] ++ w ∧ replace_aux a b l = u ++ [⟨a, b⟩] ++ w | [] := false.elim | (⟨a', b'⟩::t) := begin by_cases e : a' = a, { subst a', suffices : ∃ (u w : list Σ a, β a) (b'' : β a), (sigma.mk a b') :: t = u ++ ⟨a, b''⟩ :: w ∧ replace_aux a b (⟨a, b'⟩ :: t) = u ++ ⟨a, b⟩ :: w, {simpa}, refine ⟨[], t, b', _⟩, simp [replace_aux] }, { suffices : ∀ (x : β a) (_ : sigma.mk a x ∈ t), ∃ u w (b'' : β a), (sigma.mk a' b') :: t = u ++ ⟨a, b''⟩ :: w ∧ (sigma.mk a' b') :: (replace_aux a b t) = u ++ ⟨a, b⟩ :: w, { simpa [replace_aux, ne.symm e, e] }, intros x m, have IH : ∀ (x : β a) (_ : sigma.mk a x ∈ t), ∃ u w (b'' : β a), t = u ++ ⟨a, b''⟩ :: w ∧ replace_aux a b t = u ++ ⟨a, b⟩ :: w, { simpa using valid.replace_aux t }, rcases IH x m with ⟨u, w, b'', hl, hfl⟩, exact ⟨⟨a', b'⟩ :: u, w, b'', by simp [hl, hfl.symm, ne.symm e]⟩ } end theorem valid.replace {n : ℕ+} {bkts : bucket_array α β n} {sz : ℕ} (a : α) (b : β a) (Hc : contains_aux a (bkts.read hash_fn a)) (v : valid bkts sz) : valid (bkts.modify hash_fn a (replace_aux a b)) sz := begin have nd := v.nodup (mk_idx n (hash_fn a)), rcases hash_map.valid.replace_aux a b (array.read bkts (mk_idx n (hash_fn a))) ((contains_aux_iff nd).1 Hc) with ⟨u, w, b', hl, hfl⟩, simp [hl, list.nodup_append] at nd, refine (v.modify hash_fn u [⟨a, b'⟩] [⟨a, b⟩] w hl hfl (list.nodup_singleton _) (λa' e, by simp at e; rw e) (λa' e1 e2, _) (λa' e1 e2, _)).2; { revert e1, simp [-sigma.exists] at e2, subst a', simp [nd] } end theorem valid.insert {n : ℕ+} {bkts : bucket_array α β n} {sz : ℕ} (a : α) (b : β a) (Hnc : ¬ contains_aux a (bkts.read hash_fn a)) (v : valid bkts sz) : valid (reinsert_aux bkts a b) (sz+1) := begin have nd := v.nodup (mk_idx n (hash_fn a)), refine (v.modify hash_fn [] [] [⟨a, b⟩] (bkts.read hash_fn a) rfl rfl (list.nodup_singleton _) (λa' e, by simp at e; rw e) (λa', false.elim) (λa' e1 e2, _)).2, simp [-sigma.exists] at e2, subst a', exact Hnc ((contains_aux_iff nd).2 e1) end theorem valid.erase_aux (a : α) : Π (l : list (Σ a, β a)), a ∈ l.map sigma.fst → ∃ (u w : list Σ a, β a) b, l = u ++ [⟨a, b⟩] ++ w ∧ erase_aux a l = u ++ [] ++ w | [] := false.elim | (⟨a', b'⟩::t) := begin by_cases e : a' = a, { subst a', simpa [erase_aux, and_comm] using show ∃ u w (x : β a), t = u ++ w ∧ (sigma.mk a b') :: t = u ++ ⟨a, x⟩ :: w, from ⟨[], t, b', by simp⟩ }, { simp [erase_aux, e, ne.symm e], suffices : ∀ (b : β a) (_ : sigma.mk a b ∈ t), ∃ u w (x : β a), (sigma.mk a' b') :: t = u ++ ⟨a, x⟩ :: w ∧ (sigma.mk a' b') :: (erase_aux a t) = u ++ w, { simpa [replace_aux, ne.symm e, e] }, intros b m, have IH : ∀ (x : β a) (_ : sigma.mk a x ∈ t), ∃ u w (x : β a), t = u ++ ⟨a, x⟩ :: w ∧ erase_aux a t = u ++ w, { simpa using valid.erase_aux t }, rcases IH b m with ⟨u, w, b'', hl, hfl⟩, exact ⟨⟨a', b'⟩ :: u, w, b'', by simp [hl, hfl.symm]⟩ } end theorem valid.erase {n} {bkts : bucket_array α β n} {sz} (a : α) (Hc : contains_aux a (bkts.read hash_fn a)) (v : valid bkts sz) : valid (bkts.modify hash_fn a (erase_aux a)) (sz-1) := begin have nd := v.nodup (mk_idx n (hash_fn a)), rcases hash_map.valid.erase_aux a (array.read bkts (mk_idx n (hash_fn a))) ((contains_aux_iff nd).1 Hc) with ⟨u, w, b, hl, hfl⟩, refine (v.modify hash_fn u [⟨a, b⟩] [] w hl hfl list.nodup_nil _ _ _).2; simp end end end hash_map /-- A hash map data structure, representing a finite key-value map with key type `α` and value type `β` (which may depend on `α`). -/ structure hash_map (α : Type u) [decidable_eq α] (β : α → Type v) := (hash_fn : α → nat) (size : ℕ) (nbuckets : ℕ+) (buckets : bucket_array α β nbuckets) (is_valid : hash_map.valid hash_fn buckets size) /-- Construct an empty hash map with buffer size `nbuckets` (default 8). -/ def mk_hash_map {α : Type u} [decidable_eq α] {β : α → Type v} (hash_fn : α → nat) (nbuckets := 8) : hash_map α β := let n := if nbuckets = 0 then 8 else nbuckets in let nz : n > 0 := by abstract { cases nbuckets; simp [if_pos, nat.succ_ne_zero] } in { hash_fn := hash_fn, size := 0, nbuckets := ⟨n, nz⟩, buckets := mk_array n [], is_valid := hash_map.mk_valid _ _ } namespace hash_map variables {α : Type u} {β : α → Type v} [decidable_eq α] /-- Return the value corresponding to a key, or `none` if not found -/ def find (m : hash_map α β) (a : α) : option (β a) := find_aux a (m.buckets.read m.hash_fn a) /-- Return `tt` if the key exists in the map -/ def contains (m : hash_map α β) (a : α) : bool := (m.find a).is_some instance : has_mem α (hash_map α β) := ⟨λa m, m.contains a⟩ /-- Fold a function over the key-value pairs in the map -/ def fold {δ : Type w} (m : hash_map α β) (d : δ) (f : δ → Π a, β a → δ) : δ := m.buckets.foldl d f /-- The list of key-value pairs in the map -/ def entries (m : hash_map α β) : list Σ a, β a := m.buckets.as_list /-- The list of keys in the map -/ def keys (m : hash_map α β) : list α := m.entries.map sigma.fst theorem find_iff (m : hash_map α β) (a : α) (b : β a) : m.find a = some b ↔ sigma.mk a b ∈ m.entries := m.is_valid.find_aux_iff _ theorem contains_iff (m : hash_map α β) (a : α) : m.contains a ↔ a ∈ m.keys := m.is_valid.contains_aux_iff _ _ theorem entries_empty (hash_fn : α → nat) (n) : (@mk_hash_map α _ β hash_fn n).entries = [] := mk_as_list _ theorem keys_empty (hash_fn : α → nat) (n) : (@mk_hash_map α _ β hash_fn n).keys = [] := by dsimp [keys]; rw entries_empty; refl theorem find_empty (hash_fn : α → nat) (n a) : (@mk_hash_map α _ β hash_fn n).find a = none := by induction h : (@mk_hash_map α _ β hash_fn n).find a; [refl, { have := (find_iff _ _ _).1 h, rw entries_empty at this, contradiction }] theorem not_contains_empty (hash_fn : α → nat) (n a) : ¬ (@mk_hash_map α _ β hash_fn n).contains a := by apply bool_iff_false.2; dsimp [contains]; rw [find_empty]; refl theorem insert_lemma (hash_fn : α → nat) {n n'} {bkts : bucket_array α β n} {sz} (v : valid hash_fn bkts sz) : valid hash_fn (bkts.foldl (mk_array _ [] : bucket_array α β n') (reinsert_aux hash_fn)) sz := begin suffices : ∀ (l : list Σ a, β a) (t : bucket_array α β n') sz, valid hash_fn t sz → ((l ++ t.as_list).map sigma.fst).nodup → valid hash_fn (l.foldl (λr (a : Σ a, β a), reinsert_aux hash_fn r a.1 a.2) t) (sz + l.length), { have p := this bkts.as_list _ _ (mk_valid _ _), rw [mk_as_list, list.append_nil, zero_add, v.len] at p, rw bucket_array.foldl_eq, exact p (v.as_list_nodup _) }, intro l, induction l with c l IH; intros t sz v nd, {exact v}, rw show sz + (c :: l).length = sz + 1 + l.length, by simp [add_comm, add_assoc], rcases (show (l.map sigma.fst).nodup ∧ ((bucket_array.as_list t).map sigma.fst).nodup ∧ c.fst ∉ l.map sigma.fst ∧ c.fst ∉ (bucket_array.as_list t).map sigma.fst ∧ (l.map sigma.fst).disjoint ((bucket_array.as_list t).map sigma.fst), by simpa [list.nodup_append, not_or_distrib, and_comm, and.left_comm] using nd) with ⟨nd1, nd2, nm1, nm2, dj⟩, have v' := v.insert _ _ c.2 (λHc, nm2 $ (v.contains_aux_iff _ c.1).1 Hc), apply IH _ _ v', suffices : ∀ ⦃a : α⦄ (b : β a), sigma.mk a b ∈ l → ∀ (b' : β a), sigma.mk a b' ∈ (reinsert_aux hash_fn t c.1 c.2).as_list → false, { simpa [list.nodup_append, nd1, v'.as_list_nodup _, list.disjoint] }, intros a b m1 b' m2, rcases (reinsert_aux hash_fn t c.1 c.2).mem_as_list.1 m2 with ⟨i, im⟩, have : sigma.mk a b' ∉ array.read t i, { intro m3, have : a ∈ list.map sigma.fst t.as_list := list.mem_map_of_mem sigma.fst (t.mem_as_list.2 ⟨_, m3⟩), exact dj (list.mem_map_of_mem sigma.fst m1) this }, by_cases h : mk_idx n' (hash_fn c.1) = i, { subst h, have e : sigma.mk a b' = ⟨c.1, c.2⟩, { simpa [reinsert_aux, bucket_array.modify, array.read_write, this] using im }, injection e with e, subst a, exact nm1.elim (@list.mem_map_of_mem _ _ sigma.fst _ _ m1) }, { apply this, simpa [reinsert_aux, bucket_array.modify, array.read_write_of_ne _ _ h] using im } end /-- Insert a key-value pair into the map. (Modifies `m` in-place when applicable) -/ def insert : Π (m : hash_map α β) (a : α) (b : β a), hash_map α β | ⟨hash_fn, size, n, buckets, v⟩ a b := let bkt := buckets.read hash_fn a in if hc : contains_aux a bkt then { hash_fn := hash_fn, size := size, nbuckets := n, buckets := buckets.modify hash_fn a (replace_aux a b), is_valid := v.replace _ a b hc } else let size' := size + 1, buckets' := buckets.modify hash_fn a (λl, ⟨a, b⟩::l), valid' := v.insert _ a b hc in if size' ≤ n then { hash_fn := hash_fn, size := size', nbuckets := n, buckets := buckets', is_valid := valid' } else let n' : ℕ+ := ⟨n * 2, mul_pos n.2 dec_trivial⟩, buckets'' : bucket_array α β n' := buckets'.foldl (mk_array _ []) (reinsert_aux hash_fn) in { hash_fn := hash_fn, size := size', nbuckets := n', buckets := buckets'', is_valid := insert_lemma _ valid' } theorem mem_insert : Π (m : hash_map α β) (a b a' b'), (sigma.mk a' b' : sigma β) ∈ (m.insert a b).entries ↔ if a = a' then b == b' else sigma.mk a' b' ∈ m.entries | ⟨hash_fn, size, n, bkts, v⟩ a b a' b' := begin let bkt := bkts.read hash_fn a, have nd : (bkt.map sigma.fst).nodup := v.nodup (mk_idx n (hash_fn a)), have lem : Π (bkts' : bucket_array α β n) (v1 u w) (hl : bucket_array.as_list bkts = u ++ v1 ++ w) (hfl : bucket_array.as_list bkts' = u ++ [⟨a, b⟩] ++ w) (veq : (v1 = [] ∧ ¬ contains_aux a bkt) ∨ ∃b'', v1 = [⟨a, b''⟩]), sigma.mk a' b' ∈ bkts'.as_list ↔ if a = a' then b == b' else sigma.mk a' b' ∈ bkts.as_list, { intros bkts' v1 u w hl hfl veq, rw [hl, hfl], by_cases h : a = a', { subst a', suffices : b = b' ∨ sigma.mk a b' ∈ u ∨ sigma.mk a b' ∈ w ↔ b = b', { simpa [eq_comm, or.left_comm] }, refine or_iff_left_of_imp (not.elim $ not_or_distrib.2 _), rcases veq with ⟨rfl, Hnc⟩ | ⟨b'', rfl⟩, { have na := (not_iff_not_of_iff $ v.contains_aux_iff _ _).1 Hnc, simp [hl, not_or_distrib] at na, simp [na] }, { have nd' := v.as_list_nodup _, simp [hl, list.nodup_append] at nd', simp [nd'] } }, { suffices : sigma.mk a' b' ∉ v1, {simp [h, ne.symm h, this]}, rcases veq with ⟨rfl, Hnc⟩ | ⟨b'', rfl⟩; simp [ne.symm h] } }, by_cases Hc : (contains_aux a bkt : Prop), { rcases hash_map.valid.replace_aux a b (array.read bkts (mk_idx n (hash_fn a))) ((contains_aux_iff nd).1 Hc) with ⟨u', w', b'', hl', hfl'⟩, rcases (append_of_modify u' [⟨a, b''⟩] [⟨a, b⟩] w' hl' hfl') with ⟨u, w, hl, hfl⟩, simpa [insert, @dif_pos (contains_aux a bkt) _ Hc] using lem _ _ u w hl hfl (or.inr ⟨b'', rfl⟩) }, { let size' := size + 1, let bkts' := bkts.modify hash_fn a (λl, ⟨a, b⟩::l), have mi : sigma.mk a' b' ∈ bkts'.as_list ↔ if a = a' then b == b' else sigma.mk a' b' ∈ bkts.as_list := let ⟨u, w, hl, hfl⟩ := append_of_modify [] [] [⟨a, b⟩] _ rfl rfl in lem bkts' _ u w hl hfl $ or.inl ⟨rfl, Hc⟩, simp [insert, @dif_neg (contains_aux a bkt) _ Hc], by_cases h : size' ≤ n, -- TODO(Mario): Why does the by_cases assumption look different than the stated one? { simpa [show size' ≤ n, from h] using mi }, { let n' : ℕ+ := ⟨n * 2, mul_pos n.2 dec_trivial⟩, let bkts'' : bucket_array α β n' := bkts'.foldl (mk_array _ []) (reinsert_aux hash_fn), suffices : sigma.mk a' b' ∈ bkts''.as_list ↔ sigma.mk a' b' ∈ bkts'.as_list.reverse, { simpa [show ¬ size' ≤ n, from h, mi] }, rw [show bkts'' = bkts'.as_list.foldl _ _, from bkts'.foldl_eq _ _, ← list.foldr_reverse], induction bkts'.as_list.reverse with a l IH, { simp [mk_as_list] }, { cases a with a'' b'', let B := l.foldr (λ (y : sigma β) (x : bucket_array α β n'), reinsert_aux hash_fn x y.1 y.2) (mk_array n' []), rcases append_of_modify [] [] [⟨a'', b''⟩] _ rfl rfl with ⟨u, w, hl, hfl⟩, simp [IH.symm, or.left_comm, show B.as_list = _, from hl, show (reinsert_aux hash_fn B a'' b'').as_list = _, from hfl] } } } end theorem find_insert_eq (m : hash_map α β) (a : α) (b : β a) : (m.insert a b).find a = some b := (find_iff (m.insert a b) a b).2 $ (mem_insert m a b a b).2 $ by rw if_pos rfl theorem find_insert_ne (m : hash_map α β) (a a' : α) (b : β a) (h : a ≠ a') : (m.insert a b).find a' = m.find a' := option.eq_of_eq_some $ λb', let t := mem_insert m a b a' b' in (find_iff _ _ _).trans $ iff.trans (by rwa if_neg h at t) (find_iff _ _ _).symm theorem find_insert (m : hash_map α β) (a' a : α) (b : β a) : (m.insert a b).find a' = if h : a = a' then some (eq.rec_on h b) else m.find a' := if h : a = a' then by rw dif_pos h; exact match a', h with ._, rfl := find_insert_eq m a b end else by rw dif_neg h; exact find_insert_ne m a a' b h /-- Insert a list of key-value pairs into the map. (Modifies `m` in-place when applicable) -/ def insert_all (l : list (Σ a, β a)) (m : hash_map α β) : hash_map α β := l.foldl (λ m ⟨a, b⟩, insert m a b) m /-- Construct a hash map from a list of key-value pairs. -/ def of_list (l : list (Σ a, β a)) (hash_fn) : hash_map α β := insert_all l (mk_hash_map hash_fn (2 * l.length)) /-- Remove a key from the map. (Modifies `m` in-place when applicable) -/ def erase (m : hash_map α β) (a : α) : hash_map α β := match m with ⟨hash_fn, size, n, buckets, v⟩ := if hc : contains_aux a (buckets.read hash_fn a) then { hash_fn := hash_fn, size := size - 1, nbuckets := n, buckets := buckets.modify hash_fn a (erase_aux a), is_valid := v.erase _ a hc } else m end theorem mem_erase : Π (m : hash_map α β) (a a' b'), (sigma.mk a' b' : sigma β) ∈ (m.erase a).entries ↔ a ≠ a' ∧ sigma.mk a' b' ∈ m.entries | ⟨hash_fn, size, n, bkts, v⟩ a a' b' := begin let bkt := bkts.read hash_fn a, by_cases Hc : (contains_aux a bkt : Prop), { let bkts' := bkts.modify hash_fn a (erase_aux a), suffices : sigma.mk a' b' ∈ bkts'.as_list ↔ a ≠ a' ∧ sigma.mk a' b' ∈ bkts.as_list, { simpa [erase, @dif_pos (contains_aux a bkt) _ Hc] }, have nd := v.nodup (mk_idx n (hash_fn a)), rcases valid.erase_aux a bkt ((contains_aux_iff nd).1 Hc) with ⟨u', w', b, hl', hfl'⟩, rcases append_of_modify u' [⟨a, b⟩] [] _ hl' hfl' with ⟨u, w, hl, hfl⟩, suffices : ∀_:sigma.mk a' b' ∈ u ∨ sigma.mk a' b' ∈ w, a ≠ a', { have : sigma.mk a' b' ∈ u ∨ sigma.mk a' b' ∈ w ↔ (¬a = a' ∧ a' = a) ∧ b' == b ∨ ¬a = a' ∧ (sigma.mk a' b' ∈ u ∨ sigma.mk a' b' ∈ w), { simp [eq_comm, not_and_self_iff, and_iff_right_of_imp this] }, simpa [hl, show bkts'.as_list = _, from hfl, and_or_distrib_left, and_comm, and.left_comm, or.left_comm] }, intros m e, subst a', revert m, apply not_or_distrib.2, have nd' := v.as_list_nodup _, simp [hl, list.nodup_append] at nd', simp [nd'] }, { suffices : ∀_:sigma.mk a' b' ∈ bucket_array.as_list bkts, a ≠ a', { simp [erase, @dif_neg (contains_aux a bkt) _ Hc, entries, and_iff_right_of_imp this] }, intros m e, subst a', exact Hc ((v.contains_aux_iff _ _).2 (list.mem_map_of_mem sigma.fst m)) } end theorem find_erase_eq (m : hash_map α β) (a : α) : (m.erase a).find a = none := begin cases h : (m.erase a).find a with b, {refl}, exact absurd rfl ((mem_erase m a a b).1 ((find_iff (m.erase a) a b).1 h)).left end theorem find_erase_ne (m : hash_map α β) (a a' : α) (h : a ≠ a') : (m.erase a).find a' = m.find a' := option.eq_of_eq_some $ λb', (find_iff _ _ _).trans $ (mem_erase m a a' b').trans $ (and_iff_right h).trans (find_iff _ _ _).symm theorem find_erase (m : hash_map α β) (a' a : α) : (m.erase a).find a' = if a = a' then none else m.find a' := if h : a = a' then by subst a'; simp [find_erase_eq m a] else by rw if_neg h; exact find_erase_ne m a a' h section string variables [has_to_string α] [∀ a, has_to_string (β a)] open prod private def key_data_to_string (a : α) (b : β a) (first : bool) : string := (if first then "" else ", ") ++ sformat!"{a} ← {b}" private def to_string (m : hash_map α β) : string := "⟨" ++ (fst (fold m ("", tt) (λ p a b, (fst p ++ key_data_to_string a b (snd p), ff)))) ++ "⟩" instance : has_to_string (hash_map α β) := ⟨to_string⟩ end string section format open format prod variables [has_to_format α] [∀ a, has_to_format (β a)] private meta def format_key_data (a : α) (b : β a) (first : bool) : format := (if first then to_fmt "" else to_fmt "," ++ line) ++ to_fmt a ++ space ++ to_fmt "←" ++ space ++ to_fmt b private meta def to_format (m : hash_map α β) : format := group $ to_fmt "⟨" ++ nest 1 (fst (fold m (to_fmt "", tt) (λ p a b, (fst p ++ format_key_data a b (snd p), ff)))) ++ to_fmt "⟩" meta instance : has_to_format (hash_map α β) := ⟨to_format⟩ end format /-- `hash_map` with key type `nat` and value type that may vary. -/ instance {β : ℕ → Type*} : inhabited (hash_map ℕ β) := ⟨mk_hash_map id⟩ end hash_map
2fd54dee7b164fc2c2c0f4d9ff399fc954b58d3f
159fed64bfae88f3b6a6166836d6278f953bcbf9
/Structure/ConcreteBuildingBlocks.lean
f9059fec84c60c4376322a9edbe5a567c2ae9d48
[ "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
316
lean
-- An abstract formalization of "isomorphism is equality up to relabeling" -- ------------------------------------------------------------------------- -- -- See `README.md` for more info. -- -- TODO: Here, we need to specialize the building blocks from `AbstractBuildingBlocks.lean` to actual types and instances.
7c1773824348d3657d6e69643259111c51f81c25
1b8f093752ba748c5ca0083afef2959aaa7dace5
/src/category_theory/functor_categories/whiskering.lean
2b196d9fe2e909e7ef6ec431c1d36ce57ec1e93b
[]
no_license
khoek/lean-category-theory
7ec4cda9cc64a5a4ffeb84712ac7d020dbbba386
63dcb598e9270a3e8b56d1769eb4f825a177cd95
refs/heads/master
1,585,251,725,759
1,539,344,445,000
1,539,344,445,000
145,281,070
0
0
null
1,534,662,376,000
1,534,662,376,000
null
UTF-8
Lean
false
false
3,362
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.functor_category import category_theory.tactics.obviously set_option trace.tidy true namespace category_theory universes u₁ v₁ u₂ v₂ u₃ v₃ u₄ v₄ variables (C : Type u₁) [𝒞 : category.{u₁ v₁} C] (D : Type u₂) [𝒟 : category.{u₂ v₂} D] (E : Type u₃) [ℰ : category.{u₃ v₃} E] include 𝒞 𝒟 ℰ def whiskering_on_left : (C ⥤ D) ⥤ ((D ⥤ E) ⥤ (C ⥤ E)) := { obj := λ F, { obj := λ G, F ⋙ G, map' := λ _ _ α, (nat_trans.id _) ◫ α }, map' := λ F G τ, { app := λ H, { app := λ c, H.map (τ c), naturality' := begin intros X Y f, dsimp at *, erw [←functor.map_comp, ←functor.map_comp, ←nat_trans.naturality] end }, naturality' := begin intros X Y f, ext1, dsimp at *, simp at *, erw [←nat_trans.naturality] end } } def whiskering_on_right : (D ⥤ E) ⥤ ((C ⥤ D) ⥤ (C ⥤ E)) := { obj := λ H, { obj := λ F, F ⋙ H, map' := λ _ _ α, α ◫ (nat_trans.id _) }, map' := λ G H τ, { app := λ F, { app := λ c, τ (F c), naturality' := begin intros X Y f, dsimp at *, erw [nat_trans.naturality] end }, naturality' := begin intros X Y f, ext1, dsimp at *, simp at *, erw [←nat_trans.naturality] end } } variables {C} {D} {E} def whisker_on_left (F : C ⥤ D) {G H : D ⥤ E} (α : G ⟹ H) : (F ⋙ G) ⟹ (F ⋙ H) := ((whiskering_on_left C D E) F).map α @[simp] lemma whisker_on_left.app (F : C ⥤ D) {G H : D ⥤ E} (α : G ⟹ H) (X : C) : (whisker_on_left F α) X = α (F X) := begin dsimp [whisker_on_left, whiskering_on_left], rw category_theory.functor.map_id, rw category.comp_id, end def whisker_on_right {G H : C ⥤ D} (α : G ⟹ H) (F : D ⥤ E) : (G ⋙ F) ⟹ (H ⋙ F) := ((whiskering_on_right C D E) F).map α @[simp] lemma whisker_on_right.app {G H : C ⥤ D} (α : G ⟹ H) (F : D ⥤ E) (X : C) : (whisker_on_right α F) X = F.map (α X) := begin dsimp [whisker_on_right, whiskering_on_right], rw category.id_comp, end @[simp] lemma whisker_on_left_vcomp (F : C ⥤ D) {G H K : D ⥤ E} (α : G ⟹ H) (β : H ⟹ K) : whisker_on_left F (α ⊟ β) = ((whisker_on_left F α) ⊟ (whisker_on_left F β)) := ((whiskering_on_left C D E) F).map_comp α β @[simp] lemma whisker_on_right_vcomp {G H K : C ⥤ D} (α : G ⟹ H) (β : H ⟹ K) (F : D ⥤ E) : whisker_on_right (α ⊟ β) F = ((whisker_on_right α F) ⊟ (whisker_on_right β F)) := ((whiskering_on_right C D E) F).map_comp α β variables {B : Type u₄} [ℬ : category.{u₄ v₄} B] include ℬ @[simp] lemma whisker_on_left_twice (F : B ⥤ C) (G : C ⥤ D) {H K : D ⥤ E} (α : H ⟹ K) : whisker_on_left F (whisker_on_left G α) = (@whisker_on_left _ _ _ _ _ _ (F ⋙ G) _ _ α) := by tidy @[simp] lemma whisker_on_right_twice {H K : B ⥤ C} (F : C ⥤ D) (G : D ⥤ E) (α : H ⟹ K) : whisker_on_right (whisker_on_right α F) G = (@whisker_on_right _ _ _ _ _ _ _ _ α (F ⋙ G)) := by tidy lemma whisker_on_right_left (F : B ⥤ C) {G H : C ⥤ D} (α : G ⟹ H) (K : D ⥤ E) : whisker_on_right (whisker_on_left F α) K = whisker_on_left F (whisker_on_right α K) := by tidy end category_theory
c61ad69d2036e13cfe67bfcc367c374f46ee303d
ce6917c5bacabee346655160b74a307b4a5ab620
/src/ch5/ex0102.lean
682bcec6df3e17764b133c669c7274aa9d758458
[]
no_license
Ailrun/Theorem_Proving_in_Lean
ae6a23f3c54d62d401314d6a771e8ff8b4132db2
2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68
refs/heads/master
1,609,838,270,467
1,586,846,743,000
1,586,846,743,000
240,967,761
1
0
null
null
null
null
UTF-8
Lean
false
false
146
lean
theorem test (p q : Prop) (hp : p) (hq : q) : p ∧ q ∧ p := begin apply and.intro, exact hp, apply and.intro, exact hq, exact hp end
3ff834f6641839a7e3455f46720fcb1659047536
4727251e0cd73359b15b664c3170e5d754078599
/src/ring_theory/henselian.lean
453fb3c8464a1a9070024f8d749f47d0614206fe
[ "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
11,760
lean
/- Copyright (c) 2021 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import data.polynomial.taylor import ring_theory.ideal.local_ring import linear_algebra.adic_completion /-! # Henselian rings In this file we set up the basic theory of Henselian (local) rings. A ring `R` is *Henselian* at an ideal `I` if the following conditions hold: * `I` is contained in the Jacobson radical of `R` * for every polynomial `f` over `R`, with a *simple* root `a₀` over the quotient ring `R/I`, there exists a lift `a : R` of `a₀` that is a root of `f`. (Here, saying that a root `b` of a polynomial `g` is *simple* means that `g.derivative.eval b` is a unit. Warning: if `R/I` is not a field then it is not enough to assume that `g` has a factorization into monic linear factors in which `X - b` shows up only once; for example `1` is not a simple root of `X^2-1` over `ℤ/4ℤ`.) A local ring `R` is *Henselian* if it is Henselian at its maximal ideal. In this case the first condition is automatic, and in the second condition we may ask for `f.derivative.eval a ≠ 0`, since the quotient ring `R/I` is a field in this case. ## Main declarations * `henselian_ring`: a typeclass on commutative rings, asserting that the ring is Henselian at the ideal `I`. * `henselian_local_ring`: a typeclass on commutative rings, asserting that the ring is local Henselian. * `field.henselian`: fields are Henselian local rings * `henselian.tfae`: equivalent ways of expressing the Henselian property for local rings * `is_adic_complete.henselian`: a ring `R` with ideal `I` that is `I`-adically complete is Henselian at `I` ## References https://stacks.math.columbia.edu/tag/04GE ## Todo After a good API for etale ring homomorphisms has been developed, we can give more equivalent characterization os Henselian rings. In particular, this can give a proof that factorizations into coprime polynomials can be lifted from the residue field to the Henselian ring. The following gist contains some code sketches in that direction. https://gist.github.com/jcommelin/47d94e4af092641017a97f7f02bf9598 -/ noncomputable theory universes u v open_locale big_operators polynomial open local_ring polynomial function lemma is_local_ring_hom_of_le_jacobson_bot {R : Type*} [comm_ring R] (I : ideal R) (h : I ≤ ideal.jacobson ⊥) : is_local_ring_hom (ideal.quotient.mk I) := begin constructor, intros a h, have : is_unit (ideal.quotient.mk (ideal.jacobson ⊥) a), { rw [is_unit_iff_exists_inv] at *, obtain ⟨b, hb⟩ := h, obtain ⟨b, rfl⟩ := ideal.quotient.mk_surjective b, use ideal.quotient.mk _ b, rw [←(ideal.quotient.mk _).map_one, ←(ideal.quotient.mk _).map_mul, ideal.quotient.eq] at ⊢ hb, exact h hb }, obtain ⟨⟨x, y, h1, h2⟩, rfl : x = _⟩ := this, obtain ⟨y, rfl⟩ := ideal.quotient.mk_surjective y, rw [← (ideal.quotient.mk _).map_mul, ← (ideal.quotient.mk _).map_one, ideal.quotient.eq, ideal.mem_jacobson_bot] at h1 h2, specialize h1 1, simp at h1, exact h1.1, end /-- A ring `R` is *Henselian* at an ideal `I` if the following condition holds: for every polynomial `f` over `R`, with a *simple* root `a₀` over the quotient ring `R/I`, there exists a lift `a : R` of `a₀` that is a root of `f`. (Here, saying that a root `b` of a polynomial `g` is *simple* means that `g.derivative.eval b` is a unit. Warning: if `R/I` is not a field then it is not enough to assume that `g` has a factorization into monic linear factors in which `X - b` shows up only once; for example `1` is not a simple root of `X^2-1` over `ℤ/4ℤ`.) -/ class henselian_ring (R : Type*) [comm_ring R] (I : ideal R) : Prop := (jac : I ≤ ideal.jacobson ⊥) (is_henselian : ∀ (f : R[X]) (hf : f.monic) (a₀ : R) (h₁ : f.eval a₀ ∈ I) (h₂ : is_unit (ideal.quotient.mk I (f.derivative.eval a₀))), ∃ a : R, f.is_root a ∧ (a - a₀ ∈ I)) /-- A local ring `R` is *Henselian* if the following condition holds: for every polynomial `f` over `R`, with a *simple* root `a₀` over the residue field, there exists a lift `a : R` of `a₀` that is a root of `f`. (Recall that a root `b` of a polynomial `g` is *simple* if it is not a double root, so if `g.derivative.eval b ≠ 0`.) In other words, `R` is local Henselian if it is Henselian at the ideal `I`, in the sense of `henselian_ring`. -/ class henselian_local_ring (R : Type*) [comm_ring R] extends local_ring R : Prop := (is_henselian : ∀ (f : R[X]) (hf : f.monic) (a₀ : R) (h₁ : f.eval a₀ ∈ maximal_ideal R) (h₂ : is_unit (f.derivative.eval a₀)), ∃ a : R, f.is_root a ∧ (a - a₀ ∈ maximal_ideal R)) @[priority 100] -- see Note [lower instance priority] instance field.henselian (K : Type*) [field K] : henselian_local_ring K := { is_henselian := λ f hf a₀ h₁ h₂, begin refine ⟨a₀, _, _⟩; rwa [(maximal_ideal K).eq_bot_of_prime, ideal.mem_bot] at *, rw sub_self, end } lemma henselian_local_ring.tfae (R : Type u) [comm_ring R] [local_ring R] : tfae [ henselian_local_ring R, ∀ (f : R[X]) (hf : f.monic) (a₀ : residue_field R) (h₁ : aeval a₀ f = 0) (h₂ : aeval a₀ f.derivative ≠ 0), ∃ a : R, f.is_root a ∧ (residue R a = a₀), ∀ {K : Type u} [field K], by exactI ∀ (φ : R →+* K) (hφ : surjective φ) (f : R[X]) (hf : f.monic) (a₀ : K) (h₁ : f.eval₂ φ a₀ = 0) (h₂ : f.derivative.eval₂ φ a₀ ≠ 0), ∃ a : R, f.is_root a ∧ (φ a = a₀)] := begin tfae_have _3_2 : 3 → 2, { intro H, exact H (residue R) ideal.quotient.mk_surjective, }, tfae_have _2_1 : 2 → 1, { intros H, constructor, intros f hf a₀ h₁ h₂, specialize H f hf (residue R a₀), have aux := flip mem_nonunits_iff.mp h₂, simp only [aeval_def, ring_hom.algebra_map_to_algebra, eval₂_at_apply, ← ideal.quotient.eq_zero_iff_mem, ← local_ring.mem_maximal_ideal] at H h₁ aux, obtain ⟨a, ha₁, ha₂⟩ := H h₁ aux, refine ⟨a, ha₁, _⟩, rw ← ideal.quotient.eq_zero_iff_mem, rwa [← sub_eq_zero, ← ring_hom.map_sub] at ha₂, }, tfae_have _1_3 : 1 → 3, { introsI hR K _K φ hφ f hf a₀ h₁ h₂, obtain ⟨a₀, rfl⟩ := hφ a₀, have H := henselian_local_ring.is_henselian f hf a₀, simp only [← ker_eq_maximal_ideal φ hφ, eval₂_at_apply, φ.mem_ker] at H h₁ h₂, obtain ⟨a, ha₁, ha₂⟩ := H h₁ _, { refine ⟨a, ha₁, _⟩, rwa [φ.map_sub, sub_eq_zero] at ha₂, }, { contrapose! h₂, rwa [← mem_nonunits_iff, ← local_ring.mem_maximal_ideal, ← local_ring.ker_eq_maximal_ideal φ hφ, φ.mem_ker] at h₂, } }, tfae_finish, end instance (R : Type*) [comm_ring R] [hR : henselian_local_ring R] : henselian_ring R (maximal_ideal R) := { jac := by { rw [ideal.jacobson, le_Inf_iff], rintro I ⟨-, hI⟩, exact (eq_maximal_ideal hI).ge }, is_henselian := begin intros f hf a₀ h₁ h₂, refine henselian_local_ring.is_henselian f hf a₀ h₁ _, contrapose! h₂, rw [← mem_nonunits_iff, ← local_ring.mem_maximal_ideal, ← ideal.quotient.eq_zero_iff_mem] at h₂, rw h₂, exact not_is_unit_zero end } /-- A ring `R` that is `I`-adically complete is Henselian at `I`. -/ @[priority 100] -- see Note [lower instance priority] instance is_adic_complete.henselian_ring (R : Type*) [comm_ring R] (I : ideal R) [is_adic_complete I R] : henselian_ring R I := { jac := is_adic_complete.le_jacobson_bot _, is_henselian := begin intros f hf a₀ h₁ h₂, classical, let f' := f.derivative, -- we define a sequence `c n` by starting at `a₀` and then continually -- applying the function sending `b` to `b - f(b)/f'(b)` (Newton's method). -- Note that `f'.eval b` is a unit, because `b` has the same residue as `a₀` modulo `I`. let c : ℕ → R := λ n, nat.rec_on n a₀ (λ _ b, b - f.eval b * ring.inverse (f'.eval b)), have hc : ∀ n, c (n+1) = c n - f.eval (c n) * ring.inverse (f'.eval (c n)), { intro n, dsimp only [c, nat.rec_add_one], refl, }, -- we now spend some time determining properties of the sequence `c : ℕ → R` -- `hc_mod`: for every `n`, we have `c n ≡ a₀ [SMOD I]` -- `hf'c` : for every `n`, `f'.eval (c n)` is a unit -- `hfcI` : for every `n`, `f.eval (c n)` is contained in `I ^ (n+1)` have hc_mod : ∀ n, c n ≡ a₀ [SMOD I], { intro n, induction n with n ih, { refl }, rw [nat.succ_eq_add_one, hc, sub_eq_add_neg, ← add_zero a₀], refine ih.add _, rw [smodeq.zero, ideal.neg_mem_iff], refine I.mul_mem_right _ _, rw [← smodeq.zero] at h₁ ⊢, exact (ih.eval f).trans h₁, }, have hf'c : ∀ n, is_unit (f'.eval (c n)), { intro n, haveI := is_local_ring_hom_of_le_jacobson_bot I (is_adic_complete.le_jacobson_bot I), apply is_unit_of_map_unit (ideal.quotient.mk I), convert h₂ using 1, exact smodeq.def.mp ((hc_mod n).eval _), }, have hfcI : ∀ n, f.eval (c n) ∈ I ^ (n+1), { intro n, induction n with n ih, { simpa only [pow_one] }, simp only [nat.succ_eq_add_one], rw [← taylor_eval_sub (c n), hc], simp only [sub_eq_add_neg, add_neg_cancel_comm], rw [eval_eq_sum, sum_over_range' _ _ _ (lt_add_of_pos_right _ zero_lt_two), ← finset.sum_range_add_sum_Ico _ (nat.le_add_left _ _)], swap, { intro i, rw zero_mul }, refine ideal.add_mem _ _ _, { simp only [finset.sum_range_succ, taylor_coeff_one, mul_one, pow_one, taylor_coeff_zero, mul_neg, finset.sum_singleton, finset.range_one, pow_zero], rw [mul_left_comm, ring.mul_inverse_cancel _ (hf'c n), mul_one, add_neg_self], exact ideal.zero_mem _ }, { refine submodule.sum_mem _ _, simp only [finset.mem_Ico], rintro i ⟨h2i, hi⟩, have aux : n + 2 ≤ i * (n + 1), { transitivity 2 * (n + 1); nlinarith only [h2i], }, refine ideal.mul_mem_left _ _ (ideal.pow_le_pow aux _), rw [pow_mul'], refine ideal.pow_mem_pow ((ideal.neg_mem_iff _).2 $ ideal.mul_mem_right _ _ ih) _, } }, -- we are now in the position to show that `c : ℕ → R` is a Cauchy sequence have aux : ∀ m n, m ≤ n → c m ≡ c n [SMOD (I ^ m • ⊤ : ideal R)], { intros m n hmn, rw [← ideal.one_eq_top, ideal.smul_eq_mul, mul_one], obtain ⟨k, rfl⟩ := nat.exists_eq_add_of_le hmn, clear hmn, induction k with k ih, { rw add_zero, }, rw [nat.succ_eq_add_one, ← add_assoc, hc, ← add_zero (c m), sub_eq_add_neg], refine ih.add _, symmetry, rw [smodeq.zero, ideal.neg_mem_iff], refine ideal.mul_mem_right _ _ (ideal.pow_le_pow _ (hfcI _)), rw [add_assoc], exact le_self_add }, -- hence the sequence converges to some limit point `a`, which is the `a` we are looking for obtain ⟨a, ha⟩ := is_precomplete.prec' c aux, refine ⟨a, _, _⟩, { show f.is_root a, suffices : ∀ n, f.eval a ≡ 0 [SMOD (I ^ n • ⊤ : ideal R)], { from is_Hausdorff.haus' _ this }, intro n, specialize ha n, rw [← ideal.one_eq_top, ideal.smul_eq_mul, mul_one] at ha ⊢, refine (ha.symm.eval f).trans _, rw [smodeq.zero], exact ideal.pow_le_pow le_self_add (hfcI _), }, { show a - a₀ ∈ I, specialize ha 1, rw [hc, pow_one, ← ideal.one_eq_top, ideal.smul_eq_mul, mul_one, sub_eq_add_neg] at ha, rw [← smodeq.sub_mem, ← add_zero a₀], refine ha.symm.trans (smodeq.refl.add _), rw [smodeq.zero, ideal.neg_mem_iff], exact ideal.mul_mem_right _ _ h₁, } end }
71fc62fc08fc22770e0d7d60497b3a88c9a3fa1c
432d948a4d3d242fdfb44b81c9e1b1baacd58617
/src/ring_theory/eisenstein_criterion.lean
cba1ee88562311c5ccb7bf672c8ba08b4b116d5c
[ "Apache-2.0" ]
permissive
JLimperg/aesop3
306cc6570c556568897ed2e508c8869667252e8a
a4a116f650cc7403428e72bd2e2c4cda300fe03f
refs/heads/master
1,682,884,916,368
1,620,320,033,000
1,620,320,033,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,539
lean
/- Copyright (c) 2020 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import ring_theory.prime import ring_theory.polynomial.content /-! # Eisenstein's criterion A proof of a slight generalisation of Eisenstein's criterion for the irreducibility of a polynomial over an integral domain. -/ open polynomial ideal.quotient variables {R : Type*} [integral_domain R] namespace polynomial namespace eisenstein_criterion_aux /- Section for auxiliary lemmas used in the proof of `irreducible_of_eisenstein_criterion`-/ lemma map_eq_C_mul_X_pow_of_forall_coeff_mem {f : polynomial R} {P : ideal R} (hfP : ∀ (n : ℕ), ↑n < f.degree → f.coeff n ∈ P) (hf0 : f ≠ 0) : map (mk P) f = C ((mk P) f.leading_coeff) * X ^ f.nat_degree := polynomial.ext (λ n, begin rcases lt_trichotomy ↑n (degree f) with h | h | h, { erw [coeff_map, eq_zero_iff_mem.2 (hfP n h), coeff_C_mul, coeff_X_pow, if_neg, mul_zero], rintro rfl, exact not_lt_of_ge degree_le_nat_degree h }, { have : nat_degree f = n, from nat_degree_eq_of_degree_eq_some h.symm, rw [coeff_C_mul, coeff_X_pow, if_pos this.symm, mul_one, leading_coeff, this, coeff_map] }, { rw [coeff_eq_zero_of_degree_lt, coeff_eq_zero_of_degree_lt], { refine lt_of_le_of_lt (degree_C_mul_X_pow_le _ _) _, rwa ← degree_eq_nat_degree hf0 }, { exact lt_of_le_of_lt (degree_map_le _) h } } end) lemma le_nat_degree_of_map_eq_mul_X_pow {n : ℕ} {P : ideal R} (hP : P.is_prime) {q : polynomial R} {c : polynomial P.quotient} (hq : map (mk P) q = c * X ^ n) (hc0 : c.degree = 0) : n ≤ q.nat_degree := with_bot.coe_le_coe.1 (calc ↑n = degree (q.map (mk P)) : by rw [hq, degree_mul, hc0, zero_add, degree_pow, degree_X, nsmul_one, nat.cast_with_bot] ... ≤ degree q : degree_map_le _ ... ≤ nat_degree q : degree_le_nat_degree) lemma eval_zero_mem_ideal_of_eq_mul_X_pow {n : ℕ} {P : ideal R} {q : polynomial R} {c : polynomial P.quotient} (hq : map (mk P) q = c * X ^ n) (hn0 : 0 < n) : eval 0 q ∈ P := by rw [← coeff_zero_eq_eval_zero, ← eq_zero_iff_mem, ← coeff_map, coeff_zero_eq_eval_zero, hq, eval_mul, eval_pow, eval_X, zero_pow hn0, mul_zero] lemma is_unit_of_nat_degree_eq_zero_of_forall_dvd_is_unit {p q : polynomial R} (hu : ∀ (x : R), C x ∣ p * q → is_unit x) (hpm : p.nat_degree = 0) : is_unit p := begin rw [eq_C_of_degree_le_zero (nat_degree_eq_zero_iff_degree_le_zero.1 hpm), is_unit_C], refine hu _ _, rw [← eq_C_of_degree_le_zero (nat_degree_eq_zero_iff_degree_le_zero.1 hpm)], exact dvd_mul_right _ _ end end eisenstein_criterion_aux open eisenstein_criterion_aux /-- If `f` is a non constant polynomial with coefficients in `R`, and `P` is a prime ideal in `R`, then if every coefficient in `R` except the leading coefficient is in `P`, and the trailing coefficient is not in `P^2` and no non units in `R` divide `f`, then `f` is irreducible. -/ theorem irreducible_of_eisenstein_criterion {f : polynomial R} {P : ideal R} (hP : P.is_prime) (hfl : f.leading_coeff ∉ P) (hfP : ∀ n : ℕ, ↑n < degree f → f.coeff n ∈ P) (hfd0 : 0 < degree f) (h0 : f.coeff 0 ∉ P^2) (hu : f.is_primitive) : irreducible f := have hf0 : f ≠ 0, from λ _, by simp only [*, not_true, submodule.zero_mem, coeff_zero] at *, have hf : f.map (mk P) = C (mk P (leading_coeff f)) * X ^ nat_degree f, from map_eq_C_mul_X_pow_of_forall_coeff_mem hfP hf0, have hfd0 : 0 < f.nat_degree, from with_bot.coe_lt_coe.1 (lt_of_lt_of_le hfd0 degree_le_nat_degree), ⟨mt degree_eq_zero_of_is_unit (λ h, by simp only [*, lt_irrefl] at *), begin rintros p q rfl, rw [map_mul] at hf, rcases mul_eq_mul_prime_pow (show prime (X : polynomial (ideal.quotient P)), from prime_of_degree_eq_one_of_monic degree_X monic_X) hf with ⟨m, n, b, c, hmnd, hbc, hp, hq⟩, have hmn : 0 < m → 0 < n → false, { assume hm0 hn0, refine h0 _, rw [coeff_zero_eq_eval_zero, eval_mul, sq], exact ideal.mul_mem_mul (eval_zero_mem_ideal_of_eq_mul_X_pow hp hm0) (eval_zero_mem_ideal_of_eq_mul_X_pow hq hn0) }, have hpql0 : (mk P) (p * q).leading_coeff ≠ 0, { rwa [ne.def, eq_zero_iff_mem] }, have hp0 : p ≠ 0, from λ h, by simp only [*, zero_mul, eq_self_iff_true, not_true, ne.def] at *, have hq0 : q ≠ 0, from λ h, by simp only [*, eq_self_iff_true, not_true, ne.def, mul_zero] at *, have hbc0 : degree b = 0 ∧ degree c = 0, { apply_fun degree at hbc, rwa [degree_C hpql0, degree_mul, eq_comm, nat.with_bot.add_eq_zero_iff] at hbc }, have hmp : m ≤ nat_degree p, from le_nat_degree_of_map_eq_mul_X_pow hP hp hbc0.1, have hnq : n ≤ nat_degree q, from le_nat_degree_of_map_eq_mul_X_pow hP hq hbc0.2, have hpmqn : p.nat_degree = m ∧ q.nat_degree = n, { rw [nat_degree_mul hp0 hq0] at hmnd, clear_except hmnd hmp hnq, contrapose hmnd, apply ne_of_lt, rw not_and_distrib at hmnd, cases hmnd, { exact add_lt_add_of_lt_of_le (lt_of_le_of_ne hmp (ne.symm hmnd)) hnq }, { exact add_lt_add_of_le_of_lt hmp (lt_of_le_of_ne hnq (ne.symm hmnd)) } }, obtain rfl | rfl : m = 0 ∨ n = 0, { rwa [pos_iff_ne_zero, pos_iff_ne_zero, imp_false, not_not, ← or_iff_not_imp_left] at hmn }, { exact or.inl (is_unit_of_nat_degree_eq_zero_of_forall_dvd_is_unit hu hpmqn.1) }, { exact or.inr (is_unit_of_nat_degree_eq_zero_of_forall_dvd_is_unit (by simpa only [mul_comm] using hu) hpmqn.2) } end⟩ end polynomial
c8edd14682e631a81cb59bf4985ff9baefbb489a
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/topology/instances/nnreal.lean
d7ad5308071d291c53c02b56d73dc0fb967f48a3
[ "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
4,614
lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin Nonnegative real numbers. -/ import topology.algebra.infinite_sum noncomputable theory open set topological_space metric open_locale topological_space namespace nnreal open_locale nnreal instance : topological_space ℝ≥0 := infer_instance -- short-circuit type class inference instance : topological_semiring ℝ≥0 := { continuous_mul := continuous_subtype_mk _ $ (continuous_subtype_val.comp continuous_fst).mul (continuous_subtype_val.comp continuous_snd), continuous_add := continuous_subtype_mk _ $ (continuous_subtype_val.comp continuous_fst).add (continuous_subtype_val.comp continuous_snd) } instance : second_countable_topology nnreal := topological_space.subtype.second_countable_topology _ _ instance : order_topology ℝ≥0 := ⟨ le_antisymm (le_generate_from $ assume s hs, match s, hs with | _, ⟨⟨a, ha⟩, or.inl rfl⟩ := ⟨{b : ℝ | a < b}, is_open_lt' a, rfl⟩ | _, ⟨⟨a, ha⟩, or.inr rfl⟩ := ⟨{b : ℝ | b < a}, is_open_gt' a, set.ext $ assume b, iff.rfl⟩ end) begin apply coinduced_le_iff_le_induced.1, rw @order_topology.topology_eq_generate_intervals ℝ _, apply le_generate_from, assume s hs, rcases hs with ⟨a, rfl | rfl⟩, { show topological_space.generate_open _ {b : ℝ≥0 | a < b }, by_cases ha : 0 ≤ a, { exact topological_space.generate_open.basic _ ⟨⟨a, ha⟩, or.inl rfl⟩ }, { have : a < 0, from lt_of_not_ge ha, have : {b : ℝ≥0 | a < b } = set.univ, from (set.eq_univ_iff_forall.2 $ assume b, lt_of_lt_of_le this b.2), rw [this], exact topological_space.generate_open.univ } }, { show (topological_space.generate_from _).is_open {b : ℝ≥0 | a > b }, by_cases ha : 0 ≤ a, { exact topological_space.generate_open.basic _ ⟨⟨a, ha⟩, or.inr rfl⟩ }, { have : {b : ℝ≥0 | a > b } = ∅, from (set.eq_empty_iff_forall_not_mem.2 $ assume b hb, ha $ show 0 ≤ a, from le_trans b.2 (le_of_lt hb)), rw [this], apply @is_open_empty } }, end⟩ section coe variable {α : Type*} open filter lemma continuous_of_real : continuous nnreal.of_real := continuous_subtype_mk _ $ continuous_id.max continuous_const lemma continuous_coe : continuous (coe : nnreal → ℝ) := continuous_subtype_val lemma tendsto_coe {f : filter α} {m : α → nnreal} : ∀{x : nnreal}, tendsto (λa, (m a : ℝ)) f (𝓝 (x : ℝ)) ↔ tendsto m f (𝓝 x) | ⟨r, hr⟩ := by rw [nhds_subtype_eq_comap, tendsto_comap_iff]; refl lemma tendsto_of_real {f : filter α} {m : α → ℝ} {x : ℝ} (h : tendsto m f (𝓝 x)) : tendsto (λa, nnreal.of_real (m a)) f (𝓝 (nnreal.of_real x)) := tendsto.comp (continuous_iff_continuous_at.1 continuous_of_real _) h lemma tendsto.sub {f : filter α} {m n : α → nnreal} {r p : nnreal} (hm : tendsto m f (𝓝 r)) (hn : tendsto n f (𝓝 p)) : tendsto (λa, m a - n a) f (𝓝 (r - p)) := tendsto_of_real $ (tendsto_coe.2 hm).sub (tendsto_coe.2 hn) lemma continuous_sub : continuous (λp:nnreal×nnreal, p.1 - p.2) := continuous_subtype_mk _ $ ((continuous.comp continuous_coe continuous_fst).sub (continuous.comp continuous_coe continuous_snd)).max continuous_const lemma continuous.sub [topological_space α] {f g : α → nnreal} (hf : continuous f) (hg : continuous g) : continuous (λ a, f a - g a) := continuous_sub.comp (hf.prod_mk hg) @[norm_cast] lemma has_sum_coe {f : α → nnreal} {r : ℝ≥0} : has_sum (λa, (f a : ℝ)) (r : ℝ) ↔ has_sum f r := by simp only [has_sum, coe_sum.symm, tendsto_coe] @[norm_cast] lemma summable_coe {f : α → ℝ≥0} : summable (λa, (f a : ℝ)) ↔ summable f := begin split, exact assume ⟨a, ha⟩, ⟨⟨a, has_sum_le (λa, (f a).2) has_sum_zero ha⟩, has_sum_coe.1 ha⟩, exact assume ⟨a, ha⟩, ⟨a.1, has_sum_coe.2 ha⟩ end open_locale classical @[norm_cast] lemma coe_tsum {f : α → nnreal} : ↑(∑'a, f a) = (∑'a, (f a : ℝ)) := if hf : summable f then (eq.symm $ (has_sum_coe.2 $ hf.has_sum).tsum_eq) else by simp [tsum, hf, mt summable_coe.1 hf] lemma summable_comp_injective {β : Type*} {f : α → nnreal} (hf : summable f) {i : β → α} (hi : function.injective i) : summable (f ∘ i) := nnreal.summable_coe.1 $ show summable ((coe ∘ f) ∘ i), from (nnreal.summable_coe.2 hf).comp_injective hi end coe end nnreal
11cb47f3f7af25ba321b9c3704df2cd1139a0220
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/algebra/homology/complex_shape.lean
8be04efb55ad38869d445c813d24546326af85d7
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
6,276
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Scott Morrison -/ import algebra.group.defs import data.option.basic import logic.relation /-! # Shapes of homological complexes We define a structure `complex_shape ι` for describing the shapes of homological complexes indexed by a type `ι`. This is intended to capture chain complexes and cochain complexes, indexed by either `ℕ` or `ℤ`, as well as more exotic examples. Rather than insisting that the indexing type has a `succ` function specifying where differentials should go, inside `c : complex_shape` we have `c.rel : ι → ι → Prop`, and when we define `homological_complex` we only allow nonzero differentials `d i j` from `i` to `j` if `c.rel i j`. Further, we require that `{ j // c.rel i j }` and `{ i // c.rel i j }` are subsingletons. This means that the shape consists of some union of lines, rays, intervals, and circles. Convenience functions `c.next` and `c.prev` provide, as an `option`, these related elements when they exist. This design aims to avoid certain problems arising from dependent type theory. In particular we never have to ensure morphisms `d i : X i ⟶ X (succ i)` compose as expected (which would often require rewriting by equations in the indexing type). Instead such identities become separate proof obligations when verifying that a complex we've constructed is of the desired shape. If `α` is an `add_right_cancel_semigroup`, then we define `up α : complex_shape α`, the shape appropriate for cohomology,so `d : X i ⟶ X j` is nonzero only when `j = i + 1`, as well as `down α : complex_shape α`, appropriate for homology, so `d : X i ⟶ X j` is nonzero only when `i = j + 1`. (Later we'll introduce `cochain_complex` and `chain_complex` as abbreviations for `homological_complex` with one of these shapes baked in.) -/ open_locale classical noncomputable theory /-- A `c : complex_shape ι` describes the shape of a chain complex, with chain groups indexed by `ι`. Typically `ι` will be `ℕ`, `ℤ`, or `fin n`. There is a relation `rel : ι → ι → Prop`, and we will only allow a non-zero differential from `i` to `j` when `rel i j`. There are axioms which imply `{ j // c.rel i j }` and `{ i // c.rel i j }` are subsingletons. This means that the shape consists of some union of lines, rays, intervals, and circles. Below we define `c.next` and `c.prev` which provide, as an `option`, these related elements. -/ @[ext, nolint has_inhabited_instance] structure complex_shape (ι : Type*) := (rel : ι → ι → Prop) (next_eq : ∀ {i j j'}, rel i j → rel i j' → j = j') (prev_eq : ∀ {i i' j}, rel i j → rel i' j → i = i') namespace complex_shape variables {ι : Type*} /-- The complex shape where only differentials from each `X.i` to itself are allowed. This is mostly only useful so we can describe the relation of "related in `k` steps" below. -/ @[simps] def refl (ι : Type*) : complex_shape ι := { rel := λ i j, i = j, next_eq := λ i j j' w w', w.symm.trans w', prev_eq := λ i i' j w w', w.trans w'.symm, } /-- The reverse of a `complex_shape`. -/ @[simps] def symm (c : complex_shape ι) : complex_shape ι := { rel := λ i j, c.rel j i, next_eq := λ i j j' w w', c.prev_eq w w', prev_eq := λ i i' j w w', c.next_eq w w', } @[simp] lemma symm_symm (c : complex_shape ι) : c.symm.symm = c := by { ext, simp, } /-- The "composition" of two `complex_shape`s. We need this to define "related in k steps" later. -/ @[simp] def trans (c₁ c₂ : complex_shape ι) : complex_shape ι := { rel := relation.comp c₁.rel c₂.rel, next_eq := λ i j j' w w', begin obtain ⟨k, w₁, w₂⟩ := w, obtain ⟨k', w₁', w₂'⟩ := w', rw c₁.next_eq w₁ w₁' at w₂, exact c₂.next_eq w₂ w₂', end, prev_eq := λ i i' j w w', begin obtain ⟨k, w₁, w₂⟩ := w, obtain ⟨k', w₁', w₂'⟩ := w', rw c₂.prev_eq w₂ w₂' at w₁, exact c₁.prev_eq w₁ w₁', end } instance subsingleton_next (c : complex_shape ι) (i : ι) : subsingleton { j // c.rel i j } := begin fsplit, rintros ⟨j, rij⟩ ⟨k, rik⟩, congr, exact c.next_eq rij rik, end instance subsingleton_prev (c : complex_shape ι) (j : ι) : subsingleton { i // c.rel i j } := begin fsplit, rintros ⟨i, rik⟩ ⟨j, rjk⟩, congr, exact c.prev_eq rik rjk, end /-- An option-valued arbitary choice of index `j` such that `rel i j`, if such exists. -/ def next (c : complex_shape ι) (i : ι) : option { j // c.rel i j } := option.choice _ /-- An option-valued arbitary choice of index `i` such that `rel i j`, if such exists. -/ def prev (c : complex_shape ι) (j : ι) : option { i // c.rel i j } := option.choice _ lemma next_eq_some (c : complex_shape ι) {i j : ι} (h : c.rel i j) : c.next i = some ⟨j, h⟩ := option.choice_eq _ lemma prev_eq_some (c : complex_shape ι) {i j : ι} (h : c.rel i j) : c.prev j = some ⟨i, h⟩ := option.choice_eq _ /-- The `complex_shape` allowing differentials from `X i` to `X (i+a)`. (For example when `a = 1`, a cohomology theory indexed by `ℕ` or `ℤ`) -/ @[simps] def up' {α : Type*} [add_right_cancel_semigroup α] (a : α) : complex_shape α := { rel := λ i j , i + a = j, next_eq := λ i j k hi hj, hi.symm.trans hj, prev_eq := λ i j k hi hj, add_right_cancel (hi.trans hj.symm), } /-- The `complex_shape` allowing differentials from `X (j+a)` to `X j`. (For example when `a = 1`, a homology theory indexed by `ℕ` or `ℤ`) -/ @[simps] def down' {α : Type*} [add_right_cancel_semigroup α] (a : α) : complex_shape α := { rel := λ i j , j + a = i, next_eq := λ i j k hi hj, add_right_cancel (hi.trans (hj.symm)), prev_eq := λ i j k hi hj, hi.symm.trans hj, } /-- The `complex_shape` appropriate for cohomology, so `d : X i ⟶ X j` only when `j = i + 1`. -/ @[simps] def up (α : Type*) [add_right_cancel_semigroup α] [has_one α] : complex_shape α := up' 1 /-- The `complex_shape` appropriate for homology, so `d : X i ⟶ X j` only when `i = j + 1`. -/ @[simps] def down (α : Type*) [add_right_cancel_semigroup α] [has_one α] : complex_shape α := down' 1 end complex_shape
6ffb7967d59e29288c360288b98fab205e33a381
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/lean/ppProofs.lean
35ce06374f1028ea4c48949326774c245edd22c5
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
420
lean
set_option pp.analyze.trustSubst true set_option pp.proofs false example (h : α = β) : h ▸ (a : α) = (b : β) := _ example (h : α = β) : id h ▸ (a : α) = (b : β) := _ example (h : α = β) : id h ▸ (a : α) = (b : β) := by simp set_option pp.proofs.withType false example (h : α = β) : id h ▸ (a : α) = (b : β) := _ set_option pp.proofs true example (h : α = β) : id h ▸ (a : α) = (b : β) := _
920bbd4ba50e3d1b5dca7c32487d5914a2cf26bc
0845ae2ca02071debcfd4ac24be871236c01784f
/tests/playground/persistentarray.lean
be7457fd114cd65bdddab984f5d8cd7e6d8a6ff6
[ "Apache-2.0" ]
permissive
GaloisInc/lean4
74c267eb0e900bfaa23df8de86039483ecbd60b7
228ddd5fdcd98dd4e9c009f425284e86917938aa
refs/heads/master
1,643,131,356,301
1,562,715,572,000
1,562,715,572,000
192,390,898
0
0
null
1,560,792,750,000
1,560,792,749,000
null
UTF-8
Lean
false
false
779
lean
import init.data.persistentarray abbrev MyArray := PersistentArray Nat -- abbrev MyArray := Array Nat def mkMyArray (n : Nat) : MyArray := n.fold (λ i s, s.push i) { PersistentArray . } -- n.fold (λ i s, s.push i) Array.empty def check (n : Nat) (p : Nat → Nat → Bool) (s : MyArray) : IO Unit := n.mfor $ λ i, unless (p i (s.get i)) (throw (IO.userError ("failed at " ++ toString i ++ " " ++ toString (s.get i)))) def inc1 (n : Nat) (s : MyArray) : MyArray := n.fold (λ i s, s.set i (s.get i + 1)) s def checkId (n : Nat) (s : MyArray) : IO Unit := check n (==) s def main (xs : List String) : IO Unit := do let n := xs.head.toNat, let t := mkMyArray n, checkId n t, let t := inc1 n t, check n (λ i v, v == i + 1) t, IO.println t.size, IO.println t.stats, pure ()
affd49068c6c7f105589e8d82de19cba2f0062ff
ab34e36d695606fd149ce177e03e0fc29ad58c72
/main.lean
33d0c3b4d1150059c12d22807e9749a82833484f
[]
no_license
theemathas/loogle
85d44e527094f9ecc7dd5f0ae17ba1f86392fe70
68439fea92b16464b9080d3e44ad319970770efd
refs/heads/master
1,609,480,375,031
1,499,554,592,000
1,499,554,671,000
97,296,823
0
0
null
null
null
null
UTF-8
Lean
false
false
2,231
lean
import .logging universe u /- Checks if applying a tactic will succeed without actually applying it -/ meta def tactic.will_succeed {α : Type u} (t : tactic α) : tactic bool := λ ts, let b := match t ts with | (result.success _ _) := tt | (result.exception _ _ _) := ff end in result.success b ts /- Add arguments to `add_args_type` so it unifies with `fixed_type` Returns `some n` if unifiable after putting in `n` arguments Returns `none` if fails to unify -/ meta def unify_with_args : expr → expr → tactic (option ℕ) := λ fixed_type add_args_type, do can_unify ← tactic.will_succeed $ tactic.unify fixed_type add_args_type, if can_unify then return (some 0) else match add_args_type with | (expr.pi arg_name _ arg_type body) := do arg_mvar ← tactic.mk_meta_var arg_type, let substituted_expr := body.instantiate_var arg_mvar, recur_result ← unify_with_args fixed_type substituted_expr, return $ match recur_result with | some n := some (n + 1) | none := none end | _ := return none end @[hole_command] meta def any_value_that_fits : hole_command := { name := "Anything", descr := "Insert any value that, when inserted, typechecks", action := λ _, do tar ← tactic.target, env ← tactic.get_env, ans ← env.fold (return []) $ λ decl accum, (do accum_result ← accum, unify_with_args_result ← unify_with_args tar decl.type, return $ match unify_with_args_result with | some n := ("(" ++ decl.to_name.to_string ++ string.join (list.repeat " {! !}" n) ++ ")", "") :: accum_result | none := accum_result end ), log $ string.intercalate "\n" $ ans.map to_string, return ans } -- Applying hole command inserts `(unsigned_sz)`, which is a ℕ example := 1 + {! !} inductive MyType : ℕ → ℕ → Type | constr : ∀ x y : ℕ, MyType x y -- Applying hole command should insert `(MyType.constr {! !} {! !})` -- Doesn't work yet example : MyType 1 2 := {! !} -- Doesn't work yet example (x y z : ℕ) : ((x + y) + z = x + (y + z)) := {! !} -- Doesn't work yet example (α : Type u) (x : α) : α := {! !}
37c11fa2549e12300a7e613f127e7cd5d866bed2
e514e8b939af519a1d5e9b30a850769d058df4e9
/src/lib/array.lean
2eedbfd80527a64eaaa71a295c23902ebc082298
[]
no_license
semorrison/lean-rewrite-search
dca317c5a52e170fb6ffc87c5ab767afb5e3e51a
e804b8f2753366b8957be839908230ee73f9e89f
refs/heads/master
1,624,051,754,485
1,614,160,817,000
1,614,160,817,000
162,660,605
0
1
null
null
null
null
UTF-8
Lean
false
false
1,964
lean
universes u v z namespace array -- TODO Prove well-founded induction meta def mmap_copy_aux {k : Type v → Type z} [monad k] {α : Type u} {β : Type v} {n m : ℕ} (f : α → k β) : ℕ → array n α → array m β → k (array m β) | r x y := do if h : r < n ∧ r < m then do let fn : fin n := ⟨r, and.elim_left h⟩, let fm : fin m := ⟨r, and.elim_right h⟩, y ← y.write fm <$> f (x.read fn), mmap_copy_aux (r + 1) x y else return y meta def mmap_copy {k : Type v → Type z} [monad k] {α : Type u} {β : Type v} {n m : ℕ} (x : array n α) (y : array m β) (f : α → k β) : k (array m β) := mmap_copy_aux f 0 x y meta def map_copy_aux {α : Type u} {β : Type v} {n m : ℕ} (f : α → β) : ℕ → array n α → array m β → array m β | r x y := if h : r < n ∧ r < m then let fn : fin n := ⟨r, and.elim_left h⟩ in let fm : fin m := ⟨r, and.elim_right h⟩ in map_copy_aux (r + 1) x $ y.write fm (f $ x.read fn) else y meta def map_copy {α : Type u} {β : Type v} {n m : ℕ} (x : array n α) (y : array m β) (f : α → β) : array m β := map_copy_aux f 0 x y meta def copy {α : Type u} {n m : ℕ} (x : array n α) (y : array m α) : array m α := map_copy x y (λ a, a) def list_map_copy_from {α : Type u} {β : Type v} {n : ℕ} (x : array n β) (fn : α → β) : list α → ℕ → array n β | [] m := x | (a :: rest) m := if h : m < n then let f : fin n := ⟨m, h⟩ in (list_map_copy_from rest (m + 1)).write f (fn a) else x def list_map_copy {α : Type u} {β : Type v} {n : ℕ} (x : array n β) (fn : α → β) (l : list α) : array n β := list_map_copy_from x fn l 0 -- def list_copy {α : Type u} {n : ℕ} (x : array n α) (l : list α) : array n α := -- list_map_copy x (λ a, a) l end array
d5bf994054cf4ccf7c318dbc915fd7a0cf3ee0d9
3dc4623269159d02a444fe898d33e8c7e7e9461b
/.github/workflows/project_1_a_decrire/lean-scheme-submission/src/sheaves/stalk_on_basis.lean
0e5af7a7dacd73987b8691f0a8d1087975d1e816
[]
no_license
Or7ando/lean
cc003e6c41048eae7c34aa6bada51c9e9add9e66
d41169cf4e416a0d42092fb6bdc14131cee9dd15
refs/heads/master
1,650,600,589,722
1,587,262,906,000
1,587,262,906,000
255,387,160
0
0
null
null
null
null
UTF-8
Lean
false
false
2,715
lean
/- Stalk (of types) on basis. https://stacks.math.columbia.edu/tag/009H -/ import to_mathlib.opens import sheaves.presheaf_on_basis open topological_space universe u variables {α : Type u} [topological_space α] variables {B : set (opens α )} {HB : opens.is_basis B} variables (F : presheaf_on_basis α HB) (x : α) structure stalk_on_basis.elem := (U : opens α) (BU : U ∈ B) (Hx : x ∈ U) (s : F BU) -- Equivalence relation on the set of pairs. (U,s) ~ (V,t) iff there exists W -- open s.t. x ∈ W ⊆ U ∩ V, and s|W = t|W. def stalk_on_basis.relation : stalk_on_basis.elem F x → stalk_on_basis.elem F x → Prop := λ Us Vt, ∃ W (BW : W ∈ B) (HxW : x ∈ W) (HWU : W ⊆ Us.U) (HWV : W ⊆ Vt.U), F.res Us.BU BW HWU Us.s = F.res Vt.BU BW HWV Vt.s lemma stalk_on_basis.relation.reflexive : reflexive (stalk_on_basis.relation F x) := λ ⟨U, OU, HxU, s⟩, ⟨U, OU, HxU, set.subset.refl _, set.subset.refl _, rfl⟩ lemma stalk_on_basis.relation.symmetric : symmetric (stalk_on_basis.relation F x) := λ Us Vt ⟨W, OW, HxW, HWU, HWV, Hres⟩, ⟨W, OW, HxW, HWV, HWU, Hres.symm⟩ lemma stalk_on_basis.relation.transitive : transitive (stalk_on_basis.relation F x) := λ ⟨U, BU, HxU, sU⟩ ⟨V, BV, HxV, sV⟩ ⟨W, BW, HxW, sW⟩, λ ⟨R, BR, HxR, HRU, HRV, HresR⟩ ⟨S, BS, HxS, HSV, HSW, HresS⟩, have HxRS : x ∈ R ∩ S := ⟨HxR, HxS⟩, let ⟨T, BT, HxT, HTRS⟩ := opens.is_basis_iff_nbhd.1 HB HxRS in ⟨T, BT, HxT, λ y Hy, HRU (HTRS Hy).1, λ y Hy, HSW (HTRS Hy).2, have HTR : T ⊆ R := λ y Hy, (HTRS Hy).1, have HTS : T ⊆ S := λ y Hy, (HTRS Hy).2, have HURRS : _ := F.Hcomp BU BR BT HTR HRU, have HVRRS : _ := F.Hcomp BV BR BT HTR HRV, have HVSRS : _ := F.Hcomp BV BS BT HTS HSV, have HWSRS : _ := F.Hcomp BW BS BT HTS HSW, calc F.res BU BT _ sU = F.res BR BT _ (F.res BU BR _ sU) : congr_fun HURRS sU ... = F.res BR BT _ (F.res BV BR _ sV) : congr_arg _ HresR ... = F.res BV BT _ sV : congr_fun HVRRS.symm sV ... = F.res BS BT _ (F.res BV BS _ sV) : congr_fun HVSRS sV ... = F.res BS BT _ (F.res BW BS _ sW) : congr_arg _ HresS ... = F.res BW BT _ sW : congr_fun HWSRS.symm sW⟩ lemma stalk_on_basis.relation.equivalence : equivalence (stalk_on_basis.relation F x) := ⟨stalk_on_basis.relation.reflexive F x, stalk_on_basis.relation.symmetric F x, stalk_on_basis.relation.transitive F x⟩ instance stalk_on_basis.setoid : setoid (stalk_on_basis.elem F x) := { r := stalk_on_basis.relation F x, iseqv := stalk_on_basis.relation.equivalence F x } -- We define a stalk as the set of stalk elements under the defined relation. definition stalk_on_basis := quotient (stalk_on_basis.setoid F x)
85ff1bd84a409d01af67db9ed3b5bbbf7c27bcd3
a81826cfd4fd71c797ea79b8e827b03ccb332c17
/src/wonky_sq/differentiation.lean
55304cd436730d6fce46db2d0821cd82bc44607f
[ "Apache-2.0" ]
permissive
jamesa9283/Generalised-Trigonometric-Functions-for-Lean
0a0f4774363c3708be466526935c6d07f4b970f0
33775fb8286eacfc17397fe41af9d446cbdd78f3
refs/heads/master
1,669,337,232,958
1,597,061,947,000
1,597,061,947,000
276,104,804
0
0
null
1,593,540,795,000
1,593,523,262,000
Lean
UTF-8
Lean
false
false
563
lean
import analysis.special_functions.trigonometric import wonky_sq.basic --open real /-! Right, so this seems really confusing. So we are going to do a couple of things. * Prove that sinₘ x is continuous * Prove that sinₘ x is differentiable * Prove that sinₘ x has a differential and its on the paper * Do that for the rest of the trigonometric funcs -/ /- Firstly continuity! Basically its continuous ∀ c, limit f(x) as x → c = f(c) However, we need limits!! -/ -- Slight problem, I have no idea how to do any of these things... --end real
0cdb91ab98756be34da6d1c446fcf21c26e8d352
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/algebra/category/Module/kernels.lean
83592e487de2cf2b318ee4818ab1300e9d923be0
[ "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
2,436
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 algebra.category.Module.basic /-! # The concrete (co)kernels in the category of modules are (co)kernels in the categorical sense. -/ open category_theory open category_theory.limits open category_theory.limits.walking_parallel_pair universes u v namespace Module variables {R : Type u} [ring R] section variables {M N : Module.{v} R} (f : M ⟶ N) /-- The kernel cone induced by the concrete kernel. -/ def kernel_cone : kernel_fork f := kernel_fork.of_ι (as_hom f.ker.subtype) $ by tidy /-- The kernel of a linear map is a kernel in the categorical sense. -/ def kernel_is_limit : is_limit (kernel_cone f) := fork.is_limit.mk _ (λ s, linear_map.cod_restrict f.ker (fork.ι s) (λ c, linear_map.mem_ker.2 $ by { rw [←@function.comp_apply _ _ _ f (fork.ι s) c, ←coe_comp, fork.condition, has_zero_morphisms.comp_zero (fork.ι s) N], refl })) (λ s, linear_map.subtype_comp_cod_restrict _ _ _) (λ s m h, linear_map.ext $ λ x, subtype.ext_iff_val.2 $ have h₁ : (m ≫ (kernel_cone f).π.app zero).to_fun = (s.π.app zero).to_fun, by { congr, exact h zero }, by convert @congr_fun _ _ _ _ h₁ x ) /-- The cokernel cocone induced by the projection onto the quotient. -/ def cokernel_cocone : cokernel_cofork f := cokernel_cofork.of_π (as_hom f.range.mkq) $ linear_map.range_mkq_comp _ /-- The projection onto the quotient is a cokernel in the categorical sense. -/ def cokernel_is_colimit : is_colimit (cokernel_cocone f) := cofork.is_colimit.mk _ (λ s, f.range.liftq (cofork.π s) $ linear_map.range_le_ker_iff.2 $ cokernel_cofork.condition s) (λ s, f.range.liftq_mkq (cofork.π s) _) (λ s m h, begin haveI : epi (as_hom f.range.mkq) := epi_of_range_eq_top _ (submodule.range_mkq _), apply (cancel_epi (as_hom f.range.mkq)).1, convert h walking_parallel_pair.one, exact submodule.liftq_mkq _ _ _ end) end /-- The category of R-modules has kernels, given by the inclusion of the kernel submodule. -/ def has_kernels_Module : has_kernels (Module R) := ⟨λ X Y f, ⟨_, kernel_is_limit f⟩⟩ /-- The category or R-modules has cokernels, given by the projection onto the quotient. -/ def has_cokernels_Module : has_cokernels (Module R) := ⟨λ X Y f, ⟨_, cokernel_is_colimit f⟩⟩ end Module
9860e99c45bc2dfef043eeca86215ec330600aa4
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/tactic/subtype_instance.lean
7f027343a34e95d9505571b0201cf407fa1770fa
[ "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
2,372
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon Provides a `subtype_instance` tactic which builds instances for algebraic substructures (sub-groups, sub-rings...). -/ import data.string.basic import tactic.interactive tactic.algebra import data.subtype data.set.basic open tactic expr name list namespace tactic open tactic.interactive (get_current_field refine_struct) open lean lean.parser open interactive /-- makes the substructure axiom name from field name, by postfacing with `_mem`-/ def mk_mem_name (sub : name) : name → name | (mk_string n _) := mk_string (n ++ "_mem") sub | n := n meta def derive_field_subtype : tactic unit := do field ← get_current_field, b ← target >>= is_prop, if b then do `[simp [subtype.ext], dsimp [set.set_coe_eq_subtype]], intros, applyc field; assumption else do s ← find_local ``(set _), `(set %%α) ← infer_type s, e ← mk_const field, expl_arity ← get_expl_arity $ e α, xs ← (iota expl_arity).mmap $ λ _, intro1, args ← xs.mmap $ λ x, mk_app `subtype.val [x], hyps ← xs.mmap $ λ x, mk_app `subtype.property [x], val ← mk_app field args, subname ← local_context >>= list.mfirst (λ h, do (expr.const n _, args) ← get_app_fn_args <$> infer_type h, is_def_eq s args.ilast reducible, return n), mem_field ← resolve_constant $ mk_mem_name subname field, val_mem ← mk_app mem_field hyps, `(coe_sort %%s) <- target >>= instantiate_mvars, tactic.refine ``(@subtype.mk _ %%s %%val %%val_mem) namespace interactive /-- builds instances for algebraic substructures Example: ```lean variables {α : Type*} [monoid α] {s : set α} class is_submonoid (s : set α) : Prop := (one_mem : (1:α) ∈ s) (mul_mem {a b} : a ∈ s → b ∈ s → a * b ∈ s) instance subtype.monoid {s : set α} [is_submonoid s] : monoid s := by subtype_instance ```` -/ meta def subtype_instance := do t ← target, let cl := t.get_app_fn.const_name, src ← find_ancestors cl t.app_arg, let inst := pexpr.mk_structure_instance { struct := cl, field_values := [], field_names := [], sources := src.map to_pexpr }, refine_struct inst ; derive_field_subtype end interactive end tactic
c6bd23c93f166f7bb86c9bf0e05c3f2627252313
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/linear_algebra/affine_space/ordered.lean
ba9ede16290db11377a6f01e0ee3491d470bdd75
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
15,794
lean
/- Copyright (c) 2020 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import linear_algebra.affine_space.midpoint import algebra.module.ordered import tactic.field_simp /-! # Ordered modules as affine spaces In this file we define the slope of a function `f : k → PE` taking values in an affine space over `k` and prove some theorems about `slope` and `line_map` in the case when `PE` is an ordered module over `k`. The `slope` function naturally appears in the Mean Value Theorem, and in the proof of the fact that a function with nonnegative second derivative on an interval is convex on this interval. In the third part of this file we prove inequalities that will be used in `analysis.convex.basic` to link convexity of a function on an interval to monotonicity of the slope, see section docstring below for details. ## Implementation notes We do not introduce the notion of ordered affine spaces (yet?). Instead, we prove various theorems for an ordered module interpreted as an affine space. ## Tags affine space, ordered module, slope -/ open affine_map variables {k E PE : Type*} /-! ### Definition of `slope` and basic properties In this section we define `slope f a b` and prove some properties that do not require order on the codomain. -/ section no_order variables [field k] [add_comm_group E] [module k E] [add_torsor E PE] include E /-- `slope f a b = (b - a)⁻¹ • (f b -ᵥ f a)` is the slope of a function `f` on the interval `[a, b]`. Note that `slope f a a = 0`, not the derivative of `f` at `a`. -/ def slope (f : k → PE) (a b : k) : E := (b - a)⁻¹ • (f b -ᵥ f a) omit E lemma slope_def_field (f : k → k) (a b : k) : slope f a b = (f b - f a) / (b - a) := div_eq_inv_mul.symm @[simp] lemma slope_same (f : k → PE) (a : k) : (slope f a a : E) = 0 := by rw [slope, sub_self, inv_zero, zero_smul] include E lemma eq_of_slope_eq_zero {f : k → PE} {a b : k} (h : slope f a b = (0:E)) : f a = f b := begin rw [slope, smul_eq_zero, inv_eq_zero, sub_eq_zero, vsub_eq_zero_iff_eq] at h, exact h.elim (λ h, h ▸ rfl) eq.symm end lemma slope_comm (f : k → PE) (a b : k) : slope f a b = slope f b a := by rw [slope, slope, ← neg_vsub_eq_vsub_rev, smul_neg, ← neg_smul, neg_inv, neg_sub] /-- `slope f a c` is a linear combination of `slope f a b` and `slope f b c`. This version explicitly provides coefficients. If `a ≠ c`, then the sum of the coefficients is `1`, so it is actually an affine combination, see `line_map_slope_slope_sub_div_sub`. -/ lemma sub_div_sub_smul_slope_add_sub_div_sub_smul_slope (f : k → PE) (a b c : k) : ((b - a) / (c - a)) • slope f a b + ((c - b) / (c - a)) • slope f b c = slope f a c := begin by_cases hab : a = b, { subst hab, rw [sub_self, zero_div, zero_smul, zero_add], by_cases hac : a = c, { simp [hac] }, { rw [div_self (sub_ne_zero.2 $ ne.symm hac), one_smul] } }, by_cases hbc : b = c, { subst hbc, simp [sub_ne_zero.2 (ne.symm hab)] }, rw [add_comm], simp_rw [slope, div_eq_inv_mul, mul_smul, ← smul_add, smul_inv_smul' (sub_ne_zero.2 $ ne.symm hab), smul_inv_smul' (sub_ne_zero.2 $ ne.symm hbc), vsub_add_vsub_cancel], end /-- `slope f a c` is an affine combination of `slope f a b` and `slope f b c`. This version uses `line_map` to express this property. -/ lemma line_map_slope_slope_sub_div_sub (f : k → PE) (a b c : k) (h : a ≠ c) : line_map (slope f a b) (slope f b c) ((c - b) / (c - a)) = slope f a c := by field_simp [sub_ne_zero.2 h.symm, ← sub_div_sub_smul_slope_add_sub_div_sub_smul_slope f a b c, line_map_apply_module] /-- `slope f a b` is an affine combination of `slope f a (line_map a b r)` and `slope f (line_map a b r) b`. We use `line_map` to express this property. -/ lemma line_map_slope_line_map_slope_line_map (f : k → PE) (a b r : k) : line_map (slope f (line_map a b r) b) (slope f a (line_map a b r)) r = slope f a b := begin obtain (rfl|hab) : a = b ∨ a ≠ b := classical.em _, { simp }, rw [slope_comm _ a, slope_comm _ a, slope_comm _ _ b], convert line_map_slope_slope_sub_div_sub f b (line_map a b r) a hab.symm using 2, rw [line_map_apply_ring, eq_div_iff (sub_ne_zero.2 hab), sub_mul, one_mul, mul_sub, ← sub_sub, sub_sub_cancel] end end no_order /-! ### Monotonicity of `line_map` In this section we prove that `line_map a b r` is monotone (strictly or not) in its arguments if other arguments belong to specific domains. -/ section ordered_ring variables [ordered_ring k] [ordered_add_comm_group E] [module k E] [ordered_module k E] variables {a a' b b' : E} {r r' : k} lemma line_map_mono_left (ha : a ≤ a') (hr : r ≤ 1) : line_map a b r ≤ line_map a' b r := begin simp only [line_map_apply_module], exact add_le_add_right (smul_le_smul_of_nonneg ha (sub_nonneg.2 hr)) _ end lemma line_map_strict_mono_left (ha : a < a') (hr : r < 1) : line_map a b r < line_map a' b r := begin simp only [line_map_apply_module], exact add_lt_add_right (smul_lt_smul_of_pos ha (sub_pos.2 hr)) _ end lemma line_map_mono_right (hb : b ≤ b') (hr : 0 ≤ r) : line_map a b r ≤ line_map a b' r := begin simp only [line_map_apply_module], exact add_le_add_left (smul_le_smul_of_nonneg hb hr) _ end lemma line_map_strict_mono_right (hb : b < b') (hr : 0 < r) : line_map a b r < line_map a b' r := begin simp only [line_map_apply_module], exact add_lt_add_left (smul_lt_smul_of_pos hb hr) _ end lemma line_map_mono_endpoints (ha : a ≤ a') (hb : b ≤ b') (h₀ : 0 ≤ r) (h₁ : r ≤ 1) : line_map a b r ≤ line_map a' b' r := (line_map_mono_left ha h₁).trans (line_map_mono_right hb h₀) lemma line_map_strict_mono_endpoints (ha : a < a') (hb : b < b') (h₀ : 0 ≤ r) (h₁ : r ≤ 1) : line_map a b r < line_map a' b' r := begin rcases h₀.eq_or_lt with (rfl|h₀), { simpa }, exact (line_map_mono_left ha.le h₁).trans_lt (line_map_strict_mono_right hb h₀) end lemma line_map_lt_line_map_iff_of_lt (h : r < r') : line_map a b r < line_map a b r' ↔ a < b := begin simp only [line_map_apply_module], rw [← lt_sub_iff_add_lt, add_sub_assoc, ← sub_lt_iff_lt_add', ← sub_smul, ← sub_smul, sub_sub_sub_cancel_left, smul_lt_smul_iff_of_pos (sub_pos.2 h)], apply_instance, end lemma left_lt_line_map_iff_lt (h : 0 < r) : a < line_map a b r ↔ a < b := iff.trans (by rw line_map_apply_zero) (line_map_lt_line_map_iff_of_lt h) lemma line_map_lt_left_iff_lt (h : 0 < r) : line_map a b r < a ↔ b < a := @left_lt_line_map_iff_lt k (order_dual E) _ _ _ _ _ _ _ h lemma line_map_lt_right_iff_lt (h : r < 1) : line_map a b r < b ↔ a < b := iff.trans (by rw line_map_apply_one) (line_map_lt_line_map_iff_of_lt h) lemma right_lt_line_map_iff_lt (h : r < 1) : b < line_map a b r ↔ b < a := @line_map_lt_right_iff_lt k (order_dual E) _ _ _ _ _ _ _ h end ordered_ring section linear_ordered_ring variables [linear_ordered_ring k] [ordered_add_comm_group E] [module k E] [ordered_module k E] [invertible (2:k)] {a a' b b' : E} {r r' : k} lemma midpoint_le_midpoint (ha : a ≤ a') (hb : b ≤ b') : midpoint k a b ≤ midpoint k a' b' := line_map_mono_endpoints ha hb (inv_of_nonneg.2 zero_le_two) $ inv_of_le_one one_le_two end linear_ordered_ring section linear_ordered_field variables [linear_ordered_field k] [ordered_add_comm_group E] variables [module k E] [ordered_module k E] section variables {a b : E} {r r' : k} lemma line_map_le_line_map_iff_of_lt (h : r < r') : line_map a b r ≤ line_map a b r' ↔ a ≤ b := begin simp only [line_map_apply_module], rw [← le_sub_iff_add_le, add_sub_assoc, ← sub_le_iff_le_add', ← sub_smul, ← sub_smul, sub_sub_sub_cancel_left, smul_le_smul_iff_of_pos (sub_pos.2 h)], apply_instance, end lemma left_le_line_map_iff_le (h : 0 < r) : a ≤ line_map a b r ↔ a ≤ b := iff.trans (by rw line_map_apply_zero) (line_map_le_line_map_iff_of_lt h) @[simp] lemma left_le_midpoint : a ≤ midpoint k a b ↔ a ≤ b := left_le_line_map_iff_le $ inv_pos.2 zero_lt_two lemma line_map_le_left_iff_le (h : 0 < r) : line_map a b r ≤ a ↔ b ≤ a := @left_le_line_map_iff_le k (order_dual E) _ _ _ _ _ _ _ h @[simp] lemma midpoint_le_left : midpoint k a b ≤ a ↔ b ≤ a := line_map_le_left_iff_le $ inv_pos.2 zero_lt_two lemma line_map_le_right_iff_le (h : r < 1) : line_map a b r ≤ b ↔ a ≤ b := iff.trans (by rw line_map_apply_one) (line_map_le_line_map_iff_of_lt h) @[simp] lemma midpoint_le_right : midpoint k a b ≤ b ↔ a ≤ b := line_map_le_right_iff_le $ inv_lt_one one_lt_two lemma right_le_line_map_iff_le (h : r < 1) : b ≤ line_map a b r ↔ b ≤ a := @line_map_le_right_iff_le k (order_dual E) _ _ _ _ _ _ _ h @[simp] lemma right_le_midpoint : b ≤ midpoint k a b ↔ b ≤ a := right_le_line_map_iff_le $ inv_lt_one one_lt_two end /-! ### Convexity and slope Given an interval `[a, b]` and a point `c ∈ (a, b)`, `c = line_map a b r`, there are a few ways to say that the point `(c, f c)` is above/below the segment `[(a, f a), (b, f b)]`: * compare `f c` to `line_map (f a) (f b) r`; * compare `slope f a c` to `slope `f a b`; * compare `slope f c b` to `slope f a b`; * compare `slope f a c` to `slope f c b`. In this section we prove equivalence of these four approaches. In order to make the statements more readable, we introduce local notation `c = line_map a b r`. Then we prove lemmas like ``` lemma map_le_line_map_iff_slope_le_slope_left (h : 0 < r * (b - a)) : f c ≤ line_map (f a) (f b) r ↔ slope f a c ≤ slope f a b := ``` For each inequality between `f c` and `line_map (f a) (f b) r` we provide 3 lemmas: * `*_left` relates it to an inequality on `slope f a c` and `slope f a b`; * `*_right` relates it to an inequality on `slope f a b` and `slope f c b`; * no-suffix version relates it to an inequality on `slope f a c` and `slope f c b`. Later these inequalities will be used in to restate `convex_on` in terms of monotonicity of the slope. -/ variables {f : k → E} {a b r : k} local notation `c` := line_map a b r /-- Given `c = line_map a b r`, `a < c`, the point `(c, f c)` is non-strictly below the segment `[(a, f a), (b, f b)]` if and only if `slope f a c ≤ slope f a b`. -/ lemma map_le_line_map_iff_slope_le_slope_left (h : 0 < r * (b - a)) : f c ≤ line_map (f a) (f b) r ↔ slope f a c ≤ slope f a b := by simp_rw [line_map_apply, slope, vsub_eq_sub, vadd_eq_add, smul_eq_mul, add_sub_cancel, smul_sub, sub_le_iff_le_add, mul_inv_rev', mul_smul, ← smul_sub, ← smul_add, smul_smul, ← mul_inv_rev', smul_le_iff_of_pos (inv_pos.2 h), inv_inv', smul_smul, mul_inv_cancel_right' (right_ne_zero_of_mul h.ne'), smul_add, smul_inv_smul' (left_ne_zero_of_mul h.ne')] /-- Given `c = line_map a b r`, `a < c`, the point `(c, f c)` is non-strictly above the segment `[(a, f a), (b, f b)]` if and only if `slope f a b ≤ slope f a c`. -/ lemma line_map_le_map_iff_slope_le_slope_left (h : 0 < r * (b - a)) : line_map (f a) (f b) r ≤ f c ↔ slope f a b ≤ slope f a c := @map_le_line_map_iff_slope_le_slope_left k (order_dual E) _ _ _ _ f a b r h /-- Given `c = line_map a b r`, `a < c`, the point `(c, f c)` is strictly below the segment `[(a, f a), (b, f b)]` if and only if `slope f a c < slope f a b`. -/ lemma map_lt_line_map_iff_slope_lt_slope_left (h : 0 < r * (b - a)) : f c < line_map (f a) (f b) r ↔ slope f a c < slope f a b := lt_iff_lt_of_le_iff_le' (line_map_le_map_iff_slope_le_slope_left h) (map_le_line_map_iff_slope_le_slope_left h) /-- Given `c = line_map a b r`, `a < c`, the point `(c, f c)` is strictly above the segment `[(a, f a), (b, f b)]` if and only if `slope f a b < slope f a c`. -/ lemma line_map_lt_map_iff_slope_lt_slope_left (h : 0 < r * (b - a)) : line_map (f a) (f b) r < f c ↔ slope f a b < slope f a c := @map_lt_line_map_iff_slope_lt_slope_left k (order_dual E) _ _ _ _ f a b r h /-- Given `c = line_map a b r`, `c < b`, the point `(c, f c)` is non-strictly below the segment `[(a, f a), (b, f b)]` if and only if `slope f a b ≤ slope f c b`. -/ lemma map_le_line_map_iff_slope_le_slope_right (h : 0 < (1 - r) * (b - a)) : f c ≤ line_map (f a) (f b) r ↔ slope f a b ≤ slope f c b := begin rw [← line_map_apply_one_sub, ← line_map_apply_one_sub _ _ r], revert h, generalize : 1 - r = r', clear r, intro h, simp_rw [line_map_apply, slope, vsub_eq_sub, vadd_eq_add, smul_eq_mul], rw [sub_add_eq_sub_sub_swap, sub_self, zero_sub, le_smul_iff_of_pos, inv_inv', smul_smul, neg_mul_eq_mul_neg, neg_sub, mul_inv_cancel_right', le_sub, ← neg_sub (f b), smul_neg, neg_add_eq_sub], { exact right_ne_zero_of_mul h.ne' }, { simpa [mul_sub] using h } end /-- Given `c = line_map a b r`, `c < b`, the point `(c, f c)` is non-strictly above the segment `[(a, f a), (b, f b)]` if and only if `slope f c b ≤ slope f a b`. -/ lemma line_map_le_map_iff_slope_le_slope_right (h : 0 < (1 - r) * (b - a)) : line_map (f a) (f b) r ≤ f c ↔ slope f c b ≤ slope f a b := @map_le_line_map_iff_slope_le_slope_right k (order_dual E) _ _ _ _ f a b r h /-- Given `c = line_map a b r`, `c < b`, the point `(c, f c)` is strictly below the segment `[(a, f a), (b, f b)]` if and only if `slope f a b < slope f c b`. -/ lemma map_lt_line_map_iff_slope_lt_slope_right (h : 0 < (1 - r) * (b - a)) : f c < line_map (f a) (f b) r ↔ slope f a b < slope f c b := lt_iff_lt_of_le_iff_le' (line_map_le_map_iff_slope_le_slope_right h) (map_le_line_map_iff_slope_le_slope_right h) /-- Given `c = line_map a b r`, `c < b`, the point `(c, f c)` is strictly above the segment `[(a, f a), (b, f b)]` if and only if `slope f c b < slope f a b`. -/ lemma line_map_lt_map_iff_slope_lt_slope_right (h : 0 < (1 - r) * (b - a)) : line_map (f a) (f b) r < f c ↔ slope f c b < slope f a b := @map_lt_line_map_iff_slope_lt_slope_right k (order_dual E) _ _ _ _ f a b r h /-- Given `c = line_map a b r`, `a < c < b`, the point `(c, f c)` is non-strictly below the segment `[(a, f a), (b, f b)]` if and only if `slope f a c ≤ slope f c b`. -/ lemma map_le_line_map_iff_slope_le_slope (hab : a < b) (h₀ : 0 < r) (h₁ : r < 1) : f c ≤ line_map (f a) (f b) r ↔ slope f a c ≤ slope f c b := begin rw [map_le_line_map_iff_slope_le_slope_left (mul_pos h₀ (sub_pos.2 hab)), ← line_map_slope_line_map_slope_line_map f a b r, right_le_line_map_iff_le h₁], apply_instance, apply_instance, end /-- Given `c = line_map a b r`, `a < c < b`, the point `(c, f c)` is non-strictly above the segment `[(a, f a), (b, f b)]` if and only if `slope f c b ≤ slope f a c`. -/ lemma line_map_le_map_iff_slope_le_slope (hab : a < b) (h₀ : 0 < r) (h₁ : r < 1) : line_map (f a) (f b) r ≤ f c ↔ slope f c b ≤ slope f a c := @map_le_line_map_iff_slope_le_slope k (order_dual E) _ _ _ _ _ _ _ _ hab h₀ h₁ /-- Given `c = line_map a b r`, `a < c < b`, the point `(c, f c)` is strictly below the segment `[(a, f a), (b, f b)]` if and only if `slope f a c < slope f c b`. -/ lemma map_lt_line_map_iff_slope_lt_slope (hab : a < b) (h₀ : 0 < r) (h₁ : r < 1) : f c < line_map (f a) (f b) r ↔ slope f a c < slope f c b := lt_iff_lt_of_le_iff_le' (line_map_le_map_iff_slope_le_slope hab h₀ h₁) (map_le_line_map_iff_slope_le_slope hab h₀ h₁) /-- Given `c = line_map a b r`, `a < c < b`, the point `(c, f c)` is strictly above the segment `[(a, f a), (b, f b)]` if and only if `slope f c b < slope f a c`. -/ lemma line_map_lt_map_iff_slope_lt_slope (hab : a < b) (h₀ : 0 < r) (h₁ : r < 1) : line_map (f a) (f b) r < f c ↔ slope f c b < slope f a c := @map_lt_line_map_iff_slope_lt_slope k (order_dual E) _ _ _ _ _ _ _ _ hab h₀ h₁ end linear_ordered_field
3d46e85c4ee370fa2753a75689ba2bdba3a3e7a0
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/number_theory/multiplicity.lean
52564d9bc8ed8ec159ddfdd65b5d9472748fa3d6
[ "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,444
lean
/- Copyright (c) 2022 Tian Chen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Tian Chen, Mantas Bakšys -/ import algebra.geom_sum import data.int.parity import data.zmod.basic import number_theory.padics.padic_val /-! # Multiplicity in Number Theory This file contains results in number theory relating to multiplicity. ## Main statements * `multiplicity.int.pow_sub_pow` is the lifting the exponent lemma for odd primes. We also prove several variations of the lemma. ## References * [Wikipedia, *Lifting-the-exponent lemma*] (https://en.wikipedia.org/wiki/Lifting-the-exponent_lemma) -/ open ideal ideal.quotient finset open_locale big_operators variables {R : Type*} {n : ℕ} section comm_ring variables [comm_ring R] {a b x y : R} lemma dvd_geom_sum₂_iff_of_dvd_sub {x y p : R} (h : p ∣ x - y) : p ∣ ∑ i in range n, x ^ i * y ^ (n - 1 - i) ↔ p ∣ n * y ^ (n - 1) := begin rw [← mem_span_singleton, ← ideal.quotient.eq] at h, simp only [← mem_span_singleton, ← eq_zero_iff_mem, ring_hom.map_geom_sum₂, h, geom_sum₂_self, _root_.map_mul, map_pow, map_nat_cast] end lemma dvd_geom_sum₂_iff_of_dvd_sub' {x y p : R} (h : p ∣ x - y) : p ∣ ∑ i in range n, x ^ i * y ^ (n - 1 - i) ↔ p ∣ n * x ^ (n - 1) := by rw [geom_sum₂_comm, dvd_geom_sum₂_iff_of_dvd_sub]; simpa using (dvd_neg _ _).mpr h lemma dvd_geom_sum₂_self {x y : R} (h : ↑n ∣ x - y) : ↑n ∣ ∑ i in range n, x ^ i * y ^ (n - 1 - i):= (dvd_geom_sum₂_iff_of_dvd_sub h).mpr (dvd_mul_right _ _) lemma sq_dvd_add_pow_sub_sub (p x : R) (n : ℕ) : p ^ 2 ∣ (x + p) ^ n - x ^ (n - 1) * p * n - x ^ n := begin cases n, { simp only [pow_zero, nat.cast_zero, mul_zero, sub_zero, sub_self, dvd_zero]}, { simp only [nat.succ_sub_succ_eq_sub, tsub_zero, nat.cast_succ, add_pow, finset.sum_range_succ, nat.choose_self, nat.succ_sub _, tsub_self, pow_one, nat.choose_succ_self_right, pow_zero, mul_one, nat.cast_zero, zero_add, nat.succ_eq_add_one], suffices : p ^ 2 ∣ ∑ (i : ℕ) in range n, x ^ i * p ^ (n + 1 - i) * ↑((n + 1).choose i), { convert this; abel }, { apply finset.dvd_sum, intros y hy, calc p ^ 2 ∣ p ^ (n + 1 - y) : pow_dvd_pow p (le_tsub_of_add_le_left (by linarith [finset.mem_range.mp hy])) ... ∣ x ^ y * p ^ (n + 1 - y) * ↑((n + 1).choose y) : dvd_mul_of_dvd_left (dvd_mul_left _ _) ((n + 1).choose y) }} end lemma not_dvd_geom_sum₂ {p : R} (hp : prime p) (hxy : p ∣ x - y) (hx : ¬p ∣ x) (hn : ¬p ∣ n) : ¬p ∣ ∑ i in range n, x ^ i * y ^ (n - 1 - i) := λ h, hx $ hp.dvd_of_dvd_pow $ (hp.dvd_or_dvd $ (dvd_geom_sum₂_iff_of_dvd_sub' hxy).mp h).resolve_left hn variables {p : ℕ} (a b) lemma odd_sq_dvd_geom_sum₂_sub (hp : odd p) : ↑p ^ 2 ∣ ∑ i in range p, (a + p * b) ^ i * a ^ (p - 1 - i) - p * a ^ (p - 1) := begin have h1 : ∀ i, ↑p ^ 2 ∣ (a + ↑p * b) ^ i - (a ^ (i - 1) * (↑p * b) * ↑i + a ^ i), { intro i, calc ↑p ^ 2 ∣ (↑p * b) ^ 2 : by simp only [mul_pow, dvd_mul_right] ... ∣ (a + ↑p * b) ^ i - (a ^ (i - 1) * (↑p * b) * ↑i + a ^ i) : by simp only [sq_dvd_add_pow_sub_sub (↑p * b) a i, ← sub_sub] }, simp_rw [← mem_span_singleton, ← ideal.quotient.eq] at *, calc ideal.quotient.mk (span {↑p ^ 2}) (∑ i in range p, (a + ↑p * b) ^ i * a ^ (p - 1 - i)) = ∑ (i : ℕ) in finset.range p, mk (span {↑p ^ 2}) ((a ^ (i - 1) * (↑p * b) * ↑i + a ^ i) * a ^ (p - 1 - i)) : by simp_rw [ring_hom.map_geom_sum₂, ← map_pow, h1, ← _root_.map_mul] ... = mk (span {↑p ^ 2}) (∑ (x : ℕ) in finset.range p, a ^ (x - 1) * (a ^ (p - 1 - x) * (↑p * (b * ↑x)))) + mk (span {↑p ^ 2}) (∑ (x : ℕ) in finset.range p, a ^ (x + (p - 1 - x))) : by { ring_exp, simp only [← pow_add, map_add, finset.sum_add_distrib, ← map_sum] } ... = mk (span {↑p ^ 2}) (∑ (x : ℕ) in finset.range p, a ^ (x - 1) * (a ^ (p - 1 - x) * (↑p * (b * ↑x)))) + mk (span {↑ p ^ 2}) ∑ (x : ℕ) in finset.range p, a ^ (p - 1) : by { rw [add_right_inj, finset.sum_congr rfl], intros x hx, rw [← nat.add_sub_assoc _ x, nat.add_sub_cancel_left], exact nat.le_pred_of_lt (finset.mem_range.mp hx) } ... = mk (span {↑p ^ 2}) (∑ (x : ℕ) in finset.range p, a ^ (x - 1) * (a ^ (p - 1 - x) * (↑p * (b * ↑x)))) + mk (span {↑ p ^ 2}) (↑p * a ^ (p - 1)) : by simp only [add_right_inj, finset.sum_const, finset.card_range, nsmul_eq_mul] ... = mk (span {↑p ^ 2}) (↑p * b * ∑ (x : ℕ) in finset.range p, a ^ (p - 2) * x) + mk (span {↑p ^ 2}) (↑p * a ^ (p - 1)) : by { simp only [finset.mul_sum, ← mul_assoc, ← pow_add], rw finset.sum_congr rfl, rintros (⟨⟩|⟨x⟩) hx, { rw [nat.cast_zero, mul_zero, mul_zero] }, { have : x.succ - 1 + (p - 1 - x.succ) = p - 2, { rw ← nat.add_sub_assoc (nat.le_pred_of_lt (finset.mem_range.mp hx)), exact congr_arg nat.pred (nat.add_sub_cancel_left _ _)}, rw this, ring_exp_eq }} ... = mk (span {↑p ^ 2}) (↑p * a ^ (p - 1)) : by { simp only [add_left_eq_self, ← finset.mul_sum], norm_cast, simp only [finset.sum_range_id, nat.cast_mul, _root_.map_mul, nat.mul_div_assoc _ (even_iff_two_dvd.mp (nat.odd.sub_odd hp odd_one))], ring_exp, simp only [← map_pow, mul_eq_zero_of_left, ideal.quotient.eq_zero_iff_mem, mem_span_singleton] } end namespace multiplicity section integral_domain variables [is_domain R] [@decidable_rel R (∣)] lemma pow_sub_pow_of_prime {p : R} (hp : prime p) {x y : R} (hxy : p ∣ x - y) (hx : ¬p ∣ x) {n : ℕ} (hn : ¬p ∣ n) : multiplicity p (x ^ n - y ^ n) = multiplicity p (x - y) := by rw [←geom_sum₂_mul, multiplicity.mul hp, multiplicity_eq_zero_of_not_dvd (not_dvd_geom_sum₂ hp hxy hx hn), zero_add] variables (hp : prime (p : R)) (hp1 : odd p) (hxy : ↑p ∣ x - y) (hx : ¬↑p ∣ x) include hp hp1 hxy hx lemma geom_sum₂_eq_one : multiplicity ↑p (∑ i in range p, x ^ i * y ^ (p - 1 - i)) = 1 := begin rw ← nat.cast_one, refine multiplicity.eq_coe_iff.2 ⟨_, _⟩, { rw pow_one, exact dvd_geom_sum₂_self hxy }, rw dvd_iff_dvd_of_dvd_sub hxy at hx, cases hxy with k hk, rw [one_add_one_eq_two, eq_add_of_sub_eq' hk], refine mt (dvd_iff_dvd_of_dvd_sub (@odd_sq_dvd_geom_sum₂_sub _ _ y k _ hp1)).mp _, rw [pow_two, mul_dvd_mul_iff_left hp.ne_zero], exact mt hp.dvd_of_dvd_pow hx end lemma pow_prime_sub_pow_prime : multiplicity ↑p (x ^ p - y ^ p) = multiplicity ↑p (x - y) + 1 := by rw [←geom_sum₂_mul, multiplicity.mul hp, geom_sum₂_eq_one hp hp1 hxy hx, add_comm] lemma pow_prime_pow_sub_pow_prime_pow (a : ℕ) : multiplicity ↑p (x ^ p ^ a - y ^ p ^ a) = multiplicity ↑p (x - y) + a := begin induction a with a h_ind, { rw [nat.cast_zero, add_zero, pow_zero, pow_one, pow_one] }, rw [←nat.add_one, nat.cast_add, nat.cast_one, ←add_assoc, ←h_ind, pow_succ', pow_mul, pow_mul], apply pow_prime_sub_pow_prime hp hp1, { rw ←geom_sum₂_mul, exact dvd_mul_of_dvd_right hxy _ }, { exact λ h, hx (hp.dvd_of_dvd_pow h) } end end integral_domain section lifting_the_exponent variables (hp : nat.prime p) (hp1 : odd p) include hp hp1 /-- **Lifting the exponent lemma** for odd primes. -/ lemma int.pow_sub_pow {x y : ℤ} (hxy : ↑p ∣ x - y) (hx : ¬↑p ∣ x) (n : ℕ) : multiplicity ↑p (x ^ n - y ^ n) = multiplicity ↑p (x - y) + multiplicity p n := begin cases n, { simp only [multiplicity.zero, add_top, pow_zero, sub_self] }, have h : (multiplicity _ _).dom := finite_nat_iff.mpr ⟨hp.ne_one, n.succ_pos⟩, rcases eq_coe_iff.mp (part_enat.coe_get h).symm with ⟨⟨k, hk⟩, hpn⟩, conv_lhs { rw [hk, pow_mul, pow_mul] }, rw nat.prime_iff_prime_int at hp, rw [pow_sub_pow_of_prime hp, pow_prime_pow_sub_pow_prime_pow hp hp1 hxy hx, part_enat.coe_get], { rw ←geom_sum₂_mul, exact dvd_mul_of_dvd_right hxy _ }, { exact λ h, hx (hp.dvd_of_dvd_pow h) }, { rw int.coe_nat_dvd, rintro ⟨c, rfl⟩, refine hpn ⟨c, _⟩, rwa [pow_succ', mul_assoc] } end lemma int.pow_add_pow {x y : ℤ} (hxy : ↑p ∣ x + y) (hx : ¬↑p ∣ x) {n : ℕ} (hn : odd n) : multiplicity ↑p (x ^ n + y ^ n) = multiplicity ↑p (x + y) + multiplicity p n := begin rw ←sub_neg_eq_add at hxy, rw [←sub_neg_eq_add, ←sub_neg_eq_add, ←odd.neg_pow hn], exact int.pow_sub_pow hp hp1 hxy hx n end lemma nat.pow_sub_pow {x y : ℕ} (hxy : p ∣ x - y) (hx : ¬p ∣ x) (n : ℕ) : multiplicity p (x ^ n - y ^ n) = multiplicity p (x - y) + multiplicity p n := begin obtain hyx | hyx := le_total y x, { iterate 2 { rw ← int.coe_nat_multiplicity }, rw int.coe_nat_sub (nat.pow_le_pow_of_le_left hyx n), rw ← int.coe_nat_dvd at hxy hx, push_cast at *, exact int.pow_sub_pow hp hp1 hxy hx n }, { simp only [nat.sub_eq_zero_iff_le.mpr hyx, nat.sub_eq_zero_iff_le.mpr (nat.pow_le_pow_of_le_left hyx n), multiplicity.zero, part_enat.top_add] } end lemma nat.pow_add_pow {x y : ℕ} (hxy : p ∣ x + y) (hx : ¬p ∣ x) {n : ℕ} (hn : odd n) : multiplicity p (x ^ n + y ^ n) = multiplicity p (x + y) + multiplicity p n := begin iterate 2 { rw [←int.coe_nat_multiplicity] }, rw ←int.coe_nat_dvd at hxy hx, push_cast at *, exact int.pow_add_pow hp hp1 hxy hx hn end end lifting_the_exponent end multiplicity end comm_ring lemma pow_two_pow_sub_pow_two_pow [comm_ring R] {x y : R} (n : ℕ) : x ^ (2 ^ n) - y ^ (2 ^ n) = (∏ i in finset.range n, (x ^ (2 ^ i) + y ^ (2 ^ i))) * (x - y) := begin induction n with d hd, { simp only [pow_zero, pow_one, finset.range_zero, finset.prod_empty, one_mul] }, { suffices : x ^ 2 ^ d.succ - y ^ 2 ^ d.succ = (x ^ 2 ^ d + y ^ 2 ^ d) * (x ^ 2 ^ d - y ^ 2 ^ d), { rw [this, hd, finset.prod_range_succ, ← mul_assoc, mul_comm (x ^ 2 ^ d + y ^ 2 ^ d)] }, { ring_exp_eq } } end lemma _root_.int.sq_mod_four_eq_one_of_odd {x : ℤ} : odd x → x ^ 2 % 4 = 1 := begin intro hx, -- Replace `x : ℤ` with `y : zmod 4` replace hx : x % (2 : ℕ) = 1 % (2 : ℕ), { rw int.odd_iff at hx, norm_num [hx] }, calc x^2 % (4 : ℕ) = 1 % (4 : ℕ) : _ ... = 1 : by norm_num, rw ← zmod.int_coe_eq_int_coe_iff' at hx ⊢, push_cast, rw [← map_int_cast (zmod.cast_hom (show 2 ∣ 4, by norm_num) (zmod 2)) x] at hx, set y : zmod 4 := x, -- Now we can just consider each of the 4 possible values for y fin_cases y using hy; rw hy at ⊢ hx; revert hx; dec_trivial end lemma int.two_pow_two_pow_add_two_pow_two_pow {x y : ℤ} (hx : ¬ 2 ∣ x) (hxy : 4 ∣ (x - y)) (i : ℕ) : multiplicity 2 (x ^ 2 ^ i + y ^ 2 ^ i) = ↑(1 : ℕ) := begin have hx_odd : odd x, { rwa [int.odd_iff_not_even, even_iff_two_dvd] }, have hxy_even : even (x - y) := even_iff_two_dvd.mpr (dvd_trans (by norm_num) hxy), have hy_odd : odd y := by simpa using hx_odd.sub_even hxy_even, refine multiplicity.eq_coe_iff.mpr ⟨_, _⟩, { rw [pow_one, ← even_iff_two_dvd], exact (hx_odd.pow).add_odd hy_odd.pow }, cases i with i, { intro hxy', have : 2 * 2 ∣ 2 * x, { convert dvd_add hxy hxy', ring_exp }, have : 2 ∣ x := (mul_dvd_mul_iff_left (by norm_num)).mp this, contradiction }, suffices : ∀ (x : ℤ), odd x → x ^ (2 ^ (i + 1)) % 4 = 1, { rw [show (2 ^ (1 + 1) : ℤ) = 4, by norm_num, int.dvd_iff_mod_eq_zero, int.add_mod, this _ hx_odd, this _ hy_odd], norm_num }, intros x hx, rw [pow_succ, mul_comm, pow_mul, int.sq_mod_four_eq_one_of_odd hx.pow] end lemma int.two_pow_two_pow_sub_pow_two_pow {x y : ℤ} (n : ℕ) (hxy : 4 ∣ x - y) (hx : ¬ 2 ∣ x) : multiplicity 2 (x ^ (2 ^ n) - y ^ (2 ^ n)) = multiplicity 2 (x - y) + n := by simp only [pow_two_pow_sub_pow_two_pow n, multiplicity.mul int.prime_two, multiplicity.finset.prod (int.prime_two), add_comm, nat.cast_one, finset.sum_const, finset.card_range, nsmul_one, int.two_pow_two_pow_add_two_pow_two_pow hx hxy] lemma int.two_pow_sub_pow' {x y : ℤ} (n : ℕ) (hxy : 4 ∣ x - y) (hx : ¬ 2 ∣ x) : multiplicity 2 (x ^ n - y ^ n) = multiplicity 2 (x - y) + multiplicity (2 : ℤ) n := begin have hx_odd : odd x, { rwa [int.odd_iff_not_even, even_iff_two_dvd] }, have hxy_even : even (x - y) := even_iff_two_dvd.mpr (dvd_trans (by norm_num) hxy), have hy_odd : odd y := by simpa using hx_odd.sub_even hxy_even, cases n, { simp only [pow_zero, sub_self, multiplicity.zero, int.coe_nat_zero, part_enat.add_top] }, have h : (multiplicity 2 n.succ).dom := multiplicity.finite_nat_iff.mpr ⟨by norm_num, n.succ_pos⟩, rcases multiplicity.eq_coe_iff.mp (part_enat.coe_get h).symm with ⟨⟨k, hk⟩, hpn⟩, rw [hk, pow_mul, pow_mul, multiplicity.pow_sub_pow_of_prime, int.two_pow_two_pow_sub_pow_two_pow _ hxy hx, ← hk, part_enat.coe_get], { norm_cast }, { exact int.prime_two }, { simpa only [even_iff_two_dvd] using hx_odd.pow.sub_odd hy_odd.pow }, { simpa only [even_iff_two_dvd, int.odd_iff_not_even] using hx_odd.pow }, erw [int.coe_nat_dvd], -- `erw` to deal with `2 : ℤ` vs `(2 : ℕ) : ℤ` contrapose! hpn, rw pow_succ', conv_rhs { rw hk }, exact mul_dvd_mul_left _ hpn end /-- **Lifting the exponent lemma** for `p = 2` -/ lemma int.two_pow_sub_pow {x y : ℤ} {n : ℕ} (hxy : 2 ∣ x - y) (hx : ¬ 2 ∣ x) (hn : even n) : multiplicity 2 (x ^ n - y ^ n) + 1 = multiplicity 2 (x + y) + multiplicity 2 (x - y) + multiplicity (2 : ℤ) n := begin have hy : odd y, { rw [← even_iff_two_dvd, ← int.odd_iff_not_even] at hx, replace hxy := (@even_neg _ _ (x - y)).mpr (even_iff_two_dvd.mpr hxy), convert even.add_odd hxy hx, abel }, cases hn with d hd, subst hd, simp only [← two_mul, pow_mul], have hxy4 : 4 ∣ x ^ 2 - y ^ 2, { rw [int.dvd_iff_mod_eq_zero, int.sub_mod, int.sq_mod_four_eq_one_of_odd _, int.sq_mod_four_eq_one_of_odd hy], { norm_num }, { simp only [int.odd_iff_not_even, even_iff_two_dvd, hx, not_false_iff] }}, rw [int.two_pow_sub_pow' d hxy4 _, sq_sub_sq, ← int.coe_nat_mul_out, multiplicity.mul (int.prime_two), multiplicity.mul (int.prime_two)], suffices : multiplicity (2 : ℤ) ↑(2 : ℕ) = 1, { rw [this, add_comm (1 : part_enat), ← add_assoc] }, { norm_cast, rw multiplicity.multiplicity_self _ _, { apply prime.not_unit, simp only [← nat.prime_iff, nat.prime_two] }, { exact two_ne_zero }}, { rw [← even_iff_two_dvd, ← int.odd_iff_not_even], apply odd.pow, simp only [int.odd_iff_not_even, even_iff_two_dvd, hx, not_false_iff] } end lemma nat.two_pow_sub_pow {x y : ℕ} (hxy : 2 ∣ x - y) (hx : ¬2 ∣ x) {n : ℕ} (hn : even n) : multiplicity 2 (x ^ n - y ^ n) + 1 = multiplicity 2 (x + y) + multiplicity 2 (x - y) + multiplicity 2 n := begin obtain hyx | hyx := le_total y x, { iterate 3 { rw ←multiplicity.int.coe_nat_multiplicity }, have hxyn : y ^ n ≤ x ^ n := pow_le_pow_of_le_left' hyx _, simp only [int.coe_nat_sub hyx, int.coe_nat_sub (pow_le_pow_of_le_left' hyx _), int.coe_nat_add, int.coe_nat_pow], rw ←int.coe_nat_dvd at hx, rw [←int.coe_nat_dvd, int.coe_nat_sub hyx] at hxy, convert int.two_pow_sub_pow hxy hx hn using 2, rw ← multiplicity.int.coe_nat_multiplicity, refl }, { simp only [nat.sub_eq_zero_iff_le.mpr hyx, nat.sub_eq_zero_iff_le.mpr (pow_le_pow_of_le_left' hyx n), multiplicity.zero, part_enat.top_add, part_enat.add_top] } end namespace padic_val_nat variables {x y : ℕ} lemma pow_two_sub_pow (hyx : y < x) (hxy : 2 ∣ x - y) (hx : ¬ 2 ∣ x) {n : ℕ} (hn : 0 < n) (hneven : even n) : padic_val_nat 2 (x ^ n - y ^ n) + 1 = padic_val_nat 2 (x + y) + padic_val_nat 2 (x - y) + padic_val_nat 2 n := begin simp only [←part_enat.coe_inj, nat.cast_add], iterate 4 { rw [padic_val_nat_def, part_enat.coe_get] }, { convert nat.two_pow_sub_pow hxy hx hneven using 2 }, { exact hn }, { exact (nat.sub_pos_of_lt hyx) }, { linarith }, { simp only [tsub_pos_iff_lt, pow_lt_pow_of_lt_left hyx (@zero_le' _ y _) hn] } end variables {p : ℕ} [hp : fact p.prime] (hp1 : odd p) include hp hp1 lemma pow_sub_pow (hyx : y < x) (hxy : p ∣ x - y) (hx : ¬p ∣ x) {n : ℕ} (hn : 0 < n) : padic_val_nat p (x ^ n - y ^ n) = padic_val_nat p (x - y) + padic_val_nat p n := begin rw [←part_enat.coe_inj, nat.cast_add], iterate 3 { rw [padic_val_nat_def, part_enat.coe_get] }, { exact multiplicity.nat.pow_sub_pow hp.out hp1 hxy hx n }, { exact hn }, { exact nat.sub_pos_of_lt hyx }, { exact nat.sub_pos_of_lt (nat.pow_lt_pow_of_lt_left hyx hn) } end lemma pow_add_pow (hxy : p ∣ x + y) (hx : ¬p ∣ x) {n : ℕ} (hn : odd n) : padic_val_nat p (x ^ n + y ^ n) = padic_val_nat p (x + y) + padic_val_nat p n := begin cases y, { have := dvd_zero p, contradiction }, rw [←part_enat.coe_inj, nat.cast_add], iterate 3 { rw [padic_val_nat_def, part_enat.coe_get] }, { exact multiplicity.nat.pow_add_pow hp.out hp1 hxy hx hn }, { exact (odd.pos hn) }, { simp only [add_pos_iff, nat.succ_pos', or_true] }, { exact (nat.lt_add_left _ _ _ (pow_pos y.succ_pos _)) } end end padic_val_nat
3b1915e310f9f795c5790dbd3b2a752481c201bd
4f065978c49388d188224610d9984673079f7d91
/Knaster-Tarski.lean
f0a3897b72c4cbc34291cdaf7ae329ada4d36c01
[]
no_license
kckennylau/Lean
b323103f52706304907adcfaee6f5cb8095d4a33
907d0a4d2bd8f23785abd6142ad53d308c54fdcb
refs/heads/master
1,624,623,720,653
1,563,901,820,000
1,563,901,820,000
109,506,702
3
1
null
null
null
null
UTF-8
Lean
false
false
4,252
lean
import order.complete_lattice order.order_iso order.fixed_points open lattice universe u variables {α : Type u} [complete_lattice α] variables (f : α → α) (M : monotone f) def fixed_points : set α := { x | f x = x } namespace fixed_points def previous (x : α) : α := gfp (λ z, x ⊓ f z) variable {f} theorem previous.le {x : α} : previous f x ≤ x := gfp_le $ λ z hz, le_trans hz inf_le_left theorem previous.le_apply {x : α} : previous f x ≤ f (previous f x) := gfp_le $ λ z hz, le_trans (le_trans hz inf_le_right) $ M $ le_gfp hz theorem previous.fixed {x : α} (H : f x ≤ x) : f (previous f x) = previous f x := le_antisymm (le_gfp $ le_inf (le_trans (M previous.le) H) (M $ previous.le_apply M)) (previous.le_apply M) variable f def next (x : α) : α := lfp (λ z, x ⊔ f z) variable {f} theorem next.le {x : α} : x ≤ next f x := le_lfp $ λ z hz, le_trans le_sup_left hz theorem next.apply_le {x : α} : f (next f x) ≤ next f x := le_lfp $ λ z hz, le_trans (le_trans (M $ show next f x ≤ z, from lfp_le hz) le_sup_right) hz theorem next.fixed {x : α} (H : x ≤ f x) : f (next f x) = next f x := le_antisymm (next.apply_le M) (lfp_le $ sup_le (le_trans H (M next.le)) (M $ next.apply_le M)) variable f theorem sup_le_f_of_fixed_points (x y : fixed_points f) : x.1 ⊔ y.1 ≤ f (x.1 ⊔ y.1) := sup_le (x.2 ▸ (M $ show x.1 ≤ f x.1 ⊔ y.1, from x.2.symm ▸ le_sup_left)) (y.2 ▸ (M $ show y.1 ≤ x.1 ⊔ f y.1, from y.2.symm ▸ le_sup_right)) theorem f_le_inf_of_fixed_points (x y : fixed_points f) : f (x.1 ⊓ y.1) ≤ x.1 ⊓ y.1 := le_inf (x.2 ▸ (M $ show f (x.1) ⊓ y.1 ≤ x.1, from x.2.symm ▸ inf_le_left)) (y.2 ▸ (M $ show x.1 ⊓ f (y.1) ≤ y.1, from y.2.symm ▸ inf_le_right)) theorem Sup_le_f_of_fixed_points (A : set α) (HA : A ⊆ fixed_points f) : Sup A ≤ f (Sup A) := Sup_le $ λ x hxA, (HA hxA) ▸ (M $ le_Sup hxA) theorem f_le_Inf_of_fixed_points (A : set α) (HA : A ⊆ fixed_points f) : f (Inf A) ≤ Inf A := le_Inf $ λ x hxA, (HA hxA) ▸ (M $ Inf_le hxA) instance : complete_lattice (fixed_points f) := { le := subrel (≤) _, le_refl := λ x, le_refl x, le_trans := λ x y z, le_trans, le_antisymm := λ x y hx hy, subtype.eq $ le_antisymm hx hy, sup := λ x y, ⟨next f (x.1 ⊔ y.1), next.fixed M (sup_le_f_of_fixed_points f M x y)⟩, le_sup_left := λ x y, show x.1 ≤ _, from le_trans le_sup_left next.le, le_sup_right := λ x y, show y.1 ≤ _, from le_trans le_sup_right next.le, sup_le := λ x y z hxz hyz, lfp_le $ sup_le (sup_le hxz hyz) (z.2.symm ▸ le_refl z.1), inf := λ x y, ⟨previous f (x.1 ⊓ y.1), previous.fixed M (f_le_inf_of_fixed_points f M x y)⟩, inf_le_left := λ x y, show _ ≤ x.1, from le_trans previous.le inf_le_left, inf_le_right := λ x y, show _ ≤ y.1, from le_trans previous.le inf_le_right, le_inf := λ x y z hxy hxz, le_gfp $ le_inf (le_inf hxy hxz) (x.2.symm ▸ le_refl x), top := ⟨previous f ⊤, previous.fixed M le_top⟩, le_top := λ ⟨x, H⟩, le_gfp $ le_inf le_top (H.symm ▸ le_refl x), bot := ⟨next f ⊥, next.fixed M bot_le⟩, bot_le := λ ⟨x, H⟩, lfp_le $ sup_le bot_le (H.symm ▸ le_refl x), Sup := λ A, ⟨next f (Sup $ subtype.val '' A), next.fixed M (Sup_le_f_of_fixed_points f M (subtype.val '' A) (λ z ⟨x, hx⟩, hx.2 ▸ x.2))⟩, le_Sup := λ A x hxA, show x.1 ≤ _, from le_trans (le_Sup $ show x.1 ∈ subtype.val '' A, from ⟨x, hxA, rfl⟩) next.le, Sup_le := λ A x Hx, lfp_le $ sup_le (Sup_le $ λ z ⟨y, hyA, hyz⟩, hyz ▸ Hx y hyA) (x.2.symm ▸ le_refl x), Inf := λ A, ⟨previous f (Inf $ subtype.val '' A), previous.fixed M (f_le_Inf_of_fixed_points f M (subtype.val '' A) (λ z ⟨x, hx⟩, hx.2 ▸ x.2))⟩, le_Inf := λ A x Hx, le_gfp $ le_inf (le_Inf $ λ z ⟨y, hyA, hyz⟩, hyz ▸ Hx y hyA) (x.2.symm ▸ le_refl x.1), Inf_le := λ A x hxA, show _ ≤ x.1, from le_trans previous.le (Inf_le $ show x.1 ∈ subtype.val '' A, from ⟨x, hxA, rfl⟩) } end fixed_points
d8bd2ea0b6e9aea11d03774dd06b2881cd5e975f
ea5678cc400c34ff95b661fa26d15024e27ea8cd
/limits.lean
71f7456bbb7e99a68c1dd2c8ab08c3513f731de9
[]
no_license
ChrisHughes24/leanstuff
dca0b5349c3ed893e8792ffbd98cbcadaff20411
9efa85f72efaccd1d540385952a6acc18fce8687
refs/heads/master
1,654,883,241,759
1,652,873,885,000
1,652,873,885,000
134,599,537
1
0
null
null
null
null
UTF-8
Lean
false
false
8,317
lean
import data.complex.basic namespace cau_seq theorem const_inv {α β : Type*} [discrete_field β] [discrete_linear_ordered_field α] {abv : β → α} [is_absolute_value abv] {x : β} (hx : x ≠ 0) : const abv (x⁻¹) = inv (const abv x) (by rwa const_lim_zero) := ext (assume n, by simp[inv_apply, const_apply]) end cau_seq namespace real open cau_seq real lemma eq_lim_of_const_equiv {f : cau_seq ℝ abs} {x : ℝ} (h : cau_seq.const abs x ≈ f) : x = lim f := const_equiv.mp $ setoid.trans h $ equiv_lim f lemma lim_eq_of_equiv_const {f : cau_seq ℝ abs} {x : ℝ} (h : f ≈ cau_seq.const abs x) : lim f = x := (eq_lim_of_const_equiv $ setoid.symm h).symm lemma lim_eq_lim_of_equiv {f g : cau_seq ℝ abs} (h : f ≈ g) : lim f = lim g := lim_eq_of_equiv_const $ setoid.trans h $ equiv_lim g @[simp] lemma lim_const (x : ℝ) : lim (const abs x) = x := lim_eq_of_equiv_const $ setoid.refl _ lemma lim_add (f g : cau_seq ℝ abs) : lim f + lim g = lim ⇑(f + g) := eq_lim_of_const_equiv $ show lim_zero (const abs (lim ⇑f + lim ⇑g) - (f + g)), by rw [const_add, add_sub_comm]; exact add_lim_zero (setoid.symm (equiv_lim f)) (setoid.symm (equiv_lim g)) lemma lim_mul_lim (f g : cau_seq ℝ abs) : lim f * lim g = lim ⇑(f * g) := eq_lim_of_const_equiv $ show lim_zero (const abs (lim ⇑f * lim ⇑g) - f * g), from have h : const abs (lim ⇑f * lim ⇑g) - f * g = g * (const abs (lim f) - f) + const abs (lim f) * (const abs (lim g) - g) := by simp [mul_sub, mul_comm, const_mul, mul_add], by rw h; exact add_lim_zero (mul_lim_zero _ (setoid.symm (equiv_lim f))) (mul_lim_zero _ (setoid.symm (equiv_lim g))) lemma lim_mul (f : cau_seq ℝ abs) (x : ℝ) : lim f * x = lim ⇑(f * const abs x) := by rw [← lim_mul_lim, lim_const] lemma lim_neg (f : cau_seq ℝ abs) : lim ⇑(-f) = -lim f := lim_eq_of_equiv_const (show lim_zero (-f - const abs (-lim ⇑f)), by rw [const_neg, sub_neg_eq_add, add_comm]; exact setoid.symm (equiv_lim f)) lemma lim_eq_zero_iff (f : cau_seq ℝ abs) : lim f = 0 ↔ lim_zero f := ⟨assume h, by have hf := equiv_lim f; rw h at hf; exact (lim_zero_congr hf).mpr (const_lim_zero.mpr rfl), assume h, have h₁ : f = (f - const abs 0) := ext (λ n, by simp [sub_apply, const_apply]), by rw h₁ at h; exact lim_eq_of_equiv_const h ⟩ lemma lim_inv {f : cau_seq ℝ abs} (hf : ¬ lim_zero f) : lim ⇑(inv f hf) = (lim f)⁻¹ := have hl : lim f ≠ 0 := by rwa ← lim_eq_zero_iff at hf, lim_eq_of_equiv_const $ show lim_zero (inv f hf - const abs (lim ⇑f)⁻¹), from have h₁ : ∀ (g f : cau_seq ℝ abs) (hf : ¬ lim_zero f), lim_zero (g - f * inv f hf * g) := λ g f hf, by rw [← one_mul g, ← mul_assoc, ← sub_mul, mul_one, mul_comm, mul_comm f]; exact mul_lim_zero _ (setoid.symm (cau_seq.inv_mul_cancel _)), have h₂ : lim_zero ((inv f hf - const abs (lim ⇑f)⁻¹) - (const abs (lim f) - f) * (inv f hf * const abs (lim ⇑f)⁻¹)) := by rw [sub_mul, ← sub_add, sub_sub, sub_add_eq_sub_sub, sub_right_comm, sub_add]; exact show lim_zero (inv f hf - const abs (lim ⇑f) * (inv f hf * const abs (lim ⇑f)⁻¹) - (const abs (lim ⇑f)⁻¹ - f * (inv f hf * const abs (lim ⇑f)⁻¹))), from sub_lim_zero (by rw [← mul_assoc, mul_right_comm, const_inv hl]; exact h₁ _ _ _) (by rw [← mul_assoc]; exact h₁ _ _ _), (lim_zero_congr h₂).mpr $ by rw mul_comm; exact mul_lim_zero _ (setoid.symm (equiv_lim f)) end real namespace complex open cau_seq lemma re_const_equiv_of_const_equiv {f : ℕ → ℂ} (hf : is_cau_seq abs f) (z : ℂ) : cau_seq.const abs z ≈ ⟨f, hf⟩ → cau_seq.const _root_.abs z.re ≈ ⟨(λ (n : ℕ), (f n).re), complex.is_cau_seq_re ⟨f,hf⟩⟩ := begin assume h,assume ε ε0,cases h ε ε0 with i hi,existsi i,assume j ji, replace hi := hi j ji, simp at *, rw [←complex.neg_re,←complex.add_re], exact lt_of_le_of_lt (complex.abs_re_le_abs _) hi, end lemma im_const_equiv_of_const_equiv {f : ℕ → ℂ} (hf : is_cau_seq abs f) (z : ℂ) : cau_seq.const abs z ≈ ⟨f, hf⟩ → cau_seq.const _root_.abs z.im ≈ ⟨(λ (n : ℕ), (f n).im), complex.is_cau_seq_im ⟨f,hf⟩⟩ := begin assume h,assume ε ε0,cases h ε ε0 with i hi,existsi i,assume j ji, replace hi := hi j ji, simp at *, rw [←complex.neg_im,←complex.add_im], exact lt_of_le_of_lt (complex.abs_im_le_abs _) hi, end lemma eq_lim_of_const_equiv {f : cau_seq ℂ abs} {z: ℂ} : cau_seq.const complex.abs z ≈ f → z = complex.lim f := begin assume h, unfold complex.lim,cases z with zre zim,simp, split, have := real.equiv_lim ⟨(λ (n : ℕ), (f.1 n).re), complex.is_cau_seq_re f⟩, rw ←cau_seq.const_equiv,simp at this, have hf := complex.re_const_equiv_of_const_equiv f.2 {re := zre, im := zim} h,simp at hf, exact setoid.trans hf this, have := real.equiv_lim ⟨(λ (n : ℕ), (f.1 n).im), complex.is_cau_seq_im f⟩, rw ←cau_seq.const_equiv,simp at this, have hf := complex.im_const_equiv_of_const_equiv f.2 {re := zre, im := zim} h,simp at hf, exact setoid.trans hf this, end lemma lim_eq_of_equiv_const {f : cau_seq ℂ complex.abs} {x : ℂ} (h : f ≈ cau_seq.const complex.abs x) : lim f = x := (eq_lim_of_const_equiv $ setoid.symm h).symm lemma lim_eq_lim_of_equiv {f g : cau_seq ℂ complex.abs} (h : f ≈ g) : lim f = lim g := lim_eq_of_equiv_const $ setoid.trans h $ equiv_lim g @[simp] lemma lim_const (x : ℂ) : lim (const abs x) = x := lim_eq_of_equiv_const $ setoid.refl _ lemma lim_add (f g : cau_seq ℂ complex.abs) : lim f + lim g = lim ⇑(f + g) := eq_lim_of_const_equiv $ show lim_zero (const complex.abs (lim ⇑f + lim ⇑g) - (f + g)), by rw [const_add, add_sub_comm]; exact add_lim_zero (setoid.symm (equiv_lim f)) (setoid.symm (equiv_lim g)) lemma lim_mul_lim (f g : cau_seq ℂ complex.abs) : lim f * lim g = lim ⇑(f * g) := eq_lim_of_const_equiv $ show lim_zero (const complex.abs (lim ⇑f * lim ⇑g) - f * g), from have h : const complex.abs (lim ⇑f * lim ⇑g) - f * g = g * (const complex.abs (lim f) - f) + const complex.abs (lim f) * (const complex.abs (lim g) - g) := by simp [mul_sub, mul_comm, const_mul, mul_add], by rw h; exact add_lim_zero (mul_lim_zero _ (setoid.symm (equiv_lim f))) (mul_lim_zero _ (setoid.symm (equiv_lim g))) lemma lim_mul (f : cau_seq ℂ complex.abs) (x : ℂ) : lim f * x = lim ⇑(f * const complex.abs x) := by rw [← lim_mul_lim, lim_const] lemma lim_neg (f : cau_seq ℂ complex.abs) : lim ⇑(-f) = -lim f := lim_eq_of_equiv_const (show lim_zero (-f - const complex.abs (-lim ⇑f)), by rw [const_neg, sub_neg_eq_add, add_comm]; exact setoid.symm (equiv_lim f)) lemma lim_eq_zero_iff (f : cau_seq ℂ complex.abs) : lim f = 0 ↔ lim_zero f := ⟨assume h, by have hf := equiv_lim f; rw h at hf; exact (lim_zero_congr hf).mpr (const_lim_zero.mpr rfl), assume h, have h₁ : f = (f - const complex.abs (0 : ℂ)) := cau_seq.ext (λ n, by simp [sub_apply, const_apply]), by rw h₁ at h; exact lim_eq_of_equiv_const h ⟩ lemma lim_inv {f : cau_seq ℂ complex.abs} (hf : ¬ lim_zero f) : lim ⇑(inv f hf) = (lim f)⁻¹ := have hl : lim f ≠ 0 := by rwa ← lim_eq_zero_iff at hf, lim_eq_of_equiv_const $ show lim_zero (inv f hf - const abs (lim ⇑f)⁻¹), from have h₁ : ∀ (g f : cau_seq ℂ abs) (hf : ¬ lim_zero f), lim_zero (g - f * inv f hf * g) := λ g f hf, by rw [← one_mul g, ← mul_assoc, ← sub_mul, mul_one, mul_comm, mul_comm f]; exact mul_lim_zero _ (setoid.symm (cau_seq.inv_mul_cancel _)), have h₂ : lim_zero ((inv f hf - const abs (lim ⇑f)⁻¹) - (const abs (lim f) - f) * (inv f hf * const abs (lim ⇑f)⁻¹)) := by rw [sub_mul, ← sub_add, sub_sub, sub_add_eq_sub_sub, sub_right_comm, sub_add]; exact show lim_zero (inv f hf - const abs (lim ⇑f) * (inv f hf * const abs (lim ⇑f)⁻¹) - (const abs (lim ⇑f)⁻¹ - f * (inv f hf * const abs (lim ⇑f)⁻¹))), from sub_lim_zero (by rw [← mul_assoc, mul_right_comm, cau_seq.const_inv hl]; exact h₁ _ _ _) (by rw [← mul_assoc]; exact h₁ _ _ _), (lim_zero_congr h₂).mpr $ by rw mul_comm; exact mul_lim_zero _ (setoid.symm (equiv_lim f)) end complex
a69c3bfb664bd1984af396fc0137c6502f944a7e
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/meta/options_auto.lean
bc34af860534c1df71989aef4a94b41de045ad80
[]
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
257
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 -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.meta.name namespace Mathlib end Mathlib
42216cebf9162858b202d81f3be3ad4524635f0a
3618c6e11aa822fd542440674dfb9a7b9921dba0
/src/cyclically_reduce.lean
c30ea77d4b5b8dcbba4b1e108e4da03930889033
[]
no_license
ChrisHughes24/single_relation
99ceedcc02d236ce46d6c65d72caa669857533c5
057e157a59de6d0e43b50fcb537d66792ec20450
refs/heads/master
1,683,652,062,698
1,683,360,089,000
1,683,360,089,000
279,346,432
0
0
null
null
null
null
UTF-8
Lean
false
false
1,789
lean
import coprod.free_group import initial /-! # Cyclically reducing words This file contains three functions `cyclically_reduce`, `cyclically_conjugate` and `min_max_subscript` -/ variables {ι : Type} [decidable_eq ι] open free_group mul_aut multiplicative /-- Given a word `r` returns a pair `(g, r')` such that `r'` is cyclically reduced and `g * r * g⁻¹ = r'`. Cyclically reduced means that if it contains more than one distinct letter, then it starts and finishes with a different letter. -/ @[inline] meta def cyclically_reduce : free_group ι → free_group ι × free_group ι | ⟨[], _⟩ := (1, 1) | ⟨[i], h⟩ := (1, ⟨[i], h⟩) | ⟨i::j::l, h⟩ := let k := (j :: l : list _).last (list.cons_ne_nil _ _) in if i.1 = k.1 then let z := k.2 * i.2 in if z = 1 then let init : free_group ι := of_list (j :: l : list _).init in let w' := cyclically_reduce init in (w'.1 * of' k.1 k.2, w'.2) else (of' k.1 k.2, of_list (⟨k.1, z⟩ :: (j :: l : list _).init)) else (1, ⟨i::j::l, h⟩) /-- Given a word `r` and a letter `x` returns a pair `(g, r')` such that `r'` begins with `x` and `g * r * g⁻¹ = r'` -/ @[inline] meta def cyclically_conjugate (x : ι) (w : free_group ι) : free_group ι × free_group ι := let n : ℕ := w.to_list.find_index (λ i, i.1 = x) in ⟨(of_list (w.to_list.take n))⁻¹, of_list (w.to_list.rotate n)⟩ /-- `min_max_subscript x r` returns the minimum and maximum `n` such that `(x, n)` is a letter in `r`, assuming that `(x, 1)` is a letter in `r`. -/ @[inline] def min_max_subscript (x : ι) (r : free_group (ι × C∞)) : C∞ × C∞ := r.to_list.foldl (λ minmax i, if i.1.1 = x then (min i.1.2 minmax.1, max i.1.2 minmax.2) else minmax) 1
714de9c581526e7de0935b55a8b468a151cdb375
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/ring_theory/subsemiring/basic.lean
a3226669e9c8ae2b94047b34a5bc897b99bb9d19
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
35,059
lean
/- Copyright (c) 2020 Yury Kudryashov All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import algebra.ring.prod import algebra.module.basic import group_theory.submonoid.membership import group_theory.submonoid.center import data.set.finite import data.equiv.ring /-! # Bundled subsemirings We define bundled subsemirings and some standard constructions: `complete_lattice` structure, `subtype` and `inclusion` ring homomorphisms, subsemiring `map`, `comap` and range (`srange`) of a `ring_hom` etc. -/ open_locale big_operators universes u v w variables {R : Type u} {S : Type v} {T : Type w} [non_assoc_semiring R] [non_assoc_semiring S] [non_assoc_semiring T] (M : submonoid R) set_option old_structure_cmd true /-- A subsemiring of a semiring `R` is a subset `s` that is both a multiplicative and an additive submonoid. -/ structure subsemiring (R : Type u) [non_assoc_semiring R] extends submonoid R, add_submonoid R /-- Reinterpret a `subsemiring` as a `submonoid`. -/ add_decl_doc subsemiring.to_submonoid /-- Reinterpret a `subsemiring` as an `add_submonoid`. -/ add_decl_doc subsemiring.to_add_submonoid namespace subsemiring instance : set_like (subsemiring R) R := ⟨subsemiring.carrier, λ p q h, by cases p; cases q; congr'⟩ @[simp] lemma mem_carrier {s : subsemiring R} {x : R} : x ∈ s.carrier ↔ x ∈ s := iff.rfl /-- Two subsemirings are equal if they have the same elements. -/ @[ext] theorem ext {S T : subsemiring R} (h : ∀ x, x ∈ S ↔ x ∈ T) : S = T := set_like.ext h /-- Copy of a subsemiring with a new `carrier` equal to the old one. Useful to fix definitional equalities.-/ protected def copy (S : subsemiring R) (s : set R) (hs : s = ↑S) : subsemiring R := { carrier := s, ..S.to_add_submonoid.copy s hs, ..S.to_submonoid.copy s hs } @[simp] lemma coe_copy (S : subsemiring R) (s : set R) (hs : s = ↑S) : (S.copy s hs : set R) = s := rfl lemma copy_eq (S : subsemiring R) (s : set R) (hs : s = ↑S) : S.copy s hs = S := set_like.coe_injective hs lemma to_submonoid_injective : function.injective (to_submonoid : subsemiring R → submonoid R) | r s h := ext (set_like.ext_iff.mp h : _) @[mono] lemma to_submonoid_strict_mono : strict_mono (to_submonoid : subsemiring R → submonoid R) := λ _ _, id @[mono] lemma to_submonoid_mono : monotone (to_submonoid : subsemiring R → submonoid R) := to_submonoid_strict_mono.monotone lemma to_add_submonoid_injective : function.injective (to_add_submonoid : subsemiring R → add_submonoid R) | r s h := ext (set_like.ext_iff.mp h : _) @[mono] lemma to_add_submonoid_strict_mono : strict_mono (to_add_submonoid : subsemiring R → add_submonoid R) := λ _ _, id @[mono] lemma to_add_submonoid_mono : monotone (to_add_submonoid : subsemiring R → add_submonoid R) := to_add_submonoid_strict_mono.monotone /-- Construct a `subsemiring R` from a set `s`, a submonoid `sm`, and an additive submonoid `sa` such that `x ∈ s ↔ x ∈ sm ↔ x ∈ sa`. -/ protected def mk' (s : set R) (sm : submonoid R) (hm : ↑sm = s) (sa : add_submonoid R) (ha : ↑sa = s) : subsemiring R := { carrier := s, zero_mem' := ha ▸ sa.zero_mem, one_mem' := hm ▸ sm.one_mem, add_mem' := λ x y, by simpa only [← ha] using sa.add_mem, mul_mem' := λ x y, by simpa only [← hm] using sm.mul_mem } @[simp] lemma coe_mk' {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_submonoid R} (ha : ↑sa = s) : (subsemiring.mk' s sm hm sa ha : set R) = s := rfl @[simp] lemma mem_mk' {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_submonoid R} (ha : ↑sa = s) {x : R} : x ∈ subsemiring.mk' s sm hm sa ha ↔ x ∈ s := iff.rfl @[simp] lemma mk'_to_submonoid {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_submonoid R} (ha : ↑sa = s) : (subsemiring.mk' s sm hm sa ha).to_submonoid = sm := set_like.coe_injective hm.symm @[simp] lemma mk'_to_add_submonoid {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_submonoid R} (ha : ↑sa =s) : (subsemiring.mk' s sm hm sa ha).to_add_submonoid = sa := set_like.coe_injective ha.symm end subsemiring namespace subsemiring variables (s : subsemiring R) /-- A subsemiring contains the semiring's 1. -/ theorem one_mem : (1 : R) ∈ s := s.one_mem' /-- A subsemiring contains the semiring's 0. -/ theorem zero_mem : (0 : R) ∈ s := s.zero_mem' /-- A subsemiring is closed under multiplication. -/ theorem mul_mem : ∀ {x y : R}, x ∈ s → y ∈ s → x * y ∈ s := s.mul_mem' /-- A subsemiring is closed under addition. -/ theorem add_mem : ∀ {x y : R}, x ∈ s → y ∈ s → x + y ∈ s := s.add_mem' /-- Product of a list of elements in a `subsemiring` is in the `subsemiring`. -/ lemma list_prod_mem {R : Type*} [semiring R] (s : subsemiring R) {l : list R} : (∀x ∈ l, x ∈ s) → l.prod ∈ s := s.to_submonoid.list_prod_mem /-- Sum of a list of elements in a `subsemiring` is in the `subsemiring`. -/ lemma list_sum_mem {l : list R} : (∀x ∈ l, x ∈ s) → l.sum ∈ s := s.to_add_submonoid.list_sum_mem /-- Product of a multiset of elements in a `subsemiring` of a `comm_semiring` is in the `subsemiring`. -/ lemma multiset_prod_mem {R} [comm_semiring R] (s : subsemiring R) (m : multiset R) : (∀a ∈ m, a ∈ s) → m.prod ∈ s := s.to_submonoid.multiset_prod_mem m /-- Sum of a multiset of elements in a `subsemiring` of a `semiring` is in the `add_subsemiring`. -/ lemma multiset_sum_mem (m : multiset R) : (∀a ∈ m, a ∈ s) → m.sum ∈ s := s.to_add_submonoid.multiset_sum_mem m /-- Product of elements of a subsemiring of a `comm_semiring` indexed by a `finset` is in the subsemiring. -/ lemma prod_mem {R : Type*} [comm_semiring R] (s : subsemiring R) {ι : Type*} {t : finset ι} {f : ι → R} (h : ∀c ∈ t, f c ∈ s) : ∏ i in t, f i ∈ s := s.to_submonoid.prod_mem h /-- Sum of elements in an `subsemiring` of an `semiring` indexed by a `finset` is in the `add_subsemiring`. -/ lemma sum_mem (s : subsemiring R) {ι : Type*} {t : finset ι} {f : ι → R} (h : ∀c ∈ t, f c ∈ s) : ∑ i in t, f i ∈ s := s.to_add_submonoid.sum_mem h lemma pow_mem {R : Type*} [semiring R] (s : subsemiring R) {x : R} (hx : x ∈ s) (n : ℕ) : x^n ∈ s := s.to_submonoid.pow_mem hx n lemma nsmul_mem {x : R} (hx : x ∈ s) (n : ℕ) : n • x ∈ s := s.to_add_submonoid.nsmul_mem hx n lemma coe_nat_mem (n : ℕ) : (n : R) ∈ s := by simp only [← nsmul_one, nsmul_mem, one_mem] /-- A subsemiring of a `non_assoc_semiring` inherits a `non_assoc_semiring` structure -/ instance to_non_assoc_semiring : non_assoc_semiring s := { mul_zero := λ x, subtype.eq $ mul_zero x, zero_mul := λ x, subtype.eq $ zero_mul x, right_distrib := λ x y z, subtype.eq $ right_distrib x y z, left_distrib := λ x y z, subtype.eq $ left_distrib x y z, .. s.to_submonoid.to_mul_one_class, .. s.to_add_submonoid.to_add_comm_monoid } @[simp, norm_cast] lemma coe_one : ((1 : s) : R) = (1 : R) := rfl @[simp, norm_cast] lemma coe_zero : ((0 : s) : R) = (0 : R) := rfl @[simp, norm_cast] lemma coe_add (x y : s) : ((x + y : s) : R) = (x + y : R) := rfl @[simp, norm_cast] lemma coe_mul (x y : s) : ((x * y : s) : R) = (x * y : R) := rfl instance nontrivial [nontrivial R] : nontrivial s := nontrivial_of_ne 0 1 $ λ H, zero_ne_one (congr_arg subtype.val H) instance no_zero_divisors [no_zero_divisors R] : no_zero_divisors s := { eq_zero_or_eq_zero_of_mul_eq_zero := λ x y h, or.cases_on (eq_zero_or_eq_zero_of_mul_eq_zero $ subtype.ext_iff.mp h) (λ h, or.inl $ subtype.eq h) (λ h, or.inr $ subtype.eq h) } /-- A subsemiring of a `semiring` is a `semiring`. -/ instance to_semiring {R} [semiring R] (s : subsemiring R) : semiring s := { ..s.to_non_assoc_semiring, ..s.to_submonoid.to_monoid } @[simp, norm_cast] lemma coe_pow {R} [semiring R] (s : subsemiring R) (x : s) (n : ℕ) : ((x^n : s) : R) = (x^n : R) := begin induction n with n ih, { simp, }, { simp [pow_succ, ih], }, end /-- A subsemiring of a `comm_semiring` is a `comm_semiring`. -/ instance to_comm_semiring {R} [comm_semiring R] (s : subsemiring R) : comm_semiring s := { mul_comm := λ _ _, subtype.eq $ mul_comm _ _, ..s.to_semiring} /-- The natural ring hom from a subsemiring of semiring `R` to `R`. -/ def subtype : s →+* R := { to_fun := coe, .. s.to_submonoid.subtype, .. s.to_add_submonoid.subtype } @[simp] theorem coe_subtype : ⇑s.subtype = coe := rfl /-- A subsemiring of an `ordered_semiring` is an `ordered_semiring`. -/ instance to_ordered_semiring {R} [ordered_semiring R] (s : subsemiring R) : ordered_semiring s := subtype.coe_injective.ordered_semiring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) /-- A subsemiring of an `ordered_comm_semiring` is an `ordered_comm_semiring`. -/ instance to_ordered_comm_semiring {R} [ordered_comm_semiring R] (s : subsemiring R) : ordered_comm_semiring s := subtype.coe_injective.ordered_comm_semiring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) /-- A subsemiring of a `linear_ordered_semiring` is a `linear_ordered_semiring`. -/ instance to_linear_ordered_semiring {R} [linear_ordered_semiring R] (s : subsemiring R) : linear_ordered_semiring s := subtype.coe_injective.linear_ordered_semiring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) /-! Note: currently, there is no `linear_ordered_comm_semiring`. -/ @[simp] lemma mem_to_submonoid {s : subsemiring R} {x : R} : x ∈ s.to_submonoid ↔ x ∈ s := iff.rfl @[simp] lemma coe_to_submonoid (s : subsemiring R) : (s.to_submonoid : set R) = s := rfl @[simp] lemma mem_to_add_submonoid {s : subsemiring R} {x : R} : x ∈ s.to_add_submonoid ↔ x ∈ s := iff.rfl @[simp] lemma coe_to_add_submonoid (s : subsemiring R) : (s.to_add_submonoid : set R) = s := rfl /-- The subsemiring `R` of the semiring `R`. -/ instance : has_top (subsemiring R) := ⟨{ .. (⊤ : submonoid R), .. (⊤ : add_submonoid R) }⟩ @[simp] lemma mem_top (x : R) : x ∈ (⊤ : subsemiring R) := set.mem_univ x @[simp] lemma coe_top : ((⊤ : subsemiring R) : set R) = set.univ := rfl /-- The preimage of a subsemiring along a ring homomorphism is a subsemiring. -/ def comap (f : R →+* S) (s : subsemiring S) : subsemiring R := { carrier := f ⁻¹' s, .. s.to_submonoid.comap (f : R →* S), .. s.to_add_submonoid.comap (f : R →+ S) } @[simp] lemma coe_comap (s : subsemiring S) (f : R →+* S) : (s.comap f : set R) = f ⁻¹' s := rfl @[simp] lemma mem_comap {s : subsemiring S} {f : R →+* S} {x : R} : x ∈ s.comap f ↔ f x ∈ s := iff.rfl lemma comap_comap (s : subsemiring T) (g : S →+* T) (f : R →+* S) : (s.comap g).comap f = s.comap (g.comp f) := rfl /-- The image of a subsemiring along a ring homomorphism is a subsemiring. -/ def map (f : R →+* S) (s : subsemiring R) : subsemiring S := { carrier := f '' s, .. s.to_submonoid.map (f : R →* S), .. s.to_add_submonoid.map (f : R →+ S) } @[simp] lemma coe_map (f : R →+* S) (s : subsemiring R) : (s.map f : set S) = f '' s := rfl @[simp] lemma mem_map {f : R →+* S} {s : subsemiring R} {y : S} : y ∈ s.map f ↔ ∃ x ∈ s, f x = y := set.mem_image_iff_bex @[simp] lemma map_id : s.map (ring_hom.id R) = s := set_like.coe_injective $ set.image_id _ lemma map_map (g : S →+* T) (f : R →+* S) : (s.map f).map g = s.map (g.comp f) := set_like.coe_injective $ set.image_image _ _ _ lemma map_le_iff_le_comap {f : R →+* S} {s : subsemiring R} {t : subsemiring S} : s.map f ≤ t ↔ s ≤ t.comap f := set.image_subset_iff lemma gc_map_comap (f : R →+* S) : galois_connection (map f) (comap f) := λ S T, map_le_iff_le_comap /-- A subsemiring is isomorphic to its image under an injective function -/ noncomputable def equiv_map_of_injective (f : R →+* S) (hf : function.injective f) : s ≃+* s.map f := { map_mul' := λ _ _, subtype.ext (f.map_mul _ _), map_add' := λ _ _, subtype.ext (f.map_add _ _), ..equiv.set.image f s hf } @[simp] lemma coe_equiv_map_of_injective_apply (f : R →+* S) (hf : function.injective f) (x : s) : (equiv_map_of_injective s f hf x : S) = f x := rfl end subsemiring namespace ring_hom variables (g : S →+* T) (f : R →+* S) /-- The range of a ring homomorphism is a subsemiring. See Note [range copy pattern]. -/ def srange : subsemiring S := ((⊤ : subsemiring R).map f).copy (set.range f) set.image_univ.symm @[simp] lemma coe_srange : (f.srange : set S) = set.range f := rfl @[simp] lemma mem_srange {f : R →+* S} {y : S} : y ∈ f.srange ↔ ∃ x, f x = y := iff.rfl lemma srange_eq_map (f : R →+* S) : f.srange = (⊤ : subsemiring R).map f := by { ext, simp } lemma mem_srange_self (f : R →+* S) (x : R) : f x ∈ f.srange := mem_srange.mpr ⟨x, rfl⟩ lemma map_srange : f.srange.map g = (g.comp f).srange := by simpa only [srange_eq_map] using (⊤ : subsemiring R).map_map g f /-- The range of a morphism of semirings is a fintype, if the domain is a fintype. Note: this instance can form a diamond with `subtype.fintype` in the presence of `fintype S`.-/ instance fintype_srange [fintype R] [decidable_eq S] (f : R →+* S) : fintype (srange f) := set.fintype_range f end ring_hom namespace subsemiring instance : has_bot (subsemiring R) := ⟨(nat.cast_ring_hom R).srange⟩ instance : inhabited (subsemiring R) := ⟨⊥⟩ lemma coe_bot : ((⊥ : subsemiring R) : set R) = set.range (coe : ℕ → R) := (nat.cast_ring_hom R).coe_srange lemma mem_bot {x : R} : x ∈ (⊥ : subsemiring R) ↔ ∃ n : ℕ, ↑n=x := ring_hom.mem_srange /-- The inf of two subsemirings is their intersection. -/ instance : has_inf (subsemiring R) := ⟨λ s t, { carrier := s ∩ t, .. s.to_submonoid ⊓ t.to_submonoid, .. s.to_add_submonoid ⊓ t.to_add_submonoid }⟩ @[simp] lemma coe_inf (p p' : subsemiring R) : ((p ⊓ p' : subsemiring R) : set R) = p ∩ p' := rfl @[simp] lemma mem_inf {p p' : subsemiring R} {x : R} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl instance : has_Inf (subsemiring R) := ⟨λ s, subsemiring.mk' (⋂ t ∈ s, ↑t) (⨅ t ∈ s, subsemiring.to_submonoid t) (by simp) (⨅ t ∈ s, subsemiring.to_add_submonoid t) (by simp)⟩ @[simp, norm_cast] lemma coe_Inf (S : set (subsemiring R)) : ((Inf S : subsemiring R) : set R) = ⋂ s ∈ S, ↑s := rfl lemma mem_Inf {S : set (subsemiring R)} {x : R} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p := set.mem_Inter₂ @[simp] lemma Inf_to_submonoid (s : set (subsemiring R)) : (Inf s).to_submonoid = ⨅ t ∈ s, subsemiring.to_submonoid t := mk'_to_submonoid _ _ @[simp] lemma Inf_to_add_submonoid (s : set (subsemiring R)) : (Inf s).to_add_submonoid = ⨅ t ∈ s, subsemiring.to_add_submonoid t := mk'_to_add_submonoid _ _ /-- Subsemirings of a semiring form a complete lattice. -/ instance : complete_lattice (subsemiring R) := { bot := (⊥), bot_le := λ s x hx, let ⟨n, hn⟩ := mem_bot.1 hx in hn ▸ s.coe_nat_mem n, top := (⊤), le_top := λ s x hx, trivial, inf := (⊓), inf_le_left := λ s t x, and.left, inf_le_right := λ s t x, and.right, le_inf := λ s t₁ t₂ h₁ h₂ x hx, ⟨h₁ hx, h₂ hx⟩, .. complete_lattice_of_Inf (subsemiring R) (λ s, is_glb.of_image (λ s t, show (s : set R) ≤ t ↔ s ≤ t, from set_like.coe_subset_coe) is_glb_binfi)} lemma eq_top_iff' (A : subsemiring R) : A = ⊤ ↔ ∀ x : R, x ∈ A := eq_top_iff.trans ⟨λ h m, h $ mem_top m, λ h m _, h m⟩ section /-- The center of a semiring `R` is the set of elements that commute with everything in `R` -/ def center (R) [semiring R] : subsemiring R := { carrier := set.center R, zero_mem' := set.zero_mem_center R, add_mem' := λ a b, set.add_mem_center, .. submonoid.center R } lemma coe_center (R) [semiring R] : ↑(center R) = set.center R := rfl @[simp] lemma center_to_submonoid (R) [semiring R] : (center R).to_submonoid = submonoid.center R := rfl lemma mem_center_iff {R} [semiring R] {z : R} : z ∈ center R ↔ ∀ g, g * z = z * g := iff.rfl instance decidable_mem_center {R} [semiring R] [decidable_eq R] [fintype R] : decidable_pred (∈ center R) := λ _, decidable_of_iff' _ mem_center_iff @[simp] lemma center_eq_top (R) [comm_semiring R] : center R = ⊤ := set_like.coe_injective (set.center_eq_univ R) /-- The center is commutative. -/ instance {R} [semiring R] : comm_semiring (center R) := { ..submonoid.center.comm_monoid, ..(center R).to_semiring} end /-- The `subsemiring` generated by a set. -/ def closure (s : set R) : subsemiring R := Inf {S | s ⊆ S} lemma mem_closure {x : R} {s : set R} : x ∈ closure s ↔ ∀ S : subsemiring R, s ⊆ S → x ∈ S := mem_Inf /-- The subsemiring generated by a set includes the set. -/ @[simp] lemma subset_closure {s : set R} : s ⊆ closure s := λ x hx, mem_closure.2 $ λ S hS, hS hx lemma not_mem_of_not_mem_closure {s : set R} {P : R} (hP : P ∉ closure s) : P ∉ s := λ h, hP (subset_closure h) /-- A subsemiring `S` includes `closure s` if and only if it includes `s`. -/ @[simp] lemma closure_le {s : set R} {t : subsemiring R} : closure s ≤ t ↔ s ⊆ t := ⟨set.subset.trans subset_closure, λ h, Inf_le h⟩ /-- Subsemiring closure of a set is monotone in its argument: if `s ⊆ t`, then `closure s ≤ closure t`. -/ lemma closure_mono ⦃s t : set R⦄ (h : s ⊆ t) : closure s ≤ closure t := closure_le.2 $ set.subset.trans h subset_closure lemma closure_eq_of_le {s : set R} {t : subsemiring R} (h₁ : s ⊆ t) (h₂ : t ≤ closure s) : closure s = t := le_antisymm (closure_le.2 h₁) h₂ lemma mem_map_equiv {f : R ≃+* S} {K : subsemiring R} {x : S} : x ∈ K.map (f : R →+* S) ↔ f.symm x ∈ K := @set.mem_image_equiv _ _ ↑K f.to_equiv x lemma map_equiv_eq_comap_symm (f : R ≃+* S) (K : subsemiring R) : K.map (f : R →+* S) = K.comap f.symm := set_like.coe_injective (f.to_equiv.image_eq_preimage K) lemma comap_equiv_eq_map_symm (f : R ≃+* S) (K : subsemiring S) : K.comap (f : R →+* S) = K.map f.symm := (map_equiv_eq_comap_symm f.symm K).symm end subsemiring namespace submonoid /-- The additive closure of a submonoid is a subsemiring. -/ def subsemiring_closure (M : submonoid R) : subsemiring R := { one_mem' := add_submonoid.mem_closure.mpr (λ y hy, hy M.one_mem), mul_mem' := λ x y, M.mul_mem_add_closure, ..add_submonoid.closure (M : set R)} lemma subsemiring_closure_coe : (M.subsemiring_closure : set R) = add_submonoid.closure (M : set R) := rfl lemma subsemiring_closure_to_add_submonoid : M.subsemiring_closure.to_add_submonoid = add_submonoid.closure (M : set R) := rfl /-- The `subsemiring` generated by a multiplicative submonoid coincides with the `subsemiring.closure` of the submonoid itself . -/ lemma subsemiring_closure_eq_closure : M.subsemiring_closure = subsemiring.closure (M : set R) := begin ext, refine ⟨λ hx, _, λ hx, (subsemiring.mem_closure.mp hx) M.subsemiring_closure (λ s sM, _)⟩; rintros - ⟨H1, rfl⟩; rintros - ⟨H2, rfl⟩, { exact add_submonoid.mem_closure.mp hx H1.to_add_submonoid H2 }, { exact H2 sM } end end submonoid namespace subsemiring @[simp] lemma closure_submonoid_closure (s : set R) : closure ↑(submonoid.closure s) = closure s := le_antisymm (closure_le.mpr (λ y hy, (submonoid.mem_closure.mp hy) (closure s).to_submonoid subset_closure)) (closure_mono (submonoid.subset_closure)) /-- The elements of the subsemiring closure of `M` are exactly the elements of the additive closure of a multiplicative submonoid `M`. -/ lemma coe_closure_eq (s : set R) : (closure s : set R) = add_submonoid.closure (submonoid.closure s : set R) := by simp [← submonoid.subsemiring_closure_to_add_submonoid, submonoid.subsemiring_closure_eq_closure] lemma mem_closure_iff {s : set R} {x} : x ∈ closure s ↔ x ∈ add_submonoid.closure (submonoid.closure s : set R) := set.ext_iff.mp (coe_closure_eq s) x @[simp] lemma closure_add_submonoid_closure {s : set R} : closure ↑(add_submonoid.closure s) = closure s := begin ext x, refine ⟨λ hx, _, λ hx, closure_mono add_submonoid.subset_closure hx⟩, rintros - ⟨H, rfl⟩, rintros - ⟨J, rfl⟩, refine (add_submonoid.mem_closure.mp (mem_closure_iff.mp hx)) H.to_add_submonoid (λ y hy, _), refine (submonoid.mem_closure.mp hy) H.to_submonoid (λ z hz, _), exact (add_submonoid.mem_closure.mp hz) H.to_add_submonoid (λ w hw, J hw), end /-- An induction principle for closure membership. If `p` holds for `0`, `1`, and all elements of `s`, and is preserved under addition and multiplication, then `p` holds for all elements of the closure of `s`. -/ @[elab_as_eliminator] lemma closure_induction {s : set R} {p : R → Prop} {x} (h : x ∈ closure s) (Hs : ∀ x ∈ s, p x) (H0 : p 0) (H1 : p 1) (Hadd : ∀ x y, p x → p y → p (x + y)) (Hmul : ∀ x y, p x → p y → p (x * y)) : p x := (@closure_le _ _ _ ⟨p, H1, Hmul, H0, Hadd⟩).2 Hs h lemma mem_closure_iff_exists_list {R} [semiring R] {s : set R} {x} : x ∈ closure s ↔ ∃ L : list (list R), (∀ t ∈ L, ∀ y ∈ t, y ∈ s) ∧ (L.map list.prod).sum = x := ⟨λ hx, add_submonoid.closure_induction (mem_closure_iff.1 hx) (λ x hx, suffices ∃ t : list R, (∀ y ∈ t, y ∈ s) ∧ t.prod = x, from let ⟨t, ht1, ht2⟩ := this in ⟨[t], list.forall_mem_singleton.2 ht1, by rw [list.map_singleton, list.sum_singleton, ht2]⟩, submonoid.closure_induction hx (λ x hx, ⟨[x], list.forall_mem_singleton.2 hx, one_mul x⟩) ⟨[], list.forall_mem_nil _, rfl⟩ (λ x y ⟨t, ht1, ht2⟩ ⟨u, hu1, hu2⟩, ⟨t ++ u, list.forall_mem_append.2 ⟨ht1, hu1⟩, by rw [list.prod_append, ht2, hu2]⟩)) ⟨[], list.forall_mem_nil _, rfl⟩ (λ x y ⟨L, HL1, HL2⟩ ⟨M, HM1, HM2⟩, ⟨L ++ M, list.forall_mem_append.2 ⟨HL1, HM1⟩, by rw [list.map_append, list.sum_append, HL2, HM2]⟩), λ ⟨L, HL1, HL2⟩, HL2 ▸ list_sum_mem _ (λ r hr, let ⟨t, ht1, ht2⟩ := list.mem_map.1 hr in ht2 ▸ list_prod_mem _ (λ y hy, subset_closure $ HL1 t ht1 y hy))⟩ variable (R) /-- `closure` forms a Galois insertion with the coercion to set. -/ protected def gi : galois_insertion (@closure R _) coe := { choice := λ s _, closure s, gc := λ s t, closure_le, le_l_u := λ s, subset_closure, choice_eq := λ s h, rfl } variable {R} /-- Closure of a subsemiring `S` equals `S`. -/ lemma closure_eq (s : subsemiring R) : closure (s : set R) = s := (subsemiring.gi R).l_u_eq s @[simp] lemma closure_empty : closure (∅ : set R) = ⊥ := (subsemiring.gi R).gc.l_bot @[simp] lemma closure_univ : closure (set.univ : set R) = ⊤ := @coe_top R _ ▸ closure_eq ⊤ lemma closure_union (s t : set R) : closure (s ∪ t) = closure s ⊔ closure t := (subsemiring.gi R).gc.l_sup lemma closure_Union {ι} (s : ι → set R) : closure (⋃ i, s i) = ⨆ i, closure (s i) := (subsemiring.gi R).gc.l_supr lemma closure_sUnion (s : set (set R)) : closure (⋃₀ s) = ⨆ t ∈ s, closure t := (subsemiring.gi R).gc.l_Sup lemma map_sup (s t : subsemiring R) (f : R →+* S) : (s ⊔ t).map f = s.map f ⊔ t.map f := (gc_map_comap f).l_sup lemma map_supr {ι : Sort*} (f : R →+* S) (s : ι → subsemiring R) : (supr s).map f = ⨆ i, (s i).map f := (gc_map_comap f).l_supr lemma comap_inf (s t : subsemiring S) (f : R →+* S) : (s ⊓ t).comap f = s.comap f ⊓ t.comap f := (gc_map_comap f).u_inf lemma comap_infi {ι : Sort*} (f : R →+* S) (s : ι → subsemiring S) : (infi s).comap f = ⨅ i, (s i).comap f := (gc_map_comap f).u_infi @[simp] lemma map_bot (f : R →+* S) : (⊥ : subsemiring R).map f = ⊥ := (gc_map_comap f).l_bot @[simp] lemma comap_top (f : R →+* S) : (⊤ : subsemiring S).comap f = ⊤ := (gc_map_comap f).u_top /-- Given `subsemiring`s `s`, `t` of semirings `R`, `S` respectively, `s.prod t` is `s × t` as a subsemiring of `R × S`. -/ def prod (s : subsemiring R) (t : subsemiring S) : subsemiring (R × S) := { carrier := (s : set R) ×ˢ (t : set S), .. s.to_submonoid.prod t.to_submonoid, .. s.to_add_submonoid.prod t.to_add_submonoid} @[norm_cast] lemma coe_prod (s : subsemiring R) (t : subsemiring S) : (s.prod t : set (R × S)) = (s : set R) ×ˢ (t : set S) := rfl lemma mem_prod {s : subsemiring R} {t : subsemiring S} {p : R × S} : p ∈ s.prod t ↔ p.1 ∈ s ∧ p.2 ∈ t := iff.rfl @[mono] lemma prod_mono ⦃s₁ s₂ : subsemiring R⦄ (hs : s₁ ≤ s₂) ⦃t₁ t₂ : subsemiring S⦄ (ht : t₁ ≤ t₂) : s₁.prod t₁ ≤ s₂.prod t₂ := set.prod_mono hs ht lemma prod_mono_right (s : subsemiring R) : monotone (λ t : subsemiring S, s.prod t) := prod_mono (le_refl s) lemma prod_mono_left (t : subsemiring S) : monotone (λ s : subsemiring R, s.prod t) := λ s₁ s₂ hs, prod_mono hs (le_refl t) lemma prod_top (s : subsemiring R) : s.prod (⊤ : subsemiring S) = s.comap (ring_hom.fst R S) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_fst] lemma top_prod (s : subsemiring S) : (⊤ : subsemiring R).prod s = s.comap (ring_hom.snd R S) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_snd] @[simp] lemma top_prod_top : (⊤ : subsemiring R).prod (⊤ : subsemiring S) = ⊤ := (top_prod _).trans $ comap_top _ /-- Product of subsemirings is isomorphic to their product as monoids. -/ def prod_equiv (s : subsemiring R) (t : subsemiring S) : s.prod t ≃+* s × t := { map_mul' := λ x y, rfl, map_add' := λ x y, rfl, .. equiv.set.prod ↑s ↑t } lemma mem_supr_of_directed {ι} [hι : nonempty ι] {S : ι → subsemiring R} (hS : directed (≤) S) {x : R} : x ∈ (⨆ i, S i) ↔ ∃ i, x ∈ S i := begin refine ⟨_, λ ⟨i, hi⟩, (set_like.le_def.1 $ le_supr S i) hi⟩, let U : subsemiring R := subsemiring.mk' (⋃ i, (S i : set R)) (⨆ i, (S i).to_submonoid) (submonoid.coe_supr_of_directed $ hS.mono_comp _ (λ _ _, id)) (⨆ i, (S i).to_add_submonoid) (add_submonoid.coe_supr_of_directed $ hS.mono_comp _ (λ _ _, id)), suffices : (⨆ i, S i) ≤ U, by simpa using @this x, exact supr_le (λ i x hx, set.mem_Union.2 ⟨i, hx⟩), end lemma coe_supr_of_directed {ι} [hι : nonempty ι] {S : ι → subsemiring R} (hS : directed (≤) S) : ((⨆ i, S i : subsemiring R) : set R) = ⋃ i, ↑(S i) := set.ext $ λ x, by simp [mem_supr_of_directed hS] lemma mem_Sup_of_directed_on {S : set (subsemiring R)} (Sne : S.nonempty) (hS : directed_on (≤) S) {x : R} : x ∈ Sup S ↔ ∃ s ∈ S, x ∈ s := begin haveI : nonempty S := Sne.to_subtype, simp only [Sup_eq_supr', mem_supr_of_directed hS.directed_coe, set_coe.exists, subtype.coe_mk] end lemma coe_Sup_of_directed_on {S : set (subsemiring R)} (Sne : S.nonempty) (hS : directed_on (≤) S) : (↑(Sup S) : set R) = ⋃ s ∈ S, ↑s := set.ext $ λ x, by simp [mem_Sup_of_directed_on Sne hS] end subsemiring namespace ring_hom variables [non_assoc_semiring T] {s : subsemiring R} open subsemiring /-- Restriction of a ring homomorphism to a subsemiring of the domain. -/ def srestrict (f : R →+* S) (s : subsemiring R) : s →+* S := f.comp s.subtype @[simp] lemma srestrict_apply (f : R →+* S) (x : s) : f.srestrict s x = f x := rfl /-- Restriction of a ring homomorphism to a subsemiring of the codomain. -/ def cod_srestrict (f : R →+* S) (s : subsemiring S) (h : ∀ x, f x ∈ s) : R →+* s := { to_fun := λ n, ⟨f n, h n⟩, .. (f : R →* S).cod_mrestrict s.to_submonoid h, .. (f : R →+ S).cod_mrestrict s.to_add_submonoid h } /-- Restriction of a ring homomorphism to its range interpreted as a subsemiring. This is the bundled version of `set.range_factorization`. -/ def srange_restrict (f : R →+* S) : R →+* f.srange := f.cod_srestrict f.srange f.mem_srange_self @[simp] lemma coe_srange_restrict (f : R →+* S) (x : R) : (f.srange_restrict x : S) = f x := rfl lemma srange_restrict_surjective (f : R →+* S) : function.surjective f.srange_restrict := λ ⟨y, hy⟩, let ⟨x, hx⟩ := mem_srange.mp hy in ⟨x, subtype.ext hx⟩ lemma srange_top_iff_surjective {f : R →+* S} : f.srange = (⊤ : subsemiring S) ↔ function.surjective f := set_like.ext'_iff.trans $ iff.trans (by rw [coe_srange, coe_top]) set.range_iff_surjective /-- The range of a surjective ring homomorphism is the whole of the codomain. -/ lemma srange_top_of_surjective (f : R →+* S) (hf : function.surjective f) : f.srange = (⊤ : subsemiring S) := srange_top_iff_surjective.2 hf /-- The subsemiring of elements `x : R` such that `f x = g x` -/ def eq_slocus (f g : R →+* S) : subsemiring R := { carrier := {x | f x = g x}, .. (f : R →* S).eq_mlocus g, .. (f : R →+ S).eq_mlocus g } /-- If two ring homomorphisms are equal on a set, then they are equal on its subsemiring closure. -/ lemma eq_on_sclosure {f g : R →+* S} {s : set R} (h : set.eq_on f g s) : set.eq_on f g (closure s) := show closure s ≤ f.eq_slocus g, from closure_le.2 h lemma eq_of_eq_on_stop {f g : R →+* S} (h : set.eq_on f g (⊤ : subsemiring R)) : f = g := ext $ λ x, h trivial lemma eq_of_eq_on_sdense {s : set R} (hs : closure s = ⊤) {f g : R →+* S} (h : s.eq_on f g) : f = g := eq_of_eq_on_stop $ hs ▸ eq_on_sclosure h lemma sclosure_preimage_le (f : R →+* S) (s : set S) : closure (f ⁻¹' s) ≤ (closure s).comap f := closure_le.2 $ λ x hx, set_like.mem_coe.2 $ mem_comap.2 $ subset_closure hx /-- The image under a ring homomorphism of the subsemiring generated by a set equals the subsemiring generated by the image of the set. -/ lemma map_sclosure (f : R →+* S) (s : set R) : (closure s).map f = closure (f '' s) := le_antisymm (map_le_iff_le_comap.2 $ le_trans (closure_mono $ set.subset_preimage_image _ _) (sclosure_preimage_le _ _)) (closure_le.2 $ set.image_subset _ subset_closure) end ring_hom namespace subsemiring open ring_hom /-- The ring homomorphism associated to an inclusion of subsemirings. -/ def inclusion {S T : subsemiring R} (h : S ≤ T) : S →* T := S.subtype.cod_srestrict _ (λ x, h x.2) @[simp] lemma srange_subtype (s : subsemiring R) : s.subtype.srange = s := set_like.coe_injective $ (coe_srange _).trans subtype.range_coe @[simp] lemma range_fst : (fst R S).srange = ⊤ := (fst R S).srange_top_of_surjective $ prod.fst_surjective @[simp] lemma range_snd : (snd R S).srange = ⊤ := (snd R S).srange_top_of_surjective $ prod.snd_surjective @[simp] lemma prod_bot_sup_bot_prod (s : subsemiring R) (t : subsemiring S) : (s.prod ⊥) ⊔ (prod ⊥ t) = s.prod t := le_antisymm (sup_le (prod_mono_right s bot_le) (prod_mono_left t bot_le)) $ assume p hp, prod.fst_mul_snd p ▸ mul_mem _ ((le_sup_left : s.prod ⊥ ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨hp.1, set_like.mem_coe.2 $ one_mem ⊥⟩) ((le_sup_right : prod ⊥ t ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨set_like.mem_coe.2 $ one_mem ⊥, hp.2⟩) end subsemiring namespace ring_equiv variables {s t : subsemiring R} /-- Makes the identity isomorphism from a proof two subsemirings of a multiplicative monoid are equal. -/ def subsemiring_congr (h : s = t) : s ≃+* t := { map_mul' := λ _ _, rfl, map_add' := λ _ _, rfl, ..equiv.set_congr $ congr_arg _ h } /-- Restrict a ring homomorphism with a left inverse to a ring isomorphism to its `ring_hom.srange`. -/ def sof_left_inverse {g : S → R} {f : R →+* S} (h : function.left_inverse g f) : R ≃+* f.srange := { to_fun := λ x, f.srange_restrict x, inv_fun := λ x, (g ∘ f.srange.subtype) x, left_inv := h, right_inv := λ x, subtype.ext $ let ⟨x', hx'⟩ := ring_hom.mem_srange.mp x.prop in show f (g x) = x, by rw [←hx', h x'], ..f.srange_restrict } @[simp] lemma sof_left_inverse_apply {g : S → R} {f : R →+* S} (h : function.left_inverse g f) (x : R) : ↑(sof_left_inverse h x) = f x := rfl @[simp] lemma sof_left_inverse_symm_apply {g : S → R} {f : R →+* S} (h : function.left_inverse g f) (x : f.srange) : (sof_left_inverse h).symm x = g x := rfl /-- Given an equivalence `e : R ≃+* S` of semirings and a subsemiring `s` of `R`, `subsemiring_map e s` is the induced equivalence between `s` and `s.map e` -/ @[simps] def subsemiring_map (e : R ≃+* S) (s : subsemiring R) : s ≃+* s.map e.to_ring_hom := { ..e.to_add_equiv.add_submonoid_map s.to_add_submonoid, ..e.to_mul_equiv.submonoid_map s.to_submonoid } end ring_equiv /-! ### Actions by `subsemiring`s These are just copies of the definitions about `submonoid` starting from `submonoid.mul_action`. The only new result is `subsemiring.module`. When `R` is commutative, `algebra.of_subsemiring` provides a stronger result than those found in this file, which uses the same scalar action. -/ section actions namespace subsemiring variables {R' α β : Type*} [semiring R'] /-- The action by a subsemiring is the action by the underlying semiring. -/ instance [mul_action R' α] (S : subsemiring R') : mul_action S α := S.to_submonoid.mul_action lemma smul_def [mul_action R' α] {S : subsemiring R'} (g : S) (m : α) : g • m = (g : R') • m := rfl instance smul_comm_class_left [mul_action R' β] [has_scalar α β] [smul_comm_class R' α β] (S : subsemiring R') : smul_comm_class S α β := S.to_submonoid.smul_comm_class_left instance smul_comm_class_right [has_scalar α β] [mul_action R' β] [smul_comm_class α R' β] (S : subsemiring R') : smul_comm_class α S β := S.to_submonoid.smul_comm_class_right /-- Note that this provides `is_scalar_tower S R R` which is needed by `smul_mul_assoc`. -/ instance [has_scalar α β] [mul_action R' α] [mul_action R' β] [is_scalar_tower R' α β] (S : subsemiring R') : is_scalar_tower S α β := S.to_submonoid.is_scalar_tower instance [mul_action R' α] [has_faithful_scalar R' α] (S : subsemiring R') : has_faithful_scalar S α := S.to_submonoid.has_faithful_scalar /-- The action by a subsemiring is the action by the underlying semiring. -/ instance [add_monoid α] [distrib_mul_action R' α] (S : subsemiring R') : distrib_mul_action S α := S.to_submonoid.distrib_mul_action /-- The action by a subsemiring is the action by the underlying semiring. -/ instance [monoid α] [mul_distrib_mul_action R' α] (S : subsemiring R') : mul_distrib_mul_action S α := S.to_submonoid.mul_distrib_mul_action /-- The action by a subsemiring is the action by the underlying semiring. -/ instance [add_comm_monoid α] [module R' α] (S : subsemiring R') : module S α := { smul := (•), .. module.comp_hom _ S.subtype } end subsemiring end actions -- While this definition is not about `subsemiring`s, this is the earliest we have -- both `ordered_semiring` and `submonoid` available. /-- Submonoid of positive elements of an ordered semiring. -/ def pos_submonoid (R : Type*) [ordered_semiring R] [nontrivial R] : submonoid R := { carrier := {x | 0 < x}, one_mem' := show (0 : R) < 1, from zero_lt_one, mul_mem' := λ x y (hx : 0 < x) (hy : 0 < y), mul_pos hx hy } @[simp] lemma mem_pos_monoid {R : Type*} [ordered_semiring R] [nontrivial R] (u : Rˣ) : ↑u ∈ pos_submonoid R ↔ (0 : R) < u := iff.rfl
cdf2c5895a3801eaf18fbf098ca4d6e303fa9035
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/rat/default.lean
19df28db197c87795a75dec9046dceb33502f302
[]
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
292
lean
/- Copyright (c) 2019 Kevin Kappelmann. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Kappelmann -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.rat.floor import Mathlib.PostPort namespace Mathlib
bfdf3997f3639439d703038a4e16070d2662c8d8
e7de183433d907275be4926240a8c8ddb5915131
/recipes/plain-lean3.lean
b9148cfe098fc2b855db7a3b9c80850df1bc0e64
[ "MIT" ]
permissive
cpitclaudel/alectryon
70b01086e9b4aee4f18017621578004903ce74d3
11e8cdc8395d66858baa7371b6cf8e827ca38f4a
refs/heads/master
1,683,666,125,135
1,683,473,634,000
1,683,473,844,000
260,735,576
206
29
MIT
1,655,000,579,000
1,588,439,321,000
HTML
UTF-8
Lean
false
false
417
lean
/- To compile: alectryon --frontend lean3 plain-lean3.lean # Lean → HTML; produces ‘plain-lean3.lean.html’ -/ -- Queries: #check nat #check bool -- Proofs: example (p q r : Prop) : p ∧ q ↔ q ∧ p := begin apply iff.intro, intro H, apply and.intro, apply (and.elim_right H), apply (and.elim_left H), intro H, apply and.intro, apply (and.elim_right H), apply (and.elim_left H), end
786c2ad99e911dec94cdb3e94e22e86741a33246
4727251e0cd73359b15b664c3170e5d754078599
/src/topology/sheaves/stalks.lean
8801ae491a9dfd590f011babe8e73a900d6d1fbd
[ "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
24,410
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Justus Springer -/ import topology.category.Top.open_nhds import topology.sheaves.presheaf import topology.sheaves.sheaf_condition.unique_gluing import category_theory.limits.types import category_theory.limits.preserves.filtered import category_theory.limits.final import topology.sober import tactic.elementwise import algebra.category.Ring /-! # Stalks For a presheaf `F` on a topological space `X`, valued in some category `C`, the *stalk* of `F` at the point `x : X` is defined as the colimit of the following functor (nhds x)ᵒᵖ ⥤ (opens X)ᵒᵖ ⥤ C where the functor on the left is the inclusion of categories and the functor on the right is `F`. For an open neighborhood `U` of `x`, we define the map `F.germ x : F.obj (op U) ⟶ F.stalk x` as the canonical morphism into this colimit. Taking stalks is functorial: For every point `x : X` we define a functor `stalk_functor C x`, sending presheaves on `X` to objects of `C`. Furthermore, for a map `f : X ⟶ Y` between topological spaces, we define `stalk_pushforward` as the induced map on the stalks `(f _* ℱ).stalk (f x) ⟶ ℱ.stalk x`. Some lemmas about stalks and germs only hold for certain classes of concrete categories. A basic property of forgetful functors of categories of algebraic structures (like `Mon`, `CommRing`,...) is that they preserve filtered colimits. Since stalks are filtered colimits, this ensures that the stalks of presheaves valued in these categories behave exactly as for `Type`-valued presheaves. For example, in `germ_exist` we prove that in such a category, every element of the stalk is the germ of a section. Furthermore, if we require the forgetful functor to reflect isomorphisms and preserve limits (as is the case for most algebraic structures), we have access to the unique gluing API and can prove further properties. Most notably, in `is_iso_iff_stalk_functor_map_iso`, we prove that in such a category, a morphism of sheaves is an isomorphism if and only if all of its stalk maps are isomorphisms. See also the definition of "algebraic structures" in the stacks project: https://stacks.math.columbia.edu/tag/007L -/ noncomputable theory universes v u v' u' open category_theory open Top open category_theory.limits open topological_space open opposite variables {C : Type u} [category.{v} C] variables [has_colimits.{v} C] variables {X Y Z : Top.{v}} namespace Top.presheaf variables (C) /-- Stalks are functorial with respect to morphisms of presheaves over a fixed `X`. -/ def stalk_functor (x : X) : X.presheaf C ⥤ C := ((whiskering_left _ _ C).obj (open_nhds.inclusion x).op) ⋙ colim variables {C} /-- The stalk of a presheaf `F` at a point `x` is calculated as the colimit of the functor nbhds x ⥤ opens F.X ⥤ C -/ def stalk (ℱ : X.presheaf C) (x : X) : C := (stalk_functor C x).obj ℱ -- -- colimit ((open_nhds.inclusion x).op ⋙ ℱ) @[simp] lemma stalk_functor_obj (ℱ : X.presheaf C) (x : X) : (stalk_functor C x).obj ℱ = ℱ.stalk x := rfl /-- The germ of a section of a presheaf over an open at a point of that open. -/ def germ (F : X.presheaf C) {U : opens X} (x : U) : F.obj (op U) ⟶ stalk F x := colimit.ι ((open_nhds.inclusion x.1).op ⋙ F) (op ⟨U, x.2⟩) @[simp, elementwise] lemma germ_res (F : X.presheaf C) {U V : opens X} (i : U ⟶ V) (x : U) : F.map i.op ≫ germ F x = germ F (i x : V) := let i' : (⟨U, x.2⟩ : open_nhds x.1) ⟶ ⟨V, (i x : V).2⟩ := i in colimit.w ((open_nhds.inclusion x.1).op ⋙ F) i'.op /-- A morphism from the stalk of `F` at `x` to some object `Y` is completely determined by its composition with the `germ` morphisms. -/ lemma stalk_hom_ext (F : X.presheaf C) {x} {Y : C} {f₁ f₂ : F.stalk x ⟶ Y} (ih : ∀ (U : opens X) (hxU : x ∈ U), F.germ ⟨x, hxU⟩ ≫ f₁ = F.germ ⟨x, hxU⟩ ≫ f₂) : f₁ = f₂ := colimit.hom_ext $ λ U, by { induction U using opposite.rec, cases U with U hxU, exact ih U hxU } @[simp, reassoc, elementwise] lemma stalk_functor_map_germ {F G : X.presheaf C} (U : opens X) (x : U) (f : F ⟶ G) : germ F x ≫ (stalk_functor C x.1).map f = f.app (op U) ≫ germ G x := colimit.ι_map (whisker_left ((open_nhds.inclusion x.1).op) f) (op ⟨U, x.2⟩) variables (C) /-- For a presheaf `F` on a space `X`, a continuous map `f : X ⟶ Y` induces a morphisms between the stalk of `f _ * F` at `f x` and the stalk of `F` at `x`. -/ def stalk_pushforward (f : X ⟶ Y) (F : X.presheaf C) (x : X) : (f _* F).stalk (f x) ⟶ F.stalk x := begin -- This is a hack; Lean doesn't like to elaborate the term written directly. transitivity, swap, exact colimit.pre _ (open_nhds.map f x).op, exact colim.map (whisker_right (nat_trans.op (open_nhds.inclusion_map_iso f x).inv) F), end @[simp, elementwise, reassoc] lemma stalk_pushforward_germ (f : X ⟶ Y) (F : X.presheaf C) (U : opens Y) (x : (opens.map f).obj U) : (f _* F).germ ⟨f x, x.2⟩ ≫ F.stalk_pushforward C f x = F.germ x := begin rw [stalk_pushforward, germ, colimit.ι_map_assoc, colimit.ι_pre, whisker_right_app], erw [category_theory.functor.map_id, category.id_comp], refl, end -- Here are two other potential solutions, suggested by @fpvandoorn at -- <https://github.com/leanprover-community/mathlib/pull/1018#discussion_r283978240> -- However, I can't get the subsequent two proofs to work with either one. -- def stalk_pushforward (f : X ⟶ Y) (ℱ : X.presheaf C) (x : X) : -- (f _* ℱ).stalk (f x) ⟶ ℱ.stalk x := -- colim.map ((functor.associator _ _ _).inv ≫ -- whisker_right (nat_trans.op (open_nhds.inclusion_map_iso f x).inv) ℱ) ≫ -- colimit.pre ((open_nhds.inclusion x).op ⋙ ℱ) (open_nhds.map f x).op -- def stalk_pushforward (f : X ⟶ Y) (ℱ : X.presheaf C) (x : X) : -- (f _* ℱ).stalk (f x) ⟶ ℱ.stalk x := -- (colim.map (whisker_right (nat_trans.op (open_nhds.inclusion_map_iso f x).inv) ℱ) : -- colim.obj ((open_nhds.inclusion (f x) ⋙ opens.map f).op ⋙ ℱ) ⟶ _) ≫ -- colimit.pre ((open_nhds.inclusion x).op ⋙ ℱ) (open_nhds.map f x).op namespace stalk_pushforward local attribute [tidy] tactic.op_induction' @[simp] lemma id (ℱ : X.presheaf C) (x : X) : ℱ.stalk_pushforward C (𝟙 X) x = (stalk_functor C x).map ((pushforward.id ℱ).hom) := begin dsimp [stalk_pushforward, stalk_functor], ext1, tactic.op_induction', cases j, cases j_val, rw [colimit.ι_map_assoc, colimit.ι_map, colimit.ι_pre, whisker_left_app, whisker_right_app, pushforward.id_hom_app, eq_to_hom_map, eq_to_hom_refl], dsimp, -- FIXME A simp lemma which unfortunately doesn't fire: erw [category_theory.functor.map_id], end -- This proof is sadly not at all robust: -- having to use `erw` at all is a bad sign. @[simp] lemma comp (ℱ : X.presheaf C) (f : X ⟶ Y) (g : Y ⟶ Z) (x : X) : ℱ.stalk_pushforward C (f ≫ g) x = ((f _* ℱ).stalk_pushforward C g (f x)) ≫ (ℱ.stalk_pushforward C f x) := begin dsimp [stalk_pushforward, stalk_functor], ext U, induction U using opposite.rec, cases U, cases U_val, simp only [colimit.ι_map_assoc, colimit.ι_pre_assoc, whisker_right_app, category.assoc], dsimp, -- FIXME: Some of these are simp lemmas, but don't fire successfully: erw [category_theory.functor.map_id, category.id_comp, category.id_comp, category.id_comp, colimit.ι_pre, colimit.ι_pre], refl, end lemma stalk_pushforward_iso_of_open_embedding {f : X ⟶ Y} (hf : open_embedding f) (F : X.presheaf C) (x : X) : is_iso (F.stalk_pushforward _ f x) := begin haveI := functor.initial_of_adjunction (hf.is_open_map.adjunction_nhds x), convert is_iso.of_iso ((functor.final.colimit_iso (hf.is_open_map.functor_nhds x).op ((open_nhds.inclusion (f x)).op ⋙ f _* F) : _).symm ≪≫ colim.map_iso _), swap, { fapply nat_iso.of_components, { intro U, refine F.map_iso (eq_to_iso _), dsimp only [functor.op], exact congr_arg op (subtype.eq $ set.preimage_image_eq (unop U).1.1 hf.inj) }, { intros U V i, erw [← F.map_comp, ← F.map_comp], congr } }, { ext U, rw ← iso.comp_inv_eq, erw colimit.ι_map_assoc, rw [colimit.ι_pre, category.assoc], erw [colimit.ι_map_assoc, colimit.ι_pre, ← F.map_comp_assoc], apply colimit.w ((open_nhds.inclusion (f x)).op ⋙ f _* F) _, dsimp only [functor.op], refine ((hom_of_le _).op : op (unop U) ⟶ _), exact set.image_preimage_subset _ _ }, end end stalk_pushforward section stalk_pullback /-- The morphism `ℱ_{f x} ⟶ (f⁻¹ℱ)ₓ` that factors through `(f_*f⁻¹ℱ)_{f x}`. -/ def stalk_pullback_hom (f : X ⟶ Y) (F : Y.presheaf C) (x : X) : F.stalk (f x) ⟶ (pullback_obj f F).stalk x := (stalk_functor _ (f x)).map ((pushforward_pullback_adjunction C f).unit.app F) ≫ stalk_pushforward _ _ _ x /-- The morphism `(f⁻¹ℱ)(U) ⟶ ℱ_{f(x)}` for some `U ∋ x`. -/ def germ_to_pullback_stalk (f : X ⟶ Y) (F : Y.presheaf C) (U : opens X) (x : U) : (pullback_obj f F).obj (op U) ⟶ F.stalk (f x) := colimit.desc (Lan.diagram (opens.map f).op F (op U)) { X := F.stalk (f x), ι := { app := λ V, F.germ ⟨f x, V.hom.unop.le x.2⟩, naturality' := λ _ _ i, by { erw category.comp_id, exact F.germ_res i.left.unop _ } } } /-- The morphism `(f⁻¹ℱ)ₓ ⟶ ℱ_{f(x)}`. -/ def stalk_pullback_inv (f : X ⟶ Y) (F : Y.presheaf C) (x : X) : (pullback_obj f F).stalk x ⟶ F.stalk (f x) := colimit.desc ((open_nhds.inclusion x).op ⋙ presheaf.pullback_obj f F) { X := F.stalk (f x), ι := { app := λ U, F.germ_to_pullback_stalk _ f (unop U).1 ⟨x, (unop U).2⟩, naturality' := λ _ _ _, by { erw [colimit.pre_desc, category.comp_id], congr } } } /-- The isomorphism `ℱ_{f(x)} ≅ (f⁻¹ℱ)ₓ`. -/ def stalk_pullback_iso (f : X ⟶ Y) (F : Y.presheaf C) (x : X) : F.stalk (f x) ≅ (pullback_obj f F).stalk x := { hom := stalk_pullback_hom _ _ _ _, inv := stalk_pullback_inv _ _ _ _, hom_inv_id' := begin delta stalk_pullback_hom stalk_pullback_inv stalk_functor presheaf.pullback stalk_pushforward germ_to_pullback_stalk germ, ext j, induction j using opposite.rec, cases j, simp only [topological_space.open_nhds.inclusion_map_iso_inv, whisker_right_app, whisker_left_app, whiskering_left_obj_map, functor.comp_map, colimit.ι_map_assoc, nat_trans.op_id, Lan_obj_map, pushforward_pullback_adjunction_unit_app_app, category.assoc, colimit.ι_pre_assoc], erw [colimit.ι_desc, colimit.pre_desc, colimit.ι_desc, category.comp_id], simpa end, inv_hom_id' := begin delta stalk_pullback_hom stalk_pullback_inv stalk_functor presheaf.pullback stalk_pushforward, ext U j, induction U using opposite.rec, cases U, cases j, cases j_right, erw [colimit.map_desc, colimit.map_desc, colimit.ι_desc_assoc, colimit.ι_desc_assoc, colimit.ι_desc, category.comp_id], simp only [cocone.whisker_ι, colimit.cocone_ι, open_nhds.inclusion_map_iso_inv, cocones.precompose_obj_ι, whisker_right_app, whisker_left_app, nat_trans.comp_app, whiskering_left_obj_map, nat_trans.op_id, Lan_obj_map, pushforward_pullback_adjunction_unit_app_app], erw ←colimit.w _ (@hom_of_le (open_nhds x) _ ⟨_, U_property⟩ ⟨(opens.map f).obj (unop j_left), j_hom.unop.le U_property⟩ j_hom.unop.le).op, erw colimit.ι_pre_assoc (Lan.diagram _ F _) (costructured_arrow.map _), erw colimit.ι_pre_assoc (Lan.diagram _ F _) (costructured_arrow.map _), congr, simp only [category.assoc, costructured_arrow.map_mk], delta costructured_arrow.mk, congr end } end stalk_pullback section stalk_specializes variables {C} /-- If `x` specializes to `y`, then there is a natural map `F.stalk y ⟶ F.stalk x`. -/ noncomputable def stalk_specializes (F : X.presheaf C) {x y : X} (h : x ⤳ y) : F.stalk y ⟶ F.stalk x := begin refine colimit.desc _ ⟨_,λ U, _,_⟩, { exact colimit.ι ((open_nhds.inclusion x).op ⋙ F) (op ⟨(unop U).1, (specializes_iff_forall_open.mp h _ (unop U).1.2 (unop U).2 : _)⟩) }, { intros U V i, dsimp, rw category.comp_id, let U' : open_nhds x := ⟨_, (specializes_iff_forall_open.mp h _ (unop U).1.2 (unop U).2 : _)⟩, let V' : open_nhds x := ⟨_, (specializes_iff_forall_open.mp h _ (unop V).1.2 (unop V).2 : _)⟩, exact colimit.w ((open_nhds.inclusion x).op ⋙ F) (show V' ⟶ U', from i.unop).op } end @[simp, reassoc, elementwise] lemma germ_stalk_specializes (F : X.presheaf C) {U : opens X} {y : U} {x : X} (h : x ⤳ y) : F.germ y ≫ F.stalk_specializes h = F.germ ⟨x, specializes_iff_forall_open.mp h _ U.2 y.prop⟩ := colimit.ι_desc _ _ @[simp, reassoc, elementwise] lemma germ_stalk_specializes' (F : X.presheaf C) {U : opens X} {x y : X} (h : x ⤳ y) (hy : y ∈ U) : F.germ ⟨y, hy⟩ ≫ F.stalk_specializes h = F.germ ⟨x, specializes_iff_forall_open.mp h _ U.2 hy⟩ := colimit.ι_desc _ _ @[simp, reassoc, elementwise] lemma stalk_specializes_stalk_functor_map {F G : X.presheaf C} (f : F ⟶ G) {x y : X} (h : x ⤳ y) : F.stalk_specializes h ≫ (stalk_functor C x).map f = (stalk_functor C y).map f ≫ G.stalk_specializes h := by { ext, delta stalk_functor, simpa [stalk_specializes] } @[simp, reassoc, elementwise] lemma stalk_specializes_stalk_pushforward (f : X ⟶ Y) (F : X.presheaf C) {x y : X} (h : x ⤳ y) : (f _* F).stalk_specializes (f.map_specialization h) ≫ F.stalk_pushforward _ f x = F.stalk_pushforward _ f y ≫ F.stalk_specializes h := by { ext, delta stalk_pushforward, simpa [stalk_specializes] } end stalk_specializes section concrete variables {C} variables [concrete_category.{v} C] local attribute [instance] concrete_category.has_coe_to_sort concrete_category.has_coe_to_fun @[ext] lemma germ_ext (F : X.presheaf C) {U V : opens X} {x : X} {hxU : x ∈ U} {hxV : x ∈ V} (W : opens X) (hxW : x ∈ W) (iWU : W ⟶ U) (iWV : W ⟶ V) {sU : F.obj (op U)} {sV : F.obj (op V)} (ih : F.map iWU.op sU = F.map iWV.op sV) : F.germ ⟨x, hxU⟩ sU = F.germ ⟨x, hxV⟩ sV := by erw [← F.germ_res iWU ⟨x, hxW⟩, ← F.germ_res iWV ⟨x, hxW⟩, comp_apply, comp_apply, ih] variables [preserves_filtered_colimits (forget C)] /-- For presheaves valued in a concrete category whose forgetful functor preserves filtered colimits, every element of the stalk is the germ of a section. -/ lemma germ_exist (F : X.presheaf C) (x : X) (t : stalk F x) : ∃ (U : opens X) (m : x ∈ U) (s : F.obj (op U)), F.germ ⟨x, m⟩ s = t := begin obtain ⟨U, s, e⟩ := types.jointly_surjective.{v v} _ (is_colimit_of_preserves (forget C) (colimit.is_colimit _)) t, revert s e, rw [(show U = op (unop U), from rfl)], generalize : unop U = V, clear U, cases V with V m, intros s e, exact ⟨V, m, s, e⟩, end lemma germ_eq (F : X.presheaf C) {U V : opens X} (x : X) (mU : x ∈ U) (mV : x ∈ V) (s : F.obj (op U)) (t : F.obj (op V)) (h : germ F ⟨x, mU⟩ s = germ F ⟨x, mV⟩ t) : ∃ (W : opens X) (m : x ∈ W) (iU : W ⟶ U) (iV : W ⟶ V), F.map iU.op s = F.map iV.op t := begin obtain ⟨W, iU, iV, e⟩ := (types.filtered_colimit.is_colimit_eq_iff.{v v} _ (is_colimit_of_preserves _ (colimit.is_colimit ((open_nhds.inclusion x).op ⋙ F)))).mp h, exact ⟨(unop W).1, (unop W).2, iU.unop, iV.unop, e⟩, end lemma stalk_functor_map_injective_of_app_injective {F G : presheaf C X} (f : F ⟶ G) (h : ∀ U : opens X, function.injective (f.app (op U))) (x : X) : function.injective ((stalk_functor C x).map f) := λ s t hst, begin rcases germ_exist F x s with ⟨U₁, hxU₁, s, rfl⟩, rcases germ_exist F x t with ⟨U₂, hxU₂, t, rfl⟩, simp only [stalk_functor_map_germ_apply _ ⟨x,_⟩] at hst, obtain ⟨W, hxW, iWU₁, iWU₂, heq⟩ := G.germ_eq x hxU₁ hxU₂ _ _ hst, rw [← comp_apply, ← comp_apply, ← f.naturality, ← f.naturality, comp_apply, comp_apply] at heq, replace heq := h W heq, convert congr_arg (F.germ ⟨x,hxW⟩) heq, exacts [(F.germ_res_apply iWU₁ ⟨x,hxW⟩ s).symm, (F.germ_res_apply iWU₂ ⟨x,hxW⟩ t).symm], end variables [has_limits C] [preserves_limits (forget C)] [reflects_isomorphisms (forget C)] /-- Let `F` be a sheaf valued in a concrete category, whose forgetful functor reflects isomorphisms, preserves limits and filtered colimits. Then two sections who agree on every stalk must be equal. -/ lemma section_ext (F : sheaf C X) (U : opens X) (s t : F.1.obj (op U)) (h : ∀ x : U, F.1.germ x s = F.1.germ x t) : s = t := begin -- We use `germ_eq` and the axiom of choice, to pick for every point `x` a neighbourhood -- `V x`, such that the restrictions of `s` and `t` to `V x` coincide. choose V m i₁ i₂ heq using λ x : U, F.1.germ_eq x.1 x.2 x.2 s t (h x), -- Since `F` is a sheaf, we can prove the equality locally, if we can show that these -- neighborhoods form a cover of `U`. apply F.eq_of_locally_eq' V U i₁, { intros x hxU, rw [opens.mem_coe, opens.mem_supr], exact ⟨⟨x, hxU⟩, m ⟨x, hxU⟩⟩ }, { intro x, rw [heq, subsingleton.elim (i₁ x) (i₂ x)] } end /- Note that the analogous statement for surjectivity is false: Surjectivity on stalks does not imply surjectivity of the components of a sheaf morphism. However it does imply that the morphism is an epi, but this fact is not yet formalized. -/ lemma app_injective_of_stalk_functor_map_injective {F : sheaf C X} {G : presheaf C X} (f : F.1 ⟶ G) (U : opens X) (h : ∀ x : U, function.injective ((stalk_functor C x.val).map f)) : function.injective (f.app (op U)) := λ s t hst, section_ext F _ _ _ $ λ x, h x $ by rw [stalk_functor_map_germ_apply, stalk_functor_map_germ_apply, hst] lemma app_injective_iff_stalk_functor_map_injective {F : sheaf C X} {G : presheaf C X} (f : F.1 ⟶ G) : (∀ x : X, function.injective ((stalk_functor C x).map f)) ↔ (∀ U : opens X, function.injective (f.app (op U))) := ⟨λ h U, app_injective_of_stalk_functor_map_injective f U (λ x, h x.1), stalk_functor_map_injective_of_app_injective f⟩ /-- For surjectivity, we are given an arbitrary section `t` and need to find a preimage for it. We claim that it suffices to find preimages *locally*. That is, for each `x : U` we construct a neighborhood `V ≤ U` and a section `s : F.obj (op V))` such that `f.app (op V) s` and `t` agree on `V`. -/ lemma app_surjective_of_injective_of_locally_surjective {F G : sheaf C X} (f : F ⟶ G) (U : opens X) (hinj : ∀ x : U, function.injective ((stalk_functor C x.1).map f)) (hsurj : ∀ (t) (x : U), ∃ (V : opens X) (m : x.1 ∈ V) (iVU : V ⟶ U) (s : F.1.obj (op V)), f.app (op V) s = G.1.map iVU.op t) : function.surjective (f.app (op U)) := begin intro t, -- We use the axiom of choice to pick around each point `x` an open neighborhood `V` and a -- preimage under `f` on `V`. choose V mV iVU sf heq using hsurj t, -- These neighborhoods clearly cover all of `U`. have V_cover : U ≤ supr V, { intros x hxU, rw [opens.mem_coe, opens.mem_supr], exact ⟨⟨x, hxU⟩, mV ⟨x, hxU⟩⟩ }, -- Since `F` is a sheaf, we can glue all the local preimages together to get a global preimage. obtain ⟨s, s_spec, -⟩ := F.exists_unique_gluing' V U iVU V_cover sf _, { use s, apply G.eq_of_locally_eq' V U iVU V_cover, intro x, rw [← comp_apply, ← f.naturality, comp_apply, s_spec, heq] }, { intros x y, -- What's left to show here is that the secions `sf` are compatible, i.e. they agree on -- the intersections `V x ⊓ V y`. We prove this by showing that all germs are equal. apply section_ext, intro z, -- Here, we need to use injectivity of the stalk maps. apply (hinj ⟨z, (iVU x).le ((inf_le_left : V x ⊓ V y ≤ V x) z.2)⟩), dsimp only, erw [stalk_functor_map_germ_apply, stalk_functor_map_germ_apply], simp_rw [← comp_apply, f.naturality, comp_apply, heq, ← comp_apply, ← G.1.map_comp], refl } end lemma app_surjective_of_stalk_functor_map_bijective {F G : sheaf C X} (f : F ⟶ G) (U : opens X) (h : ∀ x : U, function.bijective ((stalk_functor C x.val).map f)) : function.surjective (f.app (op U)) := begin refine app_surjective_of_injective_of_locally_surjective f U (λ x, (h x).1) (λ t x, _), -- Now we need to prove our initial claim: That we can find preimages of `t` locally. -- Since `f` is surjective on stalks, we can find a preimage `s₀` of the germ of `t` at `x` obtain ⟨s₀,hs₀⟩ := (h x).2 (G.1.germ x t), -- ... and this preimage must come from some section `s₁` defined on some open neighborhood `V₁` obtain ⟨V₁,hxV₁,s₁,hs₁⟩ := F.1.germ_exist x.1 s₀, subst hs₁, rename hs₀ hs₁, erw stalk_functor_map_germ_apply V₁ ⟨x.1,hxV₁⟩ f s₁ at hs₁, -- Now, the germ of `f.app (op V₁) s₁` equals the germ of `t`, hence they must coincide on -- some open neighborhood `V₂`. obtain ⟨V₂, hxV₂, iV₂V₁, iV₂U, heq⟩ := G.1.germ_eq x.1 hxV₁ x.2 _ _ hs₁, -- The restriction of `s₁` to that neighborhood is our desired local preimage. use [V₂, hxV₂, iV₂U, F.1.map iV₂V₁.op s₁], rw [← comp_apply, f.naturality, comp_apply, heq], end lemma app_bijective_of_stalk_functor_map_bijective {F G : sheaf C X} (f : F ⟶ G) (U : opens X) (h : ∀ x : U, function.bijective ((stalk_functor C x.val).map f)) : function.bijective (f.app (op U)) := ⟨app_injective_of_stalk_functor_map_injective f U (λ x, (h x).1), app_surjective_of_stalk_functor_map_bijective f U h⟩ lemma app_is_iso_of_stalk_functor_map_iso {F G : sheaf C X} (f : F ⟶ G) (U : opens X) [∀ x : U, is_iso ((stalk_functor C x.val).map f)] : is_iso (f.app (op U)) := begin -- Since the forgetful functor of `C` reflects isomorphisms, it suffices to see that the -- underlying map between types is an isomorphism, i.e. bijective. suffices : is_iso ((forget C).map (f.app (op U))), { exactI is_iso_of_reflects_iso (f.app (op U)) (forget C) }, rw is_iso_iff_bijective, apply app_bijective_of_stalk_functor_map_bijective, intro x, apply (is_iso_iff_bijective _).mp, exact functor.map_is_iso (forget C) ((stalk_functor C x.1).map f) end /-- Let `F` and `G` be sheaves valued in a concrete category, whose forgetful functor reflects isomorphisms, preserves limits and filtered colimits. Then if the stalk maps of a morphism `f : F ⟶ G` are all isomorphisms, `f` must be an isomorphism. -/ -- Making this an instance would cause a loop in typeclass resolution with `functor.map_is_iso` lemma is_iso_of_stalk_functor_map_iso {F G : sheaf C X} (f : F ⟶ G) [∀ x : X, is_iso ((stalk_functor C x).map f)] : is_iso f := begin -- Since the inclusion functor from sheaves to presheaves is fully faithful, it suffices to -- show that `f`, as a morphism between _presheaves_, is an isomorphism. suffices : is_iso ((sheaf.forget C X).map f), { exactI is_iso_of_fully_faithful (sheaf.forget C X) f }, -- We show that all components of `f` are isomorphisms. suffices : ∀ U : (opens X)ᵒᵖ, is_iso (f.app U), { exact @nat_iso.is_iso_of_is_iso_app _ _ _ _ F.1 G.1 f this, }, intro U, induction U using opposite.rec, apply app_is_iso_of_stalk_functor_map_iso end /-- Let `F` and `G` be sheaves valued in a concrete category, whose forgetful functor reflects isomorphisms, preserves limits and filtered colimits. Then a morphism `f : F ⟶ G` is an isomorphism if and only if all of its stalk maps are isomorphisms. -/ lemma is_iso_iff_stalk_functor_map_iso {F G : sheaf C X} (f : F ⟶ G) : is_iso f ↔ ∀ x : X, is_iso ((stalk_functor C x).map f) := begin split, { intros h x, resetI, exact @functor.map_is_iso _ _ _ _ _ _ (stalk_functor C x) f ((sheaf.forget C X).map_is_iso f) }, { intro h, exactI is_iso_of_stalk_functor_map_iso f } end end concrete instance (F : X.presheaf CommRing) {U : opens X} (x : U) : algebra (F.obj $ op U) (F.stalk x) := (F.germ x).to_algebra @[simp] lemma stalk_open_algebra_map {X : Top} (F : X.presheaf CommRing) {U : opens X} (x : U) : algebra_map (F.obj $ op U) (F.stalk x) = F.germ x := rfl end Top.presheaf
7e344c75a5165fdc04949e7c861ea6b877528c39
e030b0259b777fedcdf73dd966f3f1556d392178
/tests/lean/run/ematch2.lean
6211e177397d498aef5e93775ed78222c046b978
[ "Apache-2.0" ]
permissive
fgdorais/lean
17b46a095b70b21fa0790ce74876658dc5faca06
c3b7c54d7cca7aaa25328f0a5660b6b75fe26055
refs/heads/master
1,611,523,590,686
1,484,412,902,000
1,484,412,902,000
38,489,734
0
0
null
1,435,923,380,000
1,435,923,379,000
null
UTF-8
Lean
false
false
1,005
lean
namespace foo universe variables u variables {α : Type u} open tactic meta def add_insts : list (expr × expr) → tactic unit | [] := skip | ((inst, pr)::r) := do assertv `_einst inst pr, add_insts r meta def internalize_hs : list expr → ematch_state → tactic ematch_state | [] s := return s | (h::hs) s := do t ← infer_type h, s ← s^.internalize t, internalize_hs hs s meta def ematch_test (h : name) : tactic unit := do cc ← cc_state.mk_using_hs, ctx ← local_context, ems ← internalize_hs ctx (ematch_state.mk default_ematch_config), tgt ← target, ems ← ems^.internalize tgt, hlemma ← hinst_lemma.mk_from_decl h, (r, cc, ems) ← ematch_all cc ems hlemma tt, add_insts r section variables [add_comm_monoid α] theorem add_comm_three (a b c : α) : a + b + c = c + b + a := begin ematch_test `add_comm, ematch_test `add_assoc, cc end theorem add.comm4 : ∀ (n m k l : α), n + m + (k + l) = n + k + (m + l) := by cc end end foo
d8997381c1363d429d008182fe7ed16f8100f577
57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d
/tests/lean/run/depElim1.lean
27127e3ded287fb7cc231b555bb1526a6c31f9c2
[ "Apache-2.0" ]
permissive
collares/lean4
861a9269c4592bce49b71059e232ff0bfe4594cc
52a4f535d853a2c7c7eea5fee8a4fa04c682c1ee
refs/heads/master
1,691,419,031,324
1,618,678,138,000
1,618,678,138,000
358,989,750
0
0
Apache-2.0
1,618,696,333,000
1,618,696,333,000
null
UTF-8
Lean
false
false
13,777
lean
import Lean.Meta.Match open Lean open Lean.Meta open Lean.Meta.Match /- Infrastructure for testing -/ universes u v def check (x : Bool) : IO Unit := do unless x do throw $ IO.userError "check failed" def inaccessible {α : Sort u} (a : α) : α := a def val {α : Sort u} (a : α) : α := a def As {α : Sort u} (v a : α) : α := a inductive Pat {α : Sort u} (a : α) : Type u | mk : Pat a inductive ArrayLit0 (α : Sort u) : Type u | mk : ArrayLit0 α inductive ArrayLit1 {α : Sort u} (a : α) : Type u | mk : ArrayLit1 a inductive ArrayLit2 {α : Sort u} (a b : α) : Type u | mk : ArrayLit2 a b inductive ArrayLit3 {α : Sort u} (a b c : α) : Type u | mk : ArrayLit3 a b c inductive ArrayLit4 {α : Sort u} (a b c d : α) : Type u | mk : ArrayLit4 a b c d private def getConstructorVal (ctorName : Name) (fn : Expr) (args : Array Expr) : MetaM (Option (ConstructorVal × Expr × Array Expr)) := do let env ← getEnv match env.find? ctorName with | some (ConstantInfo.ctorInfo v) => if args.size == v.numParams + v.numFields then return some (v, fn, args) else return none | _ => return none private def constructorApp? (e : Expr) : MetaM (Option (ConstructorVal × Expr × Array Expr)) := do let env ← getEnv match e with | Expr.lit (Literal.natVal n) _ => if n == 0 then getConstructorVal `Nat.zero (mkConst `Nat.zero) #[] else getConstructorVal `Nat.succ (mkConst `Nat.succ) #[mkNatLit (n-1)] | _ => let fn := e.getAppFn match fn with | Expr.const n _ _ => getConstructorVal n fn e.getAppArgs | _ => pure none /- Convert expression using auxiliary hints `inaccessible` and `val` into a pattern -/ partial def mkPattern : Expr → MetaM Pattern | e => do if e.isAppOfArity `val 2 then return Pattern.val e.appArg! else if e.isAppOfArity `inaccessible 2 then return Pattern.inaccessible e.appArg! else if e.isFVar then return Pattern.var e.fvarId! else if e.isAppOfArity `As 3 && (e.getArg! 1).isFVar then let v := e.getArg! 1 let p := e.getArg! 2 let p ← mkPattern p return Pattern.as v.fvarId! p else if e.isAppOfArity `ArrayLit0 1 || e.isAppOfArity `ArrayLit1 2 || e.isAppOfArity `ArrayLit2 3 || e.isAppOfArity `ArrayLit3 4 || e.isAppOfArity `ArrayLit4 5 then let args := e.getAppArgs let type := args[0] let ps := args.extract 1 args.size let ps ← ps.toList.mapM mkPattern return Pattern.arrayLit type ps else match e.arrayLit? with | some (_, es) => let pats ← es.mapM mkPattern let type ← inferType e let type ← whnfD type let elemType := type.appArg! return Pattern.arrayLit elemType pats | none => let e ← whnfD e let r? ← constructorApp? e match r? with | none => throwError "unexpected pattern" | some (cval, fn, args) => let params := args.extract 0 cval.numParams let fields := args.extract cval.numParams args.size let pats ← fields.toList.mapM mkPattern return Pattern.ctor cval.name fn.constLevels! params.toList pats partial def decodePats : Expr → MetaM (List Pattern) | e => do match e.app2? `Pat with | some (_, pat) => let pat ← mkPattern pat; return [pat] | none => match e.prod? with | none => throwError "unexpected pattern" | some (pat, pats) => let pat ← decodePats pat let pats ← decodePats pats return pat ++ pats partial def decodeAltLHS (e : Expr) : MetaM AltLHS := forallTelescopeReducing e fun args body => do let decls ← args.toList.mapM (fun arg => getLocalDecl arg.fvarId!) let pats ← decodePats body return { ref := Syntax.missing, fvarDecls := decls, patterns := pats } partial def decodeAltLHSs : Expr → MetaM (List AltLHS) | e => do match e.app2? `LHS with | some (_, lhs) => let lhs ← decodeAltLHS lhs; return [lhs] | none => match e.prod? with | none => throwError "unexpected LHS" | some (lhs, lhss) => let lhs ← decodeAltLHSs lhs let lhss ← decodeAltLHSs lhss return lhs ++ lhss def withDepElimFrom {α} (declName : Name) (numPats : Nat) (k : List FVarId → List AltLHS → MetaM α) : MetaM α := do let cinfo ← getConstInfo declName forallTelescopeReducing cinfo.type fun args body => if args.size < numPats then throwError "insufficient number of parameters" else do let xs := (args.extract (args.size - numPats) args.size).toList.map $ Expr.fvarId! let alts ← decodeAltLHSs body k xs alts inductive LHS {α : Sort u} (a : α) : Type u | mk : LHS a instance LHS.inhabited {α} (a : α) : Inhabited (LHS a) := ⟨LHS.mk⟩ -- set_option trace.Meta.debug true -- set_option trace.Meta.Tactic.cases true -- set_option trace.Meta.Tactic.subst true @[init] def register : IO Unit := registerTraceClass `Meta.mkElim /- Helper methods for testins mkElim -/ private def getUnusedLevelParam (majors : List Expr) (lhss : List AltLHS) : MetaM Level := do let mut s := {} for major in majors do let major ← instantiateMVars major let majorType ← inferType major let majorType ← instantiateMVars majorType s := collectLevelParams s major s := collectLevelParams s majorType return s.getUnusedLevelParam /- Return `Prop` if `inProf == true` and `Sort u` otherwise, where `u` is a fresh universe level parameter. -/ private def mkElimSort (majors : List Expr) (lhss : List AltLHS) (inProp : Bool) : MetaM Expr := do if inProp then return mkSort levelZero else let v ← getUnusedLevelParam majors lhss return mkSort $ v def mkTester (elimName : Name) (majors : List Expr) (lhss : List AltLHS) (inProp : Bool := false) : MetaM MatcherResult := do generalizeTelescope majors.toArray fun majors => do let resultType := if inProp then mkConst `True /- some proposition -/ else mkConst `Nat let matchType ← mkForallFVars majors resultType Match.mkMatcher elimName matchType majors.size lhss def test (ex : Name) (numPats : Nat) (elimName : Name) (inProp : Bool := false) : MetaM Unit := withDepElimFrom ex numPats fun majors alts => do let majors := majors.map mkFVar trace[Meta.debug] m!"majors: {majors.toArray}" let r ← mkTester elimName majors alts inProp unless r.counterExamples.isEmpty do throwError m!"missing cases:\n{counterExamplesToMessageData r.counterExamples}" unless r.unusedAltIdxs.isEmpty do throwError (m!"unused alternatives: " ++ toString (r.unusedAltIdxs.map fun idx => "#" ++ toString (idx+1))) let cinfo ← getConstInfo elimName IO.println (toString cinfo.name ++ " : " ++ toString cinfo.type) pure () def testFailure (ex : Name) (numPats : Nat) (elimName : Name) (inProp : Bool := false) : MetaM Unit := do let worked ← tryCatch (do test ex numPats elimName inProp; pure true) (fun ex => pure false) if worked then throwError "unexpected success" def ex0 (x : Nat) : LHS (forall (y : Nat), Pat y) := arbitrary #eval test `ex0 1 `elimTest0 #print elimTest0 def ex1 (α : Type u) (β : Type v) (n : Nat) (x : List α) (y : List β) : LHS (Pat ([] : List α) × Pat ([] : List β)) × LHS (forall (a : α) (as : List α) (b : β) (bs : List β), Pat (a::as) × Pat (b::bs)) × LHS (forall (a : α) (as : List α), Pat (a::as) × Pat ([] : List β)) × LHS (forall (b : β) (bs : List β), Pat ([] : List α) × Pat (b::bs)) := arbitrary #eval test `ex1 2 `elimTest1 #print elimTest1 inductive Vec (α : Type u) : Nat → Type u | nil : Vec α 0 | cons {n : Nat} : α → Vec α n → Vec α (n+1) def ex2 (α : Type u) (n : Nat) (xs : Vec α n) (ys : Vec α n) : LHS (Pat (inaccessible 0) × Pat (Vec.nil : Vec α 0) × Pat (Vec.nil : Vec α 0)) × LHS (forall (n : Nat) (x : α) (xs : Vec α n) (y : α) (ys : Vec α n), Pat (inaccessible (n+1)) × Pat (Vec.cons x xs) × Pat (Vec.cons y ys)) := arbitrary #eval test `ex2 3 `elimTest2 #print elimTest2 def ex3 (α : Type u) (β : Type v) (n : Nat) (x : List α) (y : List β) : LHS (Pat ([] : List α) × Pat ([] : List β)) × LHS (forall (a : α) (b : β), Pat [a] × Pat [b]) × LHS (forall (a₁ a₂ : α) (as : List α) (b₁ b₂ : β) (bs : List β), Pat (a₁::a₂::as) × Pat (b₁::b₂::bs)) × LHS (forall (as : List α) (bs : List β), Pat as × Pat bs) := arbitrary -- set_option trace.Meta.EqnCompiler.match true -- set_option trace.Meta.EqnCompiler.matchDebug true #eval test `ex3 2 `elimTest3 #print elimTest3 def ex4 (α : Type u) (n : Nat) (xs : Vec α n) : LHS (Pat (inaccessible 0) × Pat (Vec.nil : Vec α 0)) × LHS (forall (n : Nat) (xs : Vec α (n+1)), Pat (inaccessible (n+1)) × Pat xs) := arbitrary #eval test `ex4 2 `elimTest4 #print elimTest4 def ex5 (α : Type u) (n : Nat) (xs : Vec α n) : LHS (Pat Nat.zero × Pat (Vec.nil : Vec α 0)) × LHS (forall (n : Nat) (xs : Vec α (n+1)), Pat (Nat.succ n) × Pat xs) := arbitrary #eval test `ex5 2 `elimTest5 #print elimTest5 def ex6 (α : Type u) (n : Nat) (xs : Vec α n) : LHS (Pat (inaccessible Nat.zero) × Pat (Vec.nil : Vec α 0)) × LHS (forall (N : Nat) (XS : Vec α N), Pat (inaccessible N) × Pat XS) := arbitrary -- set_option trace.Meta.Match.match true -- set_option trace.Meta.Match.debug true #eval test `ex6 2 `elimTest6 -- #print elimTest6 def ex7 (α : Type u) (n : Nat) (xs : Vec α n) : LHS (forall (a : α), Pat (inaccessible 1) × Pat (Vec.cons a Vec.nil)) × LHS (forall (N : Nat) (XS : Vec α N), Pat (inaccessible N) × Pat XS) := arbitrary #eval test `ex7 2 `elimTest7 -- #check elimTest7 def isSizeOne {n : Nat} (xs : Vec Nat n) : Bool := elimTest7 _ (fun _ _ => Bool) n xs (fun _ => true) (fun _ _ => false) #eval isSizeOne Vec.nil #eval isSizeOne (Vec.cons 1 Vec.nil) #eval isSizeOne (Vec.cons 2 (Vec.cons 1 Vec.nil)) def singleton? {n : Nat} (xs : Vec Nat n) : Option Nat := elimTest7 _ (fun _ _ => Option Nat) n xs (fun a => some a) (fun _ _ => none) #eval singleton? Vec.nil #eval singleton? (Vec.cons 10 Vec.nil) #eval singleton? (Vec.cons 20 (Vec.cons 10 Vec.nil)) def ex8 (α : Type u) (n : Nat) (xs : Vec α n) : LHS (forall (a b : α), Pat (inaccessible 2) × Pat (Vec.cons a (Vec.cons b Vec.nil))) × LHS (forall (N : Nat) (XS : Vec α N), Pat (inaccessible N) × Pat XS) := arbitrary #eval test `ex8 2 `elimTest8 #print elimTest8 def pair? {n : Nat} (xs : Vec Nat n) : Option (Nat × Nat) := elimTest8 _ (fun _ _ => Option (Nat × Nat)) n xs (fun a b => some (a, b)) (fun _ _ => none) #eval pair? Vec.nil #eval pair? (Vec.cons 10 Vec.nil) #eval pair? (Vec.cons 20 (Vec.cons 10 Vec.nil)) inductive Op : Nat → Nat → Type | mk : ∀ n, Op n n structure Node : Type := (id₁ id₂ : Nat) (o : Op id₁ id₂) def ex9 (xs : List Node) : LHS (forall (h : Node) (t : List Node), Pat (h :: Node.mk 1 1 (Op.mk 1) :: t)) × LHS (forall (ys : List Node), Pat ys) := arbitrary #eval test `ex9 1 `elimTest9 #print elimTest9 def f (xs : List Node) : Bool := elimTest9 (fun _ => Bool) xs (fun _ _ => true) (fun _ => false) #eval check (f [] == false) #eval check (f [⟨0, 0, Op.mk 0⟩] == false) #eval check (f [⟨0, 0, Op.mk 0⟩, ⟨1, 1, Op.mk 1⟩]) #eval check (f [⟨0, 0, Op.mk 0⟩, ⟨2, 2, Op.mk 2⟩] == false) inductive Foo : Bool → Prop | bar : Foo false | baz : Foo false def ex10 (x : Bool) (y : Foo x) : LHS (Pat (inaccessible false) × Pat Foo.bar) × LHS (forall (x : Bool) (y : Foo x), Pat (inaccessible x) × Pat y) := arbitrary #eval test `ex10 2 `elimTest10 true def ex11 (xs : List Node) : LHS (forall (h : Node) (t : List Node), Pat (h :: Node.mk 1 1 (Op.mk 1) :: t)) × LHS (Pat ([] : List Node)) := arbitrary #eval testFailure `ex11 1 `elimTest11 -- should produce error message def ex12 (x y z : Bool) : LHS (forall (x y : Bool), Pat x × Pat y × Pat true) × LHS (forall (x z : Bool), Pat false × Pat true × Pat z) × LHS (forall (y z : Bool), Pat true × Pat false × Pat z) := arbitrary #eval testFailure `ex12 3 `elimTest12 -- should produce error message def ex13 (xs : List Node) : LHS (forall (h : Node) (t : List Node), Pat (h :: Node.mk 1 1 (Op.mk 1) :: t)) × LHS (forall (ys : List Node), Pat ys) × LHS (forall (ys : List Node), Pat ys) := arbitrary #eval testFailure `ex13 1 `elimTest13 -- should produce error message def ex14 (x y : Nat) : LHS (Pat (val 1) × Pat (val 2)) × LHS (Pat (val 2) × Pat (val 3)) × LHS (forall (x y : Nat), Pat x × Pat y) := arbitrary -- set_option trace.Meta.Match true #eval test `ex14 2 `elimTest14 #print elimTest14 def h2 (x y : Nat) : Nat := elimTest14 (fun _ _ => Nat) x y (fun _ => 0) (fun _ => 1) (fun x y => x + y) #eval check (h2 1 2 == 0) #eval check (h2 1 4 == 5) #eval check (h2 2 3 == 1) #eval check (h2 2 4 == 6) #eval check (h2 3 4 == 7) def ex15 (xs : Array (List Nat)) : LHS (forall (a : Nat), Pat (ArrayLit1 [a])) × LHS (forall (a b : Nat), Pat (ArrayLit2 [a] [b])) × LHS (forall (ys : Array (List Nat)), Pat ys) := arbitrary #eval test `ex15 1 `elimTest15 -- #check elimTest15 def h3 (xs : Array (List Nat)) : Nat := elimTest15 (fun _ => Nat) xs (fun a => a + 1) (fun a b => a + b) (fun ys => ys.size) #eval check (h3 #[[1]] == 2) #eval check (h3 #[[3], [2]] == 5) #eval check (h3 #[[1, 2]] == 1) #eval check (h3 #[[1, 2], [2, 3], [3]] == 3) def ex16 (xs : List Nat) : LHS (forall (a : Nat) (xs : List Nat) (b : Nat) (as : List Nat), Pat (a :: As xs (b :: as))) × LHS (forall (a : Nat), Pat ([a])) × LHS (Pat ([] : List Nat)) := arbitrary #eval test `ex16 1 `elimTest16 -- #check elimTest16 #print elimTest16 def h4 (xs : List Nat) : List Nat := elimTest16 (fun _ => List Nat) xs (fun a xs b ys => xs) (fun a => []) (fun _ => [1]) #eval check (h4 [1, 2, 3] == [2, 3]) #eval check (h4 [1] == []) #eval check (h4 [] == [1])
a5d90d6039edda193635f9d6497aff5cce8d17c5
2dae50597b29069308a49d0995d06d4a0720029a
/lean_modifications/tactic_modifications.lean
acdb5777c3ad37fa0ab14d142fbcae49572011aa
[ "Apache-2.0" ]
permissive
jasonrute/lean-proof-recording-public-old
d94ae9ba64c1cbf49cda9fb4f25755a3d92fb171
34e75a8bb67b6527177b217c9207191d409368dd
refs/heads/master
1,678,527,177,787
1,613,767,073,000
1,613,767,073,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,655
lean
/- This is a staging area for code which will be inserted into a Lean file. The code to be inserted is between the line comments `PR BEGIN MODIFICATION` and `PR END MODIFICATION` It will be inserted by `insert_proof_recording_code.py`. Insert info: - file: `_target/lean/library/init/meta/tactic.lean` - location: end of file Most of this code is carefully written, but any code labeled "BEGIN/END CUSTOMIZABLE CODE" encourages customization to change what is being recorded -/ prelude import init.meta.tactic universes u v --PR BEGIN MODIFICATION -- proof recording code namespace pr /-- Trace data as JSON preceeded by a `<PR>` flag. This makes it easy to filter out all the traced data. -/ meta def trace_data (table : string) (key : string) (field : string) (data : string) : tactic unit := tactic.trace $ "<PR> {" ++ "\"table\": " ++ (repr table) ++ ", " ++ "\"key\": " ++ (repr key) ++ ", " ++ "\"" ++ field ++ "\": " ++ data ++ "}" meta def trace_data_string (table : string) (key : string) (field : string) (str_data : string) : tactic unit := trace_data table key field (repr str_data) meta def trace_data_num (table : string) (key : string) (field : string) (num_data : nat) : tactic unit := trace_data table key field (repr num_data) meta def trace_data_bool (table : string) (key : string) (field : string) (bool_data : bool) : tactic unit := trace_data table key field (if bool_data then "true" else "false") -- tactic recording /-- Each tactic application within a given file can be keyed by four numbers. Combinators allow a tactic to be called more than once, and some nested tactics use the same line and column position, so we also include depth to capture nesting and index to capture executed order. (A proof can be uniquely keyed by its first tactic at depth 1, index 1.) -/ structure tactic_address := -- the line and column of the tactic instance -- using 1-indexing like editors do even though lean uses a mix of 0 and 1-indexing (line : nat) (column : nat) -- depth of tactic block (tactic inside a tactic) (depth : nat) -- index indicating the order of execution (index : nat) meta def addr_key (addr : tactic_address) : string := (repr addr.line) ++ ":" ++ (repr addr.column) ++ ":" ++ (repr addr.depth) ++ ":" ++ (repr addr.index) meta def trace_tactic_data_string (addr : tactic_address) (field : string) (str_data : string) : tactic unit := trace_data_string "tactic_instances" (addr_key addr) field str_data meta def trace_tactic_data_num (addr : tactic_address) (field : string) (num_data : nat) : tactic unit := trace_data_num "tactic_instances" (addr_key addr) field num_data meta def trace_tactic_data_bool (addr : tactic_address) (field : string) (bool_data : bool) : tactic unit := trace_data_bool "tactic_instances" (addr_key addr) field bool_data meta def trace_tactic_data_addr (addr : tactic_address) (field : string) (addr_data : tactic_address) : tactic unit := trace_data_string "tactic_instances" (addr_key addr) field (addr_key addr_data) meta def get_tactic_address (o : options) (nm : name) : tactic_address := { tactic_address . line := o.get_nat (name.mk_string "line" nm) 0, column := o.get_nat (name.mk_string "column" nm) 0, depth := o.get_nat (name.mk_string "depth" nm) 0, index := o.get_nat (name.mk_string "index" nm) 0, } meta def set_tactic_address (o : options) (nm : name) (addr : tactic_address) : options := let o := o.set_nat (name.mk_string "line" nm) addr.line in let o := o.set_nat (name.mk_string "column" nm) addr.column in let o := o.set_nat (name.mk_string "depth" nm) addr.depth in let o := o.set_nat (name.mk_string "index" nm) addr.index in o def is_same (a b : tactic_address) : bool := ( a.line = b.line ∧ a.column = b.column ∧ a.depth = b.depth ∧ a.index = b.index ) -- set the tactic state meta def set_state (new_state: tactic_state): tactic unit := -- this is in mathlib but easier to recreate λ _, interaction_monad.result.success () new_state meta def trace_tactic_state_data (addr: tactic_address) (finished : bool) := do -- BEGIN CUSTOMIZABLE CODE -- customize as needed to record different -- parts of the tactic state in different formats -- position data let t_key := (addr_key addr), let temporal := if finished then "after" else "before", let ts_key := t_key ++ ":" ++ temporal, trace_data_string "tactic_state" ts_key "tactic_instance" t_key, trace_data_string "tactic_state" ts_key "before_after" temporal, -- environment (just store fingerprints) env <- tactic.get_env, -- store fingerprint as a string to prevent large values errors trace_data_string "tactic_state" ts_key "env_fingerprint" (repr env.fingerprint), -- declaration decl <- tactic.decl_name, trace_data_string "tactic_state" ts_key "decl_name" (to_string decl), -- open namespaces open_nmspaces <- tactic.open_namespaces, trace_data_string "tactic_state" ts_key "open_namespaces" (string.intercalate " " (open_nmspaces.map repr)), -- goal stack information goals <- tactic.get_goals, trace_data_num "tactic_state" ts_key "goal_count" goals.length, let dump_all_goals : tactic unit := do { -- dump entire goal stack as a single goal (tactic_state_strings : list string) ← goals.mmap $ λ g, do { saved_ts ← tactic.read, tactic.set_goals [g], ts ← tactic.read, result ← PRINT_TACTIC_STATE ts, set_state saved_ts, pure result }, let g_key := ts_key ++ ":0", trace_data_string "tactic_state_goal" g_key "tactic_state" ts_key, trace_data_num "tactic_state_goal" g_key "ix" 0, trace_data_num "tactic_state_goal" g_key "goal_hash" ((goals.map expr.hash).foldr (+) 0), trace_data_string "tactic_state_goal" g_key "goal_pp" ("\n\n".intercalate tactic_state_strings) }, dump_all_goals, -- -- individual goal information -- goals.enum.mmap' $ λ ⟨n, g⟩, (do -- let g_key := ts_key ++ ":" ++ (repr n), -- trace_data_string "tactic_state_goal" g_key "tactic_state" ts_key, -- trace_data_num "tactic_state_goal" g_key "ix" n, -- -- store hash of goal metavariable to know if goal changed -- trace_data_num "tactic_state_goal" g_key "goal_hash" g.hash, -- -- pretty print the goal by temporarily making it the only goal -- saved_ts <- tactic.read, -- copy tactic state -- tactic.set_goals [g], -- ts <- tactic.read, -- temporary tactic state -- (printed_tactic_state : string) ← PRINT_TACTIC_STATE ts, -- trace_data_string "tactic_state_goal" g_key "goal_pp" printed_tactic_state, -- set_state saved_ts -- put tactic state back to way it was -- ), return () -- END CUSTOMIZABLE CODE meta def store_info_in_tactic_state (finished : bool) (line col : ℕ) : tactic unit := do let column := col + 1, -- use 1-indexed columns for convience -- get stored proof trace information o <- tactic.get_options, -- pop from the top of the stack let depth := o.get_nat `proof_rec.depth 0, let prev_open_addr := get_tactic_address o (mk_num_name `proof_rec.open depth), let block_addr := get_tactic_address o (mk_num_name `proof_rec.block (depth+1)), -- find the previous tactic let prev_addr := get_tactic_address o `proof_rec.prev, -- find the start of the proof let proof_addr := get_tactic_address o `proof_rec.proof, -- there are three cases. Handle each seperately match (finished, is_same prev_addr prev_open_addr) with | ⟨ ff, tt ⟩ := do -- starting a new tactic block -- calculate address let new_depth := depth + 1, let new_addr := { tactic_address . line := line, column := column, depth := new_depth, index := 1, }, let new_block_addr := new_addr, let new_proof_addr := if new_depth = 1 then new_addr else proof_addr, -- trace data to stdout trace_tactic_data_bool new_addr "executed" tt, trace_tactic_data_num new_addr "line" new_addr.line, trace_tactic_data_num new_addr "column" new_addr.column, trace_tactic_data_num new_addr "depth" new_addr.depth, trace_tactic_data_num new_addr "index" new_addr.index, trace_tactic_data_addr new_addr "proof" new_proof_addr, trace_tactic_data_addr new_addr "block" new_block_addr, trace_tactic_data_addr new_addr "parent" prev_open_addr, -- will be ⟨0,0,0,0⟩ if no parent trace_tactic_data_addr new_addr "prev" prev_addr, -- previous completed tactic (not same depth) -- trace data about the state beforehand trace_tactic_state_data new_addr ff, -- update proof trace information o <- tactic.get_options, let o := o.set_nat `proof_rec.depth new_depth, let o := set_tactic_address o (mk_num_name `proof_rec.open new_depth) new_addr, let o := set_tactic_address o (mk_num_name `proof_rec.block new_depth) new_block_addr, let o := set_tactic_address o `proof_rec.proof new_proof_addr, let o := set_tactic_address o `proof_rec.prev new_addr, tactic.set_options o, return () | ⟨ ff, ff ⟩ := do -- starting new tactic at same depth as previous tactic -- calculate address assert $ (prev_addr.depth = depth + 1), assert $ (block_addr.depth = depth + 1), let new_depth := prev_addr.depth, let new_addr := { tactic_address . line := line, column := column, depth := new_depth, index := prev_addr.index + 1, }, -- trace data to stdout trace_tactic_data_bool new_addr "executed" tt, trace_tactic_data_num new_addr "line" new_addr.line, trace_tactic_data_num new_addr "column" new_addr.column, trace_tactic_data_num new_addr "depth" new_addr.depth, trace_tactic_data_num new_addr "index" new_addr.index, trace_tactic_data_addr new_addr "proof" proof_addr, trace_tactic_data_addr new_addr "block" block_addr, trace_tactic_data_addr new_addr "parent" prev_open_addr, -- will be ⟨0,0,0,0⟩ if no parent trace_tactic_data_addr new_addr "prev" prev_addr, -- trace data about the state beforehand trace_tactic_state_data new_addr ff, -- update proof trace information (only information which changes) o <- tactic.get_options, let o := o.set_nat `proof_rec.depth new_depth, let o := set_tactic_address o (mk_num_name `proof_rec.open new_depth) new_addr, let o := set_tactic_address o `proof_rec.prev new_addr, tactic.set_options o, return () | ⟨ tt, _ ⟩ := do -- tactic completed successfully -- calculate address assert $ (line = prev_open_addr.line) ∧ (column = prev_open_addr.column) ∧ (depth = prev_open_addr.depth), let new_addr := prev_open_addr, -- trace data to stdout trace_tactic_data_bool prev_open_addr "succeeded" tt, -- trace data about the state afterward trace_tactic_state_data new_addr tt, -- update proof trace information (only information which changes) o <- tactic.get_options, let o := o.set_nat `proof_rec.depth (depth - 1), let o := set_tactic_address o `proof_rec.prev new_addr, tactic.set_options o, return () end meta def step_and_record {α : Type u} (line col : ℕ) (t : tactic α) : tactic unit := do -- only record if the pp.colors flag is set to false -- we can't make our own system option, so re-using -- one built in. (Even thought we are setting it to -- the default, we can still check that it is set.) o <- tactic.get_options, if bnot (o.get_bool `pp.colors tt) then do store_info_in_tactic_state ff line col, -- before tactic.step t, store_info_in_tactic_state tt line col -- after else tactic.step t end pr -- redefined istep to do proof recording meta def tactic.istep {α : Type u} (line0 col0 : ℕ) (line col : ℕ) (t : tactic α) : tactic unit := λ s, (@scope_trace _ line col (λ _, pr.step_and_record line col t s)).clamp_pos line0 line col --PR END MODIFICATION
4e967fbff6ca9965a516c475b08f4f9019cc73fe
fc086f79b20cf002d6f34b023749998408e94fbf
/examples/proofs-by-induction.lean
189360495d8db4f025657dd17eeb2ff94d237156
[]
no_license
semorrison/lean-tidy
f039460136b898fb282f75efedd92f2d5c5d90f8
6c1d46de6cff05e1c2c4c9692af812bca3e13b6c
refs/heads/master
1,624,461,332,392
1,559,655,744,000
1,559,655,744,000
96,569,994
9
4
null
1,538,287,895,000
1,499,455,306,000
Lean
UTF-8
Lean
false
false
3,771
lean
import algebra.big_operators data.fintype import tactic.ring import tidy.tidy -- https://xenaproject.wordpress.com/2018/03/30/proofs-by-induction/ open nat def odd : ℕ → ℕ := λ i, 2 * i + 1 def square : ℕ → ℕ := λ i, i * i @[ematch] theorem odd_square_inductive_step (d : ℕ) : odd d + square d = square (d+1) := begin dsimp [square, odd], ring, end namespace def1 definition my_sum_to_n (summand : ℕ → ℕ) : ℕ → ℕ | 0 := 0 | (n+1) := my_sum_to_n n + summand n -- TODO do we really need this? Can we write a tactic that unfolds a definition if it matches a case? @[simp] theorem my_successor_theorem (summand : ℕ → ℕ) (n : ℕ) : my_sum_to_n summand (n+1) = my_sum_to_n summand n + summand n := by obviously -- FIXME can't use obviously here: `abstract` causes problems. theorem my_odd_square_theorem : ∀ (n : ℕ), my_sum_to_n odd n = square n | 0 := rfl | (n+1) := begin obviously'' end end def1 namespace def2 definition my_sum_to_n (summand : ℕ → ℕ) : ℕ → ℕ := λ n, ((list.range n).map summand).sum theorem my_zero_theorem (summand : ℕ → ℕ) : my_sum_to_n summand 0 = 0 := rfl theorem my_successor_theorem (summand : ℕ → ℕ) (n : ℕ) : my_sum_to_n summand (succ n) = my_sum_to_n summand n + summand n := by unfold my_sum_to_n; simp [list.range_concat] theorem my_odd_square_theorem : ∀ (n : ℕ), my_sum_to_n odd n = square n | 0 := rfl | (succ n) := by rw [my_successor_theorem, my_odd_square_theorem n]; simp; exact odd_square_inductive_step n end def2 namespace def3 definition my_sum_to_n (summand : ℕ → ℕ) : ℕ → ℕ := λ n, (finset.range n).sum summand theorem my_zero_theorem (summand : ℕ → ℕ) : my_sum_to_n summand 0 = 0 := rfl theorem my_successor_theorem (summand : ℕ → ℕ) (n : ℕ) : my_sum_to_n summand (succ n) = my_sum_to_n summand n + summand n := by unfold my_sum_to_n; simp theorem my_odd_square_theorem : ∀ (n : ℕ), my_sum_to_n odd n = square n | 0 := rfl | (succ n) := by rw [my_successor_theorem, my_odd_square_theorem n]; simp; exact odd_square_inductive_step n end def3 namespace def4 open finset nat -- Credits to Chris Hughes theorem chris (n : ℕ) (f : ℕ → ℕ) (g : fin n → ℕ) (h : ∀ i : fin n, f i.1 = g i) : (range n).sum f = univ.sum g := sum_bij (λ i h, ⟨i, mem_range.1 h⟩) (λ i h, mem_univ _) (λ a ha, h ⟨a, _⟩) (λ _ _ _ _, fin.veq_of_eq) (λ ⟨b, hb⟩ _, ⟨b, mem_range.2 hb, rfl⟩) definition my_sum_to_n (summand : ℕ → ℕ) : ℕ → ℕ := λ n, (@finset.univ (fin n) _).sum (summand ∘ fin.val) theorem my_zero_theorem (summand : ℕ → ℕ) : my_sum_to_n summand 0 = 0 := rfl -- Credits to Mario Carneiro theorem my_successor_theorem (summand : ℕ → ℕ) (n : ℕ) : my_sum_to_n summand (succ n) = my_sum_to_n summand n + summand n := by unfold my_sum_to_n; rw [← chris _ _ _ (λ _, rfl), ← chris _ _ _ (λ _, rfl)]; simp theorem my_odd_square_theorem : ∀ (n : ℕ), my_sum_to_n odd n = square n | 0 := rfl | (succ n) := by rw [my_successor_theorem, my_odd_square_theorem n]; simp; exact odd_square_inductive_step n end def4 namespace equality theorem def12 : def1.my_sum_to_n = def2.my_sum_to_n := funext $ λ summand, funext $ λ n, nat.rec_on n rfl $ λ m ih, by rw [def1.my_successor_theorem, def2.my_successor_theorem, ih] theorem def23 : def2.my_sum_to_n = def3.my_sum_to_n := funext $ λ summand, funext $ λ n, nat.rec_on n rfl $ λ m ih, by rw [def2.my_successor_theorem, def3.my_successor_theorem, ih] theorem def34 : def3.my_sum_to_n = def4.my_sum_to_n := funext $ λ summand, funext $ λ n, nat.rec_on n rfl $ λ m ih, by rw [def3.my_successor_theorem, def4.my_successor_theorem, ih] end equality
da72ff5c29fe8f70800c94d6704b8309099da632
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/category_theory/limits/concrete_category.lean
151489d19408b8f8fe86407f30571bc0649cfb42
[ "Apache-2.0" ]
permissive
abentkamp/mathlib
d9a75d291ec09f4637b0f30cc3880ffb07549ee5
5360e476391508e092b5a1e5210bd0ed22dc0755
refs/heads/master
1,682,382,954,948
1,622,106,077,000
1,622,106,077,000
149,285,665
0
0
null
null
null
null
UTF-8
Lean
false
false
528
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.limits.has_limits import category_theory.concrete_category.basic import tactic.elementwise /-! # Facts about (co)limits of functors into concrete categories -/ universes w v u open category_theory namespace category_theory.limits attribute [elementwise] cone.w limit.lift_π limit.w cocone.w colimit.ι_desc colimit.w end category_theory.limits
db5f1bb00245d212a408be0b25ef93e904121528
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/test/mk_iff_of_inductive.lean
f8bafe42a7c8911decf856877e5ad2e4237e3e6f
[ "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
1,004
lean
import data.list.perm import data.multiset.basic mk_iff_of_inductive_prop list.chain test.chain_iff mk_iff_of_inductive_prop false test.false_iff mk_iff_of_inductive_prop true test.true_iff mk_iff_of_inductive_prop nonempty test.non_empty_iff mk_iff_of_inductive_prop and test.and_iff mk_iff_of_inductive_prop or test.or_iff mk_iff_of_inductive_prop eq test.eq_iff mk_iff_of_inductive_prop heq test.heq_iff mk_iff_of_inductive_prop list.perm test.perm_iff mk_iff_of_inductive_prop list.pairwise test.pairwise_iff inductive test.is_true (p : Prop) : Prop | triviality : p → test.is_true mk_iff_of_inductive_prop test.is_true test.is_true_iff @[mk_iff] structure foo (m n : ℕ) : Prop := (equal : m = n) (sum_eq_two : m + n = 2) example (m n : ℕ) : foo m n ↔ m = n ∧ m + n = 2 := foo_iff m n @[mk_iff bar] structure foo2 (m n : ℕ) : Prop := (equal : m = n) (sum_eq_two : m + n = 2) example (m n : ℕ) : foo2 m n ↔ m = n ∧ m + n = 2 := bar m n
e62a1017106f72cb48aa9f420456b61c9a3298a1
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/measure_theory/integral/layercake.lean
058d48d0699f950fa7dd0a2d93b52296b9db076e
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
18,279
lean
/- Copyright (c) 2022 Kalle Kytölä. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kalle Kytölä -/ import measure_theory.integral.interval_integral import analysis.special_functions.integrals /-! # The layer cake formula / Cavalieri's principle / tail probability formula > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we prove the following layer cake formula. Consider a non-negative measurable function `f` on a sigma-finite measure space. Apply pointwise to it an increasing absolutely continuous function `G : ℝ≥0 → ℝ≥0` vanishing at the origin, with derivative `G' = g` on the positive real line (in other words, `G` a primitive of a non-negative locally integrable function `g` on the positive real line). Then the integral of the result, `∫ G ∘ f`, can be written as the integral over the positive real line of the "tail measures" of `f` (i.e., a function giving the measures of the sets on which `f` exceeds different positive real values) weighted by `g`. In probability theory contexts, the "tail measures" could be referred to as "tail probabilities" of the random variable `f`, or as values of the "complementary cumulative distribution function" of the random variable `f`. The terminology "tail probability formula" is therefore occasionally used for the layer cake formula (or a standard application of it). The essence of the (mathematical) proof is Fubini's theorem. We also give the two most common applications of the layer cake formula * a representation of the integral of a nonnegative function f: ∫ f(ω) ∂μ(ω) = ∫ μ {ω | f(ω) ≥ t} dt * a representation of the integral of the p:th power of a nonnegative function f: ∫ f(ω)^p ∂μ(ω) = p * ∫ t^(p-1) * μ {ω | f(ω) ≥ t} dt . Variants of the formulas with measures of sets of the form {ω | f(ω) > t} instead of {ω | f(ω) ≥ t} are also included. ## Main results * `lintegral_comp_eq_lintegral_meas_le_mul` and `lintegral_comp_eq_lintegral_meas_lt_mul`: The general layer cake formulas with Lebesgue integrals, written in terms of measures of sets of the forms {ω | t ≤ f(ω)} and {ω | t < f(ω)}, respectively. * `lintegral_eq_lintegral_meas_le` and `lintegral_eq_lintegral_meas_lt`: The most common special cases of the layer cake formulas, stating that for a nonnegative function f we have ∫ f(ω) ∂μ(ω) = ∫ μ {ω | f(ω) ≥ t} dt and ∫ f(ω) ∂μ(ω) = ∫ μ {ω | f(ω) > t} dt, respectively. * `lintegral_rpow_eq_lintegral_meas_le_mul` and `lintegral_rpow_eq_lintegral_meas_lt_mul`: Other common special cases of the layer cake formulas, stating that for a nonnegative function f and p > 0, we have ∫ f(ω)^p ∂μ(ω) = p * ∫ μ {ω | f(ω) ≥ t} * t^(p-1) dt and ∫ f(ω)^p ∂μ(ω) = p * ∫ μ {ω | f(ω) > t} * t^(p-1) dt, respectively. ## Tags layer cake representation, Cavalieri's principle, tail probability formula -/ noncomputable theory open_locale ennreal measure_theory open set measure_theory filter /-! ### Layercake formula -/ section layercake namespace measure_theory variables {α : Type*} [measurable_space α] {f : α → ℝ} {g : ℝ → ℝ} {s : set α} /-- An auxiliary version of the layer cake formula (Cavalieri's principle, tail probability formula), with a measurability assumption that would also essentially follow from the integrability assumptions. See `measure_theory.lintegral_comp_eq_lintegral_meas_le_mul` and `measure_theory.lintegral_comp_eq_lintegral_meas_lt_mul` for the main formulations of the layer cake formula. -/ lemma lintegral_comp_eq_lintegral_meas_le_mul_of_measurable (μ : measure α) [sigma_finite μ] (f_nn : 0 ≤ f) (f_mble : measurable f) (g_intble : ∀ t > 0, interval_integrable g volume 0 t) (g_mble : measurable g) (g_nn : ∀ t > 0, 0 ≤ g t) : ∫⁻ ω, ennreal.of_real (∫ t in 0 .. (f ω), g t) ∂μ = ∫⁻ t in Ioi 0, (μ {a : α | t ≤ f a}) * ennreal.of_real (g t) := begin have g_intble' : ∀ (t : ℝ), 0 ≤ t → interval_integrable g volume 0 t, { intros t ht, cases eq_or_lt_of_le ht, { simp [← h], }, { exact g_intble t h, }, }, have integrand_eq : ∀ ω, ennreal.of_real (∫ t in 0 .. (f ω), g t) = ∫⁻ t in Ioc 0 (f ω), ennreal.of_real (g t), { intro ω, have g_ae_nn : 0 ≤ᵐ[volume.restrict (Ioc 0 (f ω))] g, { filter_upwards [self_mem_ae_restrict (measurable_set_Ioc : measurable_set (Ioc 0 (f ω)))] with x hx using g_nn x hx.1, }, rw ← of_real_integral_eq_lintegral_of_real (g_intble' (f ω) (f_nn ω)).1 g_ae_nn, congr, exact interval_integral.integral_of_le (f_nn ω), }, simp_rw [integrand_eq, ← lintegral_indicator (λ t, ennreal.of_real (g t)) measurable_set_Ioc, ← lintegral_indicator _ measurable_set_Ioi], rw lintegral_lintegral_swap, { apply congr_arg, funext s, have aux₁ : (λ x, (Ioc 0 (f x)).indicator (λ (t : ℝ), ennreal.of_real (g t)) s) = (λ x, (ennreal.of_real (g s) * (Ioi (0 : ℝ)).indicator (λ _, 1) s) * (Ici s).indicator (λ (t : ℝ), (1 : ℝ≥0∞)) (f x)), { funext a, by_cases s ∈ Ioc (0 : ℝ) (f a), { simp only [h, (show s ∈ Ioi (0 : ℝ), from h.1), (show f a ∈ Ici s, from h.2), indicator_of_mem, mul_one], }, { have h_copy := h, simp only [mem_Ioc, not_and, not_le] at h, by_cases h' : 0 < s, { simp only [h_copy, h h', indicator_of_not_mem, not_false_iff, mem_Ici, not_le, mul_zero], }, { have : s ∉ Ioi (0 : ℝ) := h', simp only [this, h', indicator_of_not_mem, not_false_iff, mul_zero, zero_mul, mem_Ioc, false_and], }, }, }, simp_rw aux₁, rw lintegral_const_mul', swap, { apply ennreal.mul_ne_top ennreal.of_real_ne_top, by_cases s ∈ Ioi (0 : ℝ); { simp [h], }, }, simp_rw [(show (λ a, (Ici s).indicator (λ (t : ℝ), (1 : ℝ≥0∞)) (f a)) = (λ a, {a : α | s ≤ f a}.indicator (λ _, 1) a), by { funext a, by_cases s ≤ f a; simp [h], })], rw lintegral_indicator, swap, { exact f_mble measurable_set_Ici, }, rw [lintegral_one, measure.restrict_apply measurable_set.univ, univ_inter, indicator_mul_left, mul_assoc, (show (Ioi 0).indicator (λ (_x : ℝ), (1 : ℝ≥0∞)) s * μ {a : α | s ≤ f a} = (Ioi 0).indicator (λ (_x : ℝ), 1 * μ {a : α | s ≤ f a}) s, by { by_cases 0 < s; simp [h], })], simp_rw [mul_comm _ (ennreal.of_real _), one_mul], refl, }, have aux₂ : function.uncurry (λ (x : α) (y : ℝ), (Ioc 0 (f x)).indicator (λ (t : ℝ), ennreal.of_real (g t)) y) = {p : α × ℝ | p.2 ∈ Ioc 0 (f p.1)}.indicator (λ p, ennreal.of_real (g p.2)), { funext p, cases p, rw function.uncurry_apply_pair, by_cases p_snd ∈ Ioc 0 (f p_fst), { have h' : (p_fst, p_snd) ∈ {p : α × ℝ | p.snd ∈ Ioc 0 (f p.fst)} := h, rw [set.indicator_of_mem h', set.indicator_of_mem h], }, { have h' : (p_fst, p_snd) ∉ {p : α × ℝ | p.snd ∈ Ioc 0 (f p.fst)} := h, rw [set.indicator_of_not_mem h', set.indicator_of_not_mem h], }, }, rw aux₂, have mble := measurable_set_region_between_oc measurable_zero f_mble measurable_set.univ, simp_rw [mem_univ, pi.zero_apply, true_and] at mble, exact (ennreal.measurable_of_real.comp (g_mble.comp measurable_snd)).ae_measurable.indicator mble, end /-- The layer cake formula / Cavalieri's principle / tail probability formula: Let `f` be a non-negative measurable function on a sigma-finite measure space. Let `G` be an increasing absolutely continuous function on the positive real line, vanishing at the origin, with derivative `G' = g`. Then the integral of the composition `G ∘ f` can be written as the integral over the positive real line of the "tail measures" `μ {ω | f(ω) ≥ t}` of `f` weighted by `g`. Roughly speaking, the statement is: `∫⁻ (G ∘ f) ∂μ = ∫⁻ t in 0 .. ∞, g(t) * μ {ω | f(ω) ≥ t}`. See `lintegral_comp_eq_lintegral_meas_lt_mul` for a version with sets of the form `{ω | f(ω) > t}` instead. -/ theorem lintegral_comp_eq_lintegral_meas_le_mul (μ : measure α) [sigma_finite μ] (f_nn : 0 ≤ f) (f_mble : measurable f) (g_intble : ∀ t > 0, interval_integrable g volume 0 t) (g_nn : ∀ᵐ t ∂(volume.restrict (Ioi 0)), 0 ≤ g t) : ∫⁻ ω, ennreal.of_real (∫ t in 0 .. f ω, g t) ∂μ = ∫⁻ t in Ioi 0, μ {a : α | t ≤ f a} * ennreal.of_real (g t) := begin have ex_G : ∃ (G : ℝ → ℝ), measurable G ∧ 0 ≤ G ∧ g =ᵐ[volume.restrict (Ioi 0)] G, { refine ae_measurable.exists_measurable_nonneg _ g_nn, exact ae_measurable_Ioi_of_forall_Ioc (λ t ht, (g_intble t ht).1.1.ae_measurable), }, rcases ex_G with ⟨G, G_mble, G_nn, g_eq_G⟩, have g_eq_G_on : ∀ t, g =ᵐ[volume.restrict (Ioc 0 t)] G, from λ t, ae_mono (measure.restrict_mono Ioc_subset_Ioi_self le_rfl) g_eq_G, have G_intble : ∀ t > 0, interval_integrable G volume 0 t, { refine λ t t_pos, ⟨(g_intble t t_pos).1.congr_fun_ae (g_eq_G_on t), _⟩, rw Ioc_eq_empty_of_le t_pos.lt.le, exact integrable_on_empty, }, have eq₁ : ∫⁻ t in Ioi 0, μ {a : α | t ≤ f a} * ennreal.of_real (g t) = ∫⁻ t in Ioi 0, μ {a : α | t ≤ f a} * ennreal.of_real (G t), { apply lintegral_congr_ae, filter_upwards [g_eq_G] with a ha, rw [ha], }, have eq₂ : ∀ ω, ∫ t in 0..f ω, g t = ∫ t in 0..f ω, G t, { refine λ ω, interval_integral.integral_congr_ae _, have fω_nn : 0 ≤ f ω := f_nn ω, rw [uIoc_of_le fω_nn, ← ae_restrict_iff' (measurable_set_Ioc : measurable_set (Ioc (0 : ℝ) (f ω)))], exact g_eq_G_on (f ω), }, simp_rw [eq₁, eq₂], exact lintegral_comp_eq_lintegral_meas_le_mul_of_measurable μ f_nn f_mble G_intble G_mble (λ t t_pos, G_nn t), end /-- The standard case of the layer cake formula / Cavalieri's principle / tail probability formula: For a nonnegative function `f` on a sigma-finite measure space, the Lebesgue integral of `f` can be written (roughly speaking) as: `∫⁻ f ∂μ = ∫⁻ t in 0 .. ∞, μ {ω | f(ω) ≥ t}`. See `lintegral_eq_lintegral_meas_lt` for a version with sets of the form `{ω | f(ω) > t}` instead. -/ theorem lintegral_eq_lintegral_meas_le (μ : measure α) [sigma_finite μ] (f_nn : 0 ≤ f) (f_mble : measurable f) : ∫⁻ ω, ennreal.of_real (f ω) ∂μ = ∫⁻ t in Ioi 0, (μ {a : α | t ≤ f a}) := begin set cst := λ (t : ℝ), (1 : ℝ) with def_cst, have cst_intble : ∀ t > 0, interval_integrable cst volume 0 t, from λ _ _, interval_integrable_const, have key := lintegral_comp_eq_lintegral_meas_le_mul μ f_nn f_mble cst_intble (eventually_of_forall (λ t, zero_le_one)), simp_rw [def_cst, ennreal.of_real_one, mul_one] at key, rw ← key, congr' with ω, simp only [interval_integral.integral_const, sub_zero, algebra.id.smul_eq_mul, mul_one], end /-- An application of the layer cake formula / Cavalieri's principle / tail probability formula: For a nonnegative function `f` on a sigma-finite measure space, the Lebesgue integral of `f` can be written (roughly speaking) as: `∫⁻ f^p ∂μ = p * ∫⁻ t in 0 .. ∞, t^(p-1) * μ {ω | f(ω) ≥ t}`. See `lintegral_rpow_eq_lintegral_meas_lt_mul` for a version with sets of the form `{ω | f(ω) > t}` instead. -/ theorem lintegral_rpow_eq_lintegral_meas_le_mul (μ : measure α) [sigma_finite μ] (f_nn : 0 ≤ f) (f_mble : measurable f) {p : ℝ} (p_pos: 0 < p) : ∫⁻ ω, ennreal.of_real ((f ω)^p) ∂μ = (ennreal.of_real p) * ∫⁻ t in Ioi 0, (μ {a : α | t ≤ f a}) * ennreal.of_real (t^(p-1)) := begin have one_lt_p : -1 < p - 1 := by linarith, have obs : ∀ (x : ℝ), (∫ (t : ℝ) in 0..x, t^(p-1)) = x^p / p, { intros x, rw integral_rpow (or.inl one_lt_p), simp [real.zero_rpow p_pos.ne.symm], }, set g := λ (t : ℝ), t^(p-1) with g_def, have g_nn : ∀ᵐ t ∂(volume.restrict (Ioi (0 : ℝ))), 0 ≤ g t, { filter_upwards [self_mem_ae_restrict (measurable_set_Ioi : measurable_set (Ioi (0 : ℝ)))], intros t t_pos, rw g_def, exact real.rpow_nonneg_of_nonneg (mem_Ioi.mp t_pos).le (p - 1), }, have g_intble : ∀ t > 0, interval_integrable g volume 0 t, from λ _ _, interval_integral.interval_integrable_rpow' one_lt_p, have key := lintegral_comp_eq_lintegral_meas_le_mul μ f_nn f_mble g_intble g_nn, simp_rw [g_def] at key, rw [← key, ← lintegral_const_mul (ennreal.of_real p)]; simp_rw obs, { congr' with ω, rw [← ennreal.of_real_mul p_pos.le, mul_div_cancel' ((f ω)^p) p_pos.ne.symm], }, { exact ((f_mble.pow measurable_const).div_const p).ennreal_of_real, }, end end measure_theory end layercake section layercake_lt open measure_theory variables {α : Type*} [measurable_space α] (μ : measure α) variables {β : Type*} [measurable_space β] [measurable_singleton_class β] namespace measure lemma meas_le_ne_meas_lt_subset_meas_pos {R : Type*} [linear_order R] [measurable_space R] [measurable_singleton_class R] {g : α → R} (g_mble : measurable g) {t : R} (ht : μ {a : α | t ≤ g a} ≠ μ {a : α | t < g a}) : 0 < μ {a : α | g a = t} := begin have uni : {a : α | t ≤ g a } = {a : α | t < g a} ∪ {a : α | t = g a}, { ext a, simp only [mem_set_of_eq, mem_union], apply le_iff_lt_or_eq, }, rw (show {a : α | t = g a} = {a : α | g a = t}, by simp_rw [eq_comm]) at uni, have disj : {a : α | t < g a} ∩ {a : α | g a = t} = ∅, { ext a, simp only [mem_inter_iff, mem_set_of_eq, mem_empty_iff_false, iff_false, not_and], exact ne_of_gt, }, have μ_add : μ {a : α | t ≤ g a} = μ {a : α | t < g a} + μ {a : α | g a = t}, by rw [uni, measure_union (disjoint_iff_inter_eq_empty.mpr disj) (g_mble (finite.measurable_set (finite_singleton t)))], by_contra con, rw [not_lt, nonpos_iff_eq_zero] at con, rw [con, add_zero] at μ_add, exact ht μ_add, end lemma countable_meas_le_ne_meas_lt [sigma_finite μ] {R : Type*} [linear_order R] [measurable_space R] [measurable_singleton_class R] {g : α → R} (g_mble : measurable g) : {t : R | μ {a : α | t ≤ g a } ≠ μ {a : α | t < g a}}.countable := countable.mono (show _, from λ t ht, meas_le_ne_meas_lt_subset_meas_pos μ g_mble ht) (measure.countable_meas_level_set_pos g_mble) lemma meas_le_ae_eq_meas_lt [sigma_finite μ] {R : Type*} [linear_order R] [measurable_space R] [measurable_singleton_class R] (ν : measure R) [has_no_atoms ν] {g : α → R} (g_mble : measurable g) : (λ t, μ {a : α | t ≤ g a}) =ᵐ[ν] (λ t, μ {a : α | t < g a}) := set.countable.measure_zero (measure.countable_meas_le_ne_meas_lt μ g_mble) _ end measure variables {f : α → ℝ} {g : ℝ → ℝ} {s : set α} /-- The layer cake formula / Cavalieri's principle / tail probability formula: Let `f` be a non-negative measurable function on a sigma-finite measure space. Let `G` be an increasing absolutely continuous function on the positive real line, vanishing at the origin, with derivative `G' = g`. Then the integral of the composition `G ∘ f` can be written as the integral over the positive real line of the "tail measures" `μ {ω | f(ω) > t}` of `f` weighted by `g`. Roughly speaking, the statement is: `∫⁻ (G ∘ f) ∂μ = ∫⁻ t in 0 .. ∞, g(t) * μ {ω | f(ω) > t}`. See `lintegral_comp_eq_lintegral_meas_le_mul` for a version with sets of the form `{ω | f(ω) ≥ t}` instead. -/ theorem lintegral_comp_eq_lintegral_meas_lt_mul (μ : measure α) [sigma_finite μ] (f_nn : 0 ≤ f) (f_mble : measurable f) (g_intble : ∀ t > 0, interval_integrable g volume 0 t) (g_nn : ∀ᵐ t ∂(volume.restrict (Ioi 0)), 0 ≤ g t) : ∫⁻ ω, ennreal.of_real (∫ t in 0 .. f ω, g t) ∂μ = ∫⁻ t in Ioi 0, μ {a : α | t < f a} * ennreal.of_real (g t) := begin rw lintegral_comp_eq_lintegral_meas_le_mul μ f_nn f_mble g_intble g_nn, apply lintegral_congr_ae, filter_upwards [measure.meas_le_ae_eq_meas_lt μ (volume.restrict (Ioi 0)) f_mble] with t ht, rw ht, end /-- The standard case of the layer cake formula / Cavalieri's principle / tail probability formula: For a nonnegative function `f` on a sigma-finite measure space, the Lebesgue integral of `f` can be written (roughly speaking) as: `∫⁻ f ∂μ = ∫⁻ t in 0 .. ∞, μ {ω | f(ω) > t}`. See `lintegral_eq_lintegral_meas_le` for a version with sets of the form `{ω | f(ω) ≥ t}` instead. -/ theorem lintegral_eq_lintegral_meas_lt (μ : measure α) [sigma_finite μ] (f_nn : 0 ≤ f) (f_mble : measurable f) : ∫⁻ ω, ennreal.of_real (f ω) ∂μ = ∫⁻ t in Ioi 0, (μ {a : α | t < f a}) := begin rw lintegral_eq_lintegral_meas_le μ f_nn f_mble, apply lintegral_congr_ae, filter_upwards [measure.meas_le_ae_eq_meas_lt μ (volume.restrict (Ioi 0)) f_mble] with t ht, rw ht, end /-- An application of the layer cake formula / Cavalieri's principle / tail probability formula: For a nonnegative function `f` on a sigma-finite measure space, the Lebesgue integral of `f` can be written (roughly speaking) as: `∫⁻ f^p ∂μ = p * ∫⁻ t in 0 .. ∞, t^(p-1) * μ {ω | f(ω) > t}`. See `lintegral_rpow_eq_lintegral_meas_le_mul` for a version with sets of the form `{ω | f(ω) ≥ t}` instead. -/ theorem lintegral_rpow_eq_lintegral_meas_lt_mul (μ : measure α) [sigma_finite μ] (f_nn : 0 ≤ f) (f_mble : measurable f) {p : ℝ} (p_pos: 0 < p) : ∫⁻ ω, ennreal.of_real ((f ω)^p) ∂μ = (ennreal.of_real p) * ∫⁻ t in Ioi 0, (μ {a : α | t < f a}) * ennreal.of_real (t^(p-1)) := begin rw lintegral_rpow_eq_lintegral_meas_le_mul μ f_nn f_mble p_pos, apply congr_arg (λ z, (ennreal.of_real p * z)), apply lintegral_congr_ae, filter_upwards [measure.meas_le_ae_eq_meas_lt μ (volume.restrict (Ioi 0)) f_mble] with t ht, rw ht, end end layercake_lt
2e85dc80e9d2e48ed7ffddb452db186bd29c4a07
a4673261e60b025e2c8c825dfa4ab9108246c32e
/src/Lean/Data/Occurrences.lean
8058c4512cd4b174ba92d795f4f619f67d3ae38a
[ "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
813
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ namespace Lean inductive Occurrences := | all | pos (idxs : List Nat) | neg (idxs : List Nat) namespace Occurrences instance : Inhabited Occurrences := ⟨all⟩ def contains : Occurrences → Nat → Bool | all, _ => true | pos idxs, idx => idxs.contains idx | neg idxs, idx => !idxs.contains idx def isAll : Occurrences → Bool | all => true | _ => false def beq : Occurrences → Occurrences → Bool | all, all => true | pos is₁, pos is₂ => is₁ == is₂ | neg is₁, neg is₂ => is₁ == is₂ | _, _ => false instance : BEq Occurrences := ⟨beq⟩ end Occurrences end Lean
2874635abefe5137f0f0ef6168ae6e3918c2312f
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/topology/algebra/ordered/monotone_continuity.lean
8605580df7ae9dd37da7082badc0f7928c7934bc
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
17,380
lean
/- Copyright (c) 2021 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov, Heather Macbeth -/ import topology.algebra.ordered.basic import topology.algebra.ordered.left_right /-! # Continuity of monotone functions In this file we prove the following fact: if `f` is a monotone function on a neighborhood of `a` and the image of this neighborhood is a neighborhood of `f a`, then `f` is continuous at `a`, see `continuous_at_of_monotone_on_of_image_mem_nhds`, as well as several similar facts. We also prove that an `order_iso` is continuous. ## Tags continuous, monotone -/ open set filter open_locale topological_space section linear_order variables {α β : Type*} [linear_order α] [topological_space α] [order_topology α] variables [linear_order β] [topological_space β] [order_topology β] /-- If `f` is a function strictly monotone on a right neighborhood of `a` and the image of this neighborhood under `f` meets every interval `(f a, b]`, `b > f a`, then `f` is continuous at `a` from the right. The assumption `hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioc (f a) b` is required because otherwise the function `f : ℝ → ℝ` given by `f x = if x ≤ 0 then x else x + 1` would be a counter-example at `a = 0`. -/ lemma strict_mono_on.continuous_at_right_of_exists_between {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝[Ici a] a) (hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioc (f a) b) : continuous_within_at f (Ici a) a := begin have ha : a ∈ Ici a := left_mem_Ici, have has : a ∈ s := mem_of_mem_nhds_within ha hs, refine tendsto_order.2 ⟨λ b hb, _, λ b hb, _⟩, { filter_upwards [hs, self_mem_nhds_within], intros x hxs hxa, exact hb.trans_le ((h_mono.le_iff_le has hxs).2 hxa) }, { rcases hfs b hb with ⟨c, hcs, hac, hcb⟩, rw [h_mono.lt_iff_lt has hcs] at hac, filter_upwards [hs, Ico_mem_nhds_within_Ici (left_mem_Ico.2 hac)], rintros x hx ⟨hax, hxc⟩, exact ((h_mono.lt_iff_lt hx hcs).2 hxc).trans_le hcb } end /-- If `f` is a monotone function on a right neighborhood of `a` and the image of this neighborhood under `f` meets every interval `(f a, b)`, `b > f a`, then `f` is continuous at `a` from the right. The assumption `hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioo (f a) b` cannot be replaced by the weaker assumption `hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioc (f a) b` we use for strictly monotone functions because otherwise the function `ceil : ℝ → ℤ` would be a counter-example at `a = 0`. -/ lemma continuous_at_right_of_monotone_on_of_exists_between {f : α → β} {s : set α} {a : α} (h_mono : monotone_on f s) (hs : s ∈ 𝓝[Ici a] a) (hfs : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioo (f a) b) : continuous_within_at f (Ici a) a := begin have ha : a ∈ Ici a := left_mem_Ici, have has : a ∈ s := mem_of_mem_nhds_within ha hs, refine tendsto_order.2 ⟨λ b hb, _, λ b hb, _⟩, { filter_upwards [hs, self_mem_nhds_within], intros x hxs hxa, exact hb.trans_le (h_mono has hxs hxa) }, { rcases hfs b hb with ⟨c, hcs, hac, hcb⟩, have : a < c, from not_le.1 (λ h, hac.not_le $ h_mono hcs has h), filter_upwards [hs, Ico_mem_nhds_within_Ici (left_mem_Ico.2 this)], rintros x hx ⟨hax, hxc⟩, exact (h_mono hx hcs hxc.le).trans_lt hcb } end /-- If a function `f` with a densely ordered codomain is monotone on a right neighborhood of `a` and the closure of the image of this neighborhood under `f` is a right neighborhood of `f a`, then `f` is continuous at `a` from the right. -/ lemma continuous_at_right_of_monotone_on_of_closure_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : monotone_on f s) (hs : s ∈ 𝓝[Ici a] a) (hfs : closure (f '' s) ∈ 𝓝[Ici (f a)] (f a)) : continuous_within_at f (Ici a) a := begin refine continuous_at_right_of_monotone_on_of_exists_between h_mono hs (λ b hb, _), rcases (mem_nhds_within_Ici_iff_exists_mem_Ioc_Ico_subset hb).1 hfs with ⟨b', ⟨hab', hbb'⟩, hb'⟩, rcases exists_between hab' with ⟨c', hc'⟩, rcases mem_closure_iff.1 (hb' ⟨hc'.1.le, hc'.2⟩) (Ioo (f a) b') is_open_Ioo hc' with ⟨_, hc, ⟨c, hcs, rfl⟩⟩, exact ⟨c, hcs, hc.1, hc.2.trans_le hbb'⟩ end /-- If a function `f` with a densely ordered codomain is monotone on a right neighborhood of `a` and the image of this neighborhood under `f` is a right neighborhood of `f a`, then `f` is continuous at `a` from the right. -/ lemma continuous_at_right_of_monotone_on_of_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : monotone_on f s) (hs : s ∈ 𝓝[Ici a] a) (hfs : f '' s ∈ 𝓝[Ici (f a)] (f a)) : continuous_within_at f (Ici a) a := continuous_at_right_of_monotone_on_of_closure_image_mem_nhds_within h_mono hs $ mem_of_superset hfs subset_closure /-- If a function `f` with a densely ordered codomain is strictly monotone on a right neighborhood of `a` and the closure of the image of this neighborhood under `f` is a right neighborhood of `f a`, then `f` is continuous at `a` from the right. -/ lemma strict_mono_on.continuous_at_right_of_closure_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝[Ici a] a) (hfs : closure (f '' s) ∈ 𝓝[Ici (f a)] (f a)) : continuous_within_at f (Ici a) a := continuous_at_right_of_monotone_on_of_closure_image_mem_nhds_within (λ x hx y hy, (h_mono.le_iff_le hx hy).2) hs hfs /-- If a function `f` with a densely ordered codomain is strictly monotone on a right neighborhood of `a` and the image of this neighborhood under `f` is a right neighborhood of `f a`, then `f` is continuous at `a` from the right. -/ lemma strict_mono_on.continuous_at_right_of_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝[Ici a] a) (hfs : f '' s ∈ 𝓝[Ici (f a)] (f a)) : continuous_within_at f (Ici a) a := h_mono.continuous_at_right_of_closure_image_mem_nhds_within hs (mem_of_superset hfs subset_closure) /-- If a function `f` is strictly monotone on a right neighborhood of `a` and the image of this neighborhood under `f` includes `Ioi (f a)`, then `f` is continuous at `a` from the right. -/ lemma strict_mono_on.continuous_at_right_of_surj_on {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝[Ici a] a) (hfs : surj_on f s (Ioi (f a))) : continuous_within_at f (Ici a) a := h_mono.continuous_at_right_of_exists_between hs $ λ b hb, let ⟨c, hcs, hcb⟩ := hfs hb in ⟨c, hcs, hcb.symm ▸ hb, hcb.le⟩ /-- If `f` is a strictly monotone function on a left neighborhood of `a` and the image of this neighborhood under `f` meets every interval `[b, f a)`, `b < f a`, then `f` is continuous at `a` from the left. The assumption `hfs : ∀ b < f a, ∃ c ∈ s, f c ∈ Ico b (f a)` is required because otherwise the function `f : ℝ → ℝ` given by `f x = if x < 0 then x else x + 1` would be a counter-example at `a = 0`. -/ lemma strict_mono_on.continuous_at_left_of_exists_between {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝[Iic a] a) (hfs : ∀ b < f a, ∃ c ∈ s, f c ∈ Ico b (f a)) : continuous_within_at f (Iic a) a := h_mono.dual.continuous_at_right_of_exists_between hs $ λ b hb, let ⟨c, hcs, hcb, hca⟩ := hfs b hb in ⟨c, hcs, hca, hcb⟩ /-- If `f` is a monotone function on a left neighborhood of `a` and the image of this neighborhood under `f` meets every interval `(b, f a)`, `b < f a`, then `f` is continuous at `a` from the left. The assumption `hfs : ∀ b < f a, ∃ c ∈ s, f c ∈ Ioo b (f a)` cannot be replaced by the weaker assumption `hfs : ∀ b < f a, ∃ c ∈ s, f c ∈ Ico b (f a)` we use for strictly monotone functions because otherwise the function `floor : ℝ → ℤ` would be a counter-example at `a = 0`. -/ lemma continuous_at_left_of_monotone_on_of_exists_between {f : α → β} {s : set α} {a : α} (hf : monotone_on f s) (hs : s ∈ 𝓝[Iic a] a) (hfs : ∀ b < f a, ∃ c ∈ s, f c ∈ Ioo b (f a)) : continuous_within_at f (Iic a) a := @continuous_at_right_of_monotone_on_of_exists_between (order_dual α) (order_dual β) _ _ _ _ _ _ f s a hf.dual hs $ λ b hb, let ⟨c, hcs, hcb, hca⟩ := hfs b hb in ⟨c, hcs, hca, hcb⟩ /-- If a function `f` with a densely ordered codomain is monotone on a left neighborhood of `a` and the closure of the image of this neighborhood under `f` is a left neighborhood of `f a`, then `f` is continuous at `a` from the left -/ lemma continuous_at_left_of_monotone_on_of_closure_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (hf : monotone_on f s) (hs : s ∈ 𝓝[Iic a] a) (hfs : closure (f '' s) ∈ 𝓝[Iic (f a)] (f a)) : continuous_within_at f (Iic a) a := @continuous_at_right_of_monotone_on_of_closure_image_mem_nhds_within (order_dual α) (order_dual β) _ _ _ _ _ _ _ f s a hf.dual hs hfs /-- If a function `f` with a densely ordered codomain is monotone on a left neighborhood of `a` and the image of this neighborhood under `f` is a left neighborhood of `f a`, then `f` is continuous at `a` from the left. -/ lemma continuous_at_left_of_monotone_on_of_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : monotone_on f s) (hs : s ∈ 𝓝[Iic a] a) (hfs : f '' s ∈ 𝓝[Iic (f a)] (f a)) : continuous_within_at f (Iic a) a := continuous_at_left_of_monotone_on_of_closure_image_mem_nhds_within h_mono hs (mem_of_superset hfs subset_closure) /-- If a function `f` with a densely ordered codomain is strictly monotone on a left neighborhood of `a` and the closure of the image of this neighborhood under `f` is a left neighborhood of `f a`, then `f` is continuous at `a` from the left. -/ lemma strict_mono_on.continuous_at_left_of_closure_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝[Iic a] a) (hfs : closure (f '' s) ∈ 𝓝[Iic (f a)] (f a)) : continuous_within_at f (Iic a) a := h_mono.dual.continuous_at_right_of_closure_image_mem_nhds_within hs hfs /-- If a function `f` with a densely ordered codomain is strictly monotone on a left neighborhood of `a` and the image of this neighborhood under `f` is a left neighborhood of `f a`, then `f` is continuous at `a` from the left. -/ lemma strict_mono_on.continuous_at_left_of_image_mem_nhds_within [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝[Iic a] a) (hfs : f '' s ∈ 𝓝[Iic (f a)] (f a)) : continuous_within_at f (Iic a) a := h_mono.dual.continuous_at_right_of_image_mem_nhds_within hs hfs /-- If a function `f` is strictly monotone on a left neighborhood of `a` and the image of this neighborhood under `f` includes `Iio (f a)`, then `f` is continuous at `a` from the left. -/ lemma strict_mono_on.continuous_at_left_of_surj_on {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝[Iic a] a) (hfs : surj_on f s (Iio (f a))) : continuous_within_at f (Iic a) a := h_mono.dual.continuous_at_right_of_surj_on hs hfs /-- If a function `f` is strictly monotone on a neighborhood of `a` and the image of this neighborhood under `f` meets every interval `[b, f a)`, `b < f a`, and every interval `(f a, b]`, `b > f a`, then `f` is continuous at `a`. -/ lemma strict_mono_on.continuous_at_of_exists_between {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝 a) (hfs_l : ∀ b < f a, ∃ c ∈ s, f c ∈ Ico b (f a)) (hfs_r : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioc (f a) b) : continuous_at f a := continuous_at_iff_continuous_left_right.2 ⟨h_mono.continuous_at_left_of_exists_between (mem_nhds_within_of_mem_nhds hs) hfs_l, h_mono.continuous_at_right_of_exists_between (mem_nhds_within_of_mem_nhds hs) hfs_r⟩ /-- If a function `f` with a densely ordered codomain is strictly monotone on a neighborhood of `a` and the closure of the image of this neighborhood under `f` is a neighborhood of `f a`, then `f` is continuous at `a`. -/ lemma strict_mono_on.continuous_at_of_closure_image_mem_nhds [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝 a) (hfs : closure (f '' s) ∈ 𝓝 (f a)) : continuous_at f a := continuous_at_iff_continuous_left_right.2 ⟨h_mono.continuous_at_left_of_closure_image_mem_nhds_within (mem_nhds_within_of_mem_nhds hs) (mem_nhds_within_of_mem_nhds hfs), h_mono.continuous_at_right_of_closure_image_mem_nhds_within (mem_nhds_within_of_mem_nhds hs) (mem_nhds_within_of_mem_nhds hfs)⟩ /-- If a function `f` with a densely ordered codomain is strictly monotone on a neighborhood of `a` and the image of this set under `f` is a neighborhood of `f a`, then `f` is continuous at `a`. -/ lemma strict_mono_on.continuous_at_of_image_mem_nhds [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : strict_mono_on f s) (hs : s ∈ 𝓝 a) (hfs : f '' s ∈ 𝓝 (f a)) : continuous_at f a := h_mono.continuous_at_of_closure_image_mem_nhds hs (mem_of_superset hfs subset_closure) /-- If `f` is a monotone function on a neighborhood of `a` and the image of this neighborhood under `f` meets every interval `(b, f a)`, `b < f a`, and every interval `(f a, b)`, `b > f a`, then `f` is continuous at `a`. -/ lemma continuous_at_of_monotone_on_of_exists_between {f : α → β} {s : set α} {a : α} (h_mono : monotone_on f s) (hs : s ∈ 𝓝 a) (hfs_l : ∀ b < f a, ∃ c ∈ s, f c ∈ Ioo b (f a)) (hfs_r : ∀ b > f a, ∃ c ∈ s, f c ∈ Ioo (f a) b) : continuous_at f a := continuous_at_iff_continuous_left_right.2 ⟨continuous_at_left_of_monotone_on_of_exists_between h_mono (mem_nhds_within_of_mem_nhds hs) hfs_l, continuous_at_right_of_monotone_on_of_exists_between h_mono (mem_nhds_within_of_mem_nhds hs) hfs_r⟩ /-- If a function `f` with a densely ordered codomain is monotone on a neighborhood of `a` and the closure of the image of this neighborhood under `f` is a neighborhood of `f a`, then `f` is continuous at `a`. -/ lemma continuous_at_of_monotone_on_of_closure_image_mem_nhds [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : monotone_on f s) (hs : s ∈ 𝓝 a) (hfs : closure (f '' s) ∈ 𝓝 (f a)) : continuous_at f a := continuous_at_iff_continuous_left_right.2 ⟨continuous_at_left_of_monotone_on_of_closure_image_mem_nhds_within h_mono (mem_nhds_within_of_mem_nhds hs) (mem_nhds_within_of_mem_nhds hfs), continuous_at_right_of_monotone_on_of_closure_image_mem_nhds_within h_mono (mem_nhds_within_of_mem_nhds hs) (mem_nhds_within_of_mem_nhds hfs)⟩ /-- If a function `f` with a densely ordered codomain is monotone on a neighborhood of `a` and the image of this neighborhood under `f` is a neighborhood of `f a`, then `f` is continuous at `a`. -/ lemma continuous_at_of_monotone_on_of_image_mem_nhds [densely_ordered β] {f : α → β} {s : set α} {a : α} (h_mono : monotone_on f s) (hs : s ∈ 𝓝 a) (hfs : f '' s ∈ 𝓝 (f a)) : continuous_at f a := continuous_at_of_monotone_on_of_closure_image_mem_nhds h_mono hs (mem_of_superset hfs subset_closure) /-- A monotone function with densely ordered codomain and a dense range is continuous. -/ lemma monotone.continuous_of_dense_range [densely_ordered β] {f : α → β} (h_mono : monotone f) (h_dense : dense_range f) : continuous f := continuous_iff_continuous_at.mpr $ λ a, continuous_at_of_monotone_on_of_closure_image_mem_nhds (λ x hx y hy hxy, h_mono hxy) univ_mem $ by simp only [image_univ, h_dense.closure_eq, univ_mem] /-- A monotone surjective function with a densely ordered codomain is continuous. -/ lemma monotone.continuous_of_surjective [densely_ordered β] {f : α → β} (h_mono : monotone f) (h_surj : function.surjective f) : continuous f := h_mono.continuous_of_dense_range h_surj.dense_range end linear_order /-! ### Continuity of order isomorphisms In this section we prove that an `order_iso` is continuous, hence it is a `homeomorph`. We prove this for an `order_iso` between to partial orders with order topology. -/ namespace order_iso variables {α β : Type*} [partial_order α] [partial_order β] [topological_space α] [topological_space β] [order_topology α] [order_topology β] protected lemma continuous (e : α ≃o β) : continuous e := begin rw [‹order_topology β›.topology_eq_generate_intervals], refine continuous_generated_from (λ s hs, _), rcases hs with ⟨a, rfl|rfl⟩, { rw e.preimage_Ioi, apply is_open_lt' }, { rw e.preimage_Iio, apply is_open_gt' } end /-- An order isomorphism between two linear order `order_topology` spaces is a homeomorphism. -/ def to_homeomorph (e : α ≃o β) : α ≃ₜ β := { continuous_to_fun := e.continuous, continuous_inv_fun := e.symm.continuous, .. e } @[simp] lemma coe_to_homeomorph (e : α ≃o β) : ⇑e.to_homeomorph = e := rfl @[simp] lemma coe_to_homeomorph_symm (e : α ≃o β) : ⇑e.to_homeomorph.symm = e.symm := rfl end order_iso
cf20f03cc5fb55d44f6f19eef65b15439b593d65
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/simpIssue.lean
9c52466a5b26dabfb8ca3a6d4dac60f9941685d4
[ "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
49
lean
example {a: Nat} (h: a = a): True := by simp_all
d63790b88654a27bb942ee026bb93499c0508361
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/topology/path_connected_auto.lean
25ccfa3495ee45bf52e02b7705b06acbcd4c1205
[]
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
29,692
lean
/- Copyright (c) 2020 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.topology.instances.real import Mathlib.topology.algebra.ordered.proj_Icc import Mathlib.PostPort universes u_1 l u_2 u_3 namespace Mathlib /-! # Path connectedness ## Main definitions In the file the unit interval `[0, 1]` in `ℝ` is denoted by `I`, and `X` is a topological space. * `path (x y : X)` is the type of paths from `x` to `y`, i.e., continuous maps from `I` to `X` mapping `0` to `x` and `1` to `y`. * `path.map` is the image of a path under a continuous map. * `joined (x y : X)` means there is a path between `x` and `y`. * `joined.some_path (h : joined x y)` selects some path between two points `x` and `y`. * `path_component (x : X)` is the set of points joined to `x`. * `path_connected_space X` is a predicate class asserting that `X` is non-empty and every two points of `X` are joined. Then there are corresponding relative notions for `F : set X`. * `joined_in F (x y : X)` means there is a path `γ` joining `x` to `y` with values in `F`. * `joined_in.some_path (h : joined_in F x y)` selects a path from `x` to `y` inside `F`. * `path_component_in F (x : X)` is the set of points joined to `x` in `F`. * `is_path_connected F` asserts that `F` is non-empty and every two points of `F` are joined in `F`. * `loc_path_connected_space X` is a predicate class asserting that `X` is locally path-connected: each point has a basis of path-connected neighborhoods (we do *not* ask these to be open). ## Main theorems * `joined` and `joined_in F` are transitive relations. One can link the absolute and relative version in two directions, using `(univ : set X)` or the subtype `↥F`. * `path_connected_space_iff_univ : path_connected_space X ↔ is_path_connected (univ : set X)` * `is_path_connected_iff_path_connected_space : is_path_connected F ↔ path_connected_space ↥F` For locally path connected spaces, we have * `path_connected_space_iff_connected_space : path_connected_space X ↔ connected_space X` * `is_connected_iff_is_path_connected (U_op : is_open U) : is_path_connected U ↔ is_connected U` ## Implementation notes By default, all paths have `I` as their source and `X` as their target, but there is an operation `set.Icc_extend` that will extend any continuous map `γ : I → X` into a continuous map `Icc_extend zero_le_one γ : ℝ → X` that is constant before `0` and after `1`. This is used to define `path.extend` that turns `γ : path x y` into a continuous map `γ.extend : ℝ → X` whose restriction to `I` is the original `γ`, and is equal to `x` on `(-∞, 0]` and to `y` on `[1, +∞)`. -/ /-! ### The unit interval -/ theorem Icc_zero_one_symm {t : ℝ} : t ∈ set.Icc 0 1 ↔ 1 - t ∈ set.Icc 0 1 := sorry protected instance I_has_zero : HasZero ↥(set.Icc 0 1) := { zero := { val := 0, property := sorry } } @[simp] theorem coe_I_zero : ↑0 = 0 := rfl protected instance I_has_one : HasOne ↥(set.Icc 0 1) := { one := { val := 1, property := sorry } } @[simp] theorem coe_I_one : ↑1 = 1 := rfl /-- Unit interval central symmetry. -/ def I_symm : ↥(set.Icc 0 1) → ↥(set.Icc 0 1) := fun (t : ↥(set.Icc 0 1)) => { val := 1 - subtype.val t, property := sorry } @[simp] theorem I_symm_zero : I_symm 0 = 1 := sorry @[simp] theorem I_symm_one : I_symm 1 = 0 := sorry theorem continuous_I_symm : continuous I_symm := continuous_subtype_mk (fun (x : ↥(set.Icc 0 1)) => I_symm._proof_1 x) (continuous.sub continuous_const continuous_subtype_val) protected instance set.Icc.connected_space : connected_space ↥(set.Icc 0 1) := subtype.connected_space { left := iff.mpr set.nonempty_Icc zero_le_one, right := is_preconnected_Icc } protected instance set.Icc.compact_space : compact_space ↥(set.Icc 0 1) := iff.mp compact_iff_compact_space compact_Icc /-! ### Paths -/ /-- Continuous path connecting two points `x` and `y` in a topological space -/ structure path {X : Type u_1} [topological_space X] (x : X) (y : X) where to_fun : ↥(set.Icc 0 1) → X continuous' : continuous to_fun source' : to_fun 0 = x target' : to_fun 1 = y protected instance path.has_coe_to_fun {X : Type u_1} [topological_space X] {x : X} {y : X} : has_coe_to_fun (path x y) := has_coe_to_fun.mk (fun (x : path x y) => ↥(set.Icc 0 1) → X) path.to_fun protected theorem path.ext {X : Type u_1} [topological_space X] {x : X} {y : X} {γ₁ : path x y} {γ₂ : path x y} : ⇑γ₁ = ⇑γ₂ → γ₁ = γ₂ := sorry namespace path @[simp] theorem coe_mk {X : Type u_1} [topological_space X] {x : X} {y : X} (f : ↥(set.Icc 0 1) → X) (h₁ : continuous f) (h₂ : f 0 = x) (h₃ : f 1 = y) : ⇑(mk f h₁ h₂ h₃) = f := rfl protected theorem continuous {X : Type u_1} [topological_space X] {x : X} {y : X} (γ : path x y) : continuous ⇑γ := continuous' γ @[simp] protected theorem source {X : Type u_1} [topological_space X] {x : X} {y : X} (γ : path x y) : coe_fn γ 0 = x := source' γ @[simp] protected theorem target {X : Type u_1} [topological_space X] {x : X} {y : X} (γ : path x y) : coe_fn γ 1 = y := target' γ /-- Any function `φ : Π (a : α), path (x a) (y a)` can be seen as a function `α × I → X`. -/ protected instance has_uncurry_path {X : Type u_1} {α : Type u_2} [topological_space X] {x : α → X} {y : α → X} : function.has_uncurry ((a : α) → path (x a) (y a)) (α × ↥(set.Icc 0 1)) X := function.has_uncurry.mk fun (φ : (a : α) → path (x a) (y a)) (p : α × ↥(set.Icc 0 1)) => coe_fn (φ (prod.fst p)) (prod.snd p) /-- The constant path from a point to itself -/ def refl {X : Type u_1} [topological_space X] (x : X) : path x x := mk (fun (t : ↥(set.Icc 0 1)) => x) sorry rfl rfl @[simp] theorem refl_range {X : Type u_1} [topological_space X] {a : X} : set.range ⇑(refl a) = singleton a := sorry /-- The reverse of a path from `x` to `y`, as a path from `y` to `x` -/ def symm {X : Type u_1} [topological_space X] {x : X} {y : X} (γ : path x y) : path y x := mk (⇑γ ∘ I_symm) sorry sorry sorry @[simp] theorem refl_symm {X : Type u_1} [topological_space X] {a : X} : symm (refl a) = refl a := path.ext (funext fun (x : ↥(set.Icc 0 1)) => Eq.refl (coe_fn (symm (refl a)) x)) @[simp] theorem symm_range {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) : set.range ⇑(symm γ) = set.range ⇑γ := sorry /-- A continuous map extending a path to `ℝ`, constant before `0` and after `1`. -/ def extend {X : Type u_1} [topological_space X] {x : X} {y : X} (γ : path x y) : ℝ → X := set.Icc_extend zero_le_one ⇑γ theorem continuous_extend {X : Type u_1} [topological_space X] {x : X} {y : X} (γ : path x y) : continuous (extend γ) := continuous.Icc_extend (path.continuous γ) @[simp] theorem extend_zero {X : Type u_1} [topological_space X] {x : X} {y : X} (γ : path x y) : extend γ 0 = x := Eq.trans (set.Icc_extend_left zero_le_one ⇑γ) (path.source γ) @[simp] theorem extend_one {X : Type u_1} [topological_space X] {x : X} {y : X} (γ : path x y) : extend γ 1 = y := Eq.trans (set.Icc_extend_right zero_le_one ⇑γ) (path.target γ) @[simp] theorem extend_extends {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) {t : ℝ} (ht : t ∈ set.Icc 0 1) : extend γ t = coe_fn γ { val := t, property := ht } := set.Icc_extend_of_mem zero_le_one (⇑γ) ht @[simp] theorem extend_extends' {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) (t : ↥(set.Icc 0 1)) : extend γ ↑t = coe_fn γ t := set.Icc_extend_coe zero_le_one (⇑γ) t @[simp] theorem extend_range {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) : set.range (extend γ) = set.range ⇑γ := set.Icc_extend_range zero_le_one ⇑γ theorem extend_of_le_zero {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) {t : ℝ} (ht : t ≤ 0) : extend γ t = a := Eq.trans (set.Icc_extend_of_le_left zero_le_one (⇑γ) ht) (path.source γ) theorem extend_of_one_le {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) {t : ℝ} (ht : 1 ≤ t) : extend γ t = b := Eq.trans (set.Icc_extend_of_right_le zero_le_one (⇑γ) ht) (path.target γ) @[simp] theorem refl_extend {X : Type u_1} [topological_space X] {a : X} : extend (refl a) = fun (_x : ℝ) => a := rfl /-- The path obtained from a map defined on `ℝ` by restriction to the unit interval. -/ def of_line {X : Type u_1} [topological_space X] {x : X} {y : X} {f : ℝ → X} (hf : continuous_on f (set.Icc 0 1)) (h₀ : f 0 = x) (h₁ : f 1 = y) : path x y := mk (f ∘ coe) sorry h₀ h₁ theorem of_line_mem {X : Type u_1} [topological_space X] {x : X} {y : X} {f : ℝ → X} (hf : continuous_on f (set.Icc 0 1)) (h₀ : f 0 = x) (h₁ : f 1 = y) (t : ↥(set.Icc 0 1)) : coe_fn (of_line hf h₀ h₁) t ∈ f '' set.Icc 0 1 := sorry /-- Concatenation of two paths from `x` to `y` and from `y` to `z`, putting the first path on `[0, 1/2]` and the second one on `[1/2, 1]`. -/ def trans {X : Type u_1} [topological_space X] {x : X} {y : X} {z : X} (γ : path x y) (γ' : path y z) : path x z := mk ((fun (t : ℝ) => ite (t ≤ 1 / bit0 1) (extend γ (bit0 1 * t)) (extend γ' (bit0 1 * t - 1))) ∘ coe) sorry sorry sorry @[simp] theorem refl_trans_refl {X : Type u_1} [topological_space X] {a : X} : trans (refl a) (refl a) = refl a := sorry theorem trans_range {X : Type u_1} [topological_space X] {a : X} {b : X} {c : X} (γ₁ : path a b) (γ₂ : path b c) : set.range ⇑(trans γ₁ γ₂) = set.range ⇑γ₁ ∪ set.range ⇑γ₂ := sorry /-- Image of a path from `x` to `y` by a continuous map -/ def map {X : Type u_1} [topological_space X] {x : X} {y : X} (γ : path x y) {Y : Type u_2} [topological_space Y] {f : X → Y} (h : continuous f) : path (f x) (f y) := mk (f ∘ ⇑γ) sorry sorry sorry @[simp] theorem map_coe {X : Type u_1} [topological_space X] {x : X} {y : X} (γ : path x y) {Y : Type u_2} [topological_space Y] {f : X → Y} (h : continuous f) : ⇑(map γ h) = f ∘ ⇑γ := funext fun (t : ↥(set.Icc 0 1)) => Eq.refl (coe_fn (map γ h) t) /-- Casting a path from `x` to `y` to a path from `x'` to `y'` when `x' = x` and `y' = y` -/ def cast {X : Type u_1} [topological_space X] {x : X} {y : X} (γ : path x y) {x' : X} {y' : X} (hx : x' = x) (hy : y' = y) : path x' y' := mk (⇑γ) (path.continuous γ) sorry sorry @[simp] theorem symm_cast {X : Type u_1} [topological_space X] {a₁ : X} {a₂ : X} {b₁ : X} {b₂ : X} (γ : path a₂ b₂) (ha : a₁ = a₂) (hb : b₁ = b₂) : symm (cast γ ha hb) = cast (symm γ) hb ha := rfl @[simp] theorem trans_cast {X : Type u_1} [topological_space X] {a₁ : X} {a₂ : X} {b₁ : X} {b₂ : X} {c₁ : X} {c₂ : X} (γ : path a₂ b₂) (γ' : path b₂ c₂) (ha : a₁ = a₂) (hb : b₁ = b₂) (hc : c₁ = c₂) : trans (cast γ ha hb) (cast γ' hb hc) = cast (trans γ γ') ha hc := rfl @[simp] theorem cast_coe {X : Type u_1} [topological_space X] {x : X} {y : X} (γ : path x y) {x' : X} {y' : X} (hx : x' = x) (hy : y' = y) : ⇑(cast γ hx hy) = ⇑γ := rfl theorem symm_continuous_family {X : Type u_1} {ι : Type u_2} [topological_space X] [topological_space ι] {a : ι → X} {b : ι → X} (γ : (t : ι) → path (a t) (b t)) (h : continuous ↿γ) : continuous ↿fun (t : ι) => symm (γ t) := continuous.comp h (continuous.prod_map continuous_id continuous_I_symm) theorem continuous_uncurry_extend_of_continuous_family {X : Type u_1} {ι : Type u_2} [topological_space X] [topological_space ι] {a : ι → X} {b : ι → X} (γ : (t : ι) → path (a t) (b t)) (h : continuous ↿γ) : continuous ↿fun (t : ι) => extend (γ t) := continuous.comp h (continuous.prod_map continuous_id continuous_proj_Icc) theorem trans_continuous_family {X : Type u_1} {ι : Type u_2} [topological_space X] [topological_space ι] {a : ι → X} {b : ι → X} {c : ι → X} (γ₁ : (t : ι) → path (a t) (b t)) (h₁ : continuous ↿γ₁) (γ₂ : (t : ι) → path (b t) (c t)) (h₂ : continuous ↿γ₂) : continuous ↿fun (t : ι) => trans (γ₁ t) (γ₂ t) := sorry /-! #### Truncating a path -/ /-- `γ.truncate t₀ t₁` is the path which follows the path `γ` on the time interval `[t₀, t₁]` and stays still otherwise. -/ def truncate {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) (t₀ : ℝ) (t₁ : ℝ) : path (extend γ (min t₀ t₁)) (extend γ t₁) := mk (fun (s : ↥(set.Icc 0 1)) => extend γ (min (max (↑s) t₀) t₁)) sorry sorry sorry /-- `γ.truncate_of_le t₀ t₁ h`, where `h : t₀ ≤ t₁` is `γ.truncate t₀ t₁` casted as a path from `γ.extend t₀` to `γ.extend t₁`. -/ def truncate_of_le {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) {t₀ : ℝ} {t₁ : ℝ} (h : t₀ ≤ t₁) : path (extend γ t₀) (extend γ t₁) := cast (truncate γ t₀ t₁) sorry sorry theorem truncate_range {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) {t₀ : ℝ} {t₁ : ℝ} : set.range ⇑(truncate γ t₀ t₁) ⊆ set.range ⇑γ := sorry /-- For a path `γ`, `γ.truncate` gives a "continuous family of paths", by which we mean the uncurried function which maps `(t₀, t₁, s)` to `γ.truncate t₀ t₁ s` is continuous. -/ theorem truncate_continuous_family {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) : continuous fun (x : ℝ × ℝ × ↥(set.Icc 0 1)) => coe_fn (truncate γ (prod.fst x) (prod.fst (prod.snd x))) (prod.snd (prod.snd x)) := sorry /- TODO : When `continuity` gets quicker, change the proof back to : `begin` `simp only [has_coe_to_fun.coe, coe_fn, path.truncate],` `continuity,` `exact continuous_subtype_coe` `end` -/ theorem truncate_const_continuous_family {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) (t : ℝ) : continuous ↿(truncate γ t) := sorry @[simp] theorem truncate_self {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) (t : ℝ) : truncate γ t t = cast (refl (extend γ t)) (eq.mpr (id (Eq._oldrec (Eq.refl (extend γ (min t t) = extend γ t)) (min_self t))) (Eq.refl (extend γ t))) rfl := sorry @[simp] theorem truncate_zero_zero {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) : truncate γ 0 0 = cast (refl a) (eq.mpr (id (Eq._oldrec (Eq.refl (extend γ (min 0 0) = a)) (min_self 0))) (eq.mpr (id (Eq._oldrec (Eq.refl (extend γ 0 = a)) (extend_zero γ))) (Eq.refl a))) (extend_zero γ) := sorry @[simp] theorem truncate_one_one {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) : truncate γ 1 1 = cast (refl b) (eq.mpr (id (Eq._oldrec (Eq.refl (extend γ (min 1 1) = b)) (min_self 1))) (eq.mpr (id (Eq._oldrec (Eq.refl (extend γ 1 = b)) (extend_one γ))) (Eq.refl b))) (extend_one γ) := sorry @[simp] theorem truncate_zero_one {X : Type u_1} [topological_space X] {a : X} {b : X} (γ : path a b) : truncate γ 0 1 = cast γ (eq.mpr (id (Eq.trans ((fun (a a_1 : X) (e_1 : a = a_1) (ᾰ ᾰ_1 : X) (e_2 : ᾰ = ᾰ_1) => congr (congr_arg Eq e_1) e_2) (extend γ (min 0 1)) a (Eq.trans ((fun (γ γ_1 : path a b) (e_1 : γ = γ_1) (ᾰ ᾰ_1 : ℝ) (e_2 : ᾰ = ᾰ_1) => congr (congr_arg extend e_1) e_2) γ γ (Eq.refl γ) (min 0 1) 0 (min_eq_left (iff.mpr (iff_true_intro zero_le_one) True.intro))) (extend_zero γ)) a a (Eq.refl a)) (propext (eq_self_iff_true a)))) trivial) (eq.mpr (id (Eq.trans ((fun (a a_1 : X) (e_1 : a = a_1) (ᾰ ᾰ_1 : X) (e_2 : ᾰ = ᾰ_1) => congr (congr_arg Eq e_1) e_2) (extend γ 1) b (extend_one γ) b b (Eq.refl b)) (propext (eq_self_iff_true b)))) trivial) := sorry end path /-! ### Being joined by a path -/ /-- The relation "being joined by a path". This is an equivalence relation. -/ def joined {X : Type u_1} [topological_space X] (x : X) (y : X) := Nonempty (path x y) theorem joined.refl {X : Type u_1} [topological_space X] (x : X) : joined x x := Nonempty.intro (path.refl x) /-- When two points are joined, choose some path from `x` to `y`. -/ def joined.some_path {X : Type u_1} [topological_space X] {x : X} {y : X} (h : joined x y) : path x y := nonempty.some h theorem joined.symm {X : Type u_1} [topological_space X] {x : X} {y : X} (h : joined x y) : joined y x := Nonempty.intro (path.symm (joined.some_path h)) theorem joined.trans {X : Type u_1} [topological_space X] {x : X} {y : X} {z : X} (hxy : joined x y) (hyz : joined y z) : joined x z := Nonempty.intro (path.trans (joined.some_path hxy) (joined.some_path hyz)) /-- The setoid corresponding the equivalence relation of being joined by a continuous path. -/ def path_setoid (X : Type u_1) [topological_space X] : setoid X := setoid.mk joined sorry /-- The quotient type of points of a topological space modulo being joined by a continuous path. -/ def zeroth_homotopy (X : Type u_1) [topological_space X] := quotient (path_setoid X) protected instance zeroth_homotopy.inhabited : Inhabited (zeroth_homotopy ℝ) := { default := quotient.mk 0 } /-! ### Being joined by a path inside a set -/ /-- The relation "being joined by a path in `F`". Not quite an equivalence relation since it's not reflexive for points that do not belong to `F`. -/ def joined_in {X : Type u_1} [topological_space X] (F : set X) (x : X) (y : X) := ∃ (γ : path x y), ∀ (t : ↥(set.Icc 0 1)), coe_fn γ t ∈ F theorem joined_in.mem {X : Type u_1} [topological_space X] {x : X} {y : X} {F : set X} (h : joined_in F x y) : x ∈ F ∧ y ∈ F := sorry theorem joined_in.source_mem {X : Type u_1} [topological_space X] {x : X} {y : X} {F : set X} (h : joined_in F x y) : x ∈ F := and.left (joined_in.mem h) theorem joined_in.target_mem {X : Type u_1} [topological_space X] {x : X} {y : X} {F : set X} (h : joined_in F x y) : y ∈ F := and.right (joined_in.mem h) /-- When `x` and `y` are joined in `F`, choose a path from `x` to `y` inside `F` -/ def joined_in.some_path {X : Type u_1} [topological_space X] {x : X} {y : X} {F : set X} (h : joined_in F x y) : path x y := classical.some h theorem joined_in.some_path_mem {X : Type u_1} [topological_space X] {x : X} {y : X} {F : set X} (h : joined_in F x y) (t : ↥(set.Icc 0 1)) : coe_fn (joined_in.some_path h) t ∈ F := classical.some_spec h t /-- If `x` and `y` are joined in the set `F`, then they are joined in the subtype `F`. -/ theorem joined_in.joined_subtype {X : Type u_1} [topological_space X] {x : X} {y : X} {F : set X} (h : joined_in F x y) : joined { val := x, property := joined_in.source_mem h } { val := y, property := joined_in.target_mem h } := sorry theorem joined_in.of_line {X : Type u_1} [topological_space X] {x : X} {y : X} {F : set X} {f : ℝ → X} (hf : continuous_on f (set.Icc 0 1)) (h₀ : f 0 = x) (h₁ : f 1 = y) (hF : f '' set.Icc 0 1 ⊆ F) : joined_in F x y := Exists.intro (path.of_line hf h₀ h₁) fun (t : ↥(set.Icc 0 1)) => hF (path.of_line_mem hf h₀ h₁ t) theorem joined_in.joined {X : Type u_1} [topological_space X] {x : X} {y : X} {F : set X} (h : joined_in F x y) : joined x y := Nonempty.intro (joined_in.some_path h) theorem joined_in_iff_joined {X : Type u_1} [topological_space X] {x : X} {y : X} {F : set X} (x_in : x ∈ F) (y_in : y ∈ F) : joined_in F x y ↔ joined { val := x, property := x_in } { val := y, property := y_in } := sorry @[simp] theorem joined_in_univ {X : Type u_1} [topological_space X] {x : X} {y : X} : joined_in set.univ x y ↔ joined x y := sorry theorem joined_in.mono {X : Type u_1} [topological_space X] {x : X} {y : X} {U : set X} {V : set X} (h : joined_in U x y) (hUV : U ⊆ V) : joined_in V x y := Exists.intro (joined_in.some_path h) fun (t : ↥(set.Icc 0 1)) => hUV (joined_in.some_path_mem h t) theorem joined_in.refl {X : Type u_1} [topological_space X] {x : X} {F : set X} (h : x ∈ F) : joined_in F x x := Exists.intro (path.refl x) fun (t : ↥(set.Icc 0 1)) => h theorem joined_in.symm {X : Type u_1} [topological_space X] {x : X} {y : X} {F : set X} (h : joined_in F x y) : joined_in F y x := sorry theorem joined_in.trans {X : Type u_1} [topological_space X] {x : X} {y : X} {z : X} {F : set X} (hxy : joined_in F x y) (hyz : joined_in F y z) : joined_in F x z := sorry /-! ### Path component -/ /-- The path component of `x` is the set of points that can be joined to `x`. -/ def path_component {X : Type u_1} [topological_space X] (x : X) : set X := set_of fun (y : X) => joined x y @[simp] theorem mem_path_component_self {X : Type u_1} [topological_space X] (x : X) : x ∈ path_component x := joined.refl x @[simp] theorem path_component.nonempty {X : Type u_1} [topological_space X] (x : X) : set.nonempty (path_component x) := Exists.intro x (mem_path_component_self x) theorem mem_path_component_of_mem {X : Type u_1} [topological_space X] {x : X} {y : X} (h : x ∈ path_component y) : y ∈ path_component x := joined.symm h theorem path_component_symm {X : Type u_1} [topological_space X] {x : X} {y : X} : x ∈ path_component y ↔ y ∈ path_component x := { mp := fun (h : x ∈ path_component y) => mem_path_component_of_mem h, mpr := fun (h : y ∈ path_component x) => mem_path_component_of_mem h } theorem path_component_congr {X : Type u_1} [topological_space X] {x : X} {y : X} (h : x ∈ path_component y) : path_component x = path_component y := sorry theorem path_component_subset_component {X : Type u_1} [topological_space X] (x : X) : path_component x ⊆ connected_component x := sorry /-- The path component of `x` in `F` is the set of points that can be joined to `x` in `F`. -/ def path_component_in {X : Type u_1} [topological_space X] (x : X) (F : set X) : set X := set_of fun (y : X) => joined_in F x y @[simp] theorem path_component_in_univ {X : Type u_1} [topological_space X] (x : X) : path_component_in x set.univ = path_component x := sorry theorem joined.mem_path_component {X : Type u_1} [topological_space X] {x : X} {y : X} {z : X} (hyz : joined y z) (hxy : y ∈ path_component x) : z ∈ path_component x := joined.trans hxy hyz /-! ### Path connected sets -/ /-- A set `F` is path connected if it contains a point that can be joined to all other in `F`. -/ def is_path_connected {X : Type u_1} [topological_space X] (F : set X) := ∃ (x : X), ∃ (H : x ∈ F), ∀ {y : X}, y ∈ F → joined_in F x y theorem is_path_connected_iff_eq {X : Type u_1} [topological_space X] {F : set X} : is_path_connected F ↔ ∃ (x : X), ∃ (H : x ∈ F), path_component_in x F = F := sorry theorem is_path_connected.joined_in {X : Type u_1} [topological_space X] {F : set X} (h : is_path_connected F) (x : X) (y : X) (H : x ∈ F) : y ∈ F → joined_in F x y := sorry theorem is_path_connected_iff {X : Type u_1} [topological_space X] {F : set X} : is_path_connected F ↔ set.nonempty F ∧ ∀ (x y : X), x ∈ F → y ∈ F → joined_in F x y := sorry theorem is_path_connected.image {X : Type u_1} [topological_space X] {F : set X} {Y : Type u_2} [topological_space Y] (hF : is_path_connected F) {f : X → Y} (hf : continuous f) : is_path_connected (f '' F) := sorry theorem is_path_connected.mem_path_component {X : Type u_1} [topological_space X] {x : X} {y : X} {F : set X} (h : is_path_connected F) (x_in : x ∈ F) (y_in : y ∈ F) : y ∈ path_component x := joined_in.joined (is_path_connected.joined_in h x y x_in y_in) theorem is_path_connected.subset_path_component {X : Type u_1} [topological_space X] {x : X} {F : set X} (h : is_path_connected F) (x_in : x ∈ F) : F ⊆ path_component x := fun (y : X) (y_in : y ∈ F) => is_path_connected.mem_path_component h x_in y_in theorem is_path_connected.union {X : Type u_1} [topological_space X] {U : set X} {V : set X} (hU : is_path_connected U) (hV : is_path_connected V) (hUV : set.nonempty (U ∩ V)) : is_path_connected (U ∪ V) := sorry /-- If a set `W` is path-connected, then it is also path-connected when seen as a set in a smaller ambient type `U` (when `U` contains `W`). -/ theorem is_path_connected.preimage_coe {X : Type u_1} [topological_space X] {U : set X} {W : set X} (hW : is_path_connected W) (hWU : W ⊆ U) : is_path_connected (coe ⁻¹' W) := sorry theorem is_path_connected.exists_path_through_family {X : Type u_1} [topological_space X] {n : ℕ} {s : set X} (h : is_path_connected s) (p : fin (n + 1) → X) (hp : ∀ (i : fin (n + 1)), p i ∈ s) : ∃ (γ : path (p 0) (p ↑n)), set.range ⇑γ ⊆ s ∧ ∀ (i : fin (n + 1)), p i ∈ set.range ⇑γ := sorry theorem is_path_connected.exists_path_through_family' {X : Type u_1} [topological_space X] {n : ℕ} {s : set X} (h : is_path_connected s) (p : fin (n + 1) → X) (hp : ∀ (i : fin (n + 1)), p i ∈ s) : ∃ (γ : path (p 0) (p ↑n)), ∃ (t : fin (n + 1) → ↥(set.Icc 0 1)), (∀ (t : ↥(set.Icc 0 1)), coe_fn γ t ∈ s) ∧ ∀ (i : fin (n + 1)), coe_fn γ (t i) = p i := sorry /-! ### Path connected spaces -/ /-- A topological space is path-connected if it is non-empty and every two points can be joined by a continuous path. -/ class path_connected_space (X : Type u_3) [topological_space X] where nonempty : Nonempty X joined : ∀ (x y : X), joined x y theorem path_connected_space_iff_zeroth_homotopy {X : Type u_1} [topological_space X] : path_connected_space X ↔ Nonempty (zeroth_homotopy X) ∧ subsingleton (zeroth_homotopy X) := sorry namespace path_connected_space /-- Use path-connectedness to build a path between two points. -/ def some_path {X : Type u_1} [topological_space X] [path_connected_space X] (x : X) (y : X) : path x y := nonempty.some (joined x y) end path_connected_space theorem is_path_connected_iff_path_connected_space {X : Type u_1} [topological_space X] {F : set X} : is_path_connected F ↔ path_connected_space ↥F := sorry theorem path_connected_space_iff_univ {X : Type u_1} [topological_space X] : path_connected_space X ↔ is_path_connected set.univ := sorry theorem path_connected_space_iff_eq {X : Type u_1} [topological_space X] : path_connected_space X ↔ ∃ (x : X), path_component x = set.univ := sorry protected instance path_connected_space.connected_space {X : Type u_1} [topological_space X] [path_connected_space X] : connected_space X := sorry namespace path_connected_space theorem exists_path_through_family {X : Type u_1} [topological_space X] [path_connected_space X] {n : ℕ} (p : fin (n + 1) → X) : ∃ (γ : path (p 0) (p ↑n)), ∀ (i : fin (n + 1)), p i ∈ set.range ⇑γ := sorry theorem exists_path_through_family' {X : Type u_1} [topological_space X] [path_connected_space X] {n : ℕ} (p : fin (n + 1) → X) : ∃ (γ : path (p 0) (p ↑n)), ∃ (t : fin (n + 1) → ↥(set.Icc 0 1)), ∀ (i : fin (n + 1)), coe_fn γ (t i) = p i := sorry end path_connected_space /-! ### Locally path connected spaces -/ /-- A topological space is locally path connected, at every point, path connected neighborhoods form a neighborhood basis. -/ class loc_path_connected_space (X : Type u_3) [topological_space X] where path_connected_basis : ∀ (x : X), filter.has_basis (nhds x) (fun (s : set X) => s ∈ nhds x ∧ is_path_connected s) id theorem loc_path_connected_of_bases {X : Type u_1} [topological_space X] {ι : Type u_2} {p : ι → Prop} {s : X → ι → set X} (h : ∀ (x : X), filter.has_basis (nhds x) p (s x)) (h' : ∀ (x : X) (i : ι), p i → is_path_connected (s x i)) : loc_path_connected_space X := sorry theorem path_connected_space_iff_connected_space {X : Type u_1} [topological_space X] [loc_path_connected_space X] : path_connected_space X ↔ connected_space X := sorry theorem path_connected_subset_basis {X : Type u_1} [topological_space X] {x : X} [loc_path_connected_space X] {U : set X} (h : is_open U) (hx : x ∈ U) : filter.has_basis (nhds x) (fun (s : set X) => s ∈ nhds x ∧ is_path_connected s ∧ s ⊆ U) id := filter.has_basis.has_basis_self_subset (path_connected_basis x) (mem_nhds_sets h hx) theorem loc_path_connected_of_is_open {X : Type u_1} [topological_space X] [loc_path_connected_space X] {U : set X} (h : is_open U) : loc_path_connected_space ↥U := sorry theorem is_open.is_connected_iff_is_path_connected {X : Type u_1} [topological_space X] [loc_path_connected_space X] {U : set X} (U_op : is_open U) : is_path_connected U ↔ is_connected U := sorry end Mathlib
70cfa26034e2d1808d9aa129b5028ba936a71cc5
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/algebra/ordered_monoid.lean
0948ec3262286283b581d561463a1d00a35d9f2c
[ "Apache-2.0" ]
permissive
hikari0108/mathlib
b7ea2b7350497ab1a0b87a09d093ecc025a50dfa
a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901
refs/heads/master
1,690,483,608,260
1,631,541,580,000
1,631,541,580,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
39,061
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.group.with_one import algebra.group.type_tags import algebra.group.prod import algebra.order_functions import order.bounded_lattice import algebra.ordered_monoid_lemmas import order.rel_iso /-! # Ordered monoids This file develops the basics of ordered monoids. ## Implementation details Unfortunately, the number of `'` appended to lemmas in this file may differ between the multiplicative and the additive version of a lemma. The reason is that we did not want to change existing names in the library. -/ set_option old_structure_cmd true open function universe u variable {α : Type u} /-- An ordered commutative monoid is a commutative monoid with a partial order such that `a ≤ b → c * a ≤ c * b` (multiplication is monotone) -/ @[protect_proj, ancestor comm_monoid partial_order] class ordered_comm_monoid (α : Type*) extends comm_monoid α, partial_order α := (mul_le_mul_left : ∀ a b : α, a ≤ b → ∀ c : α, c * a ≤ c * b) /-- An ordered (additive) commutative monoid is a commutative monoid with a partial order such that `a ≤ b → c + a ≤ c + b` (addition is monotone) -/ @[protect_proj, ancestor add_comm_monoid partial_order] class ordered_add_comm_monoid (α : Type*) extends add_comm_monoid α, partial_order α := (add_le_add_left : ∀ a b : α, a ≤ b → ∀ c : α, c + a ≤ c + b) attribute [to_additive] ordered_comm_monoid section ordered_instances @[to_additive] instance ordered_comm_monoid.to_covariant_class_left (M : Type*) [ordered_comm_monoid M] : covariant_class M M (*) (≤) := { elim := λ a b c bc, ordered_comm_monoid.mul_le_mul_left _ _ bc a } /- This instance can be proven with `by apply_instance`. However, `with_bot ℕ` does not pick up a `covariant_class M M (function.swap (*)) (≤)` instance without it (see PR #7940). -/ @[to_additive] instance ordered_comm_monoid.to_covariant_class_right (M : Type*) [ordered_comm_monoid M] : covariant_class M M (swap (*)) (≤) := covariant_swap_mul_le_of_covariant_mul_le M end ordered_instances /-- An `ordered_comm_monoid` with one-sided 'division' in the sense that if `a ≤ b`, there is some `c` for which `a * c = b`. This is a weaker version of the condition on canonical orderings defined by `canonically_ordered_monoid`. -/ class has_exists_mul_of_le (α : Type u) [ordered_comm_monoid α] : Prop := (exists_mul_of_le : ∀ {a b : α}, a ≤ b → ∃ (c : α), b = a * c) /-- An `ordered_add_comm_monoid` with one-sided 'subtraction' in the sense that if `a ≤ b`, then there is some `c` for which `a + c = b`. This is a weaker version of the condition on canonical orderings defined by `canonically_ordered_add_monoid`. -/ class has_exists_add_of_le (α : Type u) [ordered_add_comm_monoid α] : Prop := (exists_add_of_le : ∀ {a b : α}, a ≤ b → ∃ (c : α), b = a + c) attribute [to_additive] has_exists_mul_of_le export has_exists_mul_of_le (exists_mul_of_le) export has_exists_add_of_le (exists_add_of_le) /-- A linearly ordered additive commutative monoid. -/ @[protect_proj, ancestor linear_order ordered_add_comm_monoid] class linear_ordered_add_comm_monoid (α : Type*) extends linear_order α, ordered_add_comm_monoid α. /-- A linearly ordered commutative monoid. -/ @[protect_proj, ancestor linear_order ordered_comm_monoid, to_additive] class linear_ordered_comm_monoid (α : Type*) extends linear_order α, ordered_comm_monoid α. /-- A linearly ordered commutative monoid with a zero element. -/ class linear_ordered_comm_monoid_with_zero (α : Type*) extends linear_ordered_comm_monoid α, comm_monoid_with_zero α := (zero_le_one : (0 : α) ≤ 1) /-- A linearly ordered commutative monoid with an additively absorbing `⊤` element. Instances should include number systems with an infinite element adjoined.` -/ @[protect_proj, ancestor linear_ordered_add_comm_monoid order_top] class linear_ordered_add_comm_monoid_with_top (α : Type*) extends linear_ordered_add_comm_monoid α, order_top α := (top_add' : ∀ x : α, ⊤ + x = ⊤) section linear_ordered_add_comm_monoid_with_top variables [linear_ordered_add_comm_monoid_with_top α] {a b : α} @[simp] lemma top_add (a : α) : ⊤ + a = ⊤ := linear_ordered_add_comm_monoid_with_top.top_add' a @[simp] lemma add_top (a : α) : a + ⊤ = ⊤ := trans (add_comm _ _) (top_add _) -- TODO: Generalize to a not-yet-existing typeclass extending `linear_order` and `order_top` @[simp] lemma min_top_left (a : α) : min (⊤ : α) a = a := min_eq_right le_top @[simp] lemma min_top_right (a : α) : min a ⊤ = a := min_eq_left le_top end linear_ordered_add_comm_monoid_with_top /-- Pullback an `ordered_comm_monoid` under an injective map. See note [reducible non-instances]. -/ @[reducible, to_additive function.injective.ordered_add_comm_monoid "Pullback an `ordered_add_comm_monoid` under an injective map."] def function.injective.ordered_comm_monoid [ordered_comm_monoid α] {β : Type*} [has_one β] [has_mul β] (f : β → α) (hf : function.injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : ordered_comm_monoid β := { mul_le_mul_left := λ a b ab c, show f (c * a) ≤ f (c * b), by { rw [mul, mul], apply mul_le_mul_left', exact ab }, ..partial_order.lift f hf, ..hf.comm_monoid f one mul } /-- Pullback a `linear_ordered_comm_monoid` under an injective map. See note [reducible non-instances]. -/ @[reducible, to_additive function.injective.linear_ordered_add_comm_monoid "Pullback an `ordered_add_comm_monoid` under an injective map."] def function.injective.linear_ordered_comm_monoid [linear_ordered_comm_monoid α] {β : Type*} [has_one β] [has_mul β] (f : β → α) (hf : function.injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : linear_ordered_comm_monoid β := { .. hf.ordered_comm_monoid f one mul, .. linear_order.lift f hf } lemma bit0_pos [ordered_add_comm_monoid α] {a : α} (h : 0 < a) : 0 < bit0 a := add_pos h h namespace units @[to_additive] instance [monoid α] [preorder α] : preorder (units α) := preorder.lift (coe : units α → α) @[simp, norm_cast, to_additive] theorem coe_le_coe [monoid α] [preorder α] {a b : units α} : (a : α) ≤ b ↔ a ≤ b := iff.rfl -- should `to_additive` do this? attribute [norm_cast] add_units.coe_le_coe @[simp, norm_cast, to_additive] theorem coe_lt_coe [monoid α] [preorder α] {a b : units α} : (a : α) < b ↔ a < b := iff.rfl attribute [norm_cast] add_units.coe_lt_coe @[to_additive] instance [monoid α] [partial_order α] : partial_order (units α) := partial_order.lift coe units.ext @[to_additive] instance [monoid α] [linear_order α] : linear_order (units α) := linear_order.lift coe units.ext @[simp, norm_cast, to_additive] theorem max_coe [monoid α] [linear_order α] {a b : units α} : (↑(max a b) : α) = max a b := by by_cases b ≤ a; simp [max, h] attribute [norm_cast] add_units.max_coe @[simp, norm_cast, to_additive] theorem min_coe [monoid α] [linear_order α] {a b : units α} : (↑(min a b) : α) = min a b := by by_cases a ≤ b; simp [min, h] attribute [norm_cast] add_units.min_coe end units namespace with_zero local attribute [semireducible] with_zero instance [preorder α] : preorder (with_zero α) := with_bot.preorder instance [partial_order α] : partial_order (with_zero α) := with_bot.partial_order instance [partial_order α] : order_bot (with_zero α) := with_bot.order_bot lemma zero_le [partial_order α] (a : with_zero α) : 0 ≤ a := order_bot.bot_le a lemma zero_lt_coe [preorder α] (a : α) : (0 : with_zero α) < a := with_bot.bot_lt_coe a @[simp, norm_cast] lemma coe_lt_coe [partial_order α] {a b : α} : (a : with_zero α) < b ↔ a < b := with_bot.coe_lt_coe @[simp, norm_cast] lemma coe_le_coe [partial_order α] {a b : α} : (a : with_zero α) ≤ b ↔ a ≤ b := with_bot.coe_le_coe instance [lattice α] : lattice (with_zero α) := with_bot.lattice instance [linear_order α] : linear_order (with_zero α) := with_bot.linear_order lemma mul_le_mul_left {α : Type u} [has_mul α] [preorder α] [covariant_class α α (*) (≤)] : ∀ (a b : with_zero α), a ≤ b → ∀ (c : with_zero α), c * a ≤ c * b := begin rintro (_ | a) (_ | b) h (_ | c); try { exact λ f hf, option.no_confusion hf }, { exact false.elim (not_lt_of_le h (with_zero.zero_lt_coe a))}, { simp_rw [some_eq_coe] at h ⊢, norm_cast at h ⊢, exact covariant_class.elim _ h } end lemma lt_of_mul_lt_mul_left {α : Type u} [has_mul α] [partial_order α] [contravariant_class α α (*) (<)] : ∀ (a b c : with_zero α), a * b < a * c → b < c := begin rintro (_ | a) (_ | b) (_ | c) h; try { exact false.elim (lt_irrefl none h) }, { exact with_zero.zero_lt_coe c }, { exact false.elim (not_le_of_lt h (with_zero.zero_le _)) }, { simp_rw [some_eq_coe] at h ⊢, norm_cast at h ⊢, apply lt_of_mul_lt_mul_left' h } end instance [ordered_comm_monoid α] : ordered_comm_monoid (with_zero α) := { mul_le_mul_left := with_zero.mul_le_mul_left, ..with_zero.comm_monoid_with_zero, ..with_zero.partial_order } /- Note 1 : the below is not an instance because it requires `zero_le`. It seems like a rather pathological definition because α already has a zero. Note 2 : there is no multiplicative analogue because it does not seem necessary. Mathematicians might be more likely to use the order-dual version, where all elements are ≤ 1 and then 1 is the top element. -/ /-- If `0` is the least element in `α`, then `with_zero α` is an `ordered_add_comm_monoid`. -/ def ordered_add_comm_monoid [ordered_add_comm_monoid α] (zero_le : ∀ a : α, 0 ≤ a) : ordered_add_comm_monoid (with_zero α) := begin suffices, refine { add_le_add_left := this, ..with_zero.partial_order, ..with_zero.add_comm_monoid, .. }, { intros a b h c ca h₂, cases b with b, { rw le_antisymm h bot_le at h₂, exact ⟨_, h₂, le_refl _⟩ }, cases a with a, { change c + 0 = some ca at h₂, simp at h₂, simp [h₂], exact ⟨_, rfl, by simpa using add_le_add_left (zero_le b) _⟩ }, { simp at h, cases c with c; change some _ = _ at h₂; simp [-add_comm] at h₂; subst ca; refine ⟨_, rfl, _⟩, { exact h }, { exact add_le_add_left h _ } } } end end with_zero namespace with_top section has_one variables [has_one α] @[to_additive] instance : has_one (with_top α) := ⟨(1 : α)⟩ @[simp, to_additive] lemma coe_one : ((1 : α) : with_top α) = 1 := rfl @[simp, to_additive] lemma coe_eq_one {a : α} : (a : with_top α) = 1 ↔ a = 1 := coe_eq_coe @[simp, to_additive] theorem one_eq_coe {a : α} : 1 = (a : with_top α) ↔ a = 1 := trans eq_comm coe_eq_one attribute [norm_cast] coe_one coe_eq_one coe_zero coe_eq_zero one_eq_coe zero_eq_coe @[simp, to_additive] theorem top_ne_one : ⊤ ≠ (1 : with_top α) . @[simp, to_additive] theorem one_ne_top : (1 : with_top α) ≠ ⊤ . end has_one instance [has_add α] : has_add (with_top α) := ⟨λ o₁ o₂, o₁.bind (λ a, o₂.map (λ b, a + b))⟩ @[norm_cast] lemma coe_add [has_add α] {a b : α} : ((a + b : α) : with_top α) = a + b := rfl @[norm_cast] lemma coe_bit0 [has_add α] {a : α} : ((bit0 a : α) : with_top α) = bit0 a := rfl @[norm_cast] lemma coe_bit1 [has_add α] [has_one α] {a : α} : ((bit1 a : α) : with_top α) = bit1 a := rfl @[simp] lemma add_top [has_add α] : ∀{a : with_top α}, a + ⊤ = ⊤ | none := rfl | (some a) := rfl @[simp] lemma top_add [has_add α] {a : with_top α} : ⊤ + a = ⊤ := rfl lemma add_eq_top [has_add α] {a b : with_top α} : a + b = ⊤ ↔ a = ⊤ ∨ b = ⊤ := by cases a; cases b; simp [none_eq_top, some_eq_coe, ←with_top.coe_add, ←with_zero.coe_add] lemma add_lt_top [has_add α] [partial_order α] {a b : with_top α} : a + b < ⊤ ↔ a < ⊤ ∧ b < ⊤ := by simp [lt_top_iff_ne_top, add_eq_top, not_or_distrib] lemma add_eq_coe [has_add α] : ∀ {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] 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_monoid α] : add_monoid (with_top α) := { zero_add := begin refine with_top.rec_top_coe _ _, { simpa }, { intro, rw [←with_top.coe_zero, ←with_top.coe_add, zero_add] } end, add_zero := begin refine with_top.rec_top_coe _ _, { simpa }, { intro, rw [←with_top.coe_zero, ←with_top.coe_add, add_zero] } end, ..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 [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' := λ x, with_top.top_add, ..with_top.order_top, ..with_top.linear_order, ..with_top.ordered_add_comm_monoid, ..option.nontrivial } /-- 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 end with_top namespace with_bot instance [has_zero α] : has_zero (with_bot α) := with_top.has_zero instance [has_one α] : has_one (with_bot α) := with_top.has_one 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_monoid α] : add_monoid (with_bot α) := with_top.add_monoid instance [add_comm_monoid α] : add_comm_monoid (with_bot α) := with_top.add_comm_monoid instance [ordered_add_comm_monoid α] : ordered_add_comm_monoid (with_bot α) := begin suffices, refine { add_le_add_left := this, ..with_bot.partial_order, ..with_bot.add_comm_monoid, ..}, { intros a b h c ca h₂, cases c with c, {cases h₂}, cases a with a; cases h₂, cases b with b, {cases le_antisymm h bot_le}, simp at h, exact ⟨_, rfl, add_le_add_left h _⟩, } end instance [linear_ordered_add_comm_monoid α] : linear_ordered_add_comm_monoid (with_bot α) := { ..with_bot.linear_order, ..with_bot.ordered_add_comm_monoid } -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` lemma coe_zero [has_zero α] : ((0 : α) : with_bot α) = 0 := rfl -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` 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` lemma coe_eq_zero {α : Type*} [add_monoid α] {a : α} : (a : with_bot α) = 0 ↔ a = 0 := by norm_cast -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` lemma coe_add [add_semigroup α] (a b : α) : ((a + b : α) : with_bot α) = a + b := by norm_cast -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` lemma coe_bit0 [add_semigroup α] {a : α} : ((bit0 a : α) : with_bot α) = bit0 a := by norm_cast -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` lemma coe_bit1 [add_semigroup α] [has_one α] {a : α} : ((bit1 a : α) : with_bot α) = bit1 a := by norm_cast @[simp] lemma bot_add [add_semigroup α] (a : with_bot α) : ⊥ + a = ⊥ := rfl @[simp] lemma add_bot [add_semigroup α] (a : with_bot α) : a + ⊥ = ⊥ := by cases a; refl @[simp] lemma add_eq_bot [add_semigroup α] {m n : with_bot α} : m + n = ⊥ ↔ m = ⊥ ∨ n = ⊥ := with_top.add_eq_top end with_bot /-- A canonically ordered additive monoid is an ordered commutative additive monoid in which the ordering coincides with the subtractibility relation, which is to say, `a ≤ b` iff there exists `c` with `b = a + c`. This is satisfied by the natural numbers, for example, but not the integers or other nontrivial `ordered_add_comm_group`s. -/ @[protect_proj, ancestor ordered_add_comm_monoid order_bot] class canonically_ordered_add_monoid (α : Type*) extends ordered_add_comm_monoid α, order_bot α := (le_iff_exists_add : ∀ a b : α, a ≤ b ↔ ∃ c, b = a + c) /-- A canonically ordered monoid is an ordered commutative monoid in which the ordering coincides with the divisibility relation, which is to say, `a ≤ b` iff there exists `c` with `b = a * c`. Examples seem rare; it seems more likely that the `order_dual` of a naturally-occurring lattice satisfies this than the lattice itself (for example, dual of the lattice of ideals of a PID or Dedekind domain satisfy this; collections of all things ≤ 1 seem to be more natural that collections of all things ≥ 1). -/ @[protect_proj, ancestor ordered_comm_monoid order_bot, to_additive] class canonically_ordered_monoid (α : Type*) extends ordered_comm_monoid α, order_bot α := (le_iff_exists_mul : ∀ a b : α, a ≤ b ↔ ∃ c, b = a * c) section canonically_ordered_monoid variables [canonically_ordered_monoid α] {a b c d : α} @[to_additive] lemma le_iff_exists_mul : a ≤ b ↔ ∃c, b = a * c := canonically_ordered_monoid.le_iff_exists_mul a b @[to_additive] lemma self_le_mul_right (a b : α) : a ≤ a * b := le_iff_exists_mul.mpr ⟨b, rfl⟩ @[to_additive] lemma self_le_mul_left (a b : α) : a ≤ b * a := by { rw [mul_comm], exact self_le_mul_right a b } @[simp, to_additive zero_le] lemma one_le (a : α) : 1 ≤ a := le_iff_exists_mul.mpr ⟨a, (one_mul _).symm⟩ @[simp, to_additive] lemma bot_eq_one : (⊥ : α) = 1 := le_antisymm bot_le (one_le ⊥) @[simp, to_additive] lemma mul_eq_one_iff : a * b = 1 ↔ a = 1 ∧ b = 1 := mul_eq_one_iff' (one_le _) (one_le _) @[simp, to_additive] lemma le_one_iff_eq_one : a ≤ 1 ↔ a = 1 := iff.intro (assume h, le_antisymm h (one_le a)) (assume h, h ▸ le_refl a) @[to_additive] lemma one_lt_iff_ne_one : 1 < a ↔ a ≠ 1 := iff.intro ne_of_gt $ assume hne, lt_of_le_of_ne (one_le _) hne.symm @[to_additive] lemma exists_pos_mul_of_lt (h : a < b) : ∃ c > 1, a * c = b := begin obtain ⟨c, hc⟩ := le_iff_exists_mul.1 h.le, refine ⟨c, one_lt_iff_ne_one.2 _, hc.symm⟩, rintro rfl, simpa [hc, lt_irrefl] using h end @[to_additive] lemma le_mul_left (h : a ≤ c) : a ≤ b * c := calc a = 1 * a : by simp ... ≤ b * c : mul_le_mul' (one_le _) h @[to_additive] lemma le_mul_self : a ≤ b * a := le_mul_left (le_refl a) @[to_additive] lemma le_mul_right (h : a ≤ b) : a ≤ b * c := calc a = a * 1 : by simp ... ≤ b * c : mul_le_mul' h (one_le _) @[to_additive] lemma le_self_mul : a ≤ a * c := le_mul_right (le_refl a) @[to_additive] lemma lt_iff_exists_mul [covariant_class α α (*) (<)] : a < b ↔ ∃ c > 1, b = a * c := begin simp_rw [lt_iff_le_and_ne, and_comm, le_iff_exists_mul, ← exists_and_distrib_left, exists_prop], apply exists_congr, intro c, rw [and.congr_left_iff, gt_iff_lt], rintro rfl, split, { rw [one_lt_iff_ne_one], apply mt, rintro rfl, rw [mul_one] }, { rw [← (self_le_mul_right a c).lt_iff_ne], apply lt_mul_of_one_lt_right' } end -- This instance looks absurd: a monoid already has a zero /-- Adding a new zero to a canonically ordered additive monoid produces another one. -/ instance with_zero.canonically_ordered_add_monoid {α : Type u} [canonically_ordered_add_monoid α] : canonically_ordered_add_monoid (with_zero α) := { le_iff_exists_add := λ a b, begin apply with_zero.cases_on a, { exact iff_of_true bot_le ⟨b, (zero_add b).symm⟩ }, apply with_zero.cases_on b, { intro b', refine iff_of_false (mt (le_antisymm bot_le) (by simp)) (not_exists.mpr (λ c, _)), apply with_zero.cases_on c; simp [←with_zero.coe_add] }, { simp only [le_iff_exists_add, with_zero.coe_le_coe], intros, split; rintro ⟨c, h⟩, { exact ⟨c, congr_arg coe h⟩ }, { induction c using with_zero.cases_on, { refine ⟨0, _⟩, simpa using h }, { refine ⟨c, _⟩, simpa [←with_zero.coe_add] using h } } } end, .. with_zero.order_bot, .. with_zero.ordered_add_comm_monoid zero_le } instance with_top.canonically_ordered_add_monoid {α : Type u} [canonically_ordered_add_monoid α] : canonically_ordered_add_monoid (with_top α) := { le_iff_exists_add := assume a b, match a, b with | a, none := show a ≤ ⊤ ↔ ∃c, ⊤ = a + c, by simp; refine ⟨⊤, _⟩; cases a; refl | (some a), (some b) := show (a:with_top α) ≤ ↑b ↔ ∃c:with_top α, ↑b = ↑a + c, begin simp [canonically_ordered_add_monoid.le_iff_exists_add, -add_comm], split, { rintro ⟨c, rfl⟩, refine ⟨c, _⟩, norm_cast }, { exact assume h, match b, h with _, ⟨some c, rfl⟩ := ⟨_, rfl⟩ end } end | none, some b := show (⊤ : with_top α) ≤ b ↔ ∃c:with_top α, ↑b = ⊤ + c, by simp end, .. with_top.order_bot, .. with_top.ordered_add_comm_monoid } @[priority 100, to_additive] instance canonically_ordered_monoid.has_exists_mul_of_le (α : Type u) [canonically_ordered_monoid α] : has_exists_mul_of_le α := { exists_mul_of_le := λ a b hab, le_iff_exists_mul.mp hab } end canonically_ordered_monoid lemma pos_of_gt {M : Type*} [canonically_ordered_add_monoid M] {n m : M} (h : n < m) : 0 < m := lt_of_le_of_lt (zero_le _) h /-- A canonically linear-ordered additive monoid is a canonically ordered additive monoid whose ordering is a linear order. -/ @[protect_proj, ancestor canonically_ordered_add_monoid linear_order] class canonically_linear_ordered_add_monoid (α : Type*) extends canonically_ordered_add_monoid α, linear_order α /-- A canonically linear-ordered monoid is a canonically ordered monoid whose ordering is a linear order. -/ @[protect_proj, ancestor canonically_ordered_monoid linear_order, to_additive] class canonically_linear_ordered_monoid (α : Type*) extends canonically_ordered_monoid α, linear_order α section canonically_linear_ordered_monoid variables [canonically_linear_ordered_monoid α] @[priority 100, to_additive] -- see Note [lower instance priority] instance canonically_linear_ordered_monoid.semilattice_sup_bot : semilattice_sup_bot α := { ..lattice_of_linear_order, ..canonically_ordered_monoid.to_order_bot α } instance with_top.canonically_linear_ordered_add_monoid (α : Type*) [canonically_linear_ordered_add_monoid α] : canonically_linear_ordered_add_monoid (with_top α) := { .. (infer_instance : canonically_ordered_add_monoid (with_top α)), .. (infer_instance : linear_order (with_top α)) } @[to_additive] lemma min_mul_distrib (a b c : α) : min a (b * c) = min a (min a b * min a c) := begin cases le_total a b with hb hb, { simp [hb, le_mul_right] }, { cases le_total a c with hc hc, { simp [hc, le_mul_left] }, { simp [hb, hc] } } end @[to_additive] lemma min_mul_distrib' (a b c : α) : min (a * b) c = min (min a c * min b c) c := by simpa [min_comm _ c] using min_mul_distrib c a b @[simp, to_additive] lemma one_min (a : α) : min 1 a = 1 := min_eq_left (one_le a) @[simp, to_additive] lemma min_one (a : α) : min a 1 = 1 := min_eq_right (one_le a) end canonically_linear_ordered_monoid /-- An ordered cancellative additive commutative monoid is an additive commutative monoid with a partial order, in which addition is cancellative and monotone. -/ @[protect_proj, ancestor add_cancel_comm_monoid partial_order] class ordered_cancel_add_comm_monoid (α : Type u) extends add_cancel_comm_monoid α, partial_order α := (add_le_add_left : ∀ a b : α, a ≤ b → ∀ c : α, c + a ≤ c + b) (le_of_add_le_add_left : ∀ a b c : α, a + b ≤ a + c → b ≤ c) /-- An ordered cancellative commutative monoid is a commutative monoid with a partial order, in which multiplication is cancellative and monotone. -/ @[protect_proj, ancestor cancel_comm_monoid partial_order, to_additive] class ordered_cancel_comm_monoid (α : Type u) extends cancel_comm_monoid α, partial_order α := (mul_le_mul_left : ∀ a b : α, a ≤ b → ∀ c : α, c * a ≤ c * b) (le_of_mul_le_mul_left : ∀ a b c : α, a * b ≤ a * c → b ≤ c) section ordered_cancel_comm_monoid variables [ordered_cancel_comm_monoid α] {a b c d : α} @[to_additive] lemma ordered_cancel_comm_monoid.lt_of_mul_lt_mul_left : ∀ a b c : α, a * b < a * c → b < c := λ a b c h, lt_of_le_not_le (ordered_cancel_comm_monoid.le_of_mul_le_mul_left a b c h.le) $ mt (λ h, ordered_cancel_comm_monoid.mul_le_mul_left _ _ h _) (not_le_of_gt h) @[to_additive] instance ordered_cancel_comm_monoid.to_contravariant_class_left (M : Type*) [ordered_cancel_comm_monoid M] : contravariant_class M M (*) (<) := { elim := λ a b c, ordered_cancel_comm_monoid.lt_of_mul_lt_mul_left _ _ _ } /- This instance can be proven with `by apply_instance`. However, by analogy with the instance `ordered_cancel_comm_monoid.to_covariant_class_right` above, I imagine that without this instance, some Type would not have a `contravariant_class M M (function.swap (*)) (<)` instance. -/ @[to_additive] instance ordered_cancel_comm_monoid.to_contravariant_class_right (M : Type*) [ordered_cancel_comm_monoid M] : contravariant_class M M (swap (*)) (<) := contravariant_swap_mul_lt_of_contravariant_mul_lt M @[priority 100, to_additive] -- see Note [lower instance priority] instance ordered_cancel_comm_monoid.to_ordered_comm_monoid : ordered_comm_monoid α := { ..‹ordered_cancel_comm_monoid α› } /-- Pullback an `ordered_cancel_comm_monoid` under an injective map. See note [reducible non-instances]. -/ @[reducible, to_additive function.injective.ordered_cancel_add_comm_monoid "Pullback an `ordered_cancel_add_comm_monoid` under an injective map."] def function.injective.ordered_cancel_comm_monoid {β : Type*} [has_one β] [has_mul β] (f : β → α) (hf : function.injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : ordered_cancel_comm_monoid β := { le_of_mul_le_mul_left := λ a b c (bc : f (a * b) ≤ f (a * c)), (mul_le_mul_iff_left (f a)).mp (by rwa [← mul, ← mul]), ..hf.left_cancel_semigroup f mul, ..hf.ordered_comm_monoid f one mul } end ordered_cancel_comm_monoid /-! Some lemmas about types that have an ordering and a binary operation, with no rules relating them. -/ @[to_additive] lemma fn_min_mul_fn_max {β} [linear_order α] [comm_semigroup β] (f : α → β) (n m : α) : f (min n m) * f (max n m) = f n * f m := by { cases le_total n m with h h; simp [h, mul_comm] } @[to_additive] lemma min_mul_max [linear_order α] [comm_semigroup α] (n m : α) : min n m * max n m = n * m := fn_min_mul_fn_max id n m /-- A linearly ordered cancellative additive commutative monoid is an additive commutative monoid with a decidable linear order in which addition is cancellative and monotone. -/ @[protect_proj, ancestor ordered_cancel_add_comm_monoid linear_ordered_add_comm_monoid] class linear_ordered_cancel_add_comm_monoid (α : Type u) extends ordered_cancel_add_comm_monoid α, linear_ordered_add_comm_monoid α /-- A linearly ordered cancellative commutative monoid is a commutative monoid with a linear order in which multiplication is cancellative and monotone. -/ @[protect_proj, ancestor ordered_cancel_comm_monoid linear_ordered_comm_monoid, to_additive] class linear_ordered_cancel_comm_monoid (α : Type u) extends ordered_cancel_comm_monoid α, linear_ordered_comm_monoid α section covariant_class_mul_le variables [linear_order α] section has_mul variable [has_mul α] section left variable [covariant_class α α (*) (≤)] @[to_additive] lemma min_mul_mul_left (a b c : α) : min (a * b) (a * c) = a * min b c := (monotone_id.const_mul' a).map_min.symm @[to_additive] lemma max_mul_mul_left (a b c : α) : max (a * b) (a * c) = a * max b c := (monotone_id.const_mul' a).map_max.symm end left section right variable [covariant_class α α (function.swap (*)) (≤)] @[to_additive] lemma min_mul_mul_right (a b c : α) : min (a * c) (b * c) = min a b * c := (monotone_id.mul_const' c).map_min.symm @[to_additive] lemma max_mul_mul_right (a b c : α) : max (a * c) (b * c) = max a b * c := (monotone_id.mul_const' c).map_max.symm end right end has_mul variable [monoid α] @[to_additive] lemma min_le_mul_of_one_le_right [covariant_class α α (*) (≤)] {a b : α} (hb : 1 ≤ b) : min a b ≤ a * b := min_le_iff.2 $ or.inl $ le_mul_of_one_le_right' hb @[to_additive] lemma min_le_mul_of_one_le_left [covariant_class α α (function.swap (*)) (≤)] {a b : α} (ha : 1 ≤ a) : min a b ≤ a * b := min_le_iff.2 $ or.inr $ le_mul_of_one_le_left' ha @[to_additive] lemma max_le_mul_of_one_le [covariant_class α α (*) (≤)] [covariant_class α α (function.swap (*)) (≤)] {a b : α} (ha : 1 ≤ a) (hb : 1 ≤ b) : max a b ≤ a * b := max_le_iff.2 ⟨le_mul_of_one_le_right' hb, le_mul_of_one_le_left' ha⟩ end covariant_class_mul_le section linear_ordered_cancel_comm_monoid variables [linear_ordered_cancel_comm_monoid α] /-- Pullback a `linear_ordered_cancel_comm_monoid` under an injective map. See note [reducible non-instances]. -/ @[reducible, to_additive function.injective.linear_ordered_cancel_add_comm_monoid "Pullback a `linear_ordered_cancel_add_comm_monoid` under an injective map."] def function.injective.linear_ordered_cancel_comm_monoid {β : Type*} [has_one β] [has_mul β] (f : β → α) (hf : function.injective f) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : linear_ordered_cancel_comm_monoid β := { ..hf.linear_ordered_comm_monoid f one mul, ..hf.ordered_cancel_comm_monoid f one mul } end linear_ordered_cancel_comm_monoid namespace order_dual @[to_additive] instance [h : has_mul α] : has_mul (order_dual α) := h @[to_additive] instance [h : has_one α] : has_one (order_dual α) := h @[to_additive] instance [h : monoid α] : monoid (order_dual α) := h @[to_additive] instance [h : comm_monoid α] : comm_monoid (order_dual α) := h @[to_additive] instance [h : cancel_comm_monoid α] : cancel_comm_monoid (order_dual α) := h @[to_additive] instance contravariant_class_mul_le [has_le α] [has_mul α] [c : contravariant_class α α (*) (≤)] : contravariant_class (order_dual α) (order_dual α) (*) (≤) := ⟨c.1.flip⟩ @[to_additive] instance covariant_class_mul_le [has_le α] [has_mul α] [c : covariant_class α α (*) (≤)] : covariant_class (order_dual α) (order_dual α) (*) (≤) := ⟨c.1.flip⟩ @[to_additive] instance contravariant_class_swap_mul_le [has_le α] [has_mul α] [c : contravariant_class α α (swap (*)) (≤)] : contravariant_class (order_dual α) (order_dual α) (swap (*)) (≤) := ⟨c.1.flip⟩ @[to_additive] instance covariant_class_swap_mul_le [has_le α] [has_mul α] [c : covariant_class α α (swap (*)) (≤)] : covariant_class (order_dual α) (order_dual α) (swap (*)) (≤) := ⟨c.1.flip⟩ @[to_additive] instance contravariant_class_mul_lt [has_lt α] [has_mul α] [c : contravariant_class α α (*) (<)] : contravariant_class (order_dual α) (order_dual α) (*) (<) := ⟨c.1.flip⟩ @[to_additive] instance covariant_class_mul_lt [has_lt α] [has_mul α] [c : covariant_class α α (*) (<)] : covariant_class (order_dual α) (order_dual α) (*) (<) := ⟨c.1.flip⟩ @[to_additive] instance contravariant_class_swap_mul_lt [has_lt α] [has_mul α] [c : contravariant_class α α (swap (*)) (<)] : contravariant_class (order_dual α) (order_dual α) (swap (*)) (<) := ⟨c.1.flip⟩ @[to_additive] instance covariant_class_swap_mul_lt [has_lt α] [has_mul α] [c : covariant_class α α (swap (*)) (<)] : covariant_class (order_dual α) (order_dual α) (swap (*)) (<) := ⟨c.1.flip⟩ @[to_additive] instance [ordered_comm_monoid α] : ordered_comm_monoid (order_dual α) := { mul_le_mul_left := λ a b h c, mul_le_mul_left' h c, .. order_dual.partial_order α, .. order_dual.comm_monoid } @[to_additive ordered_cancel_add_comm_monoid.to_contravariant_class] instance ordered_cancel_comm_monoid.to_contravariant_class [ordered_cancel_comm_monoid α] : contravariant_class (order_dual α) (order_dual α) has_mul.mul has_le.le := { elim := λ a b c bc, (ordered_cancel_comm_monoid.le_of_mul_le_mul_left a c b (dual_le.mp bc)) } @[to_additive] instance [ordered_cancel_comm_monoid α] : ordered_cancel_comm_monoid (order_dual α) := { le_of_mul_le_mul_left := λ a b c : α, le_of_mul_le_mul_left', .. order_dual.ordered_comm_monoid, .. order_dual.cancel_comm_monoid } @[to_additive] instance [linear_ordered_cancel_comm_monoid α] : linear_ordered_cancel_comm_monoid (order_dual α) := { .. order_dual.linear_order α, .. order_dual.ordered_cancel_comm_monoid } @[to_additive] instance [linear_ordered_comm_monoid α] : linear_ordered_comm_monoid (order_dual α) := { .. order_dual.linear_order α, .. order_dual.ordered_comm_monoid } end order_dual section ordered_cancel_add_comm_monoid variable [ordered_cancel_add_comm_monoid α] namespace with_top lemma add_lt_add_iff_left {a b c : with_top α} (ha : a ≠ ⊤) : a + b < a + c ↔ b < c := begin lift a to α using ha, cases b; cases c, { simp [none_eq_top] }, { simp [some_eq_coe, none_eq_top, coe_lt_top] }, { simp [some_eq_coe, none_eq_top, ← coe_add, coe_lt_top] }, { simp [some_eq_coe, ← coe_add, coe_lt_coe] } end lemma add_lt_add_iff_right {a b c : with_top α} (ha : a ≠ ⊤) : (c + a < b + a ↔ c < b) := by simp only [← add_comm a, add_lt_add_iff_left ha] instance contravariant_class_add_lt : contravariant_class (with_top α) (with_top α) (+) (<) := begin refine ⟨λ a b c h, _⟩, cases a, { rw [none_eq_top, top_add, top_add] at h, exact (lt_irrefl ⊤ h).elim }, { exact (add_lt_add_iff_left coe_ne_top).1 h } end end with_top namespace with_bot lemma add_lt_add_iff_left {a b c : with_bot α} (ha : a ≠ ⊥) : a + b < a + c ↔ b < c := @with_top.add_lt_add_iff_left (order_dual α) _ a c b ha lemma add_lt_add_iff_right {a b c : with_bot α} (ha : a ≠ ⊥) : b + a < c + a ↔ b < c := @with_top.add_lt_add_iff_right (order_dual α) _ _ _ _ ha instance contravariant_class_add_lt : contravariant_class (with_bot α) (with_bot α) (+) (<) := @order_dual.contravariant_class_add_lt (with_top $ order_dual α) _ _ _ end with_bot end ordered_cancel_add_comm_monoid namespace prod variables {M N : Type*} @[to_additive] instance [ordered_cancel_comm_monoid M] [ordered_cancel_comm_monoid N] : ordered_cancel_comm_monoid (M × N) := { mul_le_mul_left := λ a b h c, ⟨mul_le_mul_left' h.1 _, mul_le_mul_left' h.2 _⟩, le_of_mul_le_mul_left := λ a b c h, ⟨le_of_mul_le_mul_left' h.1, le_of_mul_le_mul_left' h.2⟩, .. prod.cancel_comm_monoid, .. prod.partial_order M N } end prod section type_tags instance : Π [preorder α], preorder (multiplicative α) := id instance : Π [preorder α], preorder (additive α) := id instance : Π [partial_order α], partial_order (multiplicative α) := id instance : Π [partial_order α], partial_order (additive α) := id instance : Π [linear_order α], linear_order (multiplicative α) := id instance : Π [linear_order α], linear_order (additive α) := id instance [ordered_add_comm_monoid α] : ordered_comm_monoid (multiplicative α) := { mul_le_mul_left := @ordered_add_comm_monoid.add_le_add_left α _, ..multiplicative.partial_order, ..multiplicative.comm_monoid } instance [ordered_comm_monoid α] : ordered_add_comm_monoid (additive α) := { add_le_add_left := @ordered_comm_monoid.mul_le_mul_left α _, ..additive.partial_order, ..additive.add_comm_monoid } instance [ordered_cancel_add_comm_monoid α] : ordered_cancel_comm_monoid (multiplicative α) := { le_of_mul_le_mul_left := @ordered_cancel_add_comm_monoid.le_of_add_le_add_left α _, ..multiplicative.left_cancel_semigroup, ..multiplicative.ordered_comm_monoid } instance [ordered_cancel_comm_monoid α] : ordered_cancel_add_comm_monoid (additive α) := { le_of_add_le_add_left := @ordered_cancel_comm_monoid.le_of_mul_le_mul_left α _, ..additive.add_left_cancel_semigroup, ..additive.ordered_add_comm_monoid } instance [linear_ordered_add_comm_monoid α] : linear_ordered_comm_monoid (multiplicative α) := { ..multiplicative.linear_order, ..multiplicative.ordered_comm_monoid } instance [linear_ordered_comm_monoid α] : linear_ordered_add_comm_monoid (additive α) := { ..additive.linear_order, ..additive.ordered_add_comm_monoid } end type_tags /-- The order embedding sending `b` to `a * b`, for some fixed `a`. See also `order_iso.mul_left` when working in an ordered group. -/ @[to_additive "The order embedding sending `b` to `a + b`, for some fixed `a`. See also `order_iso.add_left` when working in an additive ordered group.", simps] def order_embedding.mul_left {α : Type*} [has_mul α] [linear_order α] [covariant_class α α (*) (<)] (m : α) : α ↪o α := order_embedding.of_strict_mono (λ n, m * n) (λ a b w, mul_lt_mul_left' w m) /-- The order embedding sending `b` to `b * a`, for some fixed `a`. See also `order_iso.mul_right` when working in an ordered group. -/ @[to_additive "The order embedding sending `b` to `b + a`, for some fixed `a`. See also `order_iso.add_right` when working in an additive ordered group.", simps] def order_embedding.mul_right {α : Type*} [has_mul α] [linear_order α] [covariant_class α α (swap (*)) (<)] (m : α) : α ↪o α := order_embedding.of_strict_mono (λ n, n * m) (λ a b w, mul_lt_mul_right' w m)
fc8b4c750ba7c5d4a80be2d1ebe00865739f65a5
94e33a31faa76775069b071adea97e86e218a8ee
/src/algebra/module/basic.lean
b7e07cf660abfab421112bc05114061b2ddd5bcd
[ "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
25,226
lean
/- Copyright (c) 2015 Nathaniel Thomas. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Nathaniel Thomas, Jeremy Avigad, Johannes Hölzl, Mario Carneiro -/ import algebra.big_operators.basic import algebra.smul_with_zero import group_theory.group_action.big_operators import group_theory.group_action.group import tactic.norm_num /-! # Modules over a ring In this file we define * `module R M` : an additive commutative monoid `M` is a `module` over a `semiring R` if for `r : R` and `x : M` their "scalar multiplication `r • x : M` is defined, and the operation `•` satisfies some natural associativity and distributivity axioms similar to those on a ring. ## Implementation notes In typical mathematical usage, our definition of `module` corresponds to "semimodule", and the word "module" is reserved for `module R M` where `R` is a `ring` and `M` an `add_comm_group`. If `R` is a `field` and `M` an `add_comm_group`, `M` would be called an `R`-vector space. Since those assumptions can be made by changing the typeclasses applied to `R` and `M`, without changing the axioms in `module`, mathlib calls everything a `module`. In older versions of mathlib, we had separate `semimodule` and `vector_space` abbreviations. This caused inference issues in some cases, while not providing any real advantages, so we decided to use a canonical `module` typeclass throughout. ## Tags semimodule, module, vector space -/ open function open_locale big_operators universes u v variables {α R k S M M₂ M₃ ι : Type*} /-- A module is a generalization of vector spaces to a scalar semiring. It consists of a scalar semiring `R` and an additive monoid of "vectors" `M`, connected by a "scalar multiplication" operation `r • x : M` (where `r : R` and `x : M`) with some natural associativity and distributivity axioms similar to those on a ring. -/ @[ext, protect_proj] class module (R : Type u) (M : Type v) [semiring R] [add_comm_monoid M] extends distrib_mul_action R M := (add_smul : ∀(r s : R) (x : M), (r + s) • x = r • x + s • x) (zero_smul : ∀x : M, (0 : R) • x = 0) section add_comm_monoid variables [semiring R] [add_comm_monoid M] [module R M] (r s : R) (x y : M) /-- A module over a semiring automatically inherits a `mul_action_with_zero` structure. -/ @[priority 100] -- see Note [lower instance priority] instance module.to_mul_action_with_zero : mul_action_with_zero R M := { smul_zero := smul_zero, zero_smul := module.zero_smul, ..(infer_instance : mul_action R M) } instance add_comm_monoid.nat_module : module ℕ M := { one_smul := one_nsmul, mul_smul := λ m n a, mul_nsmul a m n, smul_add := λ n a b, nsmul_add a b n, smul_zero := nsmul_zero, zero_smul := zero_nsmul, add_smul := λ r s x, add_nsmul x r s } theorem add_smul : (r + s) • x = r • x + s • x := module.add_smul r s x lemma convex.combo_self {a b : R} (h : a + b = 1) (x : M) : a • x + b • x = x := by rw [←add_smul, h, one_smul] variables (R) theorem two_smul : (2 : R) • x = x + x := by rw [bit0, add_smul, one_smul] theorem two_smul' : (2 : R) • x = bit0 x := two_smul R x @[simp] lemma inv_of_two_smul_add_inv_of_two_smul [invertible (2 : R)] (x : M) : (⅟2 : R) • x + (⅟2 : R) • x = x := convex.combo_self inv_of_two_add_inv_of_two _ /-- Pullback a `module` structure along an injective additive monoid homomorphism. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.module [add_comm_monoid M₂] [has_smul R M₂] (f : M₂ →+ M) (hf : injective f) (smul : ∀ (c : R) x, f (c • x) = c • f x) : module R M₂ := { smul := (•), add_smul := λ c₁ c₂ x, hf $ by simp only [smul, f.map_add, add_smul], zero_smul := λ x, hf $ by simp only [smul, zero_smul, f.map_zero], .. hf.distrib_mul_action f smul } /-- Pushforward a `module` structure along a surjective additive monoid homomorphism. -/ protected def function.surjective.module [add_comm_monoid M₂] [has_smul R M₂] (f : M →+ M₂) (hf : surjective f) (smul : ∀ (c : R) x, f (c • x) = c • f x) : module R M₂ := { smul := (•), add_smul := λ c₁ c₂ x, by { rcases hf x with ⟨x, rfl⟩, simp only [add_smul, ← smul, ← f.map_add] }, zero_smul := λ x, by { rcases hf x with ⟨x, rfl⟩, simp only [← f.map_zero, ← smul, zero_smul] }, .. hf.distrib_mul_action f smul } /-- Push forward the action of `R` on `M` along a compatible surjective map `f : R →+* S`. See also `function.surjective.mul_action_left` and `function.surjective.distrib_mul_action_left`. -/ @[reducible] def function.surjective.module_left {R S M : Type*} [semiring R] [add_comm_monoid M] [module R M] [semiring S] [has_smul S M] (f : R →+* S) (hf : function.surjective f) (hsmul : ∀ c (x : M), f c • x = c • x) : module S M := { smul := (•), zero_smul := λ x, by rw [← f.map_zero, hsmul, zero_smul], add_smul := hf.forall₂.mpr (λ a b x, by simp only [← f.map_add, hsmul, add_smul]), .. hf.distrib_mul_action_left f.to_monoid_hom hsmul } variables {R} (M) /-- Compose a `module` with a `ring_hom`, with action `f s • m`. See note [reducible non-instances]. -/ @[reducible] def module.comp_hom [semiring S] (f : S →+* R) : module S M := { smul := has_smul.comp.smul f, add_smul := λ r s x, by simp [add_smul], .. mul_action_with_zero.comp_hom M f.to_monoid_with_zero_hom, .. distrib_mul_action.comp_hom M (f : S →* R) } variables (R) (M) /-- `(•)` as an `add_monoid_hom`. This is a stronger version of `distrib_mul_action.to_add_monoid_End` -/ @[simps apply_apply] def module.to_add_monoid_End : R →+* add_monoid.End M := { map_zero' := add_monoid_hom.ext $ λ r, by simp, map_add' := λ x y, add_monoid_hom.ext $ λ r, by simp [add_smul], ..distrib_mul_action.to_add_monoid_End R M } /-- A convenience alias for `module.to_add_monoid_End` as an `add_monoid_hom`, usually to allow the use of `add_monoid_hom.flip`. -/ def smul_add_hom : R →+ M →+ M := (module.to_add_monoid_End R M).to_add_monoid_hom variables {R M} @[simp] lemma smul_add_hom_apply (r : R) (x : M) : smul_add_hom R M r x = r • x := rfl lemma module.eq_zero_of_zero_eq_one (zero_eq_one : (0 : R) = 1) : x = 0 := by rw [←one_smul R x, ←zero_eq_one, zero_smul] lemma list.sum_smul {l : list R} {x : M} : l.sum • x = (l.map (λ r, r • x)).sum := ((smul_add_hom R M).flip x).map_list_sum l lemma multiset.sum_smul {l : multiset R} {x : M} : l.sum • x = (l.map (λ r, r • x)).sum := ((smul_add_hom R M).flip x).map_multiset_sum l lemma finset.sum_smul {f : ι → R} {s : finset ι} {x : M} : (∑ i in s, f i) • x = (∑ i in s, (f i) • x) := ((smul_add_hom R M).flip x).map_sum f s end add_comm_monoid variables (R) /-- An `add_comm_monoid` that is a `module` over a `ring` carries a natural `add_comm_group` structure. See note [reducible non-instances]. -/ @[reducible] def module.add_comm_monoid_to_add_comm_group [ring R] [add_comm_monoid M] [module R M] : add_comm_group M := { neg := λ a, (-1 : R) • a, add_left_neg := λ a, show (-1 : R) • a + a = 0, by { nth_rewrite 1 ← one_smul _ a, rw [← add_smul, add_left_neg, zero_smul] }, ..(infer_instance : add_comm_monoid M), } variables {R} section add_comm_group variables (R M) [semiring R] [add_comm_group M] instance add_comm_group.int_module : module ℤ M := { one_smul := one_zsmul, mul_smul := λ m n a, mul_zsmul a m n, smul_add := λ n a b, zsmul_add a b n, smul_zero := zsmul_zero, zero_smul := zero_zsmul, add_smul := λ r s x, add_zsmul x r s } /-- A structure containing most informations as in a module, except the fields `zero_smul` and `smul_zero`. As these fields can be deduced from the other ones when `M` is an `add_comm_group`, this provides a way to construct a module structure by checking less properties, in `module.of_core`. -/ @[nolint has_inhabited_instance] structure module.core extends has_smul R M := (smul_add : ∀(r : R) (x y : M), r • (x + y) = r • x + r • y) (add_smul : ∀(r s : R) (x : M), (r + s) • x = r • x + s • x) (mul_smul : ∀(r s : R) (x : M), (r * s) • x = r • s • x) (one_smul : ∀x : M, (1 : R) • x = x) variables {R M} /-- Define `module` without proving `zero_smul` and `smul_zero` by using an auxiliary structure `module.core`, when the underlying space is an `add_comm_group`. -/ def module.of_core (H : module.core R M) : module R M := by letI := H.to_has_smul; exact { zero_smul := λ x, (add_monoid_hom.mk' (λ r : R, r • x) (λ r s, H.add_smul r s x)).map_zero, smul_zero := λ r, (add_monoid_hom.mk' ((•) r) (H.smul_add r)).map_zero, ..H } end add_comm_group /-- A variant of `module.ext` that's convenient for term-mode. -/ -- We'll later use this to show `module ℕ M` and `module ℤ M` are subsingletons. lemma module.ext' {R : Type*} [semiring R] {M : Type*} [add_comm_monoid M] (P Q : module R M) (w : ∀ (r : R) (m : M), by { haveI := P, exact r • m } = by { haveI := Q, exact r • m }) : P = Q := begin ext, exact w _ _ end section module variables [ring R] [add_comm_group M] [module R M] (r s : R) (x y : M) @[simp] theorem neg_smul : -r • x = - (r • x) := eq_neg_of_add_eq_zero_left $ by rw [← add_smul, add_left_neg, zero_smul] @[simp] lemma neg_smul_neg : -r • -x = r • x := by rw [neg_smul, smul_neg, neg_neg] @[simp] theorem units.neg_smul (u : Rˣ) (x : M) : -u • x = - (u • x) := by rw [units.smul_def, units.coe_neg, neg_smul, units.smul_def] variables (R) theorem neg_one_smul (x : M) : (-1 : R) • x = -x := by simp variables {R} theorem sub_smul (r s : R) (y : M) : (r - s) • y = r • y - s • y := by simp [add_smul, sub_eq_add_neg] end module /-- A module over a `subsingleton` semiring is a `subsingleton`. We cannot register this as an instance because Lean has no way to guess `R`. -/ protected theorem module.subsingleton (R M : Type*) [semiring R] [subsingleton R] [add_comm_monoid M] [module R M] : subsingleton M := ⟨λ x y, by rw [← one_smul R x, ← one_smul R y, subsingleton.elim (1:R) 0, zero_smul, zero_smul]⟩ /-- A semiring is `nontrivial` provided that there exists a nontrivial module over this semiring. -/ protected theorem module.nontrivial (R M : Type*) [semiring R] [nontrivial M] [add_comm_monoid M] [module R M] : nontrivial R := (subsingleton_or_nontrivial R).resolve_left $ λ hR, not_subsingleton M $ by exactI module.subsingleton R M @[priority 910] -- see Note [lower instance priority] instance semiring.to_module [semiring R] : module R R := { smul_add := mul_add, add_smul := add_mul, zero_smul := zero_mul, smul_zero := mul_zero } /-- Like `semiring.to_module`, but multiplies on the right. -/ @[priority 910] -- see Note [lower instance priority] instance semiring.to_opposite_module [semiring R] : module Rᵐᵒᵖ R := { smul_add := λ r x y, add_mul _ _ _, add_smul := λ r x y, mul_add _ _ _, ..monoid_with_zero.to_opposite_mul_action_with_zero R} /-- A ring homomorphism `f : R →+* M` defines a module structure by `r • x = f r * x`. -/ def ring_hom.to_module [semiring R] [semiring S] (f : R →+* S) : module R S := module.comp_hom S f /-- The tautological action by `R →+* R` on `R`. This generalizes `function.End.apply_mul_action`. -/ instance ring_hom.apply_distrib_mul_action [semiring R] : distrib_mul_action (R →+* R) R := { smul := ($), smul_zero := ring_hom.map_zero, smul_add := ring_hom.map_add, one_smul := λ _, rfl, mul_smul := λ _ _ _, rfl } @[simp] protected lemma ring_hom.smul_def [semiring R] (f : R →+* R) (a : R) : f • a = f a := rfl /-- `ring_hom.apply_distrib_mul_action` is faithful. -/ instance ring_hom.apply_has_faithful_smul [semiring R] : has_faithful_smul (R →+* R) R := ⟨ring_hom.ext⟩ section add_comm_monoid variables [semiring R] [add_comm_monoid M] [module R M] section variables (R) /-- `nsmul` is equal to any other module structure via a cast. -/ lemma nsmul_eq_smul_cast (n : ℕ) (b : M) : n • b = (n : R) • b := begin induction n with n ih, { rw [nat.cast_zero, zero_smul, zero_smul] }, { rw [nat.succ_eq_add_one, nat.cast_succ, add_smul, add_smul, one_smul, ih, one_smul], } end end /-- Convert back any exotic `ℕ`-smul to the canonical instance. This should not be needed since in mathlib all `add_comm_monoid`s should normally have exactly one `ℕ`-module structure by design. -/ lemma nat_smul_eq_nsmul (h : module ℕ M) (n : ℕ) (x : M) : @has_smul.smul ℕ M h.to_has_smul n x = n • x := by rw [nsmul_eq_smul_cast ℕ n x, nat.cast_id] /-- All `ℕ`-module structures are equal. Not an instance since in mathlib all `add_comm_monoid` should normally have exactly one `ℕ`-module structure by design. -/ def add_comm_monoid.nat_module.unique : unique (module ℕ M) := { default := by apply_instance, uniq := λ P, module.ext' P _ $ λ n, nat_smul_eq_nsmul P n } instance add_comm_monoid.nat_is_scalar_tower : is_scalar_tower ℕ R M := { smul_assoc := λ n x y, nat.rec_on n (by simp only [zero_smul]) (λ n ih, by simp only [nat.succ_eq_add_one, add_smul, one_smul, ih]) } end add_comm_monoid section add_comm_group variables [semiring S] [ring R] [add_comm_group M] [module S M] [module R M] section variables (R) /-- `zsmul` is equal to any other module structure via a cast. -/ lemma zsmul_eq_smul_cast (n : ℤ) (b : M) : n • b = (n : R) • b := have (smul_add_hom ℤ M).flip b = ((smul_add_hom R M).flip b).comp (int.cast_add_hom R), by { ext, simp }, add_monoid_hom.congr_fun this n end /-- Convert back any exotic `ℤ`-smul to the canonical instance. This should not be needed since in mathlib all `add_comm_group`s should normally have exactly one `ℤ`-module structure by design. -/ lemma int_smul_eq_zsmul (h : module ℤ M) (n : ℤ) (x : M) : @has_smul.smul ℤ M h.to_has_smul n x = n • x := by rw [zsmul_eq_smul_cast ℤ n x, int.cast_id] /-- All `ℤ`-module structures are equal. Not an instance since in mathlib all `add_comm_group` should normally have exactly one `ℤ`-module structure by design. -/ def add_comm_group.int_module.unique : unique (module ℤ M) := { default := by apply_instance, uniq := λ P, module.ext' P _ $ λ n, int_smul_eq_zsmul P n } end add_comm_group lemma map_int_cast_smul [add_comm_group M] [add_comm_group M₂] {F : Type*} [add_monoid_hom_class F M M₂] (f : F) (R S : Type*) [ring R] [ring S] [module R M] [module S M₂] (x : ℤ) (a : M) : f ((x : R) • a) = (x : S) • f a := by simp only [←zsmul_eq_smul_cast, map_zsmul] lemma map_nat_cast_smul [add_comm_monoid M] [add_comm_monoid M₂] {F : Type*} [add_monoid_hom_class F M M₂] (f : F) (R S : Type*) [semiring R] [semiring S] [module R M] [module S M₂] (x : ℕ) (a : M) : f ((x : R) • a) = (x : S) • f a := by simp only [←nsmul_eq_smul_cast, map_nsmul] lemma map_inv_int_cast_smul [add_comm_group M] [add_comm_group M₂] {F : Type*} [add_monoid_hom_class F M M₂] (f : F) (R S : Type*) [division_ring R] [division_ring S] [module R M] [module S M₂] (n : ℤ) (x : M) : f ((n⁻¹ : R) • x) = (n⁻¹ : S) • f x := begin by_cases hR : (n : R) = 0; by_cases hS : (n : S) = 0, { simp [hR, hS] }, { suffices : ∀ y, f y = 0, by simp [this], clear x, intro x, rw [← inv_smul_smul₀ hS (f x), ← map_int_cast_smul f R S], simp [hR] }, { suffices : ∀ y, f y = 0, by simp [this], clear x, intro x, rw [← smul_inv_smul₀ hR x, map_int_cast_smul f R S, hS, zero_smul] }, { rw [← inv_smul_smul₀ hS (f _), ← map_int_cast_smul f R S, smul_inv_smul₀ hR] } end lemma map_inv_nat_cast_smul [add_comm_group M] [add_comm_group M₂] {F : Type*} [add_monoid_hom_class F M M₂] (f : F) (R S : Type*) [division_ring R] [division_ring S] [module R M] [module S M₂] (n : ℕ) (x : M) : f ((n⁻¹ : R) • x) = (n⁻¹ : S) • f x := by exact_mod_cast map_inv_int_cast_smul f R S n x lemma map_rat_cast_smul [add_comm_group M] [add_comm_group M₂] {F : Type*} [add_monoid_hom_class F M M₂] (f : F) (R S : Type*) [division_ring R] [division_ring S] [module R M] [module S M₂] (c : ℚ) (x : M) : f ((c : R) • x) = (c : S) • f x := by rw [rat.cast_def, rat.cast_def, div_eq_mul_inv, div_eq_mul_inv, mul_smul, mul_smul, map_int_cast_smul f R S, map_inv_nat_cast_smul f R S] lemma map_rat_smul [add_comm_group M] [add_comm_group M₂] [module ℚ M] [module ℚ M₂] {F : Type*} [add_monoid_hom_class F M M₂] (f : F) (c : ℚ) (x : M) : f (c • x) = c • f x := rat.cast_id c ▸ map_rat_cast_smul f ℚ ℚ c x /-- There can be at most one `module ℚ E` structure on an additive commutative group. This is not an instance because `simp` becomes very slow if we have many `subsingleton` instances, see [gh-6025]. -/ lemma subsingleton_rat_module (E : Type*) [add_comm_group E] : subsingleton (module ℚ E) := ⟨λ P Q, module.ext' P Q $ λ r x, @map_rat_smul _ _ _ _ P Q _ _ (add_monoid_hom.id E) r x⟩ /-- If `E` is a vector space over two division rings `R` and `S`, then scalar multiplications agree on inverses of integer numbers in `R` and `S`. -/ lemma inv_int_cast_smul_eq {E : Type*} (R S : Type*) [add_comm_group E] [division_ring R] [division_ring S] [module R E] [module S E] (n : ℤ) (x : E) : (n⁻¹ : R) • x = (n⁻¹ : S) • x := map_inv_int_cast_smul (add_monoid_hom.id E) R S n x /-- If `E` is a vector space over two division rings `R` and `S`, then scalar multiplications agree on inverses of natural numbers in `R` and `S`. -/ lemma inv_nat_cast_smul_eq {E : Type*} (R S : Type*) [add_comm_group E] [division_ring R] [division_ring S] [module R E] [module S E] (n : ℕ) (x : E) : (n⁻¹ : R) • x = (n⁻¹ : S) • x := map_inv_nat_cast_smul (add_monoid_hom.id E) R S n x /-- If `E` is a vector space over a division rings `R` and has a monoid action by `α`, then that action commutes by scalar multiplication of inverses of integers in `R` -/ lemma inv_int_cast_smul_comm {α E : Type*} (R : Type*) [add_comm_group E] [division_ring R] [monoid α] [module R E] [distrib_mul_action α E] (n : ℤ) (s : α) (x : E) : (n⁻¹ : R) • s • x = s • (n⁻¹ : R) • x := (map_inv_int_cast_smul (distrib_mul_action.to_add_monoid_hom E s) R R n x).symm /-- If `E` is a vector space over a division rings `R` and has a monoid action by `α`, then that action commutes by scalar multiplication of inverses of natural numbers in `R`. -/ lemma inv_nat_cast_smul_comm {α E : Type*} (R : Type*) [add_comm_group E] [division_ring R] [monoid α] [module R E] [distrib_mul_action α E] (n : ℕ) (s : α) (x : E) : (n⁻¹ : R) • s • x = s • (n⁻¹ : R) • x := (map_inv_nat_cast_smul (distrib_mul_action.to_add_monoid_hom E s) R R n x).symm /-- If `E` is a vector space over two division rings `R` and `S`, then scalar multiplications agree on rational numbers in `R` and `S`. -/ lemma rat_cast_smul_eq {E : Type*} (R S : Type*) [add_comm_group E] [division_ring R] [division_ring S] [module R E] [module S E] (r : ℚ) (x : E) : (r : R) • x = (r : S) • x := map_rat_cast_smul (add_monoid_hom.id E) R S r x instance add_comm_group.int_is_scalar_tower {R : Type u} {M : Type v} [ring R] [add_comm_group M] [module R M]: is_scalar_tower ℤ R M := { smul_assoc := λ n x y, ((smul_add_hom R M).flip y).map_zsmul x n } instance is_scalar_tower.rat {R : Type u} {M : Type v} [ring R] [add_comm_group M] [module R M] [module ℚ R] [module ℚ M] : is_scalar_tower ℚ R M := { smul_assoc := λ r x y, map_rat_smul ((smul_add_hom R M).flip y) r x } instance smul_comm_class.rat {R : Type u} {M : Type v} [semiring R] [add_comm_group M] [module R M] [module ℚ M] : smul_comm_class ℚ R M := { smul_comm := λ r x y, (map_rat_smul (smul_add_hom R M x) r y).symm } instance smul_comm_class.rat' {R : Type u} {M : Type v} [semiring R] [add_comm_group M] [module R M] [module ℚ M] : smul_comm_class R ℚ M := smul_comm_class.symm _ _ _ section no_zero_smul_divisors /-! ### `no_zero_smul_divisors` This section defines the `no_zero_smul_divisors` class, and includes some tests for the vanishing of elements (especially in modules over division rings). -/ /-- `no_zero_smul_divisors R M` states that a scalar multiple is `0` only if either argument is `0`. This a version of saying that `M` is torsion free, without assuming `R` is zero-divisor free. The main application of `no_zero_smul_divisors R M`, when `M` is a module, is the result `smul_eq_zero`: a scalar multiple is `0` iff either argument is `0`. It is a generalization of the `no_zero_divisors` class to heterogeneous multiplication. -/ class no_zero_smul_divisors (R M : Type*) [has_zero R] [has_zero M] [has_smul R M] : Prop := (eq_zero_or_eq_zero_of_smul_eq_zero : ∀ {c : R} {x : M}, c • x = 0 → c = 0 ∨ x = 0) export no_zero_smul_divisors (eq_zero_or_eq_zero_of_smul_eq_zero) /-- Pullback a `no_zero_smul_divisors` instance along an injective function. -/ lemma function.injective.no_zero_smul_divisors {R M N : Type*} [has_zero R] [has_zero M] [has_zero N] [has_smul R M] [has_smul R N] [no_zero_smul_divisors R N] (f : M → N) (hf : function.injective f) (h0 : f 0 = 0) (hs : ∀ (c : R) (x : M), f (c • x) = c • f x) : no_zero_smul_divisors R M := ⟨λ c m h, or.imp_right (@hf _ _) $ h0.symm ▸ eq_zero_or_eq_zero_of_smul_eq_zero (by rw [←hs, h, h0])⟩ @[priority 100] -- See note [lower instance priority] instance no_zero_divisors.to_no_zero_smul_divisors [has_zero R] [has_mul R] [no_zero_divisors R] : no_zero_smul_divisors R R := ⟨λ c x, eq_zero_or_eq_zero_of_mul_eq_zero⟩ section module variables [semiring R] [add_comm_monoid M] [module R M] @[simp] theorem smul_eq_zero [no_zero_smul_divisors R M] {c : R} {x : M} : c • x = 0 ↔ c = 0 ∨ x = 0 := ⟨eq_zero_or_eq_zero_of_smul_eq_zero, λ h, h.elim (λ h, h.symm ▸ zero_smul R x) (λ h, h.symm ▸ smul_zero c)⟩ theorem smul_ne_zero [no_zero_smul_divisors R M] {c : R} {x : M} : c • x ≠ 0 ↔ c ≠ 0 ∧ x ≠ 0 := by simp only [ne.def, smul_eq_zero, not_or_distrib] section nat variables (R) (M) [no_zero_smul_divisors R M] [char_zero R] include R lemma nat.no_zero_smul_divisors : no_zero_smul_divisors ℕ M := ⟨by { intros c x, rw [nsmul_eq_smul_cast R, smul_eq_zero], simp }⟩ @[simp] lemma two_nsmul_eq_zero {v : M} : 2 • v = 0 ↔ v = 0 := by { haveI := nat.no_zero_smul_divisors R M, norm_num [smul_eq_zero] } end nat variables (R M) /-- If `M` is an `R`-module with one and `M` has characteristic zero, then `R` has characteristic zero as well. Usually `M` is an `R`-algebra. -/ lemma char_zero.of_module (M) [add_comm_monoid_with_one M] [char_zero M] [module R M] : char_zero R := begin refine ⟨λ m n h, @nat.cast_injective M _ _ _ _ _⟩, rw [← nsmul_one, ← nsmul_one, nsmul_eq_smul_cast R m (1 : M), nsmul_eq_smul_cast R n (1 : M), h] end end module section add_comm_group -- `R` can still be a semiring here variables [semiring R] [add_comm_group M] [module R M] section smul_injective variables (M) lemma smul_right_injective [no_zero_smul_divisors R M] {c : R} (hc : c ≠ 0) : function.injective ((•) c : M → M) := (injective_iff_map_eq_zero (smul_add_hom R M c)).2 $ λ a ha, (smul_eq_zero.mp ha).resolve_left hc variables {M} lemma smul_right_inj [no_zero_smul_divisors R M] {c : R} (hc : c ≠ 0) {x y : M} : c • x = c • y ↔ x = y := (smul_right_injective M hc).eq_iff end smul_injective section nat variables (R M) [no_zero_smul_divisors R M] [char_zero R] include R lemma self_eq_neg {v : M} : v = - v ↔ v = 0 := by rw [← two_nsmul_eq_zero R M, two_smul, add_eq_zero_iff_eq_neg] lemma neg_eq_self {v : M} : - v = v ↔ v = 0 := by rw [eq_comm, self_eq_neg R M] lemma self_ne_neg {v : M} : v ≠ -v ↔ v ≠ 0 := (self_eq_neg R M).not lemma neg_ne_self {v : M} : -v ≠ v ↔ v ≠ 0 := (neg_eq_self R M).not end nat end add_comm_group section module variables [ring R] [add_comm_group M] [module R M] [no_zero_smul_divisors R M] section smul_injective variables (R) lemma smul_left_injective {x : M} (hx : x ≠ 0) : function.injective (λ (c : R), c • x) := λ c d h, sub_eq_zero.mp ((smul_eq_zero.mp (calc (c - d) • x = c • x - d • x : sub_smul c d x ... = 0 : sub_eq_zero.mpr h)).resolve_right hx) end smul_injective end module section division_ring variables [division_ring R] [add_comm_group M] [module R M] @[priority 100] -- see note [lower instance priority] instance division_ring.to_no_zero_smul_divisors : no_zero_smul_divisors R M := ⟨λ c x h, or_iff_not_imp_left.2 $ λ hc, (smul_eq_zero_iff_eq' hc).1 h⟩ end division_ring end no_zero_smul_divisors @[simp] lemma nat.smul_one_eq_coe {R : Type*} [semiring R] (m : ℕ) : m • (1 : R) = ↑m := by rw [nsmul_eq_mul, mul_one] @[simp] lemma int.smul_one_eq_coe {R : Type*} [ring R] (m : ℤ) : m • (1 : R) = ↑m := by rw [zsmul_eq_mul, mul_one] lemma finset.cast_card [comm_semiring R] (s : finset α) : (s.card : R) = ∑ a in s, 1 := by rw [finset.sum_const, nat.smul_one_eq_coe]
ac9bd57a2a8ceaf791eb21cf7f8302eb1c59fd12
5e3548e65f2c037cb94cd5524c90c623fbd6d46a
/src_icannos_totilas/aops/1989-USAMO-Problem_3.lean
e3f3202d40ea60caf14fa43087f83b85fca036fd
[]
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
346
lean
import data.real.basic import topology.continuous_function.polynomial import data.polynomial.basic import data.polynomial.ring_division import data.complex.basic theorem USAMO_Problem_3_1989 (P : polynomial ℂ) : complex.abs(P.eval complex.I) < 1 → ∃ a b : ℝ, (P.eval (a+b*complex.I : ℂ) = 0 ∧ (a^2+b^2+1)^2 < 4*b^2+1) := sorry