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
bbe0f556863cb96f7d3e30b4a8d57b831db7e0c7
b3fced0f3ff82d577384fe81653e47df68bb2fa1
/src/measure_theory/outer_measure.lean
608799a9da3177f91bca99e36e6a38d49772252e
[ "Apache-2.0" ]
permissive
ratmice/mathlib
93b251ef5df08b6fd55074650ff47fdcc41a4c75
3a948a6a4cd5968d60e15ed914b1ad2f4423af8d
refs/heads/master
1,599,240,104,318
1,572,981,183,000
1,572,981,183,000
219,830,178
0
0
Apache-2.0
1,572,980,897,000
1,572,980,896,000
null
UTF-8
Lean
false
false
19,571
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro Outer measures -- overapproximations of measures -/ import algebra.big_operators algebra.module topology.instances.ennreal analysis.specific_limits measure_theory.measurable_space noncomputable theory open set lattice finset function filter encodable open_locale classical namespace measure_theory structure outer_measure (α : Type*) := (measure_of : set α → ennreal) (empty : measure_of ∅ = 0) (mono : ∀{s₁ s₂}, s₁ ⊆ s₂ → measure_of s₁ ≤ measure_of s₂) (Union_nat : ∀(s:ℕ → set α), measure_of (⋃i, s i) ≤ (∑i, measure_of (s i))) namespace outer_measure instance {α} : has_coe_to_fun (outer_measure α) := ⟨_, λ m, m.measure_of⟩ section basic variables {α : Type*} {ms : set (outer_measure α)} {m : outer_measure α} @[simp] theorem empty' (m : outer_measure α) : m ∅ = 0 := m.empty theorem mono' (m : outer_measure α) {s₁ s₂} (h : s₁ ⊆ s₂) : m s₁ ≤ m s₂ := m.mono h theorem Union_aux (m : set α → ennreal) (m0 : m ∅ = 0) {β} [encodable β] (s : β → set α) : (∑ b, m (s b)) = ∑ i, m (⋃ b ∈ decode2 β i, s b) := begin have H : ∀ n, m (⋃ b ∈ decode2 β n, s b) ≠ 0 → (decode2 β n).is_some, { intros n h, cases decode2 β n with b, { exact (h (by simp [m0])).elim }, { exact rfl } }, refine tsum_eq_tsum_of_ne_zero_bij (λ n h, option.get (H n h)) _ _ _, { intros m n hm hn e, have := mem_decode2.1 (option.get_mem (H n hn)), rwa [← e, mem_decode2.1 (option.get_mem (H m hm))] at this }, { intros b h, refine ⟨encode b, _, _⟩, { convert h, simp [ext_iff, encodek2] }, { exact option.get_of_mem _ (encodek2 _) } }, { intros n h, transitivity, swap, rw [show decode2 β n = _, from option.get_mem (H n h)], congr, simp [ext_iff, -option.some_get] } end protected theorem Union (m : outer_measure α) {β} [encodable β] (s : β → set α) : m (⋃i, s i) ≤ (∑i, m (s i)) := by rw [Union_decode2, Union_aux _ m.empty' s]; exact m.Union_nat _ lemma Union_null (m : outer_measure α) {β} [encodable β] {s : β → set α} (h : ∀ i, m (s i) = 0) : m (⋃i, s i) = 0 := by simpa [h] using m.Union s protected lemma union (m : outer_measure α) (s₁ s₂ : set α) : m (s₁ ∪ s₂) ≤ m s₁ + m s₂ := begin convert m.Union (λ b, cond b s₁ s₂), { simp [union_eq_Union] }, { rw tsum_fintype, change _ = _ + _, simp } end lemma union_null (m : outer_measure α) {s₁ s₂ : set α} (h₁ : m s₁ = 0) (h₂ : m s₂ = 0) : m (s₁ ∪ s₂) = 0 := by simpa [h₁, h₂] using m.union s₁ s₂ @[extensionality] lemma ext : ∀{μ₁ μ₂ : outer_measure α}, (∀s, μ₁ s = μ₂ s) → μ₁ = μ₂ | ⟨m₁, e₁, _, u₁⟩ ⟨m₂, e₂, _, u₂⟩ h := by congr; exact funext h instance : has_zero (outer_measure α) := ⟨{ measure_of := λ_, 0, empty := rfl, mono := assume _ _ _, le_refl 0, Union_nat := assume s, zero_le _ }⟩ @[simp] theorem zero_apply (s : set α) : (0 : outer_measure α) s = 0 := rfl instance : inhabited (outer_measure α) := ⟨0⟩ instance : has_add (outer_measure α) := ⟨λm₁ m₂, { measure_of := λs, m₁ s + m₂ s, empty := show m₁ ∅ + m₂ ∅ = 0, by simp [outer_measure.empty], mono := assume s₁ s₂ h, add_le_add' (m₁.mono h) (m₂.mono h), Union_nat := assume s, calc m₁ (⋃i, s i) + m₂ (⋃i, s i) ≤ (∑i, m₁ (s i)) + (∑i, m₂ (s i)) : add_le_add' (m₁.Union_nat s) (m₂.Union_nat s) ... = _ : ennreal.tsum_add.symm}⟩ @[simp] theorem add_apply (m₁ m₂ : outer_measure α) (s : set α) : (m₁ + m₂) s = m₁ s + m₂ s := rfl instance : add_comm_monoid (outer_measure α) := { zero := 0, add := (+), add_comm := assume a b, ext $ assume s, add_comm _ _, add_assoc := assume a b c, ext $ assume s, add_assoc _ _ _, add_zero := assume a, ext $ assume s, add_zero _, zero_add := assume a, ext $ assume s, zero_add _ } instance : has_bot (outer_measure α) := ⟨0⟩ instance outer_measure.order_bot : order_bot (outer_measure α) := { le := λm₁ m₂, ∀s, m₁ s ≤ m₂ s, bot := 0, le_refl := assume a s, le_refl _, le_trans := assume a b c hab hbc s, le_trans (hab s) (hbc s), le_antisymm := assume a b hab hba, ext $ assume s, le_antisymm (hab s) (hba s), bot_le := assume a s, zero_le _ } section supremum instance : has_Sup (outer_measure α) := ⟨λms, { measure_of := λs, ⨆m:ms, m.val s, empty := le_zero_iff_eq.1 $ supr_le $ λ ⟨m, h⟩, le_of_eq m.empty, mono := assume s₁ s₂ hs, supr_le_supr $ assume ⟨m, hm⟩, m.mono hs, Union_nat := assume f, supr_le $ assume m, calc m.val (⋃i, f i) ≤ (∑ (i : ℕ), m.val (f i)) : m.val.Union_nat _ ... ≤ (∑i, ⨆m:ms, m.val (f i)) : ennreal.tsum_le_tsum $ assume i, le_supr (λm:ms, m.val (f i)) m }⟩ private lemma le_Sup (hm : m ∈ ms) : m ≤ Sup ms := λ s, le_supr (λm:ms, m.val s) ⟨m, hm⟩ private lemma Sup_le (hm : ∀m' ∈ ms, m' ≤ m) : Sup ms ≤ m := λ s, (supr_le $ assume ⟨m', h'⟩, (hm m' h') s) instance : has_Inf (outer_measure α) := ⟨λs, Sup {m | ∀m'∈s, m ≤ m'}⟩ private lemma Inf_le (hm : m ∈ ms) : Inf ms ≤ m := Sup_le $ assume m' h', h' _ hm private lemma le_Inf (hm : ∀m' ∈ ms, m ≤ m') : m ≤ Inf ms := le_Sup hm instance : complete_lattice (outer_measure α) := { top := Sup univ, le_top := assume a, le_Sup (mem_univ a), Sup := Sup, Sup_le := assume s m, Sup_le, le_Sup := assume s m, le_Sup, Inf := Inf, Inf_le := assume s m, Inf_le, le_Inf := assume s m, le_Inf, sup := λa b, Sup {a, b}, le_sup_left := assume a b, le_Sup $ by simp, le_sup_right := assume a b, le_Sup $ by simp, sup_le := assume a b c ha hb, Sup_le $ by simp [or_imp_distrib, ha, hb] {contextual:=tt}, inf := λa b, Inf {a, b}, inf_le_left := assume a b, Inf_le $ by simp, inf_le_right := assume a b, Inf_le $ by simp, le_inf := assume a b c ha hb, le_Inf $ by simp [or_imp_distrib, ha, hb] {contextual:=tt}, .. outer_measure.order_bot } @[simp] theorem Sup_apply (ms : set (outer_measure α)) (s : set α) : (Sup ms) s = ⨆ m : ms, m s := rfl @[simp] theorem supr_apply {ι} (f : ι → outer_measure α) (s : set α) : (⨆ i : ι, f i) s = ⨆ i, f i s := le_antisymm (supr_le $ λ ⟨_, i, rfl⟩, le_supr _ i) (supr_le $ λ i, le_supr (λ (m : {a : outer_measure α // ∃ i, f i = a}), m.1 s) ⟨f i, i, rfl⟩) @[simp] theorem sup_apply (m₁ m₂ : outer_measure α) (s : set α) : (m₁ ⊔ m₂) s = m₁ s ⊔ m₂ s := by have := supr_apply (λ b, cond b m₁ m₂) s; rwa [supr_bool_eq, supr_bool_eq] at this end supremum def map {β} (f : α → β) (m : outer_measure α) : outer_measure β := { measure_of := λs, m (f ⁻¹' s), empty := m.empty, mono := λ s t h, m.mono (preimage_mono h), Union_nat := λ s, by rw [preimage_Union]; exact m.Union_nat (λ i, f ⁻¹' s i) } @[simp] theorem map_apply {β} (f : α → β) (m : outer_measure α) (s : set β) : map f m s = m (f ⁻¹' s) := rfl @[simp] theorem map_id (m : outer_measure α) : map id m = m := ext $ λ s, rfl @[simp] theorem map_map {β γ} (f : α → β) (g : β → γ) (m : outer_measure α) : map g (map f m) = map (g ∘ f) m := ext $ λ s, rfl instance : functor outer_measure := {map := λ α β, map} instance : is_lawful_functor outer_measure := { id_map := λ α, map_id, comp_map := λ α β γ f g m, (map_map f g m).symm } /-- The dirac outer measure. -/ def dirac (a : α) : outer_measure α := { measure_of := λs, ⨆ h : a ∈ s, 1, empty := by simp, mono := λ s t h, supr_le_supr2 (λ h', ⟨h h', le_refl _⟩), Union_nat := λ s, supr_le $ λ h, let ⟨i, h⟩ := mem_Union.1 h in le_trans (by exact le_supr _ h) (ennreal.le_tsum i) } @[simp] theorem dirac_apply (a : α) (s : set α) : dirac a s = ⨆ h : a ∈ s, 1 := rfl def sum {ι} (f : ι → outer_measure α) : outer_measure α := { measure_of := λs, ∑ i, f i s, empty := by simp, mono := λ s t h, ennreal.tsum_le_tsum (λ i, (f i).mono' h), Union_nat := λ s, by rw ennreal.tsum_comm; exact ennreal.tsum_le_tsum (λ i, (f i).Union_nat _) } @[simp] theorem sum_apply {ι} (f : ι → outer_measure α) (s : set α) : sum f s = ∑ i, f i s := rfl instance : has_scalar ennreal (outer_measure α) := ⟨λ a m, { measure_of := λs, a * m s, empty := by simp, mono := λ s t h, canonically_ordered_semiring.mul_le_mul (le_refl _) (m.mono' h), Union_nat := λ s, by rw ennreal.mul_tsum; exact canonically_ordered_semiring.mul_le_mul (le_refl _) (m.Union_nat _) }⟩ @[simp] theorem smul_apply (a : ennreal) (m : outer_measure α) (s : set α) : (a • m) s = a * m s := rfl instance : semimodule ennreal (outer_measure α) := { smul_add := λ a m₁ m₂, ext $ λ s, mul_add _ _ _, add_smul := λ a b m, ext $ λ s, add_mul _ _ _, mul_smul := λ a b m, ext $ λ s, mul_assoc _ _ _, one_smul := λ m, ext $ λ s, one_mul _, zero_smul := λ m, ext $ λ s, zero_mul _, smul_zero := λ a, ext $ λ s, mul_zero _, ..outer_measure.has_scalar } theorem smul_dirac_apply (a : ennreal) (b : α) (s : set α) : (a • dirac b) s = ⨆ h : b ∈ s, a := by by_cases b ∈ s; simp [h] theorem top_apply {s : set α} (h : s ≠ ∅) : (⊤ : outer_measure α) s = ⊤ := let ⟨a, as⟩ := set.exists_mem_of_ne_empty h in top_unique $ le_supr_of_le ⟨(⊤ : ennreal) • dirac a, trivial⟩ $ by simp [smul_dirac_apply, as] end basic section of_function set_option eqn_compiler.zeta true /-- Given any function `m` assigning measures to sets satisying `m ∅ = 0`, there is a unique maximal outer measure `μ` satisfying `μ s ≤ m s` for all `s : set α`. -/ protected def of_function {α : Type*} (m : set α → ennreal) (m_empty : m ∅ = 0) : outer_measure α := let μ := λs, ⨅{f : ℕ → set α} (h : s ⊆ ⋃i, f i), ∑i, m (f i) in { measure_of := μ, empty := le_antisymm (infi_le_of_le (λ_, ∅) $ infi_le_of_le (empty_subset _) $ by simp [m_empty]) (zero_le _), mono := assume s₁ s₂ hs, infi_le_infi $ assume f, infi_le_infi2 $ assume hb, ⟨subset.trans hs hb, le_refl _⟩, Union_nat := assume s, ennreal.le_of_forall_epsilon_le $ begin assume ε hε (hb : (∑i, μ (s i)) < ⊤), rcases ennreal.exists_pos_sum_of_encodable (ennreal.coe_lt_coe.2 hε) ℕ with ⟨ε', hε', hl⟩, refine le_trans _ (add_le_add_left' (le_of_lt hl)), rw ← ennreal.tsum_add, choose f hf using show ∀i, ∃f:ℕ → set α, s i ⊆ (⋃i, f i) ∧ (∑i, m (f i)) < μ (s i) + ε' i, { intro, have : μ (s i) < μ (s i) + ε' i := ennreal.lt_add_right (lt_of_le_of_lt (by apply ennreal.le_tsum) hb) (by simpa using hε' i), simpa [μ, infi_lt_iff] }, refine le_trans _ (ennreal.tsum_le_tsum $ λ i, le_of_lt (hf i).2), rw [← ennreal.tsum_prod, ← tsum_equiv equiv.nat_prod_nat_equiv_nat.symm], swap, {apply_instance}, refine infi_le_of_le _ (infi_le _ _), exact Union_subset (λ i, subset.trans (hf i).1 $ Union_subset $ λ j, subset.trans (by simp) $ subset_Union _ $ equiv.nat_prod_nat_equiv_nat (i, j)), end } theorem of_function_le {α : Type*} (m : set α → ennreal) (m_empty s) : outer_measure.of_function m m_empty s ≤ m s := let f : ℕ → set α := λi, nat.rec_on i s (λn s, ∅) in infi_le_of_le f $ infi_le_of_le (subset_Union f 0) $ le_of_eq $ calc (∑i, m (f i)) = ({0} : finset ℕ).sum (λi, m (f i)) : tsum_eq_sum $ by intro i; cases i; simp [m_empty] ... = m s : by simp; refl theorem le_of_function {α : Type*} {m m_empty} {μ : outer_measure α} : μ ≤ outer_measure.of_function m m_empty ↔ ∀ s, μ s ≤ m s := ⟨λ H s, le_trans (H _) (of_function_le _ _ _), λ H s, le_infi $ λ f, le_infi $ λ hs, le_trans (μ.mono hs) $ le_trans (μ.Union f) $ ennreal.tsum_le_tsum $ λ i, H _⟩ end of_function section caratheodory_measurable universe u parameters {α : Type u} (m : outer_measure α) include m local attribute [simp] set.inter_comm set.inter_left_comm set.inter_assoc variables {s s₁ s₂ : set α} private def C (s : set α) := ∀t, m t = m (t ∩ s) + m (t \ s) private lemma C_iff_le {s : set α} : C s ↔ ∀t, m (t ∩ s) + m (t \ s) ≤ m t := forall_congr $ λ t, le_antisymm_iff.trans $ and_iff_right $ by convert m.union _ _; rw inter_union_diff t s @[simp] private lemma C_empty : C ∅ := by simp [C, m.empty, diff_empty] private lemma C_compl : C s₁ → C (- s₁) := by simp [C, diff_eq] @[simp] private lemma C_compl_iff : C (- s) ↔ C s := ⟨λ h, by simpa using C_compl m h, C_compl⟩ private lemma C_union (h₁ : C s₁) (h₂ : C s₂) : C (s₁ ∪ s₂) := λ t, begin rw [h₁ t, h₂ (t ∩ s₁), h₂ (t \ s₁), h₁ (t ∩ (s₁ ∪ s₂)), inter_diff_assoc _ _ s₁, set.inter_assoc _ _ s₁, inter_eq_self_of_subset_right (set.subset_union_left _ _), union_diff_left, h₂ (t ∩ s₁)], simp [diff_eq] end private lemma measure_inter_union (h : s₁ ∩ s₂ ⊆ ∅) (h₁ : C s₁) {t : set α} : m (t ∩ (s₁ ∪ s₂)) = m (t ∩ s₁) + m (t ∩ s₂) := by rw [h₁, set.inter_assoc, union_inter_cancel_left, inter_diff_assoc, union_diff_cancel_left h] private lemma C_Union_lt {s : ℕ → set α} : ∀{n:ℕ}, (∀i<n, C (s i)) → C (⋃i<n, s i) | 0 h := by simp [nat.not_lt_zero] | (n + 1) h := by rw Union_lt_succ; exact C_union m (h n (le_refl (n + 1))) (C_Union_lt $ assume i hi, h i $ lt_of_lt_of_le hi $ nat.le_succ _) private lemma C_inter (h₁ : C s₁) (h₂ : C s₂) : C (s₁ ∩ s₂) := by rw [← C_compl_iff, compl_inter]; from C_union _ (C_compl _ h₁) (C_compl _ h₂) private lemma C_sum {s : ℕ → set α} (h : ∀i, C (s i)) (hd : pairwise (disjoint on s)) {t : set α} : ∀ {n}, (finset.range n).sum (λi, m (t ∩ s i)) = m (t ∩ ⋃i<n, s i) | 0 := by simp [nat.not_lt_zero, m.empty] | (nat.succ n) := begin simp [Union_lt_succ, range_succ], rw [measure_inter_union m _ (h n), C_sum], intro a, simpa [range_succ] using λ h₁ i hi h₂, hd _ _ (ne_of_gt hi) ⟨h₁, h₂⟩ end private lemma C_Union_nat {s : ℕ → set α} (h : ∀i, C (s i)) (hd : pairwise (disjoint on s)) : C (⋃i, s i) := C_iff_le.2 $ λ t, begin have hp : m (t ∩ ⋃i, s i) ≤ (⨆n, m (t ∩ ⋃i<n, s i)), { convert m.Union (λ i, t ∩ s i), { rw inter_Union }, { simp [ennreal.tsum_eq_supr_nat, C_sum m h hd] } }, refine le_trans (add_le_add_right' hp) _, rw ennreal.supr_add, refine supr_le (λ n, le_trans (add_le_add_left' _) (ge_of_eq (C_Union_lt m (λ i _, h i) _))), refine m.mono (diff_subset_diff_right _), exact bUnion_subset (λ i _, subset_Union _ i), end private lemma f_Union {s : ℕ → set α} (h : ∀i, C (s i)) (hd : pairwise (disjoint on s)) : m (⋃i, s i) = ∑i, m (s i) := begin refine le_antisymm (m.Union_nat s) _, rw ennreal.tsum_eq_supr_nat, refine supr_le (λ n, _), have := @C_sum _ m _ h hd univ n, simp at this, simp [this], exact m.mono (bUnion_subset (λ i _, subset_Union _ i)), end private def caratheodory_dynkin : measurable_space.dynkin_system α := { has := C, has_empty := C_empty, has_compl := assume s, C_compl, has_Union_nat := assume f hf hn, C_Union_nat hn hf } /-- Given an outer measure `μ`, the Caratheodory measurable space is defined such that `s` is measurable if `∀t, μ t = μ (t ∩ s) + μ (t \ s)`. -/ protected def caratheodory : measurable_space α := caratheodory_dynkin.to_measurable_space $ assume s₁ s₂, C_inter lemma is_caratheodory {s : set α} : caratheodory.is_measurable s ↔ ∀t, m t = m (t ∩ s) + m (t \ s) := iff.rfl lemma is_caratheodory_le {s : set α} : caratheodory.is_measurable s ↔ ∀t, m (t ∩ s) + m (t \ s) ≤ m t := C_iff_le protected lemma Union_eq_of_caratheodory {s : ℕ → set α} (h : ∀i, caratheodory.is_measurable (s i)) (hd : pairwise (disjoint on s)) : m (⋃i, s i) = ∑i, m (s i) := f_Union h hd end caratheodory_measurable variables {α : Type*} lemma caratheodory_is_measurable {m : set α → ennreal} {s : set α} {h₀ : m ∅ = 0} (hs : ∀t, m (t ∩ s) + m (t \ s) ≤ m t) : (outer_measure.of_function m h₀).caratheodory.is_measurable s := let o := (outer_measure.of_function m h₀) in (is_caratheodory_le o).2 $ λ t, le_infi $ λ f, le_infi $ λ hf, begin refine le_trans (add_le_add' (infi_le_of_le (λi, f i ∩ s) $ infi_le _ _) (infi_le_of_le (λi, f i \ s) $ infi_le _ _)) _, { rw ← Union_inter, exact inter_subset_inter_left _ hf }, { rw ← Union_diff, exact diff_subset_diff_left hf }, { rw ← ennreal.tsum_add, exact ennreal.tsum_le_tsum (λ i, hs _) } end @[simp] theorem zero_caratheodory : (0 : outer_measure α).caratheodory = ⊤ := top_unique $ λ s _ t, (add_zero _).symm theorem top_caratheodory : (⊤ : outer_measure α).caratheodory = ⊤ := top_unique $ assume s hs, (is_caratheodory_le _).2 $ assume t, by by_cases ht : t = ∅; simp [ht, top_apply] theorem le_add_caratheodory (m₁ m₂ : outer_measure α) : m₁.caratheodory ⊓ m₂.caratheodory ≤ (m₁ + m₂ : outer_measure α).caratheodory := λ s ⟨hs₁, hs₂⟩ t, by simp [hs₁ t, hs₂ t] theorem le_sum_caratheodory {ι} (m : ι → outer_measure α) : (⨅ i, (m i).caratheodory) ≤ (sum m).caratheodory := λ s h t, by simp [λ i, measurable_space.is_measurable_infi.1 h i t, ennreal.tsum_add] theorem le_smul_caratheodory (a : ennreal) (m : outer_measure α) : m.caratheodory ≤ (a • m).caratheodory := λ s h t, by simp [h t, mul_add] @[simp] theorem dirac_caratheodory (a : α) : (dirac a).caratheodory = ⊤ := top_unique $ λ s _ t, begin by_cases a ∈ t; simp [h], by_cases a ∈ s; simp [h] end section Inf_gen def Inf_gen (m : set (outer_measure α)) (s : set α) : ennreal := ⨆(h : s ≠ ∅), ⨅ (μ : outer_measure α) (h : μ ∈ m), μ s @[simp] lemma Inf_gen_empty (m : set (outer_measure α)) : Inf_gen m ∅ = 0 := by simp [Inf_gen] lemma Inf_gen_nonempty1 (m : set (outer_measure α)) (t : set α) (h : t ≠ ∅) : Inf_gen m t = (⨅ (μ : outer_measure α) (h : μ ∈ m), μ t) := by rw [Inf_gen, supr_pos h] lemma Inf_gen_nonempty2 (m : set (outer_measure α)) (μ) (h : μ ∈ m) (t) : Inf_gen m t = (⨅ (μ : outer_measure α) (h : μ ∈ m), μ t) := begin by_cases ht : t = ∅, { simp [ht], refine (bot_unique $ infi_le_of_le μ $ _).symm, refine infi_le_of_le h (le_refl ⊥) }, { exact Inf_gen_nonempty1 m t ht } end lemma Inf_eq_of_function_Inf_gen (m : set (outer_measure α)) : Inf m = outer_measure.of_function (Inf_gen m) (Inf_gen_empty m) := begin refine le_antisymm (assume t', le_of_function.2 (assume t, _) _) (lattice.le_Inf $ assume μ hμ t, le_trans (outer_measure.of_function_le _ _ _) _); by_cases ht : t = ∅; simp [ht, Inf_gen_nonempty1], { assume μ hμ, exact (show Inf m ≤ μ, from lattice.Inf_le hμ) t }, { exact infi_le_of_le μ (infi_le _ hμ) } end end Inf_gen end outer_measure end measure_theory
5c311668ff3ac94c3d0e70df4c22fb52c9958d42
c5b07d17b3c9fb19e4b302465d237fd1d988c14f
/src/functors/polynomial.lean
90ecc988c308af3e5584138c281e98ad7fbd6024
[ "MIT" ]
permissive
skaslev/papers
acaec61602b28c33d6115e53913b2002136aa29b
f15b379f3c43bbd0a37ac7bb75f4278f7e901389
refs/heads/master
1,665,505,770,318
1,660,378,602,000
1,660,378,602,000
14,101,547
0
1
MIT
1,595,414,522,000
1,383,542,702,000
Lean
UTF-8
Lean
false
false
1,357
lean
import data.iso import functors.family -- Polynomial functor -- poly(c) = Σ i:c, x^c(i) def poly (c : fam Type*) (X : Type*) := Σ i : c, c i → X -- qpf(c,r) = Σ i:c, x^c(i) / r(i) def qpf (c : fam Type*) (r : Π i X, rel (c i → X)) (X : Type*) := Σ i : c, quot (r i X) -- poly(c) ↪ qpf(c, ordered) def poly.lift_qpf {c A} : poly c A → qpf c (λ _ _, eq) A := λ ⟨i, x⟩, ⟨i, quot.mk _ x⟩ def poly.fam_iso {c A} : poly c A ≃ fam.of (poly c) A := fam.of_iso def fam.poly_iso {A} : fam A ≃ poly fam.all_types A := iso.id_iso def qpf.fam_iso {c r A} : qpf c r A ≃ fam.of (qpf c r) A := fam.of_iso namespace poly -- Polynomial functor given by the fibers of `f` -- as in "Polynomial Functors and Polynomial Monads" -- https://arxiv.org/pdf/0906.4931.pdf def from_fiber {B A} (f : B → A) := poly ⟨A, fiber f⟩ def from_fiber' {A} (f : fam A) := poly ⟨A, fiber f⟩ def map {c X Y} (f : X → Y) (p : poly c X) : poly c Y := ⟨p.1, f ∘ p.2⟩ instance {c} : functor (poly c) := { map := @map c } instance {c} : is_lawful_functor (poly c) := { id_map := by intros; simp [functor.map, map], comp_map := by intros; simp [functor.map, map] } def has_poly {F : Type* → Type*} (c : fam Type*) (A) := F A ≃ poly c A def has_poly_fiber {F : Type* → Type*} {B C} (c : B → C) (A) := F A ≃ from_fiber c A end poly
216bfb5b411e94fc2de4d18bdea0ebf647e649c4
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Compiler/LCNF/Probing.lean
8817b5ba44da5bbe2d7e0b54a824de075a2ec684
[ "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
7,062
lean
/- Copyright (c) 2022 Henrik Böving. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Henrik Böving -/ import Lean.Compiler.LCNF.CompilerM import Lean.Compiler.LCNF.PassManager import Lean.Compiler.LCNF.PhaseExt import Lean.Compiler.LCNF.ForEachExpr namespace Lean.Compiler.LCNF abbrev Probe α β := Array α → CompilerM (Array β) namespace Probe @[inline] def map (f : α → CompilerM β) : Probe α β := fun data => data.mapM f @[inline] def filter (f : α → CompilerM Bool) : Probe α α := fun data => data.filterM f @[inline] def sorted [Inhabited α] [inst : LT α] [DecidableRel inst.lt] : Probe α α := fun data => return data.qsort (· < ·) @[inline] def sortedBySize : Probe Decl (Nat × Decl) := fun decls => let decls := decls.map fun decl => (decl.size, decl) return decls.qsort fun (sz₁, decl₁) (sz₂, decl₂) => if sz₁ == sz₂ then Name.lt decl₁.name decl₂.name else sz₁ < sz₂ def countUnique [ToString α] [BEq α] [Hashable α] : Probe α (α × Nat) := fun data => do let mut map := HashMap.empty for d in data do if let some count := map.find? d then map := map.insert d (count + 1) else map := map.insert d 1 return map.toArray @[inline] def countUniqueSorted [ToString α] [BEq α] [Hashable α] [Inhabited α] : Probe α (α × Nat) := countUnique >=> fun data => return data.qsort (fun l r => l.snd < r.snd) partial def getLetValues : Probe Decl LetValue := fun decls => do let (_, res) ← start decls |>.run #[] return res where go (c : Code) : StateRefT (Array LetValue) CompilerM Unit := do match c with | .let (decl : LetDecl) (k : Code) => modify fun s => s.push decl.value go k | .fun decl k | .jp decl k => go decl.value go k | .cases (cases : CasesCore Code) => cases.alts.forM (go ·.getCode) | .jmp .. | .return .. | .unreach .. => return () start (decls : Array Decl) : StateRefT (Array LetValue) CompilerM Unit := decls.forM (go ·.value) partial def getJps : Probe Decl FunDecl := fun decls => do let (_, res) ← start decls |>.run #[] return res where go (code : Code) : StateRefT (Array FunDecl) CompilerM Unit := do match code with | .let _ k => go k | .fun decl k => go decl.value; go k | .jp decl k => modify (·.push decl); go decl.value; go k | .cases cs => cs.alts.forM (go ·.getCode) | .jmp .. | .return .. | .unreach .. => return () start (decls : Array Decl) : StateRefT (Array FunDecl) CompilerM Unit := decls.forM fun decl => go decl.value partial def filterByLet (f : LetDecl → CompilerM Bool) : Probe Decl Decl := filter (fun decl => go decl.value) where go : Code → CompilerM Bool | .let decl k => do if (← f decl) then return true else go k | .fun decl k | .jp decl k => go decl.value <||> go k | .cases cs => cs.alts.anyM (go ·.getCode) | .jmp .. | .return .. | .unreach .. => return false partial def filterByFun (f : FunDecl → CompilerM Bool) : Probe Decl Decl := filter (fun decl => go decl.value) where go : Code → CompilerM Bool | .let _ k | .jp _ k => go k | .fun decl k => do if (← f decl) then return true else go decl.value <||> go k | .cases cs => cs.alts.anyM (go ·.getCode) | .jmp .. | .return .. | .unreach .. => return false partial def filterByJp (f : FunDecl → CompilerM Bool) : Probe Decl Decl := filter (fun decl => go decl.value) where go : Code → CompilerM Bool | .let _ k => go k | .fun decl k => go decl.value <||> go k | .jp decl k => do if (← f decl) then return true else go decl.value <||> go k | .cases cs => cs.alts.anyM (go ·.getCode) | .jmp .. | .return .. | .unreach .. => return false partial def filterByFunDecl (f : FunDecl → CompilerM Bool) : Probe Decl Decl := filter (fun decl => go decl.value) where go : Code → CompilerM Bool | .let _ k => go k | .fun decl k | .jp decl k => do if (← f decl) then return true else go decl.value <||> go k | .cases cs => cs.alts.anyM (go ·.getCode) | .jmp .. | .return .. | .unreach .. => return false partial def filterByCases (f : Cases → CompilerM Bool) : Probe Decl Decl := filter (fun decl => go decl.value) where go : Code → CompilerM Bool | .let _ k => go k | .fun decl k | .jp decl k => go decl.value <||> go k | .cases cs => do if (← f cs) then return true else cs.alts.anyM (go ·.getCode) | .jmp .. | .return .. | .unreach .. => return false partial def filterByJmp (f : FVarId → Array Arg → CompilerM Bool) : Probe Decl Decl := filter (fun decl => go decl.value) where go : Code → CompilerM Bool | .let _ k => go k | .fun decl k | .jp decl k => go decl.value <||> go k | .cases cs => cs.alts.anyM (go ·.getCode) | .jmp fn var => f fn var | .return .. | .unreach .. => return false partial def filterByReturn (f : FVarId → CompilerM Bool) : Probe Decl Decl := filter (fun decl => go decl.value) where go : Code → CompilerM Bool | .let _ k => go k | .fun decl k | .jp decl k => go decl.value <||> go k | .cases cs => cs.alts.anyM (go ·.getCode) | .jmp .. | .unreach .. => return false | .return var => f var partial def filterByUnreach (f : Expr → CompilerM Bool) : Probe Decl Decl := filter (fun decl => go decl.value) where go : Code → CompilerM Bool | .let _ k => go k | .fun decl k | .jp decl k => go decl.value <||> go k | .cases cs => cs.alts.anyM (go ·.getCode) | .jmp .. | .return .. => return false | .unreach typ => f typ @[inline] def declNames : Probe Decl Name := Probe.map (fun decl => return decl.name) @[inline] def toString [ToString α] : Probe α String := Probe.map (return ToString.toString ·) @[inline] def count : Probe α Nat := fun data => return #[data.size] @[inline] def sum : Probe Nat Nat := fun data => return #[data.foldl (init := 0) (·+·)] @[inline] def tail (n : Nat) : Probe α α := fun data => return data[data.size - n:] @[inline] def head (n : Nat) : Probe α α := fun data => return data[:n] def runOnModule (moduleName : Name) (probe : Probe Decl β) (phase : Phase := Phase.base): CoreM (Array β) := do let ext := getExt phase let env ← getEnv let some modIdx := env.getModuleIdx? moduleName | throwError "module `{moduleName}` not found" let decls := ext.getModuleEntries env modIdx probe decls |>.run (phase := phase) def runGlobally (probe : Probe Decl β) (phase : Phase := Phase.base) : CoreM (Array β) := do let ext := getExt phase let env ← getEnv let mut decls := #[] for modIdx in [:env.allImportedModuleNames.size] do decls := decls.append <| ext.getModuleEntries env modIdx probe decls |>.run (phase := phase) def toPass [ToString β] (probe : Probe Decl β) (phase : Phase) : Pass where phase := phase name := `probe run := fun decls => do let res ← probe decls trace[Compiler.probe] s!"{res}" return decls builtin_initialize registerTraceClass `Compiler.probe (inherited := true) end Probe end Lean.Compiler.LCNF
d9b09150b36c6105f6737d69b4006be8ef407861
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/hott/algebra/category/constructions/product.hlean
777488013d33c2acc049c3d91df28d42a0a76345
[ "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
5,458
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Jakob von Raumer Product precategory and (TODO) category -/ import ..category ..nat_trans hit.trunc open eq prod is_trunc functor sigma trunc iso prod.ops nat_trans namespace category definition precategory_prod [constructor] [instance] (obC obD : Type) [C : precategory obC] [D : precategory obD] : precategory (obC × obD) := precategory.mk' (λ a b, hom a.1 b.1 × hom a.2 b.2) (λ a b c g f, (g.1 ∘ f.1, g.2 ∘ f.2)) (λ a, (id, id)) (λ a b c d h g f, pair_eq !assoc !assoc ) (λ a b c d h g f, pair_eq !assoc' !assoc' ) (λ a b f, prod_eq !id_left !id_left ) (λ a b f, prod_eq !id_right !id_right) (λ a, prod_eq !id_id !id_id) _ definition Precategory_prod [reducible] [constructor] (C D : Precategory) : Precategory := precategory.Mk (precategory_prod C D) infixr ` ×c `:70 := Precategory_prod variables {C C' D D' X : Precategory} {u v : carrier (C ×c D)} theorem prod_hom_of_eq (p : u.1 = v.1) (q : u.2 = v.2) : hom_of_eq (prod_eq p q) = (hom_of_eq p, hom_of_eq q) := by induction u; induction v; esimp at *; induction p; induction q; reflexivity theorem prod_inv_of_eq (p : u.1 = v.1) (q : u.2 = v.2) : inv_of_eq (prod_eq p q) = (inv_of_eq p, inv_of_eq q) := by induction u; induction v; esimp at *; induction p; induction q; reflexivity theorem pr1_hom_of_eq (p : u.1 = v.1) (q : u.2 = v.2) : (hom_of_eq (prod_eq p q)).1 = hom_of_eq p := by exact ap pr1 !prod_hom_of_eq theorem pr1_inv_of_eq (p : u.1 = v.1) (q : u.2 = v.2) : (inv_of_eq (prod_eq p q)).1 = inv_of_eq p := by exact ap pr1 !prod_inv_of_eq theorem pr2_hom_of_eq (p : u.1 = v.1) (q : u.2 = v.2) : (hom_of_eq (prod_eq p q)).2 = hom_of_eq q := by exact ap pr2 !prod_hom_of_eq theorem pr2_inv_of_eq (p : u.1 = v.1) (q : u.2 = v.2) : (inv_of_eq (prod_eq p q)).2 = inv_of_eq q := by exact ap pr2 !prod_inv_of_eq definition pr1_functor [constructor] : C ×c D ⇒ C := functor.mk pr1 (λa b, pr1) (λa, idp) (λa b c g f, idp) definition pr2_functor [constructor] : C ×c D ⇒ D := functor.mk pr2 (λa b, pr2) (λa, idp) (λa b c g f, idp) definition functor_prod [constructor] [reducible] (F : X ⇒ C) (G : X ⇒ D) : X ⇒ C ×c D := functor.mk (λ a, pair (F a) (G a)) (λ a b f, pair (F f) (G f)) (λ a, abstract pair_eq !respect_id !respect_id end) (λ a b c g f, abstract pair_eq !respect_comp !respect_comp end) infixr ` ×f `:70 := functor_prod definition prod_functor_eta (F : X ⇒ C ×c D) : pr1_functor ∘f F ×f pr2_functor ∘f F = F := begin fapply functor_eq: esimp, { intro e, apply prod_eq: reflexivity}, { intro e e' f, apply prod_eq: esimp, { refine ap (λx, x ∘ _ ∘ _) !pr1_hom_of_eq ⬝ _, refine ap (λx, _ ∘ _ ∘ x) !pr1_inv_of_eq ⬝ _, esimp, apply id_leftright}, { refine ap (λx, x ∘ _ ∘ _) !pr2_hom_of_eq ⬝ _, refine ap (λx, _ ∘ _ ∘ x) !pr2_inv_of_eq ⬝ _, esimp, apply id_leftright}} end definition pr1_functor_prod (F : X ⇒ C) (G : X ⇒ D) : pr1_functor ∘f (F ×f G) = F := functor_eq (λx, idp) (λx y f, !id_leftright) definition pr2_functor_prod (F : X ⇒ C) (G : X ⇒ D) : pr2_functor ∘f (F ×f G) = G := functor_eq (λx, idp) (λx y f, !id_leftright) -- definition universal_property_prod {C D X : Precategory} (F : X ⇒ C) (G : X ⇒ D) -- : is_contr (Σ(H : X ⇒ C ×c D), pr1_functor ∘f H = F × pr2_functor ∘f H = G) := -- is_contr.mk -- ⟨functor_prod F G, (pr1_functor_prod F G, pr2_functor_prod F G)⟩ -- begin -- intro v, induction v with H w, induction w with p q, -- symmetry, fapply sigma_eq: esimp, -- { fapply functor_eq, -- { intro x, apply prod_eq: esimp, -- { exact ap010 to_fun_ob p x}, -- { exact ap010 to_fun_ob q x}}, -- { intro x y f, apply prod_eq: esimp, -- { exact sorry}, -- { exact sorry}}}, -- { exact sorry} -- end definition prod_functor_prod [constructor] (F : C ⇒ D) (G : C' ⇒ D') : C ×c C' ⇒ D ×c D' := (F ∘f pr1_functor) ×f (G ∘f pr2_functor) definition prod_nat_trans [constructor] {C D D' : Precategory} {F F' : C ⇒ D} {G G' : C ⇒ D'} (η : F ⟹ F') (θ : G ⟹ G') : F ×f G ⟹ F' ×f G' := begin fapply nat_trans.mk: esimp, { intro c, exact (η c, θ c)}, { intro c c' f, apply prod_eq: esimp:apply naturality} end infixr ` ×n `:70 := prod_nat_trans definition prod_flip_functor [constructor] (C D : Precategory) : C ×c D ⇒ D ×c C := functor.mk (λp, (p.2, p.1)) (λp p' h, (h.2, h.1)) (λp, idp) (λp p' p'' h' h, idp) definition functor_prod_flip_functor_prod_flip (C D : Precategory) : prod_flip_functor D C ∘f (prod_flip_functor C D) = functor.id := begin fapply functor_eq, { intro p, apply prod.eta}, { intro p p' h, cases p with c d, cases p' with c' d', apply id_leftright} end end category
8563b6e4756a2648b0d49069d2c77230c05b26bb
ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5
/src/Lean/Elab/Frontend.lean
aec916031dfb69a9d07db46103c2ca2020173d84
[ "Apache-2.0" ]
permissive
dupuisf/lean4
d082d13b01243e1de29ae680eefb476961221eef
6a39c65bd28eb0e28c3870188f348c8914502718
refs/heads/master
1,676,948,755,391
1,610,665,114,000
1,610,665,114,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,203
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ import Lean.Elab.Import import Lean.Elab.Command import Lean.Util.Profile namespace Lean.Elab.Frontend structure State where commandState : Command.State parserState : Parser.ModuleParserState cmdPos : String.Pos commands : Array Syntax := #[] structure Context where inputCtx : Parser.InputContext abbrev FrontendM := ReaderT Context $ StateRefT State IO def setCommandState (commandState : Command.State) : FrontendM Unit := modify fun s => { s with commandState := commandState } @[inline] def runCommandElabM (x : Command.CommandElabM Unit) : FrontendM Unit := do let ctx ← read let s ← get let cmdCtx : Command.Context := { cmdPos := s.cmdPos, fileName := ctx.inputCtx.fileName, fileMap := ctx.inputCtx.fileMap } let sNew? ← liftM $ EIO.toIO (fun _ => IO.Error.userError "unexpected error") (do let (_, s) ← (x cmdCtx).run s.commandState; pure $ some s) match sNew? with | some sNew => setCommandState sNew | none => pure () def elabCommandAtFrontend (stx : Syntax) : FrontendM Unit := do runCommandElabM (Command.elabCommand stx) def updateCmdPos : FrontendM Unit := do modify fun s => { s with cmdPos := s.parserState.pos } def getParserState : FrontendM Parser.ModuleParserState := do pure (← get).parserState def getCommandState : FrontendM Command.State := do pure (← get).commandState def setParserState (ps : Parser.ModuleParserState) : FrontendM Unit := modify fun s => { s with parserState := ps } def setMessages (msgs : MessageLog) : FrontendM Unit := modify fun s => { s with commandState := { s.commandState with messages := msgs } } def getInputContext : FrontendM Parser.InputContext := do pure (← read).inputCtx def processCommand : FrontendM Bool := do updateCmdPos let cmdState ← getCommandState let ictx ← getInputContext let pstate ← getParserState let scope := cmdState.scopes.head! let pmctx := { env := cmdState.env, options := scope.opts, currNamespace := scope.currNamespace, openDecls := scope.openDecls } let pos := ictx.fileMap.toPosition pstate.pos match profileit "parsing" scope.opts pos fun _ => Parser.parseCommand ictx pmctx pstate cmdState.messages with | (cmd, ps, messages) => modify fun s => { s with commands := s.commands.push cmd } setParserState ps setMessages messages if Parser.isEOI cmd || Parser.isExitCommand cmd then pure true -- Done else profileitM IO.Error "elaboration" scope.opts pos $ elabCommandAtFrontend cmd pure false partial def processCommands : FrontendM Unit := do let done ← processCommand unless done do processCommands end Frontend open Frontend def IO.processCommands (inputCtx : Parser.InputContext) (parserState : Parser.ModuleParserState) (commandState : Command.State) : IO State := do let (_, s) ← (Frontend.processCommands.run { inputCtx := inputCtx }).run { commandState := commandState, parserState := parserState, cmdPos := parserState.pos } pure s def process (input : String) (env : Environment) (opts : Options) (fileName : Option String := none) : IO (Environment × MessageLog) := do let fileName := fileName.getD "<input>" let inputCtx := Parser.mkInputContext input fileName let s ← IO.processCommands inputCtx { : Parser.ModuleParserState } (Command.mkState env {} opts) pure (s.commandState.env, s.commandState.messages) @[export lean_run_frontend] def runFrontend (input : String) (opts : Options) (fileName : String) (mainModuleName : Name) : IO (Environment × Bool) := do let inputCtx := Parser.mkInputContext input fileName let (header, parserState, messages) ← Parser.parseHeader inputCtx let (env, messages) ← processHeader header opts messages inputCtx let env := env.setMainModule mainModuleName let s ← IO.processCommands inputCtx parserState (Command.mkState env messages opts) for msg in s.commandState.messages.toList do IO.print (← msg.toString) pure (s.commandState.env, !s.commandState.messages.hasErrors) end Lean.Elab
f8b718d4d30036746d507e433828e8b072abb240
bb31430994044506fa42fd667e2d556327e18dfe
/src/number_theory/arithmetic_function.lean
2d05ecfe97d50c408143c3774e02d984f109f126
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
35,812
lean
/- Copyright (c) 2020 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson -/ import algebra.big_operators.ring import algebra.module.big_operators import number_theory.divisors import data.nat.squarefree import data.nat.gcd.big_operators import algebra.invertible import data.nat.factorization.basic /-! # Arithmetic Functions and Dirichlet Convolution This file defines arithmetic functions, which are functions from `ℕ` to a specified type that map 0 to 0. In the literature, they are often instead defined as functions from `ℕ+`. These arithmetic functions are endowed with a multiplication, given by Dirichlet convolution, and pointwise addition, to form the Dirichlet ring. ## Main Definitions * `arithmetic_function R` consists of functions `f : ℕ → R` such that `f 0 = 0`. * An arithmetic function `f` `is_multiplicative` when `x.coprime y → f (x * y) = f x * f y`. * The pointwise operations `pmul` and `ppow` differ from the multiplication and power instances on `arithmetic_function R`, which use Dirichlet multiplication. * `ζ` is the arithmetic function such that `ζ x = 1` for `0 < x`. * `σ k` is the arithmetic function such that `σ k x = ∑ y in divisors x, y ^ k` for `0 < x`. * `pow k` is the arithmetic function such that `pow k x = x ^ k` for `0 < x`. * `id` is the identity arithmetic function on `ℕ`. * `ω n` is the number of distinct prime factors of `n`. * `Ω n` is the number of prime factors of `n` counted with multiplicity. * `μ` is the Möbius function (spelled `moebius` in code). ## Main Results * Several forms of Möbius inversion: * `sum_eq_iff_sum_mul_moebius_eq` for functions to a `comm_ring` * `sum_eq_iff_sum_smul_moebius_eq` for functions to an `add_comm_group` * `prod_eq_iff_prod_pow_moebius_eq` for functions to a `comm_group` * `prod_eq_iff_prod_pow_moebius_eq_of_nonzero` for functions to a `comm_group_with_zero` ## Notation The arithmetic functions `ζ` and `σ` have Greek letter names, which are localized notation in the namespace `arithmetic_function`. ## Tags arithmetic functions, dirichlet convolution, divisors -/ open finset open_locale big_operators namespace nat variable (R : Type*) /-- An arithmetic function is a function from `ℕ` that maps 0 to 0. In the literature, they are often instead defined as functions from `ℕ+`. Multiplication on `arithmetic_functions` is by Dirichlet convolution. -/ @[derive [has_zero, inhabited]] def arithmetic_function [has_zero R] := zero_hom ℕ R variable {R} namespace arithmetic_function section has_zero variable [has_zero R] instance : has_coe_to_fun (arithmetic_function R) (λ _, ℕ → R) := zero_hom.has_coe_to_fun @[simp] lemma to_fun_eq (f : arithmetic_function R) : f.to_fun = f := rfl @[simp] lemma map_zero {f : arithmetic_function R} : f 0 = 0 := zero_hom.map_zero' f theorem coe_inj {f g : arithmetic_function R} : (f : ℕ → R) = g ↔ f = g := ⟨λ h, zero_hom.coe_inj h, λ h, h ▸ rfl⟩ @[simp] lemma zero_apply {x : ℕ} : (0 : arithmetic_function R) x = 0 := zero_hom.zero_apply x @[ext] theorem ext ⦃f g : arithmetic_function R⦄ (h : ∀ x, f x = g x) : f = g := zero_hom.ext h theorem ext_iff {f g : arithmetic_function R} : f = g ↔ ∀ x, f x = g x := zero_hom.ext_iff section has_one variable [has_one R] instance : has_one (arithmetic_function R) := ⟨⟨λ x, ite (x = 1) 1 0, rfl⟩⟩ lemma one_apply {x : ℕ} : (1 : arithmetic_function R) x = ite (x = 1) 1 0 := rfl @[simp] lemma one_one : (1 : arithmetic_function R) 1 = 1 := rfl @[simp] lemma one_apply_ne {x : ℕ} (h : x ≠ 1) : (1 : arithmetic_function R) x = 0 := if_neg h end has_one end has_zero instance nat_coe [add_monoid_with_one R] : has_coe (arithmetic_function ℕ) (arithmetic_function R) := ⟨λ f, ⟨↑(f : ℕ → ℕ), by { transitivity ↑(f 0), refl, simp }⟩⟩ @[simp] lemma nat_coe_nat (f : arithmetic_function ℕ) : (↑f : arithmetic_function ℕ) = f := ext $ λ _, cast_id _ @[simp] lemma nat_coe_apply [add_monoid_with_one R] {f : arithmetic_function ℕ} {x : ℕ} : (f : arithmetic_function R) x = f x := rfl instance int_coe [add_group_with_one R] : has_coe (arithmetic_function ℤ) (arithmetic_function R) := ⟨λ f, ⟨↑(f : ℕ → ℤ), by { transitivity ↑(f 0), refl, simp }⟩⟩ @[simp] lemma int_coe_int (f : arithmetic_function ℤ) : (↑f : arithmetic_function ℤ) = f := ext $ λ _, int.cast_id _ @[simp] lemma int_coe_apply [add_group_with_one R] {f : arithmetic_function ℤ} {x : ℕ} : (f : arithmetic_function R) x = f x := rfl @[simp] lemma coe_coe [add_group_with_one R] {f : arithmetic_function ℕ} : ((f : arithmetic_function ℤ) : arithmetic_function R) = f := by { ext, simp, } @[simp] lemma nat_coe_one [add_monoid_with_one R] : ((1 : arithmetic_function ℕ) : arithmetic_function R) = 1 := by { ext n, simp [one_apply] } @[simp] lemma int_coe_one [add_group_with_one R] : ((1 : arithmetic_function ℤ) : arithmetic_function R) = 1 := by { ext n, simp [one_apply] } section add_monoid variable [add_monoid R] instance : has_add (arithmetic_function R) := ⟨λ f g, ⟨λ n, f n + g n, by simp⟩⟩ @[simp] lemma add_apply {f g : arithmetic_function R} {n : ℕ} : (f + g) n = f n + g n := rfl instance : add_monoid (arithmetic_function R) := { add_assoc := λ _ _ _, ext (λ _, add_assoc _ _ _), zero_add := λ _, ext (λ _, zero_add _), add_zero := λ _, ext (λ _, add_zero _), .. arithmetic_function.has_zero R, .. arithmetic_function.has_add } end add_monoid instance [add_monoid_with_one R] : add_monoid_with_one (arithmetic_function R) := { nat_cast := λ n, ⟨λ x, if x = 1 then (n : R) else 0, by simp⟩, nat_cast_zero := by ext; simp [nat.cast], nat_cast_succ := λ _, by ext; by_cases x = 1; simp [nat.cast, *], .. arithmetic_function.add_monoid, .. arithmetic_function.has_one } instance [add_comm_monoid R] : add_comm_monoid (arithmetic_function R) := { add_comm := λ _ _, ext (λ _, add_comm _ _), .. arithmetic_function.add_monoid } instance [add_group R] : add_group (arithmetic_function R) := { neg := λ f, ⟨λ n, - f n, by simp⟩, add_left_neg := λ _, ext (λ _, add_left_neg _), .. arithmetic_function.add_monoid } instance [add_comm_group R] : add_comm_group (arithmetic_function R) := { .. arithmetic_function.add_comm_monoid, .. arithmetic_function.add_group } section has_smul variables {M : Type*} [has_zero R] [add_comm_monoid M] [has_smul R M] /-- The Dirichlet convolution of two arithmetic functions `f` and `g` is another arithmetic function such that `(f * g) n` is the sum of `f x * g y` over all `(x,y)` such that `x * y = n`. -/ instance : has_smul (arithmetic_function R) (arithmetic_function M) := ⟨λ f g, ⟨λ n, ∑ x in divisors_antidiagonal n, f x.fst • g x.snd, by simp⟩⟩ @[simp] lemma smul_apply {f : arithmetic_function R} {g : arithmetic_function M} {n : ℕ} : (f • g) n = ∑ x in divisors_antidiagonal n, f x.fst • g x.snd := rfl end has_smul /-- The Dirichlet convolution of two arithmetic functions `f` and `g` is another arithmetic function such that `(f * g) n` is the sum of `f x * g y` over all `(x,y)` such that `x * y = n`. -/ instance [semiring R] : has_mul (arithmetic_function R) := ⟨(•)⟩ @[simp] lemma mul_apply [semiring R] {f g : arithmetic_function R} {n : ℕ} : (f * g) n = ∑ x in divisors_antidiagonal n, f x.fst * g x.snd := rfl lemma mul_apply_one [semiring R] {f g : arithmetic_function R} : (f * g) 1 = f 1 * g 1 := by simp @[simp, norm_cast] lemma nat_coe_mul [semiring R] {f g : arithmetic_function ℕ} : (↑(f * g) : arithmetic_function R) = f * g := by { ext n, simp } @[simp, norm_cast] lemma int_coe_mul [ring R] {f g : arithmetic_function ℤ} : (↑(f * g) : arithmetic_function R) = f * g := by { ext n, simp } section module variables {M : Type*} [semiring R] [add_comm_monoid M] [module R M] lemma mul_smul' (f g : arithmetic_function R) (h : arithmetic_function M) : (f * g) • h = f • g • h := begin ext n, simp only [mul_apply, smul_apply, sum_smul, mul_smul, smul_sum, finset.sum_sigma'], apply finset.sum_bij, swap 5, { rintros ⟨⟨i,j⟩, ⟨k,l⟩⟩ H, exact ⟨(k, l*j), (l, j)⟩ }, { rintros ⟨⟨i,j⟩, ⟨k,l⟩⟩ H, simp only [finset.mem_sigma, mem_divisors_antidiagonal] at H ⊢, rcases H with ⟨⟨rfl, n0⟩, rfl, i0⟩, refine ⟨⟨(mul_assoc _ _ _).symm, n0⟩, rfl, _⟩, rw mul_ne_zero_iff at *, exact ⟨i0.2, n0.2⟩, }, { rintros ⟨⟨i,j⟩, ⟨k,l⟩⟩ H, simp only [mul_assoc] }, { rintros ⟨⟨a,b⟩, ⟨c,d⟩⟩ ⟨⟨i,j⟩, ⟨k,l⟩⟩ H₁ H₂, simp only [finset.mem_sigma, mem_divisors_antidiagonal, and_imp, prod.mk.inj_iff, add_comm, heq_iff_eq] at H₁ H₂ ⊢, rintros rfl h2 rfl rfl, exact ⟨⟨eq.trans H₁.2.1.symm H₂.2.1, rfl⟩, rfl, rfl⟩ }, { rintros ⟨⟨i,j⟩, ⟨k,l⟩⟩ H, refine ⟨⟨(i*k, l), (i, k)⟩, _, _⟩, { simp only [finset.mem_sigma, mem_divisors_antidiagonal] at H ⊢, rcases H with ⟨⟨rfl, n0⟩, rfl, j0⟩, refine ⟨⟨mul_assoc _ _ _, n0⟩, rfl, _⟩, rw mul_ne_zero_iff at *, exact ⟨n0.1, j0.1⟩ }, { simp only [true_and, mem_divisors_antidiagonal, and_true, prod.mk.inj_iff, eq_self_iff_true, ne.def, mem_sigma, heq_iff_eq] at H ⊢, rw H.2.1 } } end lemma one_smul' (b : arithmetic_function M) : (1 : arithmetic_function R) • b = b := begin ext, rw smul_apply, by_cases x0 : x = 0, {simp [x0]}, have h : {(1,x)} ⊆ divisors_antidiagonal x := by simp [x0], rw ← sum_subset h, {simp}, intros y ymem ynmem, have y1ne : y.fst ≠ 1, { intro con, simp only [con, mem_divisors_antidiagonal, one_mul, ne.def] at ymem, simp only [mem_singleton, prod.ext_iff] at ynmem, tauto }, simp [y1ne], end end module section semiring variable [semiring R] instance : monoid (arithmetic_function R) := { one_mul := one_smul', mul_one := λ f, begin ext, rw mul_apply, by_cases x0 : x = 0, {simp [x0]}, have h : {(x,1)} ⊆ divisors_antidiagonal x := by simp [x0], rw ← sum_subset h, {simp}, intros y ymem ynmem, have y2ne : y.snd ≠ 1, { intro con, simp only [con, mem_divisors_antidiagonal, mul_one, ne.def] at ymem, simp only [mem_singleton, prod.ext_iff] at ynmem, tauto }, simp [y2ne], end, mul_assoc := mul_smul', .. arithmetic_function.has_one, .. arithmetic_function.has_mul } instance : semiring (arithmetic_function R) := { zero_mul := λ f, by { ext, simp only [mul_apply, zero_mul, sum_const_zero, zero_apply] }, mul_zero := λ f, by { ext, simp only [mul_apply, sum_const_zero, mul_zero, zero_apply] }, left_distrib := λ a b c, by { ext, simp only [←sum_add_distrib, mul_add, mul_apply, add_apply] }, right_distrib := λ a b c, by { ext, simp only [←sum_add_distrib, add_mul, mul_apply, add_apply] }, .. arithmetic_function.has_zero R, .. arithmetic_function.has_mul, .. arithmetic_function.has_add, .. arithmetic_function.add_comm_monoid, .. arithmetic_function.add_monoid_with_one, .. arithmetic_function.monoid } end semiring instance [comm_semiring R] : comm_semiring (arithmetic_function R) := { mul_comm := λ f g, by { ext, rw [mul_apply, ← map_swap_divisors_antidiagonal, sum_map], simp [mul_comm] }, .. arithmetic_function.semiring } instance [comm_ring R] : comm_ring (arithmetic_function R) := { .. arithmetic_function.add_comm_group, .. arithmetic_function.comm_semiring } instance {M : Type*} [semiring R] [add_comm_monoid M] [module R M] : module (arithmetic_function R) (arithmetic_function M) := { one_smul := one_smul', mul_smul := mul_smul', smul_add := λ r x y, by { ext, simp only [sum_add_distrib, smul_add, smul_apply, add_apply] }, smul_zero := λ r, by { ext, simp only [smul_apply, sum_const_zero, smul_zero, zero_apply] }, add_smul := λ r s x, by { ext, simp only [add_smul, sum_add_distrib, smul_apply, add_apply] }, zero_smul := λ r, by { ext, simp only [smul_apply, sum_const_zero, zero_smul, zero_apply] }, } section zeta /-- `ζ 0 = 0`, otherwise `ζ x = 1`. The Dirichlet Series is the Riemann ζ. -/ def zeta : arithmetic_function ℕ := ⟨λ x, ite (x = 0) 0 1, rfl⟩ localized "notation (name := arithmetic_function.zeta) `ζ` := nat.arithmetic_function.zeta" in arithmetic_function @[simp] lemma zeta_apply {x : ℕ} : ζ x = if (x = 0) then 0 else 1 := rfl lemma zeta_apply_ne {x : ℕ} (h : x ≠ 0) : ζ x = 1 := if_neg h @[simp] theorem coe_zeta_smul_apply {M} [semiring R] [add_comm_monoid M] [module R M] {f : arithmetic_function M} {x : ℕ} : ((↑ζ : arithmetic_function R) • f) x = ∑ i in divisors x, f i := begin rw smul_apply, transitivity ∑ i in divisors_antidiagonal x, f i.snd, { refine sum_congr rfl (λ i hi, _), rcases mem_divisors_antidiagonal.1 hi with ⟨rfl, h⟩, rw [nat_coe_apply, zeta_apply_ne (left_ne_zero_of_mul h), cast_one, one_smul] }, { rw [← map_div_left_divisors, sum_map, function.embedding.coe_fn_mk] } end @[simp] theorem coe_zeta_mul_apply [semiring R] {f : arithmetic_function R} {x : ℕ} : (↑ζ * f) x = ∑ i in divisors x, f i := coe_zeta_smul_apply @[simp] theorem coe_mul_zeta_apply [semiring R] {f : arithmetic_function R} {x : ℕ} : (f * ζ) x = ∑ i in divisors x, f i := begin rw mul_apply, transitivity ∑ i in divisors_antidiagonal x, f i.1, { refine sum_congr rfl (λ i hi, _), rcases mem_divisors_antidiagonal.1 hi with ⟨rfl, h⟩, rw [nat_coe_apply, zeta_apply_ne (right_ne_zero_of_mul h), cast_one, mul_one] }, { rw [← map_div_right_divisors, sum_map, function.embedding.coe_fn_mk] } end theorem zeta_mul_apply {f : arithmetic_function ℕ} {x : ℕ} : (ζ * f) x = ∑ i in divisors x, f i := by rw [← nat_coe_nat ζ, coe_zeta_mul_apply] theorem mul_zeta_apply {f : arithmetic_function ℕ} {x : ℕ} : (f * ζ) x = ∑ i in divisors x, f i := by rw [← nat_coe_nat ζ, coe_mul_zeta_apply] end zeta open_locale arithmetic_function section pmul /-- This is the pointwise product of `arithmetic_function`s. -/ def pmul [mul_zero_class R] (f g : arithmetic_function R) : arithmetic_function R := ⟨λ x, f x * g x, by simp⟩ @[simp] lemma pmul_apply [mul_zero_class R] {f g : arithmetic_function R} {x : ℕ} : f.pmul g x = f x * g x := rfl lemma pmul_comm [comm_monoid_with_zero R] (f g : arithmetic_function R) : f.pmul g = g.pmul f := by { ext, simp [mul_comm] } section non_assoc_semiring variable [non_assoc_semiring R] @[simp] lemma pmul_zeta (f : arithmetic_function R) : f.pmul ↑ζ = f := begin ext x, cases x; simp [nat.succ_ne_zero], end @[simp] lemma zeta_pmul (f : arithmetic_function R) : (ζ : arithmetic_function R).pmul f = f := begin ext x, cases x; simp [nat.succ_ne_zero], end end non_assoc_semiring variables [semiring R] /-- This is the pointwise power of `arithmetic_function`s. -/ def ppow (f : arithmetic_function R) (k : ℕ) : arithmetic_function R := if h0 : k = 0 then ζ else ⟨λ x, (f x) ^ k, by { rw [map_zero], exact zero_pow (nat.pos_of_ne_zero h0) }⟩ @[simp] lemma ppow_zero {f : arithmetic_function R} : f.ppow 0 = ζ := by rw [ppow, dif_pos rfl] @[simp] lemma ppow_apply {f : arithmetic_function R} {k x : ℕ} (kpos : 0 < k) : f.ppow k x = (f x) ^ k := by { rw [ppow, dif_neg (ne_of_gt kpos)], refl } lemma ppow_succ {f : arithmetic_function R} {k : ℕ} : f.ppow (k + 1) = f.pmul (f.ppow k) := begin ext x, rw [ppow_apply (nat.succ_pos k), pow_succ], induction k; simp, end lemma ppow_succ' {f : arithmetic_function R} {k : ℕ} {kpos : 0 < k} : f.ppow (k + 1) = (f.ppow k).pmul f := begin ext x, rw [ppow_apply (nat.succ_pos k), pow_succ'], induction k; simp, end end pmul /-- Multiplicative functions -/ def is_multiplicative [monoid_with_zero R] (f : arithmetic_function R) : Prop := f 1 = 1 ∧ (∀ {m n : ℕ}, m.coprime n → f (m * n) = f m * f n) namespace is_multiplicative section monoid_with_zero variable [monoid_with_zero R] @[simp] lemma map_one {f : arithmetic_function R} (h : f.is_multiplicative) : f 1 = 1 := h.1 @[simp] lemma map_mul_of_coprime {f : arithmetic_function R} (hf : f.is_multiplicative) {m n : ℕ} (h : m.coprime n) : f (m * n) = f m * f n := hf.2 h end monoid_with_zero lemma map_prod {ι : Type*} [comm_monoid_with_zero R] (g : ι → ℕ) {f : nat.arithmetic_function R} (hf : f.is_multiplicative) (s : finset ι) (hs : (s : set ι).pairwise (coprime on g)): f (∏ i in s, g i) = ∏ i in s, f (g i) := begin classical, induction s using finset.induction_on with a s has ih hs, { simp [hf] }, rw [coe_insert, set.pairwise_insert_of_symmetric (coprime.symmetric.comap g)] at hs, rw [prod_insert has, prod_insert has, hf.map_mul_of_coprime, ih hs.1], exact nat.coprime_prod_right (λ i hi, hs.2 _ hi (hi.ne_of_not_mem has).symm), end lemma nat_cast {f : arithmetic_function ℕ} [semiring R] (h : f.is_multiplicative) : is_multiplicative (f : arithmetic_function R) := ⟨by simp [h], λ m n cop, by simp [cop, h]⟩ lemma int_cast {f : arithmetic_function ℤ} [ring R] (h : f.is_multiplicative) : is_multiplicative (f : arithmetic_function R) := ⟨by simp [h], λ m n cop, by simp [cop, h]⟩ lemma mul [comm_semiring R] {f g : arithmetic_function R} (hf : f.is_multiplicative) (hg : g.is_multiplicative) : is_multiplicative (f * g) := ⟨by { simp [hf, hg], }, begin simp only [mul_apply], intros m n cop, rw sum_mul_sum, symmetry, apply sum_bij (λ (x : (ℕ × ℕ) × ℕ × ℕ) h, (x.1.1 * x.2.1, x.1.2 * x.2.2)), { rintros ⟨⟨a1, a2⟩, ⟨b1, b2⟩⟩ h, simp only [mem_divisors_antidiagonal, ne.def, mem_product] at h, rcases h with ⟨⟨rfl, ha⟩, ⟨rfl, hb⟩⟩, simp only [mem_divisors_antidiagonal, nat.mul_eq_zero, ne.def], split, {ring}, rw nat.mul_eq_zero at *, apply not_or ha hb }, { rintros ⟨⟨a1, a2⟩, ⟨b1, b2⟩⟩ h, simp only [mem_divisors_antidiagonal, ne.def, mem_product] at h, rcases h with ⟨⟨rfl, ha⟩, ⟨rfl, hb⟩⟩, dsimp only, rw [hf.map_mul_of_coprime cop.coprime_mul_right.coprime_mul_right_right, hg.map_mul_of_coprime cop.coprime_mul_left.coprime_mul_left_right], ring, }, { rintros ⟨⟨a1, a2⟩, ⟨b1, b2⟩⟩ ⟨⟨c1, c2⟩, ⟨d1, d2⟩⟩ hab hcd h, simp only [mem_divisors_antidiagonal, ne.def, mem_product] at hab, rcases hab with ⟨⟨rfl, ha⟩, ⟨rfl, hb⟩⟩, simp only [mem_divisors_antidiagonal, ne.def, mem_product] at hcd, simp only [prod.mk.inj_iff] at h, ext; dsimp only, { transitivity nat.gcd (a1 * a2) (a1 * b1), { rw [nat.gcd_mul_left, cop.coprime_mul_left.coprime_mul_right_right.gcd_eq_one, mul_one] }, { rw [← hcd.1.1, ← hcd.2.1] at cop, rw [← hcd.1.1, h.1, nat.gcd_mul_left, cop.coprime_mul_left.coprime_mul_right_right.gcd_eq_one, mul_one] } }, { transitivity nat.gcd (a1 * a2) (a2 * b2), { rw [mul_comm, nat.gcd_mul_left, cop.coprime_mul_right.coprime_mul_left_right.gcd_eq_one, mul_one] }, { rw [← hcd.1.1, ← hcd.2.1] at cop, rw [← hcd.1.1, h.2, mul_comm, nat.gcd_mul_left, cop.coprime_mul_right.coprime_mul_left_right.gcd_eq_one, mul_one] } }, { transitivity nat.gcd (b1 * b2) (a1 * b1), { rw [mul_comm, nat.gcd_mul_right, cop.coprime_mul_right.coprime_mul_left_right.symm.gcd_eq_one, one_mul] }, { rw [← hcd.1.1, ← hcd.2.1] at cop, rw [← hcd.2.1, h.1, mul_comm c1 d1, nat.gcd_mul_left, cop.coprime_mul_right.coprime_mul_left_right.symm.gcd_eq_one, mul_one] } }, { transitivity nat.gcd (b1 * b2) (a2 * b2), { rw [nat.gcd_mul_right, cop.coprime_mul_left.coprime_mul_right_right.symm.gcd_eq_one, one_mul] }, { rw [← hcd.1.1, ← hcd.2.1] at cop, rw [← hcd.2.1, h.2, nat.gcd_mul_right, cop.coprime_mul_left.coprime_mul_right_right.symm.gcd_eq_one, one_mul] } } }, { rintros ⟨b1, b2⟩ h, simp only [mem_divisors_antidiagonal, ne.def, mem_product] at h, use ((b1.gcd m, b2.gcd m), (b1.gcd n, b2.gcd n)), simp only [exists_prop, prod.mk.inj_iff, ne.def, mem_product, mem_divisors_antidiagonal], rw [← cop.gcd_mul _, ← cop.gcd_mul _, ← h.1, nat.gcd_mul_gcd_of_coprime_of_mul_eq_mul cop h.1, nat.gcd_mul_gcd_of_coprime_of_mul_eq_mul cop.symm _], { rw [nat.mul_eq_zero, decidable.not_or_iff_and_not] at h, simp [h.2.1, h.2.2] }, rw [mul_comm n m, h.1] } end⟩ lemma pmul [comm_semiring R] {f g : arithmetic_function R} (hf : f.is_multiplicative) (hg : g.is_multiplicative) : is_multiplicative (f.pmul g) := ⟨by { simp [hf, hg], }, λ m n cop, begin simp only [pmul_apply, hf.map_mul_of_coprime cop, hg.map_mul_of_coprime cop], ring, end⟩ /-- For any multiplicative function `f` and any `n > 0`, we can evaluate `f n` by evaluating `f` at `p ^ k` over the factorization of `n` -/ lemma multiplicative_factorization [comm_monoid_with_zero R] (f : arithmetic_function R) (hf : f.is_multiplicative) {n : ℕ} (hn : n ≠ 0) : f n = n.factorization.prod (λ p k, f (p ^ k)) := multiplicative_factorization f (λ _ _, hf.2) hf.1 hn /-- A recapitulation of the definition of multiplicative that is simpler for proofs -/ lemma iff_ne_zero [monoid_with_zero R] {f : arithmetic_function R} : is_multiplicative f ↔ f 1 = 1 ∧ (∀ {m n : ℕ}, m ≠ 0 → n ≠ 0 → m.coprime n → f (m * n) = f m * f n) := begin refine and_congr_right' (forall₂_congr (λ m n, ⟨λ h _ _, h, λ h hmn, _⟩)), rcases eq_or_ne m 0 with rfl | hm, { simp }, rcases eq_or_ne n 0 with rfl | hn, { simp }, exact h hm hn hmn, end /-- Two multiplicative functions `f` and `g` are equal if and only if they agree on prime powers -/ lemma eq_iff_eq_on_prime_powers [comm_monoid_with_zero R] (f : arithmetic_function R) (hf : f.is_multiplicative) (g : arithmetic_function R) (hg : g.is_multiplicative) : f = g ↔ ∀ (p i : ℕ), nat.prime p → f (p ^ i) = g (p ^ i) := begin split, { intros h p i _, rw [h] }, intros h, ext n, by_cases hn : n = 0, { rw [hn, arithmetic_function.map_zero, arithmetic_function.map_zero] }, rw [multiplicative_factorization f hf hn, multiplicative_factorization g hg hn], refine finset.prod_congr rfl _, simp only [support_factorization, list.mem_to_finset], intros p hp, exact h p _ (nat.prime_of_mem_factors hp), end end is_multiplicative section special_functions /-- The identity on `ℕ` as an `arithmetic_function`. -/ def id : arithmetic_function ℕ := ⟨id, rfl⟩ @[simp] lemma id_apply {x : ℕ} : id x = x := rfl /-- `pow k n = n ^ k`, except `pow 0 0 = 0`. -/ def pow (k : ℕ) : arithmetic_function ℕ := id.ppow k @[simp] lemma pow_apply {k n : ℕ} : pow k n = if (k = 0 ∧ n = 0) then 0 else n ^ k := begin cases k, { simp [pow] }, simp [pow, (ne_of_lt (nat.succ_pos k)).symm], end lemma pow_zero_eq_zeta : pow 0 = ζ := by { ext n, simp } /-- `σ k n` is the sum of the `k`th powers of the divisors of `n` -/ def sigma (k : ℕ) : arithmetic_function ℕ := ⟨λ n, ∑ d in divisors n, d ^ k, by simp⟩ localized "notation (name := arithmetic_function.sigma) `σ` := nat.arithmetic_function.sigma" in arithmetic_function lemma sigma_apply {k n : ℕ} : σ k n = ∑ d in divisors n, d ^ k := rfl lemma sigma_one_apply (n : ℕ) : σ 1 n = ∑ d in divisors n, d := by simp [sigma_apply] lemma sigma_zero_apply (n : ℕ) : σ 0 n = (divisors n).card := by simp [sigma_apply] lemma sigma_zero_apply_prime_pow {p i : ℕ} (hp : p.prime) : σ 0 (p ^ i) = i + 1 := by rw [sigma_zero_apply, divisors_prime_pow hp, card_map, card_range] lemma zeta_mul_pow_eq_sigma {k : ℕ} : ζ * pow k = σ k := begin ext, rw [sigma, zeta_mul_apply], apply sum_congr rfl, intros x hx, rw [pow_apply, if_neg (not_and_of_not_right _ _)], contrapose! hx, simp [hx], end lemma is_multiplicative_one [monoid_with_zero R] : is_multiplicative (1 : arithmetic_function R) := is_multiplicative.iff_ne_zero.2 ⟨by simp, begin intros m n hm hn hmn, rcases eq_or_ne m 1 with rfl | hm', { simp }, rw [one_apply_ne, one_apply_ne hm', zero_mul], rw [ne.def, nat.mul_eq_one_iff, not_and_distrib], exact or.inl hm' end⟩ lemma is_multiplicative_zeta : is_multiplicative ζ := is_multiplicative.iff_ne_zero.2 ⟨by simp, by simp {contextual := tt}⟩ lemma is_multiplicative_id : is_multiplicative arithmetic_function.id := ⟨rfl, λ _ _ _, rfl⟩ lemma is_multiplicative.ppow [comm_semiring R] {f : arithmetic_function R} (hf : f.is_multiplicative) {k : ℕ} : is_multiplicative (f.ppow k) := begin induction k with k hi, { exact is_multiplicative_zeta.nat_cast }, { rw ppow_succ, apply hf.pmul hi }, end lemma is_multiplicative_pow {k : ℕ} : is_multiplicative (pow k) := is_multiplicative_id.ppow lemma is_multiplicative_sigma {k : ℕ} : is_multiplicative (σ k) := begin rw [← zeta_mul_pow_eq_sigma], apply ((is_multiplicative_zeta).mul is_multiplicative_pow) end /-- `Ω n` is the number of prime factors of `n`. -/ def card_factors : arithmetic_function ℕ := ⟨λ n, n.factors.length, by simp⟩ localized "notation (name := card_factors) `Ω` := nat.arithmetic_function.card_factors" in arithmetic_function lemma card_factors_apply {n : ℕ} : Ω n = n.factors.length := rfl @[simp] lemma card_factors_one : Ω 1 = 0 := by simp [card_factors] lemma card_factors_eq_one_iff_prime {n : ℕ} : Ω n = 1 ↔ n.prime := begin refine ⟨λ h, _, λ h, list.length_eq_one.2 ⟨n, factors_prime h⟩⟩, cases n, { contrapose! h, simp }, rcases list.length_eq_one.1 h with ⟨x, hx⟩, rw [← prod_factors n.succ_ne_zero, hx, list.prod_singleton], apply prime_of_mem_factors, rw [hx, list.mem_singleton] end lemma card_factors_mul {m n : ℕ} (m0 : m ≠ 0) (n0 : n ≠ 0) : Ω (m * n) = Ω m + Ω n := by rw [card_factors_apply, card_factors_apply, card_factors_apply, ← multiset.coe_card, ← factors_eq, unique_factorization_monoid.normalized_factors_mul m0 n0, factors_eq, factors_eq, multiset.card_add, multiset.coe_card, multiset.coe_card] lemma card_factors_multiset_prod {s : multiset ℕ} (h0 : s.prod ≠ 0) : Ω s.prod = (multiset.map Ω s).sum := begin revert h0, apply s.induction_on, by simp, intros a t h h0, rw [multiset.prod_cons, mul_ne_zero_iff] at h0, simp [h0, card_factors_mul, h], end @[simp] lemma card_factors_apply_prime {p : ℕ} (hp : p.prime) : Ω p = 1 := card_factors_eq_one_iff_prime.2 hp @[simp] lemma card_factors_apply_prime_pow {p k : ℕ} (hp : p.prime) : Ω (p ^ k) = k := by rw [card_factors_apply, hp.factors_pow, list.length_repeat] /-- `ω n` is the number of distinct prime factors of `n`. -/ def card_distinct_factors : arithmetic_function ℕ := ⟨λ n, n.factors.dedup.length, by simp⟩ localized "notation (name := card_distinct_factors) `ω` := nat.arithmetic_function.card_distinct_factors" in arithmetic_function lemma card_distinct_factors_zero : ω 0 = 0 := by simp @[simp] lemma card_distinct_factors_one : ω 1 = 0 := by simp [card_distinct_factors] lemma card_distinct_factors_apply {n : ℕ} : ω n = n.factors.dedup.length := rfl lemma card_distinct_factors_eq_card_factors_iff_squarefree {n : ℕ} (h0 : n ≠ 0) : ω n = Ω n ↔ squarefree n := begin rw [squarefree_iff_nodup_factors h0, card_distinct_factors_apply], split; intro h, { rw ←n.factors.dedup_sublist.eq_of_length h, apply list.nodup_dedup }, { rw h.dedup, refl } end @[simp] lemma card_distinct_factors_apply_prime_pow {p k : ℕ} (hp : p.prime) (hk : k ≠ 0) : ω (p ^ k) = 1 := by rw [card_distinct_factors_apply, hp.factors_pow, list.repeat_dedup hk, list.length_singleton] @[simp] lemma card_distinct_factors_apply_prime {p : ℕ} (hp : p.prime) : ω p = 1 := by rw [←pow_one p, card_distinct_factors_apply_prime_pow hp one_ne_zero] /-- `μ` is the Möbius function. If `n` is squarefree with an even number of distinct prime factors, `μ n = 1`. If `n` is squarefree with an odd number of distinct prime factors, `μ n = -1`. If `n` is not squarefree, `μ n = 0`. -/ def moebius : arithmetic_function ℤ := ⟨λ n, if squarefree n then (-1) ^ (card_factors n) else 0, by simp⟩ localized "notation (name := moebius) `μ` := nat.arithmetic_function.moebius" in arithmetic_function @[simp] lemma moebius_apply_of_squarefree {n : ℕ} (h : squarefree n) : μ n = (-1) ^ card_factors n := if_pos h @[simp] lemma moebius_eq_zero_of_not_squarefree {n : ℕ} (h : ¬ squarefree n) : μ n = 0 := if_neg h lemma moebius_apply_one : μ 1 = 1 := by simp lemma moebius_ne_zero_iff_squarefree {n : ℕ} : μ n ≠ 0 ↔ squarefree n := begin split; intro h, { contrapose! h, simp [h] }, { simp [h, pow_ne_zero] } end lemma moebius_ne_zero_iff_eq_or {n : ℕ} : μ n ≠ 0 ↔ μ n = 1 ∨ μ n = -1 := begin split; intro h, { rw moebius_ne_zero_iff_squarefree at h, rw moebius_apply_of_squarefree h, apply neg_one_pow_eq_or }, { rcases h with h | h; simp [h] } end lemma moebius_apply_prime {p : ℕ} (hp : p.prime) : μ p = -1 := by rw [moebius_apply_of_squarefree hp.squarefree, card_factors_apply_prime hp, pow_one] lemma moebius_apply_prime_pow {p k : ℕ} (hp : p.prime) (hk : k ≠ 0) : μ (p ^ k) = if k = 1 then -1 else 0 := begin split_ifs, { rw [h, pow_one, moebius_apply_prime hp] }, rw [moebius_eq_zero_of_not_squarefree], rw [squarefree_pow_iff hp.ne_one hk, not_and_distrib], exact or.inr h, end lemma moebius_apply_is_prime_pow_not_prime {n : ℕ} (hn : is_prime_pow n) (hn' : ¬ n.prime) : μ n = 0 := begin obtain ⟨p, k, hp, hk, rfl⟩ := (is_prime_pow_nat_iff _).1 hn, rw [moebius_apply_prime_pow hp hk.ne', if_neg], rintro rfl, exact hn' (by simpa), end lemma is_multiplicative_moebius : is_multiplicative μ := begin rw is_multiplicative.iff_ne_zero, refine ⟨by simp, λ n m hn hm hnm, _⟩, simp only [moebius, zero_hom.coe_mk, squarefree_mul hnm, ite_and, card_factors_mul hn hm], rw [pow_add, mul_comm, ite_mul_zero_left, ite_mul_zero_right, mul_comm], end open unique_factorization_monoid @[simp] lemma moebius_mul_coe_zeta : (μ * ζ : arithmetic_function ℤ) = 1 := begin ext n, refine rec_on_pos_prime_pos_coprime _ _ _ _ n, { intros p n hp hn, rw [coe_mul_zeta_apply, sum_divisors_prime_pow hp, sum_range_succ'], simp_rw [function.embedding.coe_fn_mk, pow_zero, moebius_apply_one, moebius_apply_prime_pow hp (nat.succ_ne_zero _), nat.succ_inj', sum_ite_eq', mem_range, if_pos hn, add_left_neg], rw one_apply_ne, rw [ne.def, pow_eq_one_iff], { exact hp.ne_one }, { exact hn.ne' } }, { rw [zero_hom.map_zero, zero_hom.map_zero] }, { simp }, { intros a b ha hb hab ha' hb', rw [is_multiplicative.map_mul_of_coprime _ hab, ha', hb', is_multiplicative.map_mul_of_coprime is_multiplicative_one hab], exact is_multiplicative_moebius.mul is_multiplicative_zeta.nat_cast } end @[simp] lemma coe_zeta_mul_moebius : (ζ * μ : arithmetic_function ℤ) = 1 := by rw [mul_comm, moebius_mul_coe_zeta] @[simp] lemma coe_moebius_mul_coe_zeta [ring R] : (μ * ζ : arithmetic_function R) = 1 := by rw [←coe_coe, ←int_coe_mul, moebius_mul_coe_zeta, int_coe_one] @[simp] lemma coe_zeta_mul_coe_moebius [ring R] : (ζ * μ : arithmetic_function R) = 1 := by rw [←coe_coe, ←int_coe_mul, coe_zeta_mul_moebius, int_coe_one] section comm_ring variable [comm_ring R] instance : invertible (ζ : arithmetic_function R) := { inv_of := μ, inv_of_mul_self := coe_moebius_mul_coe_zeta, mul_inv_of_self := coe_zeta_mul_coe_moebius} /-- A unit in `arithmetic_function R` that evaluates to `ζ`, with inverse `μ`. -/ def zeta_unit : (arithmetic_function R)ˣ := ⟨ζ, μ, coe_zeta_mul_coe_moebius, coe_moebius_mul_coe_zeta⟩ @[simp] lemma coe_zeta_unit : ((zeta_unit : (arithmetic_function R)ˣ) : arithmetic_function R) = ζ := rfl @[simp] lemma inv_zeta_unit : ((zeta_unit⁻¹ : (arithmetic_function R)ˣ) : arithmetic_function R) = μ := rfl end comm_ring /-- Möbius inversion for functions to an `add_comm_group`. -/ theorem sum_eq_iff_sum_smul_moebius_eq [add_comm_group R] {f g : ℕ → R} : (∀ (n : ℕ), 0 < n → ∑ i in (n.divisors), f i = g n) ↔ ∀ (n : ℕ), 0 < n → ∑ (x : ℕ × ℕ) in n.divisors_antidiagonal, μ x.fst • g x.snd = f n := begin let f' : arithmetic_function R := ⟨λ x, if x = 0 then 0 else f x, if_pos rfl⟩, let g' : arithmetic_function R := ⟨λ x, if x = 0 then 0 else g x, if_pos rfl⟩, transitivity (ζ : arithmetic_function ℤ) • f' = g', { rw ext_iff, apply forall_congr, intro n, cases n, { simp }, rw coe_zeta_smul_apply, simp only [n.succ_ne_zero, forall_prop_of_true, succ_pos', if_false, zero_hom.coe_mk], rw sum_congr rfl (λ x hx, _), rw (if_neg (ne_of_gt (nat.pos_of_mem_divisors hx))) }, transitivity μ • g' = f', { split; intro h, { rw [← h, ← mul_smul, moebius_mul_coe_zeta, one_smul] }, { rw [← h, ← mul_smul, coe_zeta_mul_moebius, one_smul] } }, { rw ext_iff, apply forall_congr, intro n, cases n, { simp }, simp only [n.succ_ne_zero, forall_prop_of_true, succ_pos', smul_apply, if_false, zero_hom.coe_mk], rw sum_congr rfl (λ x hx, _), rw (if_neg (ne_of_gt (nat.pos_of_mem_divisors (snd_mem_divisors_of_mem_antidiagonal hx)))) }, end /-- Möbius inversion for functions to a `ring`. -/ theorem sum_eq_iff_sum_mul_moebius_eq [ring R] {f g : ℕ → R} : (∀ (n : ℕ), 0 < n → ∑ i in (n.divisors), f i = g n) ↔ ∀ (n : ℕ), 0 < n → ∑ (x : ℕ × ℕ) in n.divisors_antidiagonal, (μ x.fst : R) * g x.snd = f n := begin rw sum_eq_iff_sum_smul_moebius_eq, apply forall_congr, refine λ a, imp_congr_right (λ _, (sum_congr rfl $ λ x hx, _).congr_left), rw [zsmul_eq_mul], end /-- Möbius inversion for functions to a `comm_group`. -/ theorem prod_eq_iff_prod_pow_moebius_eq [comm_group R] {f g : ℕ → R} : (∀ (n : ℕ), 0 < n → ∏ i in (n.divisors), f i = g n) ↔ ∀ (n : ℕ), 0 < n → ∏ (x : ℕ × ℕ) in n.divisors_antidiagonal, g x.snd ^ (μ x.fst) = f n := @sum_eq_iff_sum_smul_moebius_eq (additive R) _ _ _ /-- Möbius inversion for functions to a `comm_group_with_zero`. -/ theorem prod_eq_iff_prod_pow_moebius_eq_of_nonzero [comm_group_with_zero R] {f g : ℕ → R} (hf : ∀ (n : ℕ), 0 < n → f n ≠ 0) (hg : ∀ (n : ℕ), 0 < n → g n ≠ 0) : (∀ (n : ℕ), 0 < n → ∏ i in (n.divisors), f i = g n) ↔ ∀ (n : ℕ), 0 < n → ∏ (x : ℕ × ℕ) in n.divisors_antidiagonal, g x.snd ^ (μ x.fst) = f n := begin refine iff.trans (iff.trans (forall_congr (λ n, _)) (@prod_eq_iff_prod_pow_moebius_eq Rˣ _ (λ n, if h : 0 < n then units.mk0 (f n) (hf n h) else 1) (λ n, if h : 0 < n then units.mk0 (g n) (hg n h) else 1))) (forall_congr (λ n, _)); refine imp_congr_right (λ hn, _), { dsimp, rw [dif_pos hn, ← units.eq_iff, ← units.coe_hom_apply, monoid_hom.map_prod, units.coe_mk0, prod_congr rfl _], intros x hx, rw [dif_pos (nat.pos_of_mem_divisors hx), units.coe_hom_apply, units.coe_mk0] }, { dsimp, rw [dif_pos hn, ← units.eq_iff, ← units.coe_hom_apply, monoid_hom.map_prod, units.coe_mk0, prod_congr rfl _], intros x hx, rw [dif_pos (nat.pos_of_mem_divisors (nat.snd_mem_divisors_of_mem_antidiagonal hx)), units.coe_hom_apply, units.coe_zpow, units.coe_mk0] } end end special_functions end arithmetic_function end nat
60c748bf84e0ab4a4589393962955915bd9828b0
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/category_theory/functor_category.lean
b32ad84014db5186fa78aac3c2d95fc3aeb4dcda
[ "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
4,412
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.natural_transformation namespace category_theory universes v₁ v₂ v₃ u₁ u₂ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation open nat_trans category category_theory.functor variables (C : Type u₁) [category.{v₁} C] (D : Type u₂) [category.{v₂} D] local attribute [simp] vcomp_app /-- `functor.category C D` gives the category structure on functors and natural transformations between categories `C` and `D`. Notice that if `C` and `D` are both small categories at the same universe level, this is another small category at that level. However if `C` and `D` are both large categories at the same universe level, this is a small category at the next higher level. -/ instance functor.category : category.{(max u₁ v₂)} (C ⥤ D) := { hom := λ F G, nat_trans F G, id := λ F, nat_trans.id F, comp := λ _ _ _ α β, vcomp α β } variables {C D} {E : Type u₃} [category.{v₃} E] variables {F G H I : C ⥤ D} namespace nat_trans @[simp] lemma vcomp_eq_comp (α : F ⟶ G) (β : G ⟶ H) : vcomp α β = α ≫ β := rfl lemma vcomp_app' (α : F ⟶ G) (β : G ⟶ H) (X : C) : (α ≫ β).app X = (α.app X) ≫ (β.app X) := rfl lemma congr_app {α β : F ⟶ G} (h : α = β) (X : C) : α.app X = β.app X := by rw h @[simp] lemma id_app (F : C ⥤ D) (X : C) : (𝟙 F : F ⟶ F).app X = 𝟙 (F.obj X) := rfl @[simp] lemma comp_app {F G H : C ⥤ D} (α : F ⟶ G) (β : G ⟶ H) (X : C) : (α ≫ β).app X = α.app X ≫ β.app X := rfl lemma app_naturality {F G : C ⥤ (D ⥤ E)} (T : F ⟶ G) (X : C) {Y Z : D} (f : Y ⟶ Z) : ((F.obj X).map f) ≫ ((T.app X).app Z) = ((T.app X).app Y) ≫ ((G.obj X).map f) := (T.app X).naturality f lemma naturality_app {F G : C ⥤ (D ⥤ E)} (T : F ⟶ G) (Z : D) {X Y : C} (f : X ⟶ Y) : ((F.map f).app Z) ≫ ((T.app Y).app Z) = ((T.app X).app Z) ≫ ((G.map f).app Z) := congr_fun (congr_arg app (T.naturality f)) Z /-- `hcomp α β` is the horizontal composition of natural transformations. -/ def hcomp {H I : D ⥤ E} (α : F ⟶ G) (β : H ⟶ I) : (F ⋙ H) ⟶ (G ⋙ I) := { app := λ X : C, (β.app (F.obj X)) ≫ (I.map (α.app X)), naturality' := λ X Y f, begin rw [functor.comp_map, functor.comp_map, ←assoc, naturality, assoc, ←map_comp I, naturality, map_comp, assoc] end } infix ` ◫ `:80 := hcomp @[simp] lemma hcomp_app {H I : D ⥤ E} (α : F ⟶ G) (β : H ⟶ I) (X : C) : (α ◫ β).app X = (β.app (F.obj X)) ≫ (I.map (α.app X)) := rfl @[simp] lemma hcomp_id_app {H : D ⥤ E} (α : F ⟶ G) (X : C) : (α ◫ 𝟙 H).app X = H.map (α.app X) := by {dsimp, simp} -- See note [dsimp, simp]. lemma id_hcomp_app {H : E ⥤ C} (α : F ⟶ G) (X : E) : (𝟙 H ◫ α).app X = α.app _ := by simp -- Note that we don't yet prove a `hcomp_assoc` lemma here: even stating it is painful, because we -- need to use associativity of functor composition. (It's true without the explicit associator, -- because functor composition is definitionally associative, but relying on the definitional equality -- causes bad problems with elaboration later.) lemma exchange {I J K : D ⥤ E} (α : F ⟶ G) (β : G ⟶ H) (γ : I ⟶ J) (δ : J ⟶ K) : (α ≫ β) ◫ (γ ≫ δ) = (α ◫ γ) ≫ (β ◫ δ) := by ext; simp end nat_trans open nat_trans namespace functor /-- Flip the arguments of a bifunctor. See also `currying.lean`. -/ protected def flip (F : C ⥤ (D ⥤ E)) : D ⥤ (C ⥤ E) := { obj := λ k, { obj := λ j, (F.obj j).obj k, map := λ j j' f, (F.map f).app k, map_id' := λ X, begin rw category_theory.functor.map_id, refl end, map_comp' := λ X Y Z f g, by rw [map_comp, ←comp_app] }, map := λ c c' f, { app := λ j, (F.obj j).map f } }. @[simp] lemma flip_obj_obj (F : C ⥤ (D ⥤ E)) (c) (d) : (F.flip.obj d).obj c = (F.obj c).obj d := rfl @[simp] lemma flip_obj_map (F : C ⥤ (D ⥤ E)) {c c' : C} (f : c ⟶ c') (d : D) : (F.flip.obj d).map f = (F.map f).app d := rfl @[simp] lemma flip_map_app (F : C ⥤ (D ⥤ E)) {d d' : D} (f : d ⟶ d') (c : C) : (F.flip.map f).app c = (F.obj c).map f := rfl end functor end category_theory
8808bf76142a0246ad9c896c2f5cb526ed5b07bb
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/algebra/lie/direct_sum.lean
c75c66f22566df65e013bec14c9a8d6e5bc9b358
[ "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
9,115
lean
/- Copyright (c) 2020 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import algebra.lie.basic import algebra.lie.submodule import algebra.lie.of_associative import linear_algebra.direct_sum.finsupp /-! # Direct sums of Lie algebras and Lie modules Direct sums of Lie algebras and Lie modules carry natural algbebra and module structures. ## Tags lie algebra, lie module, direct sum -/ universes u v w w₁ namespace direct_sum open dfinsupp open_locale direct_sum variables {R : Type u} {ι : Type v} [comm_ring R] section modules /-! The direct sum of Lie modules over a fixed Lie algebra carries a natural Lie module structure. -/ variables {L : Type w₁} {M : ι → Type w} variables [lie_ring L] [lie_algebra R L] variables [Π i, add_comm_group (M i)] [Π i, module R (M i)] variables [Π i, lie_ring_module L (M i)] [Π i, lie_module R L (M i)] instance : lie_ring_module L (⨁ i, M i) := { bracket := λ x m, m.map_range (λ i m', ⁅x, m'⁆) (λ i, lie_zero x), add_lie := λ x y m, by { ext, simp only [map_range_apply, add_apply, add_lie], }, lie_add := λ x m n, by { ext, simp only [map_range_apply, add_apply, lie_add], }, leibniz_lie := λ x y m, by { ext, simp only [map_range_apply, lie_lie, add_apply, sub_add_cancel], }, } @[simp] lemma lie_module_bracket_apply (x : L) (m : ⨁ i, M i) (i : ι) : ⁅x, m⁆ i = ⁅x, m i⁆ := map_range_apply _ _ m i instance : lie_module R L (⨁ i, M i) := { smul_lie := λ t x m, by { ext i, simp only [smul_lie, lie_module_bracket_apply, smul_apply], }, lie_smul := λ t x m, by { ext i, simp only [lie_smul, lie_module_bracket_apply, smul_apply], }, } variables (R ι L M) /-- The inclusion of each component into a direct sum as a morphism of Lie modules. -/ def lie_module_of [decidable_eq ι] (j : ι) : M j →ₗ⁅R,L⁆ ⨁ i, M i := { map_lie' := λ x m, begin ext i, by_cases h : j = i, { rw ← h, simp, }, { simp [lof, single_eq_of_ne h], }, end, ..lof R ι M j } /-- The projection map onto one component, as a morphism of Lie modules. -/ def lie_module_component (j : ι) : (⨁ i, M i) →ₗ⁅R,L⁆ M j := { map_lie' := λ x m, by simp only [component, lapply_apply, lie_module_bracket_apply, linear_map.to_fun_eq_coe], ..component R ι M j } end modules section algebras /-! The direct sum of Lie algebras carries a natural Lie algebra structure. -/ variables (L : ι → Type w) variables [Π i, lie_ring (L i)] [Π i, lie_algebra R (L i)] instance lie_ring : lie_ring (⨁ i, L i) := { bracket := zip_with (λ i, λ x y, ⁅x, y⁆) (λ i, lie_zero 0), add_lie := λ x y z, by { ext, simp only [zip_with_apply, add_apply, add_lie], }, lie_add := λ x y z, by { ext, simp only [zip_with_apply, add_apply, lie_add], }, lie_self := λ x, by { ext, simp only [zip_with_apply, add_apply, lie_self, zero_apply], }, leibniz_lie := λ x y z, by { ext, simp only [sub_apply, zip_with_apply, add_apply, zero_apply], apply leibniz_lie, }, ..(infer_instance : add_comm_group _) } @[simp] lemma bracket_apply (x y : ⨁ i, L i) (i : ι) : ⁅x, y⁆ i = ⁅x i, y i⁆ := zip_with_apply _ _ x y i instance lie_algebra : lie_algebra R (⨁ i, L i) := { lie_smul := λ c x y, by { ext, simp only [ zip_with_apply, smul_apply, bracket_apply, lie_smul] }, ..(infer_instance : module R _) } variables (R ι L) /-- The inclusion of each component into the direct sum as morphism of Lie algebras. -/ @[simps] def lie_algebra_of [decidable_eq ι] (j : ι) : L j →ₗ⁅R⁆ ⨁ i, L i := { to_fun := of L j, map_lie' := λ x y, by { ext i, by_cases h : j = i, { rw ← h, simp [of], }, { simp [of, single_eq_of_ne h], }, }, ..lof R ι L j, } /-- The projection map onto one component, as a morphism of Lie algebras. -/ @[simps] def lie_algebra_component (j : ι) : (⨁ i, L i) →ₗ⁅R⁆ L j := { to_fun := component R ι L j, map_lie' := λ x y, by simp only [component, bracket_apply, lapply_apply, linear_map.to_fun_eq_coe], ..component R ι L j } @[ext] lemma lie_algebra_ext {x y : ⨁ i, L i} (h : ∀ i, lie_algebra_component R ι L i x = lie_algebra_component R ι L i y) : x = y := dfinsupp.ext h include R lemma lie_of_of_ne [decidable_eq ι] {i j : ι} (hij : j ≠ i) (x : L i) (y : L j) : ⁅of L i x, of L j y⁆ = 0 := begin apply lie_algebra_ext R ι L, intros k, rw lie_hom.map_lie, simp only [component, of, lapply_apply, single_add_hom_apply, lie_algebra_component_apply, single_apply, zero_apply], by_cases hik : i = k, { simp only [dif_neg, not_false_iff, lie_zero, hik.symm, hij], }, { simp only [dif_neg, not_false_iff, zero_lie, hik], }, end lemma lie_of_of_eq [decidable_eq ι] {i j : ι} (hij : j = i) (x : L i) (y : L j) : ⁅of L i x, of L j y⁆ = of L i ⁅x, hij.rec_on y⁆ := begin have : of L j y = of L i (hij.rec_on y), { exact eq.drec (eq.refl _) hij, }, rw [this, ← lie_algebra_of_apply R ι L i ⁅x, hij.rec_on y⁆, lie_hom.map_lie, lie_algebra_of_apply, lie_algebra_of_apply], end @[simp] lemma lie_of [decidable_eq ι] {i j : ι} (x : L i) (y : L j) : ⁅of L i x, of L j y⁆ = if hij : j = i then lie_algebra_of R ι L i ⁅x, hij.rec_on y⁆ else 0 := begin by_cases hij : j = i, { simp only [lie_of_of_eq R ι L hij x y, hij, dif_pos, not_false_iff, lie_algebra_of_apply], }, { simp only [lie_of_of_ne R ι L hij x y, hij, dif_neg, not_false_iff], }, end variables {R L ι} /-- Given a family of Lie algebras `L i`, together with a family of morphisms of Lie algebras `f i : L i →ₗ⁅R⁆ L'` into a fixed Lie algebra `L'`, we have a natural linear map: `(⨁ i, L i) →ₗ[R] L'`. If in addition `⁅f i x, f j y⁆ = 0` for any `x ∈ L i` and `y ∈ L j` (`i ≠ j`) then this map is a morphism of Lie algebras. -/ @[simps] def to_lie_algebra [decidable_eq ι] (L' : Type w₁) [lie_ring L'] [lie_algebra R L'] (f : Π i, L i →ₗ⁅R⁆ L') (hf : ∀ (i j : ι), i ≠ j → ∀ (x : L i) (y : L j), ⁅f i x, f j y⁆ = 0) : (⨁ i, L i) →ₗ⁅R⁆ L' := { to_fun := to_module R ι L' (λ i, (f i : L i →ₗ[R] L')), map_lie' := λ x y, begin let f' := λ i, (f i : L i →ₗ[R] L'), /- The goal is linear in `y`. We can use this to reduce to the case that `y` has only one non-zero component. -/ suffices : ∀ (i : ι) (y : L i), to_module R ι L' f' ⁅x, of L i y⁆ = ⁅to_module R ι L' f' x, to_module R ι L' f' (of L i y)⁆, { simp only [← lie_algebra.ad_apply R], rw [← linear_map.comp_apply, ← linear_map.comp_apply], congr, clear y, ext i y, exact this i y, }, /- Similarly, we can reduce to the case that `x` has only one non-zero component. -/ suffices : ∀ i j (y : L i) (x : L j), to_module R ι L' f' ⁅of L j x, of L i y⁆ = ⁅to_module R ι L' f' (of L j x), to_module R ι L' f' (of L i y)⁆, { intros i y, rw [← lie_skew x, ← lie_skew (to_module R ι L' f' x)], simp only [linear_map.map_neg, neg_inj, ← lie_algebra.ad_apply R], rw [← linear_map.comp_apply, ← linear_map.comp_apply], congr, clear x, ext j x, exact this j i x y, }, /- Tidy up and use `lie_of`. -/ intros i j y x, simp only [lie_of R, lie_algebra_of_apply, lie_hom.coe_to_linear_map, to_add_monoid_of, coe_to_module_eq_coe_to_add_monoid, linear_map.to_add_monoid_hom_coe], /- And finish with trivial case analysis. -/ rcases eq_or_ne i j with h | h, { have h' : f j (h.rec_on y) = f i y, { exact eq.drec (eq.refl _) h, }, simp only [h, h', lie_hom.coe_to_linear_map, dif_pos, lie_hom.map_lie, to_add_monoid_of, linear_map.to_add_monoid_hom_coe], }, { simp only [h, hf j i h.symm x y, dif_neg, not_false_iff, add_monoid_hom.map_zero], }, end, .. to_module R ι L' (λ i, (f i : L i →ₗ[R] L')) } end algebras section ideals variables {L : Type w} [lie_ring L] [lie_algebra R L] (I : ι → lie_ideal R L) /-- Given a Lie algebra `L` and a family of ideals `I i ⊆ L`, informally this definition is the statement that `L = ⨁ i, I i`. More formally, the inclusions give a natural map from the (external) direct sum to the enclosing Lie algebra: `(⨁ i, I i) → L`, and this definition is the proposition that this map is bijective. -/ def lie_algebra_is_internal [decidable_eq ι] : Prop := function.bijective $ to_module R ι L $ λ i, ((I i).incl : I i →ₗ[R] L) /-- The fact that this instance is necessary seems to be a bug in typeclass inference. See [this Zulip thread](https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/ Typeclass.20resolution.20under.20binders/near/245151099). -/ instance lie_ring_of_ideals : lie_ring (⨁ i, I i) := direct_sum.lie_ring (λ i, ↥(I i)) /-- See `direct_sum.lie_ring_of_ideals` comment. -/ instance lie_algebra_of_ideals : lie_algebra R (⨁ i, I i) := direct_sum.lie_algebra (λ i, ↥(I i)) end ideals end direct_sum
d41b73ca265b5a419263cf99d5f96535f18f039a
c777c32c8e484e195053731103c5e52af26a25d1
/src/linear_algebra/cross_product.lean
67f47a6f8ec055b9eac2c1381ffa3590598b53f3
[ "Apache-2.0" ]
permissive
kbuzzard/mathlib
2ff9e85dfe2a46f4b291927f983afec17e946eb8
58537299e922f9c77df76cb613910914a479c1f7
refs/heads/master
1,685,313,702,744
1,683,974,212,000
1,683,974,212,000
128,185,277
1
0
null
1,522,920,600,000
1,522,920,600,000
null
UTF-8
Lean
false
false
5,352
lean
/- Copyright (c) 2021 Martin Dvorak. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Martin Dvorak, Kyle Miller, Eric Wieser -/ import data.matrix.notation import linear_algebra.bilinear_map import linear_algebra.matrix.determinant import algebra.lie.basic /-! # Cross products This module defines the cross product of vectors in $R^3$ for $R$ a commutative ring, as a bilinear map. ## Main definitions * `cross_product` is the cross product of pairs of vectors in $R^3$. ## Main results * `triple_product_eq_det` * `cross_dot_cross` * `jacobi_cross` ## Notation The locale `matrix` gives the following notation: * `×₃` for the cross product ## Tags crossproduct -/ open matrix open_locale matrix variables {R : Type*} [comm_ring R] /-- The cross product of two vectors in $R^3$ for $R$ a commutative ring. -/ def cross_product : (fin 3 → R) →ₗ[R] (fin 3 → R) →ₗ[R] (fin 3 → R) := begin apply linear_map.mk₂ R (λ (a b : fin 3 → R), ![a 1 * b 2 - a 2 * b 1, a 2 * b 0 - a 0 * b 2, a 0 * b 1 - a 1 * b 0]), { intros, simp [vec3_add (_ : R), add_comm, add_assoc, add_left_comm, add_mul, sub_eq_add_neg] }, { intros, simp [smul_vec3 (_ : R) (_ : R), mul_comm, mul_assoc, mul_left_comm, mul_add, sub_eq_add_neg] }, { intros, simp [vec3_add (_ : R), add_comm, add_assoc, add_left_comm, mul_add, sub_eq_add_neg] }, { intros, simp [smul_vec3 (_ : R) (_ : R), mul_comm, mul_assoc, mul_left_comm, mul_add, sub_eq_add_neg] }, end localized "infixl (name := cross_product) ` ×₃ `: 74 := cross_product" in matrix lemma cross_apply (a b : fin 3 → R) : a ×₃ b = ![a 1 * b 2 - a 2 * b 1, a 2 * b 0 - a 0 * b 2, a 0 * b 1 - a 1 * b 0] := rfl section products_properties @[simp] lemma cross_anticomm (v w : fin 3 → R) : - (v ×₃ w) = w ×₃ v := by simp [cross_apply, mul_comm] alias cross_anticomm ← neg_cross @[simp] lemma cross_anticomm' (v w : fin 3 → R) : v ×₃ w + w ×₃ v = 0 := by rw [add_eq_zero_iff_eq_neg, cross_anticomm] @[simp] lemma cross_self (v : fin 3 → R) : v ×₃ v = 0 := by simp [cross_apply, mul_comm] /-- The cross product of two vectors is perpendicular to the first vector. -/ @[simp] lemma dot_self_cross (v w : fin 3 → R) : v ⬝ᵥ (v ×₃ w) = 0 := by simp [cross_apply, vec3_dot_product, mul_sub, mul_assoc, mul_left_comm] /-- The cross product of two vectors is perpendicular to the second vector. -/ @[simp] lemma dot_cross_self (v w : fin 3 → R) : w ⬝ᵥ (v ×₃ w) = 0 := by rw [← cross_anticomm, matrix.dot_product_neg, dot_self_cross, neg_zero] /-- Cyclic permutations preserve the triple product. See also `triple_product_eq_det`. -/ lemma triple_product_permutation (u v w : fin 3 → R) : u ⬝ᵥ (v ×₃ w) = v ⬝ᵥ (w ×₃ u) := begin simp only [cross_apply, vec3_dot_product, matrix.head_cons, matrix.cons_vec_bit0_eq_alt0, matrix.empty_vec_append, matrix.cons_val_one, matrix.cons_vec_alt0, matrix.cons_vec_append, matrix.cons_val_zero], ring, end /-- The triple product of `u`, `v`, and `w` is equal to the determinant of the matrix with those vectors as its rows. -/ theorem triple_product_eq_det (u v w : fin 3 → R) : u ⬝ᵥ (v ×₃ w) = matrix.det ![u, v, w] := begin simp only [vec3_dot_product, cross_apply, matrix.det_fin_three, matrix.head_cons, matrix.cons_vec_bit0_eq_alt0, matrix.empty_vec_alt0, matrix.cons_vec_alt0, matrix.vec_head_vec_alt0, matrix.vec_append_apply_zero, matrix.empty_vec_append, matrix.cons_vec_append, matrix.cons_val', matrix.cons_val_one, matrix.cons_val_zero], ring, end /-- The scalar quadruple product identity, related to the Binet-Cauchy identity. -/ theorem cross_dot_cross (u v w x : fin 3 → R) : (u ×₃ v) ⬝ᵥ (w ×₃ x) = (u ⬝ᵥ w) * (v ⬝ᵥ x) - (u ⬝ᵥ x) * (v ⬝ᵥ w) := begin simp only [vec3_dot_product, cross_apply, cons_vec_append, cons_vec_bit0_eq_alt0, cons_val_one, cons_vec_alt0, linear_map.mk₂_apply, cons_val_zero, head_cons, empty_vec_append], ring_nf, end end products_properties section leibniz_properties /-- The cross product satisfies the Leibniz lie property. -/ lemma leibniz_cross (u v w : fin 3 → R) : u ×₃ (v ×₃ w) = (u ×₃ v) ×₃ w + v ×₃ (u ×₃ w) := begin dsimp only [cross_apply], ext i, fin_cases i; norm_num; ring, end /-- The three-dimensional vectors together with the operations + and ×₃ form a Lie ring. Note we do not make this an instance as a conflicting one already exists via `lie_ring.of_associative_ring`. -/ def cross.lie_ring : lie_ring (fin 3 → R) := { bracket := λ u v, u ×₃ v, add_lie := linear_map.map_add₂ _, lie_add := λ u, linear_map.map_add _, lie_self := cross_self, leibniz_lie := leibniz_cross, ..pi.add_comm_group } local attribute [instance] cross.lie_ring lemma cross_cross (u v w : fin 3 → R) : (u ×₃ v) ×₃ w = u ×₃ (v ×₃ w) - v ×₃ (u ×₃ w) := lie_lie u v w /-- Jacobi identity: For a cross product of three vectors, their sum over the three even permutations is equal to the zero vector. -/ theorem jacobi_cross (u v w : fin 3 → R) : u ×₃ (v ×₃ w) + v ×₃ (w ×₃ u) + w ×₃ (u ×₃ v) = 0 := lie_jacobi u v w end leibniz_properties
3f8ad0034905a74dacd0da83bd95120fccaf4f32
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/algebra/ring/basic.lean
e9cca497ba4231959cf7591b4b41cb1f2f4c467a
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
41,136
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Floris van Doorn, Amelia Livingston, Yury Kudryashov, Neil Strickland -/ import algebra.divisibility import data.set.basic /-! # Properties and homomorphisms of semirings and rings This file proves simple properties of semirings, rings and domains and their unit groups. It also defines bundled homomorphisms of semirings and rings. As with monoid and groups, we use the same structure `ring_hom a β`, a.k.a. `α →+* β`, for both homomorphism types. The unbundled homomorphisms are defined in `deprecated/ring`. They are deprecated and the plan is to slowly remove them from mathlib. ## Main definitions ring_hom, nonzero, domain, integral_domain ## Notations →+* for bundled ring homs (also use for semiring homs) ## Implementation notes There's a coercion from bundled homs to fun, and the canonical notation is to use the bundled hom as a function via this coercion. There is no `semiring_hom` -- the idea is that `ring_hom` is used. The constructor for a `ring_hom` between semirings needs a proof of `map_zero`, `map_one` and `map_add` as well as `map_mul`; a separate constructor `ring_hom.mk'` will construct ring homs between rings from monoid homs given only a proof that addition is preserved. ## Tags `ring_hom`, `semiring_hom`, `semiring`, `comm_semiring`, `ring`, `comm_ring`, `domain`, `integral_domain`, `nonzero`, `units` -/ universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {R : Type x} set_option old_structure_cmd true open function /-! ### `distrib` class -/ /-- A typeclass stating that multiplication is left and right distributive over addition. -/ @[protect_proj, ancestor has_mul has_add] class distrib (R : Type*) extends has_mul R, has_add R := (left_distrib : ∀ a b c : R, a * (b + c) = (a * b) + (a * c)) (right_distrib : ∀ a b c : R, (a + b) * c = (a * c) + (b * c)) lemma left_distrib [distrib R] (a b c : R) : a * (b + c) = a * b + a * c := distrib.left_distrib a b c alias left_distrib ← mul_add lemma right_distrib [distrib R] (a b c : R) : (a + b) * c = a * c + b * c := distrib.right_distrib a b c alias right_distrib ← add_mul /-- Pullback a `distrib` instance along an injective function. -/ protected def function.injective.distrib {S} [has_mul R] [has_add R] [distrib S] (f : R → S) (hf : injective f) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) : distrib R := { mul := (*), add := (+), left_distrib := λ x y z, hf $ by simp only [*, left_distrib], right_distrib := λ x y z, hf $ by simp only [*, right_distrib] } /-- Pushforward a `distrib` instance along a surjective function. -/ protected def function.surjective.distrib {S} [distrib R] [has_add S] [has_mul S] (f : R → S) (hf : surjective f) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) : distrib S := { mul := (*), add := (+), left_distrib := hf.forall₃.2 $ λ x y z, by simp only [← add, ← mul, left_distrib], right_distrib := hf.forall₃.2 $ λ x y z, by simp only [← add, ← mul, right_distrib] } /-! ### Semirings -/ /-- A semiring is a type with the following structures: additive commutative monoid (`add_comm_monoid`), multiplicative monoid (`monoid`), distributive laws (`distrib`), and multiplication by zero law (`mul_zero_class`). The actual definition extends `monoid_with_zero` instead of `monoid` and `mul_zero_class`. -/ @[protect_proj, ancestor add_comm_monoid monoid_with_zero distrib] class semiring (α : Type u) extends add_comm_monoid α, monoid_with_zero α, distrib α section semiring variables [semiring α] /-- Pullback a `semiring` instance along an injective function. -/ protected def function.injective.semiring [has_zero β] [has_one β] [has_add β] [has_mul β] (f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) : semiring β := { .. hf.monoid_with_zero f zero one mul, .. hf.add_comm_monoid f zero add, .. hf.distrib f add mul } /-- Pushforward a `semiring` instance along a surjective function. -/ protected def function.surjective.semiring [has_zero β] [has_one β] [has_add β] [has_mul β] (f : α → β) (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) : semiring β := { .. hf.monoid_with_zero f zero one mul, .. hf.add_comm_monoid f zero add, .. hf.distrib f add mul } lemma one_add_one_eq_two : 1 + 1 = (2 : α) := by unfold bit0 theorem two_mul (n : α) : 2 * n = n + n := eq.trans (right_distrib 1 1 n) (by simp) lemma distrib_three_right (a b c d : α) : (a + b + c) * d = a * d + b * d + c * d := by simp [right_distrib] theorem mul_two (n : α) : n * 2 = n + n := (left_distrib n 1 1).trans (by simp) theorem bit0_eq_two_mul (n : α) : bit0 n = 2 * n := (two_mul _).symm @[to_additive] lemma mul_ite {α} [has_mul α] (P : Prop) [decidable P] (a b c : α) : a * (if P then b else c) = if P then a * b else a * c := by split_ifs; refl @[to_additive] lemma ite_mul {α} [has_mul α] (P : Prop) [decidable P] (a b c : α) : (if P then a else b) * c = if P then a * c else b * c := by split_ifs; refl -- We make `mul_ite` and `ite_mul` simp lemmas, -- but not `add_ite` or `ite_add`. -- The problem we're trying to avoid is dealing with -- summations of the form `∑ x in s, (f x + ite P 1 0)`, -- in which `add_ite` followed by `sum_ite` would needlessly slice up -- the `f x` terms according to whether `P` holds at `x`. -- There doesn't appear to be a corresponding difficulty so far with -- `mul_ite` and `ite_mul`. attribute [simp] mul_ite ite_mul @[simp] lemma mul_boole {α} [semiring α] (P : Prop) [decidable P] (a : α) : a * (if P then 1 else 0) = if P then a else 0 := by simp @[simp] lemma boole_mul {α} [semiring α] (P : Prop) [decidable P] (a : α) : (if P then 1 else 0) * a = if P then a else 0 := by simp lemma ite_mul_zero_left {α : Type*} [mul_zero_class α] (P : Prop) [decidable P] (a b : α) : ite P (a * b) 0 = ite P a 0 * b := by { by_cases h : P; simp [h], } lemma ite_mul_zero_right {α : Type*} [mul_zero_class α] (P : Prop) [decidable P] (a b : α) : ite P (a * b) 0 = a * ite P b 0 := by { by_cases h : P; simp [h], } /-- An element `a` of a semiring is even if there exists `k` such `a = 2*k`. -/ def even (a : α) : Prop := ∃ k, a = 2*k lemma even_iff_two_dvd {a : α} : even a ↔ 2 ∣ a := iff.rfl @[simp] lemma range_two_mul (α : Type*) [semiring α] : set.range (λ x : α, 2 * x) = {a | even a} := by { ext x, simp [even, eq_comm] } @[simp] lemma even_bit0 (a : α) : even (bit0 a) := ⟨a, by rw [bit0, two_mul]⟩ /-- An element `a` of a semiring is odd if there exists `k` such `a = 2*k + 1`. -/ def odd (a : α) : Prop := ∃ k, a = 2*k + 1 @[simp] lemma odd_bit1 (a : α) : odd (bit1 a) := ⟨a, by rw [bit1, bit0, two_mul]⟩ @[simp] lemma range_two_mul_add_one (α : Type*) [semiring α] : set.range (λ x : α, 2 * x + 1) = {a | odd a} := by { ext x, simp [odd, eq_comm] } theorem dvd_add {a b c : α} (h₁ : a ∣ b) (h₂ : a ∣ c) : a ∣ b + c := dvd.elim h₁ (λ d hd, dvd.elim h₂ (λ e he, dvd.intro (d + e) (by simp [left_distrib, hd, he]))) end semiring namespace add_monoid_hom /-- Left multiplication by an element of a (semi)ring is an `add_monoid_hom` -/ def mul_left {R : Type*} [semiring R] (r : R) : R →+ R := { to_fun := (*) r, map_zero' := mul_zero r, map_add' := mul_add r } @[simp] lemma coe_mul_left {R : Type*} [semiring R] (r : R) : ⇑(mul_left r) = (*) r := rfl /-- Right multiplication by an element of a (semi)ring is an `add_monoid_hom` -/ def mul_right {R : Type*} [semiring R] (r : R) : R →+ R := { to_fun := λ a, a * r, map_zero' := zero_mul r, map_add' := λ _ _, add_mul _ _ r } @[simp] lemma coe_mul_right {R : Type*} [semiring R] (r : R) : ⇑(mul_right r) = (* r) := rfl lemma mul_right_apply {R : Type*} [semiring R] (a r : R) : mul_right r a = a * r := rfl end add_monoid_hom /-- Bundled semiring homomorphisms; use this for bundled ring homomorphisms too. This extends from both `monoid_hom` and `monoid_with_zero_hom` in order to put the fields in a sensible order, even though `monoid_with_zero_hom` already extends `monoid_hom`. -/ structure ring_hom (α : Type*) (β : Type*) [semiring α] [semiring β] extends monoid_hom α β, add_monoid_hom α β, monoid_with_zero_hom α β infixr ` →+* `:25 := ring_hom /-- Reinterpret a ring homomorphism `f : R →+* S` as a `monoid_with_zero_hom R S`. The `simp`-normal form is `(f : monoid_with_zero_hom R S)`. -/ add_decl_doc ring_hom.to_monoid_with_zero_hom /-- Reinterpret a ring homomorphism `f : R →+* S` as a monoid homomorphism `R →* S`. The `simp`-normal form is `(f : R →* S)`. -/ add_decl_doc ring_hom.to_monoid_hom /-- Reinterpret a ring homomorphism `f : R →+* S` as an additive monoid homomorphism `R →+ S`. The `simp`-normal form is `(f : R →+ S)`. -/ add_decl_doc ring_hom.to_add_monoid_hom namespace ring_hom section coe /-! Throughout this section, some `semiring` arguments are specified with `{}` instead of `[]`. See note [implicit instance arguments]. -/ variables {rα : semiring α} {rβ : semiring β} include rα rβ instance : has_coe_to_fun (α →+* β) := ⟨_, ring_hom.to_fun⟩ initialize_simps_projections ring_hom (to_fun → apply) @[simp] lemma to_fun_eq_coe (f : α →+* β) : f.to_fun = f := rfl @[simp] lemma coe_mk (f : α → β) (h₁ h₂ h₃ h₄) : ⇑(⟨f, h₁, h₂, h₃, h₄⟩ : α →+* β) = f := rfl instance has_coe_monoid_hom : has_coe (α →+* β) (α →* β) := ⟨ring_hom.to_monoid_hom⟩ @[simp, norm_cast] lemma coe_monoid_hom (f : α →+* β) : ⇑(f : α →* β) = f := rfl @[simp] lemma to_monoid_hom_eq_coe (f : α →+* β) : f.to_monoid_hom = f := rfl @[simp] lemma coe_monoid_hom_mk (f : α → β) (h₁ h₂ h₃ h₄) : ((⟨f, h₁, h₂, h₃, h₄⟩ : α →+* β) : α →* β) = ⟨f, h₁, h₂⟩ := rfl instance has_coe_add_monoid_hom : has_coe (α →+* β) (α →+ β) := ⟨ring_hom.to_add_monoid_hom⟩ @[simp, norm_cast] lemma coe_add_monoid_hom (f : α →+* β) : ⇑(f : α →+ β) = f := rfl @[simp] lemma to_add_monoid_hom_eq_coe (f : α →+* β) : f.to_add_monoid_hom = f := rfl @[simp] lemma coe_add_monoid_hom_mk (f : α → β) (h₁ h₂ h₃ h₄) : ((⟨f, h₁, h₂, h₃, h₄⟩ : α →+* β) : α →+ β) = ⟨f, h₃, h₄⟩ := rfl end coe variables [rα : semiring α] [rβ : semiring β] section include rα rβ variables (f : α →+* β) {x y : α} {rα rβ} theorem congr_fun {f g : α →+* β} (h : f = g) (x : α) : f x = g x := congr_arg (λ h : α →+* β, h x) h theorem congr_arg (f : α →+* β) {x y : α} (h : x = y) : f x = f y := congr_arg (λ x : α, f x) h theorem coe_inj ⦃f g : α →+* β⦄ (h : (f : α → β) = g) : f = g := by cases f; cases g; cases h; refl @[ext] theorem ext ⦃f g : α →+* β⦄ (h : ∀ x, f x = g x) : f = g := coe_inj (funext h) theorem ext_iff {f g : α →+* β} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, ext h⟩ @[simp] lemma mk_coe (f : α →+* β) (h₁ h₂ h₃ h₄) : ring_hom.mk f h₁ h₂ h₃ h₄ = f := ext $ λ _, rfl theorem coe_add_monoid_hom_injective : function.injective (coe : (α →+* β) → (α →+ β)) := λ f g h, ext (λ x, add_monoid_hom.congr_fun h x) theorem coe_monoid_hom_injective : function.injective (coe : (α →+* β) → (α →* β)) := λ f g h, ext (λ x, monoid_hom.congr_fun h x) /-- Ring homomorphisms map zero to zero. -/ @[simp] lemma map_zero (f : α →+* β) : f 0 = 0 := f.map_zero' /-- Ring homomorphisms map one to one. -/ @[simp] lemma map_one (f : α →+* β) : f 1 = 1 := f.map_one' /-- Ring homomorphisms preserve addition. -/ @[simp] lemma map_add (f : α →+* β) (a b : α) : f (a + b) = f a + f b := f.map_add' a b /-- Ring homomorphisms preserve multiplication. -/ @[simp] lemma map_mul (f : α →+* β) (a b : α) : f (a * b) = f a * f b := f.map_mul' a b /-- Ring homomorphisms preserve `bit0`. -/ @[simp] lemma map_bit0 (f : α →+* β) (a : α) : f (bit0 a) = bit0 (f a) := map_add _ _ _ /-- Ring homomorphisms preserve `bit1`. -/ @[simp] lemma map_bit1 (f : α →+* β) (a : α) : f (bit1 a) = bit1 (f a) := by simp [bit1] /-- `f : R →+* S` has a trivial codomain iff `f 1 = 0`. -/ lemma codomain_trivial_iff_map_one_eq_zero : (0 : β) = 1 ↔ f 1 = 0 := by rw [map_one, eq_comm] /-- `f : R →+* S` has a trivial codomain iff it has a trivial range. -/ lemma codomain_trivial_iff_range_trivial : (0 : β) = 1 ↔ (∀ x, f x = 0) := f.codomain_trivial_iff_map_one_eq_zero.trans ⟨λ h x, by rw [←mul_one x, map_mul, h, mul_zero], λ h, h 1⟩ /-- `f : R →+* S` has a trivial codomain iff its range is `{0}`. -/ lemma codomain_trivial_iff_range_eq_singleton_zero : (0 : β) = 1 ↔ set.range f = {0} := f.codomain_trivial_iff_range_trivial.trans ⟨ λ h, set.ext (λ y, ⟨λ ⟨x, hx⟩, by simp [←hx, h x], λ hy, ⟨0, by simpa using hy.symm⟩⟩), λ h x, set.mem_singleton_iff.mp (h ▸ set.mem_range_self x)⟩ /-- `f : R →+* S` doesn't map `1` to `0` if `S` is nontrivial -/ lemma map_one_ne_zero [nontrivial β] : f 1 ≠ 0 := mt f.codomain_trivial_iff_map_one_eq_zero.mpr zero_ne_one /-- If there is a homomorphism `f : R →+* S` and `S` is nontrivial, then `R` is nontrivial. -/ lemma domain_nontrivial [nontrivial β] : nontrivial α := ⟨⟨1, 0, mt (λ h, show f 1 = 0, by rw [h, map_zero]) f.map_one_ne_zero⟩⟩ lemma is_unit_map (f : α →+* β) {a : α} (h : is_unit a) : is_unit (f a) := h.map (f.to_monoid_hom) end /-- The identity ring homomorphism from a semiring to itself. -/ def id (α : Type*) [semiring α] : α →+* α := by refine {to_fun := id, ..}; intros; refl include rα instance : inhabited (α →+* α) := ⟨id α⟩ @[simp] lemma id_apply (x : α) : ring_hom.id α x = x := rfl variable {rγ : semiring γ} include rβ rγ /-- Composition of ring homomorphisms is a ring homomorphism. -/ def comp (hnp : β →+* γ) (hmn : α →+* β) : α →+* γ := { to_fun := hnp ∘ hmn, map_zero' := by simp, map_one' := by simp, map_add' := λ x y, by simp, map_mul' := λ x y, by simp} /-- Composition of semiring homomorphisms is associative. -/ lemma comp_assoc {δ} {rδ: semiring δ} (f : α →+* β) (g : β →+* γ) (h : γ →+* δ) : (h.comp g).comp f = h.comp (g.comp f) := rfl @[simp] lemma coe_comp (hnp : β →+* γ) (hmn : α →+* β) : (hnp.comp hmn : α → γ) = hnp ∘ hmn := rfl lemma comp_apply (hnp : β →+* γ) (hmn : α →+* β) (x : α) : (hnp.comp hmn : α → γ) x = (hnp (hmn x)) := rfl omit rγ @[simp] lemma comp_id (f : α →+* β) : f.comp (id α) = f := ext $ λ x, rfl @[simp] lemma id_comp (f : α →+* β) : (id β).comp f = f := ext $ λ x, rfl omit rβ instance : monoid (α →+* α) := { one := id α, mul := comp, mul_one := comp_id, one_mul := id_comp, mul_assoc := λ f g h, comp_assoc _ _ _ } lemma one_def : (1 : α →+* α) = id α := rfl @[simp] lemma coe_one : ⇑(1 : α →+* α) = _root_.id := rfl lemma mul_def (f g : α →+* α) : f * g = f.comp g := rfl @[simp] lemma coe_mul (f g : α →+* α) : ⇑(f * g) = f ∘ g := rfl include rβ rγ lemma cancel_right {g₁ g₂ : β →+* γ} {f : α →+* β} (hf : surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨λ h, ring_hom.ext $ (forall_iff_forall_surj hf).1 (ext_iff.1 h), λ h, h ▸ rfl⟩ lemma cancel_left {g : β →+* γ} {f₁ f₂ : α →+* β} (hg : injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨λ h, ring_hom.ext $ λ x, hg $ by rw [← comp_apply, h, comp_apply], λ h, h ▸ rfl⟩ omit rα rβ rγ end ring_hom /-- A commutative semiring is a `semiring` with commutative multiplication. In other words, it is a type with the following structures: additive commutative monoid (`add_comm_monoid`), multiplicative commutative monoid (`comm_monoid`), distributive laws (`distrib`), and multiplication by zero law (`mul_zero_class`). -/ @[protect_proj, ancestor semiring comm_monoid] class comm_semiring (α : Type u) extends semiring α, comm_monoid α @[priority 100] -- see Note [lower instance priority] instance comm_semiring.to_comm_monoid_with_zero [comm_semiring α] : comm_monoid_with_zero α := { .. comm_semiring.to_comm_monoid α, .. comm_semiring.to_semiring α } section comm_semiring variables [comm_semiring α] [comm_semiring β] {a b c : α} /-- Pullback a `semiring` instance along an injective function. -/ protected def function.injective.comm_semiring [has_zero γ] [has_one γ] [has_add γ] [has_mul γ] (f : γ → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) : comm_semiring γ := { .. hf.semiring f zero one add mul, .. hf.comm_semigroup f mul } /-- Pullback a `semiring` instance along an injective function. -/ protected def function.surjective.comm_semiring [has_zero γ] [has_one γ] [has_add γ] [has_mul γ] (f : α → γ) (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) : comm_semiring γ := { .. hf.semiring f zero one add mul, .. hf.comm_semigroup f mul } lemma add_mul_self_eq (a b : α) : (a + b) * (a + b) = a*a + 2*a*b + b*b := by simp only [two_mul, add_mul, mul_add, add_assoc, mul_comm b] @[simp] theorem two_dvd_bit0 : 2 ∣ bit0 a := ⟨a, bit0_eq_two_mul _⟩ lemma ring_hom.map_dvd (f : α →+* β) {a b : α} : a ∣ b → f a ∣ f b := λ ⟨z, hz⟩, ⟨f z, by rw [hz, f.map_mul]⟩ end comm_semiring /-! ### Rings -/ /-- A ring is a type with the following structures: additive commutative group (`add_comm_group`), multiplicative monoid (`monoid`), and distributive laws (`distrib`). Equivalently, a ring is a `semiring` with a negation operation making it an additive group. -/ @[protect_proj, ancestor add_comm_group monoid distrib] class ring (α : Type u) extends add_comm_group α, monoid α, distrib α section ring variables [ring α] {a b c d e : α} /- The instance from `ring` to `semiring` happens often in linear algebra, for which all the basic definitions are given in terms of semirings, but many applications use rings or fields. We increase a little bit its priority above 100 to try it quickly, but remaining below the default 1000 so that more specific instances are tried first. -/ @[priority 200] instance ring.to_semiring : semiring α := { zero_mul := λ a, add_left_cancel $ show 0 * a + 0 * a = 0 * a + 0, by rw [← add_mul, zero_add, add_zero], mul_zero := λ a, add_left_cancel $ show a * 0 + a * 0 = a * 0 + 0, by rw [← mul_add, add_zero, add_zero], ..‹ring α› } /-- Pullback a `ring` instance along an injective function. -/ protected def function.injective.ring [has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β] (f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) : ring β := { .. hf.add_comm_group f zero add neg sub, .. hf.monoid f one mul, .. hf.distrib f add mul } /-- Pullback a `ring` instance along an injective function. -/ protected def function.surjective.ring [has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β] (f : α → β) (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) : ring β := { .. hf.add_comm_group f zero add neg sub, .. hf.monoid f one mul, .. hf.distrib f add mul } lemma neg_mul_eq_neg_mul (a b : α) : -(a * b) = -a * b := neg_eq_of_add_eq_zero begin rw [← right_distrib, add_right_neg, zero_mul] end lemma neg_mul_eq_mul_neg (a b : α) : -(a * b) = a * -b := neg_eq_of_add_eq_zero begin rw [← left_distrib, add_right_neg, mul_zero] end @[simp] lemma neg_mul_eq_neg_mul_symm (a b : α) : - a * b = - (a * b) := eq.symm (neg_mul_eq_neg_mul a b) @[simp] lemma mul_neg_eq_neg_mul_symm (a b : α) : a * - b = - (a * b) := eq.symm (neg_mul_eq_mul_neg a b) lemma neg_mul_neg (a b : α) : -a * -b = a * b := by simp lemma neg_mul_comm (a b : α) : -a * b = a * -b := by simp theorem neg_eq_neg_one_mul (a : α) : -a = -1 * a := by simp lemma mul_sub_left_distrib (a b c : α) : a * (b - c) = a * b - a * c := by simpa only [sub_eq_add_neg, neg_mul_eq_mul_neg] using mul_add a b (-c) alias mul_sub_left_distrib ← mul_sub lemma mul_sub_right_distrib (a b c : α) : (a - b) * c = a * c - b * c := by simpa only [sub_eq_add_neg, neg_mul_eq_neg_mul] using add_mul a (-b) c alias mul_sub_right_distrib ← sub_mul /-- An element of a ring multiplied by the additive inverse of one is the element's additive inverse. -/ lemma mul_neg_one (a : α) : a * -1 = -a := by simp /-- The additive inverse of one multiplied by an element of a ring is the element's additive inverse. -/ lemma neg_one_mul (a : α) : -1 * a = -a := by simp /-- An iff statement following from right distributivity in rings and the definition of subtraction. -/ theorem mul_add_eq_mul_add_iff_sub_mul_add_eq : a * e + c = b * e + d ↔ (a - b) * e + c = d := calc a * e + c = b * e + d ↔ a * e + c = d + b * e : by simp [add_comm] ... ↔ a * e + c - b * e = d : iff.intro (λ h, begin rw h, simp end) (λ h, begin rw ← h, simp end) ... ↔ (a - b) * e + c = d : begin simp [sub_mul, sub_add_eq_add_sub] end /-- A simplification of one side of an equation exploiting right distributivity in rings and the definition of subtraction. -/ theorem sub_mul_add_eq_of_mul_add_eq_mul_add : a * e + c = b * e + d → (a - b) * e + c = d := assume h, calc (a - b) * e + c = (a * e + c) - b * e : begin simp [sub_mul, sub_add_eq_add_sub] end ... = d : begin rw h, simp [@add_sub_cancel α] end end ring namespace units variables [ring α] {a b : α} /-- Each element of the group of units of a ring has an additive inverse. -/ instance : has_neg (units α) := ⟨λu, ⟨-↑u, -↑u⁻¹, by simp, by simp⟩ ⟩ /-- Representing an element of a ring's unit group as an element of the ring commutes with mapping this element to its additive inverse. -/ @[simp, norm_cast] protected theorem coe_neg (u : units α) : (↑-u : α) = -u := rfl @[simp, norm_cast] protected theorem coe_neg_one : ((-1 : units α) : α) = -1 := rfl /-- Mapping an element of a ring's unit group to its inverse commutes with mapping this element to its additive inverse. -/ @[simp] protected theorem neg_inv (u : units α) : (-u)⁻¹ = -u⁻¹ := rfl /-- An element of a ring's unit group equals the additive inverse of its additive inverse. -/ @[simp] protected theorem neg_neg (u : units α) : - -u = u := units.ext $ neg_neg _ /-- Multiplication of elements of a ring's unit group commutes with mapping the first argument to its additive inverse. -/ @[simp] protected theorem neg_mul (u₁ u₂ : units α) : -u₁ * u₂ = -(u₁ * u₂) := units.ext $ neg_mul_eq_neg_mul_symm _ _ /-- Multiplication of elements of a ring's unit group commutes with mapping the second argument to its additive inverse. -/ @[simp] protected theorem mul_neg (u₁ u₂ : units α) : u₁ * -u₂ = -(u₁ * u₂) := units.ext $ (neg_mul_eq_mul_neg _ _).symm /-- Multiplication of the additive inverses of two elements of a ring's unit group equals multiplication of the two original elements. -/ @[simp] protected theorem neg_mul_neg (u₁ u₂ : units α) : -u₁ * -u₂ = u₁ * u₂ := by simp /-- The additive inverse of an element of a ring's unit group equals the additive inverse of one times the original element. -/ protected theorem neg_eq_neg_one_mul (u : units α) : -u = -1 * u := by simp end units namespace ring_hom /-- Ring homomorphisms preserve additive inverse. -/ @[simp] theorem map_neg {α β} [ring α] [ring β] (f : α →+* β) (x : α) : f (-x) = -(f x) := (f : α →+ β).map_neg x /-- Ring homomorphisms preserve subtraction. -/ @[simp] theorem map_sub {α β} [ring α] [ring β] (f : α →+* β) (x y : α) : f (x - y) = (f x) - (f y) := (f : α →+ β).map_sub x y /-- A ring homomorphism is injective iff its kernel is trivial. -/ theorem injective_iff {α β} [ring α] [semiring β] (f : α →+* β) : function.injective f ↔ (∀ a, f a = 0 → a = 0) := (f : α →+ β).injective_iff /-- Makes a ring homomorphism from a monoid homomorphism of rings which preserves addition. -/ def mk' {γ} [semiring α] [ring γ] (f : α →* γ) (map_add : ∀ a b : α, f (a + b) = f a + f b) : α →+* γ := { to_fun := f, .. add_monoid_hom.mk' f map_add, .. f } end ring_hom /-- A commutative ring is a `ring` with commutative multiplication. -/ @[protect_proj, ancestor ring comm_semigroup] class comm_ring (α : Type u) extends ring α, comm_semigroup α @[priority 100] -- see Note [lower instance priority] instance comm_ring.to_comm_semiring [s : comm_ring α] : comm_semiring α := { mul_zero := mul_zero, zero_mul := zero_mul, ..s } section comm_ring variables [comm_ring α] {a b c : α} /-- Pullback a `comm_ring` instance along an injective function. -/ protected def function.injective.comm_ring [has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β] (f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) : comm_ring β := { .. hf.ring f zero one add mul neg sub, .. hf.comm_semigroup f mul } /-- Pullback a `comm_ring` instance along an injective function. -/ protected def function.surjective.comm_ring [has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β] (f : α → β) (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) : comm_ring β := { .. hf.ring f zero one add mul neg sub, .. hf.comm_semigroup f mul } local attribute [simp] add_assoc add_comm add_left_comm mul_comm theorem dvd_neg_of_dvd (h : a ∣ b) : (a ∣ -b) := dvd.elim h (assume c, assume : b = a * c, dvd.intro (-c) (by simp [this])) theorem dvd_of_dvd_neg (h : a ∣ -b) : (a ∣ b) := let t := dvd_neg_of_dvd h in by rwa neg_neg at t theorem dvd_neg_iff_dvd (a b : α) : (a ∣ -b) ↔ (a ∣ b) := ⟨dvd_of_dvd_neg, dvd_neg_of_dvd⟩ theorem neg_dvd_of_dvd (h : a ∣ b) : -a ∣ b := dvd.elim h (assume c, assume : b = a * c, dvd.intro (-c) (by simp [this])) theorem dvd_of_neg_dvd (h : -a ∣ b) : a ∣ b := let t := neg_dvd_of_dvd h in by rwa neg_neg at t theorem neg_dvd_iff_dvd (a b : α) : (-a ∣ b) ↔ (a ∣ b) := ⟨dvd_of_neg_dvd, neg_dvd_of_dvd⟩ theorem dvd_sub (h₁ : a ∣ b) (h₂ : a ∣ c) : a ∣ b - c := by { rw sub_eq_add_neg, exact dvd_add h₁ (dvd_neg_of_dvd h₂) } theorem dvd_add_iff_left (h : a ∣ c) : a ∣ b ↔ a ∣ b + c := ⟨λh₂, dvd_add h₂ h, λH, by have t := dvd_sub H h; rwa add_sub_cancel at t⟩ theorem dvd_add_iff_right (h : a ∣ b) : a ∣ c ↔ a ∣ b + c := by rw add_comm; exact dvd_add_iff_left h theorem two_dvd_bit1 : 2 ∣ bit1 a ↔ (2 : α) ∣ 1 := (dvd_add_iff_right (@two_dvd_bit0 _ _ a)).symm /-- Representation of a difference of two squares in a commutative ring as a product. -/ theorem mul_self_sub_mul_self (a b : α) : a * a - b * b = (a + b) * (a - b) := by rw [add_mul, mul_sub, mul_sub, mul_comm a b, sub_add_sub_cancel] lemma mul_self_sub_one (a : α) : a * a - 1 = (a + 1) * (a - 1) := by rw [← mul_self_sub_mul_self, mul_one] /-- An element a of a commutative ring divides the additive inverse of an element b iff a divides b. -/ @[simp] lemma dvd_neg (a b : α) : (a ∣ -b) ↔ (a ∣ b) := ⟨dvd_of_dvd_neg, dvd_neg_of_dvd⟩ /-- The additive inverse of an element a of a commutative ring divides another element b iff a divides b. -/ @[simp] lemma neg_dvd (a b : α) : (-a ∣ b) ↔ (a ∣ b) := ⟨dvd_of_neg_dvd, neg_dvd_of_dvd⟩ /-- If an element a divides another element c in a commutative ring, a divides the sum of another element b with c iff a divides b. -/ theorem dvd_add_left (h : a ∣ c) : a ∣ b + c ↔ a ∣ b := (dvd_add_iff_left h).symm /-- If an element a divides another element b in a commutative ring, a divides the sum of b and another element c iff a divides c. -/ theorem dvd_add_right (h : a ∣ b) : a ∣ b + c ↔ a ∣ c := (dvd_add_iff_right h).symm /-- An element a divides the sum a + b if and only if a divides b.-/ @[simp] lemma dvd_add_self_left {a b : α} : a ∣ a + b ↔ a ∣ b := dvd_add_right (dvd_refl a) /-- An element a divides the sum b + a if and only if a divides b.-/ @[simp] lemma dvd_add_self_right {a b : α} : a ∣ b + a ↔ a ∣ b := dvd_add_left (dvd_refl a) /-- Vieta's formula for a quadratic equation, relating the coefficients of the polynomial with its roots. This particular version states that if we have a root `x` of a monic quadratic polynomial, then there is another root `y` such that `x + y` is negative the `a_1` coefficient and `x * y` is the `a_0` coefficient. -/ lemma Vieta_formula_quadratic {b c x : α} (h : x * x - b * x + c = 0) : ∃ y : α, y * y - b * y + c = 0 ∧ x + y = b ∧ x * y = c := begin have : c = -(x * x - b * x) := (neg_eq_of_add_eq_zero h).symm, have : c = x * (b - x), by subst this; simp [mul_sub, mul_comm], refine ⟨b - x, _, by simp, by rw this⟩, rw [this, sub_add, ← sub_mul, sub_self] end lemma dvd_mul_sub_mul {k a b x y : α} (hab : k ∣ a - b) (hxy : k ∣ x - y) : k ∣ a * x - b * y := begin convert dvd_add (dvd_mul_of_dvd_right hxy a) (dvd_mul_of_dvd_left hab y), rw [mul_sub_left_distrib, mul_sub_right_distrib], simp only [sub_eq_add_neg, add_assoc, neg_add_cancel_left], end lemma dvd_iff_dvd_of_dvd_sub {a b c : α} (h : a ∣ (b - c)) : (a ∣ b ↔ a ∣ c) := begin split, { intro h', convert dvd_sub h' h, exact eq.symm (sub_sub_self b c) }, { intro h', convert dvd_add h h', exact eq_add_of_sub_eq rfl } end end comm_ring lemma succ_ne_self [ring α] [nontrivial α] (a : α) : a + 1 ≠ a := λ h, one_ne_zero ((add_right_inj a).mp (by simp [h])) lemma pred_ne_self [ring α] [nontrivial α] (a : α) : a - 1 ≠ a := λ h, one_ne_zero (neg_injective ((add_right_inj a).mp (by simpa [sub_eq_add_neg] using h))) /-- A domain is a ring with no zero divisors, i.e. satisfying the condition `a * b = 0 ↔ a = 0 ∨ b = 0`. Alternatively, a domain is an integral domain without assuming commutativity of multiplication. -/ @[protect_proj] class domain (α : Type u) extends ring α, nontrivial α := (eq_zero_or_eq_zero_of_mul_eq_zero : ∀ a b : α, a * b = 0 → a = 0 ∨ b = 0) section domain variable [domain α] @[priority 100] -- see Note [lower instance priority] instance domain.to_no_zero_divisors : no_zero_divisors α := ⟨domain.eq_zero_or_eq_zero_of_mul_eq_zero⟩ @[priority 100] -- see Note [lower instance priority] instance domain.to_cancel_monoid_with_zero : cancel_monoid_with_zero α := { mul_left_cancel_of_ne_zero := λ a b c ha, by { rw [← sub_eq_zero, ← mul_sub], simp [ha, sub_eq_zero] }, mul_right_cancel_of_ne_zero := λ a b c hb, by { rw [← sub_eq_zero, ← sub_mul], simp [hb, sub_eq_zero] }, .. (infer_instance : semiring α) } /-- Pullback a `domain` instance along an injective function. -/ protected def function.injective.domain [has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β] (f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) : domain β := { .. hf.ring f zero one add mul neg sub, .. pullback_nonzero f zero one, .. hf.no_zero_divisors f zero mul } end domain /-! ### Integral domains -/ /-- An integral domain is a commutative ring with no zero divisors, i.e. satisfying the condition `a * b = 0 ↔ a = 0 ∨ b = 0`. Alternatively, an integral domain is a domain with commutative multiplication. -/ @[protect_proj, ancestor comm_ring domain] class integral_domain (α : Type u) extends comm_ring α, domain α section integral_domain variables [integral_domain α] {a b c d e : α} @[priority 100] -- see Note [lower instance priority] instance integral_domain.to_comm_cancel_monoid_with_zero : comm_cancel_monoid_with_zero α := { ..comm_semiring.to_comm_monoid_with_zero, ..domain.to_cancel_monoid_with_zero } /-- Pullback an `integral_domain` instance along an injective function. -/ protected def function.injective.integral_domain [has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β] (f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) : integral_domain β := { .. hf.comm_ring f zero one add mul neg sub, .. hf.domain f zero one add mul neg sub } lemma mul_self_eq_mul_self_iff {a b : α} : a * a = b * b ↔ a = b ∨ a = -b := by rw [← sub_eq_zero, mul_self_sub_mul_self, mul_eq_zero, or_comm, sub_eq_zero, add_eq_zero_iff_eq_neg] lemma mul_self_eq_one_iff {a : α} : a * a = 1 ↔ a = 1 ∨ a = -1 := by rw [← mul_self_eq_mul_self_iff, one_mul] /-- In the unit group of an integral domain, a unit is its own inverse iff the unit is one or one's additive inverse. -/ lemma units.inv_eq_self_iff (u : units α) : u⁻¹ = u ↔ u = 1 ∨ u = -1 := by { rw inv_eq_iff_mul_eq_one, simp only [units.ext_iff], push_cast, exact mul_self_eq_one_iff } /-- Makes a ring homomorphism from an additive group homomorphism from a commutative ring to an integral domain that commutes with self multiplication, assumes that two is nonzero and one is sent to one. -/ def add_monoid_hom.mk_ring_hom_of_mul_self_of_two_ne_zero [comm_ring β] (f : β →+ α) (h : ∀ x, f (x * x) = f x * f x) (h_two : (2 : α) ≠ 0) (h_one : f 1 = 1) : β →+* α := { map_one' := h_one, map_mul' := begin intros x y, have hxy := h (x + y), rw [mul_add, add_mul, add_mul, f.map_add, f.map_add, f.map_add, f.map_add, h x, h y, add_mul, mul_add, mul_add, ← sub_eq_zero, add_comm, ← sub_sub, ← sub_sub, ← sub_sub, mul_comm y x, mul_comm (f y) (f x)] at hxy, simp only [add_assoc, add_sub_assoc, add_sub_cancel'_right] at hxy, rw [sub_sub, ← two_mul, ← add_sub_assoc, ← two_mul, ← mul_sub, mul_eq_zero, sub_eq_zero, or_iff_not_imp_left] at hxy, exact hxy h_two, end, ..f } @[simp] lemma add_monoid_hom.coe_fn_mk_ring_hom_of_mul_self_of_two_ne_zero [comm_ring β] (f : β →+ α) (h h_two h_one) : (f.mk_ring_hom_of_mul_self_of_two_ne_zero h h_two h_one : β → α) = f := rfl @[simp] lemma add_monoid_hom.coe_add_monoid_hom_mk_ring_hom_of_mul_self_of_two_ne_zero [comm_ring β] (f : β →+ α) (h h_two h_one) : (f.mk_ring_hom_of_mul_self_of_two_ne_zero h h_two h_one : β →+ α) = f := by {ext, simp} end integral_domain namespace ring variables {M₀ : Type*} [monoid_with_zero M₀] open_locale classical /-- Introduce a function `inverse` on a monoid with zero `M₀`, which sends `x` to `x⁻¹` if `x` is invertible and to `0` otherwise. This definition is somewhat ad hoc, but one needs a fully (rather than partially) defined inverse function for some purposes, including for calculus. Note that while this is in the `ring` namespace for brevity, it requires the weaker assumption `monoid_with_zero M₀` instead of `ring M₀`. -/ noncomputable def inverse : M₀ → M₀ := λ x, if h : is_unit x then (((classical.some h)⁻¹ : units M₀) : M₀) else 0 /-- By definition, if `x` is invertible then `inverse x = x⁻¹`. -/ @[simp] lemma inverse_unit (u : units M₀) : inverse (u : M₀) = (u⁻¹ : units M₀) := begin simp only [units.is_unit, inverse, dif_pos], exact units.inv_unique (classical.some_spec u.is_unit) end /-- By definition, if `x` is not invertible then `inverse x = 0`. -/ @[simp] lemma inverse_non_unit (x : M₀) (h : ¬(is_unit x)) : inverse x = 0 := dif_neg h end ring /-- A predicate to express that a ring is an integral domain. This is mainly useful because such a predicate does not contain data, and can therefore be easily transported along ring isomorphisms. -/ structure is_integral_domain (R : Type u) [ring R] extends nontrivial R : Prop := (mul_comm : ∀ (x y : R), x * y = y * x) (eq_zero_or_eq_zero_of_mul_eq_zero : ∀ x y : R, x * y = 0 → x = 0 ∨ y = 0) -- The linter does not recognize that is_integral_domain.to_nontrivial is a structure -- projection, disable it attribute [nolint def_lemma doc_blame] is_integral_domain.to_nontrivial /-- Every integral domain satisfies the predicate for integral domains. -/ lemma integral_domain.to_is_integral_domain (R : Type u) [integral_domain R] : is_integral_domain R := { .. (‹_› : integral_domain R) } /-- If a ring satisfies the predicate for integral domains, then it can be endowed with an `integral_domain` instance whose data is definitionally equal to the existing data. -/ def is_integral_domain.to_integral_domain (R : Type u) [ring R] (h : is_integral_domain R) : integral_domain R := { .. (‹_› : ring R), .. (‹_› : is_integral_domain R) } namespace semiconj_by @[simp] lemma add_right [distrib R] {a x y x' y' : R} (h : semiconj_by a x y) (h' : semiconj_by a x' y') : semiconj_by a (x + x') (y + y') := by simp only [semiconj_by, left_distrib, right_distrib, h.eq, h'.eq] @[simp] lemma add_left [distrib R] {a b x y : R} (ha : semiconj_by a x y) (hb : semiconj_by b x y) : semiconj_by (a + b) x y := by simp only [semiconj_by, left_distrib, right_distrib, ha.eq, hb.eq] variables [ring R] {a b x y x' y' : R} lemma neg_right (h : semiconj_by a x y) : semiconj_by a (-x) (-y) := by simp only [semiconj_by, h.eq, neg_mul_eq_neg_mul_symm, mul_neg_eq_neg_mul_symm] @[simp] lemma neg_right_iff : semiconj_by a (-x) (-y) ↔ semiconj_by a x y := ⟨λ h, neg_neg x ▸ neg_neg y ▸ h.neg_right, semiconj_by.neg_right⟩ lemma neg_left (h : semiconj_by a x y) : semiconj_by (-a) x y := by simp only [semiconj_by, h.eq, neg_mul_eq_neg_mul_symm, mul_neg_eq_neg_mul_symm] @[simp] lemma neg_left_iff : semiconj_by (-a) x y ↔ semiconj_by a x y := ⟨λ h, neg_neg a ▸ h.neg_left, semiconj_by.neg_left⟩ @[simp] lemma neg_one_right (a : R) : semiconj_by a (-1) (-1) := (one_right a).neg_right @[simp] lemma neg_one_left (x : R) : semiconj_by (-1) x x := (semiconj_by.one_left x).neg_left @[simp] lemma sub_right (h : semiconj_by a x y) (h' : semiconj_by a x' y') : semiconj_by a (x - x') (y - y') := by simpa only [sub_eq_add_neg] using h.add_right h'.neg_right @[simp] lemma sub_left (ha : semiconj_by a x y) (hb : semiconj_by b x y) : semiconj_by (a - b) x y := by simpa only [sub_eq_add_neg] using ha.add_left hb.neg_left end semiconj_by namespace commute @[simp] theorem add_right [distrib R] {a b c : R} : commute a b → commute a c → commute a (b + c) := semiconj_by.add_right @[simp] theorem add_left [distrib R] {a b c : R} : commute a c → commute b c → commute (a + b) c := semiconj_by.add_left lemma bit0_right [distrib R] {x y : R} (h : commute x y) : commute x (bit0 y) := h.add_right h lemma bit0_left [distrib R] {x y : R} (h : commute x y) : commute (bit0 x) y := h.add_left h lemma bit1_right [semiring R] {x y : R} (h : commute x y) : commute x (bit1 y) := h.bit0_right.add_right (commute.one_right x) lemma bit1_left [semiring R] {x y : R} (h : commute x y) : commute (bit1 x) y := h.bit0_left.add_left (commute.one_left y) variables [ring R] {a b c : R} theorem neg_right : commute a b → commute a (- b) := semiconj_by.neg_right @[simp] theorem neg_right_iff : commute a (-b) ↔ commute a b := semiconj_by.neg_right_iff theorem neg_left : commute a b → commute (- a) b := semiconj_by.neg_left @[simp] theorem neg_left_iff : commute (-a) b ↔ commute a b := semiconj_by.neg_left_iff @[simp] theorem neg_one_right (a : R) : commute a (-1) := semiconj_by.neg_one_right a @[simp] theorem neg_one_left (a : R): commute (-1) a := semiconj_by.neg_one_left a @[simp] theorem sub_right : commute a b → commute a c → commute a (b - c) := semiconj_by.sub_right @[simp] theorem sub_left : commute a c → commute b c → commute (a - b) c := semiconj_by.sub_left end commute
9412c4d7e06070facf2fc73d46b9838d100f4526
7b66d83f3b69dae0a3dfb684d7ebe5e9e3f3c913
/src/exercises_sources/thursday/afternoon/category_theory/exercise6.lean
0cd8f15e8db14ea1e116f7183307f644e0790d1a
[]
permissive
dpochekutov/lftcm2020
58a09e10f0e638075b97884d3c2612eb90296adb
cdfbf1ac089f21058e523db73f2476c9c98ed16a
refs/heads/master
1,669,226,265,076
1,594,629,725,000
1,594,629,725,000
279,213,346
1
0
MIT
1,594,622,757,000
1,594,615,843,000
null
UTF-8
Lean
false
false
1,316
lean
import algebra.category.Module.basic import linear_algebra.finite_dimensional /-! Every monomorphism in fdVec splits. This is not-so-secretly an exercise in using the linear algebra library -/ variables (𝕜 : Type) [field 𝕜] open category_theory abbreviation Vec := Module 𝕜 @[derive category] def fdVec := { V : Vec 𝕜 // finite_dimensional 𝕜 V } /-- We set up a `has_coe_to_sort` for `fdVec 𝕜`, sending an object directly to the underlying type. -/ instance : has_coe_to_sort (fdVec 𝕜) := { S := Type*, coe := λ V, V.val, } /-- Lean can already work out that this underlying type has the `module 𝕜` typeclass. -/ example (V : fdVec 𝕜) : module 𝕜 V := by apply_instance /-- But we need to tell it about the availability of the `finite_dimensional 𝕜` typeclass. -/ instance fdVec_finite_dimensional (V : fdVec 𝕜) : finite_dimensional 𝕜 V := V.property def exercise {X Y : fdVec 𝕜} (f : X ⟶ Y) [mono f] : split_mono f := -- We want to pick a basis of `X`, using `exists_is_basis` -- see that its image under `f` is linearly independent in `Y`, using `linear_independent.image_subtype` -- extend that set to a basis of `Y` using `exists_subset_is_basis` -- define a map back using `is_basis.constr` -- check it has the right property, using `is_basis.ext` sorry
ba8c898e70c303371e17c9c5fac96d9c8f7144c5
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/linear_algebra/matrix/zpow.lean
fb7ff4cc6d15f69d56d0aa53d6de8bdf39d04da1
[ "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
12,547
lean
/- Copyright (c) 2021 Yakov Pechersky. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yakov Pechersky -/ import linear_algebra.matrix.nonsingular_inverse /-! # Integer powers of square matrices > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file, we define integer power of matrices, relying on the nonsingular inverse definition for negative powers. ## Implementation details The main definition is a direct recursive call on the integer inductive type, as provided by the `div_inv_monoid.zpow` default implementation. The lemma names are taken from `algebra.group_with_zero.power`. ## Tags matrix inverse, matrix powers -/ open_locale matrix namespace matrix variables {n' : Type*} [decidable_eq n'] [fintype n'] {R : Type*} [comm_ring R] local notation `M` := matrix n' n' R noncomputable instance : div_inv_monoid M := { ..(show monoid M, by apply_instance), ..(show has_inv M, by apply_instance) } section nat_pow @[simp] theorem inv_pow' (A : M) (n : ℕ) : (A⁻¹) ^ n = (A ^ n)⁻¹ := begin induction n with n ih, { simp }, { rw [pow_succ A, mul_eq_mul, mul_inv_rev, ← ih, ← mul_eq_mul, ← pow_succ'] } end theorem pow_sub' (A : M) {m n : ℕ} (ha : is_unit A.det) (h : n ≤ m) : A ^ (m - n) = A ^ m ⬝ (A ^ n)⁻¹ := begin rw [←tsub_add_cancel_of_le h, pow_add, mul_eq_mul, matrix.mul_assoc, mul_nonsing_inv, tsub_add_cancel_of_le h, matrix.mul_one], simpa using ha.pow n end theorem pow_inv_comm' (A : M) (m n : ℕ) : (A⁻¹) ^ m ⬝ A ^ n = A ^ n ⬝ (A⁻¹) ^ m := begin induction n with n IH generalizing m, { simp }, cases m, { simp }, rcases nonsing_inv_cancel_or_zero A with ⟨h, h'⟩ | h, { calc A⁻¹ ^ (m + 1) ⬝ A ^ (n + 1) = A⁻¹ ^ m ⬝ (A⁻¹ ⬝ A) ⬝ A ^ n : by simp only [pow_succ' A⁻¹, pow_succ A, mul_eq_mul, matrix.mul_assoc] ... = A ^ n ⬝ A⁻¹ ^ m : by simp only [h, matrix.mul_one, matrix.one_mul, IH m] ... = A ^ n ⬝ (A ⬝ A⁻¹) ⬝ A⁻¹ ^ m : by simp only [h', matrix.mul_one, matrix.one_mul] ... = A ^ (n + 1) ⬝ A⁻¹ ^ (m + 1) : by simp only [pow_succ' A, pow_succ A⁻¹, mul_eq_mul, matrix.mul_assoc] }, { simp [h] } end end nat_pow section zpow open int @[simp] theorem one_zpow : ∀ (n : ℤ), (1 : M) ^ n = 1 | (n : ℕ) := by rw [zpow_coe_nat, one_pow] | -[1+ n] := by rw [zpow_neg_succ_of_nat, one_pow, inv_one] lemma zero_zpow : ∀ z : ℤ, z ≠ 0 → (0 : M) ^ z = 0 | (n : ℕ) h := by { rw [zpow_coe_nat, zero_pow], refine lt_of_le_of_ne n.zero_le (ne.symm _), simpa using h } | -[1+n] h := by simp [zero_pow n.zero_lt_succ] lemma zero_zpow_eq (n : ℤ) : (0 : M) ^ n = if n = 0 then 1 else 0 := begin split_ifs with h, { rw [h, zpow_zero] }, { rw [zero_zpow _ h] } end theorem inv_zpow (A : M) : ∀n:ℤ, A⁻¹ ^ n = (A ^ n)⁻¹ | (n : ℕ) := by rw [zpow_coe_nat, zpow_coe_nat, inv_pow'] | -[1+ n] := by rw [zpow_neg_succ_of_nat, zpow_neg_succ_of_nat, inv_pow'] @[simp] lemma zpow_neg_one (A : M) : A ^ (-1 : ℤ) = A⁻¹ := begin convert div_inv_monoid.zpow_neg' 0 A, simp only [zpow_one, int.coe_nat_zero, int.coe_nat_succ, zpow_eq_pow, zero_add] end theorem zpow_coe_nat (A : M) (n : ℕ) : A ^ (n : ℤ) = (A ^ n) := zpow_coe_nat _ _ @[simp] theorem zpow_neg_coe_nat (A : M) (n : ℕ) : A ^ (-n : ℤ) = (A ^ n)⁻¹ := begin cases n, { simp }, { exact div_inv_monoid.zpow_neg' _ _ } end lemma _root_.is_unit.det_zpow {A : M} (h : is_unit A.det) (n : ℤ) : is_unit (A ^ n).det := begin cases n, { simpa using h.pow n }, { simpa using h.pow n.succ } end lemma is_unit_det_zpow_iff {A : M} {z : ℤ} : is_unit (A ^ z).det ↔ is_unit A.det ∨ z = 0 := begin induction z using int.induction_on with z IH z IH, { simp }, { rw [←int.coe_nat_succ, zpow_coe_nat, det_pow, is_unit_pow_succ_iff, ←int.coe_nat_zero, int.coe_nat_eq_coe_nat_iff], simp }, { rw [←neg_add', ←int.coe_nat_succ, zpow_neg_coe_nat, is_unit_nonsing_inv_det_iff, det_pow, is_unit_pow_succ_iff, neg_eq_zero, ←int.coe_nat_zero, int.coe_nat_eq_coe_nat_iff], simp } end theorem zpow_neg {A : M} (h : is_unit A.det) : ∀ (n : ℤ), A ^ -n = (A ^ n)⁻¹ | (n : ℕ) := zpow_neg_coe_nat _ _ | -[1+ n] := by { rw [zpow_neg_succ_of_nat, neg_neg_of_nat_succ, of_nat_eq_coe, zpow_coe_nat, nonsing_inv_nonsing_inv], rw det_pow, exact h.pow _ } lemma inv_zpow' {A : M} (h : is_unit A.det) (n : ℤ) : (A ⁻¹) ^ n = A ^ (-n) := by rw [zpow_neg h, inv_zpow] lemma zpow_add_one {A : M} (h : is_unit A.det) : ∀ n : ℤ, A ^ (n + 1) = A ^ n * A | (n : ℕ) := by simp only [← nat.cast_succ, pow_succ', zpow_coe_nat] | -((n : ℕ) + 1) := calc A ^ (-(n + 1) + 1 : ℤ) = (A ^ n)⁻¹ : by rw [neg_add, neg_add_cancel_right, zpow_neg h, zpow_coe_nat] ... = (A ⬝ A ^ n)⁻¹ ⬝ A : by rw [mul_inv_rev, matrix.mul_assoc, nonsing_inv_mul _ h, matrix.mul_one] ... = A ^ -(n + 1 : ℤ) * A : by rw [zpow_neg h, ← int.coe_nat_succ, zpow_coe_nat, pow_succ, mul_eq_mul, mul_eq_mul] lemma zpow_sub_one {A : M} (h : is_unit A.det) (n : ℤ) : A ^ (n - 1) = A ^ n * A⁻¹ := calc A ^ (n - 1) = A ^ (n - 1) * A * A⁻¹ : by rw [mul_assoc, mul_eq_mul A, mul_nonsing_inv _ h, mul_one] ... = A^n * A⁻¹ : by rw [← zpow_add_one h, sub_add_cancel] lemma zpow_add {A : M} (ha : is_unit A.det) (m n : ℤ) : A ^ (m + n) = A ^ m * A ^ n := begin induction n using int.induction_on with n ihn n ihn, case hz : { simp }, { simp only [← add_assoc, zpow_add_one ha, ihn, mul_assoc] }, { rw [zpow_sub_one ha, ← mul_assoc, ← ihn, ← zpow_sub_one ha, add_sub_assoc] } end lemma zpow_add_of_nonpos {A : M} {m n : ℤ} (hm : m ≤ 0) (hn : n ≤ 0) : A ^ (m + n) = A ^ m * A ^ n := begin rcases nonsing_inv_cancel_or_zero A with ⟨h, h'⟩ | h, { exact zpow_add (is_unit_det_of_left_inverse h) m n }, { obtain ⟨k, rfl⟩ := exists_eq_neg_of_nat hm, obtain ⟨l, rfl⟩ := exists_eq_neg_of_nat hn, simp_rw [←neg_add, ←int.coe_nat_add, zpow_neg_coe_nat, ←inv_pow', h, pow_add] } end lemma zpow_add_of_nonneg {A : M} {m n : ℤ} (hm : 0 ≤ m) (hn : 0 ≤ n) : A ^ (m + n) = A ^ m * A ^ n := begin obtain ⟨k, rfl⟩ := eq_coe_of_zero_le hm, obtain ⟨l, rfl⟩ := eq_coe_of_zero_le hn, rw [←int.coe_nat_add, zpow_coe_nat, zpow_coe_nat, zpow_coe_nat, pow_add], end theorem zpow_one_add {A : M} (h : is_unit A.det) (i : ℤ) : A ^ (1 + i) = A * A ^ i := by rw [zpow_add h, zpow_one] theorem semiconj_by.zpow_right {A X Y : M} (hx : is_unit X.det) (hy : is_unit Y.det) (h : semiconj_by A X Y) : ∀ m : ℤ, semiconj_by A (X^m) (Y^m) | (n : ℕ) := by simp [h.pow_right n] | -[1+n] := begin have hx' : is_unit (X ^ n.succ).det, { rw det_pow, exact hx.pow n.succ }, have hy' : is_unit (Y ^ n.succ).det, { rw det_pow, exact hy.pow n.succ }, rw [zpow_neg_succ_of_nat, zpow_neg_succ_of_nat, nonsing_inv_apply _ hx', nonsing_inv_apply _ hy', semiconj_by], refine (is_regular_of_is_left_regular_det hy'.is_regular.left).left _, rw [←mul_assoc, ←(h.pow_right n.succ).eq, mul_assoc, mul_eq_mul (X ^ _), mul_smul, mul_adjugate, mul_eq_mul, mul_eq_mul, mul_eq_mul, ←matrix.mul_assoc, mul_smul (Y ^ _) (↑(hy'.unit)⁻¹ : R), mul_adjugate, smul_smul, smul_smul, hx'.coe_inv_mul, hy'.coe_inv_mul, one_smul, matrix.mul_one, matrix.one_mul], end theorem commute.zpow_right {A B : M} (h : commute A B) (m : ℤ) : commute A (B^m) := begin rcases nonsing_inv_cancel_or_zero B with ⟨hB, hB'⟩ | hB, { refine semiconj_by.zpow_right _ _ h _; exact is_unit_det_of_left_inverse hB }, { cases m, { simpa using h.pow_right _ }, { simp [←inv_pow', hB] } } end theorem commute.zpow_left {A B : M} (h : commute A B) (m : ℤ) : commute (A^m) B := (commute.zpow_right h.symm m).symm theorem commute.zpow_zpow {A B : M} (h : commute A B) (m n : ℤ) : commute (A^m) (B^n) := commute.zpow_right (commute.zpow_left h _) _ theorem commute.zpow_self (A : M) (n : ℤ) : commute (A^n) A := commute.zpow_left (commute.refl A) _ theorem commute.self_zpow (A : M) (n : ℤ) : commute A (A^n) := commute.zpow_right (commute.refl A) _ theorem commute.zpow_zpow_self (A : M) (m n : ℤ) : commute (A^m) (A^n) := commute.zpow_zpow (commute.refl A) _ _ theorem zpow_bit0 (A : M) (n : ℤ) : A ^ bit0 n = A ^ n * A ^ n := begin cases le_total 0 n with nonneg nonpos, { exact zpow_add_of_nonneg nonneg nonneg }, { exact zpow_add_of_nonpos nonpos nonpos } end lemma zpow_add_one_of_ne_neg_one {A : M} : ∀ (n : ℤ), n ≠ -1 → A ^ (n + 1) = A ^ n * A | (n : ℕ) _ := by simp only [pow_succ', ← nat.cast_succ, zpow_coe_nat] | (-1) h := absurd rfl h | (-((n : ℕ) + 2)) _ := begin rcases nonsing_inv_cancel_or_zero A with ⟨h, h'⟩ | h, { apply zpow_add_one (is_unit_det_of_left_inverse h) }, { show A ^ (-((n + 1 : ℕ) : ℤ)) = A ^ -((n + 2 : ℕ) : ℤ) * A, simp_rw [zpow_neg_coe_nat, ←inv_pow', h, zero_pow nat.succ_pos', zero_mul] } end theorem zpow_bit1 (A : M) (n : ℤ) : A ^ bit1 n = A ^ n * A ^ n * A := begin rw [bit1, zpow_add_one_of_ne_neg_one, zpow_bit0], intro h, simpa using congr_arg bodd h end theorem zpow_mul (A : M) (h : is_unit A.det) : ∀ m n : ℤ, A ^ (m * n) = (A ^ m) ^ n | (m : ℕ) (n : ℕ) := by rw [zpow_coe_nat, zpow_coe_nat, ← pow_mul, ← zpow_coe_nat, int.coe_nat_mul] | (m : ℕ) -[1+ n] := by rw [zpow_coe_nat, zpow_neg_succ_of_nat, ← pow_mul, coe_nat_mul_neg_succ, ←int.coe_nat_mul, zpow_neg_coe_nat] | -[1+ m] (n : ℕ) := by rw [zpow_coe_nat, zpow_neg_succ_of_nat, ← inv_pow', ← pow_mul, neg_succ_mul_coe_nat, ←int.coe_nat_mul, zpow_neg_coe_nat, inv_pow'] | -[1+ m] -[1+ n] := by { rw [zpow_neg_succ_of_nat, zpow_neg_succ_of_nat, neg_succ_mul_neg_succ, ←int.coe_nat_mul, zpow_coe_nat, inv_pow', ←pow_mul, nonsing_inv_nonsing_inv], rw det_pow, exact h.pow _ } theorem zpow_mul' (A : M) (h : is_unit A.det) (m n : ℤ) : A ^ (m * n) = (A ^ n) ^ m := by rw [mul_comm, zpow_mul _ h] @[simp, norm_cast] lemma coe_units_zpow (u : Mˣ) : ∀ (n : ℤ), ((u ^ n : Mˣ) : M) = u ^ n | (n : ℕ) := by rw [_root_.zpow_coe_nat, zpow_coe_nat, units.coe_pow] | -[1+k] := by rw [zpow_neg_succ_of_nat, zpow_neg_succ_of_nat, ←inv_pow, u⁻¹.coe_pow, ←inv_pow', coe_units_inv] lemma zpow_ne_zero_of_is_unit_det [nonempty n'] [nontrivial R] {A : M} (ha : is_unit A.det) (z : ℤ) : A ^ z ≠ 0 := begin have := ha.det_zpow z, contrapose! this, rw [this, det_zero ‹_›], exact not_is_unit_zero end lemma zpow_sub {A : M} (ha : is_unit A.det) (z1 z2 : ℤ) : A ^ (z1 - z2) = A ^ z1 / A ^ z2 := by rw [sub_eq_add_neg, zpow_add ha, zpow_neg ha, div_eq_mul_inv] lemma commute.mul_zpow {A B : M} (h : commute A B) : ∀ (i : ℤ), (A * B) ^ i = (A ^ i) * (B ^ i) | (n : ℕ) := by simp [h.mul_pow n, -mul_eq_mul] | -[1+n] := by rw [zpow_neg_succ_of_nat, zpow_neg_succ_of_nat, zpow_neg_succ_of_nat, mul_eq_mul (_⁻¹), ←mul_inv_rev, ←mul_eq_mul, h.mul_pow n.succ, (h.pow_pow _ _).eq] theorem zpow_bit0' (A : M) (n : ℤ) : A ^ bit0 n = (A * A) ^ n := (zpow_bit0 A n).trans (commute.mul_zpow (commute.refl A) n).symm theorem zpow_bit1' (A : M) (n : ℤ) : A ^ bit1 n = (A * A) ^ n * A := by rw [zpow_bit1, commute.mul_zpow (commute.refl A)] theorem zpow_neg_mul_zpow_self (n : ℤ) {A : M} (h : is_unit A.det) : A ^ (-n) * A ^ n = 1 := by rw [zpow_neg h, mul_eq_mul, nonsing_inv_mul _ (h.det_zpow _)] theorem one_div_pow {A : M} (n : ℕ) : (1 / A) ^ n = 1 / A ^ n := by simp only [one_div, inv_pow'] theorem one_div_zpow {A : M} (n : ℤ) : (1 / A) ^ n = 1 / A ^ n := by simp only [one_div, inv_zpow] @[simp] theorem transpose_zpow (A : M) : ∀ (n : ℤ), (A ^ n)ᵀ = Aᵀ ^ n | (n : ℕ) := by rw [zpow_coe_nat, zpow_coe_nat, transpose_pow] | -[1+ n] := by rw [zpow_neg_succ_of_nat, zpow_neg_succ_of_nat, transpose_nonsing_inv, transpose_pow] @[simp] theorem conj_transpose_zpow [star_ring R] (A : M) : ∀ (n : ℤ), (A ^ n)ᴴ = Aᴴ ^ n | (n : ℕ) := by rw [zpow_coe_nat, zpow_coe_nat, conj_transpose_pow] | -[1+ n] := by rw [zpow_neg_succ_of_nat, zpow_neg_succ_of_nat, conj_transpose_nonsing_inv, conj_transpose_pow] end zpow end matrix
85e417b15a69d869d55ed47d00ba5dfe53aa935d
44023683920a51f2416cf51eeab3fc51c3ff0765
/tests/lean/run/rb_map1.lean
2c1ce172f11a8765281df63650b2dac15cc7063e
[ "Apache-2.0" ]
permissive
pirocks/lean
e6e3e3fd20c5e7877f7efc3b50e5e20271e8d0cf
368f17d0b1392a5a72c9eb974f15b14462cc1475
refs/heads/master
1,620,671,385,768
1,516,152,564,000
1,516,152,564,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
742
lean
import system.io open io section open native.nat_map #eval size (insert (insert (mk nat) 10 20) 10 21) meta definition m := (insert (insert (insert (mk nat) 10 20) 5 50) 10 21) #eval find m 10 #eval find m 5 #eval find m 8 #eval contains m 5 #eval contains m 8 open list meta definition m2 := of_list [((1:nat), "one"), (2, "two"), (3, "three")] #eval size m2 #eval find m2 1 #eval find m2 4 #eval find m2 3 section variable [io.interface] #eval do pp m2, put_str "\n" end #eval m2 end section open native.rb_map -- Mapping from (nat × nat) → nat meta definition m3 := insert (insert (mk (nat × nat) nat) (1, 2) 3) (2, 2) 4 #eval find m3 (1, 2) #eval find m3 (2, 1) #eval find m3 (2, 2) variable [io.interface] #eval pp m3 end
1c8a1c8e243a97cebeef76d9e166327ff55848e1
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/lua8.lean
7713ad357afb1653800cdc49b9ce7261345c0218
[ "Apache-2.0" ]
permissive
codyroux/lean0.1
1ce92751d664aacff0529e139083304a7bbc8a71
0dc6fb974aa85ed6f305a2f4b10a53a44ee5f0ef
refs/heads/master
1,610,830,535,062
1,402,150,480,000
1,402,150,480,000
19,588,851
2
0
null
null
null
null
UTF-8
Lean
false
false
242
lean
import Int. variable x : Int (* local env = get_environment() ty_x = env:type_check(Const("x")) c = context() c = context(c, "x", ty_x) c = context(c, "y", ty_x) print(c) o = env:find_object("x") print(o) o = env:find_object("y") print(o) *)
46f475ae79bc3ea7a592d7c1231c73233143c802
4bcaca5dc83d49803f72b7b5920b75b6e7d9de2d
/src/Lean/Elab/PreDefinition/Main.lean
1559c6e987e5386d3f24c5ef4e5557425dfdf24b
[ "Apache-2.0" ]
permissive
subfish-zhou/leanprover-zh_CN.github.io
30b9fba9bd790720bd95764e61ae796697d2f603
8b2985d4a3d458ceda9361ac454c28168d920d3f
refs/heads/master
1,689,709,967,820
1,632,503,056,000
1,632,503,056,000
409,962,097
1
0
null
null
null
null
UTF-8
Lean
false
false
3,847
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Elab.PreDefinition.Basic import Lean.Elab.PreDefinition.Structural import Lean.Elab.PreDefinition.WF namespace Lean.Elab open Meta open Term private def addAndCompilePartial (preDefs : Array PreDefinition) : TermElabM Unit := do for preDef in preDefs do trace[Elab.definition] "processing {preDef.declName}" forallTelescope preDef.type fun xs type => do let inh ← liftM $ mkInhabitantFor preDef.declName xs type trace[Elab.definition] "inhabitant for {preDef.declName}" addNonRec { preDef with kind := DefKind.«opaque», value := inh } addAndCompilePartialRec preDefs private def isNonRecursive (preDef : PreDefinition) : Bool := Option.isNone $ preDef.value.find? fun | Expr.const declName _ _ => preDef.declName == declName | _ => false private def partitionPreDefs (preDefs : Array PreDefinition) : Array (Array PreDefinition) := let getPreDef := fun declName => (preDefs.find? fun preDef => preDef.declName == declName).get! let vertices := preDefs.toList.map (·.declName) let successorsOf := fun declName => (getPreDef declName).value.foldConsts [] fun declName successors => if preDefs.any fun preDef => preDef.declName == declName then declName :: successors else successors let sccs := SCC.scc vertices successorsOf sccs.toArray.map fun scc => scc.toArray.map getPreDef private def collectMVarsAtPreDef (preDef : PreDefinition) : StateRefT CollectMVars.State MetaM Unit := do collectMVars preDef.value collectMVars preDef.type private def getMVarsAtPreDef (preDef : PreDefinition) : MetaM (Array MVarId) := do let (_, s) ← (collectMVarsAtPreDef preDef).run {} pure s.result private def ensureNoUnassignedMVarsAtPreDef (preDef : PreDefinition) : TermElabM PreDefinition := do let pendingMVarIds ← getMVarsAtPreDef preDef if (← logUnassignedUsingErrorInfos pendingMVarIds) then let preDef := { preDef with value := (← mkSorry preDef.type (synthetic := true)) } if (← getMVarsAtPreDef preDef).isEmpty then return preDef else throwAbortCommand else return preDef def addPreDefinitions (preDefs : Array PreDefinition) (terminationBy? : Option Syntax) : TermElabM Unit := withLCtx {} {} do for preDef in preDefs do trace[Elab.definition.body] "{preDef.declName} : {preDef.type} :=\n{preDef.value}" let preDefs ← preDefs.mapM ensureNoUnassignedMVarsAtPreDef let cliques ← partitionPreDefs preDefs let mut terminationBy ← liftMacroM <| WF.expandTerminationBy terminationBy? (cliques.map fun ds => ds.map (·.declName)) for preDefs in cliques do trace[Elab.definition.scc] "{preDefs.map (·.declName)}" if preDefs.size == 1 && isNonRecursive preDefs[0] then let preDef := preDefs[0] if preDef.modifiers.isNoncomputable then addNonRec preDef else addAndCompileNonRec preDef else if preDefs.any (·.modifiers.isUnsafe) then addAndCompileUnsafe preDefs else if preDefs.any (·.modifiers.isPartial) then addAndCompilePartial preDefs else terminationBy ← withRef (preDefs[0].ref) <| mapError (orelseMergeErrors (structuralRecursion preDefs *> pure terminationBy) (wfRecursion preDefs terminationBy)) (fun msg => let preDefMsgs := preDefs.toList.map (MessageData.ofExpr $ mkConst ·.declName) m!"fail to show termination for{indentD (MessageData.joinSep preDefMsgs Format.line)}\nwith errors\n{msg}") liftMacroM <| terminationBy.ensureIsEmpty builtin_initialize registerTraceClass `Elab.definition.body registerTraceClass `Elab.definition.scc end Lean.Elab
d31e6f31cefa980dba1d89ffedcd74042c394e60
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/lake/test/precompileArgs/lakefile.lean
b52d6f25b8d1cb3121702141446bb67fbd4ec44b
[ "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
144
lean
import Lake open Lake DSL package precompileArgs @[default_target] lean_lib Foo { precompileModules := true moreLinkArgs := #["-lBaz"] }
e9f91f982c65d02f76fb3a5934b6e788500ebb06
f3a5af2927397cf346ec0e24312bfff077f00425
/src/tactic/clear_leaks.lean
b880f6bb40c5e9c240e30a84f65a09b41ff241da
[ "Apache-2.0" ]
permissive
ImperialCollegeLondon/natural_number_game
05c39e1586408cfb563d1a12e1085a90726ab655
f29b6c2884299fc63fdfc81ae5d7daaa3219f9fd
refs/heads/master
1,688,570,964,990
1,636,908,242,000
1,636,908,242,000
195,403,790
277
84
Apache-2.0
1,694,547,955,000
1,562,328,792,000
Lean
UTF-8
Lean
false
false
33
lean
-- not sure this is ever imported
7c2b741f37c95746d366546d6c709e871e84f6db
969dbdfed67fda40a6f5a2b4f8c4a3c7dc01e0fb
/src/algebra/category/Semigroup/basic.lean
94ffe60cb45a210f026abaeb8e282f0c2e2a071c
[ "Apache-2.0" ]
permissive
SAAluthwela/mathlib
62044349d72dd63983a8500214736aa7779634d3
83a4b8b990907291421de54a78988c024dc8a552
refs/heads/master
1,679,433,873,417
1,615,998,031,000
1,615,998,031,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,324
lean
/- Copyright (c) 2021 Julian Kuelshammer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Julian Kuelshammer (heavily based on `Mon.basic` by Scott Morrison) -/ import category_theory.concrete_category.bundled_hom import category_theory.concrete_category.reflects_isomorphisms import algebra.pempty_instances /-! # Category instances for has_mul, has_add, semigroup and add_semigroup We introduce the bundled categories: * `Magma` * `AddMagma` * `Semigroup` * `AddSemigroup` along with the relevant forgetful functors between them. ## TODO * Limits in these categories * free/forgetful adjunctions -/ universes u v open category_theory /-- The category of magmas and magma morphisms. -/ @[to_additive AddMagma] def Magma : Type (u+1) := bundled has_mul /-- The category of additive magmas and additive magma morphisms. -/ add_decl_doc AddMagma namespace Magma @[to_additive] instance bundled_hom : bundled_hom @mul_hom := ⟨@mul_hom.to_fun, @mul_hom.id, @mul_hom.comp, @mul_hom.coe_inj⟩ attribute [derive [has_coe_to_sort, large_category, concrete_category]] Magma AddMagma /-- Construct a bundled `Magma` from the underlying type and typeclass. -/ @[to_additive] def of (M : Type u) [has_mul M] : Magma := bundled.of M /-- Construct a bundled `Magma` from the underlying type and typeclass. -/ add_decl_doc AddMagma.of @[to_additive] instance : inhabited Magma := ⟨Magma.of pempty⟩ @[to_additive] instance (M : Magma) : has_mul M := M.str @[simp, to_additive] lemma coe_of (R : Type u) [has_mul R] : (Magma.of R : Type u) = R := rfl end Magma /-- The category of semigroups and semigroup morphisms. -/ @[to_additive AddSemigroup] def Semigroup : Type (u+1) := bundled semigroup /-- The category of additive semigroups and semigroup morphisms. -/ add_decl_doc AddSemigroup namespace Semigroup @[to_additive] instance : bundled_hom.parent_projection semigroup.to_has_mul := ⟨⟩ attribute [derive [has_coe_to_sort, large_category, concrete_category]] Semigroup AddSemigroup /-- Construct a bundled `Semigroup` from the underlying type and typeclass. -/ @[to_additive] def of (M : Type u) [semigroup M] : Semigroup := bundled.of M /-- Construct a bundled `AddSemigroup` from the underlying type and typeclass. -/ add_decl_doc AddSemigroup.of @[to_additive] instance : inhabited Semigroup := ⟨Semigroup.of pempty⟩ @[to_additive] instance (M : Semigroup) : semigroup M := M.str @[simp, to_additive] lemma coe_of (R : Type u) [semigroup R] : (Semigroup.of R : Type u) = R := rfl @[to_additive has_forget_to_AddMagma] instance has_forget_to_Magma : has_forget₂ Semigroup Magma := bundled_hom.forget₂ _ _ end Semigroup variables {X Y : Type u} section variables [has_mul X] [has_mul Y] /-- Build an isomorphism in the category `Magma` from a `mul_equiv` between `has_mul`s. -/ @[simps, to_additive add_equiv.to_AddMagma_iso "Build an isomorphism in the category `AddMagma` from an `add_equiv` between `has_add`s."] def mul_equiv.to_Magma_iso (e : X ≃* Y) : Magma.of X ≅ Magma.of Y := { hom := e.to_mul_hom, inv := e.symm.to_mul_hom } end section variables [semigroup X] [semigroup Y] /-- Build an isomorphism in the category `Semigroup` from a `mul_equiv` between `semigroup`s. -/ @[simps, to_additive add_equiv.to_AddSemigroup_iso "Build an isomorphism in the category `AddSemigroup` from an `add_equiv` between `add_semigroup`s."] def mul_equiv.to_Semigroup_iso (e : X ≃* Y) : Semigroup.of X ≅ Semigroup.of Y := { hom := e.to_mul_hom, inv := e.symm.to_mul_hom } end namespace category_theory.iso /-- Build a `mul_equiv` from an isomorphism in the category `Magma`. -/ @[to_additive AddMagma_iso_to_add_equiv "Build an `add_equiv` from an isomorphism in the category `AddMagma`."] def Magma_iso_to_mul_equiv {X Y : Magma} (i : X ≅ Y) : X ≃* Y := { to_fun := i.hom, inv_fun := i.inv, left_inv := begin rw function.left_inverse, simp end, right_inv := begin rw function.right_inverse, rw function.left_inverse, simp end, map_mul' := by simp } /-- Build a `mul_equiv` from an isomorphism in the category `Semigroup`. -/ @[to_additive "Build an `add_equiv` from an isomorphism in the category `AddSemigroup`."] def Semigroup_iso_to_mul_equiv {X Y : Semigroup} (i : X ≅ Y) : X ≃* Y := { to_fun := i.hom, inv_fun := i.inv, left_inv := begin rw function.left_inverse, simp end, right_inv := begin rw function.right_inverse, rw function.left_inverse, simp end, map_mul' := by simp } end category_theory.iso /-- multiplicative equivalences between `has_mul`s are the same as (isomorphic to) isomorphisms in `Magma` -/ @[to_additive add_equiv_iso_AddMagma_iso "additive equivalences between `has_add`s are the same as (isomorphic to) isomorphisms in `AddMagma`"] def mul_equiv_iso_Magma_iso {X Y : Type u} [has_mul X] [has_mul Y] : (X ≃* Y) ≅ (Magma.of X ≅ Magma.of Y) := { hom := λ e, e.to_Magma_iso, inv := λ i, i.Magma_iso_to_mul_equiv } /-- multiplicative equivalences between `semigroup`s are the same as (isomorphic to) isomorphisms in `Semigroup` -/ @[to_additive add_equiv_iso_AddSemigroup_iso "additive equivalences between `add_semigroup`s are the same as (isomorphic to) isomorphisms in `AddSemigroup`"] def mul_equiv_iso_Semigroup_iso {X Y : Type u} [semigroup X] [semigroup Y] : (X ≃* Y) ≅ (Semigroup.of X ≅ Semigroup.of Y) := { hom := λ e, e.to_Semigroup_iso, inv := λ i, i.Semigroup_iso_to_mul_equiv } @[to_additive] instance Magma.forget_reflects_isos : reflects_isomorphisms (forget Magma.{u}) := { reflects := λ X Y f _, begin resetI, let i := as_iso ((forget Magma).map f), let e : X ≃* Y := { ..f, ..i.to_equiv }, exact is_iso.of_iso e.to_Magma_iso, end } @[to_additive] instance Semigroup.forget_reflects_isos : reflects_isomorphisms (forget Semigroup.{u}) := { reflects := λ X Y f _, begin resetI, let i := as_iso ((forget Semigroup).map f), let e : X ≃* Y := { ..f, ..i.to_equiv }, exact is_iso.of_iso e.to_Semigroup_iso, end } /-! Once we've shown that the forgetful functors to type reflect isomorphisms, we automatically obtain that the `forget₂` functors between our concrete categories reflect isomorphisms. -/ example : reflects_isomorphisms (forget₂ Semigroup Magma) := by apply_instance
5a7a3cea4e0d2111b309f1d87c30c664c7a150f5
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/hott/types/pi.hlean
32affc0866b566ffbdbb53196de8e393db753b06
[ "Apache-2.0" ]
permissive
jroesch/lean
30ef0860fa905d35b9ad6f76de1a4f65c9af6871
3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2
refs/heads/master
1,586,090,835,348
1,455,142,203,000
1,455,142,277,000
51,536,958
1
0
null
1,455,215,811,000
1,455,215,811,000
null
UTF-8
Lean
false
false
11,776
hlean
/- Copyright (c) 2014-15 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn Partially ported from Coq HoTT Theorems about pi-types (dependent function spaces) -/ import types.sigma arity open eq equiv is_equiv funext sigma unit bool is_trunc prod namespace pi variables {A A' : Type} {B : A → Type} {B' : A' → Type} {C : Πa, B a → Type} {D : Πa b, C a b → Type} {a a' a'' : A} {b b₁ b₂ : B a} {b' : B a'} {b'' : B a''} {f g : Πa, B a} /- Paths -/ /- Paths [p : f ≈ g] in a function type [Πx:X, P x] are equivalent to functions taking values in path types, [H : Πx:X, f x ≈ g x], or concisely, [H : f ~ g]. This equivalence, however, is just the combination of [apd10] and function extensionality [funext], and as such, [eq_of_homotopy] Now we show how these things compute. -/ definition apd10_eq_of_homotopy (h : f ~ g) : apd10 (eq_of_homotopy h) ~ h := apd10 (right_inv apd10 h) definition eq_of_homotopy_eta (p : f = g) : eq_of_homotopy (apd10 p) = p := left_inv apd10 p definition eq_of_homotopy_idp (f : Πa, B a) : eq_of_homotopy (λx : A, refl (f x)) = refl f := !eq_of_homotopy_eta /- The identification of the path space of a dependent function space, up to equivalence, is of course just funext. -/ definition eq_equiv_homotopy (f g : Πx, B x) : (f = g) ≃ (f ~ g) := equiv.mk apd10 _ definition pi_eq_equiv (f g : Πx, B x) : (f = g) ≃ (f ~ g) := !eq_equiv_homotopy definition is_equiv_eq_of_homotopy (f g : Πx, B x) : is_equiv (eq_of_homotopy : f ~ g → f = g) := _ definition homotopy_equiv_eq (f g : Πx, B x) : (f ~ g) ≃ (f = g) := equiv.mk eq_of_homotopy _ /- Transport -/ definition pi_transport (p : a = a') (f : Π(b : B a), C a b) : (transport (λa, Π(b : B a), C a b) p f) ~ (λb, !tr_inv_tr ▸ (p ▸D (f (p⁻¹ ▸ b)))) := by induction p; reflexivity /- A special case of [transport_pi] where the type [B] does not depend on [A], and so it is just a fixed type [B]. -/ definition pi_transport_constant {C : A → A' → Type} (p : a = a') (f : Π(b : A'), C a b) (b : A') : (transport _ p f) b = p ▸ (f b) := by induction p; reflexivity /- Pathovers -/ definition pi_pathover {f : Πb, C a b} {g : Πb', C a' b'} {p : a = a'} (r : Π(b : B a) (b' : B a') (q : b =[p] b'), f b =[apo011 C p q] g b') : f =[p] g := begin cases p, apply pathover_idp_of_eq, apply eq_of_homotopy, intro b, apply eq_of_pathover_idp, apply r end definition pi_pathover_left {f : Πb, C a b} {g : Πb', C a' b'} {p : a = a'} (r : Π(b : B a), f b =[apo011 C p !pathover_tr] g (p ▸ b)) : f =[p] g := begin cases p, apply pathover_idp_of_eq, apply eq_of_homotopy, intro b, apply eq_of_pathover_idp, apply r end definition pi_pathover_right {f : Πb, C a b} {g : Πb', C a' b'} {p : a = a'} (r : Π(b' : B a'), f (p⁻¹ ▸ b') =[apo011 C p !tr_pathover] g b') : f =[p] g := begin cases p, apply pathover_idp_of_eq, apply eq_of_homotopy, intro b, apply eq_of_pathover_idp, apply r end definition pi_pathover_constant {C : A → A' → Type} {f : Π(b : A'), C a b} {g : Π(b : A'), C a' b} {p : a = a'} (r : Π(b : A'), f b =[p] g b) : f =[p] g := begin cases p, apply pathover_idp_of_eq, apply eq_of_homotopy, intro b, exact eq_of_pathover_idp (r b), end -- a version where C is uncurried, but where the conclusion of r is still a proper pathover -- instead of a heterogenous equality definition pi_pathover' {C : (Σa, B a) → Type} {f : Πb, C ⟨a, b⟩} {g : Πb', C ⟨a', b'⟩} {p : a = a'} (r : Π(b : B a) (b' : B a') (q : b =[p] b'), f b =[dpair_eq_dpair p q] g b') : f =[p] g := begin cases p, apply pathover_idp_of_eq, apply eq_of_homotopy, intro b, apply (@eq_of_pathover_idp _ C), exact (r b b (pathover.idpatho b)), end definition pi_pathover_left' {C : (Σa, B a) → Type} {f : Πb, C ⟨a, b⟩} {g : Πb', C ⟨a', b'⟩} {p : a = a'} (r : Π(b : B a), f b =[dpair_eq_dpair p !pathover_tr] g (p ▸ b)) : f =[p] g := begin cases p, apply pathover_idp_of_eq, apply eq_of_homotopy, intro b, apply eq_of_pathover_idp, esimp at r, exact !pathover_ap (r b) end definition pi_pathover_right' {C : (Σa, B a) → Type} {f : Πb, C ⟨a, b⟩} {g : Πb', C ⟨a', b'⟩} {p : a = a'} (r : Π(b' : B a'), f (p⁻¹ ▸ b') =[dpair_eq_dpair p !tr_pathover] g b') : f =[p] g := begin cases p, apply pathover_idp_of_eq, apply eq_of_homotopy, intro b, apply eq_of_pathover_idp, esimp at r, exact !pathover_ap (r b) end /- Maps on paths -/ /- The action of maps given by lambda. -/ definition ap_lambdaD {C : A' → Type} (p : a = a') (f : Πa b, C b) : ap (λa b, f a b) p = eq_of_homotopy (λb, ap (λa, f a b) p) := begin apply (eq.rec_on p), apply inverse, apply eq_of_homotopy_idp end /- Dependent paths -/ /- with more implicit arguments the conclusion of the following theorem is (Π(b : B a), transportD B C p b (f b) = g (transport B p b)) ≃ (transport (λa, Π(b : B a), C a b) p f = g) -/ definition heq_piD (p : a = a') (f : Π(b : B a), C a b) (g : Π(b' : B a'), C a' b') : (Π(b : B a), p ▸D (f b) = g (p ▸ b)) ≃ (p ▸ f = g) := eq.rec_on p (λg, !homotopy_equiv_eq) g definition heq_pi {C : A → Type} (p : a = a') (f : Π(b : B a), C a) (g : Π(b' : B a'), C a') : (Π(b : B a), p ▸ (f b) = g (p ▸ b)) ≃ (p ▸ f = g) := eq.rec_on p (λg, !homotopy_equiv_eq) g section open sigma sigma.ops /- more implicit arguments: (Π(b : B a), transport C (sigma_eq p idp) (f b) = g (p ▸ b)) ≃ (Π(b : B a), transportD B (λ(a : A) (b : B a), C ⟨a, b⟩) p b (f b) = g (transport B p b)) -/ definition heq_pi_sigma {C : (Σa, B a) → Type} (p : a = a') (f : Π(b : B a), C ⟨a, b⟩) (g : Π(b' : B a'), C ⟨a', b'⟩) : (Π(b : B a), (sigma_eq p !pathover_tr) ▸ (f b) = g (p ▸ b)) ≃ (Π(b : B a), p ▸D (f b) = g (p ▸ b)) := eq.rec_on p (λg, !equiv.refl) g end /- Functorial action -/ variables (f0 : A' → A) (f1 : Π(a':A'), B (f0 a') → B' a') /- The functoriality of [forall] is slightly subtle: it is contravariant in the domain type and covariant in the codomain, but the codomain is dependent on the domain. -/ definition pi_functor [unfold_full] : (Π(a:A), B a) → (Π(a':A'), B' a') := λg a', f1 a' (g (f0 a')) definition ap_pi_functor {g g' : Π(a:A), B a} (h : g ~ g') : ap (pi_functor f0 f1) (eq_of_homotopy h) = eq_of_homotopy (λa':A', (ap (f1 a') (h (f0 a')))) := begin apply (is_equiv_rect (@apd10 A B g g')), intro p, clear h, cases p, apply concat, exact (ap (ap (pi_functor f0 f1)) (eq_of_homotopy_idp g)), apply symm, apply eq_of_homotopy_idp end /- Equivalences -/ definition is_equiv_pi_functor [instance] [constructor] [H0 : is_equiv f0] [H1 : Πa', is_equiv (f1 a')] : is_equiv (pi_functor f0 f1) := begin apply (adjointify (pi_functor f0 f1) (pi_functor f0⁻¹ (λ(a : A) (b' : B' (f0⁻¹ a)), transport B (right_inv f0 a) ((f1 (f0⁻¹ a))⁻¹ b')))), begin intro h, apply eq_of_homotopy, intro a', esimp, rewrite [adj f0 a',-tr_compose,fn_tr_eq_tr_fn _ f1,right_inv (f1 _)], apply apd end, begin intro h, apply eq_of_homotopy, intro a, esimp, rewrite [left_inv (f1 _)], apply apd end end definition pi_equiv_pi_of_is_equiv [constructor] [H : is_equiv f0] [H1 : Πa', is_equiv (f1 a')] : (Πa, B a) ≃ (Πa', B' a') := equiv.mk (pi_functor f0 f1) _ definition pi_equiv_pi [constructor] (f0 : A' ≃ A) (f1 : Πa', (B (to_fun f0 a') ≃ B' a')) : (Πa, B a) ≃ (Πa', B' a') := pi_equiv_pi_of_is_equiv (to_fun f0) (λa', to_fun (f1 a')) definition pi_equiv_pi_id [constructor] {P Q : A → Type} (g : Πa, P a ≃ Q a) : (Πa, P a) ≃ (Πa, Q a) := pi_equiv_pi equiv.refl g /- Equivalence if one of the types is contractible -/ definition pi_equiv_of_is_contr_left [constructor] (B : A → Type) [H : is_contr A] : (Πa, B a) ≃ B (center A) := begin fapply equiv.MK, { intro f, exact f (center A)}, { intro b a, exact (center_eq a) ▸ b}, { intro b, rewrite [hprop_eq_of_is_contr (center_eq (center A)) idp]}, { intro f, apply eq_of_homotopy, intro a, induction (center_eq a), rewrite [hprop_eq_of_is_contr (center_eq (center A)) idp]} end definition pi_equiv_of_is_contr_right [constructor] [H : Πa, is_contr (B a)] : (Πa, B a) ≃ unit := begin fapply equiv.MK, { intro f, exact star}, { intro u a, exact !center}, { intro u, induction u, reflexivity}, { intro f, apply eq_of_homotopy, intro a, apply is_hprop.elim} end /- Interaction with other type constructors -/ -- most of these are in the file of the other type constructor definition pi_empty_left [constructor] (B : empty → Type) : (Πx, B x) ≃ unit := begin fapply equiv.MK, { intro f, exact star}, { intro x y, contradiction}, { intro x, induction x, reflexivity}, { intro f, apply eq_of_homotopy, intro y, contradiction}, end definition pi_unit_left [constructor] (B : unit → Type) : (Πx, B x) ≃ B star := !pi_equiv_of_is_contr_left definition pi_bool_left [constructor] (B : bool → Type) : (Πx, B x) ≃ B ff × B tt := begin fapply equiv.MK, { intro f, exact (f ff, f tt)}, { intro x b, induction x, induction b: assumption}, { intro x, induction x, reflexivity}, { intro f, apply eq_of_homotopy, intro b, induction b: reflexivity}, end /- Truncatedness: any dependent product of n-types is an n-type -/ theorem is_trunc_pi (B : A → Type) (n : trunc_index) [H : ∀a, is_trunc n (B a)] : is_trunc n (Πa, B a) := begin revert B H, eapply (trunc_index.rec_on n), {intro B H, fapply is_contr.mk, intro a, apply center, intro f, apply eq_of_homotopy, intro x, apply (center_eq (f x))}, {intro n IH B H, fapply is_trunc_succ_intro, intro f g, fapply is_trunc_equiv_closed, apply equiv.symm, apply eq_equiv_homotopy, apply IH, intro a, show is_trunc n (f a = g a), from is_trunc_eq n (f a) (g a)} end local attribute is_trunc_pi [instance] theorem is_trunc_pi_eq [instance] [priority 500] (n : trunc_index) (f g : Πa, B a) [H : ∀a, is_trunc n (f a = g a)] : is_trunc n (f = g) := begin apply is_trunc_equiv_closed_rev, apply eq_equiv_homotopy end theorem is_trunc_not [instance] (n : trunc_index) (A : Type) : is_trunc (n.+1) ¬A := by unfold not;exact _ theorem is_hprop_pi_eq [instance] [priority 490] (a : A) : is_hprop (Π(a' : A), a = a') := is_hprop_of_imp_is_contr ( assume (f : Πa', a = a'), assert is_contr A, from is_contr.mk a f, by exact _) /- force type clas resolution -/ theorem is_hprop_neg (A : Type) : is_hprop (¬A) := _ local attribute ne [reducible] theorem is_hprop_ne [instance] {A : Type} (a b : A) : is_hprop (a ≠ b) := _ /- Symmetry of Π -/ definition is_equiv_flip [instance] {P : A → A' → Type} : is_equiv (@function.flip A A' P) := begin fapply is_equiv.mk, exact (@function.flip _ _ (function.flip P)), repeat (intro f; apply idp) end definition pi_comm_equiv {P : A → A' → Type} : (Πa b, P a b) ≃ (Πb a, P a b) := equiv.mk (@function.flip _ _ P) _ end pi attribute pi.is_trunc_pi [instance] [priority 1520]
cdefded269cb1940a3149cd9c049c762bb16a552
94e33a31faa76775069b071adea97e86e218a8ee
/src/analysis/normed_space/weak_dual.lean
36408415a2d67c75f2a9a010684b293bd929a36a
[ "Apache-2.0" ]
permissive
urkud/mathlib
eab80095e1b9f1513bfb7f25b4fa82fa4fd02989
6379d39e6b5b279df9715f8011369a301b634e41
refs/heads/master
1,658,425,342,662
1,658,078,703,000
1,658,078,703,000
186,910,338
0
0
Apache-2.0
1,568,512,083,000
1,557,958,709,000
Lean
UTF-8
Lean
false
false
10,011
lean
/- Copyright (c) 2021 Kalle Kytölä. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kalle Kytölä, Yury Kudryashov -/ import topology.algebra.module.weak_dual import analysis.normed_space.dual import analysis.normed_space.operator_norm /-! # Weak dual of normed space Let `E` be a normed space over a field `𝕜`. This file is concerned with properties of the weak-* topology on the dual of `E`. By the dual, we mean either of the type synonyms `normed_space.dual 𝕜 E` or `weak_dual 𝕜 E`, depending on whether it is viewed as equipped with its usual operator norm topology or the weak-* topology. It is shown that the canonical mapping `normed_space.dual 𝕜 E → weak_dual 𝕜 E` is continuous, and as a consequence the weak-* topology is coarser than the topology obtained from the operator norm (dual norm). In this file, we also establish the Banach-Alaoglu theorem about the compactness of closed balls in the dual of `E` (as well as sets of somewhat more general form) with respect to the weak-* topology. ## Main definitions The main definitions concern the canonical mapping `dual 𝕜 E → weak_dual 𝕜 E`. * `normed_space.dual.to_weak_dual` and `weak_dual.to_normed_dual`: Linear equivalences from `dual 𝕜 E` to `weak_dual 𝕜 E` and in the converse direction. * `normed_space.dual.continuous_linear_map_to_weak_dual`: A continuous linear mapping from `dual 𝕜 E` to `weak_dual 𝕜 E` (same as `normed_space.dual.to_weak_dual` but different bundled data). ## Main results The first main result concerns the comparison of the operator norm topology on `dual 𝕜 E` and the weak-* topology on (its type synonym) `weak_dual 𝕜 E`: * `dual_norm_topology_le_weak_dual_topology`: The weak-* topology on the dual of a normed space is coarser (not necessarily strictly) than the operator norm topology. * `weak_dual.is_compact_polar` (a version of the Banach-Alaoglu theorem): The polar set of a neighborhood of the origin in a normed space `E` over `𝕜` is compact in `weak_dual _ E`, if the nondiscrete normed field `𝕜` is proper as a topological space. * `weak_dual.is_compact_closed_ball` (the most common special case of the Banach-Alaoglu theorem): Closed balls in the dual of a normed space `E` over `ℝ` or `ℂ` are compact in the weak-star topology. TODOs: * Add that in finite dimensions, the weak-* topology and the dual norm topology coincide. * Add that in infinite dimensions, the weak-* topology is strictly coarser than the dual norm topology. * Add metrizability of the dual unit ball (more generally weak-star compact subsets) of `weak_dual 𝕜 E` under the assumption of separability of `E`. * Add the sequential Banach-Alaoglu theorem: the dual unit ball of a separable normed space `E` is sequentially compact in the weak-star topology. This would follow from the metrizability above. ## Notations No new notation is introduced. ## Implementation notes Weak-* topology is defined generally in the file `topology.algebra.module.weak_dual`. When `E` is a normed space, the duals `dual 𝕜 E` and `weak_dual 𝕜 E` are type synonyms with different topology instances. For the proof of Banach-Alaoglu theorem, the weak dual of `E` is embedded in the space of functions `E → 𝕜` with the topology of pointwise convergence. The polar set `polar 𝕜 s` of a subset `s` of `E` is originally defined as a subset of the dual `dual 𝕜 E`. We care about properties of these w.r.t. weak-* topology, and for this purpose give the definition `weak_dual.polar 𝕜 s` for the "same" subset viewed as a subset of `weak_dual 𝕜 E` (a type synonym of the dual but with a different topology instance). ## References * https://en.wikipedia.org/wiki/Weak_topology#Weak-*_topology * https://en.wikipedia.org/wiki/Banach%E2%80%93Alaoglu_theorem ## Tags weak-star, weak dual -/ noncomputable theory open filter function metric set open_locale topological_space filter /-! ### Weak star topology on duals of normed spaces In this section, we prove properties about the weak-* topology on duals of normed spaces. We prove in particular that the canonical mapping `dual 𝕜 E → weak_dual 𝕜 E` is continuous, i.e., that the weak-* topology is coarser (not necessarily strictly) than the topology given by the dual-norm (i.e. the operator-norm). -/ variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] variables {E : Type*} [semi_normed_group E] [normed_space 𝕜 E] namespace normed_space namespace dual /-- For normed spaces `E`, there is a canonical map `dual 𝕜 E → weak_dual 𝕜 E` (the "identity" mapping). It is a linear equivalence. -/ def to_weak_dual : dual 𝕜 E ≃ₗ[𝕜] weak_dual 𝕜 E := linear_equiv.refl 𝕜 (E →L[𝕜] 𝕜) @[simp] lemma coe_to_weak_dual (x' : dual 𝕜 E) : ⇑(x'.to_weak_dual) = x' := rfl @[simp] lemma to_weak_dual_eq_iff (x' y' : dual 𝕜 E) : x'.to_weak_dual = y'.to_weak_dual ↔ x' = y' := to_weak_dual.injective.eq_iff theorem to_weak_dual_continuous : continuous (λ (x' : dual 𝕜 E), x'.to_weak_dual) := weak_bilin.continuous_of_continuous_eval _ $ λ z, (inclusion_in_double_dual 𝕜 E z).continuous /-- For a normed space `E`, according to `to_weak_dual_continuous` the "identity mapping" `dual 𝕜 E → weak_dual 𝕜 E` is continuous. This definition implements it as a continuous linear map. -/ def continuous_linear_map_to_weak_dual : dual 𝕜 E →L[𝕜] weak_dual 𝕜 E := { cont := to_weak_dual_continuous, .. to_weak_dual, } /-- The weak-star topology is coarser than the dual-norm topology. -/ theorem dual_norm_topology_le_weak_dual_topology : (by apply_instance : topological_space (dual 𝕜 E)) ≤ (by apply_instance : topological_space (weak_dual 𝕜 E)) := by { convert to_weak_dual_continuous.le_induced, exact induced_id.symm } end dual end normed_space namespace weak_dual open normed_space /-- For normed spaces `E`, there is a canonical map `weak_dual 𝕜 E → dual 𝕜 E` (the "identity" mapping). It is a linear equivalence. Here it is implemented as the inverse of the linear equivalence `normed_space.dual.to_weak_dual` in the other direction. -/ def to_normed_dual : weak_dual 𝕜 E ≃ₗ[𝕜] dual 𝕜 E := normed_space.dual.to_weak_dual.symm lemma to_normed_dual_apply (x : weak_dual 𝕜 E) (y : E) : (to_normed_dual x) y = x y := rfl @[simp] lemma coe_to_normed_dual (x' : weak_dual 𝕜 E) : ⇑(x'.to_normed_dual) = x' := rfl @[simp] lemma to_normed_dual_eq_iff (x' y' : weak_dual 𝕜 E) : x'.to_normed_dual = y'.to_normed_dual ↔ x' = y' := weak_dual.to_normed_dual.injective.eq_iff lemma is_closed_closed_ball (x' : dual 𝕜 E) (r : ℝ) : is_closed (to_normed_dual ⁻¹' closed_ball x' r) := is_closed_induced_iff'.2 (continuous_linear_map.is_weak_closed_closed_ball x' r) /-! ### Polar sets in the weak dual space -/ variables (𝕜) /-- The polar set `polar 𝕜 s` of `s : set E` seen as a subset of the dual of `E` with the weak-star topology is `weak_dual.polar 𝕜 s`. -/ def polar (s : set E) : set (weak_dual 𝕜 E) := to_normed_dual ⁻¹' polar 𝕜 s lemma polar_def (s : set E) : polar 𝕜 s = {f : weak_dual 𝕜 E | ∀ x ∈ s, ∥f x∥ ≤ 1} := rfl /-- The polar `polar 𝕜 s` of a set `s : E` is a closed subset when the weak star topology is used. -/ lemma is_closed_polar (s : set E) : is_closed (polar 𝕜 s) := begin simp only [polar_def, set_of_forall], exact is_closed_bInter (λ x hx, is_closed_Iic.preimage (weak_bilin.eval_continuous _ _).norm) end variable {𝕜} /-- While the coercion `coe_fn : weak_dual 𝕜 E → (E → 𝕜)` is not a closed map, it sends *bounded* closed sets to closed sets. -/ lemma is_closed_image_coe_of_bounded_of_closed {s : set (weak_dual 𝕜 E)} (hb : bounded (dual.to_weak_dual ⁻¹' s)) (hc : is_closed s) : is_closed ((coe_fn : weak_dual 𝕜 E → E → 𝕜) '' s) := continuous_linear_map.is_closed_image_coe_of_bounded_of_weak_closed hb (is_closed_induced_iff'.1 hc) lemma is_compact_of_bounded_of_closed [proper_space 𝕜] {s : set (weak_dual 𝕜 E)} (hb : bounded (dual.to_weak_dual ⁻¹' s)) (hc : is_closed s) : is_compact s := (embedding.is_compact_iff_is_compact_image fun_like.coe_injective.embedding_induced).mpr $ continuous_linear_map.is_compact_image_coe_of_bounded_of_closed_image hb $ is_closed_image_coe_of_bounded_of_closed hb hc variable (𝕜) /-- The image under `coe_fn : weak_dual 𝕜 E → (E → 𝕜)` of a polar `weak_dual.polar 𝕜 s` of a neighborhood `s` of the origin is a closed set. -/ lemma is_closed_image_polar_of_mem_nhds {s : set E} (s_nhd : s ∈ 𝓝 (0 : E)) : is_closed ((coe_fn : weak_dual 𝕜 E → E → 𝕜) '' polar 𝕜 s) := is_closed_image_coe_of_bounded_of_closed (bounded_polar_of_mem_nhds_zero 𝕜 s_nhd) (is_closed_polar _ _) /-- The image under `coe_fn : normed_space.dual 𝕜 E → (E → 𝕜)` of a polar `polar 𝕜 s` of a neighborhood `s` of the origin is a closed set. -/ lemma _root_.normed_space.dual.is_closed_image_polar_of_mem_nhds {s : set E} (s_nhd : s ∈ 𝓝 (0 : E)) : is_closed ((coe_fn : dual 𝕜 E → E → 𝕜) '' normed_space.polar 𝕜 s) := is_closed_image_polar_of_mem_nhds 𝕜 s_nhd /-- The **Banach-Alaoglu theorem**: the polar set of a neighborhood `s` of the origin in a normed space `E` is a compact subset of `weak_dual 𝕜 E`. -/ theorem is_compact_polar [proper_space 𝕜] {s : set E} (s_nhd : s ∈ 𝓝 (0 : E)) : is_compact (polar 𝕜 s) := is_compact_of_bounded_of_closed (bounded_polar_of_mem_nhds_zero 𝕜 s_nhd) (is_closed_polar _ _) /-- The **Banach-Alaoglu theorem**: closed balls of the dual of a normed space `E` are compact in the weak-star topology. -/ theorem is_compact_closed_ball [proper_space 𝕜] (x' : dual 𝕜 E) (r : ℝ) : is_compact (to_normed_dual ⁻¹' (closed_ball x' r)) := is_compact_of_bounded_of_closed bounded_closed_ball (is_closed_closed_ball x' r) end weak_dual
0920028515af6d3eadc22e5f6333ee55dd47e2f6
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/matcherElimUniv.lean
0df5d48760ad5d2e398104614864b739782ca7d0
[ "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
163
lean
universe u def len {α : Type u} : List α → List α → Nat | [], bs => bs.length | a::as, bs => len as bs + 1 theorem ex1 : len [1, 2] [3, 4] = 4 := rfl
3fe7535b56e5645488a744c88a6d129dad823e33
6fca17f8d5025f89be1b2d9d15c9e0c4b4900cbf
/src/game/world9/level1.lean
18c94dd00dc8124344912de9f5fd990b500db22f
[ "Apache-2.0" ]
permissive
arolihas/natural_number_game
4f0c93feefec93b8824b2b96adff8b702b8b43ce
8e4f7b4b42888a3b77429f90cce16292bd288138
refs/heads/master
1,621,872,426,808
1,586,270,467,000
1,586,270,467,000
253,648,466
0
0
null
1,586,219,694,000
1,586,219,694,000
null
UTF-8
Lean
false
false
1,324
lean
import game.world8.level13 -- hide import game.world3.level9 namespace mynat -- hide /- # Advanced Multiplication World ## Level 1: `mul_pos` Welcome to Advanced Multiplication World! Before attempting this world you should have completed seven other worlds, including Multiplication World and Advanced Addition World. There are four levels in this world. Recall that if `b : mynat` is a hypothesis and you do `cases b with n`, your one goal will split into two goals, namely the cases `b = 0` and `b = succ(n)`. So `cases` here is like a weaker version of induction (you don't get the inductive hypothesis). ## Tricks 1) if your goal is `⊢ X ≠ Y` then `intro h` will give you `h : X = Y` and a goal of `⊢ false`. This is because `X ≠ Y` *means* `(X = Y) → false`. Conversely if your goal is `false` and you have `h : X ≠ Y` as a hypothesis then `apply h` will turn the goal into `X = Y`. 2) if `hab : succ (3 * x + 2 * y + 1) = 0` is a hypothesis and your goal is `⊢ false`, then `exact succ_ne_zero _ hab` will solve the goal, because Lean will figure out that `_` is supposed to be `3 * x + 2 * y + 1`. -/ /- Theorem The product of two non-zero natural numbers is non-zero. -/ theorem mul_pos (a b : mynat) : a ≠ 0 → b ≠ 0 → a * b ≠ 0 := begin [nat_num_game] end end mynat -- hide
006184f78b0ec8315b2bf84d9d523141fe9e4a07
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Lean/Widget/InteractiveDiagnostic.lean
77692265f8d13f336e895e1e0ba36d56734c4a37
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
9,090
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Wojciech Nawrocki -/ import Lean.Data.Lsp import Lean.Message import Lean.Elab.InfoTree import Lean.Server.Utils import Lean.Server.Rpc.Basic import Lean.Widget.TaggedText import Lean.Widget.InteractiveCode import Lean.Widget.InteractiveGoal namespace Lean.Widget open Lsp Server deriving instance RpcEncoding with { withRef := true } for MessageData inductive MsgEmbed where | expr : CodeWithInfos → MsgEmbed | goal : InteractiveGoal → MsgEmbed | lazyTrace : Nat → Name → WithRpcRef MessageData → MsgEmbed deriving Inhabited namespace MsgEmbed -- TODO(WN): `deriving RpcEncoding` for `inductive` to replace the following hack @[reducible] def rpcPacketFor {β : outParam Type} (α : Type) [RpcEncoding α β] := β private inductive RpcEncodingPacket where | expr : TaggedText (rpcPacketFor CodeToken) → RpcEncodingPacket | goal : rpcPacketFor InteractiveGoal → RpcEncodingPacket | lazyTrace : Nat → Name → Lsp.RpcRef → RpcEncodingPacket deriving Inhabited, FromJson, ToJson instance : RpcEncoding MsgEmbed RpcEncodingPacket where rpcEncode a := match a with | expr t => return RpcEncodingPacket.expr (← rpcEncode t) | goal g => return RpcEncodingPacket.goal (← rpcEncode g) | lazyTrace col n t => return RpcEncodingPacket.lazyTrace col n (← rpcEncode t) rpcDecode a := match a with | RpcEncodingPacket.expr t => return expr (← rpcDecode t) | RpcEncodingPacket.goal g => return goal (← rpcDecode g) | RpcEncodingPacket.lazyTrace col n t => return lazyTrace col n (← rpcDecode t) end MsgEmbed /-- We embed objects in LSP diagnostics by storing them in the tag of an empty subtree (`text ""`). In other words, we terminate the `MsgEmbed`-tagged tree at embedded objects and instead store the pretty-printed embed (which can itself be a `TaggedText`) in the tag. -/ abbrev InteractiveDiagnostic := Lsp.DiagnosticWith (TaggedText MsgEmbed) namespace InteractiveDiagnostic open Lsp private abbrev RpcEncodingPacket := Lsp.DiagnosticWith (TaggedText MsgEmbed.RpcEncodingPacket) instance : RpcEncoding InteractiveDiagnostic RpcEncodingPacket where rpcEncode a := return { a with message := ← rpcEncode a.message } rpcDecode a := return { a with message := ← rpcDecode a.message } end InteractiveDiagnostic namespace InteractiveDiagnostic open MsgEmbed def toDiagnostic (diag : InteractiveDiagnostic) : Lsp.Diagnostic := { diag with message := prettyTt diag.message } where prettyTt (tt : TaggedText MsgEmbed) : String := let tt : TaggedText MsgEmbed := tt.rewrite fun | expr tt, _ => TaggedText.text tt.stripTags | goal g, _ => TaggedText.text (toString g.pretty) | lazyTrace _ _ _, subTt => subTt tt.stripTags end InteractiveDiagnostic private def mkPPContext (nCtx : NamingContext) (ctx : MessageDataContext) : PPContext := { env := ctx.env, mctx := ctx.mctx, lctx := ctx.lctx, opts := ctx.opts, currNamespace := nCtx.currNamespace, openDecls := nCtx.openDecls } private inductive EmbedFmt /- Tags denote `Info` objects. -/ | expr (ctx : Elab.ContextInfo) (infos : Std.RBMap Nat Elab.Info compare) | goal (ctx : Elab.ContextInfo) (lctx : LocalContext) (g : MVarId) /- Some messages (in particular, traces) are too costly to print eagerly. Instead, we allow the user to expand sub-traces interactively. -/ | lazyTrace (nCtx : NamingContext) (ctx? : Option MessageDataContext) (cls : Name) (m : MessageData) /- Ignore any tags in this subtree. -/ | ignoreTags deriving Inhabited private abbrev MsgFmtM := StateT (Array EmbedFmt) IO open MessageData in /-- We first build a `Nat`-tagged `Format` with the most shallow tag, if any, in every branch indexing into the array of embedded objects. -/ private partial def msgToInteractiveAux (msgData : MessageData) : IO (Format × Array EmbedFmt) := go { currNamespace := Name.anonymous, openDecls := [] } none msgData #[] where pushEmbed (e : EmbedFmt) : MsgFmtM Nat := modifyGet fun es => (es.size, es.push e) withIgnoreTags (x : MsgFmtM Format) : MsgFmtM Format := do let fmt ← x let t ← pushEmbed EmbedFmt.ignoreTags return Format.tag t fmt go : NamingContext → Option MessageDataContext → MessageData → MsgFmtM Format | _, _, ofFormat fmt => withIgnoreTags fmt | _, _, ofLevel u => format u | _, _, ofName n => format n | nCtx, some ctx, ofSyntax s => withIgnoreTags (ppTerm (mkPPContext nCtx ctx) s) -- HACK: might not be a term | _, none, ofSyntax s => withIgnoreTags s.formatStx | _, none, ofExpr e => format (toString e) | nCtx, some ctx, ofExpr e => do let ci : Elab.ContextInfo := { env := ctx.env mctx := ctx.mctx fileMap := arbitrary options := ctx.opts currNamespace := nCtx.currNamespace openDecls := nCtx.openDecls } let (fmt, infos) ← ci.runMetaM ctx.lctx (formatInfos e) let t ← pushEmbed <| EmbedFmt.expr ci infos return Format.tag t fmt | _, none, ofGoal mvarId => pure $ "goal " ++ format (mkMVar mvarId) | nCtx, some ctx, ofGoal mvarId => withIgnoreTags <| ppGoal (mkPPContext nCtx ctx) mvarId | nCtx, _, withContext ctx d => go nCtx ctx d | _, ctx, withNamingContext nCtx d => go nCtx ctx d | nCtx, ctx, tagged t d => do -- We postfix trace contexts with `_traceCtx` in order to detect them in messages. if let Name.str cls "_traceCtx" _ := t then let f ← pushEmbed <| EmbedFmt.lazyTrace nCtx ctx cls d Format.tag f s!"[{cls}] (trace hidden)" else go nCtx ctx d | nCtx, ctx, nest n d => Format.nest n <$> go nCtx ctx d | nCtx, ctx, compose d₁ d₂ => do let d₁ ← go nCtx ctx d₁; let d₂ ← go nCtx ctx d₂; pure $ d₁ ++ d₂ | nCtx, ctx, group d => Format.group <$> go nCtx ctx d | nCtx, ctx, node ds => Format.nest 2 <$> ds.foldlM (fun r d => do let d ← go nCtx ctx d; pure $ r ++ Format.line ++ d) Format.nil partial def msgToInteractive (msgData : MessageData) (indent : Nat := 0) : IO (TaggedText MsgEmbed) := do let (fmt, embeds) ← msgToInteractiveAux msgData let tt := TaggedText.prettyTagged fmt indent /- Here we rewrite a `TaggedText Nat` corresponding to a whole `MessageData` into one where the tags are `TaggedText MsgEmbed`s corresponding to embedded objects with their subtree empty (`text ""`). In other words, we terminate the `MsgEmbed`-tagged -tree at embedded objects and store the pretty-printed embed (which can itself be a `TaggedText`) in the tag. -/ tt.rewriteM fun (n, col) subTt => match embeds.get! n with | EmbedFmt.expr ctx infos => let subTt' := tagExprInfos ctx infos subTt TaggedText.tag (MsgEmbed.expr subTt') (TaggedText.text subTt.stripTags) | EmbedFmt.goal ctx lctx g => -- TODO(WN): use InteractiveGoal types here unreachable! | EmbedFmt.lazyTrace nCtx ctx? cls m => let msg := match ctx? with | some ctx => MessageData.withNamingContext nCtx <| MessageData.withContext ctx m | none => MessageData.withNamingContext nCtx m TaggedText.tag (MsgEmbed.lazyTrace col cls ⟨msg⟩) (TaggedText.text subTt.stripTags) | EmbedFmt.ignoreTags => TaggedText.text subTt.stripTags /-- Transform a Lean Message concerning the given text into an LSP Diagnostic. -/ def msgToInteractiveDiagnostic (text : FileMap) (m : Message) : IO InteractiveDiagnostic := do let low : Lsp.Position := text.leanPosToLspPos m.pos let fullHigh := text.leanPosToLspPos <| m.endPos.getD m.pos let high : Lsp.Position := match m.endPos with | some endPos => /- Truncate messages that are more than one line long. This is a workaround to avoid big blocks of "red squiggly lines" on VS Code. TODO: should it be a parameter? -/ let endPos := if endPos.line > m.pos.line then { line := m.pos.line + 1, column := 0 } else endPos text.leanPosToLspPos endPos | none => low let range : Range := ⟨low, high⟩ let fullRange : Range := ⟨low, fullHigh⟩ let severity := match m.severity with | MessageSeverity.information => DiagnosticSeverity.information | MessageSeverity.warning => DiagnosticSeverity.warning | MessageSeverity.error => DiagnosticSeverity.error let source := "Lean 4" let message ← try msgToInteractive m.data catch ex => TaggedText.text s!"[error when printing message: {ex.toString}]" pure { range := range fullRange := fullRange severity? := severity source? := source message := message } end Lean.Widget
736ae925aa086f50e35f83661b47134daa04a21c
12dabd587ce2621d9a4eff9f16e354d02e206c8e
/world02/level03.lean
88d156e4303545cfa8432231e150ecc979ed62c5
[]
no_license
abdelq/natural-number-game
a1b5b8f1d52625a7addcefc97c966d3f06a48263
bbddadc6d2e78ece2e9acd40fa7702ecc2db75c2
refs/heads/master
1,668,606,478,691
1,594,175,058,000
1,594,175,058,000
278,673,209
0
1
null
null
null
null
UTF-8
Lean
false
false
168
lean
lemma succ_add (a b : mynat) : succ a + b = succ (a + b) := begin induction b with h hd, rw add_zero, rw add_zero, refl, rw add_succ, rw add_succ, rw ← hd, refl, end
0ea14253010855720b95106a34b8fa049f4cc16b
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/2262.lean
d57d04f1d4957a459343019951660d9ba270dcf1
[ "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
193
lean
macro "foo" noWs ":" ws "bar" : command => `(command| /-! -/) macro "foo'" hygieneInfo noWs hygieneInfo ":" hygieneInfo ws hygieneInfo "bar" : command => `(command| /-! -/) foo: bar foo': bar
432e737760b09d8c242f1e4f03d5052c1b61cb1e
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/blast_cc9.lean
acf4b25bcf0d27fec5d03c611d1b0d42488f8592
[ "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
406
lean
import data.list open perm set_option blast.strategy "cc" example (a b : nat) : a = b → (b = a ↔ true) := by blast example (a b c : nat) : a = b → b = c → (true ↔ a = c) := by blast example (l₁ l₂ l₃ : list nat) : l₁ ~ l₂ → l₂ ~ l₃ → (true ↔ l₁ ~ l₃) := by blast example (l₁ l₂ l₃ : list nat) : l₁ ~ l₂ → l₂ = l₃ → (true ↔ l₁ ~ l₃) := by blast
1a0a46f17a5967036a61a26b8af4f4c9a9805234
b9a81ebb9de684db509231c4469a7d2c88915808
/src/super/default.lean
bade175432b3f269a67c2c32d5eed7fd90f81ae6
[]
no_license
leanprover/super
3dd81ce8d9ac3cba20bce55e84833fadb2f5716e
47b107b4cec8f3b41d72daba9cbda2f9d54025de
refs/heads/master
1,678,482,996,979
1,676,526,367,000
1,676,526,367,000
92,215,900
12
6
null
1,513,327,539,000
1,495,570,640,000
Lean
UTF-8
Lean
false
false
167
lean
/- Copyright (c) 2017 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ import .prover
03ffeca14e82a0c02303fb01092758a61ae249b0
592ee40978ac7604005a4e0d35bbc4b467389241
/Library/generated/mathscheme-lean/Monoid1.lean
400356146f86cf3d4cd12cca05bb2d977651e933
[]
no_license
ysharoda/Deriving-Definitions
3e149e6641fae440badd35ac110a0bd705a49ad2
dfecb27572022de3d4aa702cae8db19957523a59
refs/heads/master
1,679,127,857,700
1,615,939,007,000
1,615,939,007,000
229,785,731
4
0
null
null
null
null
UTF-8
Lean
false
false
6,987
lean
import init.data.nat.basic import init.data.fin.basic import data.vector import .Prelude open Staged open nat open fin open vector section Monoid1 structure Monoid1 (A : Type) : Type := (one : A) (op : (A → (A → A))) (lunit_one : (∀ {x : A} , (op one x) = x)) (runit_one : (∀ {x : A} , (op x one) = x)) (associative_op : (∀ {x y z : A} , (op (op x y) z) = (op x (op y z)))) open Monoid1 structure Sig (AS : Type) : Type := (oneS : AS) (opS : (AS → (AS → AS))) structure Product (A : Type) : Type := (oneP : (Prod A A)) (opP : ((Prod A A) → ((Prod A A) → (Prod A A)))) (lunit_1P : (∀ {xP : (Prod A A)} , (opP oneP xP) = xP)) (runit_1P : (∀ {xP : (Prod A A)} , (opP xP oneP) = xP)) (associative_opP : (∀ {xP yP zP : (Prod A A)} , (opP (opP xP yP) zP) = (opP xP (opP yP zP)))) structure Hom {A1 : Type} {A2 : Type} (Mo1 : (Monoid1 A1)) (Mo2 : (Monoid1 A2)) : Type := (hom : (A1 → A2)) (pres_one : (hom (one Mo1)) = (one Mo2)) (pres_op : (∀ {x1 x2 : A1} , (hom ((op Mo1) x1 x2)) = ((op Mo2) (hom x1) (hom x2)))) structure RelInterp {A1 : Type} {A2 : Type} (Mo1 : (Monoid1 A1)) (Mo2 : (Monoid1 A2)) : Type 1 := (interp : (A1 → (A2 → Type))) (interp_one : (interp (one Mo1) (one Mo2))) (interp_op : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((op Mo1) x1 x2) ((op Mo2) y1 y2)))))) inductive Monoid1LTerm : Type | oneL : Monoid1LTerm | opL : (Monoid1LTerm → (Monoid1LTerm → Monoid1LTerm)) open Monoid1LTerm inductive ClMonoid1ClTerm (A : Type) : Type | sing : (A → ClMonoid1ClTerm) | oneCl : ClMonoid1ClTerm | opCl : (ClMonoid1ClTerm → (ClMonoid1ClTerm → ClMonoid1ClTerm)) open ClMonoid1ClTerm inductive OpMonoid1OLTerm (n : ℕ) : Type | v : ((fin n) → OpMonoid1OLTerm) | oneOL : OpMonoid1OLTerm | opOL : (OpMonoid1OLTerm → (OpMonoid1OLTerm → OpMonoid1OLTerm)) open OpMonoid1OLTerm inductive OpMonoid1OL2Term2 (n : ℕ) (A : Type) : Type | v2 : ((fin n) → OpMonoid1OL2Term2) | sing2 : (A → OpMonoid1OL2Term2) | oneOL2 : OpMonoid1OL2Term2 | opOL2 : (OpMonoid1OL2Term2 → (OpMonoid1OL2Term2 → OpMonoid1OL2Term2)) open OpMonoid1OL2Term2 def simplifyCl {A : Type} : ((ClMonoid1ClTerm A) → (ClMonoid1ClTerm A)) | (opCl oneCl x) := x | (opCl x oneCl) := x | oneCl := oneCl | (opCl x1 x2) := (opCl (simplifyCl x1) (simplifyCl x2)) | (sing x1) := (sing x1) def simplifyOpB {n : ℕ} : ((OpMonoid1OLTerm n) → (OpMonoid1OLTerm n)) | (opOL oneOL x) := x | (opOL x oneOL) := x | oneOL := oneOL | (opOL x1 x2) := (opOL (simplifyOpB x1) (simplifyOpB x2)) | (v x1) := (v x1) def simplifyOp {n : ℕ} {A : Type} : ((OpMonoid1OL2Term2 n A) → (OpMonoid1OL2Term2 n A)) | (opOL2 oneOL2 x) := x | (opOL2 x oneOL2) := x | oneOL2 := oneOL2 | (opOL2 x1 x2) := (opOL2 (simplifyOp x1) (simplifyOp x2)) | (v2 x1) := (v2 x1) | (sing2 x1) := (sing2 x1) def evalB {A : Type} : ((Monoid1 A) → (Monoid1LTerm → A)) | Mo oneL := (one Mo) | Mo (opL x1 x2) := ((op Mo) (evalB Mo x1) (evalB Mo x2)) def evalCl {A : Type} : ((Monoid1 A) → ((ClMonoid1ClTerm A) → A)) | Mo (sing x1) := x1 | Mo oneCl := (one Mo) | Mo (opCl x1 x2) := ((op Mo) (evalCl Mo x1) (evalCl Mo x2)) def evalOpB {A : Type} {n : ℕ} : ((Monoid1 A) → ((vector A n) → ((OpMonoid1OLTerm n) → A))) | Mo vars (v x1) := (nth vars x1) | Mo vars oneOL := (one Mo) | Mo vars (opOL x1 x2) := ((op Mo) (evalOpB Mo vars x1) (evalOpB Mo vars x2)) def evalOp {A : Type} {n : ℕ} : ((Monoid1 A) → ((vector A n) → ((OpMonoid1OL2Term2 n A) → A))) | Mo vars (v2 x1) := (nth vars x1) | Mo vars (sing2 x1) := x1 | Mo vars oneOL2 := (one Mo) | Mo vars (opOL2 x1 x2) := ((op Mo) (evalOp Mo vars x1) (evalOp Mo vars x2)) def inductionB {P : (Monoid1LTerm → Type)} : ((P oneL) → ((∀ (x1 x2 : Monoid1LTerm) , ((P x1) → ((P x2) → (P (opL x1 x2))))) → (∀ (x : Monoid1LTerm) , (P x)))) | p1l popl oneL := p1l | p1l popl (opL x1 x2) := (popl _ _ (inductionB p1l popl x1) (inductionB p1l popl x2)) def inductionCl {A : Type} {P : ((ClMonoid1ClTerm A) → Type)} : ((∀ (x1 : A) , (P (sing x1))) → ((P oneCl) → ((∀ (x1 x2 : (ClMonoid1ClTerm A)) , ((P x1) → ((P x2) → (P (opCl x1 x2))))) → (∀ (x : (ClMonoid1ClTerm A)) , (P x))))) | psing p1cl popcl (sing x1) := (psing x1) | psing p1cl popcl oneCl := p1cl | psing p1cl popcl (opCl x1 x2) := (popcl _ _ (inductionCl psing p1cl popcl x1) (inductionCl psing p1cl popcl x2)) def inductionOpB {n : ℕ} {P : ((OpMonoid1OLTerm n) → Type)} : ((∀ (fin : (fin n)) , (P (v fin))) → ((P oneOL) → ((∀ (x1 x2 : (OpMonoid1OLTerm n)) , ((P x1) → ((P x2) → (P (opOL x1 x2))))) → (∀ (x : (OpMonoid1OLTerm n)) , (P x))))) | pv p1ol popol (v x1) := (pv x1) | pv p1ol popol oneOL := p1ol | pv p1ol popol (opOL x1 x2) := (popol _ _ (inductionOpB pv p1ol popol x1) (inductionOpB pv p1ol popol x2)) def inductionOp {n : ℕ} {A : Type} {P : ((OpMonoid1OL2Term2 n A) → Type)} : ((∀ (fin : (fin n)) , (P (v2 fin))) → ((∀ (x1 : A) , (P (sing2 x1))) → ((P oneOL2) → ((∀ (x1 x2 : (OpMonoid1OL2Term2 n A)) , ((P x1) → ((P x2) → (P (opOL2 x1 x2))))) → (∀ (x : (OpMonoid1OL2Term2 n A)) , (P x)))))) | pv2 psing2 p1ol2 popol2 (v2 x1) := (pv2 x1) | pv2 psing2 p1ol2 popol2 (sing2 x1) := (psing2 x1) | pv2 psing2 p1ol2 popol2 oneOL2 := p1ol2 | pv2 psing2 p1ol2 popol2 (opOL2 x1 x2) := (popol2 _ _ (inductionOp pv2 psing2 p1ol2 popol2 x1) (inductionOp pv2 psing2 p1ol2 popol2 x2)) def stageB : (Monoid1LTerm → (Staged Monoid1LTerm)) | oneL := (Now oneL) | (opL x1 x2) := (stage2 opL (codeLift2 opL) (stageB x1) (stageB x2)) def stageCl {A : Type} : ((ClMonoid1ClTerm A) → (Staged (ClMonoid1ClTerm A))) | (sing x1) := (Now (sing x1)) | oneCl := (Now oneCl) | (opCl x1 x2) := (stage2 opCl (codeLift2 opCl) (stageCl x1) (stageCl x2)) def stageOpB {n : ℕ} : ((OpMonoid1OLTerm n) → (Staged (OpMonoid1OLTerm n))) | (v x1) := (const (code (v x1))) | oneOL := (Now oneOL) | (opOL x1 x2) := (stage2 opOL (codeLift2 opOL) (stageOpB x1) (stageOpB x2)) def stageOp {n : ℕ} {A : Type} : ((OpMonoid1OL2Term2 n A) → (Staged (OpMonoid1OL2Term2 n A))) | (sing2 x1) := (Now (sing2 x1)) | (v2 x1) := (const (code (v2 x1))) | oneOL2 := (Now oneOL2) | (opOL2 x1 x2) := (stage2 opOL2 (codeLift2 opOL2) (stageOp x1) (stageOp x2)) structure StagedRepr (A : Type) (Repr : (Type → Type)) : Type := (oneT : (Repr A)) (opT : ((Repr A) → ((Repr A) → (Repr A)))) end Monoid1
e1398d4cbdcbda848ae590aa2d95d0286d487e23
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/instance_naming.lean
bc8e113adf8dc675d8673bc01b3c6b476ed2d721
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
2,291
lean
def foo := ℕ namespace foo instance : has_add foo := nat.has_add end foo -- verify that `foo.has_add` was created example := foo.has_add namespace category_theory def functor : Type := ℕ class is_right_adjoint (F : functor) : Prop class preserves_limits (F : functor) : Prop def forgetful_functor : functor := nat.zero instance : is_right_adjoint forgetful_functor := ⟨⟩ instance : is_right_adjoint nat.zero := ⟨⟩ -- satisfy the inhabited linter instance : inhabited functor := ⟨forgetful_functor⟩ end category_theory -- here we end up with a name with a repeated component section open category_theory instance : category_theory.preserves_limits forgetful_functor := ⟨⟩ end class lie_algebra (α : Type) : Type := (bracket : α → α → α) namespace lie_algebra instance : lie_algebra ℤ := ⟨λ x y, x * y - y * x⟩ def gl : Type := unit instance : lie_algebra gl := ⟨λ _ _, ()⟩ end lie_algebra class moo (α : Type*) class zoo (β : Type*) namespace zoo -- Every zoo is a moo. instance (β : Type*) [zoo β] : moo β := ⟨⟩ end zoo -- A boo is a bundled moo. structure boo : Type 1 := (β : Type) [is_zoo : moo β] instance : has_coe_to_sort boo _ := ⟨boo.β⟩ namespace boo -- Instance naming for variable arguments works (generating `boo.moo`) -- even if the variable is actually being implicitly coerced to something else. instance (b : boo) : moo b := ⟨⟩ -- Instance naming works for projections instance (b : boo) : moo b.β := ⟨⟩ example := boo.β.moo end boo -- Instance naming works for explicit type ascriptions instance : moo (@set ℕ : Type) := ⟨⟩ example := set.moo -- also for sorts instance : moo Type := ⟨⟩ example := sort.moo -- and pis instance : moo (ℕ → ℕ) := ⟨⟩ example := pi.moo section parameter (n : ℕ) def cow := fin n -- and parameters instance : moo cow := ⟨⟩ example := cow.moo end example := category_theory.forgetful_functor.is_right_adjoint example := category_theory.nat.zero.is_right_adjoint example := category_theory.functor.inhabited example := category_theory.forgetful_functor.category_theory.preserves_limits example := lie_algebra.int.lie_algebra example := lie_algebra.gl.lie_algebra example := zoo.moo example := boo.moo example := boo.has_coe_to_sort
5658eab507f19af15ef56a9326e5c397f6c1417d
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/data/polynomial/degree/lemmas.lean
18044c2c8a9d8e7d0a5be5a0e41e0fc0b6d052ca
[ "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
9,098
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import data.polynomial.eval import tactic.interval_cases /-! # Theory of degrees of polynomials Some of the main results include - `nat_degree_comp_le` : The degree of the composition is at most the product of degrees -/ noncomputable theory open_locale classical open finsupp finset namespace polynomial universes u v w variables {R : Type u} {S : Type v} { ι : Type w} {a b : R} {m n : ℕ} section semiring variables [semiring R] {p q r : polynomial R} section degree lemma nat_degree_comp_le : nat_degree (p.comp q) ≤ nat_degree p * nat_degree q := if h0 : p.comp q = 0 then by rw [h0, nat_degree_zero]; exact nat.zero_le _ else with_bot.coe_le_coe.1 $ calc ↑(nat_degree (p.comp q)) = degree (p.comp q) : (degree_eq_nat_degree h0).symm ... = _ : congr_arg degree comp_eq_sum_left ... ≤ _ : degree_sum_le _ _ ... ≤ _ : sup_le (λ n hn, calc degree (C (coeff p n) * q ^ n) ≤ degree (C (coeff p n)) + degree (q ^ n) : degree_mul_le _ _ ... ≤ nat_degree (C (coeff p n)) + n • (degree q) : add_le_add degree_le_nat_degree (degree_pow_le _ _) ... ≤ nat_degree (C (coeff p n)) + n • (nat_degree q) : add_le_add_left (nsmul_le_nsmul_of_le_right (@degree_le_nat_degree _ _ q) n) _ ... = (n * nat_degree q : ℕ) : by rw [nat_degree_C, with_bot.coe_zero, zero_add, ← with_bot.coe_nsmul, nsmul_eq_mul]; simp ... ≤ (nat_degree p * nat_degree q : ℕ) : with_bot.coe_le_coe.2 $ mul_le_mul_of_nonneg_right (le_nat_degree_of_ne_zero (mem_support_iff.1 hn)) (nat.zero_le _)) lemma degree_pos_of_root {p : polynomial R} (hp : p ≠ 0) (h : is_root p a) : 0 < degree p := lt_of_not_ge $ λ hlt, begin have := eq_C_of_degree_le_zero hlt, rw [is_root, this, eval_C] at h, simp only [h, ring_hom.map_zero] at this, exact hp this, end lemma nat_degree_le_iff_coeff_eq_zero : p.nat_degree ≤ n ↔ ∀ N : ℕ, n < N → p.coeff N = 0 := by simp_rw [nat_degree_le_iff_degree_le, degree_le_iff_coeff_zero, with_bot.coe_lt_coe] lemma nat_degree_C_mul_le (a : R) (f : polynomial R) : (C a * f).nat_degree ≤ f.nat_degree := calc (C a * f).nat_degree ≤ (C a).nat_degree + f.nat_degree : nat_degree_mul_le ... = 0 + f.nat_degree : by rw nat_degree_C a ... = f.nat_degree : zero_add _ lemma nat_degree_mul_C_le (f : polynomial R) (a : R) : (f * C a).nat_degree ≤ f.nat_degree := calc (f * C a).nat_degree ≤ f.nat_degree + (C a).nat_degree : nat_degree_mul_le ... = f.nat_degree + 0 : by rw nat_degree_C a ... = f.nat_degree : add_zero _ lemma eq_nat_degree_of_le_mem_support (pn : p.nat_degree ≤ n) (ns : n ∈ p.support) : p.nat_degree = n := le_antisymm pn (le_nat_degree_of_mem_supp _ ns) lemma nat_degree_C_mul_eq_of_mul_eq_one {ai : R} (au : ai * a = 1) : (C a * p).nat_degree = p.nat_degree := le_antisymm (nat_degree_C_mul_le a p) (calc p.nat_degree = (1 * p).nat_degree : by nth_rewrite 0 [← one_mul p] ... = (C ai * (C a * p)).nat_degree : by rw [← C_1, ← au, ring_hom.map_mul, ← mul_assoc] ... ≤ (C a * p).nat_degree : nat_degree_C_mul_le ai (C a * p)) lemma nat_degree_mul_C_eq_of_mul_eq_one {ai : R} (au : a * ai = 1) : (p * C a).nat_degree = p.nat_degree := le_antisymm (nat_degree_mul_C_le p a) (calc p.nat_degree = (p * 1).nat_degree : by nth_rewrite 0 [← mul_one p] ... = ((p * C a) * C ai).nat_degree : by rw [← C_1, ← au, ring_hom.map_mul, ← mul_assoc] ... ≤ (p * C a).nat_degree : nat_degree_mul_C_le (p * C a) ai) /-- Although not explicitly stated, the assumptions of lemma `nat_degree_mul_C_eq_of_mul_ne_zero` force the polynomial `p` to be non-zero, via `p.leading_coeff ≠ 0`. Lemma `nat_degree_mul_C_eq_of_no_zero_divisors` below separates cases, in order to overcome this hurdle. -/ lemma nat_degree_mul_C_eq_of_mul_ne_zero (h : p.leading_coeff * a ≠ 0) : (p * C a).nat_degree = p.nat_degree := begin refine eq_nat_degree_of_le_mem_support (nat_degree_mul_C_le p a) _, refine mem_support_iff.mpr _, rwa coeff_mul_C, end /-- Although not explicitly stated, the assumptions of lemma `nat_degree_C_mul_eq_of_mul_ne_zero` force the polynomial `p` to be non-zero, via `p.leading_coeff ≠ 0`. Lemma `nat_degree_C_mul_eq_of_no_zero_divisors` below separates cases, in order to overcome this hurdle. -/ lemma nat_degree_C_mul_eq_of_mul_ne_zero (h : a * p.leading_coeff ≠ 0) : (C a * p).nat_degree = p.nat_degree := begin refine eq_nat_degree_of_le_mem_support (nat_degree_C_mul_le a p) _, refine mem_support_iff.mpr _, rwa coeff_C_mul, end lemma nat_degree_add_coeff_mul (f g : polynomial R) : (f * g).coeff (f.nat_degree + g.nat_degree) = f.coeff f.nat_degree * g.coeff g.nat_degree := by simp only [coeff_nat_degree, coeff_mul_degree_add_degree] lemma nat_degree_lt_coeff_mul (h : p.nat_degree + q.nat_degree < m + n) : (p * q).coeff (m + n) = 0 := coeff_eq_zero_of_nat_degree_lt (nat_degree_mul_le.trans_lt h) lemma degree_sum_eq_of_disjoint (f : S → polynomial R) (s : finset S) (h : set.pairwise { i | i ∈ s ∧ f i ≠ 0 } (ne on (degree ∘ f))) : degree (s.sum f) = s.sup (λ i, degree (f i)) := begin induction s using finset.induction_on with x s hx IH, { simp }, { simp only [hx, finset.sum_insert, not_false_iff, finset.sup_insert], specialize IH (h.mono (λ _, by simp {contextual := tt})), rcases lt_trichotomy (degree (f x)) (degree (s.sum f)) with H|H|H, { rw [←IH, sup_eq_right.mpr H.le, degree_add_eq_right_of_degree_lt H] }, { rcases s.eq_empty_or_nonempty with rfl|hs, { simp }, obtain ⟨y, hy, hy'⟩ := finset.exists_mem_eq_sup s hs (λ i, degree (f i)), rw [IH, hy'] at H, by_cases hx0 : f x = 0, { simp [hx0, IH] }, have hy0 : f y ≠ 0, { contrapose! H, simpa [H, degree_eq_bot] using hx0 }, refine absurd H (h _ _ (λ H, hx _)), { simp [hx0] }, { simp [hy, hy0] }, { exact H.symm ▸ hy } }, { rw [←IH, sup_eq_left.mpr H.le, degree_add_eq_left_of_degree_lt H] } } end lemma nat_degree_sum_eq_of_disjoint (f : S → polynomial R) (s : finset S) (h : set.pairwise { i | i ∈ s ∧ f i ≠ 0 } (ne on (nat_degree ∘ f))) : nat_degree (s.sum f) = s.sup (λ i, nat_degree (f i)) := begin by_cases H : ∃ x ∈ s, f x ≠ 0, { obtain ⟨x, hx, hx'⟩ := H, have hs : s.nonempty := ⟨x, hx⟩, refine nat_degree_eq_of_degree_eq_some _, rw degree_sum_eq_of_disjoint, { rw [←finset.sup'_eq_sup hs, ←finset.sup'_eq_sup hs, finset.coe_sup', ←finset.sup'_eq_sup hs], refine le_antisymm _ _, { rw finset.sup'_le_iff, intros b hb, by_cases hb' : f b = 0, { simpa [hb'] using hs }, rw degree_eq_nat_degree hb', exact finset.le_sup' _ hb }, { rw finset.sup'_le_iff, intros b hb, simp only [finset.le_sup'_iff, exists_prop, function.comp_app], by_cases hb' : f b = 0, { refine ⟨x, hx, _⟩, contrapose! hx', simpa [hb', degree_eq_bot] using hx' }, exact ⟨b, hb, (degree_eq_nat_degree hb').ge⟩ } }, { exact h.imp (λ x y hxy hxy', hxy (nat_degree_eq_of_degree_eq hxy')) } }, { push_neg at H, rw [finset.sum_eq_zero H, nat_degree_zero, eq_comm, show 0 = ⊥, from rfl, finset.sup_eq_bot_iff], intros x hx, simp [H x hx] } end variables [semiring S] lemma nat_degree_pos_of_eval₂_root {p : polynomial R} (hp : p ≠ 0) (f : R →+* S) {z : S} (hz : eval₂ f z p = 0) (inj : ∀ (x : R), f x = 0 → x = 0) : 0 < nat_degree p := lt_of_not_ge $ λ hlt, begin have A : p = C (p.coeff 0) := eq_C_of_nat_degree_le_zero hlt, rw [A, eval₂_C] at hz, simp only [inj (p.coeff 0) hz, ring_hom.map_zero] at A, exact hp A end lemma degree_pos_of_eval₂_root {p : polynomial R} (hp : p ≠ 0) (f : R →+* S) {z : S} (hz : eval₂ f z p = 0) (inj : ∀ (x : R), f x = 0 → x = 0) : 0 < degree p := nat_degree_pos_iff_degree_pos.mp (nat_degree_pos_of_eval₂_root hp f hz inj) @[simp] lemma coe_lt_degree {p : polynomial R} {n : ℕ} : ((n : with_bot ℕ) < degree p) ↔ n < nat_degree p := begin by_cases h : p = 0, { simp [h] }, rw [degree_eq_nat_degree h, with_bot.coe_lt_coe], end end degree end semiring section no_zero_divisors variables [semiring R] [no_zero_divisors R] {p q : polynomial R} lemma nat_degree_mul_C_eq_of_no_zero_divisors (a0 : a ≠ 0) : (p * C a).nat_degree = p.nat_degree := begin by_cases p0 : p = 0, { rw [p0, zero_mul] }, { exact nat_degree_mul_C_eq_of_mul_ne_zero (mul_ne_zero (leading_coeff_ne_zero.mpr p0) a0) } end lemma nat_degree_C_mul_eq_of_no_zero_divisors (a0 : a ≠ 0) : (C a * p).nat_degree = p.nat_degree := begin by_cases p0 : p = 0, { rw [p0, mul_zero] }, { exact nat_degree_C_mul_eq_of_mul_ne_zero (mul_ne_zero a0 (leading_coeff_ne_zero.mpr p0)) } end end no_zero_divisors end polynomial
afe5ed01c8a1bee6b80c9e927edf1bddc9ab7536
4fa161becb8ce7378a709f5992a594764699e268
/src/category_theory/types.lean
c0bdd91e9c9ada28ffdd0a04138397ee312ef911
[ "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
9,501
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 -/ import category_theory.fully_faithful import data.equiv.basic /-! # The category `Type`. In this section we set up the theory so that Lean's types and functions between them can be viewed as a `large_category` in our framework. Lean can not transparently view a function as a morphism in this category, and needs a hint in order to be able to type check. We provide the abbreviation `as_hom f` to guide type checking, as well as a corresponding notation `↾ f`. (Entered as `\upr `.) We provide various simplification lemmas for functors and natural transformations valued in `Type`. We define `ulift_functor`, from `Type u` to `Type (max u v)`, and show that it is fully faithful (but not, of course, essentially surjective). We prove some basic facts about the category `Type`: * epimorphisms are surjections and monomorphisms are injections, * `iso` is both `iso` and `equiv` to `equiv` (at least within a fixed universe), * every type level `is_lawful_functor` gives a categorical functor `Type ⥤ Type` (the corresponding fact about monads is in `src/category_theory/monad/types.lean`). -/ namespace category_theory universes v v' w u u' -- declare the `v`'s first; see `category_theory.category` for an explanation instance types : large_category (Type u) := { hom := λ a b, (a → b), id := λ a, id, comp := λ _ _ _ f g, g ∘ f } lemma types_hom {α β : Type u} : (α ⟶ β) = (α → β) := rfl lemma types_id (X : Type u) : 𝟙 X = id := rfl lemma types_comp {X Y Z : Type u} (f : X ⟶ Y) (g : Y ⟶ Z) : f ≫ g = g ∘ f := rfl @[simp] lemma types_id_apply (X : Type u) (x : X) : ((𝟙 X) : X → X) x = x := rfl @[simp] lemma types_comp_apply {X Y Z : Type u} (f : X ⟶ Y) (g : Y ⟶ Z) (x : X) : (f ≫ g) x = g (f x) := rfl /-- `as_hom f` helps Lean type check a function as a morphism in the category `Type`. -/ -- Unfortunately without this wrapper we can't use `category_theory` idioms, such as `is_iso f`. abbreviation as_hom {α β : Type u} (f : α → β) : α ⟶ β := f -- If you don't mind some notation you can use fewer keystrokes: notation `↾` f : 200 := as_hom f -- type as \upr in VScode section -- We verify the expected type checking behaviour of `as_hom`. variables (α β γ : Type u) (f : α → β) (g : β → γ) example : α → γ := ↾f ≫ ↾g example [is_iso ↾f] : mono ↾f := by apply_instance example [is_iso ↾f] : ↾f ≫ inv ↾f = 𝟙 α := by simp end namespace functor variables {J : Type u} [category.{v} J] /-- The sections of a functor `J ⥤ Type` are the choices of a point `u j : F.obj j` for each `j`, such that `F.map f (u j) = u j` for every morphism `f : j ⟶ j'`. We later use these to define limits in `Type` and in many concrete categories. -/ def sections (F : J ⥤ Type w) : set (Π j, F.obj j) := { u | ∀ {j j'} (f : j ⟶ j'), F.map f (u j) = u j'} end functor namespace functor_to_types variables {C : Type u} [category.{v} C] (F G H : C ⥤ Type w) {X Y Z : C} variables (σ : F ⟶ G) (τ : G ⟶ H) @[simp] lemma map_comp_apply (f : X ⟶ Y) (g : Y ⟶ Z) (a : F.obj X) : (F.map (f ≫ g)) a = (F.map g) ((F.map f) a) := by simp [types_comp] @[simp] lemma map_id_apply (a : F.obj X) : (F.map (𝟙 X)) a = a := by simp [types_id] lemma naturality (f : X ⟶ Y) (x : F.obj X) : σ.app Y ((F.map f) x) = (G.map f) (σ.app X x) := congr_fun (σ.naturality f) x @[simp] lemma comp (x : F.obj X) : (σ ≫ τ).app X x = τ.app X (σ.app X x) := rfl variables {D : Type u'} [𝒟 : category.{u'} D] (I J : D ⥤ C) (ρ : I ⟶ J) {W : D} @[simp] lemma hcomp (x : (I ⋙ F).obj W) : (ρ ◫ σ).app W x = (G.map (ρ.app W)) (σ.app (I.obj W) x) := rfl @[simp] lemma map_inv_map_hom_apply (f : X ≅ Y) (x : F.obj X) : F.map f.inv (F.map f.hom x) = x := congr_fun (F.map_iso f).hom_inv_id x @[simp] lemma map_hom_map_inv_apply (f : X ≅ Y) (y : F.obj Y) : F.map f.hom (F.map f.inv y) = y := congr_fun (F.map_iso f).inv_hom_id y end functor_to_types /-- The isomorphism between a `Type` which has been `ulift`ed to the same universe, and the original type. -/ def ulift_trivial (V : Type u) : ulift.{u} V ≅ V := by tidy /-- The functor embedding `Type u` into `Type (max u v)`. Write this as `ulift_functor.{5 2}` to get `Type 2 ⥤ Type 5`. -/ def ulift_functor : Type u ⥤ Type (max u v) := { obj := λ X, ulift.{v} X, map := λ X Y f, λ x : ulift.{v} X, ulift.up (f x.down) } @[simp] lemma ulift_functor_map {X Y : Type u} (f : X ⟶ Y) (x : ulift.{v} X) : ulift_functor.map f x = ulift.up (f x.down) := rfl instance ulift_functor_full : full.{u} ulift_functor := { preimage := λ X Y f x, (f (ulift.up x)).down } instance ulift_functor_faithful : faithful ulift_functor := { map_injective' := λ X Y f g p, funext $ λ x, congr_arg ulift.down ((congr_fun p (ulift.up x)) : ((ulift.up (f x)) = (ulift.up (g x)))) } /-- Any term `x` of a type `X` corresponds to a morphism `punit ⟶ X`. -/ -- TODO We should connect this to a general story about concrete categories -- whose forgetful functor is representable. def hom_of_element {X : Type u} (x : X) : punit ⟶ X := λ _, x lemma hom_of_element_eq_iff {X : Type u} (x y : X) : hom_of_element x = hom_of_element y ↔ x = y := ⟨λ H, congr_fun H punit.star, by cc⟩ lemma mono_iff_injective {X Y : Type u} (f : X ⟶ Y) : mono f ↔ function.injective f := begin split, { intros H x x' h, resetI, rw ←hom_of_element_eq_iff at ⊢ h, exact (cancel_mono f).mp h }, { refine λ H, ⟨λ Z g h H₂, _⟩, ext z, replace H₂ := congr_fun H₂ z, exact H H₂ } end lemma epi_iff_surjective {X Y : Type u} (f : X ⟶ Y) : epi f ↔ function.surjective f := begin split, { intros H, let g : Y ⟶ ulift Prop := λ y, ⟨true⟩, let h : Y ⟶ ulift Prop := λ y, ⟨∃ x, f x = y⟩, suffices : f ≫ g = f ≫ h, { resetI, rw cancel_epi at this, intro y, replace this := congr_fun this y, replace this : true = ∃ x, f x = y := congr_arg ulift.down this, rw ←this, trivial }, ext x, change true ↔ ∃ x', f x' = f x, rw true_iff, exact ⟨x, rfl⟩ }, { intro H, constructor, intros Z g h H₂, apply funext, rw ←forall_iff_forall_surj H, intro x, exact (congr_fun H₂ x : _) } end section /-- `of_type_functor m` converts from Lean's `Type`-based `category` to `category_theory`. This allows us to use these functors in category theory. -/ def of_type_functor (m : Type u → Type v) [_root_.functor m] [is_lawful_functor m] : Type u ⥤ Type v := { obj := m, map := λα β, _root_.functor.map, map_id' := assume α, _root_.functor.map_id, map_comp' := assume α β γ f g, funext $ assume a, is_lawful_functor.comp_map f g _ } variables (m : Type u → Type v) [_root_.functor m] [is_lawful_functor m] @[simp] lemma of_type_functor_obj : (of_type_functor m).obj = m := rfl @[simp] lemma of_type_functor_map {α β} (f : α → β) : (of_type_functor m).map f = (_root_.functor.map f : m α → m β) := rfl end end category_theory -- Isomorphisms in Type and equivalences. namespace equiv universe u variables {X Y : Type u} /-- Any equivalence between types in the same universe gives a categorical isomorphism between those types. -/ def to_iso (e : X ≃ Y) : X ≅ Y := { hom := e.to_fun, inv := e.inv_fun, hom_inv_id' := funext e.left_inv, inv_hom_id' := funext e.right_inv } @[simp] lemma to_iso_hom {e : X ≃ Y} : e.to_iso.hom = e := rfl @[simp] lemma to_iso_inv {e : X ≃ Y} : e.to_iso.inv = e.symm := rfl end equiv namespace category_theory.iso open category_theory universe u variables {X Y : Type u} /-- Any isomorphism between types gives an equivalence. -/ def to_equiv (i : X ≅ Y) : X ≃ Y := { to_fun := i.hom, inv_fun := i.inv, left_inv := λ x, congr_fun i.hom_inv_id x, right_inv := λ y, congr_fun i.inv_hom_id y } @[simp] lemma to_equiv_fun (i : X ≅ Y) : (i.to_equiv : X → Y) = i.hom := rfl @[simp] lemma to_equiv_symm_fun (i : X ≅ Y) : (i.to_equiv.symm : Y → X) = i.inv := rfl @[simp] lemma to_equiv_id (X : Type u) : (iso.refl X).to_equiv = equiv.refl X := rfl @[simp] lemma to_equiv_comp {X Y Z : Type u} (f : X ≅ Y) (g : Y ≅ Z) : (f ≪≫ g).to_equiv = f.to_equiv.trans (g.to_equiv) := rfl end category_theory.iso universe u -- We prove `equiv_iso_iso` and then use that to sneakily construct `equiv_equiv_iso`. -- (In this order the proofs are handled by `obviously`.) /-- equivalences (between types in the same universe) are the same as (isomorphic to) isomorphisms of types -/ @[simps] def equiv_iso_iso {X Y : Type u} : (X ≃ Y) ≅ (X ≅ Y) := { hom := λ e, e.to_iso, inv := λ i, i.to_equiv, } /-- equivalences (between types in the same universe) are the same as (equivalent to) isomorphisms of types -/ -- We leave `X` and `Y` as explicit arguments here, because the coercions from `equiv` to a function won't fire without them. def equiv_equiv_iso (X Y : Type u) : (X ≃ Y) ≃ (X ≅ Y) := (equiv_iso_iso).to_equiv @[simp] lemma equiv_equiv_iso_hom {X Y : Type u} (e : X ≃ Y) : (equiv_equiv_iso X Y) e = e.to_iso := rfl @[simp] lemma equiv_equiv_iso_inv {X Y : Type u} (e : X ≅ Y) : (equiv_equiv_iso X Y).symm e = e.to_equiv := rfl
4eeaaca2a9684aa2e2f75990410d061e86cfe07f
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/simp24.lean
3962e035e0d2a4b61c33cafd367cf81ebabafb71
[ "Apache-2.0" ]
permissive
codyroux/lean0.1
1ce92751d664aacff0529e139083304a7bbc8a71
0dc6fb974aa85ed6f305a2f4b10a53a44ee5f0ef
refs/heads/master
1,610,830,535,062
1,402,150,480,000
1,402,150,480,000
19,588,851
2
0
null
null
null
null
UTF-8
Lean
false
false
839
lean
rewrite_set simple add_rewrite eq_id imp_truel imp_truer Nat::add_zeror : simple variables a b : Nat (* local opts = options({"simplifier", "contextual"}, false) local t = parse_lean('λ x, a = a → x = a') local t2, pr = simplify(t, "simple", opts) print(t2) print(pr) get_environment():type_check(pr) *) (* local opts = options({"simplifier", "contextual"}, false) local t = parse_lean('λ x, x = a → x = x') local t2, pr = simplify(t, "simple", opts) print(t2) print(pr) get_environment():type_check(pr) *) (* local opts = options({"simplifier", "contextual"}, false) local t = parse_lean('λ x, x = a + 0 → a = a') local t2, pr = simplify(t, "simple", opts) print(t2) print(pr) *) (* local t = parse_lean('λ x, a + 0 = 1 → x > a') local t2, pr = simplify(t, "simple") print(t2) print(pr) get_environment():type_check(pr) *)
0fe1ddd539d700487745ab9fa3100255b6bc4bd3
e1440579fb0723caf9edf1ed07aee74bbf4f5ce7
/lean-experiments/stumps-learnable/src/stump/sample_complexity.lean
2b7302150f59d1aaf93dee4bf1373d822c47f404
[ "Apache-2.0" ]
permissive
palmskog/coq-proba
1ecc5b7f399894ea14d6094a31a063280a122099
f73e2780871e2a3dd83b7ce9d3aed19f499f07e5
refs/heads/master
1,599,620,504,720
1,572,960,008,000
1,572,960,008,000
221,326,479
0
0
Apache-2.0
1,573,598,769,000
1,573,598,768,000
null
UTF-8
Lean
false
false
2,170
lean
/- Copyright © 2019, Oracle and/or its affiliates. All rights reserved. -/ import analysis.complex.exponential import ..lib.util open real noncomputable def complexity (ε: ℝ) (δ: ℝ) : ℝ := (log(δ) / log(1 - ε)) - (1: nat) lemma complexity_enough: ∀ ε: nnreal, ∀ δ: nnreal, ∀ n: ℕ, ε > (0: nnreal) → ε < (1: nnreal) → δ > (0: nnreal) → δ < (1: nnreal) → (n: ℝ) > (complexity ε δ) → ((1 - ε)^(n+1)) ≤ δ := begin unfold complexity, intros, have h0: ((1: nnreal) - ε) > 0, by change (0 < 1 - ε);rwa[nnreal.coe_pos, nnreal.coe_sub _ _ (le_of_lt (a_1)), sub_pos,←nnreal.coe_lt], rw log_le_log_nnreal, { have h2:= log_pow_nnreal (1 - ε) h0 (n+1), unfold_coes at *, rw ← pow_coe, rw h2, apply mul_le_of_div_le_of_neg, { rw ←exp_lt_exp, simp, rw exp_log _, { rw sub_nnreal, cases ε, exact sub_lt_self 1 a, apply le_of_lt, assumption, }, { rw sub_nnreal, cases ε, exact sub_pos.mpr a_1, apply le_of_lt, assumption, }, }, { apply le_of_lt, clear h2, have nat_cast_1: ∀ x: ℕ, (nat.cast x) + (1:ℝ) = nat.cast(x + 1), exact nat.cast_succ, rw ← nat_cast_1, have minus_nnreal: ∀ x: nnreal, x < 1 → (1 - x).val = 1 - x.val, assume x h, exact nnreal.coe_sub _ _ (le_of_lt h), rw minus_nnreal, swap, assumption, rw gt_from_lt at a_4, have plus_1:= add_lt_add_right a_4 1, simp at plus_1, simp, conv { to_rhs, rw add_comm, skip, }, have silly: (1: ℝ) = nat.cast 1, { unfold nat.cast, simp, }, conv { to_rhs, rw silly, skip, }, assumption, }, }, { refine pow_pos h0 (n + 1), }, { assumption, }, end
389c17af69c49e4829c10dae8f9694a30752aa39
7fc0ec5c526f706107537a6e2e34ac4cdf88d232
/src/group_theory/representation/maschke.lean
3d59277422f1744f97e633e6837be59af42c6a6b
[ "Apache-2.0" ]
permissive
fpvandoorn/group-representations
7440a81f2ac9e0d2defa44dc1643c3167f7a2d99
bd9d72311749187d3bd4f542d5eab83e8341856c
refs/heads/master
1,684,128,141,684
1,623,338,135,000
1,623,338,135,000
256,117,709
0
2
null
null
null
null
UTF-8
Lean
false
false
6,657
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Scott Morrison -/ import data.monoid_algebra import ring_theory.algebra import algebra.invertible import algebra.char_p import linear_algebra.basis /-! # Maschke's theorem We prove Maschke's theorem for finite groups, in the formulation that every submodule of a `k[G]` module has a complement, when `k` is a field with `¬(ring_char k ∣ fintype.card G)`. We do the core computation in greater generality. For any `[comm_ring k]` in which `[invertible (fintype.card G : k)]`, and a `k[G]`-linear map `i : V → W` which admits a `k`-linear retraction `π`, we produce a `k[G]`-linear retraction by taking the average over `G` of the conjugates of `π`. ## Future work It's not so far to give the usual statement, that every finite dimensional representation of a finite group is semisimple (i.e. a direct sum of irreducibles). -/ universes u noncomputable theory open module open monoid_algebra open_locale big_operators section -- At first we work with any `[comm_ring k]`, and add the assumption that -- `[invertible (fintype.card G : k)]` when it is required. variables {k : Type u} [comm_ring k] {G : Type u} [group G] variables {V : Type u} [add_comm_group V] [module (monoid_algebra k G) V] variables {W : Type u} [add_comm_group W] [module (monoid_algebra k G) W] /-! We now do the key calculation in Maschke's theorem. Given `V → W`, an inclusion of `k[G]` modules,, assume we have some retraction `π` (i.e. `∀ v, π (i v) = v`), just as a `k`-linear map. (When `k` is a field, this will be available cheaply, by choosing a basis.) We now construct a retraction of the inclusion as a `k[G]`-linear map, by the formula $$ \frac{1}{|G|} \sum_{g \mem G} g⁻¹ • π(g • -). $$ -/ variables (π : (restrict_scalars k (monoid_algebra k G) W) →ₗ[k] (restrict_scalars k (monoid_algebra k G) V)) include π /-- We define the conjugate of `π` by `g`, as a `k`-linear map. -/ def conjugate (g : G) : (restrict_scalars k (monoid_algebra k G) W) →ₗ[k] (restrict_scalars k (monoid_algebra k G) V) := ((group_smul.linear_map k V g⁻¹).comp π).comp (group_smul.linear_map k W g) variables (i : V →ₗ[monoid_algebra k G] W) (h : ∀ v : V, π (i v) = v) section include h lemma conjugate_i (g : G) (v : V) : (conjugate π g) (i v) = v := begin dsimp [conjugate], simp only [←i.map_smul, h, ←mul_smul, single_mul_single, mul_one, mul_left_inv], change (1 : monoid_algebra k G) • v = v, simp, end end variables [fintype G] /-- The sum of the conjugates of `π` by each element `g : G`, as a `k`-linear map. (We postpone dividing by the size of the group as long as possible.) -/ def sum_of_conjugates : (restrict_scalars k (monoid_algebra k G) W) →ₗ[k] (restrict_scalars k (monoid_algebra k G) V) := ∑ g : G, conjugate π g /-- In fact, the sum over `g : G` of the conjugate of `π` by `g` is a `k[G]`-linear map. -/ def sum_of_conjugates_equivariant : W →ₗ[monoid_algebra k G] V := monoid_algebra.equivariant_of_linear_of_comm (sum_of_conjugates π) (λ g v, begin dsimp [sum_of_conjugates], simp only [linear_map.sum_apply, finset.smul_sum], dsimp [conjugate], conv_lhs { rw [←finset.univ_map_embedding (mul_right_embedding g⁻¹)], simp only [mul_right_embedding], }, simp only [←mul_smul, single_mul_single, mul_inv_rev, mul_one, function.embedding.coe_fn_mk, finset.sum_map, inv_inv, inv_mul_cancel_right], end) section variables [inv : invertible (fintype.card G : k)] include inv section local attribute [instance] linear_map_algebra_module /-- We construct our `k[G]`-linear retraction of `i` as $$ \frac{1}{|G|} \sum_{g \mem G} g⁻¹ • π(g • -). $$ -/ def equivariant_projection : W →ₗ[monoid_algebra k G] V := ⅟(fintype.card G : k) • (sum_of_conjugates_equivariant π) end include h lemma equivariant_projection_condition (v : V) : (equivariant_projection π) (i v) = v := begin rw [equivariant_projection, linear_map_algebra_module.smul_apply, sum_of_conjugates_equivariant, equivariant_of_linear_of_comm_apply, sum_of_conjugates], rw [linear_map.sum_apply], simp only [conjugate_i π i h], rw [finset.sum_const, finset.card_univ, @semimodule.nsmul_eq_smul k _ (restrict_scalars k (monoid_algebra k G) V) _ _ (fintype.card G) v, ←mul_smul, invertible.inv_of_mul_self, one_smul], end end end -- Now we work over a `[field k]`, and replace the assumption `[invertible (fintype.card G : k)]` -- with `¬(ring_char k ∣ fintype.card G)`. variables {k : Type u} [field k] {G : Type u} [fintype G] [group G] variables {V : Type u} [add_comm_group V] [module (monoid_algebra k G) V] variables {W : Type u} [add_comm_group W] [module (monoid_algebra k G) W] lemma monoid_algebra.exists_left_inverse_of_injective (not_dvd : ¬(ring_char k ∣ fintype.card G)) (f : V →ₗ[monoid_algebra k G] W) (hf_inj : f.ker = ⊥) : ∃ (g : W →ₗ[monoid_algebra k G] V), g.comp f = linear_map.id := begin let E := linear_map.exists_left_inverse_of_injective (by convert f.restrict_scalars k) (by simp [hf_inj]), fsplit, haveI : invertible (fintype.card G : k) := invertible_of_ring_char_not_dvd not_dvd, exact equivariant_projection (classical.some E), { ext v, apply equivariant_projection_condition, intro v, have := classical.some_spec E, have := congr_arg linear_map.to_fun this, exact congr_fun this v, } end lemma monoid_algebra.submodule.exists_is_compl (not_dvd : ¬(ring_char k ∣ fintype.card G)) (p : submodule (monoid_algebra k G) V) : ∃ q : submodule (monoid_algebra k G) V, is_compl p q := let ⟨f, hf⟩ := monoid_algebra.exists_left_inverse_of_injective not_dvd p.subtype p.ker_subtype in ⟨f.ker, linear_map.is_compl_of_proj $ linear_map.ext_iff.1 hf⟩ /-- An `R`-module `M` is irreducible if every invariant submodule is either `⊥` or `⊤`. -/ variables (i : V →ₗ[monoid_algebra k G] W) def is_zero_map : Prop := ∀ v : V, i v = 0 lemma check_zero : is_zero_map (0 • i) := begin unfold is_zero_map, intro, simp [i.map_smul] end def invariant_submodule (N : submodule (monoid_algebra k G) W) : Prop := ∀ v : W, ∀ g : G, (group_smul.linear_map k W g) v = v def irreducible (M : Type u) [add_comm_group M] [module (monoid_algebra k G) M] : Prop := ∀ N : submodule (monoid_algebra k G) M, N = ⊥ ∨ N = ⊤ #print irreducible lemma schur (i : V →ₗ[monoid_algebra k G] W) : irreducible V ∧ irreducible W → (function.bijective i ↔ ¬ is_zero_map i) := sorry
8557f80de48cdc807b0e759ce0ae2264c3f5d06e
432d948a4d3d242fdfb44b81c9e1b1baacd58617
/src/group_theory/subgroup.lean
17b5d5e6fbced557363ce404d458ef62729a4197
[ "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
68,961
lean
/- Copyright (c) 2020 Kexing Ying. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kexing Ying -/ import group_theory.submonoid import algebra.group.conj import algebra.pointwise import order.atoms /-! # Subgroups This file defines multiplicative and additive subgroups as an extension of submonoids, in a bundled form (unbundled subgroups are in `deprecated/subgroups.lean`). We prove subgroups of a group form a complete lattice, and results about images and preimages of subgroups under group homomorphisms. The bundled subgroups use bundled monoid homomorphisms. There are also theorems about the subgroups generated by an element or a subset of a group, defined both inductively and as the infimum of the set of subgroups containing a given element/subset. Special thanks goes to Amelia Livingston and Yury Kudryashov for their help and inspiration. ## Main definitions Notation used here: - `G N` are `group`s - `A` is an `add_group` - `H K` are `subgroup`s of `G` or `add_subgroup`s of `A` - `x` is an element of type `G` or type `A` - `f g : N →* G` are group homomorphisms - `s k` are sets of elements of type `G` Definitions in the file: * `subgroup G` : the type of subgroups of a group `G` * `add_subgroup A` : the type of subgroups of an additive group `A` * `complete_lattice (subgroup G)` : the subgroups of `G` form a complete lattice * `subgroup.closure k` : the minimal subgroup that includes the set `k` * `subgroup.subtype` : the natural group homomorphism from a subgroup of group `G` to `G` * `subgroup.gi` : `closure` forms a Galois insertion with the coercion to set * `subgroup.comap H f` : the preimage of a subgroup `H` along the group homomorphism `f` is also a subgroup * `subgroup.map f H` : the image of a subgroup `H` along the group homomorphism `f` is also a subgroup * `subgroup.prod H K` : the product of subgroups `H`, `K` of groups `G`, `N` respectively, `H × K` is a subgroup of `G × N` * `monoid_hom.range f` : the range of the group homomorphism `f` is a subgroup * `monoid_hom.ker f` : the kernel of a group homomorphism `f` is the subgroup of elements `x : G` such that `f x = 1` * `monoid_hom.eq_locus f g` : given group homomorphisms `f`, `g`, the elements of `G` such that `f x = g x` form a subgroup of `G` * `is_simple_group G` : a class indicating that a group has exactly two normal subgroups ## Implementation notes Subgroup inclusion is denoted `≤` rather than `⊆`, although `∈` is defined as membership of a subgroup's underlying set. ## Tags subgroup, subgroups -/ open_locale big_operators variables {G : Type*} [group G] variables {A : Type*} [add_group A] set_option old_structure_cmd true /-- A subgroup of a group `G` is a subset containing 1, closed under multiplication and closed under multiplicative inverse. -/ structure subgroup (G : Type*) [group G] extends submonoid G := (inv_mem' {x} : x ∈ carrier → x⁻¹ ∈ carrier) /-- An additive subgroup of an additive group `G` is a subset containing 0, closed under addition and additive inverse. -/ structure add_subgroup (G : Type*) [add_group G] extends add_submonoid G:= (neg_mem' {x} : x ∈ carrier → -x ∈ carrier) attribute [to_additive] subgroup attribute [to_additive add_subgroup.to_add_submonoid] subgroup.to_submonoid /-- Reinterpret a `subgroup` as a `submonoid`. -/ add_decl_doc subgroup.to_submonoid /-- Reinterpret an `add_subgroup` as an `add_submonoid`. -/ add_decl_doc add_subgroup.to_add_submonoid namespace subgroup @[to_additive] instance : set_like (subgroup G) G := ⟨subgroup.carrier, λ p q h, by cases p; cases q; congr'⟩ @[simp, to_additive] lemma mem_carrier {s : subgroup G} {x : G} : x ∈ s.carrier ↔ x ∈ s := iff.rfl /-- See Note [custom simps projection] -/ @[to_additive "See Note [custom simps projection]"] def simps.coe (S : subgroup G) : set G := S initialize_simps_projections subgroup (carrier → coe) initialize_simps_projections add_subgroup (carrier → coe) @[simp, to_additive] lemma coe_to_submonoid (K : subgroup G) : (K.to_submonoid : set G) = K := rfl @[simp, to_additive] lemma mem_to_submonoid (K : subgroup G) (x : G) : x ∈ K.to_submonoid ↔ x ∈ K := iff.rfl @[to_additive] instance (K : subgroup G) [d : decidable_pred (∈ K)] [fintype G] : fintype K := show fintype {g : G // g ∈ K}, from infer_instance @[to_additive] theorem to_submonoid_injective : function.injective (to_submonoid : subgroup G → submonoid G) := λ p q h, set_like.ext'_iff.2 (show _, from set_like.ext'_iff.1 h) @[simp, to_additive] theorem to_submonoid_eq {p q : subgroup G} : p.to_submonoid = q.to_submonoid ↔ p = q := to_submonoid_injective.eq_iff @[mono, to_additive] lemma to_submonoid_strict_mono : strict_mono (to_submonoid : subgroup G → submonoid G) := λ _ _, id @[mono, to_additive] lemma to_submonoid_mono : monotone (to_submonoid : subgroup G → submonoid G) := to_submonoid_strict_mono.monotone @[simp, to_additive] lemma to_submonoid_le {p q : subgroup G} : p.to_submonoid ≤ q.to_submonoid ↔ p ≤ q := iff.rfl end subgroup /-! ### Conversion to/from `additive`/`multiplicative` -/ section mul_add /-- Supgroups of a group `G` are isomorphic to additive subgroups of `additive G`. -/ @[simps] def subgroup.to_add_subgroup : subgroup G ≃o add_subgroup (additive G) := { to_fun := λ S, { neg_mem' := S.inv_mem', ..S.to_submonoid.to_add_submonoid }, inv_fun := λ S, { inv_mem' := S.neg_mem', ..S.to_add_submonoid.to_submonoid' }, left_inv := λ x, by cases x; refl, right_inv := λ x, by cases x; refl, map_rel_iff' := λ a b, iff.rfl, } /-- Additive subgroup of an additive group `additive G` are isomorphic to subgroup of `G`. -/ abbreviation add_subgroup.to_subgroup' : add_subgroup (additive G) ≃o subgroup G := subgroup.to_add_subgroup.symm /-- Additive supgroups of an additive group `A` are isomorphic to subgroups of `multiplicative A`. -/ @[simps] def add_subgroup.to_subgroup : add_subgroup A ≃o subgroup (multiplicative A) := { to_fun := λ S, { inv_mem' := S.neg_mem', ..S.to_add_submonoid.to_submonoid }, inv_fun := λ S, { neg_mem' := S.inv_mem', ..S.to_submonoid.to_add_submonoid' }, left_inv := λ x, by cases x; refl, right_inv := λ x, by cases x; refl, map_rel_iff' := λ a b, iff.rfl, } /-- Subgroups of an additive group `multiplicative A` are isomorphic to additive subgroups of `A`. -/ abbreviation subgroup.to_add_subgroup' : subgroup (multiplicative A) ≃o add_subgroup A := add_subgroup.to_subgroup.symm end mul_add namespace subgroup variables (H K : subgroup G) /-- Copy of a subgroup with a new `carrier` equal to the old one. Useful to fix definitional equalities.-/ @[to_additive "Copy of an additive subgroup with a new `carrier` equal to the old one. Useful to fix definitional equalities"] protected def copy (K : subgroup G) (s : set G) (hs : s = K) : subgroup G := { carrier := s, one_mem' := hs.symm ▸ K.one_mem', mul_mem' := hs.symm ▸ K.mul_mem', inv_mem' := hs.symm ▸ K.inv_mem' } /-- Two subgroups are equal if they have the same elements. -/ @[ext, to_additive "Two `add_subgroup`s are equal if they have the same elements."] theorem ext {H K : subgroup G} (h : ∀ x, x ∈ H ↔ x ∈ K) : H = K := set_like.ext h attribute [ext] add_subgroup.ext /-- A subgroup contains the group's 1. -/ @[to_additive "An `add_subgroup` contains the group's 0."] theorem one_mem : (1 : G) ∈ H := H.one_mem' /-- A subgroup is closed under multiplication. -/ @[to_additive "An `add_subgroup` is closed under addition."] theorem mul_mem {x y : G} : x ∈ H → y ∈ H → x * y ∈ H := λ hx hy, H.mul_mem' hx hy /-- A subgroup is closed under inverse. -/ @[to_additive "An `add_subgroup` is closed under inverse."] theorem inv_mem {x : G} : x ∈ H → x⁻¹ ∈ H := λ hx, H.inv_mem' hx /-- A subgroup is closed under division. -/ @[to_additive "An `add_subgroup` is closed under subtraction."] theorem div_mem {x y : G} (hx : x ∈ H) (hy : y ∈ H) : x / y ∈ H := by simpa only [div_eq_mul_inv] using H.mul_mem' hx (H.inv_mem' hy) @[simp, to_additive] theorem inv_mem_iff {x : G} : x⁻¹ ∈ H ↔ x ∈ H := ⟨λ h, inv_inv x ▸ H.inv_mem h, H.inv_mem⟩ @[simp, to_additive] theorem inv_coe_set : (H : set G)⁻¹ = H := by { ext, simp, } @[to_additive] lemma mul_mem_cancel_right {x y : G} (h : x ∈ H) : y * x ∈ H ↔ y ∈ H := ⟨λ hba, by simpa using H.mul_mem hba (H.inv_mem h), λ hb, H.mul_mem hb h⟩ @[to_additive] lemma mul_mem_cancel_left {x y : G} (h : x ∈ H) : x * y ∈ H ↔ y ∈ H := ⟨λ hab, by simpa using H.mul_mem (H.inv_mem h) hab, H.mul_mem h⟩ /-- Product of a list of elements in a subgroup is in the subgroup. -/ @[to_additive "Sum of a list of elements in an `add_subgroup` is in the `add_subgroup`."] lemma list_prod_mem {l : list G} : (∀ x ∈ l, x ∈ K) → l.prod ∈ K := K.to_submonoid.list_prod_mem /-- Product of a multiset of elements in a subgroup of a `comm_group` is in the subgroup. -/ @[to_additive "Sum of a multiset of elements in an `add_subgroup` of an `add_comm_group` is in the `add_subgroup`."] lemma multiset_prod_mem {G} [comm_group G] (K : subgroup G) (g : multiset G) : (∀ a ∈ g, a ∈ K) → g.prod ∈ K := K.to_submonoid.multiset_prod_mem g /-- Product of elements of a subgroup of a `comm_group` indexed by a `finset` is in the subgroup. -/ @[to_additive "Sum of elements in an `add_subgroup` of an `add_comm_group` indexed by a `finset` is in the `add_subgroup`."] lemma prod_mem {G : Type*} [comm_group G] (K : subgroup G) {ι : Type*} {t : finset ι} {f : ι → G} (h : ∀ c ∈ t, f c ∈ K) : ∏ c in t, f c ∈ K := K.to_submonoid.prod_mem h lemma pow_mem {x : G} (hx : x ∈ K) : ∀ n : ℕ, x ^ n ∈ K := K.to_submonoid.pow_mem hx lemma gpow_mem {x : G} (hx : x ∈ K) : ∀ n : ℤ, x ^ n ∈ K | (n : ℕ) := by { rw [gpow_coe_nat], exact pow_mem _ hx n } | -[1+ n] := by { rw [gpow_neg_succ_of_nat], exact K.inv_mem (K.pow_mem hx n.succ) } /-- Construct a subgroup from a nonempty set that is closed under division. -/ @[to_additive "Construct a subgroup from a nonempty set that is closed under subtraction"] def of_div (s : set G) (hsn : s.nonempty) (hs : ∀ x y ∈ s, x * y⁻¹ ∈ s) : subgroup G := have one_mem : (1 : G) ∈ s, from let ⟨x, hx⟩ := hsn in by simpa using hs x x hx hx, have inv_mem : ∀ x, x ∈ s → x⁻¹ ∈ s, from λ x hx, by simpa using hs 1 x one_mem hx, { carrier := s, one_mem' := one_mem, inv_mem' := inv_mem, mul_mem' := λ x y hx hy, by simpa using hs x y⁻¹ hx (inv_mem y hy) } /-- A subgroup of a group inherits a multiplication. -/ @[to_additive "An `add_subgroup` of an `add_group` inherits an addition."] instance has_mul : has_mul H := H.to_submonoid.has_mul /-- A subgroup of a group inherits a 1. -/ @[to_additive "An `add_subgroup` of an `add_group` inherits a zero."] instance has_one : has_one H := H.to_submonoid.has_one /-- A subgroup of a group inherits an inverse. -/ @[to_additive "A `add_subgroup` of a `add_group` inherits an inverse."] instance has_inv : has_inv H := ⟨λ a, ⟨a⁻¹, H.inv_mem a.2⟩⟩ /-- A subgroup of a group inherits a division -/ @[to_additive "An `add_subgroup` of an `add_group` inherits a subtraction."] instance has_div : has_div H := ⟨λ a b, ⟨a / b, H.div_mem a.2 b.2⟩⟩ @[simp, norm_cast, to_additive] lemma coe_mul (x y : H) : (↑(x * y) : G) = ↑x * ↑y := rfl @[simp, norm_cast, to_additive] lemma coe_one : ((1 : H) : G) = 1 := rfl @[simp, norm_cast, to_additive] lemma coe_inv (x : H) : ↑(x⁻¹ : H) = (x⁻¹ : G) := rfl @[simp, norm_cast, to_additive] lemma coe_mk (x : G) (hx : x ∈ H) : ((⟨x, hx⟩ : H) : G) = x := rfl attribute [norm_cast] add_subgroup.coe_add add_subgroup.coe_zero add_subgroup.coe_neg add_subgroup.coe_mk /-- A subgroup of a group inherits a group structure. -/ @[to_additive "An `add_subgroup` of an `add_group` inherits an `add_group` structure."] instance to_group {G : Type*} [group G] (H : subgroup G) : group H := subtype.coe_injective.group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) /-- A subgroup of a `comm_group` is a `comm_group`. -/ @[to_additive "An `add_subgroup` of an `add_comm_group` is an `add_comm_group`."] instance to_comm_group {G : Type*} [comm_group G] (H : subgroup G) : comm_group H := subtype.coe_injective.comm_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) /-- A subgroup of an `ordered_comm_group` is an `ordered_comm_group`. -/ @[to_additive "An `add_subgroup` of an `add_ordered_comm_group` is an `add_ordered_comm_group`."] instance to_ordered_comm_group {G : Type*} [ordered_comm_group G] (H : subgroup G) : ordered_comm_group H := subtype.coe_injective.ordered_comm_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) /-- A subgroup of a `linear_ordered_comm_group` is a `linear_ordered_comm_group`. -/ @[to_additive "An `add_subgroup` of a `linear_ordered_add_comm_group` is a `linear_ordered_add_comm_group`."] instance to_linear_ordered_comm_group {G : Type*} [linear_ordered_comm_group G] (H : subgroup G) : linear_ordered_comm_group H := subtype.coe_injective.linear_ordered_comm_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) /-- The natural group hom from a subgroup of group `G` to `G`. -/ @[to_additive "The natural group hom from an `add_subgroup` of `add_group` `G` to `G`."] def subtype : H →* G := ⟨coe, rfl, λ _ _, rfl⟩ @[simp, to_additive] theorem coe_subtype : ⇑H.subtype = coe := rfl @[simp, norm_cast] lemma coe_pow (x : H) (n : ℕ) : ((x ^ n : H) : G) = x ^ n := coe_subtype H ▸ monoid_hom.map_pow _ _ _ @[simp, norm_cast] lemma coe_gpow (x : H) (n : ℤ) : ((x ^ n : H) : G) = x ^ n := coe_subtype H ▸ monoid_hom.map_gpow _ _ _ /-- The inclusion homomorphism from a subgroup `H` contained in `K` to `K`. -/ @[to_additive "The inclusion homomorphism from a additive subgroup `H` contained in `K` to `K`."] def inclusion {H K : subgroup G} (h : H ≤ K) : H →* K := monoid_hom.mk' (λ x, ⟨x, h x.prop⟩) (λ ⟨a, ha⟩ ⟨b, hb⟩, rfl) @[simp, to_additive] lemma coe_inclusion {H K : subgroup G} {h : H ≤ K} (a : H) : (inclusion h a : G) = a := by { cases a, simp only [inclusion, coe_mk, monoid_hom.mk'_apply] } @[simp, to_additive] lemma subtype_comp_inclusion {H K : subgroup G} (hH : H ≤ K) : K.subtype.comp (inclusion hH) = H.subtype := by { ext, simp } /-- The subgroup `G` of the group `G`. -/ @[to_additive "The `add_subgroup G` of the `add_group G`."] instance : has_top (subgroup G) := ⟨{ inv_mem' := λ _ _, set.mem_univ _ , .. (⊤ : submonoid G) }⟩ /-- The trivial subgroup `{1}` of an group `G`. -/ @[to_additive "The trivial `add_subgroup` `{0}` of an `add_group` `G`."] instance : has_bot (subgroup G) := ⟨{ inv_mem' := λ _, by simp *, .. (⊥ : submonoid G) }⟩ @[to_additive] instance : inhabited (subgroup G) := ⟨⊥⟩ @[simp, to_additive] lemma mem_bot {x : G} : x ∈ (⊥ : subgroup G) ↔ x = 1 := iff.rfl @[simp, to_additive] lemma mem_top (x : G) : x ∈ (⊤ : subgroup G) := set.mem_univ x @[simp, to_additive] lemma coe_top : ((⊤ : subgroup G) : set G) = set.univ := rfl @[simp, to_additive] lemma coe_bot : ((⊥ : subgroup G) : set G) = {1} := rfl @[to_additive] lemma eq_bot_iff_forall : H = ⊥ ↔ ∀ x ∈ H, x = (1 : G) := begin rw set_like.ext'_iff, simp only [coe_bot, set.eq_singleton_iff_unique_mem, set_like.mem_coe, H.one_mem, true_and], end @[to_additive] lemma eq_bot_of_subsingleton [subsingleton H] : H = ⊥ := begin rw subgroup.eq_bot_iff_forall, intros y hy, rw [← subgroup.coe_mk H y hy, subsingleton.elim (⟨y, hy⟩ : H) 1, subgroup.coe_one], end @[to_additive] instance fintype_bot : fintype (⊥ : subgroup G) := ⟨{1}, by {rintro ⟨x, ⟨hx⟩⟩, exact finset.mem_singleton_self _}⟩ @[simp] lemma _root_.add_subgroup.card_bot : fintype.card (⊥ : add_subgroup A) = 1 := fintype.card_eq_one_iff.2 ⟨⟨(0 : A), set.mem_singleton 0⟩, λ ⟨y, hy⟩, subtype.eq $ add_subgroup.mem_bot.1 hy⟩ -- `@[to_additive]` doesn't work, because it converts the `1 : ℕ` to `0`. @[simp] lemma card_bot : fintype.card (⊥ : subgroup G) = 1 := fintype.card_eq_one_iff.2 ⟨⟨(1 : G), set.mem_singleton 1⟩, λ ⟨y, hy⟩, subtype.eq $ subgroup.mem_bot.1 hy⟩ @[to_additive] lemma eq_top_of_card_eq [fintype H] [fintype G] (h : fintype.card H = fintype.card G) : H = ⊤ := begin haveI : fintype (H : set G) := ‹fintype H›, rw [set_like.ext'_iff, coe_top, ← finset.coe_univ, ← (H : set G).coe_to_finset, finset.coe_inj, ← finset.card_eq_iff_eq_univ, ← h, set.to_finset_card], congr end @[to_additive] lemma nontrivial_iff_exists_ne_one (H : subgroup G) : nontrivial H ↔ ∃ x ∈ H, x ≠ (1:G) := subtype.nontrivial_iff_exists_ne (λ x, x ∈ H) (1 : H) /-- A subgroup is either the trivial subgroup or nontrivial. -/ @[to_additive] lemma bot_or_nontrivial (H : subgroup G) : H = ⊥ ∨ nontrivial H := begin classical, by_cases h : ∀ x ∈ H, x = (1 : G), { left, exact H.eq_bot_iff_forall.mpr h }, { right, push_neg at h, simpa [nontrivial_iff_exists_ne_one] using h }, end /-- A subgroup is either the trivial subgroup or contains a nonzero element. -/ @[to_additive] lemma bot_or_exists_ne_one (H : subgroup G) : H = ⊥ ∨ ∃ x ∈ H, x ≠ (1:G) := begin convert H.bot_or_nontrivial, rw nontrivial_iff_exists_ne_one end /-- The inf of two subgroups is their intersection. -/ @[to_additive "The inf of two `add_subgroups`s is their intersection."] instance : has_inf (subgroup G) := ⟨λ H₁ H₂, { inv_mem' := λ _ ⟨hx, hx'⟩, ⟨H₁.inv_mem hx, H₂.inv_mem hx'⟩, .. H₁.to_submonoid ⊓ H₂.to_submonoid }⟩ @[simp, to_additive] lemma coe_inf (p p' : subgroup G) : ((p ⊓ p' : subgroup G) : set G) = p ∩ p' := rfl @[simp, to_additive] lemma mem_inf {p p' : subgroup G} {x : G} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl @[to_additive] instance : has_Inf (subgroup G) := ⟨λ s, { inv_mem' := λ x hx, set.mem_bInter $ λ i h, i.inv_mem (by apply set.mem_bInter_iff.1 hx i h), .. (⨅ S ∈ s, subgroup.to_submonoid S).copy (⋂ S ∈ s, ↑S) (by simp) }⟩ @[simp, to_additive] lemma coe_Inf (H : set (subgroup G)) : ((Inf H : subgroup G) : set G) = ⋂ s ∈ H, ↑s := rfl attribute [norm_cast] coe_Inf add_subgroup.coe_Inf @[simp, to_additive] lemma mem_Inf {S : set (subgroup G)} {x : G} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p := set.mem_bInter_iff @[to_additive] lemma mem_infi {ι : Sort*} {S : ι → subgroup G} {x : G} : (x ∈ ⨅ i, S i) ↔ ∀ i, x ∈ S i := by simp only [infi, mem_Inf, set.forall_range_iff] @[simp, to_additive] lemma coe_infi {ι : Sort*} {S : ι → subgroup G} : (↑(⨅ i, S i) : set G) = ⋂ i, S i := by simp only [infi, coe_Inf, set.bInter_range] attribute [norm_cast] coe_infi add_subgroup.coe_infi /-- Subgroups of a group form a complete lattice. -/ @[to_additive "The `add_subgroup`s of an `add_group` form a complete lattice."] instance : complete_lattice (subgroup G) := { bot := (⊥), bot_le := λ S x hx, (mem_bot.1 hx).symm ▸ S.one_mem, top := (⊤), le_top := λ S x hx, mem_top x, inf := (⊓), le_inf := λ a b c ha hb x hx, ⟨ha hx, hb hx⟩, inf_le_left := λ a b x, and.left, inf_le_right := λ a b x, and.right, .. complete_lattice_of_Inf (subgroup G) $ λ s, is_glb.of_image (λ H K, show (H : set G) ≤ K ↔ H ≤ K, from set_like.coe_subset_coe) is_glb_binfi } @[to_additive] lemma mem_sup_left {S T : subgroup G} : ∀ {x : G}, x ∈ S → x ∈ S ⊔ T := show S ≤ S ⊔ T, from le_sup_left @[to_additive] lemma mem_sup_right {S T : subgroup G} : ∀ {x : G}, x ∈ T → x ∈ S ⊔ T := show T ≤ S ⊔ T, from le_sup_right @[to_additive] lemma mem_supr_of_mem {ι : Type*} {S : ι → subgroup G} (i : ι) : ∀ {x : G}, x ∈ S i → x ∈ supr S := show S i ≤ supr S, from le_supr _ _ @[to_additive] lemma mem_Sup_of_mem {S : set (subgroup G)} {s : subgroup G} (hs : s ∈ S) : ∀ {x : G}, x ∈ s → x ∈ Sup S := show s ≤ Sup S, from le_Sup hs @[to_additive] lemma subsingleton_iff : subsingleton G ↔ subsingleton (subgroup G) := ⟨ λ h, by exactI ⟨λ x y, subgroup.ext $ λ i, subsingleton.elim 1 i ▸ by simp [subgroup.one_mem]⟩, λ h, by exactI ⟨λ x y, have ∀ i : G, i = 1 := λ i, mem_bot.mp $ subsingleton.elim (⊤ : subgroup G) ⊥ ▸ mem_top i, (this x).trans (this y).symm⟩⟩ @[to_additive] lemma nontrivial_iff : nontrivial G ↔ nontrivial (subgroup G) := not_iff_not.mp ( (not_nontrivial_iff_subsingleton.trans subsingleton_iff).trans not_nontrivial_iff_subsingleton.symm) @[to_additive] instance [subsingleton G] : unique (subgroup G) := ⟨⟨⊥⟩, λ a, @subsingleton.elim _ (subsingleton_iff.mp ‹_›) a _⟩ @[to_additive] instance [nontrivial G] : nontrivial (subgroup G) := nontrivial_iff.mp ‹_› @[to_additive] lemma eq_top_iff' : H = ⊤ ↔ ∀ x : G, x ∈ H := eq_top_iff.trans ⟨λ h m, h $ mem_top m, λ h m _, h m⟩ /-- The `subgroup` generated by a set. -/ @[to_additive "The `add_subgroup` generated by a set"] def closure (k : set G) : subgroup G := Inf {K | k ⊆ K} variable {k : set G} @[to_additive] lemma mem_closure {x : G} : x ∈ closure k ↔ ∀ K : subgroup G, k ⊆ K → x ∈ K := mem_Inf /-- The subgroup generated by a set includes the set. -/ @[simp, to_additive "The `add_subgroup` generated by a set includes the set."] lemma subset_closure : k ⊆ closure k := λ x hx, mem_closure.2 $ λ K hK, hK hx open set /-- A subgroup `K` includes `closure k` if and only if it includes `k`. -/ @[simp, to_additive "An additive subgroup `K` includes `closure k` if and only if it includes `k`"] lemma closure_le : closure k ≤ K ↔ k ⊆ K := ⟨subset.trans subset_closure, λ h, Inf_le h⟩ @[to_additive] lemma closure_eq_of_le (h₁ : k ⊆ K) (h₂ : K ≤ closure k) : closure k = K := le_antisymm ((closure_le $ K).2 h₁) h₂ /-- An induction principle for closure membership. If `p` holds for `1` and all elements of `k`, and is preserved under multiplication and inverse, then `p` holds for all elements of the closure of `k`. -/ @[to_additive "An induction principle for additive closure membership. If `p` holds for `0` and all elements of `k`, and is preserved under addition and inverses, then `p` holds for all elements of the additive closure of `k`."] lemma closure_induction {p : G → Prop} {x} (h : x ∈ closure k) (Hk : ∀ x ∈ k, p x) (H1 : p 1) (Hmul : ∀ x y, p x → p y → p (x * y)) (Hinv : ∀ x, p x → p x⁻¹) : p x := (@closure_le _ _ ⟨p, H1, Hmul, Hinv⟩ _).2 Hk h attribute [elab_as_eliminator] subgroup.closure_induction add_subgroup.closure_induction /-- An induction principle on elements of the subtype `subgroup.closure`. If `p` holds for `1` and all elements of `k`, and is preserved under multiplication and inverse, then `p` holds for all elements `x : closure k`. The difference with `subgroup.closure_induction` is that this acts on the subtype. -/ @[to_additive "An induction principle on elements of the subtype `add_subgroup.closure`. If `p` holds for `0` and all elements of `k`, and is preserved under addition and negation, then `p` holds for all elements `x : closure k`. The difference with `add_subgroup.closure_induction` is that this acts on the subtype."] lemma closure_induction' (k : set G) {p : closure k → Prop} (Hk : ∀ x (h : x ∈ k), p ⟨x, subset_closure h⟩) (H1 : p 1) (Hmul : ∀ x y, p x → p y → p (x * y)) (Hinv : ∀ x, p x → p x⁻¹) (x : closure k) : p x := subtype.rec_on x $ λ x hx, begin refine exists.elim _ (λ (hx : x ∈ closure k) (hc : p ⟨x, hx⟩), hc), exact closure_induction hx (λ x hx, ⟨subset_closure hx, Hk x hx⟩) ⟨one_mem _, H1⟩ (λ x y hx hy, exists.elim hx $ λ hx' hx, exists.elim hy $ λ hy' hy, ⟨mul_mem _ hx' hy', Hmul _ _ hx hy⟩) (λ x hx, exists.elim hx $ λ hx' hx, ⟨inv_mem _ hx', Hinv _ hx⟩), end attribute [elab_as_eliminator] subgroup.closure_induction' add_subgroup.closure_induction' variable (G) /-- `closure` forms a Galois insertion with the coercion to set. -/ @[to_additive "`closure` forms a Galois insertion with the coercion to set."] protected def gi : galois_insertion (@closure G _) coe := { choice := λ s _, closure s, gc := λ s t, @closure_le _ _ t s, le_l_u := λ s, subset_closure, choice_eq := λ s h, rfl } variable {G} /-- Subgroup closure of a set is monotone in its argument: if `h ⊆ k`, then `closure h ≤ closure k`. -/ @[to_additive "Additive subgroup closure of a set is monotone in its argument: if `h ⊆ k`, then `closure h ≤ closure k`"] lemma closure_mono ⦃h k : set G⦄ (h' : h ⊆ k) : closure h ≤ closure k := (subgroup.gi G).gc.monotone_l h' /-- Closure of a subgroup `K` equals `K`. -/ @[simp, to_additive "Additive closure of an additive subgroup `K` equals `K`"] lemma closure_eq : closure (K : set G) = K := (subgroup.gi G).l_u_eq K @[simp, to_additive] lemma closure_empty : closure (∅ : set G) = ⊥ := (subgroup.gi G).gc.l_bot @[simp, to_additive] lemma closure_univ : closure (univ : set G) = ⊤ := @coe_top G _ ▸ closure_eq ⊤ @[to_additive] lemma closure_union (s t : set G) : closure (s ∪ t) = closure s ⊔ closure t := (subgroup.gi G).gc.l_sup @[to_additive] lemma closure_Union {ι} (s : ι → set G) : closure (⋃ i, s i) = ⨆ i, closure (s i) := (subgroup.gi G).gc.l_supr @[to_additive] lemma closure_eq_bot_iff (G : Type*) [group G] (S : set G) : closure S = ⊥ ↔ S ⊆ {1} := by { rw [← le_bot_iff], exact closure_le _} /-- The subgroup generated by an element of a group equals the set of integer number powers of the element. -/ lemma mem_closure_singleton {x y : G} : y ∈ closure ({x} : set G) ↔ ∃ n : ℤ, x ^ n = y := begin refine ⟨λ hy, closure_induction hy _ _ _ _, λ ⟨n, hn⟩, hn ▸ gpow_mem _ (subset_closure $ mem_singleton x) n⟩, { intros y hy, rw [eq_of_mem_singleton hy], exact ⟨1, gpow_one x⟩ }, { exact ⟨0, gpow_zero x⟩ }, { rintros _ _ ⟨n, rfl⟩ ⟨m, rfl⟩, exact ⟨n + m, gpow_add x n m⟩ }, rintros _ ⟨n, rfl⟩, exact ⟨-n, gpow_neg x n⟩ end lemma closure_singleton_one : closure ({1} : set G) = ⊥ := by simp [eq_bot_iff_forall, mem_closure_singleton] @[simp, to_additive] lemma inv_subset_closure (S : set G) : S⁻¹ ⊆ closure S := begin intros s hs, rw [set_like.mem_coe, ←subgroup.inv_mem_iff], exact subset_closure (mem_inv.mp hs), end @[simp, to_additive] lemma closure_inv (S : set G) : closure S⁻¹ = closure S := begin refine le_antisymm ((subgroup.closure_le _).2 _) ((subgroup.closure_le _).2 _), { exact inv_subset_closure S }, { simpa only [set.inv_inv] using inv_subset_closure S⁻¹ }, end @[to_additive] lemma closure_to_submonoid (S : set G) : (closure S).to_submonoid = submonoid.closure (S ∪ S⁻¹) := begin refine le_antisymm _ (submonoid.closure_le.2 _), { intros x hx, refine closure_induction hx (λ x hx, submonoid.closure_mono (subset_union_left S S⁻¹) (submonoid.subset_closure hx)) (submonoid.one_mem _) (λ x y hx hy, submonoid.mul_mem _ hx hy) (λ x hx, _), rwa [←submonoid.mem_closure_inv, set.union_inv, set.inv_inv, set.union_comm] }, { simp only [true_and, coe_to_submonoid, union_subset_iff, subset_closure, inv_subset_closure] } end /-- An induction principle for closure membership. If `p` holds for `1` and all elements of `k` and their inverse, and is preserved under multiplication, then `p` holds for all elements of the closure of `k`. -/ @[to_additive "An induction principle for additive closure membership. If `p` holds for `0` and all elements of `k` and their negation, and is preserved under addition, then `p` holds for all elements of the additive closure of `k`."] lemma closure_induction'' {p : G → Prop} {x} (h : x ∈ closure k) (Hk : ∀ x ∈ k, p x) (Hk_inv : ∀ x ∈ k, p x⁻¹) (H1 : p 1) (Hmul : ∀ x y, p x → p y → p (x * y)) : p x := begin rw [← mem_to_submonoid, closure_to_submonoid k] at h, refine submonoid.closure_induction h (λ x hx, _) H1 (λ x y hx hy, Hmul x y hx hy), { rw [mem_union, mem_inv] at hx, cases hx with mem invmem, { exact Hk x mem }, { rw [← inv_inv x], exact Hk_inv _ invmem } }, end @[to_additive] lemma mem_supr_of_directed {ι} [hι : nonempty ι] {K : ι → subgroup G} (hK : directed (≤) K) {x : G} : x ∈ (supr K : subgroup G) ↔ ∃ i, x ∈ K i := begin refine ⟨_, λ ⟨i, hi⟩, (set_like.le_def.1 $ le_supr K i) hi⟩, suffices : x ∈ closure (⋃ i, (K i : set G)) → ∃ i, x ∈ K i, by simpa only [closure_Union, closure_eq (K _)] using this, refine (λ hx, closure_induction hx (λ _, mem_Union.1) _ _ _), { exact hι.elim (λ i, ⟨i, (K i).one_mem⟩) }, { rintros x y ⟨i, hi⟩ ⟨j, hj⟩, rcases hK i j with ⟨k, hki, hkj⟩, exact ⟨k, (K k).mul_mem (hki hi) (hkj hj)⟩ }, rintros _ ⟨i, hi⟩, exact ⟨i, inv_mem (K i) hi⟩ end @[to_additive] lemma coe_supr_of_directed {ι} [nonempty ι] {S : ι → subgroup G} (hS : directed (≤) S) : ((⨆ i, S i : subgroup G) : set G) = ⋃ i, ↑(S i) := set.ext $ λ x, by simp [mem_supr_of_directed hS] @[to_additive] lemma mem_Sup_of_directed_on {K : set (subgroup G)} (Kne : K.nonempty) (hK : directed_on (≤) K) {x : G} : x ∈ Sup K ↔ ∃ s ∈ K, x ∈ s := begin haveI : nonempty K := Kne.to_subtype, simp only [Sup_eq_supr', mem_supr_of_directed hK.directed_coe, set_coe.exists, subtype.coe_mk] end variables {N : Type*} [group N] {P : Type*} [group P] /-- The preimage of a subgroup along a monoid homomorphism is a subgroup. -/ @[to_additive "The preimage of an `add_subgroup` along an `add_monoid` homomorphism is an `add_subgroup`."] def comap {N : Type*} [group N] (f : G →* N) (H : subgroup N) : subgroup G := { carrier := (f ⁻¹' H), inv_mem' := λ a ha, show f a⁻¹ ∈ H, by rw f.map_inv; exact H.inv_mem ha, .. H.to_submonoid.comap f } @[simp, to_additive] lemma coe_comap (K : subgroup N) (f : G →* N) : (K.comap f : set G) = f ⁻¹' K := rfl @[simp, to_additive] lemma mem_comap {K : subgroup N} {f : G →* N} {x : G} : x ∈ K.comap f ↔ f x ∈ K := iff.rfl @[to_additive] lemma comap_mono {f : G →* N} {K K' : subgroup N} : K ≤ K' → comap f K ≤ comap f K' := preimage_mono @[to_additive] lemma comap_comap (K : subgroup P) (g : N →* P) (f : G →* N) : (K.comap g).comap f = K.comap (g.comp f) := rfl /-- The image of a subgroup along a monoid homomorphism is a subgroup. -/ @[to_additive "The image of an `add_subgroup` along an `add_monoid` homomorphism is an `add_subgroup`."] def map (f : G →* N) (H : subgroup G) : subgroup N := { carrier := (f '' H), inv_mem' := by { rintros _ ⟨x, hx, rfl⟩, exact ⟨x⁻¹, H.inv_mem hx, f.map_inv x⟩ }, .. H.to_submonoid.map f } @[simp, to_additive] lemma coe_map (f : G →* N) (K : subgroup G) : (K.map f : set N) = f '' K := rfl @[simp, to_additive] lemma mem_map {f : G →* N} {K : subgroup G} {y : N} : y ∈ K.map f ↔ ∃ x ∈ K, f x = y := mem_image_iff_bex @[to_additive] lemma map_mono {f : G →* N} {K K' : subgroup G} : K ≤ K' → map f K ≤ map f K' := image_subset _ @[to_additive] lemma map_map (g : N →* P) (f : G →* N) : (K.map f).map g = K.map (g.comp f) := set_like.coe_injective $ image_image _ _ _ @[to_additive] lemma map_le_iff_le_comap {f : G →* N} {K : subgroup G} {H : subgroup N} : K.map f ≤ H ↔ K ≤ H.comap f := image_subset_iff @[to_additive] lemma gc_map_comap (f : G →* N) : galois_connection (map f) (comap f) := λ _ _, map_le_iff_le_comap @[to_additive] lemma map_sup (H K : subgroup G) (f : G →* N) : (H ⊔ K).map f = H.map f ⊔ K.map f := (gc_map_comap f).l_sup @[to_additive] lemma map_supr {ι : Sort*} (f : G →* N) (s : ι → subgroup G) : (supr s).map f = ⨆ i, (s i).map f := (gc_map_comap f).l_supr @[to_additive] lemma comap_inf (H K : subgroup N) (f : G →* N) : (H ⊓ K).comap f = H.comap f ⊓ K.comap f := (gc_map_comap f).u_inf @[to_additive] lemma comap_infi {ι : Sort*} (f : G →* N) (s : ι → subgroup N) : (infi s).comap f = ⨅ i, (s i).comap f := (gc_map_comap f).u_infi @[simp, to_additive] lemma map_bot (f : G →* N) : (⊥ : subgroup G).map f = ⊥ := (gc_map_comap f).l_bot @[simp, to_additive] lemma comap_top (f : G →* N) : (⊤ : subgroup N).comap f = ⊤ := (gc_map_comap f).u_top @[simp, to_additive] lemma comap_subtype_inf_left {H K : subgroup G} : comap H.subtype (H ⊓ K) = comap H.subtype K := ext $ λ x, and_iff_right_of_imp (λ _, x.prop) @[simp, to_additive] lemma comap_subtype_inf_right {H K : subgroup G} : comap K.subtype (H ⊓ K) = comap K.subtype H := ext $ λ x, and_iff_left_of_imp (λ _, x.prop) /-- Given `subgroup`s `H`, `K` of groups `G`, `N` respectively, `H × K` as a subgroup of `G × N`. -/ @[to_additive prod "Given `add_subgroup`s `H`, `K` of `add_group`s `A`, `B` respectively, `H × K` as an `add_subgroup` of `A × B`."] def prod (H : subgroup G) (K : subgroup N) : subgroup (G × N) := { inv_mem' := λ _ hx, ⟨H.inv_mem' hx.1, K.inv_mem' hx.2⟩, .. submonoid.prod H.to_submonoid K.to_submonoid} @[to_additive coe_prod] lemma coe_prod (H : subgroup G) (K : subgroup N) : (H.prod K : set (G × N)) = (H : set G).prod (K : set N) := rfl @[to_additive mem_prod] lemma mem_prod {H : subgroup G} {K : subgroup N} {p : G × N} : p ∈ H.prod K ↔ p.1 ∈ H ∧ p.2 ∈ K := iff.rfl @[to_additive prod_mono] lemma prod_mono : ((≤) ⇒ (≤) ⇒ (≤)) (@prod G _ N _) (@prod G _ N _) := λ s s' hs t t' ht, set.prod_mono hs ht @[to_additive prod_mono_right] lemma prod_mono_right (K : subgroup G) : monotone (λ t : subgroup N, K.prod t) := prod_mono (le_refl K) @[to_additive prod_mono_left] lemma prod_mono_left (H : subgroup N) : monotone (λ K : subgroup G, K.prod H) := λ s₁ s₂ hs, prod_mono hs (le_refl H) @[to_additive prod_top] lemma prod_top (K : subgroup G) : K.prod (⊤ : subgroup N) = K.comap (monoid_hom.fst G N) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_fst] @[to_additive top_prod] lemma top_prod (H : subgroup N) : (⊤ : subgroup G).prod H = H.comap (monoid_hom.snd G N) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_snd] @[simp, to_additive top_prod_top] lemma top_prod_top : (⊤ : subgroup G).prod (⊤ : subgroup N) = ⊤ := (top_prod _).trans $ comap_top _ @[to_additive] lemma bot_prod_bot : (⊥ : subgroup G).prod (⊥ : subgroup N) = ⊥ := set_like.coe_injective $ by simp [coe_prod, prod.one_eq_mk] /-- Product of subgroups is isomorphic to their product as groups. -/ @[to_additive prod_equiv "Product of additive subgroups is isomorphic to their product as additive groups"] def prod_equiv (H : subgroup G) (K : subgroup N) : H.prod K ≃* H × K := { map_mul' := λ x y, rfl, .. equiv.set.prod ↑H ↑K } /-- A subgroup is normal if whenever `n ∈ H`, then `g * n * g⁻¹ ∈ H` for every `g : G` -/ structure normal : Prop := (conj_mem : ∀ n, n ∈ H → ∀ g : G, g * n * g⁻¹ ∈ H) attribute [class] normal end subgroup namespace add_subgroup /-- An add_subgroup is normal if whenever `n ∈ H`, then `g + n - g ∈ H` for every `g : G` -/ structure normal (H : add_subgroup A) : Prop := (conj_mem [] : ∀ n, n ∈ H → ∀ g : A, g + n + -g ∈ H) attribute [to_additive add_subgroup.normal] subgroup.normal attribute [class] normal end add_subgroup namespace subgroup variables {H K : subgroup G} @[priority 100, to_additive] instance normal_of_comm {G : Type*} [comm_group G] (H : subgroup G) : H.normal := ⟨by simp [mul_comm, mul_left_comm]⟩ namespace normal variable (nH : H.normal) @[to_additive] lemma mem_comm {a b : G} (h : a * b ∈ H) : b * a ∈ H := have a⁻¹ * (a * b) * a⁻¹⁻¹ ∈ H, from nH.conj_mem (a * b) h a⁻¹, by simpa @[to_additive] lemma mem_comm_iff {a b : G} : a * b ∈ H ↔ b * a ∈ H := ⟨nH.mem_comm, nH.mem_comm⟩ end normal @[priority 100, to_additive] instance bot_normal : normal (⊥ : subgroup G) := ⟨by simp⟩ @[priority 100, to_additive] instance top_normal : normal (⊤ : subgroup G) := ⟨λ _ _, mem_top⟩ variable (G) /-- The center of a group `G` is the set of elements that commute with everything in `G` -/ @[to_additive "The center of a group `G` is the set of elements that commute with everything in `G`"] def center : subgroup G := { carrier := {z | ∀ g, g * z = z * g}, one_mem' := by simp, mul_mem' := λ a b (ha : ∀ g, g * a = a * g) (hb : ∀ g, g * b = b * g) g, by assoc_rw [ha, hb g], inv_mem' := λ a (ha : ∀ g, g * a = a * g) g, by rw [← inv_inj, mul_inv_rev, inv_inv, ← ha, mul_inv_rev, inv_inv] } variable {G} @[to_additive] lemma mem_center_iff {z : G} : z ∈ center G ↔ ∀ g, g * z = z * g := iff.rfl @[priority 100, to_additive] instance center_normal : (center G).normal := ⟨begin assume n hn g h, assoc_rw [hn (h * g), hn g], simp end⟩ variables {G} (H) /-- The `normalizer` of `H` is the largest subgroup of `G` inside which `H` is normal. -/ @[to_additive "The `normalizer` of `H` is the largest subgroup of `G` inside which `H` is normal."] def normalizer : subgroup G := { carrier := {g : G | ∀ n, n ∈ H ↔ g * n * g⁻¹ ∈ H}, one_mem' := by simp, mul_mem' := λ a b (ha : ∀ n, n ∈ H ↔ a * n * a⁻¹ ∈ H) (hb : ∀ n, n ∈ H ↔ b * n * b⁻¹ ∈ H) n, by { rw [hb, ha], simp [mul_assoc] }, inv_mem' := λ a (ha : ∀ n, n ∈ H ↔ a * n * a⁻¹ ∈ H) n, by { rw [ha (a⁻¹ * n * a⁻¹⁻¹)], simp [mul_assoc] } } -- variant for sets. -- TODO should this replace `normalizer`? /-- The `set_normalizer` of `S` is the subgroup of `G` whose elements satisfy `g*S*g⁻¹=S` -/ @[to_additive "The `set_normalizer` of `S` is the subgroup of `G` whose elements satisfy `g+S-g=S`."] def set_normalizer (S : set G) : subgroup G := { carrier := {g : G | ∀ n, n ∈ S ↔ g * n * g⁻¹ ∈ S}, one_mem' := by simp, mul_mem' := λ a b (ha : ∀ n, n ∈ S ↔ a * n * a⁻¹ ∈ S) (hb : ∀ n, n ∈ S ↔ b * n * b⁻¹ ∈ S) n, by { rw [hb, ha], simp [mul_assoc] }, inv_mem' := λ a (ha : ∀ n, n ∈ S ↔ a * n * a⁻¹ ∈ S) n, by { rw [ha (a⁻¹ * n * a⁻¹⁻¹)], simp [mul_assoc] } } lemma mem_normalizer_fintype {S : set G} [fintype S] {x : G} (h : ∀ n, n ∈ S → x * n * x⁻¹ ∈ S) : x ∈ subgroup.set_normalizer S := by haveI := classical.prop_decidable; haveI := set.fintype_image S (λ n, x * n * x⁻¹); exact λ n, ⟨h n, λ h₁, have heq : (λ n, x * n * x⁻¹) '' S = S := set.eq_of_subset_of_card_le (λ n ⟨y, hy⟩, hy.2 ▸ h y hy.1) (by rw set.card_image_of_injective S conj_injective), have x * n * x⁻¹ ∈ (λ n, x * n * x⁻¹) '' S := heq.symm ▸ h₁, let ⟨y, hy⟩ := this in conj_injective hy.2 ▸ hy.1⟩ variable {H} @[to_additive] lemma mem_normalizer_iff {g : G} : g ∈ normalizer H ↔ ∀ n, n ∈ H ↔ g * n * g⁻¹ ∈ H := iff.rfl @[to_additive] lemma le_normalizer : H ≤ normalizer H := λ x xH n, by rw [H.mul_mem_cancel_right (H.inv_mem xH), H.mul_mem_cancel_left xH] @[priority 100, to_additive] instance normal_in_normalizer : (H.comap H.normalizer.subtype).normal := ⟨λ x xH g, by simpa using (g.2 x).1 xH⟩ open_locale classical @[to_additive] lemma le_normalizer_of_normal [hK : (H.comap K.subtype).normal] (HK : H ≤ K) : K ≤ H.normalizer := λ x hx y, ⟨λ yH, hK.conj_mem ⟨y, HK yH⟩ yH ⟨x, hx⟩, λ yH, by simpa [mem_comap, mul_assoc] using hK.conj_mem ⟨x * y * x⁻¹, HK yH⟩ yH ⟨x⁻¹, K.inv_mem hx⟩⟩ end subgroup namespace group variables {s : set G} /-- Given a set `s`, `conjugates_of_set s` is the set of all conjugates of the elements of `s`. -/ def conjugates_of_set (s : set G) : set G := ⋃ a ∈ s, conjugates_of a lemma mem_conjugates_of_set_iff {x : G} : x ∈ conjugates_of_set s ↔ ∃ a ∈ s, is_conj a x := set.mem_bUnion_iff theorem subset_conjugates_of_set : s ⊆ conjugates_of_set s := λ (x : G) (h : x ∈ s), mem_conjugates_of_set_iff.2 ⟨x, h, is_conj_refl _⟩ theorem conjugates_of_set_mono {s t : set G} (h : s ⊆ t) : conjugates_of_set s ⊆ conjugates_of_set t := set.bUnion_subset_bUnion_left h lemma conjugates_subset_normal {N : subgroup G} [tn : N.normal] {a : G} (h : a ∈ N) : conjugates_of a ⊆ N := by { rintros a hc, obtain ⟨c, rfl⟩ := is_conj_iff.1 hc, exact tn.conj_mem a h c } theorem conjugates_of_set_subset {s : set G} {N : subgroup G} [N.normal] (h : s ⊆ N) : conjugates_of_set s ⊆ N := set.bUnion_subset (λ x H, conjugates_subset_normal (h H)) /-- The set of conjugates of `s` is closed under conjugation. -/ lemma conj_mem_conjugates_of_set {x c : G} : x ∈ conjugates_of_set s → (c * x * c⁻¹ ∈ conjugates_of_set s) := λ H, begin rcases (mem_conjugates_of_set_iff.1 H) with ⟨a,h₁,h₂⟩, exact mem_conjugates_of_set_iff.2 ⟨a, h₁, is_conj_trans h₂ (is_conj_iff.2 ⟨c,rfl⟩)⟩, end end group namespace subgroup open group variable {s : set G} /-- The normal closure of a set `s` is the subgroup closure of all the conjugates of elements of `s`. It is the smallest normal subgroup containing `s`. -/ def normal_closure (s : set G) : subgroup G := closure (conjugates_of_set s) theorem conjugates_of_set_subset_normal_closure : conjugates_of_set s ⊆ normal_closure s := subset_closure theorem subset_normal_closure : s ⊆ normal_closure s := set.subset.trans subset_conjugates_of_set conjugates_of_set_subset_normal_closure theorem le_normal_closure {H : subgroup G} : H ≤ normal_closure ↑H := λ _ h, subset_normal_closure h /-- The normal closure of `s` is a normal subgroup. -/ instance normal_closure_normal : (normal_closure s).normal := ⟨λ n h g, begin refine subgroup.closure_induction h (λ x hx, _) _ (λ x y ihx ihy, _) (λ x ihx, _), { exact (conjugates_of_set_subset_normal_closure (conj_mem_conjugates_of_set hx)) }, { simpa using (normal_closure s).one_mem }, { rw ← conj_mul, exact mul_mem _ ihx ihy }, { rw ← conj_inv, exact inv_mem _ ihx } end⟩ /-- The normal closure of `s` is the smallest normal subgroup containing `s`. -/ theorem normal_closure_le_normal {N : subgroup G} [N.normal] (h : s ⊆ N) : normal_closure s ≤ N := begin assume a w, refine closure_induction w (λ x hx, _) _ (λ x y ihx ihy, _) (λ x ihx, _), { exact (conjugates_of_set_subset h hx) }, { exact subgroup.one_mem _ }, { exact subgroup.mul_mem _ ihx ihy }, { exact subgroup.inv_mem _ ihx } end lemma normal_closure_subset_iff {N : subgroup G} [N.normal] : s ⊆ N ↔ normal_closure s ≤ N := ⟨normal_closure_le_normal, set.subset.trans (subset_normal_closure)⟩ theorem normal_closure_mono {s t : set G} (h : s ⊆ t) : normal_closure s ≤ normal_closure t := normal_closure_le_normal (set.subset.trans h subset_normal_closure) theorem normal_closure_eq_infi : normal_closure s = ⨅ (N : subgroup G) [normal N] (hs : s ⊆ N), N := le_antisymm (le_infi (λ N, le_infi (λ hN, by exactI le_infi (normal_closure_le_normal)))) (infi_le_of_le (normal_closure s) (infi_le_of_le (by apply_instance) (infi_le_of_le subset_normal_closure (le_refl _)))) @[simp] theorem normal_closure_eq_self (H : subgroup G) [H.normal] : normal_closure ↑H = H := le_antisymm (normal_closure_le_normal rfl.subset) (le_normal_closure) @[simp] theorem normal_closure_idempotent : normal_closure ↑(normal_closure s) = normal_closure s := normal_closure_eq_self _ theorem closure_le_normal_closure {s : set G} : closure s ≤ normal_closure s := by simp only [subset_normal_closure, closure_le] @[simp] theorem normal_closure_closure_eq_normal_closure {s : set G} : normal_closure ↑(closure s) = normal_closure s := le_antisymm (normal_closure_le_normal closure_le_normal_closure) (normal_closure_mono subset_closure) end subgroup namespace add_subgroup open set lemma gsmul_mem (H : add_subgroup A) {x : A} (hx : x ∈ H) : ∀ n : ℤ, n • x ∈ H | (n : ℕ) := by { rw [gsmul_coe_nat], exact add_submonoid.nsmul_mem H.to_add_submonoid hx n } | -[1+ n] := begin rw gsmul_neg_succ_of_nat, apply H.neg_mem', exact add_submonoid.nsmul_mem H.to_add_submonoid hx n.succ end /-- The `add_subgroup` generated by an element of an `add_group` equals the set of natural number multiples of the element. -/ lemma mem_closure_singleton {x y : A} : y ∈ closure ({x} : set A) ↔ ∃ n : ℤ, n • x = y := begin refine ⟨λ hy, closure_induction hy _ _ _ _, λ ⟨n, hn⟩, hn ▸ gsmul_mem _ (subset_closure $ mem_singleton x) n⟩, { intros y hy, rw [eq_of_mem_singleton hy], exact ⟨1, one_gsmul x⟩ }, { exact ⟨0, zero_gsmul x⟩ }, { rintros _ _ ⟨n, rfl⟩ ⟨m, rfl⟩, exact ⟨n + m, add_gsmul x n m⟩ }, { rintros _ ⟨n, rfl⟩, refine ⟨-n, neg_gsmul x n⟩ } end lemma closure_singleton_zero : closure ({0} : set A) = ⊥ := by simp [eq_bot_iff_forall, mem_closure_singleton] variable (H : add_subgroup A) @[simp] lemma coe_smul (x : H) (n : ℕ) : ((n • x : H) : A) = n • x := coe_subtype H ▸ add_monoid_hom.map_nsmul _ _ _ @[simp] lemma coe_gsmul (x : H) (n : ℤ) : ((n • x : H) : A) = n • x := coe_subtype H ▸ add_monoid_hom.map_gsmul _ _ _ attribute [to_additive add_subgroup.coe_smul] subgroup.coe_pow attribute [to_additive add_subgroup.coe_gsmul] subgroup.coe_gpow end add_subgroup namespace monoid_hom variables {N : Type*} {P : Type*} [group N] [group P] (K : subgroup G) open subgroup /-- The range of a monoid homomorphism from a group is a subgroup. -/ @[to_additive "The range of an `add_monoid_hom` from an `add_group` is an `add_subgroup`."] def range (f : G →* N) : subgroup N := subgroup.copy ((⊤ : subgroup G).map f) (set.range f) (by simp [set.ext_iff]) @[to_additive] instance decidable_mem_range (f : G →* N) [fintype G] [decidable_eq N] : decidable_pred (λ x, x ∈ f.range) := λ x, fintype.decidable_exists_fintype @[simp, to_additive] lemma coe_range (f : G →* N) : (f.range : set N) = set.range f := rfl @[simp, to_additive] lemma mem_range {f : G →* N} {y : N} : y ∈ f.range ↔ ∃ x, f x = y := iff.rfl @[to_additive] lemma range_eq_map (f : G →* N) : f.range = (⊤ : subgroup G).map f := by ext; simp /-- The canonical surjective group homomorphism `G →* f(G)` induced by a group homomorphism `G →* N`. -/ @[to_additive "The canonical surjective `add_group` homomorphism `G →+ f(G)` induced by a group homomorphism `G →+ N`."] def range_restrict (f : G →* N) : G →* f.range := monoid_hom.mk' (λ g, ⟨f g, ⟨g, rfl⟩⟩) $ λ a b, by {ext, exact f.map_mul' _ _} @[simp, to_additive] lemma coe_range_restrict (f : G →* N) (g : G) : (f.range_restrict g : N) = f g := rfl @[to_additive] lemma map_range (g : N →* P) (f : G →* N) : f.range.map g = (g.comp f).range := by rw [range_eq_map, range_eq_map]; exact (⊤ : subgroup G).map_map g f @[to_additive] lemma range_top_iff_surjective {N} [group N] {f : G →* N} : f.range = (⊤ : subgroup N) ↔ function.surjective f := set_like.ext'_iff.trans $ iff.trans (by rw [coe_range, coe_top]) set.range_iff_surjective /-- The range of a surjective monoid homomorphism is the whole of the codomain. -/ @[to_additive "The range of a surjective `add_monoid` homomorphism is the whole of the codomain."] lemma range_top_of_surjective {N} [group N] (f : G →* N) (hf : function.surjective f) : f.range = (⊤ : subgroup N) := range_top_iff_surjective.2 hf /-- Restriction of a group hom to a subgroup of the domain. -/ @[to_additive "Restriction of an `add_group` hom to an `add_subgroup` of the domain."] def restrict (f : G →* N) (H : subgroup G) : H →* N := f.comp H.subtype @[simp, to_additive] lemma restrict_apply {H : subgroup G} (f : G →* N) (x : H) : f.restrict H x = f (x : G) := rfl /-- Restriction of a group hom to a subgroup of the codomain. -/ @[to_additive "Restriction of an `add_group` hom to an `add_subgroup` of the codomain."] def cod_restrict (f : G →* N) (S : subgroup N) (h : ∀ x, f x ∈ S) : G →* S := { to_fun := λ n, ⟨f n, h n⟩, map_one' := subtype.eq f.map_one, map_mul' := λ x y, subtype.eq (f.map_mul x y) } /-- Computable alternative to `monoid_hom.of_injective`. -/ def of_left_inverse {f : G →* N} {g : N →* G} (h : function.left_inverse g f) : G ≃* f.range := { to_fun := f.range_restrict, inv_fun := g ∘ f.range.subtype, left_inv := h, right_inv := by { rintros ⟨x, y, rfl⟩, apply subtype.ext, rw [coe_range_restrict, function.comp_apply, subgroup.coe_subtype, subtype.coe_mk, h] }, .. f.range_restrict } @[simp] lemma of_left_inverse_apply {f : G →* N} {g : N →* G} (h : function.left_inverse g f) (x : G) : ↑(of_left_inverse h x) = f x := rfl @[simp] lemma of_left_inverse_symm_apply {f : G →* N} {g : N →* G} (h : function.left_inverse g f) (x : f.range) : (of_left_inverse h).symm x = g x := rfl /-- The range of an injective group homomorphism is isomorphic to its domain. -/ noncomputable def of_injective {f : G →* N} (hf : function.injective f) : G ≃* f.range := (mul_equiv.of_bijective (f.cod_restrict f.range (λ x, ⟨x, rfl⟩)) ⟨λ x y h, hf (subtype.ext_iff.mp h), by { rintros ⟨x, y, rfl⟩, exact ⟨y, rfl⟩ }⟩) lemma of_injective_apply {f : G →* N} (hf : function.injective f) {x : G} : ↑(of_injective hf x) = f x := rfl /-- The multiplicative kernel of a monoid homomorphism is the subgroup of elements `x : G` such that `f x = 1` -/ @[to_additive "The additive kernel of an `add_monoid` homomorphism is the `add_subgroup` of elements such that `f x = 0`"] def ker (f : G →* N) := (⊥ : subgroup N).comap f @[to_additive] lemma mem_ker (f : G →* N) {x : G} : x ∈ f.ker ↔ f x = 1 := iff.rfl @[to_additive] instance decidable_mem_ker [decidable_eq N] (f : G →* N) : decidable_pred (∈ f.ker) := λ x, decidable_of_iff (f x = 1) f.mem_ker @[to_additive] lemma comap_ker (g : N →* P) (f : G →* N) : g.ker.comap f = (g.comp f).ker := rfl @[to_additive] lemma range_restrict_ker (f : G →* N) : ker (range_restrict f) = ker f := begin ext, change (⟨f x, _⟩ : range f) = ⟨1, _⟩ ↔ f x = 1, simp only [], end @[to_additive] lemma ker_eq_bot_iff (f : G →* N) : f.ker = ⊥ ↔ function.injective f := begin split, { intros h x y hxy, rwa [←mul_inv_eq_one, ←map_inv, ←map_mul, ←mem_ker, h, mem_bot, mul_inv_eq_one] at hxy }, { exact λ h, le_bot_iff.mp (λ x hx, h (hx.trans f.map_one.symm)) }, end /-- The subgroup of elements `x : G` such that `f x = g x` -/ @[to_additive "The additive subgroup of elements `x : G` such that `f x = g x`"] def eq_locus (f g : G →* N) : subgroup G := { inv_mem' := λ x (hx : f x = g x), show f x⁻¹ = g x⁻¹, by rw [f.map_inv, g.map_inv, hx], .. eq_mlocus f g} /-- If two monoid homomorphisms are equal on a set, then they are equal on its subgroup closure. -/ @[to_additive] lemma eq_on_closure {f g : G →* N} {s : set G} (h : set.eq_on f g s) : set.eq_on f g (closure s) := show closure s ≤ f.eq_locus g, from (closure_le _).2 h @[to_additive] lemma eq_of_eq_on_top {f g : G →* N} (h : set.eq_on f g (⊤ : subgroup G)) : f = g := ext $ λ x, h trivial @[to_additive] lemma eq_of_eq_on_dense {s : set G} (hs : closure s = ⊤) {f g : G →* N} (h : s.eq_on f g) : f = g := eq_of_eq_on_top $ hs ▸ eq_on_closure h @[to_additive] lemma gclosure_preimage_le (f : G →* N) (s : set N) : closure (f ⁻¹' s) ≤ (closure s).comap f := (closure_le _).2 $ λ x hx, by rw [set_like.mem_coe, mem_comap]; exact subset_closure hx /-- The image under a monoid homomorphism of the subgroup generated by a set equals the subgroup generated by the image of the set. -/ @[to_additive "The image under an `add_monoid` hom of the `add_subgroup` generated by a set equals the `add_subgroup` generated by the image of the set."] lemma map_closure (f : G →* N) (s : set G) : (closure s).map f = closure (f '' s) := le_antisymm (map_le_iff_le_comap.2 $ le_trans (closure_mono $ set.subset_preimage_image f s) (gclosure_preimage_le _ _)) ((closure_le _).2 $ set.image_subset _ subset_closure) end monoid_hom namespace subgroup variables {N : Type*} [group N] (H : subgroup G) @[to_additive] lemma map_eq_bot_iff {f : G →* N} : H.map f = ⊥ ↔ H ≤ f.ker := begin rw eq_bot_iff, split, { exact λ h x hx, h ⟨x, hx, rfl⟩ }, { intros h x hx, obtain ⟨y, hy, rfl⟩ := hx, exact h hy }, end @[to_additive] lemma map_eq_bot_iff_of_injective {f : G →* N} (hf : function.injective f) : H.map f = ⊥ ↔ H = ⊥ := by rw [map_eq_bot_iff, f.ker_eq_bot_iff.mpr hf, le_bot_iff] end subgroup namespace subgroup open monoid_hom variables {H : Type*} [group H] @[to_additive] lemma map_le_range (f : G →* H) (K : subgroup G) : map f K ≤ f.range := (range_eq_map f).symm ▸ map_mono le_top @[to_additive] lemma ker_le_comap (f : G →* H) (K : subgroup H) : f.ker ≤ comap f K := comap_mono bot_le @[to_additive] lemma map_comap_le (f : G →* H) (K : subgroup H) : map f (comap f K) ≤ K := (gc_map_comap f).l_u_le _ @[to_additive] lemma le_comap_map (f : G →* H) (K : subgroup G) : K ≤ comap f (map f K) := (gc_map_comap f).le_u_l _ @[to_additive] lemma map_comap_eq (f : G →* H) (K : subgroup H) : map f (comap f K) = f.range ⊓ K := set_like.ext' begin convert set.image_preimage_eq_inter_range, simp [set.inter_comm], end @[to_additive] lemma comap_map_eq (f : G →* H) (K : subgroup G) : comap f (map f K) = K ⊔ f.ker := begin refine le_antisymm _ (sup_le (le_comap_map _ _) (ker_le_comap _ _)), intros x hx, simp only [exists_prop, mem_map, mem_comap] at hx, rcases hx with ⟨y, hy, hy'⟩, have : y⁻¹ * x ∈ f.ker, { rw mem_ker, simp [hy'] }, convert mul_mem _ (mem_sup_left hy) (mem_sup_right this), simp, end @[to_additive] lemma map_comap_eq_self {f : G →* H} {K : subgroup H} (h : K ≤ f.range) : map f (comap f K) = K := by rwa [map_comap_eq, inf_eq_right] @[to_additive] lemma map_comap_eq_self_of_surjective {f : G →* H} (h : function.surjective f) (K : subgroup H) : map f (comap f K) = K := map_comap_eq_self ((range_top_of_surjective _ h).symm ▸ le_top) @[to_additive] lemma comap_injective {f : G →* H} (h : function.surjective f) : function.injective (comap f) := λ K L hKL, by { apply_fun map f at hKL, simpa [map_comap_eq_self_of_surjective h] using hKL } @[to_additive] lemma comap_map_eq_self {f : G →* H} {K : subgroup G} (h : f.ker ≤ K) : comap f (map f K) = K := by rwa [comap_map_eq, sup_eq_left] @[to_additive] lemma comap_map_eq_self_of_injective {f : G →* H} (h : function.injective f) (K : subgroup G) : comap f (map f K) = K := comap_map_eq_self (((ker_eq_bot_iff _).mpr h).symm ▸ bot_le) @[to_additive] lemma map_injective {f : G →* H} (h : function.injective f) : function.injective (map f) := λ K L hKL, by { apply_fun comap f at hKL, simpa [comap_map_eq_self_of_injective h] using hKL } @[to_additive] lemma map_eq_comap_of_inverse {f : G →* H} {g : H →* G} (hl : function.left_inverse g f) (hr : function.right_inverse g f) (K : subgroup G) : map f K = comap g K := set_like.ext' $ by rw [coe_map, coe_comap, set.image_eq_preimage_of_inverse hl hr] end subgroup namespace monoid_hom variables {G₁ G₂ G₃ : Type*} [group G₁] [group G₂] [group G₃] variables (f : G₁ →* G₂) (f_inv : G₂ → G₁) /-- Auxiliary definition used to define `lift_of_right_inverse` -/ @[to_additive "Auxiliary definition used to define `lift_of_right_inverse`"] def lift_of_right_inverse_aux (hf : function.right_inverse f_inv f) (g : G₁ →* G₃) (hg : f.ker ≤ g.ker) : G₂ →* G₃ := { to_fun := λ b, g (f_inv b), map_one' := hg (hf 1), map_mul' := begin intros x y, rw [← g.map_mul, ← mul_inv_eq_one, ← g.map_inv, ← g.map_mul, ← g.mem_ker], apply hg, rw [f.mem_ker, f.map_mul, f.map_inv, mul_inv_eq_one, f.map_mul], simp only [hf _], end } @[simp, to_additive] lemma lift_of_right_inverse_aux_comp_apply (hf : function.right_inverse f_inv f) (g : G₁ →* G₃) (hg : f.ker ≤ g.ker) (x : G₁) : (f.lift_of_right_inverse_aux f_inv hf g hg) (f x) = g x := begin dsimp [lift_of_right_inverse_aux], rw [← mul_inv_eq_one, ← g.map_inv, ← g.map_mul, ← g.mem_ker], apply hg, rw [f.mem_ker, f.map_mul, f.map_inv, mul_inv_eq_one], simp only [hf _], end /-- `lift_of_right_inverse f hf g hg` is the unique group homomorphism `φ` * such that `φ.comp f = g` (`monoid_hom.lift_of_right_inverse_comp`), * where `f : G₁ →+* G₂` has a right_inverse `f_inv` (`hf`), * and `g : G₂ →+* G₃` satisfies `hg : f.ker ≤ g.ker`. See `monoid_hom.eq_lift_of_right_inverse` for the uniqueness lemma. ``` G₁. | \ f | \ g | \ v \⌟ G₂----> G₃ ∃!φ ``` -/ @[to_additive "`lift_of_right_inverse f f_inv hf g hg` is the unique additive group homomorphism `φ` * such that `φ.comp f = g` (`add_monoid_hom.lift_of_right_inverse_comp`), * where `f : G₁ →+ G₂` has a right_inverse `f_inv` (`hf`), * and `g : G₂ →+ G₃` satisfies `hg : f.ker ≤ g.ker`. See `add_monoid_hom.eq_lift_of_right_inverse` for the uniqueness lemma. ``` G₁. | \\ f | \\ g | \\ v \\⌟ G₂----> G₃ ∃!φ ```"] def lift_of_right_inverse (hf : function.right_inverse f_inv f) : {g : G₁ →* G₃ // f.ker ≤ g.ker} ≃ (G₂ →* G₃) := { to_fun := λ g, f.lift_of_right_inverse_aux f_inv hf g.1 g.2, inv_fun := λ φ, ⟨φ.comp f, λ x hx, (mem_ker _).mpr $ by simp [(mem_ker _).mp hx]⟩, left_inv := λ g, by { ext, simp only [comp_apply, lift_of_right_inverse_aux_comp_apply, subtype.coe_mk, subtype.val_eq_coe], }, right_inv := λ φ, by { ext b, simp [lift_of_right_inverse_aux, hf b], } } /-- A non-computable version of `monoid_hom.lift_of_right_inverse` for when no computable right inverse is available, that uses `function.surj_inv`. -/ @[simp, to_additive "A non-computable version of `add_monoid_hom.lift_of_right_inverse` for when no computable right inverse is available."] noncomputable abbreviation lift_of_surjective (hf : function.surjective f) : {g : G₁ →* G₃ // f.ker ≤ g.ker} ≃ (G₂ →* G₃) := f.lift_of_right_inverse (function.surj_inv hf) (function.right_inverse_surj_inv hf) @[simp, to_additive] lemma lift_of_right_inverse_comp_apply (hf : function.right_inverse f_inv f) (g : {g : G₁ →* G₃ // f.ker ≤ g.ker}) (x : G₁) : (f.lift_of_right_inverse f_inv hf g) (f x) = g x := f.lift_of_right_inverse_aux_comp_apply f_inv hf g.1 g.2 x @[simp, to_additive] lemma lift_of_right_inverse_comp (hf : function.right_inverse f_inv f) (g : {g : G₁ →* G₃ // f.ker ≤ g.ker}) : (f.lift_of_right_inverse f_inv hf g).comp f = g := monoid_hom.ext $ f.lift_of_right_inverse_comp_apply f_inv hf g @[to_additive] lemma eq_lift_of_right_inverse (hf : function.right_inverse f_inv f) (g : G₁ →* G₃) (hg : f.ker ≤ g.ker) (h : G₂ →* G₃) (hh : h.comp f = g) : h = (f.lift_of_right_inverse f_inv hf ⟨g, hg⟩) := begin simp_rw ←hh, exact ((f.lift_of_right_inverse f_inv hf).apply_symm_apply _).symm, end end monoid_hom variables {N : Type*} [group N] -- Here `H.normal` is an explicit argument so we can use dot notation with `comap`. @[to_additive] lemma subgroup.normal.comap {H : subgroup N} (hH : H.normal) (f : G →* N) : (H.comap f).normal := ⟨λ _, by simp [subgroup.mem_comap, hH.conj_mem] {contextual := tt}⟩ @[priority 100, to_additive] instance subgroup.normal_comap {H : subgroup N} [nH : H.normal] (f : G →* N) : (H.comap f).normal := nH.comap _ @[priority 100, to_additive] instance monoid_hom.normal_ker (f : G →* N) : f.ker.normal := by rw [monoid_hom.ker]; apply_instance @[priority 100, to_additive] instance subgroup.normal_inf (H N : subgroup G) [hN : N.normal] : ((H ⊓ N).comap H.subtype).normal := ⟨λ x hx g, begin simp only [subgroup.mem_inf, coe_subtype, subgroup.mem_comap] at hx, simp only [subgroup.coe_mul, subgroup.mem_inf, coe_subtype, subgroup.coe_inv, subgroup.mem_comap], exact ⟨H.mul_mem (H.mul_mem g.2 hx.1) (H.inv_mem g.2), hN.1 x hx.2 g⟩, end⟩ namespace subgroup /-- The subgroup generated by an element. -/ def gpowers (g : G) : subgroup G := subgroup.copy (gpowers_hom G g).range (set.range ((^) g : ℤ → G)) rfl @[simp] lemma mem_gpowers (g : G) : g ∈ gpowers g := ⟨1, gpow_one _⟩ lemma gpowers_eq_closure (g : G) : gpowers g = closure {g} := by { ext, exact mem_closure_singleton.symm } @[simp] lemma range_gpowers_hom (g : G) : (gpowers_hom G g).range = gpowers g := rfl lemma gpowers_subset {a : G} {K : subgroup G} (h : a ∈ K) : gpowers a ≤ K := λ x hx, match x, hx with _, ⟨i, rfl⟩ := K.gpow_mem h i end end subgroup namespace add_subgroup /-- The subgroup generated by an element. -/ def gmultiples (a : A) : add_subgroup A := add_subgroup.copy (gmultiples_hom A a).range (set.range ((• a) : ℤ → A)) rfl @[simp] lemma mem_gmultiples (a : A) : a ∈ gmultiples a := ⟨1, one_gsmul _⟩ lemma gmultiples_eq_closure (a : A) : gmultiples a = closure {a} := by { ext, exact mem_closure_singleton.symm } @[simp] lemma range_gmultiples_hom (a : A) : (gmultiples_hom A a).range = gmultiples a := rfl lemma gmultiples_subset {a : A} {B : add_subgroup A} (h : a ∈ B) : gmultiples a ≤ B := @subgroup.gpowers_subset (multiplicative A) _ _ (B.to_subgroup) h attribute [to_additive add_subgroup.gmultiples] subgroup.gpowers attribute [to_additive add_subgroup.mem_gmultiples] subgroup.mem_gpowers attribute [to_additive add_subgroup.gmultiples_eq_closure] subgroup.gpowers_eq_closure attribute [to_additive add_subgroup.range_gmultiples_hom] subgroup.range_gpowers_hom attribute [to_additive add_subgroup.gmultiples_subset] subgroup.gpowers_subset end add_subgroup lemma of_mul_image_gpowers_eq_gmultiples_of_mul { x : G } : additive.of_mul '' ((subgroup.gpowers x) : set G) = add_subgroup.gmultiples (additive.of_mul x) := begin ext y, split, { rintro ⟨z, ⟨m, hm⟩, hz2⟩, use m, simp only, rwa [← of_mul_gpow, hm] }, { rintros ⟨n, hn⟩, refine ⟨x ^ n, ⟨n, rfl⟩, _⟩, rwa of_mul_gpow } end lemma of_add_image_gmultiples_eq_gpowers_of_add {x : A} : multiplicative.of_add '' ((add_subgroup.gmultiples x) : set A) = subgroup.gpowers (multiplicative.of_add x) := begin symmetry, rw equiv.eq_image_iff_symm_image_eq, exact of_mul_image_gpowers_eq_gmultiples_of_mul, end namespace mul_equiv variables {H K : subgroup G} /-- Makes the identity isomorphism from a proof two subgroups of a multiplicative group are equal. -/ @[to_additive "Makes the identity additive isomorphism from a proof two subgroups of an additive group are equal."] def subgroup_congr (h : H = K) : H ≃* K := { map_mul' := λ _ _, rfl, ..equiv.set_congr $ congr_arg _ h } end mul_equiv -- TODO : ↥(⊤ : subgroup H) ≃* H ? namespace subgroup variables {C : Type*} [comm_group C] {s t : subgroup C} {x : C} @[to_additive] lemma mem_sup : x ∈ s ⊔ t ↔ ∃ (y ∈ s) (z ∈ t), y * z = x := ⟨λ h, begin rw [← closure_eq s, ← closure_eq t, ← closure_union] at h, apply closure_induction h, { rintro y (h | h), { exact ⟨y, h, 1, t.one_mem, by simp⟩ }, { exact ⟨1, s.one_mem, y, h, by simp⟩ } }, { exact ⟨1, s.one_mem, 1, ⟨t.one_mem, mul_one 1⟩⟩ }, { rintro _ _ ⟨y₁, hy₁, z₁, hz₁, rfl⟩ ⟨y₂, hy₂, z₂, hz₂, rfl⟩, exact ⟨_, mul_mem _ hy₁ hy₂, _, mul_mem _ hz₁ hz₂, by simp [mul_assoc]; cc⟩ }, { rintro _ ⟨y, hy, z, hz, rfl⟩, exact ⟨_, inv_mem _ hy, _, inv_mem _ hz, mul_comm z y ▸ (mul_inv_rev z y).symm⟩ } end, by rintro ⟨y, hy, z, hz, rfl⟩; exact mul_mem _ ((le_sup_left : s ≤ s ⊔ t) hy) ((le_sup_right : t ≤ s ⊔ t) hz)⟩ @[to_additive] lemma mem_sup' : x ∈ s ⊔ t ↔ ∃ (y : s) (z : t), (y:C) * z = x := mem_sup.trans $ by simp only [set_like.exists, coe_mk] @[to_additive] instance : is_modular_lattice (subgroup C) := ⟨λ x y z xz a ha, begin rw [mem_inf, mem_sup] at ha, rcases ha with ⟨⟨b, hb, c, hc, rfl⟩, haz⟩, rw mem_sup, refine ⟨b, hb, c, mem_inf.2 ⟨hc, _⟩, rfl⟩, rw ← inv_mul_cancel_left b c, apply z.mul_mem (z.inv_mem (xz hb)) haz, end⟩ end subgroup section variables (G) (A) /-- A `group` is simple when it has exactly two normal `subgroup`s. -/ class is_simple_group extends nontrivial G : Prop := (eq_bot_or_eq_top_of_normal : ∀ H : subgroup G, H.normal → H = ⊥ ∨ H = ⊤) /-- An `add_group` is simple when it has exactly two normal `add_subgroup`s. -/ class is_simple_add_group extends nontrivial A : Prop := (eq_bot_or_eq_top_of_normal : ∀ H : add_subgroup A, H.normal → H = ⊥ ∨ H = ⊤) attribute [to_additive] is_simple_group variables {G} {A} @[to_additive] lemma subgroup.normal.eq_bot_or_eq_top [is_simple_group G] {H : subgroup G} (Hn : H.normal) : H = ⊥ ∨ H = ⊤ := is_simple_group.eq_bot_or_eq_top_of_normal H Hn namespace is_simple_group @[to_additive] instance {C : Type*} [comm_group C] [is_simple_group C] : is_simple_lattice (subgroup C) := ⟨λ H, H.normal_of_comm.eq_bot_or_eq_top⟩ open subgroup @[to_additive] lemma is_simple_group_of_surjective {H : Type*} [group H] [is_simple_group G] [nontrivial H] (f : G →* H) (hf : function.surjective f) : is_simple_group H := ⟨nontrivial.exists_pair_ne, λ H iH, begin refine ((iH.comap f).eq_bot_or_eq_top).imp (λ h, _) (λ h, _), { rw [←map_bot f, ←h, map_comap_eq_self_of_surjective hf] }, { rw [←comap_top f] at h, exact comap_injective hf h } end⟩ end is_simple_group end section pointwise namespace subgroup @[to_additive] lemma closure_mul_le (S T : set G) : closure (S * T) ≤ closure S ⊔ closure T := Inf_le $ λ x ⟨s, t, hs, ht, hx⟩, hx ▸ (closure S ⊔ closure T).mul_mem (set_like.le_def.mp le_sup_left $ subset_closure hs) (set_like.le_def.mp le_sup_right $ subset_closure ht) @[to_additive] lemma sup_eq_closure (H K : subgroup G) : H ⊔ K = closure (H * K) := le_antisymm (sup_le (λ h hh, subset_closure ⟨h, 1, hh, K.one_mem, mul_one h⟩) (λ k hk, subset_closure ⟨1, k, H.one_mem, hk, one_mul k⟩)) (by conv_rhs { rw [← closure_eq H, ← closure_eq K] }; apply closure_mul_le) @[to_additive] private def mul_normal_aux (H N : subgroup G) [hN : N.normal] : subgroup G := { carrier := (H : set G) * N, one_mem' := ⟨1, 1, H.one_mem, N.one_mem, by rw mul_one⟩, mul_mem' := λ a b ⟨h, n, hh, hn, ha⟩ ⟨h', n', hh', hn', hb⟩, ⟨h * h', h'⁻¹ * n * h' * n', H.mul_mem hh hh', N.mul_mem (by simpa using hN.conj_mem _ hn h'⁻¹) hn', by simp [← ha, ← hb, mul_assoc]⟩, inv_mem' := λ x ⟨h, n, hh, hn, hx⟩, ⟨h⁻¹, h * n⁻¹ * h⁻¹, H.inv_mem hh, hN.conj_mem _ (N.inv_mem hn) h, by rw [mul_assoc h, inv_mul_cancel_left, ← hx, mul_inv_rev]⟩ } /-- The carrier of `H ⊔ N` is just `↑H * ↑N` (pointwise set product) when `N` is normal. -/ @[to_additive "The carrier of `H ⊔ N` is just `↑H + ↑N` (pointwise set addition) when `N` is normal."] lemma mul_normal (H N : subgroup G) [N.normal] : (↑(H ⊔ N) : set G) = H * N := set.subset.antisymm (show H ⊔ N ≤ mul_normal_aux H N, by { rw sup_eq_closure, apply Inf_le _, dsimp, refl }) ((sup_eq_closure H N).symm ▸ subset_closure) @[to_additive] private def normal_mul_aux (N H : subgroup G) [hN : N.normal] : subgroup G := { carrier := (N : set G) * H, one_mem' := ⟨1, 1, N.one_mem, H.one_mem, by rw mul_one⟩, mul_mem' := λ a b ⟨n, h, hn, hh, ha⟩ ⟨n', h', hn', hh', hb⟩, ⟨n * (h * n' * h⁻¹), h * h', N.mul_mem hn (hN.conj_mem _ hn' _), H.mul_mem hh hh', by simp [← ha, ← hb, mul_assoc]⟩, inv_mem' := λ x ⟨n, h, hn, hh, hx⟩, ⟨h⁻¹ * n⁻¹ * h, h⁻¹, by simpa using hN.conj_mem _ (N.inv_mem hn) h⁻¹, H.inv_mem hh, by rw [mul_inv_cancel_right, ← mul_inv_rev, hx]⟩ } /-- The carrier of `N ⊔ H` is just `↑N * ↑H` (pointwise set product) when `N` is normal. -/ @[to_additive "The carrier of `N ⊔ H` is just `↑N + ↑H` (pointwise set addition) when `N` is normal."] lemma normal_mul (N H : subgroup G) [N.normal] : (↑(N ⊔ H) : set G) = N * H := set.subset.antisymm (show N ⊔ H ≤ normal_mul_aux N H, by { rw sup_eq_closure, apply Inf_le _, dsimp, refl }) ((sup_eq_closure N H).symm ▸ subset_closure) end subgroup end pointwise
f77ff0a79fc945a9949af8c54fbe70c52817b1ea
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/module/algebra.lean
b6659e7d6af38a3b12e2289980d50ac80744e657
[ "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,012
lean
/- Copyright (c) 2022 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import algebra.module.basic import algebra.algebra.basic /-! # Additional facts about modules over algebras. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. -/ namespace linear_map section restrict_scalars variables (k : Type*) [comm_semiring k] (A : Type*) [semiring A] [algebra k A] variables (M : Type*) [add_comm_monoid M] [module k M] [module A M] [is_scalar_tower k A M] variables (N : Type*) [add_comm_monoid N] [module k N] [module A N] [is_scalar_tower k A N] /-- Restriction of scalars for linear maps between modules over a `k`-algebra is itself `k`-linear. -/ @[simps] def restrict_scalars_linear_map : (M →ₗ[A] N) →ₗ[k] (M →ₗ[k] N) := { to_fun := linear_map.restrict_scalars k, map_add' := by tidy, map_smul' := by tidy, } end restrict_scalars end linear_map
4b411239d59d3ad51d31caa0e49cf2899bf5655f
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/tests/lean/parserPrio.lean
371e4a06d25e47cbee7524af454838a9fc7d67df
[ "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
923
lean
-- -- New notation that overlaps with existing notation syntax (name := myPair) (priority := high) "(" term "," term ")" : term macro_rules (kind := myPair) | `(($a, $b)) => `([$a, $b]) #eval (1, 2) -- not ambiguous since myPair parser has higher priority theorem ex1 : (1, 2) = [1, 2] := rfl -- Define macro for expanding the builtin triple notation -- Macros bypass builtin elaboration functions macro_rules | `(($a, $b, $c)) => `($a + $b + $c) #eval (1, 2, 3) syntax (name := mySingleton) "[" term "]" : term macro_rules (kind := mySingleton) | `([$a]) => `(2 * $a) #check [1] -- ambiguous it can be `mySingleton` or the singleton list syntax (priority := 100) "(" term "," term ", " term ")" : term -- priority without a kind macro_rules | `(($a, $b, $c)) => `([$a, $b, $c]) #eval (1,2,3) theorem ex2 : (1, 2, 3) = [1, 2, 3] := rfl theorem ex3 : (1, 2, 3, 4) = Prod.mk 1 (Prod.mk 2 (Prod.mk 3 4)) := rfl
eea715df1dfac871b7c69bf744c4649cad5dc480
d436468d80b739ba7e06843c4d0d2070e43448e5
/src/analysis/normed_space/operator_norm.lean
018f6a830fcd85e4414a7e641c4dd5e755ffe262
[ "Apache-2.0" ]
permissive
roro47/mathlib
761fdc002aef92f77818f3fef06bf6ec6fc1a28e
80aa7d52537571a2ca62a3fdf71c9533a09422cf
refs/heads/master
1,599,656,410,625
1,573,649,488,000
1,573,649,488,000
221,452,951
0
0
Apache-2.0
1,573,647,693,000
1,573,647,692,000
null
UTF-8
Lean
false
false
17,703
lean
/- Copyright (c) 2019 Jan-David Salchow. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jan-David Salchow, Sébastien Gouëzel, Jean Lo Operator norm on the space of continuous linear maps Define the operator norm on the space of continuous linear maps between normed spaces, and prove its basic properties. In particular, show that this space is itself a normed space. -/ import topology.metric_space.lipschitz analysis.normed_space.riesz_lemma import analysis.asymptotics noncomputable theory open_locale classical set_option class.instance_max_depth 70 variables {𝕜 : Type*} {E : Type*} {F : Type*} {G : Type*} [normed_group E] [normed_group F] [normed_group G] open metric continuous_linear_map lemma exists_pos_bound_of_bound {f : E → F} (M : ℝ) (h : ∀x, ∥f x∥ ≤ M * ∥x∥) : ∃ N, 0 < N ∧ ∀x, ∥f x∥ ≤ N * ∥x∥ := ⟨max M 1, lt_of_lt_of_le zero_lt_one (le_max_right _ _), λx, calc ∥f x∥ ≤ M * ∥x∥ : h x ... ≤ max M 1 * ∥x∥ : mul_le_mul_of_nonneg_right (le_max_left _ _) (norm_nonneg _) ⟩ section normed_field /- Most statements in this file require the field to be non-discrete, as this is necessary to deduce an inequality ∥f x∥ ≤ C ∥x∥ from the continuity of f. However, the other direction always holds. In this section, we just assume that 𝕜 is a normed field. In the remainder of the file, it will be non-discrete. -/ variables [normed_field 𝕜] [normed_space 𝕜 E] [normed_space 𝕜 F] (f : E →ₗ[𝕜] F) lemma linear_map.continuous_of_bound (C : ℝ) (h : ∀x, ∥f x∥ ≤ C * ∥x∥) : continuous f := begin have : ∀ (x y : E), dist (f x) (f y) ≤ C * dist x y := λx y, calc dist (f x) (f y) = ∥f x - f y∥ : by rw dist_eq_norm ... = ∥f (x - y)∥ : by simp ... ≤ C * ∥x - y∥ : h _ ... = C * dist x y : by rw dist_eq_norm, exact continuous_of_lipschitz this end /-- Construct a continuous linear map from a linear map and a bound on this linear map. -/ def linear_map.with_bound (h : ∃C : ℝ, ∀x, ∥f x∥ ≤ C * ∥x∥) : E →L[𝕜] F := ⟨f, let ⟨C, hC⟩ := h in linear_map.continuous_of_bound f C hC⟩ @[simp, elim_cast] lemma linear_map_with_bound_coe (h : ∃C : ℝ, ∀x, ∥f x∥ ≤ C * ∥x∥) : ((f.with_bound h) : E →ₗ[𝕜] F) = f := rfl @[simp] lemma linear_map_with_bound_apply (h : ∃C : ℝ, ∀x, ∥f x∥ ≤ C * ∥x∥) (x : E) : f.with_bound h x = f x := rfl lemma linear_map.continuous_iff_is_closed_ker {f : E →ₗ[𝕜] 𝕜} : continuous f ↔ is_closed (f.ker : set E) := begin -- the continuity of f obviously implies that its kernel is closed refine ⟨λh, (continuous_iff_is_closed.1 h) {0} (t1_space.t1 0), λh, _⟩, -- for the other direction, we assume that the kernel is closed by_cases hf : ∀x, x ∈ f.ker, { -- if f = 0, its continuity is obvious have : (f : E → 𝕜) = (λx, 0), by { ext x, simpa using hf x }, rw this, exact continuous_const }, { /- if f is not zero, we use an element x₀ ∉ ker f such taht ∥x₀∥ ≤ 2 ∥x₀ - y∥ for all y ∈ ker f, given by Riesz's lemma, and prove that 2 ∥f x₀∥ / ∥x₀∥ gives a bound on the operator norm of f. For this, start from an arbitrary x and note that y = x₀ - (f x₀ / f x) x belongs to the kernel of f. Applying the above inequality to x₀ and y readily gives the conclusion. -/ push_neg at hf, let r : ℝ := (2 : ℝ)⁻¹, have : 0 ≤ r, by norm_num [r], have : r < 1, by norm_num [r], obtain ⟨x₀, x₀ker, h₀⟩ : ∃ (x₀ : E), x₀ ∉ f.ker ∧ ∀ y ∈ linear_map.ker f, r * ∥x₀∥ ≤ ∥x₀ - y∥, from riesz_lemma h hf this, have : x₀ ≠ 0, { assume h, have : x₀ ∈ f.ker, by { rw h, exact (linear_map.ker f).zero }, exact x₀ker this }, have rx₀_ne_zero : r * ∥x₀∥ ≠ 0, by { simp [norm_eq_zero, this], norm_num }, have : ∀x, ∥f x∥ ≤ (((r * ∥x₀∥)⁻¹) * ∥f x₀∥) * ∥x∥, { assume x, by_cases hx : f x = 0, { rw [hx, norm_zero], apply_rules [mul_nonneg', norm_nonneg, inv_nonneg.2, norm_nonneg] }, { let y := x₀ - (f x₀ * (f x)⁻¹ ) • x, have fy_zero : f y = 0, by calc f y = f x₀ - (f x₀ * (f x)⁻¹ ) * f x : by { dsimp [y], rw [f.map_add, f.map_neg, f.map_smul], refl } ... = 0 : by { rw [mul_assoc, inv_mul_cancel hx, mul_one, sub_eq_zero_of_eq], refl }, have A : r * ∥x₀∥ ≤ ∥f x₀∥ * ∥f x∥⁻¹ * ∥x∥, from calc r * ∥x₀∥ ≤ ∥x₀ - y∥ : h₀ _ (linear_map.mem_ker.2 fy_zero) ... = ∥(f x₀ * (f x)⁻¹ ) • x∥ : by { dsimp [y], congr, abel } ... = ∥f x₀∥ * ∥f x∥⁻¹ * ∥x∥ : by rw [norm_smul, normed_field.norm_mul, normed_field.norm_inv], calc ∥f x∥ = (r * ∥x₀∥)⁻¹ * (r * ∥x₀∥) * ∥f x∥ : by rwa [inv_mul_cancel, one_mul] ... ≤ (r * ∥x₀∥)⁻¹ * (∥f x₀∥ * ∥f x∥⁻¹ * ∥x∥) * ∥f x∥ : begin apply mul_le_mul_of_nonneg_right (mul_le_mul_of_nonneg_left A _) (norm_nonneg _), exact inv_nonneg.2 (mul_nonneg' (by norm_num) (norm_nonneg _)) end ... = (∥f x∥ ⁻¹ * ∥f x∥) * (((r * ∥x₀∥)⁻¹) * ∥f x₀∥) * ∥x∥ : by ring ... = (((r * ∥x₀∥)⁻¹) * ∥f x₀∥) * ∥x∥ : by { rw [inv_mul_cancel, one_mul], simp [norm_eq_zero, hx] } } }, exact linear_map.continuous_of_bound f _ this } end end normed_field variables [nondiscrete_normed_field 𝕜] [normed_space 𝕜 E] [normed_space 𝕜 F] [normed_space 𝕜 G] (c : 𝕜) (f g : E →L[𝕜] F) (h : F →L[𝕜] G) (x y z : E) include 𝕜 /-- A continuous linear map between normed spaces is bounded when the field is nondiscrete. The continuity ensures boundedness on a ball of some radius δ. The nondiscreteness is then used to rescale any element into an element of norm in [δ/C, δ], whose image has a controlled norm. The norm control for the original element follows by rescaling. -/ lemma linear_map.bound_of_continuous (f : E →ₗ[𝕜] F) (hf : continuous f) : ∃ C, 0 < C ∧ (∀ x : E, ∥f x∥ ≤ C * ∥x∥) := begin have : continuous_at f 0 := continuous_iff_continuous_at.1 hf _, rcases metric.tendsto_nhds_nhds.1 this 1 zero_lt_one with ⟨ε, ε_pos, hε⟩, let δ := ε/2, have δ_pos : δ > 0 := half_pos ε_pos, have H : ∀{a}, ∥a∥ ≤ δ → ∥f a∥ ≤ 1, { assume a ha, have : dist (f a) (f 0) ≤ 1, { apply le_of_lt (hε _), rw [dist_eq_norm, sub_zero], exact lt_of_le_of_lt ha (half_lt_self ε_pos) }, simpa using this }, rcases normed_field.exists_one_lt_norm 𝕜 with ⟨c, hc⟩, refine ⟨δ⁻¹ * ∥c∥, mul_pos (inv_pos δ_pos) (lt_trans zero_lt_one hc), (λx, _)⟩, by_cases h : x = 0, { simp only [h, norm_zero, mul_zero, linear_map.map_zero] }, { rcases rescale_to_shell hc δ_pos h with ⟨d, hd, dxle, ledx, dinv⟩, calc ∥f x∥ = ∥f ((d⁻¹ * d) • x)∥ : by rwa [inv_mul_cancel, one_smul] ... = ∥d∥⁻¹ * ∥f (d • x)∥ : by rw [mul_smul, linear_map.map_smul, norm_smul, normed_field.norm_inv] ... ≤ ∥d∥⁻¹ * 1 : mul_le_mul_of_nonneg_left (H dxle) (by { rw ← normed_field.norm_inv, exact norm_nonneg _ }) ... ≤ δ⁻¹ * ∥c∥ * ∥x∥ : by { rw mul_one, exact dinv } } end namespace continuous_linear_map theorem bound : ∃ C, 0 < C ∧ (∀ x : E, ∥f x∥ ≤ C * ∥x∥) := f.to_linear_map.bound_of_continuous f.2 section open asymptotics filter theorem is_O_id (l : filter E) : is_O f (λ x, x) l := let ⟨M, hMp, hM⟩ := f.bound in ⟨M, hMp, mem_sets_of_superset univ_mem_sets (λ x _, hM x)⟩ theorem is_O_comp {E : Type*} (g : F →L[𝕜] G) (f : E → F) (l : filter E) : is_O (λ x', g (f x')) f l := ((g.is_O_id ⊤).comp _).mono (map_le_iff_le_comap.mp lattice.le_top) theorem is_O_sub (f : E →L[𝕜] F) (l : filter E) (x : E) : is_O (λ x', f (x' - x)) (λ x', x' - x) l := is_O_comp f _ l end section op_norm open set real set_option class.instance_max_depth 100 /-- The operator norm of a continuous linear map is the inf of all its bounds. -/ def op_norm := Inf { c | c ≥ 0 ∧ ∀ x, ∥f x∥ ≤ c * ∥x∥ } instance has_op_norm : has_norm (E →L[𝕜] F) := ⟨op_norm⟩ -- So that invocations of real.Inf_le ma𝕜e sense: we show that the set of -- bounds is nonempty and bounded below. lemma bounds_nonempty {f : E →L[𝕜] F} : ∃ c, c ∈ { c | 0 ≤ c ∧ ∀ x, ∥f x∥ ≤ c * ∥x∥ } := let ⟨M, hMp, hMb⟩ := f.bound in ⟨M, le_of_lt hMp, hMb⟩ lemma bounds_bdd_below {f : E →L[𝕜] F} : bdd_below { c | 0 ≤ c ∧ ∀ x, ∥f x∥ ≤ c * ∥x∥ } := ⟨0, λ _ ⟨hn, _⟩, hn⟩ lemma op_norm_nonneg : 0 ≤ ∥f∥ := lb_le_Inf _ bounds_nonempty (λ _ ⟨hx, _⟩, hx) /-- The fundamental property of the operator norm: ∥f x∥ ≤ ∥f∥ * ∥x∥. -/ theorem le_op_norm : ∥f x∥ ≤ ∥f∥ * ∥x∥ := classical.by_cases (λ heq : x = 0, by { rw heq, simp }) (λ hne, have hlt : 0 < ∥x∥, from (norm_pos_iff _).2 hne, le_mul_of_div_le hlt ((le_Inf _ bounds_nonempty bounds_bdd_below).2 (λ c ⟨_, hc⟩, div_le_of_le_mul hlt (by { rw mul_comm, apply hc })))) lemma ratio_le_op_norm : ∥f x∥ / ∥x∥ ≤ ∥f∥ := (or.elim (lt_or_eq_of_le (norm_nonneg _)) (λ hlt, div_le_of_le_mul hlt (by { rw mul_comm, apply le_op_norm })) (λ heq, by { rw [←heq, div_zero], apply op_norm_nonneg })) /-- The image of the unit ball under a continuous linear map is bounded. -/ lemma unit_le_op_norm : ∥x∥ ≤ 1 → ∥f x∥ ≤ ∥f∥ := λ hx, begin rw [←(mul_one ∥f∥)], calc _ ≤ ∥f∥ * ∥x∥ : le_op_norm _ _ ... ≤ _ : mul_le_mul_of_nonneg_left hx (op_norm_nonneg _) end /-- If one controls the norm of every A x, then one controls the norm of A. -/ lemma op_norm_le_bound {M : ℝ} (hMp: 0 ≤ M) (hM : ∀ x, ∥f x∥ ≤ M * ∥x∥) : ∥f∥ ≤ M := Inf_le _ bounds_bdd_below ⟨hMp, hM⟩ /-- The operator norm satisfies the triangle inequality. -/ theorem op_norm_triangle : ∥f + g∥ ≤ ∥f∥ + ∥g∥ := Inf_le _ bounds_bdd_below ⟨add_nonneg (op_norm_nonneg _) (op_norm_nonneg _), λ x, by { rw add_mul, calc _ ≤ ∥f x∥ + ∥g x∥ : norm_triangle _ _ ... ≤ _ : add_le_add (le_op_norm _ _) (le_op_norm _ _) }⟩ /-- An operator is zero iff its norm vanishes. -/ theorem op_norm_zero_iff : ∥f∥ = 0 ↔ f = 0 := iff.intro (λ hn, continuous_linear_map.ext (λ x, (norm_le_zero_iff _).1 (calc _ ≤ ∥f∥ * ∥x∥ : le_op_norm _ _ ... = _ : by rw [hn, zero_mul]))) (λ hf, le_antisymm (Inf_le _ bounds_bdd_below ⟨ge_of_eq rfl, λ _, le_of_eq (by { rw [zero_mul, hf], exact norm_zero })⟩) (op_norm_nonneg _)) @[simp] lemma norm_zero : ∥(0 : E →L[𝕜] F)∥ = 0 := by rw op_norm_zero_iff /-- The norm of the identity is at most 1. It is in fact 1, except when the space is trivial where it is 0. It means that one can not do better than an inequality in general. -/ lemma norm_id : ∥(id : E →L[𝕜] E)∥ ≤ 1 := op_norm_le_bound _ zero_le_one (λx, by simp) /-- The operator norm is homogeneous. -/ lemma op_norm_smul : ∥c • f∥ = ∥c∥ * ∥f∥ := le_antisymm (Inf_le _ bounds_bdd_below ⟨mul_nonneg (norm_nonneg _) (op_norm_nonneg _), λ _, begin erw [norm_smul, mul_assoc], exact mul_le_mul_of_nonneg_left (le_op_norm _ _) (norm_nonneg _) end⟩) (lb_le_Inf _ bounds_nonempty (λ _ ⟨hn, hc⟩, (or.elim (lt_or_eq_of_le (norm_nonneg c)) (λ hlt, begin rw mul_comm, exact mul_le_of_le_div hlt (Inf_le _ bounds_bdd_below ⟨div_nonneg hn hlt, λ _, (by { rw div_mul_eq_mul_div, exact le_div_of_mul_le hlt (by { rw [ mul_comm, ←norm_smul ], exact hc _ }) })⟩) end) (λ heq, by { rw [←heq, zero_mul], exact hn })))) lemma op_norm_neg : ∥-f∥ = ∥f∥ := calc ∥-f∥ = ∥(-1:𝕜) • f∥ : by rw neg_one_smul ... = ∥(-1:𝕜)∥ * ∥f∥ : by rw op_norm_smul ... = ∥f∥ : by simp /-- Continuous linear maps themselves form a normed space with respect to the operator norm. -/ instance to_normed_group : normed_group (E →L[𝕜] F) := normed_group.of_core _ ⟨op_norm_zero_iff, op_norm_triangle, op_norm_neg⟩ /- The next instance should be found automatically, but it is not. TODO: fix me -/ instance to_normed_group_prod : normed_group (E →L[𝕜] (F × G)) := continuous_linear_map.to_normed_group instance to_normed_space : normed_space 𝕜 (E →L[𝕜] F) := ⟨op_norm_smul⟩ /-- The operator norm is submultiplicative. -/ lemma op_norm_comp_le : ∥comp h f∥ ≤ ∥h∥ * ∥f∥ := (Inf_le _ bounds_bdd_below ⟨mul_nonneg (op_norm_nonneg _) (op_norm_nonneg _), λ x, begin rw mul_assoc, calc _ ≤ ∥h∥ * ∥f x∥: le_op_norm _ _ ... ≤ _ : mul_le_mul_of_nonneg_left (le_op_norm _ _) (op_norm_nonneg _) end⟩) /-- continuous linear maps are Lipschitz continuous. -/ theorem lipschitz : lipschitz_with ∥f∥ f := ⟨op_norm_nonneg _, λ x y, by { rw [dist_eq_norm, dist_eq_norm, ←map_sub], apply le_op_norm }⟩ /-- A continuous linear map is automatically uniformly continuous. -/ theorem uniform_continuous : uniform_continuous f := f.lipschitz.to_uniform_continuous /-- A continuous linear map is a uniform embedding if it expands the norm by a constant factor. -/ theorem uniform_embedding_of_bound (C : ℝ) (hC : ∀x, ∥x∥ ≤ C * ∥f x∥) : uniform_embedding f := begin have Cpos : 0 < max C 1 := lt_of_lt_of_le zero_lt_one (le_max_right _ _), refine uniform_embedding_iff'.2 ⟨metric.uniform_continuous_iff.1 (uniform_continuous _), λδ δpos, ⟨δ / (max C 1), div_pos δpos Cpos, λx y hxy, _⟩⟩, calc dist x y = ∥x - y∥ : by rw dist_eq_norm ... ≤ C * ∥f (x - y)∥ : hC _ ... = C * dist (f x) (f y) : by rw [f.map_sub, dist_eq_norm] ... ≤ max C 1 * dist (f x) (f y) : mul_le_mul_of_nonneg_right (le_max_left _ _) dist_nonneg ... < max C 1 * (δ / max C 1) : mul_lt_mul_of_pos_left hxy Cpos ... = δ : by { rw mul_comm, exact div_mul_cancel _ (ne_of_lt Cpos).symm } end /-- If a continuous linear map is a uniform embedding, then it expands the norm by a positive factor.-/ theorem bound_of_uniform_embedding (hf : uniform_embedding f) : ∃ C : ℝ, 0 < C ∧ ∀x, ∥x∥ ≤ C * ∥f x∥ := begin obtain ⟨ε, εpos, hε⟩ : ∃ (ε : ℝ) (H : ε > 0), ∀ {x y : E}, dist (f x) (f y) < ε → dist x y < 1, from (uniform_embedding_iff.1 hf).2.2 1 zero_lt_one, let δ := ε/2, have δ_pos : δ > 0 := half_pos εpos, have H : ∀{x}, ∥f x∥ ≤ δ → ∥x∥ ≤ 1, { assume x hx, have : dist x 0 ≤ 1, { apply le_of_lt, apply hε, simp [dist_eq_norm], exact lt_of_le_of_lt hx (half_lt_self εpos) }, simpa using this }, rcases normed_field.exists_one_lt_norm 𝕜 with ⟨c, hc⟩, refine ⟨δ⁻¹ * ∥c∥, (mul_pos (inv_pos δ_pos) ((lt_trans zero_lt_one hc))), (λx, _)⟩, by_cases hx : f x = 0, { have : f x = f 0, by { simp [hx] }, have : x = 0 := (uniform_embedding_iff.1 hf).1 this, simp [this] }, { rcases rescale_to_shell hc δ_pos hx with ⟨d, hd, dxle, ledx, dinv⟩, have : ∥f (d • x)∥ ≤ δ, by simpa, have : ∥d • x∥ ≤ 1 := H this, calc ∥x∥ = ∥d∥⁻¹ * ∥d • x∥ : by rwa [← normed_field.norm_inv, ← norm_smul, ← mul_smul, inv_mul_cancel, one_smul] ... ≤ ∥d∥⁻¹ * 1 : mul_le_mul_of_nonneg_left this (inv_nonneg.2 (norm_nonneg _)) ... ≤ δ⁻¹ * ∥c∥ * ∥f x∥ : by rwa [mul_one] } end end op_norm /-- The norm of the tensor product of a scalar linear map and of an element of a normed space is the product of the norms. -/ @[simp] lemma smul_right_norm {c : E →L[𝕜] 𝕜} {f : F} : ∥smul_right c f∥ = ∥c∥ * ∥f∥ := begin refine le_antisymm _ _, { apply op_norm_le_bound _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) (λx, _), calc ∥(c x) • f∥ = ∥c x∥ * ∥f∥ : norm_smul _ _ ... ≤ (∥c∥ * ∥x∥) * ∥f∥ : mul_le_mul_of_nonneg_right (le_op_norm _ _) (norm_nonneg _) ... = ∥c∥ * ∥f∥ * ∥x∥ : by ring }, { by_cases h : ∥f∥ = 0, { rw h, simp [norm_nonneg] }, { have : 0 < ∥f∥ := lt_of_le_of_ne (norm_nonneg _) (ne.symm h), rw ← le_div_iff this, apply op_norm_le_bound _ (div_nonneg (norm_nonneg _) this) (λx, _), rw [div_mul_eq_mul_div, le_div_iff this], calc ∥c x∥ * ∥f∥ = ∥c x • f∥ : (norm_smul _ _).symm ... = ∥((smul_right c f) : E → F) x∥ : rfl ... ≤ ∥smul_right c f∥ * ∥x∥ : le_op_norm _ _ } }, end end continuous_linear_map /-- If both directions in a linear equiv `e` are continuous, then `e` is a uniform embedding. -/ lemma linear_equiv.uniform_embedding (e : E ≃ₗ[𝕜] F) (h₁ : continuous e) (h₂ : continuous e.symm) : uniform_embedding e := begin rcases linear_map.bound_of_continuous e.symm.to_linear_map h₂ with ⟨C, Cpos, hC⟩, let f : E →L[𝕜] F := { cont := h₁, ..e }, apply f.uniform_embedding_of_bound C (λx, _), have : e.symm (e x) = x := linear_equiv.symm_apply_apply _ _, conv_lhs { rw ← this }, exact hC _ end
5cd8cd42f18a687b8ee242c96879f37d3443cbcf
19e80d9dcdc050ba32a44257170460f6eb2f174b
/euler.lean
a605fc3d818161135f45c84f60565d085f84d4ca
[]
no_license
petersonbill64/lean
d36d7d93d0f1e4e5618f1426d41567e33a0da6fc
07b7b69e15785c75ebba6629be4fad92799fd7de
refs/heads/master
1,609,627,927,456
1,438,537,533,000
1,438,537,533,000
40,084,227
0
0
null
null
null
null
UTF-8
Lean
false
false
1,844
lean
import data.nat.div data.nat.gcd data.nat.sub data.set tools.fake_simplifier logic theories.number_theory.primes open nat set eq.ops well_founded decidable fake_simplifier prod bool definition eulerPhi.F (n m : ℕ) : (Π m₁ : ℕ, m₁< m → ℕ) → ℕ := nat.cases_on m (λ f, nat.zero) (λ n₁(f : Π m₁, m₁ < succ n₁ → ℕ), (if coprime (succ n₁) n then 1 else 0) + (f n₁ !lt_succ_self)) definition eulerPhi (n : ℕ) : ℕ := fix (eulerPhi.F n) n eval eulerPhi 6 eval prime eval absurd check gcd_dvd_right theorem eq_one_from_prime_dvd_lt (k p : ℕ) (primep : prime p) (k_div_p : k ∣ p) (k_lt_p : k < p) : k = 1:= have H1 : k ≠ p, from and.elim_right ((relation.iff_ops.mp (lt_iff_le_and_ne k p)) (k_lt_p)), or_resolve_left (((and.elim_right primep) k) k_div_p) H1 theorem coprime_of_prime_of_pos_of_lt (i p : ℕ) (primep : prime p) (i_gt_zero : i > 0) (i_lt_p : i < p) : coprime i p := have H1a : (gcd i p) ∣ p, from gcd_dvd_right i p, have H1b : (gcd i p) ∣ i, from gcd_dvd_left i p, have H2 : (gcd i p) ≤ i, from le_of_dvd i_gt_zero H1b, have H3 : (gcd i p) < p, from lt_of_le_of_lt H2 i_lt_p, eq_one_from_prime_dvd_lt (gcd i p) p primep H1a H3 check coprime_of_prime_of_pos_of_lt private definition eulerPhi_prime_minus_one.F (p : ℕ) : (Π p₂ : ℕ, p₂ < p → prime p₂ → eulerPhi p₂ = p₂ - 1) → prime p → eulerPhi p = p - 1 := take ff : (Π p₂ : ℕ, p₂ < p → prime p₂ → eulerPhi p₂ = p₂ - 1), take primep : prime p, nat.cases_on p (sorry) (λ p₁, sorry) check eulerPhi_prime_minus_one.F theorem eulerPhi_prime_minus_one (p : ℕ) : prime p → eulerPhi p = p - 1 := sorry check eulerPhi_prime_minus_one
681d492bd5e7a7e50f8a105f9269f2ad4f8bfe50
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/list/permutation.lean
5df5a069ed19d0945d53e2b3c7cb373524b6f913
[ "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
10,845
lean
/- Copyright (c) 2014 Parikshit Khanna. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura, Floris van Doorn, Mario Carneiro -/ import data.list.join /-! # Permutations of a list In this file we prove properties about `list.permutations`, a list of all permutations of a list. It is defined in `data.list.defs`. ## Order of the permutations Designed for performance, the order in which the permutations appear in `list.permutations` is rather intricate and not very amenable to induction. That's why we also provide `list.permutations'` as a less efficient but more straightforward way of listing permutations. ### `list.permutations` TODO. In the meantime, you can try decrypting the docstrings. ### `list.permutations'` The list of partitions is built by recursion. The permutations of `[]` are `[[]]`. Then, the permutations of `a :: l` are obtained by taking all permutations of `l` in order and adding `a` in all positions. Hence, to build `[0, 1, 2, 3].permutations'`, it does * `[[]]` * `[[3]]` * `[[2, 3], [3, 2]]]` * `[[1, 2, 3], [2, 1, 3], [2, 3, 1], [1, 3, 2], [3, 1, 2], [3, 2, 1]]` * `[[0, 1, 2, 3], [1, 0, 2, 3], [1, 2, 0, 3], [1, 2, 3, 0],` `[0, 2, 1, 3], [2, 0, 1, 3], [2, 1, 0, 3], [2, 1, 3, 0],` `[0, 2, 3, 1], [2, 0, 3, 1], [2, 3, 0, 1], [2, 3, 1, 0],` `[0, 1, 3, 2], [1, 0, 3, 2], [1, 3, 0, 2], [1, 3, 2, 0],` `[0, 3, 1, 2], [3, 0, 1, 2], [3, 1, 0, 2], [3, 1, 2, 0],` `[0, 3, 2, 1], [3, 0, 2, 1], [3, 2, 0, 1], [3, 2, 1, 0]]` ## TODO Show that `l.nodup → l.permutations.nodup`. See `data.fintype.list`. -/ open nat variables {α β : Type*} namespace list lemma permutations_aux2_fst (t : α) (ts : list α) (r : list β) : ∀ (ys : list α) (f : list α → β), (permutations_aux2 t ts r ys f).1 = ys ++ ts | [] f := rfl | (y::ys) f := match _, permutations_aux2_fst ys _ : ∀ o : list α × list β, o.1 = ys ++ ts → (permutations_aux2._match_1 t y f o).1 = y :: ys ++ ts with | ⟨_, zs⟩, rfl := rfl end @[simp] lemma permutations_aux2_snd_nil (t : α) (ts : list α) (r : list β) (f : list α → β) : (permutations_aux2 t ts r [] f).2 = r := rfl @[simp] lemma permutations_aux2_snd_cons (t : α) (ts : list α) (r : list β) (y : α) (ys : list α) (f : list α → β) : (permutations_aux2 t ts r (y::ys) f).2 = f (t :: y :: ys ++ ts) :: (permutations_aux2 t ts r ys (λx : list α, f (y::x))).2 := match _, permutations_aux2_fst t ts r _ _ : ∀ o : list α × list β, o.1 = ys ++ ts → (permutations_aux2._match_1 t y f o).2 = f (t :: y :: ys ++ ts) :: o.2 with | ⟨_, zs⟩, rfl := rfl end /-- The `r` argument to `permutations_aux2` is the same as appending. -/ lemma permutations_aux2_append (t : α) (ts : list α) (r : list β) (ys : list α) (f : list α → β) : (permutations_aux2 t ts nil ys f).2 ++ r = (permutations_aux2 t ts r ys f).2 := by induction ys generalizing f; simp * /-- The `ts` argument to `permutations_aux2` can be folded into the `f` argument. -/ lemma permutations_aux2_comp_append {t : α} {ts ys : list α} {r : list β} (f : list α → β) : (permutations_aux2 t [] r ys $ λ x, f (x ++ ts)).2 = (permutations_aux2 t ts r ys f).2 := begin induction ys generalizing f, { simp }, { simp [ys_ih (λ xs, f (ys_hd :: xs))] }, end lemma map_permutations_aux2' {α β α' β'} (g : α → α') (g' : β → β') (t : α) (ts ys : list α) (r : list β) (f : list α → β) (f' : list α' → β') (H : ∀ a, g' (f a) = f' (map g a)) : map g' (permutations_aux2 t ts r ys f).2 = (permutations_aux2 (g t) (map g ts) (map g' r) (map g ys) f').2 := begin induction ys generalizing f f'; simp *, apply ys_ih, simp [H], end /-- The `f` argument to `permutations_aux2` when `r = []` can be eliminated. -/ lemma map_permutations_aux2 (t : α) (ts : list α) (ys : list α) (f : list α → β) : (permutations_aux2 t ts [] ys id).2.map f = (permutations_aux2 t ts [] ys f).2 := begin convert map_permutations_aux2' id _ _ _ _ _ _ _ _; simp only [map_id, id.def], exact (λ _, rfl) end /-- An expository lemma to show how all of `ts`, `r`, and `f` can be eliminated from `permutations_aux2`. `(permutations_aux2 t [] [] ys id).2`, which appears on the RHS, is a list whose elements are produced by inserting `t` into every non-terminal position of `ys` in order. As an example: ```lean #eval permutations_aux2 1 [] [] [2, 3, 4] id -- [[1, 2, 3, 4], [2, 1, 3, 4], [2, 3, 1, 4]] ``` -/ lemma permutations_aux2_snd_eq (t : α) (ts : list α) (r : list β) (ys : list α) (f : list α → β) : (permutations_aux2 t ts r ys f).2 = (permutations_aux2 t [] [] ys id).2.map (λ x, f (x ++ ts)) ++ r := by rw [← permutations_aux2_append, map_permutations_aux2, permutations_aux2_comp_append] lemma map_map_permutations_aux2 {α α'} (g : α → α') (t : α) (ts ys : list α) : map (map g) (permutations_aux2 t ts [] ys id).2 = (permutations_aux2 (g t) (map g ts) [] (map g ys) id).2 := map_permutations_aux2' _ _ _ _ _ _ _ _ (λ _, rfl) lemma map_map_permutations'_aux (f : α → β) (t : α) (ts : list α) : map (map f) (permutations'_aux t ts) = permutations'_aux (f t) (map f ts) := by induction ts with a ts ih; [refl, {simp [← ih], refl}] lemma permutations'_aux_eq_permutations_aux2 (t : α) (ts : list α) : permutations'_aux t ts = (permutations_aux2 t [] [ts ++ [t]] ts id).2 := begin induction ts with a ts ih, {refl}, simp [permutations'_aux, permutations_aux2_snd_cons, ih], simp only [← permutations_aux2_append] {single_pass := tt}, simp [map_permutations_aux2], end lemma mem_permutations_aux2 {t : α} {ts : list α} {ys : list α} {l l' : list α} : l' ∈ (permutations_aux2 t ts [] ys (append l)).2 ↔ ∃ l₁ l₂, l₂ ≠ [] ∧ ys = l₁ ++ l₂ ∧ l' = l ++ l₁ ++ t :: l₂ ++ ts := begin induction ys with y ys ih generalizing l, { simp {contextual := tt} }, rw [permutations_aux2_snd_cons, show (λ (x : list α), l ++ y :: x) = append (l ++ [y]), by funext; simp, mem_cons_iff, ih], split, { rintro (e | ⟨l₁, l₂, l0, ye, _⟩), { subst l', exact ⟨[], y::ys, by simp⟩ }, { substs l' ys, exact ⟨y::l₁, l₂, l0, by simp⟩ } }, { rintro ⟨_ | ⟨y', l₁⟩, l₂, l0, ye, rfl⟩, { simp [ye] }, { simp only [cons_append] at ye, rcases ye with ⟨rfl, rfl⟩, exact or.inr ⟨l₁, l₂, l0, by simp⟩ } } end lemma mem_permutations_aux2' {t : α} {ts : list α} {ys : list α} {l : list α} : l ∈ (permutations_aux2 t ts [] ys id).2 ↔ ∃ l₁ l₂, l₂ ≠ [] ∧ ys = l₁ ++ l₂ ∧ l = l₁ ++ t :: l₂ ++ ts := by rw [show @id (list α) = append nil, by funext; refl]; apply mem_permutations_aux2 lemma length_permutations_aux2 (t : α) (ts : list α) (ys : list α) (f : list α → β) : length (permutations_aux2 t ts [] ys f).2 = length ys := by induction ys generalizing f; simp * lemma foldr_permutations_aux2 (t : α) (ts : list α) (r L : list (list α)) : foldr (λy r, (permutations_aux2 t ts r y id).2) r L = L.bind (λ y, (permutations_aux2 t ts [] y id).2) ++ r := by induction L with l L ih; [refl, {simp [ih], rw ← permutations_aux2_append}] lemma mem_foldr_permutations_aux2 {t : α} {ts : list α} {r L : list (list α)} {l' : list α} : l' ∈ foldr (λy r, (permutations_aux2 t ts r y id).2) r L ↔ l' ∈ r ∨ ∃ l₁ l₂, l₁ ++ l₂ ∈ L ∧ l₂ ≠ [] ∧ l' = l₁ ++ t :: l₂ ++ ts := have (∃ (a : list α), a ∈ L ∧ ∃ (l₁ l₂ : list α), ¬l₂ = nil ∧ a = l₁ ++ l₂ ∧ l' = l₁ ++ t :: (l₂ ++ ts)) ↔ ∃ (l₁ l₂ : list α), ¬l₂ = nil ∧ l₁ ++ l₂ ∈ L ∧ l' = l₁ ++ t :: (l₂ ++ ts), from ⟨λ ⟨a, aL, l₁, l₂, l0, e, h⟩, ⟨l₁, l₂, l0, e ▸ aL, h⟩, λ ⟨l₁, l₂, l0, aL, h⟩, ⟨_, aL, l₁, l₂, l0, rfl, h⟩⟩, by rw foldr_permutations_aux2; simp [mem_permutations_aux2', this, or.comm, or.left_comm, or.assoc, and.comm, and.left_comm, and.assoc] lemma length_foldr_permutations_aux2 (t : α) (ts : list α) (r L : list (list α)) : length (foldr (λy r, (permutations_aux2 t ts r y id).2) r L) = sum (map length L) + length r := by simp [foldr_permutations_aux2, (∘), length_permutations_aux2] lemma length_foldr_permutations_aux2' (t : α) (ts : list α) (r L : list (list α)) (n) (H : ∀ l ∈ L, length l = n) : length (foldr (λy r, (permutations_aux2 t ts r y id).2) r L) = n * length L + length r := begin rw [length_foldr_permutations_aux2, (_ : sum (map length L) = n * length L)], induction L with l L ih, {simp}, have sum_map : sum (map length L) = n * length L := ih (λ l m, H l (mem_cons_of_mem _ m)), have length_l : length l = n := H _ (mem_cons_self _ _), simp [sum_map, length_l, mul_add, add_comm] end @[simp] lemma permutations_aux_nil (is : list α) : permutations_aux [] is = [] := by rw [permutations_aux, permutations_aux.rec] @[simp] lemma permutations_aux_cons (t : α) (ts is : list α) : permutations_aux (t :: ts) is = foldr (λy r, (permutations_aux2 t ts r y id).2) (permutations_aux ts (t::is)) (permutations is) := by rw [permutations_aux, permutations_aux.rec]; refl @[simp] lemma permutations_nil : permutations ([] : list α) = [[]] := by rw [permutations, permutations_aux_nil] lemma map_permutations_aux (f : α → β) : ∀ (ts is : list α), map (map f) (permutations_aux ts is) = permutations_aux (map f ts) (map f is) := begin refine permutations_aux.rec (by simp) _, introv IH1 IH2, rw map at IH2, simp only [foldr_permutations_aux2, map_append, map, map_map_permutations_aux2, permutations, bind_map, IH1, append_assoc, permutations_aux_cons, cons_bind, ← IH2, map_bind], end lemma map_permutations (f : α → β) (ts : list α) : map (map f) (permutations ts) = permutations (map f ts) := by rw [permutations, permutations, map, map_permutations_aux, map] lemma map_permutations' (f : α → β) (ts : list α) : map (map f) (permutations' ts) = permutations' (map f ts) := by induction ts with t ts ih; [refl, simp [← ih, map_bind, ← map_map_permutations'_aux, bind_map]] lemma permutations_aux_append (is is' ts : list α) : permutations_aux (is ++ ts) is' = (permutations_aux is is').map (++ ts) ++ permutations_aux ts (is.reverse ++ is') := begin induction is with t is ih generalizing is', {simp}, simp [foldr_permutations_aux2, ih, bind_map], congr' 2, funext ys, rw [map_permutations_aux2], simp only [← permutations_aux2_comp_append] {single_pass := tt}, simp only [id, append_assoc], end lemma permutations_append (is ts : list α) : permutations (is ++ ts) = (permutations is).map (++ ts) ++ permutations_aux ts is.reverse := by simp [permutations, permutations_aux_append] end list
49767e8d23fc1ebd64d5c1e2b38a8d8c1825e220
63abd62053d479eae5abf4951554e1064a4c45b4
/src/number_theory/sum_two_squares.lean
f7a7f209c299f200ae2b75d55dde89a5e0bf3eb0
[ "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
784
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Chris Hughes -/ import data.zsqrtd.gaussian_int /-! # Sums of two squares Proof of Fermat's theorem on the sum of two squares. Every prime congruent to 1 mod 4 is the sum of two squares -/ open gaussian_int principal_ideal_ring namespace nat namespace prime /-- Fermat's theorem on the sum of two squares. Every prime congruent to 1 mod 4 is the sum of two squares -/ lemma sum_two_squares (p : ℕ) [hp : _root_.fact p.prime] (hp1 : p % 4 = 1) : ∃ a b : ℕ, a ^ 2 + b ^ 2 = p := sum_two_squares_of_nat_prime_of_not_irreducible p (by rw [irreducible_iff_prime, prime_iff_mod_four_eq_three_of_nat_prime p, hp1]; norm_num) end prime end nat
e6a2a13be58719e67d4ef04f17bcac46cd09b431
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/topology/metric_space/isometry_auto.lean
3b4c3aa8ba73282ceb453d16189ab2909e650c51
[]
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
21,477
lean
/- Copyright (c) 2018 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Isometries of emetric and metric spaces Authors: Sébastien Gouëzel -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.topology.bounded_continuous_function import Mathlib.topology.compacts import Mathlib.PostPort universes u v w u_1 u_2 l namespace Mathlib /-! # Isometries We define isometries, i.e., maps between emetric spaces that preserve the edistance (on metric spaces, these are exactly the maps that preserve distances), and prove their basic properties. We also introduce isometric bijections. -/ /-- An isometry (also known as isometric embedding) is a map preserving the edistance between emetric spaces, or equivalently the distance between metric space. -/ def isometry {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (f : α → β) := ∀ (x1 x2 : α), edist (f x1) (f x2) = edist x1 x2 /-- On metric spaces, a map is an isometry if and only if it preserves distances. -/ theorem isometry_emetric_iff_metric {α : Type u} {β : Type v} [metric_space α] [metric_space β] {f : α → β} : isometry f ↔ ∀ (x y : α), dist (f x) (f y) = dist x y := sorry /-- An isometry preserves edistances. -/ theorem isometry.edist_eq {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {f : α → β} (hf : isometry f) (x : α) (y : α) : edist (f x) (f y) = edist x y := hf x y /-- An isometry preserves distances. -/ theorem isometry.dist_eq {α : Type u} {β : Type v} [metric_space α] [metric_space β] {f : α → β} (hf : isometry f) (x : α) (y : α) : dist (f x) (f y) = dist x y := sorry theorem isometry.lipschitz {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {f : α → β} (h : isometry f) : lipschitz_with 1 f := lipschitz_with.of_edist_le fun (x y : α) => le_of_eq (h x y) theorem isometry.antilipschitz {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {f : α → β} (h : isometry f) : antilipschitz_with 1 f := sorry /-- An isometry is injective -/ theorem isometry.injective {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {f : α → β} (h : isometry f) : function.injective f := antilipschitz_with.injective (isometry.antilipschitz h) /-- Any map on a subsingleton is an isometry -/ theorem isometry_subsingleton {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {f : α → β} [subsingleton α] : isometry f := sorry /-- The identity is an isometry -/ theorem isometry_id {α : Type u} [emetric_space α] : isometry id := fun (x y : α) => rfl /-- The composition of isometries is an isometry -/ theorem isometry.comp {α : Type u} {β : Type v} {γ : Type w} [emetric_space α] [emetric_space β] [emetric_space γ] {g : β → γ} {f : α → β} (hg : isometry g) (hf : isometry f) : isometry (g ∘ f) := fun (x y : α) => Eq.trans (hg (f x) (f y)) (hf x y) /-- An isometry is an embedding -/ theorem isometry.uniform_embedding {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {f : α → β} (hf : isometry f) : uniform_embedding f := antilipschitz_with.uniform_embedding (isometry.antilipschitz hf) (lipschitz_with.uniform_continuous (isometry.lipschitz hf)) /-- An isometry is continuous. -/ theorem isometry.continuous {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {f : α → β} (hf : isometry f) : continuous f := lipschitz_with.continuous (isometry.lipschitz hf) /-- The right inverse of an isometry is an isometry. -/ theorem isometry.right_inv {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {f : α → β} {g : β → α} (h : isometry f) (hg : function.right_inverse g f) : isometry g := sorry /-- Isometries preserve the diameter in emetric spaces. -/ theorem isometry.ediam_image {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {f : α → β} (hf : isometry f) (s : set α) : emetric.diam (f '' s) = emetric.diam s := sorry theorem isometry.ediam_range {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {f : α → β} (hf : isometry f) : emetric.diam (set.range f) = emetric.diam set.univ := eq.mpr (id (Eq._oldrec (Eq.refl (emetric.diam (set.range f) = emetric.diam set.univ)) (Eq.symm set.image_univ))) (isometry.ediam_image hf set.univ) /-- The injection from a subtype is an isometry -/ theorem isometry_subtype_coe {α : Type u} [emetric_space α] {s : set α} : isometry coe := fun (x y : ↥s) => rfl /-- An isometry preserves the diameter in metric spaces. -/ theorem isometry.diam_image {α : Type u} {β : Type v} [metric_space α] [metric_space β] {f : α → β} (hf : isometry f) (s : set α) : metric.diam (f '' s) = metric.diam s := sorry theorem isometry.diam_range {α : Type u} {β : Type v} [metric_space α] [metric_space β] {f : α → β} (hf : isometry f) : metric.diam (set.range f) = metric.diam set.univ := eq.mpr (id (Eq._oldrec (Eq.refl (metric.diam (set.range f) = metric.diam set.univ)) (Eq.symm set.image_univ))) (isometry.diam_image hf set.univ) namespace add_monoid_hom theorem isometry_iff_norm {E : Type u_1} {F : Type u_2} [normed_group E] [normed_group F] (f : E →+ F) : isometry ⇑f ↔ ∀ (x : E), norm (coe_fn f x) = norm x := sorry theorem isometry_of_norm {E : Type u_1} {F : Type u_2} [normed_group E] [normed_group F] (f : E →+ F) (hf : ∀ (x : E), norm (coe_fn f x) = norm x) : isometry ⇑f := iff.mpr (isometry_iff_norm f) hf end add_monoid_hom /-- `α` and `β` are isometric if there is an isometric bijection between them. -/ structure isometric (α : Type u_1) (β : Type u_2) [emetric_space α] [emetric_space β] extends α ≃ β where isometry_to_fun : isometry (equiv.to_fun _to_equiv) infixl:25 " ≃ᵢ " => Mathlib.isometric namespace isometric protected instance has_coe_to_fun {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] : has_coe_to_fun (α ≃ᵢ β) := has_coe_to_fun.mk (fun (_x : α ≃ᵢ β) => α → β) fun (e : α ≃ᵢ β) => ⇑(to_equiv e) theorem coe_eq_to_equiv {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (h : α ≃ᵢ β) (a : α) : coe_fn h a = coe_fn (to_equiv h) a := rfl @[simp] theorem coe_to_equiv {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (h : α ≃ᵢ β) : ⇑(to_equiv h) = ⇑h := rfl protected theorem isometry {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (h : α ≃ᵢ β) : isometry ⇑h := isometry_to_fun h protected theorem edist_eq {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (h : α ≃ᵢ β) (x : α) (y : α) : edist (coe_fn h x) (coe_fn h y) = edist x y := isometry.edist_eq (isometric.isometry h) x y protected theorem dist_eq {α : Type u_1} {β : Type u_2} [metric_space α] [metric_space β] (h : α ≃ᵢ β) (x : α) (y : α) : dist (coe_fn h x) (coe_fn h y) = dist x y := isometry.dist_eq (isometric.isometry h) x y protected theorem continuous {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (h : α ≃ᵢ β) : continuous ⇑h := isometry.continuous (isometric.isometry h) @[simp] theorem ediam_image {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (h : α ≃ᵢ β) (s : set α) : emetric.diam (⇑h '' s) = emetric.diam s := isometry.ediam_image (isometric.isometry h) s theorem to_equiv_inj {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {h₁ : α ≃ᵢ β} {h₂ : α ≃ᵢ β} : to_equiv h₁ = to_equiv h₂ → h₁ = h₂ := sorry theorem ext {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {h₁ : α ≃ᵢ β} {h₂ : α ≃ᵢ β} (H : ∀ (x : α), coe_fn h₁ x = coe_fn h₂ x) : h₁ = h₂ := to_equiv_inj (equiv.ext H) /-- Alternative constructor for isometric bijections, taking as input an isometry, and a right inverse. -/ def mk' {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (f : α → β) (g : β → α) (hfg : ∀ (x : β), f (g x) = x) (hf : isometry f) : α ≃ᵢ β := mk (equiv.mk f g sorry hfg) hf /-- The identity isometry of a space. -/ protected def refl (α : Type u_1) [emetric_space α] : α ≃ᵢ α := mk (equiv.mk (equiv.to_fun (equiv.refl α)) (equiv.inv_fun (equiv.refl α)) sorry sorry) isometry_id /-- The composition of two isometric isomorphisms, as an isometric isomorphism. -/ protected def trans {α : Type u} {β : Type v} {γ : Type w} [emetric_space α] [emetric_space β] [emetric_space γ] (h₁ : α ≃ᵢ β) (h₂ : β ≃ᵢ γ) : α ≃ᵢ γ := mk (equiv.mk (equiv.to_fun (equiv.trans (to_equiv h₁) (to_equiv h₂))) (equiv.inv_fun (equiv.trans (to_equiv h₁) (to_equiv h₂))) sorry sorry) sorry @[simp] theorem trans_apply {α : Type u} {β : Type v} {γ : Type w} [emetric_space α] [emetric_space β] [emetric_space γ] (h₁ : α ≃ᵢ β) (h₂ : β ≃ᵢ γ) (x : α) : coe_fn (isometric.trans h₁ h₂) x = coe_fn h₂ (coe_fn h₁ x) := rfl /-- The inverse of an isometric isomorphism, as an isometric isomorphism. -/ protected def symm {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (h : α ≃ᵢ β) : β ≃ᵢ α := mk (equiv.symm (to_equiv h)) sorry @[simp] theorem symm_symm {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (h : α ≃ᵢ β) : isometric.symm (isometric.symm h) = h := to_equiv_inj (equiv.symm_symm (to_equiv h)) @[simp] theorem apply_symm_apply {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (h : α ≃ᵢ β) (y : β) : coe_fn h (coe_fn (isometric.symm h) y) = y := equiv.apply_symm_apply (to_equiv h) y @[simp] theorem symm_apply_apply {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (h : α ≃ᵢ β) (x : α) : coe_fn (isometric.symm h) (coe_fn h x) = x := equiv.symm_apply_apply (to_equiv h) x theorem symm_apply_eq {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (h : α ≃ᵢ β) {x : α} {y : β} : coe_fn (isometric.symm h) y = x ↔ y = coe_fn h x := equiv.symm_apply_eq (to_equiv h) theorem eq_symm_apply {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (h : α ≃ᵢ β) {x : α} {y : β} : x = coe_fn (isometric.symm h) y ↔ coe_fn h x = y := equiv.eq_symm_apply (to_equiv h) theorem symm_comp_self {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (h : α ≃ᵢ β) : ⇑(isometric.symm h) ∘ ⇑h = id := funext fun (a : α) => equiv.left_inv (to_equiv h) a theorem self_comp_symm {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (h : α ≃ᵢ β) : ⇑h ∘ ⇑(isometric.symm h) = id := funext fun (a : β) => equiv.right_inv (to_equiv h) a @[simp] theorem range_eq_univ {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (h : α ≃ᵢ β) : set.range ⇑h = set.univ := set.eq_univ_of_forall fun (b : β) => Exists.intro (coe_fn (isometric.symm h) b) (congr_fun (self_comp_symm h) b) theorem image_symm {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (h : α ≃ᵢ β) : set.image ⇑(isometric.symm h) = set.preimage ⇑h := set.image_eq_preimage_of_inverse (equiv.left_inv (to_equiv (isometric.symm h))) (equiv.right_inv (to_equiv (isometric.symm h))) theorem preimage_symm {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (h : α ≃ᵢ β) : set.preimage ⇑(isometric.symm h) = set.image ⇑h := Eq.symm (set.image_eq_preimage_of_inverse (equiv.left_inv (to_equiv h)) (equiv.right_inv (to_equiv h))) @[simp] theorem symm_trans_apply {α : Type u} {β : Type v} {γ : Type w} [emetric_space α] [emetric_space β] [emetric_space γ] (h₁ : α ≃ᵢ β) (h₂ : β ≃ᵢ γ) (x : γ) : coe_fn (isometric.symm (isometric.trans h₁ h₂)) x = coe_fn (isometric.symm h₁) (coe_fn (isometric.symm h₂) x) := rfl theorem ediam_univ {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (h : α ≃ᵢ β) : emetric.diam set.univ = emetric.diam set.univ := sorry @[simp] theorem ediam_preimage {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (h : α ≃ᵢ β) (s : set β) : emetric.diam (⇑h ⁻¹' s) = emetric.diam s := sorry /-- The (bundled) homeomorphism associated to an isometric isomorphism. -/ protected def to_homeomorph {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (h : α ≃ᵢ β) : α ≃ₜ β := homeomorph.mk (to_equiv h) @[simp] theorem coe_to_homeomorph {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (h : α ≃ᵢ β) : ⇑(isometric.to_homeomorph h) = ⇑h := rfl @[simp] theorem coe_to_homeomorph_symm {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (h : α ≃ᵢ β) : ⇑(homeomorph.symm (isometric.to_homeomorph h)) = ⇑(isometric.symm h) := rfl @[simp] theorem to_homeomorph_to_equiv {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (h : α ≃ᵢ β) : homeomorph.to_equiv (isometric.to_homeomorph h) = to_equiv h := rfl @[simp] theorem comp_continuous_on_iff {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {γ : Type u_1} [topological_space γ] (h : α ≃ᵢ β) {f : γ → α} {s : set γ} : continuous_on (⇑h ∘ f) s ↔ continuous_on f s := homeomorph.comp_continuous_on_iff (isometric.to_homeomorph h) f s @[simp] theorem comp_continuous_iff {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {γ : Type u_1} [topological_space γ] (h : α ≃ᵢ β) {f : γ → α} : continuous (⇑h ∘ f) ↔ continuous f := homeomorph.comp_continuous_iff (isometric.to_homeomorph h) @[simp] theorem comp_continuous_iff' {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {γ : Type u_1} [topological_space γ] (h : α ≃ᵢ β) {f : β → γ} : continuous (f ∘ ⇑h) ↔ continuous f := homeomorph.comp_continuous_iff' (isometric.to_homeomorph h) /-- The group of isometries. -/ protected instance group {α : Type u} [emetric_space α] : group (α ≃ᵢ α) := group.mk (fun (e₁ e₂ : α ≃ᵢ α) => isometric.trans e₂ e₁) sorry (isometric.refl α) sorry sorry isometric.symm (div_inv_monoid.div._default (fun (e₁ e₂ : α ≃ᵢ α) => isometric.trans e₂ e₁) sorry (isometric.refl α) sorry sorry isometric.symm) sorry @[simp] theorem coe_one {α : Type u} [emetric_space α] : ⇑1 = id := rfl @[simp] theorem coe_mul {α : Type u} [emetric_space α] (e₁ : α ≃ᵢ α) (e₂ : α ≃ᵢ α) : ⇑(e₁ * e₂) = ⇑e₁ ∘ ⇑e₂ := rfl theorem mul_apply {α : Type u} [emetric_space α] (e₁ : α ≃ᵢ α) (e₂ : α ≃ᵢ α) (x : α) : coe_fn (e₁ * e₂) x = coe_fn e₁ (coe_fn e₂ x) := rfl @[simp] theorem inv_apply_self {α : Type u} [emetric_space α] (e : α ≃ᵢ α) (x : α) : coe_fn (e⁻¹) (coe_fn e x) = x := symm_apply_apply e x @[simp] theorem apply_inv_self {α : Type u} [emetric_space α] (e : α ≃ᵢ α) (x : α) : coe_fn e (coe_fn (e⁻¹) x) = x := apply_symm_apply e x /-- Addition `y ↦ y + x` as an `isometry`. -/ protected def add_right {G : Type u_1} [normed_group G] (x : G) : G ≃ᵢ G := mk (equiv.mk (equiv.to_fun (equiv.add_right x)) (equiv.inv_fun (equiv.add_right x)) sorry sorry) sorry @[simp] theorem add_right_to_equiv {G : Type u_1} [normed_group G] (x : G) : to_equiv (isometric.add_right x) = equiv.add_right x := rfl @[simp] theorem coe_add_right {G : Type u_1} [normed_group G] (x : G) : ⇑(isometric.add_right x) = fun (y : G) => y + x := rfl theorem add_right_apply {G : Type u_1} [normed_group G] (x : G) (y : G) : coe_fn (isometric.add_right x) y = y + x := rfl @[simp] theorem add_right_symm {G : Type u_1} [normed_group G] (x : G) : isometric.symm (isometric.add_right x) = isometric.add_right (-x) := ext fun (y : G) => rfl /-- Addition `y ↦ x + y` as an `isometry`. -/ protected def add_left {G : Type u_1} [normed_group G] (x : G) : G ≃ᵢ G := mk (equiv.add_left x) sorry @[simp] theorem add_left_to_equiv {G : Type u_1} [normed_group G] (x : G) : to_equiv (isometric.add_left x) = equiv.add_left x := rfl @[simp] theorem coe_add_left {G : Type u_1} [normed_group G] (x : G) : ⇑(isometric.add_left x) = Add.add x := rfl @[simp] theorem add_left_symm {G : Type u_1} [normed_group G] (x : G) : isometric.symm (isometric.add_left x) = isometric.add_left (-x) := ext fun (y : G) => rfl /-- Negation `x ↦ -x` as an `isometry`. -/ protected def neg (G : Type u_1) [normed_group G] : G ≃ᵢ G := mk (equiv.neg G) sorry @[simp] theorem neg_symm {G : Type u_1} [normed_group G] : isometric.symm (isometric.neg G) = isometric.neg G := rfl @[simp] theorem neg_to_equiv {G : Type u_1} [normed_group G] : to_equiv (isometric.neg G) = equiv.neg G := rfl @[simp] theorem coe_neg {G : Type u_1} [normed_group G] : ⇑(isometric.neg G) = Neg.neg := rfl end isometric namespace isometric @[simp] theorem diam_image {α : Type u} {β : Type v} [metric_space α] [metric_space β] (h : α ≃ᵢ β) (s : set α) : metric.diam (⇑h '' s) = metric.diam s := isometry.diam_image (isometric.isometry h) s @[simp] theorem diam_preimage {α : Type u} {β : Type v} [metric_space α] [metric_space β] (h : α ≃ᵢ β) (s : set β) : metric.diam (⇑h ⁻¹' s) = metric.diam s := sorry theorem diam_univ {α : Type u} {β : Type v} [metric_space α] [metric_space β] (h : α ≃ᵢ β) : metric.diam set.univ = metric.diam set.univ := congr_arg ennreal.to_real (ediam_univ h) end isometric /-- An isometry induces an isometric isomorphism between the source space and the range of the isometry. -/ def isometry.isometric_on_range {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {f : α → β} (h : isometry f) : α ≃ᵢ ↥(set.range f) := isometric.mk (equiv.mk (equiv.to_fun (equiv.set.range f (isometry.injective h))) (equiv.inv_fun (equiv.set.range f (isometry.injective h))) sorry sorry) sorry @[simp] theorem isometry.isometric_on_range_apply {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {f : α → β} (h : isometry f) (x : α) : coe_fn (isometry.isometric_on_range h) x = { val := f x, property := set.mem_range_self x } := rfl /-- In a normed algebra, the inclusion of the base field in the extended field is an isometry. -/ theorem algebra_map_isometry (𝕜 : Type u_1) (𝕜' : Type u_2) [normed_field 𝕜] [normed_ring 𝕜'] [normed_algebra 𝕜 𝕜'] : isometry ⇑(algebra_map 𝕜 𝕜') := sorry /-- The space of bounded sequences, with its sup norm -/ def ℓ_infty_ℝ := bounded_continuous_function ℕ ℝ namespace Kuratowski_embedding /-! ### In this section, we show that any separable metric space can be embedded isometrically in ℓ^∞(ℝ) -/ /-- A metric space can be embedded in `l^∞(ℝ)` via the distances to points in a fixed countable set, if this set is dense. This map is given in the next definition, without density assumptions. -/ def embedding_of_subset {α : Type u} [metric_space α] (x : ℕ → α) (a : α) : ℓ_infty_ℝ := bounded_continuous_function.of_normed_group_discrete (fun (n : ℕ) => dist a (x n) - dist (x 0) (x n)) (dist a (x 0)) sorry theorem embedding_of_subset_coe {α : Type u} {n : ℕ} [metric_space α] (x : ℕ → α) (a : α) : coe_fn (embedding_of_subset x a) n = dist a (x n) - dist (x 0) (x n) := rfl /-- The embedding map is always a semi-contraction. -/ theorem embedding_of_subset_dist_le {α : Type u} [metric_space α] (x : ℕ → α) (a : α) (b : α) : dist (embedding_of_subset x a) (embedding_of_subset x b) ≤ dist a b := sorry /-- When the reference set is dense, the embedding map is an isometry on its image. -/ theorem embedding_of_subset_isometry {α : Type u} [metric_space α] (x : ℕ → α) (H : dense_range x) : isometry (embedding_of_subset x) := sorry /-- Every separable metric space embeds isometrically in ℓ_infty_ℝ. -/ theorem exists_isometric_embedding (α : Type u) [metric_space α] [topological_space.separable_space α] : ∃ (f : α → ℓ_infty_ℝ), isometry f := sorry end Kuratowski_embedding /-- The Kuratowski embedding is an isometric embedding of a separable metric space in ℓ^∞(ℝ) -/ def Kuratowski_embedding (α : Type u) [metric_space α] [topological_space.separable_space α] : α → ℓ_infty_ℝ := classical.some sorry /-- The Kuratowski embedding is an isometry -/ protected theorem Kuratowski_embedding.isometry (α : Type u) [metric_space α] [topological_space.separable_space α] : isometry (Kuratowski_embedding α) := classical.some_spec (Kuratowski_embedding.exists_isometric_embedding α) /-- Version of the Kuratowski embedding for nonempty compacts -/ def nonempty_compacts.Kuratowski_embedding (α : Type u) [metric_space α] [compact_space α] [Nonempty α] : topological_space.nonempty_compacts ℓ_infty_ℝ := { val := set.range (Kuratowski_embedding α), property := sorry } end Mathlib
ea58733c593288f3837eec02e4c2faa9b1d7ed72
7cef822f3b952965621309e88eadf618da0c8ae9
/src/linear_algebra/matrix.lean
75e1d5bb07f8ad59c984045284969f3ec04e8dca
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
9,533
lean
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes Hölzl, Casper Putz The equivalence between matrices and linear maps. -/ import data.matrix.basic import linear_algebra.dimension linear_algebra.tensor_product /-! # Linear maps and matrices This file defines the maps to send matrices to a linear map, and to send linear maps between modules with a finite bases to matrices. This defines a linear equivalence between linear maps between finite-dimensional vector spaces and matrices indexed by the respective bases. Some results are proved about the linear map corresponding to a diagonal matrix (range, ker and rank). ## Main definitions to_lin, to_matrix, linear_equiv_matrix ## Tags linear_map, matrix, linear_equiv, diagonal -/ noncomputable theory open set submodule universes u v variables {l m n : Type u} [fintype l] [fintype m] [fintype n] namespace matrix variables {R : Type v} [comm_ring R] instance [decidable_eq m] [decidable_eq n] (R) [fintype R] : fintype (matrix m n R) := by unfold matrix; apply_instance /-- Evaluation of matrices gives a linear map from matrix m n R to linear maps (n → R) →ₗ[R] (m → R). -/ def eval : (matrix m n R) →ₗ[R] ((n → R) →ₗ[R] (m → R)) := begin refine linear_map.mk₂ R mul_vec _ _ _ _, { assume M N v, funext x, change finset.univ.sum (λy:n, (M x y + N x y) * v y) = _, simp only [_root_.add_mul, finset.sum_add_distrib], refl }, { assume c M v, funext x, change finset.univ.sum (λy:n, (c * M x y) * v y) = _, simp only [_root_.mul_assoc, finset.mul_sum.symm], refl }, { assume M v w, funext x, change finset.univ.sum (λy:n, M x y * (v y + w y)) = _, simp [_root_.mul_add, finset.sum_add_distrib], refl }, { assume c M v, funext x, change finset.univ.sum (λy:n, M x y * (c * v y)) = _, rw [show (λy:n, M x y * (c * v y)) = (λy:n, c * (M x y * v y)), { funext n, ac_refl }, ← finset.mul_sum], refl } end /-- Evaluation of matrices gives a map from matrix m n R to linear maps (n → R) →ₗ[R] (m → R). -/ def to_lin : matrix m n R → (n → R) →ₗ[R] (m → R) := eval.to_fun lemma to_lin_add (M N : matrix m n R) : (M + N).to_lin = M.to_lin + N.to_lin := matrix.eval.map_add M N @[simp] lemma to_lin_zero : (0 : matrix m n R).to_lin = 0 := matrix.eval.map_zero instance to_lin.is_linear_map : @is_linear_map R (matrix m n R) ((n → R) →ₗ[R] (m → R)) _ _ _ _ _ to_lin := matrix.eval.is_linear instance to_lin.is_add_monoid_hom : @is_add_monoid_hom (matrix m n R) ((n → R) →ₗ[R] (m → R)) _ _ to_lin := { map_zero := to_lin_zero, map_add := to_lin_add } @[simp] lemma to_lin_apply (M : matrix m n R) (v : n → R) : (M.to_lin : (n → R) → (m → R)) v = mul_vec M v := rfl lemma mul_to_lin [decidable_eq l] (M : matrix m n R) (N : matrix n l R) : (M.mul N).to_lin = M.to_lin.comp N.to_lin := begin ext v x, simp [to_lin_apply, mul_vec, matrix.mul, finset.sum_mul, finset.mul_sum], rw [finset.sum_comm], congr, funext x, congr, funext y, rw [mul_assoc] end end matrix namespace linear_map variables {R : Type v} [comm_ring R] /-- The linear map from linear maps (n → R) →ₗ[R] (m → R) to matrix m n R. -/ def to_matrixₗ [decidable_eq n] : ((n → R) →ₗ[R] (m → R)) →ₗ[R] matrix m n R := begin refine linear_map.mk (λ f i j, f (λ n, ite (j = n) 1 0) i) _ _, { assume f g, simp only [add_apply], refl }, { assume f g, simp only [smul_apply], refl } end /-- The map from linear maps (n → R) →ₗ[R] (m → R) to matrix m n R. -/ def to_matrix [decidable_eq n] : ((n → R) →ₗ[R] (m → R)) → matrix m n R := to_matrixₗ.to_fun end linear_map section linear_equiv_matrix variables {R : Type v} [comm_ring R] [decidable_eq n] open finsupp matrix linear_map /-- to_lin is the left inverse of to_matrix. -/ lemma to_matrix_to_lin {f : (n → R) →ₗ[R] (m → R)} : to_lin (to_matrix f) = f := begin ext : 1, -- Show that the two sides are equal by showing that they are equal on a basis convert linear_eq_on (set.range _) _ (is_basis.mem_span (@pi.is_basis_fun R n _ _) _), assume e he, rw [@std_basis_eq_single R _ _ _ 1] at he, cases (set.mem_range.mp he) with i h, ext j, change finset.univ.sum (λ k, (f.to_fun (λ l, ite (k = l) 1 0)) j * (e k)) = _, rw [←h], conv_lhs { congr, skip, funext, rw [mul_comm, ←smul_eq_mul, ←pi.smul_apply, ←linear_map.smul], rw [show _ = ite (i = k) (1:R) 0, by convert single_apply], rw [show f.to_fun (ite (i = k) (1:R) 0 • (λ l, ite (k = l) 1 0)) = ite (i = k) (f.to_fun _) 0, { split_ifs, { rw [one_smul] }, { rw [zero_smul], exact linear_map.map_zero f } }] }, convert finset.sum_eq_single i _ _, { rw [if_pos rfl], convert rfl, ext, congr }, { assume _ _ hbi, rw [if_neg $ ne.symm hbi], refl }, { assume hi, exact false.elim (hi $ finset.mem_univ i) } end /-- to_lin is the right inverse of to_matrix. -/ lemma to_lin_to_matrix {M : matrix m n R} : to_matrix (to_lin M) = M := begin ext, change finset.univ.sum (λ y, M i y * ite (j = y) 1 0) = M i j, have h1 : (λ y, M i y * ite (j = y) 1 0) = (λ y, ite (j = y) (M i y) 0), { ext, split_ifs, exact mul_one _, exact ring.mul_zero _ }, have h2 : finset.univ.sum (λ y, ite (j = y) (M i y) 0) = (finset.singleton j).sum (λ y, ite (j = y) (M i y) 0), { refine (finset.sum_subset _ _).symm, { intros _ H, rwa finset.mem_singleton.1 H, exact finset.mem_univ _ }, { exact λ _ _ H, if_neg (mt (finset.mem_singleton.2 ∘ eq.symm) H) } }, rw [h1, h2, finset.sum_singleton], exact if_pos rfl end /-- Linear maps (n → R) →ₗ[R] (m → R) are linearly equivalent to matrix m n R. -/ def linear_equiv_matrix' : ((n → R) →ₗ[R] (m → R)) ≃ₗ[R] matrix m n R := { to_fun := to_matrix, inv_fun := to_lin, right_inv := λ _, to_lin_to_matrix, left_inv := λ _, to_matrix_to_lin, add := to_matrixₗ.add, smul := to_matrixₗ.smul } /-- Given a basis of two modules M₁ and M₂ over a commutative ring R, we get a linear equivalence between linear maps M₁ →ₗ M₂ and matrices over R indexed by the bases. -/ def linear_equiv_matrix {ι κ M₁ M₂ : Type*} [add_comm_group M₁] [module R M₁] [add_comm_group M₂] [module R M₂] [fintype ι] [decidable_eq ι] [fintype κ] [decidable_eq κ] {v₁ : ι → M₁} {v₂ : κ → M₂} (hv₁ : is_basis R v₁) (hv₂ : is_basis R v₂) : (M₁ →ₗ[R] M₂) ≃ₗ[R] matrix κ ι R := linear_equiv.trans (linear_equiv.arrow_congr (equiv_fun_basis hv₁) (equiv_fun_basis hv₂)) linear_equiv_matrix' end linear_equiv_matrix namespace matrix section ring variables {R : Type v} [comm_ring R] open linear_map matrix lemma proj_diagonal [decidable_eq m] (i : m) (w : m → R) : (proj i).comp (to_lin (diagonal w)) = (w i) • proj i := by ext j; simp [mul_vec_diagonal] lemma diagonal_comp_std_basis [decidable_eq n] (w : n → R) (i : n) : (diagonal w).to_lin.comp (std_basis R (λ_:n, R) i) = (w i) • std_basis R (λ_:n, R) i := begin ext a j, simp only [linear_map.comp_apply, smul_apply, to_lin_apply, mul_vec_diagonal, smul_apply, pi.smul_apply, smul_eq_mul], by_cases i = j, { subst h }, { rw [std_basis_ne R (λ_:n, R) _ _ (ne.symm h), _root_.mul_zero, _root_.mul_zero] } end end ring section vector_space variables {K : Type u} [discrete_field K] -- maybe try to relax the universe constraint open linear_map matrix lemma rank_vec_mul_vec [decidable_eq n] (w : m → K) (v : n → K) : rank (vec_mul_vec w v).to_lin ≤ 1 := begin rw [vec_mul_vec_eq, mul_to_lin], refine le_trans (rank_comp_le1 _ _) _, refine le_trans (rank_le_domain _) _, rw [dim_fun', ← cardinal.fintype_card], exact le_refl _ end set_option class.instance_max_depth 100 lemma diagonal_to_lin [decidable_eq m] (w : m → K) : (diagonal w).to_lin = linear_map.pi (λi, w i • linear_map.proj i) := by ext v j; simp [mul_vec_diagonal] lemma ker_diagonal_to_lin [decidable_eq m] (w : m → K) : ker (diagonal w).to_lin = (⨆i∈{i | w i = 0 }, range (std_basis K (λi, K) i)) := begin rw [← comap_bot, ← infi_ker_proj], simp only [comap_infi, (ker_comp _ _).symm, proj_diagonal, ker_smul'], have : univ ⊆ {i : m | w i = 0} ∪ -{i : m | w i = 0}, { rw set.union_compl_self }, exact (supr_range_std_basis_eq_infi_ker_proj K (λi:m, K) (disjoint_compl {i | w i = 0}) this (finite.of_fintype _)).symm end lemma range_diagonal [decidable_eq m] (w : m → K) : (diagonal w).to_lin.range = (⨆ i ∈ {i | w i ≠ 0}, (std_basis K (λi, K) i).range) := begin dsimp only [mem_set_of_eq], rw [← map_top, ← supr_range_std_basis, map_supr], congr, funext i, rw [← linear_map.range_comp, diagonal_comp_std_basis, range_smul'], end lemma rank_diagonal [decidable_eq m] (w : m → K) : rank (diagonal w).to_lin = fintype.card { i // w i ≠ 0 } := begin have hu : univ ⊆ - {i : m | w i = 0} ∪ {i : m | w i = 0}, { rw set.compl_union_self }, have hd : disjoint {i : m | w i ≠ 0} {i : m | w i = 0} := (disjoint_compl {i | w i = 0}).symm, have h₁ := supr_range_std_basis_eq_infi_ker_proj K (λi:m, K) hd hu (finite.of_fintype _), have h₂ := @infi_ker_proj_equiv K _ _ (λi:m, K) _ _ _ _ (by simp; apply_instance) hd hu, rw [rank, range_diagonal, h₁, ←@dim_fun' K], apply linear_equiv.dim_eq, apply h₂, end end vector_space end matrix
3a806199d67a04cefb8442b87e58fe296f369caa
c777c32c8e484e195053731103c5e52af26a25d1
/src/measure_theory/integral/average.lean
e11fa52b50090220073307cc6acbaa723daa03c0
[ "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
8,924
lean
/- Copyright (c) 2022 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import measure_theory.integral.set_integral /-! # Integral average of a function In this file we define `measure_theory.average μ f` (notation: `⨍ x, f x ∂μ`) to be the average value of `f` with respect to measure `μ`. It is defined as `∫ x, f x ∂((μ univ)⁻¹ • μ)`, so it is equal to zero if `f` is not integrable or if `μ` is an infinite measure. If `μ` is a probability measure, then the average of any function is equal to its integral. For the average on a set, we use `⨍ x in s, f x ∂μ` (notation for `⨍ x, f x ∂(μ.restrict s)`). For average w.r.t. the volume, one can omit `∂volume`. ## Implementation notes The average is defined as an integral over `(μ univ)⁻¹ • μ` so that all theorems about Bochner integrals work for the average without modifications. For theorems that require integrability of a function, we provide a convenience lemma `measure_theory.integrable.to_average`. ## Tags integral, center mass, average value -/ open measure_theory measure_theory.measure metric set filter topological_space function open_locale topology big_operators ennreal convex variables {α E F : Type*} {m0 : measurable_space α} [normed_add_comm_group E] [normed_space ℝ E] [complete_space E] [normed_add_comm_group F] [normed_space ℝ F] [complete_space F] {μ : measure α} {s : set E} /-! ### Average value of a function w.r.t. a measure The average value of a function `f` w.r.t. a measure `μ` (notation: `⨍ x, f x ∂μ`) is defined as `(μ univ).to_real⁻¹ • ∫ x, f x ∂μ`, so it is equal to zero if `f` is not integrable or if `μ` is an infinite measure. If `μ` is a probability measure, then the average of any function is equal to its integral. -/ namespace measure_theory variable (μ) include m0 /-- Average value of a function `f` w.r.t. a measure `μ`, notation: `⨍ x, f x ∂μ`. It is defined as `(μ univ).to_real⁻¹ • ∫ x, f x ∂μ`, so it is equal to zero if `f` is not integrable or if `μ` is an infinite measure. If `μ` is a probability measure, then the average of any function is equal to its integral. For the average on a set, use `⨍ x in s, f x ∂μ` (defined as `⨍ x, f x ∂(μ.restrict s)`). For average w.r.t. the volume, one can omit `∂volume`. -/ noncomputable def average (f : α → E) := ∫ x, f x ∂((μ univ)⁻¹ • μ) notation `⨍` binders `, ` r:(scoped:60 f, f) ` ∂` μ:70 := average μ r notation `⨍` binders `, ` r:(scoped:60 f, average volume f) := r notation `⨍` binders ` in ` s `, ` r:(scoped:60 f, f) ` ∂` μ:70 := average (measure.restrict μ s) r notation `⨍` binders ` in ` s `, ` r:(scoped:60 f, average (measure.restrict volume s) f) := r @[simp] lemma average_zero : ⨍ x, (0 : E) ∂μ = 0 := by rw [average, integral_zero] @[simp] lemma average_zero_measure (f : α → E) : ⨍ x, f x ∂(0 : measure α) = 0 := by rw [average, smul_zero, integral_zero_measure] @[simp] lemma average_neg (f : α → E) : ⨍ x, -f x ∂μ = -⨍ x, f x ∂μ := integral_neg f lemma average_eq' (f : α → E) : ⨍ x, f x ∂μ = ∫ x, f x ∂((μ univ)⁻¹ • μ) := rfl lemma average_eq (f : α → E) : ⨍ x, f x ∂μ = (μ univ).to_real⁻¹ • ∫ x, f x ∂μ := by rw [average_eq', integral_smul_measure, ennreal.to_real_inv] lemma average_eq_integral [is_probability_measure μ] (f : α → E) : ⨍ x, f x ∂μ = ∫ x, f x ∂μ := by rw [average, measure_univ, inv_one, one_smul] @[simp] lemma measure_smul_average [is_finite_measure μ] (f : α → E) : (μ univ).to_real • ⨍ x, f x ∂μ = ∫ x, f x ∂μ := begin cases eq_or_ne μ 0 with hμ hμ, { rw [hμ, integral_zero_measure, average_zero_measure, smul_zero] }, { rw [average_eq, smul_inv_smul₀], refine (ennreal.to_real_pos _ $ measure_ne_top _ _).ne', rwa [ne.def, measure_univ_eq_zero] } end lemma set_average_eq (f : α → E) (s : set α) : ⨍ x in s, f x ∂μ = (μ s).to_real⁻¹ • ∫ x in s, f x ∂μ := by rw [average_eq, restrict_apply_univ] lemma set_average_eq' (f : α → E) (s : set α) : ⨍ x in s, f x ∂μ = ∫ x, f x ∂((μ s)⁻¹ • μ.restrict s) := by simp only [average_eq', restrict_apply_univ] variable {μ} lemma average_congr {f g : α → E} (h : f =ᵐ[μ] g) : ⨍ x, f x ∂μ = ⨍ x, g x ∂μ := by simp only [average_eq, integral_congr_ae h] lemma average_add_measure [is_finite_measure μ] {ν : measure α} [is_finite_measure ν] {f : α → E} (hμ : integrable f μ) (hν : integrable f ν) : ⨍ x, f x ∂(μ + ν) = ((μ univ).to_real / ((μ univ).to_real + (ν univ).to_real)) • ⨍ x, f x ∂μ + ((ν univ).to_real / ((μ univ).to_real + (ν univ).to_real)) • ⨍ x, f x ∂ν := begin simp only [div_eq_inv_mul, mul_smul, measure_smul_average, ← smul_add, ← integral_add_measure hμ hν, ← ennreal.to_real_add (measure_ne_top μ _) (measure_ne_top ν _)], rw [average_eq, measure.add_apply] end lemma average_pair {f : α → E} {g : α → F} (hfi : integrable f μ) (hgi : integrable g μ) : ⨍ x, (f x, g x) ∂μ = (⨍ x, f x ∂μ, ⨍ x, g x ∂μ) := integral_pair hfi.to_average hgi.to_average lemma measure_smul_set_average (f : α → E) {s : set α} (h : μ s ≠ ∞) : (μ s).to_real • ⨍ x in s, f x ∂μ = ∫ x in s, f x ∂μ := by { haveI := fact.mk h.lt_top, rw [← measure_smul_average, restrict_apply_univ] } lemma average_union {f : α → E} {s t : set α} (hd : ae_disjoint μ s t) (ht : null_measurable_set t μ) (hsμ : μ s ≠ ∞) (htμ : μ t ≠ ∞) (hfs : integrable_on f s μ) (hft : integrable_on f t μ) : ⨍ x in s ∪ t, f x ∂μ = ((μ s).to_real / ((μ s).to_real + (μ t).to_real)) • ⨍ x in s, f x ∂μ + ((μ t).to_real / ((μ s).to_real + (μ t).to_real)) • ⨍ x in t, f x ∂μ := begin haveI := fact.mk hsμ.lt_top, haveI := fact.mk htμ.lt_top, rw [restrict_union₀ hd ht, average_add_measure hfs hft, restrict_apply_univ, restrict_apply_univ] end lemma average_union_mem_open_segment {f : α → E} {s t : set α} (hd : ae_disjoint μ s t) (ht : null_measurable_set t μ) (hs₀ : μ s ≠ 0) (ht₀ : μ t ≠ 0) (hsμ : μ s ≠ ∞) (htμ : μ t ≠ ∞) (hfs : integrable_on f s μ) (hft : integrable_on f t μ) : ⨍ x in s ∪ t, f x ∂μ ∈ open_segment ℝ (⨍ x in s, f x ∂μ) (⨍ x in t, f x ∂μ) := begin replace hs₀ : 0 < (μ s).to_real, from ennreal.to_real_pos hs₀ hsμ, replace ht₀ : 0 < (μ t).to_real, from ennreal.to_real_pos ht₀ htμ, refine mem_open_segment_iff_div.mpr ⟨(μ s).to_real, (μ t).to_real, hs₀, ht₀, (average_union hd ht hsμ htμ hfs hft).symm⟩ end lemma average_union_mem_segment {f : α → E} {s t : set α} (hd : ae_disjoint μ s t) (ht : null_measurable_set t μ) (hsμ : μ s ≠ ∞) (htμ : μ t ≠ ∞) (hfs : integrable_on f s μ) (hft : integrable_on f t μ) : ⨍ x in s ∪ t, f x ∂μ ∈ [⨍ x in s, f x ∂μ -[ℝ] ⨍ x in t, f x ∂μ] := begin by_cases hse : μ s = 0, { rw ← ae_eq_empty at hse, rw [restrict_congr_set (hse.union eventually_eq.rfl), empty_union], exact right_mem_segment _ _ _ }, { refine mem_segment_iff_div.mpr ⟨(μ s).to_real, (μ t).to_real, ennreal.to_real_nonneg, ennreal.to_real_nonneg, _, (average_union hd ht hsμ htμ hfs hft).symm⟩, calc 0 < (μ s).to_real : ennreal.to_real_pos hse hsμ ... ≤ _ : le_add_of_nonneg_right ennreal.to_real_nonneg } end lemma average_mem_open_segment_compl_self [is_finite_measure μ] {f : α → E} {s : set α} (hs : null_measurable_set s μ) (hs₀ : μ s ≠ 0) (hsc₀ : μ sᶜ ≠ 0) (hfi : integrable f μ) : ⨍ x, f x ∂μ ∈ open_segment ℝ (⨍ x in s, f x ∂μ) (⨍ x in sᶜ, f x ∂μ) := by simpa only [union_compl_self, restrict_univ] using average_union_mem_open_segment ae_disjoint_compl_right hs.compl hs₀ hsc₀ (measure_ne_top _ _) (measure_ne_top _ _) hfi.integrable_on hfi.integrable_on @[simp] lemma average_const [is_finite_measure μ] [h : μ.ae.ne_bot] (c : E) : ⨍ x, c ∂μ = c := by simp only [average_eq, integral_const, measure.restrict_apply, measurable_set.univ, one_smul, univ_inter, smul_smul, ← ennreal.to_real_inv, ← ennreal.to_real_mul, ennreal.inv_mul_cancel, measure_ne_top μ univ, ne.def, measure_univ_eq_zero, ae_ne_bot.1 h, not_false_iff, ennreal.one_to_real] lemma set_average_const {s : set α} (hs₀ : μ s ≠ 0) (hs : μ s ≠ ∞) (c : E) : ⨍ x in s, c ∂μ = c := by simp only [set_average_eq, integral_const, measure.restrict_apply, measurable_set.univ, univ_inter, smul_smul, ← ennreal.to_real_inv, ← ennreal.to_real_mul, ennreal.inv_mul_cancel hs₀ hs, ennreal.one_to_real, one_smul] end measure_theory
dcd3193ee39455c98172eadd881eead74d5b39cd
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/measure_theory/integral/set_integral.lean
57b6119822506c7d1970f158eb15ee8f3ffcd1e3
[ "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
43,709
lean
/- Copyright (c) 2020 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Yury Kudryashov -/ import measure_theory.integral.integrable_on import measure_theory.integral.bochner import order.filter.indicator_function /-! # Set integral In this file we prove some properties of `∫ x in s, f x ∂μ`. Recall that this notation is defined as `∫ x, f x ∂(μ.restrict s)`. In `integral_indicator` we prove that for a measurable function `f` and a measurable set `s` this definition coincides with another natural definition: `∫ x, indicator s f x ∂μ = ∫ x in s, f x ∂μ`, where `indicator s f x` is equal to `f x` for `x ∈ s` and is zero otherwise. Since `∫ x in s, f x ∂μ` is a notation, one can rewrite or apply any theorem about `∫ x, f x ∂μ` directly. In this file we prove some theorems about dependence of `∫ x in s, f x ∂μ` on `s`, e.g. `integral_union`, `integral_empty`, `integral_univ`. We use the property `integrable_on f s μ := integrable f (μ.restrict s)`, defined in `measure_theory.integrable_on`. We also defined in that same file a predicate `integrable_at_filter (f : α → E) (l : filter α) (μ : measure α)` saying that `f` is integrable at some set `s ∈ l`. Finally, we prove a version of the [Fundamental theorem of calculus](https://en.wikipedia.org/wiki/Fundamental_theorem_of_calculus) for set integral, see `filter.tendsto.integral_sub_linear_is_o_ae` and its corollaries. Namely, consider a measurably generated filter `l`, a measure `μ` finite at this filter, and a function `f` that has a finite limit `c` at `l ⊓ μ.ae`. Then `∫ x in s, f x ∂μ = μ s • c + o(μ s)` as `s` tends to `l.lift' powerset`, i.e. for any `ε>0` there exists `t ∈ l` such that `∥∫ x in s, f x ∂μ - μ s • c∥ ≤ ε * μ s` whenever `s ⊆ t`. We also formulate a version of this theorem for a locally finite measure `μ` and a function `f` continuous at a point `a`. ## Notation We provide the following notations for expressing the integral of a function on a set : * `∫ a in s, f a ∂μ` is `measure_theory.integral (μ.restrict s) f` * `∫ a in s, f a` is `∫ a in s, f a ∂volume` Note that the set notations are defined in the file `measure_theory/bochner_integration`, but we reference them here because all theorems about set integrals are in this file. ## TODO The file ends with over a hundred lines of commented out code. This is the old contents of this file using the `indicator` approach to the definition of `∫ x in s, f x ∂μ`. This code should be migrated to the new definition. -/ noncomputable theory open set filter topological_space measure_theory function open_locale classical topological_space interval big_operators filter ennreal measure_theory variables {α β E F : Type*} [measurable_space α] namespace measure_theory section normed_group variables [normed_group E] [measurable_space E] {f g : α → E} {s t : set α} {μ ν : measure α} {l l' : filter α} [borel_space E] [second_countable_topology E] variables [complete_space E] [normed_space ℝ E] lemma set_integral_congr_ae (hs : measurable_set s) (h : ∀ᵐ x ∂μ, x ∈ s → f x = g x) : ∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ := integral_congr_ae ((ae_restrict_iff' hs).2 h) lemma set_integral_congr (hs : measurable_set s) (h : eq_on f g s) : ∫ x in s, f x ∂μ = ∫ x in s, g x ∂μ := set_integral_congr_ae hs $ eventually_of_forall h lemma integral_union (hst : disjoint s t) (hs : measurable_set s) (ht : measurable_set t) (hfs : integrable_on f s μ) (hft : integrable_on f t μ) : ∫ x in s ∪ t, f x ∂μ = ∫ x in s, f x ∂μ + ∫ x in t, f x ∂μ := by simp only [integrable_on, measure.restrict_union hst hs ht, integral_add_measure hfs hft] lemma integral_finset_bUnion {ι : Type*} {t : finset ι} {s : ι → set α} (hs : ∀ i ∈ t, measurable_set (s i)) (h's : pairwise_on ↑t (disjoint on s)) (hf : integrable f μ) : ∫ x in (⋃ i ∈ t, s i), f x ∂ μ = ∑ i in t, ∫ x in s i, f x ∂ μ := begin induction t using finset.induction_on with a t hat IH hs h's, { simp }, { have : (⋃ i ∈ insert a t, s i) = s a ∪ (⋃ i ∈ t, s i), by simp, rw [this, integral_union _ _ _ hf.integrable_on hf.integrable_on], { simp only [hat, finset.sum_insert, not_false_iff, add_right_inj], exact IH (λ i hi, hs i (finset.mem_insert_of_mem hi)) (h's.mono (finset.subset_insert _ _)) }, { simp only [disjoint_Union_right], exact λ i hi, h's _ (finset.mem_insert_self _ _) _ (finset.mem_insert_of_mem hi) (ne_of_mem_of_not_mem hi hat).symm }, { exact hs _ (finset.mem_insert_self _ _) }, { exact finset.measurable_set_bUnion _ (λ i hi, hs i (finset.mem_insert_of_mem hi)) }, } end lemma integral_fintype_Union {ι : Type*} [fintype ι] {s : ι → set α} (hs : ∀ i, measurable_set (s i)) (h's : pairwise (disjoint on s)) (hf : integrable f μ) : ∫ x in (⋃ i, s i), f x ∂ μ = ∑ i, ∫ x in s i, f x ∂ μ := begin convert integral_finset_bUnion (λ i hi, hs i) _ hf, { simp }, { simp [pairwise_on_univ, h's] } end lemma integral_empty : ∫ x in ∅, f x ∂μ = 0 := by rw [measure.restrict_empty, integral_zero_measure] lemma integral_univ : ∫ x in univ, f x ∂μ = ∫ x, f x ∂μ := by rw [measure.restrict_univ] lemma integral_add_compl (hs : measurable_set s) (hfi : integrable f μ) : ∫ x in s, f x ∂μ + ∫ x in sᶜ, f x ∂μ = ∫ x, f x ∂μ := by rw [← integral_union (@disjoint_compl_right (set α) _ _) hs hs.compl hfi.integrable_on hfi.integrable_on, union_compl_self, integral_univ] /-- For a function `f` and a measurable set `s`, the integral of `indicator s f` over the whole space is equal to `∫ x in s, f x ∂μ` defined as `∫ x, f x ∂(μ.restrict s)`. -/ lemma integral_indicator (hs : measurable_set s) : ∫ x, indicator s f x ∂μ = ∫ x in s, f x ∂μ := begin by_cases hf : ae_measurable f (μ.restrict s), swap, { rw integral_non_ae_measurable hf, rw [← ae_measurable_indicator_iff hs] at hf, exact integral_non_ae_measurable hf }, by_cases hfi : integrable_on f s μ, swap, { rwa [integral_undef, integral_undef], rwa integrable_indicator_iff hs }, calc ∫ x, indicator s f x ∂μ = ∫ x in s, indicator s f x ∂μ + ∫ x in sᶜ, indicator s f x ∂μ : (integral_add_compl hs (hfi.indicator hs)).symm ... = ∫ x in s, f x ∂μ + ∫ x in sᶜ, 0 ∂μ : congr_arg2 (+) (integral_congr_ae (indicator_ae_eq_restrict hs)) (integral_congr_ae (indicator_ae_eq_restrict_compl hs)) ... = ∫ x in s, f x ∂μ : by simp end lemma has_sum_integral_Union {ι : Type*} [encodable ι] {s : ι → set α} {f : α → E} (hm : ∀ i, measurable_set (s i)) (hd : pairwise (disjoint on s)) (hfi : integrable f μ ) : has_sum (λ n, ∫ a in s n, f a ∂ μ) (∫ a in ⋃ n, s n, f a ∂μ) := begin have : (λ n : finset ι, ∑ i in n, ∫ a in s i, f a ∂μ) = λ (n : finset ι), ∫ a, set.indicator (⋃ i ∈ n, s i) f a ∂μ, { funext, rw [← integral_finset_bUnion (λ i hi, hm i) (hd.pairwise_on _) hfi, integral_indicator], exact finset.measurable_set_bUnion _ (λ i hi, hm i) }, rw [has_sum, this, ← integral_indicator (measurable_set.Union hm)], refine tendsto_integral_filter_of_dominated_convergence (λ x, ∥f x∥) is_countably_generated_at_top _ _ _ _ _, { apply eventually_of_forall (λ n, _), exact hfi.ae_measurable.indicator (finset.measurable_set_bUnion _ (λ i hi, hm i)) }, { exact hfi.ae_measurable.indicator (measurable_set.Union hm) }, { refine eventually_of_forall (λ n, eventually_of_forall (λ x, _)), exact norm_indicator_le_norm_self _ _ }, { exact hfi.norm }, { filter_upwards [] λa, le_trans (tendsto_indicator_bUnion_finset _ _ _) (pure_le_nhds _) }, end lemma integral_Union {ι : Type*} [encodable ι] {s : ι → set α} {f : α → E} (hm : ∀ i, measurable_set (s i)) (hd : pairwise (disjoint on s)) (hfi : integrable f μ ) : (∫ a in (⋃ n, s n), f a ∂μ) = ∑' n, ∫ a in s n, f a ∂ μ := (has_sum.tsum_eq (has_sum_integral_Union hm hd hfi)).symm lemma set_integral_eq_zero_of_forall_eq_zero {f : α → E} (hf : measurable f) (ht_eq : ∀ x ∈ t, f x = 0) : ∫ x in t, f x ∂μ = 0 := begin refine integral_eq_zero_of_ae _, rw [eventually_eq, ae_restrict_iff (measurable_set_eq_fun hf measurable_zero)], refine eventually_of_forall (λ x hx, _), rw pi.zero_apply, exact ht_eq x hx, end private lemma set_integral_union_eq_left_of_disjoint {f : α → E} (hf : measurable f) (hfi : integrable f μ) (hs : measurable_set s) (ht : measurable_set t) (ht_eq : ∀ x ∈ t, f x = 0) (hs_disj : disjoint s t) : ∫ x in (s ∪ t), f x ∂μ = ∫ x in s, f x ∂μ := by rw [integral_union hs_disj hs ht hfi.integrable_on hfi.integrable_on, set_integral_eq_zero_of_forall_eq_zero hf ht_eq, add_zero] lemma set_integral_union_eq_left {f : α → E} (hf : measurable f) (hfi : integrable f μ) (hs : measurable_set s) (ht : measurable_set t) (ht_eq : ∀ x ∈ t, f x = 0) : ∫ x in (s ∪ t), f x ∂μ = ∫ x in s, f x ∂μ := begin let s_ := s \ {x | f x = 0}, have hs_ : measurable_set s_, from hs.diff (measurable_set_eq_fun hf measurable_const), let s0 := s ∩ {x | f x = 0}, have hs0 : measurable_set s0, from hs.inter (measurable_set_eq_fun hf measurable_const), have hs0_eq : ∀ x ∈ s0, f x = 0, by { intros x hx, simp_rw [s0, set.mem_inter_iff] at hx, exact hx.2, }, have h_s_union : s = s_ ∪ s0, from (set.diff_union_inter s _).symm, have h_s_disj : disjoint s_ s0, from (@disjoint_sdiff_self_left (set α) {x | f x = 0} s _).mono_right (set.inter_subset_right _ _), rw [h_s_union, set_integral_union_eq_left_of_disjoint hf hfi hs_ hs0 hs0_eq h_s_disj], have hst0_eq : ∀ x ∈ s0 ∪ t, f x = 0, { intros x hx, rw set.mem_union at hx, cases hx, { exact hs0_eq x hx, }, { exact ht_eq x hx, }, }, have hst_disj : disjoint s_ (s0 ∪ t), { rw [← set.sup_eq_union, disjoint_sup_right], exact ⟨h_s_disj, (@disjoint_sdiff_self_left (set α) {x | f x = 0} s _).mono_right ht_eq⟩, }, rw set.union_assoc, exact set_integral_union_eq_left_of_disjoint hf hfi hs_ (hs0.union ht) hst0_eq hst_disj, end lemma set_integral_neg_eq_set_integral_nonpos [linear_order E] [order_closed_topology E] {f : α → E} (hf : measurable f) (hfi : integrable f μ) : ∫ x in {x | f x < 0}, f x ∂μ = ∫ x in {x | f x ≤ 0}, f x ∂μ := begin have h_union : {x | f x ≤ 0} = {x | f x < 0} ∪ {x | f x = 0}, by { ext, simp_rw [set.mem_union_eq, set.mem_set_of_eq], exact le_iff_lt_or_eq, }, rw h_union, exact (set_integral_union_eq_left hf hfi (measurable_set_lt hf measurable_const) (measurable_set_eq_fun hf measurable_const) (λ x hx, hx)).symm, end lemma integral_norm_eq_pos_sub_neg {f : α → ℝ} (hf : measurable f) (hfi : integrable f μ) : ∫ x, ∥f x∥ ∂μ = ∫ x in {x | 0 ≤ f x}, f x ∂μ - ∫ x in {x | f x ≤ 0}, f x ∂μ := have h_meas : measurable_set {x | 0 ≤ f x}, from measurable_set_le measurable_const hf, calc ∫ x, ∥f x∥ ∂μ = ∫ x in {x | 0 ≤ f x}, ∥f x∥ ∂μ + ∫ x in {x | 0 ≤ f x}ᶜ, ∥f x∥ ∂μ : by rw ← integral_add_compl h_meas hfi.norm ... = ∫ x in {x | 0 ≤ f x}, f x ∂μ + ∫ x in {x | 0 ≤ f x}ᶜ, ∥f x∥ ∂μ : begin congr' 1, refine set_integral_congr h_meas (λ x hx, _), dsimp only, rw [real.norm_eq_abs, abs_eq_self.mpr _], exact hx, end ... = ∫ x in {x | 0 ≤ f x}, f x ∂μ - ∫ x in {x | 0 ≤ f x}ᶜ, f x ∂μ : begin congr' 1, rw ← integral_neg, refine set_integral_congr h_meas.compl (λ x hx, _), dsimp only, rw [real.norm_eq_abs, abs_eq_neg_self.mpr _], rw [set.mem_compl_iff, set.nmem_set_of_eq] at hx, linarith, end ... = ∫ x in {x | 0 ≤ f x}, f x ∂μ - ∫ x in {x | f x ≤ 0}, f x ∂μ : by { rw ← set_integral_neg_eq_set_integral_nonpos hf hfi, congr, ext1 x, simp, } lemma set_integral_congr_set_ae (hst : s =ᵐ[μ] t) : ∫ x in s, f x ∂μ = ∫ x in t, f x ∂μ := by rw restrict_congr_set hst lemma set_integral_const (c : E) : ∫ x in s, c ∂μ = (μ s).to_real • c := by rw [integral_const, measure.restrict_apply_univ] @[simp] lemma integral_indicator_const (e : E) ⦃s : set α⦄ (s_meas : measurable_set s) : ∫ (a : α), s.indicator (λ (x : α), e) a ∂μ = (μ s).to_real • e := by rw [integral_indicator s_meas, ← set_integral_const] lemma set_integral_map {β} [measurable_space β] {g : α → β} {f : β → E} {s : set β} (hs : measurable_set s) (hf : ae_measurable f (measure.map g μ)) (hg : measurable g) : ∫ y in s, f y ∂(measure.map g μ) = ∫ x in g ⁻¹' s, f (g x) ∂μ := begin rw [measure.restrict_map hg hs, integral_map hg (hf.mono_measure _)], exact measure.map_mono g measure.restrict_le_self end lemma set_integral_map_of_closed_embedding [topological_space α] [borel_space α] {β} [measurable_space β] [topological_space β] [borel_space β] {g : α → β} {f : β → E} {s : set β} (hs : measurable_set s) (hg : closed_embedding g) : ∫ y in s, f y ∂(measure.map g μ) = ∫ x in g ⁻¹' s, f (g x) ∂μ := begin rw [measure.restrict_map hg.measurable hs, integral_map_of_closed_embedding hg], apply_instance, end lemma norm_set_integral_le_of_norm_le_const_ae {C : ℝ} (hs : μ s < ∞) (hC : ∀ᵐ x ∂μ.restrict s, ∥f x∥ ≤ C) : ∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real := begin rw ← measure.restrict_apply_univ at *, haveI : is_finite_measure (μ.restrict s) := ⟨‹_›⟩, exact norm_integral_le_of_norm_le_const hC end lemma norm_set_integral_le_of_norm_le_const_ae' {C : ℝ} (hs : μ s < ∞) (hC : ∀ᵐ x ∂μ, x ∈ s → ∥f x∥ ≤ C) (hfm : ae_measurable f (μ.restrict s)) : ∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real := begin apply norm_set_integral_le_of_norm_le_const_ae hs, have A : ∀ᵐ (x : α) ∂μ, x ∈ s → ∥ae_measurable.mk f hfm x∥ ≤ C, { filter_upwards [hC, hfm.ae_mem_imp_eq_mk], assume a h1 h2 h3, rw [← h2 h3], exact h1 h3 }, have B : measurable_set {x | ∥(hfm.mk f) x∥ ≤ C} := hfm.measurable_mk.norm measurable_set_Iic, filter_upwards [hfm.ae_eq_mk, (ae_restrict_iff B).2 A], assume a h1 h2, rwa h1 end lemma norm_set_integral_le_of_norm_le_const_ae'' {C : ℝ} (hs : μ s < ∞) (hsm : measurable_set s) (hC : ∀ᵐ x ∂μ, x ∈ s → ∥f x∥ ≤ C) : ∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real := norm_set_integral_le_of_norm_le_const_ae hs $ by rwa [ae_restrict_eq hsm, eventually_inf_principal] lemma norm_set_integral_le_of_norm_le_const {C : ℝ} (hs : μ s < ∞) (hC : ∀ x ∈ s, ∥f x∥ ≤ C) (hfm : ae_measurable f (μ.restrict s)) : ∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real := norm_set_integral_le_of_norm_le_const_ae' hs (eventually_of_forall hC) hfm lemma norm_set_integral_le_of_norm_le_const' {C : ℝ} (hs : μ s < ∞) (hsm : measurable_set s) (hC : ∀ x ∈ s, ∥f x∥ ≤ C) : ∥∫ x in s, f x ∂μ∥ ≤ C * (μ s).to_real := norm_set_integral_le_of_norm_le_const_ae'' hs hsm $ eventually_of_forall hC lemma set_integral_eq_zero_iff_of_nonneg_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ.restrict s] f) (hfi : integrable_on f s μ) : ∫ x in s, f x ∂μ = 0 ↔ f =ᵐ[μ.restrict s] 0 := integral_eq_zero_iff_of_nonneg_ae hf hfi lemma set_integral_pos_iff_support_of_nonneg_ae {f : α → ℝ} (hf : 0 ≤ᵐ[μ.restrict s] f) (hfi : integrable_on f s μ) : 0 < ∫ x in s, f x ∂μ ↔ 0 < μ (support f ∩ s) := begin rw [integral_pos_iff_support_of_nonneg_ae hf hfi, restrict_apply_of_null_measurable_set], exact hfi.ae_measurable.null_measurable_set (measurable_set_singleton 0).compl end lemma set_integral_trim {α} {m m0 : measurable_space α} {μ : measure α} (hm : m ≤ m0) {f : α → E} (hf_meas : @measurable _ _ m _ f) {s : set α} (hs : measurable_set[m] s) : ∫ x in s, f x ∂μ = ∫ x in s, f x ∂(μ.trim hm) := by rwa [integral_trim hm hf_meas, restrict_trim hm μ] end normed_group section mono variables {μ : measure α} {f g : α → ℝ} {s : set α} (hf : integrable_on f s μ) (hg : integrable_on g s μ) lemma set_integral_mono_ae_restrict (h : f ≤ᵐ[μ.restrict s] g) : ∫ a in s, f a ∂μ ≤ ∫ a in s, g a ∂μ := integral_mono_ae hf hg h lemma set_integral_mono_ae (h : f ≤ᵐ[μ] g) : ∫ a in s, f a ∂μ ≤ ∫ a in s, g a ∂μ := set_integral_mono_ae_restrict hf hg (ae_restrict_of_ae h) lemma set_integral_mono_on (hs : measurable_set s) (h : ∀ x ∈ s, f x ≤ g x) : ∫ a in s, f a ∂μ ≤ ∫ a in s, g a ∂μ := set_integral_mono_ae_restrict hf hg (by simp [hs, eventually_le, eventually_inf_principal, ae_of_all _ h]) lemma set_integral_mono (h : f ≤ g) : ∫ a in s, f a ∂μ ≤ ∫ a in s, g a ∂μ := integral_mono hf hg h end mono section nonneg variables {μ : measure α} {f : α → ℝ} {s : set α} lemma set_integral_nonneg_of_ae_restrict (hf : 0 ≤ᵐ[μ.restrict s] f) : (0:ℝ) ≤ (∫ a in s, f a ∂μ) := integral_nonneg_of_ae hf lemma set_integral_nonneg_of_ae (hf : 0 ≤ᵐ[μ] f) : (0:ℝ) ≤ (∫ a in s, f a ∂μ) := set_integral_nonneg_of_ae_restrict (ae_restrict_of_ae hf) lemma set_integral_nonneg (hs : measurable_set s) (hf : ∀ a, a ∈ s → 0 ≤ f a) : (0:ℝ) ≤ (∫ a in s, f a ∂μ) := set_integral_nonneg_of_ae_restrict ((ae_restrict_iff' hs).mpr (ae_of_all μ hf)) lemma set_integral_le_nonneg {s : set α} (hs : measurable_set s) (hf : measurable f) (hfi : integrable f μ) : ∫ x in s, f x ∂μ ≤ ∫ x in {y | 0 ≤ f y}, f x ∂μ := begin rw [← integral_indicator hs, ← integral_indicator (measurable_set_le measurable_const hf)], exact integral_mono (hfi.indicator hs) (hfi.indicator (measurable_set_le measurable_const hf)) (indicator_le_indicator_nonneg s f), end lemma set_integral_nonpos_le {s : set α} (hs : measurable_set s) {f : α → ℝ} (hf : measurable f) (hfi : integrable f μ) : ∫ x in {y | f y ≤ 0}, f x ∂μ ≤ ∫ x in s, f x ∂μ := begin rw [← integral_indicator hs, ← integral_indicator (measurable_set_le hf measurable_const)], exact integral_mono (hfi.indicator (measurable_set_le hf measurable_const)) (hfi.indicator hs) (indicator_nonpos_le_indicator s f), end end nonneg lemma set_integral_mono_set {α : Type*} [measurable_space α] {μ : measure α} {s t : set α} {f : α → ℝ} (hfi : integrable f μ) (hf : 0 ≤ᵐ[μ] f) (hst : s ≤ᵐ[μ] t) : ∫ x in s, f x ∂μ ≤ ∫ x in t, f x ∂μ := begin repeat { rw integral_eq_lintegral_of_nonneg_ae (ae_restrict_of_ae hf) (hfi.1.mono_measure measure.restrict_le_self) }, rw ennreal.to_real_le_to_real (ne_of_lt $ (has_finite_integral_iff_of_real (ae_restrict_of_ae hf)).mp hfi.integrable_on.2) (ne_of_lt $ (has_finite_integral_iff_of_real (ae_restrict_of_ae hf)).mp hfi.integrable_on.2), exact (lintegral_mono_set' hst), end section continuous_set_integral /-! ### Continuity of the set integral We prove that for any set `s`, the function `λ f : α →₁[μ] E, ∫ x in s, f x ∂μ` is continuous. -/ variables [normed_group E] [measurable_space E] [second_countable_topology E] [borel_space E] {𝕜 : Type*} [is_R_or_C 𝕜] [measurable_space 𝕜] [normed_group F] [measurable_space F] [second_countable_topology F] [borel_space F] [normed_space 𝕜 F] {p : ℝ≥0∞} {μ : measure α} /-- For `f : Lp E p μ`, we can define an element of `Lp E p (μ.restrict s)` by `(Lp.mem_ℒp f).restrict s).to_Lp f`. This map is additive. -/ lemma Lp_to_Lp_restrict_add (f g : Lp E p μ) (s : set α) : ((Lp.mem_ℒp (f + g)).restrict s).to_Lp ⇑(f + g) = ((Lp.mem_ℒp f).restrict s).to_Lp f + ((Lp.mem_ℒp g).restrict s).to_Lp g := begin ext1, refine (ae_restrict_of_ae (Lp.coe_fn_add f g)).mp _, refine (Lp.coe_fn_add (mem_ℒp.to_Lp f ((Lp.mem_ℒp f).restrict s)) (mem_ℒp.to_Lp g ((Lp.mem_ℒp g).restrict s))).mp _, refine (mem_ℒp.coe_fn_to_Lp ((Lp.mem_ℒp f).restrict s)).mp _, refine (mem_ℒp.coe_fn_to_Lp ((Lp.mem_ℒp g).restrict s)).mp _, refine (mem_ℒp.coe_fn_to_Lp ((Lp.mem_ℒp (f+g)).restrict s)).mono (λ x hx1 hx2 hx3 hx4 hx5, _), rw [hx4, hx1, pi.add_apply, hx2, hx3, hx5, pi.add_apply], end /-- For `f : Lp E p μ`, we can define an element of `Lp E p (μ.restrict s)` by `(Lp.mem_ℒp f).restrict s).to_Lp f`. This map commutes with scalar multiplication. -/ lemma Lp_to_Lp_restrict_smul [opens_measurable_space 𝕜] (c : 𝕜) (f : Lp F p μ) (s : set α) : ((Lp.mem_ℒp (c • f)).restrict s).to_Lp ⇑(c • f) = c • (((Lp.mem_ℒp f).restrict s).to_Lp f) := begin ext1, refine (ae_restrict_of_ae (Lp.coe_fn_smul c f)).mp _, refine (mem_ℒp.coe_fn_to_Lp ((Lp.mem_ℒp f).restrict s)).mp _, refine (mem_ℒp.coe_fn_to_Lp ((Lp.mem_ℒp (c • f)).restrict s)).mp _, refine (Lp.coe_fn_smul c (mem_ℒp.to_Lp f ((Lp.mem_ℒp f).restrict s))).mono (λ x hx1 hx2 hx3 hx4, _), rw [hx2, hx1, pi.smul_apply, hx3, hx4, pi.smul_apply], end /-- For `f : Lp E p μ`, we can define an element of `Lp E p (μ.restrict s)` by `(Lp.mem_ℒp f).restrict s).to_Lp f`. This map is non-expansive. -/ lemma norm_Lp_to_Lp_restrict_le (s : set α) (f : Lp E p μ) : ∥((Lp.mem_ℒp f).restrict s).to_Lp f∥ ≤ ∥f∥ := begin rw [Lp.norm_def, Lp.norm_def, ennreal.to_real_le_to_real (Lp.snorm_ne_top _) (Lp.snorm_ne_top _)], refine (le_of_eq _).trans (snorm_mono_measure _ measure.restrict_le_self), { exact s, }, exact snorm_congr_ae (mem_ℒp.coe_fn_to_Lp _), end variables (α F 𝕜) /-- Continuous linear map sending a function of `Lp F p μ` to the same function in `Lp F p (μ.restrict s)`. -/ def Lp_to_Lp_restrict_clm [borel_space 𝕜] (μ : measure α) (p : ℝ≥0∞) [hp : fact (1 ≤ p)] (s : set α) : Lp F p μ →L[𝕜] Lp F p (μ.restrict s) := @linear_map.mk_continuous 𝕜 (Lp F p μ) (Lp F p (μ.restrict s)) _ _ _ _ _ ⟨λ f, mem_ℒp.to_Lp f ((Lp.mem_ℒp f).restrict s), λ f g, Lp_to_Lp_restrict_add f g s, λ c f, Lp_to_Lp_restrict_smul c f s⟩ 1 (by { intro f, rw one_mul, exact norm_Lp_to_Lp_restrict_le s f, }) variables {α F 𝕜} variables (𝕜) lemma Lp_to_Lp_restrict_clm_coe_fn [borel_space 𝕜] [hp : fact (1 ≤ p)] (s : set α) (f : Lp F p μ) : Lp_to_Lp_restrict_clm α F 𝕜 μ p s f =ᵐ[μ.restrict s] f := mem_ℒp.coe_fn_to_Lp ((Lp.mem_ℒp f).restrict s) variables {𝕜} @[continuity] lemma continuous_set_integral [normed_space ℝ E] [complete_space E] (s : set α) : continuous (λ f : α →₁[μ] E, ∫ x in s, f x ∂μ) := begin haveI : fact ((1 : ℝ≥0∞) ≤ 1) := ⟨le_rfl⟩, have h_comp : (λ f : α →₁[μ] E, ∫ x in s, f x ∂μ) = (integral (μ.restrict s)) ∘ (λ f, Lp_to_Lp_restrict_clm α E ℝ μ 1 s f), { ext1 f, rw [function.comp_apply, integral_congr_ae (Lp_to_Lp_restrict_clm_coe_fn ℝ s f)], }, rw h_comp, exact continuous_integral.comp (Lp_to_Lp_restrict_clm α E ℝ μ 1 s).continuous, end end continuous_set_integral end measure_theory open measure_theory asymptotics metric variables {ι : Type*} [measurable_space E] [normed_group E] /-- Fundamental theorem of calculus for set integrals: if `μ` is a measure that is finite at a filter `l` and `f` is a measurable function that has a finite limit `b` at `l ⊓ μ.ae`, then `∫ x in s i, f x ∂μ = μ (s i) • b + o(μ (s i))` at a filter `li` provided that `s i` tends to `l.lift' powerset` along `li`. Since `μ (s i)` is an `ℝ≥0∞` number, we use `(μ (s i)).to_real` in the actual statement. Often there is a good formula for `(μ (s i)).to_real`, so the formalization can take an optional argument `m` with this formula and a proof `of `(λ i, (μ (s i)).to_real) =ᶠ[li] m`. Without these arguments, `m i = (μ (s i)).to_real` is used in the output. -/ lemma filter.tendsto.integral_sub_linear_is_o_ae [normed_space ℝ E] [second_countable_topology E] [complete_space E] [borel_space E] {μ : measure α} {l : filter α} [l.is_measurably_generated] {f : α → E} {b : E} (h : tendsto f (l ⊓ μ.ae) (𝓝 b)) (hfm : measurable_at_filter f l μ) (hμ : μ.finite_at_filter l) {s : ι → set α} {li : filter ι} (hs : tendsto s li (l.lift' powerset)) (m : ι → ℝ := λ i, (μ (s i)).to_real) (hsμ : (λ i, (μ (s i)).to_real) =ᶠ[li] m . tactic.interactive.refl) : is_o (λ i, ∫ x in s i, f x ∂μ - m i • b) m li := begin suffices : is_o (λ s, ∫ x in s, f x ∂μ - (μ s).to_real • b) (λ s, (μ s).to_real) (l.lift' powerset), from (this.comp_tendsto hs).congr' (hsμ.mono $ λ a ha, ha ▸ rfl) hsμ, refine is_o_iff.2 (λ ε ε₀, _), have : ∀ᶠ s in l.lift' powerset, ∀ᶠ x in μ.ae, x ∈ s → f x ∈ closed_ball b ε := eventually_lift'_powerset_eventually.2 (h.eventually $ closed_ball_mem_nhds _ ε₀), filter_upwards [hμ.eventually, (hμ.integrable_at_filter_of_tendsto_ae hfm h).eventually, hfm.eventually, this], simp only [mem_closed_ball, dist_eq_norm], intros s hμs h_integrable hfm h_norm, rw [← set_integral_const, ← integral_sub h_integrable (integrable_on_const.2 $ or.inr hμs), real.norm_eq_abs, abs_of_nonneg ennreal.to_real_nonneg], exact norm_set_integral_le_of_norm_le_const_ae' hμs h_norm (hfm.sub ae_measurable_const) end /-- Fundamental theorem of calculus for set integrals, `nhds_within` version: if `μ` is a locally finite measure and `f` is an almost everywhere measurable function that is continuous at a point `a` within a measurable set `t`, then `∫ x in s i, f x ∂μ = μ (s i) • f a + o(μ (s i))` at a filter `li` provided that `s i` tends to `(𝓝[t] a).lift' powerset` along `li`. Since `μ (s i)` is an `ℝ≥0∞` number, we use `(μ (s i)).to_real` in the actual statement. Often there is a good formula for `(μ (s i)).to_real`, so the formalization can take an optional argument `m` with this formula and a proof `of `(λ i, (μ (s i)).to_real) =ᶠ[li] m`. Without these arguments, `m i = (μ (s i)).to_real` is used in the output. -/ lemma continuous_within_at.integral_sub_linear_is_o_ae [topological_space α] [opens_measurable_space α] [normed_space ℝ E] [second_countable_topology E] [complete_space E] [borel_space E] {μ : measure α} [is_locally_finite_measure μ] {a : α} {t : set α} {f : α → E} (ha : continuous_within_at f t a) (ht : measurable_set t) (hfm : measurable_at_filter f (𝓝[t] a) μ) {s : ι → set α} {li : filter ι} (hs : tendsto s li ((𝓝[t] a).lift' powerset)) (m : ι → ℝ := λ i, (μ (s i)).to_real) (hsμ : (λ i, (μ (s i)).to_real) =ᶠ[li] m . tactic.interactive.refl) : is_o (λ i, ∫ x in s i, f x ∂μ - m i • f a) m li := by haveI : (𝓝[t] a).is_measurably_generated := ht.nhds_within_is_measurably_generated _; exact (ha.mono_left inf_le_left).integral_sub_linear_is_o_ae hfm (μ.finite_at_nhds_within a t) hs m hsμ /-- Fundamental theorem of calculus for set integrals, `nhds` version: if `μ` is a locally finite measure and `f` is an almost everywhere measurable function that is continuous at a point `a`, then `∫ x in s i, f x ∂μ = μ (s i) • f a + o(μ (s i))` at `li` provided that `s` tends to `(𝓝 a).lift' powerset` along `li. Since `μ (s i)` is an `ℝ≥0∞` number, we use `(μ (s i)).to_real` in the actual statement. Often there is a good formula for `(μ (s i)).to_real`, so the formalization can take an optional argument `m` with this formula and a proof `of `(λ i, (μ (s i)).to_real) =ᶠ[li] m`. Without these arguments, `m i = (μ (s i)).to_real` is used in the output. -/ lemma continuous_at.integral_sub_linear_is_o_ae [topological_space α] [opens_measurable_space α] [normed_space ℝ E] [second_countable_topology E] [complete_space E] [borel_space E] {μ : measure α} [is_locally_finite_measure μ] {a : α} {f : α → E} (ha : continuous_at f a) (hfm : measurable_at_filter f (𝓝 a) μ) {s : ι → set α} {li : filter ι} (hs : tendsto s li ((𝓝 a).lift' powerset)) (m : ι → ℝ := λ i, (μ (s i)).to_real) (hsμ : (λ i, (μ (s i)).to_real) =ᶠ[li] m . tactic.interactive.refl) : is_o (λ i, ∫ x in s i, f x ∂μ - m i • f a) m li := (ha.mono_left inf_le_left).integral_sub_linear_is_o_ae hfm (μ.finite_at_nhds a) hs m hsμ /-- If a function is continuous on an open set `s`, then it is measurable at the filter `𝓝 x` for all `x ∈ s`. -/ lemma continuous_on.measurable_at_filter [topological_space α] [opens_measurable_space α] [borel_space E] {f : α → E} {s : set α} {μ : measure α} (hs : is_open s) (hf : continuous_on f s) : ∀ x ∈ s, measurable_at_filter f (𝓝 x) μ := λ x hx, ⟨s, is_open.mem_nhds hs hx, hf.ae_measurable hs.measurable_set⟩ lemma continuous_at.measurable_at_filter [topological_space α] [opens_measurable_space α] [borel_space E] {f : α → E} {s : set α} {μ : measure α} (hs : is_open s) (hf : ∀ x ∈ s, continuous_at f x) : ∀ x ∈ s, measurable_at_filter f (𝓝 x) μ := continuous_on.measurable_at_filter hs $ continuous_at.continuous_on hf /-- If a function is continuous on a measurable set `s`, then it is measurable at the filter `𝓝[s] x` for all `x`. -/ lemma continuous_on.measurable_at_filter_nhds_within {α E : Type*} [measurable_space α] [measurable_space E] [normed_group E] [topological_space α] [opens_measurable_space α] [borel_space E] {f : α → E} {s : set α} {μ : measure α} (hf : continuous_on f s) (hs : measurable_set s) (x : α) : measurable_at_filter f (𝓝[s] x) μ := ⟨s, self_mem_nhds_within, hf.ae_measurable hs⟩ /-- Fundamental theorem of calculus for set integrals, `nhds_within` version: if `μ` is a locally finite measure, `f` is continuous on a measurable set `t`, and `a ∈ t`, then `∫ x in (s i), f x ∂μ = μ (s i) • f a + o(μ (s i))` at `li` provided that `s i` tends to `(𝓝[t] a).lift' powerset` along `li`. Since `μ (s i)` is an `ℝ≥0∞` number, we use `(μ (s i)).to_real` in the actual statement. Often there is a good formula for `(μ (s i)).to_real`, so the formalization can take an optional argument `m` with this formula and a proof `of `(λ i, (μ (s i)).to_real) =ᶠ[li] m`. Without these arguments, `m i = (μ (s i)).to_real` is used in the output. -/ lemma continuous_on.integral_sub_linear_is_o_ae [topological_space α] [opens_measurable_space α] [normed_space ℝ E] [second_countable_topology E] [complete_space E] [borel_space E] {μ : measure α} [is_locally_finite_measure μ] {a : α} {t : set α} {f : α → E} (hft : continuous_on f t) (ha : a ∈ t) (ht : measurable_set t) {s : ι → set α} {li : filter ι} (hs : tendsto s li ((𝓝[t] a).lift' powerset)) (m : ι → ℝ := λ i, (μ (s i)).to_real) (hsμ : (λ i, (μ (s i)).to_real) =ᶠ[li] m . tactic.interactive.refl) : is_o (λ i, ∫ x in s i, f x ∂μ - m i • f a) m li := (hft a ha).integral_sub_linear_is_o_ae ht ⟨t, self_mem_nhds_within, hft.ae_measurable ht⟩ hs m hsμ section /-! ### Continuous linear maps composed with integration The goal of this section is to prove that integration commutes with continuous linear maps. This holds for simple functions. The general result follows from the continuity of all involved operations on the space `L¹`. Note that composition by a continuous linear map on `L¹` is not just the composition, as we are dealing with classes of functions, but it has already been defined as `continuous_linear_map.comp_Lp`. We take advantage of this construction here. -/ variables {μ : measure α} {𝕜 : Type*} [is_R_or_C 𝕜] [normed_space 𝕜 E] [normed_group F] [normed_space 𝕜 F] {p : ennreal} local attribute [instance] fact_one_le_one_ennreal namespace continuous_linear_map variables [measurable_space F] [borel_space F] variables [second_countable_topology F] [complete_space F] [borel_space E] [second_countable_topology E] [normed_space ℝ F] lemma integral_comp_Lp (L : E →L[𝕜] F) (φ : Lp E p μ) : ∫ a, (L.comp_Lp φ) a ∂μ = ∫ a, L (φ a) ∂μ := integral_congr_ae $ coe_fn_comp_Lp _ _ lemma set_integral_comp_Lp (L : E →L[𝕜] F) (φ : Lp E p μ) {s : set α} (hs : measurable_set s) : ∫ a in s, (L.comp_Lp φ) a ∂μ = ∫ a in s, L (φ a) ∂μ := set_integral_congr_ae hs ((L.coe_fn_comp_Lp φ).mono (λ x hx hx2, hx)) lemma continuous_integral_comp_L1 [measurable_space 𝕜] [opens_measurable_space 𝕜] (L : E →L[𝕜] F) : continuous (λ (φ : α →₁[μ] E), ∫ (a : α), L (φ a) ∂μ) := by { rw ← funext L.integral_comp_Lp, exact continuous_integral.comp (L.comp_LpL 1 μ).continuous, } variables [complete_space E] [measurable_space 𝕜] [opens_measurable_space 𝕜] [normed_space ℝ E] [is_scalar_tower ℝ 𝕜 E] [is_scalar_tower ℝ 𝕜 F] lemma integral_comp_comm (L : E →L[𝕜] F) {φ : α → E} (φ_int : integrable φ μ) : ∫ a, L (φ a) ∂μ = L (∫ a, φ a ∂μ) := begin apply integrable.induction (λ φ, ∫ a, L (φ a) ∂μ = L (∫ a, φ a ∂μ)), { intros e s s_meas s_finite, rw [integral_indicator_const e s_meas, ← @smul_one_smul E ℝ 𝕜 _ _ _ _ _ (μ s).to_real e, continuous_linear_map.map_smul, @smul_one_smul F ℝ 𝕜 _ _ _ _ _ (μ s).to_real (L e), ← integral_indicator_const (L e) s_meas], congr' 1 with a, rw set.indicator_comp_of_zero L.map_zero }, { intros f g H f_int g_int hf hg, simp [L.map_add, integral_add f_int g_int, integral_add (L.integrable_comp f_int) (L.integrable_comp g_int), hf, hg] }, { exact is_closed_eq L.continuous_integral_comp_L1 (L.continuous.comp continuous_integral) }, { intros f g hfg f_int hf, convert hf using 1 ; clear hf, { exact integral_congr_ae (hfg.fun_comp L).symm }, { rw integral_congr_ae hfg.symm } }, all_goals { assumption } end lemma integral_apply {H : Type*} [normed_group H] [normed_space ℝ H] [second_countable_topology $ H →L[ℝ] E] {φ : α → H →L[ℝ] E} (φ_int : integrable φ μ) (v : H) : (∫ a, φ a ∂μ) v = ∫ a, φ a v ∂μ := ((continuous_linear_map.apply ℝ E v).integral_comp_comm φ_int).symm lemma integral_comp_comm' (L : E →L[𝕜] F) {K} (hL : antilipschitz_with K L) (φ : α → E) : ∫ a, L (φ a) ∂μ = L (∫ a, φ a ∂μ) := begin by_cases h : integrable φ μ, { exact integral_comp_comm L h }, have : ¬ (integrable (L ∘ φ) μ), by rwa lipschitz_with.integrable_comp_iff_of_antilipschitz L.lipschitz hL (L.map_zero), simp [integral_undef, h, this] end lemma integral_comp_L1_comm (L : E →L[𝕜] F) (φ : α →₁[μ] E) : ∫ a, L (φ a) ∂μ = L (∫ a, φ a ∂μ) := L.integral_comp_comm (L1.integrable_coe_fn φ) end continuous_linear_map namespace linear_isometry variables [measurable_space F] [borel_space F] [second_countable_topology F] [complete_space F] [normed_space ℝ F] [is_scalar_tower ℝ 𝕜 F] [borel_space E] [second_countable_topology E] [complete_space E] [normed_space ℝ E] [is_scalar_tower ℝ 𝕜 E] [measurable_space 𝕜] [opens_measurable_space 𝕜] lemma integral_comp_comm (L : E →ₗᵢ[𝕜] F) (φ : α → E) : ∫ a, L (φ a) ∂μ = L (∫ a, φ a ∂μ) := L.to_continuous_linear_map.integral_comp_comm' L.antilipschitz _ end linear_isometry variables [borel_space E] [second_countable_topology E] [complete_space E] [normed_space ℝ E] [measurable_space F] [borel_space F] [second_countable_topology F] [complete_space F] [normed_space ℝ F] [measurable_space 𝕜] [borel_space 𝕜] @[norm_cast] lemma integral_of_real {f : α → ℝ} : ∫ a, (f a : 𝕜) ∂μ = ↑∫ a, f a ∂μ := (@is_R_or_C.of_real_li 𝕜 _).integral_comp_comm f lemma integral_re {f : α → 𝕜} (hf : integrable f μ) : ∫ a, is_R_or_C.re (f a) ∂μ = is_R_or_C.re ∫ a, f a ∂μ := (@is_R_or_C.re_clm 𝕜 _).integral_comp_comm hf lemma integral_im {f : α → 𝕜} (hf : integrable f μ) : ∫ a, is_R_or_C.im (f a) ∂μ = is_R_or_C.im ∫ a, f a ∂μ := (@is_R_or_C.im_clm 𝕜 _).integral_comp_comm hf lemma integral_conj {f : α → 𝕜} : ∫ a, is_R_or_C.conj (f a) ∂μ = is_R_or_C.conj ∫ a, f a ∂μ := (@is_R_or_C.conj_lie 𝕜 _).to_linear_isometry.integral_comp_comm f lemma fst_integral {f : α → E × F} (hf : integrable f μ) : (∫ x, f x ∂μ).1 = ∫ x, (f x).1 ∂μ := ((continuous_linear_map.fst ℝ E F).integral_comp_comm hf).symm lemma snd_integral {f : α → E × F} (hf : integrable f μ) : (∫ x, f x ∂μ).2 = ∫ x, (f x).2 ∂μ := ((continuous_linear_map.snd ℝ E F).integral_comp_comm hf).symm lemma integral_pair {f : α → E} {g : α → F} (hf : integrable f μ) (hg : integrable g μ) : ∫ x, (f x, g x) ∂μ = (∫ x, f x ∂μ, ∫ x, g x ∂μ) := have _ := hf.prod_mk hg, prod.ext (fst_integral this) (snd_integral this) lemma integral_smul_const (f : α → ℝ) (c : E) : ∫ x, f x • c ∂μ = (∫ x, f x ∂μ) • c := begin by_cases hf : integrable f μ, { exact ((continuous_linear_map.id ℝ ℝ).smul_right c).integral_comp_comm hf }, { by_cases hc : c = 0, { simp only [hc, integral_zero, smul_zero] }, rw [integral_undef hf, integral_undef, zero_smul], simp_rw [integrable_smul_const hc, hf, not_false_iff] } end section inner variables {E' : Type*} [inner_product_space 𝕜 E'] [measurable_space E'] [borel_space E'] [second_countable_topology E'] [complete_space E'] [normed_space ℝ E'] [is_scalar_tower ℝ 𝕜 E'] local notation `⟪`x`, `y`⟫` := @inner 𝕜 E' _ x y lemma integral_inner {f : α → E'} (hf : integrable f μ) (c : E') : ∫ x, ⟪c, f x⟫ ∂μ = ⟪c, ∫ x, f x ∂μ⟫ := ((@inner_right 𝕜 E' _ _ c).restrict_scalars ℝ).integral_comp_comm hf lemma integral_eq_zero_of_forall_integral_inner_eq_zero (f : α → E') (hf : integrable f μ) (hf_int : ∀ (c : E'), ∫ x, ⟪c, f x⟫ ∂μ = 0) : ∫ x, f x ∂μ = 0 := by { specialize hf_int (∫ x, f x ∂μ), rwa [integral_inner hf, inner_self_eq_zero] at hf_int } end inner end /- namespace integrable variables [measurable_space α] [measurable_space β] [normed_group E] protected lemma measure_mono end integrable end measure_theory section integral_on variables [measurable_space α] [normed_group β] [second_countable_topology β] [normed_space ℝ β] [complete_space β] [measurable_space β] [borel_space β] {s t : set α} {f g : α → β} {μ : measure α} open set lemma integral_on_congr (hf : measurable f) (hg : measurable g) (hs : measurable_set s) (h : ∀ᵐ a ∂μ, a ∈ s → f a = g a) : ∫ a in s, f a ∂μ = ∫ a in s, g a ∂μ := integral_congr_ae hf hg $ _ lemma integral_on_congr_of_set (hsm : measurable_on s f) (htm : measurable_on t f) (h : ∀ᵐ a, a ∈ s ↔ a ∈ t) : (∫ a in s, f a) = (∫ a in t, f a) := integral_congr_ae hsm htm $ indicator_congr_of_set h lemma integral_on_add {s : set α} (hfm : measurable_on s f) (hfi : integrable_on s f) (hgm : measurable_on s g) (hgi : integrable_on s g) : (∫ a in s, f a + g a) = (∫ a in s, f a) + (∫ a in s, g a) := by { simp only [indicator_add], exact integral_add hfm hfi hgm hgi } lemma integral_on_sub (hfm : measurable_on s f) (hfi : integrable_on s f) (hgm : measurable_on s g) (hgi : integrable_on s g) : (∫ a in s, f a - g a) = (∫ a in s, f a) - (∫ a in s, g a) := by { simp only [indicator_sub], exact integral_sub hfm hfi hgm hgi } lemma integral_on_le_integral_on_ae {f g : α → ℝ} (hfm : measurable_on s f) (hfi : integrable_on s f) (hgm : measurable_on s g) (hgi : integrable_on s g) (h : ∀ᵐ a, a ∈ s → f a ≤ g a) : (∫ a in s, f a) ≤ (∫ a in s, g a) := begin apply integral_le_integral_ae hfm hfi hgm hgi, apply indicator_le_indicator_ae, exact h end lemma integral_on_le_integral_on {f g : α → ℝ} (hfm : measurable_on s f) (hfi : integrable_on s f) (hgm : measurable_on s g) (hgi : integrable_on s g) (h : ∀ a, a ∈ s → f a ≤ g a) : (∫ a in s, f a) ≤ (∫ a in s, g a) := integral_on_le_integral_on_ae hfm hfi hgm hgi $ by filter_upwards [] h lemma integral_on_union (hsm : measurable_on s f) (hsi : integrable_on s f) (htm : measurable_on t f) (hti : integrable_on t f) (h : disjoint s t) : (∫ a in (s ∪ t), f a) = (∫ a in s, f a) + (∫ a in t, f a) := by { rw [indicator_union_of_disjoint h, integral_add hsm hsi htm hti] } lemma integral_on_union_ae (hs : measurable_set s) (ht : measurable_set t) (hsm : measurable_on s f) (hsi : integrable_on s f) (htm : measurable_on t f) (hti : integrable_on t f) (h : ∀ᵐ a, a ∉ s ∩ t) : (∫ a in (s ∪ t), f a) = (∫ a in s, f a) + (∫ a in t, f a) := begin have := integral_congr_ae _ _ (indicator_union_ae h f), rw [this, integral_add hsm hsi htm hti], { exact hsm.union hs ht htm }, { exact measurable.add hsm htm } end lemma integral_on_nonneg_of_ae {f : α → ℝ} (hf : ∀ᵐ a, a ∈ s → 0 ≤ f a) : (0:ℝ) ≤ (∫ a in s, f a) := integral_nonneg_of_ae $ by { filter_upwards [hf] λ a h, indicator_nonneg' h } lemma integral_on_nonneg {f : α → ℝ} (hf : ∀ a, a ∈ s → 0 ≤ f a) : (0:ℝ) ≤ (∫ a in s, f a) := integral_on_nonneg_of_ae $ univ_mem' hf lemma integral_on_nonpos_of_ae {f : α → ℝ} (hf : ∀ᵐ a, a ∈ s → f a ≤ 0) : (∫ a in s, f a) ≤ 0 := integral_nonpos_of_nonpos_ae $ by { filter_upwards [hf] λ a h, indicator_nonpos' h } lemma integral_on_nonpos {f : α → ℝ} (hf : ∀ a, a ∈ s → f a ≤ 0) : (∫ a in s, f a) ≤ 0 := integral_on_nonpos_of_ae $ univ_mem' hf lemma tendsto_integral_on_of_monotone {s : ℕ → set α} {f : α → β} (hsm : ∀i, measurable_set (s i)) (h_mono : monotone s) (hfm : measurable_on (Union s) f) (hfi : integrable_on (Union s) f) : tendsto (λi, ∫ a in (s i), f a) at_top (nhds (∫ a in (Union s), f a)) := let bound : α → ℝ := indicator (Union s) (λa, ∥f a∥) in begin apply tendsto_integral_of_dominated_convergence, { assume i, exact hfm.subset (hsm i) (subset_Union _ _) }, { assumption }, { show integrable_on (Union s) (λa, ∥f a∥), rwa integrable_on_norm_iff }, { assume i, apply ae_of_all, assume a, rw [norm_indicator_eq_indicator_norm], exact indicator_le_indicator_of_subset (subset_Union _ _) (λa, norm_nonneg _) _ }, { filter_upwards [] λa, le_trans (tendsto_indicator_of_monotone _ h_mono _ _) (pure_le_nhds _) } end lemma tendsto_integral_on_of_antimono (s : ℕ → set α) (f : α → β) (hsm : ∀i, measurable_set (s i)) (h_mono : ∀i j, i ≤ j → s j ⊆ s i) (hfm : measurable_on (s 0) f) (hfi : integrable_on (s 0) f) : tendsto (λi, ∫ a in (s i), f a) at_top (nhds (∫ a in (Inter s), f a)) := let bound : α → ℝ := indicator (s 0) (λa, ∥f a∥) in begin apply tendsto_integral_of_dominated_convergence, { assume i, refine hfm.subset (hsm i) (h_mono _ _ (zero_le _)) }, { exact hfm.subset (measurable_set.Inter hsm) (Inter_subset _ _) }, { show integrable_on (s 0) (λa, ∥f a∥), rwa integrable_on_norm_iff }, { assume i, apply ae_of_all, assume a, rw [norm_indicator_eq_indicator_norm], refine indicator_le_indicator_of_subset (h_mono _ _ (zero_le _)) (λa, norm_nonneg _) _ }, { filter_upwards [] λa, le_trans (tendsto_indicator_of_antimono _ h_mono _ _) (pure_le_nhds _) } end end integral_on -/
d1911164f46266410b8751bd8ce640dcd5a6bde3
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/group_theory/sylow.lean
268541c524535aa787eff78ed764f6f5089aa31e
[ "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
11,456
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 group_theory.group_action group_theory.quotient_group import group_theory.order_of_element data.zmod.basic import data.fintype.card import data.list.rotate open equiv fintype finset mul_action function open equiv.perm is_subgroup list quotient_group universes u v w variables {G : Type u} {α : Type v} {β : Type w} [group G] local attribute [instance, priority 10] subtype.fintype set_fintype classical.prop_decidable namespace mul_action variables [mul_action G α] lemma mem_fixed_points_iff_card_orbit_eq_one {a : α} [fintype (orbit G a)] : a ∈ fixed_points G α ↔ card (orbit G a) = 1 := begin rw [fintype.card_eq_one_iff, mem_fixed_points], split, { exact λ h, ⟨⟨a, mem_orbit_self _⟩, λ ⟨b, ⟨x, hx⟩⟩, subtype.eq $ by simp [h x, hx.symm]⟩ }, { assume h x, rcases h with ⟨⟨z, hz⟩, hz₁⟩, exact calc x • a = z : subtype.mk.inj (hz₁ ⟨x • a, mem_orbit _ _⟩) ... = a : (subtype.mk.inj (hz₁ ⟨a, mem_orbit_self _⟩)).symm } end lemma card_modeq_card_fixed_points [fintype α] [fintype G] [fintype (fixed_points G α)] {p n : ℕ} (hp : nat.prime p) (h : card G = p ^ n) : card α ≡ card (fixed_points G α) [MOD p] := calc card α = card (Σ y : quotient (orbit_rel G α), {x // quotient.mk' x = y}) : card_congr (sigma_preimage_equiv (@quotient.mk' _ (orbit_rel G α))).symm ... = univ.sum (λ a : quotient (orbit_rel G α), card {x // quotient.mk' x = a}) : card_sigma _ ... ≡ (@univ (fixed_points G α) _).sum (λ _, 1) [MOD p] : begin rw [← zmodp.eq_iff_modeq_nat hp, sum_nat_cast, sum_nat_cast], refine eq.symm (sum_bij_ne_zero (λ a _ _, quotient.mk' a.1) (λ _ _ _, mem_univ _) (λ a₁ a₂ _ _ _ _ h, subtype.eq ((mem_fixed_points' α).1 a₂.2 a₁.1 (quotient.exact' h))) (λ b, _) (λ a ha _, by rw [← mem_fixed_points_iff_card_orbit_eq_one.1 a.2]; simp only [quotient.eq']; congr)), { refine quotient.induction_on' b (λ b _ hb, _), have : card (orbit G b) ∣ p ^ n, { rw [← h, fintype.card_congr (orbit_equiv_quotient_stabilizer G b)]; exact card_quotient_dvd_card _ }, rcases (nat.dvd_prime_pow hp).1 this with ⟨k, _, hk⟩, have hb' :¬ p ^ 1 ∣ p ^ k, { rw [nat.pow_one, ← hk, ← nat.modeq.modeq_zero_iff, ← zmodp.eq_iff_modeq_nat hp, nat.cast_zero, ← ne.def], exact eq.mpr (by simp only [quotient.eq']; congr) hb }, have : k = 0 := nat.le_zero_iff.1 (nat.le_of_lt_succ (lt_of_not_ge (mt (nat.pow_dvd_pow p) hb'))), refine ⟨⟨b, mem_fixed_points_iff_card_orbit_eq_one.2 $ by rw [hk, this, nat.pow_zero]⟩, mem_univ _, by simp [zero_ne_one], rfl⟩ } end ... = _ : by simp; refl end mul_action lemma quotient_group.card_preimage_mk [fintype G] (s : set G) [is_subgroup s] (t : set (quotient s)) : fintype.card (quotient_group.mk ⁻¹' t) = fintype.card s * fintype.card t := by rw [← fintype.card_prod, fintype.card_congr (preimage_mk_equiv_subgroup_times_set _ _)] namespace sylow def mk_vector_prod_eq_one (n : ℕ) (v : vector G n) : vector G (n+1) := v.to_list.prod⁻¹ :: v lemma mk_vector_prod_eq_one_inj (n : ℕ) : injective (@mk_vector_prod_eq_one G _ n) := λ ⟨v, _⟩ ⟨w, _⟩ h, subtype.eq (show v = w, by injection h with h; injection h) def vectors_prod_eq_one (G : Type*) [group G] (n : ℕ) : set (vector G n) := {v | v.to_list.prod = 1} lemma mem_vectors_prod_eq_one {n : ℕ} (v : vector G n) : v ∈ vectors_prod_eq_one G n ↔ v.to_list.prod = 1 := iff.rfl lemma mem_vectors_prod_eq_one_iff {n : ℕ} (v : vector G (n + 1)) : v ∈ vectors_prod_eq_one G (n + 1) ↔ v ∈ set.range (@mk_vector_prod_eq_one G _ n) := ⟨λ (h : v.to_list.prod = 1), ⟨v.tail, begin unfold mk_vector_prod_eq_one, conv {to_rhs, rw ← vector.cons_head_tail v}, suffices : (v.tail.to_list.prod)⁻¹ = v.head, { rw this }, rw [← mul_right_inj v.tail.to_list.prod, inv_mul_self, ← list.prod_cons, ← vector.to_list_cons, vector.cons_head_tail, h] end⟩, λ ⟨w, hw⟩, by rw [mem_vectors_prod_eq_one, ← hw, mk_vector_prod_eq_one, vector.to_list_cons, list.prod_cons, inv_mul_self]⟩ def rotate_vectors_prod_eq_one (G : Type*) [group G] (n : ℕ+) (m : multiplicative (zmod n)) (v : vectors_prod_eq_one G n) : vectors_prod_eq_one G n := ⟨⟨v.1.to_list.rotate m.1, by simp⟩, prod_rotate_eq_one_of_prod_eq_one v.2 _⟩ instance rotate_vectors_prod_eq_one.mul_action (n : ℕ+) : mul_action (multiplicative (zmod n)) (vectors_prod_eq_one G n) := { smul := (rotate_vectors_prod_eq_one G n), one_smul := λ v, subtype.eq $ vector.eq _ _ $ rotate_zero v.1.to_list, mul_smul := λ a b ⟨⟨v, hv₁⟩, hv₂⟩, subtype.eq $ vector.eq _ _ $ show v.rotate ((a + b : zmod n).val) = list.rotate (list.rotate v (b.val)) (a.val), by rw [zmod.add_val, rotate_rotate, ← rotate_mod _ (b.1 + a.1), add_comm, hv₁] } lemma one_mem_vectors_prod_eq_one (n : ℕ) : vector.repeat (1 : G) n ∈ vectors_prod_eq_one G n := by simp [vector.repeat, vectors_prod_eq_one] lemma one_mem_fixed_points_rotate (n : ℕ+) : (⟨vector.repeat (1 : G) n, one_mem_vectors_prod_eq_one n⟩ : vectors_prod_eq_one G n) ∈ fixed_points (multiplicative (zmod n)) (vectors_prod_eq_one G n) := λ m, subtype.eq $ vector.eq _ _ $ by haveI : nonempty G := ⟨1⟩; exact rotate_eq_self_iff_eq_repeat.2 ⟨(1 : G), show list.repeat (1 : G) n = list.repeat 1 (list.repeat (1 : G) n).length, by simp⟩ _ /-- Cauchy's theorem -/ lemma exists_prime_order_of_dvd_card [fintype G] {p : ℕ} (hp : nat.prime p) (hdvd : p ∣ card G) : ∃ x : G, order_of x = p := let n : ℕ+ := ⟨p - 1, nat.sub_pos_of_lt hp.one_lt⟩ in let p' : ℕ+ := ⟨p, hp.pos⟩ in have hn : p' = n + 1 := subtype.eq (nat.succ_sub hp.pos), have hcard : card (vectors_prod_eq_one G (n + 1)) = card G ^ (n : ℕ), by rw [set.ext mem_vectors_prod_eq_one_iff, set.card_range_of_injective (mk_vector_prod_eq_one_inj _), card_vector], have hzmod : fintype.card (multiplicative (zmod p')) = (p' : ℕ) ^ 1 := (nat.pow_one p').symm ▸ fintype.card_fin _, have hmodeq : _ = _ := @mul_action.card_modeq_card_fixed_points (multiplicative (zmod p')) (vectors_prod_eq_one G p') _ _ _ _ _ _ 1 hp hzmod, have hdvdcard : p ∣ fintype.card (vectors_prod_eq_one G (n + 1)) := calc p ∣ card G ^ 1 : by rwa nat.pow_one ... ∣ card G ^ (n : ℕ) : nat.pow_dvd_pow _ n.2 ... = card (vectors_prod_eq_one G (n + 1)) : hcard.symm, have hdvdcard₂ : p ∣ card (fixed_points (multiplicative (zmod p')) (vectors_prod_eq_one G p')) := nat.dvd_of_mod_eq_zero (hmodeq ▸ hn.symm ▸ nat.mod_eq_zero_of_dvd hdvdcard), have hcard_pos : 0 < card (fixed_points (multiplicative (zmod p')) (vectors_prod_eq_one G p')) := fintype.card_pos_iff.2 ⟨⟨⟨vector.repeat 1 p', one_mem_vectors_prod_eq_one _⟩, one_mem_fixed_points_rotate _⟩⟩, have hlt : 1 < card (fixed_points (multiplicative (zmod p')) (vectors_prod_eq_one G p')) := calc (1 : ℕ) < p' : hp.one_lt ... ≤ _ : nat.le_of_dvd hcard_pos hdvdcard₂, let ⟨⟨⟨⟨x, hx₁⟩, hx₂⟩, hx₃⟩, hx₄⟩ := fintype.exists_ne_of_one_lt_card hlt ⟨_, one_mem_fixed_points_rotate p'⟩ in have hx : x ≠ list.repeat (1 : G) p', from λ h, by simpa [h, vector.repeat] using hx₄, have nG : nonempty G, from ⟨1⟩, have ∃ a, x = list.repeat a x.length := by exactI rotate_eq_self_iff_eq_repeat.1 (λ n, have list.rotate x (n : zmod p').val = x := subtype.mk.inj (subtype.mk.inj (hx₃ (n : zmod p'))), by rwa [zmod.val_cast_nat, ← hx₁, rotate_mod] at this), let ⟨a, ha⟩ := this in ⟨a, have hx1 : x.prod = 1 := hx₂, have ha1: a ≠ 1, from λ h, hx (ha.symm ▸ h ▸ hx₁ ▸ rfl), have a ^ p = 1, by rwa [ha, list.prod_repeat, hx₁] at hx1, (hp.2 _ (order_of_dvd_of_pow_eq_one this)).resolve_left (λ h, ha1 (order_of_eq_one_iff.1 h))⟩ open is_subgroup is_submonoid is_group_hom mul_action lemma mem_fixed_points_mul_left_cosets_iff_mem_normalizer {H : set G} [is_subgroup H] [fintype H] {x : G} : (x : quotient H) ∈ fixed_points H (quotient H) ↔ x ∈ normalizer H := ⟨λ hx, have ha : ∀ {y : quotient H}, y ∈ orbit H (x : quotient H) → y = x, from λ _, ((mem_fixed_points' _).1 hx _), (inv_mem_iff _).1 (mem_normalizer_fintype (λ n hn, have (n⁻¹ * x)⁻¹ * x ∈ H := quotient_group.eq.1 (ha (mem_orbit _ ⟨n⁻¹, inv_mem hn⟩)), by simpa only [mul_inv_rev, inv_inv] using this)), λ (hx : ∀ (n : G), n ∈ H ↔ x * n * x⁻¹ ∈ H), (mem_fixed_points' _).2 $ λ y, quotient.induction_on' y $ λ y hy, quotient_group.eq.2 (let ⟨⟨b, hb₁⟩, hb₂⟩ := hy in have hb₂ : (b * x)⁻¹ * y ∈ H := quotient_group.eq.1 hb₂, (inv_mem_iff H).1 $ (hx _).2 $ (mul_mem_cancel_right H (inv_mem hb₁)).1 $ by rw hx at hb₂; simpa [mul_inv_rev, mul_assoc] using hb₂)⟩ def fixed_points_mul_left_cosets_equiv_quotient (H : set G) [is_subgroup H] [fintype H] : fixed_points H (quotient H) ≃ quotient (subtype.val ⁻¹' H : set (normalizer H)) := @subtype_quotient_equiv_quotient_subtype G (normalizer H) (id _) (id _) (fixed_points _ _) (λ a, mem_fixed_points_mul_left_cosets_iff_mem_normalizer.symm) (by intros; refl) local attribute [instance] set_fintype lemma exists_subgroup_card_pow_prime [fintype G] {p : ℕ} : ∀ {n : ℕ} (hp : nat.prime p) (hdvd : p ^ n ∣ card G), ∃ H : set G, is_subgroup H ∧ fintype.card H = p ^ n | 0 := λ _ _, ⟨trivial G, by apply_instance, by simp⟩ | (n+1) := λ hp hdvd, let ⟨H, ⟨hH1, hH2⟩⟩ := exists_subgroup_card_pow_prime hp (dvd.trans (nat.pow_dvd_pow _ (nat.le_succ _)) hdvd) in let ⟨s, hs⟩ := exists_eq_mul_left_of_dvd hdvd in by exactI have hcard : card (quotient H) = s * p := (nat.mul_right_inj (show card H > 0, from fintype.card_pos_iff.2 ⟨⟨1, is_submonoid.one_mem⟩⟩)).1 (by rwa [← card_eq_card_quotient_mul_card_subgroup, hH2, hs, nat.pow_succ, mul_assoc, mul_comm p]), have hm : s * p % p = card (quotient (subtype.val ⁻¹' H : set (normalizer H))) % p := card_congr (fixed_points_mul_left_cosets_equiv_quotient H) ▸ hcard ▸ card_modeq_card_fixed_points hp hH2, have hm' : p ∣ card (quotient (subtype.val ⁻¹' H : set (normalizer H))) := nat.dvd_of_mod_eq_zero (by rwa [nat.mod_eq_zero_of_dvd (dvd_mul_left _ _), eq_comm] at hm), let ⟨x, hx⟩ := @exists_prime_order_of_dvd_card _ (quotient_group.group _) _ _ hp hm' in have hxcard : ∀ {f : fintype (gpowers x)}, card (gpowers x) = p, from λ f, by rw [← hx, order_eq_card_gpowers]; congr, have is_subgroup (mk ⁻¹' gpowers x), from is_group_hom.preimage _ _, have fintype (mk ⁻¹' gpowers x), by apply_instance, have hequiv : H ≃ (subtype.val ⁻¹' H : set (normalizer H)) := ⟨λ a, ⟨⟨a.1, subset_normalizer _ a.2⟩, a.2⟩, λ a, ⟨a.1.1, a.2⟩, λ ⟨_, _⟩, rfl, λ ⟨⟨_, _⟩, _⟩, rfl⟩, ⟨subtype.val '' (mk ⁻¹' gpowers x), by apply_instance, by rw [set.card_image_of_injective (mk ⁻¹' gpowers x) subtype.val_injective, nat.pow_succ, ← hH2, fintype.card_congr hequiv, ← hx, order_eq_card_gpowers, ← fintype.card_prod]; exact @fintype.card_congr _ _ (id _) (id _) (preimage_mk_equiv_subgroup_times_set _ _)⟩ end sylow
efae268b7fa3b0649c8206c84d9ec6ca4ceff5c8
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/measure_theory/measurable_space_def.lean
a36f71f12a64f58652ffa2b23463dbef86c2bf32
[ "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
19,885
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import data.set.countable import logic.encodable.lattice import order.disjointed /-! # Measurable spaces and measurable functions This file defines measurable spaces and measurable functions. A measurable space is a set equipped with a σ-algebra, a collection of subsets closed under complementation and countable union. A function between measurable spaces is measurable if the preimage of each measurable subset is measurable. σ-algebras on a fixed set `α` form a complete lattice. Here we order σ-algebras by writing `m₁ ≤ m₂` if every set which is `m₁`-measurable is also `m₂`-measurable (that is, `m₁` is a subset of `m₂`). In particular, any collection of subsets of `α` generates a smallest σ-algebra which contains all of them. Do not add measurability lemmas (which could be tagged with @[measurability]) to this file, since the measurability tactic is downstream from here. Use `measure_theory.measurable_space` instead. ## References * <https://en.wikipedia.org/wiki/Measurable_space> * <https://en.wikipedia.org/wiki/Sigma-algebra> * <https://en.wikipedia.org/wiki/Dynkin_system> ## Tags measurable space, σ-algebra, measurable function -/ open set encodable function equiv open_locale classical variables {α β γ δ δ' : Type*} {ι : Sort*} {s t u : set α} /-- A measurable space is a space equipped with a σ-algebra. -/ structure measurable_space (α : Type*) := (measurable_set' : set α → Prop) (measurable_set_empty : measurable_set' ∅) (measurable_set_compl : ∀ s, measurable_set' s → measurable_set' sᶜ) (measurable_set_Union : ∀ f : ℕ → set α, (∀ i, measurable_set' (f i)) → measurable_set' (⋃ i, f i)) attribute [class] measurable_space instance [h : measurable_space α] : measurable_space αᵒᵈ := h section /-- `measurable_set s` means that `s` is measurable (in the ambient measure space on `α`) -/ def measurable_set [measurable_space α] : set α → Prop := ‹measurable_space α›.measurable_set' localized "notation (name := measurable_set_of) `measurable_set[` m `]` := @measurable_set hole! m" in measure_theory @[simp] lemma measurable_set.empty [measurable_space α] : measurable_set (∅ : set α) := ‹measurable_space α›.measurable_set_empty variable {m : measurable_space α} include m lemma measurable_set.compl : measurable_set s → measurable_set sᶜ := ‹measurable_space α›.measurable_set_compl s lemma measurable_set.of_compl (h : measurable_set sᶜ) : measurable_set s := compl_compl s ▸ h.compl @[simp] lemma measurable_set.compl_iff : measurable_set sᶜ ↔ measurable_set s := ⟨measurable_set.of_compl, measurable_set.compl⟩ @[simp] lemma measurable_set.univ : measurable_set (univ : set α) := by simpa using (@measurable_set.empty α _).compl @[nontriviality] lemma subsingleton.measurable_set [subsingleton α] {s : set α} : measurable_set s := subsingleton.set_cases measurable_set.empty measurable_set.univ s lemma measurable_set.congr {s t : set α} (hs : measurable_set s) (h : s = t) : measurable_set t := by rwa ← h lemma measurable_set.bUnion_decode₂ [encodable β] ⦃f : β → set α⦄ (h : ∀ b, measurable_set (f b)) (n : ℕ) : measurable_set (⋃ b ∈ decode₂ β n, f b) := encodable.Union_decode₂_cases measurable_set.empty h lemma measurable_set.Union [countable ι] ⦃f : ι → set α⦄ (h : ∀ b, measurable_set (f b)) : measurable_set (⋃ b, f b) := begin casesI nonempty_encodable (plift ι), rw [←Union_plift_down, ←encodable.Union_decode₂], exact ‹measurable_space α›.measurable_set_Union _ (measurable_set.bUnion_decode₂ $ λ _, h _), end lemma measurable_set.bUnion {f : β → set α} {s : set β} (hs : s.countable) (h : ∀ b ∈ s, measurable_set (f b)) : measurable_set (⋃ b ∈ s, f b) := begin rw bUnion_eq_Union, haveI := hs.to_encodable, exact measurable_set.Union (by simpa using h) end lemma set.finite.measurable_set_bUnion {f : β → set α} {s : set β} (hs : s.finite) (h : ∀ b ∈ s, measurable_set (f b)) : measurable_set (⋃ b ∈ s, f b) := measurable_set.bUnion hs.countable h lemma finset.measurable_set_bUnion {f : β → set α} (s : finset β) (h : ∀ b ∈ s, measurable_set (f b)) : measurable_set (⋃ b ∈ s, f b) := s.finite_to_set.measurable_set_bUnion h lemma measurable_set.sUnion {s : set (set α)} (hs : s.countable) (h : ∀ t ∈ s, measurable_set t) : measurable_set (⋃₀ s) := by { rw sUnion_eq_bUnion, exact measurable_set.bUnion hs h } lemma set.finite.measurable_set_sUnion {s : set (set α)} (hs : s.finite) (h : ∀ t ∈ s, measurable_set t) : measurable_set (⋃₀ s) := measurable_set.sUnion hs.countable h lemma measurable_set.Inter [countable ι] {f : ι → set α} (h : ∀ b, measurable_set (f b)) : measurable_set (⋂ b, f b) := measurable_set.compl_iff.1 $ by { rw compl_Inter, exact measurable_set.Union (λ b, (h b).compl) } lemma measurable_set.bInter {f : β → set α} {s : set β} (hs : s.countable) (h : ∀ b ∈ s, measurable_set (f b)) : measurable_set (⋂ b ∈ s, f b) := measurable_set.compl_iff.1 $ by { rw compl_Inter₂, exact measurable_set.bUnion hs (λ b hb, (h b hb).compl) } lemma set.finite.measurable_set_bInter {f : β → set α} {s : set β} (hs : s.finite) (h : ∀ b ∈ s, measurable_set (f b)) : measurable_set (⋂ b ∈ s, f b) := measurable_set.bInter hs.countable h lemma finset.measurable_set_bInter {f : β → set α} (s : finset β) (h : ∀ b ∈ s, measurable_set (f b)) : measurable_set (⋂ b ∈ s, f b) := s.finite_to_set.measurable_set_bInter h lemma measurable_set.sInter {s : set (set α)} (hs : s.countable) (h : ∀ t ∈ s, measurable_set t) : measurable_set (⋂₀ s) := by { rw sInter_eq_bInter, exact measurable_set.bInter hs h } lemma set.finite.measurable_set_sInter {s : set (set α)} (hs : s.finite) (h : ∀ t ∈ s, measurable_set t) : measurable_set (⋂₀ s) := measurable_set.sInter hs.countable h @[simp] lemma measurable_set.union {s₁ s₂ : set α} (h₁ : measurable_set s₁) (h₂ : measurable_set s₂) : measurable_set (s₁ ∪ s₂) := by { rw union_eq_Union, exact measurable_set.Union (bool.forall_bool.2 ⟨h₂, h₁⟩) } @[simp] lemma measurable_set.inter {s₁ s₂ : set α} (h₁ : measurable_set s₁) (h₂ : measurable_set s₂) : measurable_set (s₁ ∩ s₂) := by { rw inter_eq_compl_compl_union_compl, exact (h₁.compl.union h₂.compl).compl } @[simp] lemma measurable_set.diff {s₁ s₂ : set α} (h₁ : measurable_set s₁) (h₂ : measurable_set s₂) : measurable_set (s₁ \ s₂) := h₁.inter h₂.compl @[simp] lemma measurable_set.symm_diff {s₁ s₂ : set α} (h₁ : measurable_set s₁) (h₂ : measurable_set s₂) : measurable_set (s₁ ∆ s₂) := (h₁.diff h₂).union (h₂.diff h₁) @[simp] lemma measurable_set.ite {t s₁ s₂ : set α} (ht : measurable_set t) (h₁ : measurable_set s₁) (h₂ : measurable_set s₂) : measurable_set (t.ite s₁ s₂) := (h₁.inter ht).union (h₂.diff ht) lemma measurable_set.ite' {s t : set α} {p : Prop} (hs : p → measurable_set s) (ht : ¬ p → measurable_set t) : measurable_set (ite p s t) := by { split_ifs, exacts [hs h, ht h], } @[simp] lemma measurable_set.cond {s₁ s₂ : set α} (h₁ : measurable_set s₁) (h₂ : measurable_set s₂) {i : bool} : measurable_set (cond i s₁ s₂) := by { cases i, exacts [h₂, h₁] } @[simp] lemma measurable_set.disjointed {f : ℕ → set α} (h : ∀ i, measurable_set (f i)) (n) : measurable_set (disjointed f n) := disjointed_rec (λ t i ht, measurable_set.diff ht $ h _) (h n) @[simp] lemma measurable_set.const (p : Prop) : measurable_set {a : α | p} := by { by_cases p; simp [h, measurable_set.empty]; apply measurable_set.univ } /-- Every set has a measurable superset. Declare this as local instance as needed. -/ lemma nonempty_measurable_superset (s : set α) : nonempty { t // s ⊆ t ∧ measurable_set t} := ⟨⟨univ, subset_univ s, measurable_set.univ⟩⟩ end open_locale measure_theory @[ext] lemma measurable_space.ext : ∀ {m₁ m₂ : measurable_space α}, (∀ s : set α, measurable_set[m₁] s ↔ measurable_set[m₂] s) → m₁ = m₂ | ⟨s₁, _, _, _⟩ ⟨s₂, _, _, _⟩ h := have s₁ = s₂, from funext $ assume x, propext $ h x, by subst this @[ext] lemma measurable_space.ext_iff {m₁ m₂ : measurable_space α} : m₁ = m₂ ↔ (∀ s : set α, measurable_set[m₁] s ↔ measurable_set[m₂] s) := ⟨by { unfreezingI {rintro rfl}, intro s, refl }, measurable_space.ext⟩ /-- A typeclass mixin for `measurable_space`s such that each singleton is measurable. -/ class measurable_singleton_class (α : Type*) [measurable_space α] : Prop := (measurable_set_singleton : ∀ x, measurable_set ({x} : set α)) export measurable_singleton_class (measurable_set_singleton) attribute [simp] measurable_set_singleton section measurable_singleton_class variables [measurable_space α] [measurable_singleton_class α] lemma measurable_set_eq {a : α} : measurable_set {x | x = a} := measurable_set_singleton a lemma measurable_set.insert {s : set α} (hs : measurable_set s) (a : α) : measurable_set (insert a s) := (measurable_set_singleton a).union hs @[simp] lemma measurable_set_insert {a : α} {s : set α} : measurable_set (insert a s) ↔ measurable_set s := ⟨λ h, if ha : a ∈ s then by rwa ← insert_eq_of_mem ha else insert_diff_self_of_not_mem ha ▸ h.diff (measurable_set_singleton _), λ h, h.insert a⟩ lemma set.subsingleton.measurable_set {s : set α} (hs : s.subsingleton) : measurable_set s := hs.induction_on measurable_set.empty measurable_set_singleton lemma set.finite.measurable_set {s : set α} (hs : s.finite) : measurable_set s := finite.induction_on hs measurable_set.empty $ λ a s ha hsf hsm, hsm.insert _ protected lemma finset.measurable_set (s : finset α) : measurable_set (↑s : set α) := s.finite_to_set.measurable_set lemma set.countable.measurable_set {s : set α} (hs : s.countable) : measurable_set s := begin rw [← bUnion_of_singleton s], exact measurable_set.bUnion hs (λ b hb, measurable_set_singleton b) end end measurable_singleton_class namespace measurable_space section complete_lattice instance : has_le (measurable_space α) := { le := λ m₁ m₂, ∀ s, measurable_set[m₁] s → measurable_set[m₂] s } lemma le_def {α} {a b : measurable_space α} : a ≤ b ↔ a.measurable_set' ≤ b.measurable_set' := iff.rfl instance : partial_order (measurable_space α) := { lt := λ m₁ m₂, m₁ ≤ m₂ ∧ ¬m₂ ≤ m₁, .. measurable_space.has_le, .. partial_order.lift (@measurable_set α) (λ m₁ m₂ h, ext $ λ s, h ▸ iff.rfl) } /-- The smallest σ-algebra containing a collection `s` of basic sets -/ inductive generate_measurable (s : set (set α)) : set α → Prop | basic : ∀ u ∈ s, generate_measurable u | empty : generate_measurable ∅ | compl : ∀ s, generate_measurable s → generate_measurable sᶜ | union : ∀ f : ℕ → set α, (∀ n, generate_measurable (f n)) → generate_measurable (⋃ i, f i) /-- Construct the smallest measure space containing a collection of basic sets -/ def generate_from (s : set (set α)) : measurable_space α := { measurable_set' := generate_measurable s, measurable_set_empty := generate_measurable.empty, measurable_set_compl := generate_measurable.compl, measurable_set_Union := generate_measurable.union } lemma measurable_set_generate_from {s : set (set α)} {t : set α} (ht : t ∈ s) : @measurable_set _ (generate_from s) t := generate_measurable.basic t ht @[elab_as_eliminator] lemma generate_from_induction (p : set α → Prop) (C : set (set α)) (hC : ∀ t ∈ C, p t) (h_empty : p ∅) (h_compl : ∀ t, p t → p tᶜ) (h_Union : ∀ f : ℕ → set α, (∀ n, p (f n)) → p (⋃ i, f i)) {s : set α} (hs : measurable_set[generate_from C] s) : p s := by { induction hs, exacts [hC _ hs_H, h_empty, h_compl _ hs_ih, h_Union hs_f hs_ih], } lemma generate_from_le {s : set (set α)} {m : measurable_space α} (h : ∀ t ∈ s, measurable_set[m] t) : generate_from s ≤ m := assume t (ht : generate_measurable s t), ht.rec_on h (measurable_set_empty m) (assume s _ hs, measurable_set_compl m s hs) (assume f _ hf, measurable_set_Union m f hf) lemma generate_from_le_iff {s : set (set α)} (m : measurable_space α) : generate_from s ≤ m ↔ s ⊆ {t | measurable_set[m] t} := iff.intro (assume h u hu, h _ $ measurable_set_generate_from hu) (assume h, generate_from_le h) @[simp] lemma generate_from_measurable_set [measurable_space α] : generate_from {s : set α | measurable_set s} = ‹_› := le_antisymm (generate_from_le $ λ _, id) $ λ s, measurable_set_generate_from /-- If `g` is a collection of subsets of `α` such that the `σ`-algebra generated from `g` contains the same sets as `g`, then `g` was already a `σ`-algebra. -/ protected def mk_of_closure (g : set (set α)) (hg : {t | measurable_set[generate_from g] t} = g) : measurable_space α := { measurable_set' := λ s, s ∈ g, measurable_set_empty := hg ▸ measurable_set_empty _, measurable_set_compl := hg ▸ measurable_set_compl _, measurable_set_Union := hg ▸ measurable_set_Union _ } lemma mk_of_closure_sets {s : set (set α)} {hs : {t | measurable_set[generate_from s] t} = s} : measurable_space.mk_of_closure s hs = generate_from s := measurable_space.ext $ assume t, show t ∈ s ↔ _, by { conv_lhs { rw [← hs] }, refl } /-- We get a Galois insertion between `σ`-algebras on `α` and `set (set α)` by using `generate_from` on one side and the collection of measurable sets on the other side. -/ def gi_generate_from : galois_insertion (@generate_from α) (λ m, {t | @measurable_set α m t}) := { gc := assume s, generate_from_le_iff, le_l_u := assume m s, measurable_set_generate_from, choice := λ g hg, measurable_space.mk_of_closure g $ le_antisymm hg $ (generate_from_le_iff _).1 le_rfl, choice_eq := assume g hg, mk_of_closure_sets } instance : complete_lattice (measurable_space α) := gi_generate_from.lift_complete_lattice instance : inhabited (measurable_space α) := ⟨⊤⟩ @[mono] lemma generate_from_mono {s t : set (set α)} (h : s ⊆ t) : generate_from s ≤ generate_from t := gi_generate_from.gc.monotone_l h lemma generate_from_sup_generate_from {s t : set (set α)} : generate_from s ⊔ generate_from t = generate_from (s ∪ t) := (@gi_generate_from α).gc.l_sup.symm @[simp] lemma generate_from_insert_univ (S : set (set α)) : generate_from (insert set.univ S) = generate_from S := begin refine le_antisymm _ (generate_from_mono (set.subset_insert _ _)), rw generate_from_le_iff, intros t ht, cases ht, { rw ht, exact measurable_set.univ, }, { exact measurable_set_generate_from ht, }, end @[simp] lemma generate_from_insert_empty (S : set (set α)) : generate_from (insert ∅ S) = generate_from S := begin refine le_antisymm _ (generate_from_mono (set.subset_insert _ _)), rw generate_from_le_iff, intros t ht, cases ht, { rw ht, exact @measurable_set.empty _ (generate_from S), }, { exact measurable_set_generate_from ht, }, end @[simp] lemma generate_from_singleton_empty : generate_from {∅} = (⊥ : measurable_space α) := by { rw [eq_bot_iff, generate_from_le_iff], simp, } @[simp] lemma generate_from_singleton_univ : generate_from {set.univ} = (⊥ : measurable_space α) := by { rw [eq_bot_iff, generate_from_le_iff], simp, } lemma measurable_set_bot_iff {s : set α} : @measurable_set α ⊥ s ↔ (s = ∅ ∨ s = univ) := let b : measurable_space α := { measurable_set' := λ s, s = ∅ ∨ s = univ, measurable_set_empty := or.inl rfl, measurable_set_compl := by simp [or_imp_distrib] {contextual := tt}, measurable_set_Union := assume f hf, classical.by_cases (assume h : ∃i, f i = univ, let ⟨i, hi⟩ := h in or.inr $ eq_univ_of_univ_subset $ hi ▸ le_supr f i) (assume h : ¬ ∃i, f i = univ, or.inl $ eq_empty_of_subset_empty $ Union_subset $ assume i, (hf i).elim (by simp {contextual := tt}) (assume hi, false.elim $ h ⟨i, hi⟩)) } in have b = ⊥, from bot_unique $ assume s hs, hs.elim (λ s, s.symm ▸ @measurable_set_empty _ ⊥) (λ s, s.symm ▸ @measurable_set.univ _ ⊥), this ▸ iff.rfl @[simp] theorem measurable_set_top {s : set α} : @measurable_set _ ⊤ s := trivial @[simp] theorem measurable_set_inf {m₁ m₂ : measurable_space α} {s : set α} : @measurable_set _ (m₁ ⊓ m₂) s ↔ @measurable_set _ m₁ s ∧ @measurable_set _ m₂ s := iff.rfl @[simp] theorem measurable_set_Inf {ms : set (measurable_space α)} {s : set α} : @measurable_set _ (Inf ms) s ↔ ∀ m ∈ ms, @measurable_set _ m s := show s ∈ (⋂₀ _) ↔ _, by simp @[simp] theorem measurable_set_infi {ι} {m : ι → measurable_space α} {s : set α} : @measurable_set _ (infi m) s ↔ ∀ i, @measurable_set _ (m i) s := by rw [infi, measurable_set_Inf, forall_range_iff] theorem measurable_set_sup {m₁ m₂ : measurable_space α} {s : set α} : measurable_set[m₁ ⊔ m₂] s ↔ generate_measurable (measurable_set[m₁] ∪ measurable_set[m₂]) s := iff.refl _ theorem measurable_set_Sup {ms : set (measurable_space α)} {s : set α} : measurable_set[Sup ms] s ↔ generate_measurable {s : set α | ∃ m ∈ ms, measurable_set[m] s} s := begin change @measurable_set' _ (generate_from $ ⋃₀ _) _ ↔ _, simp [generate_from, ← set_of_exists] end theorem measurable_set_supr {ι} {m : ι → measurable_space α} {s : set α} : @measurable_set _ (supr m) s ↔ generate_measurable {s : set α | ∃ i, measurable_set[m i] s} s := by simp only [supr, measurable_set_Sup, exists_range_iff] lemma measurable_space_supr_eq (m : ι → measurable_space α) : (⨆ n, m n) = generate_from {s | ∃ n, measurable_set[m n] s} := by { ext s, rw measurable_set_supr, refl, } lemma generate_from_Union_measurable_set (m : ι → measurable_space α) : generate_from (⋃ n, {t | measurable_set[m n] t}) = ⨆ n, m n := (@gi_generate_from α).l_supr_u m end complete_lattice end measurable_space section measurable_functions open measurable_space /-- A function `f` between measurable spaces is measurable if the preimage of every measurable set is measurable. -/ def measurable [measurable_space α] [measurable_space β] (f : α → β) : Prop := ∀ ⦃t : set β⦄, measurable_set t → measurable_set (f ⁻¹' t) localized "notation (name := measurable_of) `measurable[` m `]` := @measurable hole! hole! m hole!" in measure_theory lemma measurable_id {ma : measurable_space α} : measurable (@id α) := λ t, id lemma measurable_id' {ma : measurable_space α} : measurable (λ a : α, a) := measurable_id lemma measurable.comp {mα : measurable_space α} {mβ : measurable_space β} {mγ : measurable_space γ} {g : β → γ} {f : α → β} (hg : measurable g) (hf : measurable f) : measurable (g ∘ f) := λ t ht, hf (hg ht) @[simp] lemma measurable_const {ma : measurable_space α} {mb : measurable_space β} {a : α} : measurable (λ b : β, a) := assume s hs, measurable_set.const (a ∈ s) lemma measurable.le {α} {m m0 : measurable_space α} {mb : measurable_space β} (hm : m ≤ m0) {f : α → β} (hf : measurable[m] f) : measurable[m0] f := λ s hs, hm _ (hf hs) lemma measurable_space.top.measurable {α β : Type*} [measurable_space β] (f : α → β) : @measurable α β ⊤ _ f := λ s hs, measurable_space.measurable_set_top end measurable_functions
32ce458b0f4cba4682196daac86324b0f83b3835
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/category_theory/groupoid.lean
9ab9062e12595eb1e7e042b7b94c542df2c70f52
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
1,034
lean
/- Copyright (c) 2018 Reid Barton All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton -/ import category_theory.category import category_theory.isomorphism namespace category_theory universes v u -- declare the `v`'s first; see `category_theory.category` for an explanation class groupoid (obj : Sort u) extends category.{v} obj : Sort (max u (v+1)) := (inv : Π {X Y : obj}, (X ⟶ Y) → (Y ⟶ X)) (inv_comp' : ∀ {X Y : obj} (f : X ⟶ Y), comp (inv f) f = id Y . obviously) (comp_inv' : ∀ {X Y : obj} (f : X ⟶ Y), comp f (inv f) = id X . obviously) restate_axiom groupoid.inv_comp' restate_axiom groupoid.comp_inv' attribute [simp] groupoid.inv_comp groupoid.comp_inv abbreviation large_groupoid (C : Sort (u+1)) : Sort (u+1) := groupoid.{u} C abbreviation small_groupoid (C : Sort u) : Sort (u+1) := groupoid.{u} C instance of_groupoid {C : Sort u} [groupoid.{v} C] {X Y : C} (f : X ⟶ Y) : is_iso f := { inv := groupoid.inv f } end category_theory
a3f2e8727fef8c97d75b26379927941a99ede1b7
3c9dc4ea6cc92e02634ef557110bde9eae393338
/tests/lean/run/infoTree.lean
94eb8c2929f684e5b86f804f8ade96ec76b481f0
[ "Apache-2.0" ]
permissive
shingtaklam1324/lean4
3d7efe0c8743a4e33d3c6f4adbe1300df2e71492
351285a2e8ad0cef37af05851cfabf31edfb5970
refs/heads/master
1,676,827,679,740
1,610,462,623,000
1,610,552,340,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
374
lean
import Lean open Lean.Elab elab "enableInfo!" : command => enableInfoTree elab "showInfoTrees!" : command => do let trees ← getInfoTrees trees.forM fun tree => do IO.println f!"{← tree.format}" enableInfo! def f (x : Nat) : Nat × Nat := let y := ⟨x, x⟩ id y def h : (x : Nat) → x + 0 = x := fun x => by simp exact rfl showInfoTrees!
2ee61de57073e3b4bf263898eaa0f598b1de7a27
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/order/countable_dense_linear_order.lean
c10246e06fca2008eee57e963e3f463224b02563
[ "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
9,620
lean
/- Copyright (c) 2020 David Wärn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: David Wärn -/ import order.ideal /-! # The back and forth method and countable dense linear orders ## Results Suppose `α β` are linear orders, with `α` countable and `β` dense, nonempty, without endpoints. Then there is an order embedding `α ↪ β`. If in addition `α` is dense, nonempty, without endpoints and `β` is countable, then we can upgrade this to an order isomorphism `α ≃ β`. The idea for both results is to consider "partial isomorphisms", which identify a finite subset of `α` with a finite subset of `β`, and prove that for any such partial isomorphism `f` and `a : α`, we can extend `f` to include `a` in its domain. ## References https://en.wikipedia.org/wiki/Back-and-forth_method ## Tags back and forth, dense, countable, order -/ noncomputable theory open_locale classical namespace order /-- Suppose `α` is a nonempty dense linear order without endpoints, and suppose `lo`, `hi`, are finite subssets with all of `lo` strictly before `hi`. Then there is an element of `α` strictly between `lo` and `hi`. -/ lemma exists_between_finsets {α : Type*} [linear_order α] [densely_ordered α] [no_min_order α] [no_max_order α] [nonem : nonempty α] (lo hi : finset α) (lo_lt_hi : ∀ (x ∈ lo) (y ∈ hi), x < y) : ∃ m : α, (∀ x ∈ lo, x < m) ∧ (∀ y ∈ hi, m < y) := if nlo : lo.nonempty then if nhi : hi.nonempty then -- both sets are nonempty, use densely_ordered exists.elim (exists_between (lo_lt_hi _ (finset.max'_mem _ nlo) _ (finset.min'_mem _ nhi))) (λ m hm, ⟨m, λ x hx, lt_of_le_of_lt (finset.le_max' lo x hx) hm.1, λ y hy, lt_of_lt_of_le hm.2 (finset.min'_le hi y hy)⟩) else -- upper set is empty, use `no_max_order` exists.elim (exists_gt (finset.max' lo nlo)) (λ m hm, ⟨m, λ x hx, lt_of_le_of_lt (finset.le_max' lo x hx) hm, λ y hy, (nhi ⟨y, hy⟩).elim⟩) else if nhi : hi.nonempty then -- lower set is empty, use `no_min_order` exists.elim (exists_lt (finset.min' hi nhi)) (λ m hm, ⟨m, λ x hx, (nlo ⟨x, hx⟩).elim, λ y hy, lt_of_lt_of_le hm (finset.min'_le hi y hy)⟩) else -- both sets are empty, use nonempty nonem.elim (λ m, ⟨m, λ x hx, (nlo ⟨x, hx⟩).elim, λ y hy, (nhi ⟨y, hy⟩).elim⟩) variables (α β : Type*) [linear_order α] [linear_order β] /-- The type of partial order isomorphisms between `α` and `β` defined on finite subsets. A partial order isomorphism is encoded as a finite subset of `α × β`, consisting of pairs which should be identified. -/ def partial_iso : Type* := { f : finset (α × β) // ∀ (p q ∈ f), cmp (prod.fst p) (prod.fst q) = cmp (prod.snd p) (prod.snd q) } namespace partial_iso instance : inhabited (partial_iso α β) := ⟨⟨∅, λ p h q, h.elim⟩⟩ instance : preorder (partial_iso α β) := subtype.preorder _ variables {α β} /-- For each `a`, we can find a `b` in the codomain, such that `a`'s relation to the domain of `f` is `b`'s relation to the image of `f`. Thus, if `a` is not already in `f`, then we can extend `f` by sending `a` to `b`. -/ lemma exists_across [densely_ordered β] [no_min_order β] [no_max_order β] [nonempty β] (f : partial_iso α β) (a : α) : ∃ b : β, ∀ (p ∈ f.val), cmp (prod.fst p) a = cmp (prod.snd p) b := begin by_cases h : ∃ b, (a, b) ∈ f.val, { cases h with b hb, exact ⟨b, λ p hp, f.property _ hp _ hb⟩, }, have : ∀ (x ∈ (f.val.filter (λ (p : α × β), p.fst < a)).image prod.snd) (y ∈ (f.val.filter (λ (p : α × β), a < p.fst)).image prod.snd), x < y, { intros x hx y hy, rw finset.mem_image at hx hy, rcases hx with ⟨p, hp1, rfl⟩, rcases hy with ⟨q, hq1, rfl⟩, rw finset.mem_filter at hp1 hq1, rw ←lt_iff_lt_of_cmp_eq_cmp (f.property _ hp1.1 _ hq1.1), exact lt_trans hp1.right hq1.right, }, cases exists_between_finsets _ _ this with b hb, use b, rintros ⟨p1, p2⟩ hp, have : p1 ≠ a := λ he, h ⟨p2, he ▸ hp⟩, cases lt_or_gt_of_ne this with hl hr, { have : p1 < a ∧ p2 < b := ⟨hl, hb.1 _ (finset.mem_image.mpr ⟨(p1, p2), finset.mem_filter.mpr ⟨hp, hl⟩, rfl⟩)⟩, rw [←cmp_eq_lt_iff, ←cmp_eq_lt_iff] at this, cc, }, { have : a < p1 ∧ b < p2 := ⟨hr, hb.2 _ (finset.mem_image.mpr ⟨(p1, p2), finset.mem_filter.mpr ⟨hp, hr⟩, rfl⟩)⟩, rw [←cmp_eq_gt_iff, ←cmp_eq_gt_iff] at this, cc, }, end /-- A partial isomorphism between `α` and `β` is also a partial isomorphism between `β` and `α`. -/ protected def comm : partial_iso α β → partial_iso β α := subtype.map (finset.image (equiv.prod_comm _ _)) $ λ f hf p hp q hq, eq.symm $ hf ((equiv.prod_comm α β).symm p) (by { rw [←finset.mem_coe, finset.coe_image, equiv.image_eq_preimage] at hp, rwa ←finset.mem_coe }) ((equiv.prod_comm α β).symm q) (by { rw [←finset.mem_coe, finset.coe_image, equiv.image_eq_preimage] at hq, rwa ←finset.mem_coe }) variable (β) /-- The set of partial isomorphisms defined at `a : α`, together with a proof that any partial isomorphism can be extended to one defined at `a`. -/ def defined_at_left [densely_ordered β] [no_min_order β] [no_max_order β] [nonempty β] (a : α) : cofinal (partial_iso α β) := { carrier := λ f, ∃ b : β, (a, b) ∈ f.val, mem_gt := begin intro f, cases exists_across f a with b a_b, refine ⟨⟨insert (a, b) f.val, _⟩, ⟨b, finset.mem_insert_self _ _⟩, finset.subset_insert _ _⟩, intros p hp q hq, rw finset.mem_insert at hp hq, rcases hp with rfl | pf; rcases hq with rfl | qf, { simp }, { rw cmp_eq_cmp_symm, exact a_b _ qf }, { exact a_b _ pf }, { exact f.property _ pf _ qf }, end } variables (α) {β} /-- The set of partial isomorphisms defined at `b : β`, together with a proof that any partial isomorphism can be extended to include `b`. We prove this by symmetry. -/ def defined_at_right [densely_ordered α] [no_min_order α] [no_max_order α] [nonempty α] (b : β) : cofinal (partial_iso α β) := { carrier := λ f, ∃ a, (a, b) ∈ f.val, mem_gt := begin intro f, rcases (defined_at_left α b).mem_gt f.comm with ⟨f', ⟨a, ha⟩, hl⟩, use f'.comm, split, { use a, change (a, b) ∈ f'.val.image _, rwa [←finset.mem_coe, finset.coe_image, equiv.image_eq_preimage] }, { change _ ⊆ f'.val.image _, rw [←finset.coe_subset, finset.coe_image, ← equiv.subset_image], change f.val.image _ ⊆ _ at hl, rwa [←finset.coe_subset, finset.coe_image] at hl } end } variable {α} /-- Given an ideal which intersects `defined_at_left β a`, pick `b : β` such that some partial function in the ideal maps `a` to `b`. -/ def fun_of_ideal [densely_ordered β] [no_min_order β] [no_max_order β] [nonempty β] (a : α) (I : ideal (partial_iso α β)) : (∃ f, f ∈ defined_at_left β a ∧ f ∈ I) → { b // ∃ f ∈ I, (a, b) ∈ subtype.val f } := classical.indefinite_description _ ∘ (λ ⟨f, ⟨b, hb⟩, hf⟩, ⟨b, f, hf, hb⟩) /-- Given an ideal which intersects `defined_at_right α b`, pick `a : α` such that some partial function in the ideal maps `a` to `b`. -/ def inv_of_ideal [densely_ordered α] [no_min_order α] [no_max_order α] [nonempty α] (b : β) (I : ideal (partial_iso α β)) : (∃ f, f ∈ defined_at_right α b ∧ f ∈ I) → { a // ∃ f ∈ I, (a, b) ∈ subtype.val f } := classical.indefinite_description _ ∘ (λ ⟨f, ⟨a, ha⟩, hf⟩, ⟨a, f, hf, ha⟩) end partial_iso open partial_iso variables (α β) /-- Any countable linear order embeds in any nonempty dense linear order without endpoints. -/ def embedding_from_countable_to_dense [encodable α] [densely_ordered β] [no_min_order β] [no_max_order β] [nonempty β] : α ↪o β := let our_ideal : ideal (partial_iso α β) := ideal_of_cofinals default $ defined_at_left β in let F := λ a, fun_of_ideal a our_ideal (cofinal_meets_ideal_of_cofinals _ _ a) in order_embedding.of_strict_mono (λ a, (F a).val) begin intros a₁ a₂, rcases (F a₁).property with ⟨f, hf, ha₁⟩, rcases (F a₂).property with ⟨g, hg, ha₂⟩, rcases our_ideal.directed _ hf _ hg with ⟨m, hm, fm, gm⟩, exact (lt_iff_lt_of_cmp_eq_cmp $ m.property (a₁, _) (fm ha₁) (a₂, _) (gm ha₂)).mp end /-- Any two countable dense, nonempty linear orders without endpoints are order isomorphic. -/ def iso_of_countable_dense [encodable α] [densely_ordered α] [no_min_order α] [no_max_order α] [nonempty α] [encodable β] [densely_ordered β] [no_min_order β] [no_max_order β] [nonempty β] : α ≃o β := let to_cofinal : α ⊕ β → cofinal (partial_iso α β) := λ p, sum.rec_on p (defined_at_left β) (defined_at_right α) in let our_ideal : ideal (partial_iso α β) := ideal_of_cofinals default to_cofinal in let F := λ a, fun_of_ideal a our_ideal (cofinal_meets_ideal_of_cofinals _ to_cofinal (sum.inl a)) in let G := λ b, inv_of_ideal b our_ideal (cofinal_meets_ideal_of_cofinals _ to_cofinal (sum.inr b)) in order_iso.of_cmp_eq_cmp (λ a, (F a).val) (λ b, (G b).val) begin intros a b, rcases (F a).property with ⟨f, hf, ha⟩, rcases (G b).property with ⟨g, hg, hb⟩, rcases our_ideal.directed _ hf _ hg with ⟨m, hm, fm, gm⟩, exact m.property (a, _) (fm ha) (_, b) (gm hb) end end order
446ef9a4dee69a181c88242f1faf0ad6a90e6eec
1d335ec6ac4181a0a762b12797936770cc9fcef9
/expressions/classical_geometry.lean
859d77ea6c013b4242a49be281fab738456eaeec
[]
no_license
rohanrajnair/lang
dbadcc3997e44245ca84d48dc1733cf09a2605a6
3beb4e29d8faa692983a55fa18acb6eb947134e8
refs/heads/master
1,672,640,379,424
1,597,948,771,000
1,597,948,771,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
463
lean
import ...phys.src.classical_geometry namespace lang.classicalGeometry structure var : Type := mk :: (num : ℕ) def varEq : var → var → bool | v1 v2 := v1.num = v2.num -- implicit coercion here inductive expr | lit (s : classicalGeometry) : expr | var (v : var) : expr --| GeometricProduct (e1 e2 : classicalGeometryexpr) : classicalGeometryexpr def env := (var → classicalGeometry) def init := λ v : var, worldGeometry end lang.classicalGeometry
ea952ce661bd2f3e6c613d5d4d994309d5b7c6a7
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/interactive/config.lean
9a35fa712aa1f2a779ad4022a2483c318d790e17
[ "Apache-2.0" ]
permissive
codyroux/lean0.1
1ce92751d664aacff0529e139083304a7bbc8a71
0dc6fb974aa85ed6f305a2f4b10a53a44ee5f0ef
refs/heads/master
1,610,830,535,062
1,402,150,480,000
1,402,150,480,000
19,588,851
2
0
null
null
null
null
UTF-8
Lean
false
false
103
lean
-- set_option default configuration for tests set_option pp::colors false set_option pp::unicode true
1ffd71d1896df84256d54e415a7ffe87a89aadc3
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/sites/induced_topology.lean
8c54a1624e1317105bbd714abe8df530d9d24d21
[ "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,717
lean
/- Copyright (c) 2021 Andrew Yang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Andrew Yang -/ import category_theory.sites.dense_subsite /-! # Induced Topology > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. We say that a functor `G : C ⥤ (D, K)` is locally dense if for each covering sieve `T` in `D` of some `X : C`, `T ∩ mor(C)` generates a covering sieve of `X` in `D`. A locally dense fully faithful functor then induces a topology on `C` via `{ T ∩ mor(C) | T ∈ K }`. Note that this is equal to the collection of sieves on `C` whose image generates a covering sieve. This construction would make `C` both cover-lifting and cover-preserving. Some typical examples are full and cover-dense functors (for example the functor from a basis of a topological space `X` into `opens X`). The functor `over X ⥤ C` is also locally dense, and the induced topology can then be used to construct the big sites associated to a scheme. Given a fully faithful cover-dense functor `G : C ⥤ (D, K)` between small sites, we then have `Sheaf (H.induced_topology) A ≌ Sheaf K A`. This is known as the comparison lemma. ## References * [Elephant]: *Sketches of an Elephant*, P. T. Johnstone: C2.2. * https://ncatlab.org/nlab/show/dense+sub-site * https://ncatlab.org/nlab/show/comparison+lemma -/ namespace category_theory universes v u open limits opposite presieve section variables {C : Type*} [category C] {D : Type*} [category D] {G : C ⥤ D} variables {J : grothendieck_topology C} {K : grothendieck_topology D} variables (A : Type v) [category.{u} A] -- variables (A) [full G] [faithful G] /-- We say that a functor `C ⥤ D` into a site is "locally dense" if for each covering sieve `T` in `D`, `T ∩ mor(C)` generates a covering sieve in `D`. -/ def locally_cover_dense (K : grothendieck_topology D) (G : C ⥤ D) : Prop := ∀ ⦃X⦄ (T : K (G.obj X)), (T.val.functor_pullback G).functor_pushforward G ∈ K (G.obj X) namespace locally_cover_dense variables [full G] [faithful G] (Hld : locally_cover_dense K G) include Hld lemma pushforward_cover_iff_cover_pullback {X : C} (S : sieve X) : K _ (S.functor_pushforward G) ↔ ∃ (T : K (G.obj X)), T.val.functor_pullback G = S := begin split, { intros hS, exact ⟨⟨_, hS⟩, (sieve.fully_faithful_functor_galois_coinsertion G X).u_l_eq S⟩ }, { rintros ⟨T, rfl⟩, exact Hld T } end /-- If a functor `G : C ⥤ (D, K)` is fully faithful and locally dense, then the set `{ T ∩ mor(C) | T ∈ K }` is a grothendieck topology of `C`. -/ @[simps] def induced_topology : grothendieck_topology C := { sieves := λ X S, K _ (S.functor_pushforward G), top_mem' := λ X, by { change K _ _, rw sieve.functor_pushforward_top, exact K.top_mem _ }, pullback_stable' := λ X Y S f hS, begin have : S.pullback f = ((S.functor_pushforward G).pullback (G.map f)).functor_pullback G, { conv_lhs { rw ← (sieve.fully_faithful_functor_galois_coinsertion G X).u_l_eq S }, ext, change (S.functor_pushforward G) _ ↔ (S.functor_pushforward G) _, rw G.map_comp }, rw this, change K _ _, apply Hld ⟨_, K.pullback_stable (G.map f) hS⟩ end, transitive' := λ X S hS S' H', begin apply K.transitive hS, rintros Y _ ⟨Z, g, i, hg, rfl⟩, rw sieve.pullback_comp, apply K.pullback_stable i, refine K.superset_covering _ (H' hg), rintros W _ ⟨Z', g', i', hg, rfl⟩, use ⟨Z', g' ≫ g, i', hg, by simp⟩ end } /-- `G` is cover-lifting wrt the induced topology. -/ lemma induced_topology_cover_lifting : cover_lifting Hld.induced_topology K G := ⟨λ _ S hS, Hld ⟨S, hS⟩⟩ /-- `G` is cover-preserving wrt the induced topology. -/ lemma induced_topology_cover_preserving : cover_preserving Hld.induced_topology K G := ⟨λ _ S hS, hS⟩ end locally_cover_dense lemma cover_dense.locally_cover_dense [full G] (H : cover_dense K G) : locally_cover_dense K G := begin intros X T, refine K.superset_covering _ (K.bind_covering T.property (λ Y f Hf, H.is_cover Y)), rintros Y _ ⟨Z, _, f, hf, ⟨W, g, f', (rfl : _ = _)⟩, rfl⟩, use W, use G.preimage (f' ≫ f), use g, split, simpa using T.val.downward_closed hf f', simp, end /-- Given a fully faithful cover-dense functor `G : C ⥤ (D, K)`, we may induce a topology on `C`. -/ abbreviation cover_dense.induced_topology [full G] [faithful G] (H : cover_dense K G) : grothendieck_topology C := H.locally_cover_dense.induced_topology variable (J) lemma over_forget_locally_cover_dense (X : C) : locally_cover_dense J (over.forget X) := begin intros Y T, convert T.property, ext Z f, split, { rintros ⟨_, _, g', hg, rfl⟩, exact T.val.downward_closed hg g' }, { intros hf, exact ⟨over.mk (f ≫ Y.hom), over.hom_mk f, 𝟙 _, hf, (category.id_comp _).symm⟩ } end end section small_site variables {C : Type v} [small_category C] {D : Type v} [small_category D] {G : C ⥤ D} variables {J : grothendieck_topology C} {K : grothendieck_topology D} variables (A : Type u) [category.{v} A] /-- Cover-dense functors induces an equivalence of categories of sheaves. This is known as the comparison lemma. It requires that the sites are small and the value category is complete. -/ noncomputable def cover_dense.Sheaf_equiv [full G] [faithful G] (H : cover_dense K G) [has_limits A] : Sheaf H.induced_topology A ≌ Sheaf K A := H.Sheaf_equiv_of_cover_preserving_cover_lifting H.locally_cover_dense.induced_topology_cover_preserving H.locally_cover_dense.induced_topology_cover_lifting end small_site end category_theory
a38fb799be1930b0c377221f2ae79612cd2dd59b
88fb7558b0636ec6b181f2a548ac11ad3919f8a5
/library/init/data/nat/pow.lean
0f4979879fc0fcf766f2cfb7d2329312c64de56f
[ "Apache-2.0" ]
permissive
moritayasuaki/lean
9f666c323cb6fa1f31ac597d777914aed41e3b7a
ae96ebf6ee953088c235ff7ae0e8c95066ba8001
refs/heads/master
1,611,135,440,814
1,493,852,869,000
1,493,852,869,000
90,269,903
0
0
null
1,493,906,291,000
1,493,906,291,000
null
UTF-8
Lean
false
false
436
lean
/- Copyright (c) 2017 Galois Inc. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Simon Hudon exponentiation on natural numbers This is a work-in-progress -/ prelude import init.data.nat.basic init.meta namespace nat def pow (b : ℕ) : ℕ → ℕ | 0 := 1 | (succ n) := pow n * b infix `^` := pow @[simp] lemma pow_succ (b n : ℕ) : b^(succ n) = b^n * b := rfl end nat
cb59b806c87170152048f5ea7d2bdef6168d1fa5
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/IO_test.lean
ba90095533f0a3e394b5263ca4d1ec71bbb136df
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,961
lean
prelude import Init.System.IO import Init.Data.List.Control new_frontend open IO.FS instance : HasRepr UInt8 := ⟨ toString ⟩ def check_eq {α} [HasBeq α] [HasRepr α] (tag : String) (expected actual : α) : IO Unit := «unless» (expected == actual) $ throw $ IO.userError $ "assertion failure \"" ++ tag ++ "\":\n expected: " ++ repr expected ++ "\n actual: " ++ repr actual def test : IO Unit := do let xs : ByteArray := ⟨#[1,2,3,4]⟩; let fn := "foo.txt"; withFile fn Mode.write fun h => do h.write xs; h.write xs; pure (); let ys ← withFile "foo.txt" Mode.read $ fun h => h.read 10; check_eq "1" (xs.toList ++ xs.toList) ys.toList; withFile fn Mode.append fun h => do h.write ⟨#[5,6,7,8]⟩; pure (); withFile "foo.txt" Mode.read fun h => do let ys ← h.read 10; check_eq "2" [1,2,3,4,1,2,3,4,5,6] ys.toList; let ys ← h.read 2; check_eq "3" [7,8] ys.toList; let b ← h.isEof; «unless» (!b) (throw $ IO.userError $ "wrong (4): "); let ys ← h.read 2; check_eq "5" [] ys.toList; let b ← h.isEof; «unless» b (throw $ IO.userError $ "wrong (6): "); pure () #eval test def test2 : IO Unit := do let fn2 := "foo2.txt"; let xs₀ : String := "⟨[₂,α]⟩"; let xs₁ := "⟨[6,8,@]⟩"; let xs₂ := "/* Handle.getLine : Handle → IO Unit */" ++ "/* The line returned by `lean_io_prim_handle_get_line` */" ++ "/* is truncated at the first \'\\0\' character and the */" ++ "/* rest of the line is discarded. */"; -- multi-buffer line withFile fn2 Mode.write $ fun h => pure (); withFile fn2 Mode.write $ fun h => do { h.putStr xs₀; h.putStrLn xs₀; h.putStrLn xs₂; h.putStrLn xs₁; pure () }; let ys ← withFile fn2 Mode.read $ fun h => h.getLine; IO.println ys; check_eq "1" (xs₀ ++ xs₀ ++ "\n") ys; IO.println ys; withFile fn2 Mode.append $ fun h => do { h.putStrLn xs₁; pure () }; let ys ← withFile fn2 Mode.read $ fun h => do { let ys ← (List.iota 4).mapM $ fun i => do { let ln ← h.getLine; IO.println i; IO.println ∘ repr $ ln; let b ← h.isEof; «unless» (i == 1 || !b) (throw $ IO.userError "isEof"); pure ln }; pure ys }; IO.println ys; let rs := [xs₀ ++ xs₀ ++ "\n", xs₂ ++ "\n", xs₁ ++ "\n", xs₁ ++ "\n"]; check_eq "2" rs ys; let ys ← readFile fn2; check_eq "3" (String.join rs) ys; pure () #eval test2 def test3 : IO Unit := do let fn3 := "foo3.txt" let xs₀ := "abc" let xs₁ := "" let xs₂ := "hello" let xs₃ := "world" withFile fn3 Mode.write $ fun h => do { pure () } let ys ← lines fn3 IO.println $ repr ys check_eq "1" ys #[] withFile fn3 Mode.write $ fun h => do h.putStrLn xs₀ h.putStrLn xs₁ h.putStrLn xs₂ h.putStrLn xs₃ let ys ← lines fn3 IO.println $ repr ys check_eq "2" ys #[xs₀, xs₁, xs₂, xs₃] pure () #eval test3
c2f8cd475622da094fca3fbf9e8cc2bb4a2b25a3
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/topology/metric_space/antilipschitz.lean
c1e657a6dd7c5d8ca632e8b20f91caff22d06b42
[]
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,293
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.topology.metric_space.lipschitz import Mathlib.PostPort universes u_1 u_2 u_3 namespace Mathlib /-! # Antilipschitz functions We say that a map `f : α → β` between two (extended) metric spaces is `antilipschitz_with K`, `K ≥ 0`, if for all `x, y` we have `edist x y ≤ K * edist (f x) (f y)`. For a metric space, the latter inequality is equivalent to `dist x y ≤ K * dist (f x) (f y)`. ## Implementation notes The parameter `K` has type `ℝ≥0`. This way we avoid conjuction in the definition and have coercions both to `ℝ` and `ennreal`. We do not require `0 < K` in the definition, mostly because we do not have a `posreal` type. -/ /-- We say that `f : α → β` is `antilipschitz_with K` if for any two points `x`, `y` we have `K * edist x y ≤ edist (f x) (f y)`. -/ def antilipschitz_with {α : Type u_1} {β : Type u_2} [emetric_space α] [emetric_space β] (K : nnreal) (f : α → β) := ∀ (x y : α), edist x y ≤ ↑K * edist (f x) (f y) theorem antilipschitz_with_iff_le_mul_dist {α : Type u_1} {β : Type u_2} [metric_space α] [metric_space β] {K : nnreal} {f : α → β} : antilipschitz_with K f ↔ ∀ (x y : α), dist x y ≤ ↑K * dist (f x) (f y) := sorry theorem antilipschitz_with.of_le_mul_dist {α : Type u_1} {β : Type u_2} [metric_space α] [metric_space β] {K : nnreal} {f : α → β} : (∀ (x y : α), dist x y ≤ ↑K * dist (f x) (f y)) → antilipschitz_with K f := iff.mpr antilipschitz_with_iff_le_mul_dist theorem antilipschitz_with.mul_le_dist {α : Type u_1} {β : Type u_2} [metric_space α] [metric_space β] {K : nnreal} {f : α → β} (hf : antilipschitz_with K f) (x : α) (y : α) : ↑(K⁻¹) * dist x y ≤ dist (f x) (f y) := sorry namespace antilipschitz_with /-- Extract the constant from `hf : antilipschitz_with K f`. This is useful, e.g., if `K` is given by a long formula, and we want to reuse this value. -/ protected def K {α : Type u_1} {β : Type u_2} [emetric_space α] [emetric_space β] {K : nnreal} {f : α → β} (hf : antilipschitz_with K f) : nnreal := K protected theorem injective {α : Type u_1} {β : Type u_2} [emetric_space α] [emetric_space β] {K : nnreal} {f : α → β} (hf : antilipschitz_with K f) : function.injective f := sorry theorem mul_le_edist {α : Type u_1} {β : Type u_2} [emetric_space α] [emetric_space β] {K : nnreal} {f : α → β} (hf : antilipschitz_with K f) (x : α) (y : α) : ↑(K⁻¹) * edist x y ≤ edist (f x) (f y) := sorry protected theorem id {α : Type u_1} [emetric_space α] : antilipschitz_with 1 id := sorry theorem comp {α : Type u_1} {β : Type u_2} {γ : Type u_3} [emetric_space α] [emetric_space β] [emetric_space γ] {Kg : nnreal} {g : β → γ} (hg : antilipschitz_with Kg g) {Kf : nnreal} {f : α → β} (hf : antilipschitz_with Kf f) : antilipschitz_with (Kf * Kg) (g ∘ f) := sorry theorem restrict {α : Type u_1} {β : Type u_2} [emetric_space α] [emetric_space β] {K : nnreal} {f : α → β} (hf : antilipschitz_with K f) (s : set α) : antilipschitz_with K (set.restrict f s) := fun (x y : ↥s) => hf ↑x ↑y theorem cod_restrict {α : Type u_1} {β : Type u_2} [emetric_space α] [emetric_space β] {K : nnreal} {f : α → β} (hf : antilipschitz_with K f) {s : set β} (hs : ∀ (x : α), f x ∈ s) : antilipschitz_with K (set.cod_restrict f s hs) := fun (x y : α) => hf x y theorem to_right_inv_on' {α : Type u_1} {β : Type u_2} [emetric_space α] [emetric_space β] {K : nnreal} {f : α → β} {s : set α} (hf : antilipschitz_with K (set.restrict f s)) {g : β → α} {t : set β} (g_maps : set.maps_to g t s) (g_inv : set.right_inv_on g f t) : lipschitz_with K (set.restrict g t) := sorry theorem to_right_inv_on {α : Type u_1} {β : Type u_2} [emetric_space α] [emetric_space β] {K : nnreal} {f : α → β} (hf : antilipschitz_with K f) {g : β → α} {t : set β} (h : set.right_inv_on g f t) : lipschitz_with K (set.restrict g t) := to_right_inv_on' (restrict hf set.univ) (set.maps_to_univ g t) h theorem to_right_inverse {α : Type u_1} {β : Type u_2} [emetric_space α] [emetric_space β] {K : nnreal} {f : α → β} (hf : antilipschitz_with K f) {g : β → α} (hg : function.right_inverse g f) : lipschitz_with K g := sorry theorem uniform_embedding {α : Type u_1} {β : Type u_2} [emetric_space α] [emetric_space β] {K : nnreal} {f : α → β} (hf : antilipschitz_with K f) (hfc : uniform_continuous f) : uniform_embedding f := sorry theorem subtype_coe {α : Type u_1} [emetric_space α] (s : set α) : antilipschitz_with 1 coe := restrict antilipschitz_with.id s theorem of_subsingleton {α : Type u_1} {β : Type u_2} [emetric_space α] [emetric_space β] {f : α → β} [subsingleton α] {K : nnreal} : antilipschitz_with K f := sorry end antilipschitz_with theorem lipschitz_with.to_right_inverse {α : Type u_1} {β : Type u_2} [emetric_space α] [emetric_space β] {K : nnreal} {f : α → β} (hf : lipschitz_with K f) {g : β → α} (hg : function.right_inverse g f) : antilipschitz_with K g := sorry
95551c5a7101720ea45781dbdd1ae44951929060
947b78d97130d56365ae2ec264df196ce769371a
/stage0/src/Lean/Util/Path.lean
2c65b642d861d0a547cf718233756c43aef22b18
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,416
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich Management of the Lean search path (`LEAN_PATH`), which is a list of paths containing package roots: an import `A.B.C` resolves to `path/A/B/C.olean` for the first entry `path` in `LEAN_PATH` with a directory `A/`. `import A` resolves to `path/A.olean`. -/ import Lean.Data.Name namespace Lean open System.FilePath (pathSeparator extSeparator) private def pathSep : String := toString pathSeparator def realPathNormalized (fname : String) : IO String := do fname ← IO.realPath fname; pure (System.FilePath.normalizePath fname) abbrev SearchPath := List String def mkSearchPathRef : IO (IO.Ref SearchPath) := IO.mkRef ∅ @[init mkSearchPathRef] constant searchPathRef : IO.Ref SearchPath := arbitrary _ def parseSearchPath (path : String) (sp : SearchPath := ∅) : IO SearchPath := pure $ System.FilePath.splitSearchPath path ++ sp @[extern c inline "LEAN_IS_STAGE0"] constant isStage0 (u : Unit) : Bool := arbitrary _ def getBuiltinSearchPath : IO SearchPath := do appDir ← IO.appDir; -- use stage1 stdlib with stage0 executable (which should never be distributed outside of the build directory) pure [appDir ++ pathSep ++ ".." ++ (if isStage0 () then pathSep ++ ".." ++ pathSep ++ "stage1" else "") ++ pathSep ++ "lib" ++ pathSep ++ "lean"] def addSearchPathFromEnv (sp : SearchPath) : IO SearchPath := do val ← IO.getEnv "LEAN_PATH"; match val with | none => pure sp | some val => parseSearchPath val sp @[export lean_init_search_path] def initSearchPath (path : Option String := none) : IO Unit := match path with | some path => parseSearchPath path >>= searchPathRef.set | none => do sp ← getBuiltinSearchPath; sp ← addSearchPathFromEnv sp; searchPathRef.set sp def modPathToFilePath : Name → String | Name.str p h _ => modPathToFilePath p ++ pathSep ++ h | Name.anonymous => "" | Name.num p _ _ => panic! "ill-formed import" def findOLean (mod : Name) : IO String := do sp ← searchPathRef.get; let pkg := mod.getRoot.toString; some root ← sp.findM? (fun path => IO.isDir $ path ++ pathSep ++ pkg) | throw $ IO.userError $ "unknown package '" ++ pkg ++ "'"; pure $ root ++ modPathToFilePath mod ++ ".olean" /-- Infer module name of source file name. -/ @[export lean_module_name_of_file] def moduleNameOfFileName (fname : String) (rootDir : Option String) : IO Name := do fname ← realPathNormalized fname; rootDir ← match rootDir with | some rootDir => pure rootDir | none => IO.currentDir; rootDir ← realPathNormalized rootDir; when (!rootDir.isPrefixOf fname) $ throw $ IO.userError $ "input file '" ++ fname ++ "' must be contained in root directory (" ++ rootDir ++ ")"; let fnameSuffix := fname.drop rootDir.length; let fnameSuffix := if fnameSuffix.get 0 == pathSeparator then fnameSuffix.drop 1 else fnameSuffix; some extPos ← pure (fnameSuffix.revPosOf '.') | throw (IO.userError ("failed to convert file name '" ++ fname ++ "' to module name, extension is missing")); let modNameStr := fnameSuffix.extract 0 extPos; let extStr := fnameSuffix.extract (extPos + 1) fnameSuffix.bsize; let parts := modNameStr.splitOn pathSep; let modName := parts.foldl mkNameStr Name.anonymous; pure modName end Lean
5ecab4e554daf7a98022a1dee6c3dcc40aa560cf
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/analysis/normed_space/units.lean
a74dd44616bfa2cafdfce4611d78ddaa16f0914f
[ "Apache-2.0" ]
permissive
dupuisf/mathlib
62de4ec6544bf3b79086afd27b6529acfaf2c1bb
8582b06b0a5d06c33ee07d0bdf7c646cae22cf36
refs/heads/master
1,669,494,854,016
1,595,692,409,000
1,595,692,409,000
272,046,630
0
0
Apache-2.0
1,592,066,143,000
1,592,066,142,000
null
UTF-8
Lean
false
false
3,398
lean
/- Copyright (c) 2020 Heather Macbeth. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Heather Macbeth -/ import analysis.specific_limits /-! # The group of units of a complete normed ring This file contains the basic theory for the group of units (invertible elements) of a complete normed ring (Banach algebras being a notable special case). ## Main results The constructions `one_sub`, `add` and `unit_of_nearby` state, in varying forms, that perturbations of a unit are units. The latter two are not stated in their optimal form; more precise versions would use the spectral radius. The main result is `is_open`: the group of units of a complete normed ring is an open subset of the ring. -/ noncomputable theory namespace units variables {R : Type*} [normed_ring R] [complete_space R] /-- In a complete normed ring, a perturbation of `1` by an element `t` of distance less than `1` from `1` is a unit. Here we construct its `units` structure. -/ def one_sub (t : R) (h : ∥t∥ < 1) : units R := { val := 1 - t, inv := ∑' (n : ℕ), t ^ n, val_inv := mul_neg_geom_series t h, inv_val := geom_series_mul_neg t h } @[simp] lemma one_sub_coe (t : R) (h : ∥t∥ < 1) : ((one_sub t h) : R) = 1 - t := rfl /-- In a complete normed ring, a perturbation of a unit `x` by an element `t` of distance less than `∥x⁻¹∥⁻¹` from `x` is a unit. Here we construct its `units` structure. -/ def add (x : units R) (t : R) (h : ∥t∥ < ∥((x⁻¹:units R):R)∥⁻¹) : units R := x * (units.one_sub (- (((x⁻¹:units R):R) * t)) begin rcases subsingleton_or_nontrivial R with _i|_i; resetI, { rw subsingleton.elim ((x⁻¹:units R):R) 0, have : (0:ℝ) < 1 := by norm_num, simpa, }, { have hpos : 0 < ∥((x⁻¹:units R):R)∥ := units.norm_pos x⁻¹, calc ∥-(((x⁻¹:units R):R) * t)∥ = ∥((x⁻¹:units R):R) * t∥ : by { rw norm_neg } ... ≤ ∥((x⁻¹:units R):R)∥ * ∥t∥ : norm_mul_le x.inv _ ... < ∥((x⁻¹:units R):R)∥ * ∥((x⁻¹:units R):R)∥⁻¹ : by nlinarith only [h, hpos] ... = 1 : mul_inv_cancel (ne_of_gt hpos) }, end ) @[simp] lemma add_coe (x : units R) (t : R) (h : ∥t∥ < ∥((x⁻¹:units R):R)∥⁻¹) : ((x.add t h) : R) = x + t := by { unfold units.add, simp [mul_add] } /-- In a complete normed ring, an element `y` of distance less than `∥x⁻¹∥⁻¹` from `x` is a unit. Here we construct its `units` structure. -/ def unit_of_nearby (x : units R) (y : R) (h : ∥y - x∥ < ∥((x⁻¹:units R):R)∥⁻¹) : units R := x.add ((y:R) - x) h @[simp] lemma unit_of_nearby_coe (x : units R) (y : R) (h : ∥y - x∥ < ∥((x⁻¹:units R):R)∥⁻¹) : ((x.unit_of_nearby y h) : R) = y := by { unfold units.unit_of_nearby, simp } /-- The group of units of a complete normed ring is an open subset of the ring. -/ lemma is_open : is_open {x : R | is_unit x} := begin rcases subsingleton_or_nontrivial R with _i|_i; resetI, { exact is_open_discrete is_unit }, { apply metric.is_open_iff.mpr, rintros x' ⟨x, h⟩, refine ⟨∥((x⁻¹:units R):R)∥⁻¹, inv_pos.mpr (units.norm_pos x⁻¹), _⟩, intros y hy, rw [metric.mem_ball, dist_eq_norm, ←h] at hy, use x.unit_of_nearby y hy, simp } end end units
c252f47eb6c146aa16036782468a0b6bc5f6c023
4fa161becb8ce7378a709f5992a594764699e268
/src/topology/instances/ennreal.lean
b8cd8429a00af08f1b88a128ece1b5a215e40663
[ "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
38,823
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes Hölzl -/ import topology.instances.nnreal /-! # Extended non-negative reals -/ noncomputable theory open classical set filter metric open_locale classical open_locale topological_space variables {α : Type*} {β : Type*} {γ : Type*} open_locale ennreal big_operators namespace ennreal variables {a b c d : ennreal} {r p q : nnreal} variables {x y z : ennreal} {ε ε₁ ε₂ : ennreal} {s : set ennreal} section topological_space open topological_space /-- Topology on `ennreal`. Note: this is different from the `emetric_space` topology. The `emetric_space` topology has `is_open {⊤}`, while this topology doesn't have singleton elements. -/ instance : topological_space ennreal := preorder.topology ennreal instance : order_topology ennreal := ⟨rfl⟩ instance : t2_space ennreal := by apply_instance -- short-circuit type class inference instance : second_countable_topology ennreal := ⟨⟨⋃q ≥ (0:ℚ), {{a : ennreal | a < nnreal.of_real q}, {a : ennreal | ↑(nnreal.of_real q) < a}}, (countable_encodable _).bUnion $ assume a ha, (countable_singleton _).insert _, le_antisymm (le_generate_from $ by simp [or_imp_distrib, is_open_lt', is_open_gt'] {contextual := tt}) (le_generate_from $ λ s h, begin rcases h with ⟨a, hs | hs⟩; [ rw show s = ⋃q∈{q:ℚ | 0 ≤ q ∧ a < nnreal.of_real q}, {b | ↑(nnreal.of_real q) < b}, from set.ext (assume b, by simp [hs, @ennreal.lt_iff_exists_rat_btwn a b, and_assoc]), rw show s = ⋃q∈{q:ℚ | 0 ≤ q ∧ ↑(nnreal.of_real q) < a}, {b | b < ↑(nnreal.of_real q)}, from set.ext (assume b, by simp [hs, @ennreal.lt_iff_exists_rat_btwn b a, and_comm, and_assoc])]; { apply is_open_Union, intro q, apply is_open_Union, intro hq, exact generate_open.basic _ (mem_bUnion hq.1 $ by simp) } end)⟩⟩ lemma embedding_coe : embedding (coe : nnreal → ennreal) := ⟨⟨begin refine le_antisymm _ _, { rw [@order_topology.topology_eq_generate_intervals ennreal _, ← coinduced_le_iff_le_induced], refine le_generate_from (assume s ha, _), rcases ha with ⟨a, rfl | rfl⟩, show is_open {b : nnreal | a < ↑b}, { cases a; simp [none_eq_top, some_eq_coe, is_open_lt'] }, show is_open {b : nnreal | ↑b < a}, { cases a; simp [none_eq_top, some_eq_coe, is_open_gt', is_open_const] } }, { rw [@order_topology.topology_eq_generate_intervals nnreal _], refine le_generate_from (assume s ha, _), rcases ha with ⟨a, rfl | rfl⟩, exact ⟨Ioi a, is_open_Ioi, by simp [Ioi]⟩, exact ⟨Iio a, is_open_Iio, by simp [Iio]⟩ } end⟩, assume a b, coe_eq_coe.1⟩ lemma is_open_ne_top : is_open {a : ennreal | a ≠ ⊤} := is_open_ne lemma is_open_Ico_zero : is_open (Ico 0 b) := by { rw ennreal.Ico_eq_Iio, exact is_open_Iio} lemma coe_range_mem_nhds : range (coe : nnreal → ennreal) ∈ 𝓝 (r : ennreal) := have {a : ennreal | a ≠ ⊤} = range (coe : nnreal → ennreal), from set.ext $ assume a, by cases a; simp [none_eq_top, some_eq_coe], this ▸ mem_nhds_sets is_open_ne_top coe_ne_top @[norm_cast] lemma tendsto_coe {f : filter α} {m : α → nnreal} {a : nnreal} : tendsto (λa, (m a : ennreal)) f (𝓝 ↑a) ↔ tendsto m f (𝓝 a) := embedding_coe.tendsto_nhds_iff.symm lemma continuous_coe {α} [topological_space α] {f : α → nnreal} : continuous (λa, (f a : ennreal)) ↔ continuous f := embedding_coe.continuous_iff.symm lemma nhds_coe {r : nnreal} : 𝓝 (r : ennreal) = (𝓝 r).map coe := by rw [embedding_coe.induced, map_nhds_induced_eq coe_range_mem_nhds] lemma nhds_coe_coe {r p : nnreal} : 𝓝 ((r : ennreal), (p : ennreal)) = (𝓝 (r, p)).map (λp:nnreal×nnreal, (p.1, p.2)) := begin rw [(embedding_coe.prod_mk embedding_coe).map_nhds_eq], rw [← prod_range_range_eq], exact prod_mem_nhds_sets coe_range_mem_nhds coe_range_mem_nhds end lemma continuous_of_real : continuous ennreal.of_real := (continuous_coe.2 continuous_id).comp nnreal.continuous_of_real lemma tendsto_of_real {f : filter α} {m : α → ℝ} {a : ℝ} (h : tendsto m f (𝓝 a)) : tendsto (λa, ennreal.of_real (m a)) f (𝓝 (ennreal.of_real a)) := tendsto.comp (continuous.tendsto continuous_of_real _) h lemma tendsto_to_nnreal {a : ennreal} : a ≠ ⊤ → tendsto (ennreal.to_nnreal) (𝓝 a) (𝓝 a.to_nnreal) := begin cases a; simp [some_eq_coe, none_eq_top, nhds_coe, tendsto_map'_iff, (∘)], exact tendsto_id end lemma continuous_on_to_nnreal : continuous_on ennreal.to_nnreal {a | a ≠ ∞} := continuous_on_iff_continuous_restrict.2 $ continuous_iff_continuous_at.2 $ λ x, (tendsto_to_nnreal x.2).comp continuous_at_subtype_val lemma tendsto_to_real {a : ennreal} : a ≠ ⊤ → tendsto (ennreal.to_real) (𝓝 a) (𝓝 a.to_real) := λ ha, tendsto.comp ((@nnreal.tendsto_coe _ (𝓝 a.to_nnreal) id (a.to_nnreal)).2 tendsto_id) (tendsto_to_nnreal ha) lemma tendsto_nhds_top {m : α → ennreal} {f : filter α} (h : ∀ n : ℕ, ∀ᶠ a in f, ↑n < m a) : tendsto m f (𝓝 ⊤) := tendsto_nhds_generate_from $ assume s hs, match s, hs with | _, ⟨none, or.inl rfl⟩, hr := (lt_irrefl ⊤ hr).elim | _, ⟨some r, or.inl rfl⟩, hr := let ⟨n, hrn⟩ := exists_nat_gt r in mem_sets_of_superset (h n) $ assume a hnma, show ↑r < m a, from lt_trans (show (r : ennreal) < n, from (coe_nat n) ▸ coe_lt_coe.2 hrn) hnma | _, ⟨a, or.inr rfl⟩, hr := (not_top_lt $ show ⊤ < a, from hr).elim end lemma tendsto_nat_nhds_top : tendsto (λ n : ℕ, ↑n) at_top (𝓝 ∞) := tendsto_nhds_top $ λ n, mem_at_top_sets.2 ⟨n+1, λ m hm, ennreal.coe_nat_lt_coe_nat.2 $ nat.lt_of_succ_le hm⟩ lemma nhds_top : 𝓝 ∞ = ⨅a ≠ ∞, principal (Ioi a) := nhds_top_order.trans $ by simp [lt_top_iff_ne_top, Ioi] lemma nhds_zero : 𝓝 (0 : ennreal) = ⨅a ≠ 0, principal (Iio a) := nhds_bot_order.trans $ by simp [bot_lt_iff_ne_bot, Iio] /-- The set of finite `ennreal` numbers is homeomorphic to `nnreal`. -/ def ne_top_homeomorph_nnreal : {a | a ≠ ∞} ≃ₜ nnreal := { to_fun := λ x, ennreal.to_nnreal x, inv_fun := λ x, ⟨x, coe_ne_top⟩, left_inv := λ ⟨x, hx⟩, subtype.eq $ coe_to_nnreal hx, right_inv := λ x, to_nnreal_coe, continuous_to_fun := continuous_on_iff_continuous_restrict.1 continuous_on_to_nnreal, continuous_inv_fun := continuous_subtype_mk _ (continuous_coe.2 continuous_id) } /-- The set of finite `ennreal` numbers is homeomorphic to `nnreal`. -/ def lt_top_homeomorph_nnreal : {a | a < ∞} ≃ₜ nnreal := by refine (homeomorph.set_congr $ set.ext $ λ x, _).trans ne_top_homeomorph_nnreal; simp only [mem_set_of_eq, lt_top_iff_ne_top] -- using Icc because -- • don't have 'Ioo (x - ε) (x + ε) ∈ 𝓝 x' unless x > 0 -- • (x - y ≤ ε ↔ x ≤ ε + y) is true, while (x - y < ε ↔ x < ε + y) is not @[nolint ge_or_gt] -- see Note [nolint_ge] lemma Icc_mem_nhds : x ≠ ⊤ → ε > 0 → Icc (x - ε) (x + ε) ∈ 𝓝 x := begin assume xt ε0, rw mem_nhds_sets_iff, by_cases x0 : x = 0, { use Iio (x + ε), have : Iio (x + ε) ⊆ Icc (x - ε) (x + ε), assume a, rw x0, simpa using le_of_lt, use this, exact ⟨is_open_Iio, mem_Iio_self_add xt ε0⟩ }, { use Ioo (x - ε) (x + ε), use Ioo_subset_Icc_self, exact ⟨is_open_Ioo, mem_Ioo_self_sub_add xt x0 ε0 ε0 ⟩ } end @[nolint ge_or_gt] -- see Note [nolint_ge] lemma nhds_of_ne_top : x ≠ ⊤ → 𝓝 x = ⨅ε > 0, principal (Icc (x - ε) (x + ε)) := begin assume xt, refine le_antisymm _ _, -- first direction simp only [le_infi_iff, le_principal_iff], assume ε ε0, exact Icc_mem_nhds xt ε0, -- second direction rw nhds_generate_from, refine le_infi (assume s, le_infi $ assume hs, _), simp only [mem_set_of_eq] at hs, rcases hs with ⟨xs, ⟨a, ha⟩⟩, cases ha, { rw ha at *, rcases dense xs with ⟨b, ⟨ab, bx⟩⟩, have xb_pos : x - b > 0 := zero_lt_sub_iff_lt.2 bx, have xxb : x - (x - b) = b := sub_sub_cancel (by rwa lt_top_iff_ne_top) (le_of_lt bx), refine infi_le_of_le (x - b) (infi_le_of_le xb_pos _), simp only [mem_principal_sets, le_principal_iff], assume y, rintros ⟨h₁, h₂⟩, rw xxb at h₁, calc a < b : ab ... ≤ y : h₁ }, { rw ha at *, rcases dense xs with ⟨b, ⟨xb, ba⟩⟩, have bx_pos : b - x > 0 := zero_lt_sub_iff_lt.2 xb, have xbx : x + (b - x) = b := add_sub_cancel_of_le (le_of_lt xb), refine infi_le_of_le (b - x) (infi_le_of_le bx_pos _), simp only [mem_principal_sets, le_principal_iff], assume y, rintros ⟨h₁, h₂⟩, rw xbx at h₂, calc y ≤ b : h₂ ... < a : ba }, end /-- Characterization of neighborhoods for `ennreal` numbers. See also `tendsto_order` for a version with strict inequalities. -/ @[nolint ge_or_gt] -- see Note [nolint_ge] protected theorem tendsto_nhds {f : filter α} {u : α → ennreal} {a : ennreal} (ha : a ≠ ⊤) : tendsto u f (𝓝 a) ↔ ∀ ε > 0, ∀ᶠ x in f, (u x) ∈ Icc (a - ε) (a + ε) := by simp only [nhds_of_ne_top ha, tendsto_infi, tendsto_principal, mem_Icc] @[nolint ge_or_gt] -- see Note [nolint_ge] protected lemma tendsto_at_top [nonempty β] [semilattice_sup β] {f : β → ennreal} {a : ennreal} (ha : a ≠ ⊤) : tendsto f at_top (𝓝 a) ↔ ∀ε>0, ∃N, ∀n≥N, (f n) ∈ Icc (a - ε) (a + ε) := by simp only [ennreal.tendsto_nhds ha, mem_at_top_sets, mem_set_of_eq, filter.eventually] lemma tendsto_coe_nnreal_nhds_top {α} {l : filter α} {f : α → nnreal} (h : tendsto f l at_top) : tendsto (λa, (f a : ennreal)) l (𝓝 ∞) := tendsto_nhds_top $ assume n, have ∀ᶠ a in l, ↑(n+1) ≤ f a := h $ mem_at_top _, mem_sets_of_superset this $ assume a (ha : ↑(n+1) ≤ f a), begin rw [← coe_nat], dsimp, exact coe_lt_coe.2 (lt_of_lt_of_le (nat.cast_lt.2 (nat.lt_succ_self _)) ha) end instance : topological_add_monoid ennreal := ⟨ continuous_iff_continuous_at.2 $ have hl : ∀a:ennreal, tendsto (λ (p : ennreal × ennreal), p.fst + p.snd) (𝓝 (⊤, a)) (𝓝 ⊤), from assume a, tendsto_nhds_top $ assume n, have set.prod {a | ↑n < a } univ ∈ 𝓝 ((⊤:ennreal), a), from prod_mem_nhds_sets (lt_mem_nhds $ coe_nat n ▸ coe_lt_top) univ_mem_sets, show {a : ennreal × ennreal | ↑n < a.fst + a.snd} ∈ 𝓝 (⊤, a), begin filter_upwards [this] assume ⟨a₁, a₂⟩ ⟨h₁, h₂⟩, lt_of_lt_of_le h₁ (le_add_right $ le_refl _) end, begin rintro ⟨a₁, a₂⟩, cases a₁, { simp [continuous_at, none_eq_top, hl a₂], }, cases a₂, { simp [continuous_at, none_eq_top, some_eq_coe, nhds_swap (a₁ : ennreal) ⊤, tendsto_map'_iff, (∘)], convert hl a₁, simp [add_comm] }, simp [continuous_at, some_eq_coe, nhds_coe_coe, tendsto_map'_iff, (∘)], simp only [coe_add.symm, tendsto_coe, tendsto_add] end ⟩ protected lemma tendsto_mul (ha : a ≠ 0 ∨ b ≠ ⊤) (hb : b ≠ 0 ∨ a ≠ ⊤) : tendsto (λp:ennreal×ennreal, p.1 * p.2) (𝓝 (a, b)) (𝓝 (a * b)) := have ht : ∀b:ennreal, b ≠ 0 → tendsto (λp:ennreal×ennreal, p.1 * p.2) (𝓝 ((⊤:ennreal), b)) (𝓝 ⊤), begin refine assume b hb, tendsto_nhds_top $ assume n, _, rcases dense (zero_lt_iff_ne_zero.2 hb) with ⟨ε', hε', hεb'⟩, rcases ennreal.lt_iff_exists_coe.1 hεb' with ⟨ε, rfl, h⟩, rcases exists_nat_gt (↑n / ε) with ⟨m, hm⟩, have hε : ε > 0, from coe_lt_coe.1 hε', refine mem_sets_of_superset (prod_mem_nhds_sets (lt_mem_nhds $ @coe_lt_top m) (lt_mem_nhds $ h)) _, rintros ⟨a₁, a₂⟩ ⟨h₁, h₂⟩, dsimp at h₁ h₂ ⊢, calc (n:ennreal) = ↑(((n:nnreal) / ε) * ε) : begin simp [nnreal.div_def], rw [mul_assoc, ← coe_mul, nnreal.inv_mul_cancel, coe_one, ← coe_nat, mul_one], exact zero_lt_iff_ne_zero.1 hε end ... < (↑m * ε : nnreal) : coe_lt_coe.2 $ mul_lt_mul hm (le_refl _) hε (nat.cast_nonneg _) ... ≤ a₁ * a₂ : by rw [coe_mul]; exact canonically_ordered_semiring.mul_le_mul (le_of_lt h₁) (le_of_lt h₂) end, begin cases a, {simp [none_eq_top] at hb, simp [none_eq_top, ht b hb, top_mul, hb] }, cases b, { simp [none_eq_top] at ha, simp [*, nhds_swap (a : ennreal) ⊤, none_eq_top, some_eq_coe, top_mul, tendsto_map'_iff, (∘), mul_comm] }, simp [some_eq_coe, nhds_coe_coe, tendsto_map'_iff, (∘)], simp only [coe_mul.symm, tendsto_coe, tendsto_mul] end protected lemma tendsto.mul {f : filter α} {ma : α → ennreal} {mb : α → ennreal} {a b : ennreal} (hma : tendsto ma f (𝓝 a)) (ha : a ≠ 0 ∨ b ≠ ⊤) (hmb : tendsto mb f (𝓝 b)) (hb : b ≠ 0 ∨ a ≠ ⊤) : tendsto (λa, ma a * mb a) f (𝓝 (a * b)) := show tendsto ((λp:ennreal×ennreal, p.1 * p.2) ∘ (λa, (ma a, mb a))) f (𝓝 (a * b)), from tendsto.comp (ennreal.tendsto_mul ha hb) (hma.prod_mk_nhds hmb) protected lemma tendsto.const_mul {f : filter α} {m : α → ennreal} {a b : ennreal} (hm : tendsto m f (𝓝 b)) (hb : b ≠ 0 ∨ a ≠ ⊤) : tendsto (λb, a * m b) f (𝓝 (a * b)) := by_cases (assume : a = 0, by simp [this, tendsto_const_nhds]) (assume ha : a ≠ 0, ennreal.tendsto.mul tendsto_const_nhds (or.inl ha) hm hb) protected lemma tendsto.mul_const {f : filter α} {m : α → ennreal} {a b : ennreal} (hm : tendsto m f (𝓝 a)) (ha : a ≠ 0 ∨ b ≠ ⊤) : tendsto (λx, m x * b) f (𝓝 (a * b)) := by simpa only [mul_comm] using ennreal.tendsto.const_mul hm ha protected lemma continuous_const_mul {a : ennreal} (ha : a < ⊤) : continuous ((*) a) := continuous_iff_continuous_at.2 $ λ x, tendsto.const_mul tendsto_id $ or.inr $ ne_of_lt ha protected lemma continuous_mul_const {a : ennreal} (ha : a < ⊤) : continuous (λ x, x * a) := by simpa only [mul_comm] using ennreal.continuous_const_mul ha protected lemma continuous_inv : continuous (has_inv.inv : ennreal → ennreal) := continuous_iff_continuous_at.2 $ λ a, tendsto_order.2 ⟨begin assume b hb, simp only [@ennreal.lt_inv_iff_lt_inv b], exact gt_mem_nhds (ennreal.lt_inv_iff_lt_inv.1 hb), end, begin assume b hb, simp only [gt_iff_lt, @ennreal.inv_lt_iff_inv_lt _ b], exact lt_mem_nhds (ennreal.inv_lt_iff_inv_lt.1 hb) end⟩ @[simp] protected lemma tendsto_inv_iff {f : filter α} {m : α → ennreal} {a : ennreal} : tendsto (λ x, (m x)⁻¹) f (𝓝 a⁻¹) ↔ tendsto m f (𝓝 a) := ⟨λ h, by simpa only [function.comp, ennreal.inv_inv] using (ennreal.continuous_inv.tendsto a⁻¹).comp h, (ennreal.continuous_inv.tendsto a).comp⟩ protected lemma tendsto.div {f : filter α} {ma : α → ennreal} {mb : α → ennreal} {a b : ennreal} (hma : tendsto ma f (𝓝 a)) (ha : a ≠ 0 ∨ b ≠ 0) (hmb : tendsto mb f (𝓝 b)) (hb : b ≠ ⊤ ∨ a ≠ ⊤) : tendsto (λa, ma a / mb a) f (𝓝 (a / b)) := by { apply tendsto.mul hma _ (ennreal.tendsto_inv_iff.2 hmb) _; simp [ha, hb] } protected lemma tendsto.const_div {f : filter α} {m : α → ennreal} {a b : ennreal} (hm : tendsto m f (𝓝 b)) (hb : b ≠ ⊤ ∨ a ≠ ⊤) : tendsto (λb, a / m b) f (𝓝 (a / b)) := by { apply tendsto.const_mul (ennreal.tendsto_inv_iff.2 hm), simp [hb] } protected lemma tendsto.div_const {f : filter α} {m : α → ennreal} {a b : ennreal} (hm : tendsto m f (𝓝 a)) (ha : a ≠ 0 ∨ b ≠ 0) : tendsto (λx, m x / b) f (𝓝 (a / b)) := by { apply tendsto.mul_const hm, simp [ha] } protected lemma tendsto_inv_nat_nhds_zero : tendsto (λ n : ℕ, (n : ennreal)⁻¹) at_top (𝓝 0) := ennreal.inv_top ▸ ennreal.tendsto_inv_iff.2 tendsto_nat_nhds_top lemma Sup_add {s : set ennreal} (hs : s.nonempty) : Sup s + a = ⨆b∈s, b + a := have Sup ((λb, b + a) '' s) = Sup s + a, from is_lub.Sup_eq (is_lub_of_is_lub_of_tendsto (assume x _ y _ h, add_le_add' h (le_refl _)) (is_lub_Sup s) hs (tendsto.add (tendsto_id' inf_le_left) tendsto_const_nhds)), by simp [Sup_image, -add_comm] at this; exact this.symm lemma supr_add {ι : Sort*} {s : ι → ennreal} [h : nonempty ι] : supr s + a = ⨆b, s b + a := let ⟨x⟩ := h in calc supr s + a = Sup (range s) + a : by simp [Sup_range] ... = (⨆b∈range s, b + a) : Sup_add ⟨s x, x, rfl⟩ ... = _ : supr_range lemma add_supr {ι : Sort*} {s : ι → ennreal} [h : nonempty ι] : a + supr s = ⨆b, a + s b := by rw [add_comm, supr_add]; simp [add_comm] lemma supr_add_supr {ι : Sort*} {f g : ι → ennreal} (h : ∀i j, ∃k, f i + g j ≤ f k + g k) : supr f + supr g = (⨆ a, f a + g a) := begin by_cases hι : nonempty ι, { letI := hι, refine le_antisymm _ (supr_le $ λ a, add_le_add' (le_supr _ _) (le_supr _ _)), simpa [add_supr, supr_add] using λ i j:ι, show f i + g j ≤ ⨆ a, f a + g a, from let ⟨k, hk⟩ := h i j in le_supr_of_le k hk }, { have : ∀f:ι → ennreal, (⨆i, f i) = 0 := assume f, bot_unique (supr_le $ assume i, (hι ⟨i⟩).elim), rw [this, this, this, zero_add] } end lemma supr_add_supr_of_monotone {ι : Sort*} [semilattice_sup ι] {f g : ι → ennreal} (hf : monotone f) (hg : monotone g) : supr f + supr g = (⨆ a, f a + g a) := supr_add_supr $ assume i j, ⟨i ⊔ j, add_le_add' (hf $ le_sup_left) (hg $ le_sup_right)⟩ lemma finset_sum_supr_nat {α} {ι} [semilattice_sup ι] {s : finset α} {f : α → ι → ennreal} (hf : ∀a, monotone (f a)) : ∑ a in s, supr (f a) = (⨆ n, ∑ a in s, f a n) := begin refine finset.induction_on s _ _, { simp, exact (bot_unique $ supr_le $ assume i, le_refl ⊥).symm }, { assume a s has ih, simp only [finset.sum_insert has], rw [ih, supr_add_supr_of_monotone (hf a)], assume i j h, exact (finset.sum_le_sum $ assume a ha, hf a h) } end section priority -- for some reason the next proof fails without changing the priority of this instance local attribute [instance, priority 1000] classical.prop_decidable lemma mul_Sup {s : set ennreal} {a : ennreal} : a * Sup s = ⨆i∈s, a * i := begin by_cases hs : ∀x∈s, x = (0:ennreal), { have h₁ : Sup s = 0 := (bot_unique $ Sup_le $ assume a ha, (hs a ha).symm ▸ le_refl 0), have h₂ : (⨆i ∈ s, a * i) = 0 := (bot_unique $ supr_le $ assume a, supr_le $ assume ha, by simp [hs a ha]), rw [h₁, h₂, mul_zero] }, { simp only [not_forall] at hs, rcases hs with ⟨x, hx, hx0⟩, have s₁ : Sup s ≠ 0 := zero_lt_iff_ne_zero.1 (lt_of_lt_of_le (zero_lt_iff_ne_zero.2 hx0) (le_Sup hx)), have : Sup ((λb, a * b) '' s) = a * Sup s := is_lub.Sup_eq (is_lub_of_is_lub_of_tendsto (assume x _ y _ h, canonically_ordered_semiring.mul_le_mul (le_refl _) h) (is_lub_Sup _) ⟨x, hx⟩ (ennreal.tendsto.const_mul (tendsto_id' inf_le_left) (or.inl s₁))), rw [this.symm, Sup_image] } end end priority lemma mul_supr {ι : Sort*} {f : ι → ennreal} {a : ennreal} : a * supr f = ⨆i, a * f i := by rw [← Sup_range, mul_Sup, supr_range] lemma supr_mul {ι : Sort*} {f : ι → ennreal} {a : ennreal} : supr f * a = ⨆i, f i * a := by rw [mul_comm, mul_supr]; congr; funext; rw [mul_comm] protected lemma tendsto_coe_sub : ∀{b:ennreal}, tendsto (λb:ennreal, ↑r - b) (𝓝 b) (𝓝 (↑r - b)) := begin refine (forall_ennreal.2 $ and.intro (assume a, _) _), { simp [@nhds_coe a, tendsto_map'_iff, (∘), tendsto_coe, coe_sub.symm], exact nnreal.tendsto.sub tendsto_const_nhds tendsto_id }, simp, exact (tendsto.congr' (mem_sets_of_superset (lt_mem_nhds $ @coe_lt_top r) $ by simp [le_of_lt] {contextual := tt})) tendsto_const_nhds end lemma sub_supr {ι : Sort*} [hι : nonempty ι] {b : ι → ennreal} (hr : a < ⊤) : a - (⨆i, b i) = (⨅i, a - b i) := let ⟨i⟩ := hι in let ⟨r, eq, _⟩ := lt_iff_exists_coe.mp hr in have Inf ((λb, ↑r - b) '' range b) = ↑r - (⨆i, b i), from is_glb.Inf_eq $ is_glb_of_is_lub_of_tendsto (assume x _ y _, sub_le_sub (le_refl _)) is_lub_supr ⟨_, i, rfl⟩ (tendsto.comp ennreal.tendsto_coe_sub (tendsto_id' inf_le_left)), by rw [eq, ←this]; simp [Inf_image, infi_range, -mem_range]; exact le_refl _ end topological_space section tsum variables {f g : α → ennreal} @[norm_cast] protected lemma has_sum_coe {f : α → nnreal} {r : nnreal} : has_sum (λa, (f a : ennreal)) ↑r ↔ has_sum f r := have (λs:finset α, ∑ a in s, ↑(f a)) = (coe : nnreal → ennreal) ∘ (λs:finset α, ∑ a in s, f a), from funext $ assume s, ennreal.coe_finset_sum.symm, by unfold has_sum; rw [this, tendsto_coe] protected lemma tsum_coe_eq {f : α → nnreal} (h : has_sum f r) : (∑'a, (f a : ennreal)) = r := tsum_eq_has_sum $ ennreal.has_sum_coe.2 $ h protected lemma coe_tsum {f : α → nnreal} : summable f → ↑(tsum f) = (∑'a, (f a : ennreal)) | ⟨r, hr⟩ := by rw [tsum_eq_has_sum hr, ennreal.tsum_coe_eq hr] protected lemma has_sum : has_sum f (⨆s:finset α, ∑ a in s, f a) := tendsto_order.2 ⟨assume a' ha', let ⟨s, hs⟩ := lt_supr_iff.mp ha' in mem_at_top_sets.mpr ⟨s, assume t ht, lt_of_lt_of_le hs $ finset.sum_le_sum_of_subset ht⟩, assume a' ha', univ_mem_sets' $ assume s, have ∑ a in s, f a ≤ ⨆(s : finset α), ∑ a in s, f a, from le_supr (λ(s : finset α), ∑ a in s, f a) s, lt_of_le_of_lt this ha'⟩ @[simp] protected lemma summable : summable f := ⟨_, ennreal.has_sum⟩ lemma tsum_coe_ne_top_iff_summable {f : β → nnreal} : (∑' b, (f b:ennreal)) ≠ ∞ ↔ summable f := begin refine ⟨λ h, _, λ h, ennreal.coe_tsum h ▸ ennreal.coe_ne_top⟩, lift (∑' b, (f b:ennreal)) to nnreal using h with a ha, refine ⟨a, ennreal.has_sum_coe.1 _⟩, rw ha, exact ennreal.summable.has_sum end protected lemma tsum_eq_supr_sum : (∑'a, f a) = (⨆s:finset α, ∑ a in s, f a) := tsum_eq_has_sum ennreal.has_sum protected lemma tsum_eq_supr_sum' {ι : Type*} (s : ι → finset α) (hs : ∀ t, ∃ i, t ⊆ s i) : (∑' a, f a) = ⨆ i, ∑ a in s i, f a := begin rw [ennreal.tsum_eq_supr_sum], symmetry, change (⨆i:ι, (λ t : finset α, ∑ a in t, f a) (s i)) = ⨆s:finset α, ∑ a in s, f a, exact (finset.sum_mono_set f).supr_comp_eq hs end protected lemma tsum_sigma {β : α → Type*} (f : Πa, β a → ennreal) : (∑'p:Σa, β a, f p.1 p.2) = (∑'a b, f a b) := tsum_sigma (assume b, ennreal.summable) ennreal.summable protected lemma tsum_sigma' {β : α → Type*} (f : (Σ a, β a) → ennreal) : (∑'p:(Σa, β a), f p) = (∑'a b, f ⟨a, b⟩) := tsum_sigma (assume b, ennreal.summable) ennreal.summable protected lemma tsum_prod {f : α → β → ennreal} : (∑'p:α×β, f p.1 p.2) = (∑'a, ∑'b, f a b) := let j : α × β → (Σa:α, β) := λp, sigma.mk p.1 p.2 in let i : (Σa:α, β) → α × β := λp, (p.1, p.2) in let f' : (Σa:α, β) → ennreal := λp, f p.1 p.2 in calc (∑'p:α×β, f' (j p)) = (∑'p:Σa:α, β, f p.1 p.2) : tsum_eq_tsum_of_iso j i (assume ⟨a, b⟩, rfl) (assume ⟨a, b⟩, rfl) ... = (∑'a, ∑'b, f a b) : ennreal.tsum_sigma f protected lemma tsum_comm {f : α → β → ennreal} : (∑'a, ∑'b, f a b) = (∑'b, ∑'a, f a b) := let f' : α×β → ennreal := λp, f p.1 p.2 in calc (∑'a, ∑'b, f a b) = (∑'p:α×β, f' p) : ennreal.tsum_prod.symm ... = (∑'p:β×α, f' (prod.swap p)) : (tsum_eq_tsum_of_iso prod.swap (@prod.swap α β) (assume ⟨a, b⟩, rfl) (assume ⟨a, b⟩, rfl)).symm ... = (∑'b, ∑'a, f' (prod.swap (b, a))) : @ennreal.tsum_prod β α (λb a, f' (prod.swap (b, a))) protected lemma tsum_add : (∑'a, f a + g a) = (∑'a, f a) + (∑'a, g a) := tsum_add ennreal.summable ennreal.summable protected lemma tsum_le_tsum (h : ∀a, f a ≤ g a) : (∑'a, f a) ≤ (∑'a, g a) := tsum_le_tsum h ennreal.summable ennreal.summable protected lemma tsum_eq_supr_nat {f : ℕ → ennreal} : (∑'i:ℕ, f i) = (⨆i:ℕ, ∑ a in finset.range i, f a) := ennreal.tsum_eq_supr_sum' _ finset.exists_nat_subset_range protected lemma le_tsum (a : α) : f a ≤ (∑'a, f a) := calc f a = ∑ a' in {a}, f a' : by simp ... ≤ (⨆s:finset α, ∑ a' in s, f a') : le_supr (λs:finset α, ∑ a' in s, f a') _ ... = (∑'a, f a) : by rw [ennreal.tsum_eq_supr_sum] protected lemma tsum_eq_top_of_eq_top : (∃ a, f a = ∞) → (∑' a, f a) = ∞ | ⟨a, ha⟩ := top_unique $ ha ▸ ennreal.le_tsum a protected lemma ne_top_of_tsum_ne_top (h : (∑' a, f a) ≠ ∞) (a : α) : f a ≠ ∞ := λ ha, h $ ennreal.tsum_eq_top_of_eq_top ⟨a, ha⟩ protected lemma tsum_mul_left : (∑'i, a * f i) = a * (∑'i, f i) := if h : ∀i, f i = 0 then by simp [h] else let ⟨i, (hi : f i ≠ 0)⟩ := classical.not_forall.mp h in have sum_ne_0 : (∑'i, f i) ≠ 0, from ne_of_gt $ calc 0 < f i : lt_of_le_of_ne (zero_le _) hi.symm ... ≤ (∑'i, f i) : ennreal.le_tsum _, have tendsto (λs:finset α, ∑ j in s, a * f j) at_top (𝓝 (a * (∑'i, f i))), by rw [← show (*) a ∘ (λs:finset α, ∑ j in s, f j) = λs, ∑ j in s, a * f j, from funext $ λ s, finset.mul_sum]; exact ennreal.tendsto.const_mul ennreal.summable.has_sum (or.inl sum_ne_0), tsum_eq_has_sum this protected lemma tsum_mul_right : (∑'i, f i * a) = (∑'i, f i) * a := by simp [mul_comm, ennreal.tsum_mul_left] @[simp] lemma tsum_supr_eq {α : Type*} (a : α) {f : α → ennreal} : (∑'b:α, ⨆ (h : a = b), f b) = f a := le_antisymm (by rw [ennreal.tsum_eq_supr_sum]; exact supr_le (assume s, calc (∑ b in s, ⨆ (h : a = b), f b) ≤ ∑ b in {a}, ⨆ (h : a = b), f b : finset.sum_le_sum_of_ne_zero $ assume b _ hb, suffices a = b, by simpa using this.symm, classical.by_contradiction $ assume h, by simpa [h] using hb ... = f a : by simp)) (calc f a ≤ (⨆ (h : a = a), f a) : le_supr (λh:a=a, f a) rfl ... ≤ (∑'b:α, ⨆ (h : a = b), f b) : ennreal.le_tsum _) lemma has_sum_iff_tendsto_nat {f : ℕ → ennreal} (r : ennreal) : has_sum f r ↔ tendsto (λn:ℕ, ∑ i in finset.range n, f i) at_top (𝓝 r) := begin refine ⟨has_sum.tendsto_sum_nat, assume h, _⟩, rw [← supr_eq_of_tendsto _ h, ← ennreal.tsum_eq_supr_nat], { exact ennreal.summable.has_sum }, { exact assume s t hst, finset.sum_le_sum_of_subset (finset.range_subset.2 hst) } end end tsum end ennreal namespace nnreal lemma exists_le_has_sum_of_le {f g : β → nnreal} {r : nnreal} (hgf : ∀b, g b ≤ f b) (hfr : has_sum f r) : ∃p≤r, has_sum g p := have (∑'b, (g b : ennreal)) ≤ r, begin refine has_sum_le (assume b, _) ennreal.summable.has_sum (ennreal.has_sum_coe.2 hfr), exact ennreal.coe_le_coe.2 (hgf _) end, let ⟨p, eq, hpr⟩ := ennreal.le_coe_iff.1 this in ⟨p, hpr, ennreal.has_sum_coe.1 $ eq ▸ ennreal.summable.has_sum⟩ lemma summable_of_le {f g : β → nnreal} (hgf : ∀b, g b ≤ f b) : summable f → summable g | ⟨r, hfr⟩ := let ⟨p, _, hp⟩ := exists_le_has_sum_of_le hgf hfr in hp.summable lemma has_sum_iff_tendsto_nat {f : ℕ → nnreal} (r : nnreal) : has_sum f r ↔ tendsto (λn:ℕ, ∑ i in finset.range n, f i) at_top (𝓝 r) := begin rw [← ennreal.has_sum_coe, ennreal.has_sum_iff_tendsto_nat], simp only [ennreal.coe_finset_sum.symm], exact ennreal.tendsto_coe end lemma tsum_comp_le_tsum_of_inj {β : Type*} {f : α → nnreal} (hf : summable f) {i : β → α} (hi : function.injective i) : tsum (f ∘ i) ≤ tsum f := tsum_le_tsum_of_inj i hi (λ c hc, zero_le _) (λ b, le_refl _) (summable_comp_injective hf hi) hf end nnreal lemma tsum_comp_le_tsum_of_inj {β : Type*} {f : α → ℝ} (hf : summable f) (hn : ∀ a, 0 ≤ f a) {i : β → α} (hi : function.injective i) : tsum (f ∘ i) ≤ tsum f := begin let g : α → nnreal := λ a, ⟨f a, hn a⟩, have hg : summable g, by rwa ← nnreal.summable_coe, convert nnreal.coe_le_coe.2 (nnreal.tsum_comp_le_tsum_of_inj hg hi); { rw nnreal.coe_tsum, congr } end lemma summable_of_nonneg_of_le {f g : β → ℝ} (hg : ∀b, 0 ≤ g b) (hgf : ∀b, g b ≤ f b) (hf : summable f) : summable g := let f' (b : β) : nnreal := ⟨f b, le_trans (hg b) (hgf b)⟩ in let g' (b : β) : nnreal := ⟨g b, hg b⟩ in have summable f', from nnreal.summable_coe.1 hf, have summable g', from nnreal.summable_of_le (assume b, (@nnreal.coe_le_coe (g' b) (f' b)).2 $ hgf b) this, show summable (λb, g' b : β → ℝ), from nnreal.summable_coe.2 this lemma has_sum_iff_tendsto_nat_of_nonneg {f : ℕ → ℝ} (hf : ∀i, 0 ≤ f i) (r : ℝ) : has_sum f r ↔ tendsto (λn:ℕ, ∑ i in finset.range n, f i) at_top (𝓝 r) := ⟨has_sum.tendsto_sum_nat, assume hfr, have 0 ≤ r := ge_of_tendsto at_top_ne_bot hfr $ univ_mem_sets' $ assume i, show 0 ≤ ∑ j in finset.range i, f j, from finset.sum_nonneg $ assume i _, hf i, let f' (n : ℕ) : nnreal := ⟨f n, hf n⟩, r' : nnreal := ⟨r, this⟩ in have f_eq : f = (λi:ℕ, (f' i : ℝ)) := rfl, have r_eq : r = r' := rfl, begin rw [f_eq, r_eq, nnreal.has_sum_coe, nnreal.has_sum_iff_tendsto_nat, ← nnreal.tendsto_coe], simp only [nnreal.coe_sum], exact hfr end⟩ lemma infi_real_pos_eq_infi_nnreal_pos {α : Type*} [complete_lattice α] {f : ℝ → α} : (⨅(n:ℝ) (h : 0 < n), f n) = (⨅(n:nnreal) (h : 0 < n), f n) := le_antisymm (le_infi $ assume n, le_infi $ assume hn, infi_le_of_le n $ infi_le _ (nnreal.coe_pos.2 hn)) (le_infi $ assume r, le_infi $ assume hr, infi_le_of_le ⟨r, le_of_lt hr⟩ $ infi_le _ hr) section variables [emetric_space β] open ennreal filter emetric /-- In an emetric ball, the distance between points is everywhere finite -/ lemma edist_ne_top_of_mem_ball {a : β} {r : ennreal} (x y : ball a r) : edist x.1 y.1 ≠ ⊤ := lt_top_iff_ne_top.1 $ calc edist x y ≤ edist a x + edist a y : edist_triangle_left x.1 y.1 a ... < r + r : by rw [edist_comm a x, edist_comm a y]; exact add_lt_add x.2 y.2 ... ≤ ⊤ : le_top /-- Each ball in an extended metric space gives us a metric space, as the edist is everywhere finite. -/ def metric_space_emetric_ball (a : β) (r : ennreal) : metric_space (ball a r) := emetric_space.to_metric_space edist_ne_top_of_mem_ball local attribute [instance] metric_space_emetric_ball lemma nhds_eq_nhds_emetric_ball (a x : β) (r : ennreal) (h : x ∈ ball a r) : 𝓝 x = map (coe : ball a r → β) (𝓝 ⟨x, h⟩) := (map_nhds_subtype_val_eq _ $ mem_nhds_sets emetric.is_open_ball h).symm end section variable [emetric_space α] open emetric lemma tendsto_iff_edist_tendsto_0 {l : filter β} {f : β → α} {y : α} : tendsto f l (𝓝 y) ↔ tendsto (λ x, edist (f x) y) l (𝓝 0) := by simp only [emetric.nhds_basis_eball.tendsto_right_iff, emetric.mem_ball, @tendsto_order ennreal β _ _, forall_prop_of_false ennreal.not_lt_zero, forall_const, true_and] /-- Yet another metric characterization of Cauchy sequences on integers. This one is often the most efficient. -/ lemma emetric.cauchy_seq_iff_le_tendsto_0 [nonempty β] [semilattice_sup β] {s : β → α} : cauchy_seq s ↔ (∃ (b: β → ennreal), (∀ n m N : β, N ≤ n → N ≤ m → edist (s n) (s m) ≤ b N) ∧ (tendsto b at_top (𝓝 0))) := ⟨begin assume hs, rw emetric.cauchy_seq_iff at hs, /- `s` is Cauchy sequence. The sequence `b` will be constructed by taking the supremum of the distances between `s n` and `s m` for `n m ≥ N`-/ let b := λN, Sup ((λ(p : β × β), edist (s p.1) (s p.2))''{p | p.1 ≥ N ∧ p.2 ≥ N}), --Prove that it bounds the distances of points in the Cauchy sequence have C : ∀ n m N, N ≤ n → N ≤ m → edist (s n) (s m) ≤ b N, { refine λm n N hm hn, le_Sup _, use (prod.mk m n), simp only [and_true, eq_self_iff_true, set.mem_set_of_eq], exact ⟨hm, hn⟩ }, --Prove that it tends to `0`, by using the Cauchy property of `s` have D : tendsto b at_top (𝓝 0), { refine tendsto_order.2 ⟨λa ha, absurd ha (ennreal.not_lt_zero), λε εpos, _⟩, rcases dense εpos with ⟨δ, δpos, δlt⟩, rcases hs δ δpos with ⟨N, hN⟩, refine filter.mem_at_top_sets.2 ⟨N, λn hn, _⟩, have : b n ≤ δ := Sup_le begin simp only [and_imp, set.mem_image, set.mem_set_of_eq, exists_imp_distrib, prod.exists], intros d p q hp hq hd, rw ← hd, exact le_of_lt (hN p q (le_trans hn hp) (le_trans hn hq)) end, simpa using lt_of_le_of_lt this δlt }, -- Conclude exact ⟨b, ⟨C, D⟩⟩ end, begin rintros ⟨b, ⟨b_bound, b_lim⟩⟩, /-b : ℕ → ℝ, b_bound : ∀ (n m N : ℕ), N ≤ n → N ≤ m → edist (s n) (s m) ≤ b N, b_lim : tendsto b at_top (𝓝 0)-/ refine emetric.cauchy_seq_iff.2 (λε εpos, _), have : ∀ᶠ n in at_top, b n < ε := (tendsto_order.1 b_lim ).2 _ εpos, rcases filter.mem_at_top_sets.1 this with ⟨N, hN⟩, exact ⟨N, λm n hm hn, calc edist (s m) (s n) ≤ b N : b_bound m n N hm hn ... < ε : (hN _ (le_refl N)) ⟩ end⟩ lemma continuous_of_le_add_edist {f : α → ennreal} (C : ennreal) (hC : C ≠ ⊤) (h : ∀x y, f x ≤ f y + C * edist x y) : continuous f := begin refine continuous_iff_continuous_at.2 (λx, tendsto_order.2 ⟨_, _⟩), show ∀e, e < f x → ∀ᶠ y in 𝓝 x, e < f y, { assume e he, let ε := min (f x - e) 1, have : ε < ⊤ := lt_of_le_of_lt (min_le_right _ _) (by simp [lt_top_iff_ne_top]), have : 0 < ε := by simp [ε, hC, he, ennreal.zero_lt_one], have : 0 < C⁻¹ * (ε/2) := bot_lt_iff_ne_bot.2 (by simp [hC, (ne_of_lt this).symm, ennreal.mul_eq_zero]), have I : C * (C⁻¹ * (ε/2)) < ε, { by_cases C_zero : C = 0, { simp [C_zero, ‹0 < ε›] }, { calc C * (C⁻¹ * (ε/2)) = (C * C⁻¹) * (ε/2) : by simp [mul_assoc] ... = ε/2 : by simp [ennreal.mul_inv_cancel C_zero hC] ... < ε : ennreal.half_lt_self (bot_lt_iff_ne_bot.1 ‹0 < ε›) (lt_top_iff_ne_top.1 ‹ε < ⊤›) }}, have : ball x (C⁻¹ * (ε/2)) ⊆ {y : α | e < f y}, { rintros y hy, by_cases htop : f y = ⊤, { simp [htop, lt_top_iff_ne_top, ne_top_of_lt he] }, { simp at hy, have : e + ε < f y + ε := calc e + ε ≤ e + (f x - e) : add_le_add_left' (min_le_left _ _) ... = f x : by simp [le_of_lt he] ... ≤ f y + C * edist x y : h x y ... = f y + C * edist y x : by simp [edist_comm] ... ≤ f y + C * (C⁻¹ * (ε/2)) : add_le_add_left' $ canonically_ordered_semiring.mul_le_mul (le_refl _) (le_of_lt hy) ... < f y + ε : (ennreal.add_lt_add_iff_left (lt_top_iff_ne_top.2 htop)).2 I, show e < f y, from (ennreal.add_lt_add_iff_right ‹ε < ⊤›).1 this }}, apply filter.mem_sets_of_superset (ball_mem_nhds _ (‹0 < C⁻¹ * (ε/2)›)) this }, show ∀e, f x < e → ∀ᶠ y in 𝓝 x, f y < e, { assume e he, let ε := min (e - f x) 1, have : ε < ⊤ := lt_of_le_of_lt (min_le_right _ _) (by simp [lt_top_iff_ne_top]), have : 0 < ε := by simp [ε, he, ennreal.zero_lt_one], have : 0 < C⁻¹ * (ε/2) := bot_lt_iff_ne_bot.2 (by simp [hC, (ne_of_lt this).symm, ennreal.mul_eq_zero]), have I : C * (C⁻¹ * (ε/2)) < ε, { by_cases C_zero : C = 0, simp [C_zero, ‹0 < ε›], calc C * (C⁻¹ * (ε/2)) = (C * C⁻¹) * (ε/2) : by simp [mul_assoc] ... = ε/2 : by simp [ennreal.mul_inv_cancel C_zero hC] ... < ε : ennreal.half_lt_self (bot_lt_iff_ne_bot.1 ‹0 < ε›) (lt_top_iff_ne_top.1 ‹ε < ⊤›) }, have : ball x (C⁻¹ * (ε/2)) ⊆ {y : α | f y < e}, { rintros y hy, have htop : f x ≠ ⊤ := ne_top_of_lt he, show f y < e, from calc f y ≤ f x + C * edist y x : h y x ... ≤ f x + C * (C⁻¹ * (ε/2)) : add_le_add_left' $ canonically_ordered_semiring.mul_le_mul (le_refl _) (le_of_lt hy) ... < f x + ε : (ennreal.add_lt_add_iff_left (lt_top_iff_ne_top.2 htop)).2 I ... ≤ f x + (e - f x) : add_le_add_left' (min_le_left _ _) ... = e : by simp [le_of_lt he] }, apply filter.mem_sets_of_superset (ball_mem_nhds _ (‹0 < C⁻¹ * (ε/2)›)) this }, end theorem continuous_edist : continuous (λp:α×α, edist p.1 p.2) := begin apply continuous_of_le_add_edist 2 (by simp), rintros ⟨x, y⟩ ⟨x', y'⟩, calc edist x y ≤ edist x x' + edist x' y' + edist y' y : edist_triangle4 _ _ _ _ ... = edist x' y' + (edist x x' + edist y y') : by simp [edist_comm]; cc ... ≤ edist x' y' + (edist (x, y) (x', y') + edist (x, y) (x', y')) : add_le_add_left' (add_le_add' (by simp [edist, le_refl]) (by simp [edist, le_refl])) ... = edist x' y' + 2 * edist (x, y) (x', y') : by rw [← mul_two, mul_comm] end theorem continuous.edist [topological_space β] {f g : β → α} (hf : continuous f) (hg : continuous g) : continuous (λb, edist (f b) (g b)) := continuous_edist.comp (hf.prod_mk hg) theorem filter.tendsto.edist {f g : β → α} {x : filter β} {a b : α} (hf : tendsto f x (𝓝 a)) (hg : tendsto g x (𝓝 b)) : tendsto (λx, edist (f x) (g x)) x (𝓝 (edist a b)) := (continuous_edist.tendsto (a, b)).comp (hf.prod_mk_nhds hg) lemma cauchy_seq_of_edist_le_of_tsum_ne_top {f : ℕ → α} (d : ℕ → ennreal) (hf : ∀ n, edist (f n) (f n.succ) ≤ d n) (hd : tsum d ≠ ∞) : cauchy_seq f := begin lift d to (ℕ → nnreal) using (λ i, ennreal.ne_top_of_tsum_ne_top hd i), rw ennreal.tsum_coe_ne_top_iff_summable at hd, exact cauchy_seq_of_edist_le_of_summable d hf hd end /-- If `edist (f n) (f (n+1))` is bounded above by a function `d : ℕ → ennreal`, then the distance from `f n` to the limit is bounded by `∑'_{k=n}^∞ d k`. -/ lemma edist_le_tsum_of_edist_le_of_tendsto {f : ℕ → α} (d : ℕ → ennreal) (hf : ∀ n, edist (f n) (f n.succ) ≤ d n) {a : α} (ha : tendsto f at_top (𝓝 a)) (n : ℕ) : edist (f n) a ≤ ∑' m, d (n + m) := begin refine le_of_tendsto at_top_ne_bot (tendsto_const_nhds.edist ha) (mem_at_top_sets.2 ⟨n, λ m hnm, _⟩), refine le_trans (edist_le_Ico_sum_of_edist_le hnm (λ k _ _, hf k)) _, rw [finset.sum_Ico_eq_sum_range], exact sum_le_tsum _ (λ _ _, zero_le _) ennreal.summable end /-- If `edist (f n) (f (n+1))` is bounded above by a function `d : ℕ → ennreal`, then the distance from `f 0` to the limit is bounded by `∑'_{k=0}^∞ d k`. -/ lemma edist_le_tsum_of_edist_le_of_tendsto₀ {f : ℕ → α} (d : ℕ → ennreal) (hf : ∀ n, edist (f n) (f n.succ) ≤ d n) {a : α} (ha : tendsto f at_top (𝓝 a)) : edist (f 0) a ≤ ∑' m, d m := by simpa using edist_le_tsum_of_edist_le_of_tendsto d hf ha 0 end --section
54c0aee1160b0c3953a4ad1baa44caa34f618098
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/algebraic_geometry/locally_ringed_space.lean
99cc0267c1c1e7415bc7eb4212731a819bfd7b9a
[ "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
5,635
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import algebraic_geometry.sheafed_space import algebra.category.CommRing.limits import algebra.category.CommRing.colimits import algebraic_geometry.stalks import ring_theory.ideal.basic /-! # The category of locally ringed spaces We define (bundled) locally ringed spaces (as `SheafedSpace CommRing` along with the fact that the stalks are local rings), and morphisms between these (morphisms in `SheafedSpace` with `is_local_ring_hom` on the stalk maps). ## Future work * Define the restriction along an open embedding -/ universes v u open category_theory open Top open topological_space open opposite open category_theory.category category_theory.functor namespace algebraic_geometry /-- A `LocallyRingedSpace` is a topological space equipped with a sheaf of commutative rings such that all the stalks are local rings. A morphism of locally ringed spaces is a morphism of ringed spaces such that the morphims induced on stalks are local ring homomorphisms. -/ @[nolint has_inhabited_instance] structure LocallyRingedSpace extends SheafedSpace CommRing := (local_ring : ∀ x, local_ring (presheaf.stalk x)) attribute [instance] LocallyRingedSpace.local_ring namespace LocallyRingedSpace variables (X : LocallyRingedSpace) /-- The underlying topological space of a locally ringed space. -/ def to_Top : Top := X.1.carrier instance : has_coe_to_sort LocallyRingedSpace := { S := Type u, coe := λ X : LocallyRingedSpace, (X.to_Top : Type u), } -- PROJECT: how about a typeclass "has_structure_sheaf" to mediate the 𝒪 notation, rather -- than defining it over and over for PresheafedSpace, LRS, Scheme, etc. /-- The structure sheaf of a locally ringed space. -/ def 𝒪 : sheaf CommRing X.to_Top := X.to_SheafedSpace.sheaf /-- A morphism of locally ringed spaces is a morphism of ringed spaces such that the morphims induced on stalks are local ring homomorphisms. -/ def hom (X Y : LocallyRingedSpace) : Type* := { f : X.to_SheafedSpace ⟶ Y.to_SheafedSpace // ∀ x, is_local_ring_hom (PresheafedSpace.stalk_map f x) } instance : quiver LocallyRingedSpace := ⟨hom⟩ @[ext] lemma hom_ext {X Y : LocallyRingedSpace} (f g : hom X Y) (w : f.1 = g.1) : f = g := subtype.eq w /-- The stalk of a locally ringed space, just as a `CommRing`. -/ -- TODO perhaps we should make a bundled `LocalRing` and return one here? -- TODO define `sheaf.stalk` so we can write `X.𝒪.stalk` here? noncomputable def stalk (X : LocallyRingedSpace) (x : X) : CommRing := X.presheaf.stalk x /-- A morphism of locally ringed spaces `f : X ⟶ Y` induces a local ring homomorphism from `Y.stalk (f x)` to `X.stalk x` for any `x : X`. -/ noncomputable def stalk_map {X Y : LocallyRingedSpace} (f : X ⟶ Y) (x : X) : Y.stalk (f.1.1 x) ⟶ X.stalk x := PresheafedSpace.stalk_map f.1 x instance {X Y : LocallyRingedSpace} (f : X ⟶ Y) (x : X) : is_local_ring_hom (stalk_map f x) := f.2 x /-- The identity morphism on a locally ringed space. -/ @[simps] def id (X : LocallyRingedSpace) : hom X X := ⟨𝟙 _, λ x, by { erw PresheafedSpace.stalk_map.id, apply is_local_ring_hom_id, }⟩ instance (X : LocallyRingedSpace) : inhabited (hom X X) := ⟨id X⟩ /-- Composition of morphisms of locally ringed spaces. -/ @[simps] def comp {X Y Z : LocallyRingedSpace} (f : hom X Y) (g : hom Y Z) : hom X Z := ⟨f.val ≫ g.val, λ x, begin erw PresheafedSpace.stalk_map.comp, exact @is_local_ring_hom_comp _ _ _ _ _ _ _ _ (f.2 _) (g.2 _), end⟩ /-- The category of locally ringed spaces. -/ instance : category LocallyRingedSpace := { hom := hom, id := id, comp := λ X Y Z f g, comp f g, comp_id' := by { intros, ext1, simp, }, id_comp' := by { intros, ext1, simp, }, assoc' := by { intros, ext1, simp, }, }. /-- The forgetful functor from `LocallyRingedSpace` to `SheafedSpace CommRing`. -/ def forget_to_SheafedSpace : LocallyRingedSpace ⥤ SheafedSpace CommRing := { obj := λ X, X.to_SheafedSpace, map := λ X Y f, f.1, } instance : faithful forget_to_SheafedSpace := {} -- PROJECT: once we have `PresheafedSpace.restrict_stalk_iso` -- (that restriction doesn't change stalks) we can uncomment this. /- def restrict {U : Top} (X : LocallyRingedSpace) (f : U ⟶ X.to_Top) (h : open_embedding f) : LocallyRingedSpace := { local_ring := begin intro x, dsimp at *, -- We show that the stalk of the restriction is isomorphic to the original stalk, have := X.to_SheafedSpace.to_PresheafedSpace.restrict_stalk_iso f h x, -- and then transfer `local_ring` across the ring equivalence. apply (this.CommRing_iso_to_ring_equiv).local_ring, -- import data.equiv.transfer_instance apply X.local_ring, end, .. X.to_SheafedSpace.restrict _ f h } -/ /-- The global sections, notated Gamma. -/ def Γ : LocallyRingedSpaceᵒᵖ ⥤ CommRing := forget_to_SheafedSpace.op ⋙ SheafedSpace.Γ lemma Γ_def : Γ = forget_to_SheafedSpace.op ⋙ SheafedSpace.Γ := rfl @[simp] lemma Γ_obj (X : LocallyRingedSpaceᵒᵖ) : Γ.obj X = (unop X).presheaf.obj (op ⊤) := rfl lemma Γ_obj_op (X : LocallyRingedSpace) : Γ.obj (op X) = X.presheaf.obj (op ⊤) := rfl @[simp] lemma Γ_map {X Y : LocallyRingedSpaceᵒᵖ} (f : X ⟶ Y) : Γ.map f = f.unop.1.c.app (op ⊤) ≫ (unop Y).presheaf.map (opens.le_map_top _ _).op := rfl lemma Γ_map_op {X Y : LocallyRingedSpace} (f : X ⟶ Y) : Γ.map f.op = f.1.c.app (op ⊤) ≫ X.presheaf.map (opens.le_map_top _ _).op := rfl end LocallyRingedSpace end algebraic_geometry
4a4e25cb3128dc85040c6593a001f61eb09b163c
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/437c.lean
e9dec6d67d1ac5bad02d717cb8c36aa90aaec2b3
[ "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
116
lean
example (a : ℕ) : true := begin have : ∀ n, n ≥ 0 → a ≤ a, { exact λ _ _, le_refl a }, trivial end
efc7789b91dfa26955a4181f1c65a69eed0e47d9
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/library/init/logic.lean
49746b59010f6b7d0bea0ab9f626c20562d46eae
[ "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
34,281
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad, Floris van Doorn -/ prelude import init.datatypes universe variables u v attribute [reducible] definition id {A : Type u} (a : A) : A := a /- TODO(Leo): for new elaborator - Support for partially applied recursors (use eta-expansion) - checkpoints when processing calc. -/ /- implication -/ definition implies (a b : Prop) := a → b attribute [trans] lemma implies.trans {p q r : Prop} (h₁ : implies p q) (h₂ : implies q r) : implies p r := assume hp, h₂ (h₁ hp) definition trivial : true := ⟨⟩ definition not (a : Prop) := a → false prefix `¬` := not attribute [inline] definition absurd {a : Prop} {b : Type v} (H1 : a) (H2 : ¬a) : b := false.rec b (H2 H1) attribute [intro!] lemma not.intro {a : Prop} (H : a → false) : ¬ a := H theorem mt {a b : Prop} (H1 : a → b) (H2 : ¬b) : ¬a := assume Ha : a, absurd (H1 Ha) H2 definition implies.resolve {a b : Prop} (H : a → b) (nb : ¬ b) : ¬ a := assume Ha, nb (H Ha) /- not -/ theorem not_false : ¬false := assume H : false, H definition non_contradictory (a : Prop) : Prop := ¬¬a theorem non_contradictory_intro {a : Prop} (Ha : a) : ¬¬a := assume Hna : ¬a, absurd Ha Hna /- false -/ theorem false.elim {c : Prop} (H : false) : c := false.rec c H /- eq -/ -- proof irrelevance is built in theorem proof_irrel {a : Prop} (H₁ H₂ : a) : H₁ = H₂ := rfl attribute [defeq] definition id.def {A : Type u} (a : A) : id a = a := rfl -- Remark: we provide the universe levels explicitly to make sure `eq.drec` has the same type of `eq.rec` in the HoTT library attribute [elab_as_eliminator] protected theorem {u₁ u₂} eq.drec {A : Type u₂} {a : A} {C : Π (x : A), a = x → Type u₁} (h₁ : C a (eq.refl a)) {b : A} (h₂ : a = b) : C b h₂ := eq.rec (λh₂ : a = a, show C a h₂, from h₁) h₂ h₂ namespace eq variables {A : Type u} variables {a b c a': A} attribute [elab_as_eliminator] protected theorem drec_on {a : A} {C : Π (x : A), a = x → Type v} {b : A} (h₂ : a = b) (h₁ : C a (refl a)) : C b h₂ := eq.drec h₁ h₂ theorem substr {P : A → Prop} (H₁ : b = a) : P a → P b := subst (symm H₁) theorem mp {A B : Type u} : (A = B) → A → B := eq.rec_on theorem mpr {A B : Type u} : (A = B) → B → A := assume H₁ H₂, eq.rec_on (eq.symm H₁) H₂ end eq open eq theorem congr {A : Type u} {B : Type v} {f₁ f₂ : A → B} {a₁ a₂ : A} (H₁ : f₁ = f₂) (H₂ : a₁ = a₂) : f₁ a₁ = f₂ a₂ := eq.subst H₁ (eq.subst H₂ rfl) theorem congr_fun {A : Type u} {B : A → Type v} {f g : Π x, B x} (H : f = g) (a : A) : f a = g a := eq.subst H (eq.refl (f a)) theorem congr_arg {A : Type u} {B : Type v} {a₁ a₂ : A} (f : A → B) : a₁ = a₂ → f a₁ = f a₂ := congr rfl section variables {A : Type u} {a b c: A} theorem trans_rel_left (R : A → A → Prop) (H₁ : R a b) (H₂ : b = c) : R a c := H₂ ▸ H₁ theorem trans_rel_right (R : A → A → Prop) (H₁ : a = b) (H₂ : R b c) : R a c := symm H₁ ▸ H₂ end section variable {p : Prop} theorem of_eq_true (H : p = true) : p := symm H ▸ trivial theorem not_of_eq_false (H : p = false) : ¬p := assume Hp, H ▸ Hp end attribute [inline] definition cast {A B : Type u} (H : A = B) (a : A) : B := eq.rec a H theorem cast_proof_irrel {A B : Type u} (H₁ H₂ : A = B) (a : A) : cast H₁ a = cast H₂ a := rfl theorem cast_eq {A : Type u} (H : A = A) (a : A) : cast H a = a := rfl /- ne -/ attribute [reducible] definition ne {A : Type u} (a b : A) := ¬(a = b) attribute [defeq] definition ne.def {A : Type u} (a b : A) : ne a b = ¬ (a = b) := rfl notation a ≠ b := ne a b namespace ne variable {A : Type u} variables {a b : A} theorem intro (H : a = b → false) : a ≠ b := H theorem elim (H : a ≠ b) : a = b → false := H theorem irrefl (H : a ≠ a) : false := H rfl theorem symm (H : a ≠ b) : b ≠ a := assume (H₁ : b = a), H (symm H₁) end ne theorem false_of_ne {A : Type u} {a : A} : a ≠ a → false := ne.irrefl section variables {p : Prop} theorem ne_false_of_self : p → p ≠ false := assume (Hp : p) (Heq : p = false), Heq ▸ Hp theorem ne_true_of_not : ¬p → p ≠ true := assume (Hnp : ¬p) (Heq : p = true), (Heq ▸ Hnp) trivial theorem true_ne_false : ¬true = false := ne_false_of_self trivial end infixl ` == `:50 := heq section variables {A B C : Type u} {a a' : A} {b b' : B} {c : C} theorem eq_of_heq (h : a == a') : a = a' := have ∀ (A' : Type u) (a' : A') (h₁ : @heq A a A' a') (h₂ : A = A'), (eq.rec_on h₂ a : A') = a', from λ (A' : Type u) (a' : A') (h₁ : @heq A a A' a'), heq.rec_on h₁ (λ h₂ : A = A, rfl), show (eq.rec_on (eq.refl A) a : A) = a', from this A a' h (eq.refl A) theorem heq.elim {A : Type u} {a : A} {P : A → Type v} {b : A} (H₁ : a == b) : P a → P b := eq.rec_on (eq_of_heq H₁) theorem heq.subst {P : ∀ T : Type u, T → Prop} : a == b → P A a → P B b := heq.rec_on theorem heq.symm (H : a == b) : b == a := heq.rec_on H (heq.refl a) theorem heq_of_eq (H : a = a') : a == a' := eq.subst H (heq.refl a) theorem heq.trans (H₁ : a == b) (H₂ : b == c) : a == c := heq.subst H₂ H₁ theorem heq_of_heq_of_eq (H₁ : a == b) (H₂ : b = b') : a == b' := heq.trans H₁ (heq_of_eq H₂) theorem heq_of_eq_of_heq (H₁ : a = a') (H₂ : a' == b) : a == b := heq.trans (heq_of_eq H₁) H₂ definition type_eq_of_heq (H : a == b) : A = B := heq.rec_on H (eq.refl A) end theorem eq_rec_heq {A : Type u} {P : A → Type v} : ∀ {a a' : A} (H : a = a') (p : P a), (eq.rec_on H p : P a') == p | a .a rfl p := heq.refl p theorem heq_of_eq_rec_left {A : Type u} {P : A → Type v} : ∀ {a a' : A} {p₁ : P a} {p₂ : P a'} (e : a = a') (h₂ : (eq.rec_on e p₁ : P a') = p₂), p₁ == p₂ | a .a p₁ p₂ (eq.refl .a) h := eq.rec_on h (heq.refl p₁) theorem heq_of_eq_rec_right {A : Type u} {P : A → Type v} : ∀ {a a' : A} {p₁ : P a} {p₂ : P a'} (e : a' = a) (h₂ : p₁ = eq.rec_on e p₂), p₁ == p₂ | a .a p₁ p₂ (eq.refl .a) h := have p₁ = p₂, from h, this ▸ heq.refl p₁ theorem of_heq_true {a : Prop} (H : a == true) : a := of_eq_true (eq_of_heq H) theorem eq_rec_compose : ∀ {A B C : Type u} (p₁ : B = C) (p₂ : A = B) (a : A), (eq.rec_on p₁ (eq.rec_on p₂ a : B) : C) = eq.rec_on (eq.trans p₂ p₁) a | A .A .A (eq.refl .A) (eq.refl .A) a := rfl theorem eq_rec_eq_eq_rec : ∀ {A₁ A₂ : Type u} {p : A₁ = A₂} {a₁ : A₁} {a₂ : A₂}, (eq.rec_on p a₁ : A₂) = a₂ → a₁ = eq.rec_on (eq.symm p) a₂ | A .A rfl a .a rfl := rfl theorem eq_rec_of_heq_left : ∀ {A₁ A₂ : Type u} {a₁ : A₁} {a₂ : A₂} (h : a₁ == a₂), (eq.rec_on (type_eq_of_heq h) a₁ : A₂) = a₂ | A .A a .a (heq.refl .a) := rfl theorem eq_rec_of_heq_right {A₁ A₂ : Type u} {a₁ : A₁} {a₂ : A₂} (h : a₁ == a₂) : a₁ = eq.rec_on (eq.symm (type_eq_of_heq h)) a₂ := eq_rec_eq_eq_rec (eq_rec_of_heq_left h) attribute heq.refl [refl] attribute heq.trans [trans] attribute heq_of_heq_of_eq [trans] attribute heq_of_eq_of_heq [trans] attribute heq.symm [symm] theorem cast_heq : ∀ {A B : Type u} (H : A = B) (a : A), cast H a == a | A .A (eq.refl .A) a := heq.refl a /- and -/ notation a /\ b := and a b notation a ∧ b := and a b variables {a b c d : Prop} attribute and.rec [elim] attribute and.intro [intro!] theorem and.elim (H₁ : a ∧ b) (H₂ : a → b → c) : c := and.rec H₂ H₁ theorem and.swap : a ∧ b → b ∧ a := λ h, and.rec (λHa Hb, and.intro Hb Ha) h /- or -/ notation a \/ b := or a b notation a ∨ b := or a b attribute or.rec [elim] namespace or theorem elim (H₁ : a ∨ b) (H₂ : a → c) (H₃ : b → c) : c := or.rec H₂ H₃ H₁ end or theorem non_contradictory_em (a : Prop) : ¬¬(a ∨ ¬a) := assume not_em : ¬(a ∨ ¬a), have neg_a : ¬a, from assume pos_a : a, absurd (or.inl pos_a) not_em, absurd (or.inr neg_a) not_em theorem or.swap : a ∨ b → b ∨ a := or.rec or.inr or.inl /- xor -/ definition xor (a b : Prop) := (a ∧ ¬ b) ∨ (b ∧ ¬ a) /- iff -/ definition iff (a b : Prop) := (a → b) ∧ (b → a) notation a <-> b := iff a b notation a ↔ b := iff a b theorem iff.intro : (a → b) → (b → a) → (a ↔ b) := and.intro attribute iff.intro [intro!] theorem iff.elim : ((a → b) → (b → a) → c) → (a ↔ b) → c := and.rec attribute iff.elim [recursor 5, elim] theorem iff.elim_left : (a ↔ b) → a → b := and.left definition iff.mp := @iff.elim_left theorem iff.elim_right : (a ↔ b) → b → a := and.right definition iff.mpr := @iff.elim_right attribute [refl] theorem iff.refl (a : Prop) : a ↔ a := iff.intro (assume H, H) (assume H, H) theorem iff.rfl {a : Prop} : a ↔ a := iff.refl a attribute [trans] theorem iff.trans (H₁ : a ↔ b) (H₂ : b ↔ c) : a ↔ c := iff.intro (assume Ha, iff.mp H₂ (iff.mp H₁ Ha)) (assume Hc, iff.mpr H₁ (iff.mpr H₂ Hc)) attribute [symm] theorem iff.symm (H : a ↔ b) : b ↔ a := iff.intro (iff.elim_right H) (iff.elim_left H) theorem iff.comm : (a ↔ b) ↔ (b ↔ a) := iff.intro iff.symm iff.symm theorem iff.of_eq {a b : Prop} (H : a = b) : a ↔ b := eq.rec_on H iff.rfl theorem not_iff_not_of_iff (H₁ : a ↔ b) : ¬a ↔ ¬b := iff.intro (assume (Hna : ¬ a) (Hb : b), Hna (iff.elim_right H₁ Hb)) (assume (Hnb : ¬ b) (Ha : a), Hnb (iff.elim_left H₁ Ha)) theorem of_iff_true (H : a ↔ true) : a := iff.mp (iff.symm H) trivial theorem not_of_iff_false : (a ↔ false) → ¬a := iff.mp theorem iff_true_intro (H : a) : a ↔ true := iff.intro (λ Hl, trivial) (λ Hr, H) theorem iff_false_intro (H : ¬a) : a ↔ false := iff.intro H (false.rec a) theorem not_non_contradictory_iff_absurd (a : Prop) : ¬¬¬a ↔ ¬a := iff.intro (λ (Hl : ¬¬¬a) (Ha : a), Hl (non_contradictory_intro Ha)) absurd theorem imp_congr (H1 : a ↔ c) (H2 : b ↔ d) : (a → b) ↔ (c → d) := iff.intro (λHab Hc, iff.mp H2 (Hab (iff.mpr H1 Hc))) (λHcd Ha, iff.mpr H2 (Hcd (iff.mp H1 Ha))) theorem imp_congr_ctx (H1 : a ↔ c) (H2 : c → (b ↔ d)) : (a → b) ↔ (c → d) := iff.intro (λHab Hc, have Ha : a, from iff.mpr H1 Hc, have Hb : b, from Hab Ha, iff.mp (H2 Hc) Hb) (λHcd Ha, have Hc : c, from iff.mp H1 Ha, have Hd : d, from Hcd Hc, iff.mpr (H2 Hc) Hd) theorem imp_congr_right (H : a → (b ↔ c)) : (a → b) ↔ (a → c) := iff.intro (take Hab Ha, iff.elim_left (H Ha) (Hab Ha)) (take Hab Ha, iff.elim_right (H Ha) (Hab Ha)) theorem not_not_intro (Ha : a) : ¬¬a := assume Hna : ¬a, Hna Ha theorem not_of_not_not_not (H : ¬¬¬a) : ¬a := λ Ha, absurd (not_not_intro Ha) H attribute [simp] theorem not_true : (¬ true) ↔ false := iff_false_intro (not_not_intro trivial) attribute [simp] theorem not_false_iff : (¬ false) ↔ true := iff_true_intro not_false attribute [congr] theorem not_congr (H : a ↔ b) : ¬a ↔ ¬b := iff.intro (λ H₁ H₂, H₁ (iff.mpr H H₂)) (λ H₁ H₂, H₁ (iff.mp H H₂)) attribute [simp] theorem ne_self_iff_false {A : Type u} (a : A) : (not (a = a)) ↔ false := iff.intro false_of_ne false.elim attribute [simp] theorem eq_self_iff_true {A : Type u} (a : A) : (a = a) ↔ true := iff_true_intro rfl attribute [simp] theorem heq_self_iff_true {A : Type u} (a : A) : (a == a) ↔ true := iff_true_intro (heq.refl a) attribute [simp] theorem iff_not_self (a : Prop) : (a ↔ ¬a) ↔ false := iff_false_intro (λ H, have H' : ¬a, from (λ Ha, (iff.mp H Ha) Ha), H' (iff.mpr H H')) attribute [simp] theorem not_iff_self (a : Prop) : (¬a ↔ a) ↔ false := iff_false_intro (λ H, have H' : ¬a, from (λ Ha, (iff.mpr H Ha) Ha), H' (iff.mp H H')) attribute [simp] theorem true_iff_false : (true ↔ false) ↔ false := iff_false_intro (λ H, iff.mp H trivial) attribute [simp] theorem false_iff_true : (false ↔ true) ↔ false := iff_false_intro (λ H, iff.mpr H trivial) theorem false_of_true_iff_false : (true ↔ false) → false := assume H, iff.mp H trivial /- and simp rules -/ theorem and.imp (H₂ : a → c) (H₃ : b → d) : a ∧ b → c ∧ d := λ h, and.rec (λ Ha Hb, ⟨H₂ Ha, H₃ Hb⟩) h attribute [congr] theorem and_congr (H1 : a ↔ c) (H2 : b ↔ d) : (a ∧ b) ↔ (c ∧ d) := iff.intro (and.imp (iff.mp H1) (iff.mp H2)) (and.imp (iff.mpr H1) (iff.mpr H2)) theorem and_congr_right (H : a → (b ↔ c)) : (a ∧ b) ↔ (a ∧ c) := iff.intro (take Hab, ⟨and.left Hab, iff.elim_left (H (and.left Hab)) (and.right Hab)⟩) (take Hac, ⟨and.left Hac, iff.elim_right (H (and.left Hac)) (and.right Hac)⟩) attribute [simp] theorem and.comm : a ∧ b ↔ b ∧ a := iff.intro and.swap and.swap attribute [simp] theorem and.assoc : (a ∧ b) ∧ c ↔ a ∧ (b ∧ c) := iff.intro (λ h, and.rec (λ H' Hc, and.rec (λ Ha Hb, ⟨Ha, Hb, Hc⟩) H') h) (λ h, and.rec (λ Ha Hbc, and.rec (λ Hb Hc, ⟨⟨Ha, Hb⟩, Hc⟩) Hbc) h) attribute [simp] theorem and.left_comm : a ∧ (b ∧ c) ↔ b ∧ (a ∧ c) := iff.trans (iff.symm and.assoc) (iff.trans (and_congr and.comm (iff.refl c)) and.assoc) theorem and_iff_left {a b : Prop} (Hb : b) : (a ∧ b) ↔ a := iff.intro and.left (λ Ha, ⟨Ha, Hb⟩) theorem and_iff_right {a b : Prop} (Ha : a) : (a ∧ b) ↔ b := iff.intro and.right (and.intro Ha) attribute [simp] theorem and_true (a : Prop) : a ∧ true ↔ a := and_iff_left trivial attribute [simp] theorem true_and (a : Prop) : true ∧ a ↔ a := and_iff_right trivial attribute [simp] theorem and_false (a : Prop) : a ∧ false ↔ false := iff_false_intro and.right attribute [simp] theorem false_and (a : Prop) : false ∧ a ↔ false := iff_false_intro and.left attribute [simp] theorem not_and_self (a : Prop) : (¬a ∧ a) ↔ false := iff_false_intro (λ H, and.elim H (λ H₁ H₂, absurd H₂ H₁)) attribute [simp] theorem and_not_self (a : Prop) : (a ∧ ¬a) ↔ false := iff_false_intro (λ H, and.elim H (λ H₁ H₂, absurd H₁ H₂)) attribute [simp] theorem and_self (a : Prop) : a ∧ a ↔ a := iff.intro and.left (assume H, ⟨H, H⟩) /- or simp rules -/ theorem or.imp (H₂ : a → c) (H₃ : b → d) : a ∨ b → c ∨ d := or.rec (λ H, or.inl (H₂ H)) (λ H, or.inr (H₃ H)) theorem or.imp_left (H : a → b) : a ∨ c → b ∨ c := or.imp H id theorem or.imp_right (H : a → b) : c ∨ a → c ∨ b := or.imp id H attribute [congr] theorem or_congr (H1 : a ↔ c) (H2 : b ↔ d) : (a ∨ b) ↔ (c ∨ d) := iff.intro (or.imp (iff.mp H1) (iff.mp H2)) (or.imp (iff.mpr H1) (iff.mpr H2)) attribute [simp] theorem or.comm : a ∨ b ↔ b ∨ a := iff.intro or.swap or.swap attribute [simp] theorem or.assoc : (a ∨ b) ∨ c ↔ a ∨ (b ∨ c) := iff.intro (or.rec (or.imp_right or.inl) (λ H, or.inr (or.inr H))) (or.rec (λ H, or.inl (or.inl H)) (or.imp_left or.inr)) attribute [simp] theorem or.left_comm : a ∨ (b ∨ c) ↔ b ∨ (a ∨ c) := iff.trans (iff.symm or.assoc) (iff.trans (or_congr or.comm (iff.refl c)) or.assoc) attribute [simp] theorem or_true (a : Prop) : a ∨ true ↔ true := iff_true_intro (or.inr trivial) attribute [simp] theorem true_or (a : Prop) : true ∨ a ↔ true := iff_true_intro (or.inl trivial) attribute [simp] theorem or_false (a : Prop) : a ∨ false ↔ a := iff.intro (or.rec id false.elim) or.inl attribute [simp] theorem false_or (a : Prop) : false ∨ a ↔ a := iff.trans or.comm (or_false a) attribute [simp] theorem or_self (a : Prop) : a ∨ a ↔ a := iff.intro (or.rec id id) or.inl /- or resolution rulse -/ definition or.resolve_left {a b : Prop} (H : a ∨ b) (na : ¬ a) : b := or.elim H (λ Ha, absurd Ha na) id definition or.neg_resolve_left {a b : Prop} (H : ¬ a ∨ b) (Ha : a) : b := or.elim H (λ na, absurd Ha na) id definition or.resolve_right {a b : Prop} (H : a ∨ b) (nb : ¬ b) : a := or.elim H id (λ Hb, absurd Hb nb) definition or.neg_resolve_right {a b : Prop} (H : a ∨ ¬ b) (Hb : b) : a := or.elim H id (λ nb, absurd Hb nb) /- iff simp rules -/ attribute [simp] theorem iff_true (a : Prop) : (a ↔ true) ↔ a := iff.intro (assume H, iff.mpr H trivial) iff_true_intro attribute [simp] theorem true_iff (a : Prop) : (true ↔ a) ↔ a := iff.trans iff.comm (iff_true a) attribute [simp] theorem iff_false (a : Prop) : (a ↔ false) ↔ ¬ a := iff.intro and.left iff_false_intro attribute [simp] theorem false_iff (a : Prop) : (false ↔ a) ↔ ¬ a := iff.trans iff.comm (iff_false a) attribute [simp] theorem iff_self (a : Prop) : (a ↔ a) ↔ true := iff_true_intro iff.rfl attribute [congr] theorem iff_congr (H1 : a ↔ c) (H2 : b ↔ d) : (a ↔ b) ↔ (c ↔ d) := and_congr (imp_congr H1 H2) (imp_congr H2 H1) /- exists -/ inductive Exists {A : Type u} (P : A → Prop) : Prop | intro : ∀ (a : A), P a → Exists attribute Exists.intro [intro] definition exists.intro := @Exists.intro notation `exists` binders `, ` r:(scoped P, Exists P) := r notation `∃` binders `, ` r:(scoped P, Exists P) := r attribute Exists.rec [elim] theorem exists.elim {A : Type u} {p : A → Prop} {B : Prop} (H1 : ∃x, p x) (H2 : ∀ (a : A), p a → B) : B := Exists.rec H2 H1 /- exists unique -/ definition exists_unique {A : Type u} (p : A → Prop) := ∃x, p x ∧ ∀y, p y → y = x notation `∃!` binders `, ` r:(scoped P, exists_unique P) := r attribute [intro] theorem exists_unique.intro {A : Type u} {p : A → Prop} (w : A) (H1 : p w) (H2 : ∀y, p y → y = w) : ∃!x, p x := exists.intro w ⟨H1, H2⟩ attribute [recursor 4, elim] theorem exists_unique.elim {A : Type u} {p : A → Prop} {b : Prop} (H2 : ∃!x, p x) (H1 : ∀x, p x → (∀y, p y → y = x) → b) : b := exists.elim H2 (λ w Hw, H1 w (and.left Hw) (and.right Hw)) theorem exists_unique_of_exists_of_unique {A : Type u} {p : A → Prop} (Hex : ∃ x, p x) (Hunique : ∀ y₁ y₂, p y₁ → p y₂ → y₁ = y₂) : ∃! x, p x := exists.elim Hex (λ x px, exists_unique.intro x px (take y, suppose p y, Hunique y x this px)) theorem exists_of_exists_unique {A : Type u} {p : A → Prop} (H : ∃! x, p x) : ∃ x, p x := exists.elim H (λ x Hx, ⟨x, and.left Hx⟩) theorem unique_of_exists_unique {A : Type u} {p : A → Prop} (H : ∃! x, p x) {y₁ y₂ : A} (py₁ : p y₁) (py₂ : p y₂) : y₁ = y₂ := exists_unique.elim H (take x, suppose p x, assume unique : ∀ y, p y → y = x, show y₁ = y₂, from eq.trans (unique _ py₁) (eq.symm (unique _ py₂))) /- exists, forall, exists unique congruences -/ attribute [congr] theorem forall_congr {A : Type u} {P Q : A → Prop} (H : ∀a, (P a ↔ Q a)) : (∀a, P a) ↔ ∀a, Q a := iff.intro (λ p a, iff.mp (H a) (p a)) (λ q a, iff.mpr (H a) (q a)) theorem exists_imp_exists {A : Type u} {P Q : A → Prop} (H : ∀a, (P a → Q a)) (p : ∃a, P a) : ∃a, Q a := exists.elim p (λ a Hp, ⟨a, H a Hp⟩) attribute [congr] theorem exists_congr {A : Type u} {P Q : A → Prop} (H : ∀a, (P a ↔ Q a)) : (Exists P) ↔ ∃a, Q a := iff.intro (exists_imp_exists (λa, iff.mp (H a))) (exists_imp_exists (λa, iff.mpr (H a))) attribute [congr] theorem exists_unique_congr {A : Type u} {p₁ p₂ : A → Prop} (H : ∀ x, p₁ x ↔ p₂ x) : (exists_unique p₁) ↔ (∃! x, p₂ x) := -- exists_congr (λ x, and_congr (H x) (forall_congr (λy, imp_congr (H y) iff.rfl))) /- decidable -/ inductive [class] decidable (p : Prop) | is_false : ¬p → decidable | is_true : p → decidable export decidable (is_true is_false) attribute [instance] definition decidable_true : decidable true := is_true trivial attribute [instance] definition decidable_false : decidable false := is_false not_false -- We use "dependent" if-then-else to be able to communicate the if-then-else condition -- to the branches attribute [inline] definition dite (c : Prop) [H : decidable c] {A : Type u} : (c → A) → (¬ c → A) → A := λ t e, decidable.rec_on H e t /- if-then-else -/ attribute [inline] definition ite (c : Prop) [H : decidable c] {A : Type u} (t e : A) : A := decidable.rec_on H (λ Hnc, e) (λ Hc, t) namespace decidable variables {p q : Prop} definition rec_on_true [H : decidable p] {H1 : p → Type u} {H2 : ¬p → Type u} (H3 : p) (H4 : H1 H3) : decidable.rec_on H H2 H1 := decidable.rec_on H (λh, false.rec _ (h H3)) (λh, H4) definition rec_on_false [H : decidable p] {H1 : p → Type u} {H2 : ¬p → Type u} (H3 : ¬p) (H4 : H2 H3) : decidable.rec_on H H2 H1 := decidable.rec_on H (λh, H4) (λh, false.rec _ (H3 h)) definition by_cases {q : Type u} [C : decidable p] : (p → q) → (¬p → q) → q := dite _ theorem em (p : Prop) [decidable p] : p ∨ ¬p := by_cases or.inl or.inr theorem by_contradiction [decidable p] (H : ¬p → false) : p := if H1 : p then H1 else false.rec _ (H H1) definition to_bool (p : Prop) [H : decidable p] : bool := decidable.cases_on H (λ H₁, bool.ff) (λ H₂, bool.tt) end decidable section variables {p q : Prop} definition decidable_of_decidable_of_iff (Hp : decidable p) (H : p ↔ q) : decidable q := if Hp : p then is_true (iff.mp H Hp) else is_false (iff.mp (not_iff_not_of_iff H) Hp) definition decidable_of_decidable_of_eq (Hp : decidable p) (H : p = q) : decidable q := decidable_of_decidable_of_iff Hp (iff.of_eq H) protected definition or.by_cases [decidable p] [decidable q] {A : Type u} (h : p ∨ q) (h₁ : p → A) (h₂ : q → A) : A := if hp : p then h₁ hp else if hq : q then h₂ hq else false.rec _ (or.elim h hp hq) end section variables {p q : Prop} attribute [instance] definition decidable_and [decidable p] [decidable q] : decidable (p ∧ q) := if hp : p then if hq : q then is_true ⟨hp, hq⟩ else is_false (assume H : p ∧ q, hq (and.right H)) else is_false (assume H : p ∧ q, hp (and.left H)) attribute [instance] definition decidable_or [decidable p] [decidable q] : decidable (p ∨ q) := if hp : p then is_true (or.inl hp) else if hq : q then is_true (or.inr hq) else is_false (or.rec hp hq) attribute [instance] definition decidable_not [decidable p] : decidable (¬p) := if hp : p then is_false (absurd hp) else is_true hp attribute [instance] definition decidable_implies [decidable p] [decidable q] : decidable (p → q) := if hp : p then if hq : q then is_true (assume H, hq) else is_false (assume H : p → q, absurd (H hp) hq) else is_true (assume Hp, absurd Hp hp) attribute [instance] definition decidable_iff [decidable p] [decidable q] : decidable (p ↔ q) := decidable_and end attribute [reducible] definition decidable_pred {A : Type u} (R : A → Prop) := Π (a : A), decidable (R a) attribute [reducible] definition decidable_rel {A : Type u} (R : A → A → Prop) := Π (a b : A), decidable (R a b) attribute [reducible] definition decidable_eq (A : Type u) := decidable_rel (@eq A) attribute [instance] definition decidable_ne {A : Type u} [decidable_eq A] (a b : A) : decidable (a ≠ b) := decidable_implies theorem bool.ff_ne_tt : ff = tt → false . definition is_dec_eq {A : Type u} (p : A → A → bool) : Prop := ∀ ⦃x y : A⦄, p x y = tt → x = y definition is_dec_refl {A : Type u} (p : A → A → bool) : Prop := ∀x, p x x = tt open decidable attribute [instance] protected definition bool.has_decidable_eq : ∀a b : bool, decidable (a = b) | ff ff := is_true rfl | ff tt := is_false bool.ff_ne_tt | tt ff := is_false (ne.symm bool.ff_ne_tt) | tt tt := is_true rfl definition decidable_eq_of_bool_pred {A : Type u} {p : A → A → bool} (H₁ : is_dec_eq p) (H₂ : is_dec_refl p) : decidable_eq A := take x y : A, if Hp : p x y = tt then is_true (H₁ Hp) else is_false (assume Hxy : x = y, absurd (H₂ y) (@eq.rec_on _ _ (λ z, ¬p z y = tt) _ Hxy Hp)) theorem decidable_eq_inl_refl {A : Type u} [H : decidable_eq A] (a : A) : H a a = is_true (eq.refl a) := match (H a a) with | (is_true e) := rfl | (is_false n) := absurd rfl n end theorem decidable_eq_inr_neg {A : Type u} [H : decidable_eq A] {a b : A} : Π n : a ≠ b, H a b = is_false n := assume n, match (H a b) with | (is_true e) := absurd e n | (is_false n₁) := proof_irrel n n₁ ▸ eq.refl (is_false n) end /- inhabited -/ inductive [class] inhabited (A : Type u) | mk : A → inhabited attribute [inline] protected definition inhabited.value {A : Type u} : inhabited A → A := λ h, inhabited.rec (λ a, a) h attribute [inline] protected definition inhabited.destruct {A : Type u} {B : Type v} (H1 : inhabited A) (H2 : A → B) : B := inhabited.rec H2 H1 attribute [inline] definition default (A : Type u) [H : inhabited A] : A := inhabited.value H attribute [inline, irreducible] definition arbitrary (A : Type u) [H : inhabited A] : A := inhabited.value H attribute [instance] definition Prop.is_inhabited : inhabited Prop := ⟨true⟩ attribute [instance] definition inhabited_fun (A : Type u) {B : Type v} [H : inhabited B] : inhabited (A → B) := inhabited.rec_on H (λ b, ⟨λ a, b⟩) attribute [instance] definition inhabited_Pi (A : Type u) {B : A → Type v} [Πx, inhabited (B x)] : inhabited (Πx, B x) := ⟨λ a, default (B a)⟩ attribute [inline, instance] protected definition bool.is_inhabited : inhabited bool := ⟨ff⟩ attribute [inline, instance] protected definition pos_num.is_inhabited : inhabited pos_num := ⟨pos_num.one⟩ attribute [inline, instance] protected definition num.is_inhabited : inhabited num := ⟨num.zero⟩ inductive [class] nonempty (A : Type u) : Prop | intro : A → nonempty protected definition nonempty.elim {A : Type u} {B : Prop} (H1 : nonempty A) (H2 : A → B) : B := nonempty.rec H2 H1 attribute [instance] theorem nonempty_of_inhabited {A : Type u} [inhabited A] : nonempty A := ⟨default A⟩ theorem nonempty_of_exists {A : Type u} {P : A → Prop} : (∃x, P x) → nonempty A | ⟨w, h⟩ := ⟨w⟩ /- subsingleton -/ inductive [class] subsingleton (A : Type u) : Prop | intro : (∀ a b : A, a = b) → subsingleton protected definition subsingleton.elim {A : Type u} [H : subsingleton A] : ∀(a b : A), a = b := subsingleton.rec (λp, p) H protected definition subsingleton.helim {A B : Type u} [H : subsingleton A] (h : A = B) : ∀ (a : A) (b : B), a == b := eq.rec_on h (λ a b : A, heq_of_eq (subsingleton.elim a b)) attribute [instance] definition subsingleton_prop (p : Prop) : subsingleton p := ⟨λ a b, proof_irrel a b⟩ attribute [instance] definition subsingleton_decidable (p : Prop) : subsingleton (decidable p) := subsingleton.intro (λ d₁, match d₁ with | (is_true t₁) := (λ d₂, match d₂ with | (is_true t₂) := eq.rec_on (proof_irrel t₁ t₂) rfl | (is_false f₂) := absurd t₁ f₂ end) | (is_false f₁) := (λ d₂, match d₂ with | (is_true t₂) := absurd t₂ f₁ | (is_false f₂) := eq.rec_on (proof_irrel f₁ f₂) rfl end) end) protected theorem rec_subsingleton {p : Prop} [H : decidable p] {H1 : p → Type u} {H2 : ¬p → Type u} [H3 : Π(h : p), subsingleton (H1 h)] [H4 : Π(h : ¬p), subsingleton (H2 h)] : subsingleton (decidable.rec_on H H2 H1) := match H with | (is_true h) := H3 h | (is_false h) := H4 h end theorem if_pos {c : Prop} [H : decidable c] (Hc : c) {A : Type u} {t e : A} : (ite c t e) = t := match H with | (is_true Hc) := rfl | (is_false Hnc) := absurd Hc Hnc end theorem if_neg {c : Prop} [H : decidable c] (Hnc : ¬c) {A : Type u} {t e : A} : (ite c t e) = e := match H with | (is_true Hc) := absurd Hc Hnc | (is_false Hnc) := rfl end attribute [simp] theorem if_t_t (c : Prop) [H : decidable c] {A : Type u} (t : A) : (ite c t t) = t := match H with | (is_true Hc) := rfl | (is_false Hnc) := rfl end theorem implies_of_if_pos {c t e : Prop} [decidable c] (h : ite c t e) : c → t := assume Hc, eq.rec_on (if_pos Hc : ite c t e = t) h theorem implies_of_if_neg {c t e : Prop} [decidable c] (h : ite c t e) : ¬c → e := assume Hnc, eq.rec_on (if_neg Hnc : ite c t e = e) h theorem if_ctx_congr {A : Type u} {b c : Prop} [dec_b : decidable b] [dec_c : decidable c] {x y u v : A} (h_c : b ↔ c) (h_t : c → x = u) (h_e : ¬c → y = v) : ite b x y = ite c u v := match dec_b, dec_c with | (is_false h₁), (is_false h₂) := h_e h₂ | (is_true h₁), (is_true h₂) := h_t h₂ | (is_false h₁), (is_true h₂) := absurd h₂ (iff.mp (not_iff_not_of_iff h_c) h₁) | (is_true h₁), (is_false h₂) := absurd h₁ (iff.mpr (not_iff_not_of_iff h_c) h₂) end attribute [congr] theorem if_congr {A : Type u} {b c : Prop} [dec_b : decidable b] [dec_c : decidable c] {x y u v : A} (h_c : b ↔ c) (h_t : x = u) (h_e : y = v) : ite b x y = ite c u v := @if_ctx_congr A b c dec_b dec_c x y u v h_c (λ h, h_t) (λ h, h_e) theorem if_ctx_simp_congr {A : Type u} {b c : Prop} [dec_b : decidable b] {x y u v : A} (h_c : b ↔ c) (h_t : c → x = u) (h_e : ¬c → y = v) : ite b x y = (@ite c (decidable_of_decidable_of_iff dec_b h_c) A u v) := @if_ctx_congr A b c dec_b (decidable_of_decidable_of_iff dec_b h_c) x y u v h_c h_t h_e attribute [congr] theorem if_simp_congr {A : Type u} {b c : Prop} [dec_b : decidable b] {x y u v : A} (h_c : b ↔ c) (h_t : x = u) (h_e : y = v) : ite b x y = (@ite c (decidable_of_decidable_of_iff dec_b h_c) A u v) := @if_ctx_simp_congr A b c dec_b x y u v h_c (λ h, h_t) (λ h, h_e) attribute [simp] definition if_true {A : Type u} (t e : A) : (if true then t else e) = t := if_pos trivial attribute [simp] definition if_false {A : Type u} (t e : A) : (if false then t else e) = e := if_neg not_false theorem if_ctx_congr_prop {b c x y u v : Prop} [dec_b : decidable b] [dec_c : decidable c] (h_c : b ↔ c) (h_t : c → (x ↔ u)) (h_e : ¬c → (y ↔ v)) : ite b x y ↔ ite c u v := match dec_b, dec_c with | (is_false h₁), (is_false h₂) := h_e h₂ | (is_true h₁), (is_true h₂) := h_t h₂ | (is_false h₁), (is_true h₂) := absurd h₂ (iff.mp (not_iff_not_of_iff h_c) h₁) | (is_true h₁), (is_false h₂) := absurd h₁ (iff.mpr (not_iff_not_of_iff h_c) h₂) end attribute [congr] theorem if_congr_prop {b c x y u v : Prop} [dec_b : decidable b] [dec_c : decidable c] (h_c : b ↔ c) (h_t : x ↔ u) (h_e : y ↔ v) : ite b x y ↔ ite c u v := if_ctx_congr_prop h_c (λ h, h_t) (λ h, h_e) theorem if_ctx_simp_congr_prop {b c x y u v : Prop} [dec_b : decidable b] (h_c : b ↔ c) (h_t : c → (x ↔ u)) (h_e : ¬c → (y ↔ v)) : ite b x y ↔ (@ite c (decidable_of_decidable_of_iff dec_b h_c) Prop u v) := @if_ctx_congr_prop b c x y u v dec_b (decidable_of_decidable_of_iff dec_b h_c) h_c h_t h_e attribute [congr] theorem if_simp_congr_prop {b c x y u v : Prop} [dec_b : decidable b] (h_c : b ↔ c) (h_t : x ↔ u) (h_e : y ↔ v) : ite b x y ↔ (@ite c (decidable_of_decidable_of_iff dec_b h_c) Prop u v) := @if_ctx_simp_congr_prop b c x y u v dec_b h_c (λ h, h_t) (λ h, h_e) theorem dif_pos {c : Prop} [H : decidable c] (Hc : c) {A : Type u} {t : c → A} {e : ¬ c → A} : dite c t e = t Hc := match H with | (is_true Hc) := rfl | (is_false Hnc) := absurd Hc Hnc end theorem dif_neg {c : Prop} [H : decidable c] (Hnc : ¬c) {A : Type u} {t : c → A} {e : ¬ c → A} : dite c t e = e Hnc := match H with | (is_true Hc) := absurd Hc Hnc | (is_false Hnc) := rfl end theorem dif_ctx_congr {A : Type u} {b c : Prop} [dec_b : decidable b] [dec_c : decidable c] {x : b → A} {u : c → A} {y : ¬b → A} {v : ¬c → A} (h_c : b ↔ c) (h_t : ∀ (h : c), x (iff.mpr h_c h) = u h) (h_e : ∀ (h : ¬c), y (iff.mpr (not_iff_not_of_iff h_c) h) = v h) : (@dite b dec_b A x y) = (@dite c dec_c A u v) := match dec_b, dec_c with | (is_false h₁), (is_false h₂) := h_e h₂ | (is_true h₁), (is_true h₂) := h_t h₂ | (is_false h₁), (is_true h₂) := absurd h₂ (iff.mp (not_iff_not_of_iff h_c) h₁) | (is_true h₁), (is_false h₂) := absurd h₁ (iff.mpr (not_iff_not_of_iff h_c) h₂) end theorem dif_ctx_simp_congr {A : Type u} {b c : Prop} [dec_b : decidable b] {x : b → A} {u : c → A} {y : ¬b → A} {v : ¬c → A} (h_c : b ↔ c) (h_t : ∀ (h : c), x (iff.mpr h_c h) = u h) (h_e : ∀ (h : ¬c), y (iff.mpr (not_iff_not_of_iff h_c) h) = v h) : (@dite b dec_b A x y) = (@dite c (decidable_of_decidable_of_iff dec_b h_c) A u v) := @dif_ctx_congr A b c dec_b (decidable_of_decidable_of_iff dec_b h_c) x u y v h_c h_t h_e -- Remark: dite and ite are "definitionally equal" when we ignore the proofs. theorem dite_ite_eq (c : Prop) [H : decidable c] {A : Type u} (t : A) (e : A) : dite c (λh, t) (λh, e) = ite c t e := match H with | (is_true Hc) := rfl | (is_false Hnc) := rfl end -- The following symbols should not be considered in the pattern inference procedure used by -- heuristic instantiation. attribute and or not iff ite dite eq ne heq [no_pattern] definition as_true (c : Prop) [decidable c] : Prop := if c then true else false definition as_false (c : Prop) [decidable c] : Prop := if c then false else true definition of_as_true {c : Prop} [H₁ : decidable c] (H₂ : as_true c) : c := match H₁, H₂ with | (is_true h_c), H₂ := h_c | (is_false h_c), H₂ := false.elim H₂ end -- namespace used to collect congruence rules for "contextual simplification" namespace contextual attribute if_ctx_simp_congr [congr] attribute if_ctx_simp_congr_prop [congr] attribute dif_ctx_simp_congr [congr] end contextual
ad1fb850d634cc5cdced4e08d064952b72416d08
453dcd7c0d1ef170b0843a81d7d8caedc9741dce
/group_theory/coset.lean
9f943c09fa41046228325f47ba151d100d2ae4fa
[ "Apache-2.0" ]
permissive
amswerdlow/mathlib
9af77a1f08486d8fa059448ae2d97795bd12ec0c
27f96e30b9c9bf518341705c99d641c38638dfd0
refs/heads/master
1,585,200,953,598
1,534,275,532,000
1,534,275,532,000
144,564,700
0
0
null
1,534,156,197,000
1,534,156,197,000
null
UTF-8
Lean
false
false
8,184
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*} def left_coset [has_mul α] (a : α) (s : set α) : set α := (λ x, a * x) '' s def right_coset [has_mul α] (s : set α) (a : α) : set α := (λ x, x * a) '' s local infix ` *l `:70 := left_coset local infix ` *r `:70 := right_coset section coset_mul variable [has_mul α] lemma mem_left_coset {s : set α} {x : α} (a : α) (hxS : x ∈ s) : a * x ∈ a *l s := mem_image_of_mem (λ b : α, a * b) hxS lemma mem_right_coset {s : set α} {x : α} (a : α) (hxS : x ∈ s) : x * a ∈ s *r a := mem_image_of_mem (λ b : α, b * a) hxS def left_coset_equiv (s : set α) (a b : α) := a *l s = b *l s 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] @[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] 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] @[simp] lemma right_coset_one : s *r 1 = s := set.ext $ by simp [right_coset] end coset_monoid section coset_submonoid open is_submonoid variables [monoid α] (s : set α) [is_submonoid s] 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) 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) lemma mem_left_coset_left_coset {a : α} (ha : a *l s = s) : a ∈ s := by rw [←ha]; exact mem_own_left_coset s a 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 : α} 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⟩) 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] 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)] 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)] 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] 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⟩ 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 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⟩ def left_cosets [group α] (s : set α) [is_subgroup s] : Type* := quotient (left_rel s) namespace left_cosets variables [group α] {s : set α} [is_subgroup s] def mk (a : α) : left_cosets s := quotient.mk' a instance : has_coe α (left_cosets s) := ⟨mk⟩ instance [group α] (s : set α) [is_subgroup s] : inhabited (left_cosets s) := ⟨((1 : α) : left_cosets s)⟩ @[simp] protected lemma eq {a b : α} : (a : left_cosets s) = b ↔ a⁻¹ * b ∈ s := quotient.eq' lemma eq_class_eq_left_coset [group α] (s : set α) [is_subgroup s] (g : α) : {x : α | (x : left_cosets s) = g} = left_coset g s := set.ext $ λ z, by simp [eq_comm, mem_left_coset_iff]; refl end left_cosets namespace is_subgroup 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⟩ local attribute [instance] left_rel noncomputable def group_equiv_left_cosets_times_subgroup (hs : is_subgroup s) : α ≃ (left_cosets s × s) := calc α ≃ Σ L : left_cosets s, {x : α // (x : left_cosets s)= L} : equiv.equiv_fib left_cosets.mk ... ≃ Σ L : left_cosets s, left_coset (quotient.out' L) s : equiv.sigma_congr_right (λ L, begin rw ← left_cosets.eq_class_eq_left_coset, show {x // quotient.mk' x = L} ≃ {x : α // quotient.mk' x = quotient.mk' _}, simp [-quotient.eq'] end) ... ≃ Σ L : left_cosets s, s : equiv.sigma_congr_right (λ L, left_coset_equiv_subgroup _) ... ≃ (left_cosets s × s) : equiv.sigma_equiv_prod _ _ end is_subgroup namespace left_cosets instance [group α] (s : set α) [normal_subgroup s] : group (left_cosets s) := { one := (1 : α), mul := λ a b, quotient.lift_on₂' a b (λ a b, ((a * b : α) : left_cosets s)) (λ a₁ a₂ b₁ b₂ hab₁ hab₂, quot.sound ((is_subgroup.mul_mem_cancel_left s (is_subgroup.inv_mem hab₂)).1 (by rw [mul_inv_rev, mul_inv_rev, ← mul_assoc (a₂⁻¹ * a₁⁻¹), mul_assoc _ b₂, ← mul_assoc b₂, mul_inv_self, one_mul, mul_assoc (a₂⁻¹)]; exact normal_subgroup.normal _ hab₁ _))), mul_assoc := λ a b c, quotient.induction_on₃' a b c (λ a b c, congr_arg mk (mul_assoc a b c)), one_mul := λ a, quotient.induction_on' a (λ a, congr_arg mk (one_mul a)), mul_one := λ a, quotient.induction_on' a (λ a, congr_arg mk (mul_one a)), inv := λ a, quotient.lift_on' a (λ a, ((a⁻¹ : α) : left_cosets s)) (λ a b hab, quotient.sound' begin show a⁻¹⁻¹ * b⁻¹ ∈ s, rw ← mul_inv_rev, exact is_subgroup.inv_mem (is_subgroup.mem_norm_comm hab) end), mul_left_inv := λ a, quotient.induction_on' a (λ a, congr_arg mk (mul_left_inv a)) } instance [group α] (s : set α) [normal_subgroup s] : is_group_hom (mk : α → left_cosets s) := ⟨λ _ _, rfl⟩ instance [comm_group α] (s : set α) [normal_subgroup s] : comm_group (left_cosets s) := { mul_comm := λ a b, quotient.induction_on₂' a b (λ a b, congr_arg mk (mul_comm a b)), ..left_cosets.group s } @[simp] lemma coe_one [group α] (s : set α) [normal_subgroup s] : ((1 : α) : left_cosets s) = 1 := rfl @[simp] lemma coe_mul [group α] (s : set α) [normal_subgroup s] (a b : α) : ((a * b : α) : left_cosets s) = a * b := rfl @[simp] lemma coe_inv [group α] (s : set α) [normal_subgroup s] (a : α) : ((a⁻¹ : α) : left_cosets s) = a⁻¹ := rfl @[simp] lemma coe_pow [group α] (s : set α) [normal_subgroup s] (a : α) (n : ℕ) : ((a ^ n : α) : left_cosets s) = a ^ n := @is_group_hom.pow _ _ _ _ mk _ a n @[simp] lemma coe_gpow [group α] (s : set α) [normal_subgroup s] (a : α) (n : ℤ) : ((a ^ n : α) : left_cosets s) = a ^ n := @is_group_hom.gpow _ _ _ _ mk _ a n end left_cosets
d228a7c9cc72addc398e5a621781fe0182ac65e1
968e2f50b755d3048175f176376eff7139e9df70
/examples/prop_logic_theory/unnamed_2674.lean
c92eaa6f43dbd33a599b70d98dfde37a7bf22255
[]
no_license
gihanmarasingha/mth1001_sphinx
190a003269ba5e54717b448302a27ca26e31d491
05126586cbf5786e521be1ea2ef5b4ba3c44e74a
refs/heads/master
1,672,913,933,677
1,604,516,583,000
1,604,516,583,000
309,245,750
1
0
null
null
null
null
UTF-8
Lean
false
false
790
lean
import logic.basic variables {p q : Prop} local attribute [instance] classical.prop_decidable namespace hidden -- BEGIN theorem not_imp_not : (¬q → ¬p) ↔ (p → q) := begin split, -- By iff intro., it suffices to prove 1. `¬q → ¬p → p → q` and 2. `p → q → ¬q → ¬p`. { intros h₁ h₂, -- Assume `h₁ : ¬q → ¬p`, `h₂ : p`. It suffices to prove `q`. by_contradiction h₃, -- For a contradiction, assume `h₃ : ¬q`. It suffices to prove `false`. have h₄ : ¬p, from h₁ h₃, -- We have `h₄ : ¬p` by implication elimination on `h₁` and `h₃`. show false, from h₄ h₂, -- We show `false` by false introduction on `h₄` and `h₂`. }, { exact mt }, -- We show `(p → q) → (¬q → ¬p)` by modus tollens. end -- END end hidden
d8b6c06489db97f92bd589f327e938e2482b69f7
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/cody2.lean
86cb26269a33838dcfaed242df136a03e7fe311e
[ "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
442
lean
import logic open eq definition subsets (P : Type) := P → Prop. section hypothesis A : Type. hypothesis r : A → subsets A. hypothesis i : subsets A → A. hypothesis retract {P : subsets A} {a : A} : r (i P) a = P a. definition delta (a:A) : Prop := ¬ (r a a). local notation `δ` := delta. theorem delta_aux : ¬ (δ (i delta)) := assume H : δ (i delta), H (subst (symm retract) H). check delta_aux. end
7813879f8c79cffe6a773dd2cb193ce0424200ec
7c2dd01406c42053207061adb11703dc7ce0b5e5
/src/exercises/01_equality_rewriting.lean
c95d187003e38c3dd58904bb112c496d04fba96d
[ "Apache-2.0" ]
permissive
leanprover-community/tutorials
50ec79564cbf2ad1afd1ac43d8ee3c592c2883a8
79a6872a755c4ae0c2aca57e1adfdac38b1d8bb1
refs/heads/master
1,687,466,144,386
1,672,061,276,000
1,672,061,276,000
189,169,918
186
81
Apache-2.0
1,686,350,300,000
1,559,113,678,000
Lean
UTF-8
Lean
false
false
4,947
lean
import data.real.basic /- One of the earliest kind of proofs one encounters while learning mathematics is proving by a calculation. It may not sound like a proof, but this is actually using lemmas expressing properties of operations on numbers. It also uses the fundamental property of equality: if two mathematical objects A and B are equal then, in any statement involving A, one can replace A by B. This operation is called rewriting, and the Lean "tactic" for this is `rw`. In the following exercises, we will use the following two lemmas: mul_assoc a b c : a * b * c = a * (b * c) mul_comm a b : a*b = b*a Hence the command rw mul_assoc a b c, will replace a*b*c by a*(b*c) in the current goal. In order to replace backward, we use rw ← mul_assoc a b c, replacing a*(b*c) by a*b*c in the current goal. Of course we don't want to constantly invoke those lemmas, and we will eventually introduce more powerful solutions. -/ -- Uncomment the following line if you want to see parentheses around subexpressions. -- set_option pp.parens true example (a b c : ℝ) : (a * b) * c = b * (a * c) := begin rw mul_comm a b, rw mul_assoc b a c, end -- 0001 example (a b c : ℝ) : (c * b) * a = b * (a * c) := begin sorry end -- 0002 example (a b c : ℝ) : a * (b * c) = b * (a * c) := begin sorry end /- Now let's return to the preceding example to experiment with what happens if we don't give arguments to mul_assoc or mul_comm. For instance, you can start the next proof with rw ← mul_assoc, Try to figure out what happens. -/ -- 0003 example (a b c : ℝ) : a * (b * c) = b * (a * c) := begin sorry end /- We can also perform rewriting in an assumption of the local context, using for instance rw mul_comm a b at hyp, in order to replace a*b by b*a in assumption hyp. The next example will use a third lemma: two_mul a : 2*a = a + a Also we use the `exact` tactic, which allows to provide a direct proof term. -/ example (a b c d : ℝ) (hyp : c = d*a + b) (hyp' : b = a*d) : c = 2*a*d := begin rw hyp' at hyp, rw mul_comm d a at hyp, rw ← two_mul (a*d) at hyp, rw ← mul_assoc 2 a d at hyp, exact hyp, -- Our assumption hyp is now exactly what we have to prove end /- And the next one can use: sub_self x : x - x = 0 -/ -- 0004 example (a b c d : ℝ) (hyp : c = b*a - d) (hyp' : d = a*b) : c = 0 := begin sorry end /- What is written in the two preceding example is very far away from what we would write on paper. Let's now see how to get a more natural layout. Inside each pair of curly braces below, the goal is to prove equality with the preceding line. -/ example (a b c d : ℝ) (hyp : c = d*a + b) (hyp' : b = a*d) : c = 2*a*d := begin calc c = d*a + b : by { rw hyp } ... = d*a + a*d : by { rw hyp' } ... = a*d + a*d : by { rw mul_comm d a } ... = 2*(a*d) : by { rw two_mul } ... = 2*a*d : by { rw mul_assoc }, end /- Let's note there is no comma at the end of each line of calculation. `calc` is really one command, and the comma comes only after it's fully done. From a practical point of view, when writing such a proof, it is convenient to: * pause the tactic state view update in VScode by clicking the Pause icon button in the top right corner of the Lean Goal buffer * write the full calculation, ending each line with ": by {}" * resume tactic state update by clicking the Play icon button and fill in proofs between curly braces. Let's return to the other example using this method. -/ -- 0005 example (a b c d : ℝ) (hyp : c = b*a - d) (hyp' : d = a*b) : c = 0 := begin sorry end /- The preceding proofs have exhausted our supply of "mul_comm" patience. Now it's time to get the computer to work harder. The `ring` tactic will prove any goal that follows by applying only the axioms of commutative (semi-)rings, in particular commutativity and associativity of addition and multiplication, as well as distributivity. We also note that curly braces are not necessary when we write a single tactic proof, so let's get rid of them. -/ example (a b c d : ℝ) (hyp : c = d*a + b) (hyp' : b = a*d) : c = 2*a*d := begin calc c = d*a + b : by rw hyp ... = d*a + a*d : by rw hyp' ... = 2*a*d : by ring, end /- Of course we can use `ring` outside of `calc`. Let's do the next one in one line. -/ -- 0006 example (a b c : ℝ) : a * (b * c) = b * (a * c) := begin sorry end /- This is too much fun. Let's do it again. -/ -- 0007 example (a b : ℝ) : (a + b) + a = 2*a + b := begin sorry end /- Maybe this is cheating. Let's try to do the next computation without ring. We could use: pow_two x : x^2 = x*x mul_sub a b c : a*(b-c) = a*b - a*c add_mul a b c : (a+b)*c = a*c + b*c add_sub a b c : a + (b - c) = (a + b) - c sub_sub a b c : a - b - c = a - (b + c) add_zero a : a + 0 = a -/ -- 0008 example (a b : ℝ) : (a + b)*(a - b) = a^2 - b^2 := begin sorry end /- Let's stick to ring in the end. -/
f0d253480bd75b8a99ece7ec40ca866c6319e28b
4bcaca5dc83d49803f72b7b5920b75b6e7d9de2d
/stage0/src/Init/Data/Nat/Basic.lean
dd9205c92aea0fae8d4388b3fbbcaaa0a6c69941
[ "Apache-2.0" ]
permissive
subfish-zhou/leanprover-zh_CN.github.io
30b9fba9bd790720bd95764e61ae796697d2f603
8b2985d4a3d458ceda9361ac454c28168d920d3f
refs/heads/master
1,689,709,967,820
1,632,503,056,000
1,632,503,056,000
409,962,097
1
0
null
null
null
null
UTF-8
Lean
false
false
14,519
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Leonardo de Moura -/ prelude import Init.SimpLemmas universe u namespace Nat @[specialize] def foldAux {α : Type u} (f : Nat → α → α) (s : Nat) : Nat → α → α | 0, a => a | succ n, a => foldAux f s n (f (s - (succ n)) a) @[inline] def fold {α : Type u} (f : Nat → α → α) (n : Nat) (init : α) : α := foldAux f n n init @[inline] def foldRev {α : Type u} (f : Nat → α → α) (n : Nat) (init : α) : α := let rec @[specialize] loop | 0, a => a | succ n, a => loop n (f n a) loop n init @[specialize] def anyAux (f : Nat → Bool) (s : Nat) : Nat → Bool | 0 => false | succ n => f (s - (succ n)) || anyAux f s n /- `any f n = true` iff there is `i in [0, n-1]` s.t. `f i = true` -/ @[inline] def any (f : Nat → Bool) (n : Nat) : Bool := anyAux f n n @[inline] def all (f : Nat → Bool) (n : Nat) : Bool := !any (fun i => !f i) n @[inline] def repeat {α : Type u} (f : α → α) (n : Nat) (a : α) : α := let rec @[specialize] loop | 0, a => a | succ n, a => loop n (f a) loop n a /- Nat.add theorems -/ @[simp] theorem zero_eq : Nat.zero = 0 := rfl @[simp] theorem add_eq : Nat.add x y = x + y := rfl @[simp] protected theorem zero_add : ∀ (n : Nat), 0 + n = n | 0 => rfl | n+1 => congrArg succ (Nat.zero_add n) theorem succ_add : ∀ (n m : Nat), (succ n) + m = succ (n + m) | n, 0 => rfl | n, m+1 => congrArg succ (succ_add n m) theorem add_succ (n m : Nat) : n + succ m = succ (n + m) := rfl theorem add_one (n : Nat) : n + 1 = succ n := rfl theorem succ_eq_add_one (n : Nat) : succ n = n + 1 := rfl protected theorem add_comm : ∀ (n m : Nat), n + m = m + n | n, 0 => Eq.symm (Nat.zero_add n) | n, m+1 => by have : succ (n + m) = succ (m + n) := by apply congrArg; apply Nat.add_comm rw [succ_add m n] apply this protected theorem add_assoc : ∀ (n m k : Nat), (n + m) + k = n + (m + k) | n, m, 0 => rfl | n, m, succ k => congrArg succ (Nat.add_assoc n m k) protected theorem add_left_comm (n m k : Nat) : n + (m + k) = m + (n + k) := by rw [← Nat.add_assoc, Nat.add_comm n m, Nat.add_assoc] protected theorem add_right_comm (n m k : Nat) : (n + m) + k = (n + k) + m := by rw [Nat.add_assoc, Nat.add_comm m k, ← Nat.add_assoc] protected theorem add_left_cancel {n m k : Nat} : n + m = n + k → m = k := by induction n with | zero => simp; intros; assumption | succ n ih => simp [succ_add]; intro h; apply ih h protected theorem add_right_cancel {n m k : Nat} (h : n + m = k + m) : n = k := by rw [Nat.add_comm n m, Nat.add_comm k m] at h apply Nat.add_left_cancel h /- Nat.mul theorems -/ @[simp] protected theorem mul_zero (n : Nat) : n * 0 = 0 := rfl theorem mul_succ (n m : Nat) : n * succ m = n * m + n := rfl @[simp] protected theorem zero_mul : ∀ (n : Nat), 0 * n = 0 | 0 => rfl | succ n => mul_succ 0 n ▸ (Nat.zero_mul n).symm ▸ rfl theorem succ_mul (n m : Nat) : (succ n) * m = (n * m) + m := by induction m with | zero => rfl | succ m ih => rw [mul_succ, add_succ, ih, mul_succ, add_succ, Nat.add_right_comm] protected theorem mul_comm : ∀ (n m : Nat), n * m = m * n | n, 0 => (Nat.zero_mul n).symm ▸ (Nat.mul_zero n).symm ▸ rfl | n, succ m => (mul_succ n m).symm ▸ (succ_mul m n).symm ▸ (Nat.mul_comm n m).symm ▸ rfl @[simp] protected theorem mul_one : ∀ (n : Nat), n * 1 = n := Nat.zero_add @[simp] protected theorem one_mul (n : Nat) : 1 * n = n := Nat.mul_comm n 1 ▸ Nat.mul_one n protected theorem left_distrib (n m k : Nat) : n * (m + k) = n * m + n * k := by induction n generalizing m k with | zero => repeat rw [Nat.zero_mul] | succ n ih => simp [succ_mul, ih]; rw [Nat.add_assoc, Nat.add_assoc (n*m)]; apply congrArg; apply Nat.add_left_comm protected theorem right_distrib (n m k : Nat) : (n + m) * k = n * k + m * k := have h₁ : (n + m) * k = k * (n + m) := Nat.mul_comm .. have h₂ : k * (n + m) = k * n + k * m := Nat.left_distrib .. have h₃ : k * n + k * m = n * k + k * m := Nat.mul_comm n k ▸ rfl have h₄ : n * k + k * m = n * k + m * k := Nat.mul_comm m k ▸ rfl ((h₁.trans h₂).trans h₃).trans h₄ protected theorem mul_add (n m k : Nat) : n * (m + k) = n * m + n * k := Nat.left_distrib n m k protected theorem add_mul (n m k : Nat) : (n + m) * k = n * k + m * k := Nat.right_distrib n m k protected theorem mul_assoc : ∀ (n m k : Nat), (n * m) * k = n * (m * k) | n, m, 0 => rfl | n, m, succ k => have h₁ : n * m * succ k = n * m * (k + 1) := rfl have h₂ : n * m * (k + 1) = (n * m * k) + n * m * 1 := Nat.left_distrib .. have h₃ : (n * m * k) + n * m * 1 = (n * m * k) + n * m := by rw [Nat.mul_one (n*m)] have h₄ : (n * m * k) + n * m = (n * (m * k)) + n * m := by rw [Nat.mul_assoc n m k] have h₅ : (n * (m * k)) + n * m = n * (m * k + m) := (Nat.left_distrib n (m*k) m).symm have h₆ : n * (m * k + m) = n * (m * succ k) := Nat.mul_succ m k ▸ rfl ((((h₁.trans h₂).trans h₃).trans h₄).trans h₅).trans h₆ protected theorem mul_left_comm (n m k : Nat) : n * (m * k) = m * (n * k) := by rw [← Nat.mul_assoc, Nat.mul_comm n m, Nat.mul_assoc] /- Inequalities -/ theorem succ_lt_succ {n m : Nat} : n < m → succ n < succ m := succ_le_succ theorem lt_succ_of_le {n m : Nat} : n ≤ m → n < succ m := succ_le_succ @[simp] protected theorem sub_zero (n : Nat) : n - 0 = n := rfl theorem succ_sub_succ_eq_sub (n m : Nat) : succ n - succ m = n - m := by induction m with | zero => exact rfl | succ m ih => apply congrArg pred ih theorem pred_le : ∀ (n : Nat), pred n ≤ n | zero => Nat.le.refl | succ n => le_succ _ theorem pred_lt : ∀ {n : Nat}, n ≠ 0 → pred n < n | zero, h => absurd rfl h | succ n, h => lt_succ_of_le (Nat.le_refl _) theorem sub_le (n m : Nat) : n - m ≤ n := by induction m with | zero => exact Nat.le_refl (n - 0) | succ m ih => apply Nat.le_trans (pred_le (n - m)) ih theorem sub_lt : ∀ {n m : Nat}, 0 < n → 0 < m → n - m < n | 0, m, h1, h2 => absurd h1 (Nat.lt_irrefl 0) | n+1, 0, h1, h2 => absurd h2 (Nat.lt_irrefl 0) | n+1, m+1, h1, h2 => Eq.symm (succ_sub_succ_eq_sub n m) ▸ show n - m < succ n from lt_succ_of_le (sub_le n m) theorem sub_succ (n m : Nat) : n - succ m = pred (n - m) := rfl theorem succ_sub_succ (n m : Nat) : succ n - succ m = n - m := succ_sub_succ_eq_sub n m protected theorem sub_self : ∀ (n : Nat), n - n = 0 | 0 => by rw [Nat.sub_zero] | (succ n) => by rw [succ_sub_succ, Nat.sub_self n] protected theorem lt_of_lt_of_le {n m k : Nat} : n < m → m ≤ k → n < k := Nat.le_trans protected theorem lt_of_lt_of_eq {n m k : Nat} : n < m → m = k → n < k := fun h₁ h₂ => h₂ ▸ h₁ instance : Trans (. < . : Nat → Nat → Prop) (. < . : Nat → Nat → Prop) (. < . : Nat → Nat → Prop) where trans := Nat.lt_trans instance : Trans (. ≤ . : Nat → Nat → Prop) (. ≤ . : Nat → Nat → Prop) (. ≤ . : Nat → Nat → Prop) where trans := Nat.le_trans instance : Trans (. < . : Nat → Nat → Prop) (. ≤ . : Nat → Nat → Prop) (. < . : Nat → Nat → Prop) where trans := Nat.lt_of_lt_of_le instance : Trans (. ≤ . : Nat → Nat → Prop) (. < . : Nat → Nat → Prop) (. < . : Nat → Nat → Prop) where trans := Nat.lt_of_le_of_lt protected theorem le_of_eq {n m : Nat} (p : n = m) : n ≤ m := p ▸ Nat.le_refl n theorem le_of_succ_le {n m : Nat} (h : succ n ≤ m) : n ≤ m := Nat.le_trans (le_succ n) h protected theorem le_of_lt {n m : Nat} (h : n < m) : n ≤ m := le_of_succ_le h def lt.step {n m : Nat} : n < m → n < succ m := le_step theorem eq_zero_or_pos : ∀ (n : Nat), n = 0 ∨ n > 0 | 0 => Or.inl rfl | n+1 => Or.inr (succ_pos _) def lt.base (n : Nat) : n < succ n := Nat.le_refl (succ n) theorem lt_succ_self (n : Nat) : n < succ n := lt.base n protected theorem le_total (m n : Nat) : m ≤ n ∨ n ≤ m := match Nat.lt_or_ge m n with | Or.inl h => Or.inl (Nat.le_of_lt h) | Or.inr h => Or.inr h protected theorem lt_of_le_and_ne {m n : Nat} (h₁ : m ≤ n) (h₂ : m ≠ n) : m < n := match Nat.eq_or_lt_of_le h₁ with | Or.inl h => absurd h h₂ | Or.inr h => h theorem eq_zero_of_le_zero {n : Nat} (h : n ≤ 0) : n = 0 := Nat.le_antisymm h (zero_le _) theorem lt_of_succ_lt {n m : Nat} : succ n < m → n < m := le_of_succ_le theorem lt_of_succ_lt_succ {n m : Nat} : succ n < succ m → n < m := le_of_succ_le_succ theorem lt_of_succ_le {n m : Nat} (h : succ n ≤ m) : n < m := h theorem succ_le_of_lt {n m : Nat} (h : n < m) : succ n ≤ m := h theorem lt_or_eq_or_le_succ {m n : Nat} (h : m ≤ succ n) : m ≤ n ∨ m = succ n := Decidable.byCases (fun (h' : m = succ n) => Or.inr h') (fun (h' : m ≠ succ n) => have : m < succ n := Nat.lt_of_le_and_ne h h' have : succ m ≤ succ n := succ_le_of_lt this Or.inl (le_of_succ_le_succ this)) theorem le_add_right : ∀ (n k : Nat), n ≤ n + k | n, 0 => Nat.le_refl n | n, k+1 => le_succ_of_le (le_add_right n k) theorem le_add_left (n m : Nat): n ≤ m + n := Nat.add_comm n m ▸ le_add_right n m theorem le.dest : ∀ {n m : Nat}, n ≤ m → Exists (fun k => n + k = m) | zero, zero, h => ⟨0, rfl⟩ | zero, succ n, h => ⟨succ n, Nat.add_comm 0 (succ n) ▸ rfl⟩ | succ n, zero, h => absurd h (not_succ_le_zero _) | succ n, succ m, h => have : n ≤ m := Nat.le_of_succ_le_succ h have : Exists (fun k => n + k = m) := dest this match this with | ⟨k, h⟩ => ⟨k, show succ n + k = succ m from ((succ_add n k).symm ▸ h ▸ rfl)⟩ theorem le.intro {n m k : Nat} (h : n + k = m) : n ≤ m := h ▸ le_add_right n k protected theorem not_le_of_gt {n m : Nat} (h : n > m) : ¬ n ≤ m := fun h₁ => match Nat.lt_or_ge n m with | Or.inl h₂ => absurd (Nat.lt_trans h h₂) (Nat.lt_irrefl _) | Or.inr h₂ => have Heq : n = m := Nat.le_antisymm h₁ h₂ absurd (@Eq.subst _ _ _ _ Heq h) (Nat.lt_irrefl m) theorem gt_of_not_le {n m : Nat} (h : ¬ n ≤ m) : n > m := match Nat.lt_or_ge m n with | Or.inl h₁ => h₁ | Or.inr h₁ => absurd h₁ h protected theorem add_le_add_left {n m : Nat} (h : n ≤ m) (k : Nat) : k + n ≤ k + m := match le.dest h with | ⟨w, hw⟩ => have h₁ : k + n + w = k + (n + w) := Nat.add_assoc .. have h₂ : k + (n + w) = k + m := congrArg _ hw le.intro <| h₁.trans h₂ protected theorem add_le_add_right {n m : Nat} (h : n ≤ m) (k : Nat) : n + k ≤ m + k := by rw [Nat.add_comm n k, Nat.add_comm m k] apply Nat.add_le_add_left assumption protected theorem add_lt_add_left {n m : Nat} (h : n < m) (k : Nat) : k + n < k + m := lt_of_succ_le (add_succ k n ▸ Nat.add_le_add_left (succ_le_of_lt h) k) protected theorem add_lt_add_right {n m : Nat} (h : n < m) (k : Nat) : n + k < m + k := Nat.add_comm k m ▸ Nat.add_comm k n ▸ Nat.add_lt_add_left h k protected theorem zero_lt_one : 0 < (1:Nat) := zero_lt_succ 0 theorem add_le_add {a b c d : Nat} (h₁ : a ≤ b) (h₂ : c ≤ d) : a + c ≤ b + d := Nat.le_trans (Nat.add_le_add_right h₁ c) (Nat.add_le_add_left h₂ b) theorem add_lt_add {a b c d : Nat} (h₁ : a < b) (h₂ : c < d) : a + c < b + d := Nat.lt_trans (Nat.add_lt_add_right h₁ c) (Nat.add_lt_add_left h₂ b) /- Basic theorems for comparing numerals -/ theorem ctor_eq_zero : Nat.zero = 0 := rfl protected theorem one_ne_zero : 1 ≠ (0 : Nat) := fun h => Nat.noConfusion h protected theorem zero_ne_one : 0 ≠ (1 : Nat) := fun h => Nat.noConfusion h theorem succ_ne_zero (n : Nat) : succ n ≠ 0 := fun h => Nat.noConfusion h /- mul + order -/ theorem mul_le_mul_left {n m : Nat} (k : Nat) (h : n ≤ m) : k * n ≤ k * m := match le.dest h with | ⟨l, hl⟩ => have : k * n + k * l = k * m := Nat.left_distrib k n l ▸ hl.symm ▸ rfl le.intro this theorem mul_le_mul_right {n m : Nat} (k : Nat) (h : n ≤ m) : n * k ≤ m * k := Nat.mul_comm k m ▸ Nat.mul_comm k n ▸ mul_le_mul_left k h protected theorem mul_le_mul {n₁ m₁ n₂ m₂ : Nat} (h₁ : n₁ ≤ n₂) (h₂ : m₁ ≤ m₂) : n₁ * m₁ ≤ n₂ * m₂ := Nat.le_trans (mul_le_mul_right _ h₁) (mul_le_mul_left _ h₂) protected theorem mul_lt_mul_of_pos_left {n m k : Nat} (h : n < m) (hk : k > 0) : k * n < k * m := Nat.lt_of_lt_of_le (Nat.add_lt_add_left hk _) (Nat.mul_succ k n ▸ Nat.mul_le_mul_left k (succ_le_of_lt h)) protected theorem mul_lt_mul_of_pos_right {n m k : Nat} (h : n < m) (hk : k > 0) : n * k < m * k := Nat.mul_comm k m ▸ Nat.mul_comm k n ▸ Nat.mul_lt_mul_of_pos_left h hk protected theorem mul_pos {n m : Nat} (ha : n > 0) (hb : m > 0) : n * m > 0 := have h : 0 * m < n * m := Nat.mul_lt_mul_of_pos_right ha hb Nat.zero_mul m ▸ h /- power -/ theorem pow_succ (n m : Nat) : n^(succ m) = n^m * n := rfl theorem pow_zero (n : Nat) : n^0 = 1 := rfl theorem pow_le_pow_of_le_left {n m : Nat} (h : n ≤ m) : ∀ (i : Nat), n^i ≤ m^i | 0 => Nat.le_refl _ | succ i => Nat.mul_le_mul (pow_le_pow_of_le_left h i) h theorem pow_le_pow_of_le_right {n : Nat} (hx : n > 0) {i : Nat} : ∀ {j}, i ≤ j → n^i ≤ n^j | 0, h => have : i = 0 := eq_zero_of_le_zero h this.symm ▸ Nat.le_refl _ | succ j, h => match lt_or_eq_or_le_succ h with | Or.inl h => show n^i ≤ n^j * n from have : n^i * 1 ≤ n^j * n := Nat.mul_le_mul (pow_le_pow_of_le_right hx h) hx Nat.mul_one (n^i) ▸ this | Or.inr h => h.symm ▸ Nat.le_refl _ theorem pos_pow_of_pos {n : Nat} (m : Nat) (h : 0 < n) : 0 < n^m := pow_le_pow_of_le_right h (Nat.zero_le _) /- min/max -/ protected def min (n m : Nat) : Nat := if n ≤ m then n else m protected def max (n m : Nat) : Nat := if n ≤ m then m else n end Nat namespace Prod @[inline] def foldI {α : Type u} (f : Nat → α → α) (i : Nat × Nat) (a : α) : α := Nat.foldAux f i.2 (i.2 - i.1) a @[inline] def anyI (f : Nat → Bool) (i : Nat × Nat) : Bool := Nat.anyAux f i.2 (i.2 - i.1) @[inline] def allI (f : Nat → Bool) (i : Nat × Nat) : Bool := Nat.anyAux (fun a => !f a) i.2 (i.2 - i.1) end Prod
649585276f4d2e156bbf1adbf56e3a6eac1c7501
d1a52c3f208fa42c41df8278c3d280f075eb020c
/stage0/src/Lean/Server/Rpc/RequestHandling.lean
61269d33d62a536e7ea9bf6b6db08978fc1e90aa
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
2,789
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Wojciech Nawrocki -/ import Lean.Data.Lsp.Extra import Lean.Server.Requests import Lean.Server.Rpc.Basic namespace Lean.Server private structure RpcProcedure where wrapper : (sessionId : UInt64) → Json → RequestM (RequestTask Json) builtin_initialize rpcProcedures : IO.Ref (Std.PersistentHashMap Name RpcProcedure) ← IO.mkRef {} private def handleRpcCall (p : Lsp.RpcCallParams) : RequestM (RequestTask Json) := do let rc ← read let some proc ← (← rpcProcedures.get).find? p.method | throwThe RequestError { code := JsonRpc.ErrorCode.methodNotFound message := s!"No RPC method '{p.method}' bound" } proc.wrapper p.sessionId p.params builtin_initialize registerLspRequestHandler "$/lean/rpc/call" Lsp.RpcCallParams Json handleRpcCall def registerRpcCallHandler (method : Name) paramType respType {paramLspType} [RpcEncoding paramType paramLspType] [FromJson paramLspType] {respLspType} [RpcEncoding respType respLspType] [ToJson respLspType] (handler : paramType → RequestM (RequestTask respType)) : IO Unit := do if !(← IO.initializing) then throw <| IO.userError s!"Failed to register RPC call handler for '{method}': only possible during initialization" if (←rpcProcedures.get).contains method then throw <| IO.userError s!"Failed to register RPC call handler for '{method}': already registered" let wrapper seshId j := do let rc ← read let some seshRef ← rc.rpcSessions.find? seshId | throwThe RequestError { code := JsonRpc.ErrorCode.rpcNeedsReconnect message := s!"Outdated RPC session" } let sesh ← seshRef.get let t ← RequestM.asTask do let paramsLsp ← liftExcept <| parseRequestParams paramLspType j let act := rpcDecode (α := paramType) (β := paramLspType) (m := StateM FileWorker.RpcSession) paramsLsp match act.run' sesh with | Except.ok v => return v | Except.error e => throwThe RequestError { code := JsonRpc.ErrorCode.invalidParams message := s!"Cannot decode params in RPC call '{method}({j.compress})'\n{e}" } let t ← RequestM.bindTask t fun | Except.error e => throw e | Except.ok ps => handler ps RequestM.mapTask t fun | Except.error e => throw e | Except.ok ret => do let act := rpcEncode (α := respType) (β := respLspType) (m := StateM FileWorker.RpcSession) ret let (retLsp, sesh') := act.run sesh seshRef.set sesh' return toJson retLsp rpcProcedures.modify fun ps => ps.insert method ⟨wrapper⟩ end Lean.Server
65169796e62c55feae46fa78d9c8c625ef8b4d3f
3dc4623269159d02a444fe898d33e8c7e7e9461b
/.github/workflows/geo/src/cartesian_closed.lean
d420a96ed633d0a927d1786359294f3bf0b42a96
[]
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
10,908
lean
/- Copyright (c) 2020 Bhavik Mehta, Edward Ayers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta, Edward Ayers -/ import category_theory.limits.shapes.binary_products import category_theory.adjunction import adjunction import tactic import to_mathlib import binary_products /-! # Cartesian closed categories Define exponentiable objects and cartesian closed categories. Show that exponential forms a difunctor. -/ universes v u namespace category_theory open limits category section variables {C : Type u} [𝒞 : category.{v} C] [has_binary_products.{v} C] {A U V W X Y Z : C} include 𝒞 local attribute [tidy] tactic.case_bash def prodinl (X : C) : C ⥤ C := prod_functor.obj X @[simp] lemma prodinl_map_def {f : Y ⟶ Z} : (prodinl X).map f = limits.prod.map (𝟙 X) f := rfl @[simp] lemma map_fst {f : U ⟶ V} {g : W ⟶ X} : limits.prod.map f g ≫ limits.prod.fst = limits.prod.fst ≫ f := by simp @[simp] lemma map_snd {f : U ⟶ V} {g : W ⟶ X} : limits.prod.map f g ≫ limits.prod.snd = limits.prod.snd ≫ g := by simp @[simp] lemma prod.map_id : limits.prod.map (𝟙 X) (𝟙 Y) = 𝟙 (X⨯Y) := begin apply prod.hom_ext, simp, simp end @[simp] lemma lift_fst {f : W ⟶ X} {g : W ⟶ Y} : limits.prod.lift f g ≫ limits.prod.fst = f := by simp @[simp] lemma lift_snd {f : W ⟶ X} {g : W ⟶ Y} : limits.prod.lift f g ≫ limits.prod.snd = g := by simp open category def prodinl_comp (X Y : C) : prodinl (X ⨯ Y) ≅ prodinl Y ⋙ prodinl X := nat_iso.of_components (limits.prod.associator _ _) (by tidy) end class exponentiable {C : Type u} [𝒞 : category.{v} C] [bp : @has_binary_products C 𝒞] (X : C) := (exponentiable : is_left_adjoint (prodinl X)) def binary_product_exponentiable {C : Type u} [𝒞 : category.{v} C] [bp : @has_binary_products C 𝒞] {X Y : C} (hX : exponentiable X) (hY : exponentiable Y) : exponentiable (X ⨯ Y) := { exponentiable := { right := hX.exponentiable.right ⋙ hY.exponentiable.right, adj := adjunction_of_nat_iso_left (adjunction.comp _ _ hY.exponentiable.adj hX.exponentiable.adj) (prodinl_comp _ _).symm } } class is_cartesian_closed (C : Type u) [𝒞 : category.{v} C] [has_binary_products.{v} C] extends has_terminal.{v} C := (cart_closed : Π (X : C), exponentiable X) instance exponentiable_of_cc {C : Type u} [𝒞 : category.{v} C] [@has_binary_products C 𝒞] [is_cartesian_closed C] {A : C} : exponentiable A := is_cartesian_closed.cart_closed A variables {C : Type u} [𝒞 : category.{v} C] [has_binary_products.{v} C] {X X' Y Y' Z A B : C} [exponentiable A] include 𝒞 /-- This is (-)^A -/ def exp.functor (A : C) [exponentiable A] : C ⥤ C := (exponentiable.exponentiable A).right def exp.adjunction : prodinl A ⊣ exp.functor A := (exponentiable.exponentiable A).adj def ev.nat_trans (A : C) [exponentiable A] : exp.functor A ⋙ prodinl A ⟶ 𝟭 C := exp.adjunction.counit def coev.nat_trans (A : C) [exponentiable A] : 𝟭 C ⟶ prodinl A ⋙ exp.functor A := exp.adjunction.unit /-- `B ^ A` or `A ⟹ B` -/ def exp (A : C) (B : C) [exponentiable A] : C := (exp.functor A).obj B infixl `⟹`:20 := exp -- [todo] rename as 'post compose' or similar? def post (A : C) [exponentiable A] {X Y : C} (f : X ⟶ Y) : A⟹X ⟶ A⟹Y := (exp.functor A).map f lemma post.map_comp {f : X ⟶ Y} {g : Y ⟶ Z} : post A (f ≫ g) = post A f ≫ post A g := begin show functor.map _ _ = _ ≫ _, rw (exp.functor A).map_comp', refl, end def ev : A ⨯ (A⟹B) ⟶ B := (ev.nat_trans A).app B def coev : B ⟶ A⟹(A⨯B) := (coev.nat_trans A).app B @[simp] lemma ev_coev : limits.prod.map (𝟙 A) coev ≫ ev = 𝟙 (A⨯B) := (@adjunction.left_triangle_components C _ C _ (prodinl A) (exp.functor A) exp.adjunction B) @[simp] lemma coev_ev : coev ≫ post _ ev = 𝟙 (A⟹B) := (@adjunction.right_triangle_components C _ C _ (prodinl A) (exp.functor A) exp.adjunction B) lemma coev_nat (f : X ⟶ Y) : f ≫ coev = coev ≫ post _ (limits.prod.map (𝟙 A) f) := (coev.nat_trans A).naturality f lemma ev_nat {f : X ⟶ Y} : limits.prod.map (𝟙 A) (post _ f) ≫ ev = ev ≫ f := (ev.nat_trans A).naturality f def exp_transpose : (A ⨯ Y ⟶ X) ≃ (Y ⟶ A⟹X) := exp.adjunction.hom_equiv _ _ lemma exp_transpose_natural_left (f : X ⟶ X') (g : A ⨯ X' ⟶ Y) : exp_transpose.to_fun (limits.prod.map (𝟙 _) f ≫ g) = f ≫ exp_transpose.to_fun g := adjunction.hom_equiv_naturality_left _ _ _ lemma exp_transpose_natural_right (f : A ⨯ X ⟶ Y) (g : Y ⟶ Y') : exp_transpose.to_fun (f ≫ g) = exp_transpose.to_fun f ≫ post _ g := adjunction.hom_equiv_naturality_right _ _ _ lemma exp_transpose_natural_right_symm (f : X ⟶ A⟹Y) (g : Y ⟶ Y') : exp_transpose.inv_fun (f ≫ post A g) = exp_transpose.inv_fun f ≫ g := adjunction.hom_equiv_naturality_right_symm _ _ _ lemma exp_transpose_natural_left_symm (f : X ⟶ X') (g : X' ⟶ A⟹Y) : exp_transpose.inv_fun (f ≫ g) = limits.prod.map (𝟙 _) f ≫ exp_transpose.inv_fun g := adjunction.hom_equiv_naturality_left_symm _ _ _ section terminal variable [has_terminal.{v} C] lemma prod_left_unitor_naturality (f : X ⟶ Y): (prod.left_unitor X).inv ≫ limits.prod.map (𝟙 _) f = f ≫ (prod.left_unitor Y).inv := begin apply prod.hom_ext, { apply subsingleton.elim }, { simp [id_comp C f] } end def terminal_exponentiable : exponentiable ⊤_C := { exponentiable := { right := 𝟭 C, adj := adjunction.mk_of_hom_equiv { hom_equiv := λ X _, have unitor : _, from prod.left_unitor X, ⟨λ a, unitor.inv ≫ a, λ a, unitor.hom ≫ a, by tidy, by tidy⟩ } } } attribute [instance] terminal_exponentiable def exp_terminal_iso : (⊤_C ⟹ X) ≅ X := begin apply yoneda.ext (⊤_ C ⟹ X) X _ _ _ _ _, intros Y f, exact (prod.left_unitor Y).inv ≫ exp_transpose.inv_fun f, intros Y f, exact exp_transpose.to_fun ((prod.left_unitor Y).hom ≫ f), { intros Z g, dsimp, rw ← assoc, erw iso.hom_inv_id (prod.left_unitor Z), simp [exp_transpose.right_inv g] }, { intros Z g, dsimp, rw exp_transpose.left_inv, rw ← assoc, erw iso.inv_hom_id (prod.left_unitor Z), simp }, { intros Z W f g, dsimp, rw exp_transpose_natural_left_symm, rw ← assoc, rw ← assoc, erw prod_left_unitor_naturality _, refl }, end @[reducible] def point_at_hom (f : A ⟶ Y) : ⊤_C ⟶ (A ⟹ Y) := exp_transpose.to_fun (limits.prod.fst ≫ f) end terminal section pre variables [exponentiable B] -- this notation (and the hats) are just here so i could figure out how to -- do pre_map - I think the ⟨f,g⟩ might be nice but the rest can go (TODO) local notation `⟨`f`, `g`⟩` := limits.prod.map f g @[reducible] def hat : (A ⨯ Y ⟶ X) → (Y ⟶ A ⟹ X) := exp_transpose.to_fun @[reducible] def unhat : (Y ⟶ A ⟹ X) → (A ⨯ Y ⟶ X) := exp_transpose.inv_fun def pre (X : C) (f : B ⟶ A) : (A⟹X) ⟶ B⟹X := hat (⟨f, 𝟙 (A ⟹ X)⟩ ≫ unhat (𝟙 (A ⟹ X))) lemma pre_id : pre X (𝟙 A) = 𝟙 (A⟹X) := begin dunfold pre hat, erw exp_transpose_natural_left, rw exp_transpose.right_inv, simp end lemma pre_map {D : C} [exponentiable D] {f : A ⟶ B} {g : B ⟶ D} : pre X (f ≫ g) = pre X g ≫ pre X f := begin dunfold pre, apply function.injective_of_left_inverse exp_transpose.right_inv, rw exp_transpose.left_inv, rw ← exp_transpose_natural_left, rw exp_transpose.left_inv, show ⟨f ≫ g, 𝟙 (D ⟹ X)⟩ ≫ unhat (𝟙 (D ⟹ X)) = ⟨𝟙 A, (hat (⟨g, 𝟙 (D ⟹ X)⟩ ≫ unhat (𝟙 (D ⟹ X))))⟩ ≫ ⟨f, 𝟙 (B ⟹ X)⟩ ≫ unhat (𝟙 (B ⟹ X)), suffices: ⟨f ≫ g, 𝟙 (D ⟹ X)⟩ ≫ unhat (𝟙 (D ⟹ X)) = (⟨f, 𝟙 (D ⟹ X)⟩ ≫ ⟨𝟙 B, (hat (⟨g, 𝟙 (D ⟹ X)⟩ ≫ unhat (𝟙 (D ⟹ X))))⟩) ≫ unhat (𝟙 (B ⟹ X)), rw this, rw ← assoc, congr' 1, apply prod.hom_ext, simp, dsimp, simp, simp, dsimp, simp, have: ⟨f ≫ g, 𝟙 (D ⟹ X)⟩ = ⟨f, 𝟙 _⟩ ≫ ⟨g, 𝟙 _⟩, apply prod.hom_ext, simp, simp, rw this, rw assoc, rw assoc, congr' 1, erw ← exp_transpose_natural_left_symm, apply function.injective_of_left_inverse exp_transpose.left_inv, rw exp_transpose_natural_right, rw exp_transpose.right_inv, simp, exact (exp_transpose_natural_right _ _).symm end def pre.functor [is_cartesian_closed C] (X : C) : Cᵒᵖ ⥤ C := { obj := λ A, (A.unop) ⟹ X, map := λ A B f, pre X f.unop, map_id' := begin intros, apply pre_id, end, map_comp' := begin intros, apply pre_map, end, } end pre lemma exp_natural [is_cartesian_closed C] (A B : C) (X Y : Cᵒᵖ) (f : A ⟶ B) (g : X ⟶ Y) : (pre.functor A).map g ≫ post (opposite.unop Y) f = post (opposite.unop X) f ≫ (pre.functor B).map g := begin dunfold pre.functor, dsimp, dunfold pre, show _ = _, rw ← exp_transpose_natural_right, rw ← exp_transpose_natural_left, congr' 1, rw assoc, rw ← exp_transpose_natural_right_symm, rw ← assoc, show _ = (limits.prod.map _ _ ≫ _) ≫ _, rw prod_map_comm, rw assoc, erw ← exp_transpose_natural_left_symm, rw id_comp, rw comp_id end def exp.difunctor [is_cartesian_closed C] : C ⥤ Cᵒᵖ ⥤ C := { obj := pre.functor, map := λ A B f, { app := λ X, post X.unop f, naturality' := λ X Y g, begin apply exp_natural end }, map_id' := λ X, begin ext, apply functor.map_id end, map_comp' := λ X Y Z f g, begin ext, apply functor.map_comp end } section functor universes v₂ u₂ variables {D : Type u} [category.{v} D] [has_binary_products.{v} D] variables (F : C ⥤ D) [preserves_limits_of_shape (discrete walking_pair) F] -- (implementation) def alternative_cone (A B : C) : cone (pair A B ⋙ F) := { X := F.obj A ⨯ F.obj B, π := nat_trans.of_homs (λ j, walking_pair.cases_on j limits.prod.fst limits.prod.snd)} -- (implementation) def alt_is_limit (A B : C) : is_limit (functor.map_cone F (limit.cone (pair A B))) := preserves_limit.preserves F (limit.is_limit (pair A B)) -- the multiplicative comparison isomorphism def mult_comparison (A B : C) : F.obj (A ⨯ B) ≅ F.obj A ⨯ F.obj B := { hom := prod.lift (F.map limits.prod.fst) (F.map limits.prod.snd), inv := (alt_is_limit F A B).lift (alternative_cone F A B), hom_inv_id' := begin apply is_limit.hom_ext (alt_is_limit F A B), rintro ⟨j⟩, rw assoc, rw (alt_is_limit F A B).fac, erw limit.lift_π, simp, rw assoc, rw (alt_is_limit F A B).fac, erw limit.lift_π, simp end, inv_hom_id' := begin ext ⟨j⟩, simp, erw (alt_is_limit F A B).fac, refl, simp, erw (alt_is_limit F A B).fac, refl, end } variables [is_cartesian_closed C] [is_cartesian_closed D] -- the exponential comparison map def exp_comparison (A B : C) : F.obj (A ⟹ B) ⟶ F.obj A ⟹ F.obj B := hat ((mult_comparison F A _).inv ≫ F.map ev) end functor end category_theory
cd45df8c5c3e6b04ecda6300558357f8647dacf2
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/number_theory/multiplicity.lean
8231f6de58a9145842d891f3186df3266efc5c53
[ "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,630
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 import ring_theory.ideal.quotient_operations /-! # Multiplicity in Number Theory > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. 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 h.neg_right 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.2 (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, change zmod.cast_hom _ (zmod 2) y = _ at hx, -- 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
4b082f057840286b42365bea4771652639d62cfc
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/nested_rec.lean
a248e4fb87ebc62c802759337aa7f440ee60daec
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
1,263
lean
open nat prod sigma -- We will define the following example by well-foudned recursion -- g 0 := 0 -- g (succ x) := g (g x) definition g.F (x : nat) : (Π y, y < x → Σ r : nat, r ≤ y) → Σ r : nat, r ≤ x := nat.cases_on x (λ f, ⟨zero, nat.le_refl zero⟩) (λ x₁ (f : Π y, y < succ x₁ → Σ r : nat, r ≤ y), let p₁ := f x₁ (lt.base x₁) in let gx₁ := pr₁ p₁ in let p₂ := f gx₁ (nat.lt_of_le_of_lt (pr₂ p₁) (lt.base x₁)) in let ggx₁ := pr₁ p₂ in ⟨ggx₁, le_succ_of_le (nat.le_trans (pr₂ p₂) (pr₂ p₁))⟩) definition g (x : nat) : nat := pr₁ (well_founded.fix g.F x) example : g 3 = 0 := rfl example : g 6 = 0 := rfl theorem g_zero : g 0 = 0 := rfl theorem g_succ (a : nat) : g (succ a) = g (g a) := have aux : well_founded.fix g.F (succ a) = sigma.mk (g (g a)) _, from well_founded.fix_eq g.F (succ a), calc g (succ a) = pr₁ (well_founded.fix g.F (succ a)) : rfl ... = g (g a) : {aux} theorem g_all_zero (a : nat) : g a = zero := nat.induction_on a g_zero (λ a₁ (ih : g a₁ = 0), calc g (succ a₁) = g (g a₁) : g_succ ... = g 0 : ih ... = 0 : g_zero)
7f134532115dd327609ecb24970d511302de8cdb
3f1a1d97c03bb24b55a1b9969bb4b3c619491d5a
/library/init/data/bool/lemmas.lean
35facc3a6b380ccc51847bf604d68edde36bf9a9
[ "Apache-2.0" ]
permissive
praveenmunagapati/lean
00c3b4496cef8e758396005013b9776bb82c4f56
fc760f57d20e0a486d14bc8a08d89147b60f530c
refs/heads/master
1,630,692,342,183
1,515,626,222,000
1,515,626,222,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,662
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 -/ prelude import init.data.bool.basic init.meta attribute [simp] cond bor band bnot bxor @[simp] lemma {u} cond_a_a {α : Type u} (b : bool) (a : α) : cond b a a = a := by cases b; simp @[simp] lemma band_self (b : bool) : b && b = b := by cases b; simp @[simp] lemma band_tt (b : bool) : b && tt = b := by cases b; simp @[simp] lemma band_ff (b : bool) : b && ff = ff := by cases b; simp @[simp] lemma tt_band (b : bool) : tt && b = b := by cases b; simp @[simp] lemma ff_band (b : bool) : ff && b = ff := by cases b; simp @[simp] lemma bor_self (b : bool) : b || b = b := by cases b; simp @[simp] lemma bor_tt (b : bool) : b || tt = tt := by cases b; simp @[simp] lemma bor_ff (b : bool) : b || ff = b := by cases b; simp @[simp] lemma tt_bor (b : bool) : tt || b = tt := by cases b; simp @[simp] lemma ff_bor (b : bool) : ff || b = b := by cases b; simp @[simp] lemma bxor_self (b : bool) : bxor b b = ff := by cases b; simp @[simp] lemma bxor_tt (b : bool) : bxor b tt = bnot b := by cases b; simp @[simp] lemma bxor_ff (b : bool) : bxor b ff = b := by cases b; simp @[simp] lemma tt_bxor (b : bool) : bxor tt b = bnot b := by cases b; simp @[simp] lemma ff_bxor (b : bool) : bxor ff b = b := by cases b; simp @[simp] lemma bnot_bnot (b : bool) : bnot (bnot b) = b := by cases b; simp @[simp] lemma tt_eq_ff_eq_false : ¬(tt = ff) := by contradiction @[simp] lemma ff_eq_tt_eq_false : ¬(ff = tt) := by contradiction @[simp] lemma eq_ff_eq_not_eq_tt (b : bool) : (¬(b = tt)) = (b = ff) := by cases b; simp @[simp] lemma eq_tt_eq_not_eq_ff (b : bool) : (¬(b = ff)) = (b = tt) := by cases b; simp lemma eq_ff_of_not_eq_tt {b : bool} : (¬(b = tt)) → (b = ff) := eq.mp (eq_ff_eq_not_eq_tt b) lemma eq_tt_of_not_eq_ff {b : bool} : (¬(b = ff)) → (b = tt) := eq.mp (eq_tt_eq_not_eq_ff b) @[simp] lemma band_eq_true_eq_eq_tt_and_eq_tt (a b : bool) : (a && b = tt) = (a = tt ∧ b = tt) := by cases a; cases b; simp @[simp] lemma bor_eq_true_eq_eq_tt_or_eq_tt (a b : bool) : (a || b = tt) = (a = tt ∨ b = tt) := by cases a; cases b; simp @[simp] lemma bnot_eq_true_eq_eq_ff (a : bool) : (bnot a = tt) = (a = ff) := by cases a; simp @[simp] lemma band_eq_false_eq_eq_ff_or_eq_ff (a b : bool) : (a && b = ff) = (a = ff ∨ b = ff) := by cases a; cases b; simp @[simp] lemma bor_eq_false_eq_eq_ff_and_eq_ff (a b : bool) : (a || b = ff) = (a = ff ∧ b = ff) := by cases a; cases b; simp @[simp] lemma bnot_eq_ff_eq_eq_tt (a : bool) : (bnot a = ff) = (a = tt) := by cases a; simp @[simp] lemma coe_ff : ↑ff = false := show (ff = tt) = false, by simp @[simp] lemma coe_tt : ↑tt = true := show (tt = tt) = true, by simp @[simp] theorem to_bool_iff (p : Prop) [d : decidable p] : (to_bool p = tt) ↔ p := match d with | is_true hp := ⟨λh, hp, λ_, rfl⟩ | is_false hnp := ⟨λh, bool.no_confusion h, λhp, absurd hp hnp⟩ end theorem to_bool_true {p : Prop} [decidable p] : p → to_bool p := (to_bool_iff p).2 theorem to_bool_tt {p : Prop} [decidable p] : p → to_bool p = tt := to_bool_true theorem of_to_bool_true {p : Prop} [decidable p] : to_bool p → p := (to_bool_iff p).1 theorem bool_iff_false {b : bool} : ¬ b ↔ b = ff := by cases b; exact dec_trivial theorem bool_eq_false {b : bool} : ¬ b → b = ff := bool_iff_false.1 @[simp] theorem to_bool_ff_iff (p : Prop) [decidable p] : to_bool p = ff ↔ ¬p := bool_iff_false.symm.trans (not_congr (to_bool_iff _)) theorem to_bool_ff {p : Prop} [decidable p] : ¬p → to_bool p = ff := (to_bool_ff_iff p).2 theorem of_to_bool_ff {p : Prop} [decidable p] : to_bool p = ff → ¬p := (to_bool_ff_iff p).1 theorem to_bool_congr {p q : Prop} [decidable p] [decidable q] (h : p ↔ q) : to_bool p = to_bool q := begin induction h' : to_bool q, exact to_bool_ff (mt h.1 $ of_to_bool_ff h'), exact to_bool_true (h.2 $ of_to_bool_true h') end @[simp] theorem bor_coe_iff (a b : bool) : a || b ↔ a ∨ b := by cases a; cases b; exact dec_trivial @[simp] theorem band_coe_iff (a b : bool) : a && b ↔ a ∧ b := by cases a; cases b; exact dec_trivial @[simp] theorem bxor_coe_iff (a b : bool) : bxor a b ↔ xor a b := by cases a; cases b; exact dec_trivial @[simp] theorem ite_eq_tt_distrib (c : Prop) [decidable c] (a b : bool) : ((if c then a else b) = tt) = (if c then a = tt else b = tt) := by by_cases c; simp [*] @[simp] theorem ite_eq_ff_distrib (c : Prop) [decidable c] (a b : bool) : ((if c then a else b) = ff) = (if c then a = ff else b = ff) := by by_cases c; simp [*]
54acd03a848d2b5f6620ff632b3cfa5e6fca394d
07c76fbd96ea1786cc6392fa834be62643cea420
/library/init/reserved_notation.lean
9b8bf634cbcd2a3296ca095cfd39ab0b05d8d2c9
[ "Apache-2.0" ]
permissive
fpvandoorn/lean2
5a430a153b570bf70dc8526d06f18fc000a60ad9
0889cf65b7b3cebfb8831b8731d89c2453dd1e9f
refs/heads/master
1,592,036,508,364
1,545,093,958,000
1,545,093,958,000
75,436,854
0
0
null
1,480,718,780,000
1,480,718,780,000
null
UTF-8
Lean
false
false
7,794
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad, Floris van Doorn -/ prelude import init.datatypes notation `assume` binders `,` r:(scoped f, f) := r notation `take` binders `,` r:(scoped f, f) := r structure has_zero [class] (A : Type) := (zero : A) structure has_one [class] (A : Type) := (one : A) structure has_add [class] (A : Type) := (add : A → A → A) structure has_mul [class] (A : Type) := (mul : A → A → A) structure has_inv [class] (A : Type) := (inv : A → A) structure has_neg [class] (A : Type) := (neg : A → A) structure has_sub [class] (A : Type) := (sub : A → A → A) structure has_div [class] (A : Type) := (div : A → A → A) structure has_dvd [class] (A : Type) := (dvd : A → A → Prop) structure has_mod [class] (A : Type) := (mod : A → A → A) structure has_le [class] (A : Type) := (le : A → A → Prop) structure has_lt [class] (A : Type) := (lt : A → A → Prop) definition zero {A : Type} [s : has_zero A] : A := has_zero.zero A definition one {A : Type} [s : has_one A] : A := has_one.one A definition add {A : Type} [s : has_add A] : A → A → A := has_add.add definition mul {A : Type} [s : has_mul A] : A → A → A := has_mul.mul definition sub {A : Type} [s : has_sub A] : A → A → A := has_sub.sub definition div {A : Type} [s : has_div A] : A → A → A := has_div.div definition dvd {A : Type} [s : has_dvd A] : A → A → Prop := has_dvd.dvd definition mod {A : Type} [s : has_mod A] : A → A → A := has_mod.mod definition neg {A : Type} [s : has_neg A] : A → A := has_neg.neg definition inv {A : Type} [s : has_inv A] : A → A := has_inv.inv definition le {A : Type} [s : has_le A] : A → A → Prop := has_le.le definition lt {A : Type} [s : has_lt A] : A → A → Prop := has_lt.lt definition ge [reducible] {A : Type} [s : has_le A] (a b : A) : Prop := le b a definition gt [reducible] {A : Type} [s : has_lt A] (a b : A) : Prop := lt b a /- bit0 and bit1 are two auxiliary definition used when parsing numerals such as 13, 0, 26. The parser will generate the terms (bit1 (bit0 (bit1 one))), zero, and (bit0 (bit1 (bit0 (bit1 one)))). This works in any type with an addition, a zero and a one. More specifically, there must be type class instances for the classes for has_add, has_zero and has_one -/ definition bit0 {A : Type} [s : has_add A] (a : A) : A := add a a definition bit1 {A : Type} [s₁ : has_one A] [s₂ : has_add A] (a : A) : A := add (bit0 a) one definition num_has_zero [instance] : has_zero num := has_zero.mk num.zero definition num_has_one [instance] : has_one num := has_one.mk (num.pos pos_num.one) definition pos_num_has_one [instance] : has_one pos_num := has_one.mk (pos_num.one) namespace pos_num open bool definition is_one (a : pos_num) : bool := pos_num.rec_on a tt (λn r, ff) (λn r, ff) definition pred (a : pos_num) : pos_num := pos_num.rec_on a one (λn r, bit0 n) (λn r, bool.rec_on (is_one n) (bit1 r) one) definition size (a : pos_num) : pos_num := pos_num.rec_on a one (λn r, succ r) (λn r, succ r) definition add (a b : pos_num) : pos_num := pos_num.rec_on a succ (λn f b, pos_num.rec_on b (succ (bit1 n)) (λm r, succ (bit1 (f m))) (λm r, bit1 (f m))) (λn f b, pos_num.rec_on b (bit1 n) (λm r, bit1 (f m)) (λm r, bit0 (f m))) b end pos_num definition pos_num_has_add [instance] : has_add pos_num := has_add.mk pos_num.add namespace num open pos_num definition add (a b : num) : num := num.rec_on a b (λpa, num.rec_on b (pos pa) (λpb, pos (pos_num.add pa pb))) end num definition num_has_add [instance] : has_add num := has_add.mk num.add definition std.priority.default : num := 1000 definition std.priority.max : num := 4294967295 namespace nat protected definition prio := num.add std.priority.default 100 protected definition add (a b : nat) : nat := nat.rec a (λ b₁ r, succ r) b definition of_num (n : num) : nat := num.rec zero (λ n, pos_num.rec (succ zero) (λ n r, nat.add (nat.add r r) (succ zero)) (λ n r, nat.add r r) n) n end nat attribute pos_num_has_add pos_num_has_one num_has_zero num_has_one num_has_add [instance] [priority nat.prio] definition nat_has_zero [instance] [priority nat.prio] : has_zero nat := has_zero.mk nat.zero definition nat_has_one [instance] [priority nat.prio] : has_one nat := has_one.mk (nat.succ (nat.zero)) definition nat_has_add [instance] [priority nat.prio] : has_add nat := has_add.mk nat.add /- Global declarations of right binding strength If a module reassigns these, it will be incompatible with other modules that adhere to these conventions. When hovering over a symbol, use "C-c C-k" to see how to input it. -/ definition std.prec.max : num := 1024 -- the strength of application, identifiers, (, [, etc. definition std.prec.arrow : num := 25 /- The next definition is "max + 10". It can be used e.g. for postfix operations that should be stronger than application. -/ definition std.prec.max_plus := num.succ (num.succ (num.succ (num.succ (num.succ (num.succ (num.succ (num.succ (num.succ (num.succ std.prec.max))))))))) /- Logical operations and relations -/ reserve prefix `¬`:40 reserve prefix `~`:40 reserve infixr ` ∧ `:35 reserve infixr ` /\ `:35 reserve infixr ` \/ `:30 reserve infixr ` ∨ `:30 reserve infix ` <-> `:20 reserve infix ` ↔ `:20 reserve infix ` = `:50 reserve infix ` ≠ `:50 reserve infix ` ≈ `:50 reserve infix ` ~ `:50 reserve infix ` ≡ `:50 reserve infixr ` ∘ `:60 -- input with \comp reserve postfix `⁻¹`:std.prec.max_plus -- input with \sy or \-1 or \inv reserve infixl ` ⬝ `:75 reserve infixr ` ▸ `:75 reserve infixr ` ▹ `:75 /- types and type constructors -/ reserve infixl ` ⊎ `:25 reserve infixl ` × `:30 /- arithmetic operations -/ reserve infixl ` + `:65 reserve infixl ` - `:65 reserve infixl ` * `:70 reserve infixl ` / `:70 reserve infixl ` % `:70 reserve prefix `-`:100 reserve infix ` ^ `:80 reserve infix ` <= `:50 reserve infix ` ≤ `:50 reserve infix ` < `:50 reserve infix ` >= `:50 reserve infix ` ≥ `:50 reserve infix ` > `:50 /- boolean operations -/ reserve infixl ` && `:70 reserve infixl ` || `:65 /- set operations -/ reserve infix ` ∈ `:50 reserve infix ` ∉ `:50 reserve infixl ` ∩ `:70 reserve infixl ` ∪ `:65 reserve infix ` ⊆ `:50 reserve infix ` ⊇ `:50 reserve infix ` ' `:75 -- for the image of a set under a function reserve infix ` '- `:75 -- for the preimage of a set under a function /- other symbols -/ reserve infix ` ∣ `:50 reserve infixl ` ++ `:65 reserve infixr ` :: `:67 infix + := add infix * := mul infix - := sub infix / := div infix ∣ := dvd infix % := mod prefix - := neg postfix ⁻¹ := inv infix ≤ := le infix ≥ := ge infix < := lt infix > := gt notation [parsing_only] x ` +[`:65 A:0 `] `:0 y:65 := @add A _ x y notation [parsing_only] x ` -[`:65 A:0 `] `:0 y:65 := @sub A _ x y notation [parsing_only] x ` *[`:70 A:0 `] `:0 y:70 := @mul A _ x y notation [parsing_only] x ` /[`:70 A:0 `] `:0 y:70 := @div A _ x y notation [parsing_only] x ` ∣[`:70 A:0 `] `:0 y:70 := @dvd A _ x y notation [parsing_only] x ` %[`:70 A:0 `] `:0 y:70 := @mod A _ x y notation [parsing_only] x ` ≤[`:50 A:0 `] `:0 y:50 := @le A _ x y notation [parsing_only] x ` ≥[`:50 A:0 `] `:0 y:50 := @ge A _ x y notation [parsing_only] x ` <[`:50 A:0 `] `:0 y:50 := @lt A _ x y notation [parsing_only] x ` >[`:50 A:0 `] `:0 y:50 := @gt A _ x y
13c0d7e130b50bf3636d65445dd9fb1cdf11bea2
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/data/fintype/basic.lean
09b5de12f8510fe2e237548ac7663221a7b23969
[ "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
81,497
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.array.lemmas import data.finset.pi import data.finset.powerset import data.finset.option import data.sym.basic import data.ulift import group_theory.perm.basic import order.well_founded import tactic.wlog /-! # Finite types This file defines a typeclass to state that a type is finite. ## Main declarations * `fintype α`: Typeclass saying that a type is finite. It takes as fields a `finset` and a proof that all terms of type `α` are in it. * `finset.univ`: The finset of all elements of a fintype. * `fintype.card α`: Cardinality of a fintype. Equal to `finset.univ.card`. * `perms_of_finset s`: The finset of permutations of the finset `s`. * `fintype.trunc_equiv_fin`: A fintype `α` is computably equivalent to `fin (card α)`. The `trunc`-free, noncomputable version is `fintype.equiv_fin`. * `fintype.trunc_equiv_of_card_eq` `fintype.equiv_of_card_eq`: Two fintypes of same cardinality are equivalent. See above. * `fin.equiv_iff_eq`: `fin m ≃ fin n` iff `m = n`. * `infinite α`: Typeclass saying that a type is infinite. Defined as `fintype α → false`. * `not_fintype`: No `fintype` has an `infinite` instance. * `infinite.nat_embedding`: An embedding of `ℕ` into an infinite type. We also provide the following versions of the pigeonholes principle. * `fintype.exists_ne_map_eq_of_card_lt` and `is_empty_of_card_lt`: Finitely many pigeons and pigeonholes. Weak formulation. * `fintype.exists_ne_map_eq_of_infinite`: Infinitely many pigeons in finitely many pigeonholes. Weak formulation. * `fintype.exists_infinite_fiber`: Infinitely many pigeons in finitely many pigeonholes. Strong formulation. Some more pigeonhole-like statements can be found in `data.fintype.card_embedding`. ## Instances Among others, we provide `fintype` instances for * A `subtype` of a fintype. See `fintype.subtype`. * The `option` of a fintype. * The product of two fintypes. * The sum of two fintypes. * `Prop`. and `infinite` instances for * `ℕ` * `ℤ` along with some machinery * Types which have a surjection from/an injection to a `fintype` are themselves fintypes. See `fintype.of_injective` and `fintype.of_surjective`. * Types which have an injection from/a surjection to an `infinite` type are themselves `infinite`. See `infinite.of_injective` and `infinite.of_surjective`. -/ open_locale nat universes u v variables {α β γ : Type*} /-- `fintype α` means that `α` is finite, i.e. there are only finitely many distinct elements of type `α`. The evidence of this is a finset `elems` (a list up to permutation without duplicates), together with a proof that everything of type `α` is in the list. -/ class fintype (α : Type*) := (elems [] : finset α) (complete : ∀ x : α, x ∈ elems) namespace finset variable [fintype α] /-- `univ` is the universal finite set of type `finset α` implied from the assumption `fintype α`. -/ def univ : finset α := fintype.elems α @[simp] theorem mem_univ (x : α) : x ∈ (univ : finset α) := fintype.complete x @[simp] theorem mem_univ_val : ∀ x, x ∈ (univ : finset α).1 := mem_univ @[simp] lemma coe_univ : ↑(univ : finset α) = (set.univ : set α) := by ext; simp lemma univ_nonempty_iff : (univ : finset α).nonempty ↔ nonempty α := by rw [← coe_nonempty, coe_univ, set.nonempty_iff_univ_nonempty] lemma univ_nonempty [nonempty α] : (univ : finset α).nonempty := univ_nonempty_iff.2 ‹_› lemma univ_eq_empty_iff : (univ : finset α) = ∅ ↔ is_empty α := by rw [← not_nonempty_iff, ← univ_nonempty_iff, not_nonempty_iff_eq_empty] lemma univ_eq_empty [is_empty α] : (univ : finset α) = ∅ := univ_eq_empty_iff.2 ‹_› @[simp] theorem subset_univ (s : finset α) : s ⊆ univ := λ a _, mem_univ a instance : order_top (finset α) := { top := univ, le_top := subset_univ, .. finset.partial_order } instance [decidable_eq α] : boolean_algebra (finset α) := { compl := λ s, univ \ s, inf_compl_le_bot := λ s x hx, by simpa using hx, top_le_sup_compl := λ s x hx, by simp, sdiff_eq := λ s t, by simp [ext_iff, compl], ..finset.order_top, ..finset.generalized_boolean_algebra } lemma compl_eq_univ_sdiff [decidable_eq α] (s : finset α) : sᶜ = univ \ s := rfl @[simp] lemma mem_compl [decidable_eq α] {s : finset α} {x : α} : x ∈ sᶜ ↔ x ∉ s := by simp [compl_eq_univ_sdiff] @[simp, norm_cast] lemma coe_compl [decidable_eq α] (s : finset α) : ↑(sᶜ) = (↑s : set α)ᶜ := set.ext $ λ x, mem_compl @[simp] theorem union_compl [decidable_eq α] (s : finset α) : s ∪ sᶜ = finset.univ := sup_compl_eq_top @[simp] theorem insert_compl_self [decidable_eq α] (x : α) : insert x ({x}ᶜ : finset α) = univ := by { ext y, simp [eq_or_ne] } @[simp] lemma compl_filter [decidable_eq α] (p : α → Prop) [decidable_pred p] [Π x, decidable (¬p x)] : (univ.filter p)ᶜ = univ.filter (λ x, ¬p x) := (filter_not _ _).symm theorem eq_univ_iff_forall {s : finset α} : s = univ ↔ ∀ x, x ∈ s := by simp [ext_iff] lemma compl_ne_univ_iff_nonempty [decidable_eq α] (s : finset α) : sᶜ ≠ univ ↔ s.nonempty := by simp [eq_univ_iff_forall, finset.nonempty] lemma compl_singleton [decidable_eq α] (a : α) : ({a} : finset α)ᶜ = univ.erase a := by rw [compl_eq_univ_sdiff, sdiff_singleton_eq_erase] @[simp] lemma univ_inter [decidable_eq α] (s : finset α) : univ ∩ s = s := ext $ λ a, by simp @[simp] lemma inter_univ [decidable_eq α] (s : finset α) : s ∩ univ = s := by rw [inter_comm, univ_inter] @[simp] lemma piecewise_univ [Π i : α, decidable (i ∈ (univ : finset α))] {δ : α → Sort*} (f g : Π i, δ i) : univ.piecewise f g = f := by { ext i, simp [piecewise] } lemma piecewise_compl [decidable_eq α] (s : finset α) [Π i : α, decidable (i ∈ s)] [Π i : α, decidable (i ∈ sᶜ)] {δ : α → Sort*} (f g : Π i, δ i) : sᶜ.piecewise f g = s.piecewise g f := by { ext i, simp [piecewise] } @[simp] lemma piecewise_erase_univ {δ : α → Sort*} [decidable_eq α] (a : α) (f g : Π a, δ a) : (finset.univ.erase a).piecewise f g = function.update f a (g a) := by rw [←compl_singleton, piecewise_compl, piecewise_singleton] lemma univ_map_equiv_to_embedding {α β : Type*} [fintype α] [fintype β] (e : α ≃ β) : univ.map e.to_embedding = univ := eq_univ_iff_forall.mpr (λ b, mem_map.mpr ⟨e.symm b, mem_univ _, by simp⟩) @[simp] lemma univ_filter_exists (f : α → β) [fintype β] [decidable_pred (λ y, ∃ x, f x = y)] [decidable_eq β] : finset.univ.filter (λ y, ∃ x, f x = y) = finset.univ.image f := by { ext, simp } /-- Note this is a special case of `(finset.image_preimage f univ _).symm`. -/ lemma univ_filter_mem_range (f : α → β) [fintype β] [decidable_pred (λ y, y ∈ set.range f)] [decidable_eq β] : finset.univ.filter (λ y, y ∈ set.range f) = finset.univ.image f := univ_filter_exists f /-- A special case of `finset.sup_eq_supr` that omits the useless `x ∈ univ` binder. -/ lemma sup_univ_eq_supr [complete_lattice β] (f : α → β) : finset.univ.sup f = supr f := (sup_eq_supr _ f).trans $ congr_arg _ $ funext $ λ a, supr_pos (mem_univ _) /-- A special case of `finset.inf_eq_infi` that omits the useless `x ∈ univ` binder. -/ lemma inf_univ_eq_infi [complete_lattice β] (f : α → β) : finset.univ.inf f = infi f := sup_univ_eq_supr (by exact f : α → order_dual β) end finset open finset function namespace fintype instance decidable_pi_fintype {α} {β : α → Type*} [∀ a, decidable_eq (β a)] [fintype α] : decidable_eq (Π a, β a) := λ f g, decidable_of_iff (∀ a ∈ fintype.elems α, f a = g a) (by simp [function.funext_iff, fintype.complete]) instance decidable_forall_fintype {p : α → Prop} [decidable_pred p] [fintype α] : decidable (∀ a, p a) := decidable_of_iff (∀ a ∈ @univ α _, p a) (by simp) instance decidable_exists_fintype {p : α → Prop} [decidable_pred p] [fintype α] : decidable (∃ a, p a) := decidable_of_iff (∃ a ∈ @univ α _, p a) (by simp) instance decidable_mem_range_fintype [fintype α] [decidable_eq β] (f : α → β) : decidable_pred (∈ set.range f) := λ x, fintype.decidable_exists_fintype section bundled_homs instance decidable_eq_equiv_fintype [decidable_eq β] [fintype α] : decidable_eq (α ≃ β) := λ a b, decidable_of_iff (a.1 = b.1) equiv.coe_fn_injective.eq_iff instance decidable_eq_embedding_fintype [decidable_eq β] [fintype α] : decidable_eq (α ↪ β) := λ a b, decidable_of_iff ((a : α → β) = b) function.embedding.coe_injective.eq_iff @[to_additive] instance decidable_eq_one_hom_fintype [decidable_eq β] [fintype α] [has_one α] [has_one β]: decidable_eq (one_hom α β) := λ a b, decidable_of_iff ((a : α → β) = b) (injective.eq_iff one_hom.coe_inj) @[to_additive] instance decidable_eq_mul_hom_fintype [decidable_eq β] [fintype α] [has_mul α] [has_mul β]: decidable_eq (mul_hom α β) := λ a b, decidable_of_iff ((a : α → β) = b) (injective.eq_iff mul_hom.coe_inj) @[to_additive] instance decidable_eq_monoid_hom_fintype [decidable_eq β] [fintype α] [mul_one_class α] [mul_one_class β]: decidable_eq (α →* β) := λ a b, decidable_of_iff ((a : α → β) = b) (injective.eq_iff monoid_hom.coe_inj) instance decidable_eq_monoid_with_zero_hom_fintype [decidable_eq β] [fintype α] [mul_zero_one_class α] [mul_zero_one_class β]: decidable_eq (monoid_with_zero_hom α β) := λ a b, decidable_of_iff ((a : α → β) = b) (injective.eq_iff monoid_with_zero_hom.coe_inj) instance decidable_eq_ring_hom_fintype [decidable_eq β] [fintype α] [semiring α] [semiring β]: decidable_eq (α →+* β) := λ a b, decidable_of_iff ((a : α → β) = b) (injective.eq_iff ring_hom.coe_inj) end bundled_homs instance decidable_injective_fintype [decidable_eq α] [decidable_eq β] [fintype α] : decidable_pred (injective : (α → β) → Prop) := λ x, by unfold injective; apply_instance instance decidable_surjective_fintype [decidable_eq β] [fintype α] [fintype β] : decidable_pred (surjective : (α → β) → Prop) := λ x, by unfold surjective; apply_instance instance decidable_bijective_fintype [decidable_eq α] [decidable_eq β] [fintype α] [fintype β] : decidable_pred (bijective : (α → β) → Prop) := λ x, by unfold bijective; apply_instance instance decidable_right_inverse_fintype [decidable_eq α] [fintype α] (f : α → β) (g : β → α) : decidable (function.right_inverse f g) := show decidable (∀ x, g (f x) = x), by apply_instance instance decidable_left_inverse_fintype [decidable_eq β] [fintype β] (f : α → β) (g : β → α) : decidable (function.left_inverse f g) := show decidable (∀ x, f (g x) = x), by apply_instance lemma exists_max [fintype α] [nonempty α] {β : Type*} [linear_order β] (f : α → β) : ∃ x₀ : α, ∀ x, f x ≤ f x₀ := by simpa using exists_max_image univ f univ_nonempty lemma exists_min [fintype α] [nonempty α] {β : Type*} [linear_order β] (f : α → β) : ∃ x₀ : α, ∀ x, f x₀ ≤ f x := by simpa using exists_min_image univ f univ_nonempty /-- Construct a proof of `fintype α` from a universal multiset -/ def of_multiset [decidable_eq α] (s : multiset α) (H : ∀ x : α, x ∈ s) : fintype α := ⟨s.to_finset, by simpa using H⟩ /-- Construct a proof of `fintype α` from a universal list -/ def of_list [decidable_eq α] (l : list α) (H : ∀ x : α, x ∈ l) : fintype α := ⟨l.to_finset, by simpa using H⟩ theorem exists_univ_list (α) [fintype α] : ∃ l : list α, l.nodup ∧ ∀ x : α, x ∈ l := let ⟨l, e⟩ := quotient.exists_rep (@univ α _).1 in by have := and.intro univ.2 mem_univ_val; exact ⟨_, by rwa ←e at this⟩ /-- `card α` is the number of elements in `α`, defined when `α` is a fintype. -/ def card (α) [fintype α] : ℕ := (@univ α _).card /-- If `l` lists all the elements of `α` without duplicates, then `α ≃ fin (l.length)`. -/ def equiv_fin_of_forall_mem_list {α} [decidable_eq α] {l : list α} (h : ∀ x : α, x ∈ l) (nd : l.nodup) : α ≃ fin (l.length) := ⟨λ a, ⟨_, list.index_of_lt_length.2 (h a)⟩, λ i, l.nth_le i.1 i.2, λ a, by simp, λ ⟨i, h⟩, fin.eq_of_veq $ list.nodup_iff_nth_le_inj.1 nd _ _ (list.index_of_lt_length.2 (list.nth_le_mem _ _ _)) h $ by simp⟩ /-- There is (computably) a bijection between `α` and `fin (card α)`. Since it is not unique, and depends on which permutation of the universe list is used, the bijection is wrapped in `trunc` to preserve computability. See `fintype.equiv_fin` for the noncomputable version, and `fintype.trunc_equiv_fin_of_card_eq` and `fintype.equiv_fin_of_card_eq` for an equiv `α ≃ fin n` given `fintype.card α = n`. -/ def trunc_equiv_fin (α) [decidable_eq α] [fintype α] : trunc (α ≃ fin (card α)) := by unfold card finset.card; exact quot.rec_on_subsingleton (@univ α _).1 (λ l (h : ∀ x : α, x ∈ l) (nd : l.nodup), trunc.mk (equiv_fin_of_forall_mem_list h nd)) mem_univ_val univ.2 /-- There is a (noncomputable) bijection between `α` and `fin (card α)`. See `fintype.trunc_equiv_fin` for the computable version, and `fintype.trunc_equiv_fin_of_card_eq` and `fintype.equiv_fin_of_card_eq` for an equiv `α ≃ fin n` given `fintype.card α = n`. -/ noncomputable def equiv_fin (α) [fintype α] : α ≃ fin (card α) := by { letI := classical.dec_eq α, exact (trunc_equiv_fin α).out } instance (α : Type*) : subsingleton (fintype α) := ⟨λ ⟨s₁, h₁⟩ ⟨s₂, h₂⟩, by congr; simp [finset.ext_iff, h₁, h₂]⟩ /-- Given a predicate that can be represented by a finset, the subtype associated to the predicate is a fintype. -/ protected def subtype {p : α → Prop} (s : finset α) (H : ∀ x : α, x ∈ s ↔ p x) : fintype {x // p x} := ⟨⟨multiset.pmap subtype.mk s.1 (λ x, (H x).1), multiset.nodup_pmap (λ a _ b _, congr_arg subtype.val) s.2⟩, λ ⟨x, px⟩, multiset.mem_pmap.2 ⟨x, (H x).2 px, rfl⟩⟩ theorem subtype_card {p : α → Prop} (s : finset α) (H : ∀ x : α, x ∈ s ↔ p x) : @card {x // p x} (fintype.subtype s H) = s.card := multiset.card_pmap _ _ _ theorem card_of_subtype {p : α → Prop} (s : finset α) (H : ∀ x : α, x ∈ s ↔ p x) [fintype {x // p x}] : card {x // p x} = s.card := by { rw ← subtype_card s H, congr } /-- Construct a fintype from a finset with the same elements. -/ def of_finset {p : set α} (s : finset α) (H : ∀ x, x ∈ s ↔ x ∈ p) : fintype p := fintype.subtype s H @[simp] theorem card_of_finset {p : set α} (s : finset α) (H : ∀ x, x ∈ s ↔ x ∈ p) : @fintype.card p (of_finset s H) = s.card := fintype.subtype_card s H theorem card_of_finset' {p : set α} (s : finset α) (H : ∀ x, x ∈ s ↔ x ∈ p) [fintype p] : fintype.card p = s.card := by rw ←card_of_finset s H; congr /-- If `f : α → β` is a bijection and `α` is a fintype, then `β` is also a fintype. -/ def of_bijective [fintype α] (f : α → β) (H : function.bijective f) : fintype β := ⟨univ.map ⟨f, H.1⟩, λ b, let ⟨a, e⟩ := H.2 b in e ▸ mem_map_of_mem _ (mem_univ _)⟩ /-- If `f : α → β` is a surjection and `α` is a fintype, then `β` is also a fintype. -/ def of_surjective [decidable_eq β] [fintype α] (f : α → β) (H : function.surjective f) : fintype β := ⟨univ.image f, λ b, let ⟨a, e⟩ := H b in e ▸ mem_image_of_mem _ (mem_univ _)⟩ end fintype section inv namespace function variables [fintype α] [decidable_eq β] namespace injective variables {f : α → β} (hf : function.injective f) /-- The inverse of an `hf : injective` function `f : α → β`, of the type `↥(set.range f) → α`. This is the computable version of `function.inv_fun` that requires `fintype α` and `decidable_eq β`, or the function version of applying `(equiv.of_injective f hf).symm`. This function should not usually be used for actual computation because for most cases, an explicit inverse can be stated that has better computational properties. This function computes by checking all terms `a : α` to find the `f a = b`, so it is O(N) where `N = fintype.card α`. -/ def inv_of_mem_range : set.range f → α := λ b, finset.choose (λ a, f a = b) finset.univ ((exists_unique_congr (by simp)).mp (hf.exists_unique_of_mem_range b.property)) lemma left_inv_of_inv_of_mem_range (b : set.range f) : f (hf.inv_of_mem_range b) = b := (finset.choose_spec (λ a, f a = b) _ _).right @[simp] lemma right_inv_of_inv_of_mem_range (a : α) : hf.inv_of_mem_range (⟨f a, set.mem_range_self a⟩) = a := hf (finset.choose_spec (λ a', f a' = f a) _ _).right lemma inv_fun_restrict [nonempty α] : (set.range f).restrict (inv_fun f) = hf.inv_of_mem_range := begin ext ⟨b, h⟩, apply hf, simp [hf.left_inv_of_inv_of_mem_range, @inv_fun_eq _ _ _ f b (set.mem_range.mp h)] end lemma inv_of_mem_range_surjective : function.surjective hf.inv_of_mem_range := λ a, ⟨⟨f a, set.mem_range_self a⟩, by simp⟩ end injective namespace embedding variables (f : α ↪ β) (b : set.range f) /-- The inverse of an embedding `f : α ↪ β`, of the type `↥(set.range f) → α`. This is the computable version of `function.inv_fun` that requires `fintype α` and `decidable_eq β`, or the function version of applying `(equiv.of_injective f f.injective).symm`. This function should not usually be used for actual computation because for most cases, an explicit inverse can be stated that has better computational properties. This function computes by checking all terms `a : α` to find the `f a = b`, so it is O(N) where `N = fintype.card α`. -/ def inv_of_mem_range : α := f.injective.inv_of_mem_range b @[simp] lemma left_inv_of_inv_of_mem_range : f (f.inv_of_mem_range b) = b := f.injective.left_inv_of_inv_of_mem_range b @[simp] lemma right_inv_of_inv_of_mem_range (a : α) : f.inv_of_mem_range ⟨f a, set.mem_range_self a⟩ = a := f.injective.right_inv_of_inv_of_mem_range a lemma inv_fun_restrict [nonempty α] : (set.range f).restrict (inv_fun f) = f.inv_of_mem_range := begin ext ⟨b, h⟩, apply f.injective, simp [f.left_inv_of_inv_of_mem_range, @inv_fun_eq _ _ _ f b (set.mem_range.mp h)] end lemma inv_of_mem_range_surjective : function.surjective f.inv_of_mem_range := λ a, ⟨⟨f a, set.mem_range_self a⟩, by simp⟩ end embedding end function end inv namespace fintype /-- Given an injective function to a fintype, the domain is also a fintype. This is noncomputable because injectivity alone cannot be used to construct preimages. -/ noncomputable def of_injective [fintype β] (f : α → β) (H : function.injective f) : fintype α := by letI := classical.dec; exact if hα : nonempty α then by letI := classical.inhabited_of_nonempty hα; exact of_surjective (inv_fun f) (inv_fun_surjective H) else ⟨∅, λ x, (hα ⟨x⟩).elim⟩ /-- If `f : α ≃ β` and `α` is a fintype, then `β` is also a fintype. -/ def of_equiv (α : Type*) [fintype α] (f : α ≃ β) : fintype β := of_bijective _ f.bijective theorem of_equiv_card [fintype α] (f : α ≃ β) : @card β (of_equiv α f) = card α := multiset.card_map _ _ theorem card_congr {α β} [fintype α] [fintype β] (f : α ≃ β) : card α = card β := by rw ← of_equiv_card f; congr section variables [fintype α] [fintype β] /-- If the cardinality of `α` is `n`, there is computably a bijection between `α` and `fin n`. See `fintype.equiv_fin_of_card_eq` for the noncomputable definition, and `fintype.trunc_equiv_fin` and `fintype.equiv_fin` for the bijection `α ≃ fin (card α)`. -/ def trunc_equiv_fin_of_card_eq [decidable_eq α] {n : ℕ} (h : fintype.card α = n) : trunc (α ≃ fin n) := (trunc_equiv_fin α).map (λ e, e.trans (fin.cast h).to_equiv) /-- If the cardinality of `α` is `n`, there is noncomputably a bijection between `α` and `fin n`. See `fintype.trunc_equiv_fin_of_card_eq` for the computable definition, and `fintype.trunc_equiv_fin` and `fintype.equiv_fin` for the bijection `α ≃ fin (card α)`. -/ noncomputable def equiv_fin_of_card_eq {n : ℕ} (h : fintype.card α = n) : α ≃ fin n := by { letI := classical.dec_eq α, exact (trunc_equiv_fin_of_card_eq h).out } /-- Two `fintype`s with the same cardinality are (computably) in bijection. See `fintype.equiv_of_card_eq` for the noncomputable version, and `fintype.trunc_equiv_fin_of_card_eq` and `fintype.equiv_fin_of_card_eq` for the specialization to `fin`. -/ def trunc_equiv_of_card_eq [decidable_eq α] [decidable_eq β] (h : card α = card β) : trunc (α ≃ β) := (trunc_equiv_fin_of_card_eq h).bind (λ e, (trunc_equiv_fin β).map (λ e', e.trans e'.symm)) /-- Two `fintype`s with the same cardinality are (noncomputably) in bijection. See `fintype.trunc_equiv_of_card_eq` for the computable version, and `fintype.trunc_equiv_fin_of_card_eq` and `fintype.equiv_fin_of_card_eq` for the specialization to `fin`. -/ noncomputable def equiv_of_card_eq (h : card α = card β) : α ≃ β := by { letI := classical.dec_eq α, letI := classical.dec_eq β, exact (trunc_equiv_of_card_eq h).out } end theorem card_eq {α β} [F : fintype α] [G : fintype β] : card α = card β ↔ nonempty (α ≃ β) := ⟨λ h, by { haveI := classical.prop_decidable, exact (trunc_equiv_of_card_eq h).nonempty }, λ ⟨f⟩, card_congr f⟩ /-- Any subsingleton type with a witness is a fintype (with one term). -/ def of_subsingleton (a : α) [subsingleton α] : fintype α := ⟨{a}, λ b, finset.mem_singleton.2 (subsingleton.elim _ _)⟩ @[simp] theorem univ_of_subsingleton (a : α) [subsingleton α] : @univ _ (of_subsingleton a) = {a} := rfl /-- Note: this lemma is specifically about `fintype.of_subsingleton`. For a statement about arbitrary `fintype` instances, use either `fintype.card_le_one_iff_subsingleton` or `fintype.card_unique`. -/ @[simp] theorem card_of_subsingleton (a : α) [subsingleton α] : @fintype.card _ (of_subsingleton a) = 1 := rfl @[simp] theorem card_unique [unique α] [h : fintype α] : fintype.card α = 1 := subsingleton.elim (of_subsingleton $ default α) h ▸ card_of_subsingleton _ @[priority 100] -- see Note [lower instance priority] instance of_is_empty [is_empty α] : fintype α := ⟨∅, is_empty_elim⟩ /-- Note: this lemma is specifically about `fintype.of_is_empty`. For a statement about arbitrary `fintype` instances, use `fintype.univ_is_empty`. -/ -- no-lint since while `fintype.of_is_empty` can prove this, it isn't applicable for `dsimp`. @[simp, nolint simp_nf] theorem univ_of_is_empty [is_empty α] : @univ α _ = ∅ := rfl /-- Note: this lemma is specifically about `fintype.of_is_empty`. For a statement about arbitrary `fintype` instances, use `fintype.card_eq_zero_iff`. -/ @[simp] theorem card_of_is_empty [is_empty α] : fintype.card α = 0 := rfl open_locale classical variables (α) /-- Any subsingleton type is (noncomputably) a fintype (with zero or one term). -/ @[priority 5] -- see Note [lower instance priority] noncomputable instance of_subsingleton' [subsingleton α] : fintype α := if h : nonempty α then of_subsingleton (nonempty.some h) else @fintype.of_is_empty _ $ not_nonempty_iff.mp h end fintype namespace set /-- Construct a finset enumerating a set `s`, given a `fintype` instance. -/ def to_finset (s : set α) [fintype s] : finset α := ⟨(@finset.univ s _).1.map subtype.val, multiset.nodup_map (λ a b, subtype.eq) finset.univ.2⟩ @[simp] theorem mem_to_finset {s : set α} [fintype s] {a : α} : a ∈ s.to_finset ↔ a ∈ s := by simp [to_finset] @[simp] theorem mem_to_finset_val {s : set α} [fintype s] {a : α} : a ∈ s.to_finset.1 ↔ a ∈ s := mem_to_finset -- We use an arbitrary `[fintype s]` instance here, -- not necessarily coming from a `[fintype α]`. @[simp] lemma to_finset_card {α : Type*} (s : set α) [fintype s] : s.to_finset.card = fintype.card s := multiset.card_map subtype.val finset.univ.val @[simp] theorem coe_to_finset (s : set α) [fintype s] : (↑s.to_finset : set α) = s := set.ext $ λ _, mem_to_finset @[simp] theorem to_finset_inj {s t : set α} [fintype s] [fintype t] : s.to_finset = t.to_finset ↔ s = t := ⟨λ h, by rw [←s.coe_to_finset, h, t.coe_to_finset], λ h, by simp [h]; congr⟩ @[simp, mono] theorem to_finset_mono {s t : set α} [fintype s] [fintype t] : s.to_finset ⊆ t.to_finset ↔ s ⊆ t := by simp [finset.subset_iff, set.subset_def] @[simp, mono] theorem to_finset_strict_mono {s t : set α} [fintype s] [fintype t] : s.to_finset ⊂ t.to_finset ↔ s ⊂ t := begin rw [←lt_eq_ssubset, ←finset.lt_iff_ssubset, lt_iff_le_and_ne, lt_iff_le_and_ne], simp end @[simp] theorem to_finset_disjoint_iff [decidable_eq α] {s t : set α} [fintype s] [fintype t] : disjoint s.to_finset t.to_finset ↔ disjoint s t := ⟨λ h x hx, h (by simpa using hx), λ h x hx, h (by simpa using hx)⟩ end set lemma finset.card_univ [fintype α] : (finset.univ : finset α).card = fintype.card α := rfl lemma finset.eq_univ_of_card [fintype α] (s : finset α) (hs : s.card = fintype.card α) : s = univ := eq_of_subset_of_card_le (subset_univ _) $ by rw [hs, finset.card_univ] lemma finset.card_eq_iff_eq_univ [fintype α] (s : finset α) : s.card = fintype.card α ↔ s = finset.univ := ⟨s.eq_univ_of_card, by { rintro rfl, exact finset.card_univ, }⟩ lemma finset.card_le_univ [fintype α] (s : finset α) : s.card ≤ fintype.card α := card_le_of_subset (subset_univ s) lemma finset.card_lt_univ_of_not_mem [fintype α] {s : finset α} {x : α} (hx : x ∉ s) : s.card < fintype.card α := card_lt_card ⟨subset_univ s, not_forall.2 ⟨x, λ hx', hx (hx' $ mem_univ x)⟩⟩ lemma finset.card_lt_iff_ne_univ [fintype α] (s : finset α) : s.card < fintype.card α ↔ s ≠ finset.univ := s.card_le_univ.lt_iff_ne.trans (not_iff_not_of_iff s.card_eq_iff_eq_univ) lemma finset.card_compl_lt_iff_nonempty [fintype α] [decidable_eq α] (s : finset α) : sᶜ.card < fintype.card α ↔ s.nonempty := sᶜ.card_lt_iff_ne_univ.trans s.compl_ne_univ_iff_nonempty lemma finset.card_univ_diff [decidable_eq α] [fintype α] (s : finset α) : (finset.univ \ s).card = fintype.card α - s.card := finset.card_sdiff (subset_univ s) lemma finset.card_compl [decidable_eq α] [fintype α] (s : finset α) : sᶜ.card = fintype.card α - s.card := finset.card_univ_diff s instance (n : ℕ) : fintype (fin n) := ⟨finset.fin_range n, finset.mem_fin_range⟩ lemma fin.univ_def (n : ℕ) : (univ : finset (fin n)) = finset.fin_range n := rfl @[simp] theorem fintype.card_fin (n : ℕ) : fintype.card (fin n) = n := list.length_fin_range n @[simp] lemma finset.card_fin (n : ℕ) : finset.card (finset.univ : finset (fin n)) = n := by rw [finset.card_univ, fintype.card_fin] /-- The cardinality of `fin (bit0 k)` is even, `fact` version. This `fact` is needed as an instance by `matrix.special_linear_group.has_neg`. -/ lemma fintype.card_fin_even {k : ℕ} : fact (even (fintype.card (fin (bit0 k)))) := ⟨by { rw [fintype.card_fin], exact even_bit0 k }⟩ lemma card_finset_fin_le {n : ℕ} (s : finset (fin n)) : s.card ≤ n := by simpa only [fintype.card_fin] using s.card_le_univ lemma fin.equiv_iff_eq {m n : ℕ} : nonempty (fin m ≃ fin n) ↔ m = n := ⟨λ ⟨h⟩, by simpa using fintype.card_congr h, λ h, ⟨equiv.cast $ h ▸ rfl ⟩ ⟩ @[simp] lemma fin.image_succ_above_univ {n : ℕ} (i : fin (n + 1)) : univ.image i.succ_above = {i}ᶜ := by { ext m, simp } @[simp] lemma fin.image_succ_univ (n : ℕ) : (univ : finset (fin n)).image fin.succ = {0}ᶜ := by rw [← fin.succ_above_zero, fin.image_succ_above_univ] @[simp] lemma fin.image_cast_succ (n : ℕ) : (univ : finset (fin n)).image fin.cast_succ = {fin.last n}ᶜ := by rw [← fin.succ_above_last, fin.image_succ_above_univ] /-- Embed `fin n` into `fin (n + 1)` by prepending zero to the `univ` -/ lemma fin.univ_succ (n : ℕ) : (univ : finset (fin (n + 1))) = insert 0 (univ.image fin.succ) := by simp /-- Embed `fin n` into `fin (n + 1)` by appending a new `fin.last n` to the `univ` -/ lemma fin.univ_cast_succ (n : ℕ) : (univ : finset (fin (n + 1))) = insert (fin.last n) (univ.image fin.cast_succ) := by simp /-- Embed `fin n` into `fin (n + 1)` by inserting around a specified pivot `p : fin (n + 1)` into the `univ` -/ lemma fin.univ_succ_above (n : ℕ) (p : fin (n + 1)) : (univ : finset (fin (n + 1))) = insert p (univ.image (fin.succ_above p)) := by simp @[instance, priority 10] def unique.fintype {α : Type*} [unique α] : fintype α := fintype.of_subsingleton (default α) /-- Short-circuit instance to decrease search for `unique.fintype`, since that relies on a subsingleton elimination for `unique`. -/ instance fintype.subtype_eq (y : α) : fintype {x // x = y} := fintype.subtype {y} (by simp) /-- Short-circuit instance to decrease search for `unique.fintype`, since that relies on a subsingleton elimination for `unique`. -/ instance fintype.subtype_eq' (y : α) : fintype {x // y = x} := fintype.subtype {y} (by simp [eq_comm]) @[simp] lemma univ_unique {α : Type*} [unique α] [f : fintype α] : @finset.univ α _ = {default α} := by rw [subsingleton.elim f (@unique.fintype α _)]; refl @[simp] lemma univ_is_empty {α : Type*} [is_empty α] [fintype α] : @finset.univ α _ = ∅ := finset.ext is_empty_elim @[simp] lemma fintype.card_subtype_eq (y : α) [fintype {x // x = y}] : fintype.card {x // x = y} = 1 := fintype.card_unique @[simp] lemma fintype.card_subtype_eq' (y : α) [fintype {x // y = x}] : fintype.card {x // y = x} = 1 := fintype.card_unique @[simp] theorem fintype.univ_empty : @univ empty _ = ∅ := rfl @[simp] theorem fintype.card_empty : fintype.card empty = 0 := rfl @[simp] theorem fintype.univ_pempty : @univ pempty _ = ∅ := rfl @[simp] theorem fintype.card_pempty : fintype.card pempty = 0 := rfl instance : fintype unit := fintype.of_subsingleton () theorem fintype.univ_unit : @univ unit _ = {()} := rfl theorem fintype.card_unit : fintype.card unit = 1 := rfl instance : fintype punit := fintype.of_subsingleton punit.star @[simp] theorem fintype.univ_punit : @univ punit _ = {punit.star} := rfl @[simp] theorem fintype.card_punit : fintype.card punit = 1 := rfl instance : fintype bool := ⟨⟨tt ::ₘ ff ::ₘ 0, by simp⟩, λ x, by cases x; simp⟩ @[simp] theorem fintype.univ_bool : @univ bool _ = {tt, ff} := rfl instance units_int.fintype : fintype (units ℤ) := ⟨{1, -1}, λ x, by cases int.units_eq_one_or x; simp *⟩ @[simp] lemma units_int.univ : (finset.univ : finset (units ℤ)) = {1, -1} := rfl instance additive.fintype : Π [fintype α], fintype (additive α) := id instance multiplicative.fintype : Π [fintype α], fintype (multiplicative α) := id @[simp] theorem fintype.card_units_int : fintype.card (units ℤ) = 2 := rfl @[simp] theorem fintype.card_bool : fintype.card bool = 2 := rfl instance {α : Type*} [fintype α] : fintype (option α) := ⟨univ.insert_none, λ a, by simp⟩ @[simp] theorem fintype.card_option {α : Type*} [fintype α] : fintype.card (option α) = fintype.card α + 1 := (finset.card_cons _).trans $ congr_arg2 _ (card_map _) rfl instance {α : Type*} (β : α → Type*) [fintype α] [∀ a, fintype (β a)] : fintype (sigma β) := ⟨univ.sigma (λ _, univ), λ ⟨a, b⟩, by simp⟩ @[simp] lemma finset.univ_sigma_univ {α : Type*} {β : α → Type*} [fintype α] [∀ a, fintype (β a)] : (univ : finset α).sigma (λ a, (univ : finset (β a))) = univ := rfl instance (α β : Type*) [fintype α] [fintype β] : fintype (α × β) := ⟨univ.product univ, λ ⟨a, b⟩, by simp⟩ @[simp] lemma finset.univ_product_univ {α β : Type*} [fintype α] [fintype β] : (univ : finset α).product (univ : finset β) = univ := rfl @[simp] theorem fintype.card_prod (α β : Type*) [fintype α] [fintype β] : fintype.card (α × β) = fintype.card α * fintype.card β := card_product _ _ /-- Given that `α × β` is a fintype, `α` is also a fintype. -/ def fintype.prod_left {α β} [decidable_eq α] [fintype (α × β)] [nonempty β] : fintype α := ⟨(fintype.elems (α × β)).image prod.fst, λ a, let ⟨b⟩ := ‹nonempty β› in by simp; exact ⟨b, fintype.complete _⟩⟩ /-- Given that `α × β` is a fintype, `β` is also a fintype. -/ def fintype.prod_right {α β} [decidable_eq β] [fintype (α × β)] [nonempty α] : fintype β := ⟨(fintype.elems (α × β)).image prod.snd, λ b, let ⟨a⟩ := ‹nonempty α› in by simp; exact ⟨a, fintype.complete _⟩⟩ instance (α : Type*) [fintype α] : fintype (ulift α) := fintype.of_equiv _ equiv.ulift.symm @[simp] theorem fintype.card_ulift (α : Type*) [fintype α] : fintype.card (ulift α) = fintype.card α := fintype.of_equiv_card _ instance (α : Type*) [fintype α] : fintype (plift α) := fintype.of_equiv _ equiv.plift.symm @[simp] theorem fintype.card_plift (α : Type*) [fintype α] : fintype.card (plift α) = fintype.card α := fintype.of_equiv_card _ lemma univ_sum_type {α β : Type*} [fintype α] [fintype β] [fintype (α ⊕ β)] [decidable_eq (α ⊕ β)] : (univ : finset (α ⊕ β)) = map function.embedding.inl univ ∪ map function.embedding.inr univ := begin rw [eq_comm, eq_univ_iff_forall], simp only [mem_union, mem_map, exists_prop, mem_univ, true_and], rintro (x|y), exacts [or.inl ⟨x, rfl⟩, or.inr ⟨y, rfl⟩] end instance (α : Type u) (β : Type v) [fintype α] [fintype β] : fintype (α ⊕ β) := @fintype.of_equiv _ _ (@sigma.fintype _ (λ b, cond b (ulift α) (ulift.{(max u v) v} β)) _ (λ b, by cases b; apply ulift.fintype)) ((equiv.sum_equiv_sigma_bool _ _).symm.trans (equiv.sum_congr equiv.ulift equiv.ulift)) /-- Given that `α ⊕ β` is a fintype, `α` is also a fintype. This is non-computable as it uses that `sum.inl` is an injection, but there's no clear inverse if `α` is empty. -/ noncomputable def fintype.sum_left {α β} [fintype (α ⊕ β)] : fintype α := fintype.of_injective (sum.inl : α → α ⊕ β) sum.inl_injective /-- Given that `α ⊕ β` is a fintype, `β` is also a fintype. This is non-computable as it uses that `sum.inr` is an injection, but there's no clear inverse if `β` is empty. -/ noncomputable def fintype.sum_right {α β} [fintype (α ⊕ β)] : fintype β := fintype.of_injective (sum.inr : β → α ⊕ β) sum.inr_injective @[simp] theorem fintype.card_sum [fintype α] [fintype β] : fintype.card (α ⊕ β) = fintype.card α + fintype.card β := begin classical, rw [←finset.card_univ, univ_sum_type, finset.card_union_eq], { simp [finset.card_univ] }, { intros x hx, suffices : (∃ (a : α), sum.inl a = x) ∧ ∃ (b : β), sum.inr b = x, { obtain ⟨⟨a, rfl⟩, ⟨b, hb⟩⟩ := this, simpa using hb }, simpa using hx } end /-- If the subtype of all-but-one elements is a `fintype` then the type itself is a `fintype`. -/ def fintype_of_fintype_ne (a : α) [decidable_pred (= a)] (h : fintype {b // b ≠ a}) : fintype α := fintype.of_equiv _ $ equiv.sum_compl (= a) section finset /-! ### `fintype (s : finset α)` -/ instance finset.fintype_coe_sort {α : Type u} (s : finset α) : fintype s := ⟨s.attach, s.mem_attach⟩ @[simp] lemma finset.univ_eq_attach {α : Type u} (s : finset α) : (univ : finset s) = s.attach := rfl end finset namespace fintype variables [fintype α] [fintype β] lemma card_le_of_injective (f : α → β) (hf : function.injective f) : card α ≤ card β := finset.card_le_card_of_inj_on f (λ _ _, finset.mem_univ _) (λ _ _ _ _ h, hf h) lemma card_le_of_embedding (f : α ↪ β) : card α ≤ card β := card_le_of_injective f f.2 lemma card_lt_of_injective_of_not_mem (f : α → β) (h : function.injective f) {b : β} (w : b ∉ set.range f) : card α < card β := calc card α = (univ.map ⟨f, h⟩).card : (card_map _).symm ... < card β : finset.card_lt_univ_of_not_mem $ by rwa [← mem_coe, coe_map, coe_univ, set.image_univ] lemma card_lt_of_injective_not_surjective (f : α → β) (h : function.injective f) (h' : ¬function.surjective f) : card α < card β := let ⟨y, hy⟩ := not_forall.1 h' in card_lt_of_injective_of_not_mem f h hy lemma card_le_of_surjective (f : α → β) (h : function.surjective f) : card β ≤ card α := card_le_of_injective _ (function.injective_surj_inv h) /-- The pigeonhole principle for finitely many pigeons and pigeonholes. This is the `fintype` version of `finset.exists_ne_map_eq_of_card_lt_of_maps_to`. -/ lemma exists_ne_map_eq_of_card_lt (f : α → β) (h : fintype.card β < fintype.card α) : ∃ x y, x ≠ y ∧ f x = f y := let ⟨x, _, y, _, h⟩ := finset.exists_ne_map_eq_of_card_lt_of_maps_to h (λ x _, mem_univ (f x)) in ⟨x, y, h⟩ lemma card_eq_one_iff : card α = 1 ↔ (∃ x : α, ∀ y, y = x) := by rw [←card_unit, card_eq]; exact ⟨λ ⟨a⟩, ⟨a.symm (), λ y, a.injective (subsingleton.elim _ _)⟩, λ ⟨x, hx⟩, ⟨⟨λ _, (), λ _, x, λ _, (hx _).trans (hx _).symm, λ _, subsingleton.elim _ _⟩⟩⟩ lemma card_eq_zero_iff : card α = 0 ↔ is_empty α := by rw [card, finset.card_eq_zero, univ_eq_empty_iff] lemma card_eq_zero [is_empty α] : card α = 0 := card_eq_zero_iff.2 ‹_› lemma card_eq_one_iff_nonempty_unique : card α = 1 ↔ nonempty (unique α) := ⟨λ h, let ⟨d, h⟩ := fintype.card_eq_one_iff.mp h in ⟨{ default := d, uniq := h}⟩, λ ⟨h⟩, by exactI fintype.card_unique⟩ /-- A `fintype` with cardinality zero is equivalent to `empty`. -/ def card_eq_zero_equiv_equiv_empty : card α = 0 ≃ (α ≃ empty) := (equiv.of_iff card_eq_zero_iff).trans (equiv.equiv_empty_equiv α).symm lemma card_pos_iff : 0 < card α ↔ nonempty α := pos_iff_ne_zero.trans $ not_iff_comm.mp $ not_nonempty_iff.trans card_eq_zero_iff.symm lemma card_pos [h : nonempty α] : 0 < card α := card_pos_iff.mpr h lemma card_ne_zero [nonempty α] : card α ≠ 0 := ne_of_gt card_pos lemma card_le_one_iff : card α ≤ 1 ↔ (∀ a b : α, a = b) := let n := card α in have hn : n = card α := rfl, match n, hn with | 0 := λ ha, ⟨λ h, λ a, (card_eq_zero_iff.1 ha.symm).elim a, λ _, ha ▸ nat.le_succ _⟩ | 1 := λ ha, ⟨λ h, λ a b, let ⟨x, hx⟩ := card_eq_one_iff.1 ha.symm in by rw [hx a, hx b], λ _, ha ▸ le_refl _⟩ | (n+2) := λ ha, ⟨λ h, by rw ← ha at h; exact absurd h dec_trivial, (λ h, card_unit ▸ card_le_of_injective (λ _, ()) (λ _ _ _, h _ _))⟩ end lemma card_le_one_iff_subsingleton : card α ≤ 1 ↔ subsingleton α := card_le_one_iff.trans subsingleton_iff.symm lemma one_lt_card_iff_nontrivial : 1 < card α ↔ nontrivial α := begin classical, rw ←not_iff_not, push_neg, rw [not_nontrivial_iff_subsingleton, card_le_one_iff_subsingleton] end lemma exists_ne_of_one_lt_card (h : 1 < card α) (a : α) : ∃ b : α, b ≠ a := by { haveI : nontrivial α := one_lt_card_iff_nontrivial.1 h, exact exists_ne a } lemma exists_pair_of_one_lt_card (h : 1 < card α) : ∃ (a b : α), a ≠ b := by { haveI : nontrivial α := one_lt_card_iff_nontrivial.1 h, exact exists_pair_ne α } lemma card_eq_one_of_forall_eq {i : α} (h : ∀ j, j = i) : card α = 1 := fintype.card_eq_one_iff.2 ⟨i,h⟩ lemma one_lt_card [h : nontrivial α] : 1 < fintype.card α := fintype.one_lt_card_iff_nontrivial.mpr h lemma injective_iff_surjective {f : α → α} : injective f ↔ surjective f := by haveI := classical.prop_decidable; exact have ∀ {f : α → α}, injective f → surjective f, from λ f hinj x, have h₁ : image f univ = univ := eq_of_subset_of_card_le (subset_univ _) ((card_image_of_injective univ hinj).symm ▸ le_refl _), have h₂ : x ∈ image f univ := h₁.symm ▸ mem_univ _, exists_of_bex (mem_image.1 h₂), ⟨this, λ hsurj, has_left_inverse.injective ⟨surj_inv hsurj, left_inverse_of_surjective_of_right_inverse (this (injective_surj_inv _)) (right_inverse_surj_inv _)⟩⟩ lemma injective_iff_bijective {f : α → α} : injective f ↔ bijective f := by simp [bijective, injective_iff_surjective] lemma surjective_iff_bijective {f : α → α} : surjective f ↔ bijective f := by simp [bijective, injective_iff_surjective] lemma injective_iff_surjective_of_equiv {β : Type*} {f : α → β} (e : α ≃ β) : injective f ↔ surjective f := have injective (e.symm ∘ f) ↔ surjective (e.symm ∘ f), from injective_iff_surjective, ⟨λ hinj, by simpa [function.comp] using e.surjective.comp (this.1 (e.symm.injective.comp hinj)), λ hsurj, by simpa [function.comp] using e.injective.comp (this.2 (e.symm.surjective.comp hsurj))⟩ lemma card_of_bijective {f : α → β} (hf : bijective f) : card α = card β := card_congr (equiv.of_bijective f hf) lemma bijective_iff_injective_and_card (f : α → β) : bijective f ↔ injective f ∧ card α = card β := begin split, { intro h, exact ⟨h.1, card_of_bijective h⟩ }, { rintro ⟨hf, h⟩, refine ⟨hf, _⟩, rwa ←injective_iff_surjective_of_equiv (equiv_of_card_eq h) } end lemma bijective_iff_surjective_and_card (f : α → β) : bijective f ↔ surjective f ∧ card α = card β := begin split, { intro h, exact ⟨h.2, card_of_bijective h⟩ }, { rintro ⟨hf, h⟩, refine ⟨_, hf⟩, rwa injective_iff_surjective_of_equiv (equiv_of_card_eq h) } end lemma right_inverse_of_left_inverse_of_card_le {f : α → β} {g : β → α} (hfg : left_inverse f g) (hcard : card α ≤ card β) : right_inverse f g := have hsurj : surjective f, from surjective_iff_has_right_inverse.2 ⟨g, hfg⟩, right_inverse_of_injective_of_left_inverse ((bijective_iff_surjective_and_card _).2 ⟨hsurj, le_antisymm hcard (card_le_of_surjective f hsurj)⟩ ).1 hfg lemma left_inverse_of_right_inverse_of_card_le {f : α → β} {g : β → α} (hfg : right_inverse f g) (hcard : card β ≤ card α) : left_inverse f g := right_inverse_of_left_inverse_of_card_le hfg hcard end fintype lemma fintype.coe_image_univ [fintype α] [decidable_eq β] {f : α → β} : ↑(finset.image f finset.univ) = set.range f := by { ext x, simp } instance list.subtype.fintype [decidable_eq α] (l : list α) : fintype {x // x ∈ l} := fintype.of_list l.attach l.mem_attach instance multiset.subtype.fintype [decidable_eq α] (s : multiset α) : fintype {x // x ∈ s} := fintype.of_multiset s.attach s.mem_attach instance finset.subtype.fintype (s : finset α) : fintype {x // x ∈ s} := ⟨s.attach, s.mem_attach⟩ instance finset_coe.fintype (s : finset α) : fintype (↑s : set α) := finset.subtype.fintype s @[simp] lemma fintype.card_coe (s : finset α) : fintype.card s = s.card := card_attach lemma finset.attach_eq_univ {s : finset α} : s.attach = finset.univ := rfl instance plift.fintype_Prop (p : Prop) [decidable p] : fintype (plift p) := ⟨if h : p then {⟨h⟩} else ∅, λ ⟨h⟩, by simp [h]⟩ instance Prop.fintype : fintype Prop := ⟨⟨true ::ₘ false ::ₘ 0, by simp [true_ne_false]⟩, classical.cases (by simp) (by simp)⟩ instance subtype.fintype (p : α → Prop) [decidable_pred p] [fintype α] : fintype {x // p x} := fintype.subtype (univ.filter p) (by simp) /-- A set on a fintype, when coerced to a type, is a fintype. -/ def set_fintype {α} [fintype α] (s : set α) [decidable_pred (∈ s)] : fintype s := subtype.fintype (λ x, x ∈ s) lemma set_fintype_card_le_univ {α : Type*} [fintype α] (s : set α) [fintype ↥s] : fintype.card ↥s ≤ fintype.card α := fintype.card_le_of_embedding (function.embedding.subtype s) section variables (α) /-- The `units α` type is equivalent to a subtype of `α × α`. -/ @[simps] def _root_.units_equiv_prod_subtype [monoid α] : units α ≃ {p : α × α // p.1 * p.2 = 1 ∧ p.2 * p.1 = 1} := { to_fun := λ u, ⟨(u, ↑u⁻¹), u.val_inv, u.inv_val⟩, inv_fun := λ p, units.mk (p : α × α).1 (p : α × α).2 p.prop.1 p.prop.2, left_inv := λ u, units.ext rfl, right_inv := λ p, subtype.ext $ prod.ext rfl rfl} /-- In a `group_with_zero` `α`, the unit group `units α` is equivalent to the subtype of nonzero elements. -/ @[simps] def _root_.units_equiv_ne_zero [group_with_zero α] : units α ≃ {a : α // a ≠ 0} := ⟨λ a, ⟨a, a.ne_zero⟩, λ a, units.mk0 _ a.prop, λ _, units.ext rfl, λ _, subtype.ext rfl⟩ end instance [monoid α] [fintype α] [decidable_eq α] : fintype (units α) := fintype.of_equiv _ (units_equiv_prod_subtype α).symm lemma fintype.card_units [group_with_zero α] [fintype α] [fintype (units α)] : fintype.card (units α) = fintype.card α - 1 := begin classical, rw [eq_comm, nat.sub_eq_iff_eq_add (fintype.card_pos_iff.2 ⟨(0 : α)⟩), fintype.card_congr (units_equiv_ne_zero α)], have := fintype.card_congr (equiv.sum_compl (= (0 : α))).symm, rwa [fintype.card_sum, add_comm, fintype.card_subtype_eq] at this, end namespace function.embedding /-- An embedding from a `fintype` to itself can be promoted to an equivalence. -/ noncomputable def equiv_of_fintype_self_embedding [fintype α] (e : α ↪ α) : α ≃ α := equiv.of_bijective e (fintype.injective_iff_bijective.1 e.2) @[simp] lemma equiv_of_fintype_self_embedding_to_embedding [fintype α] (e : α ↪ α) : e.equiv_of_fintype_self_embedding.to_embedding = e := by { ext, refl, } /-- If `‖β‖ < ‖α‖` there are no embeddings `α ↪ β`. This is a formulation of the pigeonhole principle. Note this cannot be an instance as it needs `h`. -/ @[simp] lemma is_empty_of_card_lt [fintype α] [fintype β] (h : fintype.card β < fintype.card α) : is_empty (α ↪ β) := ⟨λ f, let ⟨x, y, ne, feq⟩ := fintype.exists_ne_map_eq_of_card_lt f h in ne $ f.injective feq⟩ /-- A constructive embedding of a fintype `α` in another fintype `β` when `card α ≤ card β`. -/ def trunc_of_card_le [fintype α] [fintype β] [decidable_eq α] [decidable_eq β] (h : fintype.card α ≤ fintype.card β) : trunc (α ↪ β) := (fintype.trunc_equiv_fin α).bind $ λ ea, (fintype.trunc_equiv_fin β).map $ λ eb, ea.to_embedding.trans ((fin.cast_le h).to_embedding.trans eb.symm.to_embedding) lemma nonempty_of_card_le [fintype α] [fintype β] (h : fintype.card α ≤ fintype.card β) : nonempty (α ↪ β) := by { classical, exact (trunc_of_card_le h).nonempty } lemma exists_of_card_le_finset [fintype α] {s : finset β} (h : fintype.card α ≤ s.card) : ∃ (f : α ↪ β), set.range f ⊆ s := begin rw ← fintype.card_coe at h, rcases nonempty_of_card_le h with ⟨f⟩, exact ⟨f.trans (embedding.subtype _), by simp [set.range_subset_iff]⟩ end end function.embedding @[simp] lemma finset.univ_map_embedding {α : Type*} [fintype α] (e : α ↪ α) : univ.map e = univ := by rw [←e.equiv_of_fintype_self_embedding_to_embedding, univ_map_equiv_to_embedding] namespace fintype lemma card_lt_of_surjective_not_injective [fintype α] [fintype β] (f : α → β) (h : function.surjective f) (h' : ¬function.injective f) : card β < card α := card_lt_of_injective_not_surjective _ (function.injective_surj_inv h) $ λ hg, have w : function.bijective (function.surj_inv h) := ⟨function.injective_surj_inv h, hg⟩, h' $ (injective_iff_surjective_of_equiv (equiv.of_bijective _ w).symm).mpr h variables [decidable_eq α] [fintype α] {δ : α → Type*} /-- Given for all `a : α` a finset `t a` of `δ a`, then one can define the finset `fintype.pi_finset t` of all functions taking values in `t a` for all `a`. This is the analogue of `finset.pi` where the base finset is `univ` (but formally they are not the same, as there is an additional condition `i ∈ finset.univ` in the `finset.pi` definition). -/ def pi_finset (t : Π a, finset (δ a)) : finset (Π a, δ a) := (finset.univ.pi t).map ⟨λ f a, f a (mem_univ a), λ _ _, by simp [function.funext_iff]⟩ @[simp] lemma mem_pi_finset {t : Π a, finset (δ a)} {f : Π a, δ a} : f ∈ pi_finset t ↔ (∀ a, f a ∈ t a) := begin split, { simp only [pi_finset, mem_map, and_imp, forall_prop_of_true, exists_prop, mem_univ, exists_imp_distrib, mem_pi], rintro g hg hgf a, rw ← hgf, exact hg a }, { simp only [pi_finset, mem_map, forall_prop_of_true, exists_prop, mem_univ, mem_pi], exact λ hf, ⟨λ a ha, f a, hf, rfl⟩ } end lemma pi_finset_subset (t₁ t₂ : Π a, finset (δ a)) (h : ∀ a, t₁ a ⊆ t₂ a) : pi_finset t₁ ⊆ pi_finset t₂ := λ g hg, mem_pi_finset.2 $ λ a, h a $ mem_pi_finset.1 hg a lemma pi_finset_disjoint_of_disjoint [∀ a, decidable_eq (δ a)] (t₁ t₂ : Π a, finset (δ a)) {a : α} (h : disjoint (t₁ a) (t₂ a)) : disjoint (pi_finset t₁) (pi_finset t₂) := disjoint_iff_ne.2 $ λ f₁ hf₁ f₂ hf₂ eq₁₂, disjoint_iff_ne.1 h (f₁ a) (mem_pi_finset.1 hf₁ a) (f₂ a) (mem_pi_finset.1 hf₂ a) (congr_fun eq₁₂ a) end fintype /-! ### pi -/ /-- A dependent product of fintypes, indexed by a fintype, is a fintype. -/ instance pi.fintype {α : Type*} {β : α → Type*} [decidable_eq α] [fintype α] [∀ a, fintype (β a)] : fintype (Π a, β a) := ⟨fintype.pi_finset (λ _, univ), by simp⟩ @[simp] lemma fintype.pi_finset_univ {α : Type*} {β : α → Type*} [decidable_eq α] [fintype α] [∀ a, fintype (β a)] : fintype.pi_finset (λ a : α, (finset.univ : finset (β a))) = (finset.univ : finset (Π a, β a)) := rfl instance d_array.fintype {n : ℕ} {α : fin n → Type*} [∀ n, fintype (α n)] : fintype (d_array n α) := fintype.of_equiv _ (equiv.d_array_equiv_fin _).symm instance array.fintype {n : ℕ} {α : Type*} [fintype α] : fintype (array n α) := d_array.fintype instance vector.fintype {α : Type*} [fintype α] {n : ℕ} : fintype (vector α n) := fintype.of_equiv _ (equiv.vector_equiv_fin _ _).symm instance quotient.fintype [fintype α] (s : setoid α) [decidable_rel ((≈) : α → α → Prop)] : fintype (quotient s) := fintype.of_surjective quotient.mk (λ x, quotient.induction_on x (λ x, ⟨x, rfl⟩)) instance finset.fintype [fintype α] : fintype (finset α) := ⟨univ.powerset, λ x, finset.mem_powerset.2 (finset.subset_univ _)⟩ -- irreducible due to this conversation on Zulip: -- https://leanprover.zulipchat.com/#narrow/stream/113488-general/ -- topic/.60simp.60.20ignoring.20lemmas.3F/near/241824115 @[irreducible] instance function.embedding.fintype {α β} [fintype α] [fintype β] [decidable_eq α] [decidable_eq β] : fintype (α ↪ β) := fintype.of_equiv _ (equiv.subtype_injective_equiv_embedding α β) instance [decidable_eq α] [fintype α] {n : ℕ} : fintype (sym.sym' α n) := quotient.fintype _ instance [decidable_eq α] [fintype α] {n : ℕ} : fintype (sym α n) := fintype.of_equiv _ sym.sym_equiv_sym'.symm @[simp] lemma fintype.card_finset [fintype α] : fintype.card (finset α) = 2 ^ (fintype.card α) := finset.card_powerset finset.univ @[simp] lemma finset.univ_filter_card_eq (α : Type*) [fintype α] (k : ℕ) : (finset.univ : finset (finset α)).filter (λ s, s.card = k) = finset.univ.powerset_len k := by { ext, simp [finset.mem_powerset_len] } @[simp] lemma fintype.card_finset_len [fintype α] (k : ℕ) : fintype.card {s : finset α // s.card = k} = nat.choose (fintype.card α) k := by simp [fintype.subtype_card, finset.card_univ] @[simp] lemma set.to_finset_univ [fintype α] : (set.univ : set α).to_finset = finset.univ := by { ext, simp only [set.mem_univ, mem_univ, set.mem_to_finset] } @[simp] lemma set.to_finset_eq_empty_iff {s : set α} [fintype s] : s.to_finset = ∅ ↔ s = ∅ := by simp [ext_iff, set.ext_iff] @[simp] lemma set.to_finset_empty : (∅ : set α).to_finset = ∅ := set.to_finset_eq_empty_iff.mpr rfl @[simp] lemma set.to_finset_range [decidable_eq α] [fintype β] (f : β → α) [fintype (set.range f)] : (set.range f).to_finset = finset.univ.image f := by simp [ext_iff] theorem fintype.card_subtype_le [fintype α] (p : α → Prop) [decidable_pred p] : fintype.card {x // p x} ≤ fintype.card α := fintype.card_le_of_embedding (function.embedding.subtype _) theorem fintype.card_subtype_lt [fintype α] {p : α → Prop} [decidable_pred p] {x : α} (hx : ¬ p x) : fintype.card {x // p x} < fintype.card α := fintype.card_lt_of_injective_of_not_mem coe subtype.coe_injective $ by rwa subtype.range_coe_subtype lemma fintype.card_subtype [fintype α] (p : α → Prop) [decidable_pred p] : fintype.card {x // p x} = ((finset.univ : finset α).filter p).card := begin refine fintype.card_of_subtype _ _, simp end lemma fintype.card_subtype_or (p q : α → Prop) [fintype {x // p x}] [fintype {x // q x}] [fintype {x // p x ∨ q x}] : fintype.card {x // p x ∨ q x} ≤ fintype.card {x // p x} + fintype.card {x // q x} := begin classical, convert fintype.card_le_of_embedding (subtype_or_left_embedding p q), rw fintype.card_sum end lemma fintype.card_subtype_or_disjoint (p q : α → Prop) (h : disjoint p q) [fintype {x // p x}] [fintype {x // q x}] [fintype {x // p x ∨ q x}] : fintype.card {x // p x ∨ q x} = fintype.card {x // p x} + fintype.card {x // q x} := begin classical, convert fintype.card_congr (subtype_or_equiv p q h), simp end theorem fintype.card_quotient_le [fintype α] (s : setoid α) [decidable_rel ((≈) : α → α → Prop)] : fintype.card (quotient s) ≤ fintype.card α := fintype.card_le_of_surjective _ (surjective_quotient_mk _) theorem fintype.card_quotient_lt [fintype α] {s : setoid α} [decidable_rel ((≈) : α → α → Prop)] {x y : α} (h1 : x ≠ y) (h2 : x ≈ y) : fintype.card (quotient s) < fintype.card α := fintype.card_lt_of_surjective_not_injective _ (surjective_quotient_mk _) $ λ w, h1 (w $ quotient.eq.mpr h2) instance psigma.fintype {α : Type*} {β : α → Type*} [fintype α] [∀ a, fintype (β a)] : fintype (Σ' a, β a) := fintype.of_equiv _ (equiv.psigma_equiv_sigma _).symm instance psigma.fintype_prop_left {α : Prop} {β : α → Type*} [decidable α] [∀ a, fintype (β a)] : fintype (Σ' a, β a) := if h : α then fintype.of_equiv (β h) ⟨λ x, ⟨h, x⟩, psigma.snd, λ _, rfl, λ ⟨_, _⟩, rfl⟩ else ⟨∅, λ x, h x.1⟩ instance psigma.fintype_prop_right {α : Type*} {β : α → Prop} [∀ a, decidable (β a)] [fintype α] : fintype (Σ' a, β a) := fintype.of_equiv {a // β a} ⟨λ ⟨x, y⟩, ⟨x, y⟩, λ ⟨x, y⟩, ⟨x, y⟩, λ ⟨x, y⟩, rfl, λ ⟨x, y⟩, rfl⟩ instance psigma.fintype_prop_prop {α : Prop} {β : α → Prop} [decidable α] [∀ a, decidable (β a)] : fintype (Σ' a, β a) := if h : ∃ a, β a then ⟨{⟨h.fst, h.snd⟩}, λ ⟨_, _⟩, by simp⟩ else ⟨∅, λ ⟨x, y⟩, h ⟨x, y⟩⟩ instance set.fintype [fintype α] : fintype (set α) := ⟨(@finset.univ α _).powerset.map ⟨coe, coe_injective⟩, λ s, begin classical, refine mem_map.2 ⟨finset.univ.filter s, mem_powerset.2 (subset_univ _), _⟩, apply (coe_filter _ _).trans, rw [coe_univ, set.sep_univ], refl end⟩ @[simp] lemma fintype.card_set [fintype α] : fintype.card (set α) = 2 ^ fintype.card α := (finset.card_map _).trans (finset.card_powerset _) instance pfun_fintype (p : Prop) [decidable p] (α : p → Type*) [Π hp, fintype (α hp)] : fintype (Π hp : p, α hp) := if hp : p then fintype.of_equiv (α hp) ⟨λ a _, a, λ f, f hp, λ _, rfl, λ _, rfl⟩ else ⟨singleton (λ h, (hp h).elim), by simp [hp, function.funext_iff]⟩ @[simp] lemma finset.univ_pi_univ {α : Type*} {β : α → Type*} [decidable_eq α] [fintype α] [∀ a, fintype (β a)] : finset.univ.pi (λ a : α, (finset.univ : finset (β a))) = finset.univ := by { ext, simp } lemma mem_image_univ_iff_mem_range {α β : Type*} [fintype α] [decidable_eq β] {f : α → β} {b : β} : b ∈ univ.image f ↔ b ∈ set.range f := by simp /-- An auxiliary function for `quotient.fin_choice`. Given a collection of setoids indexed by a type `ι`, a (finite) list `l` of indices, and a function that for each `i ∈ l` gives a term of the corresponding quotient type, then there is a corresponding term in the quotient of the product of the setoids indexed by `l`. -/ def quotient.fin_choice_aux {ι : Type*} [decidable_eq ι] {α : ι → Type*} [S : ∀ i, setoid (α i)] : Π (l : list ι), (Π i ∈ l, quotient (S i)) → @quotient (Π i ∈ l, α i) (by apply_instance) | [] f := ⟦λ i, false.elim⟧ | (i :: l) f := begin refine quotient.lift_on₂ (f i (list.mem_cons_self _ _)) (quotient.fin_choice_aux l (λ j h, f j (list.mem_cons_of_mem _ h))) _ _, exact λ a l, ⟦λ j h, if e : j = i then by rw e; exact a else l _ (h.resolve_left e)⟧, refine λ a₁ l₁ a₂ l₂ h₁ h₂, quotient.sound (λ j h, _), by_cases e : j = i; simp [e], { subst j, exact h₁ }, { exact h₂ _ _ } end theorem quotient.fin_choice_aux_eq {ι : Type*} [decidable_eq ι] {α : ι → Type*} [S : ∀ i, setoid (α i)] : ∀ (l : list ι) (f : Π i ∈ l, α i), quotient.fin_choice_aux l (λ i h, ⟦f i h⟧) = ⟦f⟧ | [] f := quotient.sound (λ i h, h.elim) | (i :: l) f := begin simp [quotient.fin_choice_aux, quotient.fin_choice_aux_eq l], refine quotient.sound (λ j h, _), by_cases e : j = i; simp [e], subst j, refl end /-- Given a collection of setoids indexed by a fintype `ι` and a function that for each `i : ι` gives a term of the corresponding quotient type, then there is corresponding term in the quotient of the product of the setoids. -/ def quotient.fin_choice {ι : Type*} [decidable_eq ι] [fintype ι] {α : ι → Type*} [S : ∀ i, setoid (α i)] (f : Π i, quotient (S i)) : @quotient (Π i, α i) (by apply_instance) := quotient.lift_on (@quotient.rec_on _ _ (λ l : multiset ι, @quotient (Π i ∈ l, α i) (by apply_instance)) finset.univ.1 (λ l, quotient.fin_choice_aux l (λ i _, f i)) (λ a b h, begin have := λ a, quotient.fin_choice_aux_eq a (λ i h, quotient.out (f i)), simp [quotient.out_eq] at this, simp [this], let g := λ a:multiset ι, ⟦λ (i : ι) (h : i ∈ a), quotient.out (f i)⟧, refine eq_of_heq ((eq_rec_heq _ _).trans (_ : g a == g b)), congr' 1, exact quotient.sound h, end)) (λ f, ⟦λ i, f i (finset.mem_univ _)⟧) (λ a b h, quotient.sound $ λ i, h _ _) theorem quotient.fin_choice_eq {ι : Type*} [decidable_eq ι] [fintype ι] {α : ι → Type*} [∀ i, setoid (α i)] (f : Π i, α i) : quotient.fin_choice (λ i, ⟦f i⟧) = ⟦f⟧ := begin let q, swap, change quotient.lift_on q _ _ = _, have : q = ⟦λ i h, f i⟧, { dsimp [q], exact quotient.induction_on (@finset.univ ι _).1 (λ l, quotient.fin_choice_aux_eq _ _) }, simp [this], exact setoid.refl _ end section equiv open list equiv equiv.perm variables [decidable_eq α] [decidable_eq β] /-- Given a list, produce a list of all permutations of its elements. -/ def perms_of_list : list α → list (perm α) | [] := [1] | (a :: l) := perms_of_list l ++ l.bind (λ b, (perms_of_list l).map (λ f, swap a b * f)) lemma length_perms_of_list : ∀ l : list α, length (perms_of_list l) = l.length! | [] := rfl | (a :: l) := begin rw [length_cons, nat.factorial_succ], simp [perms_of_list, length_bind, length_perms_of_list, function.comp, nat.succ_mul], cc end lemma mem_perms_of_list_of_mem {l : list α} {f : perm α} (h : ∀ x, f x ≠ x → x ∈ l) : f ∈ perms_of_list l := begin induction l with a l IH generalizing f h, { exact list.mem_singleton.2 (equiv.ext $ λ x, decidable.by_contradiction $ h _) }, by_cases hfa : f a = a, { refine mem_append_left _ (IH (λ x hx, mem_of_ne_of_mem _ (h x hx))), rintro rfl, exact hx hfa }, have hfa' : f (f a) ≠ f a := mt (λ h, f.injective h) hfa, have : ∀ (x : α), (swap a (f a) * f) x ≠ x → x ∈ l, { intros x hx, have hxa : x ≠ a, { rintro rfl, apply hx, simp only [mul_apply, swap_apply_right] }, refine list.mem_of_ne_of_mem hxa (h x (λ h, _)), simp only [h, mul_apply, swap_apply_def, mul_apply, ne.def, apply_eq_iff_eq] at hx; split_ifs at hx, exacts [hxa (h.symm.trans h_1), hx h] }, suffices : f ∈ perms_of_list l ∨ ∃ (b ∈ l) (g ∈ perms_of_list l), swap a b * g = f, { simpa only [perms_of_list, exists_prop, list.mem_map, mem_append, list.mem_bind] }, refine or_iff_not_imp_left.2 (λ hfl, ⟨f a, _, swap a (f a) * f, IH this, _⟩), { by_cases hffa : f (f a) = a, { exact mem_of_ne_of_mem hfa (h _ (mt (λ h, f.injective h) hfa)) }, { apply this, simp only [mul_apply, swap_apply_def, mul_apply, ne.def, apply_eq_iff_eq], split_ifs; cc } }, { rw [←mul_assoc, mul_def (swap a (f a)) (swap a (f a)), swap_swap, ←perm.one_def, one_mul] } end lemma mem_of_mem_perms_of_list : ∀ {l : list α} {f : perm α}, f ∈ perms_of_list l → ∀ {x}, f x ≠ x → x ∈ l | [] f h := have f = 1 := by simpa [perms_of_list] using h, by rw this; simp | (a :: l) f h := (mem_append.1 h).elim (λ h x hx, mem_cons_of_mem _ (mem_of_mem_perms_of_list h hx)) (λ h x hx, let ⟨y, hy, hy'⟩ := list.mem_bind.1 h in let ⟨g, hg₁, hg₂⟩ := list.mem_map.1 hy' in if hxa : x = a then by simp [hxa] else if hxy : x = y then mem_cons_of_mem _ $ by rwa hxy else mem_cons_of_mem _ $ mem_of_mem_perms_of_list hg₁ $ by rw [eq_inv_mul_iff_mul_eq.2 hg₂, mul_apply, swap_inv, swap_apply_def]; split_ifs; cc) lemma mem_perms_of_list_iff {l : list α} {f : perm α} : f ∈ perms_of_list l ↔ ∀ {x}, f x ≠ x → x ∈ l := ⟨mem_of_mem_perms_of_list, mem_perms_of_list_of_mem⟩ lemma nodup_perms_of_list : ∀ {l : list α} (hl : l.nodup), (perms_of_list l).nodup | [] hl := by simp [perms_of_list] | (a :: l) hl := have hl' : l.nodup, from nodup_of_nodup_cons hl, have hln' : (perms_of_list l).nodup, from nodup_perms_of_list hl', have hmeml : ∀ {f : perm α}, f ∈ perms_of_list l → f a = a, from λ f hf, not_not.1 (mt (mem_of_mem_perms_of_list hf) (nodup_cons.1 hl).1), by rw [perms_of_list, list.nodup_append, list.nodup_bind, pairwise_iff_nth_le]; exact ⟨hln', ⟨λ _ _, nodup_map (λ _ _, mul_left_cancel) hln', λ i j hj hij x hx₁ hx₂, let ⟨f, hf⟩ := list.mem_map.1 hx₁ in let ⟨g, hg⟩ := list.mem_map.1 hx₂ in have hix : x a = nth_le l i (lt_trans hij hj), by rw [←hf.2, mul_apply, hmeml hf.1, swap_apply_left], have hiy : x a = nth_le l j hj, by rw [← hg.2, mul_apply, hmeml hg.1, swap_apply_left], absurd (hf.2.trans (hg.2.symm)) $ λ h, ne_of_lt hij $ nodup_iff_nth_le_inj.1 hl' i j (lt_trans hij hj) hj $ by rw [← hix, hiy]⟩, λ f hf₁ hf₂, let ⟨x, hx, hx'⟩ := list.mem_bind.1 hf₂ in let ⟨g, hg⟩ := list.mem_map.1 hx' in have hgxa : g⁻¹ x = a, from f.injective $ by rw [hmeml hf₁, ← hg.2]; simp, have hxa : x ≠ a, from λ h, (list.nodup_cons.1 hl).1 (h ▸ hx), (list.nodup_cons.1 hl).1 $ hgxa ▸ mem_of_mem_perms_of_list hg.1 (by rwa [apply_inv_self, hgxa])⟩ /-- Given a finset, produce the finset of all permutations of its elements. -/ def perms_of_finset (s : finset α) : finset (perm α) := quotient.hrec_on s.1 (λ l hl, ⟨perms_of_list l, nodup_perms_of_list hl⟩) (λ a b hab, hfunext (congr_arg _ (quotient.sound hab)) (λ ha hb _, heq_of_eq $ finset.ext $ by simp [mem_perms_of_list_iff, hab.mem_iff])) s.2 lemma mem_perms_of_finset_iff : ∀ {s : finset α} {f : perm α}, f ∈ perms_of_finset s ↔ ∀ {x}, f x ≠ x → x ∈ s := by rintros ⟨⟨l⟩, hs⟩ f; exact mem_perms_of_list_iff lemma card_perms_of_finset : ∀ (s : finset α), (perms_of_finset s).card = s.card! := by rintros ⟨⟨l⟩, hs⟩; exact length_perms_of_list l /-- The collection of permutations of a fintype is a fintype. -/ def fintype_perm [fintype α] : fintype (perm α) := ⟨perms_of_finset (@finset.univ α _), by simp [mem_perms_of_finset_iff]⟩ instance [fintype α] [fintype β] : fintype (α ≃ β) := if h : fintype.card β = fintype.card α then trunc.rec_on_subsingleton (fintype.trunc_equiv_fin α) (λ eα, trunc.rec_on_subsingleton (fintype.trunc_equiv_fin β) (λ eβ, @fintype.of_equiv _ (perm α) fintype_perm (equiv_congr (equiv.refl α) (eα.trans (eq.rec_on h eβ.symm)) : (α ≃ α) ≃ (α ≃ β)))) else ⟨∅, λ x, false.elim (h (fintype.card_eq.2 ⟨x.symm⟩))⟩ lemma fintype.card_perm [fintype α] : fintype.card (perm α) = (fintype.card α)! := subsingleton.elim (@fintype_perm α _ _) (@equiv.fintype α α _ _ _ _) ▸ card_perms_of_finset _ lemma fintype.card_equiv [fintype α] [fintype β] (e : α ≃ β) : fintype.card (α ≃ β) = (fintype.card α)! := fintype.card_congr (equiv_congr (equiv.refl α) e) ▸ fintype.card_perm lemma univ_eq_singleton_of_card_one {α} [fintype α] (x : α) (h : fintype.card α = 1) : (univ : finset α) = {x} := begin symmetry, apply eq_of_subset_of_card_le (subset_univ ({x})), apply le_of_eq, simp [h, finset.card_univ] end end equiv namespace fintype section choose open fintype equiv variables [fintype α] (p : α → Prop) [decidable_pred p] /-- Given a fintype `α` and a predicate `p`, associate to a proof that there is a unique element of `α` satisfying `p` this unique element, as an element of the corresponding subtype. -/ def choose_x (hp : ∃! a : α, p a) : {a // p a} := ⟨finset.choose p univ (by simp; exact hp), finset.choose_property _ _ _⟩ /-- Given a fintype `α` and a predicate `p`, associate to a proof that there is a unique element of `α` satisfying `p` this unique element, as an element of `α`. -/ def choose (hp : ∃! a, p a) : α := choose_x p hp lemma choose_spec (hp : ∃! a, p a) : p (choose p hp) := (choose_x p hp).property @[simp] lemma choose_subtype_eq {α : Type*} (p : α → Prop) [fintype {a : α // p a}] [decidable_eq α] (x : {a : α // p a}) (h : ∃! (a : {a // p a}), (a : α) = x := ⟨x, rfl, λ y hy, by simpa [subtype.ext_iff] using hy⟩) : fintype.choose (λ (y : {a : α // p a}), (y : α) = x) h = x := by rw [subtype.ext_iff, fintype.choose_spec (λ (y : {a : α // p a}), (y : α) = x) _] end choose section bijection_inverse open function variables [fintype α] [decidable_eq β] {f : α → β} /-- `bij_inv f` is the unique inverse to a bijection `f`. This acts as a computable alternative to `function.inv_fun`. -/ def bij_inv (f_bij : bijective f) (b : β) : α := fintype.choose (λ a, f a = b) begin rcases f_bij.right b with ⟨a', fa_eq_b⟩, rw ← fa_eq_b, exact ⟨a', ⟨rfl, (λ a h, f_bij.left h)⟩⟩ end lemma left_inverse_bij_inv (f_bij : bijective f) : left_inverse (bij_inv f_bij) f := λ a, f_bij.left (choose_spec (λ a', f a' = f a) _) lemma right_inverse_bij_inv (f_bij : bijective f) : right_inverse (bij_inv f_bij) f := λ b, choose_spec (λ a', f a' = b) _ lemma bijective_bij_inv (f_bij : bijective f) : bijective (bij_inv f_bij) := ⟨(right_inverse_bij_inv _).injective, (left_inverse_bij_inv _).surjective⟩ end bijection_inverse lemma well_founded_of_trans_of_irrefl [fintype α] (r : α → α → Prop) [is_trans α r] [is_irrefl α r] : well_founded r := by classical; exact have ∀ x y, r x y → (univ.filter (λ z, r z x)).card < (univ.filter (λ z, r z y)).card, from λ x y hxy, finset.card_lt_card $ by simp only [finset.lt_iff_ssubset.symm, lt_iff_le_not_le, finset.le_iff_subset, finset.subset_iff, mem_filter, true_and, mem_univ, hxy]; exact ⟨λ z hzx, trans hzx hxy, not_forall_of_exists_not ⟨x, not_imp.2 ⟨hxy, irrefl x⟩⟩⟩, subrelation.wf this (measure_wf _) lemma preorder.well_founded [fintype α] [preorder α] : well_founded ((<) : α → α → Prop) := well_founded_of_trans_of_irrefl _ @[instance, priority 10] lemma linear_order.is_well_order [fintype α] [linear_order α] : is_well_order α (<) := { wf := preorder.well_founded } end fintype /-- A type is said to be infinite if it has no fintype instance. Note that `infinite α` is equivalent to `is_empty (fintype α)`. -/ class infinite (α : Type*) : Prop := (not_fintype : fintype α → false) lemma not_fintype (α : Type*) [h1 : infinite α] [h2 : fintype α] : false := infinite.not_fintype h2 protected lemma fintype.false {α : Type*} [infinite α] (h : fintype α) : false := not_fintype α protected lemma infinite.false {α : Type*} [fintype α] (h : infinite α) : false := not_fintype α @[simp] lemma is_empty_fintype {α : Type*} : is_empty (fintype α) ↔ infinite α := ⟨λ ⟨x⟩, ⟨x⟩, λ ⟨x⟩, ⟨x⟩⟩ /-- A non-infinite type is a fintype. -/ noncomputable def fintype_of_not_infinite {α : Type*} (h : ¬ infinite α) : fintype α := nonempty.some $ by rwa [← not_is_empty_iff, is_empty_fintype] section open_locale classical /-- Any type is (classically) either a `fintype`, or `infinite`. One can obtain the relevant typeclasses via `cases fintype_or_infinite α; resetI`. -/ noncomputable def fintype_or_infinite (α : Type*) : psum (fintype α) (infinite α) := if h : infinite α then psum.inr h else psum.inl (fintype_of_not_infinite h) end lemma finset.exists_minimal {α : Type*} [preorder α] (s : finset α) (h : s.nonempty) : ∃ m ∈ s, ∀ x ∈ s, ¬ (x < m) := begin obtain ⟨c, hcs : c ∈ s⟩ := h, have : well_founded (@has_lt.lt {x // x ∈ s} _) := fintype.well_founded_of_trans_of_irrefl _, obtain ⟨⟨m, hms : m ∈ s⟩, -, H⟩ := this.has_min set.univ ⟨⟨c, hcs⟩, trivial⟩, exact ⟨m, hms, λ x hx hxm, H ⟨x, hx⟩ trivial hxm⟩, end lemma finset.exists_maximal {α : Type*} [preorder α] (s : finset α) (h : s.nonempty) : ∃ m ∈ s, ∀ x ∈ s, ¬ (m < x) := @finset.exists_minimal (order_dual α) _ s h namespace infinite lemma exists_not_mem_finset [infinite α] (s : finset α) : ∃ x, x ∉ s := not_forall.1 $ λ h, fintype.false ⟨s, h⟩ @[priority 100] -- see Note [lower instance priority] instance (α : Type*) [H : infinite α] : nontrivial α := ⟨let ⟨x, hx⟩ := exists_not_mem_finset (∅ : finset α) in let ⟨y, hy⟩ := exists_not_mem_finset ({x} : finset α) in ⟨y, x, by simpa only [mem_singleton] using hy⟩⟩ lemma nonempty (α : Type*) [infinite α] : nonempty α := by apply_instance lemma of_injective [infinite β] (f : β → α) (hf : injective f) : infinite α := ⟨λ I, by exactI (fintype.of_injective f hf).false⟩ lemma of_surjective [infinite β] (f : α → β) (hf : surjective f) : infinite α := ⟨λ I, by { classical, exactI (fintype.of_surjective f hf).false }⟩ private noncomputable def nat_embedding_aux (α : Type*) [infinite α] : ℕ → α | n := by letI := classical.dec_eq α; exact classical.some (exists_not_mem_finset ((multiset.range n).pmap (λ m (hm : m < n), nat_embedding_aux m) (λ _, multiset.mem_range.1)).to_finset) private lemma nat_embedding_aux_injective (α : Type*) [infinite α] : function.injective (nat_embedding_aux α) := begin rintro m n h, letI := classical.dec_eq α, wlog hmlen : m ≤ n using m n, by_contradiction hmn, have hmn : m < n, from lt_of_le_of_ne hmlen hmn, refine (classical.some_spec (exists_not_mem_finset ((multiset.range n).pmap (λ m (hm : m < n), nat_embedding_aux α m) (λ _, multiset.mem_range.1)).to_finset)) _, refine multiset.mem_to_finset.2 (multiset.mem_pmap.2 ⟨m, multiset.mem_range.2 hmn, _⟩), rw [h, nat_embedding_aux] end /-- Embedding of `ℕ` into an infinite type. -/ noncomputable def nat_embedding (α : Type*) [infinite α] : ℕ ↪ α := ⟨_, nat_embedding_aux_injective α⟩ lemma exists_subset_card_eq (α : Type*) [infinite α] (n : ℕ) : ∃ s : finset α, s.card = n := ⟨(range n).map (nat_embedding α), by rw [card_map, card_range]⟩ end infinite /-- If every finset in a type has bounded cardinality, that type is finite. -/ noncomputable def fintype_of_finset_card_le {ι : Type*} (n : ℕ) (w : ∀ s : finset ι, s.card ≤ n) : fintype ι := begin apply fintype_of_not_infinite, introI i, obtain ⟨s, c⟩ := infinite.exists_subset_card_eq ι (n+1), specialize w s, rw c at w, exact nat.not_succ_le_self n w, end lemma not_injective_infinite_fintype [infinite α] [fintype β] (f : α → β) : ¬ injective f := λ hf, (fintype.of_injective f hf).false /-- The pigeonhole principle for infinitely many pigeons in finitely many pigeonholes. If there are infinitely many pigeons in finitely many pigeonholes, then there are at least two pigeons in the same pigeonhole. See also: `fintype.exists_ne_map_eq_of_card_lt`, `fintype.exists_infinite_fiber`. -/ lemma fintype.exists_ne_map_eq_of_infinite [infinite α] [fintype β] (f : α → β) : ∃ x y : α, x ≠ y ∧ f x = f y := begin classical, by_contra hf, push_neg at hf, apply not_injective_infinite_fintype f, intros x y, contrapose, apply hf, end -- irreducible due to this conversation on Zulip: -- https://leanprover.zulipchat.com/#narrow/stream/113488-general/ -- topic/.60simp.60.20ignoring.20lemmas.3F/near/241824115 @[irreducible] instance function.embedding.is_empty {α β} [infinite α] [fintype β] : is_empty (α ↪ β) := ⟨λ f, let ⟨x, y, ne, feq⟩ := fintype.exists_ne_map_eq_of_infinite f in ne $ f.injective feq⟩ @[priority 100] noncomputable instance function.embedding.fintype' {α β : Type*} [fintype β] : fintype (α ↪ β) := begin by_cases h : infinite α, { resetI, apply_instance }, { have := fintype_of_not_infinite h, classical, apply_instance } -- the `classical` generates `decidable_eq α/β` instances, and resets instance cache end /-- The strong pigeonhole principle for infinitely many pigeons in finitely many pigeonholes. If there are infinitely many pigeons in finitely many pigeonholes, then there is a pigeonhole with infinitely many pigeons. See also: `fintype.exists_ne_map_eq_of_infinite` -/ lemma fintype.exists_infinite_fiber [infinite α] [fintype β] (f : α → β) : ∃ y : β, infinite (f ⁻¹' {y}) := begin classical, by_contra hf, push_neg at hf, haveI := λ y, fintype_of_not_infinite $ hf y, let key : fintype α := { elems := univ.bUnion (λ (y : β), (f ⁻¹' {y}).to_finset), complete := by simp }, exact key.false, end lemma not_surjective_fintype_infinite [fintype α] [infinite β] (f : α → β) : ¬ surjective f := assume (hf : surjective f), have H : infinite α := infinite.of_surjective f hf, by exactI not_fintype α instance nat.infinite : infinite ℕ := ⟨λ ⟨s, hs⟩, finset.not_mem_range_self $ s.subset_range_sup_succ (hs _)⟩ instance int.infinite : infinite ℤ := infinite.of_injective int.of_nat (λ _ _, int.of_nat.inj) section trunc /-- For `s : multiset α`, we can lift the existential statement that `∃ x, x ∈ s` to a `trunc α`. -/ def trunc_of_multiset_exists_mem {α} (s : multiset α) : (∃ x, x ∈ s) → trunc α := quotient.rec_on_subsingleton s $ λ l h, match l, h with | [], _ := false.elim (by tauto) | (a :: _), _ := trunc.mk a end /-- A `nonempty` `fintype` constructively contains an element. -/ def trunc_of_nonempty_fintype (α) [nonempty α] [fintype α] : trunc α := trunc_of_multiset_exists_mem finset.univ.val (by simp) /-- A `fintype` with positive cardinality constructively contains an element. -/ def trunc_of_card_pos {α} [fintype α] (h : 0 < fintype.card α) : trunc α := by { letI := (fintype.card_pos_iff.mp h), exact trunc_of_nonempty_fintype α } /-- By iterating over the elements of a fintype, we can lift an existential statement `∃ a, P a` to `trunc (Σ' a, P a)`, containing data. -/ def trunc_sigma_of_exists {α} [fintype α] {P : α → Prop} [decidable_pred P] (h : ∃ a, P a) : trunc (Σ' a, P a) := @trunc_of_nonempty_fintype (Σ' a, P a) (exists.elim h $ λ a ha, ⟨⟨a, ha⟩⟩) _ end trunc namespace multiset variables [fintype α] [decidable_eq α] @[simp] lemma count_univ (a : α) : count a finset.univ.val = 1 := count_eq_one_of_mem finset.univ.nodup (finset.mem_univ _) end multiset namespace fintype /-- A recursor principle for finite types, analogous to `nat.rec`. It effectively says that every `fintype` is either `empty` or `option α`, up to an `equiv`. -/ def trunc_rec_empty_option {P : Type u → Sort v} (of_equiv : ∀ {α β}, α ≃ β → P α → P β) (h_empty : P pempty) (h_option : ∀ {α} [fintype α] [decidable_eq α], P α → P (option α)) (α : Type u) [fintype α] [decidable_eq α] : trunc (P α) := begin suffices : ∀ n : ℕ, trunc (P (ulift $ fin n)), { apply trunc.bind (this (fintype.card α)), intro h, apply trunc.map _ (fintype.trunc_equiv_fin α), intro e, exact of_equiv (equiv.ulift.trans e.symm) h }, intro n, induction n with n ih, { have : card pempty = card (ulift (fin 0)), { simp only [card_fin, card_pempty, card_ulift] }, apply trunc.bind (trunc_equiv_of_card_eq this), intro e, apply trunc.mk, refine of_equiv e h_empty, }, { have : card (option (ulift (fin n))) = card (ulift (fin n.succ)), { simp only [card_fin, card_option, card_ulift] }, apply trunc.bind (trunc_equiv_of_card_eq this), intro e, apply trunc.map _ ih, intro ih, refine of_equiv e (h_option ih), }, end /-- An induction principle for finite types, analogous to `nat.rec`. It effectively says that every `fintype` is either `empty` or `option α`, up to an `equiv`. -/ lemma induction_empty_option {P : Type u → Prop} (of_equiv : ∀ {α β}, α ≃ β → P α → P β) (h_empty : P pempty) (h_option : ∀ {α} [fintype α], P α → P (option α)) (α : Type u) [fintype α] : P α := begin haveI := classical.dec_eq α, obtain ⟨p⟩ := trunc_rec_empty_option @of_equiv h_empty (λ _ _ _, by exactI h_option) α, exact p, end end fintype /-- Auxiliary definition to show `exists_seq_of_forall_finset_exists`. -/ noncomputable def seq_of_forall_finset_exists_aux {α : Type*} [decidable_eq α] (P : α → Prop) (r : α → α → Prop) (h : ∀ (s : finset α), ∃ y, (∀ x ∈ s, P x) → (P y ∧ (∀ x ∈ s, r x y))) : ℕ → α | n := classical.some (h (finset.image (λ (i : fin n), seq_of_forall_finset_exists_aux i) (finset.univ : finset (fin n)))) using_well_founded {dec_tac := `[exact i.2]} /-- Induction principle to build a sequence, by adding one point at a time satisfying a given relation with respect to all the previously chosen points. More precisely, Assume that, for any finite set `s`, one can find another point satisfying some relation `r` with respect to all the points in `s`. Then one may construct a function `f : ℕ → α` such that `r (f m) (f n)` holds whenever `m < n`. We also ensure that all constructed points satisfy a given predicate `P`. -/ lemma exists_seq_of_forall_finset_exists {α : Type*} (P : α → Prop) (r : α → α → Prop) (h : ∀ (s : finset α), (∀ x ∈ s, P x) → ∃ y, P y ∧ (∀ x ∈ s, r x y)) : ∃ (f : ℕ → α), (∀ n, P (f n)) ∧ (∀ m n, m < n → r (f m) (f n)) := begin classical, haveI : nonempty α, { rcases h ∅ (by simp) with ⟨y, hy⟩, exact ⟨y⟩ }, choose! F hF using h, have h' : ∀ (s : finset α), ∃ y, (∀ x ∈ s, P x) → (P y ∧ (∀ x ∈ s, r x y)) := λ s, ⟨F s, hF s⟩, set f := seq_of_forall_finset_exists_aux P r h' with hf, have A : ∀ (n : ℕ), P (f n), { assume n, induction n using nat.strong_induction_on with n IH, have IH' : ∀ (x : fin n), P (f x) := λ n, IH n.1 n.2, rw [hf, seq_of_forall_finset_exists_aux], exact (classical.some_spec (h' (finset.image (λ (i : fin n), f i) (finset.univ : finset (fin n)))) (by simp [IH'])).1 }, refine ⟨f, A, λ m n hmn, _⟩, nth_rewrite 1 hf, rw seq_of_forall_finset_exists_aux, apply (classical.some_spec (h' (finset.image (λ (i : fin n), f i) (finset.univ : finset (fin n)))) (by simp [A])).2, exact finset.mem_image.2 ⟨⟨m, hmn⟩, finset.mem_univ _, rfl⟩, end /-- Induction principle to build a sequence, by adding one point at a time satisfying a given symmetric relation with respect to all the previously chosen points. More precisely, Assume that, for any finite set `s`, one can find another point satisfying some relation `r` with respect to all the points in `s`. Then one may construct a function `f : ℕ → α` such that `r (f m) (f n)` holds whenever `m ≠ n`. We also ensure that all constructed points satisfy a given predicate `P`. -/ lemma exists_seq_of_forall_finset_exists' {α : Type*} (P : α → Prop) (r : α → α → Prop) [is_symm α r] (h : ∀ (s : finset α), (∀ x ∈ s, P x) → ∃ y, P y ∧ (∀ x ∈ s, r x y)) : ∃ (f : ℕ → α), (∀ n, P (f n)) ∧ (∀ m n, m ≠ n → r (f m) (f n)) := begin rcases exists_seq_of_forall_finset_exists P r h with ⟨f, hf, hf'⟩, refine ⟨f, hf, λ m n hmn, _⟩, rcases lt_trichotomy m n with h|rfl|h, { exact hf' m n h }, { exact (hmn rfl).elim }, { apply symm, exact hf' n m h } end
33d091e318682aced46e3329baf296d45da49e19
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/category_theory/structured_arrow.lean
c709409c8d453d83e5b78149dbb4fc3fbb1c9fe5
[ "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
7,395
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Adam Topaz, Scott Morrison -/ import category_theory.comma import category_theory.punit import category_theory.limits.shapes.terminal /-! # The category of "structured arrows" For `T : C ⥤ D`, a `T`-structured arrow with source `S : D` is just a morphism `S ⟶ T.obj Y`, for some `Y : D`. These form a category with morphisms `g : Y ⟶ Y'` making the obvious diagram commute. We prove that `𝟙 (T.obj Y)` is the initial object in `T`-structured objects with source `T.obj Y`. -/ namespace category_theory universes v₁ v₂ u₁ u₂ -- morphism levels before object levels. See note [category_theory universes]. variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D] /-- The category of `T`-structured arrows with domain `S : D` (here `T : C ⥤ D`), has as its objects `D`-morphisms of the form `S ⟶ T Y`, for some `Y : C`, and morphisms `C`-morphisms `Y ⟶ Y'` making the obvious triangle commute. -/ @[derive category, nolint has_inhabited_instance] def structured_arrow (S : D) (T : C ⥤ D) := comma (functor.from_punit S) T namespace structured_arrow /-- The obvious projection functor from structured arrows. -/ @[simps] def proj (S : D) (T : C ⥤ D) : structured_arrow S T ⥤ C := comma.snd _ _ variables {S S' S'' : D} {Y Y' : C} {T : C ⥤ D} /-- Construct a structured arrow from a morphism. -/ def mk (f : S ⟶ T.obj Y) : structured_arrow S T := ⟨⟨⟩, Y, f⟩ @[simp] lemma mk_left (f : S ⟶ T.obj Y) : (mk f).left = punit.star := rfl @[simp] lemma mk_right (f : S ⟶ T.obj Y) : (mk f).right = Y := rfl @[simp] lemma mk_hom_eq_self (f : S ⟶ T.obj Y) : (mk f).hom = f := rfl @[simp, reassoc] lemma w {A B : structured_arrow S T} (f : A ⟶ B) : A.hom ≫ T.map f.right = B.hom := by { have := f.w; tidy } lemma eq_mk (f : structured_arrow S T) : f = mk f.hom := by { cases f, congr, ext, } /-- To construct a morphism of structured arrows, we need a morphism of the objects underlying the target, and to check that the triangle commutes. -/ @[simps] def hom_mk {f f' : structured_arrow S T} (g : f.right ⟶ f'.right) (w : f.hom ≫ T.map g = f'.hom) : f ⟶ f' := { left := eq_to_hom (by ext), right := g, w' := by { dsimp, simpa using w.symm, }, } /-- To construct an isomorphism of structured arrows, we need an isomorphism of the objects underlying the target, and to check that the triangle commutes. -/ @[simps] def iso_mk {f f' : structured_arrow S T} (g : f.right ≅ f'.right) (w : f.hom ≫ T.map g.hom = f'.hom) : f ≅ f' := comma.iso_mk (eq_to_iso (by ext)) g (by simpa using w.symm) /-- A morphism between source objects `S ⟶ S'` contravariantly induces a functor between structured arrows, `structured_arrow S' T ⥤ structured_arrow S T`. Ideally this would be described as a 2-functor from `D` (promoted to a 2-category with equations as 2-morphisms) to `Cat`. -/ @[simps] def map (f : S ⟶ S') : structured_arrow S' T ⥤ structured_arrow S T := comma.map_left _ ((functor.const _).map f) @[simp] lemma map_mk {f : S' ⟶ T.obj Y} (g : S ⟶ S') : (map g).obj (mk f) = mk (g ≫ f) := rfl @[simp] lemma map_id {f : structured_arrow S T} : (map (𝟙 S)).obj f = f := by { rw eq_mk f, simp, } @[simp] lemma map_comp {f : S ⟶ S'} {f' : S' ⟶ S''} {h : structured_arrow S'' T} : (map (f ≫ f')).obj h = (map f).obj ((map f').obj h) := by { rw eq_mk h, simp, } instance proj_reflects_iso : reflects_isomorphisms (proj S T) := { reflects := λ Y Z f t, by exactI ⟨⟨structured_arrow.hom_mk (inv ((proj S T).map f)) (by simp), by tidy⟩⟩ } open category_theory.limits /-- The identity structured arrow is initial. -/ def mk_id_initial [full T] [faithful T] : is_initial (mk (𝟙 (T.obj Y))) := { desc := λ c, hom_mk (T.preimage c.X.hom) (by { dsimp, simp, }), uniq' := λ c m _, begin ext, apply T.map_injective, simpa only [hom_mk_right, T.image_preimage, ←w m] using (category.id_comp _).symm, end } end structured_arrow /-- The category of `S`-costructured arrows with target `T : D` (here `S : C ⥤ D`), has as its objects `D`-morphisms of the form `S Y ⟶ T`, for some `Y : C`, and morphisms `C`-morphisms `Y ⟶ Y'` making the obvious triangle commute. -/ @[derive category, nolint has_inhabited_instance] def costructured_arrow (S : C ⥤ D) (T : D) := comma S (functor.from_punit T) namespace costructured_arrow /-- The obvious projection functor from costructured arrows. -/ @[simps] def proj (S : C ⥤ D) (T : D) : costructured_arrow S T ⥤ C := comma.fst _ _ variables {T T' T'' : D} {Y Y' : C} {S : C ⥤ D} /-- Construct a costructured arrow from a morphism. -/ def mk (f : S.obj Y ⟶ T) : costructured_arrow S T := ⟨Y, ⟨⟩, f⟩ @[simp] lemma mk_left (f : S.obj Y ⟶ T) : (mk f).left = Y := rfl @[simp] lemma mk_right (f : S.obj Y ⟶ T) : (mk f).right = punit.star := rfl @[simp] lemma mk_hom_eq_self (f : S.obj Y ⟶ T) : (mk f).hom = f := rfl @[simp, reassoc] lemma w {A B : costructured_arrow S T} (f : A ⟶ B) : S.map f.left ≫ B.hom = A.hom := by tidy lemma eq_mk (f : costructured_arrow S T) : f = mk f.hom := by { cases f, congr, ext, } /-- To construct a morphism of costructured arrows, we need a morphism of the objects underlying the source, and to check that the triangle commutes. -/ @[simps] def hom_mk {f f' : costructured_arrow S T} (g : f.left ⟶ f'.left) (w : S.map g ≫ f'.hom = f.hom) : f ⟶ f' := { left := g, right := eq_to_hom (by ext), w' := by simpa using w, } /-- To construct an isomorphism of costructured arrows, we need an isomorphism of the objects underlying the source, and to check that the triangle commutes. -/ @[simps] def iso_mk {f f' : costructured_arrow S T} (g : f.left ≅ f'.left) (w : S.map g.hom ≫ f'.hom = f.hom) : f ≅ f' := comma.iso_mk g (eq_to_iso (by ext)) (by simpa using w) /-- A morphism between target objects `T ⟶ T'` covariantly induces a functor between costructured arrows, `costructured_arrow S T ⥤ costructured_arrow S T'`. Ideally this would be described as a 2-functor from `D` (promoted to a 2-category with equations as 2-morphisms) to `Cat`. -/ @[simps] def map (f : T ⟶ T') : costructured_arrow S T ⥤ costructured_arrow S T' := comma.map_right _ ((functor.const _).map f) @[simp] lemma map_mk {f : S.obj Y ⟶ T} (g : T ⟶ T') : (map g).obj (mk f) = mk (f ≫ g) := rfl @[simp] lemma map_id {f : costructured_arrow S T} : (map (𝟙 T)).obj f = f := by { rw eq_mk f, simp, } @[simp] lemma map_comp {f : T ⟶ T'} {f' : T' ⟶ T''} {h : costructured_arrow S T} : (map (f ≫ f')).obj h = (map f').obj ((map f).obj h) := by { rw eq_mk h, simp, } instance proj_reflects_iso : reflects_isomorphisms (proj S T) := { reflects := λ Y Z f t, by exactI ⟨⟨costructured_arrow.hom_mk (inv ((proj S T).map f)) (by simp), by tidy⟩⟩ } open category_theory.limits /-- The identity costructured arrow is terminal. -/ def mk_id_terminal [full S] [faithful S] : is_terminal (mk (𝟙 (S.obj Y))) := { lift := λ c, hom_mk (S.preimage c.X.hom) (by { dsimp, simp, }), uniq' := begin rintros c m -, ext, apply S.map_injective, simpa only [hom_mk_left, S.image_preimage, ←w m] using (category.comp_id _).symm, end } end costructured_arrow end category_theory
305288e3d05616be3bed5cbdc4293b0a5b48c9ee
82e44445c70db0f03e30d7be725775f122d72f3e
/src/data/mv_polynomial/variables.lean
aa635e86fa2c312d822f311c80111a0895d81231
[ "Apache-2.0" ]
permissive
stjordanis/mathlib
51e286d19140e3788ef2c470bc7b953e4991f0c9
2568d41bca08f5d6bf39d915434c8447e21f42ee
refs/heads/master
1,631,748,053,501
1,627,938,886,000
1,627,938,886,000
228,728,358
0
0
Apache-2.0
1,576,630,588,000
1,576,630,587,000
null
UTF-8
Lean
false
false
23,126
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Johan Commelin, Mario Carneiro -/ import algebra.big_operators.order import data.mv_polynomial.monad import data.set.pairwise /-! # Degrees and variables of polynomials This file establishes many results about the degree and variable sets of a multivariate polynomial. The *variable set* of a polynomial $P \in R[X]$ is a `finset` containing each $x \in X$ that appears in a monomial in $P$. The *degree set* of a polynomial $P \in R[X]$ is a `multiset` containing, for each $x$ in the variable set, $n$ copies of $x$, where $n$ is the maximum number of copies of $x$ appearing in a monomial of $P$. ## Main declarations * `mv_polynomial.degrees p` : the multiset of variables representing the union of the multisets corresponding to each non-zero monomial in `p`. For example if `7 ≠ 0` in `R` and `p = x²y+7y³` then `degrees p = {x, x, y, y, y}` * `mv_polynomial.vars p` : the finset of variables occurring in `p`. For example if `p = x⁴y+yz` then `vars p = {x, y, z}` * `mv_polynomial.degree_of n p : ℕ` : the total degree of `p` with respect to the variable `n`. For example if `p = x⁴y+yz` then `degree_of y p = 1`. * `mv_polynomial.total_degree p : ℕ` : the max of the sizes of the multisets `s` whose monomials `X^s` occur in `p`. For example if `p = x⁴y+yz` then `total_degree p = 5`. ## Notation As in other polynomial files, we typically use the notation: + `σ τ : Type*` (indexing the variables) + `R : Type*` `[comm_semiring R]` (the coefficients) + `s : σ →₀ ℕ`, a function from `σ` to `ℕ` which is zero away from a finite set. This will give rise to a monomial in `mv_polynomial σ R` which mathematicians might call `X^s` + `r : R` + `i : σ`, with corresponding monomial `X i`, often denoted `X_i` by mathematicians + `p : mv_polynomial σ R` -/ noncomputable theory open_locale classical big_operators open set function finsupp add_monoid_algebra open_locale big_operators universes u v w variables {R : Type u} {S : Type v} namespace mv_polynomial variables {σ τ : Type*} {r : R} {e : ℕ} {n m : σ} {s : σ →₀ ℕ} section comm_semiring variables [comm_semiring R] {p q : mv_polynomial σ R} section degrees /-! ### `degrees` -/ /-- The maximal degrees of each variable in a multi-variable polynomial, expressed as a multiset. (For example, `degrees (x^2 * y + y^3)` would be `{x, x, y, y, y}`.) -/ def degrees (p : mv_polynomial σ R) : multiset σ := p.support.sup (λs:σ →₀ ℕ, s.to_multiset) lemma degrees_monomial (s : σ →₀ ℕ) (a : R) : degrees (monomial s a) ≤ s.to_multiset := finset.sup_le $ assume t h, begin have := finsupp.support_single_subset h, rw [finset.mem_singleton] at this, rw this end lemma degrees_monomial_eq (s : σ →₀ ℕ) (a : R) (ha : a ≠ 0) : degrees (monomial s a) = s.to_multiset := le_antisymm (degrees_monomial s a) $ finset.le_sup $ by rw [support_monomial, if_neg ha, finset.mem_singleton] lemma degrees_C (a : R) : degrees (C a : mv_polynomial σ R) = 0 := multiset.le_zero.1 $ degrees_monomial _ _ lemma degrees_X' (n : σ) : degrees (X n : mv_polynomial σ R) ≤ {n} := le_trans (degrees_monomial _ _) $ le_of_eq $ to_multiset_single _ _ @[simp] lemma degrees_X [nontrivial R] (n : σ) : degrees (X n : mv_polynomial σ R) = {n} := (degrees_monomial_eq _ (1 : R) one_ne_zero).trans (to_multiset_single _ _) @[simp] lemma degrees_zero : degrees (0 : mv_polynomial σ R) = 0 := by { rw ← C_0, exact degrees_C 0 } @[simp] lemma degrees_one : degrees (1 : mv_polynomial σ R) = 0 := degrees_C 1 lemma degrees_add (p q : mv_polynomial σ R) : (p + q).degrees ≤ p.degrees ⊔ q.degrees := begin refine finset.sup_le (assume b hb, _), have := finsupp.support_add hb, rw finset.mem_union at this, cases this, { exact le_sup_of_le_left (finset.le_sup this) }, { exact le_sup_of_le_right (finset.le_sup this) }, end lemma degrees_sum {ι : Type*} (s : finset ι) (f : ι → mv_polynomial σ R) : (∑ i in s, f i).degrees ≤ s.sup (λi, (f i).degrees) := begin refine s.induction _ _, { simp only [finset.sum_empty, finset.sup_empty, degrees_zero], exact le_refl _ }, { assume i s his ih, rw [finset.sup_insert, finset.sum_insert his], exact le_trans (degrees_add _ _) (sup_le_sup_left ih _) } end lemma degrees_mul (p q : mv_polynomial σ R) : (p * q).degrees ≤ p.degrees + q.degrees := begin refine finset.sup_le (assume b hb, _), have := support_mul p q hb, simp only [finset.mem_bUnion, finset.mem_singleton] at this, rcases this with ⟨a₁, h₁, a₂, h₂, rfl⟩, rw [finsupp.to_multiset_add], exact add_le_add (finset.le_sup h₁) (finset.le_sup h₂) end lemma degrees_prod {ι : Type*} (s : finset ι) (f : ι → mv_polynomial σ R) : (∏ i in s, f i).degrees ≤ ∑ i in s, (f i).degrees := begin refine s.induction _ _, { simp only [finset.prod_empty, finset.sum_empty, degrees_one] }, { assume i s his ih, rw [finset.prod_insert his, finset.sum_insert his], exact le_trans (degrees_mul _ _) (add_le_add_left ih _) } end lemma degrees_pow (p : mv_polynomial σ R) : ∀(n : ℕ), (p^n).degrees ≤ n • p.degrees | 0 := begin rw [pow_zero, degrees_one], exact multiset.zero_le _ end | (n + 1) := by { rw [pow_succ, add_smul, add_comm, one_smul], exact le_trans (degrees_mul _ _) (add_le_add_left (degrees_pow n) _) } lemma mem_degrees {p : mv_polynomial σ R} {i : σ} : i ∈ p.degrees ↔ ∃ d, p.coeff d ≠ 0 ∧ i ∈ d.support := by simp only [degrees, multiset.mem_sup, ← mem_support_iff, finsupp.mem_to_multiset, exists_prop] lemma le_degrees_add {p q : mv_polynomial σ R} (h : p.degrees.disjoint q.degrees) : p.degrees ≤ (p + q).degrees := begin apply finset.sup_le, intros d hd, rw multiset.disjoint_iff_ne at h, rw multiset.le_iff_count, intros i, rw [degrees, multiset.count_sup], simp only [finsupp.count_to_multiset], by_cases h0 : d = 0, { simp only [h0, zero_le, finsupp.zero_apply], }, { refine @finset.le_sup _ _ _ (p + q).support _ d _, rw [mem_support_iff, coeff_add], suffices : q.coeff d = 0, { rwa [this, add_zero, coeff, ← finsupp.mem_support_iff], }, rw [← finsupp.support_eq_empty, ← ne.def, ← finset.nonempty_iff_ne_empty] at h0, obtain ⟨j, hj⟩ := h0, contrapose! h, rw mem_support_iff at hd, refine ⟨j, _, j, _, rfl⟩, all_goals { rw mem_degrees, refine ⟨d, _, hj⟩, assumption } } end lemma degrees_add_of_disjoint {p q : mv_polynomial σ R} (h : multiset.disjoint p.degrees q.degrees) : (p + q).degrees = p.degrees ∪ q.degrees := begin apply le_antisymm, { apply degrees_add }, { apply multiset.union_le, { apply le_degrees_add h }, { rw add_comm, apply le_degrees_add h.symm } } end lemma degrees_map [comm_semiring S] (p : mv_polynomial σ R) (f : R →+* S) : (map f p).degrees ⊆ p.degrees := begin dsimp only [degrees], apply multiset.subset_of_le, apply finset.sup_mono, apply mv_polynomial.support_map_subset end lemma degrees_rename (f : σ → τ) (φ : mv_polynomial σ R) : (rename f φ).degrees ⊆ (φ.degrees.map f) := begin intros i, rw [mem_degrees, multiset.mem_map], rintro ⟨d, hd, hi⟩, obtain ⟨x, rfl, hx⟩ := coeff_rename_ne_zero _ _ _ hd, simp only [map_domain, finsupp.mem_support_iff] at hi, rw [sum_apply, finsupp.sum] at hi, contrapose! hi, rw [finset.sum_eq_zero], intros j hj, simp only [exists_prop, mem_degrees] at hi, specialize hi j ⟨x, hx, hj⟩, rw [single_apply, if_neg hi], end lemma degrees_map_of_injective [comm_semiring S] (p : mv_polynomial σ R) {f : R →+* S} (hf : injective f) : (map f p).degrees = p.degrees := by simp only [degrees, mv_polynomial.support_map_of_injective _ hf] end degrees section vars /-! ### `vars` -/ /-- `vars p` is the set of variables appearing in the polynomial `p` -/ def vars (p : mv_polynomial σ R) : finset σ := p.degrees.to_finset @[simp] lemma vars_0 : (0 : mv_polynomial σ R).vars = ∅ := by rw [vars, degrees_zero, multiset.to_finset_zero] @[simp] lemma vars_monomial (h : r ≠ 0) : (monomial s r).vars = s.support := by rw [vars, degrees_monomial_eq _ _ h, finsupp.to_finset_to_multiset] @[simp] lemma vars_C : (C r : mv_polynomial σ R).vars = ∅ := by rw [vars, degrees_C, multiset.to_finset_zero] @[simp] lemma vars_X [nontrivial R] : (X n : mv_polynomial σ R).vars = {n} := by rw [X, vars_monomial (@one_ne_zero R _ _), finsupp.support_single_ne_zero (one_ne_zero : 1 ≠ 0)] lemma mem_vars (i : σ) : i ∈ p.vars ↔ ∃ (d : σ →₀ ℕ) (H : d ∈ p.support), i ∈ d.support := by simp only [vars, multiset.mem_to_finset, mem_degrees, mem_support_iff, exists_prop] lemma mem_support_not_mem_vars_zero {f : mv_polynomial σ R} {x : σ →₀ ℕ} (H : x ∈ f.support) {v : σ} (h : v ∉ vars f) : x v = 0 := begin rw [vars, multiset.mem_to_finset] at h, rw ← finsupp.not_mem_support_iff, contrapose! h, unfold degrees, rw (show f.support = insert x f.support, from eq.symm $ finset.insert_eq_of_mem H), rw finset.sup_insert, simp only [multiset.mem_union, multiset.sup_eq_union], left, rwa [←to_finset_to_multiset, multiset.mem_to_finset] at h, end lemma vars_add_subset (p q : mv_polynomial σ R) : (p + q).vars ⊆ p.vars ∪ q.vars := begin intros x hx, simp only [vars, finset.mem_union, multiset.mem_to_finset] at hx ⊢, simpa using multiset.mem_of_le (degrees_add _ _) hx, end lemma vars_add_of_disjoint (h : disjoint p.vars q.vars) : (p + q).vars = p.vars ∪ q.vars := begin apply finset.subset.antisymm (vars_add_subset p q), intros x hx, simp only [vars, multiset.disjoint_to_finset] at h hx ⊢, rw [degrees_add_of_disjoint h, multiset.to_finset_union], exact hx end section mul lemma vars_mul (φ ψ : mv_polynomial σ R) : (φ * ψ).vars ⊆ φ.vars ∪ ψ.vars := begin intro i, simp only [mem_vars, finset.mem_union], rintro ⟨d, hd, hi⟩, rw [mem_support_iff, coeff_mul] at hd, contrapose! hd, cases hd, rw finset.sum_eq_zero, rintro ⟨d₁, d₂⟩ H, rw finsupp.mem_antidiagonal at H, subst H, obtain H|H : i ∈ d₁.support ∨ i ∈ d₂.support, { simpa only [finset.mem_union] using finsupp.support_add hi, }, { suffices : coeff d₁ φ = 0, by simp [this], rw [coeff, ← finsupp.not_mem_support_iff], intro, solve_by_elim, }, { suffices : coeff d₂ ψ = 0, by simp [this], rw [coeff, ← finsupp.not_mem_support_iff], intro, solve_by_elim, }, end @[simp] lemma vars_one : (1 : mv_polynomial σ R).vars = ∅ := vars_C lemma vars_pow (φ : mv_polynomial σ R) (n : ℕ) : (φ ^ n).vars ⊆ φ.vars := begin induction n with n ih, { simp }, { rw pow_succ, apply finset.subset.trans (vars_mul _ _), exact finset.union_subset (finset.subset.refl _) ih } end /-- The variables of the product of a family of polynomials are a subset of the union of the sets of variables of each polynomial. -/ lemma vars_prod {ι : Type*} {s : finset ι} (f : ι → mv_polynomial σ R) : (∏ i in s, f i).vars ⊆ s.bUnion (λ i, (f i).vars) := begin apply s.induction_on, { simp }, { intros a s hs hsub, simp only [hs, finset.bUnion_insert, finset.prod_insert, not_false_iff], apply finset.subset.trans (vars_mul _ _), exact finset.union_subset_union (finset.subset.refl _) hsub } end section integral_domain variables {A : Type*} [integral_domain A] lemma vars_C_mul (a : A) (ha : a ≠ 0) (φ : mv_polynomial σ A) : (C a * φ).vars = φ.vars := begin ext1 i, simp only [mem_vars, exists_prop, mem_support_iff], apply exists_congr, intro d, apply and_congr _ iff.rfl, rw [coeff_C_mul, mul_ne_zero_iff, eq_true_intro ha, true_and], end end integral_domain end mul section sum variables {ι : Type*} (t : finset ι) (φ : ι → mv_polynomial σ R) lemma vars_sum_subset : (∑ i in t, φ i).vars ⊆ finset.bUnion t (λ i, (φ i).vars) := begin apply t.induction_on, { simp }, { intros a s has hsum, rw [finset.bUnion_insert, finset.sum_insert has], refine finset.subset.trans (vars_add_subset _ _) (finset.union_subset_union (finset.subset.refl _) _), assumption } end lemma vars_sum_of_disjoint (h : pairwise $ disjoint on (λ i, (φ i).vars)) : (∑ i in t, φ i).vars = finset.bUnion t (λ i, (φ i).vars) := begin apply t.induction_on, { simp }, { intros a s has hsum, rw [finset.bUnion_insert, finset.sum_insert has, vars_add_of_disjoint, hsum], unfold pairwise on_fun at h, rw hsum, simp only [finset.disjoint_iff_ne] at h ⊢, intros v hv v2 hv2, rw finset.mem_bUnion at hv2, rcases hv2 with ⟨i, his, hi⟩, refine h a i _ _ hv _ hi, rintro rfl, contradiction } end end sum section map variables [comm_semiring S] (f : R →+* S) variable (p) lemma vars_map : (map f p).vars ⊆ p.vars := by simp [vars, degrees_map] variable {f} lemma vars_map_of_injective (hf : injective f) : (map f p).vars = p.vars := by simp [vars, degrees_map_of_injective _ hf] lemma vars_monomial_single (i : σ) {e : ℕ} {r : R} (he : e ≠ 0) (hr : r ≠ 0) : (monomial (finsupp.single i e) r).vars = {i} := by rw [vars_monomial hr, finsupp.support_single_ne_zero he] lemma vars_eq_support_bUnion_support : p.vars = p.support.bUnion finsupp.support := by { ext i, rw [mem_vars, finset.mem_bUnion] } end map end vars section degree_of /-! ### `degree_of` -/ /-- `degree_of n p` gives the highest power of X_n that appears in `p` -/ def degree_of (n : σ) (p : mv_polynomial σ R) : ℕ := p.degrees.count n end degree_of section total_degree /-! ### `total_degree` -/ /-- `total_degree p` gives the maximum |s| over the monomials X^s in `p` -/ def total_degree (p : mv_polynomial σ R) : ℕ := p.support.sup (λs, s.sum $ λn e, e) lemma total_degree_eq (p : mv_polynomial σ R) : p.total_degree = p.support.sup (λm, m.to_multiset.card) := begin rw [total_degree], congr, funext m, exact (finsupp.card_to_multiset _).symm end lemma total_degree_le_degrees_card (p : mv_polynomial σ R) : p.total_degree ≤ p.degrees.card := begin rw [total_degree_eq], exact finset.sup_le (assume s hs, multiset.card_le_of_le $ finset.le_sup hs) end @[simp] lemma total_degree_C (a : R) : (C a : mv_polynomial σ R).total_degree = 0 := nat.eq_zero_of_le_zero $ finset.sup_le $ assume n hn, have _ := finsupp.support_single_subset hn, begin rw [finset.mem_singleton] at this, subst this, exact le_refl _ end @[simp] lemma total_degree_zero : (0 : mv_polynomial σ R).total_degree = 0 := by rw [← C_0]; exact total_degree_C (0 : R) @[simp] lemma total_degree_one : (1 : mv_polynomial σ R).total_degree = 0 := total_degree_C (1 : R) @[simp] lemma total_degree_X {R} [comm_semiring R] [nontrivial R] (s : σ) : (X s : mv_polynomial σ R).total_degree = 1 := begin rw [total_degree, support_X], simp only [finset.sup, sum_single_index, finset.fold_singleton, sup_bot_eq], end lemma total_degree_add (a b : mv_polynomial σ R) : (a + b).total_degree ≤ max a.total_degree b.total_degree := finset.sup_le $ assume n hn, have _ := finsupp.support_add hn, begin rw finset.mem_union at this, cases this, { exact le_max_of_le_left (finset.le_sup this) }, { exact le_max_of_le_right (finset.le_sup this) } end lemma total_degree_mul (a b : mv_polynomial σ R) : (a * b).total_degree ≤ a.total_degree + b.total_degree := finset.sup_le $ assume n hn, have _ := add_monoid_algebra.support_mul a b hn, begin simp only [finset.mem_bUnion, finset.mem_singleton] at this, rcases this with ⟨a₁, h₁, a₂, h₂, rfl⟩, rw [finsupp.sum_add_index], { exact add_le_add (finset.le_sup h₁) (finset.le_sup h₂) }, { assume a, refl }, { assume a b₁ b₂, refl } end lemma total_degree_pow (a : mv_polynomial σ R) (n : ℕ) : (a ^ n).total_degree ≤ n * a.total_degree := begin induction n with n ih, { simp only [nat.nat_zero_eq_zero, zero_mul, pow_zero, total_degree_one] }, rw pow_succ, calc total_degree (a * a ^ n) ≤ a.total_degree + (a^n).total_degree : total_degree_mul _ _ ... ≤ a.total_degree + n * a.total_degree : add_le_add_left ih _ ... = (n+1) * a.total_degree : by rw [add_mul, one_mul, add_comm] end lemma total_degree_list_prod : ∀(s : list (mv_polynomial σ R)), s.prod.total_degree ≤ (s.map mv_polynomial.total_degree).sum | [] := by rw [@list.prod_nil (mv_polynomial σ R) _, total_degree_one]; refl | (p :: ps) := begin rw [@list.prod_cons (mv_polynomial σ R) _, list.map, list.sum_cons], exact le_trans (total_degree_mul _ _) (add_le_add_left (total_degree_list_prod ps) _) end lemma total_degree_multiset_prod (s : multiset (mv_polynomial σ R)) : s.prod.total_degree ≤ (s.map mv_polynomial.total_degree).sum := begin refine quotient.induction_on s (assume l, _), rw [multiset.quot_mk_to_coe, multiset.coe_prod, multiset.coe_map, multiset.coe_sum], exact total_degree_list_prod l end lemma total_degree_finset_prod {ι : Type*} (s : finset ι) (f : ι → mv_polynomial σ R) : (s.prod f).total_degree ≤ ∑ i in s, (f i).total_degree := begin refine le_trans (total_degree_multiset_prod _) _, rw [multiset.map_map], refl end lemma exists_degree_lt [fintype σ] (f : mv_polynomial σ R) (n : ℕ) (h : f.total_degree < n * fintype.card σ) {d : σ →₀ ℕ} (hd : d ∈ f.support) : ∃ i, d i < n := begin contrapose! h, calc n * fintype.card σ = ∑ s:σ, n : by rw [finset.sum_const, nat.nsmul_eq_mul, mul_comm, finset.card_univ] ... ≤ ∑ s, d s : finset.sum_le_sum (λ s _, h s) ... ≤ d.sum (λ i e, e) : by { rw [finsupp.sum_fintype], intros, refl } ... ≤ f.total_degree : finset.le_sup hd, end lemma coeff_eq_zero_of_total_degree_lt {f : mv_polynomial σ R} {d : σ →₀ ℕ} (h : f.total_degree < ∑ i in d.support, d i) : coeff d f = 0 := begin classical, rw [total_degree, finset.sup_lt_iff] at h, { specialize h d, rw mem_support_iff at h, refine not_not.mp (mt h _), exact lt_irrefl _, }, { exact lt_of_le_of_lt (nat.zero_le _) h, } end lemma total_degree_rename_le (f : σ → τ) (p : mv_polynomial σ R) : (rename f p).total_degree ≤ p.total_degree := finset.sup_le $ assume b, begin assume h, rw rename_eq at h, have h' := finsupp.map_domain_support h, rw finset.mem_image at h', rcases h' with ⟨s, hs, rfl⟩, rw finsupp.sum_map_domain_index, exact le_trans (le_refl _) (finset.le_sup hs), exact assume _, rfl, exact assume _ _ _, rfl end end total_degree section eval_vars /-! ### `vars` and `eval` -/ variables [comm_semiring S] lemma eval₂_hom_eq_constant_coeff_of_vars (f : R →+* S) {g : σ → S} {p : mv_polynomial σ R} (hp : ∀ i ∈ p.vars, g i = 0) : eval₂_hom f g p = f (constant_coeff p) := begin conv_lhs { rw p.as_sum }, simp only [ring_hom.map_sum, eval₂_hom_monomial], by_cases h0 : constant_coeff p = 0, work_on_goal 0 { rw [h0, f.map_zero, finset.sum_eq_zero], intros d hd }, work_on_goal 1 { rw [finset.sum_eq_single (0 : σ →₀ ℕ)], { rw [finsupp.prod_zero_index, mul_one], refl }, intros d hd hd0, }, repeat { obtain ⟨i, hi⟩ : d.support.nonempty, { rw [constant_coeff_eq, coeff, ← finsupp.not_mem_support_iff] at h0, rw [finset.nonempty_iff_ne_empty, ne.def, finsupp.support_eq_empty], rintro rfl, contradiction }, rw [finsupp.prod, finset.prod_eq_zero hi, mul_zero], rw [hp, zero_pow (nat.pos_of_ne_zero $ finsupp.mem_support_iff.mp hi)], rw [mem_vars], exact ⟨d, hd, hi⟩ }, { rw [constant_coeff_eq, coeff, ← ne.def, ← finsupp.mem_support_iff] at h0, intro, contradiction } end lemma aeval_eq_constant_coeff_of_vars [algebra R S] {g : σ → S} {p : mv_polynomial σ R} (hp : ∀ i ∈ p.vars, g i = 0) : aeval g p = algebra_map _ _ (constant_coeff p) := eval₂_hom_eq_constant_coeff_of_vars _ hp lemma eval₂_hom_congr' {f₁ f₂ : R →+* S} {g₁ g₂ : σ → S} {p₁ p₂ : mv_polynomial σ R} : f₁ = f₂ → (∀ i, i ∈ p₁.vars → i ∈ p₂.vars → g₁ i = g₂ i) → p₁ = p₂ → eval₂_hom f₁ g₁ p₁ = eval₂_hom f₂ g₂ p₂ := begin rintro rfl h rfl, rename [p₁ p, f₁ f], rw p.as_sum, simp only [ring_hom.map_sum, eval₂_hom_monomial], apply finset.sum_congr rfl, intros d hd, congr' 1, simp only [finsupp.prod], apply finset.prod_congr rfl, intros i hi, have : i ∈ p.vars, { rw mem_vars, exact ⟨d, hd, hi⟩ }, rw h i this this, end lemma vars_bind₁ (f : σ → mv_polynomial τ R) (φ : mv_polynomial σ R) : (bind₁ f φ).vars ⊆ φ.vars.bUnion (λ i, (f i).vars) := begin calc (bind₁ f φ).vars = (φ.support.sum (λ (x : σ →₀ ℕ), (bind₁ f) (monomial x (coeff x φ)))).vars : by { rw [← alg_hom.map_sum, ← φ.as_sum], } ... ≤ φ.support.bUnion (λ (i : σ →₀ ℕ), ((bind₁ f) (monomial i (coeff i φ))).vars) : vars_sum_subset _ _ ... = φ.support.bUnion (λ (d : σ →₀ ℕ), (C (coeff d φ) * ∏ i in d.support, f i ^ d i).vars) : by simp only [bind₁_monomial] ... ≤ φ.support.bUnion (λ (d : σ →₀ ℕ), d.support.bUnion (λ i, (f i).vars)) : _ -- proof below ... ≤ φ.vars.bUnion (λ (i : σ), (f i).vars) : _, -- proof below { apply finset.bUnion_mono, intros d hd, calc (C (coeff d φ) * ∏ (i : σ) in d.support, f i ^ d i).vars ≤ (C (coeff d φ)).vars ∪ (∏ (i : σ) in d.support, f i ^ d i).vars : vars_mul _ _ ... ≤ (∏ (i : σ) in d.support, f i ^ d i).vars : by simp only [finset.empty_union, vars_C, finset.le_iff_subset, finset.subset.refl] ... ≤ d.support.bUnion (λ (i : σ), (f i ^ d i).vars) : vars_prod _ ... ≤ d.support.bUnion (λ (i : σ), (f i).vars) : _, apply finset.bUnion_mono, intros i hi, apply vars_pow, }, { intro j, simp_rw finset.mem_bUnion, rintro ⟨d, hd, ⟨i, hi, hj⟩⟩, exact ⟨i, (mem_vars _).mpr ⟨d, hd, hi⟩, hj⟩ } end lemma mem_vars_bind₁ (f : σ → mv_polynomial τ R) (φ : mv_polynomial σ R) {j : τ} (h : j ∈ (bind₁ f φ).vars) : ∃ (i : σ), i ∈ φ.vars ∧ j ∈ (f i).vars := by simpa only [exists_prop, finset.mem_bUnion, mem_support_iff, ne.def] using vars_bind₁ f φ h lemma vars_rename (f : σ → τ) (φ : mv_polynomial σ R) : (rename f φ).vars ⊆ (φ.vars.image f) := begin intros i hi, simp only [vars, exists_prop, multiset.mem_to_finset, finset.mem_image] at hi ⊢, simpa only [multiset.mem_map] using degrees_rename _ _ hi end lemma mem_vars_rename (f : σ → τ) (φ : mv_polynomial σ R) {j : τ} (h : j ∈ (rename f φ).vars) : ∃ (i : σ), i ∈ φ.vars ∧ f i = j := by simpa only [exists_prop, finset.mem_image] using vars_rename f φ h end eval_vars end comm_semiring end mv_polynomial
c0fda59803e845946aa44d580492a8a24279a018
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/category_theory/adjunction/limits.lean
3e6e4438e2693fb0473ffdccec7371964afd0eaa
[ "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
13,154
lean
/- Copyright (c) 2019 Reid Barton. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton, Johan Commelin -/ import category_theory.adjunction.basic import category_theory.limits.creates /-! # Adjunctions and limits A left adjoint preserves colimits (`category_theory.adjunction.left_adjoint_preserves_colimits`), and a right adjoint preserves limits (`category_theory.adjunction.right_adjoint_preserves_limits`). Equivalences create and reflect (co)limits. (`category_theory.adjunction.is_equivalence_creates_limits`, `category_theory.adjunction.is_equivalence_creates_colimits`, `category_theory.adjunction.is_equivalence_reflects_limits`, `category_theory.adjunction.is_equivalence_reflects_colimits`,) In `category_theory.adjunction.cocones_iso` we show that when `F ⊣ G`, the functor associating to each `Y` the cocones over `K ⋙ F` with cone point `Y` is naturally isomorphic to the functor associating to each `Y` the cocones over `K` with cone point `G.obj Y`. -/ open opposite namespace category_theory.adjunction open category_theory open category_theory.functor open category_theory.limits universes v u v₁ v₂ v₀ u₁ u₂ section arbitrary_universe variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D] variables {F : C ⥤ D} {G : D ⥤ C} (adj : F ⊣ G) include adj section preservation_colimits variables {J : Type u} [category.{v} J] (K : J ⥤ C) /-- The right adjoint of `cocones.functoriality K F : cocone K ⥤ cocone (K ⋙ F)`. Auxiliary definition for `functoriality_is_left_adjoint`. -/ def functoriality_right_adjoint : cocone (K ⋙ F) ⥤ cocone K := (cocones.functoriality _ G) ⋙ (cocones.precompose (K.right_unitor.inv ≫ (whisker_left K adj.unit) ≫ (associator _ _ _).inv)) local attribute [reducible] functoriality_right_adjoint /-- The unit for the adjunction for `cocones.functoriality K F : cocone K ⥤ cocone (K ⋙ F)`. Auxiliary definition for `functoriality_is_left_adjoint`. -/ @[simps] def functoriality_unit : 𝟭 (cocone K) ⟶ cocones.functoriality _ F ⋙ functoriality_right_adjoint adj K := { app := λ c, { hom := adj.unit.app c.X } } /-- The counit for the adjunction for `cocones.functoriality K F : cocone K ⥤ cocone (K ⋙ F)`. Auxiliary definition for `functoriality_is_left_adjoint`. -/ @[simps] def functoriality_counit : functoriality_right_adjoint adj K ⋙ cocones.functoriality _ F ⟶ 𝟭 (cocone (K ⋙ F)) := { app := λ c, { hom := adj.counit.app c.X } } /-- The functor `cocones.functoriality K F : cocone K ⥤ cocone (K ⋙ F)` is a left adjoint. -/ def functoriality_is_left_adjoint : is_left_adjoint (cocones.functoriality K F) := { right := functoriality_right_adjoint adj K, adj := mk_of_unit_counit { unit := functoriality_unit adj K, counit := functoriality_counit adj K } } /-- A left adjoint preserves colimits. See https://stacks.math.columbia.edu/tag/0038. -/ def left_adjoint_preserves_colimits : preserves_colimits_of_size.{v u} F := { preserves_colimits_of_shape := λ J 𝒥, { preserves_colimit := λ F, by exactI { preserves := λ c hc, is_colimit.iso_unique_cocone_morphism.inv (λ s, @equiv.unique _ _ (is_colimit.iso_unique_cocone_morphism.hom hc _) (((adj.functoriality_is_left_adjoint _).adj).hom_equiv _ _)) } } }. omit adj @[priority 100] -- see Note [lower instance priority] instance is_equivalence_preserves_colimits (E : C ⥤ D) [is_equivalence E] : preserves_colimits_of_size.{v u} E := left_adjoint_preserves_colimits E.adjunction @[priority 100] -- see Note [lower instance priority] instance is_equivalence_reflects_colimits (E : D ⥤ C) [is_equivalence E] : reflects_colimits_of_size.{v u} E := { reflects_colimits_of_shape := λ J 𝒥, by exactI { reflects_colimit := λ K, { reflects := λ c t, begin have l := (is_colimit_of_preserves E.inv t).map_cocone_equiv E.as_equivalence.unit_iso.symm, refine (((is_colimit.precompose_inv_equiv K.right_unitor _).symm) l).of_iso_colimit _, tidy, end } } } @[priority 100] -- see Note [lower instance priority] instance is_equivalence_creates_colimits (H : D ⥤ C) [is_equivalence H] : creates_colimits_of_size.{v u} H := { creates_colimits_of_shape := λ J 𝒥, by exactI { creates_colimit := λ F, { lifts := λ c t, { lifted_cocone := H.map_cocone_inv c, valid_lift := H.map_cocone_map_cocone_inv c } } } } -- verify the preserve_colimits instance works as expected: example (E : C ⥤ D) [is_equivalence E] (c : cocone K) (h : is_colimit c) : is_colimit (E.map_cocone c) := preserves_colimit.preserves h lemma has_colimit_comp_equivalence (E : C ⥤ D) [is_equivalence E] [has_colimit K] : has_colimit (K ⋙ E) := has_colimit.mk { cocone := E.map_cocone (colimit.cocone K), is_colimit := preserves_colimit.preserves (colimit.is_colimit K) } lemma has_colimit_of_comp_equivalence (E : C ⥤ D) [is_equivalence E] [has_colimit (K ⋙ E)] : has_colimit K := @has_colimit_of_iso _ _ _ _ (K ⋙ E ⋙ inv E) K (@has_colimit_comp_equivalence _ _ _ _ _ _ (K ⋙ E) (inv E) _ _) ((functor.right_unitor _).symm ≪≫ iso_whisker_left K (E.as_equivalence.unit_iso)) /-- Transport a `has_colimits_of_shape` instance across an equivalence. -/ lemma has_colimits_of_shape_of_equivalence (E : C ⥤ D) [is_equivalence E] [has_colimits_of_shape J D] : has_colimits_of_shape J C := ⟨λ F, by exactI has_colimit_of_comp_equivalence F E⟩ /-- Transport a `has_colimits` instance across an equivalence. -/ lemma has_colimits_of_equivalence (E : C ⥤ D) [is_equivalence E] [has_colimits_of_size.{v u} D] : has_colimits_of_size.{v u} C := ⟨λ J hJ, by { exactI has_colimits_of_shape_of_equivalence E }⟩ end preservation_colimits section preservation_limits variables {J : Type u} [category.{v} J] (K : J ⥤ D) /-- The left adjoint of `cones.functoriality K G : cone K ⥤ cone (K ⋙ G)`. Auxiliary definition for `functoriality_is_right_adjoint`. -/ def functoriality_left_adjoint : cone (K ⋙ G) ⥤ cone K := (cones.functoriality _ F) ⋙ (cones.postcompose ((associator _ _ _).hom ≫ (whisker_left K adj.counit) ≫ K.right_unitor.hom)) local attribute [reducible] functoriality_left_adjoint /-- The unit for the adjunction for`cones.functoriality K G : cone K ⥤ cone (K ⋙ G)`. Auxiliary definition for `functoriality_is_right_adjoint`. -/ @[simps] def functoriality_unit' : 𝟭 (cone (K ⋙ G)) ⟶ functoriality_left_adjoint adj K ⋙ cones.functoriality _ G := { app := λ c, { hom := adj.unit.app c.X, } } /-- The counit for the adjunction for`cones.functoriality K G : cone K ⥤ cone (K ⋙ G)`. Auxiliary definition for `functoriality_is_right_adjoint`. -/ @[simps] def functoriality_counit' : cones.functoriality _ G ⋙ functoriality_left_adjoint adj K ⟶ 𝟭 (cone K) := { app := λ c, { hom := adj.counit.app c.X, } } /-- The functor `cones.functoriality K G : cone K ⥤ cone (K ⋙ G)` is a right adjoint. -/ def functoriality_is_right_adjoint : is_right_adjoint (cones.functoriality K G) := { left := functoriality_left_adjoint adj K, adj := mk_of_unit_counit { unit := functoriality_unit' adj K, counit := functoriality_counit' adj K } } /-- A right adjoint preserves limits. See https://stacks.math.columbia.edu/tag/0038. -/ def right_adjoint_preserves_limits : preserves_limits_of_size.{v u} G := { preserves_limits_of_shape := λ J 𝒥, { preserves_limit := λ K, by exactI { preserves := λ c hc, is_limit.iso_unique_cone_morphism.inv (λ s, @equiv.unique _ _ (is_limit.iso_unique_cone_morphism.hom hc _) (((adj.functoriality_is_right_adjoint _).adj).hom_equiv _ _).symm) } } }. omit adj @[priority 100] -- see Note [lower instance priority] instance is_equivalence_preserves_limits (E : D ⥤ C) [is_equivalence E] : preserves_limits_of_size.{v u} E := right_adjoint_preserves_limits E.inv.adjunction @[priority 100] -- see Note [lower instance priority] instance is_equivalence_reflects_limits (E : D ⥤ C) [is_equivalence E] : reflects_limits_of_size.{v u} E := { reflects_limits_of_shape := λ J 𝒥, by exactI { reflects_limit := λ K, { reflects := λ c t, begin have := (is_limit_of_preserves E.inv t).map_cone_equiv E.as_equivalence.unit_iso.symm, refine (((is_limit.postcompose_hom_equiv K.left_unitor _).symm) this).of_iso_limit _, tidy, end } } } @[priority 100] -- see Note [lower instance priority] instance is_equivalence_creates_limits (H : D ⥤ C) [is_equivalence H] : creates_limits_of_size.{v u} H := { creates_limits_of_shape := λ J 𝒥, by exactI { creates_limit := λ F, { lifts := λ c t, { lifted_cone := H.map_cone_inv c, valid_lift := H.map_cone_map_cone_inv c } } } } -- verify the preserve_limits instance works as expected: example (E : D ⥤ C) [is_equivalence E] (c : cone K) [h : is_limit c] : is_limit (E.map_cone c) := preserves_limit.preserves h lemma has_limit_comp_equivalence (E : D ⥤ C) [is_equivalence E] [has_limit K] : has_limit (K ⋙ E) := has_limit.mk { cone := E.map_cone (limit.cone K), is_limit := preserves_limit.preserves (limit.is_limit K) } lemma has_limit_of_comp_equivalence (E : D ⥤ C) [is_equivalence E] [has_limit (K ⋙ E)] : has_limit K := @has_limit_of_iso _ _ _ _ (K ⋙ E ⋙ inv E) K (@has_limit_comp_equivalence _ _ _ _ _ _ (K ⋙ E) (inv E) _ _) ((iso_whisker_left K E.as_equivalence.unit_iso.symm) ≪≫ (functor.right_unitor _)) /-- Transport a `has_limits_of_shape` instance across an equivalence. -/ lemma has_limits_of_shape_of_equivalence (E : D ⥤ C) [is_equivalence E] [has_limits_of_shape J C] : has_limits_of_shape J D := ⟨λ F, by exactI has_limit_of_comp_equivalence F E⟩ /-- Transport a `has_limits` instance across an equivalence. -/ lemma has_limits_of_equivalence (E : D ⥤ C) [is_equivalence E] [has_limits_of_size.{v u} C] : has_limits_of_size.{v u} D := ⟨λ J hJ, by exactI has_limits_of_shape_of_equivalence E⟩ end preservation_limits /-- auxiliary construction for `cocones_iso` -/ @[simps] def cocones_iso_component_hom {J : Type u} [category.{v} J] {K : J ⥤ C} (Y : D) (t : ((cocones J D).obj (op (K ⋙ F))).obj Y) : (G ⋙ (cocones J C).obj (op K)).obj Y := { app := λ j, (adj.hom_equiv (K.obj j) Y) (t.app j), naturality' := λ j j' f, by { erw [← adj.hom_equiv_naturality_left, t.naturality], dsimp, simp } } /-- auxiliary construction for `cocones_iso` -/ @[simps] def cocones_iso_component_inv {J : Type u} [category.{v} J] {K : J ⥤ C} (Y : D) (t : (G ⋙ (cocones J C).obj (op K)).obj Y) : ((cocones J D).obj (op (K ⋙ F))).obj Y := { app := λ j, (adj.hom_equiv (K.obj j) Y).symm (t.app j), naturality' := λ j j' f, begin erw [← adj.hom_equiv_naturality_left_symm, ← adj.hom_equiv_naturality_right_symm, t.naturality], dsimp, simp end } /-- auxiliary construction for `cones_iso` -/ @[simps] def cones_iso_component_hom {J : Type u} [category.{v} J] {K : J ⥤ D} (X : Cᵒᵖ) (t : (functor.op F ⋙ (cones J D).obj K).obj X) : ((cones J C).obj (K ⋙ G)).obj X := { app := λ j, (adj.hom_equiv (unop X) (K.obj j)) (t.app j), naturality' := λ j j' f, begin erw [← adj.hom_equiv_naturality_right, ← t.naturality, category.id_comp, category.id_comp], refl end } /-- auxiliary construction for `cones_iso` -/ @[simps] def cones_iso_component_inv {J : Type u} [category.{v} J] {K : J ⥤ D} (X : Cᵒᵖ) (t : ((cones J C).obj (K ⋙ G)).obj X) : (functor.op F ⋙ (cones J D).obj K).obj X := { app := λ j, (adj.hom_equiv (unop X) (K.obj j)).symm (t.app j), naturality' := λ j j' f, begin erw [← adj.hom_equiv_naturality_right_symm, ← t.naturality, category.id_comp, category.id_comp] end } end arbitrary_universe variables {C : Type u₁} [category.{v₀} C] {D : Type u₂} [category.{v₀} D] {F : C ⥤ D} {G : D ⥤ C} (adj : F ⊣ G) /-- When `F ⊣ G`, the functor associating to each `Y` the cocones over `K ⋙ F` with cone point `Y` is naturally isomorphic to the functor associating to each `Y` the cocones over `K` with cone point `G.obj Y`. -/ -- Note: this is natural in K, but we do not yet have the tools to formulate that. def cocones_iso {J : Type u} [category.{v} J] {K : J ⥤ C} : (cocones J D).obj (op (K ⋙ F)) ≅ G ⋙ (cocones J C).obj (op K) := nat_iso.of_components (λ Y, { hom := cocones_iso_component_hom adj Y, inv := cocones_iso_component_inv adj Y, }) (by tidy) -- Note: this is natural in K, but we do not yet have the tools to formulate that. /-- When `F ⊣ G`, the functor associating to each `X` the cones over `K` with cone point `F.op.obj X` is naturally isomorphic to the functor associating to each `X` the cones over `K ⋙ G` with cone point `X`. -/ def cones_iso {J : Type u} [category.{v} J] {K : J ⥤ D} : F.op ⋙ (cones J D).obj K ≅ (cones J C).obj (K ⋙ G) := nat_iso.of_components (λ X, { hom := cones_iso_component_hom adj X, inv := cones_iso_component_inv adj X, } ) (by tidy) end category_theory.adjunction
ad947357ce2bfcc2798cebbecc39c9660a6a2c47
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/tests/lean/run/121.lean
c85508e08b7dae394364896e2dbfbd56cafb509a
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
227
lean
abbrev DelabM := Id abbrev Delab := DelabM Nat example : DelabM Nat := pure 1 -- works example : Delab := pure 1 -- works new_frontend example : DelabM Nat := pure 1 -- works example : Delab := pure 1 -- works
899945c7f1b1ec06111c85814d983d85e87079e1
f3849be5d845a1cb97680f0bbbe03b85518312f0
/library/init/wf.lean
0e288cab6183c74735ed09ba6ab6be0ef1dd9af6
[ "Apache-2.0" ]
permissive
bjoeris/lean
0ed95125d762b17bfcb54dad1f9721f953f92eeb
4e496b78d5e73545fa4f9a807155113d8e6b0561
refs/heads/master
1,611,251,218,281
1,495,337,658,000
1,495,337,658,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,086
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ prelude import init.data.nat.basic init.data.prod universes u v inductive acc {α : Type u} (r : α → α → Prop) : α → Prop | intro : ∀ x, (∀ y, r y x → acc y) → acc x namespace acc variables {α : Type u} {r : α → α → Prop} def inv {x y : α} (h₁ : acc r x) (h₂ : r y x) : acc r y := acc.rec_on h₁ (λ x₁ ac₁ ih h₂, ac₁ y h₂) h₂ end acc inductive well_founded {α : Type u} (r : α → α → Prop) : Prop | intro : (∀ a, acc r a) → well_founded class has_well_founded (α : Type u) : Type u := (r : α → α → Prop) (wf : well_founded r) namespace well_founded def apply {α : Type u} {r : α → α → Prop} (wf : well_founded r) : ∀ a, acc r a := take a, well_founded.rec_on wf (λ p, p) a section parameters {α : Type u} {r : α → α → Prop} local infix `≺`:50 := r parameter hwf : well_founded r lemma recursion {C : α → Sort v} (a : α) (h : Π x, (Π y, y ≺ x → C y) → C x) : C a := acc.rec_on (apply hwf a) (λ x₁ ac₁ ih, h x₁ ih) lemma induction {C : α → Prop} (a : α) (h : ∀ x, (∀ y, y ≺ x → C y) → C x) : C a := recursion a h variable {C : α → Sort v} variable F : Π x, (Π y, y ≺ x → C y) → C x def fix_F (x : α) (a : acc r x) : C x := acc.rec_on a (λ x₁ ac₁ ih, F x₁ ih) lemma fix_F_eq (x : α) (acx : acc r x) : fix_F F x acx = F x (λ (y : α) (p : y ≺ x), fix_F F y (acc.inv acx p)) := acc.drec (λ x r ih, rfl) acx end variables {α : Type u} {C : α → Sort v} {r : α → α → Prop} -- Well-founded fixpoint def fix (hwf : well_founded r) (F : Π x, (Π y, r y x → C y) → C x) (x : α) : C x := fix_F F x (apply hwf x) -- Well-founded fixpoint satisfies fixpoint equation lemma fix_eq (hwf : well_founded r) (F : Π x, (Π y, r y x → C y) → C x) (x : α) : fix hwf F x = F x (λ y h, fix hwf F y) := fix_F_eq F x (apply hwf x) end well_founded open well_founded -- Empty relation is well-founded def empty_wf {α : Type u} : well_founded empty_relation := well_founded.intro (λ (a : α), acc.intro a (λ (b : α) (lt : false), false.rec _ lt)) -- Subrelation of a well-founded relation is well-founded namespace subrelation section parameters {α : Type u} {r Q : α → α → Prop} parameters (h₁ : subrelation Q r) parameters (h₂ : well_founded r) def accessible {a : α} (ac : acc r a) : acc Q a := acc.rec_on ac (λ x ax ih, acc.intro x (λ (y : α) (lt : Q y x), ih y (h₁ lt))) def wf : well_founded Q := ⟨λ a, accessible (apply h₂ a)⟩ end end subrelation -- The inverse image of a well-founded relation is well-founded namespace inv_image section parameters {α : Type u} {β : Type v} {r : β → β → Prop} parameters (f : α → β) parameters (h : well_founded r) private def acc_aux {b : β} (ac : acc r b) : ∀ (x : α), f x = b → acc (inv_image r f) x := acc.rec_on ac (λ x acx ih z e, acc.intro z (λ y lt, eq.rec_on e (λ acx ih, ih (f y) lt y rfl) acx ih)) def accessible {a : α} (ac : acc r (f a)) : acc (inv_image r f) a := acc_aux ac a rfl def wf : well_founded (inv_image r f) := ⟨λ a, accessible (apply h (f a))⟩ end end inv_image -- The transitive closure of a well-founded relation is well-founded namespace tc section parameters {α : Type u} {r : α → α → Prop} local notation `r⁺` := tc r def accessible {z : α} (ac : acc r z) : acc (tc r) z := acc.rec_on ac (λ x acx ih, acc.intro x (λ y rel, tc.rec_on rel (λ a b rab acx ih, ih a rab) (λ a b c rab rbc ih₁ ih₂ acx ih, acc.inv (ih₂ acx ih) rab) acx ih)) def wf (h : well_founded r) : well_founded r⁺ := ⟨λ a, accessible (apply h a)⟩ end end tc -- less-than is well-founded def nat.lt_wf : well_founded nat.lt := ⟨nat.rec (acc.intro 0 (λ n h, absurd h (nat.not_lt_zero n))) (λ n ih, acc.intro (nat.succ n) (λ m h, or.elim (nat.eq_or_lt_of_le (nat.le_of_succ_le_succ h)) (λ e, eq.substr e ih) (acc.inv ih)))⟩ def measure {α : Type u} : (α → ℕ) → α → α → Prop := inv_image (<) def measure_wf {α : Type u} (f : α → ℕ) : well_founded (measure f) := inv_image.wf f nat.lt_wf def sizeof_measure (α : Type u) [has_sizeof α] : α → α → Prop := measure sizeof def sizeof_measure_wf (α : Type u) [has_sizeof α] : well_founded (sizeof_measure α) := measure_wf sizeof instance has_well_founded_of_has_sizeof (α : Type u) [has_sizeof α] : has_well_founded α := {r := sizeof_measure α, wf := sizeof_measure_wf α} namespace prod open well_founded section variables {α : Type u} {β : Type v} variable (ra : α → α → Prop) variable (rb : β → β → Prop) -- Lexicographical order based on ra and rb inductive lex : α × β → α × β → Prop | left : ∀ {a₁} b₁ {a₂} b₂, ra a₁ a₂ → lex (a₁, b₁) (a₂, b₂) | right : ∀ a {b₁ b₂}, rb b₁ b₂ → lex (a, b₁) (a, b₂) -- relational product based on ra and rb inductive rprod : α × β → α × β → Prop | intro : ∀ {a₁ b₁ a₂ b₂}, ra a₁ a₂ → rb b₁ b₂ → rprod (a₁, b₁) (a₂, b₂) end section parameters {α : Type u} {β : Type v} parameters {ra : α → α → Prop} {rb : β → β → Prop} local infix `≺`:50 := lex ra rb def lex_accessible {a} (aca : acc ra a) (acb : ∀ b, acc rb b): ∀ b, acc (lex ra rb) (a, b) := acc.rec_on aca (λ xa aca iha b, acc.rec_on (acb b) (λ xb acb ihb, acc.intro (xa, xb) (λ p lt, have aux : xa = xa → xb = xb → acc (lex ra rb) p, from @prod.lex.rec_on α β ra rb (λ p₁ p₂, fst p₂ = xa → snd p₂ = xb → acc (lex ra rb) p₁) p (xa, xb) lt (λ a₁ b₁ a₂ b₂ h (eq₂ : a₂ = xa) (eq₃ : b₂ = xb), iha a₁ (eq.rec_on eq₂ h) b₁) (λ a b₁ b₂ h (eq₂ : a = xa) (eq₃ : b₂ = xb), eq.rec_on eq₂.symm (ihb b₁ (eq.rec_on eq₃ h))), aux rfl rfl))) -- The lexicographical order of well founded relations is well-founded def lex_wf (ha : well_founded ra) (hb : well_founded rb) : well_founded (lex ra rb) := ⟨λ p, cases_on p (λ a b, lex_accessible (apply ha a) (well_founded.apply hb) b)⟩ -- relational product is a subrelation of the lex def rprod_sub_lex : ∀ a b, rprod ra rb a b → lex ra rb a b := λ a b h, prod.rprod.rec_on h (λ a₁ b₁ a₂ b₂ h₁ h₂, lex.left rb b₁ b₂ h₁) -- The relational product of well founded relations is well-founded def rprod_wf (ha : well_founded ra) (hb : well_founded rb) : well_founded (rprod ra rb) := subrelation.wf (rprod_sub_lex) (lex_wf ha hb) end instance has_well_founded {α : Type u} {β : Type v} [s₁ : has_well_founded α] [s₂ : has_well_founded β] : has_well_founded (α × β) := {r := lex s₁.r s₂.r, wf := lex_wf s₁.wf s₂.wf} end prod
ba222b91036565818c35386b77f790c23d1465e9
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/nestedInductiveRecType.lean
af5ff378fe1a73e97b39c41ee51631f19d5e2f22
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
1,990
lean
namespace Ex1 inductive T := | int : Int -> T | tuple : List T -> T @[reducible] def T.eval (t : T) : Type := @T.rec (motive_1 := fun _ => Type) (motive_2 := fun _ => Type) (fun _ => Int) (fun _ ih => ih) Unit (fun _ _ ih₁ ih₂ => ih₁ × ih₂) t @[reducible] def T.evalList (t : List T) : Type := @T.rec_1 (motive_1 := fun _ => Type) (motive_2 := fun _ => Type) (fun _ => Int) (fun _ ih => ih) Unit (fun _ _ ih₁ ih₂ => ih₁ × ih₂) t mutual def T.default (τ : T): τ.eval := match τ with | T.int v => v | T.tuple τs => defaultList τs def T.defaultList (τs : List T) : T.evalList τs := match τs with | [] => () | τ::τs => (τ.default, defaultList τs) end #eval T.default (.int 5) -- 5 #eval T.default (.tuple [.int 5]) -- (5, ()) #eval T.default (.tuple [.int 5, .int 6]) -- (5, 6, ()) end Ex1 namespace Ex2 inductive T := | int : Int -> T | tuple : List T -> T @[reducible] def T.eval (t : T) : Type := @T.rec (motive_1 := fun _ => Type) (motive_2 := fun _ => Type) (fun _ => Int) (fun _ ih => ih) Unit (fun _ τs ih₁ ih₂ => match τs with | [] => ih₁ | _ => ih₁ × ih₂) t @[reducible] def T.evalList (t : List T) : Type := @T.rec_1 (motive_1 := fun _ => Type) (motive_2 := fun _ => Type) (fun _ => Int) (fun _ ih => ih) Unit (fun _ τs ih₁ ih₂ => match τs with | [] => ih₁ | _ => ih₁ × ih₂) t mutual def T.default (τ : T): τ.eval := match τ with | T.int v => v | T.tuple τs => defaultList τs def T.defaultList (τs : List T) : T.evalList τs := match τs with | [] => () | [τ] => τ.default | τ₁::τ₂::τs => (τ₁.default, defaultList (τ₂::τs)) end #eval T.default (.int 5) -- 5 #eval T.default (.tuple [.int 5]) -- 5 #eval T.default (.tuple [.int 5, .int 6]) -- (5, 6) end Ex2
1dab7068fd0a01ff7b7ae6e68a9e43ce7369222a
ff5230333a701471f46c57e8c115a073ebaaa448
/tests/lean/run/aexp.lean
ffd23647a5644c1ae37df7f321f997be77d6575f
[ "Apache-2.0" ]
permissive
stanford-cs242/lean
f81721d2b5d00bc175f2e58c57b710d465e6c858
7bd861261f4a37326dcf8d7a17f1f1f330e4548c
refs/heads/master
1,600,957,431,849
1,576,465,093,000
1,576,465,093,000
225,779,423
0
3
Apache-2.0
1,575,433,936,000
1,575,433,935,000
null
UTF-8
Lean
false
false
1,716
lean
namespace imp open tactic @[reducible] def uname := string inductive aexp | val : nat → aexp | var : uname → aexp | plus : aexp → aexp → aexp | times : aexp → aexp → aexp instance : decidable_eq aexp := by mk_dec_eq_instance @[reducible] def value := nat def state := uname → value open aexp def aval : aexp → state → value | (val n) s := n | (var x) s := s x | (plus a₁ a₂) s := aval a₁ s + aval a₂ s | (times a₁ a₂) s := aval a₁ s * aval a₂ s example : aval (plus (val 3) (var "x")) (λ x, 0) = 3 := rfl def updt (s : state) (x : uname) (v : value) : state := λ y, if x = y then v else s y def asimp_const : aexp → aexp | (val n) := val n | (var x) := var x | (plus a₁ a₂) := match asimp_const a₁, asimp_const a₂ with | val n₁, val n₂ := val (n₁ + n₂) | b₁, b₂ := plus b₁ b₂ end | (times a₁ a₂) := match asimp_const a₁, asimp_const a₂ with | val n₁, val n₂ := val (n₁ * n₂) | b₁, b₂ := times b₁ b₂ end example : asimp_const (plus (plus (val 2) (val 3)) (var "x")) = plus (val 5) (var "x") := rfl attribute [ematch] asimp_const aval -- set_option trace.smt.ematch true meta def not_done : tactic unit := fail_if_success done lemma aval_asimp_const (a : aexp) (s : state) : aval (asimp_const a) s = aval a s := begin [smt] induction a, all_goals {destruct (asimp_const a_a_1), all_goals {destruct (asimp_const a_a), eblast}} end lemma ex2 (a : aexp) (s : state) : aval (asimp_const a) s = aval a s := begin [smt] induction a, all_goals {destruct (asimp_const a_a_1), all_goals {destruct (asimp_const a_a), eblast_using [asimp_const, aval]}} end end imp
aa129cc1b7c372306012eb3697f1ead8272269be
1446f520c1db37e157b631385707cc28a17a595e
/stage0/src/Init/Core.lean
4f15da8ad700007fd9e024dd218e4d34bcc1ad35
[ "Apache-2.0" ]
permissive
bdbabiak/lean4
cab06b8a2606d99a168dd279efdd404edb4e825a
3f4d0d78b2ce3ef541cb643bbe21496bd6b057ac
refs/heads/master
1,615,045,275,530
1,583,793,696,000
1,583,793,696,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
61,832
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura notation, basic datatypes and type classes -/ prelude notation `Prop` := Sort 0 notation f ` $ `:1 a:0 := f a /- Logical operations and relations -/ reserve prefix `¬`:40 reserve infixr ` ∧ `:35 reserve infixr ` /\ `:35 reserve infixr ` \/ `:30 reserve infixr ` ∨ `:30 reserve infix ` <-> `:20 reserve infix ` ↔ `:20 reserve infix ` = `:50 reserve infix ` == `:50 reserve infix ` != `:50 reserve infix ` ~= `:50 reserve infix ` ≅ `:50 reserve infix ` ≠ `:50 reserve infix ` ≈ `:50 reserve infixr ` ▸ `:75 /- types and Type constructors -/ reserve infixr ` × `:35 /- arithmetic operations -/ reserve infixl ` + `:65 reserve infixl ` - `:65 reserve infixl ` * `:70 reserve infixl ` / `:70 reserve infixl ` % `:70 reserve infixl ` %ₙ `:70 reserve prefix `-`:100 reserve infixr ` ^ `:80 reserve infixr ` ∘ `:90 reserve infix ` <= `:50 reserve infix ` ≤ `:50 reserve infix ` < `:50 reserve infix ` >= `:50 reserve infix ` ≥ `:50 reserve infix ` > `:50 /- boolean operations -/ reserve prefix `!`:40 reserve infixl ` && `:35 reserve infixl ` || `:30 /- other symbols -/ reserve infixl ` ++ `:65 reserve infixr ` :: `:67 /- Control -/ reserve infixr ` <|> `:2 reserve infixr ` >>= `:55 reserve infixr ` >=> `:55 reserve infixl ` <*> `:60 reserve infixl ` <* ` :60 reserve infixr ` *> ` :60 reserve infixr ` >> ` :60 reserve infixr ` <$> `:100 reserve infixr ` <$ ` :100 reserve infixr ` $> ` :100 reserve infixl ` <&> `:100 universes u v w /-- Auxiliary unsafe constant used by the Compiler when erasing proofs from code. -/ unsafe axiom lcProof {α : Prop} : α /-- Auxiliary unsafe constant used by the Compiler to mark unreachable code. -/ unsafe axiom lcUnreachable {α : Sort u} : α @[inline] def id {α : Sort u} (a : α) : α := a def inline {α : Sort u} (a : α) : α := a @[inline] def flip {α : Sort u} {β : Sort v} {φ : Sort w} (f : α → β → φ) : β → α → φ := fun b a => f a b /- The kernel definitional equality test (t =?= s) has special support for idDelta applications. It implements the following rules 1) (idDelta t) =?= t 2) t =?= (idDelta t) 3) (idDelta t) =?= s IF (unfoldOf t) =?= s 4) t =?= idDelta s IF t =?= (unfoldOf s) This is mechanism for controlling the delta reduction (aka unfolding) used in the kernel. We use idDelta applications to address performance problems when Type checking theorems generated by the equation Compiler. -/ @[inline] def idDelta {α : Sort u} (a : α) : α := a /-- Gadget for optional parameter support. -/ @[reducible] def optParam (α : Sort u) (default : α) : Sort u := α /-- Gadget for marking output parameters in type classes. -/ @[reducible] def outParam (α : Sort u) : Sort u := α /-- Auxiliary Declaration used to implement the notation (a : α) -/ @[reducible] def typedExpr (α : Sort u) (a : α) : α := a /- `idRhs` is an auxiliary Declaration used in the equation Compiler to address performance issues when proving equational theorems. The equation Compiler uses it as a marker. -/ @[macroInline, reducible] def idRhs (α : Sort u) (a : α) : α := a inductive PUnit : Sort u | unit : PUnit /-- An abbreviation for `PUnit.{0}`, its most common instantiation. This Type should be preferred over `PUnit` where possible to avoid unnecessary universe parameters. -/ abbrev Unit : Type := PUnit @[matchPattern] abbrev Unit.unit : Unit := PUnit.unit /- Remark: thunks have an efficient implementation in the runtime. -/ structure Thunk (α : Type u) : Type u := (fn : Unit → α) attribute [extern "lean_mk_thunk"] Thunk.mk @[noinline, extern "lean_thunk_pure"] protected def Thunk.pure {α : Type u} (a : α) : Thunk α := ⟨fun _ => a⟩ @[noinline, extern "lean_thunk_get_own"] protected def Thunk.get {α : Type u} (x : @& Thunk α) : α := x.fn () @[noinline, extern "lean_thunk_map"] protected def Thunk.map {α : Type u} {β : Type v} (f : α → β) (x : Thunk α) : Thunk β := ⟨fun _ => f x.get⟩ @[noinline, extern "lean_thunk_bind"] protected def Thunk.bind {α : Type u} {β : Type v} (x : Thunk α) (f : α → Thunk β) : Thunk β := ⟨fun _ => (f x.get).get⟩ /- Remark: tasks have an efficient implementation in the runtime. -/ structure Task (α : Type u) : Type u := (fn : Unit → α) attribute [extern "lean_mk_task"] Task.mk @[noinline, extern "lean_task_pure"] protected def Task.pure {α : Type u} (a : α) : Task α := ⟨fun _ => a⟩ @[noinline, extern "lean_task_get"] protected def Task.get {α : Type u} (x : @& Task α) : α := x.fn () @[noinline, extern "lean_task_map"] protected def Task.map {α : Type u} {β : Type v} (f : α → β) (x : Task α) : Task β := ⟨fun _ => f x.get⟩ @[noinline, extern "lean_task_bind"] protected def Task.bind {α : Type u} {β : Type v} (x : Task α) (f : α → Task β) : Task β := ⟨fun _ => (f x.get).get⟩ inductive True : Prop | intro : True inductive False : Prop inductive Empty : Type def Not (a : Prop) : Prop := a → False prefix `¬` := Not inductive Eq {α : Sort u} (a : α) : α → Prop | refl : Eq a @[elabAsEliminator, inline, reducible] def Eq.ndrec.{u1, u2} {α : Sort u2} {a : α} {C : α → Sort u1} (m : C a) {b : α} (h : Eq a b) : C b := @Eq.rec α a (fun α _ => C α) m b h @[elabAsEliminator, inline, reducible] def Eq.ndrecOn.{u1, u2} {α : Sort u2} {a : α} {C : α → Sort u1} {b : α} (h : Eq a b) (m : C a) : C b := @Eq.rec α a (fun α _ => C α) m b h /- Initialize the Quotient Module, which effectively adds the following definitions: constant Quot {α : Sort u} (r : α → α → Prop) : Sort u constant Quot.mk {α : Sort u} (r : α → α → Prop) (a : α) : Quot r constant Quot.lift {α : Sort u} {r : α → α → Prop} {β : Sort v} (f : α → β) : (∀ a b : α, r a b → Eq (f a) (f b)) → Quot r → β constant Quot.ind {α : Sort u} {r : α → α → Prop} {β : Quot r → Prop} : (∀ a : α, β (Quot.mk r a)) → ∀ q : Quot r, β q -/ init_quot inductive HEq {α : Sort u} (a : α) : ∀ {β : Sort u}, β → Prop | refl : HEq a structure Prod (α : Type u) (β : Type v) := (fst : α) (snd : β) attribute [unbox] Prod /-- Similar to `Prod`, but α and β can be propositions. We use this Type internally to automatically generate the brecOn recursor. -/ structure PProd (α : Sort u) (β : Sort v) := (fst : α) (snd : β) structure And (a b : Prop) : Prop := intro :: (left : a) (right : b) structure Iff (a b : Prop) : Prop := intro :: (mp : a → b) (mpr : b → a) /- Eq basic support -/ infix `=` := Eq @[matchPattern] def rfl {α : Sort u} {a : α} : a = a := Eq.refl a @[elabAsEliminator] theorem Eq.subst {α : Sort u} {P : α → Prop} {a b : α} (h₁ : a = b) (h₂ : P a) : P b := Eq.ndrec h₂ h₁ infixr `▸` := Eq.subst theorem Eq.trans {α : Sort u} {a b c : α} (h₁ : a = b) (h₂ : b = c) : a = c := h₂ ▸ h₁ theorem Eq.symm {α : Sort u} {a b : α} (h : a = b) : b = a := h ▸ rfl infix `~=` := HEq infix `≅` := HEq @[matchPattern] def HEq.rfl {α : Sort u} {a : α} : a ≅ a := HEq.refl a theorem eqOfHEq {α : Sort u} {a a' : α} (h : a ≅ a') : a = a' := have ∀ (α' : Sort u) (a' : α') (h₁ : @HEq α a α' a') (h₂ : α = α'), (Eq.recOn h₂ a : α') = a' := fun (α' : Sort u) (a' : α') (h₁ : @HEq α a α' a') => HEq.recOn h₁ (fun (h₂ : α = α) => rfl); show (Eq.ndrecOn (Eq.refl α) a : α) = a' from this α a' h (Eq.refl α) inductive Sum (α : Type u) (β : Type v) | inl {} (val : α) : Sum | inr {} (val : β) : Sum inductive PSum (α : Sort u) (β : Sort v) | inl {} (val : α) : PSum | inr {} (val : β) : PSum inductive Or (a b : Prop) : Prop | inl {} (h : a) : Or | inr {} (h : b) : Or def Or.introLeft {a : Prop} (b : Prop) (ha : a) : Or a b := Or.inl ha def Or.introRight (a : Prop) {b : Prop} (hb : b) : Or a b := Or.inr hb structure Sigma {α : Type u} (β : α → Type v) := mk :: (fst : α) (snd : β fst) attribute [unbox] Sigma structure PSigma {α : Sort u} (β : α → Sort v) := mk :: (fst : α) (snd : β fst) inductive Bool : Type | false : Bool | true : Bool /- Remark: Subtype must take a Sort instead of Type because of the axiom strongIndefiniteDescription. -/ structure Subtype {α : Sort u} (p : α → Prop) := (val : α) (property : p val) inductive Exists {α : Sort u} (p : α → Prop) : Prop | intro (w : α) (h : p w) : Exists class inductive Decidable (p : Prop) | isFalse (h : ¬p) : Decidable | isTrue (h : p) : Decidable abbrev DecidablePred {α : Sort u} (r : α → Prop) := ∀ (a : α), Decidable (r a) abbrev DecidableRel {α : Sort u} (r : α → α → Prop) := ∀ (a b : α), Decidable (r a b) abbrev DecidableEq (α : Sort u) := ∀ (a b : α), Decidable (a = b) def decEq {α : Sort u} [s : DecidableEq α] (a b : α) : Decidable (a = b) := s a b inductive Option (α : Type u) | none {} : Option | some (val : α) : Option attribute [unbox] Option export Option (none some) export Bool (false true) inductive List (T : Type u) | nil {} : List | cons (hd : T) (tl : List) : List infixr `::` := List.cons inductive Nat | zero : Nat | succ (n : Nat) : Nat /- Auxiliary axiom used to implement `sorry`. TODO: add this theorem on-demand. That is, we should only add it if after the first error. -/ axiom sorryAx (α : Sort u) (synthetic := true) : α /- Declare builtin and reserved notation -/ class HasZero (α : Type u) := (zero : α) class HasOne (α : Type u) := (one : α) class HasAdd (α : Type u) := (add : α → α → α) class HasMul (α : Type u) := (mul : α → α → α) class HasNeg (α : Type u) := (neg : α → α) class HasSub (α : Type u) := (sub : α → α → α) class HasDiv (α : Type u) := (div : α → α → α) class HasMod (α : Type u) := (mod : α → α → α) class HasModN (α : Type u) := (modn : α → Nat → α) class HasLessEq (α : Type u) := (LessEq : α → α → Prop) class HasLess (α : Type u) := (Less : α → α → Prop) class HasBeq (α : Type u) := (beq : α → α → Bool) class HasAppend (α : Type u) := (append : α → α → α) class HasOrelse (α : Type u) := (orelse : α → α → α) class HasAndthen (α : Type u) := (andthen : α → α → α) class HasEquiv (α : Sort u) := (Equiv : α → α → Prop) class HasEmptyc (α : Type u) := (emptyc : α) class HasPow (α : Type u) (β : Type v) := (pow : α → β → α) infix `+` := HasAdd.add infix `*` := HasMul.mul infix `-` := HasSub.sub infix `/` := HasDiv.div infix `%` := HasMod.mod infix `%ₙ` := HasModN.modn prefix `-` := HasNeg.neg infix `<=` := HasLessEq.LessEq infix `≤` := HasLessEq.LessEq infix `<` := HasLess.Less infix `==` := HasBeq.beq infix `++` := HasAppend.append notation `∅` := HasEmptyc.emptyc _ infix `≈` := HasEquiv.Equiv infixr `^` := HasPow.pow infixr `/\` := And infixr `∧` := And infixr `\/` := Or infixr `∨` := Or infix `<->` := Iff infix `↔` := Iff -- notation `exists` binders `, ` r:(scoped P, Exists P) := r -- notation `∃` binders `, ` r:(scoped P, Exists P) := r infixr `<|>` := HasOrelse.orelse infixr `>>` := HasAndthen.andthen @[reducible] def GreaterEq {α : Type u} [HasLessEq α] (a b : α) : Prop := HasLessEq.LessEq b a @[reducible] def Greater {α : Type u} [HasLess α] (a b : α) : Prop := HasLess.Less b a infix `>=` := GreaterEq infix `≥` := GreaterEq infix `>` := Greater @[inline] def bit0 {α : Type u} [s : HasAdd α] (a : α) : α := a + a @[inline] def bit1 {α : Type u} [s₁ : HasOne α] [s₂ : HasAdd α] (a : α) : α := (bit0 a) + 1 attribute [matchPattern] HasZero.zero HasOne.one bit0 bit1 HasAdd.add HasNeg.neg /- Nat basic instances -/ @[extern "lean_nat_add"] protected def Nat.add : (@& Nat) → (@& Nat) → Nat | a, Nat.zero => a | a, Nat.succ b => Nat.succ (Nat.add a b) /- We mark the following definitions as pattern to make sure they can be used in recursive equations, and reduced by the equation Compiler. -/ attribute [matchPattern] Nat.add Nat.add._main instance : HasZero Nat := ⟨Nat.zero⟩ instance : HasOne Nat := ⟨Nat.succ (Nat.zero)⟩ instance : HasAdd Nat := ⟨Nat.add⟩ /- Auxiliary constant used by equation compiler. -/ constant hugeFuel : Nat := 10000 def std.priority.default : Nat := 1000 def std.priority.max : Nat := 0xFFFFFFFF protected def Nat.prio := std.priority.default + 100 /- Global declarations of right binding strength If a Module reassigns these, it will be incompatible with other modules that adhere to these conventions. When hovering over a symbol, use "C-c C-k" to see how to input it. -/ def std.prec.max : Nat := 1024 -- the strength of application, identifiers, (, [, etc. def std.prec.arrow : Nat := 25 /- The next def is "max + 10". It can be used e.g. for postfix operations that should be stronger than application. -/ def std.prec.maxPlus : Nat := std.prec.max + 10 infixr `×` := Prod -- notation for n-ary tuples /- Some type that is not a scalar value in our runtime. TODO: mark opaque -/ structure NonScalar := (val : Nat) /- For numeric literals notation -/ class HasOfNat (α : Type u) := (ofNat : Nat → α) export HasOfNat (ofNat) instance : HasOfNat Nat := ⟨id⟩ /- sizeof -/ class HasSizeof (α : Sort u) := (sizeof : α → Nat) export HasSizeof (sizeof) /- Declare sizeof instances and theorems for types declared before HasSizeof. From now on, the inductive Compiler will automatically generate sizeof instances and theorems. -/ /- Every Type `α` has a default HasSizeof instance that just returns 0 for every element of `α` -/ protected def default.sizeof (α : Sort u) : α → Nat | a => 0 instance defaultHasSizeof (α : Sort u) : HasSizeof α := ⟨default.sizeof α⟩ protected def Nat.sizeof : Nat → Nat | n => n instance : HasSizeof Nat := ⟨Nat.sizeof⟩ protected def Prod.sizeof {α : Type u} {β : Type v} [HasSizeof α] [HasSizeof β] : (Prod α β) → Nat | ⟨a, b⟩ => 1 + sizeof a + sizeof b instance (α : Type u) (β : Type v) [HasSizeof α] [HasSizeof β] : HasSizeof (Prod α β) := ⟨Prod.sizeof⟩ protected def Sum.sizeof {α : Type u} {β : Type v} [HasSizeof α] [HasSizeof β] : (Sum α β) → Nat | Sum.inl a => 1 + sizeof a | Sum.inr b => 1 + sizeof b instance (α : Type u) (β : Type v) [HasSizeof α] [HasSizeof β] : HasSizeof (Sum α β) := ⟨Sum.sizeof⟩ protected def PSum.sizeof {α : Type u} {β : Type v} [HasSizeof α] [HasSizeof β] : (PSum α β) → Nat | PSum.inl a => 1 + sizeof a | PSum.inr b => 1 + sizeof b instance (α : Type u) (β : Type v) [HasSizeof α] [HasSizeof β] : HasSizeof (PSum α β) := ⟨PSum.sizeof⟩ protected def Sigma.sizeof {α : Type u} {β : α → Type v} [HasSizeof α] [∀ a, HasSizeof (β a)] : Sigma β → Nat | ⟨a, b⟩ => 1 + sizeof a + sizeof b instance (α : Type u) (β : α → Type v) [HasSizeof α] [∀ a, HasSizeof (β a)] : HasSizeof (Sigma β) := ⟨Sigma.sizeof⟩ protected def PSigma.sizeof {α : Type u} {β : α → Type v} [HasSizeof α] [∀ a, HasSizeof (β a)] : PSigma β → Nat | ⟨a, b⟩ => 1 + sizeof a + sizeof b instance (α : Type u) (β : α → Type v) [HasSizeof α] [∀ a, HasSizeof (β a)] : HasSizeof (PSigma β) := ⟨PSigma.sizeof⟩ protected def PUnit.sizeof : PUnit → Nat | u => 1 instance : HasSizeof PUnit := ⟨PUnit.sizeof⟩ protected def Bool.sizeof : Bool → Nat | b => 1 instance : HasSizeof Bool := ⟨Bool.sizeof⟩ protected def Option.sizeof {α : Type u} [HasSizeof α] : Option α → Nat | none => 1 | some a => 1 + sizeof a instance (α : Type u) [HasSizeof α] : HasSizeof (Option α) := ⟨Option.sizeof⟩ protected def List.sizeof {α : Type u} [HasSizeof α] : List α → Nat | List.nil => 1 | List.cons a l => 1 + sizeof a + List.sizeof l instance (α : Type u) [HasSizeof α] : HasSizeof (List α) := ⟨List.sizeof⟩ protected def Subtype.sizeof {α : Type u} [HasSizeof α] {p : α → Prop} : Subtype p → Nat | ⟨a, _⟩ => sizeof a instance {α : Type u} [HasSizeof α] (p : α → Prop) : HasSizeof (Subtype p) := ⟨Subtype.sizeof⟩ theorem natAddZero (n : Nat) : n + 0 = n := rfl theorem optParamEq (α : Sort u) (default : α) : optParam α default = α := rfl /-- Like `by applyInstance`, but not dependent on the tactic framework. -/ @[reducible] def inferInstance {α : Type u} [i : α] : α := i @[reducible, elabSimple] def inferInstanceAs (α : Type u) [i : α] : α := i /- Boolean operators -/ @[macroInline] def cond {a : Type u} : Bool → a → a → a | true, x, y => x | false, x, y => y @[inline] def condEq {β : Sort u} (b : Bool) (h₁ : b = true → β) (h₂ : b = false → β) : β := @Bool.casesOn (λ x => b = x → β) b h₂ h₁ rfl @[macroInline] def or : Bool → Bool → Bool | true, _ => true | false, b => b @[macroInline] def and : Bool → Bool → Bool | false, _ => false | true, b => b @[macroInline] def not : Bool → Bool | true => false | false => true @[macroInline] def xor : Bool → Bool → Bool | true, b => not b | false, b => b prefix `!` := not infix `||` := or infix `&&` := and @[extern c inline "#1 || #2"] def strictOr (b₁ b₂ : Bool) := b₁ || b₂ @[extern c inline "#1 && #2"] def strictAnd (b₁ b₂ : Bool) := b₁ && b₂ @[inline] def bne {α : Type u} [HasBeq α] (a b : α) : Bool := !(a == b) infix `!=` := bne /- Logical connectives an equality -/ def implies (a b : Prop) := a → b theorem implies.trans {p q r : Prop} (h₁ : implies p q) (h₂ : implies q r) : implies p r := fun hp => h₂ (h₁ hp) def trivial : True := ⟨⟩ @[macroInline] def False.elim {C : Sort u} (h : False) : C := False.rec (fun _ => C) h @[macroInline] def absurd {a : Prop} {b : Sort v} (h₁ : a) (h₂ : ¬a) : b := False.elim (h₂ h₁) theorem mt {a b : Prop} (h₁ : a → b) (h₂ : ¬b) : ¬a := fun ha => h₂ (h₁ ha) theorem notFalse : ¬False := id -- proof irrelevance is built in theorem proofIrrel {a : Prop} (h₁ h₂ : a) : h₁ = h₂ := rfl theorem id.def {α : Sort u} (a : α) : id a = a := rfl @[macroInline] def Eq.mp {α β : Sort u} (h₁ : α = β) (h₂ : α) : β := Eq.recOn h₁ h₂ @[macroInline] def Eq.mpr {α β : Sort u} : (α = β) → β → α := fun h₁ h₂ => Eq.recOn (Eq.symm h₁) h₂ @[elabAsEliminator] theorem Eq.substr {α : Sort u} {p : α → Prop} {a b : α} (h₁ : b = a) (h₂ : p a) : p b := Eq.subst (Eq.symm h₁) h₂ theorem congr {α : Sort u} {β : Sort v} {f₁ f₂ : α → β} {a₁ a₂ : α} (h₁ : f₁ = f₂) (h₂ : a₁ = a₂) : f₁ a₁ = f₂ a₂ := Eq.subst h₁ (Eq.subst h₂ rfl) theorem congrFun {α : Sort u} {β : α → Sort v} {f g : ∀ x, β x} (h : f = g) (a : α) : f a = g a := Eq.subst h (Eq.refl (f a)) theorem congrArg {α : Sort u} {β : Sort v} {a₁ a₂ : α} (f : α → β) (h : a₁ = a₂) : f a₁ = f a₂ := congr rfl h theorem transRelLeft {α : Sort u} {a b c : α} (r : α → α → Prop) (h₁ : r a b) (h₂ : b = c) : r a c := h₂ ▸ h₁ theorem transRelRight {α : Sort u} {a b c : α} (r : α → α → Prop) (h₁ : a = b) (h₂ : r b c) : r a c := h₁.symm ▸ h₂ theorem ofEqTrue {p : Prop} (h : p = True) : p := h.symm ▸ trivial theorem notOfEqFalse {p : Prop} (h : p = False) : ¬p := fun hp => h ▸ hp @[macroInline] def cast {α β : Sort u} (h : α = β) (a : α) : β := Eq.rec a h theorem castProofIrrel {α β : Sort u} (h₁ h₂ : α = β) (a : α) : cast h₁ a = cast h₂ a := rfl theorem castEq {α : Sort u} (h : α = α) (a : α) : cast h a = a := rfl @[reducible] def Ne {α : Sort u} (a b : α) := ¬(a = b) infix `≠` := Ne theorem Ne.def {α : Sort u} (a b : α) : a ≠ b = ¬ (a = b) := rfl section Ne variable {α : Sort u} variables {a b : α} {p : Prop} theorem Ne.intro (h : a = b → False) : a ≠ b := h theorem Ne.elim (h : a ≠ b) : a = b → False := h theorem Ne.irrefl (h : a ≠ a) : False := h rfl theorem Ne.symm (h : a ≠ b) : b ≠ a := fun h₁ => h (h₁.symm) theorem falseOfNe : a ≠ a → False := Ne.irrefl theorem neFalseOfSelf : p → p ≠ False := fun (hp : p) (h : p = False) => h ▸ hp theorem neTrueOfNot : ¬p → p ≠ True := fun (hnp : ¬p) (h : p = True) => (h ▸ hnp) trivial theorem trueNeFalse : ¬True = False := neFalseOfSelf trivial end Ne theorem eqFalseOfNeTrue : ∀ {b : Bool}, b ≠ true → b = false | true, h => False.elim (h rfl) | false, h => rfl theorem eqTrueOfNeFalse : ∀ {b : Bool}, b ≠ false → b = true | true, h => rfl | false, h => False.elim (h rfl) theorem neFalseOfEqTrue : ∀ {b : Bool}, b = true → b ≠ false | true, _ => fun h => Bool.noConfusion h | false, h => Bool.noConfusion h theorem neTrueOfEqFalse : ∀ {b : Bool}, b = false → b ≠ true | true, h => Bool.noConfusion h | false, _ => fun h => Bool.noConfusion h section variables {α β φ : Sort u} {a a' : α} {b b' : β} {c : φ} @[elabAsEliminator] theorem HEq.ndrec.{u1, u2} {α : Sort u2} {a : α} {C : ∀ {β : Sort u2}, β → Sort u1} (m : C a) {β : Sort u2} {b : β} (h : a ≅ b) : C b := @HEq.rec α a (fun β b _ => C b) m β b h @[elabAsEliminator] theorem HEq.ndrecOn.{u1, u2} {α : Sort u2} {a : α} {C : ∀ {β : Sort u2}, β → Sort u1} {β : Sort u2} {b : β} (h : a ≅ b) (m : C a) : C b := @HEq.rec α a (fun β b _ => C b) m β b h theorem HEq.elim {α : Sort u} {a : α} {p : α → Sort v} {b : α} (h₁ : a ≅ b) (h₂ : p a) : p b := Eq.recOn (eqOfHEq h₁) h₂ theorem HEq.subst {p : ∀ (T : Sort u), T → Prop} (h₁ : a ≅ b) (h₂ : p α a) : p β b := HEq.ndrecOn h₁ h₂ theorem HEq.symm (h : a ≅ b) : b ≅ a := HEq.ndrecOn h (HEq.refl a) theorem heqOfEq (h : a = a') : a ≅ a' := Eq.subst h (HEq.refl a) theorem HEq.trans (h₁ : a ≅ b) (h₂ : b ≅ c) : a ≅ c := HEq.subst h₂ h₁ theorem heqOfHEqOfEq (h₁ : a ≅ b) (h₂ : b = b') : a ≅ b' := HEq.trans h₁ (heqOfEq h₂) theorem heqOfEqOfHEq (h₁ : a = a') (h₂ : a' ≅ b) : a ≅ b := HEq.trans (heqOfEq h₁) h₂ def typeEqOfHEq (h : a ≅ b) : α = β := HEq.ndrecOn h (Eq.refl α) end theorem eqRecHEq {α : Sort u} {φ : α → Sort v} : ∀ {a a' : α} (h : a = a') (p : φ a), (Eq.recOn h p : φ a') ≅ p | a, _, rfl, p => HEq.refl p theorem ofHEqTrue {a : Prop} (h : a ≅ True) : a := ofEqTrue (eqOfHEq h) theorem castHEq : ∀ {α β : Sort u} (h : α = β) (a : α), cast h a ≅ a | α, _, rfl, a => HEq.refl a variables {a b c d : Prop} theorem And.elim (h₁ : a ∧ b) (h₂ : a → b → c) : c := And.rec h₂ h₁ theorem And.swap : a ∧ b → b ∧ a := fun ⟨ha, hb⟩ => ⟨hb, ha⟩ def And.symm := @And.swap theorem Or.elim (h₁ : a ∨ b) (h₂ : a → c) (h₃ : b → c) : c := Or.rec h₂ h₃ h₁ theorem Or.swap (h : a ∨ b) : b ∨ a := Or.elim h Or.inr Or.inl def Or.symm := @Or.swap /- xor -/ def Xor (a b : Prop) : Prop := (a ∧ ¬ b) ∨ (b ∧ ¬ a) theorem Iff.elim (h₁ : (a → b) → (b → a) → c) (h₂ : a ↔ b) : c := Iff.rec h₁ h₂ theorem Iff.left : (a ↔ b) → a → b := Iff.mp theorem Iff.right : (a ↔ b) → b → a := Iff.mpr theorem iffIffImpliesAndImplies (a b : Prop) : (a ↔ b) ↔ (a → b) ∧ (b → a) := Iff.intro (fun h => And.intro h.mp h.mpr) (fun h => Iff.intro h.left h.right) theorem Iff.refl (a : Prop) : a ↔ a := Iff.intro (fun h => h) (fun h => h) theorem Iff.rfl {a : Prop} : a ↔ a := Iff.refl a theorem Iff.trans (h₁ : a ↔ b) (h₂ : b ↔ c) : a ↔ c := Iff.intro (fun ha => Iff.mp h₂ (Iff.mp h₁ ha)) (fun hc => Iff.mpr h₁ (Iff.mpr h₂ hc)) theorem Iff.symm (h : a ↔ b) : b ↔ a := Iff.intro (Iff.right h) (Iff.left h) theorem Iff.comm : (a ↔ b) ↔ (b ↔ a) := Iff.intro Iff.symm Iff.symm theorem Eq.toIff {a b : Prop} (h : a = b) : a ↔ b := Eq.recOn h Iff.rfl theorem neqOfNotIff {a b : Prop} : ¬(a ↔ b) → a ≠ b := fun h₁ h₂ => have a ↔ b from Eq.subst h₂ (Iff.refl a); absurd this h₁ theorem notIffNotOfIff (h₁ : a ↔ b) : ¬a ↔ ¬b := Iff.intro (fun (hna : ¬ a) (hb : b) => hna (Iff.right h₁ hb)) (fun (hnb : ¬ b) (ha : a) => hnb (Iff.left h₁ ha)) theorem ofIffTrue (h : a ↔ True) : a := Iff.mp (Iff.symm h) trivial theorem notOfIffFalse : (a ↔ False) → ¬a := Iff.mp theorem iffTrueIntro (h : a) : a ↔ True := Iff.intro (fun hl => trivial) (fun hr => h) theorem iffFalseIntro (h : ¬a) : a ↔ False := Iff.intro h (False.rec (fun _ => a)) theorem notNotIntro (ha : a) : ¬¬a := fun hna => hna ha theorem notTrue : (¬ True) ↔ False := iffFalseIntro (notNotIntro trivial) /- or resolution rulses -/ theorem resolveLeft {a b : Prop} (h : a ∨ b) (na : ¬ a) : b := Or.elim h (fun ha => absurd ha na) id theorem negResolveLeft {a b : Prop} (h : ¬ a ∨ b) (ha : a) : b := Or.elim h (fun na => absurd ha na) id theorem resolveRight {a b : Prop} (h : a ∨ b) (nb : ¬ b) : a := Or.elim h id (fun hb => absurd hb nb) theorem negResolveRight {a b : Prop} (h : a ∨ ¬ b) (hb : b) : a := Or.elim h id (fun nb => absurd hb nb) /- Exists -/ theorem Exists.elim {α : Sort u} {p : α → Prop} {b : Prop} (h₁ : Exists (fun x => p x)) (h₂ : ∀ (a : α), p a → b) : b := Exists.rec h₂ h₁ /- Decidable -/ @[inlineIfReduce, nospecialize] def Decidable.decide (p : Prop) [h : Decidable p] : Bool := Decidable.casesOn h (fun h₁ => false) (fun h₂ => true) export Decidable (isTrue isFalse decide) instance beqOfEq {α : Type u} [DecidableEq α] : HasBeq α := ⟨fun a b => decide (a = b)⟩ theorem decideTrueEqTrue (h : Decidable True) : @decide True h = true := match h with | isTrue h => rfl | isFalse h => False.elim (Iff.mp notTrue h) theorem decideFalseEqFalse (h : Decidable False) : @decide False h = false := match h with | isFalse h => rfl | isTrue h => False.elim h theorem decideEqTrue : ∀ {p : Prop} [s : Decidable p], p → decide p = true | _, isTrue _, _ => rfl | _, isFalse h₁, h₂ => absurd h₂ h₁ theorem decideEqFalse : ∀ {p : Prop} [s : Decidable p], ¬p → decide p = false | _, isTrue h₁, h₂ => absurd h₁ h₂ | _, isFalse h, _ => rfl theorem ofDecideEqTrue {p : Prop} [s : Decidable p] : decide p = true → p := fun h => match s with | isTrue h₁ => h₁ | isFalse h₁ => absurd h (neTrueOfEqFalse (decideEqFalse h₁)) theorem ofDecideEqFalse {p : Prop} [s : Decidable p] : decide p = false → ¬p := fun h => match s with | isTrue h₁ => absurd h (neFalseOfEqTrue (decideEqTrue h₁)) | isFalse h₁ => h₁ /-- Similar to `decide`, but uses an explicit instance -/ @[inline] def toBoolUsing {p : Prop} (d : Decidable p) : Bool := @decide p d theorem toBoolUsingEqTrue {p : Prop} (d : Decidable p) (h : p) : toBoolUsing d = true := @decideEqTrue _ d h theorem ofBoolUsingEqTrue {p : Prop} {d : Decidable p} (h : toBoolUsing d = true) : p := @ofDecideEqTrue _ d h theorem ofBoolUsingEqFalse {p : Prop} {d : Decidable p} (h : toBoolUsing d = false) : ¬ p := @ofDecideEqFalse _ d h instance : Decidable True := isTrue trivial instance : Decidable False := isFalse notFalse -- We use "dependent" if-then-else to be able to communicate the if-then-else condition -- to the branches @[macroInline] def dite {α : Sort u} (c : Prop) [h : Decidable c] : (c → α) → (¬ c → α) → α := fun t e => Decidable.casesOn h e t /- if-then-else -/ @[macroInline] def ite {α : Sort u} (c : Prop) [h : Decidable c] (t e : α) : α := Decidable.casesOn h (fun hnc => e) (fun hc => t) namespace Decidable variables {p q : Prop} def recOnTrue [h : Decidable p] {h₁ : p → Sort u} {h₂ : ¬p → Sort u} (h₃ : p) (h₄ : h₁ h₃) : (Decidable.recOn h h₂ h₁ : Sort u) := Decidable.casesOn h (fun h => False.rec _ (h h₃)) (fun h => h₄) def recOnFalse [h : Decidable p] {h₁ : p → Sort u} {h₂ : ¬p → Sort u} (h₃ : ¬p) (h₄ : h₂ h₃) : (Decidable.recOn h h₂ h₁ : Sort u) := Decidable.casesOn h (fun h => h₄) (fun h => False.rec _ (h₃ h)) @[macroInline] def byCases {q : Sort u} [s : Decidable p] (h1 : p → q) (h2 : ¬p → q) : q := match s with | isTrue h => h1 h | isFalse h => h2 h theorem em (p : Prop) [Decidable p] : p ∨ ¬p := byCases Or.inl Or.inr theorem byContradiction [Decidable p] (h : ¬p → False) : p := byCases id (fun np => False.elim (h np)) theorem ofNotNot [Decidable p] : ¬ ¬ p → p := fun hnn => byContradiction (fun hn => absurd hn hnn) theorem notNotIff (p) [Decidable p] : (¬ ¬ p) ↔ p := Iff.intro ofNotNot notNotIntro theorem notAndIffOrNot (p q : Prop) [d₁ : Decidable p] [d₂ : Decidable q] : ¬ (p ∧ q) ↔ ¬ p ∨ ¬ q := Iff.intro (fun h => match d₁, d₂ with | isTrue h₁, isTrue h₂ => absurd (And.intro h₁ h₂) h | _, isFalse h₂ => Or.inr h₂ | isFalse h₁, _ => Or.inl h₁) (fun (h) ⟨hp, hq⟩ => Or.elim h (fun h => h hp) (fun h => h hq)) end Decidable section variables {p q : Prop} @[inline] def decidableOfDecidableOfIff (hp : Decidable p) (h : p ↔ q) : Decidable q := if hp : p then isTrue (Iff.mp h hp) else isFalse (Iff.mp (notIffNotOfIff h) hp) @[inline] def decidableOfDecidableOfEq (hp : Decidable p) (h : p = q) : Decidable q := decidableOfDecidableOfIff hp h.toIff end section variables {p q : Prop} @[macroInline] instance [Decidable p] [Decidable q] : Decidable (p ∧ q) := if hp : p then if hq : q then isTrue ⟨hp, hq⟩ else isFalse (fun h => hq (And.right h)) else isFalse (fun h => hp (And.left h)) @[macroInline] instance [Decidable p] [Decidable q] : Decidable (p ∨ q) := if hp : p then isTrue (Or.inl hp) else if hq : q then isTrue (Or.inr hq) else isFalse (fun h => Or.elim h hp hq) instance [Decidable p] : Decidable (¬p) := if hp : p then isFalse (absurd hp) else isTrue hp @[macroInline] instance implies.Decidable [Decidable p] [Decidable q] : Decidable (p → q) := if hp : p then if hq : q then isTrue (fun h => hq) else isFalse (fun h => absurd (h hp) hq) else isTrue (fun h => absurd h hp) instance [Decidable p] [Decidable q] : Decidable (p ↔ q) := if hp : p then if hq : q then isTrue ⟨fun _ => hq, fun _ => hp⟩ else isFalse $ fun h => hq (h.1 hp) else if hq : q then isFalse $ fun h => hp (h.2 hq) else isTrue $ ⟨fun h => absurd h hp, fun h => absurd h hq⟩ instance [Decidable p] [Decidable q] : Decidable (Xor p q) := if hp : p then if hq : q then isFalse (fun h => Or.elim h (fun ⟨_, h⟩ => h hq : ¬(p ∧ ¬ q)) (fun ⟨_, h⟩ => h hp : ¬(q ∧ ¬ p))) else isTrue $ Or.inl ⟨hp, hq⟩ else if hq : q then isTrue $ Or.inr ⟨hq, hp⟩ else isFalse (fun h => Or.elim h (fun ⟨h, _⟩ => hp h : ¬(p ∧ ¬ q)) (fun ⟨h, _⟩ => hq h : ¬(q ∧ ¬ p))) end @[inline] instance {α : Sort u} [DecidableEq α] (a b : α) : Decidable (a ≠ b) := match decEq a b with | isTrue h => isFalse $ fun h' => absurd h h' | isFalse h => isTrue h theorem Bool.falseNeTrue (h : false = true) : False := Bool.noConfusion h @[inline] instance : DecidableEq Bool := fun a b => match a, b with | false, false => isTrue rfl | false, true => isFalse Bool.falseNeTrue | true, false => isFalse (Ne.symm Bool.falseNeTrue) | true, true => isTrue rfl /- if-then-else expression theorems -/ theorem ifPos {c : Prop} [h : Decidable c] (hc : c) {α : Sort u} {t e : α} : (ite c t e) = t := match h with | (isTrue hc) => rfl | (isFalse hnc) => absurd hc hnc theorem ifNeg {c : Prop} [h : Decidable c] (hnc : ¬c) {α : Sort u} {t e : α} : (ite c t e) = e := match h with | (isTrue hc) => absurd hc hnc | (isFalse hnc) => rfl -- Remark: dite and ite are "defally equal" when we ignore the proofs. theorem difEqIf (c : Prop) [h : Decidable c] {α : Sort u} (t : α) (e : α) : dite c (fun h => t) (fun h => e) = ite c t e := match h with | (isTrue hc) => rfl | (isFalse hnc) => rfl instance {c t e : Prop} [dC : Decidable c] [dT : Decidable t] [dE : Decidable e] : Decidable (if c then t else e) := match dC with | (isTrue hc) => dT | (isFalse hc) => dE instance {c : Prop} {t : c → Prop} {e : ¬c → Prop} [dC : Decidable c] [dT : ∀ h, Decidable (t h)] [dE : ∀ h, Decidable (e h)] : Decidable (if h : c then t h else e h) := match dC with | (isTrue hc) => dT hc | (isFalse hc) => dE hc /-- Universe lifting operation -/ structure ULift.{r, s} (α : Type s) : Type (max s r) := up :: (down : α) namespace ULift /- Bijection between α and ULift.{v} α -/ theorem upDown {α : Type u} : ∀ (b : ULift.{v} α), up (down b) = b | up a => rfl theorem downUp {α : Type u} (a : α) : down (up.{v} a) = a := rfl end ULift /-- Universe lifting operation from Sort to Type -/ structure PLift (α : Sort u) : Type u := up :: (down : α) namespace PLift /- Bijection between α and PLift α -/ theorem upDown {α : Sort u} : ∀ (b : PLift α), up (down b) = b | up a => rfl theorem downUp {α : Sort u} (a : α) : down (up a) = a := rfl end PLift /- pointed types -/ structure PointedType := (type : Type u) (val : type) /- Inhabited -/ class Inhabited (α : Sort u) := (default : α) constant arbitrary (α : Sort u) [Inhabited α] : α := Inhabited.default α instance Prop.Inhabited : Inhabited Prop := ⟨True⟩ instance Fun.Inhabited (α : Sort u) {β : Sort v} [h : Inhabited β] : Inhabited (α → β) := Inhabited.casesOn h (fun b => ⟨fun a => b⟩) instance Forall.Inhabited (α : Sort u) {β : α → Sort v} [∀ x, Inhabited (β x)] : Inhabited (∀ x, β x) := ⟨fun a => arbitrary (β a)⟩ instance : Inhabited Bool := ⟨false⟩ instance : Inhabited True := ⟨trivial⟩ instance : Inhabited Nat := ⟨0⟩ instance : Inhabited NonScalar := ⟨⟨arbitrary _⟩⟩ instance : Inhabited PointedType := ⟨{type := PUnit, val := ⟨⟩}⟩ class inductive Nonempty (α : Sort u) : Prop | intro (val : α) : Nonempty protected def Nonempty.elim {α : Sort u} {p : Prop} (h₁ : Nonempty α) (h₂ : α → p) : p := Nonempty.rec h₂ h₁ instance nonemptyOfInhabited {α : Sort u} [Inhabited α] : Nonempty α := ⟨arbitrary α⟩ theorem nonemptyOfExists {α : Sort u} {p : α → Prop} : Exists (fun x => p x) → Nonempty α | ⟨w, h⟩ => ⟨w⟩ /- Subsingleton -/ class inductive Subsingleton (α : Sort u) : Prop | intro (h : ∀ (a b : α), a = b) : Subsingleton protected def Subsingleton.elim {α : Sort u} [h : Subsingleton α] : ∀ (a b : α), a = b := Subsingleton.casesOn h (fun p => p) protected def Subsingleton.helim {α β : Sort u} [h : Subsingleton α] (h : α = β) : ∀ (a : α) (b : β), a ≅ b := Eq.recOn h (fun a b => heqOfEq (Subsingleton.elim a b)) instance subsingletonProp (p : Prop) : Subsingleton p := ⟨fun a b => proofIrrel a b⟩ instance (p : Prop) : Subsingleton (Decidable p) := Subsingleton.intro $ fun d₁ => match d₁ with | (isTrue t₁) => fun d₂ => match d₂ with | (isTrue t₂) => Eq.recOn (proofIrrel t₁ t₂) rfl | (isFalse f₂) => absurd t₁ f₂ | (isFalse f₁) => fun d₂ => match d₂ with | (isTrue t₂) => absurd t₂ f₁ | (isFalse f₂) => Eq.recOn (proofIrrel f₁ f₂) rfl protected theorem recSubsingleton {p : Prop} [h : Decidable p] {h₁ : p → Sort u} {h₂ : ¬p → Sort u} [h₃ : ∀ (h : p), Subsingleton (h₁ h)] [h₄ : ∀ (h : ¬p), Subsingleton (h₂ h)] : Subsingleton (Decidable.casesOn h h₂ h₁) := match h with | (isTrue h) => h₃ h | (isFalse h) => h₄ h section relation variables {α : Sort u} {β : Sort v} (r : β → β → Prop) def Reflexive := ∀ x, r x x def Symmetric := ∀ {x y}, r x y → r y x def Transitive := ∀ {x y z}, r x y → r y z → r x z def Equivalence := Reflexive r ∧ Symmetric r ∧ Transitive r def Total := ∀ x y, r x y ∨ r y x def mkEquivalence (rfl : Reflexive r) (symm : Symmetric r) (trans : Transitive r) : Equivalence r := ⟨rfl, @symm, @trans⟩ def Irreflexive := ∀ x, ¬ r x x def AntiSymmetric := ∀ {x y}, r x y → r y x → x = y def emptyRelation (a₁ a₂ : α) : Prop := False def Subrelation (q r : β → β → Prop) := ∀ {x y}, q x y → r x y def InvImage (f : α → β) : α → α → Prop := fun a₁ a₂ => r (f a₁) (f a₂) theorem InvImage.Transitive (f : α → β) (h : Transitive r) : Transitive (InvImage r f) := fun (a₁ a₂ a₃ : α) (h₁ : InvImage r f a₁ a₂) (h₂ : InvImage r f a₂ a₃) => h h₁ h₂ theorem InvImage.Irreflexive (f : α → β) (h : Irreflexive r) : Irreflexive (InvImage r f) := fun (a : α) (h₁ : InvImage r f a a) => h (f a) h₁ inductive TC {α : Sort u} (r : α → α → Prop) : α → α → Prop | base : ∀ a b, r a b → TC a b | trans : ∀ a b c, TC a b → TC b c → TC a c @[elabAsEliminator] theorem TC.ndrec.{u1, u2} {α : Sort u} {r : α → α → Prop} {C : α → α → Prop} (m₁ : ∀ (a b : α), r a b → C a b) (m₂ : ∀ (a b c : α), TC r a b → TC r b c → C a b → C b c → C a c) {a b : α} (h : TC r a b) : C a b := @TC.rec α r (fun a b _ => C a b) m₁ m₂ a b h @[elabAsEliminator] theorem TC.ndrecOn.{u1, u2} {α : Sort u} {r : α → α → Prop} {C : α → α → Prop} {a b : α} (h : TC r a b) (m₁ : ∀ (a b : α), r a b → C a b) (m₂ : ∀ (a b c : α), TC r a b → TC r b c → C a b → C b c → C a c) : C a b := @TC.rec α r (fun a b _ => C a b) m₁ m₂ a b h end relation section Binary variables {α : Type u} {β : Type v} variable (f : α → α → α) def Commutative := ∀ a b, f a b = f b a def Associative := ∀ a b c, f (f a b) c = f a (f b c) def RightCommutative (h : β → α → β) := ∀ b a₁ a₂, h (h b a₁) a₂ = h (h b a₂) a₁ def LeftCommutative (h : α → β → β) := ∀ a₁ a₂ b, h a₁ (h a₂ b) = h a₂ (h a₁ b) theorem leftComm : Commutative f → Associative f → LeftCommutative f := fun hcomm hassoc a b c => ((Eq.symm (hassoc a b c)).trans (hcomm a b ▸ rfl : f (f a b) c = f (f b a) c)).trans (hassoc b a c) theorem rightComm : Commutative f → Associative f → RightCommutative f := fun hcomm hassoc a b c => ((hassoc a b c).trans (hcomm b c ▸ rfl : f a (f b c) = f a (f c b))).trans (Eq.symm (hassoc a c b)) end Binary /- Subtype -/ namespace Subtype def existsOfSubtype {α : Type u} {p : α → Prop} : { x // p x } → Exists (fun x => p x) | ⟨a, h⟩ => ⟨a, h⟩ variables {α : Type u} {p : α → Prop} theorem tagIrrelevant {a : α} (h1 h2 : p a) : mk a h1 = mk a h2 := rfl protected theorem eq : ∀ {a1 a2 : {x // p x}}, val a1 = val a2 → a1 = a2 | ⟨x, h1⟩, ⟨.(x), h2⟩, rfl => rfl theorem eta (a : {x // p x}) (h : p (val a)) : mk (val a) h = a := Subtype.eq rfl instance {α : Type u} {p : α → Prop} {a : α} (h : p a) : Inhabited {x // p x} := ⟨⟨a, h⟩⟩ instance {α : Type u} {p : α → Prop} [DecidableEq α] : DecidableEq {x : α // p x} := fun ⟨a, h₁⟩ ⟨b, h₂⟩ => if h : a = b then isTrue (Subtype.eq h) else isFalse (fun h' => Subtype.noConfusion h' (fun h' => absurd h' h)) end Subtype /- Sum -/ section variables {α : Type u} {β : Type v} instance Sum.inhabitedLeft [h : Inhabited α] : Inhabited (Sum α β) := ⟨Sum.inl (arbitrary α)⟩ instance Sum.inhabitedRight [h : Inhabited β] : Inhabited (Sum α β) := ⟨Sum.inr (arbitrary β)⟩ instance {α : Type u} {β : Type v} [DecidableEq α] [DecidableEq β] : DecidableEq (Sum α β) := fun a b => match a, b with | (Sum.inl a), (Sum.inl b) => if h : a = b then isTrue (h ▸ rfl) else isFalse (fun h' => Sum.noConfusion h' (fun h' => absurd h' h)) | (Sum.inr a), (Sum.inr b) => if h : a = b then isTrue (h ▸ rfl) else isFalse (fun h' => Sum.noConfusion h' (fun h' => absurd h' h)) | (Sum.inr a), (Sum.inl b) => isFalse (fun h => Sum.noConfusion h) | (Sum.inl a), (Sum.inr b) => isFalse (fun h => Sum.noConfusion h) end /- Product -/ section variables {α : Type u} {β : Type v} instance [Inhabited α] [Inhabited β] : Inhabited (Prod α β) := ⟨(arbitrary α, arbitrary β)⟩ instance [DecidableEq α] [DecidableEq β] : DecidableEq (α × β) := fun ⟨a, b⟩ ⟨a', b'⟩ => match (decEq a a') with | (isTrue e₁) => match (decEq b b') with | (isTrue e₂) => isTrue (Eq.recOn e₁ (Eq.recOn e₂ rfl)) | (isFalse n₂) => isFalse (fun h => Prod.noConfusion h (fun e₁' e₂' => absurd e₂' n₂)) | (isFalse n₁) => isFalse (fun h => Prod.noConfusion h (fun e₁' e₂' => absurd e₁' n₁)) instance [HasBeq α] [HasBeq β] : HasBeq (α × β) := ⟨fun ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ => a₁ == a₂ && b₁ == b₂⟩ instance [HasLess α] [HasLess β] : HasLess (α × β) := ⟨fun s t => s.1 < t.1 ∨ (s.1 = t.1 ∧ s.2 < t.2)⟩ instance prodHasDecidableLt [HasLess α] [HasLess β] [DecidableEq α] [DecidableEq β] [∀ (a b : α), Decidable (a < b)] [∀ (a b : β), Decidable (a < b)] : ∀ (s t : α × β), Decidable (s < t) := fun t s => Or.Decidable theorem Prod.ltDef [HasLess α] [HasLess β] (s t : α × β) : (s < t) = (s.1 < t.1 ∨ (s.1 = t.1 ∧ s.2 < t.2)) := rfl end def Prod.map.{u₁, u₂, v₁, v₂} {α₁ : Type u₁} {α₂ : Type u₂} {β₁ : Type v₁} {β₂ : Type v₂} (f : α₁ → α₂) (g : β₁ → β₂) : α₁ × β₁ → α₂ × β₂ | (a, b) => (f a, g b) /- Dependent products -/ -- notation `Σ` binders `, ` r:(scoped p, Sigma p) := r -- notation `Σ'` binders `, ` r:(scoped p, PSigma p) := r theorem exOfPsig {α : Type u} {p : α → Prop} : (PSigma (fun x => p x)) → Exists (fun x => p x) | ⟨x, hx⟩ => ⟨x, hx⟩ section variables {α : Type u} {β : α → Type v} protected theorem Sigma.eq : ∀ {p₁ p₂ : Sigma (fun a => β a)} (h₁ : p₁.1 = p₂.1), (Eq.recOn h₁ p₁.2 : β p₂.1) = p₂.2 → p₁ = p₂ | ⟨a, b⟩, ⟨.(a), .(b)⟩, rfl, rfl => rfl end section variables {α : Sort u} {β : α → Sort v} protected theorem PSigma.eq : ∀ {p₁ p₂ : PSigma β} (h₁ : p₁.1 = p₂.1), (Eq.recOn h₁ p₁.2 : β p₂.1) = p₂.2 → p₁ = p₂ | ⟨a, b⟩, ⟨.(a), .(b)⟩, rfl, rfl => rfl end /- Universe polymorphic unit -/ theorem punitEq (a b : PUnit) : a = b := PUnit.recOn a (PUnit.recOn b rfl) theorem punitEqPUnit (a : PUnit) : a = () := punitEq a () instance : Subsingleton PUnit := Subsingleton.intro punitEq instance : Inhabited PUnit := ⟨⟨⟩⟩ instance : DecidableEq PUnit := fun a b => isTrue (punitEq a b) /- Setoid -/ class Setoid (α : Sort u) := (r : α → α → Prop) (iseqv : Equivalence r) instance setoidHasEquiv {α : Sort u} [Setoid α] : HasEquiv α := ⟨Setoid.r⟩ namespace Setoid variables {α : Sort u} [Setoid α] theorem refl (a : α) : a ≈ a := match Setoid.iseqv α with | ⟨hRefl, hSymm, hTrans⟩ => hRefl a theorem symm {a b : α} (hab : a ≈ b) : b ≈ a := match Setoid.iseqv α with | ⟨hRefl, hSymm, hTrans⟩ => hSymm hab theorem trans {a b c : α} (hab : a ≈ b) (hbc : b ≈ c) : a ≈ c := match Setoid.iseqv α with | ⟨hRefl, hSymm, hTrans⟩ => hTrans hab hbc end Setoid /- Propositional extensionality -/ axiom propext {a b : Prop} : (a ↔ b) → a = b theorem eqTrueIntro {a : Prop} (h : a) : a = True := propext (iffTrueIntro h) theorem eqFalseIntro {a : Prop} (h : ¬a) : a = False := propext (iffFalseIntro h) /- Quotients -/ -- Iff can now be used to do substitutions in a calculation theorem iffSubst {a b : Prop} {p : Prop → Prop} (h₁ : a ↔ b) (h₂ : p a) : p b := Eq.subst (propext h₁) h₂ namespace Quot axiom sound : ∀ {α : Sort u} {r : α → α → Prop} {a b : α}, r a b → Quot.mk r a = Quot.mk r b attribute [elabAsEliminator] lift ind protected theorem liftBeta {α : Sort u} {r : α → α → Prop} {β : Sort v} (f : α → β) (c : ∀ a b, r a b → f a = f b) (a : α) : lift f c (Quot.mk r a) = f a := rfl protected theorem indBeta {α : Sort u} {r : α → α → Prop} {β : Quot r → Prop} (p : ∀ a, β (Quot.mk r a)) (a : α) : (ind p (Quot.mk r a) : β (Quot.mk r a)) = p a := rfl @[reducible, elabAsEliminator, inline] protected def liftOn {α : Sort u} {β : Sort v} {r : α → α → Prop} (q : Quot r) (f : α → β) (c : ∀ a b, r a b → f a = f b) : β := lift f c q @[elabAsEliminator] protected theorem inductionOn {α : Sort u} {r : α → α → Prop} {β : Quot r → Prop} (q : Quot r) (h : ∀ a, β (Quot.mk r a)) : β q := ind h q theorem existsRep {α : Sort u} {r : α → α → Prop} (q : Quot r) : Exists (fun a => (Quot.mk r a) = q) := Quot.inductionOn q (fun a => ⟨a, rfl⟩) section variable {α : Sort u} variable {r : α → α → Prop} variable {β : Quot r → Sort v} @[reducible, macroInline] protected def indep (f : ∀ a, β (Quot.mk r a)) (a : α) : PSigma β := ⟨Quot.mk r a, f a⟩ protected theorem indepCoherent (f : ∀ a, β (Quot.mk r a)) (h : ∀ (a b : α) (p : r a b), (Eq.rec (f a) (sound p) : β (Quot.mk r b)) = f b) : ∀ a b, r a b → Quot.indep f a = Quot.indep f b := fun a b e => PSigma.eq (sound e) (h a b e) protected theorem liftIndepPr1 (f : ∀ a, β (Quot.mk r a)) (h : ∀ (a b : α) (p : r a b), (Eq.rec (f a) (sound p) : β (Quot.mk r b)) = f b) (q : Quot r) : (lift (Quot.indep f) (Quot.indepCoherent f h) q).1 = q := Quot.ind (fun (a : α) => Eq.refl (Quot.indep f a).1) q @[reducible, elabAsEliminator, inline] protected def rec (f : ∀ a, β (Quot.mk r a)) (h : ∀ (a b : α) (p : r a b), (Eq.rec (f a) (sound p) : β (Quot.mk r b)) = f b) (q : Quot r) : β q := Eq.ndrecOn (Quot.liftIndepPr1 f h q) ((lift (Quot.indep f) (Quot.indepCoherent f h) q).2) @[reducible, elabAsEliminator, inline] protected def recOn (q : Quot r) (f : ∀ a, β (Quot.mk r a)) (h : ∀ (a b : α) (p : r a b), (Eq.rec (f a) (sound p) : β (Quot.mk r b)) = f b) : β q := Quot.rec f h q @[reducible, elabAsEliminator, inline] protected def recOnSubsingleton [h : ∀ a, Subsingleton (β (Quot.mk r a))] (q : Quot r) (f : ∀ a, β (Quot.mk r a)) : β q := Quot.rec f (fun a b h => Subsingleton.elim _ (f b)) q @[reducible, elabAsEliminator, inline] protected def hrecOn (q : Quot r) (f : ∀ a, β (Quot.mk r a)) (c : ∀ (a b : α) (p : r a b), f a ≅ f b) : β q := Quot.recOn q f $ fun a b p => eqOfHEq $ have p₁ : (Eq.rec (f a) (sound p) : β (Quot.mk r b)) ≅ f a := eqRecHEq (sound p) (f a); HEq.trans p₁ (c a b p) end end Quot def Quotient {α : Sort u} (s : Setoid α) := @Quot α Setoid.r namespace Quotient @[inline] protected def mk {α : Sort u} [s : Setoid α] (a : α) : Quotient s := Quot.mk Setoid.r a def sound {α : Sort u} [s : Setoid α] {a b : α} : a ≈ b → Quotient.mk a = Quotient.mk b := Quot.sound @[reducible, elabAsEliminator] protected def lift {α : Sort u} {β : Sort v} [s : Setoid α] (f : α → β) : (∀ a b, a ≈ b → f a = f b) → Quotient s → β := Quot.lift f @[elabAsEliminator] protected theorem ind {α : Sort u} [s : Setoid α] {β : Quotient s → Prop} : (∀ a, β (Quotient.mk a)) → ∀ q, β q := Quot.ind @[reducible, elabAsEliminator, inline] protected def liftOn {α : Sort u} {β : Sort v} [s : Setoid α] (q : Quotient s) (f : α → β) (c : ∀ a b, a ≈ b → f a = f b) : β := Quot.liftOn q f c @[elabAsEliminator] protected theorem inductionOn {α : Sort u} [s : Setoid α] {β : Quotient s → Prop} (q : Quotient s) (h : ∀ a, β (Quotient.mk a)) : β q := Quot.inductionOn q h theorem existsRep {α : Sort u} [s : Setoid α] (q : Quotient s) : Exists (fun (a : α) => Quotient.mk a = q) := Quot.existsRep q section variable {α : Sort u} variable [s : Setoid α] variable {β : Quotient s → Sort v} @[inline] protected def rec (f : ∀ a, β (Quotient.mk a)) (h : ∀ (a b : α) (p : a ≈ b), (Eq.rec (f a) (Quotient.sound p) : β (Quotient.mk b)) = f b) (q : Quotient s) : β q := Quot.rec f h q @[reducible, elabAsEliminator, inline] protected def recOn (q : Quotient s) (f : ∀ a, β (Quotient.mk a)) (h : ∀ (a b : α) (p : a ≈ b), (Eq.rec (f a) (Quotient.sound p) : β (Quotient.mk b)) = f b) : β q := Quot.recOn q f h @[reducible, elabAsEliminator, inline] protected def recOnSubsingleton [h : ∀ a, Subsingleton (β (Quotient.mk a))] (q : Quotient s) (f : ∀ a, β (Quotient.mk a)) : β q := @Quot.recOnSubsingleton _ _ _ h q f @[reducible, elabAsEliminator, inline] protected def hrecOn (q : Quotient s) (f : ∀ a, β (Quotient.mk a)) (c : ∀ (a b : α) (p : a ≈ b), f a ≅ f b) : β q := Quot.hrecOn q f c end section universes uA uB uC variables {α : Sort uA} {β : Sort uB} {φ : Sort uC} variables [s₁ : Setoid α] [s₂ : Setoid β] @[reducible, elabAsEliminator, inline] protected def lift₂ (f : α → β → φ)(c : ∀ a₁ a₂ b₁ b₂, a₁ ≈ b₁ → a₂ ≈ b₂ → f a₁ a₂ = f b₁ b₂) (q₁ : Quotient s₁) (q₂ : Quotient s₂) : φ := Quotient.lift (fun (a₁ : α) => Quotient.lift (f a₁) (fun (a b : β) => c a₁ a a₁ b (Setoid.refl a₁)) q₂) (fun (a b : α) (h : a ≈ b) => @Quotient.ind β s₂ (fun (a1 : Quotient s₂) => (Quotient.lift (f a) (fun (a1 b : β) => c a a1 a b (Setoid.refl a)) a1) = (Quotient.lift (f b) (fun (a b1 : β) => c b a b b1 (Setoid.refl b)) a1)) (fun (a' : β) => c a a' b a' h (Setoid.refl a')) q₂) q₁ @[reducible, elabAsEliminator, inline] protected def liftOn₂ (q₁ : Quotient s₁) (q₂ : Quotient s₂) (f : α → β → φ) (c : ∀ a₁ a₂ b₁ b₂, a₁ ≈ b₁ → a₂ ≈ b₂ → f a₁ a₂ = f b₁ b₂) : φ := Quotient.lift₂ f c q₁ q₂ @[elabAsEliminator] protected theorem ind₂ {φ : Quotient s₁ → Quotient s₂ → Prop} (h : ∀ a b, φ (Quotient.mk a) (Quotient.mk b)) (q₁ : Quotient s₁) (q₂ : Quotient s₂) : φ q₁ q₂ := Quotient.ind (fun a₁ => Quotient.ind (fun a₂ => h a₁ a₂) q₂) q₁ @[elabAsEliminator] protected theorem inductionOn₂ {φ : Quotient s₁ → Quotient s₂ → Prop} (q₁ : Quotient s₁) (q₂ : Quotient s₂) (h : ∀ a b, φ (Quotient.mk a) (Quotient.mk b)) : φ q₁ q₂ := Quotient.ind (fun a₁ => Quotient.ind (fun a₂ => h a₁ a₂) q₂) q₁ @[elabAsEliminator] protected theorem inductionOn₃ [s₃ : Setoid φ] {δ : Quotient s₁ → Quotient s₂ → Quotient s₃ → Prop} (q₁ : Quotient s₁) (q₂ : Quotient s₂) (q₃ : Quotient s₃) (h : ∀ a b c, δ (Quotient.mk a) (Quotient.mk b) (Quotient.mk c)) : δ q₁ q₂ q₃ := Quotient.ind (fun a₁ => Quotient.ind (fun a₂ => Quotient.ind (fun a₃ => h a₁ a₂ a₃) q₃) q₂) q₁ end section Exact variable {α : Sort u} private def rel [s : Setoid α] (q₁ q₂ : Quotient s) : Prop := Quotient.liftOn₂ q₁ q₂ (fun a₁ a₂ => a₁ ≈ a₂) (fun a₁ a₂ b₁ b₂ a₁b₁ a₂b₂ => propext (Iff.intro (fun a₁a₂ => Setoid.trans (Setoid.symm a₁b₁) (Setoid.trans a₁a₂ a₂b₂)) (fun b₁b₂ => Setoid.trans a₁b₁ (Setoid.trans b₁b₂ (Setoid.symm a₂b₂))))) private theorem rel.refl [s : Setoid α] : ∀ (q : Quotient s), rel q q := fun q => Quot.inductionOn q (fun a => Setoid.refl a) private theorem eqImpRel [s : Setoid α] {q₁ q₂ : Quotient s} : q₁ = q₂ → rel q₁ q₂ := fun h => Eq.ndrecOn h (rel.refl q₁) theorem exact [s : Setoid α] {a b : α} : Quotient.mk a = Quotient.mk b → a ≈ b := fun h => eqImpRel h end Exact section universes uA uB uC variables {α : Sort uA} {β : Sort uB} variables [s₁ : Setoid α] [s₂ : Setoid β] @[reducible, elabAsEliminator] protected def recOnSubsingleton₂ {φ : Quotient s₁ → Quotient s₂ → Sort uC} [h : ∀ a b, Subsingleton (φ (Quotient.mk a) (Quotient.mk b))] (q₁ : Quotient s₁) (q₂ : Quotient s₂) (f : ∀ a b, φ (Quotient.mk a) (Quotient.mk b)) : φ q₁ q₂:= @Quotient.recOnSubsingleton _ s₁ (fun q => φ q q₂) (fun a => Quotient.ind (fun b => h a b) q₂) q₁ (fun a => Quotient.recOnSubsingleton q₂ (fun b => f a b)) end end Quotient section variable {α : Type u} variable (r : α → α → Prop) inductive EqvGen : α → α → Prop | rel {} : ∀ x y, r x y → EqvGen x y | refl {} : ∀ x, EqvGen x x | symm {} : ∀ x y, EqvGen x y → EqvGen y x | trans {} : ∀ x y z, EqvGen x y → EqvGen y z → EqvGen x z theorem EqvGen.isEquivalence : Equivalence (@EqvGen α r) := mkEquivalence _ EqvGen.refl EqvGen.symm EqvGen.trans def EqvGen.Setoid : Setoid α := Setoid.mk _ (EqvGen.isEquivalence r) theorem Quot.exact {a b : α} (H : Quot.mk r a = Quot.mk r b) : EqvGen r a b := @Quotient.exact _ (EqvGen.Setoid r) a b (@congrArg _ _ _ _ (Quot.lift (@Quotient.mk _ (EqvGen.Setoid r)) (fun x y h => Quot.sound (EqvGen.rel x y h))) H) theorem Quot.eqvGenSound {r : α → α → Prop} {a b : α} (H : EqvGen r a b) : Quot.mk r a = Quot.mk r b := EqvGen.recOn H (fun x y h => Quot.sound h) (fun x => rfl) (fun x y _ IH => Eq.symm IH) (fun x y z _ _ IH₁ IH₂ => Eq.trans IH₁ IH₂) end instance {α : Sort u} {s : Setoid α} [d : ∀ (a b : α), Decidable (a ≈ b)] : DecidableEq (Quotient s) := fun (q₁ q₂ : Quotient s) => Quotient.recOnSubsingleton₂ q₁ q₂ (fun a₁ a₂ => match (d a₁ a₂) with | (isTrue h₁) => isTrue (Quotient.sound h₁) | (isFalse h₂) => isFalse (fun h => absurd (Quotient.exact h) h₂)) /- Function extensionality -/ namespace Function variables {α : Sort u} {β : α → Sort v} def Equiv (f₁ f₂ : ∀ (x : α), β x) : Prop := ∀ x, f₁ x = f₂ x protected theorem Equiv.refl (f : ∀ (x : α), β x) : Equiv f f := fun x => rfl protected theorem Equiv.symm {f₁ f₂ : ∀ (x : α), β x} : Equiv f₁ f₂ → Equiv f₂ f₁ := fun h x => Eq.symm (h x) protected theorem Equiv.trans {f₁ f₂ f₃ : ∀ (x : α), β x} : Equiv f₁ f₂ → Equiv f₂ f₃ → Equiv f₁ f₃ := fun h₁ h₂ x => Eq.trans (h₁ x) (h₂ x) protected theorem Equiv.isEquivalence (α : Sort u) (β : α → Sort v) : Equivalence (@Function.Equiv α β) := mkEquivalence (@Function.Equiv α β) (@Equiv.refl α β) (@Equiv.symm α β) (@Equiv.trans α β) end Function section open Quotient variables {α : Sort u} {β : α → Sort v} @[instance] private def funSetoid (α : Sort u) (β : α → Sort v) : Setoid (∀ (x : α), β x) := Setoid.mk (@Function.Equiv α β) (Function.Equiv.isEquivalence α β) private def extfunApp (f : Quotient $ funSetoid α β) : ∀ (x : α), β x := fun x => Quot.liftOn f (fun (f : ∀ (x : α), β x) => f x) (fun f₁ f₂ h => h x) theorem funext {f₁ f₂ : ∀ (x : α), β x} (h : ∀ x, f₁ x = f₂ x) : f₁ = f₂ := show extfunApp (Quotient.mk f₁) = extfunApp (Quotient.mk f₂) from congrArg extfunApp (sound h) end instance Forall.Subsingleton {α : Sort u} {β : α → Sort v} [∀ a, Subsingleton (β a)] : Subsingleton (∀ a, β a) := ⟨fun f₁ f₂ => funext (fun a => Subsingleton.elim (f₁ a) (f₂ a))⟩ /- General operations on functions -/ namespace Function universes u₁ u₂ u₃ u₄ variables {α : Sort u₁} {β : Sort u₂} {φ : Sort u₃} {δ : Sort u₄} {ζ : Sort u₁} @[inline, reducible] def comp (f : β → φ) (g : α → β) : α → φ := fun x => f (g x) infixr ` ∘ ` := Function.comp @[inline, reducible] def onFun (f : β → β → φ) (g : α → β) : α → α → φ := fun x y => f (g x) (g y) @[inline, reducible] def combine (f : α → β → φ) (op : φ → δ → ζ) (g : α → β → δ) : α → β → ζ := fun x y => op (f x y) (g x y) @[inline, reducible] def const (β : Sort u₂) (a : α) : β → α := fun x => a @[inline, reducible] def swap {φ : α → β → Sort u₃} (f : ∀ x y, φ x y) : ∀ y x, φ x y := fun y x => f x y end Function /- Squash -/ def Squash (α : Type u) := Quot (fun (a b : α) => True) def Squash.mk {α : Type u} (x : α) : Squash α := Quot.mk _ x theorem Squash.ind {α : Type u} {β : Squash α → Prop} (h : ∀ (a : α), β (Squash.mk a)) : ∀ (q : Squash α), β q := Quot.ind h @[inline] def Squash.lift {α β} [Subsingleton β] (s : Squash α) (f : α → β) : β := Quot.lift f (fun a b _ => Subsingleton.elim _ _) s instance Squash.Subsingleton {α} : Subsingleton (Squash α) := ⟨Squash.ind (fun (a : α) => Squash.ind (fun (b : α) => Quot.sound trivial))⟩ /- Classical reasoning support -/ namespace Classical axiom choice {α : Sort u} : Nonempty α → α noncomputable def indefiniteDescription {α : Sort u} (p : α → Prop) (h : Exists (fun x => p x)) : {x // p x} := choice $ let ⟨x, px⟩ := h; ⟨⟨x, px⟩⟩ noncomputable def choose {α : Sort u} {p : α → Prop} (h : Exists (fun x => p x)) : α := (indefiniteDescription p h).val theorem chooseSpec {α : Sort u} {p : α → Prop} (h : Exists (fun x => p x)) : p (choose h) := (indefiniteDescription p h).property /- Diaconescu's theorem: excluded middle from choice, Function extensionality and propositional extensionality. -/ theorem em (p : Prop) : p ∨ ¬p := let U (x : Prop) : Prop := x = True ∨ p; let V (x : Prop) : Prop := x = False ∨ p; have exU : Exists (fun x => U x) from ⟨True, Or.inl rfl⟩; have exV : Exists (fun x => V x) from ⟨False, Or.inl rfl⟩; let u : Prop := choose exU; let v : Prop := choose exV; have uDef : U u from chooseSpec exU; have vDef : V v from chooseSpec exV; have notUvOrP : u ≠ v ∨ p from Or.elim uDef (fun hut => Or.elim vDef (fun hvf => have hne : u ≠ v from hvf.symm ▸ hut.symm ▸ trueNeFalse; Or.inl hne) Or.inr) Or.inr; have pImpliesUv : p → u = v from fun hp => have hpred : U = V from funext $ fun x => have hl : (x = True ∨ p) → (x = False ∨ p) from fun a => Or.inr hp; have hr : (x = False ∨ p) → (x = True ∨ p) from fun a => Or.inr hp; show (x = True ∨ p) = (x = False ∨ p) from propext (Iff.intro hl hr); have h₀ : ∀ exU exV, @choose _ U exU = @choose _ V exV from hpred ▸ fun exU exV => rfl; show u = v from h₀ _ _; Or.elim notUvOrP (fun (hne : u ≠ v) => Or.inr (mt pImpliesUv hne)) Or.inl theorem existsTrueOfNonempty {α : Sort u} : Nonempty α → Exists (fun (x : α) => True) | ⟨x⟩ => ⟨x, trivial⟩ noncomputable def inhabitedOfNonempty {α : Sort u} (h : Nonempty α) : Inhabited α := ⟨choice h⟩ noncomputable def inhabitedOfExists {α : Sort u} {p : α → Prop} (h : Exists (fun x => p x)) : Inhabited α := inhabitedOfNonempty (Exists.elim h (fun w hw => ⟨w⟩)) /- all propositions are Decidable -/ noncomputable def propDecidable (a : Prop) : Decidable a := choice $ Or.elim (em a) (fun ha => ⟨isTrue ha⟩) (fun hna => ⟨isFalse hna⟩) noncomputable def decidableInhabited (a : Prop) : Inhabited (Decidable a) := ⟨propDecidable a⟩ noncomputable def typeDecidableEq (α : Sort u) : DecidableEq α := fun x y => propDecidable (x = y) noncomputable def typeDecidable (α : Sort u) : PSum α (α → False) := match (propDecidable (Nonempty α)) with | (isTrue hp) => PSum.inl (@arbitrary _ (inhabitedOfNonempty hp)) | (isFalse hn) => PSum.inr (fun a => absurd (Nonempty.intro a) hn) noncomputable def strongIndefiniteDescription {α : Sort u} (p : α → Prop) (h : Nonempty α) : {x : α // Exists (fun (y : α) => p y) → p x} := @dite _ (Exists (fun (x : α) => p x)) (propDecidable _) (fun (hp : Exists (fun (x : α) => p x)) => show {x : α // Exists (fun (y : α) => p y) → p x} from let xp := indefiniteDescription _ hp; ⟨xp.val, fun h' => xp.property⟩) (fun hp => ⟨choice h, fun h => absurd h hp⟩) /- the Hilbert epsilon Function -/ noncomputable def epsilon {α : Sort u} [h : Nonempty α] (p : α → Prop) : α := (strongIndefiniteDescription p h).val theorem epsilonSpecAux {α : Sort u} (h : Nonempty α) (p : α → Prop) : Exists (fun y => p y) → p (@epsilon α h p) := (strongIndefiniteDescription p h).property theorem epsilonSpec {α : Sort u} {p : α → Prop} (hex : Exists (fun y => p y)) : p (@epsilon α (nonemptyOfExists hex) p) := epsilonSpecAux (nonemptyOfExists hex) p hex theorem epsilonSingleton {α : Sort u} (x : α) : @epsilon α ⟨x⟩ (fun y => y = x) = x := @epsilonSpec α (fun y => y = x) ⟨x, rfl⟩ /- the axiom of choice -/ theorem axiomOfChoice {α : Sort u} {β : α → Sort v} {r : ∀ x, β x → Prop} (h : ∀ x, Exists (fun y => r x y)) : Exists (fun (f : ∀ x, β x) => ∀ x, r x (f x)) := ⟨_, fun x => chooseSpec (h x)⟩ theorem skolem {α : Sort u} {b : α → Sort v} {p : ∀ x, b x → Prop} : (∀ x, Exists (fun y => p x y)) ↔ Exists (fun (f : ∀ x, b x) => ∀ x, p x (f x)) := ⟨axiomOfChoice, fun ⟨f, hw⟩ (x) => ⟨f x, hw x⟩⟩ theorem propComplete (a : Prop) : a = True ∨ a = False := Or.elim (em a) (fun t => Or.inl (eqTrueIntro t)) (fun f => Or.inr (eqFalseIntro f)) -- this supercedes byCases in Decidable theorem byCases {p q : Prop} (hpq : p → q) (hnpq : ¬p → q) : q := @Decidable.byCases _ _ (propDecidable _) hpq hnpq -- this supercedes byContradiction in Decidable theorem byContradiction {p : Prop} (h : ¬p → False) : p := @Decidable.byContradiction _ (propDecidable _) h end Classical
f451a71ab3a2f630e63c743cb27016cfaf44c822
00f20606b80ff481edcb3343798a879c6c568f6c
/M3P14/sheet1.lean
74d56494254ec3a093a993ce7d83cbc8abd1a201
[ "Apache-2.0" ]
permissive
SG4316/xena-UROP-2018
53c21afcc6909bde186e3d9b8e8bf4c888a5f32e
d20b155d317e210917e9ce88d91457a7c0f2efac
refs/heads/master
1,584,877,373,507
1,530,817,535,000
1,530,817,535,000
139,990,847
0
0
null
1,530,884,893,000
1,530,884,893,000
null
UTF-8
Lean
false
false
4,013
lean
import data.nat.gcd import data.nat.modeq import data.nat.prime import tactic.norm_num --import algebra.euclidean_domain namespace nat -- TODO: import mathlib -- TODO: change some theorem signatures from ℕ to ℤ (need to extend the definition of some gcd lemmas) -- Show that for a, b, d integers, we have (da, db) = d(a,b). theorem q1a (a b d : ℕ) : gcd (d*a) (d*b) = d * (gcd a b) := gcd_mul_left d a b -- Let a, b, n integers, and suppose that n|ab. Show that n/(a,b) divides b. theorem q1b (a b n : ℕ) (ha : a > 0) (hn : n > 0) : n ∣ (a*b) → (n / gcd a n) ∣ b := λ h, have n / gcd n a ∣ b * (a / gcd n a) := dvd_of_mul_dvd_mul_right (gcd_pos_of_pos_left a hn) begin rw ← nat.mul_div_assoc _ (gcd_dvd_right n a), rw nat.div_mul_cancel (gcd_dvd_left n a), rw mul_comm b, rwa nat.div_mul_cancel (dvd_trans (gcd_dvd_left n a) h), end, begin cases exists_eq_mul_left_of_dvd h with c hc, have := coprime.dvd_of_dvd_mul_right (coprime_div_gcd_div_gcd (gcd_pos_of_pos_left a hn)) this, rwa gcd_comm, end -- Express 18 as an integer linear combination of 327 and 120. theorem q2a : ∃ x y : ℕ, 18 = 327*x + 120*y := ⟨-66, 180, by norm_num⟩ -- Find, with proof, all solutions to the linear diophantine equation 100x + 68y = 14. theorem q2b : ∀ x y : ℕ, 100*x + 68*y = 14 := sorry -- Find a multiplicative inverse of 31 modulo 132. theorem q2c : -- Find an integer congruent to 3 mod 9 and congruent to 1 mod 49. theorem q2d : ∃ x : ℤ, x % 9 = 3 → x % 49 = 1 := ⟨-195, by rfl⟩ -- Find, with proof, the smallest nonnegative integer n such that n = 1 (mod 3), n = 4 (mod 5), and n = 3 (mod 7). theorem q2e : ∃ n : ℕ, ∀ n₂ : ℕ, n % 3 = 1 → n % 5 = 4 → n % 7 = 3 → n₂ % 3 = 1 → n₂ % 5 = 4 → n₂ % 7 = 3 → n ≤ n₂ := sorry -- Let m and n be integers. Show that the greatest common divisor of m and n is the unique positive integer d such that: -- - d divides both m and n, and -- - if x divides both m and n, then x divides d. theorem q3 : ∀ m n : ℕ, ∃! d : ℕ, ∀ x : ℕ, gcd m n = d → d ∣ m → d ∣ n → x ∣ m → x ∣ n → x ∣ d := sorry -- Let a and b be nonzero integers. Show that there is a unique positive integer m with the following two properties: -- - a and b divide m, and -- - if n is any number divisible by both a and b, then m|n. -- The number m is called the least common multiple of a and b. theorem q4a : ∀ a b : ℕ, ∃! m : ℕ, ∀ n : ℕ, ¬(a = 0) → ¬(b = 0) → a ∣ m → b ∣ m → a ∣ n → b ∣ n → m ∣ n := sorry -- Show that the least common multiple of a and b is given by |ab|/(a,b) theorem q4b : -- Let m and n be positive integers, and let K be the kernel of the map: -- ℤ/mnℤ → ℤ/mℤ x ℤ/nℤ -- that takes a class mod mn to the corresponding classes modulo m and n. -- Show that K has (m, n) elements. What are they? theorem q5 : -- Show that the equation ax = b (mod n) has no solutions if b is not divisible by (a, n), and exactly (a, n) solutions in ℤ/n otherwise. theorem q6 : -- For n a positive integer, let σ(n) denote the sum Σ d for d∣n and d>0, of the positive divisors of n. -- Show that the function n ↦ σ(n) is multiplicative. theorem q7 : -- Let p be a prime, and a be any integer. Show that a^(p²+p+1) is congruent to a^3 modulo p. theorem q8: ∀ a p : ℕ, p → a^(p^2+p+1) ≡ a^3 [MOD p] := sorry -- Let n be a squarefree positive integer, and suppose that for all primes p dividing n, we have (p-1)∣(n - 1). -- Show that for all integers a with (a, n) = 1, we have a^n = a (mod n). theorem q9 : -- Let n be a positive integer. Show that Σ Φ(d) for d∣n and d>0 = n. -- [Hint: First show that the number of integers a with a ≤ 0 < n and (a, n) = n/d is equal to Φ(d).] theorem q10 : end nat
e1c1092fe9201e7c9674328b938cb420101f19e0
74caf7451c921a8d5ab9c6e2b828c9d0a35aae95
/tests/lean/run/xrewrite1.lean
4d176f231bda2944882d1c26cac4ba978e14bcf5
[ "Apache-2.0" ]
permissive
sakas--/lean
f37b6fad4fd4206f2891b89f0f8135f57921fc3f
570d9052820be1d6442a5cc58ece37397f8a9e4c
refs/heads/master
1,586,127,145,194
1,480,960,018,000
1,480,960,635,000
40,137,176
0
0
null
1,438,621,351,000
1,438,621,351,000
null
UTF-8
Lean
false
false
303
lean
open nat tactic constant zeroadd (a : nat) : 0 + a = a meta definition xrewrite (th_name : name) : tactic unit := do th ← mk_const th_name, rewrite_core semireducible tt occurrences.all ff th, try reflexivity example (a : nat) : (0 + a) + (0 + a) + (0 + a) = a + a + a := by xrewrite `zeroadd
ecae4d650f0055a562c600b60eb8b72803c45b5b
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/linear_algebra/ray.lean
503db73e8a277ac75e03e392c3e5d2626f042a5f
[ "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
26,543
lean
/- Copyright (c) 2021 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers -/ import group_theory.subgroup.actions import linear_algebra.linear_independent /-! # Rays in modules > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines rays in modules. ## Main definitions * `same_ray`: two vectors belong to the same ray if they are proportional with a nonnegative coefficient. * `module.ray` is a type for the equivalence class of nonzero vectors in a module with some common positive multiple. -/ noncomputable theory open_locale big_operators section strict_ordered_comm_semiring variables (R : Type*) [strict_ordered_comm_semiring R] variables {M : Type*} [add_comm_monoid M] [module R M] variables {N : Type*} [add_comm_monoid N] [module R N] variables (ι : Type*) [decidable_eq ι] /-- Two vectors are in the same ray if either one of them is zero or some positive multiples of them are equal (in the typical case over a field, this means one of them is a nonnegative multiple of the other). -/ def same_ray (v₁ v₂ : M) : Prop := v₁ = 0 ∨ v₂ = 0 ∨ ∃ (r₁ r₂ : R), 0 < r₁ ∧ 0 < r₂ ∧ r₁ • v₁ = r₂ • v₂ variables {R} namespace same_ray variables {x y z : M} @[simp] lemma zero_left (y : M) : same_ray R 0 y := or.inl rfl @[simp] lemma zero_right (x : M) : same_ray R x 0 := or.inr $ or.inl rfl @[nontriviality] lemma of_subsingleton [subsingleton M] (x y : M) : same_ray R x y := by { rw [subsingleton.elim x 0], exact zero_left _ } @[nontriviality] lemma of_subsingleton' [subsingleton R] (x y : M) : same_ray R x y := by { haveI := module.subsingleton R M, exact of_subsingleton x y } /-- `same_ray` is reflexive. -/ @[refl] lemma refl (x : M) : same_ray R x x := begin nontriviality R, exact or.inr (or.inr $ ⟨1, 1, zero_lt_one, zero_lt_one, rfl⟩) end protected lemma rfl : same_ray R x x := refl _ /-- `same_ray` is symmetric. -/ @[symm] lemma symm (h : same_ray R x y) : same_ray R y x := (or.left_comm.1 h).imp_right $ or.imp_right $ λ ⟨r₁, r₂, h₁, h₂, h⟩, ⟨r₂, r₁, h₂, h₁, h.symm⟩ /-- If `x` and `y` are nonzero vectors on the same ray, then there exist positive numbers `r₁ r₂` such that `r₁ • x = r₂ • y`. -/ lemma exists_pos (h : same_ray R x y) (hx : x ≠ 0) (hy : y ≠ 0) : ∃ r₁ r₂ : R, 0 < r₁ ∧ 0 < r₂ ∧ r₁ • x = r₂ • y := (h.resolve_left hx).resolve_left hy lemma _root_.same_ray_comm : same_ray R x y ↔ same_ray R y x := ⟨same_ray.symm, same_ray.symm⟩ /-- `same_ray` is transitive unless the vector in the middle is zero and both other vectors are nonzero. -/ lemma trans (hxy : same_ray R x y) (hyz : same_ray R y z) (hy : y = 0 → x = 0 ∨ z = 0) : same_ray R x z := begin rcases eq_or_ne x 0 with rfl|hx, { exact zero_left z }, rcases eq_or_ne z 0 with rfl|hz, { exact zero_right x }, rcases eq_or_ne y 0 with rfl|hy, { exact (hy rfl).elim (λ h, (hx h).elim) (λ h, (hz h).elim) }, rcases hxy.exists_pos hx hy with ⟨r₁, r₂, hr₁, hr₂, h₁⟩, rcases hyz.exists_pos hy hz with ⟨r₃, r₄, hr₃, hr₄, h₂⟩, refine or.inr (or.inr $ ⟨r₃ * r₁, r₂ * r₄, mul_pos hr₃ hr₁, mul_pos hr₂ hr₄, _⟩), rw [mul_smul, mul_smul, h₁, ← h₂, smul_comm] end /-- A vector is in the same ray as a nonnegative multiple of itself. -/ lemma _root_.same_ray_nonneg_smul_right (v : M) {r : R} (h : 0 ≤ r) : same_ray R v (r • v) := or.inr $ h.eq_or_lt.imp (λ h, h ▸ zero_smul R v) $ λ h, ⟨r, 1, h, by { nontriviality R, exact zero_lt_one }, (one_smul _ _).symm⟩ /-- A vector is in the same ray as a positive multiple of itself. -/ lemma _root_.same_ray_pos_smul_right (v : M) {r : R} (h : 0 < r) : same_ray R v (r • v) := same_ray_nonneg_smul_right v h.le /-- A vector is in the same ray as a nonnegative multiple of one it is in the same ray as. -/ lemma nonneg_smul_right {r : R} (h : same_ray R x y) (hr : 0 ≤ r) : same_ray R x (r • y) := h.trans (same_ray_nonneg_smul_right y hr) $ λ hy, or.inr $ by rw [hy, smul_zero] /-- A vector is in the same ray as a positive multiple of one it is in the same ray as. -/ lemma pos_smul_right {r : R} (h : same_ray R x y) (hr : 0 < r) : same_ray R x (r • y) := h.nonneg_smul_right hr.le /-- A nonnegative multiple of a vector is in the same ray as that vector. -/ lemma _root_.same_ray_nonneg_smul_left (v : M) {r : R} (h : 0 ≤ r) : same_ray R (r • v) v := (same_ray_nonneg_smul_right v h).symm /-- A positive multiple of a vector is in the same ray as that vector. -/ lemma _root_.same_ray_pos_smul_left (v : M) {r : R} (h : 0 < r) : same_ray R (r • v) v := same_ray_nonneg_smul_left v h.le /-- A nonnegative multiple of a vector is in the same ray as one it is in the same ray as. -/ lemma nonneg_smul_left {r : R} (h : same_ray R x y) (hr : 0 ≤ r) : same_ray R (r • x) y := (h.symm.nonneg_smul_right hr).symm /-- A positive multiple of a vector is in the same ray as one it is in the same ray as. -/ lemma pos_smul_left {r : R} (h : same_ray R x y) (hr : 0 < r) : same_ray R (r • x) y := h.nonneg_smul_left hr.le /-- If two vectors are on the same ray then they remain so after applying a linear map. -/ lemma map (f : M →ₗ[R] N) (h : same_ray R x y) : same_ray R (f x) (f y) := h.imp (λ hx, by rw [hx, map_zero]) $ or.imp (λ hy, by rw [hy, map_zero]) $ λ ⟨r₁, r₂, hr₁, hr₂, h⟩, ⟨r₁, r₂, hr₁, hr₂, by rw [←f.map_smul, ←f.map_smul, h]⟩ /-- The images of two vectors under an injective linear map are on the same ray if and only if the original vectors are on the same ray. -/ lemma _root_.function.injective.same_ray_map_iff {F : Type*} [linear_map_class F R M N] {f : F} (hf : function.injective f) : same_ray R (f x) (f y) ↔ same_ray R x y := by simp only [same_ray, map_zero, ← hf.eq_iff, map_smul] /-- The images of two vectors under a linear equivalence are on the same ray if and only if the original vectors are on the same ray. -/ @[simp] lemma _root_.same_ray_map_iff (e : M ≃ₗ[R] N) : same_ray R (e x) (e y) ↔ same_ray R x y := function.injective.same_ray_map_iff (equiv_like.injective e) /-- If two vectors are on the same ray then both scaled by the same action are also on the same ray. -/ lemma smul {S : Type*} [monoid S] [distrib_mul_action S M] [smul_comm_class R S M] (h : same_ray R x y) (s : S) : same_ray R (s • x) (s • y) := h.map (s • (linear_map.id : M →ₗ[R] M)) /-- If `x` and `y` are on the same ray as `z`, then so is `x + y`. -/ lemma add_left (hx : same_ray R x z) (hy : same_ray R y z) : same_ray R (x + y) z := begin rcases eq_or_ne x 0 with rfl|hx₀, { rwa zero_add }, rcases eq_or_ne y 0 with rfl|hy₀, { rwa add_zero }, rcases eq_or_ne z 0 with rfl|hz₀, { apply zero_right }, rcases hx.exists_pos hx₀ hz₀ with ⟨rx, rz₁, hrx, hrz₁, Hx⟩, rcases hy.exists_pos hy₀ hz₀ with ⟨ry, rz₂, hry, hrz₂, Hy⟩, refine or.inr (or.inr ⟨rx * ry, ry * rz₁ + rx * rz₂, mul_pos hrx hry, _, _⟩), { apply_rules [add_pos, mul_pos] }, { simp only [mul_smul, smul_add, add_smul, ← Hx, ← Hy], rw smul_comm } end /-- If `y` and `z` are on the same ray as `x`, then so is `y + z`. -/ lemma add_right (hy : same_ray R x y) (hz : same_ray R x z) : same_ray R x (y + z) := (hy.symm.add_left hz.symm).symm end same_ray /-- Nonzero vectors, as used to define rays. This type depends on an unused argument `R` so that `ray_vector.setoid` can be an instance. -/ @[nolint unused_arguments has_nonempty_instance] def ray_vector (R M : Type*) [has_zero M] := {v : M // v ≠ 0} instance ray_vector.has_coe {R M : Type*} [has_zero M] : has_coe (ray_vector R M) M := coe_subtype instance {R M : Type*} [has_zero M] [nontrivial M] : nonempty (ray_vector R M) := let ⟨x, hx⟩ := exists_ne (0 : M) in ⟨⟨x, hx⟩⟩ variables (R M) /-- The setoid of the `same_ray` relation for the subtype of nonzero vectors. -/ instance : setoid (ray_vector R M) := { r := λ x y, same_ray R (x : M) y, iseqv := ⟨λ x, same_ray.refl _, λ x y h, h.symm, λ x y z hxy hyz, hxy.trans hyz $ λ hy, (y.2 hy).elim⟩ } /-- A ray (equivalence class of nonzero vectors with common positive multiples) in a module. -/ @[nolint has_nonempty_instance] def module.ray := quotient (ray_vector.setoid R M) variables {R M} /-- Equivalence of nonzero vectors, in terms of same_ray. -/ lemma equiv_iff_same_ray {v₁ v₂ : ray_vector R M} : v₁ ≈ v₂ ↔ same_ray R (v₁ : M) v₂ := iff.rfl variables (R) /-- The ray given by a nonzero vector. -/ protected def ray_of_ne_zero (v : M) (h : v ≠ 0) : module.ray R M := ⟦⟨v, h⟩⟧ /-- An induction principle for `module.ray`, used as `induction x using module.ray.ind`. -/ lemma module.ray.ind {C : module.ray R M → Prop} (h : ∀ v (hv : v ≠ 0), C (ray_of_ne_zero R v hv)) (x : module.ray R M) : C x := quotient.ind (subtype.rec $ by exact h) x variable {R} instance [nontrivial M] : nonempty (module.ray R M) := nonempty.map quotient.mk infer_instance /-- The rays given by two nonzero vectors are equal if and only if those vectors satisfy `same_ray`. -/ lemma ray_eq_iff {v₁ v₂ : M} (hv₁ : v₁ ≠ 0) (hv₂ : v₂ ≠ 0) : ray_of_ne_zero R _ hv₁ = ray_of_ne_zero R _ hv₂ ↔ same_ray R v₁ v₂ := quotient.eq /-- The ray given by a positive multiple of a nonzero vector. -/ @[simp] lemma ray_pos_smul {v : M} (h : v ≠ 0) {r : R} (hr : 0 < r) (hrv : r • v ≠ 0) : ray_of_ne_zero R (r • v) hrv = ray_of_ne_zero R v h := (ray_eq_iff _ _).2 $ same_ray_pos_smul_left v hr /-- An equivalence between modules implies an equivalence between ray vectors. -/ def ray_vector.map_linear_equiv (e : M ≃ₗ[R] N) : ray_vector R M ≃ ray_vector R N := equiv.subtype_equiv e.to_equiv $ λ _, e.map_ne_zero_iff.symm /-- An equivalence between modules implies an equivalence between rays. -/ def module.ray.map (e : M ≃ₗ[R] N) : module.ray R M ≃ module.ray R N := quotient.congr (ray_vector.map_linear_equiv e) $ λ ⟨a, ha⟩ ⟨b, hb⟩, (same_ray_map_iff _).symm @[simp] lemma module.ray.map_apply (e : M ≃ₗ[R] N) (v : M) (hv : v ≠ 0) : module.ray.map e (ray_of_ne_zero _ v hv) = ray_of_ne_zero _ (e v) (e.map_ne_zero_iff.2 hv) := rfl @[simp] lemma module.ray.map_refl : (module.ray.map $ linear_equiv.refl R M) = equiv.refl _ := equiv.ext $ module.ray.ind R $ λ _ _, rfl @[simp] lemma module.ray.map_symm (e : M ≃ₗ[R] N) : (module.ray.map e).symm = module.ray.map e.symm := rfl section action variables {G : Type*} [group G] [distrib_mul_action G M] /-- Any invertible action preserves the non-zeroness of ray vectors. This is primarily of interest when `G = Rˣ` -/ instance {R : Type*} : mul_action G (ray_vector R M) := { smul := λ r, (subtype.map ((•) r) $ λ a, (smul_ne_zero_iff_ne _).2), mul_smul := λ a b m, subtype.ext $ mul_smul a b _, one_smul := λ m, subtype.ext $ one_smul _ _ } variables [smul_comm_class R G M] /-- Any invertible action preserves the non-zeroness of rays. This is primarily of interest when `G = Rˣ` -/ instance : mul_action G (module.ray R M) := { smul := λ r, quotient.map ((•) r) (λ a b h, h.smul _), mul_smul := λ a b, quotient.ind $ by exact(λ m, congr_arg quotient.mk $ mul_smul a b _), one_smul := quotient.ind $ by exact (λ m, congr_arg quotient.mk $ one_smul _ _), } /-- The action via `linear_equiv.apply_distrib_mul_action` corresponds to `module.ray.map`. -/ @[simp] lemma module.ray.linear_equiv_smul_eq_map (e : M ≃ₗ[R] M) (v : module.ray R M) : e • v = module.ray.map e v := rfl @[simp] lemma smul_ray_of_ne_zero (g : G) (v : M) (hv) : g • ray_of_ne_zero R v hv = ray_of_ne_zero R (g • v) ((smul_ne_zero_iff_ne _).2 hv) := rfl end action namespace module.ray /-- Scaling by a positive unit is a no-op. -/ lemma units_smul_of_pos (u : Rˣ) (hu : 0 < (u : R)) (v : module.ray R M) : u • v = v := begin induction v using module.ray.ind, rw [smul_ray_of_ne_zero, ray_eq_iff], exact same_ray_pos_smul_left _ hu end /-- An arbitrary `ray_vector` giving a ray. -/ def some_ray_vector (x : module.ray R M) : ray_vector R M := quotient.out x /-- The ray of `some_ray_vector`. -/ @[simp] lemma some_ray_vector_ray (x : module.ray R M) : (⟦x.some_ray_vector⟧ : module.ray R M) = x := quotient.out_eq _ /-- An arbitrary nonzero vector giving a ray. -/ def some_vector (x : module.ray R M) : M := x.some_ray_vector /-- `some_vector` is nonzero. -/ @[simp] lemma some_vector_ne_zero (x : module.ray R M) : x.some_vector ≠ 0 := x.some_ray_vector.property /-- The ray of `some_vector`. -/ @[simp] lemma some_vector_ray (x : module.ray R M) : ray_of_ne_zero R _ x.some_vector_ne_zero = x := (congr_arg _ (subtype.coe_eta _ _) : _).trans x.out_eq end module.ray end strict_ordered_comm_semiring section strict_ordered_comm_ring variables {R : Type*} [strict_ordered_comm_ring R] variables {M N : Type*} [add_comm_group M] [add_comm_group N] [module R M] [module R N] {x y : M} /-- `same_ray.neg` as an `iff`. -/ @[simp] lemma same_ray_neg_iff : same_ray R (-x) (-y) ↔ same_ray R x y := by simp only [same_ray, neg_eq_zero, smul_neg, neg_inj] alias same_ray_neg_iff ↔ same_ray.of_neg same_ray.neg lemma same_ray_neg_swap : same_ray R (-x) y ↔ same_ray R x (-y) := by rw [← same_ray_neg_iff, neg_neg] lemma eq_zero_of_same_ray_neg_smul_right [no_zero_smul_divisors R M] {r : R} (hr : r < 0) (h : same_ray R x (r • x)) : x = 0 := begin rcases h with rfl|h₀|⟨r₁, r₂, hr₁, hr₂, h⟩, { refl }, { simpa [hr.ne] using h₀ }, { rw [← sub_eq_zero, smul_smul, ← sub_smul, smul_eq_zero] at h, refine h.resolve_left (ne_of_gt $ sub_pos.2 _), exact (mul_neg_of_pos_of_neg hr₂ hr).trans hr₁ } end /-- If a vector is in the same ray as its negation, that vector is zero. -/ lemma eq_zero_of_same_ray_self_neg [no_zero_smul_divisors R M] (h : same_ray R x (-x)) : x = 0 := begin nontriviality M, haveI : nontrivial R := module.nontrivial R M, refine eq_zero_of_same_ray_neg_smul_right (neg_lt_zero.2 (zero_lt_one' R)) _, rwa [neg_one_smul] end namespace ray_vector /-- Negating a nonzero vector. -/ instance {R : Type*} : has_neg (ray_vector R M) := ⟨λ v, ⟨-v, neg_ne_zero.2 v.prop⟩⟩ /-- Negating a nonzero vector commutes with coercion to the underlying module. -/ @[simp, norm_cast] lemma coe_neg {R : Type*} (v : ray_vector R M) : ↑(-v) = -(v : M) := rfl /-- Negating a nonzero vector twice produces the original vector. -/ instance {R : Type*} : has_involutive_neg (ray_vector R M) := { neg := has_neg.neg, neg_neg := λ v, by rw [subtype.ext_iff, coe_neg, coe_neg, neg_neg] } /-- If two nonzero vectors are equivalent, so are their negations. -/ @[simp] lemma equiv_neg_iff {v₁ v₂ : ray_vector R M} : -v₁ ≈ -v₂ ↔ v₁ ≈ v₂ := same_ray_neg_iff end ray_vector variables (R) /-- Negating a ray. -/ instance : has_neg (module.ray R M) := ⟨quotient.map (λ v, -v) (λ v₁ v₂, ray_vector.equiv_neg_iff.2)⟩ /-- The ray given by the negation of a nonzero vector. -/ @[simp] lemma neg_ray_of_ne_zero (v : M) (h : v ≠ 0) : -(ray_of_ne_zero R _ h) = ray_of_ne_zero R (-v) (neg_ne_zero.2 h) := rfl namespace module.ray variables {R} /-- Negating a ray twice produces the original ray. -/ instance : has_involutive_neg (module.ray R M) := { neg := has_neg.neg, neg_neg := λ x, quotient.ind (λ a, congr_arg quotient.mk $ neg_neg _) x } variables {R M} /-- A ray does not equal its own negation. -/ lemma ne_neg_self [no_zero_smul_divisors R M] (x : module.ray R M) : x ≠ -x := begin induction x using module.ray.ind with x hx, rw [neg_ray_of_ne_zero, ne.def, ray_eq_iff], exact mt eq_zero_of_same_ray_self_neg hx end lemma neg_units_smul (u : Rˣ) (v : module.ray R M) : (-u) • v = - (u • v) := begin induction v using module.ray.ind, simp only [smul_ray_of_ne_zero, units.smul_def, units.coe_neg, neg_smul, neg_ray_of_ne_zero] end /-- Scaling by a negative unit is negation. -/ lemma units_smul_of_neg (u : Rˣ) (hu : (u : R) < 0) (v : module.ray R M) : u • v = -v := begin rw [← neg_inj, neg_neg, ← neg_units_smul, units_smul_of_pos], rwa [units.coe_neg, right.neg_pos_iff] end @[simp] protected lemma map_neg (f : M ≃ₗ[R] N) (v : module.ray R M) : map f (-v) = - map f v := begin induction v using module.ray.ind with g hg, simp, end end module.ray end strict_ordered_comm_ring section linear_ordered_comm_ring variables {R : Type*} [linear_ordered_comm_ring R] variables {M : Type*} [add_comm_group M] [module R M] /-- `same_ray` follows from membership of `mul_action.orbit` for the `units.pos_subgroup`. -/ lemma same_ray_of_mem_orbit {v₁ v₂ : M} (h : v₁ ∈ mul_action.orbit (units.pos_subgroup R) v₂) : same_ray R v₁ v₂ := begin rcases h with ⟨⟨r, hr : 0 < (r : R)⟩, (rfl : r • v₂ = v₁)⟩, exact same_ray_pos_smul_left _ hr end /-- Scaling by an inverse unit is the same as scaling by itself. -/ @[simp] lemma units_inv_smul (u : Rˣ) (v : module.ray R M) : u⁻¹ • v = u • v := calc u⁻¹ • v = (u * u) • u⁻¹ • v : eq.symm $ (u⁻¹ • v).units_smul_of_pos _ $ mul_self_pos.2 u.ne_zero ... = u • v : by rw [mul_smul, smul_inv_smul] section variables [no_zero_smul_divisors R M] @[simp] lemma same_ray_smul_right_iff {v : M} {r : R} : same_ray R v (r • v) ↔ 0 ≤ r ∨ v = 0 := ⟨λ hrv, or_iff_not_imp_left.2 $ λ hr, eq_zero_of_same_ray_neg_smul_right (not_le.1 hr) hrv, or_imp_distrib.2 ⟨same_ray_nonneg_smul_right v, λ h, h.symm ▸ same_ray.zero_left _⟩⟩ /-- A nonzero vector is in the same ray as a multiple of itself if and only if that multiple is positive. -/ lemma same_ray_smul_right_iff_of_ne {v : M} (hv : v ≠ 0) {r : R} (hr : r ≠ 0) : same_ray R v (r • v) ↔ 0 < r := by simp only [same_ray_smul_right_iff, hv, or_false, hr.symm.le_iff_lt] @[simp] lemma same_ray_smul_left_iff {v : M} {r : R} : same_ray R (r • v) v ↔ 0 ≤ r ∨ v = 0 := same_ray_comm.trans same_ray_smul_right_iff /-- A multiple of a nonzero vector is in the same ray as that vector if and only if that multiple is positive. -/ lemma same_ray_smul_left_iff_of_ne {v : M} (hv : v ≠ 0) {r : R} (hr : r ≠ 0) : same_ray R (r • v) v ↔ 0 < r := same_ray_comm.trans (same_ray_smul_right_iff_of_ne hv hr) @[simp] lemma same_ray_neg_smul_right_iff {v : M} {r : R} : same_ray R (-v) (r • v) ↔ r ≤ 0 ∨ v = 0 := by rw [← same_ray_neg_iff, neg_neg, ← neg_smul, same_ray_smul_right_iff, neg_nonneg] lemma same_ray_neg_smul_right_iff_of_ne {v : M} {r : R} (hv : v ≠ 0) (hr : r ≠ 0) : same_ray R (-v) (r • v) ↔ r < 0 := by simp only [same_ray_neg_smul_right_iff, hv, or_false, hr.le_iff_lt] @[simp] lemma same_ray_neg_smul_left_iff {v : M} {r : R} : same_ray R (r • v) (-v) ↔ r ≤ 0 ∨ v = 0 := same_ray_comm.trans same_ray_neg_smul_right_iff lemma same_ray_neg_smul_left_iff_of_ne {v : M} {r : R} (hv : v ≠ 0) (hr : r ≠ 0) : same_ray R (r • v) (-v) ↔ r < 0 := same_ray_comm.trans $ same_ray_neg_smul_right_iff_of_ne hv hr @[simp] lemma units_smul_eq_self_iff {u : Rˣ} {v : module.ray R M} : u • v = v ↔ (0 : R) < u := begin induction v using module.ray.ind with v hv, simp only [smul_ray_of_ne_zero, ray_eq_iff, units.smul_def, same_ray_smul_left_iff_of_ne hv u.ne_zero] end @[simp] lemma units_smul_eq_neg_iff {u : Rˣ} {v : module.ray R M} : u • v = -v ↔ ↑u < (0 : R) := by rw [← neg_inj, neg_neg, ← module.ray.neg_units_smul, units_smul_eq_self_iff, units.coe_neg, neg_pos] /-- Two vectors are in the same ray, or the first is in the same ray as the negation of the second, if and only if they are not linearly independent. -/ lemma same_ray_or_same_ray_neg_iff_not_linear_independent {x y : M} : (same_ray R x y ∨ same_ray R x (-y)) ↔ ¬ linear_independent R ![x, y] := begin by_cases hx : x = 0, { simp [hx, λ h : linear_independent R ![0, y], h.ne_zero 0 rfl] }, by_cases hy : y = 0, { simp [hy, λ h : linear_independent R ![x, 0], h.ne_zero 1 rfl] }, simp_rw [fintype.not_linear_independent_iff, fin.sum_univ_two, fin.exists_fin_two], refine ⟨λ h, _, λ h, _⟩, { rcases h with (hx0|hy0|⟨r₁, r₂, hr₁, hr₂, h⟩)|(hx0|hy0|⟨r₁, r₂, hr₁, hr₂, h⟩), { exact false.elim (hx hx0) }, { exact false.elim (hy hy0) }, { refine ⟨![r₁, -r₂], _⟩, simp [h, hr₁.ne.symm] }, { exact false.elim (hx hx0) }, { exact false.elim (hy (neg_eq_zero.1 hy0)) }, { refine ⟨![r₁, r₂], _⟩, simp [h, hr₁.ne.symm] } }, { rcases h with ⟨m, hm, hmne⟩, change m 0 • x + m 1 • y = 0 at hm, rw add_eq_zero_iff_eq_neg at hm, rcases lt_trichotomy (m 0) 0 with hm0|hm0|hm0; rcases lt_trichotomy (m 1) 0 with hm1|hm1|hm1, { refine or.inr (or.inr (or.inr ⟨-(m 0), -(m 1), left.neg_pos_iff.2 hm0, left.neg_pos_iff.2 hm1, _⟩)), simp [hm] }, { exfalso, simpa [hm1, hx, hm0.ne] using hm }, { refine or.inl (or.inr (or.inr ⟨-(m 0), m 1, left.neg_pos_iff.2 hm0, hm1, _⟩)), simp [hm] }, { exfalso, simpa [hm0, hy, hm1.ne] using hm }, { refine false.elim (not_and_distrib.2 hmne ⟨hm0, hm1⟩) }, { exfalso, simpa [hm0, hy, hm1.ne.symm] using hm }, { refine or.inl (or.inr (or.inr ⟨m 0, -(m 1), hm0, left.neg_pos_iff.2 hm1, _⟩)), simp [hm] }, { exfalso, simpa [hm1, hx, hm0.ne.symm] using hm }, { refine or.inr (or.inr (or.inr ⟨m 0, m 1, hm0, hm1, _⟩)), simp [hm] } } end /-- Two vectors are in the same ray, or they are nonzero and the first is in the same ray as the negation of the second, if and only if they are not linearly independent. -/ lemma same_ray_or_ne_zero_and_same_ray_neg_iff_not_linear_independent {x y : M} : (same_ray R x y ∨ x ≠ 0 ∧ y ≠ 0 ∧ same_ray R x (-y)) ↔ ¬ linear_independent R ![x, y] := begin rw ←same_ray_or_same_ray_neg_iff_not_linear_independent, by_cases hx : x = 0, { simp [hx] }, by_cases hy : y = 0; simp [hx, hy] end end end linear_ordered_comm_ring namespace same_ray variables {R : Type*} [linear_ordered_field R] variables {M : Type*} [add_comm_group M] [module R M] {x y v₁ v₂ : M} lemma exists_pos_left (h : same_ray R x y) (hx : x ≠ 0) (hy : y ≠ 0) : ∃ r : R, 0 < r ∧ r • x = y := let ⟨r₁, r₂, hr₁, hr₂, h⟩ := h.exists_pos hx hy in ⟨r₂⁻¹ * r₁, mul_pos (inv_pos.2 hr₂) hr₁, by rw [mul_smul, h, inv_smul_smul₀ hr₂.ne']⟩ lemma exists_pos_right (h : same_ray R x y) (hx : x ≠ 0) (hy : y ≠ 0) : ∃ r : R, 0 < r ∧ x = r • y := (h.symm.exists_pos_left hy hx).imp $ λ _, and.imp_right eq.symm /-- If a vector `v₂` is on the same ray as a nonzero vector `v₁`, then it is equal to `c • v₁` for some nonnegative `c`. -/ lemma exists_nonneg_left (h : same_ray R x y) (hx : x ≠ 0) : ∃ r : R, 0 ≤ r ∧ r • x = y := begin obtain rfl | hy := eq_or_ne y 0, { exact ⟨0, le_rfl, zero_smul _ _⟩ }, { exact (h.exists_pos_left hx hy).imp (λ _, and.imp_left le_of_lt) } end /-- If a vector `v₁` is on the same ray as a nonzero vector `v₂`, then it is equal to `c • v₂` for some nonnegative `c`. -/ lemma exists_nonneg_right (h : same_ray R x y) (hy : y ≠ 0) : ∃ r : R, 0 ≤ r ∧ x = r • y := (h.symm.exists_nonneg_left hy).imp $ λ _, and.imp_right eq.symm /-- If vectors `v₁` and `v₂` are on the same ray, then for some nonnegative `a b`, `a + b = 1`, we have `v₁ = a • (v₁ + v₂)` and `v₂ = b • (v₁ + v₂)`. -/ lemma exists_eq_smul_add (h : same_ray R v₁ v₂) : ∃ a b : R, 0 ≤ a ∧ 0 ≤ b ∧ a + b = 1 ∧ v₁ = a • (v₁ + v₂) ∧ v₂ = b • (v₁ + v₂) := begin rcases h with rfl|rfl|⟨r₁, r₂, h₁, h₂, H⟩, { use [0, 1], simp }, { use [1, 0], simp }, { have h₁₂ : 0 < r₁ + r₂, from add_pos h₁ h₂, refine ⟨r₂ / (r₁ + r₂), r₁ / (r₁ + r₂), div_nonneg h₂.le h₁₂.le, div_nonneg h₁.le h₁₂.le, _, _, _⟩, { rw [← add_div, add_comm, div_self h₁₂.ne'] }, { rw [div_eq_inv_mul, mul_smul, smul_add, ← H, ← add_smul, add_comm r₂, inv_smul_smul₀ h₁₂.ne'] }, { rw [div_eq_inv_mul, mul_smul, smul_add, H, ← add_smul, add_comm r₂, inv_smul_smul₀ h₁₂.ne'] } } end /-- If vectors `v₁` and `v₂` are on the same ray, then they are nonnegative multiples of the same vector. Actually, this vector can be assumed to be `v₁ + v₂`, see `same_ray.exists_eq_smul_add`. -/ lemma exists_eq_smul (h : same_ray R v₁ v₂) : ∃ (u : M) (a b : R), 0 ≤ a ∧ 0 ≤ b ∧ a + b = 1 ∧ v₁ = a • u ∧ v₂ = b • u := ⟨v₁ + v₂, h.exists_eq_smul_add⟩ end same_ray section linear_ordered_field variables {R : Type*} [linear_ordered_field R] variables {M : Type*} [add_comm_group M] [module R M] {x y : M} lemma exists_pos_left_iff_same_ray (hx : x ≠ 0) (hy : y ≠ 0) : (∃ r : R, 0 < r ∧ r • x = y) ↔ same_ray R x y := begin refine ⟨λ h, _, λ h, h.exists_pos_left hx hy⟩, rcases h with ⟨r, hr, rfl⟩, exact same_ray_pos_smul_right x hr end lemma exists_pos_left_iff_same_ray_and_ne_zero (hx : x ≠ 0) : (∃ r : R, 0 < r ∧ r • x = y) ↔ (same_ray R x y ∧ y ≠ 0) := begin split, { rintro ⟨r, hr, rfl⟩, simp [hx, hr.le, hr.ne'] }, { rintro ⟨hxy, hy⟩, exact (exists_pos_left_iff_same_ray hx hy).2 hxy } end lemma exists_nonneg_left_iff_same_ray (hx : x ≠ 0) : (∃ r : R, 0 ≤ r ∧ r • x = y) ↔ same_ray R x y := begin refine ⟨λ h, _, λ h, h.exists_nonneg_left hx⟩, rcases h with ⟨r, hr, rfl⟩, exact same_ray_nonneg_smul_right x hr end lemma exists_pos_right_iff_same_ray (hx : x ≠ 0) (hy : y ≠ 0) : (∃ r : R, 0 < r ∧ x = r • y) ↔ same_ray R x y := by simpa only [same_ray_comm, eq_comm] using exists_pos_left_iff_same_ray hy hx lemma exists_pos_right_iff_same_ray_and_ne_zero (hy : y ≠ 0) : (∃ r : R, 0 < r ∧ x = r • y) ↔ (same_ray R x y ∧ x ≠ 0) := by simpa only [same_ray_comm, eq_comm] using exists_pos_left_iff_same_ray_and_ne_zero hy lemma exists_nonneg_right_iff_same_ray (hy : y ≠ 0) : (∃ r : R, 0 ≤ r ∧ x = r • y) ↔ same_ray R x y := by simpa only [same_ray_comm, eq_comm] using exists_nonneg_left_iff_same_ray hy end linear_ordered_field
ef79e854f46256793dbd0d08bf00edb11596926a
6b45072eb2b3db3ecaace2a7a0241ce81f815787
/algebra/lattice/fixed_points.lean
538e4a77faf9ad353b48c6b626f57a87873dc429
[]
no_license
avigad/library_dev
27b47257382667b5eb7e6476c4f5b0d685dd3ddc
9d8ac7c7798ca550874e90fed585caad030bbfac
refs/heads/master
1,610,452,468,791
1,500,712,839,000
1,500,713,478,000
69,311,142
1
0
null
1,474,942,903,000
1,474,942,902,000
null
UTF-8
Lean
false
false
4,457
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 Fixed point construction on complete lattices. -/ import .complete_lattice universes u v w variables {α : Type u} {β : Type v} {γ : Type w} lemma ge_of_eq [weak_order α] {a b : α} : a = b → a ≥ b := λ h, h ▸ le_refl a namespace lattice section fixedpoint variables [complete_lattice α] {f : α → α} def lfp (f : α → α) : α := Inf {a | f a ≤ a} def gfp (f : α → α) : α := Sup {a | a ≤ f a} lemma lfp_le {a : α} (h : f a ≤ a) : lfp f ≤ a := Inf_le h lemma le_lfp {a : α} (h : ∀b, f b ≤ b → a ≤ b) : a ≤ lfp f := le_Inf h lemma lfp_eq (m : monotone f) : lfp f = f (lfp f) := have f (lfp f) ≤ lfp f, from le_lfp $ assume b, assume h : f b ≤ b, le_trans (m (lfp_le h)) h, le_antisymm (lfp_le (m this)) this lemma lfp_induct {p : α → Prop} (m : monotone f) (step : ∀a, p a → a ≤ lfp f → p (f a)) (sup : ∀s, (∀a∈s, p a) → p (Sup s)) : p (lfp f) := let s := {a | a ≤ lfp f ∧ p a} in have p_s : p (Sup s), from sup s (assume a ⟨_, h⟩, h), have Sup s ≤ lfp f, from le_lfp $ assume a, assume h : f a ≤ a, Sup_le $ assume b ⟨b_le, _⟩, le_trans b_le (lfp_le h), have Sup s = lfp f, from le_antisymm this $ lfp_le $ le_Sup ⟨le_trans (m this) $ ge_of_eq $ lfp_eq m, step _ p_s this⟩, this ▸ p_s lemma monotone_lfp : monotone (@lfp α _) := assume f g, assume : f ≤ g, le_lfp $ assume a, assume : g a ≤ a, lfp_le $ le_trans (‹f ≤ g› a) this lemma le_gfp {a : α} (h : a ≤ f a) : a ≤ gfp f := le_Sup h lemma gfp_le {a : α} (h : ∀b, b ≤ f b → b ≤ a) : gfp f ≤ a := Sup_le h lemma gfp_eq (m : monotone f) : gfp f = f (gfp f) := have gfp f ≤ f (gfp f), from gfp_le $ assume b, assume h : b ≤ f b, le_trans h (m (le_gfp h)), le_antisymm this (le_gfp (m this)) lemma gfp_induct {p : α → Prop} (m : monotone f) (step : ∀a, p a → gfp f ≤ a → p (f a)) (inf : ∀s, (∀a∈s, p a) → p (Inf s)) : p (gfp f) := let s := {a | gfp f ≤ a ∧ p a} in have p_s : p (Inf s), from inf s (assume a ⟨_, h⟩, h), have gfp f ≤ Inf s, from gfp_le $ assume a, assume h : a ≤ f a, le_Inf $ assume b ⟨le_b, _⟩, le_trans (le_gfp h) le_b, have Inf s = gfp f, from le_antisymm (le_gfp $ Inf_le ⟨le_trans (le_of_eq $ gfp_eq m) (m this), step _ p_s this⟩) this, this ▸ p_s lemma monotone_gfp : monotone (@gfp α _) := assume f g, assume : f ≤ g, gfp_le $ assume a, assume : a ≤ f a, le_gfp $ le_trans this (‹f ≤ g› a) end fixedpoint section fixedpoint_eqn variables [complete_lattice α] [complete_lattice β] {f : β → α} {g : α → β} -- Rolling rule lemma lfp_comp (m_f : monotone f) (m_g : monotone g) : lfp (f ∘ g) = f (lfp (g ∘ f)) := le_antisymm (lfp_le $ m_f $ ge_of_eq $ lfp_eq $ monotone_comp m_f m_g) (le_lfp $ assume a fg_le, le_trans (m_f $ lfp_le $ show (g ∘ f) (g a) ≤ g a, from m_g fg_le) fg_le) lemma gfp_comp (m_f : monotone f) (m_g : monotone g) : gfp (f ∘ g) = f (gfp (g ∘ f)) := le_antisymm (gfp_le $ assume a fg_le, le_trans fg_le $ m_f $ le_gfp $ show g a ≤ (g ∘ f) (g a), from m_g fg_le) (le_gfp $ m_f $ le_of_eq $ gfp_eq $ monotone_comp m_f m_g) -- Diagonal rule lemma lfp_lfp {h : α → α → α} (m : ∀⦃a b c d⦄, a ≤ b → c ≤ d → h a c ≤ h b d) : lfp (lfp ∘ h) = lfp (λx, h x x) := let f := lfp (lfp ∘ h) in have f_eq : f = lfp (h f), from lfp_eq $ monotone_comp (assume a b h x, m h (le_refl _)) monotone_lfp, le_antisymm (lfp_le $ lfp_le $ ge_of_eq $ lfp_eq $ assume a b h, m h h) (lfp_le $ ge_of_eq $ calc f = lfp (h f) : f_eq ... = h f (lfp (h f)) : lfp_eq $ assume a b h, m (le_refl _) h ... = h f f : congr_arg (h f) f_eq^.symm) lemma gfp_gfp {h : α → α → α} (m : ∀⦃a b c d⦄, a ≤ b → c ≤ d → h a c ≤ h b d) : gfp (gfp ∘ h) = gfp (λx, h x x) := let f := gfp (gfp ∘ h) in have f_eq : f = gfp (h f), from gfp_eq $ monotone_comp (assume a b h x, m h (le_refl _)) monotone_gfp, le_antisymm (le_gfp $ le_of_eq $ calc f = gfp (h f) : f_eq ... = h f (gfp (h f)) : gfp_eq $ assume a b h, m (le_refl _) h ... = h f f : congr_arg (h f) f_eq^.symm) (le_gfp $ le_gfp $ le_of_eq $ gfp_eq $ assume a b h, m h h) end fixedpoint_eqn end lattice
45a86a29083f753cad1c7b73c3a7442e91e23c8a
a1179fa077c09acc49e4fbc8d67084ba89ac4f4c
/tutorials/src/solutions/00_first_proofs.lean
1fb0fb3bf2e4609d079480a0b5e8cf9d0e388f85
[]
no_license
Seeram/Lean-proof-assistant
11ca0ca0e0446bacdd1773c4c481a3653b2f1074
e672d46e0e5f39d8de2933ad4f4cac095ca6094f
refs/heads/master
1,682,754,224,366
1,620,959,431,000
1,620,959,431,000
299,000,950
0
1
null
1,620,680,462,000
1,601,200,258,000
Lean
UTF-8
Lean
false
false
18,055
lean
/- This file is intended for Lean beginners. The goal is to demonstrate what it feels like to prove things using Lean and mathlib. Complicated definitions and theory building are not covered. Everything is covered again more slowly and with exercises in the next files. -/ -- We want real numbers and their basic properties import data.real.basic -- We want to be able to use Lean's built-in "help" functionality import tactic.suggest -- We want to be able to define functions using the law of excluded middle noncomputable theory open_locale classical /- Our first goal is to define the set of upper bounds of a set of real numbers. This is already defined in mathlib (in a more general context), but we repeat it for the sake of exposition. Right-click "upper_bounds" below to get offered to jump to mathlib's version -/ #check upper_bounds /-- The set of upper bounds of a set of real numbers ℝ -/ def up_bounds (A : set ℝ) := { x : ℝ | ∀ a ∈ A, a ≤ x} /-- Predicate `is_max a A` means `a` is a maximum of `A` -/ def is_max (a : ℝ) (A : set ℝ) := a ∈ A ∧ a ∈ up_bounds A /- In the above definition, the symbol `∧` means "and". We also see the most visible difference between set theoretic foundations and type theoretic ones (used by almost all proof assistants). In set theory, everything is a set, and the only relation you get from foundations are `=` and `∈`. In type theory, there is a meta-theoretic relation of "typing": `a : ℝ` reads "`a` is a real number" or, more precisely, "the type of `a` is `ℝ`". Here "meta-theoretic" means this is not a statement you can prove or disprove inside the theory, it's a fact that is true or not. Here we impose this fact, in other circumstances, it would be checked by the Lean kernel. By contrast, `a ∈ A` is a statement inside the theory. Here it's part of the definition, in other circumstances it could be something proven inside Lean. -/ /- For illustrative purposes, we now define an infix version of the above predicate. It will allow us to write `a is_a_max_of A`, which is closer to a sentence. -/ infix ` is_a_max_of `:55 := is_max /- Let's prove something now! A set of real numbers has at most one maximum. Here everything left of the final `:` is introducing the objects and assumption. The equality `x = y` right of the colon is the conclusion. -/ lemma unique_max (A : set ℝ) (x y : ℝ) (hx : x is_a_max_of A) (hy : y is_a_max_of A) : x = y := begin -- We first break our assumptions in their two constituent pieces. -- We are free to choose the name following `with` cases hx with x_in x_up, cases hy with y_in y_up, -- Assumption `x_up` means x isn't less than elements of A, let's apply this to y specialize x_up y, -- Assumption `x_up` now needs the information that `y` is indeed in `A`. specialize x_up y_in, -- Let's do this quicker with roles swapped specialize y_up x x_in, -- We explained to Lean the idea of this proof. -- Now we know `x ≤ y` and `y ≤ x`, and Lean shouldn't need more help. -- `linarith` proves equalities and inequalities that follow linearly from -- the assumption we have. linarith, end /- The above proof is too long, even if you remove comments. We don't really need the unpacking steps at the beginning; we can access both parts of the assumption `hx : x is_a_max_of A` using shortcuts `h.1` and `h.2`. We can also improve readability without assistance from the tactic state display, clearly announcing intermediate goals using `have`. This way we get to the following version of the same proof. -/ example (A : set ℝ) (x y : ℝ) (hx : x is_a_max_of A) (hy : y is_a_max_of A) : x = y := begin have : x ≤ y, from hy.2 x hx.1, have : y ≤ x, from hx.2 y hy.1, linarith, end /- Notice how mathematics based on type theory treats the assumption `∀ a ∈ A, a ≤ y` as a function turning an element `a` of `A` into the statement `a ≤ y`. More precisely, this assumption is the abbreviation of `∀ a : ℝ, a ∈ A → a ≤ y`. The expression `hy.2 x` appearing in the above proof is then the statement `x ∈ A → x ≤ y`, which itself is a function turning a statement `x ∈ A` into `x ≤ y` so that the full expression `hy.2 x hx.1` is indeed a proof of `x ≤ y`. One could argue a three-line-long proof of this lemma is still two lines too long. This is debatable, but mathlib's style is to write very short proofs for trivial lemmas. Those proofs are not easy to read but they are meant to indicate that the proof is probably not worth reading. In order to reach this stage, we need to know what `linarith` did for us. It invoked the lemma `le_antisymm` which says: `x ≤ y → y ≤ x → x = y`. This arrow, which is used both for function and implication, is right associative. So the statement is `x ≤ y → (y ≤ x → x = y)` which reads: I will send a proof `p` of `x ≤ y` to a function sending a proof `q'` of `y ≤ x` to a proof of `x = y`. Hence `le_antisymm p q'` is a proof of `x = y`. Using this we can get our one-line proof: -/ example (A : set ℝ) (x y : ℝ) (hx : x is_a_max_of A) (hy : y is_a_max_of A) : x = y := le_antisymm (hy.2 x hx.1) (hx.2 y hy.1) /- Such a proof is called a proof term (or a "term mode" proof). Notice it has no `begin` and `end`. It is directly the kind of low level proof that the Lean kernel is consuming. Commands like `cases`, `specialize` or `linarith` are called tactics, they help users constructing proof terms that could be very tedious to write directly. The most efficient proof style combines tactics with proof terms like our previous `have : x ≤ y, from hy.2 x hx.1` where `hy.2 x hx.1` is a proof term embeded inside a tactic mode proof. In the remaining of this file, we'll be characterizing infima of sets of real numbers in term of sequences. -/ /-- The set of lower bounds of a set of real numbers ℝ -/ def low_bounds (A : set ℝ) := { x : ℝ | ∀ a ∈ A, x ≤ a} /- We now define `a` is an infimum of `A`. Again there is already a more general version in mathlib. -/ def is_inf (x : ℝ) (A : set ℝ) := x is_a_max_of (low_bounds A) infix ` is_an_inf_of `:55 := is_inf /- We need to prove that any number which is greater than the infimum of A is greater than some element of A. -/ lemma inf_lt {A : set ℝ} {x : ℝ} (hx : x is_an_inf_of A) : ∀ y, x < y → ∃ a ∈ A, a < y := begin -- Let `y` be any real number. intro y, -- Let's prove the contrapositive contrapose, -- The symbol `¬` means negation. Let's ask Lean to rewrite the goal without negation, -- pushing negation through quantifiers and inequalities push_neg, -- Let's assume the premise, calling the assumption `h` intro h, -- `h` is exactly saying `y` is a lower bound of `A` so the second part of -- the infimum assumption `hx` applied to `y` and `h` is exactly what we want. exact hx.2 y h end /- In the above proof, the sequence `contrapose, push_neg` is so common that it can be abbreviated to `contrapose!`. With these commands, we enter the gray zone between proof checking and proof finding. Practical computer proof checking crucially needs the computer to handle tedious proof steps. In the next proof, we'll start using `linarith` a bit more seriously, going one step further into automation. Our next real goal is to prove inequalities for limits of sequences. We extract the following lemma: if `y ≤ x + ε` for all positive `ε` then `y ≤ x`. -/ lemma le_of_le_add_eps {x y : ℝ} : (∀ ε > 0, y ≤ x + ε) → y ≤ x := begin -- Let's prove the contrapositive, asking Lean to push negations right away. contrapose!, -- Assume `h : x < y`. intro h, -- We need to find `ε` such that `ε` is positive and `x + ε < y`. -- Let's use `(y-x)/2` use ((y-x)/2), -- we now have two properties to prove. Let's do both in turn, using `linarith` split, linarith, linarith, end /- Note how `linarith` was used for both sub-goals at the end of the above proof. We could have shortened that using the semi-colon combinator instead of comma, writing `split ; linarith`. Next we will study a compressed version of that proof: -/ example {x y : ℝ} : (∀ ε > 0, y ≤ x + ε) → y ≤ x := begin contrapose!, exact assume h, ⟨(y-x)/2, by linarith, by linarith⟩, end /- The angle brackets `⟨` and `⟩` introduce compound data or proofs. A proof of a `∃ z, P z` statemement is composed of a witness `z₀` and a proof `h` of `P z₀`. The compound is denoted by `⟨z₀, h⟩`. In the example above, the predicate is itself compound, it is a conjunction `P z ∧ Q z`. So the proof term should read `⟨z₀, ⟨h₁, h₂⟩⟩` where `h₁` (resp. `h₂`) is a proof of `P z₀` (resp. `Q z₀`). But these so-called "anonymous constructor" brackets are right-associative, so we can get rid of the nested brackets. The keyword `by` introduces tactic mode inside term mode, it is a shorter version of the `begin`/`end` pair, which is more convenient for single tactic blocks. In this example, `begin` enters tactic mode, `exact` leaves it, `by` re-enters it. Going all the way to a proof term would make the proof much longer, because we crucially use automation with `contrapose!` and `linarith`. We can still get a one-line proof using curly braces to gather several tactic invocations, and the `by` abbreviation instead of `begin`/`end`: -/ example {x y : ℝ} : (∀ ε > 0, y ≤ x + ε) → y ≤ x := by { contrapose!, exact assume h, ⟨(y-x)/2, by linarith, by linarith⟩ } /- One could argue that the above proof is a bit too terse, and we are relying too much on linarith. Let's have more `linarith` calls for smaller steps. For the sake of (tiny) variation, we will also assume the premise and argue by contradiction instead of contraposing. -/ example {x y : ℝ} : (∀ ε > 0, y ≤ x + ε) → y ≤ x := begin intro h, -- Assume the conclusion is false, and call this assumption H. by_contradiction H, push_neg at H, -- Now let's compute. have key := calc -- Each line must end with a colon followed by a proof term -- We want to specialize our assumption `h` to `ε = (y-x)/2` but this is long to -- type, so let's put a hole `_` that Lean will fill in by comparing the -- statement we want to prove and our proof term with a hole. As usual, -- positivity of `(y-x)/2` is proved by `linarith` y ≤ x + (y-x)/2 : h _ (by linarith) ... = x/2 + y/2 : by ring ... < y : by linarith, -- our key now says `y < y` (notice how the sequence `≤`, `=`, `<` was correctly -- merged into a `<`). Let `linarith` find the desired contradiction now. linarith, -- alternatively, we could have provided the proof term -- `exact lt_irrefl y key` end /- Now we are ready for some analysis. Let's set up notation for absolute value -/ local notation `|`x`|` := abs x /- And let's define convergence of sequences of real numbers (of course there is a much more general definition in mathlib). -/ /-- The sequence `u` tends to `l` -/ def limit (u : ℕ → ℝ) (l : ℝ) := ∀ ε > 0, ∃ N, ∀ n ≥ N, |u n - l| ≤ ε /- In the above definition, `u n` denotes the n-th term of the sequence. We can add parentheses to get `u(n)` but we try to avoid parentheses because they pile up very quickly -/ -- If y ≤ u n for all n and u n goes to x then y ≤ x lemma le_lim {x y : ℝ} {u : ℕ → ℝ} (hu : limit u x) (ineq : ∀ n, y ≤ u n) : y ≤ x := begin -- Let's apply our previous lemma apply le_of_le_add_eps, -- We need to prove y ≤ x + ε for all positive ε. -- Let ε be any positive real intros ε ε_pos, -- we now specialize our limit assumption to this `ε`, and immediately -- fix a `N` as promised by the definition. cases hu ε ε_pos with N HN, -- Now we only need to compute until reaching the conclusion calc y ≤ u N : ineq N ... = x + (u N - x) : by linarith -- We'll need `add_le_add` which says `a ≤ b` and `c ≤ d` implies `a + c ≤ b + d` -- We need a lemma saying `z ≤ |z|`. Because we don't know the name of this lemma, -- let's use `library_search`. Because searching thourgh the library is slow, -- Lean will write what it found in the Lean message window when cursor is on -- that line, so that we can replace it by the lemma. We see `le_max_self`, which -- says `a ≤ |a|`, exactly what we're looking for. ... ≤ x + |u N - x| : add_le_add (by linarith) (le_abs_self (u N - x)) ... ≤ x + ε : add_le_add (by linarith) (HN N (by linarith)), end /- The next lemma has been extracted from the main proof in order to discuss numbers. In ordinary maths, we know that ℕ is *not* contained in `ℝ`, whatever the construction of real numbers that we use. For instance a natural number is not an equivalence class of Cauchy sequences. But it's very easy to pretend otherwise. Formal maths requires slightly more care. In the statement below, the "type ascription" `(n + 1 : ℝ)` forces Lean to convert the natural number `n+1` into a real number. The "inclusion" map will be displayed in tactic state as `↑`. There are various lemmas asserting this map is compatible with addition and monotone, but we don't want to bother writing their names. The `norm_cast` tactic is designed to wisely apply those lemmas for us. -/ lemma inv_succ_pos : ∀ n : ℕ, 1/(n+1 : ℝ) > 0 := begin -- Let `n` be any integer intro n, -- Since we don't know the name of the relevant lemma, asserting that the inverse of -- a positive number is positive, let's state that is suffices -- to prove that `n+1`, seen as a real number, is positive, and ask `library_search` suffices : (n + 1 : ℝ) > 0, { library_search }, -- Now we want to reduce to a statement about natural numbers, not real numbers -- coming from natural numbers. norm_cast, -- and then get the usual help from `linarith` linarith, end /- That was a pretty long proof for an obvious fact. And stating it as a lemma feels stupid, so let's find a way to write it on one line in case we want to include it in some other proof without stating a lemma. First the `library_search` call above displays the name of the relevant lemma: `one_div_pos`. We can also replace the `linarith` call on the last line by `library_search` to learn the name of the lemma `nat.succ_pos` asserting that the successor of a natural number is positive. There is also a variant on `norm_cast` that combines it with `exact`. The term mode analogue of `intro` is `λ`. We get down to: -/ example : ∀ n : ℕ, 1/(n+1 : ℝ) > 0 := λ n, one_div_pos.mpr (by exact_mod_cast nat.succ_pos n) /- The next proof uses mostly known things, so we will commment only new aspects. -/ lemma limit_inv_succ : ∀ ε > 0, ∃ N : ℕ, ∀ n ≥ N, 1/(n + 1 : ℝ) ≤ ε := begin intros ε ε_pos, suffices : ∃ N : ℕ, 1/ε ≤ N, { -- Because we didn't provide a name for the above statement, Lean called it `this`. -- Let's fix an `N` that works. cases this with N HN, use N, intros n Hn, -- Now we want to rewrite the goal using lemmas -- `div_le_iff' : 0 < b → (a / b ≤ c ↔ a ≤ b * c)` -- `div_le_iff : 0 < b → (a / b ≤ c ↔ a ≤ c * b)` -- the second one will be rewritten from right to left, as indicated by `←`. -- Lean will create a side goal for the required positivity assumption that -- we don't provide for `div_le_iff'`. rw [div_le_iff', ← div_le_iff ε_pos], -- We want to replace assumption `Hn` by its real counter-part so that -- linarith can find what it needs. replace Hn : (N : ℝ) ≤ n, exact_mod_cast Hn, linarith, -- we are still left with the positivity assumption, but already discussed -- how to prove it in the preceding lemma exact_mod_cast nat.succ_pos n }, -- Now we need to prove that sufficient statement. -- We want to use that `ℝ` is archimedean. So we start typing -- `exact archimedean_` and hit Ctrl-space to see what completion Lean proposes -- the lemma `archimedean_iff_nat_le` sounds promising. We select the left to -- right implication using `.1`. This a generic lemma for fields equiped with -- a linear (ie total) order. We need to provide a proof that `ℝ` is indeed -- archimedean. This is done using the `apply_instance` tactic that will be -- covered elsewhere. exact archimedean_iff_nat_le.1 (by apply_instance) (1/ε), end /- We can now put all pieces together, with almost no new things to explain. -/ lemma inf_seq (A : set ℝ) (x : ℝ) : (x is_an_inf_of A) ↔ (x ∈ low_bounds A ∧ ∃ u : ℕ → ℝ, limit u x ∧ ∀ n, u n ∈ A ) := begin split, { intro h, split, { exact h.1 }, -- On the next line, we don't need to tell Lean to treat `n+1` as a real number because -- we add `x` to it, so Lean knows there is only one way to make sense of this expression. have key : ∀ n : ℕ, ∃ a ∈ A, a < x + 1/(n+1), { intro n, -- we can use the lemma we proved above apply inf_lt h, -- and another one we proved! have : 0 < 1/(n+1 : ℝ), from inv_succ_pos n, linarith }, -- Now we need to use axiom of (countable) choice choose u hu using key, use u, split, { intros ε ε_pos, -- again we use a lemma we proved, specializing it to our fixed `ε`, and fixing a `N` cases limit_inv_succ ε ε_pos with N H, use N, intros n hn, have : x ≤ u n, from h.1 _ (hu n).1, have := calc u n < x + 1/(n + 1) : (hu n).2 ... ≤ x + ε : add_le_add (le_refl x) (H n hn), rw abs_of_nonneg ; linarith }, { intro n, exact (hu n).1 } }, { intro h, -- Assumption `h` is made of nested compound statements. We can use the -- recursive version of `cases` to unpack it in one go. rcases h with ⟨x_min, u, lim, huA⟩, split, exact x_min, intros y y_mino, apply le_lim lim, intro n, exact y_mino (u n) (huA n) }, end
83bf880cefb09d4239f669595dcdc2e2dccf0fb8
aa101d73b1a3173c7ec56de02b96baa8ca64c42e
/src/exercises/05_sequence_limits.lean
116556e58610455ec445fcbcc2984a6e61566bc1
[ "Apache-2.0" ]
permissive
gihanmarasingha/tutorials
b554d4d53866c493c4341dc13e914b01444e95a6
56617114ef0f9f7b808476faffd11e22e4380918
refs/heads/master
1,671,141,758,153
1,599,173,318,000
1,599,173,318,000
282,405,870
0
0
Apache-2.0
1,595,666,751,000
1,595,666,750,000
null
UTF-8
Lean
false
false
5,893
lean
import data.real.basic import algebra.pi_instances import tuto_lib notation `|`x`|` := abs x /- In this file we manipulate the elementary definition of limits of sequences of real numbers. mathlib has a much more general definition of limits, but here we want to practice using the logical operators and relations covered in the previous files. A sequence u is a function from ℕ to ℝ, hence Lean says u : ℕ → ℝ The definition we'll be using is: -- Definition of « u tends to l » def seq_limit (u : ℕ → ℝ) (l : ℝ) : Prop := ∀ ε > 0, ∃ N, ∀ n ≥ N, |u n - l| ≤ ε Note the use of `∀ ε > 0, ...` which is an abbreviation of `∀ ε, ε > 0 → ... ` In particular, a statement like `h : ∀ ε > 0, ...` can be specialized to a given ε₀ by `specialize h ε₀ hε₀` where hε₀ is a proof of ε₀ > 0. Also recall that, wherever Lean expects some proof term, we can start a tactic mode proof using the keyword `by` (followed by curly braces if you need more than one tactic invocation). For instance, if the local context contains: δ : ℝ δ_pos : δ > 0 h : ∀ ε > 0, ... then we can specialize h to the real number δ/2 using: `specialize h (δ/2) (by linarith)` where `by linarith` will provide the proof of `δ/2 > 0` expected by Lean. We'll take this opportunity to use two new tactics: `norm_num` will perform numerical normalization on the goal and `norm_num at h` will do the same in assumption `h`. This will get rid of trivial calculations on numbers, like replacing |l - l| by zero in the next exercise. `congr'` will try to prove equalities between applications of functions by recursively proving the arguments are the same. For instance, if the goal is `f x + g y = f z + g t` then congr will replace it by two goals: `x = z` and `y = t`. You can limit the recursion depth by specifying a natural number after `congr'`. For instance, in the above example, `congr' 1` will give new goals `f x = f z` and `g y = g t`, which only inspect arguments of the addition and not deeper. -/ variables (u v w : ℕ → ℝ) (l l' : ℝ) -- If u is constant with value l then u tends to l -- 0033 example : (∀ n, u n = l) → seq_limit u l := begin sorry end /- When dealing with absolute values, we'll use lemmas: abs_le (x y : ℝ) : |x| ≤ y ↔ -y ≤ x ∧ x ≤ y abs_add (x y : ℝ) : |x + y| ≤ |x| + |y| abs_sub (x y : ℝ) : |x - y| = |y - x| You should probably write them down on a sheet of paper that you keep at hand since they are used in many exercises. -/ -- Assume l > 0. Then u tends to l implies u n ≥ l/2 for large enough n -- 0034 example (hl : l > 0) : seq_limit u l → ∃ N, ∀ n ≥ N, u n ≥ l/2 := begin sorry end /- When dealing with max, you can use ge_max_iff (p q r) : r ≥ max p q ↔ r ≥ p ∧ r ≥ q le_max_left p q : p ≤ max p q le_max_right p q : q ≤ max p q You should probably add them to the sheet of paper where you wrote the `abs` lemmas since they are used in many exercises. Let's see an example. -/ -- If u tends to l and v tends l' then u+v tends to l+l' example (hu : seq_limit u l) (hv : seq_limit v l') : seq_limit (u + v) (l + l') := begin intros ε ε_pos, cases hu (ε/2) (by linarith) with N₁ hN₁, cases hv (ε/2) (by linarith) with N₂ hN₂, use max N₁ N₂, intros n hn, cases ge_max_iff.mp hn with hn₁ hn₂, have fact₁ : |u n - l| ≤ ε/2, from hN₁ n (by linarith), -- note the use of `from`. -- This is an alias for `exact`, -- but reads nicer in this context have fact₂ : |v n - l'| ≤ ε/2, from hN₂ n (by linarith), calc |(u + v) n - (l + l')| = |u n + v n - (l + l')| : rfl ... = |(u n - l) + (v n - l')| : by congr' 1 ; ring ... ≤ |u n - l| + |v n - l'| : by apply abs_add ... ≤ ε : by linarith, end /- In the above proof, we used `have` to prepare facts for `linarith` consumption in the last line. Since we have direct proof terms for them, we can feed them directly to `linarith` as in the next proof of the same statement. Another variation we introduce is rewriting using `ge_max_iff` and letting `linarith` handle the conjunction, instead of creating two new assumptions. -/ example (hu : seq_limit u l) (hv : seq_limit v l') : seq_limit (u + v) (l + l') := begin intros ε ε_pos, cases hu (ε/2) (by linarith) with N₁ hN₁, cases hv (ε/2) (by linarith) with N₂ hN₂, use max N₁ N₂, intros n hn, rw ge_max_iff at hn, calc |(u + v) n - (l + l')| = |u n + v n - (l + l')| : rfl ... = |(u n - l) + (v n - l')| : by congr' 1 ; ring ... ≤ |u n - l| + |v n - l'| : by apply abs_add ... ≤ ε : by linarith [hN₁ n (by linarith), hN₂ n (by linarith)], end /- Let's do something similar: the squeezing theorem. -/ -- 0035 example (hu : seq_limit u l) (hw : seq_limit w l) (h : ∀ n, u n ≤ v n) (h' : ∀ n, v n ≤ w n) : seq_limit v l := begin sorry end /- What about < ε? -/ -- 0036 example (u l) : seq_limit u l ↔ ∀ ε > 0, ∃ N, ∀ n ≥ N, |u n - l| < ε := begin sorry end /- In the next exercise, we'll use eq_of_abs_sub_le_all (x y : ℝ) : (∀ ε > 0, |x - y| ≤ ε) → x = y -/ -- A sequence admits at most one limit -- 0037 example : seq_limit u l → seq_limit u l' → l = l' := begin sorry end /- Let's now practice deciphering definitions before proving. -/ def non_decreasing (u : ℕ → ℝ) := ∀ n m, n ≤ m → u n ≤ u m def is_seq_sup (M : ℝ) (u : ℕ → ℝ) := (∀ n, u n ≤ M) ∧ ∀ ε > 0, ∃ n₀, u n₀ ≥ M - ε -- 0038 example (M : ℝ) (h : is_seq_sup M u) (h' : non_decreasing u) : seq_limit u M := begin sorry end
62b916151f8d528af82e235b9b30d788af446544
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/test/elementwise.lean
ec94c57f84ca0a76ff54168969d64d265495f786
[ "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,821
lean
import algebra.category.Mon.basic import tactic.elementwise open category_theory universes w v u @[elementwise] lemma foo {C : Type u} [category.{v} C] {M N K : C} {f : M ⟶ N} {g : N ⟶ K} {h : M ⟶ K} (w : f ≫ g = h) : f ≫ 𝟙 N ≫ g = h := by simp [w] @[elementwise] lemma foo' {C : Type*} [category C] {M N K : C} {f : M ⟶ N} {g : N ⟶ K} {h : M ⟶ K} (w : f ≫ g = h) : f ≫ 𝟙 N ≫ g = h := by simp [w] local attribute [instance] concrete_category.has_coe_to_sort concrete_category.has_coe_to_fun lemma bar {C : Type u} [category.{v} C] [concrete_category.{w} C] {M N K : C} {f : M ⟶ N} {g : N ⟶ K} {h : M ⟶ K} (w : f ≫ g = h) (x : M) : g (f x) = h x := by apply foo_apply w lemma bar' {M N K : Mon.{u}} {f : M ⟶ N} {g : N ⟶ K} {h : M ⟶ K} (w : f ≫ g = h) (x : M) : g (f x) = h x := by exact foo_apply w x lemma bar'' {M N K : Mon.{0}} {f : M ⟶ N} {g : N ⟶ K} {h : M ⟶ K} (w : f ≫ g = h) (x : M) : g (f x) = h x := by apply foo_apply w lemma bar''' {M N K : Mon} {f : M ⟶ N} {g : N ⟶ K} {h : M ⟶ K} (w : f ≫ g = h) (x : M) : g (f x) = h x := by apply foo_apply w example (M N K : Mon.{u}) (f : M ⟶ N) (g : N ⟶ K) (h : M ⟶ K) (w : f ≫ g = h) (m : M) : g (f m) = h m := begin rw elementwise_of w, end example (M N K : Mon.{0}) (f : M ⟶ N) (g : N ⟶ K) (h : M ⟶ K) (w : f ≫ g = h) (m : M) : g (f m) = h m := begin elementwise! w, apply w, end example {α β : Type} (f g : α ⟶ β) (w : f = g) (a : α) : f a = g a := begin elementwise! w, -- make sure this works even when there is no simplification to do rw w, end example {α β : Type} (f g : α ⟶ β) (w : f ≫ 𝟙 β = g) (a : α) : f a = g a := begin elementwise! w, rw w, -- this used to not work, because we produced `w : ⇑f a = ⇑g a`. end
a8bc9eb35e1727e85cc508f6f8c600c127952e0b
82e44445c70db0f03e30d7be725775f122d72f3e
/src/category_theory/subobject/factor_thru.lean
75d5ca9334817705cdcf93aa00b3093a5db532d3
[ "Apache-2.0" ]
permissive
stjordanis/mathlib
51e286d19140e3788ef2c470bc7b953e4991f0c9
2568d41bca08f5d6bf39d915434c8447e21f42ee
refs/heads/master
1,631,748,053,501
1,627,938,886,000
1,627,938,886,000
228,728,358
0
0
Apache-2.0
1,576,630,588,000
1,576,630,587,000
null
UTF-8
Lean
false
false
6,903
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta, Scott Morrison -/ import category_theory.subobject.basic /-! # Factoring through subobjects The predicate `h : P.factors f`, for `P : subobject Y` and `f : X ⟶ Y` asserts the existence of some `P.factor_thru f : X ⟶ (P : C)` making the obvious diagram commute. -/ universes v₁ v₂ u₁ u₂ noncomputable theory open category_theory category_theory.category category_theory.limits variables {C : Type u₁} [category.{v₁} C] {X Y Z : C} variables {D : Type u₂} [category.{v₂} D] namespace category_theory namespace mono_over /-- When `f : X ⟶ Y` and `P : mono_over Y`, `P.factors f` expresses that there exists a factorisation of `f` through `P`. Given `h : P.factors f`, you can recover the morphism as `P.factor_thru f h`. -/ def factors {X Y : C} (P : mono_over Y) (f : X ⟶ Y) : Prop := ∃ g : X ⟶ (P : C), g ≫ P.arrow = f lemma factors_congr {X : C} {f g : mono_over X} {Y : C} (h : Y ⟶ X) (e : f ≅ g) : f.factors h ↔ g.factors h := ⟨λ ⟨u, hu⟩, ⟨u ≫ (((mono_over.forget _).map e.hom)).left, by simp [hu]⟩, λ ⟨u, hu⟩, ⟨u ≫ (((mono_over.forget _).map e.inv)).left, by simp [hu]⟩⟩ /-- `P.factor_thru f h` provides a factorisation of `f : X ⟶ Y` through some `P : mono_over Y`, given the evidence `h : P.factors f` that such a factorisation exists. -/ def factor_thru {X Y : C} (P : mono_over Y) (f : X ⟶ Y) (h : factors P f) : X ⟶ (P : C) := classical.some h end mono_over namespace subobject /-- When `f : X ⟶ Y` and `P : subobject Y`, `P.factors f` expresses that there exists a factorisation of `f` through `P`. Given `h : P.factors f`, you can recover the morphism as `P.factor_thru f h`. -/ def factors {X Y : C} (P : subobject Y) (f : X ⟶ Y) : Prop := quotient.lift_on' P (λ P, P.factors f) begin rintros P Q ⟨h⟩, apply propext, split, { rintro ⟨i, w⟩, exact ⟨i ≫ h.hom.left, by erw [category.assoc, over.w h.hom, w]⟩, }, { rintro ⟨i, w⟩, exact ⟨i ≫ h.inv.left, by erw [category.assoc, over.w h.inv, w]⟩, }, end @[simp] lemma mk_factors_iff {X Y Z : C} (f : Y ⟶ X) [mono f] (g : Z ⟶ X) : (subobject.mk f).factors g ↔ (mono_over.mk' f).factors g := iff.rfl lemma factors_iff {X Y : C} (P : subobject Y) (f : X ⟶ Y) : P.factors f ↔ (representative.obj P).factors f := quot.induction_on P $ λ a, mono_over.factors_congr _ (representative_iso _).symm lemma factors_self {X : C} (P : subobject X) : P.factors P.arrow := (factors_iff _ _).mpr ⟨𝟙 P, (by simp)⟩ lemma factors_comp_arrow {X Y : C} {P : subobject Y} (f : X ⟶ P) : P.factors (f ≫ P.arrow) := (factors_iff _ _).mpr ⟨f, rfl⟩ lemma factors_of_factors_right {X Y Z : C} {P : subobject Z} (f : X ⟶ Y) {g : Y ⟶ Z} (h : P.factors g) : P.factors (f ≫ g) := begin revert P, refine quotient.ind' _, intro P, rintro ⟨g, rfl⟩, exact ⟨f ≫ g, by simp⟩, end lemma factors_zero [has_zero_morphisms C] {X Y : C} {P : subobject Y} : P.factors (0 : X ⟶ Y) := (factors_iff _ _).mpr ⟨0, by simp⟩ lemma factors_of_le {Y Z : C} {P Q : subobject Y} (f : Z ⟶ Y) (h : P ≤ Q) : P.factors f → Q.factors f := by { simp only [factors_iff], exact λ ⟨u, hu⟩, ⟨u ≫ of_le _ _ h, by simp [←hu]⟩ } /-- `P.factor_thru f h` provides a factorisation of `f : X ⟶ Y` through some `P : subobject Y`, given the evidence `h : P.factors f` that such a factorisation exists. -/ def factor_thru {X Y : C} (P : subobject Y) (f : X ⟶ Y) (h : factors P f) : X ⟶ P := classical.some ((factors_iff _ _).mp h) @[simp, reassoc] lemma factor_thru_arrow {X Y : C} (P : subobject Y) (f : X ⟶ Y) (h : factors P f) : P.factor_thru f h ≫ P.arrow = f := classical.some_spec ((factors_iff _ _).mp h) @[simp] lemma factor_thru_self {X : C} (P : subobject X) (h) : P.factor_thru P.arrow h = 𝟙 P := by { ext, simp, } @[simp] lemma factor_thru_comp_arrow {X Y : C} {P : subobject Y} (f : X ⟶ P) (h) : P.factor_thru (f ≫ P.arrow) h = f := by { ext, simp, } @[simp] lemma factor_thru_eq_zero [has_zero_morphisms C] {X Y : C} {P : subobject Y} {f : X ⟶ Y} {h : factors P f} : P.factor_thru f h = 0 ↔ f = 0 := begin fsplit, { intro w, replace w := w =≫ P.arrow, simpa using w, }, { rintro rfl, ext, simp, }, end lemma factor_thru_right {X Y Z : C} {P : subobject Z} (f : X ⟶ Y) (g : Y ⟶ Z) (h : P.factors g) : f ≫ P.factor_thru g h = P.factor_thru (f ≫ g) (factors_of_factors_right f h) := begin apply (cancel_mono P.arrow).mp, simp, end @[simp] lemma factor_thru_zero [has_zero_morphisms C] {X Y : C} {P : subobject Y} (h : P.factors (0 : X ⟶ Y)) : P.factor_thru 0 h = 0 := by simp -- `h` is an explicit argument here so we can use -- `rw factor_thru_le h`, obtaining a subgoal `P.factors f`. -- (While the reverse direction looks plausible as a simp lemma, it seems to be unproductive.) lemma factor_thru_of_le {Y Z : C} {P Q : subobject Y} {f : Z ⟶ Y} (h : P ≤ Q) (w : P.factors f) : Q.factor_thru f (factors_of_le f h w) = P.factor_thru f w ≫ of_le P Q h := by { ext, simp, } section preadditive variables [preadditive C] lemma factors_add {X Y : C} {P : subobject Y} (f g : X ⟶ Y) (wf : P.factors f) (wg : P.factors g) : P.factors (f + g) := (factors_iff _ _).mpr ⟨P.factor_thru f wf + P.factor_thru g wg, by simp⟩ -- This can't be a `simp` lemma as `wf` and `wg` may not exist. -- However you can `rw` by it to assert that `f` and `g` factor through `P` separately. lemma factor_thru_add {X Y : C} {P : subobject Y} (f g : X ⟶ Y) (w : P.factors (f + g)) (wf : P.factors f) (wg : P.factors g) : P.factor_thru (f + g) w = P.factor_thru f wf + P.factor_thru g wg := by { ext, simp, } lemma factors_left_of_factors_add {X Y : C} {P : subobject Y} (f g : X ⟶ Y) (w : P.factors (f + g)) (wg : P.factors g) : P.factors f := (factors_iff _ _).mpr ⟨P.factor_thru (f + g) w - P.factor_thru g wg, by simp⟩ @[simp] lemma factor_thru_add_sub_factor_thru_right {X Y : C} {P : subobject Y} (f g : X ⟶ Y) (w : P.factors (f + g)) (wg : P.factors g) : P.factor_thru (f + g) w - P.factor_thru g wg = P.factor_thru f (factors_left_of_factors_add f g w wg) := by { ext, simp, } lemma factors_right_of_factors_add {X Y : C} {P : subobject Y} (f g : X ⟶ Y) (w : P.factors (f + g)) (wf : P.factors f) : P.factors g := (factors_iff _ _).mpr ⟨P.factor_thru (f + g) w - P.factor_thru f wf, by simp⟩ @[simp] lemma factor_thru_add_sub_factor_thru_left {X Y : C} {P : subobject Y} (f g : X ⟶ Y) (w : P.factors (f + g)) (wf : P.factors f) : P.factor_thru (f + g) w - P.factor_thru f wf = P.factor_thru g (factors_right_of_factors_add f g w wf) := by { ext, simp, } end preadditive end subobject end category_theory
ddd8960a2eadf9c1729563eb62a40f642024a0c9
80746c6dba6a866de5431094bf9f8f841b043d77
/src/topology/sequences.lean
8cda46c05ad1c5436224990f4f79cb1cc8418ce8
[ "Apache-2.0" ]
permissive
leanprover-fork/mathlib-backup
8b5c95c535b148fca858f7e8db75a76252e32987
0eb9db6a1a8a605f0cf9e33873d0450f9f0ae9b0
refs/heads/master
1,585,156,056,139
1,548,864,430,000
1,548,864,438,000
143,964,213
0
0
Apache-2.0
1,550,795,966,000
1,533,705,322,000
Lean
UTF-8
Lean
false
false
10,549
lean
/- Copyright (c) 2018 Jan-David Salchow. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jan-David Salchow Sequences in topological spaces. In this file we define sequences in topological spaces and show how they are related to filters and the topology. In particular, we * associate a filter with a sequence and prove equivalence of convergence of the two, * define the sequential closure of a set and prove that it's contained in the closure, * define a type class "sequential_space" in which closure and sequential closure agree, * define sequential continuity and show that it coincides with continuity in sequential spaces, * provide an instance that shows that every metric space is a sequential space. TODO: * There should be an instance that associates a sequential space with a first countable space. * Sequential compactness should be handled here. -/ import topology.basic topology.continuity topology.metric_space.basic import analysis.specific_limits open set filter variables {α : Type*} {β : Type*} local notation f `⟶` limit := tendsto f at_top (nhds limit) /- Statements about sequences in general topological spaces. -/ section topological_space variables [topological_space α] [topological_space β] /-- A sequence converges in the sence of topological spaces iff the associated statement for filter holds. -/ @[simp] lemma topological_space.seq_tendsto_iff {x : ℕ → α} {limit : α} : tendsto x at_top (nhds limit) ↔ ∀ U : set α, limit ∈ U → is_open U → ∃ n0 : ℕ, ∀ n ≥ n0, (x n) ∈ U := iff.intro (assume ttol : tendsto x at_top (nhds limit), show ∀ U : set α, limit ∈ U → is_open U → ∃ n0 : ℕ, ∀ n ≥ n0, (x n) ∈ U, from assume U limitInU isOpenU, have {n | (x n) ∈ U} ∈ at_top.sets := mem_map.mp $ le_def.mp ttol U $ mem_nhds_sets isOpenU limitInU, show ∃ n0 : ℕ, ∀ n ≥ n0, (x n) ∈ U, from mem_at_top_sets.mp this) (assume xtol : ∀ U : set α, limit ∈ U → is_open U → ∃ n0 : ℕ, ∀ n ≥ n0, (x n) ∈ U, suffices ∀ U, limit ∈ U → is_open U → x ⁻¹' U ∈ at_top.sets, from tendsto_nhds this, assume U limitInU isOpenU, suffices ∃ n0 : ℕ, ∀ n ≥ n0, (x n) ∈ U, by simp [this], xtol U limitInU isOpenU) /-- The sequential closure of a subset M ⊆ α of a topological space α is the set of all p ∈ α which arise as limit of sequences in M. -/ def sequential_closure (M : set α) : set α := {p | ∃ x : ℕ → α, (∀ n : ℕ, x n ∈ M) ∧ (x ⟶ p)} lemma subset_sequential_closure (M : set α) : M ⊆ sequential_closure M := assume p (_ : p ∈ M), show p ∈ sequential_closure M, from ⟨λ n, p, assume n, ‹p ∈ M›, tendsto_const_nhds⟩ def is_seq_closed (A : set α) : Prop := A = sequential_closure A /-- A convenience lemma for showing that a set is sequentially closed. -/ lemma is_seq_closed_of_def {A : set α} (h : ∀(x : ℕ → α) (p : α), (∀ n : ℕ, x n ∈ A) → (x ⟶ p) → p ∈ A) : is_seq_closed A := show A = sequential_closure A, from subset.antisymm (subset_sequential_closure A) (show ∀ p, p ∈ sequential_closure A → p ∈ A, from (assume p ⟨x, _, _⟩, show p ∈ A, from h x p ‹∀ n : ℕ, ((x n) ∈ A)› ‹(x ⟶ p)›)) /-- The sequential closure of a set is contained in the closure of that set. The converse is not true. -/ lemma sequential_closure_subset_closure (M : set α) : sequential_closure M ⊆ closure M := show ∀ p, p ∈ sequential_closure M → p ∈ closure M, from assume p, assume : ∃ x : ℕ → α, (∀ n : ℕ, ((x n) ∈ M)) ∧ (x ⟶ p), let ⟨x, ⟨_, _⟩⟩ := this in show p ∈ closure M, from -- we have to show that p is in the closure of M -- using mem_closure_iff, this is equivalent to proving that every open neighbourhood -- has nonempty intersection with M, but this is witnessed by our sequence x suffices ∀ O, is_open O → p ∈ O → O ∩ M ≠ ∅, from mem_closure_iff.mpr this, have ∀ (U : set α), p ∈ U → is_open U → (∃ n0, ∀ n, n ≥ n0 → x n ∈ U), by rwa[←topological_space.seq_tendsto_iff], assume O is_open_O p_in_O, let ⟨n0, _⟩ := this O ‹p ∈ O› ‹is_open O› in have (x n0) ∈ O, from ‹∀ n ≥ n0, x n ∈ O› n0 (show n0 ≥ n0, from le_refl n0), have (x n0) ∈ O ∩ M, from ⟨this, ‹∀n, x n ∈ M› n0⟩, set.ne_empty_of_mem this /-- A set is sequentially closed if it is closed. -/ lemma is_seq_closed_of_is_closed (M : set α) (_ : is_closed M) : is_seq_closed M := suffices sequential_closure M ⊆ M, from set.eq_of_subset_of_subset (subset_sequential_closure M) this, calc sequential_closure M ⊆ closure M : sequential_closure_subset_closure M ... = M : closure_eq_of_is_closed ‹is_closed M› /-- The limit of a convergent sequence in a sequentially closed set is in that set.-/ lemma mem_of_is_seq_closed {A : set α} (_ : is_seq_closed A) {x : ℕ → α} (_ : ∀ n, x n ∈ A) {limit : α} (_ : (x ⟶ limit)) : limit ∈ A := have limit ∈ sequential_closure A, from show ∃ x : ℕ → α, (∀ n : ℕ, x n ∈ A) ∧ (x ⟶ limit), from ⟨x, ‹∀ n, x n ∈ A›, ‹(x ⟶ limit)›⟩, eq.subst (eq.symm ‹is_seq_closed A›) ‹limit ∈ sequential_closure A› /-- The limit of a convergent sequence in a closed set is in that set.-/ lemma mem_of_is_closed_sequential {A : set α} (_ : is_closed A) {x : ℕ → α} (_ : ∀ n, x n ∈ A) {limit : α} (_ : x ⟶ limit) : limit ∈ A := mem_of_is_seq_closed (is_seq_closed_of_is_closed A ‹is_closed A›) ‹∀ n, x n ∈ A› ‹(x ⟶ limit)› /-- A sequential space is a space in which 'sequences are enough to probe the topology'. This can be formalised by demanding that the sequential closure and the closure coincide. The following statements show that other topological properties can be deduced from sequences in sequential spaces. -/ class sequential_space (α : Type*) [topological_space α] : Prop := (sequential_closure_eq_closure : ∀ M : set α, sequential_closure M = closure M) /-- In a sequential space, a set is closed iff it's sequentially closed. -/ lemma is_seq_closed_iff_is_closed [sequential_space α] {M : set α} : is_seq_closed M ↔ is_closed M := iff.intro (assume _, closure_eq_iff_is_closed.mp (eq.symm (calc M = sequential_closure M : by assumption ... = closure M : sequential_space.sequential_closure_eq_closure M))) (is_seq_closed_of_is_closed M) /-- A function between topological spaces is sequentially continuous if it commutes with limit of convergent sequences. -/ def sequentially_continuous (f : α → β) : Prop := ∀ (x : ℕ → α), ∀ {limit : α}, (x ⟶ limit) → (f∘x ⟶ f limit) /- A continuous function is sequentially continuous. -/ lemma continuous.to_sequentially_continuous {f : α → β} (_ : continuous f) : sequentially_continuous f := assume x limit (_ : x ⟶ limit), have tendsto f (nhds limit) (nhds (f limit)), from continuous.tendsto ‹continuous f› limit, show (f ∘ x) ⟶ (f limit), from tendsto.comp ‹(x ⟶ limit)› this /-- In a sequential space, continuity and sequential continuity coincide. -/ lemma continuous_iff_sequentially_continuous {f : α → β} [sequential_space α] : continuous f ↔ sequentially_continuous f := iff.intro (assume _, ‹continuous f›.to_sequentially_continuous) (assume : sequentially_continuous f, show continuous f, from suffices h : ∀ {A : set β}, is_closed A → is_seq_closed (f ⁻¹' A), from continuous_iff_is_closed.mpr (assume A _, is_seq_closed_iff_is_closed.mp $ h ‹is_closed A›), assume A (_ : is_closed A), is_seq_closed_of_def $ assume (x : ℕ → α) p (_ : ∀ n, f (x n) ∈ A) (_ : x ⟶ p), have (f ∘ x) ⟶ (f p), from ‹sequentially_continuous f› x ‹(x ⟶ p)›, show f p ∈ A, from mem_of_is_closed_sequential ‹is_closed A› ‹∀ n, f (x n) ∈ A› ‹(f∘x ⟶ f p)›) end topological_space /- Statements about sequences in metric spaces -/ namespace metric variable [metric_space α] variables {ε : ℝ} -- necessary for the next instance set_option eqn_compiler.zeta true /-- Show that every metric space is sequential. -/ instance : sequential_space α := ⟨show ∀ M, sequential_closure M = closure M, from assume M, suffices closure M ⊆ sequential_closure M, from set.subset.antisymm (sequential_closure_subset_closure M) this, assume (p : α) (_ : p ∈ closure M), -- we construct a sequence in α, with values in M, that converges to p -- the first step is to use (p ∈ closure M) ↔ "all nhds of p contain elements of M" on metric -- balls have ∀ n : ℕ, ball p ((1:ℝ)/((n+1):ℝ)) ∩ M ≠ ∅ := assume n : ℕ, mem_closure_iff.mp ‹p ∈ (closure M)› (ball p ((1:ℝ)/((n+1):ℝ))) (is_open_ball) (mem_ball_self $ one_div_pos_of_pos $ add_pos_of_nonneg_of_pos (nat.cast_nonneg n) zero_lt_one), -- from this, construct a "sequence of hypothesis" h, (h n) := _ ∈ {x // x ∈ ball (1/n+1) p ∩ M} let h := λ n : ℕ, (classical.indefinite_description _ (set.exists_mem_of_ne_empty (this n))), -- and the actual sequence x := λ n : ℕ, (h n).val in -- now we construct the promised sequence and show the claim show ∃ x : ℕ → α, (∀ n : ℕ, ((x n) ∈ M)) ∧ (x ⟶ p), from ⟨x, assume n, have (x n) ∈ ball p ((1:ℝ)/((n+1):ℝ)) ∩ M := (h n).property, this.2, suffices ∀ ε > 0, ∃ n0 : ℕ, ∀ n ≥ n0, dist (x n) p < ε, by simpa only [metric.tendsto_at_top], assume ε _, -- we apply that 1/n converges to zero to the fact that (x n) ∈ ball p ε have ∀ ε > 0, ∃ n0 : ℕ, ∀ n ≥ n0, dist (1 / (↑n + 1)) (0:ℝ) < ε := metric.tendsto_at_top.mp tendsto_one_div_add_at_top_nhds_0_nat, let ⟨n0, hn0⟩ := this ε ‹ε > 0› in show ∃ n0 : ℕ, ∀ n ≥ n0, dist (x n) p < ε, from ⟨n0, assume n ngtn0, calc dist (x n) p < (1:ℝ)/↑(n+1) : (h n).property.1 ... = abs ((1:ℝ)/↑(n+1)) : eq.symm $ abs_of_nonneg $ div_nonneg' zero_le_one $ nat.cast_nonneg _ ... = abs ((1:ℝ)/↑(n+1) - 0) : by simp ... = dist ((1:ℝ)/↑(n+1)) 0 : eq.symm $ real.dist_eq ((1:ℝ)/↑(n+1)) 0 ... < ε : hn0 n ‹n ≥ n0›⟩⟩⟩ set_option eqn_compiler.zeta false end metric
0aa378f82fa318a2bf1c32ec59c54adfa2eccb17
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/tc_loop.lean
95b9df429915e70419123c7bce1e7f1070baa5bc
[ "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
304
lean
open tactic check (do { trace "foo", trace_state } : tactic unit) axiom Sorry {A : Type*} : A check λ (A : Type*) (a b c d : A) (H₁ : a = b) (H₂ : c = b) (H₃ : d = c), have a = c, by do { trace "have-expr...", trace_state, transitivity, assumption, symmetry, assumption }, show a = d, from Sorry
3a4886bc3168821e1282afb026e4d27b1ada6918
bb31430994044506fa42fd667e2d556327e18dfe
/src/algebra/module/equiv.lean
a9fc1ee36fbae97ba20b1fd8f40ebb1aad7f2350
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
25,229
lean
/- Copyright (c) 2020 Anne Baanen. 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, Anne Baanen, Frédéric Dupuis, Heather Macbeth -/ import algebra.module.linear_map /-! # (Semi)linear equivalences In this file we define * `linear_equiv σ M M₂`, `M ≃ₛₗ[σ] M₂`: an invertible semilinear map. Here, `σ` is a `ring_hom` from `R` to `R₂` and an `e : M ≃ₛₗ[σ] M₂` satisfies `e (c • x) = (σ c) • (e x)`. The plain linear version, with `σ` being `ring_hom.id R`, is denoted by `M ≃ₗ[R] M₂`, and the star-linear version (with `σ` being `star_ring_end`) is denoted by `M ≃ₗ⋆[R] M₂`. ## Implementation notes To ensure that composition works smoothly for semilinear equivalences, we use the typeclasses `ring_hom_comp_triple`, `ring_hom_inv_pair` and `ring_hom_surjective` from `algebra/ring/comp_typeclasses`. The group structure on automorphisms, `linear_equiv.automorphism_group`, is provided elsewhere. ## TODO * Parts of this file have not yet been generalized to semilinear maps ## Tags linear equiv, linear equivalences, linear isomorphism, linear isomorphic -/ open function universes u u' v w x y z variables {R : Type*} {R₁ : Type*} {R₂ : Type*} {R₃ : Type*} variables {k : Type*} {S : Type*} {M : Type*} {M₁ : Type*} {M₂ : Type*} {M₃ : Type*} variables {N₁ : Type*} {N₂ : Type*} {N₃ : Type*} {N₄ : Type*} {ι : Type*} section set_option old_structure_cmd true /-- A linear equivalence is an invertible linear map. -/ @[nolint has_nonempty_instance] structure linear_equiv {R : Type*} {S : Type*} [semiring R] [semiring S] (σ : R →+* S) {σ' : S →+* R} [ring_hom_inv_pair σ σ'] [ring_hom_inv_pair σ' σ] (M : Type*) (M₂ : Type*) [add_comm_monoid M] [add_comm_monoid M₂] [module R M] [module S M₂] extends linear_map σ M M₂, M ≃+ M₂ attribute [nolint doc_blame] linear_equiv.to_linear_map attribute [nolint doc_blame] linear_equiv.to_add_equiv notation M ` ≃ₛₗ[`:50 σ `] ` M₂ := linear_equiv σ M M₂ notation M ` ≃ₗ[`:50 R `] ` M₂ := linear_equiv (ring_hom.id R) M M₂ notation M ` ≃ₗ⋆[`:50 R `] ` M₂ := linear_equiv (star_ring_end R) M M₂ /-- `semilinear_equiv_class F σ M M₂` asserts `F` is a type of bundled `σ`-semilinear equivs `M → M₂`. See also `linear_equiv_class F R M M₂` for the case where `σ` is the identity map on `R`. A map `f` between an `R`-module and an `S`-module over a ring homomorphism `σ : R →+* S` is semilinear if it satisfies the two properties `f (x + y) = f x + f y` and `f (c • x) = (σ c) • f x`. -/ class semilinear_equiv_class (F : Type*) {R S : out_param Type*} [semiring R] [semiring S] (σ : out_param $ R →+* S) {σ' : out_param $ S →+* R} [ring_hom_inv_pair σ σ'] [ring_hom_inv_pair σ' σ] (M M₂ : out_param Type*) [add_comm_monoid M] [add_comm_monoid M₂] [module R M] [module S M₂] extends add_equiv_class F M M₂ := (map_smulₛₗ : ∀ (f : F) (r : R) (x : M), f (r • x) = (σ r) • f x) -- `R, S, σ, σ'` become metavars, but it's OK since they are outparams. attribute [nolint dangerous_instance] semilinear_equiv_class.to_add_equiv_class /-- `linear_equiv_class F R M M₂` asserts `F` is a type of bundled `R`-linear equivs `M → M₂`. This is an abbreviation for `semilinear_equiv_class F (ring_hom.id R) M M₂`. -/ abbreviation linear_equiv_class (F : Type*) (R M M₂ : out_param Type*) [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [module R M] [module R M₂] := semilinear_equiv_class F (ring_hom.id R) M M₂ end namespace semilinear_equiv_class variables (F : Type*) [semiring R] [semiring S] variables [add_comm_monoid M] [add_comm_monoid M₁] [add_comm_monoid M₂] variables [module R M] [module S M₂] {σ : R →+* S} {σ' : S →+* R} -- `σ'` becomes a metavariable, but it's OK since it's an outparam @[priority 100, nolint dangerous_instance] instance [ring_hom_inv_pair σ σ'] [ring_hom_inv_pair σ' σ] [s : semilinear_equiv_class F σ M M₂] : semilinear_map_class F σ M M₂ := { coe := (coe : F → M → M₂), coe_injective' := @fun_like.coe_injective F _ _ _, ..s } end semilinear_equiv_class namespace linear_equiv section add_comm_monoid variables {M₄ : Type*} variables [semiring R] [semiring S] section variables [add_comm_monoid M] [add_comm_monoid M₁] [add_comm_monoid M₂] variables [module R M] [module S M₂] {σ : R →+* S} {σ' : S →+* R} variables [ring_hom_inv_pair σ σ'] [ring_hom_inv_pair σ' σ] include R include σ' instance : has_coe (M ≃ₛₗ[σ] M₂) (M →ₛₗ[σ] M₂) := ⟨to_linear_map⟩ -- see Note [function coercion] instance : has_coe_to_fun (M ≃ₛₗ[σ] M₂) (λ _, M → M₂) := ⟨to_fun⟩ @[simp] lemma coe_mk {to_fun inv_fun map_add map_smul left_inv right_inv } : ⇑(⟨to_fun, map_add, map_smul, inv_fun, left_inv, right_inv⟩ : M ≃ₛₗ[σ] M₂) = to_fun := rfl -- This exists for compatibility, previously `≃ₗ[R]` extended `≃` instead of `≃+`. @[nolint doc_blame] def to_equiv : (M ≃ₛₗ[σ] M₂) → M ≃ M₂ := λ f, f.to_add_equiv.to_equiv lemma to_equiv_injective : function.injective (to_equiv : (M ≃ₛₗ[σ] M₂) → M ≃ M₂) := λ ⟨_, _, _, _, _, _⟩ ⟨_, _, _, _, _, _⟩ h, linear_equiv.mk.inj_eq.mpr (equiv.mk.inj h) @[simp] lemma to_equiv_inj {e₁ e₂ : M ≃ₛₗ[σ] M₂} : e₁.to_equiv = e₂.to_equiv ↔ e₁ = e₂ := to_equiv_injective.eq_iff lemma to_linear_map_injective : injective (coe : (M ≃ₛₗ[σ] M₂) → (M →ₛₗ[σ] M₂)) := λ e₁ e₂ H, to_equiv_injective $ equiv.ext $ linear_map.congr_fun H @[simp, norm_cast] lemma to_linear_map_inj {e₁ e₂ : M ≃ₛₗ[σ] M₂} : (e₁ : M →ₛₗ[σ] M₂) = e₂ ↔ e₁ = e₂ := to_linear_map_injective.eq_iff instance : semilinear_equiv_class (M ≃ₛₗ[σ] M₂) σ M M₂ := { coe := linear_equiv.to_fun, inv := linear_equiv.inv_fun, coe_injective' := λ f g h₁ h₂, by { cases f, cases g, congr' }, left_inv := linear_equiv.left_inv, right_inv := linear_equiv.right_inv, map_add := map_add', map_smulₛₗ := map_smul' } lemma coe_injective : @injective (M ≃ₛₗ[σ] M₂) (M → M₂) coe_fn := fun_like.coe_injective end section variables [semiring R₁] [semiring R₂] [semiring R₃] variables [add_comm_monoid M] [add_comm_monoid M₁] [add_comm_monoid M₂] variables [add_comm_monoid M₃] [add_comm_monoid M₄] variables [add_comm_monoid N₁] [add_comm_monoid N₂] variables {module_M : module R M} {module_S_M₂ : module S M₂} {σ : R →+* S} {σ' : S →+* R} variables {re₁ : ring_hom_inv_pair σ σ'} {re₂ : ring_hom_inv_pair σ' σ} variables (e e' : M ≃ₛₗ[σ] M₂) lemma to_linear_map_eq_coe : e.to_linear_map = (e : M →ₛₗ[σ] M₂) := rfl @[simp, norm_cast] theorem coe_coe : ⇑(e : M →ₛₗ[σ] M₂) = e := rfl @[simp] lemma coe_to_equiv : ⇑e.to_equiv = e := rfl @[simp] lemma coe_to_linear_map : ⇑e.to_linear_map = e := rfl @[simp] lemma to_fun_eq_coe : e.to_fun = e := rfl section variables {e e'} @[ext] lemma ext (h : ∀ x, e x = e' x) : e = e' := fun_like.ext _ _ h lemma ext_iff : e = e' ↔ ∀ x, e x = e' x := fun_like.ext_iff protected lemma congr_arg {x x'} : x = x' → e x = e x' := fun_like.congr_arg e protected lemma congr_fun (h : e = e') (x : M) : e x = e' x := fun_like.congr_fun h x end section variables (M R) /-- The identity map is a linear equivalence. -/ @[refl] def refl [module R M] : M ≃ₗ[R] M := { .. linear_map.id, .. equiv.refl M } end @[simp] lemma refl_apply [module R M] (x : M) : refl R M x = x := rfl include module_M module_S_M₂ re₁ re₂ /-- Linear equivalences are symmetric. -/ @[symm] def symm (e : M ≃ₛₗ[σ] M₂) : M₂ ≃ₛₗ[σ'] M := { to_fun := e.to_linear_map.inverse e.inv_fun e.left_inv e.right_inv, inv_fun := e.to_equiv.symm.inv_fun, map_smul' := λ r x, by rw map_smulₛₗ, .. e.to_linear_map.inverse e.inv_fun e.left_inv e.right_inv, .. e.to_equiv.symm } omit module_M module_S_M₂ re₁ re₂ /-- See Note [custom simps projection] -/ def simps.symm_apply {R : Type*} {S : Type*} [semiring R] [semiring S] {σ : R →+* S} {σ' : S →+* R} [ring_hom_inv_pair σ σ'] [ring_hom_inv_pair σ' σ] {M : Type*} {M₂ : Type*} [add_comm_monoid M] [add_comm_monoid M₂] [module R M] [module S M₂] (e : M ≃ₛₗ[σ] M₂) : M₂ → M := e.symm initialize_simps_projections linear_equiv (to_fun → apply, inv_fun → symm_apply) include σ' @[simp] lemma inv_fun_eq_symm : e.inv_fun = e.symm := rfl omit σ' @[simp] lemma coe_to_equiv_symm : ⇑e.to_equiv.symm = e.symm := rfl variables {module_M₁ : module R₁ M₁} {module_M₂ : module R₂ M₂} {module_M₃ : module R₃ M₃} variables {module_N₁ : module R₁ N₁} {module_N₂ : module R₁ N₂} variables {σ₁₂ : R₁ →+* R₂} {σ₂₃ : R₂ →+* R₃} {σ₁₃ : R₁ →+* R₃} variables {σ₂₁ : R₂ →+* R₁} {σ₃₂ : R₃ →+* R₂} {σ₃₁ : R₃ →+* R₁} variables [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] variables [ring_hom_comp_triple σ₃₂ σ₂₁ σ₃₁] variables {re₁₂ : ring_hom_inv_pair σ₁₂ σ₂₁} {re₂₃ : ring_hom_inv_pair σ₂₃ σ₃₂} variables [ring_hom_inv_pair σ₁₃ σ₃₁] {re₂₁ : ring_hom_inv_pair σ₂₁ σ₁₂} variables {re₃₂ : ring_hom_inv_pair σ₃₂ σ₂₃} [ring_hom_inv_pair σ₃₁ σ₁₃] variables (e₁₂ : M₁ ≃ₛₗ[σ₁₂] M₂) (e₂₃ : M₂ ≃ₛₗ[σ₂₃] M₃) include σ₃₁ /-- Linear equivalences are transitive. -/ -- Note: The linter thinks the `ring_hom_comp_triple` argument is doubled -- it is not. @[trans, nolint unused_arguments] def trans : M₁ ≃ₛₗ[σ₁₃] M₃ := { .. e₂₃.to_linear_map.comp e₁₂.to_linear_map, .. e₁₂.to_equiv.trans e₂₃.to_equiv } omit σ₃₁ infixl ` ≪≫ₗ `:80 := @linear_equiv.trans _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ (ring_hom.id _) (ring_hom.id _) (ring_hom.id _) (ring_hom.id _) (ring_hom.id _) (ring_hom.id _) ring_hom_comp_triple.ids ring_hom_comp_triple.ids ring_hom_inv_pair.ids ring_hom_inv_pair.ids ring_hom_inv_pair.ids ring_hom_inv_pair.ids ring_hom_inv_pair.ids ring_hom_inv_pair.ids variables {e₁₂} {e₂₃} @[simp] lemma coe_to_add_equiv : ⇑(e.to_add_equiv) = e := rfl /-- The two paths coercion can take to an `add_monoid_hom` are equivalent -/ lemma to_add_monoid_hom_commutes : e.to_linear_map.to_add_monoid_hom = e.to_add_equiv.to_add_monoid_hom := rfl include σ₃₁ @[simp] theorem trans_apply (c : M₁) : (e₁₂.trans e₂₃ : M₁ ≃ₛₗ[σ₁₃] M₃) c = e₂₃ (e₁₂ c) := rfl theorem coe_trans : (e₁₂.trans e₂₃ : M₁ →ₛₗ[σ₁₃] M₃) = (e₂₃ : M₂ →ₛₗ[σ₂₃] M₃).comp (e₁₂ : M₁ →ₛₗ[σ₁₂] M₂) := rfl omit σ₃₁ include σ' @[simp] theorem apply_symm_apply (c : M₂) : e (e.symm c) = c := e.right_inv c @[simp] theorem symm_apply_apply (b : M) : e.symm (e b) = b := e.left_inv b omit σ' include σ₃₁ σ₂₁ σ₃₂ @[simp] lemma trans_symm : (e₁₂.trans e₂₃ : M₁ ≃ₛₗ[σ₁₃] M₃).symm = e₂₃.symm.trans e₁₂.symm := rfl lemma symm_trans_apply (c : M₃) : (e₁₂.trans e₂₃ : M₁ ≃ₛₗ[σ₁₃] M₃).symm c = e₁₂.symm (e₂₃.symm c) := rfl omit σ₃₁ σ₂₁ σ₃₂ @[simp] lemma trans_refl : e.trans (refl S M₂) = e := to_equiv_injective e.to_equiv.trans_refl @[simp] lemma refl_trans : (refl R M).trans e = e := to_equiv_injective e.to_equiv.refl_trans include σ' lemma symm_apply_eq {x y} : e.symm x = y ↔ x = e y := e.to_equiv.symm_apply_eq lemma eq_symm_apply {x y} : y = e.symm x ↔ e y = x := e.to_equiv.eq_symm_apply omit σ' lemma eq_comp_symm {α : Type*} (f : M₂ → α) (g : M₁ → α) : f = g ∘ e₁₂.symm ↔ f ∘ e₁₂ = g := e₁₂.to_equiv.eq_comp_symm f g lemma comp_symm_eq {α : Type*} (f : M₂ → α) (g : M₁ → α) : g ∘ e₁₂.symm = f ↔ g = f ∘ e₁₂ := e₁₂.to_equiv.comp_symm_eq f g lemma eq_symm_comp {α : Type*} (f : α → M₁) (g : α → M₂) : f = e₁₂.symm ∘ g ↔ e₁₂ ∘ f = g := e₁₂.to_equiv.eq_symm_comp f g lemma symm_comp_eq {α : Type*} (f : α → M₁) (g : α → M₂) : e₁₂.symm ∘ g = f ↔ g = e₁₂ ∘ f := e₁₂.to_equiv.symm_comp_eq f g variables [ring_hom_comp_triple σ₂₁ σ₁₃ σ₂₃] [ring_hom_comp_triple σ₃₁ σ₁₂ σ₃₂] include module_M₃ lemma eq_comp_to_linear_map_symm (f : M₂ →ₛₗ[σ₂₃] M₃) (g : M₁ →ₛₗ[σ₁₃] M₃) : f = g.comp e₁₂.symm.to_linear_map ↔ f.comp e₁₂.to_linear_map = g := begin split; intro H; ext, { simp [H, e₁₂.to_equiv.eq_comp_symm f g] }, { simp [←H, ←e₁₂.to_equiv.eq_comp_symm f g] } end lemma comp_to_linear_map_symm_eq (f : M₂ →ₛₗ[σ₂₃] M₃) (g : M₁ →ₛₗ[σ₁₃] M₃) : g.comp e₁₂.symm.to_linear_map = f ↔ g = f.comp e₁₂.to_linear_map := begin split; intro H; ext, { simp [←H, ←e₁₂.to_equiv.comp_symm_eq f g] }, { simp [H, e₁₂.to_equiv.comp_symm_eq f g] } end lemma eq_to_linear_map_symm_comp (f : M₃ →ₛₗ[σ₃₁] M₁) (g : M₃ →ₛₗ[σ₃₂] M₂) : f = e₁₂.symm.to_linear_map.comp g ↔ e₁₂.to_linear_map.comp f = g := begin split; intro H; ext, { simp [H, e₁₂.to_equiv.eq_symm_comp f g] }, { simp [←H, ←e₁₂.to_equiv.eq_symm_comp f g] } end lemma to_linear_map_symm_comp_eq (f : M₃ →ₛₗ[σ₃₁] M₁) (g : M₃ →ₛₗ[σ₃₂] M₂) : e₁₂.symm.to_linear_map.comp g = f ↔ g = e₁₂.to_linear_map.comp f := begin split; intro H; ext, { simp [←H, ←e₁₂.to_equiv.symm_comp_eq f g] }, { simp [H, e₁₂.to_equiv.symm_comp_eq f g] } end omit module_M₃ @[simp] lemma refl_symm [module R M] : (refl R M).symm = linear_equiv.refl R M := rfl include re₁₂ re₂₁ module_M₁ module_M₂ @[simp] lemma self_trans_symm (f : M₁ ≃ₛₗ[σ₁₂] M₂) : f.trans f.symm = linear_equiv.refl R₁ M₁ := by { ext x, simp } @[simp] lemma symm_trans_self (f : M₁ ≃ₛₗ[σ₁₂] M₂) : f.symm.trans f = linear_equiv.refl R₂ M₂ := by { ext x, simp } omit re₁₂ re₂₁ module_M₁ module_M₂ @[simp, norm_cast] lemma refl_to_linear_map [module R M] : (linear_equiv.refl R M : M →ₗ[R] M) = linear_map.id := rfl @[simp, norm_cast] lemma comp_coe [module R M] [module R M₂] [module R M₃] (f : M ≃ₗ[R] M₂) (f' : M₂ ≃ₗ[R] M₃) : (f' : M₂ →ₗ[R] M₃).comp (f : M →ₗ[R] M₂) = (f.trans f' : M ≃ₗ[R] M₃) := rfl @[simp] lemma mk_coe (h₁ h₂ f h₃ h₄) : (linear_equiv.mk e h₁ h₂ f h₃ h₄ : M ≃ₛₗ[σ] M₂) = e := ext $ λ _, rfl protected theorem map_add (a b : M) : e (a + b) = e a + e b := map_add e a b protected theorem map_zero : e 0 = 0 := map_zero e -- TODO: `simp` isn't picking up `map_smulₛₗ` for `linear_equiv`s without specifying `map_smulₛₗ f` @[simp] protected theorem map_smulₛₗ (c : R) (x : M) : e (c • x) = (σ c) • e x := e.map_smul' c x include module_N₁ module_N₂ theorem map_smul (e : N₁ ≃ₗ[R₁] N₂) (c : R₁) (x : N₁) : e (c • x) = c • e x := map_smulₛₗ e c x omit module_N₁ module_N₂ @[simp] theorem map_eq_zero_iff {x : M} : e x = 0 ↔ x = 0 := e.to_add_equiv.map_eq_zero_iff theorem map_ne_zero_iff {x : M} : e x ≠ 0 ↔ x ≠ 0 := e.to_add_equiv.map_ne_zero_iff include module_M module_S_M₂ re₁ re₂ @[simp] theorem symm_symm (e : M ≃ₛₗ[σ] M₂): e.symm.symm = e := by { cases e, refl } omit module_M module_S_M₂ re₁ re₂ lemma symm_bijective [module R M] [module S M₂] [ring_hom_inv_pair σ' σ] [ring_hom_inv_pair σ σ'] : function.bijective (symm : (M ≃ₛₗ[σ] M₂) → (M₂ ≃ₛₗ[σ'] M)) := equiv.bijective ⟨(symm : (M ≃ₛₗ[σ] M₂) → (M₂ ≃ₛₗ[σ'] M)), (symm : (M₂ ≃ₛₗ[σ'] M) → (M ≃ₛₗ[σ] M₂)), symm_symm, symm_symm⟩ @[simp] lemma mk_coe' (f h₁ h₂ h₃ h₄) : (linear_equiv.mk f h₁ h₂ ⇑e h₃ h₄ : M₂ ≃ₛₗ[σ'] M) = e.symm := symm_bijective.injective $ ext $ λ x, rfl @[simp] theorem symm_mk (f h₁ h₂ h₃ h₄) : (⟨e, h₁, h₂, f, h₃, h₄⟩ : M ≃ₛₗ[σ] M₂).symm = { to_fun := f, inv_fun := e, ..(⟨e, h₁, h₂, f, h₃, h₄⟩ : M ≃ₛₗ[σ] M₂).symm } := rfl @[simp] lemma coe_symm_mk [module R M] [module R M₂] {to_fun inv_fun map_add map_smul left_inv right_inv} : ⇑((⟨to_fun, map_add, map_smul, inv_fun, left_inv, right_inv⟩ : M ≃ₗ[R] M₂).symm) = inv_fun := rfl protected lemma bijective : function.bijective e := e.to_equiv.bijective protected lemma injective : function.injective e := e.to_equiv.injective protected lemma surjective : function.surjective e := e.to_equiv.surjective protected lemma image_eq_preimage (s : set M) : e '' s = e.symm ⁻¹' s := e.to_equiv.image_eq_preimage s protected lemma image_symm_eq_preimage (s : set M₂) : e.symm '' s = e ⁻¹' s := e.to_equiv.symm.image_eq_preimage s end /-- Interpret a `ring_equiv` `f` as an `f`-semilinear equiv. -/ @[simps] def _root_.ring_equiv.to_semilinear_equiv (f : R ≃+* S) : by haveI := ring_hom_inv_pair.of_ring_equiv f; haveI := ring_hom_inv_pair.symm (↑f : R →+* S) (f.symm : S →+* R); exact (R ≃ₛₗ[(↑f : R →+* S)] S) := by exact { to_fun := f, map_smul' := f.map_mul, .. f} variables [semiring R₁] [semiring R₂] [semiring R₃] variables [add_comm_monoid M] [add_comm_monoid M₁] [add_comm_monoid M₂] /-- An involutive linear map is a linear equivalence. -/ def of_involutive {σ σ' : R →+* R} [ring_hom_inv_pair σ σ'] [ring_hom_inv_pair σ' σ] {module_M : module R M} (f : M →ₛₗ[σ] M) (hf : involutive f) : M ≃ₛₗ[σ] M := { .. f, .. hf.to_perm f } @[simp] lemma coe_of_involutive {σ σ' : R →+* R} [ring_hom_inv_pair σ σ'] [ring_hom_inv_pair σ' σ] {module_M : module R M} (f : M →ₛₗ[σ] M) (hf : involutive f) : ⇑(of_involutive f hf) = f := rfl section restrict_scalars variables (R) [module R M] [module R M₂] [module S M] [module S M₂] [linear_map.compatible_smul M M₂ R S] /-- If `M` and `M₂` are both `R`-semimodules and `S`-semimodules and `R`-semimodule structures are defined by an action of `R` on `S` (formally, we have two scalar towers), then any `S`-linear equivalence from `M` to `M₂` is also an `R`-linear equivalence. See also `linear_map.restrict_scalars`. -/ @[simps] def restrict_scalars (f : M ≃ₗ[S] M₂) : M ≃ₗ[R] M₂ := { to_fun := f, inv_fun := f.symm, left_inv := f.left_inv, right_inv := f.right_inv, .. f.to_linear_map.restrict_scalars R } lemma restrict_scalars_injective : function.injective (restrict_scalars R : (M ≃ₗ[S] M₂) → (M ≃ₗ[R] M₂)) := λ f g h, ext (linear_equiv.congr_fun h : _) @[simp] lemma restrict_scalars_inj (f g : M ≃ₗ[S] M₂) : f.restrict_scalars R = g.restrict_scalars R ↔ f = g := (restrict_scalars_injective R).eq_iff end restrict_scalars section automorphisms variables [module R M] instance automorphism_group : group (M ≃ₗ[R] M) := { mul := λ f g, g.trans f, one := linear_equiv.refl R M, inv := λ f, f.symm, mul_assoc := λ f g h, rfl, mul_one := λ f, ext $ λ x, rfl, one_mul := λ f, ext $ λ x, rfl, mul_left_inv := λ f, ext $ f.left_inv } /-- Restriction from `R`-linear automorphisms of `M` to `R`-linear endomorphisms of `M`, promoted to a monoid hom. -/ @[simps] def automorphism_group.to_linear_map_monoid_hom : (M ≃ₗ[R] M) →* (M →ₗ[R] M) := { to_fun := coe, map_one' := rfl, map_mul' := λ _ _, rfl } /-- The tautological action by `M ≃ₗ[R] M` on `M`. This generalizes `function.End.apply_mul_action`. -/ instance apply_distrib_mul_action : distrib_mul_action (M ≃ₗ[R] M) M := { smul := ($), smul_zero := linear_equiv.map_zero, smul_add := linear_equiv.map_add, one_smul := λ _, rfl, mul_smul := λ _ _ _, rfl } @[simp] protected lemma smul_def (f : M ≃ₗ[R] M) (a : M) : f • a = f a := rfl /-- `linear_equiv.apply_distrib_mul_action` is faithful. -/ instance apply_has_faithful_smul : has_faithful_smul (M ≃ₗ[R] M) M := ⟨λ _ _, linear_equiv.ext⟩ instance apply_smul_comm_class : smul_comm_class R (M ≃ₗ[R] M) M := { smul_comm := λ r e m, (e.map_smul r m).symm } instance apply_smul_comm_class' : smul_comm_class (M ≃ₗ[R] M) R M := { smul_comm := linear_equiv.map_smul } end automorphisms section of_subsingleton variables (M M₂) [module R M] [module R M₂] [subsingleton M] [subsingleton M₂] /-- Any two modules that are subsingletons are isomorphic. -/ @[simps] def of_subsingleton : M ≃ₗ[R] M₂ := { to_fun := λ _, 0, inv_fun := λ _, 0, left_inv := λ x, subsingleton.elim _ _, right_inv := λ x, subsingleton.elim _ _, .. (0 : M →ₗ[R] M₂)} @[simp] lemma of_subsingleton_self : of_subsingleton M M = refl R M := by { ext, simp } end of_subsingleton end add_comm_monoid end linear_equiv namespace module /-- `g : R ≃+* S` is `R`-linear when the module structure on `S` is `module.comp_hom S g` . -/ @[simps] def comp_hom.to_linear_equiv {R S : Type*} [semiring R] [semiring S] (g : R ≃+* S) : (by haveI := comp_hom S (↑g : R →+* S); exact (R ≃ₗ[R] S)) := by exact { to_fun := (g : R → S), inv_fun := (g.symm : S → R), map_smul' := g.map_mul, ..g } end module namespace distrib_mul_action variables (R M) [semiring R] [add_comm_monoid M] [module R M] variables [group S] [distrib_mul_action S M] [smul_comm_class S R M] /-- Each element of the group defines a linear equivalence. This is a stronger version of `distrib_mul_action.to_add_equiv`. -/ @[simps] def to_linear_equiv (s : S) : M ≃ₗ[R] M := { ..to_add_equiv M s, ..to_linear_map R M s } /-- Each element of the group defines a module automorphism. This is a stronger version of `distrib_mul_action.to_add_aut`. -/ @[simps] def to_module_aut : S →* M ≃ₗ[R] M := { to_fun := to_linear_equiv R M, map_one' := linear_equiv.ext $ one_smul _, map_mul' := λ a b, linear_equiv.ext $ mul_smul _ _ } end distrib_mul_action namespace add_equiv section add_comm_monoid variables [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] variables [module R M] [module R M₂] variable (e : M ≃+ M₂) /-- An additive equivalence whose underlying function preserves `smul` is a linear equivalence. -/ def to_linear_equiv (h : ∀ (c : R) x, e (c • x) = c • e x) : M ≃ₗ[R] M₂ := { map_smul' := h, .. e, } @[simp] lemma coe_to_linear_equiv (h : ∀ (c : R) x, e (c • x) = c • e x) : ⇑(e.to_linear_equiv h) = e := rfl @[simp] lemma coe_to_linear_equiv_symm (h : ∀ (c : R) x, e (c • x) = c • e x) : ⇑(e.to_linear_equiv h).symm = e.symm := rfl /-- An additive equivalence between commutative additive monoids is a linear equivalence between ℕ-modules -/ def to_nat_linear_equiv : M ≃ₗ[ℕ] M₂ := e.to_linear_equiv $ λ c a, by { erw e.to_add_monoid_hom.map_nsmul, refl } @[simp] lemma coe_to_nat_linear_equiv : ⇑(e.to_nat_linear_equiv) = e := rfl @[simp] lemma to_nat_linear_equiv_to_add_equiv : e.to_nat_linear_equiv.to_add_equiv = e := by { ext, refl } @[simp] lemma _root_.linear_equiv.to_add_equiv_to_nat_linear_equiv (e : M ≃ₗ[ℕ] M₂) : e.to_add_equiv.to_nat_linear_equiv = e := fun_like.coe_injective rfl @[simp] lemma to_nat_linear_equiv_symm : (e.to_nat_linear_equiv).symm = e.symm.to_nat_linear_equiv := rfl @[simp] lemma to_nat_linear_equiv_refl : ((add_equiv.refl M).to_nat_linear_equiv) = linear_equiv.refl ℕ M := rfl @[simp] lemma to_nat_linear_equiv_trans (e₂ : M₂ ≃+ M₃) : (e.to_nat_linear_equiv).trans (e₂.to_nat_linear_equiv) = (e.trans e₂).to_nat_linear_equiv := rfl end add_comm_monoid section add_comm_group variables [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variable (e : M ≃+ M₂) /-- An additive equivalence between commutative additive groups is a linear equivalence between ℤ-modules -/ def to_int_linear_equiv : M ≃ₗ[ℤ] M₂ := e.to_linear_equiv $ λ c a, e.to_add_monoid_hom.map_zsmul a c @[simp] lemma coe_to_int_linear_equiv : ⇑(e.to_int_linear_equiv) = e := rfl @[simp] lemma to_int_linear_equiv_to_add_equiv : e.to_int_linear_equiv.to_add_equiv = e := by { ext, refl } @[simp] lemma _root_.linear_equiv.to_add_equiv_to_int_linear_equiv (e : M ≃ₗ[ℤ] M₂) : e.to_add_equiv.to_int_linear_equiv = e := fun_like.coe_injective rfl @[simp] lemma to_int_linear_equiv_symm : (e.to_int_linear_equiv).symm = e.symm.to_int_linear_equiv := rfl @[simp] lemma to_int_linear_equiv_refl : ((add_equiv.refl M).to_int_linear_equiv) = linear_equiv.refl ℤ M := rfl @[simp] lemma to_int_linear_equiv_trans (e₂ : M₂ ≃+ M₃) : (e.to_int_linear_equiv).trans (e₂.to_int_linear_equiv) = (e.trans e₂).to_int_linear_equiv := rfl end add_comm_group end add_equiv
fe40ec019f4ee477c5382dd0481739ecc5f80582
a726f88081e44db9edfd14d32cfe9c4393ee56a4
/src/game/world8/level7.lean
6b5152253e9caa589b036774915fcea35c6c2673
[]
no_license
b-mehta/natural_number_game
80451bf10277adc89a55dbe8581692c36d822462
9faf799d0ab48ecbc89b3d70babb65ba64beee3b
refs/heads/master
1,598,525,389,186
1,573,516,674,000
1,573,516,674,000
217,339,684
0
0
null
1,571,933,100,000
1,571,933,099,000
null
UTF-8
Lean
false
false
641
lean
import game.world8.level6 -- hide namespace mynat -- hide /- # Advanced Addition World ## Level 7: `add_right_cancel_iff` It's sometimes convenient to have the "if and only if" version of theorems like `add_right_cancel`. Remember that you can use `split` to split an `↔` goal into the `→` goal and the `←` goal. -/ /- Theorem For all naturals $a$, $b$ and $t$, $$ a + t = b + t\iff a=b. $$ -/ theorem add_right_cancel_iff (t a b : mynat) : a + t = b + t ↔ a = b := begin [less_leaky] split, { exact add_right_cancel}, -- done that way already, { intro H, -- H : a = b, rw H, refl, } end end mynat -- hide
b27411139f894b2a15b31b956eb74bcc1e1de278
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/pfunctor/multivariate/W.lean
0db36a3207b4cc86cfbece9bf43a336b66bd1268
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
11,903
lean
/- Copyright (c) 2018 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad, Simon Hudon -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.pfunctor.multivariate.basic import Mathlib.PostPort universes u l u_1 u_2 namespace Mathlib /-! # The W construction as a multivariate polynomial functor. W types are well-founded tree-like structures. They are defined as the least fixpoint of a polynomial functor. ## Main definitions * `W_mk` - constructor * `W_dest - destructor * `W_rec` - recursor: basis for defining functions by structural recursion on `P.W α` * `W_rec_eq` - defining equation for `W_rec` * `W_ind` - induction principle for `P.W α` ## Implementation notes Three views of M-types: * `Wp`: polynomial functor * `W`: data type inductively defined by a triple: shape of the root, data in the root and children of the root * `W`: least fixed point of a polynomial functor Specifically, we define the polynomial functor `Wp` as: * A := a tree-like structure without information in the nodes * B := given the tree-like structure `t`, `B t` is a valid path (specified inductively by `W_path`) from the root of `t` to any given node. As a result `Wp.obj α` is made of a dataless tree and a function from its valid paths to values of `α` ## Reference * [Jeremy Avigad, Mario M. Carneiro and Simon Hudon, *Data Types as Quotients of Polynomial Functors*][avigad-carneiro-hudon2019] -/ namespace mvpfunctor /-- A path from the root of a tree to one of its node -/ inductive W_path {n : ℕ} (P : mvpfunctor (n + 1)) : pfunctor.W (last P) → fin2 n → Type u where | root : (a : A P) → (f : pfunctor.B (last P) a → pfunctor.W (last P)) → (i : fin2 n) → B (drop P) a i → W_path P (W_type.mk a f) i | child : (a : A P) → (f : pfunctor.B (last P) a → pfunctor.W (last P)) → (i : fin2 n) → (j : pfunctor.B (last P) a) → W_path P (f j) i → W_path P (W_type.mk a f) i protected instance W_path.inhabited {n : ℕ} (P : mvpfunctor (n + 1)) (x : pfunctor.W (last P)) {i : fin2 n} [I : Inhabited (B (drop P) (pfunctor.W.head x) i)] : Inhabited (W_path P x i) := { default := sorry } /-- Specialized destructor on `W_path` -/ def W_path_cases_on {n : ℕ} (P : mvpfunctor (n + 1)) {α : typevec n} {a : A P} {f : pfunctor.B (last P) a → pfunctor.W (last P)} (g' : typevec.arrow (B (drop P) a) α) (g : (j : pfunctor.B (last P) a) → typevec.arrow (W_path P (f j)) α) : typevec.arrow (W_path P (W_type.mk a f)) α := sorry /-- Specialized destructor on `W_path` -/ def W_path_dest_left {n : ℕ} (P : mvpfunctor (n + 1)) {α : typevec n} {a : A P} {f : pfunctor.B (last P) a → pfunctor.W (last P)} (h : typevec.arrow (W_path P (W_type.mk a f)) α) : typevec.arrow (B (drop P) a) α := fun (i : fin2 n) (c : B (drop P) a i) => h i (W_path.root a f i c) /-- Specialized destructor on `W_path` -/ def W_path_dest_right {n : ℕ} (P : mvpfunctor (n + 1)) {α : typevec n} {a : A P} {f : pfunctor.B (last P) a → pfunctor.W (last P)} (h : typevec.arrow (W_path P (W_type.mk a f)) α) (j : pfunctor.B (last P) a) : typevec.arrow (W_path P (f j)) α := fun (i : fin2 n) (c : W_path P (f j) i) => h i (W_path.child a f i j c) theorem W_path_dest_left_W_path_cases_on {n : ℕ} (P : mvpfunctor (n + 1)) {α : typevec n} {a : A P} {f : pfunctor.B (last P) a → pfunctor.W (last P)} (g' : typevec.arrow (B (drop P) a) α) (g : (j : pfunctor.B (last P) a) → typevec.arrow (W_path P (f j)) α) : W_path_dest_left P (W_path_cases_on P g' g) = g' := rfl theorem W_path_dest_right_W_path_cases_on {n : ℕ} (P : mvpfunctor (n + 1)) {α : typevec n} {a : A P} {f : pfunctor.B (last P) a → pfunctor.W (last P)} (g' : typevec.arrow (B (drop P) a) α) (g : (j : pfunctor.B (last P) a) → typevec.arrow (W_path P (f j)) α) : W_path_dest_right P (W_path_cases_on P g' g) = g := rfl theorem W_path_cases_on_eta {n : ℕ} (P : mvpfunctor (n + 1)) {α : typevec n} {a : A P} {f : pfunctor.B (last P) a → pfunctor.W (last P)} (h : typevec.arrow (W_path P (W_type.mk a f)) α) : W_path_cases_on P (W_path_dest_left P h) (W_path_dest_right P h) = h := sorry theorem comp_W_path_cases_on {n : ℕ} (P : mvpfunctor (n + 1)) {α : typevec n} {β : typevec n} (h : typevec.arrow α β) {a : A P} {f : pfunctor.B (last P) a → pfunctor.W (last P)} (g' : typevec.arrow (B (drop P) a) α) (g : (j : pfunctor.B (last P) a) → typevec.arrow (W_path P (f j)) α) : typevec.comp h (W_path_cases_on P g' g) = W_path_cases_on P (typevec.comp h g') fun (i : pfunctor.B (last P) a) => typevec.comp h (g i) := sorry /-- Polynomial functor for the W-type of `P`. `A` is a data-less well-founded tree whereas, for a given `a : A`, `B a` is a valid path in tree `a` so that `Wp.obj α` is made of a tree and a function from its valid paths to the values it contains -/ def Wp {n : ℕ} (P : mvpfunctor (n + 1)) : mvpfunctor n := mk (pfunctor.W (last P)) (W_path P) /-- W-type of `P` -/ def W {n : ℕ} (P : mvpfunctor (n + 1)) (α : typevec n) := obj (Wp P) α protected instance mvfunctor_W {n : ℕ} (P : mvpfunctor (n + 1)) : mvfunctor (W P) := id (obj.mvfunctor (Wp P)) /-! First, describe operations on `W` as a polynomial functor. -/ /-- Constructor for `Wp` -/ def Wp_mk {n : ℕ} (P : mvpfunctor (n + 1)) {α : typevec n} (a : A P) (f : pfunctor.B (last P) a → pfunctor.W (last P)) (f' : typevec.arrow (W_path P (W_type.mk a f)) α) : W P α := sigma.mk (W_type.mk a f) f' /-- Recursor for `Wp` -/ def Wp_rec {n : ℕ} (P : mvpfunctor (n + 1)) {α : typevec n} {C : Type u_2} (g : (a : A P) → (f : pfunctor.B (last P) a → pfunctor.W (last P)) → typevec.arrow (W_path P (W_type.mk a f)) α → (pfunctor.B (last P) a → C) → C) (x : pfunctor.W (last P)) (f' : typevec.arrow (W_path P x) α) : C := sorry theorem Wp_rec_eq {n : ℕ} (P : mvpfunctor (n + 1)) {α : typevec n} {C : Type u_2} (g : (a : A P) → (f : pfunctor.B (last P) a → pfunctor.W (last P)) → typevec.arrow (W_path P (W_type.mk a f)) α → (pfunctor.B (last P) a → C) → C) (a : A P) (f : pfunctor.B (last P) a → pfunctor.W (last P)) (f' : typevec.arrow (W_path P (W_type.mk a f)) α) : Wp_rec P g (W_type.mk a f) f' = g a f f' fun (i : pfunctor.B (last P) a) => Wp_rec P g (f i) (W_path_dest_right P f' i) := rfl -- Note: we could replace Prop by Type* and obtain a dependent recursor theorem Wp_ind {n : ℕ} (P : mvpfunctor (n + 1)) {α : typevec n} {C : (x : pfunctor.W (last P)) → typevec.arrow (W_path P x) α → Prop} (ih : ∀ (a : A P) (f : pfunctor.B (last P) a → pfunctor.W (last P)) (f' : typevec.arrow (W_path P (W_type.mk a f)) α), (∀ (i : pfunctor.B (last P) a), C (f i) (W_path_dest_right P f' i)) → C (W_type.mk a f) f') (x : pfunctor.W (last P)) (f' : typevec.arrow (W_path P x) α) : C x f' := sorry /-! Now think of W as defined inductively by the data ⟨a, f', f⟩ where - `a : P.A` is the shape of the top node - `f' : P.drop.B a ⟹ α` is the contents of the top node - `f : P.last.B a → P.last.W` are the subtrees -/ /-- Constructor for `W` -/ def W_mk {n : ℕ} (P : mvpfunctor (n + 1)) {α : typevec n} (a : A P) (f' : typevec.arrow (B (drop P) a) α) (f : pfunctor.B (last P) a → W P α) : W P α := let g : pfunctor.B (last P) a → pfunctor.W (last P) := fun (i : pfunctor.B (last P) a) => sigma.fst (f i); let g' : typevec.arrow (W_path P (W_type.mk a g)) α := W_path_cases_on P f' fun (i : pfunctor.B (last P) a) => sigma.snd (f i); sigma.mk (W_type.mk a g) g' /-- Recursor for `W` -/ def W_rec {n : ℕ} (P : mvpfunctor (n + 1)) {α : typevec n} {C : Type u_1} (g : (a : A P) → typevec.arrow (B (drop P) a) α → (pfunctor.B (last P) a → W P α) → (pfunctor.B (last P) a → C) → C) : W P α → C := sorry /-- Defining equation for the recursor of `W` -/ theorem W_rec_eq {n : ℕ} (P : mvpfunctor (n + 1)) {α : typevec n} {C : Type u_1} (g : (a : A P) → typevec.arrow (B (drop P) a) α → (pfunctor.B (last P) a → W P α) → (pfunctor.B (last P) a → C) → C) (a : A P) (f' : typevec.arrow (B (drop P) a) α) (f : pfunctor.B (last P) a → W P α) : W_rec P g (W_mk P a f' f) = g a f' f fun (i : pfunctor.B (last P) a) => W_rec P g (f i) := sorry /-- Induction principle for `W` -/ theorem W_ind {n : ℕ} (P : mvpfunctor (n + 1)) {α : typevec n} {C : W P α → Prop} (ih : ∀ (a : A P) (f' : typevec.arrow (B (drop P) a) α) (f : pfunctor.B (last P) a → W P α), (∀ (i : pfunctor.B (last P) a), C (f i)) → C (W_mk P a f' f)) (x : W P α) : C x := sorry theorem W_cases {n : ℕ} (P : mvpfunctor (n + 1)) {α : typevec n} {C : W P α → Prop} (ih : ∀ (a : A P) (f' : typevec.arrow (B (drop P) a) α) (f : pfunctor.B (last P) a → W P α), C (W_mk P a f' f)) (x : W P α) : C x := W_ind P fun (a : A P) (f' : typevec.arrow (B (drop P) a) α) (f : pfunctor.B (last P) a → W P α) (ih' : ∀ (i : pfunctor.B (last P) a), C (f i)) => ih a f' f /-- W-types are functorial -/ def W_map {n : ℕ} (P : mvpfunctor (n + 1)) {α : typevec n} {β : typevec n} (g : typevec.arrow α β) : W P α → W P β := fun (x : W P α) => mvfunctor.map g x theorem W_mk_eq {n : ℕ} (P : mvpfunctor (n + 1)) {α : typevec n} (a : A P) (f : pfunctor.B (last P) a → pfunctor.W (last P)) (g' : typevec.arrow (B (drop P) a) α) (g : (j : pfunctor.B (last P) a) → typevec.arrow (W_path P (f j)) α) : (W_mk P a g' fun (i : pfunctor.B (last P) a) => sigma.mk (f i) (g i)) = sigma.mk (W_type.mk a f) (W_path_cases_on P g' g) := rfl theorem W_map_W_mk {n : ℕ} (P : mvpfunctor (n + 1)) {α : typevec n} {β : typevec n} (g : typevec.arrow α β) (a : A P) (f' : typevec.arrow (B (drop P) a) α) (f : pfunctor.B (last P) a → W P α) : mvfunctor.map g (W_mk P a f' f) = W_mk P a (typevec.comp g f') fun (i : pfunctor.B (last P) a) => mvfunctor.map g (f i) := sorry -- TODO: this technical theorem is used in one place in constructing the initial algebra. -- Can it be avoided? /-- Constructor of a value of `P.obj (α ::: β)` from components. Useful to avoid complicated type annotation -/ def obj_append1 {n : ℕ} (P : mvpfunctor (n + 1)) {α : typevec n} {β : Type u} (a : A P) (f' : typevec.arrow (B (drop P) a) α) (f : pfunctor.B (last P) a → β) : obj P (α ::: β) := sigma.mk a (typevec.split_fun f' f) theorem map_obj_append1 {n : ℕ} (P : mvpfunctor (n + 1)) {α : typevec n} {γ : typevec n} (g : typevec.arrow α γ) (a : A P) (f' : typevec.arrow (B (drop P) a) α) (f : pfunctor.B (last P) a → W P α) : mvfunctor.map (g ::: W_map P g) (obj_append1 P a f' f) = obj_append1 P a (typevec.comp g f') fun (x : pfunctor.B (last P) a) => W_map P g (f x) := sorry /-! Yet another view of the W type: as a fixed point for a multivariate polynomial functor. These are needed to use the W-construction to construct a fixed point of a qpf, since the qpf axioms are expressed in terms of `map` on `P`. -/ /-- Constructor for the W-type of `P` -/ def W_mk' {n : ℕ} (P : mvpfunctor (n + 1)) {α : typevec n} : obj P (α ::: W P α) → W P α := sorry /-- Destructor for the W-type of `P` -/ def W_dest' {n : ℕ} (P : mvpfunctor (n + 1)) {α : typevec n} : W P α → obj P (α ::: W P α) := W_rec P fun (a : A P) (f' : typevec.arrow (B (drop P) a) α) (f : pfunctor.B (last P) a → W P α) (_x : pfunctor.B (last P) a → obj P (α ::: W P α)) => sigma.mk a (typevec.split_fun f' f) theorem W_dest'_W_mk {n : ℕ} (P : mvpfunctor (n + 1)) {α : typevec n} (a : A P) (f' : typevec.arrow (B (drop P) a) α) (f : pfunctor.B (last P) a → W P α) : W_dest' P (W_mk P a f' f) = sigma.mk a (typevec.split_fun f' f) := sorry theorem W_dest'_W_mk' {n : ℕ} (P : mvpfunctor (n + 1)) {α : typevec n} (x : obj P (α ::: W P α)) : W_dest' P (W_mk' P x) = x := sorry
312c76b426702f700037a1ff268147e2313f443c
63abd62053d479eae5abf4951554e1064a4c45b4
/src/measure_theory/category/Meas.lean
328dfac331bb7754dd1b30d978524d085eec5d90
[ "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
4,264
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 -/ import topology.category.Top.basic import measure_theory.giry_monad import category_theory.monad.algebra /- * Meas, the category of measurable spaces Measurable spaces and measurable functions form a (concrete) category Meas. Measure : Meas ⥤ Meas is the functor which sends a measurable space X to the space of measures on X; it is a monad (the "Giry monad"). Borel : Top ⥤ Meas sends a topological space X to X equipped with the σ-algebra of Borel sets (the σ-algebra generated by the open subsets of X). ## Tags measurable space, giry monad, borel -/ noncomputable theory open category_theory measure_theory universes u v /-- The category of measurable spaces and measurable functions. -/ @[derive has_coe_to_sort] def Meas : Type (u+1) := bundled measurable_space namespace Meas instance (X : Meas) : measurable_space X := X.str /-- Construct a bundled `Meas` from the underlying type and the typeclass. -/ def of (α : Type u) [measurable_space α] : Meas := ⟨α⟩ @[simp] lemma coe_of (X : Type u) [measurable_space X] : (of X : Type u) = X := rfl instance unbundled_hom : unbundled_hom @measurable := ⟨@measurable_id, @measurable.comp⟩ attribute [derive [large_category, concrete_category]] Meas instance : inhabited Meas := ⟨Meas.of empty⟩ /-- `Measure X` is the measurable space of measures over the measurable space `X`. It is the weakest measurable space, s.t. λμ, μ s is measurable for all measurable sets `s` in `X`. An important purpose is to assign a monadic structure on it, the Giry monad. In the Giry monad, the pure values are the Dirac measure, and the bind operation maps to the integral: `(μ >>= ν) s = ∫ x. (ν x) s dμ`. In probability theory, the `Meas`-morphisms `X → Prob X` are (sub-)Markov kernels (here `Prob` is the restriction of `Measure` to (sub-)probability space.) -/ def Measure : Meas ⥤ Meas := { obj := λX, ⟨@measure_theory.measure X.1 X.2⟩, map := λX Y f, ⟨measure.map (f : X → Y), measure.measurable_map f f.2⟩, map_id' := assume ⟨α, I⟩, subtype.eq $ funext $ assume μ, @measure.map_id α I μ, map_comp':= assume X Y Z ⟨f, hf⟩ ⟨g, hg⟩, subtype.eq $ funext $ assume μ, (measure.map_map hg hf).symm } /-- The Giry monad, i.e. the monadic structure associated with `Measure`. -/ instance : category_theory.monad Measure.{u} := { η := { app := λX, ⟨@measure.dirac X.1 X.2, measure.measurable_dirac⟩, naturality' := assume X Y ⟨f, hf⟩, subtype.eq $ funext $ assume a, (measure.map_dirac hf a).symm }, μ := { app := λX, ⟨@measure.join X.1 X.2, measure.measurable_join⟩, naturality' := assume X Y ⟨f, hf⟩, subtype.eq $ funext $ assume μ, measure.join_map_map hf μ }, assoc' := assume ⟨α, I⟩, subtype.eq $ funext $ assume μ, @measure.join_map_join α I μ, left_unit' := assume ⟨α, I⟩, subtype.eq $ funext $ assume μ, @measure.join_dirac α I μ, right_unit' := assume ⟨α, I⟩, subtype.eq $ funext $ assume μ, @measure.join_map_dirac α I μ } /-- An example for an algebra on `Measure`: the nonnegative Lebesgue integral is a hom, behaving nicely under the monad operations. -/ def Integral : monad.algebra Measure := { A := Meas.of ennreal , a := ⟨λm:measure ennreal, ∫⁻ x, x ∂m, measure.measurable_lintegral measurable_id ⟩, unit' := subtype.eq $ funext $ assume r:ennreal, lintegral_dirac _ measurable_id, assoc' := subtype.eq $ funext $ assume μ : measure (measure ennreal), show ∫⁻ x, x ∂ μ.join = ∫⁻ x, x ∂ (measure.map (λm:measure ennreal, ∫⁻ x, x ∂m) μ), by rw [measure.lintegral_join, lintegral_map]; apply_rules [measurable_id, measure.measurable_lintegral] } end Meas instance Top.has_forget_to_Meas : has_forget₂ Top.{u} Meas.{u} := bundled_hom.mk_has_forget₂ borel (λ X Y f, ⟨f.1, f.2.borel_measurable⟩) (by intros; refl) /-- The Borel functor, the canonical embedding of topological spaces into measurable spaces. -/ @[reducible] def Borel : Top.{u} ⥤ Meas.{u} := forget₂ Top.{u} Meas.{u}
69ef8d7beb38644fb4a267f86c4f99d01c9a9177
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/tactic/monotonicity/basic.lean
c613c21aae897b1ed0fb934ca19beda019f21c88
[ "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
5,531
lean
/- Copyright (c) 2019 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import order.bounded_lattice namespace tactic.interactive open tactic list open lean lean.parser interactive open interactive.types @[derive inhabited] structure mono_cfg := (unify := ff) @[derive [decidable_eq, has_reflect, inhabited]] inductive mono_selection : Type | left : mono_selection | right : mono_selection | both : mono_selection declare_trace mono.relation section compare parameter opt : mono_cfg meta def compare (e₀ e₁ : expr) : tactic unit := do if opt.unify then do guard (¬ e₀.is_mvar ∧ ¬ e₁.is_mvar), unify e₀ e₁ else is_def_eq e₀ e₁ meta def find_one_difference : list expr → list expr → tactic (list expr × expr × expr × list expr) | (x :: xs) (y :: ys) := do c ← try_core (compare x y), if c.is_some then prod.map (cons x) id <$> find_one_difference xs ys else do guard (xs.length = ys.length), mzip_with' compare xs ys, return ([],x,y,xs) | xs ys := fail format!"find_one_difference: {xs}, {ys}" end compare def last_two {α : Type*} (l : list α) : option (α × α) := match l.reverse with | (x₁ :: x₀ :: _) := some (x₀, x₁) | _ := none end meta def match_imp : expr → tactic (expr × expr) | `(%%e₀ → %%e₁) := do guard (¬ e₁.has_var), return (e₀,e₁) | _ := failed open expr meta def same_operator : expr → expr → bool | (app e₀ _) (app e₁ _) := let fn₀ := e₀.get_app_fn, fn₁ := e₁.get_app_fn in fn₀.is_constant ∧ fn₀.const_name = fn₁.const_name | (pi _ _ _ _) (pi _ _ _ _) := tt | _ _ := ff meta def get_operator (e : expr) : option name := guard (¬ e.is_pi) >> pure e.get_app_fn.const_name meta def monotonicity.check_rel (l r : expr) : tactic (option name) := do guard (same_operator l r) <|> do { fail format!"{l} and {r} should be the f x and f y for some f" }, if l.is_pi then pure none else pure r.get_app_fn.const_name @[reducible] def mono_key := (with_bot name × with_bot name) meta instance mono_key.has_lt : has_lt mono_key := { lt := prod.lex (<) (<) } open nat meta def mono_head_candidates : ℕ → list expr → expr → tactic mono_key | 0 _ h := fail!"Cannot find relation in {h}" | (succ n) xs h := do { (rel,l,r) ← if h.is_arrow then pure (none,h.binding_domain,h.binding_body) else guard h.get_app_fn.is_constant >> prod.mk (some h.get_app_fn.const_name) <$> last_two h.get_app_args, prod.mk <$> monotonicity.check_rel l r <*> pure rel } <|> match xs with | [] := fail format!"oh? {h}" | (x::xs) := mono_head_candidates n xs (h.pis [x]) end meta def monotonicity.check (lm_n : name) : tactic mono_key := do lm ← mk_const lm_n, lm_t ← infer_type lm >>= instantiate_mvars, when_tracing `mono.relation trace!"[mono] Looking for relation in {lm_t}", let s := simp_lemmas.mk, s ← s.add_simp ``monotone, s ← s.add_simp ``strict_mono, lm_t ← s.dsimplify [] lm_t { fail_if_unchanged := ff }, when_tracing `mono.relation trace!"[mono] Looking for relation in {lm_t} (after unfolding)", (xs,h) ← open_pis lm_t, mono_head_candidates 3 xs.reverse h meta instance : has_to_format mono_selection := ⟨ λ x, match x with | mono_selection.left := "left" | mono_selection.right := "right" | mono_selection.both := "both" end ⟩ meta def side : lean.parser mono_selection := with_desc "expecting 'left', 'right' or 'both' (default)" $ do some n ← optional ident | pure mono_selection.both, if n = `left then pure $ mono_selection.left else if n = `right then pure $ mono_selection.right else if n = `both then pure $ mono_selection.both else fail format!"invalid argument: {n}, expecting 'left', 'right' or 'both' (default)" open function @[user_attribute] meta def monotonicity.attr : user_attribute (native.rb_lmap mono_key (name)) (option mono_key × mono_selection) := { name := `mono , descr := "monotonicity of function `f` wrt relations `R₀` and `R₁`: R₀ x y → R₁ (f x) (f y)" , cache_cfg := { dependencies := [], mk_cache := λ ls, do ps ← ls.mmap monotonicity.attr.get_param, let ps := ps.filter_map prod.fst, pure $ (ps.zip ls).foldl (flip $ uncurry (λ k n m, m.insert k n)) (native.rb_lmap.mk mono_key _) } , after_set := some $ λ n prio p, do { (none,v) ← monotonicity.attr.get_param n | pure (), k ← monotonicity.check n, monotonicity.attr.set n (some k,v) p } , parser := prod.mk none <$> side } meta def filter_instances (e : mono_selection) (ns : list name) : tactic (list name) := ns.mfilter $ λ n, do d ← user_attribute.get_param_untyped monotonicity.attr n, (_,d) ← to_expr ``(id %%d) >>= eval_expr (option mono_key × mono_selection), return (e = d : bool) meta def get_monotonicity_lemmas (k : expr) (e : mono_selection) : tactic (list name) := do ns ← monotonicity.attr.get_cache, k' ← if k.is_pi then pure (get_operator k.binding_domain,none) else do { (x₀,x₁) ← last_two k.get_app_args, pure (get_operator x₀,some k.get_app_fn.const_name) }, let ns := ns.find_def [] k', ns' ← filter_instances e ns, if e ≠ mono_selection.both then (++) ns' <$> filter_instances mono_selection.both ns else pure ns' end tactic.interactive
8a84cafd58fbdb991b41ffd2c2da12ba38f71eda
367134ba5a65885e863bdc4507601606690974c1
/src/group_theory/perm/sign.lean
4384b04c363de678cfeac22cb091b7e5b4a94a6f
[ "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
33,769
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.fintype.basic import data.finset.sort import group_theory.perm.basic import group_theory.order_of_element import tactic.norm_swap /-! # Sign of a permutation The main definition of this file is `equiv.perm.sign`, associating a `units ℤ` sign with a permutation. This file also contains miscellaneous lemmas about `equiv.perm` and `equiv.swap`, building on top of those in `data/equiv/basic` and `data/equiv/perm`. -/ universes u v open equiv function fintype finset open_locale big_operators variables {α : Type u} {β : Type v} namespace equiv.perm /-- `mod_swap i j` contains permutations up to swapping `i` and `j`. We use this to partition permutations in `matrix.det_zero_of_row_eq`, such that each partition sums up to `0`. -/ def mod_swap [decidable_eq α] (i j : α) : setoid (perm α) := ⟨λ σ τ, σ = τ ∨ σ = swap i j * τ, λ σ, or.inl (refl σ), λ σ τ h, or.cases_on h (λ h, or.inl h.symm) (λ h, or.inr (by rw [h, swap_mul_self_mul])), λ σ τ υ hστ hτυ, by cases hστ; cases hτυ; try {rw [hστ, hτυ, swap_mul_self_mul]}; finish⟩ instance {α : Type*} [fintype α] [decidable_eq α] (i j : α) : decidable_rel (mod_swap i j).r := λ σ τ, or.decidable lemma perm_inv_on_of_perm_on_finset {s : finset α} {f : perm α} (h : ∀ x ∈ s, f x ∈ s) {y : α} (hy : y ∈ s) : f⁻¹ y ∈ s := begin have h0 : ∀ y ∈ s, ∃ x (hx : x ∈ s), y = (λ i (hi : i ∈ s), f i) x hx := finset.surj_on_of_inj_on_of_card_le (λ x hx, (λ i hi, f i) x hx) (λ a ha, h a ha) (λ a₁ a₂ ha₁ ha₂ heq, (equiv.apply_eq_iff_eq f).mp heq) rfl.ge, obtain ⟨y2, hy2, heq⟩ := h0 y hy, convert hy2, rw heq, simp only [inv_apply_self] end lemma perm_inv_maps_to_of_maps_to (f : perm α) {s : set α} [fintype s] (h : set.maps_to f s s) : set.maps_to (f⁻¹ : _) s s := λ x hx, set.mem_to_finset.mp $ perm_inv_on_of_perm_on_finset (λ a ha, set.mem_to_finset.mpr (h (set.mem_to_finset.mp ha))) (set.mem_to_finset.mpr hx) @[simp] lemma perm_inv_maps_to_iff_maps_to {f : perm α} {s : set α} [fintype s] : set.maps_to (f⁻¹ : _) s s ↔ set.maps_to f s s := ⟨perm_inv_maps_to_of_maps_to f⁻¹, perm_inv_maps_to_of_maps_to f⟩ lemma perm_inv_on_of_perm_on_fintype {f : perm α} {p : α → Prop} [fintype {x // p x}] (h : ∀ x, p x → p (f x)) {x : α} (hx : p x) : p (f⁻¹ x) := begin letI : fintype ↥(show set α, from p) := ‹fintype {x // p x}›, exact perm_inv_maps_to_of_maps_to f h hx end /-- If the permutation `f` maps `{x // p x}` into itself, then this returns the permutation on `{x // p x}` induced by `f`. Note that the `h` hypothesis is weaker than for `equiv.perm.subtype_perm`. -/ abbreviation subtype_perm_of_fintype (f : perm α) {p : α → Prop} [fintype {x // p x}] (h : ∀ x, p x → p (f x)) : perm {x // p x} := f.subtype_perm (λ x, ⟨h x, λ h₂, f.inv_apply_self x ▸ perm_inv_on_of_perm_on_fintype h h₂⟩) @[simp] lemma subtype_perm_of_fintype_apply (f : perm α) {p : α → Prop} [fintype {x // p x}] (h : ∀ x, p x → p (f x)) (x : {x // p x}) : subtype_perm_of_fintype f h x = ⟨f x, h x x.2⟩ := rfl @[simp] lemma subtype_perm_of_fintype_one (p : α → Prop) [fintype {x // p x}] (h : ∀ x, p x → p ((1 : perm α) x)) : @subtype_perm_of_fintype α 1 p _ h = 1 := equiv.ext $ λ ⟨_, _⟩, rfl lemma perm_maps_to_inl_iff_maps_to_inr {m n : Type*} [fintype m] [fintype n] (σ : equiv.perm (m ⊕ n)) : set.maps_to σ (set.range sum.inl) (set.range sum.inl) ↔ set.maps_to σ (set.range sum.inr) (set.range sum.inr) := begin split; id { intros h, classical, rw ←perm_inv_maps_to_iff_maps_to at h, intro x, cases hx : σ x with l r, }, { rintros ⟨a, rfl⟩, obtain ⟨y, hy⟩ := h ⟨l, rfl⟩, rw [←hx, σ.inv_apply_self] at hy, exact absurd hy sum.inl_ne_inr}, { rintros ⟨a, ha⟩, exact ⟨r, rfl⟩, }, { rintros ⟨a, ha⟩, exact ⟨l, rfl⟩, }, { rintros ⟨a, rfl⟩, obtain ⟨y, hy⟩ := h ⟨r, rfl⟩, rw [←hx, σ.inv_apply_self] at hy, exact absurd hy sum.inr_ne_inl}, end lemma perm_on_inl_iff_perm_on_inr {m n : Type*} [fintype m] [fintype n] (σ : equiv.perm (m ⊕ n)) : (∀ a1, ∃ a2, sum.inl a2 = σ (sum.inl a1)) ↔ ∀ b1, ∃ b2, sum.inr b2 = σ (sum.inr b1) := begin have := perm_maps_to_inl_iff_maps_to_inr σ, rw [set.maps_range_to, set.maps_range_to] at this, convert this; simp end /-- Two permutations `f` and `g` are `disjoint` if their supports are disjoint, i.e., every element is fixed either by `f`, or by `g`. -/ def disjoint (f g : perm α) := ∀ x, f x = x ∨ g x = x @[symm] lemma disjoint.symm {f g : perm α} : disjoint f g → disjoint g f := by simp only [disjoint, or.comm, imp_self] lemma disjoint_comm {f g : perm α} : disjoint f g ↔ disjoint g f := ⟨disjoint.symm, disjoint.symm⟩ lemma disjoint.mul_comm {f g : perm α} (h : disjoint f g) : f * g = g * f := equiv.ext $ λ x, (h x).elim (λ hf, (h (g x)).elim (λ hg, by simp [mul_apply, hf, hg]) (λ hg, by simp [mul_apply, hf, g.injective hg])) (λ hg, (h (f x)).elim (λ hf, by simp [mul_apply, f.injective hf, hg]) (λ hf, by simp [mul_apply, hf, hg])) @[simp] lemma disjoint_one_left (f : perm α) : disjoint 1 f := λ _, or.inl rfl @[simp] lemma disjoint_one_right (f : perm α) : disjoint f 1 := λ _, or.inr rfl lemma disjoint.mul_left {f g h : perm α} (H1 : disjoint f h) (H2 : disjoint g h) : disjoint (f * g) h := λ x, by cases H1 x; cases H2 x; simp * lemma disjoint.mul_right {f g h : perm α} (H1 : disjoint f g) (H2 : disjoint f h) : disjoint f (g * h) := by { rw disjoint_comm, exact H1.symm.mul_left H2.symm } lemma disjoint_prod_right {f : perm α} (l : list (perm α)) (h : ∀ g ∈ l, disjoint f g) : disjoint f l.prod := begin induction l with g l ih, { exact disjoint_one_right _ }, { rw list.prod_cons, exact (h _ (list.mem_cons_self _ _)).mul_right (ih (λ g hg, h g (list.mem_cons_of_mem _ hg))) } end lemma disjoint_prod_perm {l₁ l₂ : list (perm α)} (hl : l₁.pairwise disjoint) (hp : l₁ ~ l₂) : l₁.prod = l₂.prod := hp.prod_eq' $ hl.imp $ λ f g, disjoint.mul_comm lemma pow_apply_eq_self_of_apply_eq_self {f : perm α} {x : α} (hfx : f x = x) : ∀ n : ℕ, (f ^ n) x = x | 0 := rfl | (n+1) := by rw [pow_succ', mul_apply, hfx, pow_apply_eq_self_of_apply_eq_self] lemma gpow_apply_eq_self_of_apply_eq_self {f : perm α} {x : α} (hfx : f x = x) : ∀ n : ℤ, (f ^ n) x = x | (n : ℕ) := pow_apply_eq_self_of_apply_eq_self hfx n | -[1+ n] := by rw [gpow_neg_succ_of_nat, inv_eq_iff_eq, pow_apply_eq_self_of_apply_eq_self hfx] lemma pow_apply_eq_of_apply_apply_eq_self {f : perm α} {x : α} (hffx : f (f x) = x) : ∀ n : ℕ, (f ^ n) x = x ∨ (f ^ n) x = f x | 0 := or.inl rfl | (n+1) := (pow_apply_eq_of_apply_apply_eq_self n).elim (λ h, or.inr (by rw [pow_succ, mul_apply, h])) (λ h, or.inl (by rw [pow_succ, mul_apply, h, hffx])) lemma gpow_apply_eq_of_apply_apply_eq_self {f : perm α} {x : α} (hffx : f (f x) = x) : ∀ i : ℤ, (f ^ i) x = x ∨ (f ^ i) x = f x | (n : ℕ) := pow_apply_eq_of_apply_apply_eq_self hffx n | -[1+ n] := by { rw [gpow_neg_succ_of_nat, inv_eq_iff_eq, ← f.injective.eq_iff, ← mul_apply, ← pow_succ, eq_comm, inv_eq_iff_eq, ← mul_apply, ← pow_succ', @eq_comm _ x, or.comm], exact pow_apply_eq_of_apply_apply_eq_self hffx _ } variable [decidable_eq α] /-- The `finset` of nonfixed points of a permutation. -/ def support [fintype α] (f : perm α) : finset α := univ.filter (λ x, f x ≠ x) @[simp] lemma mem_support [fintype α] {f : perm α} {x : α} : x ∈ f.support ↔ f x ≠ x := by simp only [support, true_and, mem_filter, mem_univ] /-- `f.is_swap` indicates that the permutation `f` is a transposition of two elements. -/ def is_swap (f : perm α) : Prop := ∃ x y, x ≠ y ∧ f = swap x y lemma is_swap.of_subtype_is_swap {p : α → Prop} [decidable_pred p] {f : perm (subtype p)} (h : f.is_swap) : (of_subtype f).is_swap := let ⟨⟨x, hx⟩, ⟨y, hy⟩, hxy⟩ := h in ⟨x, y, by { simp only [ne.def] at hxy, exact hxy.1 }, equiv.ext $ λ z, begin rw [hxy.2, of_subtype], simp only [swap_apply_def, coe_fn_mk, swap_inv, subtype.mk_eq_mk, monoid_hom.coe_mk], split_ifs; rw subtype.coe_mk <|> cc, end⟩ lemma ne_and_ne_of_swap_mul_apply_ne_self {f : perm α} {x y : α} (hy : (swap x (f x) * f) y ≠ y) : f y ≠ y ∧ y ≠ x := begin simp only [swap_apply_def, mul_apply, f.injective.eq_iff] at *, by_cases h : f y = x, { split; intro; simp only [*, if_true, eq_self_iff_true, not_true, ne.def] at * }, { split_ifs at hy; cc } end lemma support_swap_mul_eq [fintype α] {f : perm α} {x : α} (hffx : f (f x) ≠ x) : (swap x (f x) * f).support = f.support.erase x := have hfx : f x ≠ x, from λ hfx, by simpa [hfx] using hffx, finset.ext $ λ y, ⟨λ hy, have hy' : (swap x (f x) * f) y ≠ y, from mem_support.1 hy, mem_erase.2 ⟨λ hyx, by simp [hyx, mul_apply, *] at *, mem_support.2 $ λ hfy, by simp only [mul_apply, swap_apply_def, hfy] at hy'; split_ifs at hy'; simp only [*, eq_self_iff_true, not_true, ne.def, apply_eq_iff_eq] at *⟩, λ hy, by simp only [mem_erase, mem_support, swap_apply_def, mul_apply] at *; intro; split_ifs at *; simp only [*, eq_self_iff_true, not_true, ne.def] at *⟩ lemma card_support_swap_mul [fintype α] {f : perm α} {x : α} (hx : f x ≠ x) : (swap x (f x) * f).support.card < f.support.card := finset.card_lt_card ⟨λ z hz, mem_support.2 (ne_and_ne_of_swap_mul_apply_ne_self (mem_support.1 hz)).1, λ h, absurd (h (mem_support.2 hx)) (mt mem_support.1 (by simp))⟩ /-- Given a list `l : list α` and a permutation `f : perm α` such that the nonfixed points of `f` are in `l`, recursively factors `f` as a product of transpositions. -/ def swap_factors_aux : Π (l : list α) (f : perm α), (∀ {x}, f x ≠ x → x ∈ l) → {l : list (perm α) // l.prod = f ∧ ∀ g ∈ l, is_swap g} | [] := λ f h, ⟨[], equiv.ext $ λ x, by { rw [list.prod_nil], exact (not_not.1 (mt h (list.not_mem_nil _))).symm }, by simp⟩ | (x :: l) := λ f h, if hfx : x = f x then swap_factors_aux l f (λ y hy, list.mem_of_ne_of_mem (λ h : y = x, by simpa [h, hfx.symm] using hy) (h hy)) else let m := swap_factors_aux l (swap x (f x) * f) (λ y hy, have f y ≠ y ∧ y ≠ x, from ne_and_ne_of_swap_mul_apply_ne_self hy, list.mem_of_ne_of_mem this.2 (h this.1)) in ⟨swap x (f x) :: m.1, by rw [list.prod_cons, m.2.1, ← mul_assoc, mul_def (swap x (f x)), swap_swap, ← one_def, one_mul], λ g hg, ((list.mem_cons_iff _ _ _).1 hg).elim (λ h, ⟨x, f x, hfx, h⟩) (m.2.2 _)⟩ /-- `swap_factors` represents a permutation as a product of a list of transpositions. The representation is non unique and depends on the linear order structure. For types without linear order `trunc_swap_factors` can be used. -/ def swap_factors [fintype α] [linear_order α] (f : perm α) : {l : list (perm α) // l.prod = f ∧ ∀ g ∈ l, is_swap g} := swap_factors_aux ((@univ α _).sort (≤)) f (λ _ _, (mem_sort _).2 (mem_univ _)) /-- This computably represents the fact that any permutation can be represented as the product of a list of transpositions. -/ def trunc_swap_factors [fintype α] (f : perm α) : trunc {l : list (perm α) // l.prod = f ∧ ∀ g ∈ l, is_swap g} := quotient.rec_on_subsingleton (@univ α _).1 (λ l h, trunc.mk (swap_factors_aux l f h)) (show ∀ x, f x ≠ x → x ∈ (@univ α _).1, from λ _ _, mem_univ _) /-- An induction principle for permutations. If `P` holds for the identity permutation, and is preserved under composition with a non-trivial swap, then `P` holds for all permutations. -/ @[elab_as_eliminator] lemma swap_induction_on [fintype α] {P : perm α → Prop} (f : perm α) : P 1 → (∀ f x y, x ≠ y → P f → P (swap x y * f)) → P f := begin cases (trunc_swap_factors f).out with l hl, induction l with g l ih generalizing f, { simp only [hl.left.symm, list.prod_nil, forall_true_iff] {contextual := tt} }, { assume h1 hmul_swap, rcases hl.2 g (by simp) with ⟨x, y, hxy⟩, rw [← hl.1, list.prod_cons, hxy.2], exact hmul_swap _ _ _ hxy.1 (ih _ ⟨rfl, λ v hv, hl.2 _ (list.mem_cons_of_mem _ hv)⟩ h1 hmul_swap) } end /-- Like `swap_induction_on`, but with the composition on the right of `f`. An induction principle for permutations. If `P` holds for the identity permutation, and is preserved under composition with a non-trivial swap, then `P` holds for all permutations. -/ @[elab_as_eliminator] lemma swap_induction_on' [fintype α] {P : perm α → Prop} (f : perm α) : P 1 → (∀ f x y, x ≠ y → P f → P (f * swap x y)) → P f := λ h1 IH, inv_inv f ▸ swap_induction_on f⁻¹ h1 (λ f, IH f⁻¹) lemma is_conj_swap {w x y z : α} (hwx : w ≠ x) (hyz : y ≠ z) : is_conj (swap w x) (swap y z) := have h : ∀ {y z : α}, y ≠ z → w ≠ z → (swap w y * swap x z) * swap w x * (swap w y * swap x z)⁻¹ = swap y z := λ y z hyz hwz, by rw [mul_inv_rev, swap_inv, swap_inv, mul_assoc (swap w y), mul_assoc (swap w y), ← mul_assoc _ (swap x z), swap_mul_swap_mul_swap hwx hwz, ← mul_assoc, swap_mul_swap_mul_swap hwz.symm hyz.symm], if hwz : w = z then have hwy : w ≠ y, by cc, ⟨swap w z * swap x y, by rw [swap_comm y z, h hyz.symm hwy]⟩ else ⟨swap w y * swap x z, h hyz hwz⟩ /-- set of all pairs (⟨a, b⟩ : Σ a : fin n, fin n) such that b < a -/ def fin_pairs_lt (n : ℕ) : finset (Σ a : fin n, fin n) := (univ : finset (fin n)).sigma (λ a, (range a).attach_fin (λ m hm, (mem_range.1 hm).trans a.2)) lemma mem_fin_pairs_lt {n : ℕ} {a : Σ a : fin n, fin n} : a ∈ fin_pairs_lt n ↔ a.2 < a.1 := by simp only [fin_pairs_lt, fin.lt_iff_coe_lt_coe, true_and, mem_attach_fin, mem_range, mem_univ, mem_sigma] /-- `sign_aux σ` is the sign of a permutation on `fin n`, defined as the parity of the number of pairs `(x₁, x₂)` such that `x₂ < x₁` but `σ x₁ ≤ σ x₂` -/ def sign_aux {n : ℕ} (a : perm (fin n)) : units ℤ := ∏ x in fin_pairs_lt n, if a x.1 ≤ a x.2 then -1 else 1 @[simp] lemma sign_aux_one (n : ℕ) : sign_aux (1 : perm (fin n)) = 1 := begin unfold sign_aux, conv { to_rhs, rw ← @finset.prod_const_one _ (units ℤ) (fin_pairs_lt n) }, exact finset.prod_congr rfl (λ a ha, if_neg (mem_fin_pairs_lt.1 ha).not_le) end /-- `sign_bij_aux f ⟨a, b⟩` returns the pair consisting of `f a` and `f b` in decreasing order. -/ def sign_bij_aux {n : ℕ} (f : perm (fin n)) (a : Σ a : fin n, fin n) : Σ a : fin n, fin n := if hxa : f a.2 < f a.1 then ⟨f a.1, f a.2⟩ else ⟨f a.2, f a.1⟩ lemma sign_bij_aux_inj {n : ℕ} {f : perm (fin n)} : ∀ a b : Σ a : fin n, fin n, a ∈ fin_pairs_lt n → b ∈ fin_pairs_lt n → sign_bij_aux f a = sign_bij_aux f b → a = b := λ ⟨a₁, a₂⟩ ⟨b₁, b₂⟩ ha hb h, begin unfold sign_bij_aux at h, rw mem_fin_pairs_lt at *, have : ¬b₁ < b₂ := hb.le.not_lt, split_ifs at h; simp only [*, (equiv.injective f).eq_iff, eq_self_iff_true, and_self, heq_iff_eq] at *, end lemma sign_bij_aux_surj {n : ℕ} {f : perm (fin n)} : ∀ a ∈ fin_pairs_lt n, ∃ b ∈ fin_pairs_lt n, a = sign_bij_aux f b := λ ⟨a₁, a₂⟩ ha, if hxa : f⁻¹ a₂ < f⁻¹ a₁ then ⟨⟨f⁻¹ a₁, f⁻¹ a₂⟩, mem_fin_pairs_lt.2 hxa, by { dsimp [sign_bij_aux], rw [apply_inv_self, apply_inv_self, if_pos (mem_fin_pairs_lt.1 ha)] }⟩ else ⟨⟨f⁻¹ a₂, f⁻¹ a₁⟩, mem_fin_pairs_lt.2 $ (le_of_not_gt hxa).lt_of_ne $ λ h, by simpa [mem_fin_pairs_lt, (f⁻¹).injective h, lt_irrefl] using ha, by { dsimp [sign_bij_aux], rw [apply_inv_self, apply_inv_self, if_neg (mem_fin_pairs_lt.1 ha).le.not_lt] }⟩ lemma sign_bij_aux_mem {n : ℕ} {f : perm (fin n)} : ∀ a : Σ a : fin n, fin n, a ∈ fin_pairs_lt n → sign_bij_aux f a ∈ fin_pairs_lt n := λ ⟨a₁, a₂⟩ ha, begin unfold sign_bij_aux, split_ifs with h, { exact mem_fin_pairs_lt.2 h }, { exact mem_fin_pairs_lt.2 ((le_of_not_gt h).lt_of_ne (λ h, (mem_fin_pairs_lt.1 ha).ne (f.injective h.symm))) } end @[simp] lemma sign_aux_inv {n : ℕ} (f : perm (fin n)) : sign_aux f⁻¹ = sign_aux f := prod_bij (λ a ha, sign_bij_aux f⁻¹ a) sign_bij_aux_mem (λ ⟨a, b⟩ hab, if h : f⁻¹ b < f⁻¹ a then by rw [sign_bij_aux, dif_pos h, if_neg h.not_le, apply_inv_self, apply_inv_self, if_neg (mem_fin_pairs_lt.1 hab).not_le] else by rw [sign_bij_aux, if_pos (le_of_not_gt h), dif_neg h, apply_inv_self, apply_inv_self, if_pos (mem_fin_pairs_lt.1 hab).le]) sign_bij_aux_inj sign_bij_aux_surj lemma sign_aux_mul {n : ℕ} (f g : perm (fin n)) : sign_aux (f * g) = sign_aux f * sign_aux g := begin rw ← sign_aux_inv g, unfold sign_aux, rw ← prod_mul_distrib, refine prod_bij (λ a ha, sign_bij_aux g a) sign_bij_aux_mem _ sign_bij_aux_inj sign_bij_aux_surj, rintros ⟨a, b⟩ hab, rw [sign_bij_aux, mul_apply, mul_apply], rw mem_fin_pairs_lt at hab, by_cases h : g b < g a, { rw dif_pos h, simp only [not_le_of_gt hab, mul_one, perm.inv_apply_self, if_false] }, { rw [dif_neg h, inv_apply_self, inv_apply_self, if_pos hab.le], by_cases h₁ : f (g b) ≤ f (g a), { have : f (g b) ≠ f (g a), { rw [ne.def, f.injective.eq_iff, g.injective.eq_iff], exact ne_of_lt hab }, rw [if_pos h₁, if_neg (h₁.lt_of_ne this).not_le], refl }, { rw [if_neg h₁, if_pos (lt_of_not_ge h₁).le], refl } } end private lemma sign_aux_swap_zero_one' (n : ℕ) : sign_aux (swap (0 : fin (n + 2)) 1) = -1 := show _ = ∏ x : Σ a : fin (n + 2), fin (n + 2) in {(⟨1, 0⟩ : Σ a : fin (n + 2), fin (n + 2))}, if (equiv.swap 0 1) x.1 ≤ swap 0 1 x.2 then (-1 : units ℤ) else 1, begin refine eq.symm (prod_subset (λ ⟨x₁, x₂⟩, by simp [mem_fin_pairs_lt, fin.one_pos] {contextual := tt}) (λ a ha₁ ha₂, _)), rcases a with ⟨a₁, a₂⟩, replace ha₁ : a₂ < a₁ := mem_fin_pairs_lt.1 ha₁, dsimp only, rcases a₁.zero_le.eq_or_lt with rfl|H, { exact absurd a₂.zero_le ha₁.not_le }, rcases a₂.zero_le.eq_or_lt with rfl|H', { simp only [and_true, eq_self_iff_true, heq_iff_eq, mem_singleton] at ha₂, have : 1 < a₁ := lt_of_le_of_ne (nat.succ_le_of_lt ha₁) (ne.symm ha₂), norm_num [swap_apply_of_ne_of_ne (ne_of_gt H) ha₂, this.not_le] }, { have le : 1 ≤ a₂ := nat.succ_le_of_lt H', have lt : 1 < a₁ := le.trans_lt ha₁, rcases le.eq_or_lt with rfl|lt', { norm_num [swap_apply_of_ne_of_ne (ne_of_gt H) (ne_of_gt lt), H.not_le] }, { norm_num [swap_apply_of_ne_of_ne (ne_of_gt H) (ne_of_gt lt), swap_apply_of_ne_of_ne (ne_of_gt H') (ne_of_gt lt'), ha₁.not_le] } } end private lemma sign_aux_swap_zero_one {n : ℕ} (hn : 2 ≤ n) : sign_aux (swap (⟨0, lt_of_lt_of_le dec_trivial hn⟩ : fin n) ⟨1, lt_of_lt_of_le dec_trivial hn⟩) = -1 := begin rcases n with _|_|n, { norm_num at hn }, { norm_num at hn }, { exact sign_aux_swap_zero_one' n } end lemma sign_aux_swap : ∀ {n : ℕ} {x y : fin n} (hxy : x ≠ y), sign_aux (swap x y) = -1 | 0 := dec_trivial | 1 := dec_trivial | (n+2) := λ x y hxy, have h2n : 2 ≤ n + 2 := dec_trivial, by { rw [← is_conj_iff_eq, ← sign_aux_swap_zero_one h2n], exact (monoid_hom.mk' sign_aux sign_aux_mul).map_is_conj (is_conj_swap hxy dec_trivial) } /-- When the list `l : list α` contains all nonfixed points of the permutation `f : perm α`, `sign_aux2 l f` recursively calculates the sign of `f`. -/ def sign_aux2 : list α → perm α → units ℤ | [] f := 1 | (x::l) f := if x = f x then sign_aux2 l f else -sign_aux2 l (swap x (f x) * f) lemma sign_aux_eq_sign_aux2 {n : ℕ} : ∀ (l : list α) (f : perm α) (e : α ≃ fin n) (h : ∀ x, f x ≠ x → x ∈ l), sign_aux ((e.symm.trans f).trans e) = sign_aux2 l f | [] f e h := have f = 1, from equiv.ext $ λ y, not_not.1 (mt (h y) (list.not_mem_nil _)), by rw [this, one_def, equiv.trans_refl, equiv.symm_trans, ← one_def, sign_aux_one, sign_aux2] | (x::l) f e h := begin rw sign_aux2, by_cases hfx : x = f x, { rw if_pos hfx, exact sign_aux_eq_sign_aux2 l f _ (λ y (hy : f y ≠ y), list.mem_of_ne_of_mem (λ h : y = x, by simpa [h, hfx.symm] using hy) (h y hy) ) }, { have hy : ∀ y : α, (swap x (f x) * f) y ≠ y → y ∈ l, from λ y hy, have f y ≠ y ∧ y ≠ x, from ne_and_ne_of_swap_mul_apply_ne_self hy, list.mem_of_ne_of_mem this.2 (h _ this.1), have : (e.symm.trans (swap x (f x) * f)).trans e = (swap (e x) (e (f x))) * (e.symm.trans f).trans e, by ext; simp [← equiv.symm_trans_swap_trans, mul_def], have hefx : e x ≠ e (f x), from mt e.injective.eq_iff.1 hfx, rw [if_neg hfx, ← sign_aux_eq_sign_aux2 _ _ e hy, this, sign_aux_mul, sign_aux_swap hefx], simp only [units.neg_neg, one_mul, units.neg_mul]} end /-- When the multiset `s : multiset α` contains all nonfixed points of the permutation `f : perm α`, `sign_aux2 f _` recursively calculates the sign of `f`. -/ def sign_aux3 [fintype α] (f : perm α) {s : multiset α} : (∀ x, x ∈ s) → units ℤ := quotient.hrec_on s (λ l h, sign_aux2 l f) (trunc.induction_on (equiv_fin α) (λ e l₁ l₂ h, function.hfunext (show (∀ x, x ∈ l₁) = ∀ x, x ∈ l₂, by simp only [h.mem_iff]) (λ h₁ h₂ _, by rw [← sign_aux_eq_sign_aux2 _ _ e (λ _ _, h₁ _), ← sign_aux_eq_sign_aux2 _ _ e (λ _ _, h₂ _)]))) lemma sign_aux3_mul_and_swap [fintype α] (f g : perm α) (s : multiset α) (hs : ∀ x, x ∈ s) : sign_aux3 (f * g) hs = sign_aux3 f hs * sign_aux3 g hs ∧ ∀ x y, x ≠ y → sign_aux3 (swap x y) hs = -1 := let ⟨l, hl⟩ := quotient.exists_rep s in let ⟨e, _⟩ := (equiv_fin α).exists_rep in begin clear _let_match _let_match, subst hl, show sign_aux2 l (f * g) = sign_aux2 l f * sign_aux2 l g ∧ ∀ x y, x ≠ y → sign_aux2 l (swap x y) = -1, have hfg : (e.symm.trans (f * g)).trans e = (e.symm.trans f).trans e * (e.symm.trans g).trans e, from equiv.ext (λ h, by simp [mul_apply]), split, { rw [← sign_aux_eq_sign_aux2 _ _ e (λ _ _, hs _), ← sign_aux_eq_sign_aux2 _ _ e (λ _ _, hs _), ← sign_aux_eq_sign_aux2 _ _ e (λ _ _, hs _), hfg, sign_aux_mul] }, { assume x y hxy, have hexy : e x ≠ e y, from mt e.injective.eq_iff.1 hxy, rw [← sign_aux_eq_sign_aux2 _ _ e (λ _ _, hs _), symm_trans_swap_trans, sign_aux_swap hexy] } end /-- `sign` of a permutation returns the signature or parity of a permutation, `1` for even permutations, `-1` for odd permutations. It is the unique surjective group homomorphism from `perm α` to the group with two elements.-/ def sign [fintype α] : perm α →* units ℤ := monoid_hom.mk' (λ f, sign_aux3 f mem_univ) (λ f g, (sign_aux3_mul_and_swap f g _ mem_univ).1) section sign variable [fintype α] @[simp] lemma sign_mul (f g : perm α) : sign (f * g) = sign f * sign g := monoid_hom.map_mul sign f g @[simp] lemma sign_trans (f g : perm α) : sign (f.trans g) = sign g * sign f := by rw [←mul_def, sign_mul] @[simp] lemma sign_one : (sign (1 : perm α)) = 1 := monoid_hom.map_one sign @[simp] lemma sign_refl : sign (equiv.refl α) = 1 := monoid_hom.map_one sign @[simp] lemma sign_inv (f : perm α) : sign f⁻¹ = sign f := by rw [monoid_hom.map_inv sign f, int.units_inv_eq_self] @[simp] lemma sign_symm (e : perm α) : sign e.symm = sign e := sign_inv e lemma sign_swap {x y : α} (h : x ≠ y) : sign (swap x y) = -1 := (sign_aux3_mul_and_swap 1 1 _ mem_univ).2 x y h @[simp] lemma sign_swap' {x y : α} : (swap x y).sign = if x = y then 1 else -1 := if H : x = y then by simp [H, swap_self] else by simp [sign_swap H, H] lemma is_swap.sign_eq {f : perm α} (h : f.is_swap) : sign f = -1 := let ⟨x, y, hxy⟩ := h in hxy.2.symm ▸ sign_swap hxy.1 lemma sign_aux3_symm_trans_trans [decidable_eq β] [fintype β] (f : perm α) (e : α ≃ β) {s : multiset α} {t : multiset β} (hs : ∀ x, x ∈ s) (ht : ∀ x, x ∈ t) : sign_aux3 ((e.symm.trans f).trans e) ht = sign_aux3 f hs := quotient.induction_on₂ t s (λ l₁ l₂ h₁ h₂, show sign_aux2 _ _ = sign_aux2 _ _, from let n := (equiv_fin β).out in by { rw [← sign_aux_eq_sign_aux2 _ _ n (λ _ _, h₁ _), ← sign_aux_eq_sign_aux2 _ _ (e.trans n) (λ _ _, h₂ _)], exact congr_arg sign_aux (equiv.ext (λ x, by simp only [equiv.coe_trans, apply_eq_iff_eq, symm_trans_apply])) }) ht hs @[simp] lemma sign_symm_trans_trans [decidable_eq β] [fintype β] (f : perm α) (e : α ≃ β) : sign ((e.symm.trans f).trans e) = sign f := sign_aux3_symm_trans_trans f e mem_univ mem_univ @[simp] lemma sign_trans_trans_symm [decidable_eq β] [fintype β] (f : perm β) (e : α ≃ β) : sign ((e.trans f).trans e.symm) = sign f := sign_symm_trans_trans f e.symm lemma sign_prod_list_swap {l : list (perm α)} (hl : ∀ g ∈ l, is_swap g) : sign l.prod = (-1) ^ l.length := have h₁ : l.map sign = list.repeat (-1) l.length := list.eq_repeat.2 ⟨by simp, λ u hu, let ⟨g, hg⟩ := list.mem_map.1 hu in hg.2 ▸ (hl _ hg.1).sign_eq⟩, by rw [← list.prod_repeat, ← h₁, list.prod_hom _ (@sign α _ _)] lemma sign_surjective (hα : 1 < fintype.card α) : function.surjective (sign : perm α → units ℤ) := λ a, (int.units_eq_one_or a).elim (λ h, ⟨1, by simp [h]⟩) (λ h, let ⟨x⟩ := fintype.card_pos_iff.1 (lt_trans zero_lt_one hα) in let ⟨y, hxy⟩ := fintype.exists_ne_of_one_lt_card hα x in ⟨swap y x, by rw [sign_swap hxy, h]⟩ ) lemma eq_sign_of_surjective_hom {s : perm α →* units ℤ} (hs : surjective s) : s = sign := have ∀ {f}, is_swap f → s f = -1 := λ f ⟨x, y, hxy, hxy'⟩, hxy'.symm ▸ by_contradiction (λ h, have ∀ f, is_swap f → s f = 1 := λ f ⟨a, b, hab, hab'⟩, by { rw [← is_conj_iff_eq, ← or.resolve_right (int.units_eq_one_or _) h, hab'], exact (monoid_hom.of s).map_is_conj (is_conj_swap hab hxy) }, let ⟨g, hg⟩ := hs (-1) in let ⟨l, hl⟩ := (trunc_swap_factors g).out in have ∀ a ∈ l.map s, a = (1 : units ℤ) := λ a ha, let ⟨g, hg⟩ := list.mem_map.1 ha in hg.2 ▸ this _ (hl.2 _ hg.1), have s l.prod = 1, by rw [← l.prod_hom s, list.eq_repeat'.2 this, list.prod_repeat, one_pow], by { rw [hl.1, hg] at this, exact absurd this dec_trivial }), monoid_hom.ext $ λ f, let ⟨l, hl₁, hl₂⟩ := (trunc_swap_factors f).out in have hsl : ∀ a ∈ l.map s, a = (-1 : units ℤ) := λ a ha, let ⟨g, hg⟩ := list.mem_map.1 ha in hg.2 ▸ this (hl₂ _ hg.1), by rw [← hl₁, ← l.prod_hom s, list.eq_repeat'.2 hsl, list.length_map, list.prod_repeat, sign_prod_list_swap hl₂] lemma sign_subtype_perm (f : perm α) {p : α → Prop} [decidable_pred p] (h₁ : ∀ x, p x ↔ p (f x)) (h₂ : ∀ x, f x ≠ x → p x) : sign (subtype_perm f h₁) = sign f := let l := (trunc_swap_factors (subtype_perm f h₁)).out in have hl' : ∀ g' ∈ l.1.map of_subtype, is_swap g' := λ g' hg', let ⟨g, hg⟩ := list.mem_map.1 hg' in hg.2 ▸ (l.2.2 _ hg.1).of_subtype_is_swap, have hl'₂ : (l.1.map of_subtype).prod = f, by rw [l.1.prod_hom of_subtype, l.2.1, of_subtype_subtype_perm _ h₂], by { conv { congr, rw ← l.2.1, skip, rw ← hl'₂ }, rw [sign_prod_list_swap l.2.2, sign_prod_list_swap hl', list.length_map] } @[simp] lemma sign_of_subtype {p : α → Prop} [decidable_pred p] (f : perm (subtype p)) : sign (of_subtype f) = sign f := have ∀ x, of_subtype f x ≠ x → p x, from λ x, not_imp_comm.1 (of_subtype_apply_of_not_mem f), by conv {to_rhs, rw [← subtype_perm_of_subtype f, sign_subtype_perm _ _ this]} lemma sign_eq_sign_of_equiv [decidable_eq β] [fintype β] (f : perm α) (g : perm β) (e : α ≃ β) (h : ∀ x, e (f x) = g (e x)) : sign f = sign g := have hg : g = (e.symm.trans f).trans e, from equiv.ext $ by simp [h], by rw [hg, sign_symm_trans_trans] lemma sign_bij [decidable_eq β] [fintype β] {f : perm α} {g : perm β} (i : Π x : α, f x ≠ x → β) (h : ∀ x hx hx', i (f x) hx' = g (i x hx)) (hi : ∀ x₁ x₂ hx₁ hx₂, i x₁ hx₁ = i x₂ hx₂ → x₁ = x₂) (hg : ∀ y, g y ≠ y → ∃ x hx, i x hx = y) : sign f = sign g := calc sign f = sign (@subtype_perm _ f (λ x, f x ≠ x) (by simp)) : (sign_subtype_perm _ _ (λ _, id)).symm ... = sign (@subtype_perm _ g (λ x, g x ≠ x) (by simp)) : sign_eq_sign_of_equiv _ _ (equiv.of_bijective (λ x : {x // f x ≠ x}, (⟨i x.1 x.2, have f (f x) ≠ f x, from mt (λ h, f.injective h) x.2, by { rw [← h _ x.2 this], exact mt (hi _ _ this x.2) x.2 }⟩ : {y // g y ≠ y})) ⟨λ ⟨x, hx⟩ ⟨y, hy⟩ h, subtype.eq (hi _ _ _ _ (subtype.mk.inj h)), λ ⟨y, hy⟩, let ⟨x, hfx, hx⟩ := hg y hy in ⟨⟨x, hfx⟩, subtype.eq hx⟩⟩) (λ ⟨x, _⟩, subtype.eq (h x _ _)) ... = sign g : sign_subtype_perm _ _ (λ _, id) @[simp] lemma support_swap {x y : α} (hxy : x ≠ y) : (swap x y).support = {x, y} := finset.ext $ λ a, by { simp only [swap_apply_def, mem_insert, ne.def, mem_support, mem_singleton], split_ifs; cc } lemma card_support_swap {x y : α} (hxy : x ≠ y) : (swap x y).support.card = 2 := show (swap x y).support.card = finset.card ⟨x ::ₘ y ::ₘ 0, by simp [hxy]⟩, from congr_arg card $ by simp [support_swap hxy, *, finset.ext_iff] /-- If we apply `prod_extend_right a (σ a)` for all `a : α` in turn, we get `prod_congr_right σ`. -/ lemma prod_prod_extend_right {α : Type*} [decidable_eq α] (σ : α → perm β) {l : list α} (hl : l.nodup) (mem_l : ∀ a, a ∈ l) : (l.map (λ a, prod_extend_right a (σ a))).prod = prod_congr_right σ := begin ext ⟨a, b⟩ : 1, -- We'll use induction on the list of elements, -- but we have to keep track of whether we already passed `a` in the list. suffices : (a ∈ l ∧ (l.map (λ a, prod_extend_right a (σ a))).prod (a, b) = (a, σ a b)) ∨ (a ∉ l ∧ (l.map (λ a, prod_extend_right a (σ a))).prod (a, b) = (a, b)), { obtain ⟨_, prod_eq⟩ := or.resolve_right this (not_and.mpr (λ h _, h (mem_l a))), rw [prod_eq, prod_congr_right_apply] }, clear mem_l, induction l with a' l ih, { refine or.inr ⟨list.not_mem_nil _, _⟩, rw [list.map_nil, list.prod_nil, one_apply] }, rw [list.map_cons, list.prod_cons, mul_apply], rcases ih (list.nodup_cons.mp hl).2 with ⟨mem_l, prod_eq⟩ | ⟨not_mem_l, prod_eq⟩; rw prod_eq, { refine or.inl ⟨list.mem_cons_of_mem _ mem_l, _⟩, rw prod_extend_right_apply_ne _ (λ (h : a = a'), (list.nodup_cons.mp hl).1 (h ▸ mem_l)) }, by_cases ha' : a = a', { rw ← ha' at *, refine or.inl ⟨l.mem_cons_self a, _⟩, rw prod_extend_right_apply_eq }, { refine or.inr ⟨λ h, not_or ha' not_mem_l ((list.mem_cons_iff _ _ _).mp h), _⟩, rw prod_extend_right_apply_ne _ ha' }, end section congr variables [decidable_eq β] [fintype β] @[simp] lemma sign_prod_extend_right (a : α) (σ : perm β) : (prod_extend_right a σ).sign = σ.sign := sign_bij (λ (ab : α × β) _, ab.snd) (λ ⟨a', b⟩ hab hab', by simp [eq_of_prod_extend_right_ne hab]) (λ ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ hab₁ hab₂ h, by simpa [eq_of_prod_extend_right_ne hab₁, eq_of_prod_extend_right_ne hab₂] using h) (λ y hy, ⟨(a, y), by simpa, by simp⟩) lemma sign_prod_congr_right (σ : α → perm β) : sign (prod_congr_right σ) = ∏ k, (σ k).sign := begin obtain ⟨l, hl, mem_l⟩ := fintype.exists_univ_list α, have l_to_finset : l.to_finset = finset.univ, { apply eq_top_iff.mpr, intros b _, exact list.mem_to_finset.mpr (mem_l b) }, rw [← prod_prod_extend_right σ hl mem_l, sign.map_list_prod, list.map_map, ← l_to_finset, list.prod_to_finset _ hl], simp_rw ← λ a, sign_prod_extend_right a (σ a) end lemma sign_prod_congr_left (σ : α → perm β) : sign (prod_congr_left σ) = ∏ k, (σ k).sign := begin refine (sign_eq_sign_of_equiv _ _ (prod_comm β α) _).trans (sign_prod_congr_right σ), rintro ⟨b, α⟩, refl end @[simp] lemma sign_perm_congr (e : α ≃ β) (p : perm α) : (e.perm_congr p).sign = p.sign := sign_eq_sign_of_equiv _ _ e.symm (by simp) @[simp] lemma sign_sum_congr (σa : perm α) (σb : perm β) : (sum_congr σa σb).sign = σa.sign * σb.sign := begin suffices : (sum_congr σa (1 : perm β)).sign = σa.sign ∧ (sum_congr (1 : perm α) σb).sign = σb.sign, { rw [←this.1, ←this.2, ←sign_mul, sum_congr_mul, one_mul, mul_one], }, split, { apply σa.swap_induction_on _ (λ σa' a₁ a₂ ha ih, _), { simp }, { rw [←one_mul (1 : perm β), ←sum_congr_mul, sign_mul, sign_mul, ih, sum_congr_swap_one, sign_swap ha, sign_swap (sum.injective_inl.ne_iff.mpr ha)], }, }, { apply σb.swap_induction_on _ (λ σb' b₁ b₂ hb ih, _), { simp }, { rw [←one_mul (1 : perm α), ←sum_congr_mul, sign_mul, sign_mul, ih, sum_congr_one_swap, sign_swap hb, sign_swap (sum.injective_inr.ne_iff.mpr hb)], }, } end @[simp] lemma sign_subtype_congr {p : α → Prop} [decidable_pred p] (ep : perm {a // p a}) (en : perm {a // ¬ p a}) : (ep.subtype_congr en).sign = ep.sign * en.sign := by simp [subtype_congr] end congr end sign end equiv.perm
2bc9d41efdde35db96186f122dd5ceed8b2d673b
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/ring_theory/nullstellensatz.lean
b0dfe0bff6d209d545c07877fd56fd3edb6078dc
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
6,646
lean
/- Copyright (c) 2021 Devon Tuma. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Devon Tuma -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.ring_theory.jacobson import Mathlib.field_theory.algebraic_closure import Mathlib.field_theory.mv_polynomial import Mathlib.algebraic_geometry.prime_spectrum import Mathlib.PostPort universes u_1 u_2 namespace Mathlib /-! # Nullstellensatz This file establishes a version of Hilbert's classical Nullstellensatz for `mv_polynomial`s. The main statement of the theorem is `vanishing_ideal_zero_locus_eq_radical`. The statement is in terms of new definitions `vanishing_ideal` and `zero_locus`. Mathlib already has versions of these in terms of the prime spectrum of a ring, but those are not well-suited for expressing this result. Suggestions for better ways to state this theorem or organize things are welcome. The machinery around `vanishing_ideal` and `zero_locus` is also minimal, I only added lemmas directly needed in this proof, since I'm not sure if they are the right approach. -/ namespace mv_polynomial /-- Set of points that are zeroes of all polynomials in an ideal -/ def zero_locus {k : Type u_1} [field k] {σ : Type u_2} (I : ideal (mv_polynomial σ k)) : set (σ → k) := set_of fun (x : σ → k) => ∀ (p : mv_polynomial σ k), p ∈ I → coe_fn (eval x) p = 0 @[simp] theorem mem_zero_locus_iff {k : Type u_1} [field k] {σ : Type u_2} {I : ideal (mv_polynomial σ k)} {x : σ → k} : x ∈ zero_locus I ↔ ∀ (p : mv_polynomial σ k), p ∈ I → coe_fn (eval x) p = 0 := iff.rfl theorem zero_locus_anti_mono {k : Type u_1} [field k] {σ : Type u_2} {I : ideal (mv_polynomial σ k)} {J : ideal (mv_polynomial σ k)} (h : I ≤ J) : zero_locus J ≤ zero_locus I := fun (x : σ → k) (hx : x ∈ zero_locus J) (p : mv_polynomial σ k) (hp : p ∈ I) => hx p (h hp) theorem zero_locus_bot {k : Type u_1} [field k] {σ : Type u_2} : zero_locus ⊥ = ⊤ := iff.mpr eq_top_iff fun (x : σ → k) (hx : x ∈ ⊤) (p : mv_polynomial σ k) (hp : p ∈ ⊥) => trans (congr_arg (⇑(eval x)) (iff.mp ideal.mem_bot hp)) (ring_hom.map_zero (eval x)) theorem zero_locus_top {k : Type u_1} [field k] {σ : Type u_2} : zero_locus ⊤ = ⊥ := iff.mpr eq_bot_iff fun (x : σ → k) (hx : x ∈ zero_locus ⊤) => one_ne_zero (ring_hom.map_one (eval x) ▸ hx 1 submodule.mem_top) /-- Ideal of polynomials with common zeroes at all elements of a set -/ def vanishing_ideal {k : Type u_1} [field k] {σ : Type u_2} (V : set (σ → k)) : ideal (mv_polynomial σ k) := submodule.mk (set_of fun (p : mv_polynomial σ k) => ∀ (x : σ → k), x ∈ V → coe_fn (eval x) p = 0) sorry sorry sorry @[simp] theorem mem_vanishing_ideal_iff {k : Type u_1} [field k] {σ : Type u_2} {V : set (σ → k)} {p : mv_polynomial σ k} : p ∈ vanishing_ideal V ↔ ∀ (x : σ → k), x ∈ V → coe_fn (eval x) p = 0 := iff.rfl theorem vanishing_ideal_anti_mono {k : Type u_1} [field k] {σ : Type u_2} {A : set (σ → k)} {B : set (σ → k)} (h : A ≤ B) : vanishing_ideal B ≤ vanishing_ideal A := fun (p : mv_polynomial σ k) (hp : p ∈ vanishing_ideal B) (x : σ → k) (hx : x ∈ A) => hp x (h hx) theorem vanishing_ideal_empty {k : Type u_1} [field k] {σ : Type u_2} : vanishing_ideal ∅ = ⊤ := le_antisymm le_top fun (p : mv_polynomial σ k) (hp : p ∈ ⊤) (x : σ → k) (hx : x ∈ ∅) => absurd hx (set.not_mem_empty x) theorem le_vanishing_ideal_zero_locus {k : Type u_1} [field k] {σ : Type u_2} (I : ideal (mv_polynomial σ k)) : I ≤ vanishing_ideal (zero_locus I) := fun (p : mv_polynomial σ k) (hp : p ∈ I) (x : σ → k) (hx : x ∈ zero_locus I) => hx p hp theorem zero_locus_vanishing_ideal_le {k : Type u_1} [field k] {σ : Type u_2} (V : set (σ → k)) : V ≤ zero_locus (vanishing_ideal V) := fun (V_1 : σ → k) (hV : V_1 ∈ V) (p : mv_polynomial σ k) (hp : p ∈ vanishing_ideal V) => hp V_1 hV theorem zero_locus_vanishing_ideal_galois_connection {k : Type u_1} [field k] {σ : Type u_2} : galois_connection zero_locus vanishing_ideal := fun (I : ideal (mv_polynomial σ k)) (V : order_dual (set (σ → k))) => { mp := fun (h : zero_locus I ≤ V) => le_trans (le_vanishing_ideal_zero_locus I) (vanishing_ideal_anti_mono h), mpr := fun (h : I ≤ vanishing_ideal V) => le_trans (zero_locus_anti_mono h) (zero_locus_vanishing_ideal_le V) } theorem mem_vanishing_ideal_singleton_iff {k : Type u_1} [field k] {σ : Type u_2} (x : σ → k) (p : mv_polynomial σ k) : p ∈ vanishing_ideal (singleton x) ↔ coe_fn (eval x) p = 0 := { mp := fun (h : p ∈ vanishing_ideal (singleton x)) => h x rfl, mpr := fun (hpx : coe_fn (eval x) p = 0) (y : σ → k) (hy : y ∈ singleton x) => Eq.symm hy ▸ hpx } protected instance vanishing_ideal_singleton_is_maximal {k : Type u_1} [field k] {σ : Type u_2} {x : σ → k} : ideal.is_maximal (vanishing_ideal (singleton x)) := sorry theorem radical_le_vanishing_ideal_zero_locus {k : Type u_1} [field k] {σ : Type u_2} (I : ideal (mv_polynomial σ k)) : ideal.radical I ≤ vanishing_ideal (zero_locus I) := sorry /-- The point in the prime spectrum assosiated to a given point -/ def point_to_point {k : Type u_1} [field k] {σ : Type u_2} (x : σ → k) : prime_spectrum (mv_polynomial σ k) := { val := vanishing_ideal (singleton x), property := sorry } @[simp] theorem vanishing_ideal_point_to_point {k : Type u_1} [field k] {σ : Type u_2} (V : set (σ → k)) : prime_spectrum.vanishing_ideal (point_to_point '' V) = vanishing_ideal V := sorry theorem point_to_point_zero_locus_le {k : Type u_1} [field k] {σ : Type u_2} (I : ideal (mv_polynomial σ k)) : point_to_point '' zero_locus I ≤ prime_spectrum.zero_locus ↑I := sorry theorem is_maximal_iff_eq_vanishing_ideal_singleton {k : Type u_1} [field k] {σ : Type u_2} [is_alg_closed k] [fintype σ] (I : ideal (mv_polynomial σ k)) : ideal.is_maximal I ↔ ∃ (x : σ → k), I = vanishing_ideal (singleton x) := sorry /-- Main statement of the Nullstellensatz -/ @[simp] theorem vanishing_ideal_zero_locus_eq_radical {k : Type u_1} [field k] {σ : Type u_2} [is_alg_closed k] [fintype σ] (I : ideal (mv_polynomial σ k)) : vanishing_ideal (zero_locus I) = ideal.radical I := sorry @[simp] theorem is_prime.vanishing_ideal_zero_locus {k : Type u_1} [field k] {σ : Type u_2} [is_alg_closed k] [fintype σ] (P : ideal (mv_polynomial σ k)) [h : ideal.is_prime P] : vanishing_ideal (zero_locus P) = P := trans (vanishing_ideal_zero_locus_eq_radical P) (ideal.is_prime.radical h)
2016ce6cf73ca368b52694b3ee6c6df643353a07
c777c32c8e484e195053731103c5e52af26a25d1
/src/combinatorics/set_family/compression/uv.lean
cec8c3800f4bb337bb3e58d6971be2bb339b58e6
[ "Apache-2.0" ]
permissive
kbuzzard/mathlib
2ff9e85dfe2a46f4b291927f983afec17e946eb8
58537299e922f9c77df76cb613910914a479c1f7
refs/heads/master
1,685,313,702,744
1,683,974,212,000
1,683,974,212,000
128,185,277
1
0
null
1,522,920,600,000
1,522,920,600,000
null
UTF-8
Lean
false
false
9,011
lean
/- Copyright (c) 2021 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies, Bhavik Mehta -/ import data.finset.card /-! # UV-compressions > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines UV-compression. It is an operation on a set family that reduces its shadow. UV-compressing `a : α` along `u v : α` means replacing `a` by `(a ⊔ u) \ v` if `a` and `u` are disjoint and `v ≤ a`. In some sense, it's moving `a` from `v` to `u`. UV-compressions are immensely useful to prove the Kruskal-Katona theorem. The idea is that compressing a set family might decrease the size of its shadow, so iterated compressions hopefully minimise the shadow. ## Main declarations * `uv.compress`: `compress u v a` is `a` compressed along `u` and `v`. * `uv.compression`: `compression u v s` is the compression of the set family `s` along `u` and `v`. It is the compressions of the elements of `s` whose compression is not already in `s` along with the element whose compression is already in `s`. This way of splitting into what moves and what does not ensures the compression doesn't squash the set family, which is proved by `uv.card_compress`. ## Notation `𝓒` (typed with `\MCC`) is notation for `uv.compression` in locale `finset_family`. ## Notes Even though our emphasis is on `finset α`, we define UV-compressions more generally in a generalized boolean algebra, so that one can use it for `set α`. ## TODO Prove that compressing reduces the size of shadow. This result and some more already exist on the branch `combinatorics`. ## References * https://github.com/b-mehta/maths-notes/blob/master/iii/mich/combinatorics.pdf ## Tags compression, UV-compression, shadow -/ open finset variable {α : Type*} /-- UV-compression is injective on the elements it moves. See `uv.compress`. -/ lemma sup_sdiff_inj_on [generalized_boolean_algebra α] (u v : α) : {x | disjoint u x ∧ v ≤ x}.inj_on (λ x, (x ⊔ u) \ v) := begin rintro a ha b hb hab, have h : (a ⊔ u) \ v \ u ⊔ v = (b ⊔ u) \ v \ u ⊔ v, { dsimp at hab, rw hab }, rwa [sdiff_sdiff_comm, ha.1.symm.sup_sdiff_cancel_right, sdiff_sdiff_comm, hb.1.symm.sup_sdiff_cancel_right, sdiff_sup_cancel ha.2, sdiff_sup_cancel hb.2] at h, end -- The namespace is here to distinguish from other compressions. namespace uv /-! ### UV-compression in generalized boolean algebras -/ section generalized_boolean_algebra variables [generalized_boolean_algebra α] [decidable_rel (@disjoint α _ _)] [decidable_rel ((≤) : α → α → Prop)] {s : finset α} {u v a b : α} local attribute [instance] decidable_eq_of_decidable_le /-- To UV-compress `a`, if it doesn't touch `U` and does contain `V`, we remove `V` and put `U` in. We'll only really use this when `|U| = |V|` and `U ∩ V = ∅`. -/ def compress (u v a : α) : α := if disjoint u a ∧ v ≤ a then (a ⊔ u) \ v else a /-- To UV-compress a set family, we compress each of its elements, except that we don't want to reduce the cardinality, so we keep all elements whose compression is already present. -/ def compression (u v : α) (s : finset α) := s.filter (λ a, compress u v a ∈ s) ∪ (s.image $ compress u v).filter (λ a, a ∉ s) localized "notation (name := uv.compression) `𝓒 ` := uv.compression" in finset_family /-- `is_compressed u v s` expresses that `s` is UV-compressed. -/ def is_compressed (u v : α) (s : finset α) := 𝓒 u v s = s lemma compress_of_disjoint_of_le (hua : disjoint u a) (hva : v ≤ a) : compress u v a = (a ⊔ u) \ v := if_pos ⟨hua, hva⟩ /-- `a` is in the UV-compressed family iff it's in the original and its compression is in the original, or it's not in the original but it's the compression of something in the original. -/ lemma mem_compression : a ∈ 𝓒 u v s ↔ a ∈ s ∧ compress u v a ∈ s ∨ a ∉ s ∧ ∃ b ∈ s, compress u v b = a := by simp_rw [compression, mem_union, mem_filter, mem_image, and_comm (a ∉ s)] @[simp] lemma compress_self (u a : α) : compress u u a = a := begin unfold compress, split_ifs, { exact h.1.symm.sup_sdiff_cancel_right }, { refl } end @[simp] lemma compression_self (u : α) (s : finset α) : 𝓒 u u s = s := begin unfold compression, convert union_empty s, { ext a, rw [mem_filter, compress_self, and_self] }, { refine eq_empty_of_forall_not_mem (λ a ha, _), simp_rw [mem_filter, mem_image, compress_self] at ha, obtain ⟨⟨b, hb, rfl⟩, hb'⟩ := ha, exact hb' hb } end /-- Any family is compressed along two identical elements. -/ lemma is_compressed_self (u : α) (s : finset α) : is_compressed u u s := compression_self u s lemma compress_disjoint (u v : α) : disjoint (s.filter (λ a, compress u v a ∈ s)) ((s.image $ compress u v).filter (λ a, a ∉ s)) := disjoint_left.2 $ λ a ha₁ ha₂, (mem_filter.1 ha₂).2 (mem_filter.1 ha₁).1 /-- Compressing an element is idempotent. -/ @[simp] lemma compress_idem (u v a : α) : compress u v (compress u v a) = compress u v a := begin unfold compress, split_ifs with h h', { rw [le_sdiff_iff.1 h'.2, sdiff_bot, sdiff_bot, sup_assoc, sup_idem] }, { refl }, { refl } end lemma compress_mem_compression (ha : a ∈ s) : compress u v a ∈ 𝓒 u v s := begin rw mem_compression, by_cases compress u v a ∈ s, { rw compress_idem, exact or.inl ⟨h, h⟩ }, { exact or.inr ⟨h, a, ha, rfl⟩ } end -- This is a special case of `compress_mem_compression` once we have `compression_idem`. lemma compress_mem_compression_of_mem_compression (ha : a ∈ 𝓒 u v s) : compress u v a ∈ 𝓒 u v s := begin rw mem_compression at ⊢ ha, simp only [compress_idem, exists_prop], obtain ⟨_, ha⟩ | ⟨_, b, hb, rfl⟩ := ha, { exact or.inl ⟨ha, ha⟩ }, { exact or.inr ⟨by rwa compress_idem, b, hb, (compress_idem _ _ _).symm⟩ } end /-- Compressing a family is idempotent. -/ @[simp] lemma compression_idem (u v : α) (s : finset α) : 𝓒 u v (𝓒 u v s) = 𝓒 u v s := begin have h : filter (λ a, compress u v a ∉ 𝓒 u v s) (𝓒 u v s) = ∅ := filter_false_of_mem (λ a ha h, h $ compress_mem_compression_of_mem_compression ha), rw [compression, image_filter, h, image_empty, ←h], exact filter_union_filter_neg_eq _ (compression u v s), end /-- Compressing a family doesn't change its size. -/ lemma card_compression (u v : α) (s : finset α) : (𝓒 u v s).card = s.card := begin rw [compression, card_disjoint_union (compress_disjoint _ _), image_filter, card_image_of_inj_on, ←card_disjoint_union, filter_union_filter_neg_eq], { rw disjoint_iff_inter_eq_empty, exact filter_inter_filter_neg_eq _ _ _ }, intros a ha b hb hab, dsimp at hab, rw [mem_coe, mem_filter, function.comp_app] at ha hb, rw compress at ha hab, split_ifs at ha hab with has, { rw compress at hb hab, split_ifs at hb hab with hbs, { exact sup_sdiff_inj_on u v has hbs hab }, { exact (hb.2 hb.1).elim } }, { exact (ha.2 ha.1).elim } end /-- If `a` is in the family compression and can be compressed, then its compression is in the original family. -/ lemma sup_sdiff_mem_of_mem_compression (ha : a ∈ 𝓒 u v s) (hva : v ≤ a) (hua : disjoint u a) : (a ⊔ u) \ v ∈ s := begin rw [mem_compression, compress_of_disjoint_of_le hua hva] at ha, obtain ⟨_, ha⟩ | ⟨_, b, hb, rfl⟩ := ha, { exact ha }, have hu : u = ⊥, { suffices : disjoint u (u \ v), { rwa [(hua.mono_right hva).sdiff_eq_left, disjoint_self] at this }, refine hua.mono_right _, rw [←compress_idem, compress_of_disjoint_of_le hua hva], exact sdiff_le_sdiff_right le_sup_right }, have hv : v = ⊥, { rw ←disjoint_self, apply disjoint.mono_right hva, rw [←compress_idem, compress_of_disjoint_of_le hua hva], exact disjoint_sdiff_self_right }, rwa [hu, hv, compress_self, sup_bot_eq, sdiff_bot], end /-- If `a` is in the `u, v`-compression but `v ≤ a`, then `a` must have been in the original family. -/ lemma mem_of_mem_compression (ha : a ∈ 𝓒 u v s) (hva : v ≤ a) (hvu : v = ⊥ → u = ⊥) : a ∈ s := begin rw mem_compression at ha, obtain ha | ⟨_, b, hb, h⟩ := ha, { exact ha.1 }, unfold compress at h, split_ifs at h, { rw [←h, le_sdiff_iff] at hva, rw [hvu hva, hva, sup_bot_eq, sdiff_bot] at h, rwa ←h }, { rwa ←h } end end generalized_boolean_algebra /-! ### UV-compression on finsets -/ open_locale finset_family variables [decidable_eq α] {𝒜 : finset (finset α)} {U V A : finset α} /-- Compressing a finset doesn't change its size. -/ lemma card_compress (hUV : U.card = V.card) (A : finset α) : (compress U V A).card = A.card := begin unfold compress, split_ifs, { rw [card_sdiff (h.2.trans le_sup_left), sup_eq_union, card_disjoint_union h.1.symm, hUV, add_tsub_cancel_right] }, { refl } end end uv